Update change log
[platform/upstream/gcc48.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992-2013 Free Software Foundation, Inc.
3    Contributed by Gary Funck (gary@intrepid.com).
4    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
5    Extensively modified by Jason Merrill (jason@cygnus.com).
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 /* TODO: Emit .debug_line header even when there are no functions, since
24            the file numbers are used by .debug_info.  Alternately, leave
25            out locations for types and decls.
26          Avoid talking about ctors and op= for PODs.
27          Factor out common prologue sequences into multiple CIEs.  */
28
29 /* The first part of this file deals with the DWARF 2 frame unwind
30    information, which is also used by the GCC efficient exception handling
31    mechanism.  The second part, controlled only by an #ifdef
32    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
33    information.  */
34
35 /* DWARF2 Abbreviation Glossary:
36
37    CFA = Canonical Frame Address
38            a fixed address on the stack which identifies a call frame.
39            We define it to be the value of SP just before the call insn.
40            The CFA register and offset, which may change during the course
41            of the function, are used to calculate its value at runtime.
42
43    CFI = Call Frame Instruction
44            an instruction for the DWARF2 abstract machine
45
46    CIE = Common Information Entry
47            information describing information common to one or more FDEs
48
49    DIE = Debugging Information Entry
50
51    FDE = Frame Description Entry
52            information describing the stack call frame, in particular,
53            how to restore registers
54
55    DW_CFA_... = DWARF2 CFA call frame instruction
56    DW_TAG_... = DWARF2 DIE tag */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "tree.h"
63 #include "version.h"
64 #include "flags.h"
65 #include "rtl.h"
66 #include "hard-reg-set.h"
67 #include "regs.h"
68 #include "insn-config.h"
69 #include "reload.h"
70 #include "function.h"
71 #include "output.h"
72 #include "expr.h"
73 #include "except.h"
74 #include "dwarf2.h"
75 #include "dwarf2out.h"
76 #include "dwarf2asm.h"
77 #include "toplev.h"
78 #include "ggc.h"
79 #include "md5.h"
80 #include "tm_p.h"
81 #include "diagnostic.h"
82 #include "tree-pretty-print.h"
83 #include "debug.h"
84 #include "target.h"
85 #include "common/common-target.h"
86 #include "langhooks.h"
87 #include "hashtab.h"
88 #include "cgraph.h"
89 #include "input.h"
90 #include "gimple.h"
91 #include "ira.h"
92 #include "lra.h"
93 #include "dumpfile.h"
94 #include "opts.h"
95
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97 static rtx last_var_location_insn;
98 static rtx cached_next_real_insn;
99
100 #ifdef VMS_DEBUGGING_INFO
101 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
102
103 /* Define this macro to be a nonzero value if the directory specifications
104     which are output in the debug info should end with a separator.  */
105 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
106 /* Define this macro to evaluate to a nonzero value if GCC should refrain
107    from generating indirect strings in DWARF2 debug information, for instance
108    if your target is stuck with an old version of GDB that is unable to
109    process them properly or uses VMS Debug.  */
110 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
111 #else
112 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
113 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
114 #endif
115
116 /* ??? Poison these here until it can be done generically.  They've been
117    totally replaced in this file; make sure it stays that way.  */
118 #undef DWARF2_UNWIND_INFO
119 #undef DWARF2_FRAME_INFO
120 #if (GCC_VERSION >= 3000)
121  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
122 #endif
123
124 /* The size of the target's pointer type.  */
125 #ifndef PTR_SIZE
126 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
127 #endif
128
129 /* Array of RTXes referenced by the debugging information, which therefore
130    must be kept around forever.  */
131 static GTY(()) vec<rtx, va_gc> *used_rtx_array;
132
133 /* A pointer to the base of a list of incomplete types which might be
134    completed at some later time.  incomplete_types_list needs to be a
135    vec<tree, va_gc> *because we want to tell the garbage collector about
136    it.  */
137 static GTY(()) vec<tree, va_gc> *incomplete_types;
138
139 /* A pointer to the base of a table of references to declaration
140    scopes.  This table is a display which tracks the nesting
141    of declaration scopes at the current scope and containing
142    scopes.  This table is used to find the proper place to
143    define type declaration DIE's.  */
144 static GTY(()) vec<tree, va_gc> *decl_scope_table;
145
146 /* Pointers to various DWARF2 sections.  */
147 static GTY(()) section *debug_info_section;
148 static GTY(()) section *debug_skeleton_info_section;
149 static GTY(()) section *debug_abbrev_section;
150 static GTY(()) section *debug_skeleton_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_addr_section;
153 static GTY(()) section *debug_macinfo_section;
154 static GTY(()) section *debug_line_section;
155 static GTY(()) section *debug_skeleton_line_section;
156 static GTY(()) section *debug_loc_section;
157 static GTY(()) section *debug_pubnames_section;
158 static GTY(()) section *debug_pubtypes_section;
159 static GTY(()) section *debug_str_section;
160 static GTY(()) section *debug_str_offsets_section;
161 static GTY(()) section *debug_ranges_section;
162 static GTY(()) section *debug_frame_section;
163
164 /* Maximum size (in bytes) of an artificially generated label.  */
165 #define MAX_ARTIFICIAL_LABEL_BYTES      30
166
167 /* According to the (draft) DWARF 3 specification, the initial length
168    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
169    bytes are 0xffffffff, followed by the length stored in the next 8
170    bytes.
171
172    However, the SGI/MIPS ABI uses an initial length which is equal to
173    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
174
175 #ifndef DWARF_INITIAL_LENGTH_SIZE
176 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
177 #endif
178
179 /* Round SIZE up to the nearest BOUNDARY.  */
180 #define DWARF_ROUND(SIZE,BOUNDARY) \
181   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
182
183 /* CIE identifier.  */
184 #if HOST_BITS_PER_WIDE_INT >= 64
185 #define DWARF_CIE_ID \
186   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
187 #else
188 #define DWARF_CIE_ID DW_CIE_ID
189 #endif
190
191
192 /* A vector for a table that contains frame description
193    information for each routine.  */
194 #define NOT_INDEXED (-1U)
195 #define NO_INDEX_ASSIGNED (-2U)
196
197 static GTY(()) vec<dw_fde_ref, va_gc> *fde_vec;
198
199 struct GTY(()) indirect_string_node {
200   const char *str;
201   unsigned int refcount;
202   enum dwarf_form form;
203   char *label;
204   unsigned int index;
205 };
206
207 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
208
209 static GTY(()) int dw2_string_counter;
210
211 /* True if the compilation unit places functions in more than one section.  */
212 static GTY(()) bool have_multiple_function_sections = false;
213
214 /* Whether the default text and cold text sections have been used at all.  */
215
216 static GTY(()) bool text_section_used = false;
217 static GTY(()) bool cold_text_section_used = false;
218
219 /* The default cold text section.  */
220 static GTY(()) section *cold_text_section;
221
222 /* Forward declarations for functions defined in this file.  */
223
224 static char *stripattributes (const char *);
225 static void output_call_frame_info (int);
226 static void dwarf2out_note_section_used (void);
227
228 /* Personality decl of current unit.  Used only when assembler does not support
229    personality CFI.  */
230 static GTY(()) rtx current_unit_personality;
231
232 /* Data and reference forms for relocatable data.  */
233 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
234 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
235
236 #ifndef DEBUG_FRAME_SECTION
237 #define DEBUG_FRAME_SECTION     ".debug_frame"
238 #endif
239
240 #ifndef FUNC_BEGIN_LABEL
241 #define FUNC_BEGIN_LABEL        "LFB"
242 #endif
243
244 #ifndef FUNC_END_LABEL
245 #define FUNC_END_LABEL          "LFE"
246 #endif
247
248 #ifndef PROLOGUE_END_LABEL
249 #define PROLOGUE_END_LABEL      "LPE"
250 #endif
251
252 #ifndef EPILOGUE_BEGIN_LABEL
253 #define EPILOGUE_BEGIN_LABEL    "LEB"
254 #endif
255
256 #ifndef FRAME_BEGIN_LABEL
257 #define FRAME_BEGIN_LABEL       "Lframe"
258 #endif
259 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
260 #define CIE_END_LABEL           "LECIE"
261 #define FDE_LABEL               "LSFDE"
262 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
263 #define FDE_END_LABEL           "LEFDE"
264 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
265 #define LINE_NUMBER_END_LABEL   "LELT"
266 #define LN_PROLOG_AS_LABEL      "LASLTP"
267 #define LN_PROLOG_END_LABEL     "LELTP"
268 #define DIE_LABEL_PREFIX        "DW"
269 \f
270 /* Match the base name of a file to the base name of a compilation unit. */
271
272 static int
273 matches_main_base (const char *path)
274 {
275   /* Cache the last query. */
276   static const char *last_path = NULL;
277   static int last_match = 0;
278   if (path != last_path)
279     {
280       const char *base;
281       int length = base_of_path (path, &base);
282       last_path = path;
283       last_match = (length == main_input_baselength
284                     && memcmp (base, main_input_basename, length) == 0);
285     }
286   return last_match;
287 }
288
289 #ifdef DEBUG_DEBUG_STRUCT
290
291 static int
292 dump_struct_debug (tree type, enum debug_info_usage usage,
293                    enum debug_struct_file criterion, int generic,
294                    int matches, int result)
295 {
296   /* Find the type name. */
297   tree type_decl = TYPE_STUB_DECL (type);
298   tree t = type_decl;
299   const char *name = 0;
300   if (TREE_CODE (t) == TYPE_DECL)
301     t = DECL_NAME (t);
302   if (t)
303     name = IDENTIFIER_POINTER (t);
304
305   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
306            criterion,
307            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
308            matches ? "bas" : "hdr",
309            generic ? "gen" : "ord",
310            usage == DINFO_USAGE_DFN ? ";" :
311              usage == DINFO_USAGE_DIR_USE ? "." : "*",
312            result,
313            (void*) type_decl, name);
314   return result;
315 }
316 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
317   dump_struct_debug (type, usage, criterion, generic, matches, result)
318
319 #else
320
321 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
322   (result)
323
324 #endif
325
326 static bool
327 should_emit_struct_debug (tree type, enum debug_info_usage usage)
328 {
329   enum debug_struct_file criterion;
330   tree type_decl;
331   bool generic = lang_hooks.types.generic_p (type);
332
333   if (generic)
334     criterion = debug_struct_generic[usage];
335   else
336     criterion = debug_struct_ordinary[usage];
337
338   if (criterion == DINFO_STRUCT_FILE_NONE)
339     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
340   if (criterion == DINFO_STRUCT_FILE_ANY)
341     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
342
343   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
344
345   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
346     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
347
348   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
349     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
350   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
351 }
352 \f
353 /* Return a pointer to a copy of the section string name S with all
354    attributes stripped off, and an asterisk prepended (for assemble_name).  */
355
356 static inline char *
357 stripattributes (const char *s)
358 {
359   char *stripped = XNEWVEC (char, strlen (s) + 2);
360   char *p = stripped;
361
362   *p++ = '*';
363
364   while (*s && *s != ',')
365     *p++ = *s++;
366
367   *p = '\0';
368   return stripped;
369 }
370
371 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
372    switch to the data section instead, and write out a synthetic start label
373    for collect2 the first time around.  */
374
375 static void
376 switch_to_eh_frame_section (bool back)
377 {
378   tree label;
379
380 #ifdef EH_FRAME_SECTION_NAME
381   if (eh_frame_section == 0)
382     {
383       int flags;
384
385       if (EH_TABLES_CAN_BE_READ_ONLY)
386         {
387           int fde_encoding;
388           int per_encoding;
389           int lsda_encoding;
390
391           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
392                                                        /*global=*/0);
393           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
394                                                        /*global=*/1);
395           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
396                                                         /*global=*/0);
397           flags = ((! flag_pic
398                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
399                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
400                         && (per_encoding & 0x70) != DW_EH_PE_absptr
401                         && (per_encoding & 0x70) != DW_EH_PE_aligned
402                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
403                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
404                    ? 0 : SECTION_WRITE);
405         }
406       else
407         flags = SECTION_WRITE;
408       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
409     }
410 #endif /* EH_FRAME_SECTION_NAME */
411
412   if (eh_frame_section)
413     switch_to_section (eh_frame_section);
414   else
415     {
416       /* We have no special eh_frame section.  Put the information in
417          the data section and emit special labels to guide collect2.  */
418       switch_to_section (data_section);
419
420       if (!back)
421         {
422           label = get_file_function_name ("F");
423           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
424           targetm.asm_out.globalize_label (asm_out_file,
425                                            IDENTIFIER_POINTER (label));
426           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
427         }
428     }
429 }
430
431 /* Switch [BACK] to the eh or debug frame table section, depending on
432    FOR_EH.  */
433
434 static void
435 switch_to_frame_table_section (int for_eh, bool back)
436 {
437   if (for_eh)
438     switch_to_eh_frame_section (back);
439   else
440     {
441       if (!debug_frame_section)
442         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
443                                            SECTION_DEBUG, NULL);
444       switch_to_section (debug_frame_section);
445     }
446 }
447
448 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
449
450 enum dw_cfi_oprnd_type
451 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
452 {
453   switch (cfi)
454     {
455     case DW_CFA_nop:
456     case DW_CFA_GNU_window_save:
457     case DW_CFA_remember_state:
458     case DW_CFA_restore_state:
459       return dw_cfi_oprnd_unused;
460
461     case DW_CFA_set_loc:
462     case DW_CFA_advance_loc1:
463     case DW_CFA_advance_loc2:
464     case DW_CFA_advance_loc4:
465     case DW_CFA_MIPS_advance_loc8:
466       return dw_cfi_oprnd_addr;
467
468     case DW_CFA_offset:
469     case DW_CFA_offset_extended:
470     case DW_CFA_def_cfa:
471     case DW_CFA_offset_extended_sf:
472     case DW_CFA_def_cfa_sf:
473     case DW_CFA_restore:
474     case DW_CFA_restore_extended:
475     case DW_CFA_undefined:
476     case DW_CFA_same_value:
477     case DW_CFA_def_cfa_register:
478     case DW_CFA_register:
479     case DW_CFA_expression:
480       return dw_cfi_oprnd_reg_num;
481
482     case DW_CFA_def_cfa_offset:
483     case DW_CFA_GNU_args_size:
484     case DW_CFA_def_cfa_offset_sf:
485       return dw_cfi_oprnd_offset;
486
487     case DW_CFA_def_cfa_expression:
488       return dw_cfi_oprnd_loc;
489
490     default:
491       gcc_unreachable ();
492     }
493 }
494
495 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
496
497 enum dw_cfi_oprnd_type
498 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
499 {
500   switch (cfi)
501     {
502     case DW_CFA_def_cfa:
503     case DW_CFA_def_cfa_sf:
504     case DW_CFA_offset:
505     case DW_CFA_offset_extended_sf:
506     case DW_CFA_offset_extended:
507       return dw_cfi_oprnd_offset;
508
509     case DW_CFA_register:
510       return dw_cfi_oprnd_reg_num;
511
512     case DW_CFA_expression:
513       return dw_cfi_oprnd_loc;
514
515     default:
516       return dw_cfi_oprnd_unused;
517     }
518 }
519
520 /* Output one FDE.  */
521
522 static void
523 output_fde (dw_fde_ref fde, bool for_eh, bool second,
524             char *section_start_label, int fde_encoding, char *augmentation,
525             bool any_lsda_needed, int lsda_encoding)
526 {
527   const char *begin, *end;
528   static unsigned int j;
529   char l1[20], l2[20];
530
531   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
532                                      /* empty */ 0);
533   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
534                                   for_eh + j);
535   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
536   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
537   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
538     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
539                          " indicating 64-bit DWARF extension");
540   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
541                         "FDE Length");
542   ASM_OUTPUT_LABEL (asm_out_file, l1);
543
544   if (for_eh)
545     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
546   else
547     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
548                            debug_frame_section, "FDE CIE offset");
549
550   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
551   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
552
553   if (for_eh)
554     {
555       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
556       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
557       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
558                                        "FDE initial location");
559       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
560                             end, begin, "FDE address range");
561     }
562   else
563     {
564       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
565       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
566     }
567
568   if (augmentation[0])
569     {
570       if (any_lsda_needed)
571         {
572           int size = size_of_encoded_value (lsda_encoding);
573
574           if (lsda_encoding == DW_EH_PE_aligned)
575             {
576               int offset = (  4         /* Length */
577                             + 4         /* CIE offset */
578                             + 2 * size_of_encoded_value (fde_encoding)
579                             + 1         /* Augmentation size */ );
580               int pad = -offset & (PTR_SIZE - 1);
581
582               size += pad;
583               gcc_assert (size_of_uleb128 (size) == 1);
584             }
585
586           dw2_asm_output_data_uleb128 (size, "Augmentation size");
587
588           if (fde->uses_eh_lsda)
589             {
590               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
591                                            fde->funcdef_number);
592               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
593                                                gen_rtx_SYMBOL_REF (Pmode, l1),
594                                                false,
595                                                "Language Specific Data Area");
596             }
597           else
598             {
599               if (lsda_encoding == DW_EH_PE_aligned)
600                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
601               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
602                                    "Language Specific Data Area (none)");
603             }
604         }
605       else
606         dw2_asm_output_data_uleb128 (0, "Augmentation size");
607     }
608
609   /* Loop through the Call Frame Instructions associated with this FDE.  */
610   fde->dw_fde_current_label = begin;
611   {
612     size_t from, until, i;
613
614     from = 0;
615     until = vec_safe_length (fde->dw_fde_cfi);
616
617     if (fde->dw_fde_second_begin == NULL)
618       ;
619     else if (!second)
620       until = fde->dw_fde_switch_cfi_index;
621     else
622       from = fde->dw_fde_switch_cfi_index;
623
624     for (i = from; i < until; i++)
625       output_cfi ((*fde->dw_fde_cfi)[i], fde, for_eh);
626   }
627
628   /* If we are to emit a ref/link from function bodies to their frame tables,
629      do it now.  This is typically performed to make sure that tables
630      associated with functions are dragged with them and not discarded in
631      garbage collecting links. We need to do this on a per function basis to
632      cope with -ffunction-sections.  */
633
634 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
635   /* Switch to the function section, emit the ref to the tables, and
636      switch *back* into the table section.  */
637   switch_to_section (function_section (fde->decl));
638   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
639   switch_to_frame_table_section (for_eh, true);
640 #endif
641
642   /* Pad the FDE out to an address sized boundary.  */
643   ASM_OUTPUT_ALIGN (asm_out_file,
644                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
645   ASM_OUTPUT_LABEL (asm_out_file, l2);
646
647   j += 2;
648 }
649
650 /* Return true if frame description entry FDE is needed for EH.  */
651
652 static bool
653 fde_needed_for_eh_p (dw_fde_ref fde)
654 {
655   if (flag_asynchronous_unwind_tables)
656     return true;
657
658   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
659     return true;
660
661   if (fde->uses_eh_lsda)
662     return true;
663
664   /* If exceptions are enabled, we have collected nothrow info.  */
665   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
666     return false;
667
668   return true;
669 }
670
671 /* Output the call frame information used to record information
672    that relates to calculating the frame pointer, and records the
673    location of saved registers.  */
674
675 static void
676 output_call_frame_info (int for_eh)
677 {
678   unsigned int i;
679   dw_fde_ref fde;
680   dw_cfi_ref cfi;
681   char l1[20], l2[20], section_start_label[20];
682   bool any_lsda_needed = false;
683   char augmentation[6];
684   int augmentation_size;
685   int fde_encoding = DW_EH_PE_absptr;
686   int per_encoding = DW_EH_PE_absptr;
687   int lsda_encoding = DW_EH_PE_absptr;
688   int return_reg;
689   rtx personality = NULL;
690   int dw_cie_version;
691
692   /* Don't emit a CIE if there won't be any FDEs.  */
693   if (!fde_vec)
694     return;
695
696   /* Nothing to do if the assembler's doing it all.  */
697   if (dwarf2out_do_cfi_asm ())
698     return;
699
700   /* If we don't have any functions we'll want to unwind out of, don't emit
701      any EH unwind information.  If we make FDEs linkonce, we may have to
702      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
703      want to avoid having an FDE kept around when the function it refers to
704      is discarded.  Example where this matters: a primary function template
705      in C++ requires EH information, an explicit specialization doesn't.  */
706   if (for_eh)
707     {
708       bool any_eh_needed = false;
709
710       FOR_EACH_VEC_ELT (*fde_vec, i, fde)
711         {
712           if (fde->uses_eh_lsda)
713             any_eh_needed = any_lsda_needed = true;
714           else if (fde_needed_for_eh_p (fde))
715             any_eh_needed = true;
716           else if (TARGET_USES_WEAK_UNWIND_INFO)
717             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
718         }
719
720       if (!any_eh_needed)
721         return;
722     }
723
724   /* We're going to be generating comments, so turn on app.  */
725   if (flag_debug_asm)
726     app_enable ();
727
728   /* Switch to the proper frame section, first time.  */
729   switch_to_frame_table_section (for_eh, false);
730
731   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
732   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
733
734   /* Output the CIE.  */
735   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
736   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
737   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
738     dw2_asm_output_data (4, 0xffffffff,
739       "Initial length escape value indicating 64-bit DWARF extension");
740   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
741                         "Length of Common Information Entry");
742   ASM_OUTPUT_LABEL (asm_out_file, l1);
743
744   /* Now that the CIE pointer is PC-relative for EH,
745      use 0 to identify the CIE.  */
746   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
747                        (for_eh ? 0 : DWARF_CIE_ID),
748                        "CIE Identifier Tag");
749
750   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
751      use CIE version 1, unless that would produce incorrect results
752      due to overflowing the return register column.  */
753   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
754   dw_cie_version = 1;
755   if (return_reg >= 256 || dwarf_version > 2)
756     dw_cie_version = 3;
757   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
758
759   augmentation[0] = 0;
760   augmentation_size = 0;
761
762   personality = current_unit_personality;
763   if (for_eh)
764     {
765       char *p;
766
767       /* Augmentation:
768          z      Indicates that a uleb128 is present to size the
769                 augmentation section.
770          L      Indicates the encoding (and thus presence) of
771                 an LSDA pointer in the FDE augmentation.
772          R      Indicates a non-default pointer encoding for
773                 FDE code pointers.
774          P      Indicates the presence of an encoding + language
775                 personality routine in the CIE augmentation.  */
776
777       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
778       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
779       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
780
781       p = augmentation + 1;
782       if (personality)
783         {
784           *p++ = 'P';
785           augmentation_size += 1 + size_of_encoded_value (per_encoding);
786           assemble_external_libcall (personality);
787         }
788       if (any_lsda_needed)
789         {
790           *p++ = 'L';
791           augmentation_size += 1;
792         }
793       if (fde_encoding != DW_EH_PE_absptr)
794         {
795           *p++ = 'R';
796           augmentation_size += 1;
797         }
798       if (p > augmentation + 1)
799         {
800           augmentation[0] = 'z';
801           *p = '\0';
802         }
803
804       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
805       if (personality && per_encoding == DW_EH_PE_aligned)
806         {
807           int offset = (  4             /* Length */
808                         + 4             /* CIE Id */
809                         + 1             /* CIE version */
810                         + strlen (augmentation) + 1     /* Augmentation */
811                         + size_of_uleb128 (1)           /* Code alignment */
812                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
813                         + 1             /* RA column */
814                         + 1             /* Augmentation size */
815                         + 1             /* Personality encoding */ );
816           int pad = -offset & (PTR_SIZE - 1);
817
818           augmentation_size += pad;
819
820           /* Augmentations should be small, so there's scarce need to
821              iterate for a solution.  Die if we exceed one uleb128 byte.  */
822           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
823         }
824     }
825
826   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
827   if (dw_cie_version >= 4)
828     {
829       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
830       dw2_asm_output_data (1, 0, "CIE Segment Size");
831     }
832   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
833   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
834                                "CIE Data Alignment Factor");
835
836   if (dw_cie_version == 1)
837     dw2_asm_output_data (1, return_reg, "CIE RA Column");
838   else
839     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
840
841   if (augmentation[0])
842     {
843       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
844       if (personality)
845         {
846           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
847                                eh_data_format_name (per_encoding));
848           dw2_asm_output_encoded_addr_rtx (per_encoding,
849                                            personality,
850                                            true, NULL);
851         }
852
853       if (any_lsda_needed)
854         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
855                              eh_data_format_name (lsda_encoding));
856
857       if (fde_encoding != DW_EH_PE_absptr)
858         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
859                              eh_data_format_name (fde_encoding));
860     }
861
862   FOR_EACH_VEC_ELT (*cie_cfi_vec, i, cfi)
863     output_cfi (cfi, NULL, for_eh);
864
865   /* Pad the CIE out to an address sized boundary.  */
866   ASM_OUTPUT_ALIGN (asm_out_file,
867                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
868   ASM_OUTPUT_LABEL (asm_out_file, l2);
869
870   /* Loop through all of the FDE's.  */
871   FOR_EACH_VEC_ELT (*fde_vec, i, fde)
872     {
873       unsigned int k;
874
875       /* Don't emit EH unwind info for leaf functions that don't need it.  */
876       if (for_eh && !fde_needed_for_eh_p (fde))
877         continue;
878
879       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
880         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
881                     augmentation, any_lsda_needed, lsda_encoding);
882     }
883
884   if (for_eh && targetm.terminate_dw2_eh_frame_info)
885     dw2_asm_output_data (4, 0, "End of Table");
886
887   /* Turn off app to make assembly quicker.  */
888   if (flag_debug_asm)
889     app_disable ();
890 }
891
892 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
893
894 static void
895 dwarf2out_do_cfi_startproc (bool second)
896 {
897   int enc;
898   rtx ref;
899   rtx personality = get_personality_function (current_function_decl);
900
901   fprintf (asm_out_file, "\t.cfi_startproc\n");
902
903   if (personality)
904     {
905       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
906       ref = personality;
907
908       /* ??? The GAS support isn't entirely consistent.  We have to
909          handle indirect support ourselves, but PC-relative is done
910          in the assembler.  Further, the assembler can't handle any
911          of the weirder relocation types.  */
912       if (enc & DW_EH_PE_indirect)
913         ref = dw2_force_const_mem (ref, true);
914
915       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
916       output_addr_const (asm_out_file, ref);
917       fputc ('\n', asm_out_file);
918     }
919
920   if (crtl->uses_eh_lsda)
921     {
922       char lab[20];
923
924       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
925       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
926                                    current_function_funcdef_no);
927       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
928       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
929
930       if (enc & DW_EH_PE_indirect)
931         ref = dw2_force_const_mem (ref, true);
932
933       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
934       output_addr_const (asm_out_file, ref);
935       fputc ('\n', asm_out_file);
936     }
937 }
938
939 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
940    this allocation may be done before pass_final.  */
941
942 dw_fde_ref
943 dwarf2out_alloc_current_fde (void)
944 {
945   dw_fde_ref fde;
946
947   fde = ggc_alloc_cleared_dw_fde_node ();
948   fde->decl = current_function_decl;
949   fde->funcdef_number = current_function_funcdef_no;
950   fde->fde_index = vec_safe_length (fde_vec);
951   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
952   fde->uses_eh_lsda = crtl->uses_eh_lsda;
953   fde->nothrow = crtl->nothrow;
954   fde->drap_reg = INVALID_REGNUM;
955   fde->vdrap_reg = INVALID_REGNUM;
956
957   /* Record the FDE associated with this function.  */
958   cfun->fde = fde;
959   vec_safe_push (fde_vec, fde);
960
961   return fde;
962 }
963
964 /* Output a marker (i.e. a label) for the beginning of a function, before
965    the prologue.  */
966
967 void
968 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
969                           const char *file ATTRIBUTE_UNUSED)
970 {
971   char label[MAX_ARTIFICIAL_LABEL_BYTES];
972   char * dup_label;
973   dw_fde_ref fde;
974   section *fnsec;
975   bool do_frame;
976
977   current_function_func_begin_label = NULL;
978
979   do_frame = dwarf2out_do_frame ();
980
981   /* ??? current_function_func_begin_label is also used by except.c for
982      call-site information.  We must emit this label if it might be used.  */
983   if (!do_frame
984       && (!flag_exceptions
985           || targetm_common.except_unwind_info (&global_options) == UI_SJLJ))
986     return;
987
988   fnsec = function_section (current_function_decl);
989   switch_to_section (fnsec);
990   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
991                                current_function_funcdef_no);
992   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
993                           current_function_funcdef_no);
994   dup_label = xstrdup (label);
995   current_function_func_begin_label = dup_label;
996
997   /* We can elide the fde allocation if we're not emitting debug info.  */
998   if (!do_frame)
999     return;
1000
1001   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1002      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1003      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1004      do so now.  */
1005   fde = cfun->fde;
1006   if (fde == NULL)
1007     fde = dwarf2out_alloc_current_fde ();
1008
1009   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1010   fde->dw_fde_begin = dup_label;
1011   fde->dw_fde_current_label = dup_label;
1012   fde->in_std_section = (fnsec == text_section
1013                          || (cold_text_section && fnsec == cold_text_section));
1014
1015   /* We only want to output line number information for the genuine dwarf2
1016      prologue case, not the eh frame case.  */
1017 #ifdef DWARF2_DEBUGGING_INFO
1018   if (file)
1019     dwarf2out_source_line (line, file, 0, true);
1020 #endif
1021
1022   if (dwarf2out_do_cfi_asm ())
1023     dwarf2out_do_cfi_startproc (false);
1024   else
1025     {
1026       rtx personality = get_personality_function (current_function_decl);
1027       if (!current_unit_personality)
1028         current_unit_personality = personality;
1029
1030       /* We cannot keep a current personality per function as without CFI
1031          asm, at the point where we emit the CFI data, there is no current
1032          function anymore.  */
1033       if (personality && current_unit_personality != personality)
1034         sorry ("multiple EH personalities are supported only with assemblers "
1035                "supporting .cfi_personality directive");
1036     }
1037 }
1038
1039 /* Output a marker (i.e. a label) for the end of the generated code
1040    for a function prologue.  This gets called *after* the prologue code has
1041    been generated.  */
1042
1043 void
1044 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1045                         const char *file ATTRIBUTE_UNUSED)
1046 {
1047   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1048
1049   /* Output a label to mark the endpoint of the code generated for this
1050      function.  */
1051   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1052                                current_function_funcdef_no);
1053   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1054                           current_function_funcdef_no);
1055   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1056 }
1057
1058 /* Output a marker (i.e. a label) for the beginning of the generated code
1059    for a function epilogue.  This gets called *before* the prologue code has
1060    been generated.  */
1061
1062 void
1063 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1064                           const char *file ATTRIBUTE_UNUSED)
1065 {
1066   dw_fde_ref fde = cfun->fde;
1067   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1068
1069   if (fde->dw_fde_vms_begin_epilogue)
1070     return;
1071
1072   /* Output a label to mark the endpoint of the code generated for this
1073      function.  */
1074   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1075                                current_function_funcdef_no);
1076   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1077                           current_function_funcdef_no);
1078   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1079 }
1080
1081 /* Output a marker (i.e. a label) for the absolute end of the generated code
1082    for a function definition.  This gets called *after* the epilogue code has
1083    been generated.  */
1084
1085 void
1086 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1087                         const char *file ATTRIBUTE_UNUSED)
1088 {
1089   dw_fde_ref fde;
1090   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1091
1092   last_var_location_insn = NULL_RTX;
1093   cached_next_real_insn = NULL_RTX;
1094
1095   if (dwarf2out_do_cfi_asm ())
1096     fprintf (asm_out_file, "\t.cfi_endproc\n");
1097
1098   /* Output a label to mark the endpoint of the code generated for this
1099      function.  */
1100   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1101                                current_function_funcdef_no);
1102   ASM_OUTPUT_LABEL (asm_out_file, label);
1103   fde = cfun->fde;
1104   gcc_assert (fde != NULL);
1105   if (fde->dw_fde_second_begin == NULL)
1106     fde->dw_fde_end = xstrdup (label);
1107 }
1108
1109 void
1110 dwarf2out_frame_finish (void)
1111 {
1112   /* Output call frame information.  */
1113   if (targetm.debug_unwind_info () == UI_DWARF2)
1114     output_call_frame_info (0);
1115
1116   /* Output another copy for the unwinder.  */
1117   if ((flag_unwind_tables || flag_exceptions)
1118       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1119     output_call_frame_info (1);
1120 }
1121
1122 /* Note that the current function section is being used for code.  */
1123
1124 static void
1125 dwarf2out_note_section_used (void)
1126 {
1127   section *sec = current_function_section ();
1128   if (sec == text_section)
1129     text_section_used = true;
1130   else if (sec == cold_text_section)
1131     cold_text_section_used = true;
1132 }
1133
1134 static void var_location_switch_text_section (void);
1135 static void set_cur_line_info_table (section *);
1136
1137 void
1138 dwarf2out_switch_text_section (void)
1139 {
1140   section *sect;
1141   dw_fde_ref fde = cfun->fde;
1142
1143   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1144
1145   if (!in_cold_section_p)
1146     {
1147       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1148       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1149       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1150     }
1151   else
1152     {
1153       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1154       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1155       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1156     }
1157   have_multiple_function_sections = true;
1158
1159   /* There is no need to mark used sections when not debugging.  */
1160   if (cold_text_section != NULL)
1161     dwarf2out_note_section_used ();
1162
1163   if (dwarf2out_do_cfi_asm ())
1164     fprintf (asm_out_file, "\t.cfi_endproc\n");
1165
1166   /* Now do the real section switch.  */
1167   sect = current_function_section ();
1168   switch_to_section (sect);
1169
1170   fde->second_in_std_section
1171     = (sect == text_section
1172        || (cold_text_section && sect == cold_text_section));
1173
1174   if (dwarf2out_do_cfi_asm ())
1175     dwarf2out_do_cfi_startproc (true);
1176
1177   var_location_switch_text_section ();
1178
1179   if (cold_text_section != NULL)
1180     set_cur_line_info_table (sect);
1181 }
1182 \f
1183 /* And now, the subset of the debugging information support code necessary
1184    for emitting location expressions.  */
1185
1186 /* Data about a single source file.  */
1187 struct GTY(()) dwarf_file_data {
1188   const char * filename;
1189   int emitted_number;
1190 };
1191
1192 typedef struct GTY(()) deferred_locations_struct
1193 {
1194   tree variable;
1195   dw_die_ref die;
1196 } deferred_locations;
1197
1198
1199 static GTY(()) vec<deferred_locations, va_gc> *deferred_locations_list;
1200
1201
1202 /* Describe an entry into the .debug_addr section.  */
1203
1204 enum ate_kind {
1205   ate_kind_rtx,
1206   ate_kind_rtx_dtprel,
1207   ate_kind_label
1208 };
1209
1210 typedef struct GTY(()) addr_table_entry_struct {
1211   enum ate_kind kind;
1212   unsigned int refcount;
1213   unsigned int index;
1214   union addr_table_entry_struct_union
1215     {
1216       rtx GTY ((tag ("0"))) rtl;
1217       char * GTY ((tag ("1"))) label;
1218     }
1219   GTY ((desc ("%1.kind"))) addr;
1220 }
1221 addr_table_entry;
1222
1223 /* Location lists are ranges + location descriptions for that range,
1224    so you can track variables that are in different places over
1225    their entire life.  */
1226 typedef struct GTY(()) dw_loc_list_struct {
1227   dw_loc_list_ref dw_loc_next;
1228   const char *begin; /* Label and addr_entry for start of range */
1229   addr_table_entry *begin_entry;
1230   const char *end;  /* Label for end of range */
1231   char *ll_symbol; /* Label for beginning of location list.
1232                       Only on head of list */
1233   const char *section; /* Section this loclist is relative to */
1234   dw_loc_descr_ref expr;
1235   hashval_t hash;
1236   /* True if all addresses in this and subsequent lists are known to be
1237      resolved.  */
1238   bool resolved_addr;
1239   /* True if this list has been replaced by dw_loc_next.  */
1240   bool replaced;
1241   bool emitted;
1242   /* True if the range should be emitted even if begin and end
1243      are the same.  */
1244   bool force;
1245 } dw_loc_list_node;
1246
1247 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1248
1249 /* Convert a DWARF stack opcode into its string name.  */
1250
1251 static const char *
1252 dwarf_stack_op_name (unsigned int op)
1253 {
1254   const char *name = get_DW_OP_name (op);
1255
1256   if (name != NULL)
1257     return name;
1258
1259   return "OP_<unknown>";
1260 }
1261
1262 /* Return a pointer to a newly allocated location description.  Location
1263    descriptions are simple expression terms that can be strung
1264    together to form more complicated location (address) descriptions.  */
1265
1266 static inline dw_loc_descr_ref
1267 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1268                unsigned HOST_WIDE_INT oprnd2)
1269 {
1270   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1271
1272   descr->dw_loc_opc = op;
1273   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1274   descr->dw_loc_oprnd1.val_entry = NULL;
1275   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1276   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1277   descr->dw_loc_oprnd2.val_entry = NULL;
1278   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1279
1280   return descr;
1281 }
1282
1283 /* Return a pointer to a newly allocated location description for
1284    REG and OFFSET.  */
1285
1286 static inline dw_loc_descr_ref
1287 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1288 {
1289   if (reg <= 31)
1290     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1291                           offset, 0);
1292   else
1293     return new_loc_descr (DW_OP_bregx, reg, offset);
1294 }
1295
1296 /* Add a location description term to a location description expression.  */
1297
1298 static inline void
1299 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1300 {
1301   dw_loc_descr_ref *d;
1302
1303   /* Find the end of the chain.  */
1304   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1305     ;
1306
1307   *d = descr;
1308 }
1309
1310 /* Compare two location operands for exact equality.  */
1311
1312 static bool
1313 dw_val_equal_p (dw_val_node *a, dw_val_node *b)
1314 {
1315   if (a->val_class != b->val_class)
1316     return false;
1317   switch (a->val_class)
1318     {
1319     case dw_val_class_none:
1320       return true;
1321     case dw_val_class_addr:
1322       return rtx_equal_p (a->v.val_addr, b->v.val_addr);
1323
1324     case dw_val_class_offset:
1325     case dw_val_class_unsigned_const:
1326     case dw_val_class_const:
1327     case dw_val_class_range_list:
1328     case dw_val_class_lineptr:
1329     case dw_val_class_macptr:
1330       /* These are all HOST_WIDE_INT, signed or unsigned.  */
1331       return a->v.val_unsigned == b->v.val_unsigned;
1332
1333     case dw_val_class_loc:
1334       return a->v.val_loc == b->v.val_loc;
1335     case dw_val_class_loc_list:
1336       return a->v.val_loc_list == b->v.val_loc_list;
1337     case dw_val_class_die_ref:
1338       return a->v.val_die_ref.die == b->v.val_die_ref.die;
1339     case dw_val_class_fde_ref:
1340       return a->v.val_fde_index == b->v.val_fde_index;
1341     case dw_val_class_lbl_id:
1342     case dw_val_class_high_pc:
1343       return strcmp (a->v.val_lbl_id, b->v.val_lbl_id) == 0;
1344     case dw_val_class_str:
1345       return a->v.val_str == b->v.val_str;
1346     case dw_val_class_flag:
1347       return a->v.val_flag == b->v.val_flag;
1348     case dw_val_class_file:
1349       return a->v.val_file == b->v.val_file;
1350     case dw_val_class_decl_ref:
1351       return a->v.val_decl_ref == b->v.val_decl_ref;
1352     
1353     case dw_val_class_const_double:
1354       return (a->v.val_double.high == b->v.val_double.high
1355               && a->v.val_double.low == b->v.val_double.low);
1356
1357     case dw_val_class_vec:
1358       {
1359         size_t a_len = a->v.val_vec.elt_size * a->v.val_vec.length;
1360         size_t b_len = b->v.val_vec.elt_size * b->v.val_vec.length;
1361
1362         return (a_len == b_len
1363                 && !memcmp (a->v.val_vec.array, b->v.val_vec.array, a_len));
1364       }
1365
1366     case dw_val_class_data8:
1367       return memcmp (a->v.val_data8, b->v.val_data8, 8) == 0;
1368
1369     case dw_val_class_vms_delta:
1370       return (!strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1)
1371               && !strcmp (a->v.val_vms_delta.lbl1, b->v.val_vms_delta.lbl1));
1372     }
1373   gcc_unreachable ();
1374 }
1375
1376 /* Compare two location atoms for exact equality.  */
1377
1378 static bool
1379 loc_descr_equal_p_1 (dw_loc_descr_ref a, dw_loc_descr_ref b)
1380 {
1381   if (a->dw_loc_opc != b->dw_loc_opc)
1382     return false;
1383
1384   /* ??? This is only ever set for DW_OP_constNu, for N equal to the
1385      address size, but since we always allocate cleared storage it
1386      should be zero for other types of locations.  */
1387   if (a->dtprel != b->dtprel)
1388     return false;
1389
1390   return (dw_val_equal_p (&a->dw_loc_oprnd1, &b->dw_loc_oprnd1)
1391           && dw_val_equal_p (&a->dw_loc_oprnd2, &b->dw_loc_oprnd2));
1392 }
1393
1394 /* Compare two complete location expressions for exact equality.  */
1395
1396 bool
1397 loc_descr_equal_p (dw_loc_descr_ref a, dw_loc_descr_ref b)
1398 {
1399   while (1)
1400     {
1401       if (a == b)
1402         return true;
1403       if (a == NULL || b == NULL)
1404         return false;
1405       if (!loc_descr_equal_p_1 (a, b))
1406         return false;
1407
1408       a = a->dw_loc_next;
1409       b = b->dw_loc_next;
1410     }
1411 }
1412
1413
1414 /* Add a constant OFFSET to a location expression.  */
1415
1416 static void
1417 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1418 {
1419   dw_loc_descr_ref loc;
1420   HOST_WIDE_INT *p;
1421
1422   gcc_assert (*list_head != NULL);
1423
1424   if (!offset)
1425     return;
1426
1427   /* Find the end of the chain.  */
1428   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1429     ;
1430
1431   p = NULL;
1432   if (loc->dw_loc_opc == DW_OP_fbreg
1433       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1434     p = &loc->dw_loc_oprnd1.v.val_int;
1435   else if (loc->dw_loc_opc == DW_OP_bregx)
1436     p = &loc->dw_loc_oprnd2.v.val_int;
1437
1438   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1439      offset.  Don't optimize if an signed integer overflow would happen.  */
1440   if (p != NULL
1441       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1442           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1443     *p += offset;
1444
1445   else if (offset > 0)
1446     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1447
1448   else
1449     {
1450       loc->dw_loc_next = int_loc_descriptor (-offset);
1451       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1452     }
1453 }
1454
1455 /* Add a constant OFFSET to a location list.  */
1456
1457 static void
1458 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1459 {
1460   dw_loc_list_ref d;
1461   for (d = list_head; d != NULL; d = d->dw_loc_next)
1462     loc_descr_plus_const (&d->expr, offset);
1463 }
1464
1465 #define DWARF_REF_SIZE  \
1466   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1467
1468 static unsigned long int get_base_type_offset (dw_die_ref);
1469
1470 /* Return the size of a location descriptor.  */
1471
1472 static unsigned long
1473 size_of_loc_descr (dw_loc_descr_ref loc)
1474 {
1475   unsigned long size = 1;
1476
1477   switch (loc->dw_loc_opc)
1478     {
1479     case DW_OP_addr:
1480       size += DWARF2_ADDR_SIZE;
1481       break;
1482     case DW_OP_GNU_addr_index:
1483     case DW_OP_GNU_const_index:
1484       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1485       size += size_of_uleb128 (loc->dw_loc_oprnd1.val_entry->index);
1486       break;
1487     case DW_OP_const1u:
1488     case DW_OP_const1s:
1489       size += 1;
1490       break;
1491     case DW_OP_const2u:
1492     case DW_OP_const2s:
1493       size += 2;
1494       break;
1495     case DW_OP_const4u:
1496     case DW_OP_const4s:
1497       size += 4;
1498       break;
1499     case DW_OP_const8u:
1500     case DW_OP_const8s:
1501       size += 8;
1502       break;
1503     case DW_OP_constu:
1504       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1505       break;
1506     case DW_OP_consts:
1507       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1508       break;
1509     case DW_OP_pick:
1510       size += 1;
1511       break;
1512     case DW_OP_plus_uconst:
1513       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1514       break;
1515     case DW_OP_skip:
1516     case DW_OP_bra:
1517       size += 2;
1518       break;
1519     case DW_OP_breg0:
1520     case DW_OP_breg1:
1521     case DW_OP_breg2:
1522     case DW_OP_breg3:
1523     case DW_OP_breg4:
1524     case DW_OP_breg5:
1525     case DW_OP_breg6:
1526     case DW_OP_breg7:
1527     case DW_OP_breg8:
1528     case DW_OP_breg9:
1529     case DW_OP_breg10:
1530     case DW_OP_breg11:
1531     case DW_OP_breg12:
1532     case DW_OP_breg13:
1533     case DW_OP_breg14:
1534     case DW_OP_breg15:
1535     case DW_OP_breg16:
1536     case DW_OP_breg17:
1537     case DW_OP_breg18:
1538     case DW_OP_breg19:
1539     case DW_OP_breg20:
1540     case DW_OP_breg21:
1541     case DW_OP_breg22:
1542     case DW_OP_breg23:
1543     case DW_OP_breg24:
1544     case DW_OP_breg25:
1545     case DW_OP_breg26:
1546     case DW_OP_breg27:
1547     case DW_OP_breg28:
1548     case DW_OP_breg29:
1549     case DW_OP_breg30:
1550     case DW_OP_breg31:
1551       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1552       break;
1553     case DW_OP_regx:
1554       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1555       break;
1556     case DW_OP_fbreg:
1557       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1558       break;
1559     case DW_OP_bregx:
1560       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1561       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1562       break;
1563     case DW_OP_piece:
1564       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1565       break;
1566     case DW_OP_bit_piece:
1567       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1568       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1569       break;
1570     case DW_OP_deref_size:
1571     case DW_OP_xderef_size:
1572       size += 1;
1573       break;
1574     case DW_OP_call2:
1575       size += 2;
1576       break;
1577     case DW_OP_call4:
1578       size += 4;
1579       break;
1580     case DW_OP_call_ref:
1581       size += DWARF_REF_SIZE;
1582       break;
1583     case DW_OP_implicit_value:
1584       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1585               + loc->dw_loc_oprnd1.v.val_unsigned;
1586       break;
1587     case DW_OP_GNU_implicit_pointer:
1588       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1589       break;
1590     case DW_OP_GNU_entry_value:
1591       {
1592         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1593         size += size_of_uleb128 (op_size) + op_size;
1594         break;
1595       }
1596     case DW_OP_GNU_const_type:
1597       {
1598         unsigned long o
1599           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1600         size += size_of_uleb128 (o) + 1;
1601         switch (loc->dw_loc_oprnd2.val_class)
1602           {
1603           case dw_val_class_vec:
1604             size += loc->dw_loc_oprnd2.v.val_vec.length
1605                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1606             break;
1607           case dw_val_class_const:
1608             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1609             break;
1610           case dw_val_class_const_double:
1611             size += HOST_BITS_PER_DOUBLE_INT / BITS_PER_UNIT;
1612             break;
1613           default:
1614             gcc_unreachable ();
1615           }
1616         break;
1617       }
1618     case DW_OP_GNU_regval_type:
1619       {
1620         unsigned long o
1621           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1622         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1623                 + size_of_uleb128 (o);
1624       }
1625       break;
1626     case DW_OP_GNU_deref_type:
1627       {
1628         unsigned long o
1629           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1630         size += 1 + size_of_uleb128 (o);
1631       }
1632       break;
1633     case DW_OP_GNU_convert:
1634     case DW_OP_GNU_reinterpret:
1635       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1636         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1637       else
1638         {
1639           unsigned long o
1640             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1641           size += size_of_uleb128 (o);
1642         }
1643       break;
1644     case DW_OP_GNU_parameter_ref:
1645       size += 4;
1646       break;
1647     default:
1648       break;
1649     }
1650
1651   return size;
1652 }
1653
1654 /* Return the size of a series of location descriptors.  */
1655
1656 unsigned long
1657 size_of_locs (dw_loc_descr_ref loc)
1658 {
1659   dw_loc_descr_ref l;
1660   unsigned long size;
1661
1662   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1663      field, to avoid writing to a PCH file.  */
1664   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1665     {
1666       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1667         break;
1668       size += size_of_loc_descr (l);
1669     }
1670   if (! l)
1671     return size;
1672
1673   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1674     {
1675       l->dw_loc_addr = size;
1676       size += size_of_loc_descr (l);
1677     }
1678
1679   return size;
1680 }
1681
1682 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1683 static void get_ref_die_offset_label (char *, dw_die_ref);
1684 static unsigned long int get_ref_die_offset (dw_die_ref);
1685
1686 /* Output location description stack opcode's operands (if any).
1687    The for_eh_or_skip parameter controls whether register numbers are
1688    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1689    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1690    info).  This should be suppressed for the cases that have not been converted
1691    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1692
1693 static void
1694 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1695 {
1696   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1697   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1698
1699   switch (loc->dw_loc_opc)
1700     {
1701 #ifdef DWARF2_DEBUGGING_INFO
1702     case DW_OP_const2u:
1703     case DW_OP_const2s:
1704       dw2_asm_output_data (2, val1->v.val_int, NULL);
1705       break;
1706     case DW_OP_const4u:
1707       if (loc->dtprel)
1708         {
1709           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1710           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1711                                                val1->v.val_addr);
1712           fputc ('\n', asm_out_file);
1713           break;
1714         }
1715       /* FALLTHRU */
1716     case DW_OP_const4s:
1717       dw2_asm_output_data (4, val1->v.val_int, NULL);
1718       break;
1719     case DW_OP_const8u:
1720       if (loc->dtprel)
1721         {
1722           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1723           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1724                                                val1->v.val_addr);
1725           fputc ('\n', asm_out_file);
1726           break;
1727         }
1728       /* FALLTHRU */
1729     case DW_OP_const8s:
1730       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1731       dw2_asm_output_data (8, val1->v.val_int, NULL);
1732       break;
1733     case DW_OP_skip:
1734     case DW_OP_bra:
1735       {
1736         int offset;
1737
1738         gcc_assert (val1->val_class == dw_val_class_loc);
1739         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1740
1741         dw2_asm_output_data (2, offset, NULL);
1742       }
1743       break;
1744     case DW_OP_implicit_value:
1745       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1746       switch (val2->val_class)
1747         {
1748         case dw_val_class_const:
1749           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1750           break;
1751         case dw_val_class_vec:
1752           {
1753             unsigned int elt_size = val2->v.val_vec.elt_size;
1754             unsigned int len = val2->v.val_vec.length;
1755             unsigned int i;
1756             unsigned char *p;
1757
1758             if (elt_size > sizeof (HOST_WIDE_INT))
1759               {
1760                 elt_size /= 2;
1761                 len *= 2;
1762               }
1763             for (i = 0, p = val2->v.val_vec.array;
1764                  i < len;
1765                  i++, p += elt_size)
1766               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1767                                    "fp or vector constant word %u", i);
1768           }
1769           break;
1770         case dw_val_class_const_double:
1771           {
1772             unsigned HOST_WIDE_INT first, second;
1773
1774             if (WORDS_BIG_ENDIAN)
1775               {
1776                 first = val2->v.val_double.high;
1777                 second = val2->v.val_double.low;
1778               }
1779             else
1780               {
1781                 first = val2->v.val_double.low;
1782                 second = val2->v.val_double.high;
1783               }
1784             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1785                                  first, NULL);
1786             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
1787                                  second, NULL);
1788           }
1789           break;
1790         case dw_val_class_addr:
1791           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
1792           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
1793           break;
1794         default:
1795           gcc_unreachable ();
1796         }
1797       break;
1798 #else
1799     case DW_OP_const2u:
1800     case DW_OP_const2s:
1801     case DW_OP_const4u:
1802     case DW_OP_const4s:
1803     case DW_OP_const8u:
1804     case DW_OP_const8s:
1805     case DW_OP_skip:
1806     case DW_OP_bra:
1807     case DW_OP_implicit_value:
1808       /* We currently don't make any attempt to make sure these are
1809          aligned properly like we do for the main unwind info, so
1810          don't support emitting things larger than a byte if we're
1811          only doing unwinding.  */
1812       gcc_unreachable ();
1813 #endif
1814     case DW_OP_const1u:
1815     case DW_OP_const1s:
1816       dw2_asm_output_data (1, val1->v.val_int, NULL);
1817       break;
1818     case DW_OP_constu:
1819       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1820       break;
1821     case DW_OP_consts:
1822       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1823       break;
1824     case DW_OP_pick:
1825       dw2_asm_output_data (1, val1->v.val_int, NULL);
1826       break;
1827     case DW_OP_plus_uconst:
1828       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1829       break;
1830     case DW_OP_breg0:
1831     case DW_OP_breg1:
1832     case DW_OP_breg2:
1833     case DW_OP_breg3:
1834     case DW_OP_breg4:
1835     case DW_OP_breg5:
1836     case DW_OP_breg6:
1837     case DW_OP_breg7:
1838     case DW_OP_breg8:
1839     case DW_OP_breg9:
1840     case DW_OP_breg10:
1841     case DW_OP_breg11:
1842     case DW_OP_breg12:
1843     case DW_OP_breg13:
1844     case DW_OP_breg14:
1845     case DW_OP_breg15:
1846     case DW_OP_breg16:
1847     case DW_OP_breg17:
1848     case DW_OP_breg18:
1849     case DW_OP_breg19:
1850     case DW_OP_breg20:
1851     case DW_OP_breg21:
1852     case DW_OP_breg22:
1853     case DW_OP_breg23:
1854     case DW_OP_breg24:
1855     case DW_OP_breg25:
1856     case DW_OP_breg26:
1857     case DW_OP_breg27:
1858     case DW_OP_breg28:
1859     case DW_OP_breg29:
1860     case DW_OP_breg30:
1861     case DW_OP_breg31:
1862       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1863       break;
1864     case DW_OP_regx:
1865       {
1866         unsigned r = val1->v.val_unsigned;
1867         if (for_eh_or_skip >= 0)
1868           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1869         gcc_assert (size_of_uleb128 (r) 
1870                     == size_of_uleb128 (val1->v.val_unsigned));
1871         dw2_asm_output_data_uleb128 (r, NULL);  
1872       }
1873       break;
1874     case DW_OP_fbreg:
1875       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
1876       break;
1877     case DW_OP_bregx:
1878       {
1879         unsigned r = val1->v.val_unsigned;
1880         if (for_eh_or_skip >= 0)
1881           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
1882         gcc_assert (size_of_uleb128 (r) 
1883                     == size_of_uleb128 (val1->v.val_unsigned));
1884         dw2_asm_output_data_uleb128 (r, NULL);  
1885         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1886       }
1887       break;
1888     case DW_OP_piece:
1889       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1890       break;
1891     case DW_OP_bit_piece:
1892       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1893       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
1894       break;
1895     case DW_OP_deref_size:
1896     case DW_OP_xderef_size:
1897       dw2_asm_output_data (1, val1->v.val_int, NULL);
1898       break;
1899
1900     case DW_OP_addr:
1901       if (loc->dtprel)
1902         {
1903           if (targetm.asm_out.output_dwarf_dtprel)
1904             {
1905               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
1906                                                    DWARF2_ADDR_SIZE,
1907                                                    val1->v.val_addr);
1908               fputc ('\n', asm_out_file);
1909             }
1910           else
1911             gcc_unreachable ();
1912         }
1913       else
1914         {
1915 #ifdef DWARF2_DEBUGGING_INFO
1916           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
1917 #else
1918           gcc_unreachable ();
1919 #endif
1920         }
1921       break;
1922
1923     case DW_OP_GNU_addr_index:
1924     case DW_OP_GNU_const_index:
1925       gcc_assert (loc->dw_loc_oprnd1.val_entry->index != NO_INDEX_ASSIGNED);
1926       dw2_asm_output_data_uleb128 (loc->dw_loc_oprnd1.val_entry->index,
1927                                    "(index into .debug_addr)");
1928       break;
1929
1930     case DW_OP_GNU_implicit_pointer:
1931       {
1932         char label[MAX_ARTIFICIAL_LABEL_BYTES
1933                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
1934         gcc_assert (val1->val_class == dw_val_class_die_ref);
1935         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
1936         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
1937         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
1938       }
1939       break;
1940
1941     case DW_OP_GNU_entry_value:
1942       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
1943       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
1944       break;
1945
1946     case DW_OP_GNU_const_type:
1947       {
1948         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
1949         gcc_assert (o);
1950         dw2_asm_output_data_uleb128 (o, NULL);
1951         switch (val2->val_class)
1952           {
1953           case dw_val_class_const:
1954             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1955             dw2_asm_output_data (1, l, NULL);
1956             dw2_asm_output_data (l, val2->v.val_int, NULL);
1957             break;
1958           case dw_val_class_vec:
1959             {
1960               unsigned int elt_size = val2->v.val_vec.elt_size;
1961               unsigned int len = val2->v.val_vec.length;
1962               unsigned int i;
1963               unsigned char *p;
1964
1965               l = len * elt_size;
1966               dw2_asm_output_data (1, l, NULL);
1967               if (elt_size > sizeof (HOST_WIDE_INT))
1968                 {
1969                   elt_size /= 2;
1970                   len *= 2;
1971                 }
1972               for (i = 0, p = val2->v.val_vec.array;
1973                    i < len;
1974                    i++, p += elt_size)
1975                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1976                                      "fp or vector constant word %u", i);
1977             }
1978             break;
1979           case dw_val_class_const_double:
1980             {
1981               unsigned HOST_WIDE_INT first, second;
1982               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
1983
1984               dw2_asm_output_data (1, 2 * l, NULL);
1985               if (WORDS_BIG_ENDIAN)
1986                 {
1987                   first = val2->v.val_double.high;
1988                   second = val2->v.val_double.low;
1989                 }
1990               else
1991                 {
1992                   first = val2->v.val_double.low;
1993                   second = val2->v.val_double.high;
1994                 }
1995               dw2_asm_output_data (l, first, NULL);
1996               dw2_asm_output_data (l, second, NULL);
1997             }
1998             break;
1999           default:
2000             gcc_unreachable ();
2001           }
2002       }
2003       break;
2004     case DW_OP_GNU_regval_type:
2005       {
2006         unsigned r = val1->v.val_unsigned;
2007         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2008         gcc_assert (o);
2009         if (for_eh_or_skip >= 0)
2010           {
2011             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2012             gcc_assert (size_of_uleb128 (r)
2013                         == size_of_uleb128 (val1->v.val_unsigned));
2014           }
2015         dw2_asm_output_data_uleb128 (r, NULL);
2016         dw2_asm_output_data_uleb128 (o, NULL);
2017       }
2018       break;
2019     case DW_OP_GNU_deref_type:
2020       {
2021         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2022         gcc_assert (o);
2023         dw2_asm_output_data (1, val1->v.val_int, NULL);
2024         dw2_asm_output_data_uleb128 (o, NULL);
2025       }
2026       break;
2027     case DW_OP_GNU_convert:
2028     case DW_OP_GNU_reinterpret:
2029       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2030         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2031       else
2032         {
2033           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2034           gcc_assert (o);
2035           dw2_asm_output_data_uleb128 (o, NULL);
2036         }
2037       break;
2038
2039     case DW_OP_GNU_parameter_ref:
2040       {
2041         unsigned long o;
2042         gcc_assert (val1->val_class == dw_val_class_die_ref);
2043         o = get_ref_die_offset (val1->v.val_die_ref.die);
2044         dw2_asm_output_data (4, o, NULL);
2045       }
2046       break;
2047
2048     default:
2049       /* Other codes have no operands.  */
2050       break;
2051     }
2052 }
2053
2054 /* Output a sequence of location operations.  
2055    The for_eh_or_skip parameter controls whether register numbers are
2056    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2057    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2058    info).  This should be suppressed for the cases that have not been converted
2059    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2060
2061 void
2062 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2063 {
2064   for (; loc != NULL; loc = loc->dw_loc_next)
2065     {
2066       enum dwarf_location_atom opc = loc->dw_loc_opc;
2067       /* Output the opcode.  */
2068       if (for_eh_or_skip >= 0 
2069           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2070         {
2071           unsigned r = (opc - DW_OP_breg0);
2072           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2073           gcc_assert (r <= 31);
2074           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2075         }
2076       else if (for_eh_or_skip >= 0 
2077                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2078         {
2079           unsigned r = (opc - DW_OP_reg0);
2080           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2081           gcc_assert (r <= 31);
2082           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2083         }
2084
2085       dw2_asm_output_data (1, opc,
2086                              "%s", dwarf_stack_op_name (opc));
2087
2088       /* Output the operand(s) (if any).  */
2089       output_loc_operands (loc, for_eh_or_skip);
2090     }
2091 }
2092
2093 /* Output location description stack opcode's operands (if any).
2094    The output is single bytes on a line, suitable for .cfi_escape.  */
2095
2096 static void
2097 output_loc_operands_raw (dw_loc_descr_ref loc)
2098 {
2099   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2100   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2101
2102   switch (loc->dw_loc_opc)
2103     {
2104     case DW_OP_addr:
2105     case DW_OP_GNU_addr_index:
2106     case DW_OP_GNU_const_index:
2107     case DW_OP_implicit_value:
2108       /* We cannot output addresses in .cfi_escape, only bytes.  */
2109       gcc_unreachable ();
2110
2111     case DW_OP_const1u:
2112     case DW_OP_const1s:
2113     case DW_OP_pick:
2114     case DW_OP_deref_size:
2115     case DW_OP_xderef_size:
2116       fputc (',', asm_out_file);
2117       dw2_asm_output_data_raw (1, val1->v.val_int);
2118       break;
2119
2120     case DW_OP_const2u:
2121     case DW_OP_const2s:
2122       fputc (',', asm_out_file);
2123       dw2_asm_output_data_raw (2, val1->v.val_int);
2124       break;
2125
2126     case DW_OP_const4u:
2127     case DW_OP_const4s:
2128       fputc (',', asm_out_file);
2129       dw2_asm_output_data_raw (4, val1->v.val_int);
2130       break;
2131
2132     case DW_OP_const8u:
2133     case DW_OP_const8s:
2134       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2135       fputc (',', asm_out_file);
2136       dw2_asm_output_data_raw (8, val1->v.val_int);
2137       break;
2138
2139     case DW_OP_skip:
2140     case DW_OP_bra:
2141       {
2142         int offset;
2143
2144         gcc_assert (val1->val_class == dw_val_class_loc);
2145         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2146
2147         fputc (',', asm_out_file);
2148         dw2_asm_output_data_raw (2, offset);
2149       }
2150       break;
2151
2152     case DW_OP_regx:
2153       {
2154         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2155         gcc_assert (size_of_uleb128 (r) 
2156                     == size_of_uleb128 (val1->v.val_unsigned));
2157         fputc (',', asm_out_file);
2158         dw2_asm_output_data_uleb128_raw (r);
2159       }
2160       break;
2161       
2162     case DW_OP_constu:
2163     case DW_OP_plus_uconst:
2164     case DW_OP_piece:
2165       fputc (',', asm_out_file);
2166       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2167       break;
2168
2169     case DW_OP_bit_piece:
2170       fputc (',', asm_out_file);
2171       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2172       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2173       break;
2174
2175     case DW_OP_consts:
2176     case DW_OP_breg0:
2177     case DW_OP_breg1:
2178     case DW_OP_breg2:
2179     case DW_OP_breg3:
2180     case DW_OP_breg4:
2181     case DW_OP_breg5:
2182     case DW_OP_breg6:
2183     case DW_OP_breg7:
2184     case DW_OP_breg8:
2185     case DW_OP_breg9:
2186     case DW_OP_breg10:
2187     case DW_OP_breg11:
2188     case DW_OP_breg12:
2189     case DW_OP_breg13:
2190     case DW_OP_breg14:
2191     case DW_OP_breg15:
2192     case DW_OP_breg16:
2193     case DW_OP_breg17:
2194     case DW_OP_breg18:
2195     case DW_OP_breg19:
2196     case DW_OP_breg20:
2197     case DW_OP_breg21:
2198     case DW_OP_breg22:
2199     case DW_OP_breg23:
2200     case DW_OP_breg24:
2201     case DW_OP_breg25:
2202     case DW_OP_breg26:
2203     case DW_OP_breg27:
2204     case DW_OP_breg28:
2205     case DW_OP_breg29:
2206     case DW_OP_breg30:
2207     case DW_OP_breg31:
2208     case DW_OP_fbreg:
2209       fputc (',', asm_out_file);
2210       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2211       break;
2212
2213     case DW_OP_bregx:
2214       {
2215         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2216         gcc_assert (size_of_uleb128 (r) 
2217                     == size_of_uleb128 (val1->v.val_unsigned));
2218         fputc (',', asm_out_file);
2219         dw2_asm_output_data_uleb128_raw (r);
2220         fputc (',', asm_out_file);
2221         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2222       }
2223       break;
2224
2225     case DW_OP_GNU_implicit_pointer:
2226     case DW_OP_GNU_entry_value:
2227     case DW_OP_GNU_const_type:
2228     case DW_OP_GNU_regval_type:
2229     case DW_OP_GNU_deref_type:
2230     case DW_OP_GNU_convert:
2231     case DW_OP_GNU_reinterpret:
2232     case DW_OP_GNU_parameter_ref:
2233       gcc_unreachable ();
2234       break;
2235
2236     default:
2237       /* Other codes have no operands.  */
2238       break;
2239     }
2240 }
2241
2242 void
2243 output_loc_sequence_raw (dw_loc_descr_ref loc)
2244 {
2245   while (1)
2246     {
2247       enum dwarf_location_atom opc = loc->dw_loc_opc;
2248       /* Output the opcode.  */
2249       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2250         {
2251           unsigned r = (opc - DW_OP_breg0);
2252           r = DWARF2_FRAME_REG_OUT (r, 1);
2253           gcc_assert (r <= 31);
2254           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2255         }
2256       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2257         {
2258           unsigned r = (opc - DW_OP_reg0);
2259           r = DWARF2_FRAME_REG_OUT (r, 1);
2260           gcc_assert (r <= 31);
2261           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2262         }
2263       /* Output the opcode.  */
2264       fprintf (asm_out_file, "%#x", opc);
2265       output_loc_operands_raw (loc);
2266
2267       if (!loc->dw_loc_next)
2268         break;
2269       loc = loc->dw_loc_next;
2270
2271       fputc (',', asm_out_file);
2272     }
2273 }
2274
2275 /* This function builds a dwarf location descriptor sequence from a
2276    dw_cfa_location, adding the given OFFSET to the result of the
2277    expression.  */
2278
2279 struct dw_loc_descr_struct *
2280 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2281 {
2282   struct dw_loc_descr_struct *head, *tmp;
2283
2284   offset += cfa->offset;
2285
2286   if (cfa->indirect)
2287     {
2288       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2289       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2290       head->dw_loc_oprnd1.val_entry = NULL;
2291       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2292       add_loc_descr (&head, tmp);
2293       if (offset != 0)
2294         {
2295           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2296           add_loc_descr (&head, tmp);
2297         }
2298     }
2299   else
2300     head = new_reg_loc_descr (cfa->reg, offset);
2301
2302   return head;
2303 }
2304
2305 /* This function builds a dwarf location descriptor sequence for
2306    the address at OFFSET from the CFA when stack is aligned to
2307    ALIGNMENT byte.  */
2308
2309 struct dw_loc_descr_struct *
2310 build_cfa_aligned_loc (dw_cfa_location *cfa,
2311                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2312 {
2313   struct dw_loc_descr_struct *head;
2314   unsigned int dwarf_fp
2315     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2316
2317   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2318   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2319     {
2320       head = new_reg_loc_descr (dwarf_fp, 0);
2321       add_loc_descr (&head, int_loc_descriptor (alignment));
2322       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2323       loc_descr_plus_const (&head, offset);
2324     }
2325   else
2326     head = new_reg_loc_descr (dwarf_fp, offset);
2327   return head;
2328 }
2329 \f
2330 /* And now, the support for symbolic debugging information.  */
2331
2332 /* .debug_str support.  */
2333 static int output_indirect_string (void **, void *);
2334
2335 static void dwarf2out_init (const char *);
2336 static void dwarf2out_finish (const char *);
2337 static void dwarf2out_assembly_start (void);
2338 static void dwarf2out_define (unsigned int, const char *);
2339 static void dwarf2out_undef (unsigned int, const char *);
2340 static void dwarf2out_start_source_file (unsigned, const char *);
2341 static void dwarf2out_end_source_file (unsigned);
2342 static void dwarf2out_function_decl (tree);
2343 static void dwarf2out_begin_block (unsigned, unsigned);
2344 static void dwarf2out_end_block (unsigned, unsigned);
2345 static bool dwarf2out_ignore_block (const_tree);
2346 static void dwarf2out_global_decl (tree);
2347 static void dwarf2out_type_decl (tree, int);
2348 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2349 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2350                                                  dw_die_ref);
2351 static void dwarf2out_abstract_function (tree);
2352 static void dwarf2out_var_location (rtx);
2353 static void dwarf2out_begin_function (tree);
2354 static void dwarf2out_end_function (unsigned int);
2355 static void dwarf2out_set_name (tree, tree);
2356
2357 /* The debug hooks structure.  */
2358
2359 const struct gcc_debug_hooks dwarf2_debug_hooks =
2360 {
2361   dwarf2out_init,
2362   dwarf2out_finish,
2363   dwarf2out_assembly_start,
2364   dwarf2out_define,
2365   dwarf2out_undef,
2366   dwarf2out_start_source_file,
2367   dwarf2out_end_source_file,
2368   dwarf2out_begin_block,
2369   dwarf2out_end_block,
2370   dwarf2out_ignore_block,
2371   dwarf2out_source_line,
2372   dwarf2out_begin_prologue,
2373 #if VMS_DEBUGGING_INFO
2374   dwarf2out_vms_end_prologue,
2375   dwarf2out_vms_begin_epilogue,
2376 #else
2377   debug_nothing_int_charstar,
2378   debug_nothing_int_charstar,
2379 #endif
2380   dwarf2out_end_epilogue,
2381   dwarf2out_begin_function,
2382   dwarf2out_end_function,       /* end_function */
2383   dwarf2out_function_decl,      /* function_decl */
2384   dwarf2out_global_decl,
2385   dwarf2out_type_decl,          /* type_decl */
2386   dwarf2out_imported_module_or_decl,
2387   debug_nothing_tree,           /* deferred_inline_function */
2388   /* The DWARF 2 backend tries to reduce debugging bloat by not
2389      emitting the abstract description of inline functions until
2390      something tries to reference them.  */
2391   dwarf2out_abstract_function,  /* outlining_inline_function */
2392   debug_nothing_rtx,            /* label */
2393   debug_nothing_int,            /* handle_pch */
2394   dwarf2out_var_location,
2395   dwarf2out_switch_text_section,
2396   dwarf2out_set_name,
2397   1,                            /* start_end_main_source_file */
2398   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2399 };
2400 \f
2401 /* NOTE: In the comments in this file, many references are made to
2402    "Debugging Information Entries".  This term is abbreviated as `DIE'
2403    throughout the remainder of this file.  */
2404
2405 /* An internal representation of the DWARF output is built, and then
2406    walked to generate the DWARF debugging info.  The walk of the internal
2407    representation is done after the entire program has been compiled.
2408    The types below are used to describe the internal representation.  */
2409
2410 /* Whether to put type DIEs into their own section .debug_types instead
2411    of making them part of the .debug_info section.  Only supported for
2412    Dwarf V4 or higher and the user didn't disable them through
2413    -fno-debug-types-section.  It is more efficient to put them in a
2414    separate comdat sections since the linker will then be able to
2415    remove duplicates.  But not all tools support .debug_types sections
2416    yet.  */
2417
2418 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2419
2420 /* Various DIE's use offsets relative to the beginning of the
2421    .debug_info section to refer to each other.  */
2422
2423 typedef long int dw_offset;
2424
2425 /* Define typedefs here to avoid circular dependencies.  */
2426
2427 typedef struct dw_attr_struct *dw_attr_ref;
2428 typedef struct dw_line_info_struct *dw_line_info_ref;
2429 typedef struct pubname_struct *pubname_ref;
2430 typedef struct dw_ranges_struct *dw_ranges_ref;
2431 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2432 typedef struct comdat_type_struct *comdat_type_node_ref;
2433
2434 /* The entries in the line_info table more-or-less mirror the opcodes
2435    that are used in the real dwarf line table.  Arrays of these entries
2436    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2437    supported.  */
2438
2439 enum dw_line_info_opcode {
2440   /* Emit DW_LNE_set_address; the operand is the label index.  */
2441   LI_set_address,
2442
2443   /* Emit a row to the matrix with the given line.  This may be done
2444      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2445      special opcodes.  */
2446   LI_set_line,
2447
2448   /* Emit a DW_LNS_set_file.  */
2449   LI_set_file,
2450
2451   /* Emit a DW_LNS_set_column.  */
2452   LI_set_column,
2453
2454   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2455   LI_negate_stmt,
2456
2457   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2458   LI_set_prologue_end,
2459   LI_set_epilogue_begin,
2460
2461   /* Emit a DW_LNE_set_discriminator.  */
2462   LI_set_discriminator
2463 };
2464
2465 typedef struct GTY(()) dw_line_info_struct {
2466   enum dw_line_info_opcode opcode;
2467   unsigned int val;
2468 } dw_line_info_entry;
2469
2470
2471 typedef struct GTY(()) dw_line_info_table_struct {
2472   /* The label that marks the end of this section.  */
2473   const char *end_label;
2474
2475   /* The values for the last row of the matrix, as collected in the table.
2476      These are used to minimize the changes to the next row.  */
2477   unsigned int file_num;
2478   unsigned int line_num;
2479   unsigned int column_num;
2480   int discrim_num;
2481   bool is_stmt;
2482   bool in_use;
2483
2484   vec<dw_line_info_entry, va_gc> *entries;
2485 } dw_line_info_table;
2486
2487 typedef dw_line_info_table *dw_line_info_table_p;
2488
2489
2490 /* Each DIE attribute has a field specifying the attribute kind,
2491    a link to the next attribute in the chain, and an attribute value.
2492    Attributes are typically linked below the DIE they modify.  */
2493
2494 typedef struct GTY(()) dw_attr_struct {
2495   enum dwarf_attribute dw_attr;
2496   dw_val_node dw_attr_val;
2497 }
2498 dw_attr_node;
2499
2500
2501 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2502    The children of each node form a circular list linked by
2503    die_sib.  die_child points to the node *before* the "first" child node.  */
2504
2505 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2506   union die_symbol_or_type_node
2507     {
2508       const char * GTY ((tag ("0"))) die_symbol;
2509       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2510     }
2511   GTY ((desc ("%0.comdat_type_p"))) die_id;
2512   vec<dw_attr_node, va_gc> *die_attr;
2513   dw_die_ref die_parent;
2514   dw_die_ref die_child;
2515   dw_die_ref die_sib;
2516   dw_die_ref die_definition; /* ref from a specification to its definition */
2517   dw_offset die_offset;
2518   unsigned long die_abbrev;
2519   int die_mark;
2520   unsigned int decl_id;
2521   enum dwarf_tag die_tag;
2522   /* Die is used and must not be pruned as unused.  */
2523   BOOL_BITFIELD die_perennial_p : 1;
2524   BOOL_BITFIELD comdat_type_p : 1; /* DIE has a type signature */
2525   /* Lots of spare bits.  */
2526 }
2527 die_node;
2528
2529 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2530 #define FOR_EACH_CHILD(die, c, expr) do {       \
2531   c = die->die_child;                           \
2532   if (c) do {                                   \
2533     c = c->die_sib;                             \
2534     expr;                                       \
2535   } while (c != die->die_child);                \
2536 } while (0)
2537
2538 /* The pubname structure */
2539
2540 typedef struct GTY(()) pubname_struct {
2541   dw_die_ref die;
2542   const char *name;
2543 }
2544 pubname_entry;
2545
2546
2547 struct GTY(()) dw_ranges_struct {
2548   /* If this is positive, it's a block number, otherwise it's a
2549      bitwise-negated index into dw_ranges_by_label.  */
2550   int num;
2551 };
2552
2553 /* A structure to hold a macinfo entry.  */
2554
2555 typedef struct GTY(()) macinfo_struct {
2556   unsigned char code;
2557   unsigned HOST_WIDE_INT lineno;
2558   const char *info;
2559 }
2560 macinfo_entry;
2561
2562
2563 struct GTY(()) dw_ranges_by_label_struct {
2564   const char *begin;
2565   const char *end;
2566 };
2567
2568 /* The comdat type node structure.  */
2569 typedef struct GTY(()) comdat_type_struct
2570 {
2571   dw_die_ref root_die;
2572   dw_die_ref type_die;
2573   dw_die_ref skeleton_die;
2574   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2575   struct comdat_type_struct *next;
2576 }
2577 comdat_type_node;
2578
2579 /* The limbo die list structure.  */
2580 typedef struct GTY(()) limbo_die_struct {
2581   dw_die_ref die;
2582   tree created_for;
2583   struct limbo_die_struct *next;
2584 }
2585 limbo_die_node;
2586
2587 typedef struct skeleton_chain_struct
2588 {
2589   dw_die_ref old_die;
2590   dw_die_ref new_die;
2591   struct skeleton_chain_struct *parent;
2592 }
2593 skeleton_chain_node;
2594
2595 /* Define a macro which returns nonzero for a TYPE_DECL which was
2596    implicitly generated for a type.
2597
2598    Note that, unlike the C front-end (which generates a NULL named
2599    TYPE_DECL node for each complete tagged type, each array type,
2600    and each function type node created) the C++ front-end generates
2601    a _named_ TYPE_DECL node for each tagged type node created.
2602    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2603    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2604    front-end, but for each type, tagged or not.  */
2605
2606 #define TYPE_DECL_IS_STUB(decl)                         \
2607   (DECL_NAME (decl) == NULL_TREE                        \
2608    || (DECL_ARTIFICIAL (decl)                           \
2609        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2610            /* This is necessary for stub decls that     \
2611               appear in nested inline functions.  */    \
2612            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2613                && (decl_ultimate_origin (decl)          \
2614                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2615
2616 /* Information concerning the compilation unit's programming
2617    language, and compiler version.  */
2618
2619 /* Fixed size portion of the DWARF compilation unit header.  */
2620 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2621   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2622
2623 /* Fixed size portion of the DWARF comdat type unit header.  */
2624 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2625   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2626    + DWARF_OFFSET_SIZE)
2627
2628 /* Fixed size portion of public names info.  */
2629 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2630
2631 /* Fixed size portion of the address range info.  */
2632 #define DWARF_ARANGES_HEADER_SIZE                                       \
2633   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2634                 DWARF2_ADDR_SIZE * 2)                                   \
2635    - DWARF_INITIAL_LENGTH_SIZE)
2636
2637 /* Size of padding portion in the address range info.  It must be
2638    aligned to twice the pointer size.  */
2639 #define DWARF_ARANGES_PAD_SIZE \
2640   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2641                 DWARF2_ADDR_SIZE * 2)                              \
2642    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2643
2644 /* Use assembler line directives if available.  */
2645 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2646 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2647 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2648 #else
2649 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2650 #endif
2651 #endif
2652
2653 /* Minimum line offset in a special line info. opcode.
2654    This value was chosen to give a reasonable range of values.  */
2655 #define DWARF_LINE_BASE  -10
2656
2657 /* First special line opcode - leave room for the standard opcodes.  */
2658 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2659
2660 /* Range of line offsets in a special line info. opcode.  */
2661 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2662
2663 /* Flag that indicates the initial value of the is_stmt_start flag.
2664    In the present implementation, we do not mark any lines as
2665    the beginning of a source statement, because that information
2666    is not made available by the GCC front-end.  */
2667 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2668
2669 /* Maximum number of operations per instruction bundle.  */
2670 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2671 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2672 #endif
2673
2674 /* This location is used by calc_die_sizes() to keep track
2675    the offset of each DIE within the .debug_info section.  */
2676 static unsigned long next_die_offset;
2677
2678 /* Record the root of the DIE's built for the current compilation unit.  */
2679 static GTY(()) dw_die_ref single_comp_unit_die;
2680
2681 /* A list of type DIEs that have been separated into comdat sections.  */
2682 static GTY(()) comdat_type_node *comdat_type_list;
2683
2684 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2685 static GTY(()) limbo_die_node *limbo_die_list;
2686
2687 /* A list of DIEs for which we may have to generate
2688    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2689 static GTY(()) limbo_die_node *deferred_asm_name;
2690
2691 /* Filenames referenced by this compilation unit.  */
2692 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2693
2694 /* A hash table of references to DIE's that describe declarations.
2695    The key is a DECL_UID() which is a unique number identifying each decl.  */
2696 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2697
2698 /* A hash table of references to DIE's that describe COMMON blocks.
2699    The key is DECL_UID() ^ die_parent.  */
2700 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2701
2702 typedef struct GTY(()) die_arg_entry_struct {
2703     dw_die_ref die;
2704     tree arg;
2705 } die_arg_entry;
2706
2707
2708 /* Node of the variable location list.  */
2709 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2710   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2711      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2712      in mode of the EXPR_LIST node and first EXPR_LIST operand
2713      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2714      location or NULL for padding.  For larger bitsizes,
2715      mode is 0 and first operand is a CONCAT with bitsize
2716      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2717      NULL as second operand.  */
2718   rtx GTY (()) loc;
2719   const char * GTY (()) label;
2720   struct var_loc_node * GTY (()) next;
2721 };
2722
2723 /* Variable location list.  */
2724 struct GTY (()) var_loc_list_def {
2725   struct var_loc_node * GTY (()) first;
2726
2727   /* Pointer to the last but one or last element of the
2728      chained list.  If the list is empty, both first and
2729      last are NULL, if the list contains just one node
2730      or the last node certainly is not redundant, it points
2731      to the last node, otherwise points to the last but one.
2732      Do not mark it for GC because it is marked through the chain.  */
2733   struct var_loc_node * GTY ((skip ("%h"))) last;
2734
2735   /* Pointer to the last element before section switch,
2736      if NULL, either sections weren't switched or first
2737      is after section switch.  */
2738   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2739
2740   /* DECL_UID of the variable decl.  */
2741   unsigned int decl_id;
2742 };
2743 typedef struct var_loc_list_def var_loc_list;
2744
2745 /* Call argument location list.  */
2746 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2747   rtx GTY (()) call_arg_loc_note;
2748   const char * GTY (()) label;
2749   tree GTY (()) block;
2750   bool tail_call_p;
2751   rtx GTY (()) symbol_ref;
2752   struct call_arg_loc_node * GTY (()) next;
2753 };
2754
2755
2756 /* Table of decl location linked lists.  */
2757 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2758
2759 /* Head and tail of call_arg_loc chain.  */
2760 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2761 static struct call_arg_loc_node *call_arg_loc_last;
2762
2763 /* Number of call sites in the current function.  */
2764 static int call_site_count = -1;
2765 /* Number of tail call sites in the current function.  */
2766 static int tail_call_site_count = -1;
2767
2768 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2769    DIEs.  */
2770 static vec<dw_die_ref> block_map;
2771
2772 /* A cached location list.  */
2773 struct GTY (()) cached_dw_loc_list_def {
2774   /* The DECL_UID of the decl that this entry describes.  */
2775   unsigned int decl_id;
2776
2777   /* The cached location list.  */
2778   dw_loc_list_ref loc_list;
2779 };
2780 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
2781
2782 /* Table of cached location lists.  */
2783 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
2784
2785 /* A pointer to the base of a list of references to DIE's that
2786    are uniquely identified by their tag, presence/absence of
2787    children DIE's, and list of attribute/value pairs.  */
2788 static GTY((length ("abbrev_die_table_allocated")))
2789   dw_die_ref *abbrev_die_table;
2790
2791 /* Number of elements currently allocated for abbrev_die_table.  */
2792 static GTY(()) unsigned abbrev_die_table_allocated;
2793
2794 /* Number of elements in type_die_table currently in use.  */
2795 static GTY(()) unsigned abbrev_die_table_in_use;
2796
2797 /* Size (in elements) of increments by which we may expand the
2798    abbrev_die_table.  */
2799 #define ABBREV_DIE_TABLE_INCREMENT 256
2800
2801 /* A global counter for generating labels for line number data.  */
2802 static unsigned int line_info_label_num;
2803
2804 /* The current table to which we should emit line number information
2805    for the current function.  This will be set up at the beginning of
2806    assembly for the function.  */
2807 static dw_line_info_table *cur_line_info_table;
2808
2809 /* The two default tables of line number info.  */
2810 static GTY(()) dw_line_info_table *text_section_line_info;
2811 static GTY(()) dw_line_info_table *cold_text_section_line_info;
2812
2813 /* The set of all non-default tables of line number info.  */
2814 static GTY(()) vec<dw_line_info_table_p, va_gc> *separate_line_info;
2815
2816 /* A flag to tell pubnames/types export if there is an info section to
2817    refer to.  */
2818 static bool info_section_emitted;
2819
2820 /* A pointer to the base of a table that contains a list of publicly
2821    accessible names.  */
2822 static GTY (()) vec<pubname_entry, va_gc> *pubname_table;
2823
2824 /* A pointer to the base of a table that contains a list of publicly
2825    accessible types.  */
2826 static GTY (()) vec<pubname_entry, va_gc> *pubtype_table;
2827
2828 /* A pointer to the base of a table that contains a list of macro
2829    defines/undefines (and file start/end markers).  */
2830 static GTY (()) vec<macinfo_entry, va_gc> *macinfo_table;
2831
2832 /* True if .debug_macinfo or .debug_macros section is going to be
2833    emitted.  */
2834 #define have_macinfo \
2835   (debug_info_level >= DINFO_LEVEL_VERBOSE \
2836    && !macinfo_table->is_empty ())
2837
2838 /* Array of dies for which we should generate .debug_ranges info.  */
2839 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
2840
2841 /* Number of elements currently allocated for ranges_table.  */
2842 static GTY(()) unsigned ranges_table_allocated;
2843
2844 /* Number of elements in ranges_table currently in use.  */
2845 static GTY(()) unsigned ranges_table_in_use;
2846
2847 /* Array of pairs of labels referenced in ranges_table.  */
2848 static GTY ((length ("ranges_by_label_allocated")))
2849      dw_ranges_by_label_ref ranges_by_label;
2850
2851 /* Number of elements currently allocated for ranges_by_label.  */
2852 static GTY(()) unsigned ranges_by_label_allocated;
2853
2854 /* Number of elements in ranges_by_label currently in use.  */
2855 static GTY(()) unsigned ranges_by_label_in_use;
2856
2857 /* Size (in elements) of increments by which we may expand the
2858    ranges_table.  */
2859 #define RANGES_TABLE_INCREMENT 64
2860
2861 /* Whether we have location lists that need outputting */
2862 static GTY(()) bool have_location_lists;
2863
2864 /* Unique label counter.  */
2865 static GTY(()) unsigned int loclabel_num;
2866
2867 /* Unique label counter for point-of-call tables.  */
2868 static GTY(()) unsigned int poc_label_num;
2869
2870 /* Record whether the function being analyzed contains inlined functions.  */
2871 static int current_function_has_inlines;
2872
2873 /* The last file entry emitted by maybe_emit_file().  */
2874 static GTY(()) struct dwarf_file_data * last_emitted_file;
2875
2876 /* Number of internal labels generated by gen_internal_sym().  */
2877 static GTY(()) int label_num;
2878
2879 /* Cached result of previous call to lookup_filename.  */
2880 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
2881
2882 static GTY(()) vec<die_arg_entry, va_gc> *tmpl_value_parm_die_table;
2883
2884 /* Instances of generic types for which we need to generate debug
2885    info that describe their generic parameters and arguments. That
2886    generation needs to happen once all types are properly laid out so
2887    we do it at the end of compilation.  */
2888 static GTY(()) vec<tree, va_gc> *generic_type_instances;
2889
2890 /* Offset from the "steady-state frame pointer" to the frame base,
2891    within the current function.  */
2892 static HOST_WIDE_INT frame_pointer_fb_offset;
2893 static bool frame_pointer_fb_offset_valid;
2894
2895 static vec<dw_die_ref> base_types;
2896
2897 /* Forward declarations for functions defined in this file.  */
2898
2899 static int is_pseudo_reg (const_rtx);
2900 static tree type_main_variant (tree);
2901 static int is_tagged_type (const_tree);
2902 static const char *dwarf_tag_name (unsigned);
2903 static const char *dwarf_attr_name (unsigned);
2904 static const char *dwarf_form_name (unsigned);
2905 static tree decl_ultimate_origin (const_tree);
2906 static tree decl_class_context (tree);
2907 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
2908 static inline enum dw_val_class AT_class (dw_attr_ref);
2909 static inline unsigned int AT_index (dw_attr_ref);
2910 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
2911 static inline unsigned AT_flag (dw_attr_ref);
2912 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
2913 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
2914 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
2915 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
2916 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
2917                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2918 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
2919                                unsigned int, unsigned char *);
2920 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
2921 static hashval_t debug_str_do_hash (const void *);
2922 static int debug_str_eq (const void *, const void *);
2923 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
2924 static inline const char *AT_string (dw_attr_ref);
2925 static enum dwarf_form AT_string_form (dw_attr_ref);
2926 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
2927 static void add_AT_specification (dw_die_ref, dw_die_ref);
2928 static inline dw_die_ref AT_ref (dw_attr_ref);
2929 static inline int AT_ref_external (dw_attr_ref);
2930 static inline void set_AT_ref_external (dw_attr_ref, int);
2931 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
2932 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
2933 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
2934 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
2935                              dw_loc_list_ref);
2936 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
2937 static addr_table_entry *add_addr_table_entry (void *, enum ate_kind);
2938 static void remove_addr_table_entry (addr_table_entry *);
2939 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx, bool);
2940 static inline rtx AT_addr (dw_attr_ref);
2941 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
2942 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
2943 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
2944 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
2945                            unsigned HOST_WIDE_INT);
2946 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
2947                                unsigned long, bool);
2948 static inline const char *AT_lbl (dw_attr_ref);
2949 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
2950 static const char *get_AT_low_pc (dw_die_ref);
2951 static const char *get_AT_hi_pc (dw_die_ref);
2952 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
2953 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
2954 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
2955 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
2956 static bool is_cxx (void);
2957 static bool is_fortran (void);
2958 static bool is_ada (void);
2959 static void remove_AT (dw_die_ref, enum dwarf_attribute);
2960 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
2961 static void add_child_die (dw_die_ref, dw_die_ref);
2962 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
2963 static dw_die_ref lookup_type_die (tree);
2964 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
2965 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
2966 static void equate_type_number_to_die (tree, dw_die_ref);
2967 static hashval_t decl_die_table_hash (const void *);
2968 static int decl_die_table_eq (const void *, const void *);
2969 static dw_die_ref lookup_decl_die (tree);
2970 static hashval_t common_block_die_table_hash (const void *);
2971 static int common_block_die_table_eq (const void *, const void *);
2972 static hashval_t decl_loc_table_hash (const void *);
2973 static int decl_loc_table_eq (const void *, const void *);
2974 static var_loc_list *lookup_decl_loc (const_tree);
2975 static void equate_decl_number_to_die (tree, dw_die_ref);
2976 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
2977 static void print_spaces (FILE *);
2978 static void print_die (dw_die_ref, FILE *);
2979 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
2980 static dw_die_ref pop_compile_unit (dw_die_ref);
2981 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
2982 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
2983 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
2984 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
2985 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
2986 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
2987 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
2988                                    struct md5_ctx *, int *);
2989 struct checksum_attributes;
2990 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
2991 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
2992 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
2993 static void generate_type_signature (dw_die_ref, comdat_type_node *);
2994 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
2995 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
2996 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
2997 static int same_die_p (dw_die_ref, dw_die_ref, int *);
2998 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
2999 static void compute_section_prefix (dw_die_ref);
3000 static int is_type_die (dw_die_ref);
3001 static int is_comdat_die (dw_die_ref);
3002 static int is_symbol_die (dw_die_ref);
3003 static void assign_symbol_names (dw_die_ref);
3004 static void break_out_includes (dw_die_ref);
3005 static int is_declaration_die (dw_die_ref);
3006 static int should_move_die_to_comdat (dw_die_ref);
3007 static dw_die_ref clone_as_declaration (dw_die_ref);
3008 static dw_die_ref clone_die (dw_die_ref);
3009 static dw_die_ref clone_tree (dw_die_ref);
3010 static dw_die_ref copy_declaration_context (dw_die_ref, dw_die_ref);
3011 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3012 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3013 static dw_die_ref generate_skeleton (dw_die_ref);
3014 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3015                                                          dw_die_ref,
3016                                                          dw_die_ref);
3017 static void break_out_comdat_types (dw_die_ref);
3018 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3019 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3020 static void copy_decls_for_unworthy_types (dw_die_ref);
3021
3022 static hashval_t htab_cu_hash (const void *);
3023 static int htab_cu_eq (const void *, const void *);
3024 static void htab_cu_del (void *);
3025 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3026 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3027 static void add_sibling_attributes (dw_die_ref);
3028 static void build_abbrev_table (dw_die_ref, htab_t);
3029 static void output_location_lists (dw_die_ref);
3030 static int constant_size (unsigned HOST_WIDE_INT);
3031 static unsigned long size_of_die (dw_die_ref);
3032 static void calc_die_sizes (dw_die_ref);
3033 static void calc_base_type_die_sizes (void);
3034 static void mark_dies (dw_die_ref);
3035 static void unmark_dies (dw_die_ref);
3036 static void unmark_all_dies (dw_die_ref);
3037 static unsigned long size_of_pubnames (vec<pubname_entry, va_gc> *);
3038 static unsigned long size_of_aranges (void);
3039 static enum dwarf_form value_format (dw_attr_ref);
3040 static void output_value_format (dw_attr_ref);
3041 static void output_abbrev_section (void);
3042 static void output_die_abbrevs (unsigned long, dw_die_ref);
3043 static void output_die_symbol (dw_die_ref);
3044 static void output_die (dw_die_ref);
3045 static void output_compilation_unit_header (void);
3046 static void output_comp_unit (dw_die_ref, int);
3047 static void output_comdat_type_unit (comdat_type_node *);
3048 static const char *dwarf2_name (tree, int);
3049 static void add_pubname (tree, dw_die_ref);
3050 static void add_enumerator_pubname (const char *, dw_die_ref);
3051 static void add_pubname_string (const char *, dw_die_ref);
3052 static void add_pubtype (tree, dw_die_ref);
3053 static void output_pubnames (vec<pubname_entry, va_gc> *);
3054 static void output_aranges (unsigned long);
3055 static unsigned int add_ranges_num (int);
3056 static unsigned int add_ranges (const_tree);
3057 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3058                                   bool *, bool);
3059 static void output_ranges (void);
3060 static dw_line_info_table *new_line_info_table (void);
3061 static void output_line_info (bool);
3062 static void output_file_names (void);
3063 static dw_die_ref base_type_die (tree);
3064 static int is_base_type (tree);
3065 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3066 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3067 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3068 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3069 static int type_is_enum (const_tree);
3070 static unsigned int dbx_reg_number (const_rtx);
3071 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3072 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3073 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3074                                                 enum var_init_status);
3075 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3076                                                      enum var_init_status);
3077 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3078                                          enum var_init_status);
3079 static int is_based_loc (const_rtx);
3080 static int resolve_one_addr (rtx *, void *);
3081 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3082                                                enum var_init_status);
3083 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3084                                         enum var_init_status);
3085 static dw_loc_list_ref loc_list_from_tree (tree, int);
3086 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3087 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3088 static tree field_type (const_tree);
3089 static unsigned int simple_type_align_in_bits (const_tree);
3090 static unsigned int simple_decl_align_in_bits (const_tree);
3091 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3092 static HOST_WIDE_INT field_byte_offset (const_tree);
3093 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3094                                          dw_loc_list_ref);
3095 static void add_data_member_location_attribute (dw_die_ref, tree);
3096 static bool add_const_value_attribute (dw_die_ref, rtx);
3097 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3098 static void insert_double (double_int, unsigned char *);
3099 static void insert_float (const_rtx, unsigned char *);
3100 static rtx rtl_for_decl_location (tree);
3101 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3102                                                    enum dwarf_attribute);
3103 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3104 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3105 static void add_name_attribute (dw_die_ref, const char *);
3106 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3107 static void add_comp_dir_attribute (dw_die_ref);
3108 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3109 static void add_subscript_info (dw_die_ref, tree, bool);
3110 static void add_byte_size_attribute (dw_die_ref, tree);
3111 static void add_bit_offset_attribute (dw_die_ref, tree);
3112 static void add_bit_size_attribute (dw_die_ref, tree);
3113 static void add_prototyped_attribute (dw_die_ref, tree);
3114 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3115 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3116 static void add_src_coords_attributes (dw_die_ref, tree);
3117 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3118 static void push_decl_scope (tree);
3119 static void pop_decl_scope (void);
3120 static dw_die_ref scope_die_for (tree, dw_die_ref);
3121 static inline int local_scope_p (dw_die_ref);
3122 static inline int class_scope_p (dw_die_ref);
3123 static inline int class_or_namespace_scope_p (dw_die_ref);
3124 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3125 static void add_calling_convention_attribute (dw_die_ref, tree);
3126 static const char *type_tag (const_tree);
3127 static tree member_declared_type (const_tree);
3128 #if 0
3129 static const char *decl_start_label (tree);
3130 #endif
3131 static void gen_array_type_die (tree, dw_die_ref);
3132 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3133 #if 0
3134 static void gen_entry_point_die (tree, dw_die_ref);
3135 #endif
3136 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3137 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3138 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3139 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3140 static void gen_formal_types_die (tree, dw_die_ref);
3141 static void gen_subprogram_die (tree, dw_die_ref);
3142 static void gen_variable_die (tree, tree, dw_die_ref);
3143 static void gen_const_die (tree, dw_die_ref);
3144 static void gen_label_die (tree, dw_die_ref);
3145 static void gen_lexical_block_die (tree, dw_die_ref, int);
3146 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3147 static void gen_field_die (tree, dw_die_ref);
3148 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3149 static dw_die_ref gen_compile_unit_die (const char *);
3150 static void gen_inheritance_die (tree, tree, dw_die_ref);
3151 static void gen_member_die (tree, dw_die_ref);
3152 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3153                                                 enum debug_info_usage);
3154 static void gen_subroutine_type_die (tree, dw_die_ref);
3155 static void gen_typedef_die (tree, dw_die_ref);
3156 static void gen_type_die (tree, dw_die_ref);
3157 static void gen_block_die (tree, dw_die_ref, int);
3158 static void decls_for_scope (tree, dw_die_ref, int);
3159 static inline int is_redundant_typedef (const_tree);
3160 static bool is_naming_typedef_decl (const_tree);
3161 static inline dw_die_ref get_context_die (tree);
3162 static void gen_namespace_die (tree, dw_die_ref);
3163 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3164 static dw_die_ref force_decl_die (tree);
3165 static dw_die_ref force_type_die (tree);
3166 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3167 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3168 static struct dwarf_file_data * lookup_filename (const char *);
3169 static void retry_incomplete_types (void);
3170 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3171 static void gen_generic_params_dies (tree);
3172 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3173 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3174 static void splice_child_die (dw_die_ref, dw_die_ref);
3175 static int file_info_cmp (const void *, const void *);
3176 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3177                                      const char *, const char *);
3178 static void output_loc_list (dw_loc_list_ref);
3179 static char *gen_internal_sym (const char *);
3180 static bool want_pubnames (void);
3181
3182 static void prune_unmark_dies (dw_die_ref);
3183 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3184 static void prune_unused_types_mark (dw_die_ref, int);
3185 static void prune_unused_types_walk (dw_die_ref);
3186 static void prune_unused_types_walk_attribs (dw_die_ref);
3187 static void prune_unused_types_prune (dw_die_ref);
3188 static void prune_unused_types (void);
3189 static int maybe_emit_file (struct dwarf_file_data *fd);
3190 static inline const char *AT_vms_delta1 (dw_attr_ref);
3191 static inline const char *AT_vms_delta2 (dw_attr_ref);
3192 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3193                                      const char *, const char *);
3194 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3195 static void gen_remaining_tmpl_value_param_die_attribute (void);
3196 static bool generic_type_p (tree);
3197 static void schedule_generic_params_dies_gen (tree t);
3198 static void gen_scheduled_generic_parms_dies (void);
3199
3200 /* enum for tracking thread-local variables whose address is really an offset
3201    relative to the TLS pointer, which will need link-time relocation, but will
3202    not need relocation by the DWARF consumer.  */
3203
3204 enum dtprel_bool
3205 {
3206   dtprel_false = 0,
3207   dtprel_true = 1
3208 };
3209
3210 /* Return the operator to use for an address of a variable.  For dtprel_true, we
3211    use DW_OP_const*.  For regular variables, which need both link-time
3212    relocation and consumer-level relocation (e.g., to account for shared objects
3213    loaded at a random address), we use DW_OP_addr*.  */
3214
3215 static inline enum dwarf_location_atom
3216 dw_addr_op (enum dtprel_bool dtprel)
3217 {
3218   if (dtprel == dtprel_true)
3219     return (dwarf_split_debug_info ? DW_OP_GNU_const_index
3220             : (DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u));
3221   else
3222     return dwarf_split_debug_info ? DW_OP_GNU_addr_index : DW_OP_addr;
3223 }
3224
3225 /* Return a pointer to a newly allocated address location description.  If
3226    dwarf_split_debug_info is true, then record the address with the appropriate
3227    relocation.  */
3228 static inline dw_loc_descr_ref
3229 new_addr_loc_descr (rtx addr, enum dtprel_bool dtprel)
3230 {
3231   dw_loc_descr_ref ref = new_loc_descr (dw_addr_op (dtprel), 0, 0);
3232
3233   ref->dw_loc_oprnd1.val_class = dw_val_class_addr;
3234   ref->dw_loc_oprnd1.v.val_addr = addr;
3235   ref->dtprel = dtprel;
3236   if (dwarf_split_debug_info)
3237     ref->dw_loc_oprnd1.val_entry
3238         = add_addr_table_entry (addr,
3239                                 dtprel ? ate_kind_rtx_dtprel : ate_kind_rtx);
3240   else
3241     ref->dw_loc_oprnd1.val_entry = NULL;
3242
3243   return ref;
3244 }
3245
3246 /* Section names used to hold DWARF debugging information.  */
3247
3248 #ifndef DEBUG_INFO_SECTION
3249 #define DEBUG_INFO_SECTION      ".debug_info"
3250 #endif
3251 #ifndef DEBUG_DWO_INFO_SECTION
3252 #define DEBUG_DWO_INFO_SECTION ".debug_info.dwo"
3253 #endif
3254 #ifndef DEBUG_ABBREV_SECTION
3255 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3256 #endif
3257 #ifndef DEBUG_DWO_ABBREV_SECTION
3258 #define DEBUG_DWO_ABBREV_SECTION ".debug_abbrev.dwo"
3259 #endif
3260 #ifndef DEBUG_ARANGES_SECTION
3261 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3262 #endif
3263 #ifndef DEBUG_ADDR_SECTION
3264 #define DEBUG_ADDR_SECTION     ".debug_addr"
3265 #endif
3266 #ifndef DEBUG_NORM_MACINFO_SECTION
3267 #define DEBUG_NORM_MACINFO_SECTION     ".debug_macinfo"
3268 #endif
3269 #ifndef DEBUG_DWO_MACINFO_SECTION
3270 #define DEBUG_DWO_MACINFO_SECTION      ".debug_macinfo.dwo"
3271 #endif
3272 #ifndef DEBUG_MACINFO_SECTION
3273 #define DEBUG_MACINFO_SECTION                                           \
3274   (!dwarf_split_debug_info                                              \
3275    ? (DEBUG_NORM_MACINFO_SECTION) : (DEBUG_DWO_MACINFO_SECTION))
3276 #endif
3277 #ifndef DEBUG_NORM_MACRO_SECTION
3278 #define DEBUG_NORM_MACRO_SECTION ".debug_macro"
3279 #endif
3280 #ifndef DEBUG_DWO_MACRO_SECTION
3281 #define DEBUG_DWO_MACRO_SECTION        ".debug_macro.dwo"
3282 #endif
3283 #ifndef DEBUG_MACRO_SECTION
3284 #define DEBUG_MACRO_SECTION                                             \
3285   (!dwarf_split_debug_info                                              \
3286    ? (DEBUG_NORM_MACRO_SECTION) : (DEBUG_DWO_MACRO_SECTION))
3287 #endif
3288 #ifndef DEBUG_LINE_SECTION
3289 #define DEBUG_LINE_SECTION      ".debug_line"
3290 #endif
3291 #ifndef DEBUG_DWO_LINE_SECTION
3292 #define DEBUG_DWO_LINE_SECTION ".debug_line.dwo"
3293 #endif
3294 #ifndef DEBUG_LOC_SECTION
3295 #define DEBUG_LOC_SECTION       ".debug_loc"
3296 #endif
3297 #ifndef DEBUG_DWO_LOC_SECTION
3298 #define DEBUG_DWO_LOC_SECTION  ".debug_loc.dwo"
3299 #endif
3300 #ifndef DEBUG_PUBNAMES_SECTION
3301 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3302 #endif
3303 #ifndef DEBUG_PUBTYPES_SECTION
3304 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3305 #endif
3306 #define DEBUG_NORM_STR_OFFSETS_SECTION ".debug_str_offsets"
3307 #define DEBUG_DWO_STR_OFFSETS_SECTION ".debug_str_offsets.dwo"
3308 #ifndef DEBUG_STR_OFFSETS_SECTION
3309 #define DEBUG_STR_OFFSETS_SECTION                                       \
3310   (!dwarf_split_debug_info                                              \
3311    ? (DEBUG_NORM_STR_OFFSETS_SECTION) : (DEBUG_DWO_STR_OFFSETS_SECTION))
3312 #endif
3313 #define DEBUG_DWO_STR_SECTION   ".debug_str.dwo"
3314 #define DEBUG_NORM_STR_SECTION  ".debug_str"
3315 #ifndef DEBUG_STR_SECTION
3316 #define DEBUG_STR_SECTION                               \
3317   (!dwarf_split_debug_info ? (DEBUG_NORM_STR_SECTION) : (DEBUG_DWO_STR_SECTION))
3318 #endif
3319 #ifndef DEBUG_RANGES_SECTION
3320 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3321 #endif
3322
3323 /* Standard ELF section names for compiled code and data.  */
3324 #ifndef TEXT_SECTION_NAME
3325 #define TEXT_SECTION_NAME       ".text"
3326 #endif
3327
3328 /* Section flags for .debug_macinfo/.debug_macro section.  */
3329 #define DEBUG_MACRO_SECTION_FLAGS \
3330   (dwarf_split_debug_info ? SECTION_DEBUG | SECTION_EXCLUDE : SECTION_DEBUG)
3331
3332 /* Section flags for .debug_str section.  */
3333 #define DEBUG_STR_SECTION_FLAGS \
3334   (dwarf_split_debug_info \
3335    ? SECTION_DEBUG | SECTION_EXCLUDE \
3336    : (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings \
3337       ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3338       : SECTION_DEBUG))
3339
3340 /* Labels we insert at beginning sections we can reference instead of
3341    the section names themselves.  */
3342
3343 #ifndef TEXT_SECTION_LABEL
3344 #define TEXT_SECTION_LABEL                 "Ltext"
3345 #endif
3346 #ifndef COLD_TEXT_SECTION_LABEL
3347 #define COLD_TEXT_SECTION_LABEL             "Ltext_cold"
3348 #endif
3349 #ifndef DEBUG_LINE_SECTION_LABEL
3350 #define DEBUG_LINE_SECTION_LABEL           "Ldebug_line"
3351 #endif
3352 #ifndef DEBUG_SKELETON_LINE_SECTION_LABEL
3353 #define DEBUG_SKELETON_LINE_SECTION_LABEL   "Lskeleton_debug_line"
3354 #endif
3355 #ifndef DEBUG_INFO_SECTION_LABEL
3356 #define DEBUG_INFO_SECTION_LABEL           "Ldebug_info"
3357 #endif
3358 #ifndef DEBUG_SKELETON_INFO_SECTION_LABEL
3359 #define DEBUG_SKELETON_INFO_SECTION_LABEL   "Lskeleton_debug_info"
3360 #endif
3361 #ifndef DEBUG_ABBREV_SECTION_LABEL
3362 #define DEBUG_ABBREV_SECTION_LABEL         "Ldebug_abbrev"
3363 #endif
3364 #ifndef DEBUG_SKELETON_ABBREV_SECTION_LABEL
3365 #define DEBUG_SKELETON_ABBREV_SECTION_LABEL "Lskeleton_debug_abbrev"
3366 #endif
3367 #ifndef DEBUG_ADDR_SECTION_LABEL
3368 #define DEBUG_ADDR_SECTION_LABEL           "Ldebug_addr"
3369 #endif
3370 #ifndef DEBUG_LOC_SECTION_LABEL
3371 #define DEBUG_LOC_SECTION_LABEL                    "Ldebug_loc"
3372 #endif
3373 #ifndef DEBUG_RANGES_SECTION_LABEL
3374 #define DEBUG_RANGES_SECTION_LABEL         "Ldebug_ranges"
3375 #endif
3376 #ifndef DEBUG_MACINFO_SECTION_LABEL
3377 #define DEBUG_MACINFO_SECTION_LABEL         "Ldebug_macinfo"
3378 #endif
3379 #ifndef DEBUG_MACRO_SECTION_LABEL
3380 #define DEBUG_MACRO_SECTION_LABEL          "Ldebug_macro"
3381 #endif
3382 #define SKELETON_COMP_DIE_ABBREV 1
3383 #define SKELETON_TYPE_DIE_ABBREV 2
3384
3385 /* Definitions of defaults for formats and names of various special
3386    (artificial) labels which may be generated within this file (when the -g
3387    options is used and DWARF2_DEBUGGING_INFO is in effect.
3388    If necessary, these may be overridden from within the tm.h file, but
3389    typically, overriding these defaults is unnecessary.  */
3390
3391 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3392 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3393 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3394 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3395 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3396 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3397 static char debug_skeleton_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3398 static char debug_skeleton_abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3399 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3400 static char debug_addr_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3401 static char debug_skeleton_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3402 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3403 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3404 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3405
3406 #ifndef TEXT_END_LABEL
3407 #define TEXT_END_LABEL          "Letext"
3408 #endif
3409 #ifndef COLD_END_LABEL
3410 #define COLD_END_LABEL          "Letext_cold"
3411 #endif
3412 #ifndef BLOCK_BEGIN_LABEL
3413 #define BLOCK_BEGIN_LABEL       "LBB"
3414 #endif
3415 #ifndef BLOCK_END_LABEL
3416 #define BLOCK_END_LABEL         "LBE"
3417 #endif
3418 #ifndef LINE_CODE_LABEL
3419 #define LINE_CODE_LABEL         "LM"
3420 #endif
3421
3422 \f
3423 /* Return the root of the DIE's built for the current compilation unit.  */
3424 static dw_die_ref
3425 comp_unit_die (void)
3426 {
3427   if (!single_comp_unit_die)
3428     single_comp_unit_die = gen_compile_unit_die (NULL);
3429   return single_comp_unit_die;
3430 }
3431
3432 /* We allow a language front-end to designate a function that is to be
3433    called to "demangle" any name before it is put into a DIE.  */
3434
3435 static const char *(*demangle_name_func) (const char *);
3436
3437 void
3438 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3439 {
3440   demangle_name_func = func;
3441 }
3442
3443 /* Test if rtl node points to a pseudo register.  */
3444
3445 static inline int
3446 is_pseudo_reg (const_rtx rtl)
3447 {
3448   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3449           || (GET_CODE (rtl) == SUBREG
3450               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3451 }
3452
3453 /* Return a reference to a type, with its const and volatile qualifiers
3454    removed.  */
3455
3456 static inline tree
3457 type_main_variant (tree type)
3458 {
3459   type = TYPE_MAIN_VARIANT (type);
3460
3461   /* ??? There really should be only one main variant among any group of
3462      variants of a given type (and all of the MAIN_VARIANT values for all
3463      members of the group should point to that one type) but sometimes the C
3464      front-end messes this up for array types, so we work around that bug
3465      here.  */
3466   if (TREE_CODE (type) == ARRAY_TYPE)
3467     while (type != TYPE_MAIN_VARIANT (type))
3468       type = TYPE_MAIN_VARIANT (type);
3469
3470   return type;
3471 }
3472
3473 /* Return nonzero if the given type node represents a tagged type.  */
3474
3475 static inline int
3476 is_tagged_type (const_tree type)
3477 {
3478   enum tree_code code = TREE_CODE (type);
3479
3480   return (code == RECORD_TYPE || code == UNION_TYPE
3481           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3482 }
3483
3484 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3485
3486 static void
3487 get_ref_die_offset_label (char *label, dw_die_ref ref)
3488 {
3489   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3490 }
3491
3492 /* Return die_offset of a DIE reference to a base type.  */
3493
3494 static unsigned long int
3495 get_base_type_offset (dw_die_ref ref)
3496 {
3497   if (ref->die_offset)
3498     return ref->die_offset;
3499   if (comp_unit_die ()->die_abbrev)
3500     {
3501       calc_base_type_die_sizes ();
3502       gcc_assert (ref->die_offset);
3503     }
3504   return ref->die_offset;
3505 }
3506
3507 /* Return die_offset of a DIE reference other than base type.  */
3508
3509 static unsigned long int
3510 get_ref_die_offset (dw_die_ref ref)
3511 {
3512   gcc_assert (ref->die_offset);
3513   return ref->die_offset;
3514 }
3515
3516 /* Convert a DIE tag into its string name.  */
3517
3518 static const char *
3519 dwarf_tag_name (unsigned int tag)
3520 {
3521   const char *name = get_DW_TAG_name (tag);
3522
3523   if (name != NULL)
3524     return name;
3525
3526   return "DW_TAG_<unknown>";
3527 }
3528
3529 /* Convert a DWARF attribute code into its string name.  */
3530
3531 static const char *
3532 dwarf_attr_name (unsigned int attr)
3533 {
3534   const char *name;
3535
3536   switch (attr)
3537     {
3538 #if VMS_DEBUGGING_INFO
3539     case DW_AT_HP_prologue:
3540       return "DW_AT_HP_prologue";
3541 #else
3542     case DW_AT_MIPS_loop_unroll_factor:
3543       return "DW_AT_MIPS_loop_unroll_factor";
3544 #endif
3545
3546 #if VMS_DEBUGGING_INFO
3547     case DW_AT_HP_epilogue:
3548       return "DW_AT_HP_epilogue";
3549 #else
3550     case DW_AT_MIPS_stride:
3551       return "DW_AT_MIPS_stride";
3552 #endif
3553     }
3554
3555   name = get_DW_AT_name (attr);
3556
3557   if (name != NULL)
3558     return name;
3559
3560   return "DW_AT_<unknown>";
3561 }
3562
3563 /* Convert a DWARF value form code into its string name.  */
3564
3565 static const char *
3566 dwarf_form_name (unsigned int form)
3567 {
3568   const char *name = get_DW_FORM_name (form);
3569
3570   if (name != NULL)
3571     return name;
3572
3573   return "DW_FORM_<unknown>";
3574 }
3575 \f
3576 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
3577    instance of an inlined instance of a decl which is local to an inline
3578    function, so we have to trace all of the way back through the origin chain
3579    to find out what sort of node actually served as the original seed for the
3580    given block.  */
3581
3582 static tree
3583 decl_ultimate_origin (const_tree decl)
3584 {
3585   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
3586     return NULL_TREE;
3587
3588   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
3589      nodes in the function to point to themselves; ignore that if
3590      we're trying to output the abstract instance of this function.  */
3591   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
3592     return NULL_TREE;
3593
3594   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
3595      most distant ancestor, this should never happen.  */
3596   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
3597
3598   return DECL_ABSTRACT_ORIGIN (decl);
3599 }
3600
3601 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
3602    of a virtual function may refer to a base class, so we check the 'this'
3603    parameter.  */
3604
3605 static tree
3606 decl_class_context (tree decl)
3607 {
3608   tree context = NULL_TREE;
3609
3610   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
3611     context = DECL_CONTEXT (decl);
3612   else
3613     context = TYPE_MAIN_VARIANT
3614       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
3615
3616   if (context && !TYPE_P (context))
3617     context = NULL_TREE;
3618
3619   return context;
3620 }
3621 \f
3622 /* Add an attribute/value pair to a DIE.  */
3623
3624 static inline void
3625 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
3626 {
3627   /* Maybe this should be an assert?  */
3628   if (die == NULL)
3629     return;
3630
3631   vec_safe_reserve (die->die_attr, 1);
3632   vec_safe_push (die->die_attr, *attr);
3633 }
3634
3635 static inline enum dw_val_class
3636 AT_class (dw_attr_ref a)
3637 {
3638   return a->dw_attr_val.val_class;
3639 }
3640
3641 /* Return the index for any attribute that will be referenced with a
3642    DW_FORM_GNU_addr_index or DW_FORM_GNU_str_index.  String indices
3643    are stored in dw_attr_val.v.val_str for reference counting
3644    pruning.  */
3645
3646 static inline unsigned int
3647 AT_index (dw_attr_ref a)
3648 {
3649   if (AT_class (a) == dw_val_class_str)
3650     return a->dw_attr_val.v.val_str->index;
3651   else if (a->dw_attr_val.val_entry != NULL)
3652     return a->dw_attr_val.val_entry->index;
3653   return NOT_INDEXED;
3654 }
3655
3656 /* Add a flag value attribute to a DIE.  */
3657
3658 static inline void
3659 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
3660 {
3661   dw_attr_node attr;
3662
3663   attr.dw_attr = attr_kind;
3664   attr.dw_attr_val.val_class = dw_val_class_flag;
3665   attr.dw_attr_val.val_entry = NULL;
3666   attr.dw_attr_val.v.val_flag = flag;
3667   add_dwarf_attr (die, &attr);
3668 }
3669
3670 static inline unsigned
3671 AT_flag (dw_attr_ref a)
3672 {
3673   gcc_assert (a && AT_class (a) == dw_val_class_flag);
3674   return a->dw_attr_val.v.val_flag;
3675 }
3676
3677 /* Add a signed integer attribute value to a DIE.  */
3678
3679 static inline void
3680 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
3681 {
3682   dw_attr_node attr;
3683
3684   attr.dw_attr = attr_kind;
3685   attr.dw_attr_val.val_class = dw_val_class_const;
3686   attr.dw_attr_val.val_entry = NULL;
3687   attr.dw_attr_val.v.val_int = int_val;
3688   add_dwarf_attr (die, &attr);
3689 }
3690
3691 static inline HOST_WIDE_INT
3692 AT_int (dw_attr_ref a)
3693 {
3694   gcc_assert (a && AT_class (a) == dw_val_class_const);
3695   return a->dw_attr_val.v.val_int;
3696 }
3697
3698 /* Add an unsigned integer attribute value to a DIE.  */
3699
3700 static inline void
3701 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
3702                  unsigned HOST_WIDE_INT unsigned_val)
3703 {
3704   dw_attr_node attr;
3705
3706   attr.dw_attr = attr_kind;
3707   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
3708   attr.dw_attr_val.val_entry = NULL;
3709   attr.dw_attr_val.v.val_unsigned = unsigned_val;
3710   add_dwarf_attr (die, &attr);
3711 }
3712
3713 static inline unsigned HOST_WIDE_INT
3714 AT_unsigned (dw_attr_ref a)
3715 {
3716   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
3717   return a->dw_attr_val.v.val_unsigned;
3718 }
3719
3720 /* Add an unsigned double integer attribute value to a DIE.  */
3721
3722 static inline void
3723 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
3724                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
3725 {
3726   dw_attr_node attr;
3727
3728   attr.dw_attr = attr_kind;
3729   attr.dw_attr_val.val_class = dw_val_class_const_double;
3730   attr.dw_attr_val.val_entry = NULL;
3731   attr.dw_attr_val.v.val_double.high = high;
3732   attr.dw_attr_val.v.val_double.low = low;
3733   add_dwarf_attr (die, &attr);
3734 }
3735
3736 /* Add a floating point attribute value to a DIE and return it.  */
3737
3738 static inline void
3739 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
3740             unsigned int length, unsigned int elt_size, unsigned char *array)
3741 {
3742   dw_attr_node attr;
3743
3744   attr.dw_attr = attr_kind;
3745   attr.dw_attr_val.val_class = dw_val_class_vec;
3746   attr.dw_attr_val.val_entry = NULL;
3747   attr.dw_attr_val.v.val_vec.length = length;
3748   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
3749   attr.dw_attr_val.v.val_vec.array = array;
3750   add_dwarf_attr (die, &attr);
3751 }
3752
3753 /* Add an 8-byte data attribute value to a DIE.  */
3754
3755 static inline void
3756 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
3757               unsigned char data8[8])
3758 {
3759   dw_attr_node attr;
3760
3761   attr.dw_attr = attr_kind;
3762   attr.dw_attr_val.val_class = dw_val_class_data8;
3763   attr.dw_attr_val.val_entry = NULL;
3764   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
3765   add_dwarf_attr (die, &attr);
3766 }
3767
3768 /* Add DW_AT_low_pc and DW_AT_high_pc to a DIE.  When using
3769    dwarf_split_debug_info, address attributes in dies destined for the
3770    final executable have force_direct set to avoid using indexed
3771    references.  */
3772
3773 static inline void
3774 add_AT_low_high_pc (dw_die_ref die, const char *lbl_low, const char *lbl_high,
3775                     bool force_direct)
3776 {
3777   dw_attr_node attr;
3778   char * lbl_id;
3779
3780   lbl_id = xstrdup (lbl_low);
3781   attr.dw_attr = DW_AT_low_pc;
3782   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3783   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3784   if (dwarf_split_debug_info && !force_direct)
3785     attr.dw_attr_val.val_entry
3786         = add_addr_table_entry (lbl_id, ate_kind_label);
3787   else
3788     attr.dw_attr_val.val_entry = NULL;
3789   add_dwarf_attr (die, &attr);
3790
3791   attr.dw_attr = DW_AT_high_pc;
3792   if (dwarf_version < 4)
3793     attr.dw_attr_val.val_class = dw_val_class_lbl_id;
3794   else
3795     attr.dw_attr_val.val_class = dw_val_class_high_pc;
3796   lbl_id = xstrdup (lbl_high);
3797   attr.dw_attr_val.v.val_lbl_id = lbl_id;
3798   if (attr.dw_attr_val.val_class == dw_val_class_lbl_id
3799       && dwarf_split_debug_info && !force_direct)
3800     attr.dw_attr_val.val_entry
3801         = add_addr_table_entry (lbl_id, ate_kind_label);
3802   else
3803     attr.dw_attr_val.val_entry = NULL;
3804   add_dwarf_attr (die, &attr);
3805 }
3806
3807 /* Hash and equality functions for debug_str_hash.  */
3808
3809 static hashval_t
3810 debug_str_do_hash (const void *x)
3811 {
3812   return htab_hash_string (((const struct indirect_string_node *)x)->str);
3813 }
3814
3815 static int
3816 debug_str_eq (const void *x1, const void *x2)
3817 {
3818   return strcmp ((((const struct indirect_string_node *)x1)->str),
3819                  (const char *)x2) == 0;
3820 }
3821
3822 /* Add STR to the indirect string hash table.  */
3823
3824 static struct indirect_string_node *
3825 find_AT_string (const char *str)
3826 {
3827   struct indirect_string_node *node;
3828   void **slot;
3829
3830   if (! debug_str_hash)
3831     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
3832                                       debug_str_eq, NULL);
3833
3834   slot = htab_find_slot_with_hash (debug_str_hash, str,
3835                                    htab_hash_string (str), INSERT);
3836   if (*slot == NULL)
3837     {
3838       node = ggc_alloc_cleared_indirect_string_node ();
3839       node->str = ggc_strdup (str);
3840       *slot = node;
3841     }
3842   else
3843     node = (struct indirect_string_node *) *slot;
3844
3845   node->refcount++;
3846   return node;
3847 }
3848
3849 /* Add a string attribute value to a DIE.  */
3850
3851 static inline void
3852 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
3853 {
3854   dw_attr_node attr;
3855   struct indirect_string_node *node;
3856
3857   node = find_AT_string (str);
3858
3859   attr.dw_attr = attr_kind;
3860   attr.dw_attr_val.val_class = dw_val_class_str;
3861   attr.dw_attr_val.val_entry = NULL;
3862   attr.dw_attr_val.v.val_str = node;
3863   add_dwarf_attr (die, &attr);
3864 }
3865
3866 static inline const char *
3867 AT_string (dw_attr_ref a)
3868 {
3869   gcc_assert (a && AT_class (a) == dw_val_class_str);
3870   return a->dw_attr_val.v.val_str->str;
3871 }
3872
3873 /* Call this function directly to bypass AT_string_form's logic to put
3874    the string inline in the die. */
3875
3876 static void
3877 set_indirect_string (struct indirect_string_node *node)
3878 {
3879   char label[32];
3880   /* Already indirect is a no op.  */
3881   if (node->form == DW_FORM_strp || node->form == DW_FORM_GNU_str_index)
3882     {
3883       gcc_assert (node->label);
3884       return;
3885     }
3886   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
3887   ++dw2_string_counter;
3888   node->label = xstrdup (label);
3889
3890   if (!dwarf_split_debug_info)
3891     {
3892       node->form = DW_FORM_strp;
3893       node->index = NOT_INDEXED;
3894     }
3895   else
3896     {
3897       node->form = DW_FORM_GNU_str_index;
3898       node->index = NO_INDEX_ASSIGNED;
3899     }
3900 }
3901
3902 /* Find out whether a string should be output inline in DIE
3903    or out-of-line in .debug_str section.  */
3904
3905 static enum dwarf_form
3906 find_string_form (struct indirect_string_node *node)
3907 {
3908   unsigned int len;
3909
3910   if (node->form)
3911     return node->form;
3912
3913   len = strlen (node->str) + 1;
3914
3915   /* If the string is shorter or equal to the size of the reference, it is
3916      always better to put it inline.  */
3917   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
3918     return node->form = DW_FORM_string;
3919
3920   /* If we cannot expect the linker to merge strings in .debug_str
3921      section, only put it into .debug_str if it is worth even in this
3922      single module.  */
3923   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
3924       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
3925       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
3926     return node->form = DW_FORM_string;
3927
3928   set_indirect_string (node);
3929
3930   return node->form;
3931 }
3932
3933 /* Find out whether the string referenced from the attribute should be
3934    output inline in DIE or out-of-line in .debug_str section.  */
3935
3936 static enum dwarf_form
3937 AT_string_form (dw_attr_ref a)
3938 {
3939   gcc_assert (a && AT_class (a) == dw_val_class_str);
3940   return find_string_form (a->dw_attr_val.v.val_str);
3941 }
3942
3943 /* Add a DIE reference attribute value to a DIE.  */
3944
3945 static inline void
3946 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
3947 {
3948   dw_attr_node attr;
3949
3950 #ifdef ENABLE_CHECKING
3951   gcc_assert (targ_die != NULL);
3952 #else
3953   /* With LTO we can end up trying to reference something we didn't create
3954      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
3955   if (targ_die == NULL)
3956     return;
3957 #endif
3958
3959   attr.dw_attr = attr_kind;
3960   attr.dw_attr_val.val_class = dw_val_class_die_ref;
3961   attr.dw_attr_val.val_entry = NULL;
3962   attr.dw_attr_val.v.val_die_ref.die = targ_die;
3963   attr.dw_attr_val.v.val_die_ref.external = 0;
3964   add_dwarf_attr (die, &attr);
3965 }
3966
3967 /* Change DIE reference REF to point to NEW_DIE instead.  */
3968
3969 static inline void
3970 change_AT_die_ref (dw_attr_ref ref, dw_die_ref new_die)
3971 {
3972   gcc_assert (ref->dw_attr_val.val_class == dw_val_class_die_ref);
3973   ref->dw_attr_val.v.val_die_ref.die = new_die;
3974   ref->dw_attr_val.v.val_die_ref.external = 0;
3975 }
3976
3977 /* Add an AT_specification attribute to a DIE, and also make the back
3978    pointer from the specification to the definition.  */
3979
3980 static inline void
3981 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
3982 {
3983   add_AT_die_ref (die, DW_AT_specification, targ_die);
3984   gcc_assert (!targ_die->die_definition);
3985   targ_die->die_definition = die;
3986 }
3987
3988 static inline dw_die_ref
3989 AT_ref (dw_attr_ref a)
3990 {
3991   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
3992   return a->dw_attr_val.v.val_die_ref.die;
3993 }
3994
3995 static inline int
3996 AT_ref_external (dw_attr_ref a)
3997 {
3998   if (a && AT_class (a) == dw_val_class_die_ref)
3999     return a->dw_attr_val.v.val_die_ref.external;
4000
4001   return 0;
4002 }
4003
4004 static inline void
4005 set_AT_ref_external (dw_attr_ref a, int i)
4006 {
4007   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4008   a->dw_attr_val.v.val_die_ref.external = i;
4009 }
4010
4011 /* Add an FDE reference attribute value to a DIE.  */
4012
4013 static inline void
4014 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4015 {
4016   dw_attr_node attr;
4017
4018   attr.dw_attr = attr_kind;
4019   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4020   attr.dw_attr_val.val_entry = NULL;
4021   attr.dw_attr_val.v.val_fde_index = targ_fde;
4022   add_dwarf_attr (die, &attr);
4023 }
4024
4025 /* Add a location description attribute value to a DIE.  */
4026
4027 static inline void
4028 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4029 {
4030   dw_attr_node attr;
4031
4032   attr.dw_attr = attr_kind;
4033   attr.dw_attr_val.val_class = dw_val_class_loc;
4034   attr.dw_attr_val.val_entry = NULL;
4035   attr.dw_attr_val.v.val_loc = loc;
4036   add_dwarf_attr (die, &attr);
4037 }
4038
4039 static inline dw_loc_descr_ref
4040 AT_loc (dw_attr_ref a)
4041 {
4042   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4043   return a->dw_attr_val.v.val_loc;
4044 }
4045
4046 static inline void
4047 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4048 {
4049   dw_attr_node attr;
4050
4051   attr.dw_attr = attr_kind;
4052   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4053   attr.dw_attr_val.val_entry = NULL;
4054   attr.dw_attr_val.v.val_loc_list = loc_list;
4055   add_dwarf_attr (die, &attr);
4056   have_location_lists = true;
4057 }
4058
4059 static inline dw_loc_list_ref
4060 AT_loc_list (dw_attr_ref a)
4061 {
4062   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4063   return a->dw_attr_val.v.val_loc_list;
4064 }
4065
4066 static inline dw_loc_list_ref *
4067 AT_loc_list_ptr (dw_attr_ref a)
4068 {
4069   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4070   return &a->dw_attr_val.v.val_loc_list;
4071 }
4072
4073 /* Table of entries into the .debug_addr section.  */
4074
4075 static GTY ((param_is (addr_table_entry))) htab_t addr_index_table;
4076
4077 /* Hash an address_table_entry.  */
4078
4079 static hashval_t
4080 addr_table_entry_do_hash (const void *x)
4081 {
4082   const addr_table_entry *a = (const addr_table_entry *) x;
4083   switch (a->kind)
4084     {
4085       case ate_kind_rtx:
4086         return iterative_hash_rtx (a->addr.rtl, 0);
4087       case ate_kind_rtx_dtprel:
4088         return iterative_hash_rtx (a->addr.rtl, 1);
4089       case ate_kind_label:
4090         return htab_hash_string (a->addr.label);
4091       default:
4092         gcc_unreachable ();
4093     }
4094 }
4095
4096 /* Determine equality for two address_table_entries.  */
4097
4098 static int
4099 addr_table_entry_eq (const void *x1, const void *x2)
4100 {
4101   const addr_table_entry *a1 = (const addr_table_entry *) x1;
4102   const addr_table_entry *a2 = (const addr_table_entry *) x2;
4103
4104   if (a1->kind != a2->kind)
4105     return 0;
4106   switch (a1->kind)
4107     {
4108       case ate_kind_rtx:
4109       case ate_kind_rtx_dtprel:
4110         return rtx_equal_p (a1->addr.rtl, a2->addr.rtl);
4111       case ate_kind_label:
4112         return strcmp (a1->addr.label, a2->addr.label) == 0;
4113       default:
4114         gcc_unreachable ();
4115     }
4116 }
4117
4118 /* Initialize an addr_table_entry.  */
4119
4120 void
4121 init_addr_table_entry (addr_table_entry *e, enum ate_kind kind, void *addr)
4122 {
4123   e->kind = kind;
4124   switch (kind)
4125     {
4126       case ate_kind_rtx:
4127       case ate_kind_rtx_dtprel:
4128         e->addr.rtl = (rtx) addr;
4129         break;
4130       case ate_kind_label:
4131         e->addr.label = (char *) addr;
4132         break;
4133     }
4134   e->refcount = 0;
4135   e->index = NO_INDEX_ASSIGNED;
4136 }
4137
4138 /* Add attr to the address table entry to the table.  Defer setting an
4139    index until output time.  */
4140
4141 static addr_table_entry *
4142 add_addr_table_entry (void *addr, enum ate_kind kind)
4143 {
4144   addr_table_entry *node;
4145   addr_table_entry finder;
4146   void **slot;
4147
4148   gcc_assert (dwarf_split_debug_info);
4149   if (! addr_index_table)
4150     addr_index_table = htab_create_ggc (10, addr_table_entry_do_hash,
4151                                         addr_table_entry_eq, NULL);
4152   init_addr_table_entry (&finder, kind, addr);
4153   slot = htab_find_slot (addr_index_table, &finder, INSERT);
4154
4155   if (*slot == HTAB_EMPTY_ENTRY)
4156     {
4157       node = ggc_alloc_cleared_addr_table_entry ();
4158       init_addr_table_entry (node, kind, addr);
4159       *slot = node;
4160     }
4161   else
4162     node = (addr_table_entry *) *slot;
4163
4164   node->refcount++;
4165   return node;
4166 }
4167
4168 /* Remove an entry from the addr table by decrementing its refcount.
4169    Strictly, decrementing the refcount would be enough, but the
4170    assertion that the entry is actually in the table has found
4171    bugs.  */
4172
4173 static void
4174 remove_addr_table_entry (addr_table_entry *entry)
4175 {
4176   addr_table_entry *node;
4177
4178   gcc_assert (dwarf_split_debug_info && addr_index_table);
4179   node = (addr_table_entry *) htab_find (addr_index_table, entry);
4180   /* After an index is assigned, the table is frozen.  */
4181   gcc_assert (node->refcount > 0 && node->index == NO_INDEX_ASSIGNED);
4182   node->refcount--;
4183 }
4184
4185 /* Given a location list, remove all addresses it refers to from the
4186    address_table.  */
4187
4188 static void
4189 remove_loc_list_addr_table_entries (dw_loc_descr_ref descr)
4190 {
4191   for (; descr; descr = descr->dw_loc_next)
4192     if (descr->dw_loc_oprnd1.val_entry != NULL)
4193       {
4194         gcc_assert (descr->dw_loc_oprnd1.val_entry->index == NO_INDEX_ASSIGNED);
4195         remove_addr_table_entry (descr->dw_loc_oprnd1.val_entry);
4196       }
4197 }
4198
4199 /* A helper function for dwarf2out_finish called through
4200    htab_traverse.  Assign an addr_table_entry its index.  All entries
4201    must be collected into the table when this function is called,
4202    because the indexing code relies on htab_traverse to traverse nodes
4203    in the same order for each run. */
4204
4205 static int
4206 index_addr_table_entry (void **h, void *v)
4207 {
4208   addr_table_entry *node = (addr_table_entry *) *h;
4209   unsigned int *index = (unsigned int *) v;
4210
4211   /* Don't index unreferenced nodes.  */
4212   if (node->refcount == 0)
4213     return 1;
4214
4215   gcc_assert(node->index == NO_INDEX_ASSIGNED);
4216   node->index = *index;
4217   *index += 1;
4218
4219   return 1;
4220 }
4221
4222 /* Add an address constant attribute value to a DIE.  When using
4223    dwarf_split_debug_info, address attributes in dies destined for the
4224    final executable should be direct references--setting the parameter
4225    force_direct ensures this behavior.  */
4226
4227 static inline void
4228 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr,
4229              bool force_direct)
4230 {
4231   dw_attr_node attr;
4232
4233   attr.dw_attr = attr_kind;
4234   attr.dw_attr_val.val_class = dw_val_class_addr;
4235   attr.dw_attr_val.v.val_addr = addr;
4236   if (dwarf_split_debug_info && !force_direct)
4237     attr.dw_attr_val.val_entry = add_addr_table_entry (addr, ate_kind_rtx);
4238   else
4239     attr.dw_attr_val.val_entry = NULL;
4240   add_dwarf_attr (die, &attr);
4241 }
4242
4243 /* Get the RTX from to an address DIE attribute.  */
4244
4245 static inline rtx
4246 AT_addr (dw_attr_ref a)
4247 {
4248   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4249   return a->dw_attr_val.v.val_addr;
4250 }
4251
4252 /* Add a file attribute value to a DIE.  */
4253
4254 static inline void
4255 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4256              struct dwarf_file_data *fd)
4257 {
4258   dw_attr_node attr;
4259
4260   attr.dw_attr = attr_kind;
4261   attr.dw_attr_val.val_class = dw_val_class_file;
4262   attr.dw_attr_val.val_entry = NULL;
4263   attr.dw_attr_val.v.val_file = fd;
4264   add_dwarf_attr (die, &attr);
4265 }
4266
4267 /* Get the dwarf_file_data from a file DIE attribute.  */
4268
4269 static inline struct dwarf_file_data *
4270 AT_file (dw_attr_ref a)
4271 {
4272   gcc_assert (a && AT_class (a) == dw_val_class_file);
4273   return a->dw_attr_val.v.val_file;
4274 }
4275
4276 /* Add a vms delta attribute value to a DIE.  */
4277
4278 static inline void
4279 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4280                   const char *lbl1, const char *lbl2)
4281 {
4282   dw_attr_node attr;
4283
4284   attr.dw_attr = attr_kind;
4285   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4286   attr.dw_attr_val.val_entry = NULL;
4287   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4288   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4289   add_dwarf_attr (die, &attr);
4290 }
4291
4292 /* Add a label identifier attribute value to a DIE.  */
4293
4294 static inline void
4295 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind,
4296                const char *lbl_id)
4297 {
4298   dw_attr_node attr;
4299
4300   attr.dw_attr = attr_kind;
4301   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4302   attr.dw_attr_val.val_entry = NULL;
4303   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4304   if (dwarf_split_debug_info)
4305     attr.dw_attr_val.val_entry
4306         = add_addr_table_entry (attr.dw_attr_val.v.val_lbl_id,
4307                                 ate_kind_label);
4308   add_dwarf_attr (die, &attr);
4309 }
4310
4311 /* Add a section offset attribute value to a DIE, an offset into the
4312    debug_line section.  */
4313
4314 static inline void
4315 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4316                 const char *label)
4317 {
4318   dw_attr_node attr;
4319
4320   attr.dw_attr = attr_kind;
4321   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4322   attr.dw_attr_val.val_entry = NULL;
4323   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4324   add_dwarf_attr (die, &attr);
4325 }
4326
4327 /* Add a section offset attribute value to a DIE, an offset into the
4328    debug_macinfo section.  */
4329
4330 static inline void
4331 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4332                const char *label)
4333 {
4334   dw_attr_node attr;
4335
4336   attr.dw_attr = attr_kind;
4337   attr.dw_attr_val.val_class = dw_val_class_macptr;
4338   attr.dw_attr_val.val_entry = NULL;
4339   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4340   add_dwarf_attr (die, &attr);
4341 }
4342
4343 /* Add an offset attribute value to a DIE.  */
4344
4345 static inline void
4346 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4347                unsigned HOST_WIDE_INT offset)
4348 {
4349   dw_attr_node attr;
4350
4351   attr.dw_attr = attr_kind;
4352   attr.dw_attr_val.val_class = dw_val_class_offset;
4353   attr.dw_attr_val.val_entry = NULL;
4354   attr.dw_attr_val.v.val_offset = offset;
4355   add_dwarf_attr (die, &attr);
4356 }
4357
4358 /* Add a range_list attribute value to a DIE.  When using
4359    dwarf_split_debug_info, address attributes in dies destined for the
4360    final executable should be direct references--setting the parameter
4361    force_direct ensures this behavior.  */
4362
4363 #define UNRELOCATED_OFFSET ((addr_table_entry *) 1)
4364 #define RELOCATED_OFFSET (NULL)
4365
4366 static void
4367 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4368                    long unsigned int offset, bool force_direct)
4369 {
4370   dw_attr_node attr;
4371
4372   attr.dw_attr = attr_kind;
4373   attr.dw_attr_val.val_class = dw_val_class_range_list;
4374   /* For the range_list attribute, use val_entry to store whether the
4375      offset should follow split-debug-info or normal semantics.  This
4376      value is read in output_range_list_offset.  */
4377   if (dwarf_split_debug_info && !force_direct)
4378     attr.dw_attr_val.val_entry = UNRELOCATED_OFFSET;
4379   else
4380     attr.dw_attr_val.val_entry = RELOCATED_OFFSET;
4381   attr.dw_attr_val.v.val_offset = offset;
4382   add_dwarf_attr (die, &attr);
4383 }
4384
4385 /* Return the start label of a delta attribute.  */
4386
4387 static inline const char *
4388 AT_vms_delta1 (dw_attr_ref a)
4389 {
4390   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4391   return a->dw_attr_val.v.val_vms_delta.lbl1;
4392 }
4393
4394 /* Return the end label of a delta attribute.  */
4395
4396 static inline const char *
4397 AT_vms_delta2 (dw_attr_ref a)
4398 {
4399   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4400   return a->dw_attr_val.v.val_vms_delta.lbl2;
4401 }
4402
4403 static inline const char *
4404 AT_lbl (dw_attr_ref a)
4405 {
4406   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4407                     || AT_class (a) == dw_val_class_lineptr
4408                     || AT_class (a) == dw_val_class_macptr
4409                     || AT_class (a) == dw_val_class_high_pc));
4410   return a->dw_attr_val.v.val_lbl_id;
4411 }
4412
4413 /* Get the attribute of type attr_kind.  */
4414
4415 static dw_attr_ref
4416 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4417 {
4418   dw_attr_ref a;
4419   unsigned ix;
4420   dw_die_ref spec = NULL;
4421
4422   if (! die)
4423     return NULL;
4424
4425   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4426     if (a->dw_attr == attr_kind)
4427       return a;
4428     else if (a->dw_attr == DW_AT_specification
4429              || a->dw_attr == DW_AT_abstract_origin)
4430       spec = AT_ref (a);
4431
4432   if (spec)
4433     return get_AT (spec, attr_kind);
4434
4435   return NULL;
4436 }
4437
4438 /* Returns the parent of the declaration of DIE.  */
4439
4440 static dw_die_ref
4441 get_die_parent (dw_die_ref die)
4442 {
4443   dw_die_ref t;
4444
4445   if (!die)
4446     return NULL;
4447
4448   if ((t = get_AT_ref (die, DW_AT_abstract_origin))
4449       || (t = get_AT_ref (die, DW_AT_specification)))
4450     die = t;
4451
4452   return die->die_parent;
4453 }
4454
4455 /* Return the "low pc" attribute value, typically associated with a subprogram
4456    DIE.  Return null if the "low pc" attribute is either not present, or if it
4457    cannot be represented as an assembler label identifier.  */
4458
4459 static inline const char *
4460 get_AT_low_pc (dw_die_ref die)
4461 {
4462   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4463
4464   return a ? AT_lbl (a) : NULL;
4465 }
4466
4467 /* Return the "high pc" attribute value, typically associated with a subprogram
4468    DIE.  Return null if the "high pc" attribute is either not present, or if it
4469    cannot be represented as an assembler label identifier.  */
4470
4471 static inline const char *
4472 get_AT_hi_pc (dw_die_ref die)
4473 {
4474   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4475
4476   return a ? AT_lbl (a) : NULL;
4477 }
4478
4479 /* Return the value of the string attribute designated by ATTR_KIND, or
4480    NULL if it is not present.  */
4481
4482 static inline const char *
4483 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4484 {
4485   dw_attr_ref a = get_AT (die, attr_kind);
4486
4487   return a ? AT_string (a) : NULL;
4488 }
4489
4490 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4491    if it is not present.  */
4492
4493 static inline int
4494 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4495 {
4496   dw_attr_ref a = get_AT (die, attr_kind);
4497
4498   return a ? AT_flag (a) : 0;
4499 }
4500
4501 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4502    if it is not present.  */
4503
4504 static inline unsigned
4505 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4506 {
4507   dw_attr_ref a = get_AT (die, attr_kind);
4508
4509   return a ? AT_unsigned (a) : 0;
4510 }
4511
4512 static inline dw_die_ref
4513 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4514 {
4515   dw_attr_ref a = get_AT (die, attr_kind);
4516
4517   return a ? AT_ref (a) : NULL;
4518 }
4519
4520 static inline struct dwarf_file_data *
4521 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4522 {
4523   dw_attr_ref a = get_AT (die, attr_kind);
4524
4525   return a ? AT_file (a) : NULL;
4526 }
4527
4528 /* Return TRUE if the language is C++.  */
4529
4530 static inline bool
4531 is_cxx (void)
4532 {
4533   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4534
4535   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4536 }
4537
4538 /* Return TRUE if the language is Fortran.  */
4539
4540 static inline bool
4541 is_fortran (void)
4542 {
4543   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4544
4545   return (lang == DW_LANG_Fortran77
4546           || lang == DW_LANG_Fortran90
4547           || lang == DW_LANG_Fortran95);
4548 }
4549
4550 /* Return TRUE if the language is Ada.  */
4551
4552 static inline bool
4553 is_ada (void)
4554 {
4555   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4556
4557   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4558 }
4559
4560 /* Remove the specified attribute if present.  */
4561
4562 static void
4563 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4564 {
4565   dw_attr_ref a;
4566   unsigned ix;
4567
4568   if (! die)
4569     return;
4570
4571   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
4572     if (a->dw_attr == attr_kind)
4573       {
4574         if (AT_class (a) == dw_val_class_str)
4575           if (a->dw_attr_val.v.val_str->refcount)
4576             a->dw_attr_val.v.val_str->refcount--;
4577
4578         /* vec::ordered_remove should help reduce the number of abbrevs
4579            that are needed.  */
4580         die->die_attr->ordered_remove (ix);
4581         return;
4582       }
4583 }
4584
4585 /* Remove CHILD from its parent.  PREV must have the property that
4586    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4587
4588 static void
4589 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4590 {
4591   gcc_assert (child->die_parent == prev->die_parent);
4592   gcc_assert (prev->die_sib == child);
4593   if (prev == child)
4594     {
4595       gcc_assert (child->die_parent->die_child == child);
4596       prev = NULL;
4597     }
4598   else
4599     prev->die_sib = child->die_sib;
4600   if (child->die_parent->die_child == child)
4601     child->die_parent->die_child = prev;
4602 }
4603
4604 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4605    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4606
4607 static void
4608 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4609 {
4610   dw_die_ref parent = old_child->die_parent;
4611
4612   gcc_assert (parent == prev->die_parent);
4613   gcc_assert (prev->die_sib == old_child);
4614
4615   new_child->die_parent = parent;
4616   if (prev == old_child)
4617     {
4618       gcc_assert (parent->die_child == old_child);
4619       new_child->die_sib = new_child;
4620     }
4621   else
4622     {
4623       prev->die_sib = new_child;
4624       new_child->die_sib = old_child->die_sib;
4625     }
4626   if (old_child->die_parent->die_child == old_child)
4627     old_child->die_parent->die_child = new_child;
4628 }
4629
4630 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4631
4632 static void
4633 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4634 {
4635   dw_die_ref c;
4636   new_parent->die_child = old_parent->die_child;
4637   old_parent->die_child = NULL;
4638   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4639 }
4640
4641 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4642    matches TAG.  */
4643
4644 static void
4645 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4646 {
4647   dw_die_ref c;
4648
4649   c = die->die_child;
4650   if (c) do {
4651     dw_die_ref prev = c;
4652     c = c->die_sib;
4653     while (c->die_tag == tag)
4654       {
4655         remove_child_with_prev (c, prev);
4656         /* Might have removed every child.  */
4657         if (c == c->die_sib)
4658           return;
4659         c = c->die_sib;
4660       }
4661   } while (c != die->die_child);
4662 }
4663
4664 /* Add a CHILD_DIE as the last child of DIE.  */
4665
4666 static void
4667 add_child_die (dw_die_ref die, dw_die_ref child_die)
4668 {
4669   /* FIXME this should probably be an assert.  */
4670   if (! die || ! child_die)
4671     return;
4672   gcc_assert (die != child_die);
4673
4674   child_die->die_parent = die;
4675   if (die->die_child)
4676     {
4677       child_die->die_sib = die->die_child->die_sib;
4678       die->die_child->die_sib = child_die;
4679     }
4680   else
4681     child_die->die_sib = child_die;
4682   die->die_child = child_die;
4683 }
4684
4685 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4686    is the specification, to the end of PARENT's list of children.
4687    This is done by removing and re-adding it.  */
4688
4689 static void
4690 splice_child_die (dw_die_ref parent, dw_die_ref child)
4691 {
4692   dw_die_ref p;
4693
4694   /* We want the declaration DIE from inside the class, not the
4695      specification DIE at toplevel.  */
4696   if (child->die_parent != parent)
4697     {
4698       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4699
4700       if (tmp)
4701         child = tmp;
4702     }
4703
4704   gcc_assert (child->die_parent == parent
4705               || (child->die_parent
4706                   == get_AT_ref (parent, DW_AT_specification)));
4707
4708   for (p = child->die_parent->die_child; ; p = p->die_sib)
4709     if (p->die_sib == child)
4710       {
4711         remove_child_with_prev (child, p);
4712         break;
4713       }
4714
4715   add_child_die (parent, child);
4716 }
4717
4718 /* Return a pointer to a newly created DIE node.  */
4719
4720 static inline dw_die_ref
4721 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4722 {
4723   dw_die_ref die = ggc_alloc_cleared_die_node ();
4724
4725   die->die_tag = tag_value;
4726
4727   if (parent_die != NULL)
4728     add_child_die (parent_die, die);
4729   else
4730     {
4731       limbo_die_node *limbo_node;
4732
4733       limbo_node = ggc_alloc_cleared_limbo_die_node ();
4734       limbo_node->die = die;
4735       limbo_node->created_for = t;
4736       limbo_node->next = limbo_die_list;
4737       limbo_die_list = limbo_node;
4738     }
4739
4740   return die;
4741 }
4742
4743 /* Return the DIE associated with the given type specifier.  */
4744
4745 static inline dw_die_ref
4746 lookup_type_die (tree type)
4747 {
4748   return TYPE_SYMTAB_DIE (type);
4749 }
4750
4751 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4752    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4753    anonymous type instead the one of the naming typedef.  */
4754
4755 static inline dw_die_ref
4756 strip_naming_typedef (tree type, dw_die_ref type_die)
4757 {
4758   if (type
4759       && TREE_CODE (type) == RECORD_TYPE
4760       && type_die
4761       && type_die->die_tag == DW_TAG_typedef
4762       && is_naming_typedef_decl (TYPE_NAME (type)))
4763     type_die = get_AT_ref (type_die, DW_AT_type);
4764   return type_die;
4765 }
4766
4767 /* Like lookup_type_die, but if type is an anonymous type named by a
4768    typedef[1], return the DIE of the anonymous type instead the one of
4769    the naming typedef.  This is because in gen_typedef_die, we did
4770    equate the anonymous struct named by the typedef with the DIE of
4771    the naming typedef. So by default, lookup_type_die on an anonymous
4772    struct yields the DIE of the naming typedef.
4773
4774    [1]: Read the comment of is_naming_typedef_decl to learn about what
4775    a naming typedef is.  */
4776
4777 static inline dw_die_ref
4778 lookup_type_die_strip_naming_typedef (tree type)
4779 {
4780   dw_die_ref die = lookup_type_die (type);
4781   return strip_naming_typedef (type, die);
4782 }
4783
4784 /* Equate a DIE to a given type specifier.  */
4785
4786 static inline void
4787 equate_type_number_to_die (tree type, dw_die_ref type_die)
4788 {
4789   TYPE_SYMTAB_DIE (type) = type_die;
4790 }
4791
4792 /* Returns a hash value for X (which really is a die_struct).  */
4793
4794 static hashval_t
4795 decl_die_table_hash (const void *x)
4796 {
4797   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
4798 }
4799
4800 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
4801
4802 static int
4803 decl_die_table_eq (const void *x, const void *y)
4804 {
4805   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
4806 }
4807
4808 /* Return the DIE associated with a given declaration.  */
4809
4810 static inline dw_die_ref
4811 lookup_decl_die (tree decl)
4812 {
4813   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
4814 }
4815
4816 /* Returns a hash value for X (which really is a var_loc_list).  */
4817
4818 static hashval_t
4819 decl_loc_table_hash (const void *x)
4820 {
4821   return (hashval_t) ((const var_loc_list *) x)->decl_id;
4822 }
4823
4824 /* Return nonzero if decl_id of var_loc_list X is the same as
4825    UID of decl *Y.  */
4826
4827 static int
4828 decl_loc_table_eq (const void *x, const void *y)
4829 {
4830   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
4831 }
4832
4833 /* Return the var_loc list associated with a given declaration.  */
4834
4835 static inline var_loc_list *
4836 lookup_decl_loc (const_tree decl)
4837 {
4838   if (!decl_loc_table)
4839     return NULL;
4840   return (var_loc_list *)
4841     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
4842 }
4843
4844 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
4845
4846 static hashval_t
4847 cached_dw_loc_list_table_hash (const void *x)
4848 {
4849   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
4850 }
4851
4852 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
4853    UID of decl *Y.  */
4854
4855 static int
4856 cached_dw_loc_list_table_eq (const void *x, const void *y)
4857 {
4858   return (((const cached_dw_loc_list *) x)->decl_id
4859           == DECL_UID ((const_tree) y));
4860 }
4861
4862 /* Equate a DIE to a particular declaration.  */
4863
4864 static void
4865 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
4866 {
4867   unsigned int decl_id = DECL_UID (decl);
4868   void **slot;
4869
4870   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
4871   *slot = decl_die;
4872   decl_die->decl_id = decl_id;
4873 }
4874
4875 /* Return how many bits covers PIECE EXPR_LIST.  */
4876
4877 static int
4878 decl_piece_bitsize (rtx piece)
4879 {
4880   int ret = (int) GET_MODE (piece);
4881   if (ret)
4882     return ret;
4883   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
4884               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
4885   return INTVAL (XEXP (XEXP (piece, 0), 0));
4886 }
4887
4888 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
4889
4890 static rtx *
4891 decl_piece_varloc_ptr (rtx piece)
4892 {
4893   if ((int) GET_MODE (piece))
4894     return &XEXP (piece, 0);
4895   else
4896     return &XEXP (XEXP (piece, 0), 1);
4897 }
4898
4899 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
4900    Next is the chain of following piece nodes.  */
4901
4902 static rtx
4903 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
4904 {
4905   if (bitsize <= (int) MAX_MACHINE_MODE)
4906     return alloc_EXPR_LIST (bitsize, loc_note, next);
4907   else
4908     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
4909                                                GEN_INT (bitsize),
4910                                                loc_note), next);
4911 }
4912
4913 /* Return rtx that should be stored into loc field for
4914    LOC_NOTE and BITPOS/BITSIZE.  */
4915
4916 static rtx
4917 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
4918                       HOST_WIDE_INT bitsize)
4919 {
4920   if (bitsize != -1)
4921     {
4922       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
4923       if (bitpos != 0)
4924         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
4925     }
4926   return loc_note;
4927 }
4928
4929 /* This function either modifies location piece list *DEST in
4930    place (if SRC and INNER is NULL), or copies location piece list
4931    *SRC to *DEST while modifying it.  Location BITPOS is modified
4932    to contain LOC_NOTE, any pieces overlapping it are removed resp.
4933    not copied and if needed some padding around it is added.
4934    When modifying in place, DEST should point to EXPR_LIST where
4935    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
4936    to the start of the whole list and INNER points to the EXPR_LIST
4937    where earlier pieces cover PIECE_BITPOS bits.  */
4938
4939 static void
4940 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
4941                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
4942                    HOST_WIDE_INT bitsize, rtx loc_note)
4943 {
4944   int diff;
4945   bool copy = inner != NULL;
4946
4947   if (copy)
4948     {
4949       /* First copy all nodes preceding the current bitpos.  */
4950       while (src != inner)
4951         {
4952           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
4953                                    decl_piece_bitsize (*src), NULL_RTX);
4954           dest = &XEXP (*dest, 1);
4955           src = &XEXP (*src, 1);
4956         }
4957     }
4958   /* Add padding if needed.  */
4959   if (bitpos != piece_bitpos)
4960     {
4961       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
4962                                copy ? NULL_RTX : *dest);
4963       dest = &XEXP (*dest, 1);
4964     }
4965   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
4966     {
4967       gcc_assert (!copy);
4968       /* A piece with correct bitpos and bitsize already exist,
4969          just update the location for it and return.  */
4970       *decl_piece_varloc_ptr (*dest) = loc_note;
4971       return;
4972     }
4973   /* Add the piece that changed.  */
4974   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
4975   dest = &XEXP (*dest, 1);
4976   /* Skip over pieces that overlap it.  */
4977   diff = bitpos - piece_bitpos + bitsize;
4978   if (!copy)
4979     src = dest;
4980   while (diff > 0 && *src)
4981     {
4982       rtx piece = *src;
4983       diff -= decl_piece_bitsize (piece);
4984       if (copy)
4985         src = &XEXP (piece, 1);
4986       else
4987         {
4988           *src = XEXP (piece, 1);
4989           free_EXPR_LIST_node (piece);
4990         }
4991     }
4992   /* Add padding if needed.  */
4993   if (diff < 0 && *src)
4994     {
4995       if (!copy)
4996         dest = src;
4997       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
4998       dest = &XEXP (*dest, 1);
4999     }
5000   if (!copy)
5001     return;
5002   /* Finally copy all nodes following it.  */
5003   while (*src)
5004     {
5005       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5006                                decl_piece_bitsize (*src), NULL_RTX);
5007       dest = &XEXP (*dest, 1);
5008       src = &XEXP (*src, 1);
5009     }
5010 }
5011
5012 /* Add a variable location node to the linked list for DECL.  */
5013
5014 static struct var_loc_node *
5015 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5016 {
5017   unsigned int decl_id;
5018   var_loc_list *temp;
5019   void **slot;
5020   struct var_loc_node *loc = NULL;
5021   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5022
5023   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5024     {
5025       tree realdecl = DECL_DEBUG_EXPR (decl);
5026       if (realdecl
5027           && (handled_component_p (realdecl)
5028               || (TREE_CODE (realdecl) == MEM_REF
5029                   && TREE_CODE (TREE_OPERAND (realdecl, 0)) == ADDR_EXPR)))
5030         {
5031           HOST_WIDE_INT maxsize;
5032           tree innerdecl;
5033           innerdecl
5034             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5035           if (!DECL_P (innerdecl)
5036               || DECL_IGNORED_P (innerdecl)
5037               || TREE_STATIC (innerdecl)
5038               || bitsize <= 0
5039               || bitpos + bitsize > 256
5040               || bitsize != maxsize)
5041             return NULL;
5042           decl = innerdecl;
5043         }
5044     }
5045
5046   decl_id = DECL_UID (decl);
5047   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5048   if (*slot == NULL)
5049     {
5050       temp = ggc_alloc_cleared_var_loc_list ();
5051       temp->decl_id = decl_id;
5052       *slot = temp;
5053     }
5054   else
5055     temp = (var_loc_list *) *slot;
5056
5057   /* For PARM_DECLs try to keep around the original incoming value,
5058      even if that means we'll emit a zero-range .debug_loc entry.  */
5059   if (temp->last
5060       && temp->first == temp->last
5061       && TREE_CODE (decl) == PARM_DECL
5062       && GET_CODE (temp->first->loc) == NOTE
5063       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5064       && DECL_INCOMING_RTL (decl)
5065       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5066       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5067          == GET_CODE (DECL_INCOMING_RTL (decl))
5068       && prev_real_insn (temp->first->loc) == NULL_RTX
5069       && (bitsize != -1
5070           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5071                            NOTE_VAR_LOCATION_LOC (loc_note))
5072           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5073               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5074     {
5075       loc = ggc_alloc_cleared_var_loc_node ();
5076       temp->first->next = loc;
5077       temp->last = loc;
5078       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5079     }
5080   else if (temp->last)
5081     {
5082       struct var_loc_node *last = temp->last, *unused = NULL;
5083       rtx *piece_loc = NULL, last_loc_note;
5084       int piece_bitpos = 0;
5085       if (last->next)
5086         {
5087           last = last->next;
5088           gcc_assert (last->next == NULL);
5089         }
5090       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5091         {
5092           piece_loc = &last->loc;
5093           do
5094             {
5095               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5096               if (piece_bitpos + cur_bitsize > bitpos)
5097                 break;
5098               piece_bitpos += cur_bitsize;
5099               piece_loc = &XEXP (*piece_loc, 1);
5100             }
5101           while (*piece_loc);
5102         }
5103       /* TEMP->LAST here is either pointer to the last but one or
5104          last element in the chained list, LAST is pointer to the
5105          last element.  */
5106       if (label && strcmp (last->label, label) == 0)
5107         {
5108           /* For SRA optimized variables if there weren't any real
5109              insns since last note, just modify the last node.  */
5110           if (piece_loc != NULL)
5111             {
5112               adjust_piece_list (piece_loc, NULL, NULL,
5113                                  bitpos, piece_bitpos, bitsize, loc_note);
5114               return NULL;
5115             }
5116           /* If the last note doesn't cover any instructions, remove it.  */
5117           if (temp->last != last)
5118             {
5119               temp->last->next = NULL;
5120               unused = last;
5121               last = temp->last;
5122               gcc_assert (strcmp (last->label, label) != 0);
5123             }
5124           else
5125             {
5126               gcc_assert (temp->first == temp->last
5127                           || (temp->first->next == temp->last
5128                               && TREE_CODE (decl) == PARM_DECL));
5129               memset (temp->last, '\0', sizeof (*temp->last));
5130               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5131               return temp->last;
5132             }
5133         }
5134       if (bitsize == -1 && NOTE_P (last->loc))
5135         last_loc_note = last->loc;
5136       else if (piece_loc != NULL
5137                && *piece_loc != NULL_RTX
5138                && piece_bitpos == bitpos
5139                && decl_piece_bitsize (*piece_loc) == bitsize)
5140         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5141       else
5142         last_loc_note = NULL_RTX;
5143       /* If the current location is the same as the end of the list,
5144          and either both or neither of the locations is uninitialized,
5145          we have nothing to do.  */
5146       if (last_loc_note == NULL_RTX
5147           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5148                             NOTE_VAR_LOCATION_LOC (loc_note)))
5149           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5150                != NOTE_VAR_LOCATION_STATUS (loc_note))
5151               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5152                    == VAR_INIT_STATUS_UNINITIALIZED)
5153                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5154                       == VAR_INIT_STATUS_UNINITIALIZED))))
5155         {
5156           /* Add LOC to the end of list and update LAST.  If the last
5157              element of the list has been removed above, reuse its
5158              memory for the new node, otherwise allocate a new one.  */
5159           if (unused)
5160             {
5161               loc = unused;
5162               memset (loc, '\0', sizeof (*loc));
5163             }
5164           else
5165             loc = ggc_alloc_cleared_var_loc_node ();
5166           if (bitsize == -1 || piece_loc == NULL)
5167             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5168           else
5169             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5170                                bitpos, piece_bitpos, bitsize, loc_note);
5171           last->next = loc;
5172           /* Ensure TEMP->LAST will point either to the new last but one
5173              element of the chain, or to the last element in it.  */
5174           if (last != temp->last)
5175             temp->last = last;
5176         }
5177       else if (unused)
5178         ggc_free (unused);
5179     }
5180   else
5181     {
5182       loc = ggc_alloc_cleared_var_loc_node ();
5183       temp->first = loc;
5184       temp->last = loc;
5185       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5186     }
5187   return loc;
5188 }
5189 \f
5190 /* Keep track of the number of spaces used to indent the
5191    output of the debugging routines that print the structure of
5192    the DIE internal representation.  */
5193 static int print_indent;
5194
5195 /* Indent the line the number of spaces given by print_indent.  */
5196
5197 static inline void
5198 print_spaces (FILE *outfile)
5199 {
5200   fprintf (outfile, "%*s", print_indent, "");
5201 }
5202
5203 /* Print a type signature in hex.  */
5204
5205 static inline void
5206 print_signature (FILE *outfile, char *sig)
5207 {
5208   int i;
5209
5210   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5211     fprintf (outfile, "%02x", sig[i] & 0xff);
5212 }
5213
5214 /* Print the information associated with a given DIE, and its children.
5215    This routine is a debugging aid only.  */
5216
5217 static void
5218 print_die (dw_die_ref die, FILE *outfile)
5219 {
5220   dw_attr_ref a;
5221   dw_die_ref c;
5222   unsigned ix;
5223
5224   print_spaces (outfile);
5225   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5226            die->die_offset, dwarf_tag_name (die->die_tag),
5227            (void*) die);
5228   print_spaces (outfile);
5229   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5230   fprintf (outfile, " offset: %ld", die->die_offset);
5231   fprintf (outfile, " mark: %d\n", die->die_mark);
5232
5233   if (die->comdat_type_p)
5234     {
5235       print_spaces (outfile);
5236       fprintf (outfile, "  signature: ");
5237       print_signature (outfile, die->die_id.die_type_node->signature);
5238       fprintf (outfile, "\n");
5239     }
5240
5241   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5242     {
5243       print_spaces (outfile);
5244       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5245
5246       switch (AT_class (a))
5247         {
5248         case dw_val_class_addr:
5249           fprintf (outfile, "address");
5250           break;
5251         case dw_val_class_offset:
5252           fprintf (outfile, "offset");
5253           break;
5254         case dw_val_class_loc:
5255           fprintf (outfile, "location descriptor");
5256           break;
5257         case dw_val_class_loc_list:
5258           fprintf (outfile, "location list -> label:%s",
5259                    AT_loc_list (a)->ll_symbol);
5260           break;
5261         case dw_val_class_range_list:
5262           fprintf (outfile, "range list");
5263           break;
5264         case dw_val_class_const:
5265           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5266           break;
5267         case dw_val_class_unsigned_const:
5268           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5269           break;
5270         case dw_val_class_const_double:
5271           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5272                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5273                    a->dw_attr_val.v.val_double.high,
5274                    a->dw_attr_val.v.val_double.low);
5275           break;
5276         case dw_val_class_vec:
5277           fprintf (outfile, "floating-point or vector constant");
5278           break;
5279         case dw_val_class_flag:
5280           fprintf (outfile, "%u", AT_flag (a));
5281           break;
5282         case dw_val_class_die_ref:
5283           if (AT_ref (a) != NULL)
5284             {
5285               if (AT_ref (a)->comdat_type_p)
5286                 {
5287                   fprintf (outfile, "die -> signature: ");
5288                   print_signature (outfile,
5289                                    AT_ref (a)->die_id.die_type_node->signature);
5290                 }
5291               else if (AT_ref (a)->die_id.die_symbol)
5292                 fprintf (outfile, "die -> label: %s",
5293                          AT_ref (a)->die_id.die_symbol);
5294               else
5295                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5296               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5297             }
5298           else
5299             fprintf (outfile, "die -> <null>");
5300           break;
5301         case dw_val_class_vms_delta:
5302           fprintf (outfile, "delta: @slotcount(%s-%s)",
5303                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5304           break;
5305         case dw_val_class_lbl_id:
5306         case dw_val_class_lineptr:
5307         case dw_val_class_macptr:
5308         case dw_val_class_high_pc:
5309           fprintf (outfile, "label: %s", AT_lbl (a));
5310           break;
5311         case dw_val_class_str:
5312           if (AT_string (a) != NULL)
5313             fprintf (outfile, "\"%s\"", AT_string (a));
5314           else
5315             fprintf (outfile, "<null>");
5316           break;
5317         case dw_val_class_file:
5318           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5319                    AT_file (a)->emitted_number);
5320           break;
5321         case dw_val_class_data8:
5322           {
5323             int i;
5324
5325             for (i = 0; i < 8; i++)
5326               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5327             break;
5328           }
5329         default:
5330           break;
5331         }
5332
5333       fprintf (outfile, "\n");
5334     }
5335
5336   if (die->die_child != NULL)
5337     {
5338       print_indent += 4;
5339       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5340       print_indent -= 4;
5341     }
5342   if (print_indent == 0)
5343     fprintf (outfile, "\n");
5344 }
5345
5346 /* Print the information collected for a given DIE.  */
5347
5348 DEBUG_FUNCTION void
5349 debug_dwarf_die (dw_die_ref die)
5350 {
5351   print_die (die, stderr);
5352 }
5353
5354 /* Print all DWARF information collected for the compilation unit.
5355    This routine is a debugging aid only.  */
5356
5357 DEBUG_FUNCTION void
5358 debug_dwarf (void)
5359 {
5360   print_indent = 0;
5361   print_die (comp_unit_die (), stderr);
5362 }
5363 \f
5364 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5365    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5366    DIE that marks the start of the DIEs for this include file.  */
5367
5368 static dw_die_ref
5369 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5370 {
5371   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5372   dw_die_ref new_unit = gen_compile_unit_die (filename);
5373
5374   new_unit->die_sib = old_unit;
5375   return new_unit;
5376 }
5377
5378 /* Close an include-file CU and reopen the enclosing one.  */
5379
5380 static dw_die_ref
5381 pop_compile_unit (dw_die_ref old_unit)
5382 {
5383   dw_die_ref new_unit = old_unit->die_sib;
5384
5385   old_unit->die_sib = NULL;
5386   return new_unit;
5387 }
5388
5389 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5390 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5391
5392 /* Calculate the checksum of a location expression.  */
5393
5394 static inline void
5395 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5396 {
5397   int tem;
5398
5399   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5400   CHECKSUM (tem);
5401   CHECKSUM (loc->dw_loc_oprnd1);
5402   CHECKSUM (loc->dw_loc_oprnd2);
5403 }
5404
5405 /* Calculate the checksum of an attribute.  */
5406
5407 static void
5408 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5409 {
5410   dw_loc_descr_ref loc;
5411   rtx r;
5412
5413   CHECKSUM (at->dw_attr);
5414
5415   /* We don't care that this was compiled with a different compiler
5416      snapshot; if the output is the same, that's what matters.  */
5417   if (at->dw_attr == DW_AT_producer)
5418     return;
5419
5420   switch (AT_class (at))
5421     {
5422     case dw_val_class_const:
5423       CHECKSUM (at->dw_attr_val.v.val_int);
5424       break;
5425     case dw_val_class_unsigned_const:
5426       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5427       break;
5428     case dw_val_class_const_double:
5429       CHECKSUM (at->dw_attr_val.v.val_double);
5430       break;
5431     case dw_val_class_vec:
5432       CHECKSUM (at->dw_attr_val.v.val_vec);
5433       break;
5434     case dw_val_class_flag:
5435       CHECKSUM (at->dw_attr_val.v.val_flag);
5436       break;
5437     case dw_val_class_str:
5438       CHECKSUM_STRING (AT_string (at));
5439       break;
5440
5441     case dw_val_class_addr:
5442       r = AT_addr (at);
5443       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5444       CHECKSUM_STRING (XSTR (r, 0));
5445       break;
5446
5447     case dw_val_class_offset:
5448       CHECKSUM (at->dw_attr_val.v.val_offset);
5449       break;
5450
5451     case dw_val_class_loc:
5452       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5453         loc_checksum (loc, ctx);
5454       break;
5455
5456     case dw_val_class_die_ref:
5457       die_checksum (AT_ref (at), ctx, mark);
5458       break;
5459
5460     case dw_val_class_fde_ref:
5461     case dw_val_class_vms_delta:
5462     case dw_val_class_lbl_id:
5463     case dw_val_class_lineptr:
5464     case dw_val_class_macptr:
5465     case dw_val_class_high_pc:
5466       break;
5467
5468     case dw_val_class_file:
5469       CHECKSUM_STRING (AT_file (at)->filename);
5470       break;
5471
5472     case dw_val_class_data8:
5473       CHECKSUM (at->dw_attr_val.v.val_data8);
5474       break;
5475
5476     default:
5477       break;
5478     }
5479 }
5480
5481 /* Calculate the checksum of a DIE.  */
5482
5483 static void
5484 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5485 {
5486   dw_die_ref c;
5487   dw_attr_ref a;
5488   unsigned ix;
5489
5490   /* To avoid infinite recursion.  */
5491   if (die->die_mark)
5492     {
5493       CHECKSUM (die->die_mark);
5494       return;
5495     }
5496   die->die_mark = ++(*mark);
5497
5498   CHECKSUM (die->die_tag);
5499
5500   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5501     attr_checksum (a, ctx, mark);
5502
5503   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5504 }
5505
5506 #undef CHECKSUM
5507 #undef CHECKSUM_STRING
5508
5509 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5510 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5511 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5512 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5513 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5514 #define CHECKSUM_ATTR(FOO) \
5515   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5516
5517 /* Calculate the checksum of a number in signed LEB128 format.  */
5518
5519 static void
5520 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5521 {
5522   unsigned char byte;
5523   bool more;
5524
5525   while (1)
5526     {
5527       byte = (value & 0x7f);
5528       value >>= 7;
5529       more = !((value == 0 && (byte & 0x40) == 0)
5530                 || (value == -1 && (byte & 0x40) != 0));
5531       if (more)
5532         byte |= 0x80;
5533       CHECKSUM (byte);
5534       if (!more)
5535         break;
5536     }
5537 }
5538
5539 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5540
5541 static void
5542 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5543 {
5544   while (1)
5545     {
5546       unsigned char byte = (value & 0x7f);
5547       value >>= 7;
5548       if (value != 0)
5549         /* More bytes to follow.  */
5550         byte |= 0x80;
5551       CHECKSUM (byte);
5552       if (value == 0)
5553         break;
5554     }
5555 }
5556
5557 /* Checksum the context of the DIE.  This adds the names of any
5558    surrounding namespaces or structures to the checksum.  */
5559
5560 static void
5561 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5562 {
5563   const char *name;
5564   dw_die_ref spec;
5565   int tag = die->die_tag;
5566
5567   if (tag != DW_TAG_namespace
5568       && tag != DW_TAG_structure_type
5569       && tag != DW_TAG_class_type)
5570     return;
5571
5572   name = get_AT_string (die, DW_AT_name);
5573
5574   spec = get_AT_ref (die, DW_AT_specification);
5575   if (spec != NULL)
5576     die = spec;
5577
5578   if (die->die_parent != NULL)
5579     checksum_die_context (die->die_parent, ctx);
5580
5581   CHECKSUM_ULEB128 ('C');
5582   CHECKSUM_ULEB128 (tag);
5583   if (name != NULL)
5584     CHECKSUM_STRING (name);
5585 }
5586
5587 /* Calculate the checksum of a location expression.  */
5588
5589 static inline void
5590 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5591 {
5592   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5593      were emitted as a DW_FORM_sdata instead of a location expression.  */
5594   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5595     {
5596       CHECKSUM_ULEB128 (DW_FORM_sdata);
5597       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5598       return;
5599     }
5600
5601   /* Otherwise, just checksum the raw location expression.  */
5602   while (loc != NULL)
5603     {
5604       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5605       CHECKSUM (loc->dw_loc_oprnd1);
5606       CHECKSUM (loc->dw_loc_oprnd2);
5607       loc = loc->dw_loc_next;
5608     }
5609 }
5610
5611 /* Calculate the checksum of an attribute.  */
5612
5613 static void
5614 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5615                        struct md5_ctx *ctx, int *mark)
5616 {
5617   dw_loc_descr_ref loc;
5618   rtx r;
5619
5620   if (AT_class (at) == dw_val_class_die_ref)
5621     {
5622       dw_die_ref target_die = AT_ref (at);
5623
5624       /* For pointer and reference types, we checksum only the (qualified)
5625          name of the target type (if there is a name).  For friend entries,
5626          we checksum only the (qualified) name of the target type or function.
5627          This allows the checksum to remain the same whether the target type
5628          is complete or not.  */
5629       if ((at->dw_attr == DW_AT_type
5630            && (tag == DW_TAG_pointer_type
5631                || tag == DW_TAG_reference_type
5632                || tag == DW_TAG_rvalue_reference_type
5633                || tag == DW_TAG_ptr_to_member_type))
5634           || (at->dw_attr == DW_AT_friend
5635               && tag == DW_TAG_friend))
5636         {
5637           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5638
5639           if (name_attr != NULL)
5640             {
5641               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5642
5643               if (decl == NULL)
5644                 decl = target_die;
5645               CHECKSUM_ULEB128 ('N');
5646               CHECKSUM_ULEB128 (at->dw_attr);
5647               if (decl->die_parent != NULL)
5648                 checksum_die_context (decl->die_parent, ctx);
5649               CHECKSUM_ULEB128 ('E');
5650               CHECKSUM_STRING (AT_string (name_attr));
5651               return;
5652             }
5653         }
5654
5655       /* For all other references to another DIE, we check to see if the
5656          target DIE has already been visited.  If it has, we emit a
5657          backward reference; if not, we descend recursively.  */
5658       if (target_die->die_mark > 0)
5659         {
5660           CHECKSUM_ULEB128 ('R');
5661           CHECKSUM_ULEB128 (at->dw_attr);
5662           CHECKSUM_ULEB128 (target_die->die_mark);
5663         }
5664       else
5665         {
5666           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5667
5668           if (decl == NULL)
5669             decl = target_die;
5670           target_die->die_mark = ++(*mark);
5671           CHECKSUM_ULEB128 ('T');
5672           CHECKSUM_ULEB128 (at->dw_attr);
5673           if (decl->die_parent != NULL)
5674             checksum_die_context (decl->die_parent, ctx);
5675           die_checksum_ordered (target_die, ctx, mark);
5676         }
5677       return;
5678     }
5679
5680   CHECKSUM_ULEB128 ('A');
5681   CHECKSUM_ULEB128 (at->dw_attr);
5682
5683   switch (AT_class (at))
5684     {
5685     case dw_val_class_const:
5686       CHECKSUM_ULEB128 (DW_FORM_sdata);
5687       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5688       break;
5689
5690     case dw_val_class_unsigned_const:
5691       CHECKSUM_ULEB128 (DW_FORM_sdata);
5692       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5693       break;
5694
5695     case dw_val_class_const_double:
5696       CHECKSUM_ULEB128 (DW_FORM_block);
5697       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5698       CHECKSUM (at->dw_attr_val.v.val_double);
5699       break;
5700
5701     case dw_val_class_vec:
5702       CHECKSUM_ULEB128 (DW_FORM_block);
5703       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5704       CHECKSUM (at->dw_attr_val.v.val_vec);
5705       break;
5706
5707     case dw_val_class_flag:
5708       CHECKSUM_ULEB128 (DW_FORM_flag);
5709       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5710       break;
5711
5712     case dw_val_class_str:
5713       CHECKSUM_ULEB128 (DW_FORM_string);
5714       CHECKSUM_STRING (AT_string (at));
5715       break;
5716
5717     case dw_val_class_addr:
5718       r = AT_addr (at);
5719       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5720       CHECKSUM_ULEB128 (DW_FORM_string);
5721       CHECKSUM_STRING (XSTR (r, 0));
5722       break;
5723
5724     case dw_val_class_offset:
5725       CHECKSUM_ULEB128 (DW_FORM_sdata);
5726       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5727       break;
5728
5729     case dw_val_class_loc:
5730       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5731         loc_checksum_ordered (loc, ctx);
5732       break;
5733
5734     case dw_val_class_fde_ref:
5735     case dw_val_class_lbl_id:
5736     case dw_val_class_lineptr:
5737     case dw_val_class_macptr:
5738     case dw_val_class_high_pc:
5739       break;
5740
5741     case dw_val_class_file:
5742       CHECKSUM_ULEB128 (DW_FORM_string);
5743       CHECKSUM_STRING (AT_file (at)->filename);
5744       break;
5745
5746     case dw_val_class_data8:
5747       CHECKSUM (at->dw_attr_val.v.val_data8);
5748       break;
5749
5750     default:
5751       break;
5752     }
5753 }
5754
5755 struct checksum_attributes
5756 {
5757   dw_attr_ref at_name;
5758   dw_attr_ref at_type;
5759   dw_attr_ref at_friend;
5760   dw_attr_ref at_accessibility;
5761   dw_attr_ref at_address_class;
5762   dw_attr_ref at_allocated;
5763   dw_attr_ref at_artificial;
5764   dw_attr_ref at_associated;
5765   dw_attr_ref at_binary_scale;
5766   dw_attr_ref at_bit_offset;
5767   dw_attr_ref at_bit_size;
5768   dw_attr_ref at_bit_stride;
5769   dw_attr_ref at_byte_size;
5770   dw_attr_ref at_byte_stride;
5771   dw_attr_ref at_const_value;
5772   dw_attr_ref at_containing_type;
5773   dw_attr_ref at_count;
5774   dw_attr_ref at_data_location;
5775   dw_attr_ref at_data_member_location;
5776   dw_attr_ref at_decimal_scale;
5777   dw_attr_ref at_decimal_sign;
5778   dw_attr_ref at_default_value;
5779   dw_attr_ref at_digit_count;
5780   dw_attr_ref at_discr;
5781   dw_attr_ref at_discr_list;
5782   dw_attr_ref at_discr_value;
5783   dw_attr_ref at_encoding;
5784   dw_attr_ref at_endianity;
5785   dw_attr_ref at_explicit;
5786   dw_attr_ref at_is_optional;
5787   dw_attr_ref at_location;
5788   dw_attr_ref at_lower_bound;
5789   dw_attr_ref at_mutable;
5790   dw_attr_ref at_ordering;
5791   dw_attr_ref at_picture_string;
5792   dw_attr_ref at_prototyped;
5793   dw_attr_ref at_small;
5794   dw_attr_ref at_segment;
5795   dw_attr_ref at_string_length;
5796   dw_attr_ref at_threads_scaled;
5797   dw_attr_ref at_upper_bound;
5798   dw_attr_ref at_use_location;
5799   dw_attr_ref at_use_UTF8;
5800   dw_attr_ref at_variable_parameter;
5801   dw_attr_ref at_virtuality;
5802   dw_attr_ref at_visibility;
5803   dw_attr_ref at_vtable_elem_location;
5804 };
5805
5806 /* Collect the attributes that we will want to use for the checksum.  */
5807
5808 static void
5809 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
5810 {
5811   dw_attr_ref a;
5812   unsigned ix;
5813
5814   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
5815     {
5816       switch (a->dw_attr)
5817         {
5818         case DW_AT_name:
5819           attrs->at_name = a;
5820           break;
5821         case DW_AT_type:
5822           attrs->at_type = a;
5823           break;
5824         case DW_AT_friend:
5825           attrs->at_friend = a;
5826           break;
5827         case DW_AT_accessibility:
5828           attrs->at_accessibility = a;
5829           break;
5830         case DW_AT_address_class:
5831           attrs->at_address_class = a;
5832           break;
5833         case DW_AT_allocated:
5834           attrs->at_allocated = a;
5835           break;
5836         case DW_AT_artificial:
5837           attrs->at_artificial = a;
5838           break;
5839         case DW_AT_associated:
5840           attrs->at_associated = a;
5841           break;
5842         case DW_AT_binary_scale:
5843           attrs->at_binary_scale = a;
5844           break;
5845         case DW_AT_bit_offset:
5846           attrs->at_bit_offset = a;
5847           break;
5848         case DW_AT_bit_size:
5849           attrs->at_bit_size = a;
5850           break;
5851         case DW_AT_bit_stride:
5852           attrs->at_bit_stride = a;
5853           break;
5854         case DW_AT_byte_size:
5855           attrs->at_byte_size = a;
5856           break;
5857         case DW_AT_byte_stride:
5858           attrs->at_byte_stride = a;
5859           break;
5860         case DW_AT_const_value:
5861           attrs->at_const_value = a;
5862           break;
5863         case DW_AT_containing_type:
5864           attrs->at_containing_type = a;
5865           break;
5866         case DW_AT_count:
5867           attrs->at_count = a;
5868           break;
5869         case DW_AT_data_location:
5870           attrs->at_data_location = a;
5871           break;
5872         case DW_AT_data_member_location:
5873           attrs->at_data_member_location = a;
5874           break;
5875         case DW_AT_decimal_scale:
5876           attrs->at_decimal_scale = a;
5877           break;
5878         case DW_AT_decimal_sign:
5879           attrs->at_decimal_sign = a;
5880           break;
5881         case DW_AT_default_value:
5882           attrs->at_default_value = a;
5883           break;
5884         case DW_AT_digit_count:
5885           attrs->at_digit_count = a;
5886           break;
5887         case DW_AT_discr:
5888           attrs->at_discr = a;
5889           break;
5890         case DW_AT_discr_list:
5891           attrs->at_discr_list = a;
5892           break;
5893         case DW_AT_discr_value:
5894           attrs->at_discr_value = a;
5895           break;
5896         case DW_AT_encoding:
5897           attrs->at_encoding = a;
5898           break;
5899         case DW_AT_endianity:
5900           attrs->at_endianity = a;
5901           break;
5902         case DW_AT_explicit:
5903           attrs->at_explicit = a;
5904           break;
5905         case DW_AT_is_optional:
5906           attrs->at_is_optional = a;
5907           break;
5908         case DW_AT_location:
5909           attrs->at_location = a;
5910           break;
5911         case DW_AT_lower_bound:
5912           attrs->at_lower_bound = a;
5913           break;
5914         case DW_AT_mutable:
5915           attrs->at_mutable = a;
5916           break;
5917         case DW_AT_ordering:
5918           attrs->at_ordering = a;
5919           break;
5920         case DW_AT_picture_string:
5921           attrs->at_picture_string = a;
5922           break;
5923         case DW_AT_prototyped:
5924           attrs->at_prototyped = a;
5925           break;
5926         case DW_AT_small:
5927           attrs->at_small = a;
5928           break;
5929         case DW_AT_segment:
5930           attrs->at_segment = a;
5931           break;
5932         case DW_AT_string_length:
5933           attrs->at_string_length = a;
5934           break;
5935         case DW_AT_threads_scaled:
5936           attrs->at_threads_scaled = a;
5937           break;
5938         case DW_AT_upper_bound:
5939           attrs->at_upper_bound = a;
5940           break;
5941         case DW_AT_use_location:
5942           attrs->at_use_location = a;
5943           break;
5944         case DW_AT_use_UTF8:
5945           attrs->at_use_UTF8 = a;
5946           break;
5947         case DW_AT_variable_parameter:
5948           attrs->at_variable_parameter = a;
5949           break;
5950         case DW_AT_virtuality:
5951           attrs->at_virtuality = a;
5952           break;
5953         case DW_AT_visibility:
5954           attrs->at_visibility = a;
5955           break;
5956         case DW_AT_vtable_elem_location:
5957           attrs->at_vtable_elem_location = a;
5958           break;
5959         default:
5960           break;
5961         }
5962     }
5963 }
5964
5965 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
5966
5967 static void
5968 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5969 {
5970   dw_die_ref c;
5971   dw_die_ref decl;
5972   struct checksum_attributes attrs;
5973
5974   CHECKSUM_ULEB128 ('D');
5975   CHECKSUM_ULEB128 (die->die_tag);
5976
5977   memset (&attrs, 0, sizeof (attrs));
5978
5979   decl = get_AT_ref (die, DW_AT_specification);
5980   if (decl != NULL)
5981     collect_checksum_attributes (&attrs, decl);
5982   collect_checksum_attributes (&attrs, die);
5983
5984   CHECKSUM_ATTR (attrs.at_name);
5985   CHECKSUM_ATTR (attrs.at_accessibility);
5986   CHECKSUM_ATTR (attrs.at_address_class);
5987   CHECKSUM_ATTR (attrs.at_allocated);
5988   CHECKSUM_ATTR (attrs.at_artificial);
5989   CHECKSUM_ATTR (attrs.at_associated);
5990   CHECKSUM_ATTR (attrs.at_binary_scale);
5991   CHECKSUM_ATTR (attrs.at_bit_offset);
5992   CHECKSUM_ATTR (attrs.at_bit_size);
5993   CHECKSUM_ATTR (attrs.at_bit_stride);
5994   CHECKSUM_ATTR (attrs.at_byte_size);
5995   CHECKSUM_ATTR (attrs.at_byte_stride);
5996   CHECKSUM_ATTR (attrs.at_const_value);
5997   CHECKSUM_ATTR (attrs.at_containing_type);
5998   CHECKSUM_ATTR (attrs.at_count);
5999   CHECKSUM_ATTR (attrs.at_data_location);
6000   CHECKSUM_ATTR (attrs.at_data_member_location);
6001   CHECKSUM_ATTR (attrs.at_decimal_scale);
6002   CHECKSUM_ATTR (attrs.at_decimal_sign);
6003   CHECKSUM_ATTR (attrs.at_default_value);
6004   CHECKSUM_ATTR (attrs.at_digit_count);
6005   CHECKSUM_ATTR (attrs.at_discr);
6006   CHECKSUM_ATTR (attrs.at_discr_list);
6007   CHECKSUM_ATTR (attrs.at_discr_value);
6008   CHECKSUM_ATTR (attrs.at_encoding);
6009   CHECKSUM_ATTR (attrs.at_endianity);
6010   CHECKSUM_ATTR (attrs.at_explicit);
6011   CHECKSUM_ATTR (attrs.at_is_optional);
6012   CHECKSUM_ATTR (attrs.at_location);
6013   CHECKSUM_ATTR (attrs.at_lower_bound);
6014   CHECKSUM_ATTR (attrs.at_mutable);
6015   CHECKSUM_ATTR (attrs.at_ordering);
6016   CHECKSUM_ATTR (attrs.at_picture_string);
6017   CHECKSUM_ATTR (attrs.at_prototyped);
6018   CHECKSUM_ATTR (attrs.at_small);
6019   CHECKSUM_ATTR (attrs.at_segment);
6020   CHECKSUM_ATTR (attrs.at_string_length);
6021   CHECKSUM_ATTR (attrs.at_threads_scaled);
6022   CHECKSUM_ATTR (attrs.at_upper_bound);
6023   CHECKSUM_ATTR (attrs.at_use_location);
6024   CHECKSUM_ATTR (attrs.at_use_UTF8);
6025   CHECKSUM_ATTR (attrs.at_variable_parameter);
6026   CHECKSUM_ATTR (attrs.at_virtuality);
6027   CHECKSUM_ATTR (attrs.at_visibility);
6028   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6029   CHECKSUM_ATTR (attrs.at_type);
6030   CHECKSUM_ATTR (attrs.at_friend);
6031
6032   /* Checksum the child DIEs, except for nested types and member functions.  */
6033   c = die->die_child;
6034   if (c) do {
6035     dw_attr_ref name_attr;
6036
6037     c = c->die_sib;
6038     name_attr = get_AT (c, DW_AT_name);
6039     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6040         && name_attr != NULL)
6041       {
6042         CHECKSUM_ULEB128 ('S');
6043         CHECKSUM_ULEB128 (c->die_tag);
6044         CHECKSUM_STRING (AT_string (name_attr));
6045       }
6046     else
6047       {
6048         /* Mark this DIE so it gets processed when unmarking.  */
6049         if (c->die_mark == 0)
6050           c->die_mark = -1;
6051         die_checksum_ordered (c, ctx, mark);
6052       }
6053   } while (c != die->die_child);
6054
6055   CHECKSUM_ULEB128 (0);
6056 }
6057
6058 #undef CHECKSUM
6059 #undef CHECKSUM_STRING
6060 #undef CHECKSUM_ATTR
6061 #undef CHECKSUM_LEB128
6062 #undef CHECKSUM_ULEB128
6063
6064 /* Generate the type signature for DIE.  This is computed by generating an
6065    MD5 checksum over the DIE's tag, its relevant attributes, and its
6066    children.  Attributes that are references to other DIEs are processed
6067    by recursion, using the MARK field to prevent infinite recursion.
6068    If the DIE is nested inside a namespace or another type, we also
6069    need to include that context in the signature.  The lower 64 bits
6070    of the resulting MD5 checksum comprise the signature.  */
6071
6072 static void
6073 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6074 {
6075   int mark;
6076   const char *name;
6077   unsigned char checksum[16];
6078   struct md5_ctx ctx;
6079   dw_die_ref decl;
6080   dw_die_ref parent;
6081
6082   name = get_AT_string (die, DW_AT_name);
6083   decl = get_AT_ref (die, DW_AT_specification);
6084   parent = get_die_parent (die);
6085
6086   /* First, compute a signature for just the type name (and its surrounding
6087      context, if any.  This is stored in the type unit DIE for link-time
6088      ODR (one-definition rule) checking.  */
6089
6090   if (is_cxx() && name != NULL)
6091     {
6092       md5_init_ctx (&ctx);
6093
6094       /* Checksum the names of surrounding namespaces and structures.  */
6095       if (parent != NULL)
6096         checksum_die_context (parent, &ctx);
6097
6098       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6099       md5_process_bytes (name, strlen (name) + 1, &ctx);
6100       md5_finish_ctx (&ctx, checksum);
6101
6102       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6103     }
6104
6105   /* Next, compute the complete type signature.  */
6106
6107   md5_init_ctx (&ctx);
6108   mark = 1;
6109   die->die_mark = mark;
6110
6111   /* Checksum the names of surrounding namespaces and structures.  */
6112   if (parent != NULL)
6113     checksum_die_context (parent, &ctx);
6114
6115   /* Checksum the DIE and its children.  */
6116   die_checksum_ordered (die, &ctx, &mark);
6117   unmark_all_dies (die);
6118   md5_finish_ctx (&ctx, checksum);
6119
6120   /* Store the signature in the type node and link the type DIE and the
6121      type node together.  */
6122   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6123           DWARF_TYPE_SIGNATURE_SIZE);
6124   die->comdat_type_p = true;
6125   die->die_id.die_type_node = type_node;
6126   type_node->type_die = die;
6127
6128   /* If the DIE is a specification, link its declaration to the type node
6129      as well.  */
6130   if (decl != NULL)
6131     {
6132       decl->comdat_type_p = true;
6133       decl->die_id.die_type_node = type_node;
6134     }
6135 }
6136
6137 /* Do the location expressions look same?  */
6138 static inline int
6139 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6140 {
6141   return loc1->dw_loc_opc == loc2->dw_loc_opc
6142          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6143          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6144 }
6145
6146 /* Do the values look the same?  */
6147 static int
6148 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6149 {
6150   dw_loc_descr_ref loc1, loc2;
6151   rtx r1, r2;
6152
6153   if (v1->val_class != v2->val_class)
6154     return 0;
6155
6156   switch (v1->val_class)
6157     {
6158     case dw_val_class_const:
6159       return v1->v.val_int == v2->v.val_int;
6160     case dw_val_class_unsigned_const:
6161       return v1->v.val_unsigned == v2->v.val_unsigned;
6162     case dw_val_class_const_double:
6163       return v1->v.val_double.high == v2->v.val_double.high
6164              && v1->v.val_double.low == v2->v.val_double.low;
6165     case dw_val_class_vec:
6166       if (v1->v.val_vec.length != v2->v.val_vec.length
6167           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6168         return 0;
6169       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6170                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6171         return 0;
6172       return 1;
6173     case dw_val_class_flag:
6174       return v1->v.val_flag == v2->v.val_flag;
6175     case dw_val_class_str:
6176       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6177
6178     case dw_val_class_addr:
6179       r1 = v1->v.val_addr;
6180       r2 = v2->v.val_addr;
6181       if (GET_CODE (r1) != GET_CODE (r2))
6182         return 0;
6183       return !rtx_equal_p (r1, r2);
6184
6185     case dw_val_class_offset:
6186       return v1->v.val_offset == v2->v.val_offset;
6187
6188     case dw_val_class_loc:
6189       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6190            loc1 && loc2;
6191            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6192         if (!same_loc_p (loc1, loc2, mark))
6193           return 0;
6194       return !loc1 && !loc2;
6195
6196     case dw_val_class_die_ref:
6197       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6198
6199     case dw_val_class_fde_ref:
6200     case dw_val_class_vms_delta:
6201     case dw_val_class_lbl_id:
6202     case dw_val_class_lineptr:
6203     case dw_val_class_macptr:
6204     case dw_val_class_high_pc:
6205       return 1;
6206
6207     case dw_val_class_file:
6208       return v1->v.val_file == v2->v.val_file;
6209
6210     case dw_val_class_data8:
6211       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6212
6213     default:
6214       return 1;
6215     }
6216 }
6217
6218 /* Do the attributes look the same?  */
6219
6220 static int
6221 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6222 {
6223   if (at1->dw_attr != at2->dw_attr)
6224     return 0;
6225
6226   /* We don't care that this was compiled with a different compiler
6227      snapshot; if the output is the same, that's what matters. */
6228   if (at1->dw_attr == DW_AT_producer)
6229     return 1;
6230
6231   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6232 }
6233
6234 /* Do the dies look the same?  */
6235
6236 static int
6237 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6238 {
6239   dw_die_ref c1, c2;
6240   dw_attr_ref a1;
6241   unsigned ix;
6242
6243   /* To avoid infinite recursion.  */
6244   if (die1->die_mark)
6245     return die1->die_mark == die2->die_mark;
6246   die1->die_mark = die2->die_mark = ++(*mark);
6247
6248   if (die1->die_tag != die2->die_tag)
6249     return 0;
6250
6251   if (vec_safe_length (die1->die_attr) != vec_safe_length (die2->die_attr))
6252     return 0;
6253
6254   FOR_EACH_VEC_SAFE_ELT (die1->die_attr, ix, a1)
6255     if (!same_attr_p (a1, &(*die2->die_attr)[ix], mark))
6256       return 0;
6257
6258   c1 = die1->die_child;
6259   c2 = die2->die_child;
6260   if (! c1)
6261     {
6262       if (c2)
6263         return 0;
6264     }
6265   else
6266     for (;;)
6267       {
6268         if (!same_die_p (c1, c2, mark))
6269           return 0;
6270         c1 = c1->die_sib;
6271         c2 = c2->die_sib;
6272         if (c1 == die1->die_child)
6273           {
6274             if (c2 == die2->die_child)
6275               break;
6276             else
6277               return 0;
6278           }
6279     }
6280
6281   return 1;
6282 }
6283
6284 /* Do the dies look the same?  Wrapper around same_die_p.  */
6285
6286 static int
6287 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6288 {
6289   int mark = 0;
6290   int ret = same_die_p (die1, die2, &mark);
6291
6292   unmark_all_dies (die1);
6293   unmark_all_dies (die2);
6294
6295   return ret;
6296 }
6297
6298 /* The prefix to attach to symbols on DIEs in the current comdat debug
6299    info section.  */
6300 static const char *comdat_symbol_id;
6301
6302 /* The index of the current symbol within the current comdat CU.  */
6303 static unsigned int comdat_symbol_number;
6304
6305 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6306    children, and set comdat_symbol_id accordingly.  */
6307
6308 static void
6309 compute_section_prefix (dw_die_ref unit_die)
6310 {
6311   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6312   const char *base = die_name ? lbasename (die_name) : "anonymous";
6313   char *name = XALLOCAVEC (char, strlen (base) + 64);
6314   char *p;
6315   int i, mark;
6316   unsigned char checksum[16];
6317   struct md5_ctx ctx;
6318
6319   /* Compute the checksum of the DIE, then append part of it as hex digits to
6320      the name filename of the unit.  */
6321
6322   md5_init_ctx (&ctx);
6323   mark = 0;
6324   die_checksum (unit_die, &ctx, &mark);
6325   unmark_all_dies (unit_die);
6326   md5_finish_ctx (&ctx, checksum);
6327
6328   sprintf (name, "%s.", base);
6329   clean_symbol_name (name);
6330
6331   p = name + strlen (name);
6332   for (i = 0; i < 4; i++)
6333     {
6334       sprintf (p, "%.2x", checksum[i]);
6335       p += 2;
6336     }
6337
6338   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6339   comdat_symbol_number = 0;
6340 }
6341
6342 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6343
6344 static int
6345 is_type_die (dw_die_ref die)
6346 {
6347   switch (die->die_tag)
6348     {
6349     case DW_TAG_array_type:
6350     case DW_TAG_class_type:
6351     case DW_TAG_interface_type:
6352     case DW_TAG_enumeration_type:
6353     case DW_TAG_pointer_type:
6354     case DW_TAG_reference_type:
6355     case DW_TAG_rvalue_reference_type:
6356     case DW_TAG_string_type:
6357     case DW_TAG_structure_type:
6358     case DW_TAG_subroutine_type:
6359     case DW_TAG_union_type:
6360     case DW_TAG_ptr_to_member_type:
6361     case DW_TAG_set_type:
6362     case DW_TAG_subrange_type:
6363     case DW_TAG_base_type:
6364     case DW_TAG_const_type:
6365     case DW_TAG_file_type:
6366     case DW_TAG_packed_type:
6367     case DW_TAG_volatile_type:
6368     case DW_TAG_typedef:
6369       return 1;
6370     default:
6371       return 0;
6372     }
6373 }
6374
6375 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6376    Basically, we want to choose the bits that are likely to be shared between
6377    compilations (types) and leave out the bits that are specific to individual
6378    compilations (functions).  */
6379
6380 static int
6381 is_comdat_die (dw_die_ref c)
6382 {
6383   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6384      we do for stabs.  The advantage is a greater likelihood of sharing between
6385      objects that don't include headers in the same order (and therefore would
6386      put the base types in a different comdat).  jason 8/28/00 */
6387
6388   if (c->die_tag == DW_TAG_base_type)
6389     return 0;
6390
6391   if (c->die_tag == DW_TAG_pointer_type
6392       || c->die_tag == DW_TAG_reference_type
6393       || c->die_tag == DW_TAG_rvalue_reference_type
6394       || c->die_tag == DW_TAG_const_type
6395       || c->die_tag == DW_TAG_volatile_type)
6396     {
6397       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6398
6399       return t ? is_comdat_die (t) : 0;
6400     }
6401
6402   return is_type_die (c);
6403 }
6404
6405 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6406    compilation unit.  */
6407
6408 static int
6409 is_symbol_die (dw_die_ref c)
6410 {
6411   return (is_type_die (c)
6412           || is_declaration_die (c)
6413           || c->die_tag == DW_TAG_namespace
6414           || c->die_tag == DW_TAG_module);
6415 }
6416
6417 /* Returns true iff C is a compile-unit DIE.  */
6418
6419 static inline bool
6420 is_cu_die (dw_die_ref c)
6421 {
6422   return c && c->die_tag == DW_TAG_compile_unit;
6423 }
6424
6425 /* Returns true iff C is a unit DIE of some sort.  */
6426
6427 static inline bool
6428 is_unit_die (dw_die_ref c)
6429 {
6430   return c && (c->die_tag == DW_TAG_compile_unit
6431                || c->die_tag == DW_TAG_partial_unit
6432                || c->die_tag == DW_TAG_type_unit);
6433 }
6434
6435 /* Returns true iff C is a namespace DIE.  */
6436
6437 static inline bool
6438 is_namespace_die (dw_die_ref c)
6439 {
6440   return c && c->die_tag == DW_TAG_namespace;
6441 }
6442
6443 /* Returns true iff C is a class or structure DIE.  */
6444
6445 static inline bool
6446 is_class_die (dw_die_ref c)
6447 {
6448   return c && (c->die_tag == DW_TAG_class_type
6449                || c->die_tag == DW_TAG_structure_type);
6450 }
6451
6452 static char *
6453 gen_internal_sym (const char *prefix)
6454 {
6455   char buf[256];
6456
6457   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6458   return xstrdup (buf);
6459 }
6460
6461 /* Assign symbols to all worthy DIEs under DIE.  */
6462
6463 static void
6464 assign_symbol_names (dw_die_ref die)
6465 {
6466   dw_die_ref c;
6467
6468   if (is_symbol_die (die) && !die->comdat_type_p)
6469     {
6470       if (comdat_symbol_id)
6471         {
6472           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6473
6474           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6475                    comdat_symbol_id, comdat_symbol_number++);
6476           die->die_id.die_symbol = xstrdup (p);
6477         }
6478       else
6479         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6480     }
6481
6482   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6483 }
6484
6485 struct cu_hash_table_entry
6486 {
6487   dw_die_ref cu;
6488   unsigned min_comdat_num, max_comdat_num;
6489   struct cu_hash_table_entry *next;
6490 };
6491
6492 /* Routines to manipulate hash table of CUs.  */
6493 static hashval_t
6494 htab_cu_hash (const void *of)
6495 {
6496   const struct cu_hash_table_entry *const entry =
6497     (const struct cu_hash_table_entry *) of;
6498
6499   return htab_hash_string (entry->cu->die_id.die_symbol);
6500 }
6501
6502 static int
6503 htab_cu_eq (const void *of1, const void *of2)
6504 {
6505   const struct cu_hash_table_entry *const entry1 =
6506     (const struct cu_hash_table_entry *) of1;
6507   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6508
6509   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6510 }
6511
6512 static void
6513 htab_cu_del (void *what)
6514 {
6515   struct cu_hash_table_entry *next,
6516     *entry = (struct cu_hash_table_entry *) what;
6517
6518   while (entry)
6519     {
6520       next = entry->next;
6521       free (entry);
6522       entry = next;
6523     }
6524 }
6525
6526 /* Check whether we have already seen this CU and set up SYM_NUM
6527    accordingly.  */
6528 static int
6529 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6530 {
6531   struct cu_hash_table_entry dummy;
6532   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6533
6534   dummy.max_comdat_num = 0;
6535
6536   slot = (struct cu_hash_table_entry **)
6537     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6538         INSERT);
6539   entry = *slot;
6540
6541   for (; entry; last = entry, entry = entry->next)
6542     {
6543       if (same_die_p_wrap (cu, entry->cu))
6544         break;
6545     }
6546
6547   if (entry)
6548     {
6549       *sym_num = entry->min_comdat_num;
6550       return 1;
6551     }
6552
6553   entry = XCNEW (struct cu_hash_table_entry);
6554   entry->cu = cu;
6555   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6556   entry->next = *slot;
6557   *slot = entry;
6558
6559   return 0;
6560 }
6561
6562 /* Record SYM_NUM to record of CU in HTABLE.  */
6563 static void
6564 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6565 {
6566   struct cu_hash_table_entry **slot, *entry;
6567
6568   slot = (struct cu_hash_table_entry **)
6569     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6570         NO_INSERT);
6571   entry = *slot;
6572
6573   entry->max_comdat_num = sym_num;
6574 }
6575
6576 /* Traverse the DIE (which is always comp_unit_die), and set up
6577    additional compilation units for each of the include files we see
6578    bracketed by BINCL/EINCL.  */
6579
6580 static void
6581 break_out_includes (dw_die_ref die)
6582 {
6583   dw_die_ref c;
6584   dw_die_ref unit = NULL;
6585   limbo_die_node *node, **pnode;
6586   htab_t cu_hash_table;
6587
6588   c = die->die_child;
6589   if (c) do {
6590     dw_die_ref prev = c;
6591     c = c->die_sib;
6592     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6593            || (unit && is_comdat_die (c)))
6594       {
6595         dw_die_ref next = c->die_sib;
6596
6597         /* This DIE is for a secondary CU; remove it from the main one.  */
6598         remove_child_with_prev (c, prev);
6599
6600         if (c->die_tag == DW_TAG_GNU_BINCL)
6601           unit = push_new_compile_unit (unit, c);
6602         else if (c->die_tag == DW_TAG_GNU_EINCL)
6603           unit = pop_compile_unit (unit);
6604         else
6605           add_child_die (unit, c);
6606         c = next;
6607         if (c == die->die_child)
6608           break;
6609       }
6610   } while (c != die->die_child);
6611
6612 #if 0
6613   /* We can only use this in debugging, since the frontend doesn't check
6614      to make sure that we leave every include file we enter.  */
6615   gcc_assert (!unit);
6616 #endif
6617
6618   assign_symbol_names (die);
6619   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6620   for (node = limbo_die_list, pnode = &limbo_die_list;
6621        node;
6622        node = node->next)
6623     {
6624       int is_dupl;
6625
6626       compute_section_prefix (node->die);
6627       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6628                         &comdat_symbol_number);
6629       assign_symbol_names (node->die);
6630       if (is_dupl)
6631         *pnode = node->next;
6632       else
6633         {
6634           pnode = &node->next;
6635           record_comdat_symbol_number (node->die, cu_hash_table,
6636                 comdat_symbol_number);
6637         }
6638     }
6639   htab_delete (cu_hash_table);
6640 }
6641
6642 /* Return non-zero if this DIE is a declaration.  */
6643
6644 static int
6645 is_declaration_die (dw_die_ref die)
6646 {
6647   dw_attr_ref a;
6648   unsigned ix;
6649
6650   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6651     if (a->dw_attr == DW_AT_declaration)
6652       return 1;
6653
6654   return 0;
6655 }
6656
6657 /* Return non-zero if this DIE is nested inside a subprogram.  */
6658
6659 static int
6660 is_nested_in_subprogram (dw_die_ref die)
6661 {
6662   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6663
6664   if (decl == NULL)
6665     decl = die;
6666   return local_scope_p (decl);
6667 }
6668
6669 /* Return non-zero if this DIE contains a defining declaration of a
6670    subprogram.  */
6671
6672 static int
6673 contains_subprogram_definition (dw_die_ref die)
6674 {
6675   dw_die_ref c;
6676
6677   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6678     return 1;
6679   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6680   return 0;
6681 }
6682
6683 /* Return non-zero if this is a type DIE that should be moved to a
6684    COMDAT .debug_types section.  */
6685
6686 static int
6687 should_move_die_to_comdat (dw_die_ref die)
6688 {
6689   switch (die->die_tag)
6690     {
6691     case DW_TAG_class_type:
6692     case DW_TAG_structure_type:
6693     case DW_TAG_enumeration_type:
6694     case DW_TAG_union_type:
6695       /* Don't move declarations, inlined instances, or types nested in a
6696          subprogram.  */
6697       if (is_declaration_die (die)
6698           || get_AT (die, DW_AT_abstract_origin)
6699           || is_nested_in_subprogram (die))
6700         return 0;
6701       /* A type definition should never contain a subprogram definition.  */
6702       gcc_assert (!contains_subprogram_definition (die));
6703       return 1;
6704     case DW_TAG_array_type:
6705     case DW_TAG_interface_type:
6706     case DW_TAG_pointer_type:
6707     case DW_TAG_reference_type:
6708     case DW_TAG_rvalue_reference_type:
6709     case DW_TAG_string_type:
6710     case DW_TAG_subroutine_type:
6711     case DW_TAG_ptr_to_member_type:
6712     case DW_TAG_set_type:
6713     case DW_TAG_subrange_type:
6714     case DW_TAG_base_type:
6715     case DW_TAG_const_type:
6716     case DW_TAG_file_type:
6717     case DW_TAG_packed_type:
6718     case DW_TAG_volatile_type:
6719     case DW_TAG_typedef:
6720     default:
6721       return 0;
6722     }
6723 }
6724
6725 /* Make a clone of DIE.  */
6726
6727 static dw_die_ref
6728 clone_die (dw_die_ref die)
6729 {
6730   dw_die_ref clone;
6731   dw_attr_ref a;
6732   unsigned ix;
6733
6734   clone = ggc_alloc_cleared_die_node ();
6735   clone->die_tag = die->die_tag;
6736
6737   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6738     add_dwarf_attr (clone, a);
6739
6740   return clone;
6741 }
6742
6743 /* Make a clone of the tree rooted at DIE.  */
6744
6745 static dw_die_ref
6746 clone_tree (dw_die_ref die)
6747 {
6748   dw_die_ref c;
6749   dw_die_ref clone = clone_die (die);
6750
6751   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6752
6753   return clone;
6754 }
6755
6756 /* Make a clone of DIE as a declaration.  */
6757
6758 static dw_die_ref
6759 clone_as_declaration (dw_die_ref die)
6760 {
6761   dw_die_ref clone;
6762   dw_die_ref decl;
6763   dw_attr_ref a;
6764   unsigned ix;
6765
6766   /* If the DIE is already a declaration, just clone it.  */
6767   if (is_declaration_die (die))
6768     return clone_die (die);
6769
6770   /* If the DIE is a specification, just clone its declaration DIE.  */
6771   decl = get_AT_ref (die, DW_AT_specification);
6772   if (decl != NULL)
6773     {
6774       clone = clone_die (decl);
6775       if (die->comdat_type_p)
6776         add_AT_die_ref (clone, DW_AT_signature, die);
6777       return clone;
6778     }
6779
6780   clone = ggc_alloc_cleared_die_node ();
6781   clone->die_tag = die->die_tag;
6782
6783   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
6784     {
6785       /* We don't want to copy over all attributes.
6786          For example we don't want DW_AT_byte_size because otherwise we will no
6787          longer have a declaration and GDB will treat it as a definition.  */
6788
6789       switch (a->dw_attr)
6790         {
6791         case DW_AT_artificial:
6792         case DW_AT_containing_type:
6793         case DW_AT_external:
6794         case DW_AT_name:
6795         case DW_AT_type:
6796         case DW_AT_virtuality:
6797         case DW_AT_linkage_name:
6798         case DW_AT_MIPS_linkage_name:
6799           add_dwarf_attr (clone, a);
6800           break;
6801         case DW_AT_byte_size:
6802         default:
6803           break;
6804         }
6805     }
6806
6807   if (die->comdat_type_p)
6808     add_AT_die_ref (clone, DW_AT_signature, die);
6809
6810   add_AT_flag (clone, DW_AT_declaration, 1);
6811   return clone;
6812 }
6813
6814 /* Copy the declaration context to the new type unit DIE.  This includes
6815    any surrounding namespace or type declarations.  If the DIE has an
6816    AT_specification attribute, it also includes attributes and children
6817    attached to the specification, and returns a pointer to the original
6818    parent of the declaration DIE.  Returns NULL otherwise.  */
6819
6820 static dw_die_ref
6821 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6822 {
6823   dw_die_ref decl;
6824   dw_die_ref new_decl;
6825   dw_die_ref orig_parent = NULL;
6826
6827   decl = get_AT_ref (die, DW_AT_specification);
6828   if (decl == NULL)
6829     decl = die;
6830   else
6831     {
6832       unsigned ix;
6833       dw_die_ref c;
6834       dw_attr_ref a;
6835
6836       /* The original DIE will be changed to a declaration, and must
6837          be moved to be a child of the original declaration DIE.  */
6838       orig_parent = decl->die_parent;
6839
6840       /* Copy the type node pointer from the new DIE to the original
6841          declaration DIE so we can forward references later.  */
6842       decl->comdat_type_p = true;
6843       decl->die_id.die_type_node = die->die_id.die_type_node;
6844
6845       remove_AT (die, DW_AT_specification);
6846
6847       FOR_EACH_VEC_SAFE_ELT (decl->die_attr, ix, a)
6848         {
6849           if (a->dw_attr != DW_AT_name
6850               && a->dw_attr != DW_AT_declaration
6851               && a->dw_attr != DW_AT_external)
6852             add_dwarf_attr (die, a);
6853         }
6854
6855       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
6856     }
6857
6858   if (decl->die_parent != NULL
6859       && !is_unit_die (decl->die_parent))
6860     {
6861       new_decl = copy_ancestor_tree (unit, decl, NULL);
6862       if (new_decl != NULL)
6863         {
6864           remove_AT (new_decl, DW_AT_signature);
6865           add_AT_specification (die, new_decl);
6866         }
6867     }
6868
6869   return orig_parent;
6870 }
6871
6872 /* Generate the skeleton ancestor tree for the given NODE, then clone
6873    the DIE and add the clone into the tree.  */
6874
6875 static void
6876 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
6877 {
6878   if (node->new_die != NULL)
6879     return;
6880
6881   node->new_die = clone_as_declaration (node->old_die);
6882
6883   if (node->parent != NULL)
6884     {
6885       generate_skeleton_ancestor_tree (node->parent);
6886       add_child_die (node->parent->new_die, node->new_die);
6887     }
6888 }
6889
6890 /* Generate a skeleton tree of DIEs containing any declarations that are
6891    found in the original tree.  We traverse the tree looking for declaration
6892    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
6893
6894 static void
6895 generate_skeleton_bottom_up (skeleton_chain_node *parent)
6896 {
6897   skeleton_chain_node node;
6898   dw_die_ref c;
6899   dw_die_ref first;
6900   dw_die_ref prev = NULL;
6901   dw_die_ref next = NULL;
6902
6903   node.parent = parent;
6904
6905   first = c = parent->old_die->die_child;
6906   if (c)
6907     next = c->die_sib;
6908   if (c) do {
6909     if (prev == NULL || prev->die_sib == c)
6910       prev = c;
6911     c = next;
6912     next = (c == first ? NULL : c->die_sib);
6913     node.old_die = c;
6914     node.new_die = NULL;
6915     if (is_declaration_die (c))
6916       {
6917         /* Clone the existing DIE, move the original to the skeleton
6918            tree (which is in the main CU), and put the clone, with
6919            all the original's children, where the original came from.  */
6920         dw_die_ref clone = clone_die (c);
6921         move_all_children (c, clone);
6922
6923         replace_child (c, clone, prev);
6924         generate_skeleton_ancestor_tree (parent);
6925         add_child_die (parent->new_die, c);
6926         node.new_die = c;
6927         c = clone;
6928       }
6929     generate_skeleton_bottom_up (&node);
6930   } while (next != NULL);
6931 }
6932
6933 /* Wrapper function for generate_skeleton_bottom_up.  */
6934
6935 static dw_die_ref
6936 generate_skeleton (dw_die_ref die)
6937 {
6938   skeleton_chain_node node;
6939
6940   node.old_die = die;
6941   node.new_die = NULL;
6942   node.parent = NULL;
6943
6944   /* If this type definition is nested inside another type,
6945      always leave at least a declaration in its place.  */
6946   if (die->die_parent != NULL && is_type_die (die->die_parent))
6947     node.new_die = clone_as_declaration (die);
6948
6949   generate_skeleton_bottom_up (&node);
6950   return node.new_die;
6951 }
6952
6953 /* Remove the CHILD DIE from its parent, possibly replacing it with a cloned
6954    declaration.  The original DIE is moved to a new compile unit so that
6955    existing references to it follow it to the new location.  If any of the
6956    original DIE's descendants is a declaration, we need to replace the
6957    original DIE with a skeleton tree and move the declarations back into the
6958    skeleton tree.  */
6959
6960 static dw_die_ref
6961 remove_child_or_replace_with_skeleton (dw_die_ref unit, dw_die_ref child,
6962                                        dw_die_ref prev)
6963 {
6964   dw_die_ref skeleton, orig_parent;
6965
6966   /* Copy the declaration context to the type unit DIE.  If the returned
6967      ORIG_PARENT is not NULL, the skeleton needs to be added as a child of
6968      that DIE.  */
6969   orig_parent = copy_declaration_context (unit, child);
6970
6971   skeleton = generate_skeleton (child);
6972   if (skeleton == NULL)
6973     remove_child_with_prev (child, prev);
6974   else
6975     {
6976       skeleton->comdat_type_p = true;
6977       skeleton->die_id.die_type_node = child->die_id.die_type_node;
6978
6979       /* If the original DIE was a specification, we need to put
6980          the skeleton under the parent DIE of the declaration.
6981          This leaves the original declaration in the tree, but
6982          it will be pruned later since there are no longer any
6983          references to it.  */
6984       if (orig_parent != NULL)
6985         {
6986           remove_child_with_prev (child, prev);
6987           add_child_die (orig_parent, skeleton);
6988         }
6989       else
6990         replace_child (child, skeleton, prev);
6991     }
6992
6993   return skeleton;
6994 }
6995
6996 /* Traverse the DIE and set up additional .debug_types sections for each
6997    type worthy of being placed in a COMDAT section.  */
6998
6999 static void
7000 break_out_comdat_types (dw_die_ref die)
7001 {
7002   dw_die_ref c;
7003   dw_die_ref first;
7004   dw_die_ref prev = NULL;
7005   dw_die_ref next = NULL;
7006   dw_die_ref unit = NULL;
7007
7008   first = c = die->die_child;
7009   if (c)
7010     next = c->die_sib;
7011   if (c) do {
7012     if (prev == NULL || prev->die_sib == c)
7013       prev = c;
7014     c = next;
7015     next = (c == first ? NULL : c->die_sib);
7016     if (should_move_die_to_comdat (c))
7017       {
7018         dw_die_ref replacement;
7019         comdat_type_node_ref type_node;
7020
7021         /* Create a new type unit DIE as the root for the new tree, and
7022            add it to the list of comdat types.  */
7023         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7024         add_AT_unsigned (unit, DW_AT_language,
7025                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7026         type_node = ggc_alloc_cleared_comdat_type_node ();
7027         type_node->root_die = unit;
7028         type_node->next = comdat_type_list;
7029         comdat_type_list = type_node;
7030
7031         /* Generate the type signature.  */
7032         generate_type_signature (c, type_node);
7033
7034         /* Copy the declaration context, attributes, and children of the
7035            declaration into the new type unit DIE, then remove this DIE
7036            from the main CU (or replace it with a skeleton if necessary).  */
7037         replacement = remove_child_or_replace_with_skeleton (unit, c, prev);
7038         type_node->skeleton_die = replacement;
7039
7040         /* Break out nested types into their own type units.  */
7041         break_out_comdat_types (c);
7042
7043         /* Add the DIE to the new compunit.  */
7044         add_child_die (unit, c);
7045
7046         if (replacement != NULL)
7047           c = replacement;
7048       }
7049     else if (c->die_tag == DW_TAG_namespace
7050              || c->die_tag == DW_TAG_class_type
7051              || c->die_tag == DW_TAG_structure_type
7052              || c->die_tag == DW_TAG_union_type)
7053       {
7054         /* Look for nested types that can be broken out.  */
7055         break_out_comdat_types (c);
7056       }
7057   } while (next != NULL);
7058 }
7059
7060 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7061
7062 struct decl_table_entry
7063 {
7064   dw_die_ref orig;
7065   dw_die_ref copy;
7066 };
7067
7068 /* Routines to manipulate hash table of copied declarations.  */
7069
7070 static hashval_t
7071 htab_decl_hash (const void *of)
7072 {
7073   const struct decl_table_entry *const entry =
7074     (const struct decl_table_entry *) of;
7075
7076   return htab_hash_pointer (entry->orig);
7077 }
7078
7079 static int
7080 htab_decl_eq (const void *of1, const void *of2)
7081 {
7082   const struct decl_table_entry *const entry1 =
7083     (const struct decl_table_entry *) of1;
7084   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7085
7086   return entry1->orig == entry2;
7087 }
7088
7089 static void
7090 htab_decl_del (void *what)
7091 {
7092   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7093
7094   free (entry);
7095 }
7096
7097 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7098    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7099    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7100    to check if the ancestor has already been copied into UNIT.  */
7101
7102 static dw_die_ref
7103 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7104 {
7105   dw_die_ref parent = die->die_parent;
7106   dw_die_ref new_parent = unit;
7107   dw_die_ref copy;
7108   void **slot = NULL;
7109   struct decl_table_entry *entry = NULL;
7110
7111   if (decl_table)
7112     {
7113       /* Check if the entry has already been copied to UNIT.  */
7114       slot = htab_find_slot_with_hash (decl_table, die,
7115                                        htab_hash_pointer (die), INSERT);
7116       if (*slot != HTAB_EMPTY_ENTRY)
7117         {
7118           entry = (struct decl_table_entry *) *slot;
7119           return entry->copy;
7120         }
7121
7122       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7123       entry = XCNEW (struct decl_table_entry);
7124       entry->orig = die;
7125       entry->copy = NULL;
7126       *slot = entry;
7127     }
7128
7129   if (parent != NULL)
7130     {
7131       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7132       if (spec != NULL)
7133         parent = spec;
7134       if (!is_unit_die (parent))
7135         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7136     }
7137
7138   copy = clone_as_declaration (die);
7139   add_child_die (new_parent, copy);
7140
7141   if (decl_table != NULL)
7142     {
7143       /* Record the pointer to the copy.  */
7144       entry->copy = copy;
7145     }
7146
7147   return copy;
7148 }
7149
7150 /* Like clone_tree, but additionally enter all the children into
7151    the hash table decl_table.  */
7152
7153 static dw_die_ref
7154 clone_tree_hash (dw_die_ref die, htab_t decl_table)
7155 {
7156   dw_die_ref c;
7157   dw_die_ref clone = clone_die (die);
7158   struct decl_table_entry *entry;
7159   void **slot = htab_find_slot_with_hash (decl_table, die,
7160                                           htab_hash_pointer (die), INSERT);
7161   /* Assert that DIE isn't in the hash table yet.  If it would be there
7162      before, the ancestors would be necessarily there as well, therefore
7163      clone_tree_hash wouldn't be called.  */
7164   gcc_assert (*slot == HTAB_EMPTY_ENTRY);
7165   entry = XCNEW (struct decl_table_entry);
7166   entry->orig = die;
7167   entry->copy = clone;
7168   *slot = entry;
7169
7170   FOR_EACH_CHILD (die, c,
7171                   add_child_die (clone, clone_tree_hash (c, decl_table)));
7172
7173   return clone;
7174 }
7175
7176 /* Walk the DIE and its children, looking for references to incomplete
7177    or trivial types that are unmarked (i.e., that are not in the current
7178    type_unit).  */
7179
7180 static void
7181 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7182 {
7183   dw_die_ref c;
7184   dw_attr_ref a;
7185   unsigned ix;
7186
7187   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7188     {
7189       if (AT_class (a) == dw_val_class_die_ref)
7190         {
7191           dw_die_ref targ = AT_ref (a);
7192           void **slot;
7193           struct decl_table_entry *entry;
7194
7195           if (targ->die_mark != 0 || targ->comdat_type_p)
7196             continue;
7197
7198           slot = htab_find_slot_with_hash (decl_table, targ,
7199                                            htab_hash_pointer (targ), INSERT);
7200
7201           if (*slot != HTAB_EMPTY_ENTRY)
7202             {
7203               /* TARG has already been copied, so we just need to
7204                  modify the reference to point to the copy.  */
7205               entry = (struct decl_table_entry *) *slot;
7206               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7207             }
7208           else
7209             {
7210               dw_die_ref parent = unit;
7211               dw_die_ref copy = clone_die (targ);
7212
7213               /* Record in DECL_TABLE that TARG has been copied.
7214                  Need to do this now, before the recursive call,
7215                  because DECL_TABLE may be expanded and SLOT
7216                  would no longer be a valid pointer.  */
7217               entry = XCNEW (struct decl_table_entry);
7218               entry->orig = targ;
7219               entry->copy = copy;
7220               *slot = entry;
7221
7222               FOR_EACH_CHILD (targ, c,
7223                               add_child_die (copy,
7224                                              clone_tree_hash (c, decl_table)));
7225
7226               /* Make sure the cloned tree is marked as part of the
7227                  type unit.  */
7228               mark_dies (copy);
7229
7230               /* If TARG has surrounding context, copy its ancestor tree
7231                  into the new type unit.  */
7232               if (targ->die_parent != NULL
7233                   && !is_unit_die (targ->die_parent))
7234                 parent = copy_ancestor_tree (unit, targ->die_parent,
7235                                              decl_table);
7236
7237               add_child_die (parent, copy);
7238               a->dw_attr_val.v.val_die_ref.die = copy;
7239
7240               /* Make sure the newly-copied DIE is walked.  If it was
7241                  installed in a previously-added context, it won't
7242                  get visited otherwise.  */
7243               if (parent != unit)
7244                 {
7245                   /* Find the highest point of the newly-added tree,
7246                      mark each node along the way, and walk from there.  */
7247                   parent->die_mark = 1;
7248                   while (parent->die_parent
7249                          && parent->die_parent->die_mark == 0)
7250                     {
7251                       parent = parent->die_parent;
7252                       parent->die_mark = 1;
7253                     }
7254                   copy_decls_walk (unit, parent, decl_table);
7255                 }
7256             }
7257         }
7258     }
7259
7260   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7261 }
7262
7263 /* Copy declarations for "unworthy" types into the new comdat section.
7264    Incomplete types, modified types, and certain other types aren't broken
7265    out into comdat sections of their own, so they don't have a signature,
7266    and we need to copy the declaration into the same section so that we
7267    don't have an external reference.  */
7268
7269 static void
7270 copy_decls_for_unworthy_types (dw_die_ref unit)
7271 {
7272   htab_t decl_table;
7273
7274   mark_dies (unit);
7275   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7276   copy_decls_walk (unit, unit, decl_table);
7277   htab_delete (decl_table);
7278   unmark_dies (unit);
7279 }
7280
7281 /* Traverse the DIE and add a sibling attribute if it may have the
7282    effect of speeding up access to siblings.  To save some space,
7283    avoid generating sibling attributes for DIE's without children.  */
7284
7285 static void
7286 add_sibling_attributes (dw_die_ref die)
7287 {
7288   dw_die_ref c;
7289
7290   if (! die->die_child)
7291     return;
7292
7293   if (die->die_parent && die != die->die_parent->die_child)
7294     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7295
7296   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7297 }
7298
7299 /* Output all location lists for the DIE and its children.  */
7300
7301 static void
7302 output_location_lists (dw_die_ref die)
7303 {
7304   dw_die_ref c;
7305   dw_attr_ref a;
7306   unsigned ix;
7307
7308   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7309     if (AT_class (a) == dw_val_class_loc_list)
7310       output_loc_list (AT_loc_list (a));
7311
7312   FOR_EACH_CHILD (die, c, output_location_lists (c));
7313 }
7314
7315 /* We want to limit the number of external references, because they are
7316    larger than local references: a relocation takes multiple words, and
7317    even a sig8 reference is always eight bytes, whereas a local reference
7318    can be as small as one byte (though DW_FORM_ref is usually 4 in GCC).
7319    So if we encounter multiple external references to the same type DIE, we
7320    make a local typedef stub for it and redirect all references there.
7321
7322    This is the element of the hash table for keeping track of these
7323    references.  */
7324
7325 struct external_ref
7326 {
7327   dw_die_ref type;
7328   dw_die_ref stub;
7329   unsigned n_refs;
7330 };
7331
7332 /* Hash an external_ref.  */
7333
7334 static hashval_t
7335 hash_external_ref (const void *p)
7336 {
7337   const struct external_ref *r = (const struct external_ref *)p;
7338   return htab_hash_pointer (r->type);
7339 }
7340
7341 /* Compare external_refs.  */
7342
7343 static int
7344 external_ref_eq (const void *p1, const void *p2)
7345 {
7346   const struct external_ref *r1 = (const struct external_ref *)p1;
7347   const struct external_ref *r2 = (const struct external_ref *)p2;
7348   return r1->type == r2->type;
7349 }
7350
7351 /* Return a pointer to the external_ref for references to DIE.  */
7352
7353 static struct external_ref *
7354 lookup_external_ref (htab_t map, dw_die_ref die)
7355 {
7356   struct external_ref ref, *ref_p;
7357   void ** slot;
7358
7359   ref.type = die;
7360   slot = htab_find_slot (map, &ref, INSERT);
7361   if (*slot != HTAB_EMPTY_ENTRY)
7362     return (struct external_ref *) *slot;
7363
7364   ref_p = XCNEW (struct external_ref);
7365   ref_p->type = die;
7366   *slot = ref_p;
7367   return ref_p;
7368 }
7369
7370 /* Subroutine of optimize_external_refs, below.
7371
7372    If we see a type skeleton, record it as our stub.  If we see external
7373    references, remember how many we've seen.  */
7374
7375 static void
7376 optimize_external_refs_1 (dw_die_ref die, htab_t map)
7377 {
7378   dw_die_ref c;
7379   dw_attr_ref a;
7380   unsigned ix;
7381   struct external_ref *ref_p;
7382
7383   if (is_type_die (die)
7384       && (c = get_AT_ref (die, DW_AT_signature)))
7385     {
7386       /* This is a local skeleton; use it for local references.  */
7387       ref_p = lookup_external_ref (map, c);
7388       ref_p->stub = die;
7389     }
7390
7391   /* Scan the DIE references, and remember any that refer to DIEs from
7392      other CUs (i.e. those which are not marked).  */
7393   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7394     if (AT_class (a) == dw_val_class_die_ref
7395         && (c = AT_ref (a))->die_mark == 0
7396         && is_type_die (c))
7397       {
7398         ref_p = lookup_external_ref (map, c);
7399         ref_p->n_refs++;
7400       }
7401
7402   FOR_EACH_CHILD (die, c, optimize_external_refs_1 (c, map));
7403 }
7404
7405 /* htab_traverse callback function for optimize_external_refs, below.  SLOT
7406    points to an external_ref, DATA is the CU we're processing.  If we don't
7407    already have a local stub, and we have multiple refs, build a stub.  */
7408
7409 static int
7410 build_local_stub (void **slot, void *data)
7411 {
7412   struct external_ref *ref_p = (struct external_ref *)*slot;
7413
7414   if (ref_p->stub == NULL && ref_p->n_refs > 1 && !dwarf_strict)
7415     {
7416       /* We have multiple references to this type, so build a small stub.
7417          Both of these forms are a bit dodgy from the perspective of the
7418          DWARF standard, since technically they should have names.  */
7419       dw_die_ref cu = (dw_die_ref) data;
7420       dw_die_ref type = ref_p->type;
7421       dw_die_ref stub = NULL;
7422
7423       if (type->comdat_type_p)
7424         {
7425           /* If we refer to this type via sig8, use AT_signature.  */
7426           stub = new_die (type->die_tag, cu, NULL_TREE);
7427           add_AT_die_ref (stub, DW_AT_signature, type);
7428         }
7429       else
7430         {
7431           /* Otherwise, use a typedef with no name.  */
7432           stub = new_die (DW_TAG_typedef, cu, NULL_TREE);
7433           add_AT_die_ref (stub, DW_AT_type, type);
7434         }
7435
7436       stub->die_mark++;
7437       ref_p->stub = stub;
7438     }
7439   return 1;
7440 }
7441
7442 /* DIE is a unit; look through all the DIE references to see if there are
7443    any external references to types, and if so, create local stubs for
7444    them which will be applied in build_abbrev_table.  This is useful because
7445    references to local DIEs are smaller.  */
7446
7447 static htab_t
7448 optimize_external_refs (dw_die_ref die)
7449 {
7450   htab_t map = htab_create (10, hash_external_ref, external_ref_eq, free);
7451   optimize_external_refs_1 (die, map);
7452   htab_traverse (map, build_local_stub, die);
7453   return map;
7454 }
7455
7456 /* The format of each DIE (and its attribute value pairs) is encoded in an
7457    abbreviation table.  This routine builds the abbreviation table and assigns
7458    a unique abbreviation id for each abbreviation entry.  The children of each
7459    die are visited recursively.  */
7460
7461 static void
7462 build_abbrev_table (dw_die_ref die, htab_t extern_map)
7463 {
7464   unsigned long abbrev_id;
7465   unsigned int n_alloc;
7466   dw_die_ref c;
7467   dw_attr_ref a;
7468   unsigned ix;
7469
7470   /* Scan the DIE references, and replace any that refer to
7471      DIEs from other CUs (i.e. those which are not marked) with
7472      the local stubs we built in optimize_external_refs.  */
7473   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7474     if (AT_class (a) == dw_val_class_die_ref
7475         && (c = AT_ref (a))->die_mark == 0)
7476       {
7477         struct external_ref *ref_p;
7478         gcc_assert (AT_ref (a)->comdat_type_p || AT_ref (a)->die_id.die_symbol);
7479
7480         ref_p = lookup_external_ref (extern_map, c);
7481         if (ref_p->stub && ref_p->stub != die)
7482           change_AT_die_ref (a, ref_p->stub);
7483         else
7484           /* We aren't changing this reference, so mark it external.  */
7485           set_AT_ref_external (a, 1);
7486       }
7487
7488   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7489     {
7490       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7491       dw_attr_ref die_a, abbrev_a;
7492       unsigned ix;
7493       bool ok = true;
7494
7495       if (abbrev->die_tag != die->die_tag)
7496         continue;
7497       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7498         continue;
7499
7500       if (vec_safe_length (abbrev->die_attr) != vec_safe_length (die->die_attr))
7501         continue;
7502
7503       FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, die_a)
7504         {
7505           abbrev_a = &(*abbrev->die_attr)[ix];
7506           if ((abbrev_a->dw_attr != die_a->dw_attr)
7507               || (value_format (abbrev_a) != value_format (die_a)))
7508             {
7509               ok = false;
7510               break;
7511             }
7512         }
7513       if (ok)
7514         break;
7515     }
7516
7517   if (abbrev_id >= abbrev_die_table_in_use)
7518     {
7519       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7520         {
7521           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7522           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7523                                             n_alloc);
7524
7525           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7526                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7527           abbrev_die_table_allocated = n_alloc;
7528         }
7529
7530       ++abbrev_die_table_in_use;
7531       abbrev_die_table[abbrev_id] = die;
7532     }
7533
7534   die->die_abbrev = abbrev_id;
7535   FOR_EACH_CHILD (die, c, build_abbrev_table (c, extern_map));
7536 }
7537 \f
7538 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7539
7540 static int
7541 constant_size (unsigned HOST_WIDE_INT value)
7542 {
7543   int log;
7544
7545   if (value == 0)
7546     log = 0;
7547   else
7548     log = floor_log2 (value);
7549
7550   log = log / 8;
7551   log = 1 << (floor_log2 (log) + 1);
7552
7553   return log;
7554 }
7555
7556 /* Return the size of a DIE as it is represented in the
7557    .debug_info section.  */
7558
7559 static unsigned long
7560 size_of_die (dw_die_ref die)
7561 {
7562   unsigned long size = 0;
7563   dw_attr_ref a;
7564   unsigned ix;
7565   enum dwarf_form form;
7566
7567   size += size_of_uleb128 (die->die_abbrev);
7568   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7569     {
7570       switch (AT_class (a))
7571         {
7572         case dw_val_class_addr:
7573           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7574             {
7575               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7576               size += size_of_uleb128 (AT_index (a));
7577             }
7578           else
7579             size += DWARF2_ADDR_SIZE;
7580           break;
7581         case dw_val_class_offset:
7582           size += DWARF_OFFSET_SIZE;
7583           break;
7584         case dw_val_class_loc:
7585           {
7586             unsigned long lsize = size_of_locs (AT_loc (a));
7587
7588             /* Block length.  */
7589             if (dwarf_version >= 4)
7590               size += size_of_uleb128 (lsize);
7591             else
7592               size += constant_size (lsize);
7593             size += lsize;
7594           }
7595           break;
7596         case dw_val_class_loc_list:
7597           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7598             {
7599               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7600               size += size_of_uleb128 (AT_index (a));
7601             }
7602           else
7603             size += DWARF_OFFSET_SIZE;
7604           break;
7605         case dw_val_class_range_list:
7606           size += DWARF_OFFSET_SIZE;
7607           break;
7608         case dw_val_class_const:
7609           size += size_of_sleb128 (AT_int (a));
7610           break;
7611         case dw_val_class_unsigned_const:
7612           {
7613             int csize = constant_size (AT_unsigned (a));
7614             if (dwarf_version == 3
7615                 && a->dw_attr == DW_AT_data_member_location
7616                 && csize >= 4)
7617               size += size_of_uleb128 (AT_unsigned (a));
7618             else
7619               size += csize;
7620           }
7621           break;
7622         case dw_val_class_const_double:
7623           size += HOST_BITS_PER_DOUBLE_INT / HOST_BITS_PER_CHAR;
7624           if (HOST_BITS_PER_WIDE_INT >= 64)
7625             size++; /* block */
7626           break;
7627         case dw_val_class_vec:
7628           size += constant_size (a->dw_attr_val.v.val_vec.length
7629                                  * a->dw_attr_val.v.val_vec.elt_size)
7630                   + a->dw_attr_val.v.val_vec.length
7631                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7632           break;
7633         case dw_val_class_flag:
7634           if (dwarf_version >= 4)
7635             /* Currently all add_AT_flag calls pass in 1 as last argument,
7636                so DW_FORM_flag_present can be used.  If that ever changes,
7637                we'll need to use DW_FORM_flag and have some optimization
7638                in build_abbrev_table that will change those to
7639                DW_FORM_flag_present if it is set to 1 in all DIEs using
7640                the same abbrev entry.  */
7641             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7642           else
7643             size += 1;
7644           break;
7645         case dw_val_class_die_ref:
7646           if (AT_ref_external (a))
7647             {
7648               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7649                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7650                  is sized by target address length, whereas in DWARF3
7651                  it's always sized as an offset.  */
7652               if (use_debug_types)
7653                 size += DWARF_TYPE_SIGNATURE_SIZE;
7654               else if (dwarf_version == 2)
7655                 size += DWARF2_ADDR_SIZE;
7656               else
7657                 size += DWARF_OFFSET_SIZE;
7658             }
7659           else
7660             size += DWARF_OFFSET_SIZE;
7661           break;
7662         case dw_val_class_fde_ref:
7663           size += DWARF_OFFSET_SIZE;
7664           break;
7665         case dw_val_class_lbl_id:
7666           if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
7667             {
7668               gcc_assert (AT_index (a) != NO_INDEX_ASSIGNED);
7669               size += size_of_uleb128 (AT_index (a));
7670             }
7671           else
7672             size += DWARF2_ADDR_SIZE;
7673           break;
7674         case dw_val_class_lineptr:
7675         case dw_val_class_macptr:
7676           size += DWARF_OFFSET_SIZE;
7677           break;
7678         case dw_val_class_str:
7679           form = AT_string_form (a);
7680           if (form == DW_FORM_strp)
7681             size += DWARF_OFFSET_SIZE;
7682          else if (form == DW_FORM_GNU_str_index)
7683             size += size_of_uleb128 (AT_index (a));
7684           else
7685             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7686           break;
7687         case dw_val_class_file:
7688           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7689           break;
7690         case dw_val_class_data8:
7691           size += 8;
7692           break;
7693         case dw_val_class_vms_delta:
7694           size += DWARF_OFFSET_SIZE;
7695           break;
7696         case dw_val_class_high_pc:
7697           size += DWARF2_ADDR_SIZE;
7698           break;
7699         default:
7700           gcc_unreachable ();
7701         }
7702     }
7703
7704   return size;
7705 }
7706
7707 /* Size the debugging information associated with a given DIE.  Visits the
7708    DIE's children recursively.  Updates the global variable next_die_offset, on
7709    each time through.  Uses the current value of next_die_offset to update the
7710    die_offset field in each DIE.  */
7711
7712 static void
7713 calc_die_sizes (dw_die_ref die)
7714 {
7715   dw_die_ref c;
7716
7717   gcc_assert (die->die_offset == 0
7718               || (unsigned long int) die->die_offset == next_die_offset);
7719   die->die_offset = next_die_offset;
7720   next_die_offset += size_of_die (die);
7721
7722   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7723
7724   if (die->die_child != NULL)
7725     /* Count the null byte used to terminate sibling lists.  */
7726     next_die_offset += 1;
7727 }
7728
7729 /* Size just the base type children at the start of the CU.
7730    This is needed because build_abbrev needs to size locs
7731    and sizing of type based stack ops needs to know die_offset
7732    values for the base types.  */
7733
7734 static void
7735 calc_base_type_die_sizes (void)
7736 {
7737   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7738   unsigned int i;
7739   dw_die_ref base_type;
7740 #if ENABLE_ASSERT_CHECKING
7741   dw_die_ref prev = comp_unit_die ()->die_child;
7742 #endif
7743
7744   die_offset += size_of_die (comp_unit_die ());
7745   for (i = 0; base_types.iterate (i, &base_type); i++)
7746     {
7747 #if ENABLE_ASSERT_CHECKING
7748       gcc_assert (base_type->die_offset == 0
7749                   && prev->die_sib == base_type
7750                   && base_type->die_child == NULL
7751                   && base_type->die_abbrev);
7752       prev = base_type;
7753 #endif
7754       base_type->die_offset = die_offset;
7755       die_offset += size_of_die (base_type);
7756     }
7757 }
7758
7759 /* Set the marks for a die and its children.  We do this so
7760    that we know whether or not a reference needs to use FORM_ref_addr; only
7761    DIEs in the same CU will be marked.  We used to clear out the offset
7762    and use that as the flag, but ran into ordering problems.  */
7763
7764 static void
7765 mark_dies (dw_die_ref die)
7766 {
7767   dw_die_ref c;
7768
7769   gcc_assert (!die->die_mark);
7770
7771   die->die_mark = 1;
7772   FOR_EACH_CHILD (die, c, mark_dies (c));
7773 }
7774
7775 /* Clear the marks for a die and its children.  */
7776
7777 static void
7778 unmark_dies (dw_die_ref die)
7779 {
7780   dw_die_ref c;
7781
7782   if (! use_debug_types)
7783     gcc_assert (die->die_mark);
7784
7785   die->die_mark = 0;
7786   FOR_EACH_CHILD (die, c, unmark_dies (c));
7787 }
7788
7789 /* Clear the marks for a die, its children and referred dies.  */
7790
7791 static void
7792 unmark_all_dies (dw_die_ref die)
7793 {
7794   dw_die_ref c;
7795   dw_attr_ref a;
7796   unsigned ix;
7797
7798   if (!die->die_mark)
7799     return;
7800   die->die_mark = 0;
7801
7802   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7803
7804   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
7805     if (AT_class (a) == dw_val_class_die_ref)
7806       unmark_all_dies (AT_ref (a));
7807 }
7808
7809 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7810    generated for the compilation unit.  */
7811
7812 static unsigned long
7813 size_of_pubnames (vec<pubname_entry, va_gc> *names)
7814 {
7815   unsigned long size;
7816   unsigned i;
7817   pubname_ref p;
7818
7819   size = DWARF_PUBNAMES_HEADER_SIZE;
7820   FOR_EACH_VEC_ELT (*names, i, p)
7821     if (names != pubtype_table
7822         || p->die->die_offset != 0
7823         || !flag_eliminate_unused_debug_types)
7824       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7825
7826   size += DWARF_OFFSET_SIZE;
7827   return size;
7828 }
7829
7830 /* Return the size of the information in the .debug_aranges section.  */
7831
7832 static unsigned long
7833 size_of_aranges (void)
7834 {
7835   unsigned long size;
7836
7837   size = DWARF_ARANGES_HEADER_SIZE;
7838
7839   /* Count the address/length pair for this compilation unit.  */
7840   if (text_section_used)
7841     size += 2 * DWARF2_ADDR_SIZE;
7842   if (cold_text_section_used)
7843     size += 2 * DWARF2_ADDR_SIZE;
7844   if (have_multiple_function_sections)
7845     {
7846       unsigned fde_idx;
7847       dw_fde_ref fde;
7848
7849       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
7850         {
7851           if (DECL_IGNORED_P (fde->decl))
7852             continue;
7853           if (!fde->in_std_section)
7854             size += 2 * DWARF2_ADDR_SIZE;
7855           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7856             size += 2 * DWARF2_ADDR_SIZE;
7857         }
7858     }
7859
7860   /* Count the two zero words used to terminated the address range table.  */
7861   size += 2 * DWARF2_ADDR_SIZE;
7862   return size;
7863 }
7864 \f
7865 /* Select the encoding of an attribute value.  */
7866
7867 static enum dwarf_form
7868 value_format (dw_attr_ref a)
7869 {
7870   switch (AT_class (a))
7871     {
7872     case dw_val_class_addr:
7873       /* Only very few attributes allow DW_FORM_addr.  */
7874       switch (a->dw_attr)
7875         {
7876         case DW_AT_low_pc:
7877         case DW_AT_high_pc:
7878         case DW_AT_entry_pc:
7879         case DW_AT_trampoline:
7880           return (AT_index (a) == NOT_INDEXED
7881                   ? DW_FORM_addr : DW_FORM_GNU_addr_index);
7882         default:
7883           break;
7884         }
7885       switch (DWARF2_ADDR_SIZE)
7886         {
7887         case 1:
7888           return DW_FORM_data1;
7889         case 2:
7890           return DW_FORM_data2;
7891         case 4:
7892           return DW_FORM_data4;
7893         case 8:
7894           return DW_FORM_data8;
7895         default:
7896           gcc_unreachable ();
7897         }
7898     case dw_val_class_range_list:
7899     case dw_val_class_loc_list:
7900       if (dwarf_version >= 4)
7901         return DW_FORM_sec_offset;
7902       /* FALLTHRU */
7903     case dw_val_class_vms_delta:
7904     case dw_val_class_offset:
7905       switch (DWARF_OFFSET_SIZE)
7906         {
7907         case 4:
7908           return DW_FORM_data4;
7909         case 8:
7910           return DW_FORM_data8;
7911         default:
7912           gcc_unreachable ();
7913         }
7914     case dw_val_class_loc:
7915       if (dwarf_version >= 4)
7916         return DW_FORM_exprloc;
7917       switch (constant_size (size_of_locs (AT_loc (a))))
7918         {
7919         case 1:
7920           return DW_FORM_block1;
7921         case 2:
7922           return DW_FORM_block2;
7923         case 4:
7924           return DW_FORM_block4;
7925         default:
7926           gcc_unreachable ();
7927         }
7928     case dw_val_class_const:
7929       return DW_FORM_sdata;
7930     case dw_val_class_unsigned_const:
7931       switch (constant_size (AT_unsigned (a)))
7932         {
7933         case 1:
7934           return DW_FORM_data1;
7935         case 2:
7936           return DW_FORM_data2;
7937         case 4:
7938           /* In DWARF3 DW_AT_data_member_location with
7939              DW_FORM_data4 or DW_FORM_data8 is a loclistptr, not
7940              constant, so we need to use DW_FORM_udata if we need
7941              a large constant.  */
7942           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7943             return DW_FORM_udata;
7944           return DW_FORM_data4;
7945         case 8:
7946           if (dwarf_version == 3 && a->dw_attr == DW_AT_data_member_location)
7947             return DW_FORM_udata;
7948           return DW_FORM_data8;
7949         default:
7950           gcc_unreachable ();
7951         }
7952     case dw_val_class_const_double:
7953       switch (HOST_BITS_PER_WIDE_INT)
7954         {
7955         case 8:
7956           return DW_FORM_data2;
7957         case 16:
7958           return DW_FORM_data4;
7959         case 32:
7960           return DW_FORM_data8;
7961         case 64:
7962         default:
7963           return DW_FORM_block1;
7964         }
7965     case dw_val_class_vec:
7966       switch (constant_size (a->dw_attr_val.v.val_vec.length
7967                              * a->dw_attr_val.v.val_vec.elt_size))
7968         {
7969         case 1:
7970           return DW_FORM_block1;
7971         case 2:
7972           return DW_FORM_block2;
7973         case 4:
7974           return DW_FORM_block4;
7975         default:
7976           gcc_unreachable ();
7977         }
7978     case dw_val_class_flag:
7979       if (dwarf_version >= 4)
7980         {
7981           /* Currently all add_AT_flag calls pass in 1 as last argument,
7982              so DW_FORM_flag_present can be used.  If that ever changes,
7983              we'll need to use DW_FORM_flag and have some optimization
7984              in build_abbrev_table that will change those to
7985              DW_FORM_flag_present if it is set to 1 in all DIEs using
7986              the same abbrev entry.  */
7987           gcc_assert (a->dw_attr_val.v.val_flag == 1);
7988           return DW_FORM_flag_present;
7989         }
7990       return DW_FORM_flag;
7991     case dw_val_class_die_ref:
7992       if (AT_ref_external (a))
7993         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7994       else
7995         return DW_FORM_ref;
7996     case dw_val_class_fde_ref:
7997       return DW_FORM_data;
7998     case dw_val_class_lbl_id:
7999       return (AT_index (a) == NOT_INDEXED
8000               ? DW_FORM_addr : DW_FORM_GNU_addr_index);
8001     case dw_val_class_lineptr:
8002     case dw_val_class_macptr:
8003       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8004     case dw_val_class_str:
8005       return AT_string_form (a);
8006     case dw_val_class_file:
8007       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8008         {
8009         case 1:
8010           return DW_FORM_data1;
8011         case 2:
8012           return DW_FORM_data2;
8013         case 4:
8014           return DW_FORM_data4;
8015         default:
8016           gcc_unreachable ();
8017         }
8018
8019     case dw_val_class_data8:
8020       return DW_FORM_data8;
8021
8022     case dw_val_class_high_pc:
8023       switch (DWARF2_ADDR_SIZE)
8024         {
8025         case 1:
8026           return DW_FORM_data1;
8027         case 2:
8028           return DW_FORM_data2;
8029         case 4:
8030           return DW_FORM_data4;
8031         case 8:
8032           return DW_FORM_data8;
8033         default:
8034           gcc_unreachable ();
8035         }
8036
8037     default:
8038       gcc_unreachable ();
8039     }
8040 }
8041
8042 /* Output the encoding of an attribute value.  */
8043
8044 static void
8045 output_value_format (dw_attr_ref a)
8046 {
8047   enum dwarf_form form = value_format (a);
8048
8049   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8050 }
8051
8052 /* Given a die and id, produce the appropriate abbreviations.  */
8053
8054 static void
8055 output_die_abbrevs (unsigned long abbrev_id, dw_die_ref abbrev)
8056 {
8057   unsigned ix;
8058   dw_attr_ref a_attr;
8059
8060   dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8061   dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8062                                dwarf_tag_name (abbrev->die_tag));
8063
8064   if (abbrev->die_child != NULL)
8065     dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8066   else
8067     dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8068
8069   for (ix = 0; vec_safe_iterate (abbrev->die_attr, ix, &a_attr); ix++)
8070     {
8071       dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8072                                    dwarf_attr_name (a_attr->dw_attr));
8073       output_value_format (a_attr);
8074     }
8075
8076   dw2_asm_output_data (1, 0, NULL);
8077   dw2_asm_output_data (1, 0, NULL);
8078 }
8079
8080
8081 /* Output the .debug_abbrev section which defines the DIE abbreviation
8082    table.  */
8083
8084 static void
8085 output_abbrev_section (void)
8086 {
8087   unsigned long abbrev_id;
8088
8089   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8090     output_die_abbrevs (abbrev_id, abbrev_die_table[abbrev_id]);
8091
8092   /* Terminate the table.  */
8093   dw2_asm_output_data (1, 0, NULL);
8094 }
8095
8096 /* Output a symbol we can use to refer to this DIE from another CU.  */
8097
8098 static inline void
8099 output_die_symbol (dw_die_ref die)
8100 {
8101   const char *sym = die->die_id.die_symbol;
8102
8103   gcc_assert (!die->comdat_type_p);
8104
8105   if (sym == 0)
8106     return;
8107
8108   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8109     /* We make these global, not weak; if the target doesn't support
8110        .linkonce, it doesn't support combining the sections, so debugging
8111        will break.  */
8112     targetm.asm_out.globalize_label (asm_out_file, sym);
8113
8114   ASM_OUTPUT_LABEL (asm_out_file, sym);
8115 }
8116
8117 /* Return a new location list, given the begin and end range, and the
8118    expression.  */
8119
8120 static inline dw_loc_list_ref
8121 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8122               const char *section)
8123 {
8124   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8125
8126   retlist->begin = begin;
8127   retlist->begin_entry = NULL;
8128   retlist->end = end;
8129   retlist->expr = expr;
8130   retlist->section = section;
8131
8132   return retlist;
8133 }
8134
8135 /* Generate a new internal symbol for this location list node, if it
8136    hasn't got one yet.  */
8137
8138 static inline void
8139 gen_llsym (dw_loc_list_ref list)
8140 {
8141   gcc_assert (!list->ll_symbol);
8142   list->ll_symbol = gen_internal_sym ("LLST");
8143 }
8144
8145 /* Output the location list given to us.  */
8146
8147 static void
8148 output_loc_list (dw_loc_list_ref list_head)
8149 {
8150   dw_loc_list_ref curr = list_head;
8151
8152   if (list_head->emitted)
8153     return;
8154   list_head->emitted = true;
8155
8156   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8157
8158   /* Walk the location list, and output each range + expression.  */
8159   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8160     {
8161       unsigned long size;
8162       /* Don't output an entry that starts and ends at the same address.  */
8163       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8164         continue;
8165       size = size_of_locs (curr->expr);
8166       /* If the expression is too large, drop it on the floor.  We could
8167          perhaps put it into DW_TAG_dwarf_procedure and refer to that
8168          in the expression, but >= 64KB expressions for a single value
8169          in a single range are unlikely very useful.  */
8170       if (size > 0xffff)
8171         continue;
8172       if (dwarf_split_debug_info)
8173         {
8174           dw2_asm_output_data (1, DW_LLE_GNU_start_length_entry,
8175                                "Location list start/length entry (%s)",
8176                                list_head->ll_symbol);
8177           dw2_asm_output_data_uleb128 (curr->begin_entry->index,
8178                                        "Location list range start index (%s)",
8179                                        curr->begin);
8180           /* The length field is 4 bytes.  If we ever need to support
8181             an 8-byte length, we can add a new DW_LLE code or fall back
8182             to DW_LLE_GNU_start_end_entry.  */
8183           dw2_asm_output_delta (4, curr->end, curr->begin,
8184                                 "Location list range length (%s)",
8185                                 list_head->ll_symbol);
8186         }
8187       else if (!have_multiple_function_sections)
8188         {
8189           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8190                                 "Location list begin address (%s)",
8191                                 list_head->ll_symbol);
8192           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8193                                 "Location list end address (%s)",
8194                                 list_head->ll_symbol);
8195         }
8196       else
8197         {
8198           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8199                                "Location list begin address (%s)",
8200                                list_head->ll_symbol);
8201           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8202                                "Location list end address (%s)",
8203                                list_head->ll_symbol);
8204         }
8205
8206       /* Output the block length for this list of location operations.  */
8207       gcc_assert (size <= 0xffff);
8208       dw2_asm_output_data (2, size, "%s", "Location expression size");
8209
8210       output_loc_sequence (curr->expr, -1);
8211     }
8212
8213   if (dwarf_split_debug_info)
8214     dw2_asm_output_data (1, DW_LLE_GNU_end_of_list_entry,
8215                          "Location list terminator (%s)",
8216                          list_head->ll_symbol);
8217   else
8218     {
8219       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8220                            "Location list terminator begin (%s)",
8221                            list_head->ll_symbol);
8222       dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8223                            "Location list terminator end (%s)",
8224                            list_head->ll_symbol);
8225     }
8226 }
8227
8228 /* Output a range_list offset into the debug_range section.  Emit a
8229    relocated reference if val_entry is NULL, otherwise, emit an
8230    indirect reference.  */
8231
8232 static void
8233 output_range_list_offset (dw_attr_ref a)
8234 {
8235   const char *name = dwarf_attr_name (a->dw_attr);
8236
8237   if (a->dw_attr_val.val_entry == RELOCATED_OFFSET)
8238     {
8239       char *p = strchr (ranges_section_label, '\0');
8240       sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX, a->dw_attr_val.v.val_offset);
8241       dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8242                              debug_ranges_section, "%s", name);
8243       *p = '\0';
8244     }
8245   else
8246     dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8247                          "%s (offset from %s)", name, ranges_section_label);
8248 }
8249
8250 /* Output the offset into the debug_loc section.  */
8251
8252 static void
8253 output_loc_list_offset (dw_attr_ref a)
8254 {
8255   char *sym = AT_loc_list (a)->ll_symbol;
8256
8257   gcc_assert (sym);
8258   if (dwarf_split_debug_info)
8259     dw2_asm_output_delta (DWARF_OFFSET_SIZE, sym, loc_section_label,
8260                           "%s", dwarf_attr_name (a->dw_attr));
8261   else
8262     dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8263                            "%s", dwarf_attr_name (a->dw_attr));
8264 }
8265
8266 /* Output an attribute's index or value appropriately.  */
8267
8268 static void
8269 output_attr_index_or_value (dw_attr_ref a)
8270 {
8271   const char *name = dwarf_attr_name (a->dw_attr);
8272
8273   if (dwarf_split_debug_info && AT_index (a) != NOT_INDEXED)
8274     {
8275       dw2_asm_output_data_uleb128 (AT_index (a), "%s", name);
8276       return;
8277     }
8278   switch (AT_class (a))
8279     {
8280       case dw_val_class_addr:
8281         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8282         break;
8283       case dw_val_class_high_pc:
8284       case dw_val_class_lbl_id:
8285         dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8286         break;
8287       case dw_val_class_loc_list:
8288         output_loc_list_offset (a);
8289         break;
8290       default:
8291         gcc_unreachable ();
8292     }
8293 }
8294
8295 /* Output a type signature.  */
8296
8297 static inline void
8298 output_signature (const char *sig, const char *name)
8299 {
8300   int i;
8301
8302   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8303     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8304 }
8305
8306 /* Output the DIE and its attributes.  Called recursively to generate
8307    the definitions of each child DIE.  */
8308
8309 static void
8310 output_die (dw_die_ref die)
8311 {
8312   dw_attr_ref a;
8313   dw_die_ref c;
8314   unsigned long size;
8315   unsigned ix;
8316
8317   /* If someone in another CU might refer to us, set up a symbol for
8318      them to point to.  */
8319   if (! die->comdat_type_p && die->die_id.die_symbol)
8320     output_die_symbol (die);
8321
8322   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8323                                (unsigned long)die->die_offset,
8324                                dwarf_tag_name (die->die_tag));
8325
8326   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
8327     {
8328       const char *name = dwarf_attr_name (a->dw_attr);
8329
8330       switch (AT_class (a))
8331         {
8332         case dw_val_class_addr:
8333           output_attr_index_or_value (a);
8334           break;
8335
8336         case dw_val_class_offset:
8337           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8338                                "%s", name);
8339           break;
8340
8341         case dw_val_class_range_list:
8342           output_range_list_offset (a);
8343           break;
8344
8345         case dw_val_class_loc:
8346           size = size_of_locs (AT_loc (a));
8347
8348           /* Output the block length for this list of location operations.  */
8349           if (dwarf_version >= 4)
8350             dw2_asm_output_data_uleb128 (size, "%s", name);
8351           else
8352             dw2_asm_output_data (constant_size (size), size, "%s", name);
8353
8354           output_loc_sequence (AT_loc (a), -1);
8355           break;
8356
8357         case dw_val_class_const:
8358           /* ??? It would be slightly more efficient to use a scheme like is
8359              used for unsigned constants below, but gdb 4.x does not sign
8360              extend.  Gdb 5.x does sign extend.  */
8361           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8362           break;
8363
8364         case dw_val_class_unsigned_const:
8365           {
8366             int csize = constant_size (AT_unsigned (a));
8367             if (dwarf_version == 3
8368                 && a->dw_attr == DW_AT_data_member_location
8369                 && csize >= 4)
8370               dw2_asm_output_data_uleb128 (AT_unsigned (a), "%s", name);
8371             else
8372               dw2_asm_output_data (csize, AT_unsigned (a), "%s", name);
8373           }
8374           break;
8375
8376         case dw_val_class_const_double:
8377           {
8378             unsigned HOST_WIDE_INT first, second;
8379
8380             if (HOST_BITS_PER_WIDE_INT >= 64)
8381               dw2_asm_output_data (1,
8382                                    HOST_BITS_PER_DOUBLE_INT
8383                                    / HOST_BITS_PER_CHAR,
8384                                    NULL);
8385
8386             if (WORDS_BIG_ENDIAN)
8387               {
8388                 first = a->dw_attr_val.v.val_double.high;
8389                 second = a->dw_attr_val.v.val_double.low;
8390               }
8391             else
8392               {
8393                 first = a->dw_attr_val.v.val_double.low;
8394                 second = a->dw_attr_val.v.val_double.high;
8395               }
8396
8397             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8398                                  first, "%s", name);
8399             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8400                                  second, NULL);
8401           }
8402           break;
8403
8404         case dw_val_class_vec:
8405           {
8406             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8407             unsigned int len = a->dw_attr_val.v.val_vec.length;
8408             unsigned int i;
8409             unsigned char *p;
8410
8411             dw2_asm_output_data (constant_size (len * elt_size),
8412                                  len * elt_size, "%s", name);
8413             if (elt_size > sizeof (HOST_WIDE_INT))
8414               {
8415                 elt_size /= 2;
8416                 len *= 2;
8417               }
8418             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8419                  i < len;
8420                  i++, p += elt_size)
8421               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8422                                    "fp or vector constant word %u", i);
8423             break;
8424           }
8425
8426         case dw_val_class_flag:
8427           if (dwarf_version >= 4)
8428             {
8429               /* Currently all add_AT_flag calls pass in 1 as last argument,
8430                  so DW_FORM_flag_present can be used.  If that ever changes,
8431                  we'll need to use DW_FORM_flag and have some optimization
8432                  in build_abbrev_table that will change those to
8433                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8434                  the same abbrev entry.  */
8435               gcc_assert (AT_flag (a) == 1);
8436               if (flag_debug_asm)
8437                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8438                          ASM_COMMENT_START, name);
8439               break;
8440             }
8441           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8442           break;
8443
8444         case dw_val_class_loc_list:
8445           output_attr_index_or_value (a);
8446           break;
8447
8448         case dw_val_class_die_ref:
8449           if (AT_ref_external (a))
8450             {
8451               if (AT_ref (a)->comdat_type_p)
8452                 {
8453                   comdat_type_node_ref type_node =
8454                     AT_ref (a)->die_id.die_type_node;
8455
8456                   gcc_assert (type_node);
8457                   output_signature (type_node->signature, name);
8458                 }
8459               else
8460                 {
8461                   const char *sym = AT_ref (a)->die_id.die_symbol;
8462                   int size;
8463
8464                   gcc_assert (sym);
8465                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8466                      length, whereas in DWARF3 it's always sized as an
8467                      offset.  */
8468                   if (dwarf_version == 2)
8469                     size = DWARF2_ADDR_SIZE;
8470                   else
8471                     size = DWARF_OFFSET_SIZE;
8472                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8473                                          name);
8474                 }
8475             }
8476           else
8477             {
8478               gcc_assert (AT_ref (a)->die_offset);
8479               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8480                                    "%s", name);
8481             }
8482           break;
8483
8484         case dw_val_class_fde_ref:
8485           {
8486             char l1[20];
8487
8488             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8489                                          a->dw_attr_val.v.val_fde_index * 2);
8490             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8491                                    "%s", name);
8492           }
8493           break;
8494
8495         case dw_val_class_vms_delta:
8496           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8497                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8498                                     "%s", name);
8499           break;
8500
8501         case dw_val_class_lbl_id:
8502           output_attr_index_or_value (a);
8503           break;
8504
8505         case dw_val_class_lineptr:
8506           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8507                                  debug_line_section, "%s", name);
8508           break;
8509
8510         case dw_val_class_macptr:
8511           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8512                                  debug_macinfo_section, "%s", name);
8513           break;
8514
8515         case dw_val_class_str:
8516           if (a->dw_attr_val.v.val_str->form == DW_FORM_strp)
8517             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8518                                    a->dw_attr_val.v.val_str->label,
8519                                    debug_str_section,
8520                                    "%s: \"%s\"", name, AT_string (a));
8521           else if (a->dw_attr_val.v.val_str->form == DW_FORM_GNU_str_index)
8522             dw2_asm_output_data_uleb128 (AT_index (a),
8523                                          "%s: \"%s\"", name, AT_string (a));
8524           else
8525             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8526           break;
8527
8528         case dw_val_class_file:
8529           {
8530             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8531
8532             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8533                                  a->dw_attr_val.v.val_file->filename);
8534             break;
8535           }
8536
8537         case dw_val_class_data8:
8538           {
8539             int i;
8540
8541             for (i = 0; i < 8; i++)
8542               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8543                                    i == 0 ? "%s" : NULL, name);
8544             break;
8545           }
8546
8547         case dw_val_class_high_pc:
8548           dw2_asm_output_delta (DWARF2_ADDR_SIZE, AT_lbl (a),
8549                                 get_AT_low_pc (die), "DW_AT_high_pc");
8550           break;
8551
8552         default:
8553           gcc_unreachable ();
8554         }
8555     }
8556
8557   FOR_EACH_CHILD (die, c, output_die (c));
8558
8559   /* Add null byte to terminate sibling list.  */
8560   if (die->die_child != NULL)
8561     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8562                          (unsigned long) die->die_offset);
8563 }
8564
8565 /* Output the compilation unit that appears at the beginning of the
8566    .debug_info section, and precedes the DIE descriptions.  */
8567
8568 static void
8569 output_compilation_unit_header (void)
8570 {
8571   int ver = dwarf_version;
8572
8573   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8574     dw2_asm_output_data (4, 0xffffffff,
8575       "Initial length escape value indicating 64-bit DWARF extension");
8576   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8577                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8578                        "Length of Compilation Unit Info");
8579   dw2_asm_output_data (2, ver, "DWARF version number");
8580   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8581                          debug_abbrev_section,
8582                          "Offset Into Abbrev. Section");
8583   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8584 }
8585
8586 /* Output the compilation unit DIE and its children.  */
8587
8588 static void
8589 output_comp_unit (dw_die_ref die, int output_if_empty)
8590 {
8591   const char *secname, *oldsym;
8592   char *tmp;
8593   htab_t extern_map;
8594
8595   /* Unless we are outputting main CU, we may throw away empty ones.  */
8596   if (!output_if_empty && die->die_child == NULL)
8597     return;
8598
8599   /* Even if there are no children of this DIE, we must output the information
8600      about the compilation unit.  Otherwise, on an empty translation unit, we
8601      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8602      will then complain when examining the file.  First mark all the DIEs in
8603      this CU so we know which get local refs.  */
8604   mark_dies (die);
8605
8606   extern_map = optimize_external_refs (die);
8607
8608   build_abbrev_table (die, extern_map);
8609
8610   htab_delete (extern_map);
8611
8612   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8613   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8614   calc_die_sizes (die);
8615
8616   oldsym = die->die_id.die_symbol;
8617   if (oldsym)
8618     {
8619       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8620
8621       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8622       secname = tmp;
8623       die->die_id.die_symbol = NULL;
8624       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8625     }
8626   else
8627     {
8628       switch_to_section (debug_info_section);
8629       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8630       info_section_emitted = true;
8631     }
8632
8633   /* Output debugging information.  */
8634   output_compilation_unit_header ();
8635   output_die (die);
8636
8637   /* Leave the marks on the main CU, so we can check them in
8638      output_pubnames.  */
8639   if (oldsym)
8640     {
8641       unmark_dies (die);
8642       die->die_id.die_symbol = oldsym;
8643     }
8644 }
8645
8646 /* Whether to generate the DWARF accelerator tables in .debug_pubnames
8647    and .debug_pubtypes.  This is configured per-target, but can be
8648    overridden by the -gpubnames or -gno-pubnames options.  */
8649
8650 static inline bool
8651 want_pubnames (void)
8652 {
8653   return (debug_generate_pub_sections != -1
8654           ? debug_generate_pub_sections
8655           : targetm.want_debug_pub_sections);
8656 }
8657
8658 /* Add the DW_AT_GNU_pubnames and DW_AT_GNU_pubtypes attributes.  */
8659
8660 static void
8661 add_AT_pubnames (dw_die_ref die)
8662 {
8663   if (want_pubnames ())
8664     add_AT_flag (die, DW_AT_GNU_pubnames, 1);
8665 }
8666
8667 /* Helper function to generate top-level dies for skeleton debug_info and
8668    debug_types.  */
8669
8670 static void
8671 add_top_level_skeleton_die_attrs (dw_die_ref die)
8672 {
8673   const char *dwo_file_name = concat (aux_base_name, ".dwo", NULL);
8674   dw_attr_ref attr;
8675
8676   add_comp_dir_attribute (die);
8677   add_AT_string (die, DW_AT_GNU_dwo_name, dwo_file_name);
8678   /* The specification suggests that these attributes be inline to avoid
8679      having a .debug_str section.  We know that they exist in the die because
8680      we just added them.  */
8681   attr = get_AT (die, DW_AT_GNU_dwo_name);
8682   attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8683   attr = get_AT (die, DW_AT_comp_dir);
8684   attr->dw_attr_val.v.val_str->form = DW_FORM_string;
8685
8686   add_AT_pubnames (die);
8687   add_AT_lineptr (die, DW_AT_GNU_addr_base, debug_addr_section_label);
8688 }
8689
8690 /* Return the single type-unit die for skeleton type units.  */
8691
8692 static dw_die_ref
8693 get_skeleton_type_unit (void)
8694 {
8695   /* For dwarf_split_debug_sections with use_type info, all type units in the
8696      skeleton sections have identical dies (but different headers).  This
8697      single die will be output many times.  */
8698
8699   static dw_die_ref skeleton_type_unit = NULL;
8700
8701   if (skeleton_type_unit == NULL)
8702     {
8703       skeleton_type_unit = new_die (DW_TAG_type_unit, NULL, NULL);
8704       add_top_level_skeleton_die_attrs (skeleton_type_unit);
8705       skeleton_type_unit->die_abbrev = SKELETON_TYPE_DIE_ABBREV;
8706     }
8707   return skeleton_type_unit;
8708 }
8709
8710 /* Output skeleton debug sections that point to the dwo file.  */
8711
8712 static void
8713 output_skeleton_debug_sections (dw_die_ref comp_unit)
8714 {
8715   /* These attributes will be found in the full debug_info section.  */
8716   remove_AT (comp_unit, DW_AT_producer);
8717   remove_AT (comp_unit, DW_AT_language);
8718
8719   /* Add attributes common to skeleton compile_units and type_units.  */
8720   add_top_level_skeleton_die_attrs (comp_unit);
8721
8722   switch_to_section (debug_skeleton_info_section);
8723   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_info_section_label);
8724
8725   /* Produce the skeleton compilation-unit header.  This one differs enough from
8726      a normal CU header that it's better not to call output_compilation_unit
8727      header.  */
8728   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8729     dw2_asm_output_data (4, 0xffffffff,
8730       "Initial length escape value indicating 64-bit DWARF extension");
8731
8732   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8733                        DWARF_COMPILE_UNIT_HEADER_SIZE
8734                        - DWARF_INITIAL_LENGTH_SIZE
8735                        + size_of_die (comp_unit),
8736                       "Length of Compilation Unit Info");
8737   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8738   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_abbrev_section_label,
8739                          debug_abbrev_section,
8740                          "Offset Into Abbrev. Section");
8741   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8742
8743   comp_unit->die_abbrev = SKELETON_COMP_DIE_ABBREV;
8744   output_die (comp_unit);
8745
8746   /* Build the skeleton debug_abbrev section.  */
8747   switch_to_section (debug_skeleton_abbrev_section);
8748   ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_abbrev_section_label);
8749
8750   output_die_abbrevs (SKELETON_COMP_DIE_ABBREV, comp_unit);
8751   if (use_debug_types)
8752     output_die_abbrevs (SKELETON_TYPE_DIE_ABBREV, get_skeleton_type_unit ());
8753
8754   dw2_asm_output_data (1, 0, "end of skeleton .debug_abbrev");
8755 }
8756
8757 /* Output a comdat type unit DIE and its children.  */
8758
8759 static void
8760 output_comdat_type_unit (comdat_type_node *node)
8761 {
8762   const char *secname;
8763   char *tmp;
8764   int i;
8765 #if defined (OBJECT_FORMAT_ELF)
8766   tree comdat_key;
8767 #endif
8768   htab_t extern_map;
8769
8770   /* First mark all the DIEs in this CU so we know which get local refs.  */
8771   mark_dies (node->root_die);
8772
8773   extern_map = optimize_external_refs (node->root_die);
8774
8775   build_abbrev_table (node->root_die, extern_map);
8776
8777   htab_delete (extern_map);
8778
8779   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8780   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8781   calc_die_sizes (node->root_die);
8782
8783 #if defined (OBJECT_FORMAT_ELF)
8784   if (!dwarf_split_debug_info)
8785     secname = ".debug_types";
8786   else
8787     secname = ".debug_types.dwo";
8788
8789   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8790   sprintf (tmp, "wt.");
8791   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8792     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8793   comdat_key = get_identifier (tmp);
8794   targetm.asm_out.named_section (secname,
8795                                  SECTION_DEBUG | SECTION_LINKONCE,
8796                                  comdat_key);
8797 #else
8798   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8799   sprintf (tmp, ".gnu.linkonce.wt.");
8800   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8801     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8802   secname = tmp;
8803   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8804 #endif
8805
8806   /* Output debugging information.  */
8807   output_compilation_unit_header ();
8808   output_signature (node->signature, "Type Signature");
8809   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8810                        "Offset to Type DIE");
8811   output_die (node->root_die);
8812
8813   unmark_dies (node->root_die);
8814
8815 #if defined (OBJECT_FORMAT_ELF)
8816   if (dwarf_split_debug_info)
8817     {
8818       /* Produce the skeleton type-unit header.  */
8819       const char *secname = ".debug_types";
8820
8821       targetm.asm_out.named_section (secname,
8822                                      SECTION_DEBUG | SECTION_LINKONCE,
8823                                      comdat_key);
8824       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8825         dw2_asm_output_data (4, 0xffffffff,
8826           "Initial length escape value indicating 64-bit DWARF extension");
8827
8828       dw2_asm_output_data (DWARF_OFFSET_SIZE,
8829                            DWARF_COMPILE_UNIT_HEADER_SIZE
8830                            - DWARF_INITIAL_LENGTH_SIZE
8831                            + size_of_die (get_skeleton_type_unit ())
8832                            + DWARF_TYPE_SIGNATURE_SIZE + DWARF_OFFSET_SIZE,
8833                            "Length of Type Unit Info");
8834       dw2_asm_output_data (2, dwarf_version, "DWARF version number");
8835       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8836                              debug_skeleton_abbrev_section_label,
8837                              debug_abbrev_section,
8838                              "Offset Into Abbrev. Section");
8839       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8840       output_signature (node->signature, "Type Signature");
8841       dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "Offset to Type DIE");
8842
8843       output_die (get_skeleton_type_unit ());
8844     }
8845 #endif
8846 }
8847
8848 /* Return the DWARF2/3 pubname associated with a decl.  */
8849
8850 static const char *
8851 dwarf2_name (tree decl, int scope)
8852 {
8853   if (DECL_NAMELESS (decl))
8854     return NULL;
8855   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8856 }
8857
8858 /* Add a new entry to .debug_pubnames if appropriate.  */
8859
8860 static void
8861 add_pubname_string (const char *str, dw_die_ref die)
8862 {
8863   pubname_entry e;
8864
8865   e.die = die;
8866   e.name = xstrdup (str);
8867   vec_safe_push (pubname_table, e);
8868 }
8869
8870 static void
8871 add_pubname (tree decl, dw_die_ref die)
8872 {
8873   if (!want_pubnames ())
8874     return;
8875
8876   /* Don't add items to the table when we expect that the consumer will have
8877      just read the enclosing die.  For example, if the consumer is looking at a
8878      class_member, it will either be inside the class already, or will have just
8879      looked up the class to find the member.  Either way, searching the class is
8880      faster than searching the index.  */
8881   if ((TREE_PUBLIC (decl) && !class_scope_p (die->die_parent))
8882       || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8883     {
8884       const char *name = dwarf2_name (decl, 1);
8885
8886       if (name)
8887         add_pubname_string (name, die);
8888     }
8889 }
8890
8891 /* Add an enumerator to the pubnames section.  */
8892
8893 static void
8894 add_enumerator_pubname (const char *scope_name, dw_die_ref die)
8895 {
8896   pubname_entry e;
8897
8898   gcc_assert (scope_name);
8899   e.name = concat (scope_name, get_AT_string (die, DW_AT_name), NULL);
8900   e.die = die;
8901   vec_safe_push (pubname_table, e);
8902 }
8903
8904 /* Add a new entry to .debug_pubtypes if appropriate.  */
8905
8906 static void
8907 add_pubtype (tree decl, dw_die_ref die)
8908 {
8909   pubname_entry e;
8910
8911   if (!want_pubnames ())
8912     return;
8913
8914   if ((TREE_PUBLIC (decl)
8915        || is_cu_die (die->die_parent) || is_namespace_die (die->die_parent))
8916       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8917     {
8918       tree scope = NULL;
8919       const char *scope_name = "";
8920       const char *sep = is_cxx () ? "::" : ".";
8921       const char *name;
8922
8923       scope = TYPE_P (decl) ? TYPE_CONTEXT (decl) : NULL;
8924       if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
8925         {
8926           scope_name = lang_hooks.dwarf_name (scope, 1);
8927           if (scope_name != NULL && scope_name[0] != '\0')
8928             scope_name = concat (scope_name, sep, NULL);
8929           else
8930             scope_name = "";
8931         }
8932
8933       if (TYPE_P (decl))
8934         name = type_tag (decl);
8935       else
8936         name = lang_hooks.dwarf_name (decl, 1);
8937
8938       /* If we don't have a name for the type, there's no point in adding
8939          it to the table.  */
8940       if (name != NULL && name[0] != '\0')
8941         {
8942           e.die = die;
8943           e.name = concat (scope_name, name, NULL);
8944           vec_safe_push (pubtype_table, e);
8945         }
8946
8947       /* Although it might be more consistent to add the pubinfo for the
8948          enumerators as their dies are created, they should only be added if the
8949          enum type meets the criteria above.  So rather than re-check the parent
8950          enum type whenever an enumerator die is created, just output them all
8951          here.  This isn't protected by the name conditional because anonymous
8952          enums don't have names.  */
8953       if (die->die_tag == DW_TAG_enumeration_type)
8954         {
8955           dw_die_ref c;
8956
8957           FOR_EACH_CHILD (die, c, add_enumerator_pubname (scope_name, c));
8958         }
8959     }
8960 }
8961
8962 /* Output the public names table used to speed up access to externally
8963    visible names; or the public types table used to find type definitions.  */
8964
8965 static void
8966 output_pubnames (vec<pubname_entry, va_gc> *names)
8967 {
8968   unsigned i;
8969   unsigned long pubnames_length = size_of_pubnames (names);
8970   pubname_ref pub;
8971
8972   if (!want_pubnames () || !info_section_emitted)
8973     return;
8974   if (names == pubname_table)
8975     switch_to_section (debug_pubnames_section);
8976   else
8977     switch_to_section (debug_pubtypes_section);
8978   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8979     dw2_asm_output_data (4, 0xffffffff,
8980       "Initial length escape value indicating 64-bit DWARF extension");
8981   if (names == pubname_table)
8982     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8983                          "Length of Public Names Info");
8984   else
8985     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8986                          "Length of Public Type Names Info");
8987   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8988   dw2_asm_output_data (2, 2, "DWARF Version");
8989   if (dwarf_split_debug_info)
8990     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
8991                            debug_skeleton_info_section,
8992                            "Offset of Compilation Unit Info");
8993   else
8994     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8995                            debug_info_section,
8996                            "Offset of Compilation Unit Info");
8997   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8998                        "Compilation Unit Length");
8999
9000   FOR_EACH_VEC_ELT (*names, i, pub)
9001     {
9002       /* Enumerator names are part of the pubname table, but the parent
9003          DW_TAG_enumeration_type die may have been pruned.  Don't output
9004          them if that is the case.  */
9005       if (pub->die->die_tag == DW_TAG_enumerator && !pub->die->die_mark)
9006         continue;
9007
9008       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9009       if (names == pubname_table)
9010         gcc_assert (pub->die->die_mark);
9011
9012       if (names != pubtype_table
9013           || pub->die->die_offset != 0
9014           || !flag_eliminate_unused_debug_types)
9015         {
9016           dw_offset die_offset = pub->die->die_offset;
9017
9018           /* If we're putting types in their own .debug_types sections,
9019              the .debug_pubtypes table will still point to the compile
9020              unit (not the type unit), so we want to use the offset of
9021              the skeleton DIE (if there is one).  */
9022           if (pub->die->comdat_type_p && names == pubtype_table)
9023             {
9024               comdat_type_node_ref type_node = pub->die->die_id.die_type_node;
9025
9026               if (type_node != NULL)
9027                 die_offset = (type_node->skeleton_die != NULL
9028                               ? type_node->skeleton_die->die_offset
9029                               : 0);
9030             }
9031
9032           dw2_asm_output_data (DWARF_OFFSET_SIZE, die_offset, "DIE offset");
9033
9034           dw2_asm_output_nstring (pub->name, -1, "external name");
9035         }
9036     }
9037
9038   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9039 }
9040
9041 /* Output the information that goes into the .debug_aranges table.
9042    Namely, define the beginning and ending address range of the
9043    text section generated for this compilation unit.  */
9044
9045 static void
9046 output_aranges (unsigned long aranges_length)
9047 {
9048   unsigned i;
9049
9050   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9051     dw2_asm_output_data (4, 0xffffffff,
9052       "Initial length escape value indicating 64-bit DWARF extension");
9053   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9054                        "Length of Address Ranges Info");
9055   /* Version number for aranges is still 2, even in DWARF3.  */
9056   dw2_asm_output_data (2, 2, "DWARF Version");
9057   if (dwarf_split_debug_info)
9058     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_skeleton_info_section_label,
9059                            debug_skeleton_info_section,
9060                            "Offset of Compilation Unit Info");
9061   else
9062     dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9063                            debug_info_section,
9064                            "Offset of Compilation Unit Info");
9065   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9066   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9067
9068   /* We need to align to twice the pointer size here.  */
9069   if (DWARF_ARANGES_PAD_SIZE)
9070     {
9071       /* Pad using a 2 byte words so that padding is correct for any
9072          pointer size.  */
9073       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9074                            2 * DWARF2_ADDR_SIZE);
9075       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9076         dw2_asm_output_data (2, 0, NULL);
9077     }
9078
9079   /* It is necessary not to output these entries if the sections were
9080      not used; if the sections were not used, the length will be 0 and
9081      the address may end up as 0 if the section is discarded by ld
9082      --gc-sections, leaving an invalid (0, 0) entry that can be
9083      confused with the terminator.  */
9084   if (text_section_used)
9085     {
9086       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9087       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9088                             text_section_label, "Length");
9089     }
9090   if (cold_text_section_used)
9091     {
9092       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9093                            "Address");
9094       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9095                             cold_text_section_label, "Length");
9096     }
9097
9098   if (have_multiple_function_sections)
9099     {
9100       unsigned fde_idx;
9101       dw_fde_ref fde;
9102
9103       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
9104         {
9105           if (DECL_IGNORED_P (fde->decl))
9106             continue;
9107           if (!fde->in_std_section)
9108             {
9109               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9110                                    "Address");
9111               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9112                                     fde->dw_fde_begin, "Length");
9113             }
9114           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9115             {
9116               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9117                                    "Address");
9118               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9119                                     fde->dw_fde_second_begin, "Length");
9120             }
9121         }
9122     }
9123
9124   /* Output the terminator words.  */
9125   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9126   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9127 }
9128
9129 /* Add a new entry to .debug_ranges.  Return the offset at which it
9130    was placed.  */
9131
9132 static unsigned int
9133 add_ranges_num (int num)
9134 {
9135   unsigned int in_use = ranges_table_in_use;
9136
9137   if (in_use == ranges_table_allocated)
9138     {
9139       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9140       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9141                                     ranges_table_allocated);
9142       memset (ranges_table + ranges_table_in_use, 0,
9143               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9144     }
9145
9146   ranges_table[in_use].num = num;
9147   ranges_table_in_use = in_use + 1;
9148
9149   return in_use * 2 * DWARF2_ADDR_SIZE;
9150 }
9151
9152 /* Add a new entry to .debug_ranges corresponding to a block, or a
9153    range terminator if BLOCK is NULL.  */
9154
9155 static unsigned int
9156 add_ranges (const_tree block)
9157 {
9158   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9159 }
9160
9161 /* Add a new entry to .debug_ranges corresponding to a pair of labels.
9162    When using dwarf_split_debug_info, address attributes in dies destined
9163    for the final executable should be direct references--setting the
9164    parameter force_direct ensures this behavior.  */
9165
9166 static void
9167 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9168                       bool *added, bool force_direct)
9169 {
9170   unsigned int in_use = ranges_by_label_in_use;
9171   unsigned int offset;
9172
9173   if (in_use == ranges_by_label_allocated)
9174     {
9175       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9176       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9177                                        ranges_by_label,
9178                                        ranges_by_label_allocated);
9179       memset (ranges_by_label + ranges_by_label_in_use, 0,
9180               RANGES_TABLE_INCREMENT
9181               * sizeof (struct dw_ranges_by_label_struct));
9182     }
9183
9184   ranges_by_label[in_use].begin = begin;
9185   ranges_by_label[in_use].end = end;
9186   ranges_by_label_in_use = in_use + 1;
9187
9188   offset = add_ranges_num (-(int)in_use - 1);
9189   if (!*added)
9190     {
9191       add_AT_range_list (die, DW_AT_ranges, offset, force_direct);
9192       *added = true;
9193     }
9194 }
9195
9196 static void
9197 output_ranges (void)
9198 {
9199   unsigned i;
9200   static const char *const start_fmt = "Offset %#x";
9201   const char *fmt = start_fmt;
9202
9203   for (i = 0; i < ranges_table_in_use; i++)
9204     {
9205       int block_num = ranges_table[i].num;
9206
9207       if (block_num > 0)
9208         {
9209           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9210           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9211
9212           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9213           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9214
9215           /* If all code is in the text section, then the compilation
9216              unit base address defaults to DW_AT_low_pc, which is the
9217              base of the text section.  */
9218           if (!have_multiple_function_sections)
9219             {
9220               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9221                                     text_section_label,
9222                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9223               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9224                                     text_section_label, NULL);
9225             }
9226
9227           /* Otherwise, the compilation unit base address is zero,
9228              which allows us to use absolute addresses, and not worry
9229              about whether the target supports cross-section
9230              arithmetic.  */
9231           else
9232             {
9233               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9234                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9235               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9236             }
9237
9238           fmt = NULL;
9239         }
9240
9241       /* Negative block_num stands for an index into ranges_by_label.  */
9242       else if (block_num < 0)
9243         {
9244           int lab_idx = - block_num - 1;
9245
9246           if (!have_multiple_function_sections)
9247             {
9248               gcc_unreachable ();
9249 #if 0
9250               /* If we ever use add_ranges_by_labels () for a single
9251                  function section, all we have to do is to take out
9252                  the #if 0 above.  */
9253               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9254                                     ranges_by_label[lab_idx].begin,
9255                                     text_section_label,
9256                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9257               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9258                                     ranges_by_label[lab_idx].end,
9259                                     text_section_label, NULL);
9260 #endif
9261             }
9262           else
9263             {
9264               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9265                                    ranges_by_label[lab_idx].begin,
9266                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9267               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9268                                    ranges_by_label[lab_idx].end,
9269                                    NULL);
9270             }
9271         }
9272       else
9273         {
9274           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9275           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9276           fmt = start_fmt;
9277         }
9278     }
9279 }
9280
9281 /* Data structure containing information about input files.  */
9282 struct file_info
9283 {
9284   const char *path;     /* Complete file name.  */
9285   const char *fname;    /* File name part.  */
9286   int length;           /* Length of entire string.  */
9287   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9288   int dir_idx;          /* Index in directory table.  */
9289 };
9290
9291 /* Data structure containing information about directories with source
9292    files.  */
9293 struct dir_info
9294 {
9295   const char *path;     /* Path including directory name.  */
9296   int length;           /* Path length.  */
9297   int prefix;           /* Index of directory entry which is a prefix.  */
9298   int count;            /* Number of files in this directory.  */
9299   int dir_idx;          /* Index of directory used as base.  */
9300 };
9301
9302 /* Callback function for file_info comparison.  We sort by looking at
9303    the directories in the path.  */
9304
9305 static int
9306 file_info_cmp (const void *p1, const void *p2)
9307 {
9308   const struct file_info *const s1 = (const struct file_info *) p1;
9309   const struct file_info *const s2 = (const struct file_info *) p2;
9310   const unsigned char *cp1;
9311   const unsigned char *cp2;
9312
9313   /* Take care of file names without directories.  We need to make sure that
9314      we return consistent values to qsort since some will get confused if
9315      we return the same value when identical operands are passed in opposite
9316      orders.  So if neither has a directory, return 0 and otherwise return
9317      1 or -1 depending on which one has the directory.  */
9318   if ((s1->path == s1->fname || s2->path == s2->fname))
9319     return (s2->path == s2->fname) - (s1->path == s1->fname);
9320
9321   cp1 = (const unsigned char *) s1->path;
9322   cp2 = (const unsigned char *) s2->path;
9323
9324   while (1)
9325     {
9326       ++cp1;
9327       ++cp2;
9328       /* Reached the end of the first path?  If so, handle like above.  */
9329       if ((cp1 == (const unsigned char *) s1->fname)
9330           || (cp2 == (const unsigned char *) s2->fname))
9331         return ((cp2 == (const unsigned char *) s2->fname)
9332                 - (cp1 == (const unsigned char *) s1->fname));
9333
9334       /* Character of current path component the same?  */
9335       else if (*cp1 != *cp2)
9336         return *cp1 - *cp2;
9337     }
9338 }
9339
9340 struct file_name_acquire_data
9341 {
9342   struct file_info *files;
9343   int used_files;
9344   int max_files;
9345 };
9346
9347 /* Traversal function for the hash table.  */
9348
9349 static int
9350 file_name_acquire (void ** slot, void *data)
9351 {
9352   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9353   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9354   struct file_info *fi;
9355   const char *f;
9356
9357   gcc_assert (fnad->max_files >= d->emitted_number);
9358
9359   if (! d->emitted_number)
9360     return 1;
9361
9362   gcc_assert (fnad->max_files != fnad->used_files);
9363
9364   fi = fnad->files + fnad->used_files++;
9365
9366   /* Skip all leading "./".  */
9367   f = d->filename;
9368   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9369     f += 2;
9370
9371   /* Create a new array entry.  */
9372   fi->path = f;
9373   fi->length = strlen (f);
9374   fi->file_idx = d;
9375
9376   /* Search for the file name part.  */
9377   f = strrchr (f, DIR_SEPARATOR);
9378 #if defined (DIR_SEPARATOR_2)
9379   {
9380     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9381
9382     if (g != NULL)
9383       {
9384         if (f == NULL || f < g)
9385           f = g;
9386       }
9387   }
9388 #endif
9389
9390   fi->fname = f == NULL ? fi->path : f + 1;
9391   return 1;
9392 }
9393
9394 /* Output the directory table and the file name table.  We try to minimize
9395    the total amount of memory needed.  A heuristic is used to avoid large
9396    slowdowns with many input files.  */
9397
9398 static void
9399 output_file_names (void)
9400 {
9401   struct file_name_acquire_data fnad;
9402   int numfiles;
9403   struct file_info *files;
9404   struct dir_info *dirs;
9405   int *saved;
9406   int *savehere;
9407   int *backmap;
9408   int ndirs;
9409   int idx_offset;
9410   int i;
9411
9412   if (!last_emitted_file)
9413     {
9414       dw2_asm_output_data (1, 0, "End directory table");
9415       dw2_asm_output_data (1, 0, "End file name table");
9416       return;
9417     }
9418
9419   numfiles = last_emitted_file->emitted_number;
9420
9421   /* Allocate the various arrays we need.  */
9422   files = XALLOCAVEC (struct file_info, numfiles);
9423   dirs = XALLOCAVEC (struct dir_info, numfiles);
9424
9425   fnad.files = files;
9426   fnad.used_files = 0;
9427   fnad.max_files = numfiles;
9428   htab_traverse (file_table, file_name_acquire, &fnad);
9429   gcc_assert (fnad.used_files == fnad.max_files);
9430
9431   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9432
9433   /* Find all the different directories used.  */
9434   dirs[0].path = files[0].path;
9435   dirs[0].length = files[0].fname - files[0].path;
9436   dirs[0].prefix = -1;
9437   dirs[0].count = 1;
9438   dirs[0].dir_idx = 0;
9439   files[0].dir_idx = 0;
9440   ndirs = 1;
9441
9442   for (i = 1; i < numfiles; i++)
9443     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9444         && memcmp (dirs[ndirs - 1].path, files[i].path,
9445                    dirs[ndirs - 1].length) == 0)
9446       {
9447         /* Same directory as last entry.  */
9448         files[i].dir_idx = ndirs - 1;
9449         ++dirs[ndirs - 1].count;
9450       }
9451     else
9452       {
9453         int j;
9454
9455         /* This is a new directory.  */
9456         dirs[ndirs].path = files[i].path;
9457         dirs[ndirs].length = files[i].fname - files[i].path;
9458         dirs[ndirs].count = 1;
9459         dirs[ndirs].dir_idx = ndirs;
9460         files[i].dir_idx = ndirs;
9461
9462         /* Search for a prefix.  */
9463         dirs[ndirs].prefix = -1;
9464         for (j = 0; j < ndirs; j++)
9465           if (dirs[j].length < dirs[ndirs].length
9466               && dirs[j].length > 1
9467               && (dirs[ndirs].prefix == -1
9468                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9469               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9470             dirs[ndirs].prefix = j;
9471
9472         ++ndirs;
9473       }
9474
9475   /* Now to the actual work.  We have to find a subset of the directories which
9476      allow expressing the file name using references to the directory table
9477      with the least amount of characters.  We do not do an exhaustive search
9478      where we would have to check out every combination of every single
9479      possible prefix.  Instead we use a heuristic which provides nearly optimal
9480      results in most cases and never is much off.  */
9481   saved = XALLOCAVEC (int, ndirs);
9482   savehere = XALLOCAVEC (int, ndirs);
9483
9484   memset (saved, '\0', ndirs * sizeof (saved[0]));
9485   for (i = 0; i < ndirs; i++)
9486     {
9487       int j;
9488       int total;
9489
9490       /* We can always save some space for the current directory.  But this
9491          does not mean it will be enough to justify adding the directory.  */
9492       savehere[i] = dirs[i].length;
9493       total = (savehere[i] - saved[i]) * dirs[i].count;
9494
9495       for (j = i + 1; j < ndirs; j++)
9496         {
9497           savehere[j] = 0;
9498           if (saved[j] < dirs[i].length)
9499             {
9500               /* Determine whether the dirs[i] path is a prefix of the
9501                  dirs[j] path.  */
9502               int k;
9503
9504               k = dirs[j].prefix;
9505               while (k != -1 && k != (int) i)
9506                 k = dirs[k].prefix;
9507
9508               if (k == (int) i)
9509                 {
9510                   /* Yes it is.  We can possibly save some memory by
9511                      writing the filenames in dirs[j] relative to
9512                      dirs[i].  */
9513                   savehere[j] = dirs[i].length;
9514                   total += (savehere[j] - saved[j]) * dirs[j].count;
9515                 }
9516             }
9517         }
9518
9519       /* Check whether we can save enough to justify adding the dirs[i]
9520          directory.  */
9521       if (total > dirs[i].length + 1)
9522         {
9523           /* It's worthwhile adding.  */
9524           for (j = i; j < ndirs; j++)
9525             if (savehere[j] > 0)
9526               {
9527                 /* Remember how much we saved for this directory so far.  */
9528                 saved[j] = savehere[j];
9529
9530                 /* Remember the prefix directory.  */
9531                 dirs[j].dir_idx = i;
9532               }
9533         }
9534     }
9535
9536   /* Emit the directory name table.  */
9537   idx_offset = dirs[0].length > 0 ? 1 : 0;
9538   for (i = 1 - idx_offset; i < ndirs; i++)
9539     dw2_asm_output_nstring (dirs[i].path,
9540                             dirs[i].length
9541                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9542                             "Directory Entry: %#x", i + idx_offset);
9543
9544   dw2_asm_output_data (1, 0, "End directory table");
9545
9546   /* We have to emit them in the order of emitted_number since that's
9547      used in the debug info generation.  To do this efficiently we
9548      generate a back-mapping of the indices first.  */
9549   backmap = XALLOCAVEC (int, numfiles);
9550   for (i = 0; i < numfiles; i++)
9551     backmap[files[i].file_idx->emitted_number - 1] = i;
9552
9553   /* Now write all the file names.  */
9554   for (i = 0; i < numfiles; i++)
9555     {
9556       int file_idx = backmap[i];
9557       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9558
9559 #ifdef VMS_DEBUGGING_INFO
9560 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9561
9562       /* Setting these fields can lead to debugger miscomparisons,
9563          but VMS Debug requires them to be set correctly.  */
9564
9565       int ver;
9566       long long cdt;
9567       long siz;
9568       int maxfilelen = strlen (files[file_idx].path)
9569                                + dirs[dir_idx].length
9570                                + MAX_VMS_VERSION_LEN + 1;
9571       char *filebuf = XALLOCAVEC (char, maxfilelen);
9572
9573       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9574       snprintf (filebuf, maxfilelen, "%s;%d",
9575                 files[file_idx].path + dirs[dir_idx].length, ver);
9576
9577       dw2_asm_output_nstring
9578         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9579
9580       /* Include directory index.  */
9581       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9582
9583       /* Modification time.  */
9584       dw2_asm_output_data_uleb128
9585         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9586           ? cdt : 0,
9587          NULL);
9588
9589       /* File length in bytes.  */
9590       dw2_asm_output_data_uleb128
9591         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9592           ? siz : 0,
9593          NULL);
9594 #else
9595       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9596                               "File Entry: %#x", (unsigned) i + 1);
9597
9598       /* Include directory index.  */
9599       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9600
9601       /* Modification time.  */
9602       dw2_asm_output_data_uleb128 (0, NULL);
9603
9604       /* File length in bytes.  */
9605       dw2_asm_output_data_uleb128 (0, NULL);
9606 #endif /* VMS_DEBUGGING_INFO */
9607     }
9608
9609   dw2_asm_output_data (1, 0, "End file name table");
9610 }
9611
9612
9613 /* Output one line number table into the .debug_line section.  */
9614
9615 static void
9616 output_one_line_info_table (dw_line_info_table *table)
9617 {
9618   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9619   unsigned int current_line = 1;
9620   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9621   dw_line_info_entry *ent;
9622   size_t i;
9623
9624   FOR_EACH_VEC_SAFE_ELT (table->entries, i, ent)
9625     {
9626       switch (ent->opcode)
9627         {
9628         case LI_set_address:
9629           /* ??? Unfortunately, we have little choice here currently, and
9630              must always use the most general form.  GCC does not know the
9631              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9632              ports do have length attributes which will give an upper bound
9633              on the address range.  We could perhaps use length attributes
9634              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9635           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9636
9637           /* This can handle any delta.  This takes
9638              4+DWARF2_ADDR_SIZE bytes.  */
9639           dw2_asm_output_data (1, 0, "set address %s", line_label);
9640           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9641           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9642           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9643           break;
9644
9645         case LI_set_line:
9646           if (ent->val == current_line)
9647             {
9648               /* We still need to start a new row, so output a copy insn.  */
9649               dw2_asm_output_data (1, DW_LNS_copy,
9650                                    "copy line %u", current_line);
9651             }
9652           else
9653             {
9654               int line_offset = ent->val - current_line;
9655               int line_delta = line_offset - DWARF_LINE_BASE;
9656
9657               current_line = ent->val;
9658               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9659                 {
9660                   /* This can handle deltas from -10 to 234, using the current
9661                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9662                      This takes 1 byte.  */
9663                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9664                                        "line %u", current_line);
9665                 }
9666               else
9667                 {
9668                   /* This can handle any delta.  This takes at least 4 bytes,
9669                      depending on the value being encoded.  */
9670                   dw2_asm_output_data (1, DW_LNS_advance_line,
9671                                        "advance to line %u", current_line);
9672                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9673                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9674                 }
9675             }
9676           break;
9677
9678         case LI_set_file:
9679           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9680           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9681           break;
9682
9683         case LI_set_column:
9684           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9685           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9686           break;
9687
9688         case LI_negate_stmt:
9689           current_is_stmt = !current_is_stmt;
9690           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9691                                "is_stmt %d", current_is_stmt);
9692           break;
9693
9694         case LI_set_prologue_end:
9695           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9696                                "set prologue end");
9697           break;
9698           
9699         case LI_set_epilogue_begin:
9700           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9701                                "set epilogue begin");
9702           break;
9703
9704         case LI_set_discriminator:
9705           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9706           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9707           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9708           dw2_asm_output_data_uleb128 (ent->val, NULL);
9709           break;
9710         }
9711     }
9712
9713   /* Emit debug info for the address of the end of the table.  */
9714   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9715   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9716   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9717   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9718
9719   dw2_asm_output_data (1, 0, "end sequence");
9720   dw2_asm_output_data_uleb128 (1, NULL);
9721   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9722 }
9723
9724 /* Output the source line number correspondence information.  This
9725    information goes into the .debug_line section.  */
9726
9727 static void
9728 output_line_info (bool prologue_only)
9729 {
9730   char l1[20], l2[20], p1[20], p2[20];
9731   int ver = dwarf_version;
9732   bool saw_one = false;
9733   int opc;
9734
9735   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9736   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9737   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9738   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9739
9740   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9741     dw2_asm_output_data (4, 0xffffffff,
9742       "Initial length escape value indicating 64-bit DWARF extension");
9743   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9744                         "Length of Source Line Info");
9745   ASM_OUTPUT_LABEL (asm_out_file, l1);
9746
9747   dw2_asm_output_data (2, ver, "DWARF Version");
9748   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9749   ASM_OUTPUT_LABEL (asm_out_file, p1);
9750
9751   /* Define the architecture-dependent minimum instruction length (in bytes).
9752      In this implementation of DWARF, this field is used for information
9753      purposes only.  Since GCC generates assembly language, we have no
9754      a priori knowledge of how many instruction bytes are generated for each
9755      source line, and therefore can use only the DW_LNE_set_address and
9756      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9757      this as '1', which is "correct enough" for all architectures,
9758      and don't let the target override.  */
9759   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9760
9761   if (ver >= 4)
9762     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9763                          "Maximum Operations Per Instruction");
9764   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9765                        "Default is_stmt_start flag");
9766   dw2_asm_output_data (1, DWARF_LINE_BASE,
9767                        "Line Base Value (Special Opcodes)");
9768   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9769                        "Line Range Value (Special Opcodes)");
9770   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9771                        "Special Opcode Base");
9772
9773   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9774     {
9775       int n_op_args;
9776       switch (opc)
9777         {
9778         case DW_LNS_advance_pc:
9779         case DW_LNS_advance_line:
9780         case DW_LNS_set_file:
9781         case DW_LNS_set_column:
9782         case DW_LNS_fixed_advance_pc:
9783         case DW_LNS_set_isa:
9784           n_op_args = 1;
9785           break;
9786         default:
9787           n_op_args = 0;
9788           break;
9789         }
9790
9791       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9792                            opc, n_op_args);
9793     }
9794
9795   /* Write out the information about the files we use.  */
9796   output_file_names ();
9797   ASM_OUTPUT_LABEL (asm_out_file, p2);
9798   if (prologue_only)
9799     {
9800       /* Output the marker for the end of the line number info.  */
9801       ASM_OUTPUT_LABEL (asm_out_file, l2);
9802       return;
9803     }
9804
9805   if (separate_line_info)
9806     {
9807       dw_line_info_table *table;
9808       size_t i;
9809
9810       FOR_EACH_VEC_ELT (*separate_line_info, i, table)
9811         if (table->in_use)
9812           {
9813             output_one_line_info_table (table);
9814             saw_one = true;
9815           }
9816     }
9817   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9818     {
9819       output_one_line_info_table (cold_text_section_line_info);
9820       saw_one = true;
9821     }
9822
9823   /* ??? Some Darwin linkers crash on a .debug_line section with no
9824      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9825      sufficient -- the address column must also be initialized.
9826      Make sure to output at least one set_address/end_sequence pair,
9827      choosing .text since that section is always present.  */
9828   if (text_section_line_info->in_use || !saw_one)
9829     output_one_line_info_table (text_section_line_info);
9830
9831   /* Output the marker for the end of the line number info.  */
9832   ASM_OUTPUT_LABEL (asm_out_file, l2);
9833 }
9834 \f
9835 /* Given a pointer to a tree node for some base type, return a pointer to
9836    a DIE that describes the given type.
9837
9838    This routine must only be called for GCC type nodes that correspond to
9839    Dwarf base (fundamental) types.  */
9840
9841 static dw_die_ref
9842 base_type_die (tree type)
9843 {
9844   dw_die_ref base_type_result;
9845   enum dwarf_type encoding;
9846
9847   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9848     return 0;
9849
9850   /* If this is a subtype that should not be emitted as a subrange type,
9851      use the base type.  See subrange_type_for_debug_p.  */
9852   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9853     type = TREE_TYPE (type);
9854
9855   switch (TREE_CODE (type))
9856     {
9857     case INTEGER_TYPE:
9858       if ((dwarf_version >= 4 || !dwarf_strict)
9859           && TYPE_NAME (type)
9860           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9861           && DECL_IS_BUILTIN (TYPE_NAME (type))
9862           && DECL_NAME (TYPE_NAME (type)))
9863         {
9864           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9865           if (strcmp (name, "char16_t") == 0
9866               || strcmp (name, "char32_t") == 0)
9867             {
9868               encoding = DW_ATE_UTF;
9869               break;
9870             }
9871         }
9872       if (TYPE_STRING_FLAG (type))
9873         {
9874           if (TYPE_UNSIGNED (type))
9875             encoding = DW_ATE_unsigned_char;
9876           else
9877             encoding = DW_ATE_signed_char;
9878         }
9879       else if (TYPE_UNSIGNED (type))
9880         encoding = DW_ATE_unsigned;
9881       else
9882         encoding = DW_ATE_signed;
9883       break;
9884
9885     case REAL_TYPE:
9886       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9887         {
9888           if (dwarf_version >= 3 || !dwarf_strict)
9889             encoding = DW_ATE_decimal_float;
9890           else
9891             encoding = DW_ATE_lo_user;
9892         }
9893       else
9894         encoding = DW_ATE_float;
9895       break;
9896
9897     case FIXED_POINT_TYPE:
9898       if (!(dwarf_version >= 3 || !dwarf_strict))
9899         encoding = DW_ATE_lo_user;
9900       else if (TYPE_UNSIGNED (type))
9901         encoding = DW_ATE_unsigned_fixed;
9902       else
9903         encoding = DW_ATE_signed_fixed;
9904       break;
9905
9906       /* Dwarf2 doesn't know anything about complex ints, so use
9907          a user defined type for it.  */
9908     case COMPLEX_TYPE:
9909       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9910         encoding = DW_ATE_complex_float;
9911       else
9912         encoding = DW_ATE_lo_user;
9913       break;
9914
9915     case BOOLEAN_TYPE:
9916       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9917       encoding = DW_ATE_boolean;
9918       break;
9919
9920     default:
9921       /* No other TREE_CODEs are Dwarf fundamental types.  */
9922       gcc_unreachable ();
9923     }
9924
9925   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9926
9927   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9928                    int_size_in_bytes (type));
9929   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9930   add_pubtype (type, base_type_result);
9931
9932   return base_type_result;
9933 }
9934
9935 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9936    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9937
9938 static inline int
9939 is_base_type (tree type)
9940 {
9941   switch (TREE_CODE (type))
9942     {
9943     case ERROR_MARK:
9944     case VOID_TYPE:
9945     case INTEGER_TYPE:
9946     case REAL_TYPE:
9947     case FIXED_POINT_TYPE:
9948     case COMPLEX_TYPE:
9949     case BOOLEAN_TYPE:
9950       return 1;
9951
9952     case ARRAY_TYPE:
9953     case RECORD_TYPE:
9954     case UNION_TYPE:
9955     case QUAL_UNION_TYPE:
9956     case ENUMERAL_TYPE:
9957     case FUNCTION_TYPE:
9958     case METHOD_TYPE:
9959     case POINTER_TYPE:
9960     case REFERENCE_TYPE:
9961     case NULLPTR_TYPE:
9962     case OFFSET_TYPE:
9963     case LANG_TYPE:
9964     case VECTOR_TYPE:
9965       return 0;
9966
9967     default:
9968       gcc_unreachable ();
9969     }
9970
9971   return 0;
9972 }
9973
9974 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9975    node, return the size in bits for the type if it is a constant, or else
9976    return the alignment for the type if the type's size is not constant, or
9977    else return BITS_PER_WORD if the type actually turns out to be an
9978    ERROR_MARK node.  */
9979
9980 static inline unsigned HOST_WIDE_INT
9981 simple_type_size_in_bits (const_tree type)
9982 {
9983   if (TREE_CODE (type) == ERROR_MARK)
9984     return BITS_PER_WORD;
9985   else if (TYPE_SIZE (type) == NULL_TREE)
9986     return 0;
9987   else if (host_integerp (TYPE_SIZE (type), 1))
9988     return tree_low_cst (TYPE_SIZE (type), 1);
9989   else
9990     return TYPE_ALIGN (type);
9991 }
9992
9993 /* Similarly, but return a double_int instead of UHWI.  */
9994
9995 static inline double_int
9996 double_int_type_size_in_bits (const_tree type)
9997 {
9998   if (TREE_CODE (type) == ERROR_MARK)
9999     return double_int::from_uhwi (BITS_PER_WORD);
10000   else if (TYPE_SIZE (type) == NULL_TREE)
10001     return double_int_zero;
10002   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10003     return tree_to_double_int (TYPE_SIZE (type));
10004   else
10005     return double_int::from_uhwi (TYPE_ALIGN (type));
10006 }
10007
10008 /*  Given a pointer to a tree node for a subrange type, return a pointer
10009     to a DIE that describes the given type.  */
10010
10011 static dw_die_ref
10012 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10013 {
10014   dw_die_ref subrange_die;
10015   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10016
10017   if (context_die == NULL)
10018     context_die = comp_unit_die ();
10019
10020   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10021
10022   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10023     {
10024       /* The size of the subrange type and its base type do not match,
10025          so we need to generate a size attribute for the subrange type.  */
10026       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10027     }
10028
10029   if (low)
10030     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10031   if (high)
10032     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10033
10034   return subrange_die;
10035 }
10036
10037 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10038    entry that chains various modifiers in front of the given type.  */
10039
10040 static dw_die_ref
10041 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10042                    dw_die_ref context_die)
10043 {
10044   enum tree_code code = TREE_CODE (type);
10045   dw_die_ref mod_type_die;
10046   dw_die_ref sub_die = NULL;
10047   tree item_type = NULL;
10048   tree qualified_type;
10049   tree name, low, high;
10050   dw_die_ref mod_scope;
10051
10052   if (code == ERROR_MARK)
10053     return NULL;
10054
10055   /* See if we already have the appropriately qualified variant of
10056      this type.  */
10057   qualified_type
10058     = get_qualified_type (type,
10059                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10060                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10061
10062   if (qualified_type == sizetype
10063       && TYPE_NAME (qualified_type)
10064       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10065     {
10066       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10067
10068       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10069                            && TYPE_PRECISION (t)
10070                            == TYPE_PRECISION (qualified_type)
10071                            && TYPE_UNSIGNED (t)
10072                            == TYPE_UNSIGNED (qualified_type));
10073       qualified_type = t;
10074     }
10075
10076   /* If we do, then we can just use its DIE, if it exists.  */
10077   if (qualified_type)
10078     {
10079       mod_type_die = lookup_type_die (qualified_type);
10080       if (mod_type_die)
10081         return mod_type_die;
10082     }
10083
10084   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10085
10086   /* Handle C typedef types.  */
10087   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10088       && !DECL_ARTIFICIAL (name))
10089     {
10090       tree dtype = TREE_TYPE (name);
10091
10092       if (qualified_type == dtype)
10093         {
10094           /* For a named type, use the typedef.  */
10095           gen_type_die (qualified_type, context_die);
10096           return lookup_type_die (qualified_type);
10097         }
10098       else if (is_const_type < TYPE_READONLY (dtype)
10099                || is_volatile_type < TYPE_VOLATILE (dtype)
10100                || (is_const_type <= TYPE_READONLY (dtype)
10101                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10102                    && DECL_ORIGINAL_TYPE (name) != type))
10103         /* cv-unqualified version of named type.  Just use the unnamed
10104            type to which it refers.  */
10105         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10106                                   is_const_type, is_volatile_type,
10107                                   context_die);
10108       /* Else cv-qualified version of named type; fall through.  */
10109     }
10110
10111   mod_scope = scope_die_for (type, context_die);
10112
10113   if (is_const_type
10114       /* If both is_const_type and is_volatile_type, prefer the path
10115          which leads to a qualified type.  */
10116       && (!is_volatile_type
10117           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10118           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10119     {
10120       mod_type_die = new_die (DW_TAG_const_type, mod_scope, type);
10121       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10122     }
10123   else if (is_volatile_type)
10124     {
10125       mod_type_die = new_die (DW_TAG_volatile_type, mod_scope, type);
10126       sub_die = modified_type_die (type, is_const_type, 0, context_die);
10127     }
10128   else if (code == POINTER_TYPE)
10129     {
10130       mod_type_die = new_die (DW_TAG_pointer_type, mod_scope, type);
10131       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10132                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10133       item_type = TREE_TYPE (type);
10134       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10135         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10136                          TYPE_ADDR_SPACE (item_type));
10137     }
10138   else if (code == REFERENCE_TYPE)
10139     {
10140       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10141         mod_type_die = new_die (DW_TAG_rvalue_reference_type, mod_scope,
10142                                 type);
10143       else
10144         mod_type_die = new_die (DW_TAG_reference_type, mod_scope, type);
10145       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10146                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10147       item_type = TREE_TYPE (type);
10148       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10149         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10150                          TYPE_ADDR_SPACE (item_type));
10151     }
10152   else if (code == INTEGER_TYPE
10153            && TREE_TYPE (type) != NULL_TREE
10154            && subrange_type_for_debug_p (type, &low, &high))
10155     {
10156       mod_type_die = subrange_type_die (type, low, high, context_die);
10157       item_type = TREE_TYPE (type);
10158     }
10159   else if (is_base_type (type))
10160     mod_type_die = base_type_die (type);
10161   else
10162     {
10163       gen_type_die (type, context_die);
10164
10165       /* We have to get the type_main_variant here (and pass that to the
10166          `lookup_type_die' routine) because the ..._TYPE node we have
10167          might simply be a *copy* of some original type node (where the
10168          copy was created to help us keep track of typedef names) and
10169          that copy might have a different TYPE_UID from the original
10170          ..._TYPE node.  */
10171       if (TREE_CODE (type) != VECTOR_TYPE)
10172         return lookup_type_die (type_main_variant (type));
10173       else
10174         /* Vectors have the debugging information in the type,
10175            not the main variant.  */
10176         return lookup_type_die (type);
10177     }
10178
10179   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10180      don't output a DW_TAG_typedef, since there isn't one in the
10181      user's program; just attach a DW_AT_name to the type.
10182      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10183      if the base type already has the same name.  */
10184   if (name
10185       && ((TREE_CODE (name) != TYPE_DECL
10186            && (qualified_type == TYPE_MAIN_VARIANT (type)
10187                || (!is_const_type && !is_volatile_type)))
10188           || (TREE_CODE (name) == TYPE_DECL
10189               && TREE_TYPE (name) == qualified_type
10190               && DECL_NAME (name))))
10191     {
10192       if (TREE_CODE (name) == TYPE_DECL)
10193         /* Could just call add_name_and_src_coords_attributes here,
10194            but since this is a builtin type it doesn't have any
10195            useful source coordinates anyway.  */
10196         name = DECL_NAME (name);
10197       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10198     }
10199   /* This probably indicates a bug.  */
10200   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10201     {
10202       name = TYPE_NAME (type);
10203       if (name
10204           && TREE_CODE (name) == TYPE_DECL)
10205         name = DECL_NAME (name);
10206       add_name_attribute (mod_type_die,
10207                           name ? IDENTIFIER_POINTER (name) : "__unknown__");
10208     }
10209
10210   if (qualified_type)
10211     equate_type_number_to_die (qualified_type, mod_type_die);
10212
10213   if (item_type)
10214     /* We must do this after the equate_type_number_to_die call, in case
10215        this is a recursive type.  This ensures that the modified_type_die
10216        recursion will terminate even if the type is recursive.  Recursive
10217        types are possible in Ada.  */
10218     sub_die = modified_type_die (item_type,
10219                                  TYPE_READONLY (item_type),
10220                                  TYPE_VOLATILE (item_type),
10221                                  context_die);
10222
10223   if (sub_die != NULL)
10224     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10225
10226   add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10227   if (TYPE_ARTIFICIAL (type))
10228     add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10229
10230   return mod_type_die;
10231 }
10232
10233 /* Generate DIEs for the generic parameters of T.
10234    T must be either a generic type or a generic function.
10235    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10236
10237 static void
10238 gen_generic_params_dies (tree t)
10239 {
10240   tree parms, args;
10241   int parms_num, i;
10242   dw_die_ref die = NULL;
10243
10244   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10245     return;
10246
10247   if (TYPE_P (t))
10248     die = lookup_type_die (t);
10249   else if (DECL_P (t))
10250     die = lookup_decl_die (t);
10251
10252   gcc_assert (die);
10253
10254   parms = lang_hooks.get_innermost_generic_parms (t);
10255   if (!parms)
10256     /* T has no generic parameter. It means T is neither a generic type
10257        or function. End of story.  */
10258     return;
10259
10260   parms_num = TREE_VEC_LENGTH (parms);
10261   args = lang_hooks.get_innermost_generic_args (t);
10262   for (i = 0; i < parms_num; i++)
10263     {
10264       tree parm, arg, arg_pack_elems;
10265
10266       parm = TREE_VEC_ELT (parms, i);
10267       arg = TREE_VEC_ELT (args, i);
10268       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10269       gcc_assert (parm && TREE_VALUE (parm) && arg);
10270
10271       if (parm && TREE_VALUE (parm) && arg)
10272         {
10273           /* If PARM represents a template parameter pack,
10274              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10275              by DW_TAG_template_*_parameter DIEs for the argument
10276              pack elements of ARG. Note that ARG would then be
10277              an argument pack.  */
10278           if (arg_pack_elems)
10279             template_parameter_pack_die (TREE_VALUE (parm),
10280                                          arg_pack_elems,
10281                                          die);
10282           else
10283             generic_parameter_die (TREE_VALUE (parm), arg,
10284                                    true /* Emit DW_AT_name */, die);
10285         }
10286     }
10287 }
10288
10289 /* Create and return a DIE for PARM which should be
10290    the representation of a generic type parameter.
10291    For instance, in the C++ front end, PARM would be a template parameter.
10292    ARG is the argument to PARM.
10293    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10294    name of the PARM.
10295    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10296    as a child node.  */
10297
10298 static dw_die_ref
10299 generic_parameter_die (tree parm, tree arg,
10300                        bool emit_name_p,
10301                        dw_die_ref parent_die)
10302 {
10303   dw_die_ref tmpl_die = NULL;
10304   const char *name = NULL;
10305
10306   if (!parm || !DECL_NAME (parm) || !arg)
10307     return NULL;
10308
10309   /* We support non-type generic parameters and arguments,
10310      type generic parameters and arguments, as well as
10311      generic generic parameters (a.k.a. template template parameters in C++)
10312      and arguments.  */
10313   if (TREE_CODE (parm) == PARM_DECL)
10314     /* PARM is a nontype generic parameter  */
10315     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10316   else if (TREE_CODE (parm) == TYPE_DECL)
10317     /* PARM is a type generic parameter.  */
10318     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10319   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10320     /* PARM is a generic generic parameter.
10321        Its DIE is a GNU extension. It shall have a
10322        DW_AT_name attribute to represent the name of the template template
10323        parameter, and a DW_AT_GNU_template_name attribute to represent the
10324        name of the template template argument.  */
10325     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10326                         parent_die, parm);
10327   else
10328     gcc_unreachable ();
10329
10330   if (tmpl_die)
10331     {
10332       tree tmpl_type;
10333
10334       /* If PARM is a generic parameter pack, it means we are
10335          emitting debug info for a template argument pack element.
10336          In other terms, ARG is a template argument pack element.
10337          In that case, we don't emit any DW_AT_name attribute for
10338          the die.  */
10339       if (emit_name_p)
10340         {
10341           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10342           gcc_assert (name);
10343           add_AT_string (tmpl_die, DW_AT_name, name);
10344         }
10345
10346       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10347         {
10348           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10349              TMPL_DIE should have a child DW_AT_type attribute that is set
10350              to the type of the argument to PARM, which is ARG.
10351              If PARM is a type generic parameter, TMPL_DIE should have a
10352              child DW_AT_type that is set to ARG.  */
10353           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10354           add_type_attribute (tmpl_die, tmpl_type, 0,
10355                               TREE_THIS_VOLATILE (tmpl_type),
10356                               parent_die);
10357         }
10358       else
10359         {
10360           /* So TMPL_DIE is a DIE representing a
10361              a generic generic template parameter, a.k.a template template
10362              parameter in C++ and arg is a template.  */
10363
10364           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10365              to the name of the argument.  */
10366           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10367           if (name)
10368             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10369         }
10370
10371       if (TREE_CODE (parm) == PARM_DECL)
10372         /* So PARM is a non-type generic parameter.
10373            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10374            attribute of TMPL_DIE which value represents the value
10375            of ARG.
10376            We must be careful here:
10377            The value of ARG might reference some function decls.
10378            We might currently be emitting debug info for a generic
10379            type and types are emitted before function decls, we don't
10380            know if the function decls referenced by ARG will actually be
10381            emitted after cgraph computations.
10382            So must defer the generation of the DW_AT_const_value to
10383            after cgraph is ready.  */
10384         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10385     }
10386
10387   return tmpl_die;
10388 }
10389
10390 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10391    PARM_PACK must be a template parameter pack. The returned DIE
10392    will be child DIE of PARENT_DIE.  */
10393
10394 static dw_die_ref
10395 template_parameter_pack_die (tree parm_pack,
10396                              tree parm_pack_args,
10397                              dw_die_ref parent_die)
10398 {
10399   dw_die_ref die;
10400   int j;
10401
10402   gcc_assert (parent_die && parm_pack);
10403
10404   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10405   add_name_and_src_coords_attributes (die, parm_pack);
10406   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10407     generic_parameter_die (parm_pack,
10408                            TREE_VEC_ELT (parm_pack_args, j),
10409                            false /* Don't emit DW_AT_name */,
10410                            die);
10411   return die;
10412 }
10413
10414 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10415    an enumerated type.  */
10416
10417 static inline int
10418 type_is_enum (const_tree type)
10419 {
10420   return TREE_CODE (type) == ENUMERAL_TYPE;
10421 }
10422
10423 /* Return the DBX register number described by a given RTL node.  */
10424
10425 static unsigned int
10426 dbx_reg_number (const_rtx rtl)
10427 {
10428   unsigned regno = REGNO (rtl);
10429
10430   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10431
10432 #ifdef LEAF_REG_REMAP
10433   if (crtl->uses_only_leaf_regs)
10434     {
10435       int leaf_reg = LEAF_REG_REMAP (regno);
10436       if (leaf_reg != -1)
10437         regno = (unsigned) leaf_reg;
10438     }
10439 #endif
10440
10441   regno = DBX_REGISTER_NUMBER (regno);
10442   gcc_assert (regno != INVALID_REGNUM);
10443   return regno;
10444 }
10445
10446 /* Optionally add a DW_OP_piece term to a location description expression.
10447    DW_OP_piece is only added if the location description expression already
10448    doesn't end with DW_OP_piece.  */
10449
10450 static void
10451 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10452 {
10453   dw_loc_descr_ref loc;
10454
10455   if (*list_head != NULL)
10456     {
10457       /* Find the end of the chain.  */
10458       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10459         ;
10460
10461       if (loc->dw_loc_opc != DW_OP_piece)
10462         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10463     }
10464 }
10465
10466 /* Return a location descriptor that designates a machine register or
10467    zero if there is none.  */
10468
10469 static dw_loc_descr_ref
10470 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10471 {
10472   rtx regs;
10473
10474   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10475     return 0;
10476
10477   /* We only use "frame base" when we're sure we're talking about the
10478      post-prologue local stack frame.  We do this by *not* running
10479      register elimination until this point, and recognizing the special
10480      argument pointer and soft frame pointer rtx's.
10481      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10482   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10483       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10484     {
10485       dw_loc_descr_ref result = NULL;
10486
10487       if (dwarf_version >= 4 || !dwarf_strict)
10488         {
10489           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10490                                        initialized);
10491           if (result)
10492             add_loc_descr (&result,
10493                            new_loc_descr (DW_OP_stack_value, 0, 0));
10494         }
10495       return result;
10496     }
10497
10498   regs = targetm.dwarf_register_span (rtl);
10499
10500   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10501     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10502   else
10503     {
10504       unsigned int dbx_regnum = dbx_reg_number (rtl);
10505       if (dbx_regnum == IGNORED_DWARF_REGNUM)
10506         return 0;
10507       return one_reg_loc_descriptor (dbx_regnum, initialized);
10508     }
10509 }
10510
10511 /* Return a location descriptor that designates a machine register for
10512    a given hard register number.  */
10513
10514 static dw_loc_descr_ref
10515 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10516 {
10517   dw_loc_descr_ref reg_loc_descr;
10518
10519   if (regno <= 31)
10520     reg_loc_descr
10521       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10522   else
10523     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10524
10525   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10526     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10527
10528   return reg_loc_descr;
10529 }
10530
10531 /* Given an RTL of a register, return a location descriptor that
10532    designates a value that spans more than one register.  */
10533
10534 static dw_loc_descr_ref
10535 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10536                              enum var_init_status initialized)
10537 {
10538   int nregs, size, i;
10539   unsigned reg;
10540   dw_loc_descr_ref loc_result = NULL;
10541
10542   reg = REGNO (rtl);
10543 #ifdef LEAF_REG_REMAP
10544   if (crtl->uses_only_leaf_regs)
10545     {
10546       int leaf_reg = LEAF_REG_REMAP (reg);
10547       if (leaf_reg != -1)
10548         reg = (unsigned) leaf_reg;
10549     }
10550 #endif
10551   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10552   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10553
10554   /* Simple, contiguous registers.  */
10555   if (regs == NULL_RTX)
10556     {
10557       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10558
10559       loc_result = NULL;
10560       while (nregs--)
10561         {
10562           dw_loc_descr_ref t;
10563
10564           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10565                                       VAR_INIT_STATUS_INITIALIZED);
10566           add_loc_descr (&loc_result, t);
10567           add_loc_descr_op_piece (&loc_result, size);
10568           ++reg;
10569         }
10570       return loc_result;
10571     }
10572
10573   /* Now onto stupid register sets in non contiguous locations.  */
10574
10575   gcc_assert (GET_CODE (regs) == PARALLEL);
10576
10577   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10578   loc_result = NULL;
10579
10580   for (i = 0; i < XVECLEN (regs, 0); ++i)
10581     {
10582       dw_loc_descr_ref t;
10583
10584       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10585                                   VAR_INIT_STATUS_INITIALIZED);
10586       add_loc_descr (&loc_result, t);
10587       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10588       add_loc_descr_op_piece (&loc_result, size);
10589     }
10590
10591   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10592     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10593   return loc_result;
10594 }
10595
10596 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10597
10598 /* Return a location descriptor that designates a constant i,
10599    as a compound operation from constant (i >> shift), constant shift
10600    and DW_OP_shl.  */
10601
10602 static dw_loc_descr_ref
10603 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10604 {
10605   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10606   add_loc_descr (&ret, int_loc_descriptor (shift));
10607   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10608   return ret;
10609 }
10610
10611 /* Return a location descriptor that designates a constant.  */
10612
10613 static dw_loc_descr_ref
10614 int_loc_descriptor (HOST_WIDE_INT i)
10615 {
10616   enum dwarf_location_atom op;
10617
10618   /* Pick the smallest representation of a constant, rather than just
10619      defaulting to the LEB encoding.  */
10620   if (i >= 0)
10621     {
10622       int clz = clz_hwi (i);
10623       int ctz = ctz_hwi (i);
10624       if (i <= 31)
10625         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10626       else if (i <= 0xff)
10627         op = DW_OP_const1u;
10628       else if (i <= 0xffff)
10629         op = DW_OP_const2u;
10630       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10631                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10632         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10633            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10634            while DW_OP_const4u is 5 bytes.  */
10635         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10636       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10637                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10638         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10639            while DW_OP_const4u is 5 bytes.  */
10640         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10641       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10642         op = DW_OP_const4u;
10643       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10644                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10645         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10646            while DW_OP_constu of constant >= 0x100000000 takes at least
10647            6 bytes.  */
10648         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10649       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10650                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10651                   >= HOST_BITS_PER_WIDE_INT)
10652         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10653            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10654            while DW_OP_constu takes in this case at least 6 bytes.  */
10655         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10656       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10657                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10658                && size_of_uleb128 (i) > 6)
10659         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10660         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10661       else
10662         op = DW_OP_constu;
10663     }
10664   else
10665     {
10666       if (i >= -0x80)
10667         op = DW_OP_const1s;
10668       else if (i >= -0x8000)
10669         op = DW_OP_const2s;
10670       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10671         {
10672           if (size_of_int_loc_descriptor (i) < 5)
10673             {
10674               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10675               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10676               return ret;
10677             }
10678           op = DW_OP_const4s;
10679         }
10680       else
10681         {
10682           if (size_of_int_loc_descriptor (i)
10683               < (unsigned long) 1 + size_of_sleb128 (i))
10684             {
10685               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10686               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10687               return ret;
10688             }
10689           op = DW_OP_consts;
10690         }
10691     }
10692
10693   return new_loc_descr (op, i, 0);
10694 }
10695
10696 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10697    without actually allocating it.  */
10698
10699 static unsigned long
10700 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10701 {
10702   return size_of_int_loc_descriptor (i >> shift)
10703          + size_of_int_loc_descriptor (shift)
10704          + 1;
10705 }
10706
10707 /* Return size_of_locs (int_loc_descriptor (i)) without
10708    actually allocating it.  */
10709
10710 static unsigned long
10711 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10712 {
10713   unsigned long s;
10714
10715   if (i >= 0)
10716     {
10717       int clz, ctz;
10718       if (i <= 31)
10719         return 1;
10720       else if (i <= 0xff)
10721         return 2;
10722       else if (i <= 0xffff)
10723         return 3;
10724       clz = clz_hwi (i);
10725       ctz = ctz_hwi (i);
10726       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10727           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10728         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10729                                                     - clz - 5);
10730       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10731                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10732         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10733                                                     - clz - 8);
10734       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10735         return 5;
10736       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10737       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10738           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10739         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10740                                                     - clz - 8);
10741       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10742                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10743         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10744                                                     - clz - 16);
10745       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10746                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10747                && s > 6)
10748         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10749                                                     - clz - 32);
10750       else
10751         return 1 + s;
10752     }
10753   else
10754     {
10755       if (i >= -0x80)
10756         return 2;
10757       else if (i >= -0x8000)
10758         return 3;
10759       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10760         {
10761           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10762             {
10763               s = size_of_int_loc_descriptor (-i) + 1;
10764               if (s < 5)
10765                 return s;
10766             }
10767           return 5;
10768         }
10769       else
10770         {
10771           unsigned long r = 1 + size_of_sleb128 (i);
10772           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10773             {
10774               s = size_of_int_loc_descriptor (-i) + 1;
10775               if (s < r)
10776                 return s;
10777             }
10778           return r;
10779         }
10780     }
10781 }
10782
10783 /* Return loc description representing "address" of integer value.
10784    This can appear only as toplevel expression.  */
10785
10786 static dw_loc_descr_ref
10787 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10788 {
10789   int litsize;
10790   dw_loc_descr_ref loc_result = NULL;
10791
10792   if (!(dwarf_version >= 4 || !dwarf_strict))
10793     return NULL;
10794
10795   litsize = size_of_int_loc_descriptor (i);
10796   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10797      is more compact.  For DW_OP_stack_value we need:
10798      litsize + 1 (DW_OP_stack_value)
10799      and for DW_OP_implicit_value:
10800      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10801   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10802     {
10803       loc_result = int_loc_descriptor (i);
10804       add_loc_descr (&loc_result,
10805                      new_loc_descr (DW_OP_stack_value, 0, 0));
10806       return loc_result;
10807     }
10808
10809   loc_result = new_loc_descr (DW_OP_implicit_value,
10810                               size, 0);
10811   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10812   loc_result->dw_loc_oprnd2.v.val_int = i;
10813   return loc_result;
10814 }
10815
10816 /* Return a location descriptor that designates a base+offset location.  */
10817
10818 static dw_loc_descr_ref
10819 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10820                  enum var_init_status initialized)
10821 {
10822   unsigned int regno;
10823   dw_loc_descr_ref result;
10824   dw_fde_ref fde = cfun->fde;
10825
10826   /* We only use "frame base" when we're sure we're talking about the
10827      post-prologue local stack frame.  We do this by *not* running
10828      register elimination until this point, and recognizing the special
10829      argument pointer and soft frame pointer rtx's.  */
10830   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10831     {
10832       rtx elim = (ira_use_lra_p
10833                   ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
10834                   : eliminate_regs (reg, VOIDmode, NULL_RTX));
10835
10836       if (elim != reg)
10837         {
10838           if (GET_CODE (elim) == PLUS)
10839             {
10840               offset += INTVAL (XEXP (elim, 1));
10841               elim = XEXP (elim, 0);
10842             }
10843           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10844                        && (elim == hard_frame_pointer_rtx
10845                            || elim == stack_pointer_rtx))
10846                       || elim == (frame_pointer_needed
10847                                   ? hard_frame_pointer_rtx
10848                                   : stack_pointer_rtx));
10849
10850           /* If drap register is used to align stack, use frame
10851              pointer + offset to access stack variables.  If stack
10852              is aligned without drap, use stack pointer + offset to
10853              access stack variables.  */
10854           if (crtl->stack_realign_tried
10855               && reg == frame_pointer_rtx)
10856             {
10857               int base_reg
10858                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10859                                       ? HARD_FRAME_POINTER_REGNUM
10860                                       : REGNO (elim));
10861               return new_reg_loc_descr (base_reg, offset);
10862             }
10863
10864           gcc_assert (frame_pointer_fb_offset_valid);
10865           offset += frame_pointer_fb_offset;
10866           return new_loc_descr (DW_OP_fbreg, offset, 0);
10867         }
10868     }
10869
10870   regno = REGNO (reg);
10871 #ifdef LEAF_REG_REMAP
10872   if (crtl->uses_only_leaf_regs)
10873     {
10874       int leaf_reg = LEAF_REG_REMAP (regno);
10875       if (leaf_reg != -1)
10876         regno = (unsigned) leaf_reg;
10877     }
10878 #endif
10879   regno = DWARF_FRAME_REGNUM (regno);
10880
10881   if (!optimize && fde
10882       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10883     {
10884       /* Use cfa+offset to represent the location of arguments passed
10885          on the stack when drap is used to align stack.
10886          Only do this when not optimizing, for optimized code var-tracking
10887          is supposed to track where the arguments live and the register
10888          used as vdrap or drap in some spot might be used for something
10889          else in other part of the routine.  */
10890       return new_loc_descr (DW_OP_fbreg, offset, 0);
10891     }
10892
10893   if (regno <= 31)
10894     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10895                             offset, 0);
10896   else
10897     result = new_loc_descr (DW_OP_bregx, regno, offset);
10898
10899   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10900     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10901
10902   return result;
10903 }
10904
10905 /* Return true if this RTL expression describes a base+offset calculation.  */
10906
10907 static inline int
10908 is_based_loc (const_rtx rtl)
10909 {
10910   return (GET_CODE (rtl) == PLUS
10911           && ((REG_P (XEXP (rtl, 0))
10912                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10913                && CONST_INT_P (XEXP (rtl, 1)))));
10914 }
10915
10916 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10917    failed.  */
10918
10919 static dw_loc_descr_ref
10920 tls_mem_loc_descriptor (rtx mem)
10921 {
10922   tree base;
10923   dw_loc_descr_ref loc_result;
10924
10925   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10926     return NULL;
10927
10928   base = get_base_address (MEM_EXPR (mem));
10929   if (base == NULL
10930       || TREE_CODE (base) != VAR_DECL
10931       || !DECL_THREAD_LOCAL_P (base))
10932     return NULL;
10933
10934   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10935   if (loc_result == NULL)
10936     return NULL;
10937
10938   if (MEM_OFFSET (mem))
10939     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10940
10941   return loc_result;
10942 }
10943
10944 /* Output debug info about reason why we failed to expand expression as dwarf
10945    expression.  */
10946
10947 static void
10948 expansion_failed (tree expr, rtx rtl, char const *reason)
10949 {
10950   if (dump_file && (dump_flags & TDF_DETAILS))
10951     {
10952       fprintf (dump_file, "Failed to expand as dwarf: ");
10953       if (expr)
10954         print_generic_expr (dump_file, expr, dump_flags);
10955       if (rtl)
10956         {
10957           fprintf (dump_file, "\n");
10958           print_rtl (dump_file, rtl);
10959         }
10960       fprintf (dump_file, "\nReason: %s\n", reason);
10961     }
10962 }
10963
10964 /* Helper function for const_ok_for_output, called either directly
10965    or via for_each_rtx.  */
10966
10967 static int
10968 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10969 {
10970   rtx rtl = *rtlp;
10971
10972   if (GET_CODE (rtl) == UNSPEC)
10973     {
10974       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10975          we can't express it in the debug info.  */
10976 #ifdef ENABLE_CHECKING
10977       /* Don't complain about TLS UNSPECs, those are just too hard to
10978          delegitimize.  Note this could be a non-decl SYMBOL_REF such as
10979          one in a constant pool entry, so testing SYMBOL_REF_TLS_MODEL
10980          rather than DECL_THREAD_LOCAL_P is not just an optimization.  */
10981       if (XVECLEN (rtl, 0) == 0
10982           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10983           || SYMBOL_REF_TLS_MODEL (XVECEXP (rtl, 0, 0)) == TLS_MODEL_NONE)
10984         inform (current_function_decl
10985                 ? DECL_SOURCE_LOCATION (current_function_decl)
10986                 : UNKNOWN_LOCATION,
10987 #if NUM_UNSPEC_VALUES > 0
10988                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10989                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10990                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10991                 XINT (rtl, 1));
10992 #else
10993                 "non-delegitimized UNSPEC %d found in variable location",
10994                 XINT (rtl, 1));
10995 #endif
10996 #endif
10997       expansion_failed (NULL_TREE, rtl,
10998                         "UNSPEC hasn't been delegitimized.\n");
10999       return 1;
11000     }
11001
11002   if (targetm.const_not_ok_for_debug_p (rtl))
11003     {
11004       expansion_failed (NULL_TREE, rtl,
11005                         "Expression rejected for debug by the backend.\n");
11006       return 1;
11007     }
11008
11009   if (GET_CODE (rtl) != SYMBOL_REF)
11010     return 0;
11011
11012   if (CONSTANT_POOL_ADDRESS_P (rtl))
11013     {
11014       bool marked;
11015       get_pool_constant_mark (rtl, &marked);
11016       /* If all references to this pool constant were optimized away,
11017          it was not output and thus we can't represent it.  */
11018       if (!marked)
11019         {
11020           expansion_failed (NULL_TREE, rtl,
11021                             "Constant was removed from constant pool.\n");
11022           return 1;
11023         }
11024     }
11025
11026   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11027     return 1;
11028
11029   /* Avoid references to external symbols in debug info, on several targets
11030      the linker might even refuse to link when linking a shared library,
11031      and in many other cases the relocations for .debug_info/.debug_loc are
11032      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11033      to be defined within the same shared library or executable are fine.  */
11034   if (SYMBOL_REF_EXTERNAL_P (rtl))
11035     {
11036       tree decl = SYMBOL_REF_DECL (rtl);
11037
11038       if (decl == NULL || !targetm.binds_local_p (decl))
11039         {
11040           expansion_failed (NULL_TREE, rtl,
11041                             "Symbol not defined in current TU.\n");
11042           return 1;
11043         }
11044     }
11045
11046   return 0;
11047 }
11048
11049 /* Return true if constant RTL can be emitted in DW_OP_addr or
11050    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11051    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11052
11053 static bool
11054 const_ok_for_output (rtx rtl)
11055 {
11056   if (GET_CODE (rtl) == SYMBOL_REF)
11057     return const_ok_for_output_1 (&rtl, NULL) == 0;
11058
11059   if (GET_CODE (rtl) == CONST)
11060     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11061
11062   return true;
11063 }
11064
11065 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11066    if possible, NULL otherwise.  */
11067
11068 static dw_die_ref
11069 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11070 {
11071   dw_die_ref type_die;
11072   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11073
11074   if (type == NULL)
11075     return NULL;
11076   switch (TREE_CODE (type))
11077     {
11078     case INTEGER_TYPE:
11079     case REAL_TYPE:
11080       break;
11081     default:
11082       return NULL;
11083     }
11084   type_die = lookup_type_die (type);
11085   if (!type_die)
11086     type_die = modified_type_die (type, false, false, comp_unit_die ());
11087   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11088     return NULL;
11089   return type_die;
11090 }
11091
11092 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11093    type matching MODE, or, if MODE is narrower than or as wide as
11094    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11095    possible.  */
11096
11097 static dw_loc_descr_ref
11098 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11099 {
11100   enum machine_mode outer_mode = mode;
11101   dw_die_ref type_die;
11102   dw_loc_descr_ref cvt;
11103
11104   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11105     {
11106       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11107       return op;
11108     }
11109   type_die = base_type_for_mode (outer_mode, 1);
11110   if (type_die == NULL)
11111     return NULL;
11112   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11113   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11114   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11115   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11116   add_loc_descr (&op, cvt);
11117   return op;
11118 }
11119
11120 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11121
11122 static dw_loc_descr_ref
11123 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11124                         dw_loc_descr_ref op1)
11125 {
11126   dw_loc_descr_ref ret = op0;
11127   add_loc_descr (&ret, op1);
11128   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11129   if (STORE_FLAG_VALUE != 1)
11130     {
11131       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11132       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11133     }
11134   return ret;
11135 }
11136
11137 /* Return location descriptor for signed comparison OP RTL.  */
11138
11139 static dw_loc_descr_ref
11140 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11141                          enum machine_mode mem_mode)
11142 {
11143   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11144   dw_loc_descr_ref op0, op1;
11145   int shift;
11146
11147   if (op_mode == VOIDmode)
11148     op_mode = GET_MODE (XEXP (rtl, 1));
11149   if (op_mode == VOIDmode)
11150     return NULL;
11151
11152   if (dwarf_strict
11153       && (GET_MODE_CLASS (op_mode) != MODE_INT
11154           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11155     return NULL;
11156
11157   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11158                             VAR_INIT_STATUS_INITIALIZED);
11159   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11160                             VAR_INIT_STATUS_INITIALIZED);
11161
11162   if (op0 == NULL || op1 == NULL)
11163     return NULL;
11164
11165   if (GET_MODE_CLASS (op_mode) != MODE_INT
11166       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11167     return compare_loc_descriptor (op, op0, op1);
11168
11169   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11170     {
11171       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11172       dw_loc_descr_ref cvt;
11173
11174       if (type_die == NULL)
11175         return NULL;
11176       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11177       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11178       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11179       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11180       add_loc_descr (&op0, cvt);
11181       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11182       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11183       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11184       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11185       add_loc_descr (&op1, cvt);
11186       return compare_loc_descriptor (op, op0, op1);
11187     }
11188
11189   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11190   /* For eq/ne, if the operands are known to be zero-extended,
11191      there is no need to do the fancy shifting up.  */
11192   if (op == DW_OP_eq || op == DW_OP_ne)
11193     {
11194       dw_loc_descr_ref last0, last1;
11195       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11196         ;
11197       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11198         ;
11199       /* deref_size zero extends, and for constants we can check
11200          whether they are zero extended or not.  */
11201       if (((last0->dw_loc_opc == DW_OP_deref_size
11202             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11203            || (CONST_INT_P (XEXP (rtl, 0))
11204                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11205                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11206           && ((last1->dw_loc_opc == DW_OP_deref_size
11207                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11208               || (CONST_INT_P (XEXP (rtl, 1))
11209                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11210                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11211         return compare_loc_descriptor (op, op0, op1);
11212
11213       /* EQ/NE comparison against constant in narrower type than
11214          DWARF2_ADDR_SIZE can be performed either as
11215          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
11216          DW_OP_{eq,ne}
11217          or
11218          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
11219          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
11220       if (CONST_INT_P (XEXP (rtl, 1))
11221           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
11222           && (size_of_int_loc_descriptor (shift) + 1
11223               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
11224               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
11225                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11226                                                & GET_MODE_MASK (op_mode))))
11227         {
11228           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
11229           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11230           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
11231                                     & GET_MODE_MASK (op_mode));
11232           return compare_loc_descriptor (op, op0, op1);
11233         }
11234     }
11235   add_loc_descr (&op0, int_loc_descriptor (shift));
11236   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11237   if (CONST_INT_P (XEXP (rtl, 1)))
11238     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11239   else
11240     {
11241       add_loc_descr (&op1, int_loc_descriptor (shift));
11242       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11243     }
11244   return compare_loc_descriptor (op, op0, op1);
11245 }
11246
11247 /* Return location descriptor for unsigned comparison OP RTL.  */
11248
11249 static dw_loc_descr_ref
11250 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11251                          enum machine_mode mem_mode)
11252 {
11253   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11254   dw_loc_descr_ref op0, op1;
11255
11256   if (op_mode == VOIDmode)
11257     op_mode = GET_MODE (XEXP (rtl, 1));
11258   if (op_mode == VOIDmode)
11259     return NULL;
11260   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11261     return NULL;
11262
11263   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11264     return NULL;
11265
11266   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11267                             VAR_INIT_STATUS_INITIALIZED);
11268   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11269                             VAR_INIT_STATUS_INITIALIZED);
11270
11271   if (op0 == NULL || op1 == NULL)
11272     return NULL;
11273
11274   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11275     {
11276       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11277       dw_loc_descr_ref last0, last1;
11278       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11279         ;
11280       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11281         ;
11282       if (CONST_INT_P (XEXP (rtl, 0)))
11283         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11284       /* deref_size zero extends, so no need to mask it again.  */
11285       else if (last0->dw_loc_opc != DW_OP_deref_size
11286                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11287         {
11288           add_loc_descr (&op0, int_loc_descriptor (mask));
11289           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11290         }
11291       if (CONST_INT_P (XEXP (rtl, 1)))
11292         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11293       /* deref_size zero extends, so no need to mask it again.  */
11294       else if (last1->dw_loc_opc != DW_OP_deref_size
11295                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11296         {
11297           add_loc_descr (&op1, int_loc_descriptor (mask));
11298           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11299         }
11300     }
11301   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11302     {
11303       HOST_WIDE_INT bias = 1;
11304       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11305       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11306       if (CONST_INT_P (XEXP (rtl, 1)))
11307         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11308                                   + INTVAL (XEXP (rtl, 1)));
11309       else
11310         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11311                                             bias, 0));
11312     }
11313   return compare_loc_descriptor (op, op0, op1);
11314 }
11315
11316 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11317
11318 static dw_loc_descr_ref
11319 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11320                        enum machine_mode mem_mode)
11321 {
11322   enum dwarf_location_atom op;
11323   dw_loc_descr_ref op0, op1, ret;
11324   dw_loc_descr_ref bra_node, drop_node;
11325
11326   if (dwarf_strict
11327       && (GET_MODE_CLASS (mode) != MODE_INT
11328           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11329     return NULL;
11330
11331   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11332                             VAR_INIT_STATUS_INITIALIZED);
11333   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11334                             VAR_INIT_STATUS_INITIALIZED);
11335
11336   if (op0 == NULL || op1 == NULL)
11337     return NULL;
11338
11339   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11340   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11341   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11342   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11343     {
11344       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11345         {
11346           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11347           add_loc_descr (&op0, int_loc_descriptor (mask));
11348           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11349           add_loc_descr (&op1, int_loc_descriptor (mask));
11350           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11351         }
11352       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11353         {
11354           HOST_WIDE_INT bias = 1;
11355           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11356           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11357           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11358         }
11359     }
11360   else if (GET_MODE_CLASS (mode) == MODE_INT
11361            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11362     {
11363       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11364       add_loc_descr (&op0, int_loc_descriptor (shift));
11365       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11366       add_loc_descr (&op1, int_loc_descriptor (shift));
11367       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11368     }
11369   else if (GET_MODE_CLASS (mode) == MODE_INT
11370            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11371     {
11372       dw_die_ref type_die = base_type_for_mode (mode, 0);
11373       dw_loc_descr_ref cvt;
11374       if (type_die == NULL)
11375         return NULL;
11376       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11377       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11378       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11379       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11380       add_loc_descr (&op0, cvt);
11381       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11382       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11383       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11384       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11385       add_loc_descr (&op1, cvt);
11386     }
11387
11388   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11389     op = DW_OP_lt;
11390   else
11391     op = DW_OP_gt;
11392   ret = op0;
11393   add_loc_descr (&ret, op1);
11394   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11395   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11396   add_loc_descr (&ret, bra_node);
11397   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11398   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11399   add_loc_descr (&ret, drop_node);
11400   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11401   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11402   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11403       && GET_MODE_CLASS (mode) == MODE_INT
11404       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11405     ret = convert_descriptor_to_mode (mode, ret);
11406   return ret;
11407 }
11408
11409 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11410    but after converting arguments to type_die, afterwards
11411    convert back to unsigned.  */
11412
11413 static dw_loc_descr_ref
11414 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11415              enum machine_mode mode, enum machine_mode mem_mode)
11416 {
11417   dw_loc_descr_ref cvt, op0, op1;
11418
11419   if (type_die == NULL)
11420     return NULL;
11421   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11422                             VAR_INIT_STATUS_INITIALIZED);
11423   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11424                             VAR_INIT_STATUS_INITIALIZED);
11425   if (op0 == NULL || op1 == NULL)
11426     return NULL;
11427   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11428   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11429   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11430   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11431   add_loc_descr (&op0, cvt);
11432   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11433   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11434   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11435   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11436   add_loc_descr (&op1, cvt);
11437   add_loc_descr (&op0, op1);
11438   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11439   return convert_descriptor_to_mode (mode, op0);
11440 }
11441
11442 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11443    const0 is DW_OP_lit0 or corresponding typed constant,
11444    const1 is DW_OP_lit1 or corresponding typed constant
11445    and constMSB is constant with just the MSB bit set
11446    for the mode):
11447        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11448    L1: const0 DW_OP_swap
11449    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11450        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11451    L3: DW_OP_drop
11452    L4: DW_OP_nop
11453
11454    CTZ is similar:
11455        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11456    L1: const0 DW_OP_swap
11457    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11458        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11459    L3: DW_OP_drop
11460    L4: DW_OP_nop
11461
11462    FFS is similar:
11463        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11464    L1: const1 DW_OP_swap
11465    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11466        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11467    L3: DW_OP_drop
11468    L4: DW_OP_nop  */
11469
11470 static dw_loc_descr_ref
11471 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11472                     enum machine_mode mem_mode)
11473 {
11474   dw_loc_descr_ref op0, ret, tmp;
11475   HOST_WIDE_INT valv;
11476   dw_loc_descr_ref l1jump, l1label;
11477   dw_loc_descr_ref l2jump, l2label;
11478   dw_loc_descr_ref l3jump, l3label;
11479   dw_loc_descr_ref l4jump, l4label;
11480   rtx msb;
11481
11482   if (GET_MODE_CLASS (mode) != MODE_INT
11483       || GET_MODE (XEXP (rtl, 0)) != mode
11484       || (GET_CODE (rtl) == CLZ
11485           && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_DOUBLE_INT))
11486     return NULL;
11487
11488   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11489                             VAR_INIT_STATUS_INITIALIZED);
11490   if (op0 == NULL)
11491     return NULL;
11492   ret = op0;
11493   if (GET_CODE (rtl) == CLZ)
11494     {
11495       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11496         valv = GET_MODE_BITSIZE (mode);
11497     }
11498   else if (GET_CODE (rtl) == FFS)
11499     valv = 0;
11500   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11501     valv = GET_MODE_BITSIZE (mode);
11502   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11503   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11504   add_loc_descr (&ret, l1jump);
11505   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11506   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11507                             VAR_INIT_STATUS_INITIALIZED);
11508   if (tmp == NULL)
11509     return NULL;
11510   add_loc_descr (&ret, tmp);
11511   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11512   add_loc_descr (&ret, l4jump);
11513   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11514                                 ? const1_rtx : const0_rtx,
11515                                 mode, mem_mode,
11516                                 VAR_INIT_STATUS_INITIALIZED);
11517   if (l1label == NULL)
11518     return NULL;
11519   add_loc_descr (&ret, l1label);
11520   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11521   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11522   add_loc_descr (&ret, l2label);
11523   if (GET_CODE (rtl) != CLZ)
11524     msb = const1_rtx;
11525   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11526     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11527                    << (GET_MODE_BITSIZE (mode) - 1));
11528   else
11529     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11530                                   << (GET_MODE_BITSIZE (mode)
11531                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11532   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11533     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11534                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11535                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11536   else
11537     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11538                               VAR_INIT_STATUS_INITIALIZED);
11539   if (tmp == NULL)
11540     return NULL;
11541   add_loc_descr (&ret, tmp);
11542   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11543   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11544   add_loc_descr (&ret, l3jump);
11545   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11546                             VAR_INIT_STATUS_INITIALIZED);
11547   if (tmp == NULL)
11548     return NULL;
11549   add_loc_descr (&ret, tmp);
11550   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11551                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11552   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11553   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11554   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11555   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11556   add_loc_descr (&ret, l2jump);
11557   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11558   add_loc_descr (&ret, l3label);
11559   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11560   add_loc_descr (&ret, l4label);
11561   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11562   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11563   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11564   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11565   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11566   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11567   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11568   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11569   return ret;
11570 }
11571
11572 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11573    const1 is DW_OP_lit1 or corresponding typed constant):
11574        const0 DW_OP_swap
11575    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11576        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11577    L2: DW_OP_drop
11578
11579    PARITY is similar:
11580    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11581        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11582    L2: DW_OP_drop  */
11583
11584 static dw_loc_descr_ref
11585 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11586                          enum machine_mode mem_mode)
11587 {
11588   dw_loc_descr_ref op0, ret, tmp;
11589   dw_loc_descr_ref l1jump, l1label;
11590   dw_loc_descr_ref l2jump, l2label;
11591
11592   if (GET_MODE_CLASS (mode) != MODE_INT
11593       || GET_MODE (XEXP (rtl, 0)) != mode)
11594     return NULL;
11595
11596   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11597                             VAR_INIT_STATUS_INITIALIZED);
11598   if (op0 == NULL)
11599     return NULL;
11600   ret = op0;
11601   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11602                             VAR_INIT_STATUS_INITIALIZED);
11603   if (tmp == NULL)
11604     return NULL;
11605   add_loc_descr (&ret, tmp);
11606   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11607   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11608   add_loc_descr (&ret, l1label);
11609   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11610   add_loc_descr (&ret, l2jump);
11611   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11612   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11613   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11614                             VAR_INIT_STATUS_INITIALIZED);
11615   if (tmp == NULL)
11616     return NULL;
11617   add_loc_descr (&ret, tmp);
11618   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11619   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11620                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11621   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11622   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11623                             VAR_INIT_STATUS_INITIALIZED);
11624   add_loc_descr (&ret, tmp);
11625   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11626   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11627   add_loc_descr (&ret, l1jump);
11628   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11629   add_loc_descr (&ret, l2label);
11630   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11631   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11632   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11633   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11634   return ret;
11635 }
11636
11637 /* BSWAP (constS is initial shift count, either 56 or 24):
11638        constS const0
11639    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11640        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11641        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11642        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11643    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11644
11645 static dw_loc_descr_ref
11646 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11647                       enum machine_mode mem_mode)
11648 {
11649   dw_loc_descr_ref op0, ret, tmp;
11650   dw_loc_descr_ref l1jump, l1label;
11651   dw_loc_descr_ref l2jump, l2label;
11652
11653   if (GET_MODE_CLASS (mode) != MODE_INT
11654       || BITS_PER_UNIT != 8
11655       || (GET_MODE_BITSIZE (mode) != 32
11656           &&  GET_MODE_BITSIZE (mode) != 64))
11657     return NULL;
11658
11659   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11660                             VAR_INIT_STATUS_INITIALIZED);
11661   if (op0 == NULL)
11662     return NULL;
11663
11664   ret = op0;
11665   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11666                             mode, mem_mode,
11667                             VAR_INIT_STATUS_INITIALIZED);
11668   if (tmp == NULL)
11669     return NULL;
11670   add_loc_descr (&ret, tmp);
11671   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11672                             VAR_INIT_STATUS_INITIALIZED);
11673   if (tmp == NULL)
11674     return NULL;
11675   add_loc_descr (&ret, tmp);
11676   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11677   add_loc_descr (&ret, l1label);
11678   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11679                             mode, mem_mode,
11680                             VAR_INIT_STATUS_INITIALIZED);
11681   add_loc_descr (&ret, tmp);
11682   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11683   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11684   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11685   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11686                             VAR_INIT_STATUS_INITIALIZED);
11687   if (tmp == NULL)
11688     return NULL;
11689   add_loc_descr (&ret, tmp);
11690   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11691   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11692   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11693   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11694   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11695   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11696   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11697                             VAR_INIT_STATUS_INITIALIZED);
11698   add_loc_descr (&ret, tmp);
11699   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11700   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11701   add_loc_descr (&ret, l2jump);
11702   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11703                             VAR_INIT_STATUS_INITIALIZED);
11704   add_loc_descr (&ret, tmp);
11705   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11706   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11707   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11708   add_loc_descr (&ret, l1jump);
11709   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11710   add_loc_descr (&ret, l2label);
11711   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11712   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11713   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11714   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11715   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11716   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11717   return ret;
11718 }
11719
11720 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11721    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11722    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11723    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11724
11725    ROTATERT is similar:
11726    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11727    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11728    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11729
11730 static dw_loc_descr_ref
11731 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11732                        enum machine_mode mem_mode)
11733 {
11734   rtx rtlop1 = XEXP (rtl, 1);
11735   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11736   int i;
11737
11738   if (GET_MODE_CLASS (mode) != MODE_INT)
11739     return NULL;
11740
11741   if (GET_MODE (rtlop1) != VOIDmode
11742       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11743     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11744   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11745                             VAR_INIT_STATUS_INITIALIZED);
11746   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11747                             VAR_INIT_STATUS_INITIALIZED);
11748   if (op0 == NULL || op1 == NULL)
11749     return NULL;
11750   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11751     for (i = 0; i < 2; i++)
11752       {
11753         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11754           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11755                                         mode, mem_mode,
11756                                         VAR_INIT_STATUS_INITIALIZED);
11757         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11758           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11759                                    ? DW_OP_const4u
11760                                    : HOST_BITS_PER_WIDE_INT == 64
11761                                    ? DW_OP_const8u : DW_OP_constu,
11762                                    GET_MODE_MASK (mode), 0);
11763         else
11764           mask[i] = NULL;
11765         if (mask[i] == NULL)
11766           return NULL;
11767         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11768       }
11769   ret = op0;
11770   add_loc_descr (&ret, op1);
11771   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11772   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11773   if (GET_CODE (rtl) == ROTATERT)
11774     {
11775       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11776       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11777                                           GET_MODE_BITSIZE (mode), 0));
11778     }
11779   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11780   if (mask[0] != NULL)
11781     add_loc_descr (&ret, mask[0]);
11782   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11783   if (mask[1] != NULL)
11784     {
11785       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11786       add_loc_descr (&ret, mask[1]);
11787       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11788     }
11789   if (GET_CODE (rtl) == ROTATE)
11790     {
11791       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11792       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11793                                           GET_MODE_BITSIZE (mode), 0));
11794     }
11795   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11796   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11797   return ret;
11798 }
11799
11800 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11801    for DEBUG_PARAMETER_REF RTL.  */
11802
11803 static dw_loc_descr_ref
11804 parameter_ref_descriptor (rtx rtl)
11805 {
11806   dw_loc_descr_ref ret;
11807   dw_die_ref ref;
11808
11809   if (dwarf_strict)
11810     return NULL;
11811   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11812   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11813   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11814   if (ref)
11815     {
11816       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11817       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11818       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11819     }
11820   else
11821     {
11822       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11823       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11824     }
11825   return ret;
11826 }
11827
11828 /* The following routine converts the RTL for a variable or parameter
11829    (resident in memory) into an equivalent Dwarf representation of a
11830    mechanism for getting the address of that same variable onto the top of a
11831    hypothetical "address evaluation" stack.
11832
11833    When creating memory location descriptors, we are effectively transforming
11834    the RTL for a memory-resident object into its Dwarf postfix expression
11835    equivalent.  This routine recursively descends an RTL tree, turning
11836    it into Dwarf postfix code as it goes.
11837
11838    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11839
11840    MEM_MODE is the mode of the memory reference, needed to handle some
11841    autoincrement addressing modes.
11842
11843    Return 0 if we can't represent the location.  */
11844
11845 dw_loc_descr_ref
11846 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11847                     enum machine_mode mem_mode,
11848                     enum var_init_status initialized)
11849 {
11850   dw_loc_descr_ref mem_loc_result = NULL;
11851   enum dwarf_location_atom op;
11852   dw_loc_descr_ref op0, op1;
11853   rtx inner = NULL_RTX;
11854
11855   if (mode == VOIDmode)
11856     mode = GET_MODE (rtl);
11857
11858   /* Note that for a dynamically sized array, the location we will generate a
11859      description of here will be the lowest numbered location which is
11860      actually within the array.  That's *not* necessarily the same as the
11861      zeroth element of the array.  */
11862
11863   rtl = targetm.delegitimize_address (rtl);
11864
11865   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11866     return NULL;
11867
11868   switch (GET_CODE (rtl))
11869     {
11870     case POST_INC:
11871     case POST_DEC:
11872     case POST_MODIFY:
11873       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11874
11875     case SUBREG:
11876       /* The case of a subreg may arise when we have a local (register)
11877          variable or a formal (register) parameter which doesn't quite fill
11878          up an entire register.  For now, just assume that it is
11879          legitimate to make the Dwarf info refer to the whole register which
11880          contains the given subreg.  */
11881       if (!subreg_lowpart_p (rtl))
11882         break;
11883       inner = SUBREG_REG (rtl);
11884     case TRUNCATE:
11885       if (inner == NULL_RTX)
11886         inner = XEXP (rtl, 0);
11887       if (GET_MODE_CLASS (mode) == MODE_INT
11888           && GET_MODE_CLASS (GET_MODE (inner)) == MODE_INT
11889           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11890 #ifdef POINTERS_EXTEND_UNSIGNED
11891               || (mode == Pmode && mem_mode != VOIDmode)
11892 #endif
11893              )
11894           && GET_MODE_SIZE (GET_MODE (inner)) <= DWARF2_ADDR_SIZE)
11895         {
11896           mem_loc_result = mem_loc_descriptor (inner,
11897                                                GET_MODE (inner),
11898                                                mem_mode, initialized);
11899           break;
11900         }
11901       if (dwarf_strict)
11902         break;
11903       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (inner)))
11904         break;
11905       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (inner))
11906           && (GET_MODE_CLASS (mode) != MODE_INT
11907               || GET_MODE_CLASS (GET_MODE (inner)) != MODE_INT))
11908         break;
11909       else
11910         {
11911           dw_die_ref type_die;
11912           dw_loc_descr_ref cvt;
11913
11914           mem_loc_result = mem_loc_descriptor (inner,
11915                                                GET_MODE (inner),
11916                                                mem_mode, initialized);
11917           if (mem_loc_result == NULL)
11918             break;
11919           type_die = base_type_for_mode (mode,
11920                                          GET_MODE_CLASS (mode) == MODE_INT);
11921           if (type_die == NULL)
11922             {
11923               mem_loc_result = NULL;
11924               break;
11925             }
11926           if (GET_MODE_SIZE (mode)
11927               != GET_MODE_SIZE (GET_MODE (inner)))
11928             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11929           else
11930             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11931           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11932           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11933           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11934           add_loc_descr (&mem_loc_result, cvt);
11935         }
11936       break;
11937
11938     case REG:
11939       if (GET_MODE_CLASS (mode) != MODE_INT
11940           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11941               && rtl != arg_pointer_rtx
11942               && rtl != frame_pointer_rtx
11943 #ifdef POINTERS_EXTEND_UNSIGNED
11944               && (mode != Pmode || mem_mode == VOIDmode)
11945 #endif
11946               ))
11947         {
11948           dw_die_ref type_die;
11949           unsigned int dbx_regnum;
11950
11951           if (dwarf_strict)
11952             break;
11953           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11954             break;
11955           type_die = base_type_for_mode (mode,
11956                                          GET_MODE_CLASS (mode) == MODE_INT);
11957           if (type_die == NULL)
11958             break;
11959
11960           dbx_regnum = dbx_reg_number (rtl);
11961           if (dbx_regnum == IGNORED_DWARF_REGNUM)
11962             break;
11963           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11964                                           dbx_regnum, 0);
11965           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11966           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11967           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11968           break;
11969         }
11970       /* Whenever a register number forms a part of the description of the
11971          method for calculating the (dynamic) address of a memory resident
11972          object, DWARF rules require the register number be referred to as
11973          a "base register".  This distinction is not based in any way upon
11974          what category of register the hardware believes the given register
11975          belongs to.  This is strictly DWARF terminology we're dealing with
11976          here. Note that in cases where the location of a memory-resident
11977          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11978          OP_CONST (0)) the actual DWARF location descriptor that we generate
11979          may just be OP_BASEREG (basereg).  This may look deceptively like
11980          the object in question was allocated to a register (rather than in
11981          memory) so DWARF consumers need to be aware of the subtle
11982          distinction between OP_REG and OP_BASEREG.  */
11983       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11984         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11985       else if (stack_realign_drap
11986                && crtl->drap_reg
11987                && crtl->args.internal_arg_pointer == rtl
11988                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11989         {
11990           /* If RTL is internal_arg_pointer, which has been optimized
11991              out, use DRAP instead.  */
11992           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11993                                             VAR_INIT_STATUS_INITIALIZED);
11994         }
11995       break;
11996
11997     case SIGN_EXTEND:
11998     case ZERO_EXTEND:
11999       if (GET_MODE_CLASS (mode) != MODE_INT)
12000         break;
12001       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12002                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12003       if (op0 == 0)
12004         break;
12005       else if (GET_CODE (rtl) == ZERO_EXTEND
12006                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12007                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12008                   < HOST_BITS_PER_WIDE_INT
12009                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12010                   to expand zero extend as two shifts instead of
12011                   masking.  */
12012                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12013         {
12014           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12015           mem_loc_result = op0;
12016           add_loc_descr (&mem_loc_result,
12017                          int_loc_descriptor (GET_MODE_MASK (imode)));
12018           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12019         }
12020       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12021         {
12022           int shift = DWARF2_ADDR_SIZE
12023                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12024           shift *= BITS_PER_UNIT;
12025           if (GET_CODE (rtl) == SIGN_EXTEND)
12026             op = DW_OP_shra;
12027           else
12028             op = DW_OP_shr;
12029           mem_loc_result = op0;
12030           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12031           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12032           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12033           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12034         }
12035       else if (!dwarf_strict)
12036         {
12037           dw_die_ref type_die1, type_die2;
12038           dw_loc_descr_ref cvt;
12039
12040           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12041                                           GET_CODE (rtl) == ZERO_EXTEND);
12042           if (type_die1 == NULL)
12043             break;
12044           type_die2 = base_type_for_mode (mode, 1);
12045           if (type_die2 == NULL)
12046             break;
12047           mem_loc_result = op0;
12048           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12049           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12050           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12051           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12052           add_loc_descr (&mem_loc_result, cvt);
12053           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12054           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12055           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12056           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12057           add_loc_descr (&mem_loc_result, cvt);
12058         }
12059       break;
12060
12061     case MEM:
12062       {
12063         rtx new_rtl = avoid_constant_pool_reference (rtl);
12064         if (new_rtl != rtl)
12065           {
12066             mem_loc_result = mem_loc_descriptor (new_rtl, mode, mem_mode,
12067                                                  initialized);
12068             if (mem_loc_result != NULL)
12069               return mem_loc_result;
12070           }
12071       }
12072       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12073                                            get_address_mode (rtl), mode,
12074                                            VAR_INIT_STATUS_INITIALIZED);
12075       if (mem_loc_result == NULL)
12076         mem_loc_result = tls_mem_loc_descriptor (rtl);
12077       if (mem_loc_result != NULL)
12078         {
12079           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12080               || GET_MODE_CLASS (mode) != MODE_INT)
12081             {
12082               dw_die_ref type_die;
12083               dw_loc_descr_ref deref;
12084
12085               if (dwarf_strict)
12086                 return NULL;
12087               type_die
12088                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12089               if (type_die == NULL)
12090                 return NULL;
12091               deref = new_loc_descr (DW_OP_GNU_deref_type,
12092                                      GET_MODE_SIZE (mode), 0);
12093               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12094               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12095               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12096               add_loc_descr (&mem_loc_result, deref);
12097             }
12098           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12099             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12100           else
12101             add_loc_descr (&mem_loc_result,
12102                            new_loc_descr (DW_OP_deref_size,
12103                                           GET_MODE_SIZE (mode), 0));
12104         }
12105       break;
12106
12107     case LO_SUM:
12108       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12109
12110     case LABEL_REF:
12111       /* Some ports can transform a symbol ref into a label ref, because
12112          the symbol ref is too far away and has to be dumped into a constant
12113          pool.  */
12114     case CONST:
12115     case SYMBOL_REF:
12116       if (GET_MODE_CLASS (mode) != MODE_INT
12117           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12118 #ifdef POINTERS_EXTEND_UNSIGNED
12119               && (mode != Pmode || mem_mode == VOIDmode)
12120 #endif
12121               ))
12122         break;
12123       if (GET_CODE (rtl) == SYMBOL_REF
12124           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12125         {
12126           dw_loc_descr_ref temp;
12127
12128           /* If this is not defined, we have no way to emit the data.  */
12129           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12130             break;
12131
12132           temp = new_addr_loc_descr (rtl, dtprel_true);
12133
12134           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12135           add_loc_descr (&mem_loc_result, temp);
12136
12137           break;
12138         }
12139
12140       if (!const_ok_for_output (rtl))
12141         break;
12142
12143     symref:
12144       mem_loc_result = new_addr_loc_descr (rtl, dtprel_false);
12145       vec_safe_push (used_rtx_array, rtl);
12146       break;
12147
12148     case CONCAT:
12149     case CONCATN:
12150     case VAR_LOCATION:
12151     case DEBUG_IMPLICIT_PTR:
12152       expansion_failed (NULL_TREE, rtl,
12153                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12154       return 0;
12155
12156     case ENTRY_VALUE:
12157       if (dwarf_strict)
12158         return NULL;
12159       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12160         {
12161           if (GET_MODE_CLASS (mode) != MODE_INT
12162               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12163             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12164                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12165           else
12166             {
12167               unsigned int dbx_regnum = dbx_reg_number (ENTRY_VALUE_EXP (rtl));
12168               if (dbx_regnum == IGNORED_DWARF_REGNUM)
12169                 return NULL;
12170               op0 = one_reg_loc_descriptor (dbx_regnum,
12171                                             VAR_INIT_STATUS_INITIALIZED);
12172             }
12173         }
12174       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12175                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12176         {
12177           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12178                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12179           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12180             return NULL;
12181         }
12182       else
12183         gcc_unreachable ();
12184       if (op0 == NULL)
12185         return NULL;
12186       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12187       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12188       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12189       break;
12190
12191     case DEBUG_PARAMETER_REF:
12192       mem_loc_result = parameter_ref_descriptor (rtl);
12193       break;
12194
12195     case PRE_MODIFY:
12196       /* Extract the PLUS expression nested inside and fall into
12197          PLUS code below.  */
12198       rtl = XEXP (rtl, 1);
12199       goto plus;
12200
12201     case PRE_INC:
12202     case PRE_DEC:
12203       /* Turn these into a PLUS expression and fall into the PLUS code
12204          below.  */
12205       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12206                           GEN_INT (GET_CODE (rtl) == PRE_INC
12207                                    ? GET_MODE_UNIT_SIZE (mem_mode)
12208                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
12209
12210       /* ... fall through ...  */
12211
12212     case PLUS:
12213     plus:
12214       if (is_based_loc (rtl)
12215           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12216               || XEXP (rtl, 0) == arg_pointer_rtx
12217               || XEXP (rtl, 0) == frame_pointer_rtx)
12218           && GET_MODE_CLASS (mode) == MODE_INT)
12219         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12220                                           INTVAL (XEXP (rtl, 1)),
12221                                           VAR_INIT_STATUS_INITIALIZED);
12222       else
12223         {
12224           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12225                                                VAR_INIT_STATUS_INITIALIZED);
12226           if (mem_loc_result == 0)
12227             break;
12228
12229           if (CONST_INT_P (XEXP (rtl, 1))
12230               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12231             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12232           else
12233             {
12234               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12235                                         VAR_INIT_STATUS_INITIALIZED);
12236               if (op1 == 0)
12237                 break;
12238               add_loc_descr (&mem_loc_result, op1);
12239               add_loc_descr (&mem_loc_result,
12240                              new_loc_descr (DW_OP_plus, 0, 0));
12241             }
12242         }
12243       break;
12244
12245     /* If a pseudo-reg is optimized away, it is possible for it to
12246        be replaced with a MEM containing a multiply or shift.  */
12247     case MINUS:
12248       op = DW_OP_minus;
12249       goto do_binop;
12250
12251     case MULT:
12252       op = DW_OP_mul;
12253       goto do_binop;
12254
12255     case DIV:
12256       if (!dwarf_strict
12257           && GET_MODE_CLASS (mode) == MODE_INT
12258           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12259         {
12260           mem_loc_result = typed_binop (DW_OP_div, rtl,
12261                                         base_type_for_mode (mode, 0),
12262                                         mode, mem_mode);
12263           break;
12264         }
12265       op = DW_OP_div;
12266       goto do_binop;
12267
12268     case UMOD:
12269       op = DW_OP_mod;
12270       goto do_binop;
12271
12272     case ASHIFT:
12273       op = DW_OP_shl;
12274       goto do_shift;
12275
12276     case ASHIFTRT:
12277       op = DW_OP_shra;
12278       goto do_shift;
12279
12280     case LSHIFTRT:
12281       op = DW_OP_shr;
12282       goto do_shift;
12283
12284     do_shift:
12285       if (GET_MODE_CLASS (mode) != MODE_INT)
12286         break;
12287       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12288                                 VAR_INIT_STATUS_INITIALIZED);
12289       {
12290         rtx rtlop1 = XEXP (rtl, 1);
12291         if (GET_MODE (rtlop1) != VOIDmode
12292             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12293                < GET_MODE_BITSIZE (mode))
12294           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12295         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12296                                   VAR_INIT_STATUS_INITIALIZED);
12297       }
12298
12299       if (op0 == 0 || op1 == 0)
12300         break;
12301
12302       mem_loc_result = op0;
12303       add_loc_descr (&mem_loc_result, op1);
12304       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12305       break;
12306
12307     case AND:
12308       op = DW_OP_and;
12309       goto do_binop;
12310
12311     case IOR:
12312       op = DW_OP_or;
12313       goto do_binop;
12314
12315     case XOR:
12316       op = DW_OP_xor;
12317       goto do_binop;
12318
12319     do_binop:
12320       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12321                                 VAR_INIT_STATUS_INITIALIZED);
12322       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12323                                 VAR_INIT_STATUS_INITIALIZED);
12324
12325       if (op0 == 0 || op1 == 0)
12326         break;
12327
12328       mem_loc_result = op0;
12329       add_loc_descr (&mem_loc_result, op1);
12330       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12331       break;
12332
12333     case MOD:
12334       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12335         {
12336           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12337                                         base_type_for_mode (mode, 0),
12338                                         mode, mem_mode);
12339           break;
12340         }
12341
12342       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12343                                 VAR_INIT_STATUS_INITIALIZED);
12344       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12345                                 VAR_INIT_STATUS_INITIALIZED);
12346
12347       if (op0 == 0 || op1 == 0)
12348         break;
12349
12350       mem_loc_result = op0;
12351       add_loc_descr (&mem_loc_result, op1);
12352       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12353       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12354       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12355       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12356       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12357       break;
12358
12359     case UDIV:
12360       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12361         {
12362           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12363             {
12364               op = DW_OP_div;
12365               goto do_binop;
12366             }
12367           mem_loc_result = typed_binop (DW_OP_div, rtl,
12368                                         base_type_for_mode (mode, 1),
12369                                         mode, mem_mode);
12370         }
12371       break;
12372
12373     case NOT:
12374       op = DW_OP_not;
12375       goto do_unop;
12376
12377     case ABS:
12378       op = DW_OP_abs;
12379       goto do_unop;
12380
12381     case NEG:
12382       op = DW_OP_neg;
12383       goto do_unop;
12384
12385     do_unop:
12386       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12387                                 VAR_INIT_STATUS_INITIALIZED);
12388
12389       if (op0 == 0)
12390         break;
12391
12392       mem_loc_result = op0;
12393       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12394       break;
12395
12396     case CONST_INT:
12397       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12398 #ifdef POINTERS_EXTEND_UNSIGNED
12399           || (mode == Pmode
12400               && mem_mode != VOIDmode
12401               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12402 #endif
12403           )
12404         {
12405           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12406           break;
12407         }
12408       if (!dwarf_strict
12409           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12410               || GET_MODE_BITSIZE (mode) == HOST_BITS_PER_DOUBLE_INT))
12411         {
12412           dw_die_ref type_die = base_type_for_mode (mode, 1);
12413           enum machine_mode amode;
12414           if (type_die == NULL)
12415             return NULL;
12416           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
12417                                  MODE_INT, 0);
12418           if (INTVAL (rtl) >= 0
12419               && amode != BLKmode
12420               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
12421               /* const DW_OP_GNU_convert <XXX> vs.
12422                  DW_OP_GNU_const_type <XXX, 1, const>.  */
12423               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
12424                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
12425             {
12426               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12427               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12428               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12429               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12430               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
12431               add_loc_descr (&mem_loc_result, op0);
12432               return mem_loc_result;
12433             }
12434           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12435                                           INTVAL (rtl));
12436           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12437           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12438           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12439           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12440             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12441           else
12442             {
12443               mem_loc_result->dw_loc_oprnd2.val_class
12444                 = dw_val_class_const_double;
12445               mem_loc_result->dw_loc_oprnd2.v.val_double
12446                 = double_int::from_shwi (INTVAL (rtl));
12447             }
12448         }
12449       break;
12450
12451     case CONST_DOUBLE:
12452       if (!dwarf_strict)
12453         {
12454           dw_die_ref type_die;
12455
12456           /* Note that a CONST_DOUBLE rtx could represent either an integer
12457              or a floating-point constant.  A CONST_DOUBLE is used whenever
12458              the constant requires more than one word in order to be
12459              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12460           if (mode == VOIDmode
12461               || (GET_MODE (rtl) == VOIDmode
12462                   && GET_MODE_BITSIZE (mode) != HOST_BITS_PER_DOUBLE_INT))
12463             break;
12464           type_die = base_type_for_mode (mode,
12465                                          GET_MODE_CLASS (mode) == MODE_INT);
12466           if (type_die == NULL)
12467             return NULL;
12468           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12469           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12470           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12471           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12472           if (SCALAR_FLOAT_MODE_P (mode))
12473             {
12474               unsigned int length = GET_MODE_SIZE (mode);
12475               unsigned char *array
12476                   = (unsigned char*) ggc_alloc_atomic (length);
12477
12478               insert_float (rtl, array);
12479               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12480               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12481               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12482               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12483             }
12484           else
12485             {
12486               mem_loc_result->dw_loc_oprnd2.val_class
12487                 = dw_val_class_const_double;
12488               mem_loc_result->dw_loc_oprnd2.v.val_double
12489                 = rtx_to_double_int (rtl);
12490             }
12491         }
12492       break;
12493
12494     case EQ:
12495       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12496       break;
12497
12498     case GE:
12499       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12500       break;
12501
12502     case GT:
12503       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12504       break;
12505
12506     case LE:
12507       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12508       break;
12509
12510     case LT:
12511       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12512       break;
12513
12514     case NE:
12515       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12516       break;
12517
12518     case GEU:
12519       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12520       break;
12521
12522     case GTU:
12523       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12524       break;
12525
12526     case LEU:
12527       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12528       break;
12529
12530     case LTU:
12531       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12532       break;
12533
12534     case UMIN:
12535     case UMAX:
12536       if (GET_MODE_CLASS (mode) != MODE_INT)
12537         break;
12538       /* FALLTHRU */
12539     case SMIN:
12540     case SMAX:
12541       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12542       break;
12543
12544     case ZERO_EXTRACT:
12545     case SIGN_EXTRACT:
12546       if (CONST_INT_P (XEXP (rtl, 1))
12547           && CONST_INT_P (XEXP (rtl, 2))
12548           && ((unsigned) INTVAL (XEXP (rtl, 1))
12549               + (unsigned) INTVAL (XEXP (rtl, 2))
12550               <= GET_MODE_BITSIZE (mode))
12551           && GET_MODE_CLASS (mode) == MODE_INT
12552           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12553           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12554         {
12555           int shift, size;
12556           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12557                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12558           if (op0 == 0)
12559             break;
12560           if (GET_CODE (rtl) == SIGN_EXTRACT)
12561             op = DW_OP_shra;
12562           else
12563             op = DW_OP_shr;
12564           mem_loc_result = op0;
12565           size = INTVAL (XEXP (rtl, 1));
12566           shift = INTVAL (XEXP (rtl, 2));
12567           if (BITS_BIG_ENDIAN)
12568             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12569                     - shift - size;
12570           if (shift + size != (int) DWARF2_ADDR_SIZE)
12571             {
12572               add_loc_descr (&mem_loc_result,
12573                              int_loc_descriptor (DWARF2_ADDR_SIZE
12574                                                  - shift - size));
12575               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12576             }
12577           if (size != (int) DWARF2_ADDR_SIZE)
12578             {
12579               add_loc_descr (&mem_loc_result,
12580                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12581               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12582             }
12583         }
12584       break;
12585
12586     case IF_THEN_ELSE:
12587       {
12588         dw_loc_descr_ref op2, bra_node, drop_node;
12589         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12590                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12591                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12592                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12593         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12594                                   VAR_INIT_STATUS_INITIALIZED);
12595         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12596                                   VAR_INIT_STATUS_INITIALIZED);
12597         if (op0 == NULL || op1 == NULL || op2 == NULL)
12598           break;
12599
12600         mem_loc_result = op1;
12601         add_loc_descr (&mem_loc_result, op2);
12602         add_loc_descr (&mem_loc_result, op0);
12603         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12604         add_loc_descr (&mem_loc_result, bra_node);
12605         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12606         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12607         add_loc_descr (&mem_loc_result, drop_node);
12608         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12609         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12610       }
12611       break;
12612
12613     case FLOAT_EXTEND:
12614     case FLOAT_TRUNCATE:
12615     case FLOAT:
12616     case UNSIGNED_FLOAT:
12617     case FIX:
12618     case UNSIGNED_FIX:
12619       if (!dwarf_strict)
12620         {
12621           dw_die_ref type_die;
12622           dw_loc_descr_ref cvt;
12623
12624           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12625                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12626           if (op0 == NULL)
12627             break;
12628           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12629               && (GET_CODE (rtl) == FLOAT
12630                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12631                      <= DWARF2_ADDR_SIZE))
12632             {
12633               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12634                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12635               if (type_die == NULL)
12636                 break;
12637               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12638               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12639               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12640               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12641               add_loc_descr (&op0, cvt);
12642             }
12643           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12644           if (type_die == NULL)
12645             break;
12646           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12647           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12648           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12649           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12650           add_loc_descr (&op0, cvt);
12651           if (GET_MODE_CLASS (mode) == MODE_INT
12652               && (GET_CODE (rtl) == FIX
12653                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12654             {
12655               op0 = convert_descriptor_to_mode (mode, op0);
12656               if (op0 == NULL)
12657                 break;
12658             }
12659           mem_loc_result = op0;
12660         }
12661       break;
12662
12663     case CLZ:
12664     case CTZ:
12665     case FFS:
12666       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12667       break;
12668
12669     case POPCOUNT:
12670     case PARITY:
12671       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12672       break;
12673
12674     case BSWAP:
12675       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12676       break;
12677
12678     case ROTATE:
12679     case ROTATERT:
12680       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12681       break;
12682
12683     case COMPARE:
12684       /* In theory, we could implement the above.  */
12685       /* DWARF cannot represent the unsigned compare operations
12686          natively.  */
12687     case SS_MULT:
12688     case US_MULT:
12689     case SS_DIV:
12690     case US_DIV:
12691     case SS_PLUS:
12692     case US_PLUS:
12693     case SS_MINUS:
12694     case US_MINUS:
12695     case SS_NEG:
12696     case US_NEG:
12697     case SS_ABS:
12698     case SS_ASHIFT:
12699     case US_ASHIFT:
12700     case SS_TRUNCATE:
12701     case US_TRUNCATE:
12702     case UNORDERED:
12703     case ORDERED:
12704     case UNEQ:
12705     case UNGE:
12706     case UNGT:
12707     case UNLE:
12708     case UNLT:
12709     case LTGT:
12710     case FRACT_CONVERT:
12711     case UNSIGNED_FRACT_CONVERT:
12712     case SAT_FRACT:
12713     case UNSIGNED_SAT_FRACT:
12714     case SQRT:
12715     case ASM_OPERANDS:
12716     case VEC_MERGE:
12717     case VEC_SELECT:
12718     case VEC_CONCAT:
12719     case VEC_DUPLICATE:
12720     case UNSPEC:
12721     case HIGH:
12722     case FMA:
12723     case STRICT_LOW_PART:
12724     case CONST_VECTOR:
12725     case CONST_FIXED:
12726     case CLRSB:
12727     case CLOBBER:
12728       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12729          can't express it in the debug info.  This can happen e.g. with some
12730          TLS UNSPECs.  */
12731       break;
12732
12733     case CONST_STRING:
12734       resolve_one_addr (&rtl, NULL);
12735       goto symref;
12736
12737     default:
12738 #ifdef ENABLE_CHECKING
12739       print_rtl (stderr, rtl);
12740       gcc_unreachable ();
12741 #else
12742       break;
12743 #endif
12744     }
12745
12746   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12747     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12748
12749   return mem_loc_result;
12750 }
12751
12752 /* Return a descriptor that describes the concatenation of two locations.
12753    This is typically a complex variable.  */
12754
12755 static dw_loc_descr_ref
12756 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12757 {
12758   dw_loc_descr_ref cc_loc_result = NULL;
12759   dw_loc_descr_ref x0_ref
12760     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12761   dw_loc_descr_ref x1_ref
12762     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12763
12764   if (x0_ref == 0 || x1_ref == 0)
12765     return 0;
12766
12767   cc_loc_result = x0_ref;
12768   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12769
12770   add_loc_descr (&cc_loc_result, x1_ref);
12771   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12772
12773   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12774     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12775
12776   return cc_loc_result;
12777 }
12778
12779 /* Return a descriptor that describes the concatenation of N
12780    locations.  */
12781
12782 static dw_loc_descr_ref
12783 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12784 {
12785   unsigned int i;
12786   dw_loc_descr_ref cc_loc_result = NULL;
12787   unsigned int n = XVECLEN (concatn, 0);
12788
12789   for (i = 0; i < n; ++i)
12790     {
12791       dw_loc_descr_ref ref;
12792       rtx x = XVECEXP (concatn, 0, i);
12793
12794       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12795       if (ref == NULL)
12796         return NULL;
12797
12798       add_loc_descr (&cc_loc_result, ref);
12799       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12800     }
12801
12802   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12803     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12804
12805   return cc_loc_result;
12806 }
12807
12808 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12809    for DEBUG_IMPLICIT_PTR RTL.  */
12810
12811 static dw_loc_descr_ref
12812 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12813 {
12814   dw_loc_descr_ref ret;
12815   dw_die_ref ref;
12816
12817   if (dwarf_strict)
12818     return NULL;
12819   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12820               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12821               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12822   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12823   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12824   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12825   if (ref)
12826     {
12827       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12828       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12829       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12830     }
12831   else
12832     {
12833       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12834       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12835     }
12836   return ret;
12837 }
12838
12839 /* Output a proper Dwarf location descriptor for a variable or parameter
12840    which is either allocated in a register or in a memory location.  For a
12841    register, we just generate an OP_REG and the register number.  For a
12842    memory location we provide a Dwarf postfix expression describing how to
12843    generate the (dynamic) address of the object onto the address stack.
12844
12845    MODE is mode of the decl if this loc_descriptor is going to be used in
12846    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12847    allowed, VOIDmode otherwise.
12848
12849    If we don't know how to describe it, return 0.  */
12850
12851 static dw_loc_descr_ref
12852 loc_descriptor (rtx rtl, enum machine_mode mode,
12853                 enum var_init_status initialized)
12854 {
12855   dw_loc_descr_ref loc_result = NULL;
12856
12857   switch (GET_CODE (rtl))
12858     {
12859     case SUBREG:
12860       /* The case of a subreg may arise when we have a local (register)
12861          variable or a formal (register) parameter which doesn't quite fill
12862          up an entire register.  For now, just assume that it is
12863          legitimate to make the Dwarf info refer to the whole register which
12864          contains the given subreg.  */
12865       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12866         loc_result = loc_descriptor (SUBREG_REG (rtl),
12867                                      GET_MODE (SUBREG_REG (rtl)), initialized);
12868       else
12869         goto do_default;
12870       break;
12871
12872     case REG:
12873       loc_result = reg_loc_descriptor (rtl, initialized);
12874       break;
12875
12876     case MEM:
12877       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12878                                        GET_MODE (rtl), initialized);
12879       if (loc_result == NULL)
12880         loc_result = tls_mem_loc_descriptor (rtl);
12881       if (loc_result == NULL)
12882         {
12883           rtx new_rtl = avoid_constant_pool_reference (rtl);
12884           if (new_rtl != rtl)
12885             loc_result = loc_descriptor (new_rtl, mode, initialized);
12886         }
12887       break;
12888
12889     case CONCAT:
12890       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12891                                           initialized);
12892       break;
12893
12894     case CONCATN:
12895       loc_result = concatn_loc_descriptor (rtl, initialized);
12896       break;
12897
12898     case VAR_LOCATION:
12899       /* Single part.  */
12900       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12901         {
12902           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12903           if (GET_CODE (loc) == EXPR_LIST)
12904             loc = XEXP (loc, 0);
12905           loc_result = loc_descriptor (loc, mode, initialized);
12906           break;
12907         }
12908
12909       rtl = XEXP (rtl, 1);
12910       /* FALLTHRU */
12911
12912     case PARALLEL:
12913       {
12914         rtvec par_elems = XVEC (rtl, 0);
12915         int num_elem = GET_NUM_ELEM (par_elems);
12916         enum machine_mode mode;
12917         int i;
12918
12919         /* Create the first one, so we have something to add to.  */
12920         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12921                                      VOIDmode, initialized);
12922         if (loc_result == NULL)
12923           return NULL;
12924         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12925         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12926         for (i = 1; i < num_elem; i++)
12927           {
12928             dw_loc_descr_ref temp;
12929
12930             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12931                                    VOIDmode, initialized);
12932             if (temp == NULL)
12933               return NULL;
12934             add_loc_descr (&loc_result, temp);
12935             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12936             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12937           }
12938       }
12939       break;
12940
12941     case CONST_INT:
12942       if (mode != VOIDmode && mode != BLKmode)
12943         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12944                                                     INTVAL (rtl));
12945       break;
12946
12947     case CONST_DOUBLE:
12948       if (mode == VOIDmode)
12949         mode = GET_MODE (rtl);
12950
12951       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12952         {
12953           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12954
12955           /* Note that a CONST_DOUBLE rtx could represent either an integer
12956              or a floating-point constant.  A CONST_DOUBLE is used whenever
12957              the constant requires more than one word in order to be
12958              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12959           loc_result = new_loc_descr (DW_OP_implicit_value,
12960                                       GET_MODE_SIZE (mode), 0);
12961           if (SCALAR_FLOAT_MODE_P (mode))
12962             {
12963               unsigned int length = GET_MODE_SIZE (mode);
12964               unsigned char *array
12965                   = (unsigned char*) ggc_alloc_atomic (length);
12966
12967               insert_float (rtl, array);
12968               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12969               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12970               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12971               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12972             }
12973           else
12974             {
12975               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12976               loc_result->dw_loc_oprnd2.v.val_double
12977                 = rtx_to_double_int (rtl);
12978             }
12979         }
12980       break;
12981
12982     case CONST_VECTOR:
12983       if (mode == VOIDmode)
12984         mode = GET_MODE (rtl);
12985
12986       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12987         {
12988           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12989           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12990           unsigned char *array = (unsigned char *)
12991             ggc_alloc_atomic (length * elt_size);
12992           unsigned int i;
12993           unsigned char *p;
12994
12995           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12996           switch (GET_MODE_CLASS (mode))
12997             {
12998             case MODE_VECTOR_INT:
12999               for (i = 0, p = array; i < length; i++, p += elt_size)
13000                 {
13001                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13002                   double_int val = rtx_to_double_int (elt);
13003
13004                   if (elt_size <= sizeof (HOST_WIDE_INT))
13005                     insert_int (val.to_shwi (), elt_size, p);
13006                   else
13007                     {
13008                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13009                       insert_double (val, p);
13010                     }
13011                 }
13012               break;
13013
13014             case MODE_VECTOR_FLOAT:
13015               for (i = 0, p = array; i < length; i++, p += elt_size)
13016                 {
13017                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13018                   insert_float (elt, p);
13019                 }
13020               break;
13021
13022             default:
13023               gcc_unreachable ();
13024             }
13025
13026           loc_result = new_loc_descr (DW_OP_implicit_value,
13027                                       length * elt_size, 0);
13028           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13029           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13030           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13031           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13032         }
13033       break;
13034
13035     case CONST:
13036       if (mode == VOIDmode
13037           || CONST_SCALAR_INT_P (XEXP (rtl, 0))
13038           || CONST_DOUBLE_AS_FLOAT_P (XEXP (rtl, 0))
13039           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13040         {
13041           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13042           break;
13043         }
13044       /* FALLTHROUGH */
13045     case SYMBOL_REF:
13046       if (!const_ok_for_output (rtl))
13047         break;
13048     case LABEL_REF:
13049       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13050           && (dwarf_version >= 4 || !dwarf_strict))
13051         {
13052          loc_result = new_addr_loc_descr (rtl, dtprel_false);
13053           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13054           vec_safe_push (used_rtx_array, rtl);
13055         }
13056       break;
13057
13058     case DEBUG_IMPLICIT_PTR:
13059       loc_result = implicit_ptr_descriptor (rtl, 0);
13060       break;
13061
13062     case PLUS:
13063       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13064           && CONST_INT_P (XEXP (rtl, 1)))
13065         {
13066           loc_result
13067             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13068           break;
13069         }
13070       /* FALLTHRU */
13071     do_default:
13072     default:
13073       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13074            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13075            && dwarf_version >= 4)
13076           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13077         {
13078           /* Value expression.  */
13079           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13080           if (loc_result)
13081             add_loc_descr (&loc_result,
13082                            new_loc_descr (DW_OP_stack_value, 0, 0));
13083         }
13084       break;
13085     }
13086
13087   return loc_result;
13088 }
13089
13090 /* We need to figure out what section we should use as the base for the
13091    address ranges where a given location is valid.
13092    1. If this particular DECL has a section associated with it, use that.
13093    2. If this function has a section associated with it, use that.
13094    3. Otherwise, use the text section.
13095    XXX: If you split a variable across multiple sections, we won't notice.  */
13096
13097 static const char *
13098 secname_for_decl (const_tree decl)
13099 {
13100   const char *secname;
13101
13102   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13103     {
13104       tree sectree = DECL_SECTION_NAME (decl);
13105       secname = TREE_STRING_POINTER (sectree);
13106     }
13107   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13108     {
13109       tree sectree = DECL_SECTION_NAME (current_function_decl);
13110       secname = TREE_STRING_POINTER (sectree);
13111     }
13112   else if (cfun && in_cold_section_p)
13113     secname = crtl->subsections.cold_section_label;
13114   else
13115     secname = text_section_label;
13116
13117   return secname;
13118 }
13119
13120 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13121
13122 static bool
13123 decl_by_reference_p (tree decl)
13124 {
13125   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13126            || TREE_CODE (decl) == VAR_DECL)
13127           && DECL_BY_REFERENCE (decl));
13128 }
13129
13130 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13131    for VARLOC.  */
13132
13133 static dw_loc_descr_ref
13134 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13135                enum var_init_status initialized)
13136 {
13137   int have_address = 0;
13138   dw_loc_descr_ref descr;
13139   enum machine_mode mode;
13140
13141   if (want_address != 2)
13142     {
13143       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13144       /* Single part.  */
13145       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13146         {
13147           varloc = PAT_VAR_LOCATION_LOC (varloc);
13148           if (GET_CODE (varloc) == EXPR_LIST)
13149             varloc = XEXP (varloc, 0);
13150           mode = GET_MODE (varloc);
13151           if (MEM_P (varloc))
13152             {
13153               rtx addr = XEXP (varloc, 0);
13154               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13155                                           mode, initialized);
13156               if (descr)
13157                 have_address = 1;
13158               else
13159                 {
13160                   rtx x = avoid_constant_pool_reference (varloc);
13161                   if (x != varloc)
13162                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13163                                                 initialized);
13164                 }
13165             }
13166           else
13167             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13168         }
13169       else
13170         return 0;
13171     }
13172   else
13173     {
13174       if (GET_CODE (varloc) == VAR_LOCATION)
13175         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13176       else
13177         mode = DECL_MODE (loc);
13178       descr = loc_descriptor (varloc, mode, initialized);
13179       have_address = 1;
13180     }
13181
13182   if (!descr)
13183     return 0;
13184
13185   if (want_address == 2 && !have_address
13186       && (dwarf_version >= 4 || !dwarf_strict))
13187     {
13188       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13189         {
13190           expansion_failed (loc, NULL_RTX,
13191                             "DWARF address size mismatch");
13192           return 0;
13193         }
13194       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13195       have_address = 1;
13196     }
13197   /* Show if we can't fill the request for an address.  */
13198   if (want_address && !have_address)
13199     {
13200       expansion_failed (loc, NULL_RTX,
13201                         "Want address and only have value");
13202       return 0;
13203     }
13204
13205   /* If we've got an address and don't want one, dereference.  */
13206   if (!want_address && have_address)
13207     {
13208       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13209       enum dwarf_location_atom op;
13210
13211       if (size > DWARF2_ADDR_SIZE || size == -1)
13212         {
13213           expansion_failed (loc, NULL_RTX,
13214                             "DWARF address size mismatch");
13215           return 0;
13216         }
13217       else if (size == DWARF2_ADDR_SIZE)
13218         op = DW_OP_deref;
13219       else
13220         op = DW_OP_deref_size;
13221
13222       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13223     }
13224
13225   return descr;
13226 }
13227
13228 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13229    if it is not possible.  */
13230
13231 static dw_loc_descr_ref
13232 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13233 {
13234   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13235     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13236   else if (dwarf_version >= 3 || !dwarf_strict)
13237     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13238   else
13239     return NULL;
13240 }
13241
13242 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13243    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13244
13245 static dw_loc_descr_ref
13246 dw_sra_loc_expr (tree decl, rtx loc)
13247 {
13248   rtx p;
13249   unsigned int padsize = 0;
13250   dw_loc_descr_ref descr, *descr_tail;
13251   unsigned HOST_WIDE_INT decl_size;
13252   rtx varloc;
13253   enum var_init_status initialized;
13254
13255   if (DECL_SIZE (decl) == NULL
13256       || !host_integerp (DECL_SIZE (decl), 1))
13257     return NULL;
13258
13259   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13260   descr = NULL;
13261   descr_tail = &descr;
13262
13263   for (p = loc; p; p = XEXP (p, 1))
13264     {
13265       unsigned int bitsize = decl_piece_bitsize (p);
13266       rtx loc_note = *decl_piece_varloc_ptr (p);
13267       dw_loc_descr_ref cur_descr;
13268       dw_loc_descr_ref *tail, last = NULL;
13269       unsigned int opsize = 0;
13270
13271       if (loc_note == NULL_RTX
13272           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13273         {
13274           padsize += bitsize;
13275           continue;
13276         }
13277       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13278       varloc = NOTE_VAR_LOCATION (loc_note);
13279       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13280       if (cur_descr == NULL)
13281         {
13282           padsize += bitsize;
13283           continue;
13284         }
13285
13286       /* Check that cur_descr either doesn't use
13287          DW_OP_*piece operations, or their sum is equal
13288          to bitsize.  Otherwise we can't embed it.  */
13289       for (tail = &cur_descr; *tail != NULL;
13290            tail = &(*tail)->dw_loc_next)
13291         if ((*tail)->dw_loc_opc == DW_OP_piece)
13292           {
13293             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13294                       * BITS_PER_UNIT;
13295             last = *tail;
13296           }
13297         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13298           {
13299             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13300             last = *tail;
13301           }
13302
13303       if (last != NULL && opsize != bitsize)
13304         {
13305           padsize += bitsize;
13306           continue;
13307         }
13308
13309       /* If there is a hole, add DW_OP_*piece after empty DWARF
13310          expression, which means that those bits are optimized out.  */
13311       if (padsize)
13312         {
13313           if (padsize > decl_size)
13314             return NULL;
13315           decl_size -= padsize;
13316           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13317           if (*descr_tail == NULL)
13318             return NULL;
13319           descr_tail = &(*descr_tail)->dw_loc_next;
13320           padsize = 0;
13321         }
13322       *descr_tail = cur_descr;
13323       descr_tail = tail;
13324       if (bitsize > decl_size)
13325         return NULL;
13326       decl_size -= bitsize;
13327       if (last == NULL)
13328         {
13329           HOST_WIDE_INT offset = 0;
13330           if (GET_CODE (varloc) == VAR_LOCATION
13331               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13332             {
13333               varloc = PAT_VAR_LOCATION_LOC (varloc);
13334               if (GET_CODE (varloc) == EXPR_LIST)
13335                 varloc = XEXP (varloc, 0);
13336             }
13337           do 
13338             {
13339               if (GET_CODE (varloc) == CONST
13340                   || GET_CODE (varloc) == SIGN_EXTEND
13341                   || GET_CODE (varloc) == ZERO_EXTEND)
13342                 varloc = XEXP (varloc, 0);
13343               else if (GET_CODE (varloc) == SUBREG)
13344                 varloc = SUBREG_REG (varloc);
13345               else
13346                 break;
13347             }
13348           while (1);
13349           /* DW_OP_bit_size offset should be zero for register
13350              or implicit location descriptions and empty location
13351              descriptions, but for memory addresses needs big endian
13352              adjustment.  */
13353           if (MEM_P (varloc))
13354             {
13355               unsigned HOST_WIDE_INT memsize
13356                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
13357               if (memsize != bitsize)
13358                 {
13359                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13360                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13361                     return NULL;
13362                   if (memsize < bitsize)
13363                     return NULL;
13364                   if (BITS_BIG_ENDIAN)
13365                     offset = memsize - bitsize;
13366                 }
13367             }
13368
13369           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13370           if (*descr_tail == NULL)
13371             return NULL;
13372           descr_tail = &(*descr_tail)->dw_loc_next;
13373         }
13374     }
13375
13376   /* If there were any non-empty expressions, add padding till the end of
13377      the decl.  */
13378   if (descr != NULL && decl_size != 0)
13379     {
13380       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13381       if (*descr_tail == NULL)
13382         return NULL;
13383     }
13384   return descr;
13385 }
13386
13387 /* Return the dwarf representation of the location list LOC_LIST of
13388    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13389    function.  */
13390
13391 static dw_loc_list_ref
13392 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13393 {
13394   const char *endname, *secname;
13395   rtx varloc;
13396   enum var_init_status initialized;
13397   struct var_loc_node *node;
13398   dw_loc_descr_ref descr;
13399   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13400   dw_loc_list_ref list = NULL;
13401   dw_loc_list_ref *listp = &list;
13402
13403   /* Now that we know what section we are using for a base,
13404      actually construct the list of locations.
13405      The first location information is what is passed to the
13406      function that creates the location list, and the remaining
13407      locations just get added on to that list.
13408      Note that we only know the start address for a location
13409      (IE location changes), so to build the range, we use
13410      the range [current location start, next location start].
13411      This means we have to special case the last node, and generate
13412      a range of [last location start, end of function label].  */
13413
13414   secname = secname_for_decl (decl);
13415
13416   for (node = loc_list->first; node; node = node->next)
13417     if (GET_CODE (node->loc) == EXPR_LIST
13418         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13419       {
13420         if (GET_CODE (node->loc) == EXPR_LIST)
13421           {
13422             /* This requires DW_OP_{,bit_}piece, which is not usable
13423                inside DWARF expressions.  */
13424             if (want_address != 2)
13425               continue;
13426             descr = dw_sra_loc_expr (decl, node->loc);
13427             if (descr == NULL)
13428               continue;
13429           }
13430         else
13431           {
13432             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13433             varloc = NOTE_VAR_LOCATION (node->loc);
13434             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13435           }
13436         if (descr)
13437           {
13438             bool range_across_switch = false;
13439             /* If section switch happens in between node->label
13440                and node->next->label (or end of function) and
13441                we can't emit it as a single entry list,
13442                emit two ranges, first one ending at the end
13443                of first partition and second one starting at the
13444                beginning of second partition.  */
13445             if (node == loc_list->last_before_switch
13446                 && (node != loc_list->first || loc_list->first->next)
13447                 && current_function_decl)
13448               {
13449                 endname = cfun->fde->dw_fde_end;
13450                 range_across_switch = true;
13451               }
13452             /* The variable has a location between NODE->LABEL and
13453                NODE->NEXT->LABEL.  */
13454             else if (node->next)
13455               endname = node->next->label;
13456             /* If the variable has a location at the last label
13457                it keeps its location until the end of function.  */
13458             else if (!current_function_decl)
13459               endname = text_end_label;
13460             else
13461               {
13462                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13463                                              current_function_funcdef_no);
13464                 endname = ggc_strdup (label_id);
13465               }
13466
13467             *listp = new_loc_list (descr, node->label, endname, secname);
13468             if (TREE_CODE (decl) == PARM_DECL
13469                 && node == loc_list->first
13470                 && GET_CODE (node->loc) == NOTE
13471                 && strcmp (node->label, endname) == 0)
13472               (*listp)->force = true;
13473             listp = &(*listp)->dw_loc_next;
13474
13475             if (range_across_switch)
13476               {
13477                 if (GET_CODE (node->loc) == EXPR_LIST)
13478                   descr = dw_sra_loc_expr (decl, node->loc);
13479                 else
13480                   {
13481                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13482                     varloc = NOTE_VAR_LOCATION (node->loc);
13483                     descr = dw_loc_list_1 (decl, varloc, want_address,
13484                                            initialized);
13485                   }
13486                 gcc_assert (descr);
13487                 /* The variable has a location between NODE->LABEL and
13488                    NODE->NEXT->LABEL.  */
13489                 if (node->next)
13490                   endname = node->next->label;
13491                 else
13492                   endname = cfun->fde->dw_fde_second_end;
13493                 *listp = new_loc_list (descr,
13494                                        cfun->fde->dw_fde_second_begin,
13495                                        endname, secname);
13496                 listp = &(*listp)->dw_loc_next;
13497               }
13498           }
13499       }
13500
13501   /* Try to avoid the overhead of a location list emitting a location
13502      expression instead, but only if we didn't have more than one
13503      location entry in the first place.  If some entries were not
13504      representable, we don't want to pretend a single entry that was
13505      applies to the entire scope in which the variable is
13506      available.  */
13507   if (list && loc_list->first->next)
13508     gen_llsym (list);
13509
13510   return list;
13511 }
13512
13513 /* Return if the loc_list has only single element and thus can be represented
13514    as location description.   */
13515
13516 static bool
13517 single_element_loc_list_p (dw_loc_list_ref list)
13518 {
13519   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13520   return !list->ll_symbol;
13521 }
13522
13523 /* To each location in list LIST add loc descr REF.  */
13524
13525 static void
13526 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13527 {
13528   dw_loc_descr_ref copy;
13529   add_loc_descr (&list->expr, ref);
13530   list = list->dw_loc_next;
13531   while (list)
13532     {
13533       copy = ggc_alloc_dw_loc_descr_node ();
13534       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13535       add_loc_descr (&list->expr, copy);
13536       while (copy->dw_loc_next)
13537         {
13538           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13539           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13540           copy->dw_loc_next = new_copy;
13541           copy = new_copy;
13542         }
13543       list = list->dw_loc_next;
13544     }
13545 }
13546
13547 /* Given two lists RET and LIST
13548    produce location list that is result of adding expression in LIST
13549    to expression in RET on each position in program.
13550    Might be destructive on both RET and LIST.
13551
13552    TODO: We handle only simple cases of RET or LIST having at most one
13553    element. General case would inolve sorting the lists in program order
13554    and merging them that will need some additional work.
13555    Adding that will improve quality of debug info especially for SRA-ed
13556    structures.  */
13557
13558 static void
13559 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13560 {
13561   if (!list)
13562     return;
13563   if (!*ret)
13564     {
13565       *ret = list;
13566       return;
13567     }
13568   if (!list->dw_loc_next)
13569     {
13570       add_loc_descr_to_each (*ret, list->expr);
13571       return;
13572     }
13573   if (!(*ret)->dw_loc_next)
13574     {
13575       add_loc_descr_to_each (list, (*ret)->expr);
13576       *ret = list;
13577       return;
13578     }
13579   expansion_failed (NULL_TREE, NULL_RTX,
13580                     "Don't know how to merge two non-trivial"
13581                     " location lists.\n");
13582   *ret = NULL;
13583   return;
13584 }
13585
13586 /* LOC is constant expression.  Try a luck, look it up in constant
13587    pool and return its loc_descr of its address.  */
13588
13589 static dw_loc_descr_ref
13590 cst_pool_loc_descr (tree loc)
13591 {
13592   /* Get an RTL for this, if something has been emitted.  */
13593   rtx rtl = lookup_constant_def (loc);
13594
13595   if (!rtl || !MEM_P (rtl))
13596     {
13597       gcc_assert (!rtl);
13598       return 0;
13599     }
13600   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13601
13602   /* TODO: We might get more coverage if we was actually delaying expansion
13603      of all expressions till end of compilation when constant pools are fully
13604      populated.  */
13605   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13606     {
13607       expansion_failed (loc, NULL_RTX,
13608                         "CST value in contant pool but not marked.");
13609       return 0;
13610     }
13611   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13612                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13613 }
13614
13615 /* Return dw_loc_list representing address of addr_expr LOC
13616    by looking for inner INDIRECT_REF expression and turning
13617    it into simple arithmetics.  */
13618
13619 static dw_loc_list_ref
13620 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13621 {
13622   tree obj, offset;
13623   HOST_WIDE_INT bitsize, bitpos, bytepos;
13624   enum machine_mode mode;
13625   int unsignedp, volatilep = 0;
13626   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13627
13628   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13629                              &bitsize, &bitpos, &offset, &mode,
13630                              &unsignedp, &volatilep, false);
13631   STRIP_NOPS (obj);
13632   if (bitpos % BITS_PER_UNIT)
13633     {
13634       expansion_failed (loc, NULL_RTX, "bitfield access");
13635       return 0;
13636     }
13637   if (!INDIRECT_REF_P (obj))
13638     {
13639       expansion_failed (obj,
13640                         NULL_RTX, "no indirect ref in inner refrence");
13641       return 0;
13642     }
13643   if (!offset && !bitpos)
13644     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13645   else if (toplev
13646            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13647            && (dwarf_version >= 4 || !dwarf_strict))
13648     {
13649       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13650       if (!list_ret)
13651         return 0;
13652       if (offset)
13653         {
13654           /* Variable offset.  */
13655           list_ret1 = loc_list_from_tree (offset, 0);
13656           if (list_ret1 == 0)
13657             return 0;
13658           add_loc_list (&list_ret, list_ret1);
13659           if (!list_ret)
13660             return 0;
13661           add_loc_descr_to_each (list_ret,
13662                                  new_loc_descr (DW_OP_plus, 0, 0));
13663         }
13664       bytepos = bitpos / BITS_PER_UNIT;
13665       if (bytepos > 0)
13666         add_loc_descr_to_each (list_ret,
13667                                new_loc_descr (DW_OP_plus_uconst,
13668                                               bytepos, 0));
13669       else if (bytepos < 0)
13670         loc_list_plus_const (list_ret, bytepos);
13671       add_loc_descr_to_each (list_ret,
13672                              new_loc_descr (DW_OP_stack_value, 0, 0));
13673     }
13674   return list_ret;
13675 }
13676
13677
13678 /* Generate Dwarf location list representing LOC.
13679    If WANT_ADDRESS is false, expression computing LOC will be computed
13680    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13681    if WANT_ADDRESS is 2, expression computing address useable in location
13682      will be returned (i.e. DW_OP_reg can be used
13683      to refer to register values).  */
13684
13685 static dw_loc_list_ref
13686 loc_list_from_tree (tree loc, int want_address)
13687 {
13688   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13689   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13690   int have_address = 0;
13691   enum dwarf_location_atom op;
13692
13693   /* ??? Most of the time we do not take proper care for sign/zero
13694      extending the values properly.  Hopefully this won't be a real
13695      problem...  */
13696
13697   switch (TREE_CODE (loc))
13698     {
13699     case ERROR_MARK:
13700       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13701       return 0;
13702
13703     case PLACEHOLDER_EXPR:
13704       /* This case involves extracting fields from an object to determine the
13705          position of other fields.  We don't try to encode this here.  The
13706          only user of this is Ada, which encodes the needed information using
13707          the names of types.  */
13708       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13709       return 0;
13710
13711     case CALL_EXPR:
13712       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13713       /* There are no opcodes for these operations.  */
13714       return 0;
13715
13716     case PREINCREMENT_EXPR:
13717     case PREDECREMENT_EXPR:
13718     case POSTINCREMENT_EXPR:
13719     case POSTDECREMENT_EXPR:
13720       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13721       /* There are no opcodes for these operations.  */
13722       return 0;
13723
13724     case ADDR_EXPR:
13725       /* If we already want an address, see if there is INDIRECT_REF inside
13726          e.g. for &this->field.  */
13727       if (want_address)
13728         {
13729           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13730                        (loc, want_address == 2);
13731           if (list_ret)
13732             have_address = 1;
13733           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13734                    && (ret = cst_pool_loc_descr (loc)))
13735             have_address = 1;
13736         }
13737         /* Otherwise, process the argument and look for the address.  */
13738       if (!list_ret && !ret)
13739         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13740       else
13741         {
13742           if (want_address)
13743             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13744           return NULL;
13745         }
13746       break;
13747
13748     case VAR_DECL:
13749       if (DECL_THREAD_LOCAL_P (loc))
13750         {
13751           rtx rtl;
13752          enum dwarf_location_atom tls_op;
13753          enum dtprel_bool dtprel = dtprel_false;
13754
13755           if (targetm.have_tls)
13756             {
13757               /* If this is not defined, we have no way to emit the
13758                  data.  */
13759               if (!targetm.asm_out.output_dwarf_dtprel)
13760                 return 0;
13761
13762                /* The way DW_OP_GNU_push_tls_address is specified, we
13763                   can only look up addresses of objects in the current
13764                   module.  We used DW_OP_addr as first op, but that's
13765                   wrong, because DW_OP_addr is relocated by the debug
13766                   info consumer, while DW_OP_GNU_push_tls_address
13767                   operand shouldn't be.  */
13768               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13769                 return 0;
13770              dtprel = dtprel_true;
13771              tls_op = DW_OP_GNU_push_tls_address;
13772             }
13773           else
13774             {
13775               if (!targetm.emutls.debug_form_tls_address
13776                   || !(dwarf_version >= 3 || !dwarf_strict))
13777                 return 0;
13778               /* We stuffed the control variable into the DECL_VALUE_EXPR
13779                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13780                  no longer appear in gimple code.  We used the control
13781                  variable in specific so that we could pick it up here.  */
13782               loc = DECL_VALUE_EXPR (loc);
13783               tls_op = DW_OP_form_tls_address;
13784             }
13785
13786           rtl = rtl_for_decl_location (loc);
13787           if (rtl == NULL_RTX)
13788             return 0;
13789
13790           if (!MEM_P (rtl))
13791             return 0;
13792           rtl = XEXP (rtl, 0);
13793           if (! CONSTANT_P (rtl))
13794             return 0;
13795
13796           ret = new_addr_loc_descr (rtl, dtprel);
13797           ret1 = new_loc_descr (tls_op, 0, 0);
13798           add_loc_descr (&ret, ret1);
13799
13800           have_address = 1;
13801           break;
13802         }
13803       /* FALLTHRU */
13804
13805     case PARM_DECL:
13806     case RESULT_DECL:
13807       if (DECL_HAS_VALUE_EXPR_P (loc))
13808         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13809                                    want_address);
13810       /* FALLTHRU */
13811
13812     case FUNCTION_DECL:
13813       {
13814         rtx rtl;
13815         var_loc_list *loc_list = lookup_decl_loc (loc);
13816
13817         if (loc_list && loc_list->first)
13818           {
13819             list_ret = dw_loc_list (loc_list, loc, want_address);
13820             have_address = want_address != 0;
13821             break;
13822           }
13823         rtl = rtl_for_decl_location (loc);
13824         if (rtl == NULL_RTX)
13825           {
13826             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13827             return 0;
13828           }
13829         else if (CONST_INT_P (rtl))
13830           {
13831             HOST_WIDE_INT val = INTVAL (rtl);
13832             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13833               val &= GET_MODE_MASK (DECL_MODE (loc));
13834             ret = int_loc_descriptor (val);
13835           }
13836         else if (GET_CODE (rtl) == CONST_STRING)
13837           {
13838             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13839             return 0;
13840           }
13841         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13842           ret = new_addr_loc_descr (rtl, dtprel_false);
13843         else
13844           {
13845             enum machine_mode mode, mem_mode;
13846
13847             /* Certain constructs can only be represented at top-level.  */
13848             if (want_address == 2)
13849               {
13850                 ret = loc_descriptor (rtl, VOIDmode,
13851                                       VAR_INIT_STATUS_INITIALIZED);
13852                 have_address = 1;
13853               }
13854             else
13855               {
13856                 mode = GET_MODE (rtl);
13857                 mem_mode = VOIDmode;
13858                 if (MEM_P (rtl))
13859                   {
13860                     mem_mode = mode;
13861                     mode = get_address_mode (rtl);
13862                     rtl = XEXP (rtl, 0);
13863                     have_address = 1;
13864                   }
13865                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13866                                           VAR_INIT_STATUS_INITIALIZED);
13867               }
13868             if (!ret)
13869               expansion_failed (loc, rtl,
13870                                 "failed to produce loc descriptor for rtl");
13871           }
13872       }
13873       break;
13874
13875     case MEM_REF:
13876       /* ??? FIXME.  */
13877       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13878         return 0;
13879       /* Fallthru.  */
13880     case INDIRECT_REF:
13881       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13882       have_address = 1;
13883       break;
13884
13885     case COMPOUND_EXPR:
13886       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13887
13888     CASE_CONVERT:
13889     case VIEW_CONVERT_EXPR:
13890     case SAVE_EXPR:
13891     case MODIFY_EXPR:
13892       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13893
13894     case COMPONENT_REF:
13895     case BIT_FIELD_REF:
13896     case ARRAY_REF:
13897     case ARRAY_RANGE_REF:
13898     case REALPART_EXPR:
13899     case IMAGPART_EXPR:
13900       {
13901         tree obj, offset;
13902         HOST_WIDE_INT bitsize, bitpos, bytepos;
13903         enum machine_mode mode;
13904         int unsignedp, volatilep = 0;
13905
13906         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13907                                    &unsignedp, &volatilep, false);
13908
13909         gcc_assert (obj != loc);
13910
13911         list_ret = loc_list_from_tree (obj,
13912                                        want_address == 2
13913                                        && !bitpos && !offset ? 2 : 1);
13914         /* TODO: We can extract value of the small expression via shifting even
13915            for nonzero bitpos.  */
13916         if (list_ret == 0)
13917           return 0;
13918         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13919           {
13920             expansion_failed (loc, NULL_RTX,
13921                               "bitfield access");
13922             return 0;
13923           }
13924
13925         if (offset != NULL_TREE)
13926           {
13927             /* Variable offset.  */
13928             list_ret1 = loc_list_from_tree (offset, 0);
13929             if (list_ret1 == 0)
13930               return 0;
13931             add_loc_list (&list_ret, list_ret1);
13932             if (!list_ret)
13933               return 0;
13934             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13935           }
13936
13937         bytepos = bitpos / BITS_PER_UNIT;
13938         if (bytepos > 0)
13939           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13940         else if (bytepos < 0)
13941           loc_list_plus_const (list_ret, bytepos);
13942
13943         have_address = 1;
13944         break;
13945       }
13946
13947     case INTEGER_CST:
13948       if ((want_address || !host_integerp (loc, 0))
13949           && (ret = cst_pool_loc_descr (loc)))
13950         have_address = 1;
13951       else if (want_address == 2
13952                && host_integerp (loc, 0)
13953                && (ret = address_of_int_loc_descriptor
13954                            (int_size_in_bytes (TREE_TYPE (loc)),
13955                             tree_low_cst (loc, 0))))
13956         have_address = 1;
13957       else if (host_integerp (loc, 0))
13958         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13959       else
13960         {
13961           expansion_failed (loc, NULL_RTX,
13962                             "Integer operand is not host integer");
13963           return 0;
13964         }
13965       break;
13966
13967     case CONSTRUCTOR:
13968     case REAL_CST:
13969     case STRING_CST:
13970     case COMPLEX_CST:
13971       if ((ret = cst_pool_loc_descr (loc)))
13972         have_address = 1;
13973       else
13974       /* We can construct small constants here using int_loc_descriptor.  */
13975         expansion_failed (loc, NULL_RTX,
13976                           "constructor or constant not in constant pool");
13977       break;
13978
13979     case TRUTH_AND_EXPR:
13980     case TRUTH_ANDIF_EXPR:
13981     case BIT_AND_EXPR:
13982       op = DW_OP_and;
13983       goto do_binop;
13984
13985     case TRUTH_XOR_EXPR:
13986     case BIT_XOR_EXPR:
13987       op = DW_OP_xor;
13988       goto do_binop;
13989
13990     case TRUTH_OR_EXPR:
13991     case TRUTH_ORIF_EXPR:
13992     case BIT_IOR_EXPR:
13993       op = DW_OP_or;
13994       goto do_binop;
13995
13996     case FLOOR_DIV_EXPR:
13997     case CEIL_DIV_EXPR:
13998     case ROUND_DIV_EXPR:
13999     case TRUNC_DIV_EXPR:
14000       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14001         return 0;
14002       op = DW_OP_div;
14003       goto do_binop;
14004
14005     case MINUS_EXPR:
14006       op = DW_OP_minus;
14007       goto do_binop;
14008
14009     case FLOOR_MOD_EXPR:
14010     case CEIL_MOD_EXPR:
14011     case ROUND_MOD_EXPR:
14012     case TRUNC_MOD_EXPR:
14013       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14014         {
14015           op = DW_OP_mod;
14016           goto do_binop;
14017         }
14018       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14019       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14020       if (list_ret == 0 || list_ret1 == 0)
14021         return 0;
14022
14023       add_loc_list (&list_ret, list_ret1);
14024       if (list_ret == 0)
14025         return 0;
14026       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14027       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14028       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14029       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14030       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14031       break;
14032
14033     case MULT_EXPR:
14034       op = DW_OP_mul;
14035       goto do_binop;
14036
14037     case LSHIFT_EXPR:
14038       op = DW_OP_shl;
14039       goto do_binop;
14040
14041     case RSHIFT_EXPR:
14042       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14043       goto do_binop;
14044
14045     case POINTER_PLUS_EXPR:
14046     case PLUS_EXPR:
14047       if (host_integerp (TREE_OPERAND (loc, 1), 0))
14048         {
14049           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14050           if (list_ret == 0)
14051             return 0;
14052
14053           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14054           break;
14055         }
14056
14057       op = DW_OP_plus;
14058       goto do_binop;
14059
14060     case LE_EXPR:
14061       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14062         return 0;
14063
14064       op = DW_OP_le;
14065       goto do_binop;
14066
14067     case GE_EXPR:
14068       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14069         return 0;
14070
14071       op = DW_OP_ge;
14072       goto do_binop;
14073
14074     case LT_EXPR:
14075       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14076         return 0;
14077
14078       op = DW_OP_lt;
14079       goto do_binop;
14080
14081     case GT_EXPR:
14082       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14083         return 0;
14084
14085       op = DW_OP_gt;
14086       goto do_binop;
14087
14088     case EQ_EXPR:
14089       op = DW_OP_eq;
14090       goto do_binop;
14091
14092     case NE_EXPR:
14093       op = DW_OP_ne;
14094       goto do_binop;
14095
14096     do_binop:
14097       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14098       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14099       if (list_ret == 0 || list_ret1 == 0)
14100         return 0;
14101
14102       add_loc_list (&list_ret, list_ret1);
14103       if (list_ret == 0)
14104         return 0;
14105       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14106       break;
14107
14108     case TRUTH_NOT_EXPR:
14109     case BIT_NOT_EXPR:
14110       op = DW_OP_not;
14111       goto do_unop;
14112
14113     case ABS_EXPR:
14114       op = DW_OP_abs;
14115       goto do_unop;
14116
14117     case NEGATE_EXPR:
14118       op = DW_OP_neg;
14119       goto do_unop;
14120
14121     do_unop:
14122       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14123       if (list_ret == 0)
14124         return 0;
14125
14126       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14127       break;
14128
14129     case MIN_EXPR:
14130     case MAX_EXPR:
14131       {
14132         const enum tree_code code =
14133           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14134
14135         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14136                       build2 (code, integer_type_node,
14137                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14138                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14139       }
14140
14141       /* ... fall through ...  */
14142
14143     case COND_EXPR:
14144       {
14145         dw_loc_descr_ref lhs
14146           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14147         dw_loc_list_ref rhs
14148           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14149         dw_loc_descr_ref bra_node, jump_node, tmp;
14150
14151         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14152         if (list_ret == 0 || lhs == 0 || rhs == 0)
14153           return 0;
14154
14155         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14156         add_loc_descr_to_each (list_ret, bra_node);
14157
14158         add_loc_list (&list_ret, rhs);
14159         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14160         add_loc_descr_to_each (list_ret, jump_node);
14161
14162         add_loc_descr_to_each (list_ret, lhs);
14163         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14164         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14165
14166         /* ??? Need a node to point the skip at.  Use a nop.  */
14167         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14168         add_loc_descr_to_each (list_ret, tmp);
14169         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14170         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14171       }
14172       break;
14173
14174     case FIX_TRUNC_EXPR:
14175       return 0;
14176
14177     default:
14178       /* Leave front-end specific codes as simply unknown.  This comes
14179          up, for instance, with the C STMT_EXPR.  */
14180       if ((unsigned int) TREE_CODE (loc)
14181           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14182         {
14183           expansion_failed (loc, NULL_RTX,
14184                             "language specific tree node");
14185           return 0;
14186         }
14187
14188 #ifdef ENABLE_CHECKING
14189       /* Otherwise this is a generic code; we should just lists all of
14190          these explicitly.  We forgot one.  */
14191       gcc_unreachable ();
14192 #else
14193       /* In a release build, we want to degrade gracefully: better to
14194          generate incomplete debugging information than to crash.  */
14195       return NULL;
14196 #endif
14197     }
14198
14199   if (!ret && !list_ret)
14200     return 0;
14201
14202   if (want_address == 2 && !have_address
14203       && (dwarf_version >= 4 || !dwarf_strict))
14204     {
14205       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14206         {
14207           expansion_failed (loc, NULL_RTX,
14208                             "DWARF address size mismatch");
14209           return 0;
14210         }
14211       if (ret)
14212         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14213       else
14214         add_loc_descr_to_each (list_ret,
14215                                new_loc_descr (DW_OP_stack_value, 0, 0));
14216       have_address = 1;
14217     }
14218   /* Show if we can't fill the request for an address.  */
14219   if (want_address && !have_address)
14220     {
14221       expansion_failed (loc, NULL_RTX,
14222                         "Want address and only have value");
14223       return 0;
14224     }
14225
14226   gcc_assert (!ret || !list_ret);
14227
14228   /* If we've got an address and don't want one, dereference.  */
14229   if (!want_address && have_address)
14230     {
14231       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14232
14233       if (size > DWARF2_ADDR_SIZE || size == -1)
14234         {
14235           expansion_failed (loc, NULL_RTX,
14236                             "DWARF address size mismatch");
14237           return 0;
14238         }
14239       else if (size == DWARF2_ADDR_SIZE)
14240         op = DW_OP_deref;
14241       else
14242         op = DW_OP_deref_size;
14243
14244       if (ret)
14245         add_loc_descr (&ret, new_loc_descr (op, size, 0));
14246       else
14247         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14248     }
14249   if (ret)
14250     list_ret = new_loc_list (ret, NULL, NULL, NULL);
14251
14252   return list_ret;
14253 }
14254
14255 /* Same as above but return only single location expression.  */
14256 static dw_loc_descr_ref
14257 loc_descriptor_from_tree (tree loc, int want_address)
14258 {
14259   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14260   if (!ret)
14261     return NULL;
14262   if (ret->dw_loc_next)
14263     {
14264       expansion_failed (loc, NULL_RTX,
14265                         "Location list where only loc descriptor needed");
14266       return NULL;
14267     }
14268   return ret->expr;
14269 }
14270
14271 /* Given a value, round it up to the lowest multiple of `boundary'
14272    which is not less than the value itself.  */
14273
14274 static inline HOST_WIDE_INT
14275 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14276 {
14277   return (((value + boundary - 1) / boundary) * boundary);
14278 }
14279
14280 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14281    pointer to the declared type for the relevant field variable, or return
14282    `integer_type_node' if the given node turns out to be an
14283    ERROR_MARK node.  */
14284
14285 static inline tree
14286 field_type (const_tree decl)
14287 {
14288   tree type;
14289
14290   if (TREE_CODE (decl) == ERROR_MARK)
14291     return integer_type_node;
14292
14293   type = DECL_BIT_FIELD_TYPE (decl);
14294   if (type == NULL_TREE)
14295     type = TREE_TYPE (decl);
14296
14297   return type;
14298 }
14299
14300 /* Given a pointer to a tree node, return the alignment in bits for
14301    it, or else return BITS_PER_WORD if the node actually turns out to
14302    be an ERROR_MARK node.  */
14303
14304 static inline unsigned
14305 simple_type_align_in_bits (const_tree type)
14306 {
14307   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14308 }
14309
14310 static inline unsigned
14311 simple_decl_align_in_bits (const_tree decl)
14312 {
14313   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14314 }
14315
14316 /* Return the result of rounding T up to ALIGN.  */
14317
14318 static inline double_int
14319 round_up_to_align (double_int t, unsigned int align)
14320 {
14321   double_int alignd = double_int::from_uhwi (align);
14322   t += alignd;
14323   t += double_int_minus_one;
14324   t = t.div (alignd, true, TRUNC_DIV_EXPR);
14325   t *= alignd;
14326   return t;
14327 }
14328
14329 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14330    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14331    or return 0 if we are unable to determine what that offset is, either
14332    because the argument turns out to be a pointer to an ERROR_MARK node, or
14333    because the offset is actually variable.  (We can't handle the latter case
14334    just yet).  */
14335
14336 static HOST_WIDE_INT
14337 field_byte_offset (const_tree decl)
14338 {
14339   double_int object_offset_in_bits;
14340   double_int object_offset_in_bytes;
14341   double_int bitpos_int;
14342
14343   if (TREE_CODE (decl) == ERROR_MARK)
14344     return 0;
14345
14346   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14347
14348   /* We cannot yet cope with fields whose positions are variable, so
14349      for now, when we see such things, we simply return 0.  Someday, we may
14350      be able to handle such cases, but it will be damn difficult.  */
14351   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14352     return 0;
14353
14354   bitpos_int = tree_to_double_int (bit_position (decl));
14355
14356 #ifdef PCC_BITFIELD_TYPE_MATTERS
14357   if (PCC_BITFIELD_TYPE_MATTERS)
14358     {
14359       tree type;
14360       tree field_size_tree;
14361       double_int deepest_bitpos;
14362       double_int field_size_in_bits;
14363       unsigned int type_align_in_bits;
14364       unsigned int decl_align_in_bits;
14365       double_int type_size_in_bits;
14366
14367       type = field_type (decl);
14368       type_size_in_bits = double_int_type_size_in_bits (type);
14369       type_align_in_bits = simple_type_align_in_bits (type);
14370
14371       field_size_tree = DECL_SIZE (decl);
14372
14373       /* The size could be unspecified if there was an error, or for
14374          a flexible array member.  */
14375       if (!field_size_tree)
14376         field_size_tree = bitsize_zero_node;
14377
14378       /* If the size of the field is not constant, use the type size.  */
14379       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14380         field_size_in_bits = tree_to_double_int (field_size_tree);
14381       else
14382         field_size_in_bits = type_size_in_bits;
14383
14384       decl_align_in_bits = simple_decl_align_in_bits (decl);
14385
14386       /* The GCC front-end doesn't make any attempt to keep track of the
14387          starting bit offset (relative to the start of the containing
14388          structure type) of the hypothetical "containing object" for a
14389          bit-field.  Thus, when computing the byte offset value for the
14390          start of the "containing object" of a bit-field, we must deduce
14391          this information on our own. This can be rather tricky to do in
14392          some cases.  For example, handling the following structure type
14393          definition when compiling for an i386/i486 target (which only
14394          aligns long long's to 32-bit boundaries) can be very tricky:
14395
14396          struct S { int field1; long long field2:31; };
14397
14398          Fortunately, there is a simple rule-of-thumb which can be used
14399          in such cases.  When compiling for an i386/i486, GCC will
14400          allocate 8 bytes for the structure shown above.  It decides to
14401          do this based upon one simple rule for bit-field allocation.
14402          GCC allocates each "containing object" for each bit-field at
14403          the first (i.e. lowest addressed) legitimate alignment boundary
14404          (based upon the required minimum alignment for the declared
14405          type of the field) which it can possibly use, subject to the
14406          condition that there is still enough available space remaining
14407          in the containing object (when allocated at the selected point)
14408          to fully accommodate all of the bits of the bit-field itself.
14409
14410          This simple rule makes it obvious why GCC allocates 8 bytes for
14411          each object of the structure type shown above.  When looking
14412          for a place to allocate the "containing object" for `field2',
14413          the compiler simply tries to allocate a 64-bit "containing
14414          object" at each successive 32-bit boundary (starting at zero)
14415          until it finds a place to allocate that 64- bit field such that
14416          at least 31 contiguous (and previously unallocated) bits remain
14417          within that selected 64 bit field.  (As it turns out, for the
14418          example above, the compiler finds it is OK to allocate the
14419          "containing object" 64-bit field at bit-offset zero within the
14420          structure type.)
14421
14422          Here we attempt to work backwards from the limited set of facts
14423          we're given, and we try to deduce from those facts, where GCC
14424          must have believed that the containing object started (within
14425          the structure type). The value we deduce is then used (by the
14426          callers of this routine) to generate DW_AT_location and
14427          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14428          the case of DW_AT_location, regular fields as well).  */
14429
14430       /* Figure out the bit-distance from the start of the structure to
14431          the "deepest" bit of the bit-field.  */
14432       deepest_bitpos = bitpos_int + field_size_in_bits;
14433
14434       /* This is the tricky part.  Use some fancy footwork to deduce
14435          where the lowest addressed bit of the containing object must
14436          be.  */
14437       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14438
14439       /* Round up to type_align by default.  This works best for
14440          bitfields.  */
14441       object_offset_in_bits
14442         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14443
14444       if (object_offset_in_bits.ugt (bitpos_int))
14445         {
14446           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
14447
14448           /* Round up to decl_align instead.  */
14449           object_offset_in_bits
14450             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14451         }
14452     }
14453   else
14454 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14455     object_offset_in_bits = bitpos_int;
14456
14457   object_offset_in_bytes
14458     = object_offset_in_bits.div (double_int::from_uhwi (BITS_PER_UNIT),
14459                                  true, TRUNC_DIV_EXPR);
14460   return object_offset_in_bytes.to_shwi ();
14461 }
14462 \f
14463 /* The following routines define various Dwarf attributes and any data
14464    associated with them.  */
14465
14466 /* Add a location description attribute value to a DIE.
14467
14468    This emits location attributes suitable for whole variables and
14469    whole parameters.  Note that the location attributes for struct fields are
14470    generated by the routine `data_member_location_attribute' below.  */
14471
14472 static inline void
14473 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14474                              dw_loc_list_ref descr)
14475 {
14476   if (descr == 0)
14477     return;
14478   if (single_element_loc_list_p (descr))
14479     add_AT_loc (die, attr_kind, descr->expr);
14480   else
14481     add_AT_loc_list (die, attr_kind, descr);
14482 }
14483
14484 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14485
14486 static void
14487 add_accessibility_attribute (dw_die_ref die, tree decl)
14488 {
14489   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14490      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14491      the default has always been DW_ACCESS_public.  */
14492   if (TREE_PROTECTED (decl))
14493     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14494   else if (TREE_PRIVATE (decl))
14495     {
14496       if (dwarf_version == 2
14497           || die->die_parent == NULL
14498           || die->die_parent->die_tag != DW_TAG_class_type)
14499         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14500     }
14501   else if (dwarf_version > 2
14502            && die->die_parent
14503            && die->die_parent->die_tag == DW_TAG_class_type)
14504     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14505 }
14506
14507 /* Attach the specialized form of location attribute used for data members of
14508    struct and union types.  In the special case of a FIELD_DECL node which
14509    represents a bit-field, the "offset" part of this special location
14510    descriptor must indicate the distance in bytes from the lowest-addressed
14511    byte of the containing struct or union type to the lowest-addressed byte of
14512    the "containing object" for the bit-field.  (See the `field_byte_offset'
14513    function above).
14514
14515    For any given bit-field, the "containing object" is a hypothetical object
14516    (of some integral or enum type) within which the given bit-field lives.  The
14517    type of this hypothetical "containing object" is always the same as the
14518    declared type of the individual bit-field itself (for GCC anyway... the
14519    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14520    bytes) of the hypothetical "containing object" which will be given in the
14521    DW_AT_byte_size attribute for this bit-field.  (See the
14522    `byte_size_attribute' function below.)  It is also used when calculating the
14523    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14524    function below.)  */
14525
14526 static void
14527 add_data_member_location_attribute (dw_die_ref die, tree decl)
14528 {
14529   HOST_WIDE_INT offset;
14530   dw_loc_descr_ref loc_descr = 0;
14531
14532   if (TREE_CODE (decl) == TREE_BINFO)
14533     {
14534       /* We're working on the TAG_inheritance for a base class.  */
14535       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14536         {
14537           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14538              aren't at a fixed offset from all (sub)objects of the same
14539              type.  We need to extract the appropriate offset from our
14540              vtable.  The following dwarf expression means
14541
14542                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14543
14544              This is specific to the V3 ABI, of course.  */
14545
14546           dw_loc_descr_ref tmp;
14547
14548           /* Make a copy of the object address.  */
14549           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14550           add_loc_descr (&loc_descr, tmp);
14551
14552           /* Extract the vtable address.  */
14553           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14554           add_loc_descr (&loc_descr, tmp);
14555
14556           /* Calculate the address of the offset.  */
14557           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14558           gcc_assert (offset < 0);
14559
14560           tmp = int_loc_descriptor (-offset);
14561           add_loc_descr (&loc_descr, tmp);
14562           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14563           add_loc_descr (&loc_descr, tmp);
14564
14565           /* Extract the offset.  */
14566           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14567           add_loc_descr (&loc_descr, tmp);
14568
14569           /* Add it to the object address.  */
14570           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14571           add_loc_descr (&loc_descr, tmp);
14572         }
14573       else
14574         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14575     }
14576   else
14577     offset = field_byte_offset (decl);
14578
14579   if (! loc_descr)
14580     {
14581       if (dwarf_version > 2)
14582         {
14583           /* Don't need to output a location expression, just the constant. */
14584           if (offset < 0)
14585             add_AT_int (die, DW_AT_data_member_location, offset);
14586           else
14587             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14588           return;
14589         }
14590       else
14591         {
14592           enum dwarf_location_atom op;
14593
14594           /* The DWARF2 standard says that we should assume that the structure
14595              address is already on the stack, so we can specify a structure
14596              field address by using DW_OP_plus_uconst.  */
14597           op = DW_OP_plus_uconst;
14598           loc_descr = new_loc_descr (op, offset, 0);
14599         }
14600     }
14601
14602   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14603 }
14604
14605 /* Writes integer values to dw_vec_const array.  */
14606
14607 static void
14608 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14609 {
14610   while (size != 0)
14611     {
14612       *dest++ = val & 0xff;
14613       val >>= 8;
14614       --size;
14615     }
14616 }
14617
14618 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14619
14620 static HOST_WIDE_INT
14621 extract_int (const unsigned char *src, unsigned int size)
14622 {
14623   HOST_WIDE_INT val = 0;
14624
14625   src += size;
14626   while (size != 0)
14627     {
14628       val <<= 8;
14629       val |= *--src & 0xff;
14630       --size;
14631     }
14632   return val;
14633 }
14634
14635 /* Writes double_int values to dw_vec_const array.  */
14636
14637 static void
14638 insert_double (double_int val, unsigned char *dest)
14639 {
14640   unsigned char *p0 = dest;
14641   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14642
14643   if (WORDS_BIG_ENDIAN)
14644     {
14645       p0 = p1;
14646       p1 = dest;
14647     }
14648
14649   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14650   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14651 }
14652
14653 /* Writes floating point values to dw_vec_const array.  */
14654
14655 static void
14656 insert_float (const_rtx rtl, unsigned char *array)
14657 {
14658   REAL_VALUE_TYPE rv;
14659   long val[4];
14660   int i;
14661
14662   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14663   real_to_target (val, &rv, GET_MODE (rtl));
14664
14665   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14666   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14667     {
14668       insert_int (val[i], 4, array);
14669       array += 4;
14670     }
14671 }
14672
14673 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14674    does not have a "location" either in memory or in a register.  These
14675    things can arise in GNU C when a constant is passed as an actual parameter
14676    to an inlined function.  They can also arise in C++ where declared
14677    constants do not necessarily get memory "homes".  */
14678
14679 static bool
14680 add_const_value_attribute (dw_die_ref die, rtx rtl)
14681 {
14682   switch (GET_CODE (rtl))
14683     {
14684     case CONST_INT:
14685       {
14686         HOST_WIDE_INT val = INTVAL (rtl);
14687
14688         if (val < 0)
14689           add_AT_int (die, DW_AT_const_value, val);
14690         else
14691           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14692       }
14693       return true;
14694
14695     case CONST_DOUBLE:
14696       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14697          floating-point constant.  A CONST_DOUBLE is used whenever the
14698          constant requires more than one word in order to be adequately
14699          represented.  */
14700       {
14701         enum machine_mode mode = GET_MODE (rtl);
14702
14703         if (SCALAR_FLOAT_MODE_P (mode))
14704           {
14705             unsigned int length = GET_MODE_SIZE (mode);
14706             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14707
14708             insert_float (rtl, array);
14709             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14710           }
14711         else
14712           add_AT_double (die, DW_AT_const_value,
14713                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14714       }
14715       return true;
14716
14717     case CONST_VECTOR:
14718       {
14719         enum machine_mode mode = GET_MODE (rtl);
14720         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14721         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14722         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14723           (length * elt_size);
14724         unsigned int i;
14725         unsigned char *p;
14726
14727         switch (GET_MODE_CLASS (mode))
14728           {
14729           case MODE_VECTOR_INT:
14730             for (i = 0, p = array; i < length; i++, p += elt_size)
14731               {
14732                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14733                 double_int val = rtx_to_double_int (elt);
14734
14735                 if (elt_size <= sizeof (HOST_WIDE_INT))
14736                   insert_int (val.to_shwi (), elt_size, p);
14737                 else
14738                   {
14739                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14740                     insert_double (val, p);
14741                   }
14742               }
14743             break;
14744
14745           case MODE_VECTOR_FLOAT:
14746             for (i = 0, p = array; i < length; i++, p += elt_size)
14747               {
14748                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14749                 insert_float (elt, p);
14750               }
14751             break;
14752
14753           default:
14754             gcc_unreachable ();
14755           }
14756
14757         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14758       }
14759       return true;
14760
14761     case CONST_STRING:
14762       if (dwarf_version >= 4 || !dwarf_strict)
14763         {
14764           dw_loc_descr_ref loc_result;
14765           resolve_one_addr (&rtl, NULL);
14766         rtl_addr:
14767           loc_result = new_addr_loc_descr (rtl, dtprel_false);
14768           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14769           add_AT_loc (die, DW_AT_location, loc_result);
14770           vec_safe_push (used_rtx_array, rtl);
14771           return true;
14772         }
14773       return false;
14774
14775     case CONST:
14776       if (CONSTANT_P (XEXP (rtl, 0)))
14777         return add_const_value_attribute (die, XEXP (rtl, 0));
14778       /* FALLTHROUGH */
14779     case SYMBOL_REF:
14780       if (!const_ok_for_output (rtl))
14781         return false;
14782     case LABEL_REF:
14783       if (dwarf_version >= 4 || !dwarf_strict)
14784         goto rtl_addr;
14785       return false;
14786
14787     case PLUS:
14788       /* In cases where an inlined instance of an inline function is passed
14789          the address of an `auto' variable (which is local to the caller) we
14790          can get a situation where the DECL_RTL of the artificial local
14791          variable (for the inlining) which acts as a stand-in for the
14792          corresponding formal parameter (of the inline function) will look
14793          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14794          exactly a compile-time constant expression, but it isn't the address
14795          of the (artificial) local variable either.  Rather, it represents the
14796          *value* which the artificial local variable always has during its
14797          lifetime.  We currently have no way to represent such quasi-constant
14798          values in Dwarf, so for now we just punt and generate nothing.  */
14799       return false;
14800
14801     case HIGH:
14802     case CONST_FIXED:
14803       return false;
14804
14805     case MEM:
14806       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14807           && MEM_READONLY_P (rtl)
14808           && GET_MODE (rtl) == BLKmode)
14809         {
14810           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14811           return true;
14812         }
14813       return false;
14814
14815     default:
14816       /* No other kinds of rtx should be possible here.  */
14817       gcc_unreachable ();
14818     }
14819   return false;
14820 }
14821
14822 /* Determine whether the evaluation of EXPR references any variables
14823    or functions which aren't otherwise used (and therefore may not be
14824    output).  */
14825 static tree
14826 reference_to_unused (tree * tp, int * walk_subtrees,
14827                      void * data ATTRIBUTE_UNUSED)
14828 {
14829   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14830     *walk_subtrees = 0;
14831
14832   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14833       && ! TREE_ASM_WRITTEN (*tp))
14834     return *tp;
14835   /* ???  The C++ FE emits debug information for using decls, so
14836      putting gcc_unreachable here falls over.  See PR31899.  For now
14837      be conservative.  */
14838   else if (!cgraph_global_info_ready
14839            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14840     return *tp;
14841   else if (TREE_CODE (*tp) == VAR_DECL)
14842     {
14843       struct varpool_node *node = varpool_get_node (*tp);
14844       if (!node || !node->analyzed)
14845         return *tp;
14846     }
14847   else if (TREE_CODE (*tp) == FUNCTION_DECL
14848            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14849     {
14850       /* The call graph machinery must have finished analyzing,
14851          optimizing and gimplifying the CU by now.
14852          So if *TP has no call graph node associated
14853          to it, it means *TP will not be emitted.  */
14854       if (!cgraph_get_node (*tp))
14855         return *tp;
14856     }
14857   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14858     return *tp;
14859
14860   return NULL_TREE;
14861 }
14862
14863 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14864    for use in a later add_const_value_attribute call.  */
14865
14866 static rtx
14867 rtl_for_decl_init (tree init, tree type)
14868 {
14869   rtx rtl = NULL_RTX;
14870
14871   STRIP_NOPS (init);
14872
14873   /* If a variable is initialized with a string constant without embedded
14874      zeros, build CONST_STRING.  */
14875   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14876     {
14877       tree enttype = TREE_TYPE (type);
14878       tree domain = TYPE_DOMAIN (type);
14879       enum machine_mode mode = TYPE_MODE (enttype);
14880
14881       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14882           && domain
14883           && integer_zerop (TYPE_MIN_VALUE (domain))
14884           && compare_tree_int (TYPE_MAX_VALUE (domain),
14885                                TREE_STRING_LENGTH (init) - 1) == 0
14886           && ((size_t) TREE_STRING_LENGTH (init)
14887               == strlen (TREE_STRING_POINTER (init)) + 1))
14888         {
14889           rtl = gen_rtx_CONST_STRING (VOIDmode,
14890                                       ggc_strdup (TREE_STRING_POINTER (init)));
14891           rtl = gen_rtx_MEM (BLKmode, rtl);
14892           MEM_READONLY_P (rtl) = 1;
14893         }
14894     }
14895   /* Other aggregates, and complex values, could be represented using
14896      CONCAT: FIXME!  */
14897   else if (AGGREGATE_TYPE_P (type)
14898            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14899                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14900            || TREE_CODE (type) == COMPLEX_TYPE)
14901     ;
14902   /* Vectors only work if their mode is supported by the target.
14903      FIXME: generic vectors ought to work too.  */
14904   else if (TREE_CODE (type) == VECTOR_TYPE
14905            && !VECTOR_MODE_P (TYPE_MODE (type)))
14906     ;
14907   /* If the initializer is something that we know will expand into an
14908      immediate RTL constant, expand it now.  We must be careful not to
14909      reference variables which won't be output.  */
14910   else if (initializer_constant_valid_p (init, type)
14911            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14912     {
14913       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14914          possible.  */
14915       if (TREE_CODE (type) == VECTOR_TYPE)
14916         switch (TREE_CODE (init))
14917           {
14918           case VECTOR_CST:
14919             break;
14920           case CONSTRUCTOR:
14921             if (TREE_CONSTANT (init))
14922               {
14923                 vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (init);
14924                 bool constant_p = true;
14925                 tree value;
14926                 unsigned HOST_WIDE_INT ix;
14927
14928                 /* Even when ctor is constant, it might contain non-*_CST
14929                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14930                    belong into VECTOR_CST nodes.  */
14931                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14932                   if (!CONSTANT_CLASS_P (value))
14933                     {
14934                       constant_p = false;
14935                       break;
14936                     }
14937
14938                 if (constant_p)
14939                   {
14940                     init = build_vector_from_ctor (type, elts);
14941                     break;
14942                   }
14943               }
14944             /* FALLTHRU */
14945
14946           default:
14947             return NULL;
14948           }
14949
14950       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14951
14952       /* If expand_expr returns a MEM, it wasn't immediate.  */
14953       gcc_assert (!rtl || !MEM_P (rtl));
14954     }
14955
14956   return rtl;
14957 }
14958
14959 /* Generate RTL for the variable DECL to represent its location.  */
14960
14961 static rtx
14962 rtl_for_decl_location (tree decl)
14963 {
14964   rtx rtl;
14965
14966   /* Here we have to decide where we are going to say the parameter "lives"
14967      (as far as the debugger is concerned).  We only have a couple of
14968      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14969
14970      DECL_RTL normally indicates where the parameter lives during most of the
14971      activation of the function.  If optimization is enabled however, this
14972      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14973      that the parameter doesn't really live anywhere (as far as the code
14974      generation parts of GCC are concerned) during most of the function's
14975      activation.  That will happen (for example) if the parameter is never
14976      referenced within the function.
14977
14978      We could just generate a location descriptor here for all non-NULL
14979      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14980      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14981      where DECL_RTL is NULL or is a pseudo-reg.
14982
14983      Note however that we can only get away with using DECL_INCOMING_RTL as
14984      a backup substitute for DECL_RTL in certain limited cases.  In cases
14985      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14986      we can be sure that the parameter was passed using the same type as it is
14987      declared to have within the function, and that its DECL_INCOMING_RTL
14988      points us to a place where a value of that type is passed.
14989
14990      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14991      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14992      because in these cases DECL_INCOMING_RTL points us to a value of some
14993      type which is *different* from the type of the parameter itself.  Thus,
14994      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14995      such cases, the debugger would end up (for example) trying to fetch a
14996      `float' from a place which actually contains the first part of a
14997      `double'.  That would lead to really incorrect and confusing
14998      output at debug-time.
14999
15000      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15001      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15002      are a couple of exceptions however.  On little-endian machines we can
15003      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15004      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15005      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15006      when (on a little-endian machine) a non-prototyped function has a
15007      parameter declared to be of type `short' or `char'.  In such cases,
15008      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15009      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15010      passed `int' value.  If the debugger then uses that address to fetch
15011      a `short' or a `char' (on a little-endian machine) the result will be
15012      the correct data, so we allow for such exceptional cases below.
15013
15014      Note that our goal here is to describe the place where the given formal
15015      parameter lives during most of the function's activation (i.e. between the
15016      end of the prologue and the start of the epilogue).  We'll do that as best
15017      as we can. Note however that if the given formal parameter is modified
15018      sometime during the execution of the function, then a stack backtrace (at
15019      debug-time) will show the function as having been called with the *new*
15020      value rather than the value which was originally passed in.  This happens
15021      rarely enough that it is not a major problem, but it *is* a problem, and
15022      I'd like to fix it.
15023
15024      A future version of dwarf2out.c may generate two additional attributes for
15025      any given DW_TAG_formal_parameter DIE which will describe the "passed
15026      type" and the "passed location" for the given formal parameter in addition
15027      to the attributes we now generate to indicate the "declared type" and the
15028      "active location" for each parameter.  This additional set of attributes
15029      could be used by debuggers for stack backtraces. Separately, note that
15030      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15031      This happens (for example) for inlined-instances of inline function formal
15032      parameters which are never referenced.  This really shouldn't be
15033      happening.  All PARM_DECL nodes should get valid non-NULL
15034      DECL_INCOMING_RTL values.  FIXME.  */
15035
15036   /* Use DECL_RTL as the "location" unless we find something better.  */
15037   rtl = DECL_RTL_IF_SET (decl);
15038
15039   /* When generating abstract instances, ignore everything except
15040      constants, symbols living in memory, and symbols living in
15041      fixed registers.  */
15042   if (! reload_completed)
15043     {
15044       if (rtl
15045           && (CONSTANT_P (rtl)
15046               || (MEM_P (rtl)
15047                   && CONSTANT_P (XEXP (rtl, 0)))
15048               || (REG_P (rtl)
15049                   && TREE_CODE (decl) == VAR_DECL
15050                   && TREE_STATIC (decl))))
15051         {
15052           rtl = targetm.delegitimize_address (rtl);
15053           return rtl;
15054         }
15055       rtl = NULL_RTX;
15056     }
15057   else if (TREE_CODE (decl) == PARM_DECL)
15058     {
15059       if (rtl == NULL_RTX
15060           || is_pseudo_reg (rtl)
15061           || (MEM_P (rtl)
15062               && is_pseudo_reg (XEXP (rtl, 0))
15063               && DECL_INCOMING_RTL (decl)
15064               && MEM_P (DECL_INCOMING_RTL (decl))
15065               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15066         {
15067           tree declared_type = TREE_TYPE (decl);
15068           tree passed_type = DECL_ARG_TYPE (decl);
15069           enum machine_mode dmode = TYPE_MODE (declared_type);
15070           enum machine_mode pmode = TYPE_MODE (passed_type);
15071
15072           /* This decl represents a formal parameter which was optimized out.
15073              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15074              all cases where (rtl == NULL_RTX) just below.  */
15075           if (dmode == pmode)
15076             rtl = DECL_INCOMING_RTL (decl);
15077           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15078                    && SCALAR_INT_MODE_P (dmode)
15079                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15080                    && DECL_INCOMING_RTL (decl))
15081             {
15082               rtx inc = DECL_INCOMING_RTL (decl);
15083               if (REG_P (inc))
15084                 rtl = inc;
15085               else if (MEM_P (inc))
15086                 {
15087                   if (BYTES_BIG_ENDIAN)
15088                     rtl = adjust_address_nv (inc, dmode,
15089                                              GET_MODE_SIZE (pmode)
15090                                              - GET_MODE_SIZE (dmode));
15091                   else
15092                     rtl = inc;
15093                 }
15094             }
15095         }
15096
15097       /* If the parm was passed in registers, but lives on the stack, then
15098          make a big endian correction if the mode of the type of the
15099          parameter is not the same as the mode of the rtl.  */
15100       /* ??? This is the same series of checks that are made in dbxout.c before
15101          we reach the big endian correction code there.  It isn't clear if all
15102          of these checks are necessary here, but keeping them all is the safe
15103          thing to do.  */
15104       else if (MEM_P (rtl)
15105                && XEXP (rtl, 0) != const0_rtx
15106                && ! CONSTANT_P (XEXP (rtl, 0))
15107                /* Not passed in memory.  */
15108                && !MEM_P (DECL_INCOMING_RTL (decl))
15109                /* Not passed by invisible reference.  */
15110                && (!REG_P (XEXP (rtl, 0))
15111                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15112                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15113 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15114                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15115 #endif
15116                      )
15117                /* Big endian correction check.  */
15118                && BYTES_BIG_ENDIAN
15119                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15120                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15121                    < UNITS_PER_WORD))
15122         {
15123           enum machine_mode addr_mode = get_address_mode (rtl);
15124           int offset = (UNITS_PER_WORD
15125                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15126
15127           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15128                              plus_constant (addr_mode, XEXP (rtl, 0), offset));
15129         }
15130     }
15131   else if (TREE_CODE (decl) == VAR_DECL
15132            && rtl
15133            && MEM_P (rtl)
15134            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15135            && BYTES_BIG_ENDIAN)
15136     {
15137       enum machine_mode addr_mode = get_address_mode (rtl);
15138       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15139       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15140
15141       /* If a variable is declared "register" yet is smaller than
15142          a register, then if we store the variable to memory, it
15143          looks like we're storing a register-sized value, when in
15144          fact we are not.  We need to adjust the offset of the
15145          storage location to reflect the actual value's bytes,
15146          else gdb will not be able to display it.  */
15147       if (rsize > dsize)
15148         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15149                            plus_constant (addr_mode, XEXP (rtl, 0),
15150                                           rsize - dsize));
15151     }
15152
15153   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15154      and will have been substituted directly into all expressions that use it.
15155      C does not have such a concept, but C++ and other languages do.  */
15156   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15157     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15158
15159   if (rtl)
15160     rtl = targetm.delegitimize_address (rtl);
15161
15162   /* If we don't look past the constant pool, we risk emitting a
15163      reference to a constant pool entry that isn't referenced from
15164      code, and thus is not emitted.  */
15165   if (rtl)
15166     rtl = avoid_constant_pool_reference (rtl);
15167
15168   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15169      in the current CU, resolve_addr will remove the expression referencing
15170      it.  */
15171   if (rtl == NULL_RTX
15172       && TREE_CODE (decl) == VAR_DECL
15173       && !DECL_EXTERNAL (decl)
15174       && TREE_STATIC (decl)
15175       && DECL_NAME (decl)
15176       && !DECL_HARD_REGISTER (decl)
15177       && DECL_MODE (decl) != VOIDmode)
15178     {
15179       rtl = make_decl_rtl_for_debug (decl);
15180       if (!MEM_P (rtl)
15181           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15182           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15183         rtl = NULL_RTX;
15184     }
15185
15186   return rtl;
15187 }
15188
15189 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15190    returned.  If so, the decl for the COMMON block is returned, and the
15191    value is the offset into the common block for the symbol.  */
15192
15193 static tree
15194 fortran_common (tree decl, HOST_WIDE_INT *value)
15195 {
15196   tree val_expr, cvar;
15197   enum machine_mode mode;
15198   HOST_WIDE_INT bitsize, bitpos;
15199   tree offset;
15200   int unsignedp, volatilep = 0;
15201
15202   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15203      it does not have a value (the offset into the common area), or if it
15204      is thread local (as opposed to global) then it isn't common, and shouldn't
15205      be handled as such.  */
15206   if (TREE_CODE (decl) != VAR_DECL
15207       || !TREE_STATIC (decl)
15208       || !DECL_HAS_VALUE_EXPR_P (decl)
15209       || !is_fortran ())
15210     return NULL_TREE;
15211
15212   val_expr = DECL_VALUE_EXPR (decl);
15213   if (TREE_CODE (val_expr) != COMPONENT_REF)
15214     return NULL_TREE;
15215
15216   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15217                               &mode, &unsignedp, &volatilep, true);
15218
15219   if (cvar == NULL_TREE
15220       || TREE_CODE (cvar) != VAR_DECL
15221       || DECL_ARTIFICIAL (cvar)
15222       || !TREE_PUBLIC (cvar))
15223     return NULL_TREE;
15224
15225   *value = 0;
15226   if (offset != NULL)
15227     {
15228       if (!host_integerp (offset, 0))
15229         return NULL_TREE;
15230       *value = tree_low_cst (offset, 0);
15231     }
15232   if (bitpos != 0)
15233     *value += bitpos / BITS_PER_UNIT;
15234
15235   return cvar;
15236 }
15237
15238 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15239    data attribute for a variable or a parameter.  We generate the
15240    DW_AT_const_value attribute only in those cases where the given variable
15241    or parameter does not have a true "location" either in memory or in a
15242    register.  This can happen (for example) when a constant is passed as an
15243    actual argument in a call to an inline function.  (It's possible that
15244    these things can crop up in other ways also.)  Note that one type of
15245    constant value which can be passed into an inlined function is a constant
15246    pointer.  This can happen for example if an actual argument in an inlined
15247    function call evaluates to a compile-time constant address.
15248
15249    CACHE_P is true if it is worth caching the location list for DECL,
15250    so that future calls can reuse it rather than regenerate it from scratch.
15251    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15252    since we will need to refer to them each time the function is inlined.  */
15253
15254 static bool
15255 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15256                                        enum dwarf_attribute attr)
15257 {
15258   rtx rtl;
15259   dw_loc_list_ref list;
15260   var_loc_list *loc_list;
15261   cached_dw_loc_list *cache;
15262   void **slot;
15263
15264   if (TREE_CODE (decl) == ERROR_MARK)
15265     return false;
15266
15267   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15268               || TREE_CODE (decl) == RESULT_DECL);
15269
15270   /* Try to get some constant RTL for this decl, and use that as the value of
15271      the location.  */
15272
15273   rtl = rtl_for_decl_location (decl);
15274   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15275       && add_const_value_attribute (die, rtl))
15276     return true;
15277
15278   /* See if we have single element location list that is equivalent to
15279      a constant value.  That way we are better to use add_const_value_attribute
15280      rather than expanding constant value equivalent.  */
15281   loc_list = lookup_decl_loc (decl);
15282   if (loc_list
15283       && loc_list->first
15284       && loc_list->first->next == NULL
15285       && NOTE_P (loc_list->first->loc)
15286       && NOTE_VAR_LOCATION (loc_list->first->loc)
15287       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15288     {
15289       struct var_loc_node *node;
15290
15291       node = loc_list->first;
15292       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15293       if (GET_CODE (rtl) == EXPR_LIST)
15294         rtl = XEXP (rtl, 0);
15295       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15296           && add_const_value_attribute (die, rtl))
15297          return true;
15298     }
15299   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15300      list several times.  See if we've already cached the contents.  */
15301   list = NULL;
15302   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15303     cache_p = false;
15304   if (cache_p)
15305     {
15306       cache = (cached_dw_loc_list *)
15307         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15308       if (cache)
15309         list = cache->loc_list;
15310     }
15311   if (list == NULL)
15312     {
15313       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15314       /* It is usually worth caching this result if the decl is from
15315          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
15316       if (cache_p && list && list->dw_loc_next)
15317         {
15318           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15319                                            DECL_UID (decl), INSERT);
15320           cache = ggc_alloc_cleared_cached_dw_loc_list ();
15321           cache->decl_id = DECL_UID (decl);
15322           cache->loc_list = list;
15323           *slot = cache;
15324         }
15325     }
15326   if (list)
15327     {
15328       add_AT_location_description (die, attr, list);
15329       return true;
15330     }
15331   /* None of that worked, so it must not really have a location;
15332      try adding a constant value attribute from the DECL_INITIAL.  */
15333   return tree_add_const_value_attribute_for_decl (die, decl);
15334 }
15335
15336 /* Add VARIABLE and DIE into deferred locations list.  */
15337
15338 static void
15339 defer_location (tree variable, dw_die_ref die)
15340 {
15341   deferred_locations entry;
15342   entry.variable = variable;
15343   entry.die = die;
15344   vec_safe_push (deferred_locations_list, entry);
15345 }
15346
15347 /* Helper function for tree_add_const_value_attribute.  Natively encode
15348    initializer INIT into an array.  Return true if successful.  */
15349
15350 static bool
15351 native_encode_initializer (tree init, unsigned char *array, int size)
15352 {
15353   tree type;
15354
15355   if (init == NULL_TREE)
15356     return false;
15357
15358   STRIP_NOPS (init);
15359   switch (TREE_CODE (init))
15360     {
15361     case STRING_CST:
15362       type = TREE_TYPE (init);
15363       if (TREE_CODE (type) == ARRAY_TYPE)
15364         {
15365           tree enttype = TREE_TYPE (type);
15366           enum machine_mode mode = TYPE_MODE (enttype);
15367
15368           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15369             return false;
15370           if (int_size_in_bytes (type) != size)
15371             return false;
15372           if (size > TREE_STRING_LENGTH (init))
15373             {
15374               memcpy (array, TREE_STRING_POINTER (init),
15375                       TREE_STRING_LENGTH (init));
15376               memset (array + TREE_STRING_LENGTH (init),
15377                       '\0', size - TREE_STRING_LENGTH (init));
15378             }
15379           else
15380             memcpy (array, TREE_STRING_POINTER (init), size);
15381           return true;
15382         }
15383       return false;
15384     case CONSTRUCTOR:
15385       type = TREE_TYPE (init);
15386       if (int_size_in_bytes (type) != size)
15387         return false;
15388       if (TREE_CODE (type) == ARRAY_TYPE)
15389         {
15390           HOST_WIDE_INT min_index;
15391           unsigned HOST_WIDE_INT cnt;
15392           int curpos = 0, fieldsize;
15393           constructor_elt *ce;
15394
15395           if (TYPE_DOMAIN (type) == NULL_TREE
15396               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15397             return false;
15398
15399           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15400           if (fieldsize <= 0)
15401             return false;
15402
15403           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15404           memset (array, '\0', size);
15405           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15406             {
15407               tree val = ce->value;
15408               tree index = ce->index;
15409               int pos = curpos;
15410               if (index && TREE_CODE (index) == RANGE_EXPR)
15411                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15412                       * fieldsize;
15413               else if (index)
15414                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15415
15416               if (val)
15417                 {
15418                   STRIP_NOPS (val);
15419                   if (!native_encode_initializer (val, array + pos, fieldsize))
15420                     return false;
15421                 }
15422               curpos = pos + fieldsize;
15423               if (index && TREE_CODE (index) == RANGE_EXPR)
15424                 {
15425                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15426                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15427                   while (count-- > 0)
15428                     {
15429                       if (val)
15430                         memcpy (array + curpos, array + pos, fieldsize);
15431                       curpos += fieldsize;
15432                     }
15433                 }
15434               gcc_assert (curpos <= size);
15435             }
15436           return true;
15437         }
15438       else if (TREE_CODE (type) == RECORD_TYPE
15439                || TREE_CODE (type) == UNION_TYPE)
15440         {
15441           tree field = NULL_TREE;
15442           unsigned HOST_WIDE_INT cnt;
15443           constructor_elt *ce;
15444
15445           if (int_size_in_bytes (type) != size)
15446             return false;
15447
15448           if (TREE_CODE (type) == RECORD_TYPE)
15449             field = TYPE_FIELDS (type);
15450
15451           FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (init), cnt, ce)
15452             {
15453               tree val = ce->value;
15454               int pos, fieldsize;
15455
15456               if (ce->index != 0)
15457                 field = ce->index;
15458
15459               if (val)
15460                 STRIP_NOPS (val);
15461
15462               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15463                 return false;
15464
15465               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15466                   && TYPE_DOMAIN (TREE_TYPE (field))
15467                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15468                 return false;
15469               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15470                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15471                 return false;
15472               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15473               pos = int_byte_position (field);
15474               gcc_assert (pos + fieldsize <= size);
15475               if (val
15476                   && !native_encode_initializer (val, array + pos, fieldsize))
15477                 return false;
15478             }
15479           return true;
15480         }
15481       return false;
15482     case VIEW_CONVERT_EXPR:
15483     case NON_LVALUE_EXPR:
15484       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15485     default:
15486       return native_encode_expr (init, array, size) == size;
15487     }
15488 }
15489
15490 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15491    attribute is the const value T.  */
15492
15493 static bool
15494 tree_add_const_value_attribute (dw_die_ref die, tree t)
15495 {
15496   tree init;
15497   tree type = TREE_TYPE (t);
15498   rtx rtl;
15499
15500   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15501     return false;
15502
15503   init = t;
15504   gcc_assert (!DECL_P (init));
15505
15506   rtl = rtl_for_decl_init (init, type);
15507   if (rtl)
15508     return add_const_value_attribute (die, rtl);
15509   /* If the host and target are sane, try harder.  */
15510   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15511            && initializer_constant_valid_p (init, type))
15512     {
15513       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15514       if (size > 0 && (int) size == size)
15515         {
15516           unsigned char *array = (unsigned char *)
15517             ggc_alloc_cleared_atomic (size);
15518
15519           if (native_encode_initializer (init, array, size))
15520             {
15521               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15522               return true;
15523             }
15524         }
15525     }
15526   return false;
15527 }
15528
15529 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15530    attribute is the const value of T, where T is an integral constant
15531    variable with static storage duration
15532    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15533
15534 static bool
15535 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15536 {
15537
15538   if (!decl
15539       || (TREE_CODE (decl) != VAR_DECL
15540           && TREE_CODE (decl) != CONST_DECL)
15541       || (TREE_CODE (decl) == VAR_DECL
15542           && !TREE_STATIC (decl)))
15543     return false;
15544
15545     if (TREE_READONLY (decl)
15546         && ! TREE_THIS_VOLATILE (decl)
15547         && DECL_INITIAL (decl))
15548       /* OK */;
15549     else
15550       return false;
15551
15552   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15553   if (get_AT (var_die, DW_AT_const_value))
15554     return false;
15555
15556   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15557 }
15558
15559 /* Convert the CFI instructions for the current function into a
15560    location list.  This is used for DW_AT_frame_base when we targeting
15561    a dwarf2 consumer that does not support the dwarf3
15562    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15563    expressions.  */
15564
15565 static dw_loc_list_ref
15566 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15567 {
15568   int ix;
15569   dw_fde_ref fde;
15570   dw_loc_list_ref list, *list_tail;
15571   dw_cfi_ref cfi;
15572   dw_cfa_location last_cfa, next_cfa;
15573   const char *start_label, *last_label, *section;
15574   dw_cfa_location remember;
15575
15576   fde = cfun->fde;
15577   gcc_assert (fde != NULL);
15578
15579   section = secname_for_decl (current_function_decl);
15580   list_tail = &list;
15581   list = NULL;
15582
15583   memset (&next_cfa, 0, sizeof (next_cfa));
15584   next_cfa.reg = INVALID_REGNUM;
15585   remember = next_cfa;
15586
15587   start_label = fde->dw_fde_begin;
15588
15589   /* ??? Bald assumption that the CIE opcode list does not contain
15590      advance opcodes.  */
15591   FOR_EACH_VEC_ELT (*cie_cfi_vec, ix, cfi)
15592     lookup_cfa_1 (cfi, &next_cfa, &remember);
15593
15594   last_cfa = next_cfa;
15595   last_label = start_label;
15596
15597   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15598     {
15599       /* If the first partition contained no CFI adjustments, the
15600          CIE opcodes apply to the whole first partition.  */
15601       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15602                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15603       list_tail =&(*list_tail)->dw_loc_next;
15604       start_label = last_label = fde->dw_fde_second_begin;
15605     }
15606
15607   FOR_EACH_VEC_SAFE_ELT (fde->dw_fde_cfi, ix, cfi)
15608     {
15609       switch (cfi->dw_cfi_opc)
15610         {
15611         case DW_CFA_set_loc:
15612         case DW_CFA_advance_loc1:
15613         case DW_CFA_advance_loc2:
15614         case DW_CFA_advance_loc4:
15615           if (!cfa_equal_p (&last_cfa, &next_cfa))
15616             {
15617               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15618                                          start_label, last_label, section);
15619
15620               list_tail = &(*list_tail)->dw_loc_next;
15621               last_cfa = next_cfa;
15622               start_label = last_label;
15623             }
15624           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15625           break;
15626
15627         case DW_CFA_advance_loc:
15628           /* The encoding is complex enough that we should never emit this.  */
15629           gcc_unreachable ();
15630
15631         default:
15632           lookup_cfa_1 (cfi, &next_cfa, &remember);
15633           break;
15634         }
15635       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15636         {
15637           if (!cfa_equal_p (&last_cfa, &next_cfa))
15638             {
15639               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15640                                          start_label, last_label, section);
15641
15642               list_tail = &(*list_tail)->dw_loc_next;
15643               last_cfa = next_cfa;
15644               start_label = last_label;
15645             }
15646           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15647                                      start_label, fde->dw_fde_end, section);
15648           list_tail = &(*list_tail)->dw_loc_next;
15649           start_label = last_label = fde->dw_fde_second_begin;
15650         }
15651     }
15652
15653   if (!cfa_equal_p (&last_cfa, &next_cfa))
15654     {
15655       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15656                                  start_label, last_label, section);
15657       list_tail = &(*list_tail)->dw_loc_next;
15658       start_label = last_label;
15659     }
15660
15661   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15662                              start_label,
15663                              fde->dw_fde_second_begin
15664                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15665                              section);
15666
15667   if (list && list->dw_loc_next)
15668     gen_llsym (list);
15669
15670   return list;
15671 }
15672
15673 /* Compute a displacement from the "steady-state frame pointer" to the
15674    frame base (often the same as the CFA), and store it in
15675    frame_pointer_fb_offset.  OFFSET is added to the displacement
15676    before the latter is negated.  */
15677
15678 static void
15679 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15680 {
15681   rtx reg, elim;
15682
15683 #ifdef FRAME_POINTER_CFA_OFFSET
15684   reg = frame_pointer_rtx;
15685   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15686 #else
15687   reg = arg_pointer_rtx;
15688   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15689 #endif
15690
15691   elim = (ira_use_lra_p
15692           ? lra_eliminate_regs (reg, VOIDmode, NULL_RTX)
15693           : eliminate_regs (reg, VOIDmode, NULL_RTX));
15694   if (GET_CODE (elim) == PLUS)
15695     {
15696       offset += INTVAL (XEXP (elim, 1));
15697       elim = XEXP (elim, 0);
15698     }
15699
15700   frame_pointer_fb_offset = -offset;
15701
15702   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15703      in which to eliminate.  This is because it's stack pointer isn't 
15704      directly accessible as a register within the ISA.  To work around
15705      this, assume that while we cannot provide a proper value for
15706      frame_pointer_fb_offset, we won't need one either.  */
15707   frame_pointer_fb_offset_valid
15708     = ((SUPPORTS_STACK_ALIGNMENT
15709         && (elim == hard_frame_pointer_rtx
15710             || elim == stack_pointer_rtx))
15711        || elim == (frame_pointer_needed
15712                    ? hard_frame_pointer_rtx
15713                    : stack_pointer_rtx));
15714 }
15715
15716 /* Generate a DW_AT_name attribute given some string value to be included as
15717    the value of the attribute.  */
15718
15719 static void
15720 add_name_attribute (dw_die_ref die, const char *name_string)
15721 {
15722   if (name_string != NULL && *name_string != 0)
15723     {
15724       if (demangle_name_func)
15725         name_string = (*demangle_name_func) (name_string);
15726
15727       add_AT_string (die, DW_AT_name, name_string);
15728     }
15729 }
15730
15731 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15732    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15733    of TYPE accordingly.
15734
15735    ??? This is a temporary measure until after we're able to generate
15736    regular DWARF for the complex Ada type system.  */
15737
15738 static void 
15739 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15740                                      dw_die_ref context_die)
15741 {
15742   tree dtype;
15743   dw_die_ref dtype_die;
15744
15745   if (!lang_hooks.types.descriptive_type)
15746     return;
15747
15748   dtype = lang_hooks.types.descriptive_type (type);
15749   if (!dtype)
15750     return;
15751
15752   dtype_die = lookup_type_die (dtype);
15753   if (!dtype_die)
15754     {
15755       gen_type_die (dtype, context_die);
15756       dtype_die = lookup_type_die (dtype);
15757       gcc_assert (dtype_die);
15758     }
15759
15760   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15761 }
15762
15763 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15764
15765 static void
15766 add_comp_dir_attribute (dw_die_ref die)
15767 {
15768   const char *wd = get_src_pwd ();
15769   char *wd1;
15770
15771   if (wd == NULL)
15772     return;
15773
15774   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15775     {
15776       int wdlen;
15777
15778       wdlen = strlen (wd);
15779       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15780       strcpy (wd1, wd);
15781       wd1 [wdlen] = DIR_SEPARATOR;
15782       wd1 [wdlen + 1] = 0;
15783       wd = wd1;
15784     }
15785
15786     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15787 }
15788
15789 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15790    default.  */
15791
15792 static int
15793 lower_bound_default (void)
15794 {
15795   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15796     {
15797     case DW_LANG_C:
15798     case DW_LANG_C89:
15799     case DW_LANG_C99:
15800     case DW_LANG_C_plus_plus:
15801     case DW_LANG_ObjC:
15802     case DW_LANG_ObjC_plus_plus:
15803     case DW_LANG_Java:
15804       return 0;
15805     case DW_LANG_Fortran77:
15806     case DW_LANG_Fortran90:
15807     case DW_LANG_Fortran95:
15808       return 1;
15809     case DW_LANG_UPC:
15810     case DW_LANG_D:
15811     case DW_LANG_Python:
15812       return dwarf_version >= 4 ? 0 : -1;
15813     case DW_LANG_Ada95:
15814     case DW_LANG_Ada83:
15815     case DW_LANG_Cobol74:
15816     case DW_LANG_Cobol85:
15817     case DW_LANG_Pascal83:
15818     case DW_LANG_Modula2:
15819     case DW_LANG_PLI:
15820       return dwarf_version >= 4 ? 1 : -1;
15821     default:
15822       return -1;
15823     }
15824 }
15825
15826 /* Given a tree node describing an array bound (either lower or upper) output
15827    a representation for that bound.  */
15828
15829 static void
15830 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15831 {
15832   switch (TREE_CODE (bound))
15833     {
15834     case ERROR_MARK:
15835       return;
15836
15837     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15838     case INTEGER_CST:
15839       {
15840         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15841         int dflt;
15842
15843         /* Use the default if possible.  */
15844         if (bound_attr == DW_AT_lower_bound
15845             && host_integerp (bound, 0)
15846             && (dflt = lower_bound_default ()) != -1
15847             && tree_low_cst (bound, 0) == dflt)
15848           ;
15849
15850         /* Otherwise represent the bound as an unsigned value with the
15851            precision of its type.  The precision and signedness of the
15852            type will be necessary to re-interpret it unambiguously.  */
15853         else if (prec < HOST_BITS_PER_WIDE_INT)
15854           {
15855             unsigned HOST_WIDE_INT mask
15856               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15857             add_AT_unsigned (subrange_die, bound_attr,
15858                              TREE_INT_CST_LOW (bound) & mask);
15859           }
15860         else if (prec == HOST_BITS_PER_WIDE_INT
15861                  || TREE_INT_CST_HIGH (bound) == 0)
15862           add_AT_unsigned (subrange_die, bound_attr,
15863                            TREE_INT_CST_LOW (bound));
15864         else
15865           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15866                          TREE_INT_CST_LOW (bound));
15867       }
15868       break;
15869
15870     CASE_CONVERT:
15871     case VIEW_CONVERT_EXPR:
15872       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15873       break;
15874
15875     case SAVE_EXPR:
15876       break;
15877
15878     case VAR_DECL:
15879     case PARM_DECL:
15880     case RESULT_DECL:
15881       {
15882         dw_die_ref decl_die = lookup_decl_die (bound);
15883
15884         /* ??? Can this happen, or should the variable have been bound
15885            first?  Probably it can, since I imagine that we try to create
15886            the types of parameters in the order in which they exist in
15887            the list, and won't have created a forward reference to a
15888            later parameter.  */
15889         if (decl_die != NULL)
15890           {
15891             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15892             break;
15893           }
15894       }
15895       /* FALLTHRU */
15896
15897     default:
15898       {
15899         /* Otherwise try to create a stack operation procedure to
15900            evaluate the value of the array bound.  */
15901
15902         dw_die_ref ctx, decl_die;
15903         dw_loc_list_ref list;
15904
15905         list = loc_list_from_tree (bound, 2);
15906         if (list == NULL || single_element_loc_list_p (list))
15907           {
15908             /* If DW_AT_*bound is not a reference nor constant, it is
15909                a DWARF expression rather than location description.
15910                For that loc_list_from_tree (bound, 0) is needed.
15911                If that fails to give a single element list,
15912                fall back to outputting this as a reference anyway.  */
15913             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15914             if (list2 && single_element_loc_list_p (list2))
15915               {
15916                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15917                 break;
15918               }
15919           }
15920         if (list == NULL)
15921           break;
15922
15923         if (current_function_decl == 0)
15924           ctx = comp_unit_die ();
15925         else
15926           ctx = lookup_decl_die (current_function_decl);
15927
15928         decl_die = new_die (DW_TAG_variable, ctx, bound);
15929         add_AT_flag (decl_die, DW_AT_artificial, 1);
15930         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15931         add_AT_location_description (decl_die, DW_AT_location, list);
15932         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15933         break;
15934       }
15935     }
15936 }
15937
15938 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15939    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15940    Note that the block of subscript information for an array type also
15941    includes information about the element type of the given array type.  */
15942
15943 static void
15944 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15945 {
15946   unsigned dimension_number;
15947   tree lower, upper;
15948   dw_die_ref subrange_die;
15949
15950   for (dimension_number = 0;
15951        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15952        type = TREE_TYPE (type), dimension_number++)
15953     {
15954       tree domain = TYPE_DOMAIN (type);
15955
15956       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15957         break;
15958
15959       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15960          and (in GNU C only) variable bounds.  Handle all three forms
15961          here.  */
15962       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15963       if (domain)
15964         {
15965           /* We have an array type with specified bounds.  */
15966           lower = TYPE_MIN_VALUE (domain);
15967           upper = TYPE_MAX_VALUE (domain);
15968
15969           /* Define the index type.  */
15970           if (TREE_TYPE (domain))
15971             {
15972               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15973                  TREE_TYPE field.  We can't emit debug info for this
15974                  because it is an unnamed integral type.  */
15975               if (TREE_CODE (domain) == INTEGER_TYPE
15976                   && TYPE_NAME (domain) == NULL_TREE
15977                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15978                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15979                 ;
15980               else
15981                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15982                                     type_die);
15983             }
15984
15985           /* ??? If upper is NULL, the array has unspecified length,
15986              but it does have a lower bound.  This happens with Fortran
15987                dimension arr(N:*)
15988              Since the debugger is definitely going to need to know N
15989              to produce useful results, go ahead and output the lower
15990              bound solo, and hope the debugger can cope.  */
15991
15992           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15993           if (upper)
15994             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15995         }
15996
15997       /* Otherwise we have an array type with an unspecified length.  The
15998          DWARF-2 spec does not say how to handle this; let's just leave out the
15999          bounds.  */
16000     }
16001 }
16002
16003 static void
16004 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16005 {
16006   dw_die_ref decl_die;
16007   unsigned size;
16008
16009   switch (TREE_CODE (tree_node))
16010     {
16011     case ERROR_MARK:
16012       size = 0;
16013       break;
16014     case ENUMERAL_TYPE:
16015     case RECORD_TYPE:
16016     case UNION_TYPE:
16017     case QUAL_UNION_TYPE:
16018       if (TREE_CODE (TYPE_SIZE_UNIT (tree_node)) == VAR_DECL
16019           && (decl_die = lookup_decl_die (TYPE_SIZE_UNIT (tree_node))))
16020         {
16021           add_AT_die_ref (die, DW_AT_byte_size, decl_die);
16022           return;
16023         }
16024       size = int_size_in_bytes (tree_node);
16025       break;
16026     case FIELD_DECL:
16027       /* For a data member of a struct or union, the DW_AT_byte_size is
16028          generally given as the number of bytes normally allocated for an
16029          object of the *declared* type of the member itself.  This is true
16030          even for bit-fields.  */
16031       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16032       break;
16033     default:
16034       gcc_unreachable ();
16035     }
16036
16037   /* Note that `size' might be -1 when we get to this point.  If it is, that
16038      indicates that the byte size of the entity in question is variable.  We
16039      have no good way of expressing this fact in Dwarf at the present time,
16040      so just let the -1 pass on through.  */
16041   add_AT_unsigned (die, DW_AT_byte_size, size);
16042 }
16043
16044 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16045    which specifies the distance in bits from the highest order bit of the
16046    "containing object" for the bit-field to the highest order bit of the
16047    bit-field itself.
16048
16049    For any given bit-field, the "containing object" is a hypothetical object
16050    (of some integral or enum type) within which the given bit-field lives.  The
16051    type of this hypothetical "containing object" is always the same as the
16052    declared type of the individual bit-field itself.  The determination of the
16053    exact location of the "containing object" for a bit-field is rather
16054    complicated.  It's handled by the `field_byte_offset' function (above).
16055
16056    Note that it is the size (in bytes) of the hypothetical "containing object"
16057    which will be given in the DW_AT_byte_size attribute for this bit-field.
16058    (See `byte_size_attribute' above).  */
16059
16060 static inline void
16061 add_bit_offset_attribute (dw_die_ref die, tree decl)
16062 {
16063   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16064   tree type = DECL_BIT_FIELD_TYPE (decl);
16065   HOST_WIDE_INT bitpos_int;
16066   HOST_WIDE_INT highest_order_object_bit_offset;
16067   HOST_WIDE_INT highest_order_field_bit_offset;
16068   HOST_WIDE_INT bit_offset;
16069
16070   /* Must be a field and a bit field.  */
16071   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16072
16073   /* We can't yet handle bit-fields whose offsets are variable, so if we
16074      encounter such things, just return without generating any attribute
16075      whatsoever.  Likewise for variable or too large size.  */
16076   if (! host_integerp (bit_position (decl), 0)
16077       || ! host_integerp (DECL_SIZE (decl), 1))
16078     return;
16079
16080   bitpos_int = int_bit_position (decl);
16081
16082   /* Note that the bit offset is always the distance (in bits) from the
16083      highest-order bit of the "containing object" to the highest-order bit of
16084      the bit-field itself.  Since the "high-order end" of any object or field
16085      is different on big-endian and little-endian machines, the computation
16086      below must take account of these differences.  */
16087   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16088   highest_order_field_bit_offset = bitpos_int;
16089
16090   if (! BYTES_BIG_ENDIAN)
16091     {
16092       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16093       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16094     }
16095
16096   bit_offset
16097     = (! BYTES_BIG_ENDIAN
16098        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16099        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16100
16101   if (bit_offset < 0)
16102     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16103   else
16104     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16105 }
16106
16107 /* For a FIELD_DECL node which represents a bit field, output an attribute
16108    which specifies the length in bits of the given field.  */
16109
16110 static inline void
16111 add_bit_size_attribute (dw_die_ref die, tree decl)
16112 {
16113   /* Must be a field and a bit field.  */
16114   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16115               && DECL_BIT_FIELD_TYPE (decl));
16116
16117   if (host_integerp (DECL_SIZE (decl), 1))
16118     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16119 }
16120
16121 /* If the compiled language is ANSI C, then add a 'prototyped'
16122    attribute, if arg types are given for the parameters of a function.  */
16123
16124 static inline void
16125 add_prototyped_attribute (dw_die_ref die, tree func_type)
16126 {
16127   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16128       && prototype_p (func_type))
16129     add_AT_flag (die, DW_AT_prototyped, 1);
16130 }
16131
16132 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16133    by looking in either the type declaration or object declaration
16134    equate table.  */
16135
16136 static inline dw_die_ref
16137 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16138 {
16139   dw_die_ref origin_die = NULL;
16140
16141   if (TREE_CODE (origin) != FUNCTION_DECL)
16142     {
16143       /* We may have gotten separated from the block for the inlined
16144          function, if we're in an exception handler or some such; make
16145          sure that the abstract function has been written out.
16146
16147          Doing this for nested functions is wrong, however; functions are
16148          distinct units, and our context might not even be inline.  */
16149       tree fn = origin;
16150
16151       if (TYPE_P (fn))
16152         fn = TYPE_STUB_DECL (fn);
16153
16154       fn = decl_function_context (fn);
16155       if (fn)
16156         dwarf2out_abstract_function (fn);
16157     }
16158
16159   if (DECL_P (origin))
16160     origin_die = lookup_decl_die (origin);
16161   else if (TYPE_P (origin))
16162     origin_die = lookup_type_die (origin);
16163
16164   /* XXX: Functions that are never lowered don't always have correct block
16165      trees (in the case of java, they simply have no block tree, in some other
16166      languages).  For these functions, there is nothing we can really do to
16167      output correct debug info for inlined functions in all cases.  Rather
16168      than die, we'll just produce deficient debug info now, in that we will
16169      have variables without a proper abstract origin.  In the future, when all
16170      functions are lowered, we should re-add a gcc_assert (origin_die)
16171      here.  */
16172
16173   if (origin_die)
16174     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16175   return origin_die;
16176 }
16177
16178 /* We do not currently support the pure_virtual attribute.  */
16179
16180 static inline void
16181 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16182 {
16183   if (DECL_VINDEX (func_decl))
16184     {
16185       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16186
16187       if (host_integerp (DECL_VINDEX (func_decl), 0))
16188         add_AT_loc (die, DW_AT_vtable_elem_location,
16189                     new_loc_descr (DW_OP_constu,
16190                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
16191                                    0));
16192
16193       /* GNU extension: Record what type this method came from originally.  */
16194       if (debug_info_level > DINFO_LEVEL_TERSE
16195           && DECL_CONTEXT (func_decl))
16196         add_AT_die_ref (die, DW_AT_containing_type,
16197                         lookup_type_die (DECL_CONTEXT (func_decl)));
16198     }
16199 }
16200 \f
16201 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16202    given decl.  This used to be a vendor extension until after DWARF 4
16203    standardized it.  */
16204
16205 static void
16206 add_linkage_attr (dw_die_ref die, tree decl)
16207 {
16208   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16209
16210   /* Mimic what assemble_name_raw does with a leading '*'.  */
16211   if (name[0] == '*')
16212     name = &name[1];
16213
16214   if (dwarf_version >= 4)
16215     add_AT_string (die, DW_AT_linkage_name, name);
16216   else
16217     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16218 }
16219
16220 /* Add source coordinate attributes for the given decl.  */
16221
16222 static void
16223 add_src_coords_attributes (dw_die_ref die, tree decl)
16224 {
16225   expanded_location s;
16226
16227   if (LOCATION_LOCUS (DECL_SOURCE_LOCATION (decl)) == UNKNOWN_LOCATION)
16228     return;
16229   s = expand_location (DECL_SOURCE_LOCATION (decl));
16230   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16231   add_AT_unsigned (die, DW_AT_decl_line, s.line);
16232 }
16233
16234 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
16235
16236 static void
16237 add_linkage_name (dw_die_ref die, tree decl)
16238 {
16239   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16240        && TREE_PUBLIC (decl)
16241        && !DECL_ABSTRACT (decl)
16242        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16243        && die->die_tag != DW_TAG_member)
16244     {
16245       /* Defer until we have an assembler name set.  */
16246       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16247         {
16248           limbo_die_node *asm_name;
16249
16250           asm_name = ggc_alloc_cleared_limbo_die_node ();
16251           asm_name->die = die;
16252           asm_name->created_for = decl;
16253           asm_name->next = deferred_asm_name;
16254           deferred_asm_name = asm_name;
16255         }
16256       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16257         add_linkage_attr (die, decl);
16258     }
16259 }
16260
16261 /* Add a DW_AT_name attribute and source coordinate attribute for the
16262    given decl, but only if it actually has a name.  */
16263
16264 static void
16265 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16266 {
16267   tree decl_name;
16268
16269   decl_name = DECL_NAME (decl);
16270   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16271     {
16272       const char *name = dwarf2_name (decl, 0);
16273       if (name)
16274         add_name_attribute (die, name);
16275       if (! DECL_ARTIFICIAL (decl))
16276         add_src_coords_attributes (die, decl);
16277
16278       add_linkage_name (die, decl);
16279     }
16280
16281 #ifdef VMS_DEBUGGING_INFO
16282   /* Get the function's name, as described by its RTL.  This may be different
16283      from the DECL_NAME name used in the source file.  */
16284   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16285     {
16286       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16287                   XEXP (DECL_RTL (decl), 0), false);
16288       vec_safe_push (used_rtx_array, XEXP (DECL_RTL (decl), 0));
16289     }
16290 #endif /* VMS_DEBUGGING_INFO */
16291 }
16292
16293 #ifdef VMS_DEBUGGING_INFO
16294 /* Output the debug main pointer die for VMS */
16295
16296 void
16297 dwarf2out_vms_debug_main_pointer (void)
16298 {
16299   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16300   dw_die_ref die;
16301
16302   /* Allocate the VMS debug main subprogram die.  */
16303   die = ggc_alloc_cleared_die_node ();
16304   die->die_tag = DW_TAG_subprogram;
16305   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16306   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16307                                current_function_funcdef_no);
16308   add_AT_lbl_id (die, DW_AT_entry_pc, label);
16309
16310   /* Make it the first child of comp_unit_die ().  */
16311   die->die_parent = comp_unit_die ();
16312   if (comp_unit_die ()->die_child)
16313     {
16314       die->die_sib = comp_unit_die ()->die_child->die_sib;
16315       comp_unit_die ()->die_child->die_sib = die;
16316     }
16317   else
16318     {
16319       die->die_sib = die;
16320       comp_unit_die ()->die_child = die;
16321     }
16322 }
16323 #endif /* VMS_DEBUGGING_INFO */
16324
16325 /* Push a new declaration scope.  */
16326
16327 static void
16328 push_decl_scope (tree scope)
16329 {
16330   vec_safe_push (decl_scope_table, scope);
16331 }
16332
16333 /* Pop a declaration scope.  */
16334
16335 static inline void
16336 pop_decl_scope (void)
16337 {
16338   decl_scope_table->pop ();
16339 }
16340
16341 /* walk_tree helper function for uses_local_type, below.  */
16342
16343 static tree
16344 uses_local_type_r (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
16345 {
16346   if (!TYPE_P (*tp))
16347     *walk_subtrees = 0;
16348   else
16349     {
16350       tree name = TYPE_NAME (*tp);
16351       if (name && DECL_P (name) && decl_function_context (name))
16352         return *tp;
16353     }
16354   return NULL_TREE;
16355 }
16356
16357 /* If TYPE involves a function-local type (including a local typedef to a
16358    non-local type), returns that type; otherwise returns NULL_TREE.  */
16359
16360 static tree
16361 uses_local_type (tree type)
16362 {
16363   tree used = walk_tree_without_duplicates (&type, uses_local_type_r, NULL);
16364   return used;
16365 }
16366
16367 /* Return the DIE for the scope that immediately contains this type.
16368    Non-named types that do not involve a function-local type get global
16369    scope.  Named types nested in namespaces or other types get their
16370    containing scope.  All other types (i.e. function-local named types) get
16371    the current active scope.  */
16372
16373 static dw_die_ref
16374 scope_die_for (tree t, dw_die_ref context_die)
16375 {
16376   dw_die_ref scope_die = NULL;
16377   tree containing_scope;
16378
16379   /* Non-types always go in the current scope.  */
16380   gcc_assert (TYPE_P (t));
16381
16382   /* Use the scope of the typedef, rather than the scope of the type
16383      it refers to.  */
16384   if (TYPE_NAME (t) && DECL_P (TYPE_NAME (t)))
16385     containing_scope = DECL_CONTEXT (TYPE_NAME (t));
16386   else
16387     containing_scope = TYPE_CONTEXT (t);
16388
16389   /* Use the containing namespace if there is one.  */
16390   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16391     {
16392       if (context_die == lookup_decl_die (containing_scope))
16393         /* OK */;
16394       else if (debug_info_level > DINFO_LEVEL_TERSE)
16395         context_die = get_context_die (containing_scope);
16396       else
16397         containing_scope = NULL_TREE;
16398     }
16399
16400   /* Ignore function type "scopes" from the C frontend.  They mean that
16401      a tagged type is local to a parmlist of a function declarator, but
16402      that isn't useful to DWARF.  */
16403   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16404     containing_scope = NULL_TREE;
16405
16406   if (SCOPE_FILE_SCOPE_P (containing_scope))
16407     {
16408       /* If T uses a local type keep it local as well, to avoid references
16409          to function-local DIEs from outside the function.  */
16410       if (current_function_decl && uses_local_type (t))
16411         scope_die = context_die;
16412       else
16413         scope_die = comp_unit_die ();
16414     }
16415   else if (TYPE_P (containing_scope))
16416     {
16417       /* For types, we can just look up the appropriate DIE.  */
16418       if (debug_info_level > DINFO_LEVEL_TERSE)
16419         scope_die = get_context_die (containing_scope);
16420       else
16421         {
16422           scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16423           if (scope_die == NULL)
16424             scope_die = comp_unit_die ();
16425         }
16426     }
16427   else
16428     scope_die = context_die;
16429
16430   return scope_die;
16431 }
16432
16433 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16434
16435 static inline int
16436 local_scope_p (dw_die_ref context_die)
16437 {
16438   for (; context_die; context_die = context_die->die_parent)
16439     if (context_die->die_tag == DW_TAG_inlined_subroutine
16440         || context_die->die_tag == DW_TAG_subprogram)
16441       return 1;
16442
16443   return 0;
16444 }
16445
16446 /* Returns nonzero if CONTEXT_DIE is a class.  */
16447
16448 static inline int
16449 class_scope_p (dw_die_ref context_die)
16450 {
16451   return (context_die
16452           && (context_die->die_tag == DW_TAG_structure_type
16453               || context_die->die_tag == DW_TAG_class_type
16454               || context_die->die_tag == DW_TAG_interface_type
16455               || context_die->die_tag == DW_TAG_union_type));
16456 }
16457
16458 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16459    whether or not to treat a DIE in this context as a declaration.  */
16460
16461 static inline int
16462 class_or_namespace_scope_p (dw_die_ref context_die)
16463 {
16464   return (class_scope_p (context_die)
16465           || (context_die && context_die->die_tag == DW_TAG_namespace));
16466 }
16467
16468 /* Many forms of DIEs require a "type description" attribute.  This
16469    routine locates the proper "type descriptor" die for the type given
16470    by 'type', and adds a DW_AT_type attribute below the given die.  */
16471
16472 static void
16473 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16474                     int decl_volatile, dw_die_ref context_die)
16475 {
16476   enum tree_code code  = TREE_CODE (type);
16477   dw_die_ref type_die  = NULL;
16478
16479   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16480      or fixed-point type, use the inner type.  This is because we have no
16481      support for unnamed types in base_type_die.  This can happen if this is
16482      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16483   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16484       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16485     type = TREE_TYPE (type), code = TREE_CODE (type);
16486
16487   if (code == ERROR_MARK
16488       /* Handle a special case.  For functions whose return type is void, we
16489          generate *no* type attribute.  (Note that no object may have type
16490          `void', so this only applies to function return types).  */
16491       || code == VOID_TYPE)
16492     return;
16493
16494   type_die = modified_type_die (type,
16495                                 decl_const || TYPE_READONLY (type),
16496                                 decl_volatile || TYPE_VOLATILE (type),
16497                                 context_die);
16498
16499   if (type_die != NULL)
16500     add_AT_die_ref (object_die, DW_AT_type, type_die);
16501 }
16502
16503 /* Given an object die, add the calling convention attribute for the
16504    function call type.  */
16505 static void
16506 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16507 {
16508   enum dwarf_calling_convention value = DW_CC_normal;
16509
16510   value = ((enum dwarf_calling_convention)
16511            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16512
16513   if (is_fortran ()
16514       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16515     {
16516       /* DWARF 2 doesn't provide a way to identify a program's source-level
16517         entry point.  DW_AT_calling_convention attributes are only meant
16518         to describe functions' calling conventions.  However, lacking a
16519         better way to signal the Fortran main program, we used this for 
16520         a long time, following existing custom.  Now, DWARF 4 has 
16521         DW_AT_main_subprogram, which we add below, but some tools still
16522         rely on the old way, which we thus keep.  */
16523       value = DW_CC_program;
16524
16525       if (dwarf_version >= 4 || !dwarf_strict)
16526         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16527     }
16528
16529   /* Only add the attribute if the backend requests it, and
16530      is not DW_CC_normal.  */
16531   if (value && (value != DW_CC_normal))
16532     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16533 }
16534
16535 /* Given a tree pointer to a struct, class, union, or enum type node, return
16536    a pointer to the (string) tag name for the given type, or zero if the type
16537    was declared without a tag.  */
16538
16539 static const char *
16540 type_tag (const_tree type)
16541 {
16542   const char *name = 0;
16543
16544   if (TYPE_NAME (type) != 0)
16545     {
16546       tree t = 0;
16547
16548       /* Find the IDENTIFIER_NODE for the type name.  */
16549       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16550           && !TYPE_NAMELESS (type))
16551         t = TYPE_NAME (type);
16552
16553       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16554          a TYPE_DECL node, regardless of whether or not a `typedef' was
16555          involved.  */
16556       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16557                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16558         {
16559           /* We want to be extra verbose.  Don't call dwarf_name if
16560              DECL_NAME isn't set.  The default hook for decl_printable_name
16561              doesn't like that, and in this context it's correct to return
16562              0, instead of "<anonymous>" or the like.  */
16563           if (DECL_NAME (TYPE_NAME (type))
16564               && !DECL_NAMELESS (TYPE_NAME (type)))
16565             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16566         }
16567
16568       /* Now get the name as a string, or invent one.  */
16569       if (!name && t != 0)
16570         name = IDENTIFIER_POINTER (t);
16571     }
16572
16573   return (name == 0 || *name == '\0') ? 0 : name;
16574 }
16575
16576 /* Return the type associated with a data member, make a special check
16577    for bit field types.  */
16578
16579 static inline tree
16580 member_declared_type (const_tree member)
16581 {
16582   return (DECL_BIT_FIELD_TYPE (member)
16583           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16584 }
16585
16586 /* Get the decl's label, as described by its RTL. This may be different
16587    from the DECL_NAME name used in the source file.  */
16588
16589 #if 0
16590 static const char *
16591 decl_start_label (tree decl)
16592 {
16593   rtx x;
16594   const char *fnname;
16595
16596   x = DECL_RTL (decl);
16597   gcc_assert (MEM_P (x));
16598
16599   x = XEXP (x, 0);
16600   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16601
16602   fnname = XSTR (x, 0);
16603   return fnname;
16604 }
16605 #endif
16606 \f
16607 /* These routines generate the internal representation of the DIE's for
16608    the compilation unit.  Debugging information is collected by walking
16609    the declaration trees passed in from dwarf2out_decl().  */
16610
16611 static void
16612 gen_array_type_die (tree type, dw_die_ref context_die)
16613 {
16614   dw_die_ref scope_die = scope_die_for (type, context_die);
16615   dw_die_ref array_die;
16616
16617   /* GNU compilers represent multidimensional array types as sequences of one
16618      dimensional array types whose element types are themselves array types.
16619      We sometimes squish that down to a single array_type DIE with multiple
16620      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16621      say that we are allowed to do this kind of compression in C, because
16622      there is no difference between an array of arrays and a multidimensional
16623      array.  We don't do this for Ada to remain as close as possible to the
16624      actual representation, which is especially important against the language
16625      flexibilty wrt arrays of variable size.  */
16626
16627   bool collapse_nested_arrays = !is_ada ();
16628   tree element_type;
16629
16630   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16631      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16632   if (TYPE_STRING_FLAG (type)
16633       && TREE_CODE (type) == ARRAY_TYPE
16634       && is_fortran ()
16635       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16636     {
16637       HOST_WIDE_INT size;
16638
16639       array_die = new_die (DW_TAG_string_type, scope_die, type);
16640       add_name_attribute (array_die, type_tag (type));
16641       equate_type_number_to_die (type, array_die);
16642       size = int_size_in_bytes (type);
16643       if (size >= 0)
16644         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16645       else if (TYPE_DOMAIN (type) != NULL_TREE
16646                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16647                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16648         {
16649           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16650           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16651
16652           size = int_size_in_bytes (TREE_TYPE (szdecl));
16653           if (loc && size > 0)
16654             {
16655               add_AT_location_description (array_die, DW_AT_string_length, loc);
16656               if (size != DWARF2_ADDR_SIZE)
16657                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16658             }
16659         }
16660       return;
16661     }
16662
16663   array_die = new_die (DW_TAG_array_type, scope_die, type);
16664   add_name_attribute (array_die, type_tag (type));
16665   equate_type_number_to_die (type, array_die);
16666
16667   if (TREE_CODE (type) == VECTOR_TYPE)
16668     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16669
16670   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16671   if (is_fortran ()
16672       && TREE_CODE (type) == ARRAY_TYPE
16673       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16674       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16675     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16676
16677 #if 0
16678   /* We default the array ordering.  SDB will probably do
16679      the right things even if DW_AT_ordering is not present.  It's not even
16680      an issue until we start to get into multidimensional arrays anyway.  If
16681      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16682      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16683      and when we find out that we need to put these in, we will only do so
16684      for multidimensional arrays.  */
16685   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16686 #endif
16687
16688   if (TREE_CODE (type) == VECTOR_TYPE)
16689     {
16690       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16691       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16692       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16693       add_bound_info (subrange_die, DW_AT_upper_bound,
16694                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16695     }
16696   else
16697     add_subscript_info (array_die, type, collapse_nested_arrays);
16698
16699   /* Add representation of the type of the elements of this array type and
16700      emit the corresponding DIE if we haven't done it already.  */
16701   element_type = TREE_TYPE (type);
16702   if (collapse_nested_arrays)
16703     while (TREE_CODE (element_type) == ARRAY_TYPE)
16704       {
16705         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16706           break;
16707         element_type = TREE_TYPE (element_type);
16708       }
16709
16710   add_type_attribute (array_die, element_type, 0, 0, context_die);
16711
16712   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16713   if (TYPE_ARTIFICIAL (type))
16714     add_AT_flag (array_die, DW_AT_artificial, 1);
16715
16716   if (get_AT (array_die, DW_AT_name))
16717     add_pubtype (type, array_die);
16718 }
16719
16720 static dw_loc_descr_ref
16721 descr_info_loc (tree val, tree base_decl)
16722 {
16723   HOST_WIDE_INT size;
16724   dw_loc_descr_ref loc, loc2;
16725   enum dwarf_location_atom op;
16726
16727   if (val == base_decl)
16728     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16729
16730   switch (TREE_CODE (val))
16731     {
16732     CASE_CONVERT:
16733       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16734     case VAR_DECL:
16735       return loc_descriptor_from_tree (val, 0);
16736     case INTEGER_CST:
16737       if (host_integerp (val, 0))
16738         return int_loc_descriptor (tree_low_cst (val, 0));
16739       break;
16740     case INDIRECT_REF:
16741       size = int_size_in_bytes (TREE_TYPE (val));
16742       if (size < 0)
16743         break;
16744       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16745       if (!loc)
16746         break;
16747       if (size == DWARF2_ADDR_SIZE)
16748         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16749       else
16750         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16751       return loc;
16752     case POINTER_PLUS_EXPR:
16753     case PLUS_EXPR:
16754       if (host_integerp (TREE_OPERAND (val, 1), 1)
16755           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16756              < 16384)
16757         {
16758           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16759           if (!loc)
16760             break;
16761           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16762         }
16763       else
16764         {
16765           op = DW_OP_plus;
16766         do_binop:
16767           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16768           if (!loc)
16769             break;
16770           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16771           if (!loc2)
16772             break;
16773           add_loc_descr (&loc, loc2);
16774           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16775         }
16776       return loc;
16777     case MINUS_EXPR:
16778       op = DW_OP_minus;
16779       goto do_binop;
16780     case MULT_EXPR:
16781       op = DW_OP_mul;
16782       goto do_binop;
16783     case EQ_EXPR:
16784       op = DW_OP_eq;
16785       goto do_binop;
16786     case NE_EXPR:
16787       op = DW_OP_ne;
16788       goto do_binop;
16789     default:
16790       break;
16791     }
16792   return NULL;
16793 }
16794
16795 static void
16796 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16797                       tree val, tree base_decl)
16798 {
16799   dw_loc_descr_ref loc;
16800
16801   if (host_integerp (val, 0))
16802     {
16803       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16804       return;
16805     }
16806
16807   loc = descr_info_loc (val, base_decl);
16808   if (!loc)
16809     return;
16810
16811   add_AT_loc (die, attr, loc);
16812 }
16813
16814 /* This routine generates DIE for array with hidden descriptor, details
16815    are filled into *info by a langhook.  */
16816
16817 static void
16818 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16819                           dw_die_ref context_die)
16820 {
16821   dw_die_ref scope_die = scope_die_for (type, context_die);
16822   dw_die_ref array_die;
16823   int dim;
16824
16825   array_die = new_die (DW_TAG_array_type, scope_die, type);
16826   add_name_attribute (array_die, type_tag (type));
16827   equate_type_number_to_die (type, array_die);
16828
16829   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16830   if (is_fortran ()
16831       && info->ndimensions >= 2)
16832     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16833
16834   if (info->data_location)
16835     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16836                           info->base_decl);
16837   if (info->associated)
16838     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16839                           info->base_decl);
16840   if (info->allocated)
16841     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16842                           info->base_decl);
16843
16844   for (dim = 0; dim < info->ndimensions; dim++)
16845     {
16846       dw_die_ref subrange_die
16847         = new_die (DW_TAG_subrange_type, array_die, NULL);
16848
16849       if (info->dimen[dim].lower_bound)
16850         {
16851           /* If it is the default value, omit it.  */
16852           int dflt;
16853
16854           if (host_integerp (info->dimen[dim].lower_bound, 0)
16855               && (dflt = lower_bound_default ()) != -1
16856               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16857             ;
16858           else
16859             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16860                                   info->dimen[dim].lower_bound,
16861                                   info->base_decl);
16862         }
16863       if (info->dimen[dim].upper_bound)
16864         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16865                               info->dimen[dim].upper_bound,
16866                               info->base_decl);
16867       if (info->dimen[dim].stride)
16868         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16869                               info->dimen[dim].stride,
16870                               info->base_decl);
16871     }
16872
16873   gen_type_die (info->element_type, context_die);
16874   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16875
16876   if (get_AT (array_die, DW_AT_name))
16877     add_pubtype (type, array_die);
16878 }
16879
16880 #if 0
16881 static void
16882 gen_entry_point_die (tree decl, dw_die_ref context_die)
16883 {
16884   tree origin = decl_ultimate_origin (decl);
16885   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16886
16887   if (origin != NULL)
16888     add_abstract_origin_attribute (decl_die, origin);
16889   else
16890     {
16891       add_name_and_src_coords_attributes (decl_die, decl);
16892       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16893                           0, 0, context_die);
16894     }
16895
16896   if (DECL_ABSTRACT (decl))
16897     equate_decl_number_to_die (decl, decl_die);
16898   else
16899     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16900 }
16901 #endif
16902
16903 /* Walk through the list of incomplete types again, trying once more to
16904    emit full debugging info for them.  */
16905
16906 static void
16907 retry_incomplete_types (void)
16908 {
16909   int i;
16910
16911   for (i = vec_safe_length (incomplete_types) - 1; i >= 0; i--)
16912     if (should_emit_struct_debug ((*incomplete_types)[i], DINFO_USAGE_DIR_USE))
16913       gen_type_die ((*incomplete_types)[i], comp_unit_die ());
16914 }
16915
16916 /* Determine what tag to use for a record type.  */
16917
16918 static enum dwarf_tag
16919 record_type_tag (tree type)
16920 {
16921   if (! lang_hooks.types.classify_record)
16922     return DW_TAG_structure_type;
16923
16924   switch (lang_hooks.types.classify_record (type))
16925     {
16926     case RECORD_IS_STRUCT:
16927       return DW_TAG_structure_type;
16928
16929     case RECORD_IS_CLASS:
16930       return DW_TAG_class_type;
16931
16932     case RECORD_IS_INTERFACE:
16933       if (dwarf_version >= 3 || !dwarf_strict)
16934         return DW_TAG_interface_type;
16935       return DW_TAG_structure_type;
16936
16937     default:
16938       gcc_unreachable ();
16939     }
16940 }
16941
16942 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16943    include all of the information about the enumeration values also. Each
16944    enumerated type name/value is listed as a child of the enumerated type
16945    DIE.  */
16946
16947 static dw_die_ref
16948 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16949 {
16950   dw_die_ref type_die = lookup_type_die (type);
16951
16952   if (type_die == NULL)
16953     {
16954       type_die = new_die (DW_TAG_enumeration_type,
16955                           scope_die_for (type, context_die), type);
16956       equate_type_number_to_die (type, type_die);
16957       add_name_attribute (type_die, type_tag (type));
16958       if (dwarf_version >= 4 || !dwarf_strict)
16959         {
16960           if (ENUM_IS_SCOPED (type))
16961             add_AT_flag (type_die, DW_AT_enum_class, 1);
16962           if (ENUM_IS_OPAQUE (type))
16963             add_AT_flag (type_die, DW_AT_declaration, 1);
16964         }
16965     }
16966   else if (! TYPE_SIZE (type))
16967     return type_die;
16968   else
16969     remove_AT (type_die, DW_AT_declaration);
16970
16971   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16972      given enum type is incomplete, do not generate the DW_AT_byte_size
16973      attribute or the DW_AT_element_list attribute.  */
16974   if (TYPE_SIZE (type))
16975     {
16976       tree link;
16977
16978       TREE_ASM_WRITTEN (type) = 1;
16979       add_byte_size_attribute (type_die, type);
16980       if (TYPE_STUB_DECL (type) != NULL_TREE)
16981         {
16982           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16983           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16984         }
16985
16986       /* If the first reference to this type was as the return type of an
16987          inline function, then it may not have a parent.  Fix this now.  */
16988       if (type_die->die_parent == NULL)
16989         add_child_die (scope_die_for (type, context_die), type_die);
16990
16991       for (link = TYPE_VALUES (type);
16992            link != NULL; link = TREE_CHAIN (link))
16993         {
16994           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16995           tree value = TREE_VALUE (link);
16996
16997           add_name_attribute (enum_die,
16998                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16999
17000           if (TREE_CODE (value) == CONST_DECL)
17001             value = DECL_INITIAL (value);
17002
17003           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17004             /* DWARF2 does not provide a way of indicating whether or
17005                not enumeration constants are signed or unsigned.  GDB
17006                always assumes the values are signed, so we output all
17007                values as if they were signed.  That means that
17008                enumeration constants with very large unsigned values
17009                will appear to have negative values in the debugger.  */
17010             add_AT_int (enum_die, DW_AT_const_value,
17011                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17012         }
17013
17014       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17015       if (TYPE_ARTIFICIAL (type))
17016         add_AT_flag (type_die, DW_AT_artificial, 1);
17017     }
17018   else
17019     add_AT_flag (type_die, DW_AT_declaration, 1);
17020
17021   add_pubtype (type, type_die);
17022
17023   return type_die;
17024 }
17025
17026 /* Generate a DIE to represent either a real live formal parameter decl or to
17027    represent just the type of some formal parameter position in some function
17028    type.
17029
17030    Note that this routine is a bit unusual because its argument may be a
17031    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17032    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17033    node.  If it's the former then this function is being called to output a
17034    DIE to represent a formal parameter object (or some inlining thereof).  If
17035    it's the latter, then this function is only being called to output a
17036    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17037    argument type of some subprogram type.
17038    If EMIT_NAME_P is true, name and source coordinate attributes
17039    are emitted.  */
17040
17041 static dw_die_ref
17042 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17043                           dw_die_ref context_die)
17044 {
17045   tree node_or_origin = node ? node : origin;
17046   tree ultimate_origin;
17047   dw_die_ref parm_die
17048     = new_die (DW_TAG_formal_parameter, context_die, node);
17049
17050   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17051     {
17052     case tcc_declaration:
17053       ultimate_origin = decl_ultimate_origin (node_or_origin);
17054       if (node || ultimate_origin)
17055         origin = ultimate_origin;
17056       if (origin != NULL)
17057         add_abstract_origin_attribute (parm_die, origin);
17058       else if (emit_name_p)
17059         add_name_and_src_coords_attributes (parm_die, node);
17060       if (origin == NULL
17061           || (! DECL_ABSTRACT (node_or_origin)
17062               && variably_modified_type_p (TREE_TYPE (node_or_origin),
17063                                            decl_function_context
17064                                                             (node_or_origin))))
17065         {
17066           tree type = TREE_TYPE (node_or_origin);
17067           if (decl_by_reference_p (node_or_origin))
17068             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17069                                 context_die);
17070           else
17071             add_type_attribute (parm_die, type,
17072                                 TREE_READONLY (node_or_origin),
17073                                 TREE_THIS_VOLATILE (node_or_origin),
17074                                 context_die);
17075         }
17076       if (origin == NULL && DECL_ARTIFICIAL (node))
17077         add_AT_flag (parm_die, DW_AT_artificial, 1);
17078
17079       if (node && node != origin)
17080         equate_decl_number_to_die (node, parm_die);
17081       if (! DECL_ABSTRACT (node_or_origin))
17082         add_location_or_const_value_attribute (parm_die, node_or_origin,
17083                                                node == NULL, DW_AT_location);
17084
17085       break;
17086
17087     case tcc_type:
17088       /* We were called with some kind of a ..._TYPE node.  */
17089       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17090       break;
17091
17092     default:
17093       gcc_unreachable ();
17094     }
17095
17096   return parm_die;
17097 }
17098
17099 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17100    children DW_TAG_formal_parameter DIEs representing the arguments of the
17101    parameter pack.
17102
17103    PARM_PACK must be a function parameter pack.
17104    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17105    must point to the subsequent arguments of the function PACK_ARG belongs to.
17106    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17107    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17108    following the last one for which a DIE was generated.  */
17109
17110 static dw_die_ref
17111 gen_formal_parameter_pack_die  (tree parm_pack,
17112                                 tree pack_arg,
17113                                 dw_die_ref subr_die,
17114                                 tree *next_arg)
17115 {
17116   tree arg;
17117   dw_die_ref parm_pack_die;
17118
17119   gcc_assert (parm_pack
17120               && lang_hooks.function_parameter_pack_p (parm_pack)
17121               && subr_die);
17122
17123   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17124   add_src_coords_attributes (parm_pack_die, parm_pack);
17125
17126   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17127     {
17128       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17129                                                                  parm_pack))
17130         break;
17131       gen_formal_parameter_die (arg, NULL,
17132                                 false /* Don't emit name attribute.  */,
17133                                 parm_pack_die);
17134     }
17135   if (next_arg)
17136     *next_arg = arg;
17137   return parm_pack_die;
17138 }
17139
17140 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17141    at the end of an (ANSI prototyped) formal parameters list.  */
17142
17143 static void
17144 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17145 {
17146   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17147 }
17148
17149 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17150    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17151    parameters as specified in some function type specification (except for
17152    those which appear as part of a function *definition*).  */
17153
17154 static void
17155 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17156 {
17157   tree link;
17158   tree formal_type = NULL;
17159   tree first_parm_type;
17160   tree arg;
17161
17162   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17163     {
17164       arg = DECL_ARGUMENTS (function_or_method_type);
17165       function_or_method_type = TREE_TYPE (function_or_method_type);
17166     }
17167   else
17168     arg = NULL_TREE;
17169
17170   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17171
17172   /* Make our first pass over the list of formal parameter types and output a
17173      DW_TAG_formal_parameter DIE for each one.  */
17174   for (link = first_parm_type; link; )
17175     {
17176       dw_die_ref parm_die;
17177
17178       formal_type = TREE_VALUE (link);
17179       if (formal_type == void_type_node)
17180         break;
17181
17182       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17183       parm_die = gen_formal_parameter_die (formal_type, NULL,
17184                                            true /* Emit name attribute.  */,
17185                                            context_die);
17186       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17187           && link == first_parm_type)
17188         {
17189           add_AT_flag (parm_die, DW_AT_artificial, 1);
17190           if (dwarf_version >= 3 || !dwarf_strict)
17191             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17192         }
17193       else if (arg && DECL_ARTIFICIAL (arg))
17194         add_AT_flag (parm_die, DW_AT_artificial, 1);
17195
17196       link = TREE_CHAIN (link);
17197       if (arg)
17198         arg = DECL_CHAIN (arg);
17199     }
17200
17201   /* If this function type has an ellipsis, add a
17202      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17203   if (formal_type != void_type_node)
17204     gen_unspecified_parameters_die (function_or_method_type, context_die);
17205
17206   /* Make our second (and final) pass over the list of formal parameter types
17207      and output DIEs to represent those types (as necessary).  */
17208   for (link = TYPE_ARG_TYPES (function_or_method_type);
17209        link && TREE_VALUE (link);
17210        link = TREE_CHAIN (link))
17211     gen_type_die (TREE_VALUE (link), context_die);
17212 }
17213
17214 /* We want to generate the DIE for TYPE so that we can generate the
17215    die for MEMBER, which has been defined; we will need to refer back
17216    to the member declaration nested within TYPE.  If we're trying to
17217    generate minimal debug info for TYPE, processing TYPE won't do the
17218    trick; we need to attach the member declaration by hand.  */
17219
17220 static void
17221 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17222 {
17223   gen_type_die (type, context_die);
17224
17225   /* If we're trying to avoid duplicate debug info, we may not have
17226      emitted the member decl for this function.  Emit it now.  */
17227   if (TYPE_STUB_DECL (type)
17228       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17229       && ! lookup_decl_die (member))
17230     {
17231       dw_die_ref type_die;
17232       gcc_assert (!decl_ultimate_origin (member));
17233
17234       push_decl_scope (type);
17235       type_die = lookup_type_die_strip_naming_typedef (type);
17236       if (TREE_CODE (member) == FUNCTION_DECL)
17237         gen_subprogram_die (member, type_die);
17238       else if (TREE_CODE (member) == FIELD_DECL)
17239         {
17240           /* Ignore the nameless fields that are used to skip bits but handle
17241              C++ anonymous unions and structs.  */
17242           if (DECL_NAME (member) != NULL_TREE
17243               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17244               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17245             {
17246               gen_type_die (member_declared_type (member), type_die);
17247               gen_field_die (member, type_die);
17248             }
17249         }
17250       else
17251         gen_variable_die (member, NULL_TREE, type_die);
17252
17253       pop_decl_scope ();
17254     }
17255 }
17256 \f
17257 /* Forward declare these functions, because they are mutually recursive
17258   with their set_block_* pairing functions.  */
17259 static void set_decl_origin_self (tree);
17260 static void set_decl_abstract_flags (tree, int);
17261
17262 /* Given a pointer to some BLOCK node, if the BLOCK_ABSTRACT_ORIGIN for the
17263    given BLOCK node is NULL, set the BLOCK_ABSTRACT_ORIGIN for the node so
17264    that it points to the node itself, thus indicating that the node is its
17265    own (abstract) origin.  Additionally, if the BLOCK_ABSTRACT_ORIGIN for
17266    the given node is NULL, recursively descend the decl/block tree which
17267    it is the root of, and for each other ..._DECL or BLOCK node contained
17268    therein whose DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also
17269    still NULL, set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN
17270    values to point to themselves.  */
17271
17272 static void
17273 set_block_origin_self (tree stmt)
17274 {
17275   if (BLOCK_ABSTRACT_ORIGIN (stmt) == NULL_TREE)
17276     {
17277       BLOCK_ABSTRACT_ORIGIN (stmt) = stmt;
17278
17279       {
17280         tree local_decl;
17281
17282         for (local_decl = BLOCK_VARS (stmt);
17283              local_decl != NULL_TREE;
17284              local_decl = DECL_CHAIN (local_decl))
17285           if (! DECL_EXTERNAL (local_decl))
17286             set_decl_origin_self (local_decl);  /* Potential recursion.  */
17287       }
17288
17289       {
17290         tree subblock;
17291
17292         for (subblock = BLOCK_SUBBLOCKS (stmt);
17293              subblock != NULL_TREE;
17294              subblock = BLOCK_CHAIN (subblock))
17295           set_block_origin_self (subblock);     /* Recurse.  */
17296       }
17297     }
17298 }
17299
17300 /* Given a pointer to some ..._DECL node, if the DECL_ABSTRACT_ORIGIN for
17301    the given ..._DECL node is NULL, set the DECL_ABSTRACT_ORIGIN for the
17302    node to so that it points to the node itself, thus indicating that the
17303    node represents its own (abstract) origin.  Additionally, if the
17304    DECL_ABSTRACT_ORIGIN for the given node is NULL, recursively descend
17305    the decl/block tree of which the given node is the root of, and for
17306    each other ..._DECL or BLOCK node contained therein whose
17307    DECL_ABSTRACT_ORIGINs or BLOCK_ABSTRACT_ORIGINs are also still NULL,
17308    set *their* DECL_ABSTRACT_ORIGIN or BLOCK_ABSTRACT_ORIGIN values to
17309    point to themselves.  */
17310
17311 static void
17312 set_decl_origin_self (tree decl)
17313 {
17314   if (DECL_ABSTRACT_ORIGIN (decl) == NULL_TREE)
17315     {
17316       DECL_ABSTRACT_ORIGIN (decl) = decl;
17317       if (TREE_CODE (decl) == FUNCTION_DECL)
17318         {
17319           tree arg;
17320
17321           for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17322             DECL_ABSTRACT_ORIGIN (arg) = arg;
17323           if (DECL_INITIAL (decl) != NULL_TREE
17324               && DECL_INITIAL (decl) != error_mark_node)
17325             set_block_origin_self (DECL_INITIAL (decl));
17326         }
17327     }
17328 }
17329 \f
17330 /* Given a pointer to some BLOCK node, and a boolean value to set the
17331    "abstract" flags to, set that value into the BLOCK_ABSTRACT flag for
17332    the given block, and for all local decls and all local sub-blocks
17333    (recursively) which are contained therein.  */
17334
17335 static void
17336 set_block_abstract_flags (tree stmt, int setting)
17337 {
17338   tree local_decl;
17339   tree subblock;
17340   unsigned int i;
17341
17342   BLOCK_ABSTRACT (stmt) = setting;
17343
17344   for (local_decl = BLOCK_VARS (stmt);
17345        local_decl != NULL_TREE;
17346        local_decl = DECL_CHAIN (local_decl))
17347     if (! DECL_EXTERNAL (local_decl))
17348       set_decl_abstract_flags (local_decl, setting);
17349
17350   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17351     {
17352       local_decl = BLOCK_NONLOCALIZED_VAR (stmt, i);
17353       if ((TREE_CODE (local_decl) == VAR_DECL && !TREE_STATIC (local_decl))
17354           || TREE_CODE (local_decl) == PARM_DECL)
17355         set_decl_abstract_flags (local_decl, setting);
17356     }
17357
17358   for (subblock = BLOCK_SUBBLOCKS (stmt);
17359        subblock != NULL_TREE;
17360        subblock = BLOCK_CHAIN (subblock))
17361     set_block_abstract_flags (subblock, setting);
17362 }
17363
17364 /* Given a pointer to some ..._DECL node, and a boolean value to set the
17365    "abstract" flags to, set that value into the DECL_ABSTRACT flag for the
17366    given decl, and (in the case where the decl is a FUNCTION_DECL) also
17367    set the abstract flags for all of the parameters, local vars, local
17368    blocks and sub-blocks (recursively) to the same setting.  */
17369
17370 static void
17371 set_decl_abstract_flags (tree decl, int setting)
17372 {
17373   DECL_ABSTRACT (decl) = setting;
17374   if (TREE_CODE (decl) == FUNCTION_DECL)
17375     {
17376       tree arg;
17377
17378       for (arg = DECL_ARGUMENTS (decl); arg; arg = DECL_CHAIN (arg))
17379         DECL_ABSTRACT (arg) = setting;
17380       if (DECL_INITIAL (decl) != NULL_TREE
17381           && DECL_INITIAL (decl) != error_mark_node)
17382         set_block_abstract_flags (DECL_INITIAL (decl), setting);
17383     }
17384 }
17385
17386 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17387    may later generate inlined and/or out-of-line instances of.  */
17388
17389 static void
17390 dwarf2out_abstract_function (tree decl)
17391 {
17392   dw_die_ref old_die;
17393   tree save_fn;
17394   tree context;
17395   int was_abstract;
17396   htab_t old_decl_loc_table;
17397   htab_t old_cached_dw_loc_list_table;
17398   int old_call_site_count, old_tail_call_site_count;
17399   struct call_arg_loc_node *old_call_arg_locations;
17400
17401   /* Make sure we have the actual abstract inline, not a clone.  */
17402   decl = DECL_ORIGIN (decl);
17403
17404   old_die = lookup_decl_die (decl);
17405   if (old_die && get_AT (old_die, DW_AT_inline))
17406     /* We've already generated the abstract instance.  */
17407     return;
17408
17409   /* We can be called while recursively when seeing block defining inlined subroutine
17410      DIE.  Be sure to not clobber the outer location table nor use it or we would
17411      get locations in abstract instantces.  */
17412   old_decl_loc_table = decl_loc_table;
17413   decl_loc_table = NULL;
17414   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17415   cached_dw_loc_list_table = NULL;
17416   old_call_arg_locations = call_arg_locations;
17417   call_arg_locations = NULL;
17418   old_call_site_count = call_site_count;
17419   call_site_count = -1;
17420   old_tail_call_site_count = tail_call_site_count;
17421   tail_call_site_count = -1;
17422
17423   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17424      we don't get confused by DECL_ABSTRACT.  */
17425   if (debug_info_level > DINFO_LEVEL_TERSE)
17426     {
17427       context = decl_class_context (decl);
17428       if (context)
17429         gen_type_die_for_member
17430           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17431     }
17432
17433   /* Pretend we've just finished compiling this function.  */
17434   save_fn = current_function_decl;
17435   current_function_decl = decl;
17436
17437   was_abstract = DECL_ABSTRACT (decl);
17438   set_decl_abstract_flags (decl, 1);
17439   dwarf2out_decl (decl);
17440   if (! was_abstract)
17441     set_decl_abstract_flags (decl, 0);
17442
17443   current_function_decl = save_fn;
17444   decl_loc_table = old_decl_loc_table;
17445   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17446   call_arg_locations = old_call_arg_locations;
17447   call_site_count = old_call_site_count;
17448   tail_call_site_count = old_tail_call_site_count;
17449 }
17450
17451 /* Helper function of premark_used_types() which gets called through
17452    htab_traverse.
17453
17454    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17455    marked as unused by prune_unused_types.  */
17456
17457 static int
17458 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17459 {
17460   tree type;
17461   dw_die_ref die;
17462
17463   type = (tree) *slot;
17464   die = lookup_type_die (type);
17465   if (die != NULL)
17466     die->die_perennial_p = 1;
17467   return 1;
17468 }
17469
17470 /* Helper function of premark_types_used_by_global_vars which gets called
17471    through htab_traverse.
17472
17473    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17474    marked as unused by prune_unused_types. The DIE of the type is marked
17475    only if the global variable using the type will actually be emitted.  */
17476
17477 static int
17478 premark_types_used_by_global_vars_helper (void **slot,
17479                                           void *data ATTRIBUTE_UNUSED)
17480 {
17481   struct types_used_by_vars_entry *entry;
17482   dw_die_ref die;
17483
17484   entry = (struct types_used_by_vars_entry *) *slot;
17485   gcc_assert (entry->type != NULL
17486               && entry->var_decl != NULL);
17487   die = lookup_type_die (entry->type);
17488   if (die)
17489     {
17490       /* Ask cgraph if the global variable really is to be emitted.
17491          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17492       struct varpool_node *node = varpool_get_node (entry->var_decl);
17493       if (node && node->analyzed)
17494         {
17495           die->die_perennial_p = 1;
17496           /* Keep the parent DIEs as well.  */
17497           while ((die = die->die_parent) && die->die_perennial_p == 0)
17498             die->die_perennial_p = 1;
17499         }
17500     }
17501   return 1;
17502 }
17503
17504 /* Mark all members of used_types_hash as perennial.  */
17505
17506 static void
17507 premark_used_types (struct function *fun)
17508 {
17509   if (fun && fun->used_types_hash)
17510     htab_traverse (fun->used_types_hash, premark_used_types_helper, NULL);
17511 }
17512
17513 /* Mark all members of types_used_by_vars_entry as perennial.  */
17514
17515 static void
17516 premark_types_used_by_global_vars (void)
17517 {
17518   if (types_used_by_vars_hash)
17519     htab_traverse (types_used_by_vars_hash,
17520                    premark_types_used_by_global_vars_helper, NULL);
17521 }
17522
17523 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17524    for CA_LOC call arg loc node.  */
17525
17526 static dw_die_ref
17527 gen_call_site_die (tree decl, dw_die_ref subr_die,
17528                    struct call_arg_loc_node *ca_loc)
17529 {
17530   dw_die_ref stmt_die = NULL, die;
17531   tree block = ca_loc->block;
17532
17533   while (block
17534          && block != DECL_INITIAL (decl)
17535          && TREE_CODE (block) == BLOCK)
17536     {
17537       if (block_map.length () > BLOCK_NUMBER (block))
17538         stmt_die = block_map[BLOCK_NUMBER (block)];
17539       if (stmt_die)
17540         break;
17541       block = BLOCK_SUPERCONTEXT (block);
17542     }
17543   if (stmt_die == NULL)
17544     stmt_die = subr_die;
17545   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17546   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17547   if (ca_loc->tail_call_p)
17548     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17549   if (ca_loc->symbol_ref)
17550     {
17551       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17552       if (tdie)
17553         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17554       else
17555         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref, false);
17556     }
17557   return die;
17558 }
17559
17560 /* Generate a DIE to represent a declared function (either file-scope or
17561    block-local).  */
17562
17563 static void
17564 gen_subprogram_die (tree decl, dw_die_ref context_die)
17565 {
17566   tree origin = decl_ultimate_origin (decl);
17567   dw_die_ref subr_die;
17568   tree outer_scope;
17569   dw_die_ref old_die = lookup_decl_die (decl);
17570   int declaration = (current_function_decl != decl
17571                      || class_or_namespace_scope_p (context_die));
17572
17573   premark_used_types (DECL_STRUCT_FUNCTION (decl));
17574
17575   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17576      started to generate the abstract instance of an inline, decided to output
17577      its containing class, and proceeded to emit the declaration of the inline
17578      from the member list for the class.  If so, DECLARATION takes priority;
17579      we'll get back to the abstract instance when done with the class.  */
17580
17581   /* The class-scope declaration DIE must be the primary DIE.  */
17582   if (origin && declaration && class_or_namespace_scope_p (context_die))
17583     {
17584       origin = NULL;
17585       gcc_assert (!old_die);
17586     }
17587
17588   /* Now that the C++ front end lazily declares artificial member fns, we
17589      might need to retrofit the declaration into its class.  */
17590   if (!declaration && !origin && !old_die
17591       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17592       && !class_or_namespace_scope_p (context_die)
17593       && debug_info_level > DINFO_LEVEL_TERSE)
17594     old_die = force_decl_die (decl);
17595
17596   if (origin != NULL)
17597     {
17598       gcc_assert (!declaration || local_scope_p (context_die));
17599
17600       /* Fixup die_parent for the abstract instance of a nested
17601          inline function.  */
17602       if (old_die && old_die->die_parent == NULL)
17603         add_child_die (context_die, old_die);
17604
17605       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17606       add_abstract_origin_attribute (subr_die, origin);
17607       /*  This is where the actual code for a cloned function is.
17608           Let's emit linkage name attribute for it.  This helps
17609           debuggers to e.g, set breakpoints into
17610           constructors/destructors when the user asks "break
17611           K::K".  */
17612       add_linkage_name (subr_die, decl);
17613     }
17614   else if (old_die)
17615     {
17616       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17617       struct dwarf_file_data * file_index = lookup_filename (s.file);
17618
17619       if (!get_AT_flag (old_die, DW_AT_declaration)
17620           /* We can have a normal definition following an inline one in the
17621              case of redefinition of GNU C extern inlines.
17622              It seems reasonable to use AT_specification in this case.  */
17623           && !get_AT (old_die, DW_AT_inline))
17624         {
17625           /* Detect and ignore this case, where we are trying to output
17626              something we have already output.  */
17627           return;
17628         }
17629
17630       /* If the definition comes from the same place as the declaration,
17631          maybe use the old DIE.  We always want the DIE for this function
17632          that has the *_pc attributes to be under comp_unit_die so the
17633          debugger can find it.  We also need to do this for abstract
17634          instances of inlines, since the spec requires the out-of-line copy
17635          to have the same parent.  For local class methods, this doesn't
17636          apply; we just use the old DIE.  */
17637       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17638           && (DECL_ARTIFICIAL (decl)
17639               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17640                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17641                       == (unsigned) s.line))))
17642         {
17643           subr_die = old_die;
17644
17645           /* Clear out the declaration attribute and the formal parameters.
17646              Do not remove all children, because it is possible that this
17647              declaration die was forced using force_decl_die(). In such
17648              cases die that forced declaration die (e.g. TAG_imported_module)
17649              is one of the children that we do not want to remove.  */
17650           remove_AT (subr_die, DW_AT_declaration);
17651           remove_AT (subr_die, DW_AT_object_pointer);
17652           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17653         }
17654       else
17655         {
17656           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17657           add_AT_specification (subr_die, old_die);
17658           add_pubname (decl, subr_die);
17659           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17660             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17661           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17662             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17663         }
17664     }
17665   else
17666     {
17667       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17668
17669       if (TREE_PUBLIC (decl))
17670         add_AT_flag (subr_die, DW_AT_external, 1);
17671
17672       add_name_and_src_coords_attributes (subr_die, decl);
17673       add_pubname (decl, subr_die);
17674       if (debug_info_level > DINFO_LEVEL_TERSE)
17675         {
17676           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17677           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17678                               0, 0, context_die);
17679         }
17680
17681       add_pure_or_virtual_attribute (subr_die, decl);
17682       if (DECL_ARTIFICIAL (decl))
17683         add_AT_flag (subr_die, DW_AT_artificial, 1);
17684
17685       add_accessibility_attribute (subr_die, decl);
17686     }
17687
17688   if (declaration)
17689     {
17690       if (!old_die || !get_AT (old_die, DW_AT_inline))
17691         {
17692           add_AT_flag (subr_die, DW_AT_declaration, 1);
17693
17694           /* If this is an explicit function declaration then generate
17695              a DW_AT_explicit attribute.  */
17696           if (lang_hooks.decls.function_decl_explicit_p (decl)
17697               && (dwarf_version >= 3 || !dwarf_strict))
17698             add_AT_flag (subr_die, DW_AT_explicit, 1);
17699
17700           /* The first time we see a member function, it is in the context of
17701              the class to which it belongs.  We make sure of this by emitting
17702              the class first.  The next time is the definition, which is
17703              handled above.  The two may come from the same source text.
17704
17705              Note that force_decl_die() forces function declaration die. It is
17706              later reused to represent definition.  */
17707           equate_decl_number_to_die (decl, subr_die);
17708         }
17709     }
17710   else if (DECL_ABSTRACT (decl))
17711     {
17712       if (DECL_DECLARED_INLINE_P (decl))
17713         {
17714           if (cgraph_function_possibly_inlined_p (decl))
17715             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17716           else
17717             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17718         }
17719       else
17720         {
17721           if (cgraph_function_possibly_inlined_p (decl))
17722             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17723           else
17724             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17725         }
17726
17727       if (DECL_DECLARED_INLINE_P (decl)
17728           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17729         add_AT_flag (subr_die, DW_AT_artificial, 1);
17730
17731       equate_decl_number_to_die (decl, subr_die);
17732     }
17733   else if (!DECL_EXTERNAL (decl))
17734     {
17735       HOST_WIDE_INT cfa_fb_offset;
17736       struct function *fun = DECL_STRUCT_FUNCTION (decl);
17737
17738       if (!old_die || !get_AT (old_die, DW_AT_inline))
17739         equate_decl_number_to_die (decl, subr_die);
17740
17741       gcc_checking_assert (fun);
17742       if (!flag_reorder_blocks_and_partition)
17743         {
17744           dw_fde_ref fde = fun->fde;
17745           if (fde->dw_fde_begin)
17746             {
17747               /* We have already generated the labels.  */
17748              add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17749                                  fde->dw_fde_end, false);
17750             }
17751           else
17752             {
17753               /* Create start/end labels and add the range.  */
17754               char label_id_low[MAX_ARTIFICIAL_LABEL_BYTES];
17755               char label_id_high[MAX_ARTIFICIAL_LABEL_BYTES];
17756               ASM_GENERATE_INTERNAL_LABEL (label_id_low, FUNC_BEGIN_LABEL,
17757                                            current_function_funcdef_no);
17758               ASM_GENERATE_INTERNAL_LABEL (label_id_high, FUNC_END_LABEL,
17759                                            current_function_funcdef_no);
17760              add_AT_low_high_pc (subr_die, label_id_low, label_id_high,
17761                                  false);
17762             }
17763
17764 #if VMS_DEBUGGING_INFO
17765       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17766          Section 2.3 Prologue and Epilogue Attributes:
17767          When a breakpoint is set on entry to a function, it is generally
17768          desirable for execution to be suspended, not on the very first
17769          instruction of the function, but rather at a point after the
17770          function's frame has been set up, after any language defined local
17771          declaration processing has been completed, and before execution of
17772          the first statement of the function begins. Debuggers generally
17773          cannot properly determine where this point is.  Similarly for a
17774          breakpoint set on exit from a function. The prologue and epilogue
17775          attributes allow a compiler to communicate the location(s) to use.  */
17776
17777       {
17778         if (fde->dw_fde_vms_end_prologue)
17779           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17780             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17781
17782         if (fde->dw_fde_vms_begin_epilogue)
17783           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17784             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17785       }
17786 #endif
17787
17788         }
17789       else
17790         {
17791           /* Generate pubnames entries for the split function code ranges.  */
17792           dw_fde_ref fde = fun->fde;
17793
17794           if (fde->dw_fde_second_begin)
17795             {
17796               if (dwarf_version >= 3 || !dwarf_strict)
17797                 {
17798                   /* We should use ranges for non-contiguous code section 
17799                      addresses.  Use the actual code range for the initial
17800                      section, since the HOT/COLD labels might precede an 
17801                      alignment offset.  */
17802                   bool range_list_added = false;
17803                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17804                                         fde->dw_fde_end, &range_list_added,
17805                                         false);
17806                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17807                                         fde->dw_fde_second_end,
17808                                        &range_list_added, false);
17809                   if (range_list_added)
17810                     add_ranges (NULL);
17811                 }
17812               else
17813                 {
17814                   /* There is no real support in DW2 for this .. so we make
17815                      a work-around.  First, emit the pub name for the segment
17816                      containing the function label.  Then make and emit a
17817                      simplified subprogram DIE for the second segment with the
17818                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17819                      linkage name for the second die so that gdb will find both
17820                      sections when given "b foo".  */
17821                   const char *name = NULL;
17822                   tree decl_name = DECL_NAME (decl);
17823                   dw_die_ref seg_die;
17824
17825                   /* Do the 'primary' section.   */
17826                   add_AT_low_high_pc (subr_die, fde->dw_fde_begin,
17827                                       fde->dw_fde_end, false);
17828
17829                   /* Build a minimal DIE for the secondary section.  */
17830                   seg_die = new_die (DW_TAG_subprogram,
17831                                      subr_die->die_parent, decl);
17832
17833                   if (TREE_PUBLIC (decl))
17834                     add_AT_flag (seg_die, DW_AT_external, 1);
17835
17836                   if (decl_name != NULL 
17837                       && IDENTIFIER_POINTER (decl_name) != NULL)
17838                     {
17839                       name = dwarf2_name (decl, 1);
17840                       if (! DECL_ARTIFICIAL (decl))
17841                         add_src_coords_attributes (seg_die, decl);
17842
17843                       add_linkage_name (seg_die, decl);
17844                     }
17845                   gcc_assert (name != NULL);
17846                   add_pure_or_virtual_attribute (seg_die, decl);
17847                   if (DECL_ARTIFICIAL (decl))
17848                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17849
17850                   name = concat ("__second_sect_of_", name, NULL); 
17851                   add_AT_low_high_pc (seg_die, fde->dw_fde_second_begin,
17852                                       fde->dw_fde_second_end, false);
17853                   add_name_attribute (seg_die, name);
17854                   if (want_pubnames ())
17855                     add_pubname_string (name, seg_die);
17856                 }
17857             }
17858           else
17859            add_AT_low_high_pc (subr_die, fde->dw_fde_begin, fde->dw_fde_end,
17860                                false);
17861         }
17862
17863       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17864
17865       /* We define the "frame base" as the function's CFA.  This is more
17866          convenient for several reasons: (1) It's stable across the prologue
17867          and epilogue, which makes it better than just a frame pointer,
17868          (2) With dwarf3, there exists a one-byte encoding that allows us
17869          to reference the .debug_frame data by proxy, but failing that,
17870          (3) We can at least reuse the code inspection and interpretation
17871          code that determines the CFA position at various points in the
17872          function.  */
17873       if (dwarf_version >= 3 && targetm.debug_unwind_info () == UI_DWARF2)
17874         {
17875           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17876           add_AT_loc (subr_die, DW_AT_frame_base, op);
17877         }
17878       else
17879         {
17880           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17881           if (list->dw_loc_next)
17882             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17883           else
17884             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17885         }
17886
17887       /* Compute a displacement from the "steady-state frame pointer" to
17888          the CFA.  The former is what all stack slots and argument slots
17889          will reference in the rtl; the latter is what we've told the
17890          debugger about.  We'll need to adjust all frame_base references
17891          by this displacement.  */
17892       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17893
17894       if (fun->static_chain_decl)
17895         add_AT_location_description (subr_die, DW_AT_static_link,
17896                  loc_list_from_tree (fun->static_chain_decl, 2));
17897     }
17898
17899   /* Generate child dies for template paramaters.  */
17900   if (debug_info_level > DINFO_LEVEL_TERSE)
17901     gen_generic_params_dies (decl);
17902
17903   /* Now output descriptions of the arguments for this function. This gets
17904      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17905      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17906      `...' at the end of the formal parameter list.  In order to find out if
17907      there was a trailing ellipsis or not, we must instead look at the type
17908      associated with the FUNCTION_DECL.  This will be a node of type
17909      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17910      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17911      an ellipsis at the end.  */
17912
17913   /* In the case where we are describing a mere function declaration, all we
17914      need to do here (and all we *can* do here) is to describe the *types* of
17915      its formal parameters.  */
17916   if (debug_info_level <= DINFO_LEVEL_TERSE)
17917     ;
17918   else if (declaration)
17919     gen_formal_types_die (decl, subr_die);
17920   else
17921     {
17922       /* Generate DIEs to represent all known formal parameters.  */
17923       tree parm = DECL_ARGUMENTS (decl);
17924       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17925       tree generic_decl_parm = generic_decl
17926                                 ? DECL_ARGUMENTS (generic_decl)
17927                                 : NULL;
17928
17929       /* Now we want to walk the list of parameters of the function and
17930          emit their relevant DIEs.
17931
17932          We consider the case of DECL being an instance of a generic function
17933          as well as it being a normal function.
17934
17935          If DECL is an instance of a generic function we walk the
17936          parameters of the generic function declaration _and_ the parameters of
17937          DECL itself. This is useful because we want to emit specific DIEs for
17938          function parameter packs and those are declared as part of the
17939          generic function declaration. In that particular case,
17940          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17941          That DIE has children DIEs representing the set of arguments
17942          of the pack. Note that the set of pack arguments can be empty.
17943          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17944          children DIE.
17945
17946          Otherwise, we just consider the parameters of DECL.  */
17947       while (generic_decl_parm || parm)
17948         {
17949           if (generic_decl_parm
17950               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17951             gen_formal_parameter_pack_die (generic_decl_parm,
17952                                            parm, subr_die,
17953                                            &parm);
17954           else if (parm)
17955             {
17956               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17957
17958               if (parm == DECL_ARGUMENTS (decl)
17959                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17960                   && parm_die
17961                   && (dwarf_version >= 3 || !dwarf_strict))
17962                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17963
17964               parm = DECL_CHAIN (parm);
17965             }
17966
17967           if (generic_decl_parm)
17968             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17969         }
17970
17971       /* Decide whether we need an unspecified_parameters DIE at the end.
17972          There are 2 more cases to do this for: 1) the ansi ... declaration -
17973          this is detectable when the end of the arg list is not a
17974          void_type_node 2) an unprototyped function declaration (not a
17975          definition).  This just means that we have no info about the
17976          parameters at all.  */
17977       if (prototype_p (TREE_TYPE (decl)))
17978         {
17979           /* This is the prototyped case, check for....  */
17980           if (stdarg_p (TREE_TYPE (decl)))
17981             gen_unspecified_parameters_die (decl, subr_die);
17982         }
17983       else if (DECL_INITIAL (decl) == NULL_TREE)
17984         gen_unspecified_parameters_die (decl, subr_die);
17985     }
17986
17987   /* Output Dwarf info for all of the stuff within the body of the function
17988      (if it has one - it may be just a declaration).  */
17989   outer_scope = DECL_INITIAL (decl);
17990
17991   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17992      a function.  This BLOCK actually represents the outermost binding contour
17993      for the function, i.e. the contour in which the function's formal
17994      parameters and labels get declared. Curiously, it appears that the front
17995      end doesn't actually put the PARM_DECL nodes for the current function onto
17996      the BLOCK_VARS list for this outer scope, but are strung off of the
17997      DECL_ARGUMENTS list for the function instead.
17998
17999      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18000      the LABEL_DECL nodes for the function however, and we output DWARF info
18001      for those in decls_for_scope.  Just within the `outer_scope' there will be
18002      a BLOCK node representing the function's outermost pair of curly braces,
18003      and any blocks used for the base and member initializers of a C++
18004      constructor function.  */
18005   if (! declaration && outer_scope && TREE_CODE (outer_scope) != ERROR_MARK)
18006     {
18007       int call_site_note_count = 0;
18008       int tail_call_site_note_count = 0;
18009
18010       /* Emit a DW_TAG_variable DIE for a named return value.  */
18011       if (DECL_NAME (DECL_RESULT (decl)))
18012         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18013
18014       current_function_has_inlines = 0;
18015       decls_for_scope (outer_scope, subr_die, 0);
18016
18017       if (call_arg_locations && !dwarf_strict)
18018         {
18019           struct call_arg_loc_node *ca_loc;
18020           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
18021             {
18022               dw_die_ref die = NULL;
18023               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
18024               rtx arg, next_arg;
18025
18026               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
18027                    arg; arg = next_arg)
18028                 {
18029                   dw_loc_descr_ref reg, val;
18030                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
18031                   dw_die_ref cdie, tdie = NULL;
18032
18033                   next_arg = XEXP (arg, 1);
18034                   if (REG_P (XEXP (XEXP (arg, 0), 0))
18035                       && next_arg
18036                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
18037                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
18038                       && REGNO (XEXP (XEXP (arg, 0), 0))
18039                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
18040                     next_arg = XEXP (next_arg, 1);
18041                   if (mode == VOIDmode)
18042                     {
18043                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
18044                       if (mode == VOIDmode)
18045                         mode = GET_MODE (XEXP (arg, 0));
18046                     }
18047                   if (mode == VOIDmode || mode == BLKmode)
18048                     continue;
18049                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
18050                     {
18051                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18052                       tloc = XEXP (XEXP (arg, 0), 1);
18053                       continue;
18054                     }
18055                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
18056                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
18057                     {
18058                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
18059                       tlocc = XEXP (XEXP (arg, 0), 1);
18060                       continue;
18061                     }
18062                   reg = NULL;
18063                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
18064                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18065                                               VAR_INIT_STATUS_INITIALIZED);
18066                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18067                     {
18068                       rtx mem = XEXP (XEXP (arg, 0), 0);
18069                       reg = mem_loc_descriptor (XEXP (mem, 0),
18070                                                 get_address_mode (mem),
18071                                                 GET_MODE (mem),
18072                                                 VAR_INIT_STATUS_INITIALIZED);
18073                     }
18074                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18075                            == DEBUG_PARAMETER_REF)
18076                     {
18077                       tree tdecl
18078                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18079                       tdie = lookup_decl_die (tdecl);
18080                       if (tdie == NULL)
18081                         continue;
18082                     }
18083                   else
18084                     continue;
18085                   if (reg == NULL
18086                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
18087                          != DEBUG_PARAMETER_REF)
18088                     continue;
18089                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18090                                             VOIDmode,
18091                                             VAR_INIT_STATUS_INITIALIZED);
18092                   if (val == NULL)
18093                     continue;
18094                   if (die == NULL)
18095                     die = gen_call_site_die (decl, subr_die, ca_loc);
18096                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18097                                   NULL_TREE);
18098                   if (reg != NULL)
18099                     add_AT_loc (cdie, DW_AT_location, reg);
18100                   else if (tdie != NULL)
18101                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18102                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18103                   if (next_arg != XEXP (arg, 1))
18104                     {
18105                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18106                       if (mode == VOIDmode)
18107                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18108                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18109                                                             0), 1),
18110                                                 mode, VOIDmode,
18111                                                 VAR_INIT_STATUS_INITIALIZED);
18112                       if (val != NULL)
18113                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18114                     }
18115                 }
18116               if (die == NULL
18117                   && (ca_loc->symbol_ref || tloc))
18118                 die = gen_call_site_die (decl, subr_die, ca_loc);
18119               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18120                 {
18121                   dw_loc_descr_ref tval = NULL;
18122
18123                   if (tloc != NULL_RTX)
18124                     tval = mem_loc_descriptor (tloc,
18125                                                GET_MODE (tloc) == VOIDmode
18126                                                ? Pmode : GET_MODE (tloc),
18127                                                VOIDmode,
18128                                                VAR_INIT_STATUS_INITIALIZED);
18129                   if (tval)
18130                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18131                   else if (tlocc != NULL_RTX)
18132                     {
18133                       tval = mem_loc_descriptor (tlocc,
18134                                                  GET_MODE (tlocc) == VOIDmode
18135                                                  ? Pmode : GET_MODE (tlocc),
18136                                                  VOIDmode,
18137                                                  VAR_INIT_STATUS_INITIALIZED);
18138                       if (tval)
18139                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18140                                     tval);
18141                     }
18142                 }
18143               if (die != NULL)
18144                 {
18145                   call_site_note_count++;
18146                   if (ca_loc->tail_call_p)
18147                     tail_call_site_note_count++;
18148                 }
18149             }
18150         }
18151       call_arg_locations = NULL;
18152       call_arg_loc_last = NULL;
18153       if (tail_call_site_count >= 0
18154           && tail_call_site_count == tail_call_site_note_count
18155           && !dwarf_strict)
18156         {
18157           if (call_site_count >= 0
18158               && call_site_count == call_site_note_count)
18159             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18160           else
18161             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18162         }
18163       call_site_count = -1;
18164       tail_call_site_count = -1;
18165     }
18166   /* Add the calling convention attribute if requested.  */
18167   add_calling_convention_attribute (subr_die, decl);
18168
18169 }
18170
18171 /* Returns a hash value for X (which really is a die_struct).  */
18172
18173 static hashval_t
18174 common_block_die_table_hash (const void *x)
18175 {
18176   const_dw_die_ref d = (const_dw_die_ref) x;
18177   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18178 }
18179
18180 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18181    as decl_id and die_parent of die_struct Y.  */
18182
18183 static int
18184 common_block_die_table_eq (const void *x, const void *y)
18185 {
18186   const_dw_die_ref d = (const_dw_die_ref) x;
18187   const_dw_die_ref e = (const_dw_die_ref) y;
18188   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18189 }
18190
18191 /* Generate a DIE to represent a declared data object.
18192    Either DECL or ORIGIN must be non-null.  */
18193
18194 static void
18195 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18196 {
18197   HOST_WIDE_INT off = 0;
18198   tree com_decl;
18199   tree decl_or_origin = decl ? decl : origin;
18200   tree ultimate_origin;
18201   dw_die_ref var_die;
18202   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18203   dw_die_ref origin_die;
18204   bool declaration = (DECL_EXTERNAL (decl_or_origin)
18205                       || class_or_namespace_scope_p (context_die));
18206   bool specialization_p = false;
18207
18208   ultimate_origin = decl_ultimate_origin (decl_or_origin);
18209   if (decl || ultimate_origin)
18210     origin = ultimate_origin;
18211   com_decl = fortran_common (decl_or_origin, &off);
18212
18213   /* Symbol in common gets emitted as a child of the common block, in the form
18214      of a data member.  */
18215   if (com_decl)
18216     {
18217       dw_die_ref com_die;
18218       dw_loc_list_ref loc;
18219       die_node com_die_arg;
18220
18221       var_die = lookup_decl_die (decl_or_origin);
18222       if (var_die)
18223         {
18224           if (get_AT (var_die, DW_AT_location) == NULL)
18225             {
18226               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18227               if (loc)
18228                 {
18229                   if (off)
18230                     {
18231                       /* Optimize the common case.  */
18232                       if (single_element_loc_list_p (loc)
18233                           && loc->expr->dw_loc_opc == DW_OP_addr
18234                           && loc->expr->dw_loc_next == NULL
18235                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18236                              == SYMBOL_REF)
18237                         {
18238                           rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18239                           loc->expr->dw_loc_oprnd1.v.val_addr
18240                             = plus_constant (GET_MODE (x), x , off);
18241                         }
18242                       else
18243                         loc_list_plus_const (loc, off);
18244                     }
18245                   add_AT_location_description (var_die, DW_AT_location, loc);
18246                   remove_AT (var_die, DW_AT_declaration);
18247                 }
18248             }
18249           return;
18250         }
18251
18252       if (common_block_die_table == NULL)
18253         common_block_die_table
18254           = htab_create_ggc (10, common_block_die_table_hash,
18255                              common_block_die_table_eq, NULL);
18256
18257       com_die_arg.decl_id = DECL_UID (com_decl);
18258       com_die_arg.die_parent = context_die;
18259       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18260       loc = loc_list_from_tree (com_decl, 2);
18261       if (com_die == NULL)
18262         {
18263           const char *cnam
18264             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18265           void **slot;
18266
18267           com_die = new_die (DW_TAG_common_block, context_die, decl);
18268           add_name_and_src_coords_attributes (com_die, com_decl);
18269           if (loc)
18270             {
18271               add_AT_location_description (com_die, DW_AT_location, loc);
18272               /* Avoid sharing the same loc descriptor between
18273                  DW_TAG_common_block and DW_TAG_variable.  */
18274               loc = loc_list_from_tree (com_decl, 2);
18275             }
18276           else if (DECL_EXTERNAL (decl))
18277             add_AT_flag (com_die, DW_AT_declaration, 1);
18278           if (want_pubnames ())
18279             add_pubname_string (cnam, com_die); /* ??? needed? */
18280           com_die->decl_id = DECL_UID (com_decl);
18281           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18282           *slot = (void *) com_die;
18283         }
18284       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18285         {
18286           add_AT_location_description (com_die, DW_AT_location, loc);
18287           loc = loc_list_from_tree (com_decl, 2);
18288           remove_AT (com_die, DW_AT_declaration);
18289         }
18290       var_die = new_die (DW_TAG_variable, com_die, decl);
18291       add_name_and_src_coords_attributes (var_die, decl);
18292       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18293                           TREE_THIS_VOLATILE (decl), context_die);
18294       add_AT_flag (var_die, DW_AT_external, 1);
18295       if (loc)
18296         {
18297           if (off)
18298             {
18299               /* Optimize the common case.  */
18300               if (single_element_loc_list_p (loc)
18301                   && loc->expr->dw_loc_opc == DW_OP_addr
18302                   && loc->expr->dw_loc_next == NULL
18303                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18304                 {
18305                   rtx x = loc->expr->dw_loc_oprnd1.v.val_addr;
18306                   loc->expr->dw_loc_oprnd1.v.val_addr
18307                     = plus_constant (GET_MODE (x), x, off);
18308                 }
18309               else
18310                 loc_list_plus_const (loc, off);
18311             }
18312           add_AT_location_description (var_die, DW_AT_location, loc);
18313         }
18314       else if (DECL_EXTERNAL (decl))
18315         add_AT_flag (var_die, DW_AT_declaration, 1);
18316       equate_decl_number_to_die (decl, var_die);
18317       return;
18318     }
18319
18320   /* If the compiler emitted a definition for the DECL declaration
18321      and if we already emitted a DIE for it, don't emit a second
18322      DIE for it again. Allow re-declarations of DECLs that are
18323      inside functions, though.  */
18324   if (old_die && declaration && !local_scope_p (context_die))
18325     return;
18326
18327   /* For static data members, the declaration in the class is supposed
18328      to have DW_TAG_member tag; the specification should still be
18329      DW_TAG_variable referencing the DW_TAG_member DIE.  */
18330   if (declaration && class_scope_p (context_die))
18331     var_die = new_die (DW_TAG_member, context_die, decl);
18332   else
18333     var_die = new_die (DW_TAG_variable, context_die, decl);
18334
18335   origin_die = NULL;
18336   if (origin != NULL)
18337     origin_die = add_abstract_origin_attribute (var_die, origin);
18338
18339   /* Loop unrolling can create multiple blocks that refer to the same
18340      static variable, so we must test for the DW_AT_declaration flag.
18341
18342      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18343      copy decls and set the DECL_ABSTRACT flag on them instead of
18344      sharing them.
18345
18346      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18347
18348      ??? The declare_in_namespace support causes us to get two DIEs for one
18349      variable, both of which are declarations.  We want to avoid considering
18350      one to be a specification, so we must test that this DIE is not a
18351      declaration.  */
18352   else if (old_die && TREE_STATIC (decl) && ! declaration
18353            && get_AT_flag (old_die, DW_AT_declaration) == 1)
18354     {
18355       /* This is a definition of a C++ class level static.  */
18356       add_AT_specification (var_die, old_die);
18357       specialization_p = true;
18358       if (DECL_NAME (decl))
18359         {
18360           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18361           struct dwarf_file_data * file_index = lookup_filename (s.file);
18362
18363           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18364             add_AT_file (var_die, DW_AT_decl_file, file_index);
18365
18366           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18367             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18368
18369           if (old_die->die_tag == DW_TAG_member)
18370             add_linkage_name (var_die, decl);
18371         }
18372     }
18373   else
18374     add_name_and_src_coords_attributes (var_die, decl);
18375
18376   if ((origin == NULL && !specialization_p)
18377       || (origin != NULL
18378           && !DECL_ABSTRACT (decl_or_origin)
18379           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18380                                        decl_function_context
18381                                                         (decl_or_origin))))
18382     {
18383       tree type = TREE_TYPE (decl_or_origin);
18384
18385       if (decl_by_reference_p (decl_or_origin))
18386         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18387       else
18388         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18389                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
18390     }
18391
18392   if (origin == NULL && !specialization_p)
18393     {
18394       if (TREE_PUBLIC (decl))
18395         add_AT_flag (var_die, DW_AT_external, 1);
18396
18397       if (DECL_ARTIFICIAL (decl))
18398         add_AT_flag (var_die, DW_AT_artificial, 1);
18399
18400       add_accessibility_attribute (var_die, decl);
18401     }
18402
18403   if (declaration)
18404     add_AT_flag (var_die, DW_AT_declaration, 1);
18405
18406   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18407     equate_decl_number_to_die (decl, var_die);
18408
18409   if (! declaration
18410       && (! DECL_ABSTRACT (decl_or_origin)
18411           /* Local static vars are shared between all clones/inlines,
18412              so emit DW_AT_location on the abstract DIE if DECL_RTL is
18413              already set.  */
18414           || (TREE_CODE (decl_or_origin) == VAR_DECL
18415               && TREE_STATIC (decl_or_origin)
18416               && DECL_RTL_SET_P (decl_or_origin)))
18417       /* When abstract origin already has DW_AT_location attribute, no need
18418          to add it again.  */
18419       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18420     {
18421       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18422           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18423         defer_location (decl_or_origin, var_die);
18424       else
18425         add_location_or_const_value_attribute (var_die, decl_or_origin,
18426                                                decl == NULL, DW_AT_location);
18427       add_pubname (decl_or_origin, var_die);
18428     }
18429   else
18430     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18431 }
18432
18433 /* Generate a DIE to represent a named constant.  */
18434
18435 static void
18436 gen_const_die (tree decl, dw_die_ref context_die)
18437 {
18438   dw_die_ref const_die;
18439   tree type = TREE_TYPE (decl);
18440
18441   const_die = new_die (DW_TAG_constant, context_die, decl);
18442   add_name_and_src_coords_attributes (const_die, decl);
18443   add_type_attribute (const_die, type, 1, 0, context_die);
18444   if (TREE_PUBLIC (decl))
18445     add_AT_flag (const_die, DW_AT_external, 1);
18446   if (DECL_ARTIFICIAL (decl))
18447     add_AT_flag (const_die, DW_AT_artificial, 1);
18448   tree_add_const_value_attribute_for_decl (const_die, decl);
18449 }
18450
18451 /* Generate a DIE to represent a label identifier.  */
18452
18453 static void
18454 gen_label_die (tree decl, dw_die_ref context_die)
18455 {
18456   tree origin = decl_ultimate_origin (decl);
18457   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18458   rtx insn;
18459   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18460
18461   if (origin != NULL)
18462     add_abstract_origin_attribute (lbl_die, origin);
18463   else
18464     add_name_and_src_coords_attributes (lbl_die, decl);
18465
18466   if (DECL_ABSTRACT (decl))
18467     equate_decl_number_to_die (decl, lbl_die);
18468   else
18469     {
18470       insn = DECL_RTL_IF_SET (decl);
18471
18472       /* Deleted labels are programmer specified labels which have been
18473          eliminated because of various optimizations.  We still emit them
18474          here so that it is possible to put breakpoints on them.  */
18475       if (insn
18476           && (LABEL_P (insn)
18477               || ((NOTE_P (insn)
18478                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18479         {
18480           /* When optimization is enabled (via -O) some parts of the compiler
18481              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18482              represent source-level labels which were explicitly declared by
18483              the user.  This really shouldn't be happening though, so catch
18484              it if it ever does happen.  */
18485           gcc_assert (!INSN_DELETED_P (insn));
18486
18487           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18488           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18489         }
18490       else if (insn
18491                && NOTE_P (insn)
18492                && NOTE_KIND (insn) == NOTE_INSN_DELETED_DEBUG_LABEL
18493                && CODE_LABEL_NUMBER (insn) != -1)
18494         {
18495           ASM_GENERATE_INTERNAL_LABEL (label, "LDL", CODE_LABEL_NUMBER (insn));
18496           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18497         }
18498     }
18499 }
18500
18501 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18502    attributes to the DIE for a block STMT, to describe where the inlined
18503    function was called from.  This is similar to add_src_coords_attributes.  */
18504
18505 static inline void
18506 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18507 {
18508   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18509
18510   if (dwarf_version >= 3 || !dwarf_strict)
18511     {
18512       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18513       add_AT_unsigned (die, DW_AT_call_line, s.line);
18514     }
18515 }
18516
18517
18518 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18519    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18520
18521 static inline void
18522 add_high_low_attributes (tree stmt, dw_die_ref die)
18523 {
18524   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18525
18526   if (BLOCK_FRAGMENT_CHAIN (stmt)
18527       && (dwarf_version >= 3 || !dwarf_strict))
18528     {
18529       tree chain, superblock = NULL_TREE;
18530       dw_die_ref pdie;
18531       dw_attr_ref attr = NULL;
18532
18533       if (inlined_function_outer_scope_p (stmt))
18534         {
18535           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18536                                        BLOCK_NUMBER (stmt));
18537           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18538         }
18539
18540       /* Optimize duplicate .debug_ranges lists or even tails of
18541          lists.  If this BLOCK has same ranges as its supercontext,
18542          lookup DW_AT_ranges attribute in the supercontext (and
18543          recursively so), verify that the ranges_table contains the
18544          right values and use it instead of adding a new .debug_range.  */
18545       for (chain = stmt, pdie = die;
18546            BLOCK_SAME_RANGE (chain);
18547            chain = BLOCK_SUPERCONTEXT (chain))
18548         {
18549           dw_attr_ref new_attr;
18550
18551           pdie = pdie->die_parent;
18552           if (pdie == NULL)
18553             break;
18554           if (BLOCK_SUPERCONTEXT (chain) == NULL_TREE)
18555             break;
18556           new_attr = get_AT (pdie, DW_AT_ranges);
18557           if (new_attr == NULL
18558               || new_attr->dw_attr_val.val_class != dw_val_class_range_list)
18559             break;
18560           attr = new_attr;
18561           superblock = BLOCK_SUPERCONTEXT (chain);
18562         }
18563       if (attr != NULL
18564           && (ranges_table[attr->dw_attr_val.v.val_offset
18565                            / 2 / DWARF2_ADDR_SIZE].num
18566               == BLOCK_NUMBER (superblock))
18567           && BLOCK_FRAGMENT_CHAIN (superblock))
18568         {
18569           unsigned long off = attr->dw_attr_val.v.val_offset
18570                               / 2 / DWARF2_ADDR_SIZE;
18571           unsigned long supercnt = 0, thiscnt = 0;
18572           for (chain = BLOCK_FRAGMENT_CHAIN (superblock);
18573                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18574             {
18575               ++supercnt;
18576               gcc_checking_assert (ranges_table[off + supercnt].num
18577                                    == BLOCK_NUMBER (chain));
18578             }
18579           gcc_checking_assert (ranges_table[off + supercnt + 1].num == 0);
18580           for (chain = BLOCK_FRAGMENT_CHAIN (stmt);
18581                chain; chain = BLOCK_FRAGMENT_CHAIN (chain))
18582             ++thiscnt;
18583           gcc_assert (supercnt >= thiscnt);
18584           add_AT_range_list (die, DW_AT_ranges,
18585                              ((off + supercnt - thiscnt)
18586                               * 2 * DWARF2_ADDR_SIZE),
18587                              false);
18588           return;
18589         }
18590
18591       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt), false);
18592
18593       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18594       do
18595         {
18596           add_ranges (chain);
18597           chain = BLOCK_FRAGMENT_CHAIN (chain);
18598         }
18599       while (chain);
18600       add_ranges (NULL);
18601     }
18602   else
18603     {
18604       char label_high[MAX_ARTIFICIAL_LABEL_BYTES];
18605       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18606                                    BLOCK_NUMBER (stmt));
18607       ASM_GENERATE_INTERNAL_LABEL (label_high, BLOCK_END_LABEL,
18608                                    BLOCK_NUMBER (stmt));
18609       add_AT_low_high_pc (die, label, label_high, false);
18610     }
18611 }
18612
18613 /* Generate a DIE for a lexical block.  */
18614
18615 static void
18616 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18617 {
18618   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18619
18620   if (call_arg_locations)
18621     {
18622       if (block_map.length () <= BLOCK_NUMBER (stmt))
18623         block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18624       block_map[BLOCK_NUMBER (stmt)] = stmt_die;
18625     }
18626
18627   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18628     add_high_low_attributes (stmt, stmt_die);
18629
18630   decls_for_scope (stmt, stmt_die, depth);
18631 }
18632
18633 /* Generate a DIE for an inlined subprogram.  */
18634
18635 static void
18636 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18637 {
18638   tree decl;
18639
18640   /* The instance of function that is effectively being inlined shall not
18641      be abstract.  */
18642   gcc_assert (! BLOCK_ABSTRACT (stmt));
18643
18644   decl = block_ultimate_origin (stmt);
18645
18646   /* Emit info for the abstract instance first, if we haven't yet.  We
18647      must emit this even if the block is abstract, otherwise when we
18648      emit the block below (or elsewhere), we may end up trying to emit
18649      a die whose origin die hasn't been emitted, and crashing.  */
18650   dwarf2out_abstract_function (decl);
18651
18652   if (! BLOCK_ABSTRACT (stmt))
18653     {
18654       dw_die_ref subr_die
18655         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18656
18657       if (call_arg_locations)
18658         {
18659           if (block_map.length () <= BLOCK_NUMBER (stmt))
18660             block_map.safe_grow_cleared (BLOCK_NUMBER (stmt) + 1);
18661           block_map[BLOCK_NUMBER (stmt)] = subr_die;
18662         }
18663       add_abstract_origin_attribute (subr_die, decl);
18664       if (TREE_ASM_WRITTEN (stmt))
18665         add_high_low_attributes (stmt, subr_die);
18666       add_call_src_coords_attributes (stmt, subr_die);
18667
18668       decls_for_scope (stmt, subr_die, depth);
18669       current_function_has_inlines = 1;
18670     }
18671 }
18672
18673 /* Generate a DIE for a field in a record, or structure.  */
18674
18675 static void
18676 gen_field_die (tree decl, dw_die_ref context_die)
18677 {
18678   dw_die_ref decl_die;
18679
18680   if (TREE_TYPE (decl) == error_mark_node)
18681     return;
18682
18683   decl_die = new_die (DW_TAG_member, context_die, decl);
18684   add_name_and_src_coords_attributes (decl_die, decl);
18685   add_type_attribute (decl_die, member_declared_type (decl),
18686                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18687                       context_die);
18688
18689   if (DECL_BIT_FIELD_TYPE (decl))
18690     {
18691       add_byte_size_attribute (decl_die, decl);
18692       add_bit_size_attribute (decl_die, decl);
18693       add_bit_offset_attribute (decl_die, decl);
18694     }
18695
18696   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18697     add_data_member_location_attribute (decl_die, decl);
18698
18699   if (DECL_ARTIFICIAL (decl))
18700     add_AT_flag (decl_die, DW_AT_artificial, 1);
18701
18702   add_accessibility_attribute (decl_die, decl);
18703
18704   /* Equate decl number to die, so that we can look up this decl later on.  */
18705   equate_decl_number_to_die (decl, decl_die);
18706 }
18707
18708 #if 0
18709 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18710    Use modified_type_die instead.
18711    We keep this code here just in case these types of DIEs may be needed to
18712    represent certain things in other languages (e.g. Pascal) someday.  */
18713
18714 static void
18715 gen_pointer_type_die (tree type, dw_die_ref context_die)
18716 {
18717   dw_die_ref ptr_die
18718     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18719
18720   equate_type_number_to_die (type, ptr_die);
18721   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18722   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18723 }
18724
18725 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18726    Use modified_type_die instead.
18727    We keep this code here just in case these types of DIEs may be needed to
18728    represent certain things in other languages (e.g. Pascal) someday.  */
18729
18730 static void
18731 gen_reference_type_die (tree type, dw_die_ref context_die)
18732 {
18733   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18734
18735   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18736     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18737   else
18738     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18739
18740   equate_type_number_to_die (type, ref_die);
18741   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18742   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18743 }
18744 #endif
18745
18746 /* Generate a DIE for a pointer to a member type.  */
18747
18748 static void
18749 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18750 {
18751   dw_die_ref ptr_die
18752     = new_die (DW_TAG_ptr_to_member_type,
18753                scope_die_for (type, context_die), type);
18754
18755   equate_type_number_to_die (type, ptr_die);
18756   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18757                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18758   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18759 }
18760
18761 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18762
18763 static char *producer_string;
18764
18765 /* Return a heap allocated producer string including command line options
18766    if -grecord-gcc-switches.  */
18767
18768 static char *
18769 gen_producer_string (void)
18770 {
18771   size_t j;
18772   vec<dchar_p> switches = vNULL;
18773   const char *language_string = lang_hooks.name;
18774   char *producer, *tail;
18775   const char *p;
18776   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18777   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18778
18779   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18780     switch (save_decoded_options[j].opt_index)
18781       {
18782       case OPT_o:
18783       case OPT_d:
18784       case OPT_dumpbase:
18785       case OPT_dumpdir:
18786       case OPT_auxbase:
18787       case OPT_auxbase_strip:
18788       case OPT_quiet:
18789       case OPT_version:
18790       case OPT_v:
18791       case OPT_w:
18792       case OPT_L:
18793       case OPT_D:
18794       case OPT_I:
18795       case OPT_U:
18796       case OPT_SPECIAL_unknown:
18797       case OPT_SPECIAL_ignore:
18798       case OPT_SPECIAL_program_name:
18799       case OPT_SPECIAL_input_file:
18800       case OPT_grecord_gcc_switches:
18801       case OPT_gno_record_gcc_switches:
18802       case OPT__output_pch_:
18803       case OPT_fdiagnostics_show_location_:
18804       case OPT_fdiagnostics_show_option:
18805       case OPT_fdiagnostics_show_caret:
18806       case OPT_fverbose_asm:
18807       case OPT____:
18808       case OPT__sysroot_:
18809       case OPT_nostdinc:
18810       case OPT_nostdinc__:
18811         /* Ignore these.  */
18812         continue;
18813       default:
18814         if (cl_options[save_decoded_options[j].opt_index].flags
18815             & CL_NO_DWARF_RECORD)
18816           continue;
18817         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18818                              == '-');
18819         switch (save_decoded_options[j].canonical_option[0][1])
18820           {
18821           case 'M':
18822           case 'i':
18823           case 'W':
18824             continue;
18825           case 'f':
18826             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18827                          "dump", 4) == 0)
18828               continue;
18829             break;
18830           default:
18831             break;
18832           }
18833         switches.safe_push (save_decoded_options[j].orig_option_with_args_text);
18834         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18835         break;
18836       }
18837
18838   producer = XNEWVEC (char, plen + 1 + len + 1);
18839   tail = producer;
18840   sprintf (tail, "%s %s", language_string, version_string);
18841   tail += plen;
18842
18843   FOR_EACH_VEC_ELT (switches, j, p)
18844     {
18845       len = strlen (p);
18846       *tail = ' ';
18847       memcpy (tail + 1, p, len);
18848       tail += len + 1;
18849     }
18850
18851   *tail = '\0';
18852   switches.release ();
18853   return producer;
18854 }
18855
18856 /* Generate the DIE for the compilation unit.  */
18857
18858 static dw_die_ref
18859 gen_compile_unit_die (const char *filename)
18860 {
18861   dw_die_ref die;
18862   const char *language_string = lang_hooks.name;
18863   int language;
18864
18865   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18866
18867   if (filename)
18868     {
18869       add_name_attribute (die, filename);
18870       /* Don't add cwd for <built-in>.  */
18871       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18872         add_comp_dir_attribute (die);
18873     }
18874
18875   add_AT_string (die, DW_AT_producer, producer_string ? producer_string : "");
18876
18877   /* If our producer is LTO try to figure out a common language to use
18878      from the global list of translation units.  */
18879   if (strcmp (language_string, "GNU GIMPLE") == 0)
18880     {
18881       unsigned i;
18882       tree t;
18883       const char *common_lang = NULL;
18884
18885       FOR_EACH_VEC_SAFE_ELT (all_translation_units, i, t)
18886         {
18887           if (!TRANSLATION_UNIT_LANGUAGE (t))
18888             continue;
18889           if (!common_lang)
18890             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18891           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18892             ;
18893           else if (strncmp (common_lang, "GNU C", 5) == 0
18894                     && strncmp(TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18895             /* Mixing C and C++ is ok, use C++ in that case.  */
18896             common_lang = "GNU C++";
18897           else
18898             {
18899               /* Fall back to C.  */
18900               common_lang = NULL;
18901               break;
18902             }
18903         }
18904
18905       if (common_lang)
18906         language_string = common_lang;
18907     }
18908
18909   language = DW_LANG_C89;
18910   if (strcmp (language_string, "GNU C++") == 0)
18911     language = DW_LANG_C_plus_plus;
18912   else if (strcmp (language_string, "GNU F77") == 0)
18913     language = DW_LANG_Fortran77;
18914   else if (strcmp (language_string, "GNU Pascal") == 0)
18915     language = DW_LANG_Pascal83;
18916   else if (dwarf_version >= 3 || !dwarf_strict)
18917     {
18918       if (strcmp (language_string, "GNU Ada") == 0)
18919         language = DW_LANG_Ada95;
18920       else if (strcmp (language_string, "GNU Fortran") == 0)
18921         language = DW_LANG_Fortran95;
18922       else if (strcmp (language_string, "GNU Java") == 0)
18923         language = DW_LANG_Java;
18924       else if (strcmp (language_string, "GNU Objective-C") == 0)
18925         language = DW_LANG_ObjC;
18926       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18927         language = DW_LANG_ObjC_plus_plus;
18928       else if (dwarf_version >= 5 || !dwarf_strict)
18929         {
18930           if (strcmp (language_string, "GNU Go") == 0)
18931             language = DW_LANG_Go;
18932         }
18933     }
18934   /* Use a degraded Fortran setting in strict DWARF2 so is_fortran works.  */
18935   else if (strcmp (language_string, "GNU Fortran") == 0)
18936     language = DW_LANG_Fortran90;
18937
18938   add_AT_unsigned (die, DW_AT_language, language);
18939
18940   switch (language)
18941     {
18942     case DW_LANG_Fortran77:
18943     case DW_LANG_Fortran90:
18944     case DW_LANG_Fortran95:
18945       /* Fortran has case insensitive identifiers and the front-end
18946          lowercases everything.  */
18947       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18948       break;
18949     default:
18950       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18951       break;
18952     }
18953   return die;
18954 }
18955
18956 /* Generate the DIE for a base class.  */
18957
18958 static void
18959 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18960 {
18961   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18962
18963   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18964   add_data_member_location_attribute (die, binfo);
18965
18966   if (BINFO_VIRTUAL_P (binfo))
18967     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18968
18969   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18970      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18971      the default has always been DW_ACCESS_private.  */
18972   if (access == access_public_node)
18973     {
18974       if (dwarf_version == 2
18975           || context_die->die_tag == DW_TAG_class_type)
18976       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18977     }
18978   else if (access == access_protected_node)
18979     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18980   else if (dwarf_version > 2
18981            && context_die->die_tag != DW_TAG_class_type)
18982     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18983 }
18984
18985 /* Generate a DIE for a class member.  */
18986
18987 static void
18988 gen_member_die (tree type, dw_die_ref context_die)
18989 {
18990   tree member;
18991   tree binfo = TYPE_BINFO (type);
18992   dw_die_ref child;
18993
18994   /* If this is not an incomplete type, output descriptions of each of its
18995      members. Note that as we output the DIEs necessary to represent the
18996      members of this record or union type, we will also be trying to output
18997      DIEs to represent the *types* of those members. However the `type'
18998      function (above) will specifically avoid generating type DIEs for member
18999      types *within* the list of member DIEs for this (containing) type except
19000      for those types (of members) which are explicitly marked as also being
19001      members of this (containing) type themselves.  The g++ front- end can
19002      force any given type to be treated as a member of some other (containing)
19003      type by setting the TYPE_CONTEXT of the given (member) type to point to
19004      the TREE node representing the appropriate (containing) type.  */
19005
19006   /* First output info about the base classes.  */
19007   if (binfo)
19008     {
19009       vec<tree, va_gc> *accesses = BINFO_BASE_ACCESSES (binfo);
19010       int i;
19011       tree base;
19012
19013       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19014         gen_inheritance_die (base,
19015                              (accesses ? (*accesses)[i] : access_public_node),
19016                              context_die);
19017     }
19018
19019   /* Now output info about the data members and type members.  */
19020   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19021     {
19022       /* If we thought we were generating minimal debug info for TYPE
19023          and then changed our minds, some of the member declarations
19024          may have already been defined.  Don't define them again, but
19025          do put them in the right order.  */
19026
19027       child = lookup_decl_die (member);
19028       if (child)
19029         splice_child_die (context_die, child);
19030       else
19031         gen_decl_die (member, NULL, context_die);
19032     }
19033
19034   /* Now output info about the function members (if any).  */
19035   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19036     {
19037       /* Don't include clones in the member list.  */
19038       if (DECL_ABSTRACT_ORIGIN (member))
19039         continue;
19040
19041       child = lookup_decl_die (member);
19042       if (child)
19043         splice_child_die (context_die, child);
19044       else
19045         gen_decl_die (member, NULL, context_die);
19046     }
19047 }
19048
19049 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19050    is set, we pretend that the type was never defined, so we only get the
19051    member DIEs needed by later specification DIEs.  */
19052
19053 static void
19054 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19055                                 enum debug_info_usage usage)
19056 {
19057   dw_die_ref type_die = lookup_type_die (type);
19058   dw_die_ref scope_die = 0;
19059   int nested = 0;
19060   int complete = (TYPE_SIZE (type)
19061                   && (! TYPE_STUB_DECL (type)
19062                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19063   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19064   complete = complete && should_emit_struct_debug (type, usage);
19065
19066   if (type_die && ! complete)
19067     return;
19068
19069   if (TYPE_CONTEXT (type) != NULL_TREE
19070       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19071           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19072     nested = 1;
19073
19074   scope_die = scope_die_for (type, context_die);
19075
19076   /* Generate child dies for template paramaters.  */
19077   if (!type_die && debug_info_level > DINFO_LEVEL_TERSE)
19078     schedule_generic_params_dies_gen (type);
19079
19080   if (! type_die || (nested && is_cu_die (scope_die)))
19081     /* First occurrence of type or toplevel definition of nested class.  */
19082     {
19083       dw_die_ref old_die = type_die;
19084
19085       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19086                           ? record_type_tag (type) : DW_TAG_union_type,
19087                           scope_die, type);
19088       equate_type_number_to_die (type, type_die);
19089       if (old_die)
19090         add_AT_specification (type_die, old_die);
19091       else
19092         add_name_attribute (type_die, type_tag (type));
19093     }
19094   else
19095     remove_AT (type_die, DW_AT_declaration);
19096
19097   /* If this type has been completed, then give it a byte_size attribute and
19098      then give a list of members.  */
19099   if (complete && !ns_decl)
19100     {
19101       /* Prevent infinite recursion in cases where the type of some member of
19102          this type is expressed in terms of this type itself.  */
19103       TREE_ASM_WRITTEN (type) = 1;
19104       add_byte_size_attribute (type_die, type);
19105       if (TYPE_STUB_DECL (type) != NULL_TREE)
19106         {
19107           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19108           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19109         }
19110
19111       /* If the first reference to this type was as the return type of an
19112          inline function, then it may not have a parent.  Fix this now.  */
19113       if (type_die->die_parent == NULL)
19114         add_child_die (scope_die, type_die);
19115
19116       push_decl_scope (type);
19117       gen_member_die (type, type_die);
19118       pop_decl_scope ();
19119
19120       add_gnat_descriptive_type_attribute (type_die, type, context_die);
19121       if (TYPE_ARTIFICIAL (type))
19122         add_AT_flag (type_die, DW_AT_artificial, 1);
19123
19124       /* GNU extension: Record what type our vtable lives in.  */
19125       if (TYPE_VFIELD (type))
19126         {
19127           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19128
19129           gen_type_die (vtype, context_die);
19130           add_AT_die_ref (type_die, DW_AT_containing_type,
19131                           lookup_type_die (vtype));
19132         }
19133     }
19134   else
19135     {
19136       add_AT_flag (type_die, DW_AT_declaration, 1);
19137
19138       /* We don't need to do this for function-local types.  */
19139       if (TYPE_STUB_DECL (type)
19140           && ! decl_function_context (TYPE_STUB_DECL (type)))
19141         vec_safe_push (incomplete_types, type);
19142     }
19143
19144   if (get_AT (type_die, DW_AT_name))
19145     add_pubtype (type, type_die);
19146 }
19147
19148 /* Generate a DIE for a subroutine _type_.  */
19149
19150 static void
19151 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19152 {
19153   tree return_type = TREE_TYPE (type);
19154   dw_die_ref subr_die
19155     = new_die (DW_TAG_subroutine_type,
19156                scope_die_for (type, context_die), type);
19157
19158   equate_type_number_to_die (type, subr_die);
19159   add_prototyped_attribute (subr_die, type);
19160   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19161   gen_formal_types_die (type, subr_die);
19162
19163   if (get_AT (subr_die, DW_AT_name))
19164     add_pubtype (type, subr_die);
19165 }
19166
19167 /* Generate a DIE for a type definition.  */
19168
19169 static void
19170 gen_typedef_die (tree decl, dw_die_ref context_die)
19171 {
19172   dw_die_ref type_die;
19173   tree origin;
19174
19175   if (TREE_ASM_WRITTEN (decl))
19176     return;
19177
19178   TREE_ASM_WRITTEN (decl) = 1;
19179   type_die = new_die (DW_TAG_typedef, context_die, decl);
19180   origin = decl_ultimate_origin (decl);
19181   if (origin != NULL)
19182     add_abstract_origin_attribute (type_die, origin);
19183   else
19184     {
19185       tree type;
19186
19187       add_name_and_src_coords_attributes (type_die, decl);
19188       if (DECL_ORIGINAL_TYPE (decl))
19189         {
19190           type = DECL_ORIGINAL_TYPE (decl);
19191
19192           gcc_assert (type != TREE_TYPE (decl));
19193           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19194         }
19195       else
19196         {
19197           type = TREE_TYPE (decl);
19198
19199           if (is_naming_typedef_decl (TYPE_NAME (type)))
19200             {
19201               /* Here, we are in the case of decl being a typedef naming
19202                  an anonymous type, e.g:
19203                      typedef struct {...} foo;
19204                  In that case TREE_TYPE (decl) is not a typedef variant
19205                  type and TYPE_NAME of the anonymous type is set to the
19206                  TYPE_DECL of the typedef. This construct is emitted by
19207                  the C++ FE.
19208
19209                  TYPE is the anonymous struct named by the typedef
19210                  DECL. As we need the DW_AT_type attribute of the
19211                  DW_TAG_typedef to point to the DIE of TYPE, let's
19212                  generate that DIE right away. add_type_attribute
19213                  called below will then pick (via lookup_type_die) that
19214                  anonymous struct DIE.  */
19215               if (!TREE_ASM_WRITTEN (type))
19216                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19217
19218               /* This is a GNU Extension.  We are adding a
19219                  DW_AT_linkage_name attribute to the DIE of the
19220                  anonymous struct TYPE.  The value of that attribute
19221                  is the name of the typedef decl naming the anonymous
19222                  struct.  This greatly eases the work of consumers of
19223                  this debug info.  */
19224               add_linkage_attr (lookup_type_die (type), decl);
19225             }
19226         }
19227
19228       add_type_attribute (type_die, type, TREE_READONLY (decl),
19229                           TREE_THIS_VOLATILE (decl), context_die);
19230
19231       if (is_naming_typedef_decl (decl))
19232         /* We want that all subsequent calls to lookup_type_die with
19233            TYPE in argument yield the DW_TAG_typedef we have just
19234            created.  */
19235         equate_type_number_to_die (type, type_die);
19236
19237       add_accessibility_attribute (type_die, decl);
19238     }
19239
19240   if (DECL_ABSTRACT (decl))
19241     equate_decl_number_to_die (decl, type_die);
19242
19243   if (get_AT (type_die, DW_AT_name))
19244     add_pubtype (decl, type_die);
19245 }
19246
19247 /* Generate a DIE for a struct, class, enum or union type.  */
19248
19249 static void
19250 gen_tagged_type_die (tree type,
19251                      dw_die_ref context_die,
19252                      enum debug_info_usage usage)
19253 {
19254   int need_pop;
19255
19256   if (type == NULL_TREE
19257       || !is_tagged_type (type))
19258     return;
19259
19260   /* If this is a nested type whose containing class hasn't been written
19261      out yet, writing it out will cover this one, too.  This does not apply
19262      to instantiations of member class templates; they need to be added to
19263      the containing class as they are generated.  FIXME: This hurts the
19264      idea of combining type decls from multiple TUs, since we can't predict
19265      what set of template instantiations we'll get.  */
19266   if (TYPE_CONTEXT (type)
19267       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19268       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19269     {
19270       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19271
19272       if (TREE_ASM_WRITTEN (type))
19273         return;
19274
19275       /* If that failed, attach ourselves to the stub.  */
19276       push_decl_scope (TYPE_CONTEXT (type));
19277       context_die = lookup_type_die (TYPE_CONTEXT (type));
19278       need_pop = 1;
19279     }
19280   else if (TYPE_CONTEXT (type) != NULL_TREE
19281            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19282     {
19283       /* If this type is local to a function that hasn't been written
19284          out yet, use a NULL context for now; it will be fixed up in
19285          decls_for_scope.  */
19286       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19287       /* A declaration DIE doesn't count; nested types need to go in the
19288          specification.  */
19289       if (context_die && is_declaration_die (context_die))
19290         context_die = NULL;
19291       need_pop = 0;
19292     }
19293   else
19294     {
19295       context_die = declare_in_namespace (type, context_die);
19296       need_pop = 0;
19297     }
19298
19299   if (TREE_CODE (type) == ENUMERAL_TYPE)
19300     {
19301       /* This might have been written out by the call to
19302          declare_in_namespace.  */
19303       if (!TREE_ASM_WRITTEN (type))
19304         gen_enumeration_type_die (type, context_die);
19305     }
19306   else
19307     gen_struct_or_union_type_die (type, context_die, usage);
19308
19309   if (need_pop)
19310     pop_decl_scope ();
19311
19312   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19313      it up if it is ever completed.  gen_*_type_die will set it for us
19314      when appropriate.  */
19315 }
19316
19317 /* Generate a type description DIE.  */
19318
19319 static void
19320 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19321                          enum debug_info_usage usage)
19322 {
19323   struct array_descr_info info;
19324
19325   if (type == NULL_TREE || type == error_mark_node)
19326     return;
19327
19328   if (TYPE_NAME (type) != NULL_TREE
19329       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19330       && is_redundant_typedef (TYPE_NAME (type))
19331       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19332     /* The DECL of this type is a typedef we don't want to emit debug
19333        info for but we want debug info for its underlying typedef.
19334        This can happen for e.g, the injected-class-name of a C++
19335        type.  */
19336     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19337
19338   /* If TYPE is a typedef type variant, let's generate debug info
19339      for the parent typedef which TYPE is a type of.  */
19340   if (typedef_variant_p (type))
19341     {
19342       if (TREE_ASM_WRITTEN (type))
19343         return;
19344
19345       /* Prevent broken recursion; we can't hand off to the same type.  */
19346       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19347
19348       /* Give typedefs the right scope.  */
19349       context_die = scope_die_for (type, context_die);
19350
19351       TREE_ASM_WRITTEN (type) = 1;
19352
19353       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19354       return;
19355     }
19356
19357   /* If type is an anonymous tagged type named by a typedef, let's
19358      generate debug info for the typedef.  */
19359   if (is_naming_typedef_decl (TYPE_NAME (type)))
19360     {
19361       /* Use the DIE of the containing namespace as the parent DIE of
19362          the type description DIE we want to generate.  */
19363       if (DECL_CONTEXT (TYPE_NAME (type))
19364           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19365         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19366       
19367       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19368       return;
19369     }
19370
19371   /* If this is an array type with hidden descriptor, handle it first.  */
19372   if (!TREE_ASM_WRITTEN (type)
19373       && lang_hooks.types.get_array_descr_info
19374       && lang_hooks.types.get_array_descr_info (type, &info)
19375       && (dwarf_version >= 3 || !dwarf_strict))
19376     {
19377       gen_descr_array_type_die (type, &info, context_die);
19378       TREE_ASM_WRITTEN (type) = 1;
19379       return;
19380     }
19381
19382   /* We are going to output a DIE to represent the unqualified version
19383      of this type (i.e. without any const or volatile qualifiers) so
19384      get the main variant (i.e. the unqualified version) of this type
19385      now.  (Vectors are special because the debugging info is in the
19386      cloned type itself).  */
19387   if (TREE_CODE (type) != VECTOR_TYPE)
19388     type = type_main_variant (type);
19389
19390   if (TREE_ASM_WRITTEN (type))
19391     return;
19392
19393   switch (TREE_CODE (type))
19394     {
19395     case ERROR_MARK:
19396       break;
19397
19398     case POINTER_TYPE:
19399     case REFERENCE_TYPE:
19400       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19401          ensures that the gen_type_die recursion will terminate even if the
19402          type is recursive.  Recursive types are possible in Ada.  */
19403       /* ??? We could perhaps do this for all types before the switch
19404          statement.  */
19405       TREE_ASM_WRITTEN (type) = 1;
19406
19407       /* For these types, all that is required is that we output a DIE (or a
19408          set of DIEs) to represent the "basis" type.  */
19409       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19410                                 DINFO_USAGE_IND_USE);
19411       break;
19412
19413     case OFFSET_TYPE:
19414       /* This code is used for C++ pointer-to-data-member types.
19415          Output a description of the relevant class type.  */
19416       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19417                                         DINFO_USAGE_IND_USE);
19418
19419       /* Output a description of the type of the object pointed to.  */
19420       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19421                                         DINFO_USAGE_IND_USE);
19422
19423       /* Now output a DIE to represent this pointer-to-data-member type
19424          itself.  */
19425       gen_ptr_to_mbr_type_die (type, context_die);
19426       break;
19427
19428     case FUNCTION_TYPE:
19429       /* Force out return type (in case it wasn't forced out already).  */
19430       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19431                                         DINFO_USAGE_DIR_USE);
19432       gen_subroutine_type_die (type, context_die);
19433       break;
19434
19435     case METHOD_TYPE:
19436       /* Force out return type (in case it wasn't forced out already).  */
19437       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19438                                         DINFO_USAGE_DIR_USE);
19439       gen_subroutine_type_die (type, context_die);
19440       break;
19441
19442     case ARRAY_TYPE:
19443       gen_array_type_die (type, context_die);
19444       break;
19445
19446     case VECTOR_TYPE:
19447       gen_array_type_die (type, context_die);
19448       break;
19449
19450     case ENUMERAL_TYPE:
19451     case RECORD_TYPE:
19452     case UNION_TYPE:
19453     case QUAL_UNION_TYPE:
19454       gen_tagged_type_die (type, context_die, usage);
19455       return;
19456
19457     case VOID_TYPE:
19458     case INTEGER_TYPE:
19459     case REAL_TYPE:
19460     case FIXED_POINT_TYPE:
19461     case COMPLEX_TYPE:
19462     case BOOLEAN_TYPE:
19463       /* No DIEs needed for fundamental types.  */
19464       break;
19465
19466     case NULLPTR_TYPE:
19467     case LANG_TYPE:
19468       /* Just use DW_TAG_unspecified_type.  */
19469       {
19470         dw_die_ref type_die = lookup_type_die (type);
19471         if (type_die == NULL)
19472           {
19473             tree name = TYPE_NAME (type);
19474             if (TREE_CODE (name) == TYPE_DECL)
19475               name = DECL_NAME (name);
19476             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19477             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19478             equate_type_number_to_die (type, type_die);
19479           }
19480       }
19481       break;
19482
19483     default:
19484       gcc_unreachable ();
19485     }
19486
19487   TREE_ASM_WRITTEN (type) = 1;
19488 }
19489
19490 static void
19491 gen_type_die (tree type, dw_die_ref context_die)
19492 {
19493   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19494 }
19495
19496 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19497    things which are local to the given block.  */
19498
19499 static void
19500 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19501 {
19502   int must_output_die = 0;
19503   bool inlined_func;
19504
19505   /* Ignore blocks that are NULL.  */
19506   if (stmt == NULL_TREE)
19507     return;
19508
19509   inlined_func = inlined_function_outer_scope_p (stmt);
19510
19511   /* If the block is one fragment of a non-contiguous block, do not
19512      process the variables, since they will have been done by the
19513      origin block.  Do process subblocks.  */
19514   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19515     {
19516       tree sub;
19517
19518       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19519         gen_block_die (sub, context_die, depth + 1);
19520
19521       return;
19522     }
19523
19524   /* Determine if we need to output any Dwarf DIEs at all to represent this
19525      block.  */
19526   if (inlined_func)
19527     /* The outer scopes for inlinings *must* always be represented.  We
19528        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19529     must_output_die = 1;
19530   else
19531     {
19532       /* Determine if this block directly contains any "significant"
19533          local declarations which we will need to output DIEs for.  */
19534       if (debug_info_level > DINFO_LEVEL_TERSE)
19535         /* We are not in terse mode so *any* local declaration counts
19536            as being a "significant" one.  */
19537         must_output_die = ((BLOCK_VARS (stmt) != NULL
19538                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19539                            && (TREE_USED (stmt)
19540                                || TREE_ASM_WRITTEN (stmt)
19541                                || BLOCK_ABSTRACT (stmt)));
19542       else if ((TREE_USED (stmt)
19543                 || TREE_ASM_WRITTEN (stmt)
19544                 || BLOCK_ABSTRACT (stmt))
19545                && !dwarf2out_ignore_block (stmt))
19546         must_output_die = 1;
19547     }
19548
19549   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19550      DIE for any block which contains no significant local declarations at
19551      all.  Rather, in such cases we just call `decls_for_scope' so that any
19552      needed Dwarf info for any sub-blocks will get properly generated. Note
19553      that in terse mode, our definition of what constitutes a "significant"
19554      local declaration gets restricted to include only inlined function
19555      instances and local (nested) function definitions.  */
19556   if (must_output_die)
19557     {
19558       if (inlined_func)
19559         {
19560           /* If STMT block is abstract, that means we have been called
19561              indirectly from dwarf2out_abstract_function.
19562              That function rightfully marks the descendent blocks (of
19563              the abstract function it is dealing with) as being abstract,
19564              precisely to prevent us from emitting any
19565              DW_TAG_inlined_subroutine DIE as a descendent
19566              of an abstract function instance. So in that case, we should
19567              not call gen_inlined_subroutine_die.
19568
19569              Later though, when cgraph asks dwarf2out to emit info
19570              for the concrete instance of the function decl into which
19571              the concrete instance of STMT got inlined, the later will lead
19572              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19573           if (! BLOCK_ABSTRACT (stmt))
19574             gen_inlined_subroutine_die (stmt, context_die, depth);
19575         }
19576       else
19577         gen_lexical_block_die (stmt, context_die, depth);
19578     }
19579   else
19580     decls_for_scope (stmt, context_die, depth);
19581 }
19582
19583 /* Process variable DECL (or variable with origin ORIGIN) within
19584    block STMT and add it to CONTEXT_DIE.  */
19585 static void
19586 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19587 {
19588   dw_die_ref die;
19589   tree decl_or_origin = decl ? decl : origin;
19590
19591   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19592     die = lookup_decl_die (decl_or_origin);
19593   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19594            && TYPE_DECL_IS_STUB (decl_or_origin))
19595     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19596   else
19597     die = NULL;
19598
19599   if (die != NULL && die->die_parent == NULL)
19600     add_child_die (context_die, die);
19601   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19602     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19603                                          stmt, context_die);
19604   else
19605     gen_decl_die (decl, origin, context_die);
19606 }
19607
19608 /* Generate all of the decls declared within a given scope and (recursively)
19609    all of its sub-blocks.  */
19610
19611 static void
19612 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19613 {
19614   tree decl;
19615   unsigned int i;
19616   tree subblocks;
19617
19618   /* Ignore NULL blocks.  */
19619   if (stmt == NULL_TREE)
19620     return;
19621
19622   /* Output the DIEs to represent all of the data objects and typedefs
19623      declared directly within this block but not within any nested
19624      sub-blocks.  Also, nested function and tag DIEs have been
19625      generated with a parent of NULL; fix that up now.  */
19626   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19627     process_scope_var (stmt, decl, NULL_TREE, context_die);
19628   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19629     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19630                        context_die);
19631
19632   /* If we're at -g1, we're not interested in subblocks.  */
19633   if (debug_info_level <= DINFO_LEVEL_TERSE)
19634     return;
19635
19636   /* Output the DIEs to represent all sub-blocks (and the items declared
19637      therein) of this block.  */
19638   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19639        subblocks != NULL;
19640        subblocks = BLOCK_CHAIN (subblocks))
19641     gen_block_die (subblocks, context_die, depth + 1);
19642 }
19643
19644 /* Is this a typedef we can avoid emitting?  */
19645
19646 static inline int
19647 is_redundant_typedef (const_tree decl)
19648 {
19649   if (TYPE_DECL_IS_STUB (decl))
19650     return 1;
19651
19652   if (DECL_ARTIFICIAL (decl)
19653       && DECL_CONTEXT (decl)
19654       && is_tagged_type (DECL_CONTEXT (decl))
19655       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19656       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19657     /* Also ignore the artificial member typedef for the class name.  */
19658     return 1;
19659
19660   return 0;
19661 }
19662
19663 /* Return TRUE if TYPE is a typedef that names a type for linkage
19664    purposes. This kind of typedefs is produced by the C++ FE for
19665    constructs like:
19666
19667    typedef struct {...} foo;
19668
19669    In that case, there is no typedef variant type produced for foo.
19670    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19671    struct type.  */
19672
19673 static bool
19674 is_naming_typedef_decl (const_tree decl)
19675 {
19676   if (decl == NULL_TREE
19677       || TREE_CODE (decl) != TYPE_DECL
19678       || !is_tagged_type (TREE_TYPE (decl))
19679       || DECL_IS_BUILTIN (decl)
19680       || is_redundant_typedef (decl)
19681       /* It looks like Ada produces TYPE_DECLs that are very similar
19682          to C++ naming typedefs but that have different
19683          semantics. Let's be specific to c++ for now.  */
19684       || !is_cxx ())
19685     return FALSE;
19686
19687   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19688           && TYPE_NAME (TREE_TYPE (decl)) == decl
19689           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19690               != TYPE_NAME (TREE_TYPE (decl))));
19691 }
19692
19693 /* Returns the DIE for a context.  */
19694
19695 static inline dw_die_ref
19696 get_context_die (tree context)
19697 {
19698   if (context)
19699     {
19700       /* Find die that represents this context.  */
19701       if (TYPE_P (context))
19702         {
19703           context = TYPE_MAIN_VARIANT (context);
19704           return strip_naming_typedef (context, force_type_die (context));
19705         }
19706       else
19707         return force_decl_die (context);
19708     }
19709   return comp_unit_die ();
19710 }
19711
19712 /* Returns the DIE for decl.  A DIE will always be returned.  */
19713
19714 static dw_die_ref
19715 force_decl_die (tree decl)
19716 {
19717   dw_die_ref decl_die;
19718   unsigned saved_external_flag;
19719   tree save_fn = NULL_TREE;
19720   decl_die = lookup_decl_die (decl);
19721   if (!decl_die)
19722     {
19723       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19724
19725       decl_die = lookup_decl_die (decl);
19726       if (decl_die)
19727         return decl_die;
19728
19729       switch (TREE_CODE (decl))
19730         {
19731         case FUNCTION_DECL:
19732           /* Clear current_function_decl, so that gen_subprogram_die thinks
19733              that this is a declaration. At this point, we just want to force
19734              declaration die.  */
19735           save_fn = current_function_decl;
19736           current_function_decl = NULL_TREE;
19737           gen_subprogram_die (decl, context_die);
19738           current_function_decl = save_fn;
19739           break;
19740
19741         case VAR_DECL:
19742           /* Set external flag to force declaration die. Restore it after
19743            gen_decl_die() call.  */
19744           saved_external_flag = DECL_EXTERNAL (decl);
19745           DECL_EXTERNAL (decl) = 1;
19746           gen_decl_die (decl, NULL, context_die);
19747           DECL_EXTERNAL (decl) = saved_external_flag;
19748           break;
19749
19750         case NAMESPACE_DECL:
19751           if (dwarf_version >= 3 || !dwarf_strict)
19752             dwarf2out_decl (decl);
19753           else
19754             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19755             decl_die = comp_unit_die ();
19756           break;
19757
19758         case TRANSLATION_UNIT_DECL:
19759           decl_die = comp_unit_die ();
19760           break;
19761
19762         default:
19763           gcc_unreachable ();
19764         }
19765
19766       /* We should be able to find the DIE now.  */
19767       if (!decl_die)
19768         decl_die = lookup_decl_die (decl);
19769       gcc_assert (decl_die);
19770     }
19771
19772   return decl_die;
19773 }
19774
19775 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19776    always returned.  */
19777
19778 static dw_die_ref
19779 force_type_die (tree type)
19780 {
19781   dw_die_ref type_die;
19782
19783   type_die = lookup_type_die (type);
19784   if (!type_die)
19785     {
19786       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19787
19788       type_die = modified_type_die (type, TYPE_READONLY (type),
19789                                     TYPE_VOLATILE (type), context_die);
19790       gcc_assert (type_die);
19791     }
19792   return type_die;
19793 }
19794
19795 /* Force out any required namespaces to be able to output DECL,
19796    and return the new context_die for it, if it's changed.  */
19797
19798 static dw_die_ref
19799 setup_namespace_context (tree thing, dw_die_ref context_die)
19800 {
19801   tree context = (DECL_P (thing)
19802                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19803   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19804     /* Force out the namespace.  */
19805     context_die = force_decl_die (context);
19806
19807   return context_die;
19808 }
19809
19810 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19811    type) within its namespace, if appropriate.
19812
19813    For compatibility with older debuggers, namespace DIEs only contain
19814    declarations; all definitions are emitted at CU scope.  */
19815
19816 static dw_die_ref
19817 declare_in_namespace (tree thing, dw_die_ref context_die)
19818 {
19819   dw_die_ref ns_context;
19820
19821   if (debug_info_level <= DINFO_LEVEL_TERSE)
19822     return context_die;
19823
19824   /* If this decl is from an inlined function, then don't try to emit it in its
19825      namespace, as we will get confused.  It would have already been emitted
19826      when the abstract instance of the inline function was emitted anyways.  */
19827   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19828     return context_die;
19829
19830   ns_context = setup_namespace_context (thing, context_die);
19831
19832   if (ns_context != context_die)
19833     {
19834       if (is_fortran ())
19835         return ns_context;
19836       if (DECL_P (thing))
19837         gen_decl_die (thing, NULL, ns_context);
19838       else
19839         gen_type_die (thing, ns_context);
19840     }
19841   return context_die;
19842 }
19843
19844 /* Generate a DIE for a namespace or namespace alias.  */
19845
19846 static void
19847 gen_namespace_die (tree decl, dw_die_ref context_die)
19848 {
19849   dw_die_ref namespace_die;
19850
19851   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19852      they are an alias of.  */
19853   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19854     {
19855       /* Output a real namespace or module.  */
19856       context_die = setup_namespace_context (decl, comp_unit_die ());
19857       namespace_die = new_die (is_fortran ()
19858                                ? DW_TAG_module : DW_TAG_namespace,
19859                                context_die, decl);
19860       /* For Fortran modules defined in different CU don't add src coords.  */
19861       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19862         {
19863           const char *name = dwarf2_name (decl, 0);
19864           if (name)
19865             add_name_attribute (namespace_die, name);
19866         }
19867       else
19868         add_name_and_src_coords_attributes (namespace_die, decl);
19869       if (DECL_EXTERNAL (decl))
19870         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19871       equate_decl_number_to_die (decl, namespace_die);
19872     }
19873   else
19874     {
19875       /* Output a namespace alias.  */
19876
19877       /* Force out the namespace we are an alias of, if necessary.  */
19878       dw_die_ref origin_die
19879         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19880
19881       if (DECL_FILE_SCOPE_P (decl)
19882           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19883         context_die = setup_namespace_context (decl, comp_unit_die ());
19884       /* Now create the namespace alias DIE.  */
19885       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19886       add_name_and_src_coords_attributes (namespace_die, decl);
19887       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19888       equate_decl_number_to_die (decl, namespace_die);
19889     }
19890   /* Bypass dwarf2_name's check for DECL_NAMELESS.  */
19891   if (want_pubnames ())
19892     add_pubname_string (lang_hooks.dwarf_name (decl, 1), namespace_die);
19893 }
19894
19895 /* Generate Dwarf debug information for a decl described by DECL.
19896    The return value is currently only meaningful for PARM_DECLs,
19897    for all other decls it returns NULL.  */
19898
19899 static dw_die_ref
19900 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19901 {
19902   tree decl_or_origin = decl ? decl : origin;
19903   tree class_origin = NULL, ultimate_origin;
19904
19905   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19906     return NULL;
19907
19908   switch (TREE_CODE (decl_or_origin))
19909     {
19910     case ERROR_MARK:
19911       break;
19912
19913     case CONST_DECL:
19914       if (!is_fortran () && !is_ada ())
19915         {
19916           /* The individual enumerators of an enum type get output when we output
19917              the Dwarf representation of the relevant enum type itself.  */
19918           break;
19919         }
19920
19921       /* Emit its type.  */
19922       gen_type_die (TREE_TYPE (decl), context_die);
19923
19924       /* And its containing namespace.  */
19925       context_die = declare_in_namespace (decl, context_die);
19926
19927       gen_const_die (decl, context_die);
19928       break;
19929
19930     case FUNCTION_DECL:
19931       /* Don't output any DIEs to represent mere function declarations,
19932          unless they are class members or explicit block externs.  */
19933       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19934           && DECL_FILE_SCOPE_P (decl_or_origin)
19935           && (current_function_decl == NULL_TREE
19936               || DECL_ARTIFICIAL (decl_or_origin)))
19937         break;
19938
19939 #if 0
19940       /* FIXME */
19941       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19942          on local redeclarations of global functions.  That seems broken.  */
19943       if (current_function_decl != decl)
19944         /* This is only a declaration.  */;
19945 #endif
19946
19947       /* If we're emitting a clone, emit info for the abstract instance.  */
19948       if (origin || DECL_ORIGIN (decl) != decl)
19949         dwarf2out_abstract_function (origin
19950                                      ? DECL_ORIGIN (origin)
19951                                      : DECL_ABSTRACT_ORIGIN (decl));
19952
19953       /* If we're emitting an out-of-line copy of an inline function,
19954          emit info for the abstract instance and set up to refer to it.  */
19955       else if (cgraph_function_possibly_inlined_p (decl)
19956                && ! DECL_ABSTRACT (decl)
19957                && ! class_or_namespace_scope_p (context_die)
19958                /* dwarf2out_abstract_function won't emit a die if this is just
19959                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19960                   that case, because that works only if we have a die.  */
19961                && DECL_INITIAL (decl) != NULL_TREE)
19962         {
19963           dwarf2out_abstract_function (decl);
19964           set_decl_origin_self (decl);
19965         }
19966
19967       /* Otherwise we're emitting the primary DIE for this decl.  */
19968       else if (debug_info_level > DINFO_LEVEL_TERSE)
19969         {
19970           /* Before we describe the FUNCTION_DECL itself, make sure that we
19971              have its containing type.  */
19972           if (!origin)
19973             origin = decl_class_context (decl);
19974           if (origin != NULL_TREE)
19975             gen_type_die (origin, context_die);
19976
19977           /* And its return type.  */
19978           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19979
19980           /* And its virtual context.  */
19981           if (DECL_VINDEX (decl) != NULL_TREE)
19982             gen_type_die (DECL_CONTEXT (decl), context_die);
19983
19984           /* Make sure we have a member DIE for decl.  */
19985           if (origin != NULL_TREE)
19986             gen_type_die_for_member (origin, decl, context_die);
19987
19988           /* And its containing namespace.  */
19989           context_die = declare_in_namespace (decl, context_die);
19990         }
19991
19992       /* Now output a DIE to represent the function itself.  */
19993       if (decl)
19994         gen_subprogram_die (decl, context_die);
19995       break;
19996
19997     case TYPE_DECL:
19998       /* If we are in terse mode, don't generate any DIEs to represent any
19999          actual typedefs.  */
20000       if (debug_info_level <= DINFO_LEVEL_TERSE)
20001         break;
20002
20003       /* In the special case of a TYPE_DECL node representing the declaration
20004          of some type tag, if the given TYPE_DECL is marked as having been
20005          instantiated from some other (original) TYPE_DECL node (e.g. one which
20006          was generated within the original definition of an inline function) we
20007          used to generate a special (abbreviated) DW_TAG_structure_type,
20008          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20009          should be actually referencing those DIEs, as variable DIEs with that
20010          type would be emitted already in the abstract origin, so it was always
20011          removed during unused type prunning.  Don't add anything in this
20012          case.  */
20013       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20014         break;
20015
20016       if (is_redundant_typedef (decl))
20017         gen_type_die (TREE_TYPE (decl), context_die);
20018       else
20019         /* Output a DIE to represent the typedef itself.  */
20020         gen_typedef_die (decl, context_die);
20021       break;
20022
20023     case LABEL_DECL:
20024       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20025         gen_label_die (decl, context_die);
20026       break;
20027
20028     case VAR_DECL:
20029     case RESULT_DECL:
20030       /* If we are in terse mode, don't generate any DIEs to represent any
20031          variable declarations or definitions.  */
20032       if (debug_info_level <= DINFO_LEVEL_TERSE)
20033         break;
20034
20035       /* Output any DIEs that are needed to specify the type of this data
20036          object.  */
20037       if (decl_by_reference_p (decl_or_origin))
20038         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20039       else
20040         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20041
20042       /* And its containing type.  */
20043       class_origin = decl_class_context (decl_or_origin);
20044       if (class_origin != NULL_TREE)
20045         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20046
20047       /* And its containing namespace.  */
20048       context_die = declare_in_namespace (decl_or_origin, context_die);
20049
20050       /* Now output the DIE to represent the data object itself.  This gets
20051          complicated because of the possibility that the VAR_DECL really
20052          represents an inlined instance of a formal parameter for an inline
20053          function.  */
20054       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20055       if (ultimate_origin != NULL_TREE
20056           && TREE_CODE (ultimate_origin) == PARM_DECL)
20057         gen_formal_parameter_die (decl, origin,
20058                                   true /* Emit name attribute.  */,
20059                                   context_die);
20060       else
20061         gen_variable_die (decl, origin, context_die);
20062       break;
20063
20064     case FIELD_DECL:
20065       /* Ignore the nameless fields that are used to skip bits but handle C++
20066          anonymous unions and structs.  */
20067       if (DECL_NAME (decl) != NULL_TREE
20068           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20069           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20070         {
20071           gen_type_die (member_declared_type (decl), context_die);
20072           gen_field_die (decl, context_die);
20073         }
20074       break;
20075
20076     case PARM_DECL:
20077       if (DECL_BY_REFERENCE (decl_or_origin))
20078         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20079       else
20080         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20081       return gen_formal_parameter_die (decl, origin,
20082                                        true /* Emit name attribute.  */,
20083                                        context_die);
20084
20085     case NAMESPACE_DECL:
20086     case IMPORTED_DECL:
20087       if (dwarf_version >= 3 || !dwarf_strict)
20088         gen_namespace_die (decl, context_die);
20089       break;
20090
20091     default:
20092       /* Probably some frontend-internal decl.  Assume we don't care.  */
20093       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20094       break;
20095     }
20096
20097   return NULL;
20098 }
20099 \f
20100 /* Output debug information for global decl DECL.  Called from toplev.c after
20101    compilation proper has finished.  */
20102
20103 static void
20104 dwarf2out_global_decl (tree decl)
20105 {
20106   /* Output DWARF2 information for file-scope tentative data object
20107      declarations, file-scope (extern) function declarations (which
20108      had no corresponding body) and file-scope tagged type declarations
20109      and definitions which have not yet been forced out.  */
20110   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20111     dwarf2out_decl (decl);
20112 }
20113
20114 /* Output debug information for type decl DECL.  Called from toplev.c
20115    and from language front ends (to record built-in types).  */
20116 static void
20117 dwarf2out_type_decl (tree decl, int local)
20118 {
20119   if (!local)
20120     dwarf2out_decl (decl);
20121 }
20122
20123 /* Output debug information for imported module or decl DECL.
20124    NAME is non-NULL name in the lexical block if the decl has been renamed.
20125    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20126    that DECL belongs to.
20127    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20128 static void
20129 dwarf2out_imported_module_or_decl_1 (tree decl,
20130                                      tree name,
20131                                      tree lexical_block,
20132                                      dw_die_ref lexical_block_die)
20133 {
20134   expanded_location xloc;
20135   dw_die_ref imported_die = NULL;
20136   dw_die_ref at_import_die;
20137
20138   if (TREE_CODE (decl) == IMPORTED_DECL)
20139     {
20140       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20141       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20142       gcc_assert (decl);
20143     }
20144   else
20145     xloc = expand_location (input_location);
20146
20147   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20148     {
20149       at_import_die = force_type_die (TREE_TYPE (decl));
20150       /* For namespace N { typedef void T; } using N::T; base_type_die
20151          returns NULL, but DW_TAG_imported_declaration requires
20152          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20153       if (!at_import_die)
20154         {
20155           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20156           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20157           at_import_die = lookup_type_die (TREE_TYPE (decl));
20158           gcc_assert (at_import_die);
20159         }
20160     }
20161   else
20162     {
20163       at_import_die = lookup_decl_die (decl);
20164       if (!at_import_die)
20165         {
20166           /* If we're trying to avoid duplicate debug info, we may not have
20167              emitted the member decl for this field.  Emit it now.  */
20168           if (TREE_CODE (decl) == FIELD_DECL)
20169             {
20170               tree type = DECL_CONTEXT (decl);
20171
20172               if (TYPE_CONTEXT (type)
20173                   && TYPE_P (TYPE_CONTEXT (type))
20174                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20175                                                 DINFO_USAGE_DIR_USE))
20176                 return;
20177               gen_type_die_for_member (type, decl,
20178                                        get_context_die (TYPE_CONTEXT (type)));
20179             }
20180           at_import_die = force_decl_die (decl);
20181         }
20182     }
20183
20184   if (TREE_CODE (decl) == NAMESPACE_DECL)
20185     {
20186       if (dwarf_version >= 3 || !dwarf_strict)
20187         imported_die = new_die (DW_TAG_imported_module,
20188                                 lexical_block_die,
20189                                 lexical_block);
20190       else
20191         return;
20192     }
20193   else
20194     imported_die = new_die (DW_TAG_imported_declaration,
20195                             lexical_block_die,
20196                             lexical_block);
20197
20198   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20199   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20200   if (name)
20201     add_AT_string (imported_die, DW_AT_name,
20202                    IDENTIFIER_POINTER (name));
20203   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20204 }
20205
20206 /* Output debug information for imported module or decl DECL.
20207    NAME is non-NULL name in context if the decl has been renamed.
20208    CHILD is true if decl is one of the renamed decls as part of
20209    importing whole module.  */
20210
20211 static void
20212 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20213                                    bool child)
20214 {
20215   /* dw_die_ref at_import_die;  */
20216   dw_die_ref scope_die;
20217
20218   if (debug_info_level <= DINFO_LEVEL_TERSE)
20219     return;
20220
20221   gcc_assert (decl);
20222
20223   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20224      We need decl DIE for reference and scope die. First, get DIE for the decl
20225      itself.  */
20226
20227   /* Get the scope die for decl context. Use comp_unit_die for global module
20228      or decl. If die is not found for non globals, force new die.  */
20229   if (context
20230       && TYPE_P (context)
20231       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20232     return;
20233
20234   if (!(dwarf_version >= 3 || !dwarf_strict))
20235     return;
20236
20237   scope_die = get_context_die (context);
20238
20239   if (child)
20240     {
20241       gcc_assert (scope_die->die_child);
20242       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20243       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20244       scope_die = scope_die->die_child;
20245     }
20246
20247   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20248   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20249
20250 }
20251
20252 /* Write the debugging output for DECL.  */
20253
20254 void
20255 dwarf2out_decl (tree decl)
20256 {
20257   dw_die_ref context_die = comp_unit_die ();
20258
20259   switch (TREE_CODE (decl))
20260     {
20261     case ERROR_MARK:
20262       return;
20263
20264     case FUNCTION_DECL:
20265       /* What we would really like to do here is to filter out all mere
20266          file-scope declarations of file-scope functions which are never
20267          referenced later within this translation unit (and keep all of ones
20268          that *are* referenced later on) but we aren't clairvoyant, so we have
20269          no idea which functions will be referenced in the future (i.e. later
20270          on within the current translation unit). So here we just ignore all
20271          file-scope function declarations which are not also definitions.  If
20272          and when the debugger needs to know something about these functions,
20273          it will have to hunt around and find the DWARF information associated
20274          with the definition of the function.
20275
20276          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20277          nodes represent definitions and which ones represent mere
20278          declarations.  We have to check DECL_INITIAL instead. That's because
20279          the C front-end supports some weird semantics for "extern inline"
20280          function definitions.  These can get inlined within the current
20281          translation unit (and thus, we need to generate Dwarf info for their
20282          abstract instances so that the Dwarf info for the concrete inlined
20283          instances can have something to refer to) but the compiler never
20284          generates any out-of-lines instances of such things (despite the fact
20285          that they *are* definitions).
20286
20287          The important point is that the C front-end marks these "extern
20288          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20289          them anyway. Note that the C++ front-end also plays some similar games
20290          for inline function definitions appearing within include files which
20291          also contain `#pragma interface' pragmas.
20292
20293          If we are called from dwarf2out_abstract_function output a DIE
20294          anyway.  We can end up here this way with early inlining and LTO
20295          where the inlined function is output in a different LTRANS unit
20296          or not at all.  */
20297       if (DECL_INITIAL (decl) == NULL_TREE
20298           && ! DECL_ABSTRACT (decl))
20299         return;
20300
20301       /* If we're a nested function, initially use a parent of NULL; if we're
20302          a plain function, this will be fixed up in decls_for_scope.  If
20303          we're a method, it will be ignored, since we already have a DIE.  */
20304       if (decl_function_context (decl)
20305           /* But if we're in terse mode, we don't care about scope.  */
20306           && debug_info_level > DINFO_LEVEL_TERSE)
20307         context_die = NULL;
20308       break;
20309
20310     case VAR_DECL:
20311       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20312          declaration and if the declaration was never even referenced from
20313          within this entire compilation unit.  We suppress these DIEs in
20314          order to save space in the .debug section (by eliminating entries
20315          which are probably useless).  Note that we must not suppress
20316          block-local extern declarations (whether used or not) because that
20317          would screw-up the debugger's name lookup mechanism and cause it to
20318          miss things which really ought to be in scope at a given point.  */
20319       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20320         return;
20321
20322       /* For local statics lookup proper context die.  */
20323       if (TREE_STATIC (decl)
20324           && DECL_CONTEXT (decl)
20325           && TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
20326         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20327
20328       /* If we are in terse mode, don't generate any DIEs to represent any
20329          variable declarations or definitions.  */
20330       if (debug_info_level <= DINFO_LEVEL_TERSE)
20331         return;
20332       break;
20333
20334     case CONST_DECL:
20335       if (debug_info_level <= DINFO_LEVEL_TERSE)
20336         return;
20337       if (!is_fortran () && !is_ada ())
20338         return;
20339       if (TREE_STATIC (decl) && decl_function_context (decl))
20340         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20341       break;
20342
20343     case NAMESPACE_DECL:
20344     case IMPORTED_DECL:
20345       if (debug_info_level <= DINFO_LEVEL_TERSE)
20346         return;
20347       if (lookup_decl_die (decl) != NULL)
20348         return;
20349       break;
20350
20351     case TYPE_DECL:
20352       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20353       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20354         return;
20355
20356       /* Don't bother trying to generate any DIEs to represent any of the
20357          normal built-in types for the language we are compiling.  */
20358       if (DECL_IS_BUILTIN (decl))
20359         return;
20360
20361       /* If we are in terse mode, don't generate any DIEs for types.  */
20362       if (debug_info_level <= DINFO_LEVEL_TERSE)
20363         return;
20364
20365       /* If we're a function-scope tag, initially use a parent of NULL;
20366          this will be fixed up in decls_for_scope.  */
20367       if (decl_function_context (decl))
20368         context_die = NULL;
20369
20370       break;
20371
20372     default:
20373       return;
20374     }
20375
20376   gen_decl_die (decl, NULL, context_die);
20377 }
20378
20379 /* Write the debugging output for DECL.  */
20380
20381 static void
20382 dwarf2out_function_decl (tree decl)
20383 {
20384   dwarf2out_decl (decl);
20385   call_arg_locations = NULL;
20386   call_arg_loc_last = NULL;
20387   call_site_count = -1;
20388   tail_call_site_count = -1;
20389   block_map.release ();
20390   htab_empty (decl_loc_table);
20391   htab_empty (cached_dw_loc_list_table);
20392 }
20393
20394 /* Output a marker (i.e. a label) for the beginning of the generated code for
20395    a lexical block.  */
20396
20397 static void
20398 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20399                        unsigned int blocknum)
20400 {
20401   switch_to_section (current_function_section ());
20402   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20403 }
20404
20405 /* Output a marker (i.e. a label) for the end of the generated code for a
20406    lexical block.  */
20407
20408 static void
20409 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20410 {
20411   switch_to_section (current_function_section ());
20412   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20413 }
20414
20415 /* Returns nonzero if it is appropriate not to emit any debugging
20416    information for BLOCK, because it doesn't contain any instructions.
20417
20418    Don't allow this for blocks with nested functions or local classes
20419    as we would end up with orphans, and in the presence of scheduling
20420    we may end up calling them anyway.  */
20421
20422 static bool
20423 dwarf2out_ignore_block (const_tree block)
20424 {
20425   tree decl;
20426   unsigned int i;
20427
20428   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20429     if (TREE_CODE (decl) == FUNCTION_DECL
20430         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20431       return 0;
20432   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20433     {
20434       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20435       if (TREE_CODE (decl) == FUNCTION_DECL
20436           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20437       return 0;
20438     }
20439
20440   return 1;
20441 }
20442
20443 /* Hash table routines for file_hash.  */
20444
20445 static int
20446 file_table_eq (const void *p1_p, const void *p2_p)
20447 {
20448   const struct dwarf_file_data *const p1 =
20449     (const struct dwarf_file_data *) p1_p;
20450   const char *const p2 = (const char *) p2_p;
20451   return filename_cmp (p1->filename, p2) == 0;
20452 }
20453
20454 static hashval_t
20455 file_table_hash (const void *p_p)
20456 {
20457   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20458   return htab_hash_string (p->filename);
20459 }
20460
20461 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20462    dwarf2out.c) and return its "index".  The index of each (known) filename is
20463    just a unique number which is associated with only that one filename.  We
20464    need such numbers for the sake of generating labels (in the .debug_sfnames
20465    section) and references to those files numbers (in the .debug_srcinfo
20466    and.debug_macinfo sections).  If the filename given as an argument is not
20467    found in our current list, add it to the list and assign it the next
20468    available unique index number.  In order to speed up searches, we remember
20469    the index of the filename was looked up last.  This handles the majority of
20470    all searches.  */
20471
20472 static struct dwarf_file_data *
20473 lookup_filename (const char *file_name)
20474 {
20475   void ** slot;
20476   struct dwarf_file_data * created;
20477
20478   /* Check to see if the file name that was searched on the previous
20479      call matches this file name.  If so, return the index.  */
20480   if (file_table_last_lookup
20481       && (file_name == file_table_last_lookup->filename
20482           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20483     return file_table_last_lookup;
20484
20485   /* Didn't match the previous lookup, search the table.  */
20486   slot = htab_find_slot_with_hash (file_table, file_name,
20487                                    htab_hash_string (file_name), INSERT);
20488   if (*slot)
20489     return (struct dwarf_file_data *) *slot;
20490
20491   created = ggc_alloc_dwarf_file_data ();
20492   created->filename = file_name;
20493   created->emitted_number = 0;
20494   *slot = created;
20495   return created;
20496 }
20497
20498 /* If the assembler will construct the file table, then translate the compiler
20499    internal file table number into the assembler file table number, and emit
20500    a .file directive if we haven't already emitted one yet.  The file table
20501    numbers are different because we prune debug info for unused variables and
20502    types, which may include filenames.  */
20503
20504 static int
20505 maybe_emit_file (struct dwarf_file_data * fd)
20506 {
20507   if (! fd->emitted_number)
20508     {
20509       if (last_emitted_file)
20510         fd->emitted_number = last_emitted_file->emitted_number + 1;
20511       else
20512         fd->emitted_number = 1;
20513       last_emitted_file = fd;
20514
20515       if (DWARF2_ASM_LINE_DEBUG_INFO)
20516         {
20517           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20518           output_quoted_string (asm_out_file,
20519                                 remap_debug_filename (fd->filename));
20520           fputc ('\n', asm_out_file);
20521         }
20522     }
20523
20524   return fd->emitted_number;
20525 }
20526
20527 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20528    That generation should happen after function debug info has been
20529    generated. The value of the attribute is the constant value of ARG.  */
20530
20531 static void
20532 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20533 {
20534   die_arg_entry entry;
20535
20536   if (!die || !arg)
20537     return;
20538
20539   if (!tmpl_value_parm_die_table)
20540     vec_alloc (tmpl_value_parm_die_table, 32);
20541
20542   entry.die = die;
20543   entry.arg = arg;
20544   vec_safe_push (tmpl_value_parm_die_table, entry);
20545 }
20546
20547 /* Return TRUE if T is an instance of generic type, FALSE
20548    otherwise.  */
20549
20550 static bool
20551 generic_type_p (tree t)
20552 {
20553   if (t == NULL_TREE || !TYPE_P (t))
20554     return false;
20555   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20556 }
20557
20558 /* Schedule the generation of the generic parameter dies for the
20559   instance of generic type T. The proper generation itself is later
20560   done by gen_scheduled_generic_parms_dies. */
20561
20562 static void
20563 schedule_generic_params_dies_gen (tree t)
20564 {
20565   if (!generic_type_p (t))
20566     return;
20567
20568   if (!generic_type_instances)
20569     vec_alloc (generic_type_instances, 256);
20570
20571   vec_safe_push (generic_type_instances, t);
20572 }
20573
20574 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20575    by append_entry_to_tmpl_value_parm_die_table. This function must
20576    be called after function DIEs have been generated.  */
20577
20578 static void
20579 gen_remaining_tmpl_value_param_die_attribute (void)
20580 {
20581   if (tmpl_value_parm_die_table)
20582     {
20583       unsigned i;
20584       die_arg_entry *e;
20585
20586       FOR_EACH_VEC_ELT (*tmpl_value_parm_die_table, i, e)
20587         tree_add_const_value_attribute (e->die, e->arg);
20588     }
20589 }
20590
20591 /* Generate generic parameters DIEs for instances of generic types
20592    that have been previously scheduled by
20593    schedule_generic_params_dies_gen. This function must be called
20594    after all the types of the CU have been laid out.  */
20595
20596 static void
20597 gen_scheduled_generic_parms_dies (void)
20598 {
20599   unsigned i;
20600   tree t;
20601
20602   if (!generic_type_instances)
20603     return;
20604   
20605   FOR_EACH_VEC_ELT (*generic_type_instances, i, t)
20606     if (COMPLETE_TYPE_P (t))
20607       gen_generic_params_dies (t);
20608 }
20609
20610
20611 /* Replace DW_AT_name for the decl with name.  */
20612
20613 static void
20614 dwarf2out_set_name (tree decl, tree name)
20615 {
20616   dw_die_ref die;
20617   dw_attr_ref attr;
20618   const char *dname;
20619
20620   die = TYPE_SYMTAB_DIE (decl);
20621   if (!die)
20622     return;
20623
20624   dname = dwarf2_name (name, 0);
20625   if (!dname)
20626     return;
20627
20628   attr = get_AT (die, DW_AT_name);
20629   if (attr)
20630     {
20631       struct indirect_string_node *node;
20632
20633       node = find_AT_string (dname);
20634       /* replace the string.  */
20635       attr->dw_attr_val.v.val_str = node;
20636     }
20637
20638   else
20639     add_name_attribute (die, dname);
20640 }
20641
20642 /* True if before or during processing of the first function being emitted.  */
20643 static bool in_first_function_p = true;
20644 /* True if loc_note during dwarf2out_var_location call might still be
20645    before first real instruction at address equal to .Ltext0.  */
20646 static bool maybe_at_text_label_p = true;
20647 /* One above highest N where .LVLN label might be equal to .Ltext0 label.  */
20648 static unsigned int first_loclabel_num_not_at_text_label;
20649
20650 /* Called by the final INSN scan whenever we see a var location.  We
20651    use it to drop labels in the right places, and throw the location in
20652    our lookup table.  */
20653
20654 static void
20655 dwarf2out_var_location (rtx loc_note)
20656 {
20657   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20658   struct var_loc_node *newloc;
20659   rtx next_real, next_note;
20660   static const char *last_label;
20661   static const char *last_postcall_label;
20662   static bool last_in_cold_section_p;
20663   static rtx expected_next_loc_note;
20664   tree decl;
20665   bool var_loc_p;
20666
20667   if (!NOTE_P (loc_note))
20668     {
20669       if (CALL_P (loc_note))
20670         {
20671           call_site_count++;
20672           if (SIBLING_CALL_P (loc_note))
20673             tail_call_site_count++;
20674         }
20675       return;
20676     }
20677
20678   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20679   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20680     return;
20681
20682   /* Optimize processing a large consecutive sequence of location
20683      notes so we don't spend too much time in next_real_insn.  If the
20684      next insn is another location note, remember the next_real_insn
20685      calculation for next time.  */
20686   next_real = cached_next_real_insn;
20687   if (next_real)
20688     {
20689       if (expected_next_loc_note != loc_note)
20690         next_real = NULL_RTX;
20691     }
20692
20693   next_note = NEXT_INSN (loc_note);
20694   if (! next_note
20695       || INSN_DELETED_P (next_note)
20696       || GET_CODE (next_note) != NOTE
20697       || (NOTE_KIND (next_note) != NOTE_INSN_VAR_LOCATION
20698           && NOTE_KIND (next_note) != NOTE_INSN_CALL_ARG_LOCATION))
20699     next_note = NULL_RTX;
20700
20701   if (! next_real)
20702     next_real = next_real_insn (loc_note);
20703
20704   if (next_note)
20705     {
20706       expected_next_loc_note = next_note;
20707       cached_next_real_insn = next_real;
20708     }
20709   else
20710     cached_next_real_insn = NULL_RTX;
20711
20712   /* If there are no instructions which would be affected by this note,
20713      don't do anything.  */
20714   if (var_loc_p
20715       && next_real == NULL_RTX
20716       && !NOTE_DURING_CALL_P (loc_note))
20717     return;
20718
20719   if (next_real == NULL_RTX)
20720     next_real = get_last_insn ();
20721
20722   /* If there were any real insns between note we processed last time
20723      and this note (or if it is the first note), clear
20724      last_{,postcall_}label so that they are not reused this time.  */
20725   if (last_var_location_insn == NULL_RTX
20726       || last_var_location_insn != next_real
20727       || last_in_cold_section_p != in_cold_section_p)
20728     {
20729       last_label = NULL;
20730       last_postcall_label = NULL;
20731     }
20732
20733   if (var_loc_p)
20734     {
20735       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20736       newloc = add_var_loc_to_decl (decl, loc_note,
20737                                     NOTE_DURING_CALL_P (loc_note)
20738                                     ? last_postcall_label : last_label);
20739       if (newloc == NULL)
20740         return;
20741     }
20742   else
20743     {
20744       decl = NULL_TREE;
20745       newloc = NULL;
20746     }
20747
20748   /* If there were no real insns between note we processed last time
20749      and this note, use the label we emitted last time.  Otherwise
20750      create a new label and emit it.  */
20751   if (last_label == NULL)
20752     {
20753       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20754       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20755       loclabel_num++;
20756       last_label = ggc_strdup (loclabel);
20757       /* See if loclabel might be equal to .Ltext0.  If yes,
20758          bump first_loclabel_num_not_at_text_label.  */
20759       if (!have_multiple_function_sections
20760           && in_first_function_p
20761           && maybe_at_text_label_p)
20762         {
20763           static rtx last_start;
20764           rtx insn;
20765           for (insn = loc_note; insn; insn = previous_insn (insn))
20766             if (insn == last_start)
20767               break;
20768             else if (!NONDEBUG_INSN_P (insn))
20769               continue;
20770             else
20771               {
20772                 rtx body = PATTERN (insn);
20773                 if (GET_CODE (body) == USE || GET_CODE (body) == CLOBBER)
20774                   continue;
20775                 /* Inline asm could occupy zero bytes.  */
20776                 else if (GET_CODE (body) == ASM_INPUT
20777                          || asm_noperands (body) >= 0)
20778                   continue;
20779 #ifdef HAVE_attr_length
20780                 else if (get_attr_min_length (insn) == 0)
20781                   continue;
20782 #endif
20783                 else
20784                   {
20785                     /* Assume insn has non-zero length.  */
20786                     maybe_at_text_label_p = false;
20787                     break;
20788                   }
20789               }
20790           if (maybe_at_text_label_p)
20791             {
20792               last_start = loc_note;
20793               first_loclabel_num_not_at_text_label = loclabel_num;
20794             }
20795         }
20796     }
20797
20798   if (!var_loc_p)
20799     {
20800       struct call_arg_loc_node *ca_loc
20801         = ggc_alloc_cleared_call_arg_loc_node ();
20802       rtx prev = prev_real_insn (loc_note), x;
20803       ca_loc->call_arg_loc_note = loc_note;
20804       ca_loc->next = NULL;
20805       ca_loc->label = last_label;
20806       gcc_assert (prev
20807                   && (CALL_P (prev)
20808                       || (NONJUMP_INSN_P (prev)
20809                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20810                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20811       if (!CALL_P (prev))
20812         prev = XVECEXP (PATTERN (prev), 0, 0);
20813       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20814       x = get_call_rtx_from (PATTERN (prev));
20815       if (x)
20816         {
20817           x = XEXP (XEXP (x, 0), 0);
20818           if (GET_CODE (x) == SYMBOL_REF
20819               && SYMBOL_REF_DECL (x)
20820               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20821             ca_loc->symbol_ref = x;
20822         }
20823       ca_loc->block = insn_scope (prev);
20824       if (call_arg_locations)
20825         call_arg_loc_last->next = ca_loc;
20826       else
20827         call_arg_locations = ca_loc;
20828       call_arg_loc_last = ca_loc;
20829     }
20830   else if (!NOTE_DURING_CALL_P (loc_note))
20831     newloc->label = last_label;
20832   else
20833     {
20834       if (!last_postcall_label)
20835         {
20836           sprintf (loclabel, "%s-1", last_label);
20837           last_postcall_label = ggc_strdup (loclabel);
20838         }
20839       newloc->label = last_postcall_label;
20840     }
20841
20842   last_var_location_insn = next_real;
20843   last_in_cold_section_p = in_cold_section_p;
20844 }
20845
20846 /* Note in one location list that text section has changed.  */
20847
20848 static int
20849 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20850 {
20851   var_loc_list *list = (var_loc_list *) *slot;
20852   if (list->first)
20853     list->last_before_switch
20854       = list->last->next ? list->last->next : list->last;
20855   return 1;
20856 }
20857
20858 /* Note in all location lists that text section has changed.  */
20859
20860 static void
20861 var_location_switch_text_section (void)
20862 {
20863   if (decl_loc_table == NULL)
20864     return;
20865
20866   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20867 }
20868
20869 /* Create a new line number table.  */
20870
20871 static dw_line_info_table *
20872 new_line_info_table (void)
20873 {
20874   dw_line_info_table *table;
20875
20876   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20877   table->file_num = 1;
20878   table->line_num = 1;
20879   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20880
20881   return table;
20882 }
20883
20884 /* Lookup the "current" table into which we emit line info, so
20885    that we don't have to do it for every source line.  */
20886
20887 static void
20888 set_cur_line_info_table (section *sec)
20889 {
20890   dw_line_info_table *table;
20891
20892   if (sec == text_section)
20893     table = text_section_line_info;
20894   else if (sec == cold_text_section)
20895     {
20896       table = cold_text_section_line_info;
20897       if (!table)
20898         {
20899           cold_text_section_line_info = table = new_line_info_table ();
20900           table->end_label = cold_end_label;
20901         }
20902     }
20903   else
20904     {
20905       const char *end_label;
20906
20907       if (flag_reorder_blocks_and_partition)
20908         {
20909           if (in_cold_section_p)
20910             end_label = crtl->subsections.cold_section_end_label;
20911           else
20912             end_label = crtl->subsections.hot_section_end_label;
20913         }
20914       else
20915         {
20916           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20917           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20918                                        current_function_funcdef_no);
20919           end_label = ggc_strdup (label);
20920         }
20921
20922       table = new_line_info_table ();
20923       table->end_label = end_label;
20924
20925       vec_safe_push (separate_line_info, table);
20926     }
20927
20928   if (DWARF2_ASM_LINE_DEBUG_INFO)
20929     table->is_stmt = (cur_line_info_table
20930                       ? cur_line_info_table->is_stmt
20931                       : DWARF_LINE_DEFAULT_IS_STMT_START);
20932   cur_line_info_table = table;
20933 }
20934
20935
20936 /* We need to reset the locations at the beginning of each
20937    function. We can't do this in the end_function hook, because the
20938    declarations that use the locations won't have been output when
20939    that hook is called.  Also compute have_multiple_function_sections here.  */
20940
20941 static void
20942 dwarf2out_begin_function (tree fun)
20943 {
20944   section *sec = function_section (fun);
20945
20946   if (sec != text_section)
20947     have_multiple_function_sections = true;
20948
20949   if (flag_reorder_blocks_and_partition && !cold_text_section)
20950     {
20951       gcc_assert (current_function_decl == fun);
20952       cold_text_section = unlikely_text_section ();
20953       switch_to_section (cold_text_section);
20954       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20955       switch_to_section (sec);
20956     }
20957
20958   dwarf2out_note_section_used ();
20959   call_site_count = 0;
20960   tail_call_site_count = 0;
20961
20962   set_cur_line_info_table (sec);
20963 }
20964
20965 /* Helper function of dwarf2out_end_function, called only after emitting
20966    the very first function into assembly.  Check if some .debug_loc range
20967    might end with a .LVL* label that could be equal to .Ltext0.
20968    In that case we must force using absolute addresses in .debug_loc ranges,
20969    because this range could be .LVLN-.Ltext0 .. .LVLM-.Ltext0 for
20970    .LVLN == .LVLM == .Ltext0, thus 0 .. 0, which is a .debug_loc
20971    list terminator.
20972    Set have_multiple_function_sections to true in that case and
20973    terminate htab traversal.  */
20974
20975 static int
20976 find_empty_loc_ranges_at_text_label (void **slot, void *)
20977 {
20978   var_loc_list *entry;
20979   struct var_loc_node *node;
20980
20981   entry = (var_loc_list *) *slot;
20982   node = entry->first;
20983   if (node && node->next && node->next->label)
20984     {
20985       unsigned int i;
20986       const char *label = node->next->label;
20987       char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
20988
20989       for (i = 0; i < first_loclabel_num_not_at_text_label; i++)
20990         {
20991           ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", i);
20992           if (strcmp (label, loclabel) == 0)
20993             {
20994               have_multiple_function_sections = true;
20995               return 0;
20996             }
20997         }
20998     }
20999   return 1;
21000 }
21001
21002 /* Hook called after emitting a function into assembly.
21003    This does something only for the very first function emitted.  */
21004
21005 static void
21006 dwarf2out_end_function (unsigned int)
21007 {
21008   if (in_first_function_p
21009       && !have_multiple_function_sections
21010       && first_loclabel_num_not_at_text_label
21011       && decl_loc_table)
21012     htab_traverse (decl_loc_table, find_empty_loc_ranges_at_text_label,
21013                    NULL);
21014   in_first_function_p = false;
21015   maybe_at_text_label_p = false;
21016 }
21017
21018 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
21019
21020 static void
21021 push_dw_line_info_entry (dw_line_info_table *table,
21022                          enum dw_line_info_opcode opcode, unsigned int val)
21023 {
21024   dw_line_info_entry e;
21025   e.opcode = opcode;
21026   e.val = val;
21027   vec_safe_push (table->entries, e);
21028 }
21029
21030 /* Output a label to mark the beginning of a source code line entry
21031    and record information relating to this source line, in
21032    'line_info_table' for later output of the .debug_line section.  */
21033 /* ??? The discriminator parameter ought to be unsigned.  */
21034
21035 static void
21036 dwarf2out_source_line (unsigned int line, const char *filename,
21037                        int discriminator, bool is_stmt)
21038 {
21039   unsigned int file_num;
21040   dw_line_info_table *table;
21041
21042   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
21043     return;
21044
21045   /* The discriminator column was added in dwarf4.  Simplify the below
21046      by simply removing it if we're not supposed to output it.  */
21047   if (dwarf_version < 4 && dwarf_strict)
21048     discriminator = 0;
21049
21050   table = cur_line_info_table;
21051   file_num = maybe_emit_file (lookup_filename (filename));
21052
21053   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
21054      the debugger has used the second (possibly duplicate) line number
21055      at the beginning of the function to mark the end of the prologue.
21056      We could eliminate any other duplicates within the function.  For
21057      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
21058      that second line number entry.  */
21059   /* Recall that this end-of-prologue indication is *not* the same thing
21060      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
21061      to which the hook corresponds, follows the last insn that was 
21062      emitted by gen_prologue.  What we need is to precede the first insn
21063      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
21064      insn that corresponds to something the user wrote.  These may be
21065      very different locations once scheduling is enabled.  */
21066
21067   if (0 && file_num == table->file_num
21068       && line == table->line_num
21069       && discriminator == table->discrim_num
21070       && is_stmt == table->is_stmt)
21071     return;
21072
21073   switch_to_section (current_function_section ());
21074
21075   /* If requested, emit something human-readable.  */
21076   if (flag_debug_asm)
21077     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
21078
21079   if (DWARF2_ASM_LINE_DEBUG_INFO)
21080     {
21081       /* Emit the .loc directive understood by GNU as.  */
21082       /* "\t.loc %u %u 0 is_stmt %u discriminator %u",
21083          file_num, line, is_stmt, discriminator */
21084       fputs ("\t.loc ", asm_out_file);
21085       fprint_ul (asm_out_file, file_num);
21086       putc (' ', asm_out_file);
21087       fprint_ul (asm_out_file, line);
21088       putc (' ', asm_out_file);
21089       putc ('0', asm_out_file);
21090
21091       if (is_stmt != table->is_stmt)
21092         {
21093           fputs (" is_stmt ", asm_out_file);
21094           putc (is_stmt ? '1' : '0', asm_out_file);
21095         }
21096       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21097         {
21098           gcc_assert (discriminator > 0);
21099           fputs (" discriminator ", asm_out_file);
21100           fprint_ul (asm_out_file, (unsigned long) discriminator);
21101         }
21102       putc ('\n', asm_out_file);
21103     }
21104   else
21105     {
21106       unsigned int label_num = ++line_info_label_num;
21107
21108       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
21109
21110       push_dw_line_info_entry (table, LI_set_address, label_num);
21111       if (file_num != table->file_num)
21112         push_dw_line_info_entry (table, LI_set_file, file_num);
21113       if (discriminator != table->discrim_num)
21114         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
21115       if (is_stmt != table->is_stmt)
21116         push_dw_line_info_entry (table, LI_negate_stmt, 0);
21117       push_dw_line_info_entry (table, LI_set_line, line);
21118     }
21119
21120   table->file_num = file_num;
21121   table->line_num = line;
21122   table->discrim_num = discriminator;
21123   table->is_stmt = is_stmt;
21124   table->in_use = true;
21125 }
21126
21127 /* Record the beginning of a new source file.  */
21128
21129 static void
21130 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21131 {
21132   if (flag_eliminate_dwarf2_dups)
21133     {
21134       /* Record the beginning of the file for break_out_includes.  */
21135       dw_die_ref bincl_die;
21136
21137       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
21138       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21139     }
21140
21141   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21142     {
21143       macinfo_entry e;
21144       e.code = DW_MACINFO_start_file;
21145       e.lineno = lineno;
21146       e.info = ggc_strdup (filename);
21147       vec_safe_push (macinfo_table, e);
21148     }
21149 }
21150
21151 /* Record the end of a source file.  */
21152
21153 static void
21154 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21155 {
21156   if (flag_eliminate_dwarf2_dups)
21157     /* Record the end of the file for break_out_includes.  */
21158     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
21159
21160   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21161     {
21162       macinfo_entry e;
21163       e.code = DW_MACINFO_end_file;
21164       e.lineno = lineno;
21165       e.info = NULL;
21166       vec_safe_push (macinfo_table, e);
21167     }
21168 }
21169
21170 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21171    the tail part of the directive line, i.e. the part which is past the
21172    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21173
21174 static void
21175 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21176                   const char *buffer ATTRIBUTE_UNUSED)
21177 {
21178   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21179     {
21180       macinfo_entry e;
21181       /* Insert a dummy first entry to be able to optimize the whole
21182          predefined macro block using DW_MACRO_GNU_transparent_include.  */
21183       if (macinfo_table->is_empty () && lineno <= 1)
21184         {
21185           e.code = 0;
21186           e.lineno = 0;
21187           e.info = NULL;
21188           vec_safe_push (macinfo_table, e);
21189         }
21190       e.code = DW_MACINFO_define;
21191       e.lineno = lineno;
21192       e.info = ggc_strdup (buffer);
21193       vec_safe_push (macinfo_table, e);
21194     }
21195 }
21196
21197 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21198    the tail part of the directive line, i.e. the part which is past the
21199    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21200
21201 static void
21202 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21203                  const char *buffer ATTRIBUTE_UNUSED)
21204 {
21205   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21206     {
21207       macinfo_entry e;
21208       /* Insert a dummy first entry to be able to optimize the whole
21209          predefined macro block using DW_MACRO_GNU_transparent_include.  */
21210       if (macinfo_table->is_empty () && lineno <= 1)
21211         {
21212           e.code = 0;
21213           e.lineno = 0;
21214           e.info = NULL;
21215           vec_safe_push (macinfo_table, e);
21216         }
21217       e.code = DW_MACINFO_undef;
21218       e.lineno = lineno;
21219       e.info = ggc_strdup (buffer);
21220       vec_safe_push (macinfo_table, e);
21221     }
21222 }
21223
21224 /* Routines to manipulate hash table of CUs.  */
21225
21226 static hashval_t
21227 htab_macinfo_hash (const void *of)
21228 {
21229   const macinfo_entry *const entry =
21230     (const macinfo_entry *) of;
21231
21232   return htab_hash_string (entry->info);
21233 }
21234
21235 static int
21236 htab_macinfo_eq (const void *of1, const void *of2)
21237 {
21238   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
21239   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
21240
21241   return !strcmp (entry1->info, entry2->info);
21242 }
21243
21244 /* Output a single .debug_macinfo entry.  */
21245
21246 static void
21247 output_macinfo_op (macinfo_entry *ref)
21248 {
21249   int file_num;
21250   size_t len;
21251   struct indirect_string_node *node;
21252   char label[MAX_ARTIFICIAL_LABEL_BYTES];
21253   struct dwarf_file_data *fd;
21254
21255   switch (ref->code)
21256     {
21257     case DW_MACINFO_start_file:
21258       fd = lookup_filename (ref->info);
21259       file_num = maybe_emit_file (fd);
21260       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21261       dw2_asm_output_data_uleb128 (ref->lineno,
21262                                    "Included from line number %lu", 
21263                                    (unsigned long) ref->lineno);
21264       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
21265       break;
21266     case DW_MACINFO_end_file:
21267       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21268       break;
21269     case DW_MACINFO_define:
21270     case DW_MACINFO_undef:
21271       len = strlen (ref->info) + 1;
21272       if (!dwarf_strict
21273           && len > DWARF_OFFSET_SIZE
21274           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21275           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21276         {
21277           ref->code = ref->code == DW_MACINFO_define
21278                       ? DW_MACRO_GNU_define_indirect
21279                       : DW_MACRO_GNU_undef_indirect;
21280           output_macinfo_op (ref);
21281           return;
21282         }
21283       dw2_asm_output_data (1, ref->code,
21284                            ref->code == DW_MACINFO_define
21285                            ? "Define macro" : "Undefine macro");
21286       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
21287                                    (unsigned long) ref->lineno);
21288       dw2_asm_output_nstring (ref->info, -1, "The macro");
21289       break;
21290     case DW_MACRO_GNU_define_indirect:
21291     case DW_MACRO_GNU_undef_indirect:
21292       node = find_AT_string (ref->info);
21293       gcc_assert (node
21294                   && ((node->form == DW_FORM_strp)
21295                       || (node->form == DW_FORM_GNU_str_index)));
21296       dw2_asm_output_data (1, ref->code,
21297                            ref->code == DW_MACRO_GNU_define_indirect
21298                            ? "Define macro indirect"
21299                            : "Undefine macro indirect");
21300       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
21301                                    (unsigned long) ref->lineno);
21302       if (node->form == DW_FORM_strp)
21303         dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
21304                                debug_str_section, "The macro: \"%s\"",
21305                                ref->info);
21306       else
21307         dw2_asm_output_data_uleb128 (node->index, "The macro: \"%s\"",
21308                                      ref->info);
21309       break;
21310     case DW_MACRO_GNU_transparent_include:
21311       dw2_asm_output_data (1, ref->code, "Transparent include");
21312       ASM_GENERATE_INTERNAL_LABEL (label,
21313                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
21314       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
21315       break;
21316     default:
21317       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
21318                ASM_COMMENT_START, (unsigned long) ref->code);
21319       break;
21320     }
21321 }
21322
21323 /* Attempt to make a sequence of define/undef macinfo ops shareable with
21324    other compilation unit .debug_macinfo sections.  IDX is the first
21325    index of a define/undef, return the number of ops that should be
21326    emitted in a comdat .debug_macinfo section and emit
21327    a DW_MACRO_GNU_transparent_include entry referencing it.
21328    If the define/undef entry should be emitted normally, return 0.  */
21329
21330 static unsigned
21331 optimize_macinfo_range (unsigned int idx, vec<macinfo_entry, va_gc> *files,
21332                         htab_t *macinfo_htab)
21333 {
21334   macinfo_entry *first, *second, *cur, *inc;
21335   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
21336   unsigned char checksum[16];
21337   struct md5_ctx ctx;
21338   char *grp_name, *tail;
21339   const char *base;
21340   unsigned int i, count, encoded_filename_len, linebuf_len;
21341   void **slot;
21342
21343   first = &(*macinfo_table)[idx];
21344   second = &(*macinfo_table)[idx + 1];
21345
21346   /* Optimize only if there are at least two consecutive define/undef ops,
21347      and either all of them are before first DW_MACINFO_start_file
21348      with lineno {0,1} (i.e. predefined macro block), or all of them are
21349      in some included header file.  */
21350   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
21351     return 0;
21352   if (vec_safe_is_empty (files))
21353     {
21354       if (first->lineno > 1 || second->lineno > 1)
21355         return 0;
21356     }
21357   else if (first->lineno == 0)
21358     return 0;
21359
21360   /* Find the last define/undef entry that can be grouped together
21361      with first and at the same time compute md5 checksum of their
21362      codes, linenumbers and strings.  */
21363   md5_init_ctx (&ctx);
21364   for (i = idx; macinfo_table->iterate (i, &cur); i++)
21365     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
21366       break;
21367     else if (vec_safe_is_empty (files) && cur->lineno > 1)
21368       break;
21369     else
21370       {
21371         unsigned char code = cur->code;
21372         md5_process_bytes (&code, 1, &ctx);
21373         checksum_uleb128 (cur->lineno, &ctx);
21374         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
21375       }
21376   md5_finish_ctx (&ctx, checksum);
21377   count = i - idx;
21378
21379   /* From the containing include filename (if any) pick up just
21380      usable characters from its basename.  */
21381   if (vec_safe_is_empty (files))
21382     base = "";
21383   else
21384     base = lbasename (files->last ().info);
21385   for (encoded_filename_len = 0, i = 0; base[i]; i++)
21386     if (ISIDNUM (base[i]) || base[i] == '.')
21387       encoded_filename_len++;
21388   /* Count . at the end.  */
21389   if (encoded_filename_len)
21390     encoded_filename_len++;
21391
21392   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
21393   linebuf_len = strlen (linebuf);
21394
21395   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
21396   grp_name = XALLOCAVEC (char, 4 + encoded_filename_len + linebuf_len + 1
21397                          + 16 * 2 + 1);
21398   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
21399   tail = grp_name + 4;
21400   if (encoded_filename_len)
21401     {
21402       for (i = 0; base[i]; i++)
21403         if (ISIDNUM (base[i]) || base[i] == '.')
21404           *tail++ = base[i];
21405       *tail++ = '.';
21406     }
21407   memcpy (tail, linebuf, linebuf_len);
21408   tail += linebuf_len;
21409   *tail++ = '.';
21410   for (i = 0; i < 16; i++)
21411     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
21412
21413   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
21414      in the empty vector entry before the first define/undef.  */
21415   inc = &(*macinfo_table)[idx - 1];
21416   inc->code = DW_MACRO_GNU_transparent_include;
21417   inc->lineno = 0;
21418   inc->info = ggc_strdup (grp_name);
21419   if (*macinfo_htab == NULL)
21420     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
21421   /* Avoid emitting duplicates.  */
21422   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
21423   if (*slot != NULL)
21424     {
21425       inc->code = 0;
21426       inc->info = NULL;
21427       /* If such an entry has been used before, just emit
21428          a DW_MACRO_GNU_transparent_include op.  */
21429       inc = (macinfo_entry *) *slot;
21430       output_macinfo_op (inc);
21431       /* And clear all macinfo_entry in the range to avoid emitting them
21432          in the second pass.  */
21433       for (i = idx; macinfo_table->iterate (i, &cur) && i < idx + count; i++)
21434         {
21435           cur->code = 0;
21436           cur->info = NULL;
21437         }
21438     }
21439   else
21440     {
21441       *slot = inc;
21442       inc->lineno = htab_elements (*macinfo_htab);
21443       output_macinfo_op (inc);
21444     }
21445   return count;
21446 }
21447
21448 /* Save any strings needed by the macinfo table in the debug str
21449    table.  All strings must be collected into the table by the time
21450    index_string is called.  */
21451
21452 static void
21453 save_macinfo_strings (void)
21454 {
21455   unsigned len;
21456   unsigned i;
21457   macinfo_entry *ref;
21458
21459   for (i = 0; macinfo_table && macinfo_table->iterate (i, &ref); i++)
21460     {
21461       switch (ref->code)
21462         {
21463           /* Match the logic in output_macinfo_op to decide on
21464              indirect strings.  */
21465           case DW_MACINFO_define:
21466           case DW_MACINFO_undef:
21467             len = strlen (ref->info) + 1;
21468             if (!dwarf_strict
21469                 && len > DWARF_OFFSET_SIZE
21470                 && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
21471                 && (debug_str_section->common.flags & SECTION_MERGE) != 0)
21472               set_indirect_string (find_AT_string (ref->info));
21473             break;
21474           case DW_MACRO_GNU_define_indirect:
21475           case DW_MACRO_GNU_undef_indirect:
21476             set_indirect_string (find_AT_string (ref->info));
21477             break;
21478           default:
21479             break;
21480         }
21481     }
21482 }
21483
21484 /* Output macinfo section(s).  */
21485
21486 static void
21487 output_macinfo (void)
21488 {
21489   unsigned i;
21490   unsigned long length = vec_safe_length (macinfo_table);
21491   macinfo_entry *ref;
21492   vec<macinfo_entry, va_gc> *files = NULL;
21493   htab_t macinfo_htab = NULL;
21494
21495   if (! length)
21496     return;
21497
21498   /* output_macinfo* uses these interchangeably.  */
21499   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
21500               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
21501               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
21502               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
21503
21504   /* For .debug_macro emit the section header.  */
21505   if (!dwarf_strict)
21506     {
21507       dw2_asm_output_data (2, 4, "DWARF macro version number");
21508       if (DWARF_OFFSET_SIZE == 8)
21509         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
21510       else
21511         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
21512       dw2_asm_output_offset (DWARF_OFFSET_SIZE,
21513                              (!dwarf_split_debug_info ? debug_line_section_label
21514                               : debug_skeleton_line_section_label),
21515                              debug_line_section, NULL);
21516     }
21517
21518   /* In the first loop, it emits the primary .debug_macinfo section
21519      and after each emitted op the macinfo_entry is cleared.
21520      If a longer range of define/undef ops can be optimized using
21521      DW_MACRO_GNU_transparent_include, the
21522      DW_MACRO_GNU_transparent_include op is emitted and kept in
21523      the vector before the first define/undef in the range and the
21524      whole range of define/undef ops is not emitted and kept.  */
21525   for (i = 0; macinfo_table->iterate (i, &ref); i++)
21526     {
21527       switch (ref->code)
21528         {
21529         case DW_MACINFO_start_file:
21530           vec_safe_push (files, *ref);
21531           break;
21532         case DW_MACINFO_end_file:
21533           if (!vec_safe_is_empty (files))
21534             files->pop ();
21535           break;
21536         case DW_MACINFO_define:
21537         case DW_MACINFO_undef:
21538           if (!dwarf_strict
21539               && HAVE_COMDAT_GROUP
21540               && vec_safe_length (files) != 1
21541               && i > 0
21542               && i + 1 < length
21543               && (*macinfo_table)[i - 1].code == 0)
21544             {
21545               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
21546               if (count)
21547                 {
21548                   i += count - 1;
21549                   continue;
21550                 }
21551             }
21552           break;
21553         case 0:
21554           /* A dummy entry may be inserted at the beginning to be able
21555              to optimize the whole block of predefined macros.  */
21556           if (i == 0)
21557             continue;
21558         default:
21559           break;
21560         }
21561       output_macinfo_op (ref);
21562       ref->info = NULL;
21563       ref->code = 0;
21564     }
21565
21566   if (macinfo_htab == NULL)
21567     return;
21568
21569   htab_delete (macinfo_htab);
21570
21571   /* If any DW_MACRO_GNU_transparent_include were used, on those
21572      DW_MACRO_GNU_transparent_include entries terminate the
21573      current chain and switch to a new comdat .debug_macinfo
21574      section and emit the define/undef entries within it.  */
21575   for (i = 0; macinfo_table->iterate (i, &ref); i++)
21576     switch (ref->code)
21577       {
21578       case 0:
21579         continue;
21580       case DW_MACRO_GNU_transparent_include:
21581         {
21582           char label[MAX_ARTIFICIAL_LABEL_BYTES];
21583           tree comdat_key = get_identifier (ref->info);
21584           /* Terminate the previous .debug_macinfo section.  */
21585           dw2_asm_output_data (1, 0, "End compilation unit");
21586           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
21587                                          SECTION_DEBUG
21588                                          | SECTION_LINKONCE,
21589                                          comdat_key);
21590           ASM_GENERATE_INTERNAL_LABEL (label,
21591                                        DEBUG_MACRO_SECTION_LABEL,
21592                                        ref->lineno);
21593           ASM_OUTPUT_LABEL (asm_out_file, label);
21594           ref->code = 0;
21595           ref->info = NULL;
21596           dw2_asm_output_data (2, 4, "DWARF macro version number");
21597           if (DWARF_OFFSET_SIZE == 8)
21598             dw2_asm_output_data (1, 1, "Flags: 64-bit");
21599           else
21600             dw2_asm_output_data (1, 0, "Flags: 32-bit");
21601         }
21602         break;
21603       case DW_MACINFO_define:
21604       case DW_MACINFO_undef:
21605         output_macinfo_op (ref);
21606         ref->code = 0;
21607         ref->info = NULL;
21608         break;
21609       default:
21610         gcc_unreachable ();
21611       }
21612 }
21613
21614 /* Set up for Dwarf output at the start of compilation.  */
21615
21616 static void
21617 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21618 {
21619   /* Allocate the file_table.  */
21620   file_table = htab_create_ggc (50, file_table_hash,
21621                                 file_table_eq, NULL);
21622
21623   /* Allocate the decl_die_table.  */
21624   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21625                                     decl_die_table_eq, NULL);
21626
21627   /* Allocate the decl_loc_table.  */
21628   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21629                                     decl_loc_table_eq, NULL);
21630
21631   /* Allocate the cached_dw_loc_list_table.  */
21632   cached_dw_loc_list_table
21633     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
21634                        cached_dw_loc_list_table_eq, NULL);
21635
21636   /* Allocate the initial hunk of the decl_scope_table.  */
21637   vec_alloc (decl_scope_table, 256);
21638
21639   /* Allocate the initial hunk of the abbrev_die_table.  */
21640   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21641     (ABBREV_DIE_TABLE_INCREMENT);
21642   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21643   /* Zero-th entry is allocated, but unused.  */
21644   abbrev_die_table_in_use = 1;
21645
21646   /* Allocate the pubtypes and pubnames vectors.  */
21647   vec_alloc (pubname_table, 32);
21648   vec_alloc (pubtype_table, 32);
21649
21650   vec_alloc (incomplete_types, 64);
21651
21652   vec_alloc (used_rtx_array, 32);
21653
21654   if (!dwarf_split_debug_info)
21655     {
21656       debug_info_section = get_section (DEBUG_INFO_SECTION,
21657                                         SECTION_DEBUG, NULL);
21658       debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21659                                           SECTION_DEBUG, NULL);
21660       debug_loc_section = get_section (DEBUG_LOC_SECTION,
21661                                        SECTION_DEBUG, NULL);
21662     }
21663   else
21664     {
21665       debug_info_section = get_section (DEBUG_DWO_INFO_SECTION,
21666                                         SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21667       debug_abbrev_section = get_section (DEBUG_DWO_ABBREV_SECTION,
21668                                           SECTION_DEBUG | SECTION_EXCLUDE,
21669                                           NULL);
21670       debug_addr_section = get_section (DEBUG_ADDR_SECTION,
21671                                         SECTION_DEBUG, NULL);
21672       debug_skeleton_info_section = get_section (DEBUG_INFO_SECTION,
21673                                                  SECTION_DEBUG, NULL);
21674       debug_skeleton_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21675                                                    SECTION_DEBUG, NULL);
21676       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_abbrev_section_label,
21677                                   DEBUG_SKELETON_ABBREV_SECTION_LABEL, 0);
21678
21679       /* Somewhat confusing detail: The skeleton_[abbrev|info] sections stay in
21680          the main .o, but the skeleton_line goes into the split off dwo.  */
21681       debug_skeleton_line_section
21682           = get_section (DEBUG_DWO_LINE_SECTION,
21683                          SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21684       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_line_section_label,
21685                                    DEBUG_SKELETON_LINE_SECTION_LABEL, 0);
21686       debug_str_offsets_section = get_section (DEBUG_STR_OFFSETS_SECTION,
21687                                                SECTION_DEBUG | SECTION_EXCLUDE,
21688                                                NULL);
21689       ASM_GENERATE_INTERNAL_LABEL (debug_skeleton_info_section_label,
21690                                    DEBUG_SKELETON_INFO_SECTION_LABEL, 0);
21691       debug_loc_section = get_section (DEBUG_DWO_LOC_SECTION,
21692                                        SECTION_DEBUG | SECTION_EXCLUDE, NULL);
21693     }
21694   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21695                                        SECTION_DEBUG, NULL);
21696   debug_macinfo_section = get_section (dwarf_strict
21697                                        ? DEBUG_MACINFO_SECTION
21698                                        : DEBUG_MACRO_SECTION,
21699                                        DEBUG_MACRO_SECTION_FLAGS, NULL);
21700   debug_line_section = get_section (DEBUG_LINE_SECTION,
21701                                     SECTION_DEBUG, NULL);
21702   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21703                                         SECTION_DEBUG, NULL);
21704   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21705                                         SECTION_DEBUG, NULL);
21706   debug_str_section = get_section (DEBUG_STR_SECTION,
21707                                    DEBUG_STR_SECTION_FLAGS, NULL);
21708   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21709                                       SECTION_DEBUG, NULL);
21710   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21711                                      SECTION_DEBUG, NULL);
21712
21713   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21714   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21715                                DEBUG_ABBREV_SECTION_LABEL, 0);
21716   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21717   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21718                                COLD_TEXT_SECTION_LABEL, 0);
21719   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21720
21721   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21722                                DEBUG_INFO_SECTION_LABEL, 0);
21723   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21724                                DEBUG_LINE_SECTION_LABEL, 0);
21725   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21726                                DEBUG_RANGES_SECTION_LABEL, 0);
21727   ASM_GENERATE_INTERNAL_LABEL (debug_addr_section_label,
21728                                DEBUG_ADDR_SECTION_LABEL, 0);
21729   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21730                                dwarf_strict
21731                                ? DEBUG_MACINFO_SECTION_LABEL
21732                                : DEBUG_MACRO_SECTION_LABEL, 0);
21733   ASM_GENERATE_INTERNAL_LABEL (loc_section_label, DEBUG_LOC_SECTION_LABEL, 0);
21734
21735   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21736     vec_alloc (macinfo_table, 64);
21737
21738   switch_to_section (text_section);
21739   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21740
21741   /* Make sure the line number table for .text always exists.  */
21742   text_section_line_info = new_line_info_table ();
21743   text_section_line_info->end_label = text_end_label;
21744 }
21745
21746 /* Called before compile () starts outputtting functions, variables
21747    and toplevel asms into assembly.  */
21748
21749 static void
21750 dwarf2out_assembly_start (void)
21751 {
21752   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
21753       && dwarf2out_do_cfi_asm ()
21754       && (!(flag_unwind_tables || flag_exceptions)
21755           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
21756     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21757 }
21758
21759 /* A helper function for dwarf2out_finish called through
21760    htab_traverse.  Assign a string its index.  All strings must be
21761    collected into the table by the time index_string is called,
21762    because the indexing code relies on htab_traverse to traverse nodes
21763    in the same order for each run. */
21764
21765 static int
21766 index_string (void **h, void *v)
21767 {
21768   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21769   unsigned int *index = (unsigned int *) v;
21770
21771   find_string_form (node);
21772   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21773     {
21774       gcc_assert(node->index == NO_INDEX_ASSIGNED);
21775       node->index = *index;
21776       *index += 1;
21777     }
21778   return 1;
21779 }
21780
21781 /* A helper function for output_indirect_strings called through
21782    htab_traverse.  Output the offset to a string and update the
21783    current offset.  */
21784
21785 static int
21786 output_index_string_offset (void **h, void *v)
21787 {
21788   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21789   unsigned int *offset = (unsigned int *) v;
21790
21791   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21792     {
21793       /* Assert that this node has been assigned an index.  */
21794       gcc_assert (node->index != NO_INDEX_ASSIGNED
21795                   && node->index != NOT_INDEXED);
21796       dw2_asm_output_data (DWARF_OFFSET_SIZE, *offset,
21797                            "indexed string 0x%x: %s", node->index, node->str);
21798       *offset += strlen (node->str) + 1;
21799     }
21800   return 1;
21801 }
21802
21803 /* A helper function for dwarf2out_finish called through
21804    htab_traverse.  Output the indexed string.  */
21805
21806 static int
21807 output_index_string (void **h, void *v)
21808 {
21809   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21810   unsigned int *cur_idx = (unsigned int *) v;
21811
21812   if (node->form == DW_FORM_GNU_str_index && node->refcount > 0)
21813     {
21814       /* Assert that the strings are output in the same order as their
21815          indexes were assigned.  */
21816       gcc_assert (*cur_idx == node->index);
21817       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21818       assemble_string (node->str, strlen (node->str) + 1);
21819       *cur_idx += 1;
21820     }
21821   return 1;
21822 }
21823
21824 /* A helper function for dwarf2out_finish called through
21825    htab_traverse.  Emit one queued .debug_str string.  */
21826
21827 static int
21828 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21829 {
21830   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21831
21832   if (node->form == DW_FORM_strp && node->refcount > 0)
21833     {
21834       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21835       assemble_string (node->str, strlen (node->str) + 1);
21836     }
21837
21838   return 1;
21839 }
21840
21841 /* Output the indexed string table.  */
21842
21843 static void
21844 output_indirect_strings (void)
21845 {
21846   if (!dwarf_split_debug_info)
21847     {
21848       switch_to_section (debug_str_section);
21849       htab_traverse (debug_str_hash, output_indirect_string, NULL);
21850     }
21851   else
21852     {
21853       unsigned int offset = 0;
21854       unsigned int cur_idx = 0;
21855
21856       switch_to_section (debug_str_offsets_section);
21857       htab_traverse_noresize (debug_str_hash,
21858                               output_index_string_offset,
21859                               &offset);
21860       switch_to_section (debug_str_section);
21861       htab_traverse_noresize (debug_str_hash,
21862                               output_index_string,
21863                               &cur_idx);
21864     }
21865 }
21866
21867 /* Callback for htab_traverse to assign an index to an entry in the
21868    table, and to write that entry to the .debug_addr section.  */
21869
21870 static int
21871 output_addr_table_entry (void **slot, void *data)
21872 {
21873   addr_table_entry *entry = (addr_table_entry *) *slot;
21874   unsigned int *cur_index = (unsigned int *)data;
21875
21876   if (entry->refcount == 0)
21877     {
21878       gcc_assert (entry->index == NO_INDEX_ASSIGNED
21879                   || entry->index == NOT_INDEXED);
21880       return 1;
21881     }
21882
21883   gcc_assert (entry->index == *cur_index);
21884   (*cur_index)++;
21885
21886   switch (entry->kind)
21887     {
21888       case ate_kind_rtx:
21889         dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, entry->addr.rtl,
21890                                  "0x%x", entry->index);
21891         break;
21892       case ate_kind_rtx_dtprel:
21893         gcc_assert (targetm.asm_out.output_dwarf_dtprel);
21894         targetm.asm_out.output_dwarf_dtprel (asm_out_file,
21895                                              DWARF2_ADDR_SIZE,
21896                                              entry->addr.rtl);
21897         fputc ('\n', asm_out_file);
21898         break;
21899       case ate_kind_label:
21900         dw2_asm_output_addr (DWARF2_ADDR_SIZE, entry->addr.label,
21901                                  "0x%x", entry->index);
21902         break;
21903       default:
21904         gcc_unreachable ();
21905     }
21906   return 1;
21907 }
21908
21909 /* Produce the .debug_addr section.  */
21910
21911 static void
21912 output_addr_table (void)
21913 {
21914   unsigned int index = 0;
21915   if (addr_index_table == NULL || htab_size (addr_index_table) == 0)
21916     return;
21917
21918   switch_to_section (debug_addr_section);
21919   htab_traverse_noresize (addr_index_table, output_addr_table_entry, &index);
21920 }
21921
21922 #if ENABLE_ASSERT_CHECKING
21923 /* Verify that all marks are clear.  */
21924
21925 static void
21926 verify_marks_clear (dw_die_ref die)
21927 {
21928   dw_die_ref c;
21929
21930   gcc_assert (! die->die_mark);
21931   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21932 }
21933 #endif /* ENABLE_ASSERT_CHECKING */
21934
21935 /* Clear the marks for a die and its children.
21936    Be cool if the mark isn't set.  */
21937
21938 static void
21939 prune_unmark_dies (dw_die_ref die)
21940 {
21941   dw_die_ref c;
21942
21943   if (die->die_mark)
21944     die->die_mark = 0;
21945   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21946 }
21947
21948 /* Given DIE that we're marking as used, find any other dies
21949    it references as attributes and mark them as used.  */
21950
21951 static void
21952 prune_unused_types_walk_attribs (dw_die_ref die)
21953 {
21954   dw_attr_ref a;
21955   unsigned ix;
21956
21957   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
21958     {
21959       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21960         {
21961           /* A reference to another DIE.
21962              Make sure that it will get emitted.
21963              If it was broken out into a comdat group, don't follow it.  */
21964           if (! AT_ref (a)->comdat_type_p
21965               || a->dw_attr == DW_AT_specification)
21966             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21967         }
21968       /* Set the string's refcount to 0 so that prune_unused_types_mark
21969          accounts properly for it.  */
21970       if (AT_class (a) == dw_val_class_str)
21971         a->dw_attr_val.v.val_str->refcount = 0;
21972     }
21973 }
21974
21975 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21976
21977 static void
21978 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21979 {
21980   dw_die_ref c;
21981
21982   if (die == NULL || die->die_child == NULL)
21983     return;
21984   c = die->die_child;
21985   do
21986     {
21987       switch (c->die_tag)
21988         {
21989         case DW_TAG_template_type_param:
21990         case DW_TAG_template_value_param:
21991         case DW_TAG_GNU_template_template_param:
21992         case DW_TAG_GNU_template_parameter_pack:
21993           prune_unused_types_mark (c, 1);
21994           break;
21995         default:
21996           break;
21997         }
21998       c = c->die_sib;
21999     } while (c && c != die->die_child);
22000 }
22001
22002 /* Mark DIE as being used.  If DOKIDS is true, then walk down
22003    to DIE's children.  */
22004
22005 static void
22006 prune_unused_types_mark (dw_die_ref die, int dokids)
22007 {
22008   dw_die_ref c;
22009
22010   if (die->die_mark == 0)
22011     {
22012       /* We haven't done this node yet.  Mark it as used.  */
22013       die->die_mark = 1;
22014       /* If this is the DIE of a generic type instantiation,
22015          mark the children DIEs that describe its generic parms and
22016          args.  */
22017       prune_unused_types_mark_generic_parms_dies (die);
22018
22019       /* We also have to mark its parents as used.
22020          (But we don't want to mark our parent's kids due to this,
22021          unless it is a class.)  */
22022       if (die->die_parent)
22023         prune_unused_types_mark (die->die_parent,
22024                                  class_scope_p (die->die_parent));
22025
22026       /* Mark any referenced nodes.  */
22027       prune_unused_types_walk_attribs (die);
22028
22029       /* If this node is a specification,
22030          also mark the definition, if it exists.  */
22031       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
22032         prune_unused_types_mark (die->die_definition, 1);
22033     }
22034
22035   if (dokids && die->die_mark != 2)
22036     {
22037       /* We need to walk the children, but haven't done so yet.
22038          Remember that we've walked the kids.  */
22039       die->die_mark = 2;
22040
22041       /* If this is an array type, we need to make sure our
22042          kids get marked, even if they're types.  If we're
22043          breaking out types into comdat sections, do this
22044          for all type definitions.  */
22045       if (die->die_tag == DW_TAG_array_type
22046           || (use_debug_types
22047               && is_type_die (die) && ! is_declaration_die (die)))
22048         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
22049       else
22050         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22051     }
22052 }
22053
22054 /* For local classes, look if any static member functions were emitted
22055    and if so, mark them.  */
22056
22057 static void
22058 prune_unused_types_walk_local_classes (dw_die_ref die)
22059 {
22060   dw_die_ref c;
22061
22062   if (die->die_mark == 2)
22063     return;
22064
22065   switch (die->die_tag)
22066     {
22067     case DW_TAG_structure_type:
22068     case DW_TAG_union_type:
22069     case DW_TAG_class_type:
22070       break;
22071
22072     case DW_TAG_subprogram:
22073       if (!get_AT_flag (die, DW_AT_declaration)
22074           || die->die_definition != NULL)
22075         prune_unused_types_mark (die, 1);
22076       return;
22077
22078     default:
22079       return;
22080     }
22081
22082   /* Mark children.  */
22083   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
22084 }
22085
22086 /* Walk the tree DIE and mark types that we actually use.  */
22087
22088 static void
22089 prune_unused_types_walk (dw_die_ref die)
22090 {
22091   dw_die_ref c;
22092
22093   /* Don't do anything if this node is already marked and
22094      children have been marked as well.  */
22095   if (die->die_mark == 2)
22096     return;
22097
22098   switch (die->die_tag)
22099     {
22100     case DW_TAG_structure_type:
22101     case DW_TAG_union_type:
22102     case DW_TAG_class_type:
22103       if (die->die_perennial_p)
22104         break;
22105
22106       for (c = die->die_parent; c; c = c->die_parent)
22107         if (c->die_tag == DW_TAG_subprogram)
22108           break;
22109
22110       /* Finding used static member functions inside of classes
22111          is needed just for local classes, because for other classes
22112          static member function DIEs with DW_AT_specification
22113          are emitted outside of the DW_TAG_*_type.  If we ever change
22114          it, we'd need to call this even for non-local classes.  */
22115       if (c)
22116         prune_unused_types_walk_local_classes (die);
22117
22118       /* It's a type node --- don't mark it.  */
22119       return;
22120
22121     case DW_TAG_const_type:
22122     case DW_TAG_packed_type:
22123     case DW_TAG_pointer_type:
22124     case DW_TAG_reference_type:
22125     case DW_TAG_rvalue_reference_type:
22126     case DW_TAG_volatile_type:
22127     case DW_TAG_typedef:
22128     case DW_TAG_array_type:
22129     case DW_TAG_interface_type:
22130     case DW_TAG_friend:
22131     case DW_TAG_variant_part:
22132     case DW_TAG_enumeration_type:
22133     case DW_TAG_subroutine_type:
22134     case DW_TAG_string_type:
22135     case DW_TAG_set_type:
22136     case DW_TAG_subrange_type:
22137     case DW_TAG_ptr_to_member_type:
22138     case DW_TAG_file_type:
22139       if (die->die_perennial_p)
22140         break;
22141
22142       /* It's a type node --- don't mark it.  */
22143       return;
22144
22145     default:
22146       /* Mark everything else.  */
22147       break;
22148   }
22149
22150   if (die->die_mark == 0)
22151     {
22152       die->die_mark = 1;
22153
22154       /* Now, mark any dies referenced from here.  */
22155       prune_unused_types_walk_attribs (die);
22156     }
22157
22158   die->die_mark = 2;
22159
22160   /* Mark children.  */
22161   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
22162 }
22163
22164 /* Increment the string counts on strings referred to from DIE's
22165    attributes.  */
22166
22167 static void
22168 prune_unused_types_update_strings (dw_die_ref die)
22169 {
22170   dw_attr_ref a;
22171   unsigned ix;
22172
22173   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22174     if (AT_class (a) == dw_val_class_str)
22175       {
22176         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
22177         s->refcount++;
22178         /* Avoid unnecessarily putting strings that are used less than
22179            twice in the hash table.  */
22180         if (s->refcount
22181             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
22182           {
22183             void ** slot;
22184             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
22185                                              htab_hash_string (s->str),
22186                                              INSERT);
22187             gcc_assert (*slot == NULL);
22188             *slot = s;
22189           }
22190       }
22191 }
22192
22193 /* Remove from the tree DIE any dies that aren't marked.  */
22194
22195 static void
22196 prune_unused_types_prune (dw_die_ref die)
22197 {
22198   dw_die_ref c;
22199
22200   gcc_assert (die->die_mark);
22201   prune_unused_types_update_strings (die);
22202
22203   if (! die->die_child)
22204     return;
22205
22206   c = die->die_child;
22207   do {
22208     dw_die_ref prev = c;
22209     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
22210       if (c == die->die_child)
22211         {
22212           /* No marked children between 'prev' and the end of the list.  */
22213           if (prev == c)
22214             /* No marked children at all.  */
22215             die->die_child = NULL;
22216           else
22217             {
22218               prev->die_sib = c->die_sib;
22219               die->die_child = prev;
22220             }
22221           return;
22222         }
22223
22224     if (c != prev->die_sib)
22225       prev->die_sib = c;
22226     prune_unused_types_prune (c);
22227   } while (c != die->die_child);
22228 }
22229
22230 /* Remove dies representing declarations that we never use.  */
22231
22232 static void
22233 prune_unused_types (void)
22234 {
22235   unsigned int i;
22236   limbo_die_node *node;
22237   comdat_type_node *ctnode;
22238   pubname_ref pub;
22239   dw_die_ref base_type;
22240
22241 #if ENABLE_ASSERT_CHECKING
22242   /* All the marks should already be clear.  */
22243   verify_marks_clear (comp_unit_die ());
22244   for (node = limbo_die_list; node; node = node->next)
22245     verify_marks_clear (node->die);
22246   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22247     verify_marks_clear (ctnode->root_die);
22248 #endif /* ENABLE_ASSERT_CHECKING */
22249
22250   /* Mark types that are used in global variables.  */
22251   premark_types_used_by_global_vars ();
22252
22253   /* Set the mark on nodes that are actually used.  */
22254   prune_unused_types_walk (comp_unit_die ());
22255   for (node = limbo_die_list; node; node = node->next)
22256     prune_unused_types_walk (node->die);
22257   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22258     {
22259       prune_unused_types_walk (ctnode->root_die);
22260       prune_unused_types_mark (ctnode->type_die, 1);
22261     }
22262
22263   /* Also set the mark on nodes referenced from the pubname_table.  Enumerators
22264      are unusual in that they are pubnames that are the children of pubtypes.
22265      They should only be marked via their parent DW_TAG_enumeration_type die,
22266      not as roots in themselves.  */
22267   FOR_EACH_VEC_ELT (*pubname_table, i, pub)
22268     if (pub->die->die_tag != DW_TAG_enumerator)
22269       prune_unused_types_mark (pub->die, 1);
22270   for (i = 0; base_types.iterate (i, &base_type); i++)
22271     prune_unused_types_mark (base_type, 1);
22272
22273   if (debug_str_hash)
22274     htab_empty (debug_str_hash);
22275   prune_unused_types_prune (comp_unit_die ());
22276   for (node = limbo_die_list; node; node = node->next)
22277     prune_unused_types_prune (node->die);
22278   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22279     prune_unused_types_prune (ctnode->root_die);
22280
22281   /* Leave the marks clear.  */
22282   prune_unmark_dies (comp_unit_die ());
22283   for (node = limbo_die_list; node; node = node->next)
22284     prune_unmark_dies (node->die);
22285   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22286     prune_unmark_dies (ctnode->root_die);
22287 }
22288
22289 /* Set the parameter to true if there are any relative pathnames in
22290    the file table.  */
22291 static int
22292 file_table_relative_p (void ** slot, void *param)
22293 {
22294   bool *p = (bool *) param;
22295   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22296   if (!IS_ABSOLUTE_PATH (d->filename))
22297     {
22298       *p = true;
22299       return 0;
22300     }
22301   return 1;
22302 }
22303
22304 /* Routines to manipulate hash table of comdat type units.  */
22305
22306 static hashval_t
22307 htab_ct_hash (const void *of)
22308 {
22309   hashval_t h;
22310   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22311
22312   memcpy (&h, type_node->signature, sizeof (h));
22313   return h;
22314 }
22315
22316 static int
22317 htab_ct_eq (const void *of1, const void *of2)
22318 {
22319   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22320   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22321
22322   return (! memcmp (type_node_1->signature, type_node_2->signature,
22323                     DWARF_TYPE_SIGNATURE_SIZE));
22324 }
22325
22326 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22327    to the location it would have been added, should we know its
22328    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22329    probably improve compactness of debug info, removing equivalent
22330    abbrevs, and hide any differences caused by deferring the
22331    computation of the assembler name, triggered by e.g. PCH.  */
22332
22333 static inline void
22334 move_linkage_attr (dw_die_ref die)
22335 {
22336   unsigned ix = vec_safe_length (die->die_attr);
22337   dw_attr_node linkage = (*die->die_attr)[ix - 1];
22338
22339   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22340               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22341
22342   while (--ix > 0)
22343     {
22344       dw_attr_node *prev = &(*die->die_attr)[ix - 1];
22345
22346       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22347         break;
22348     }
22349
22350   if (ix != vec_safe_length (die->die_attr) - 1)
22351     {
22352       die->die_attr->pop ();
22353       die->die_attr->quick_insert (ix, linkage);
22354     }
22355 }
22356
22357 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
22358    referenced from typed stack ops and count how often they are used.  */
22359
22360 static void
22361 mark_base_types (dw_loc_descr_ref loc)
22362 {
22363   dw_die_ref base_type = NULL;
22364
22365   for (; loc; loc = loc->dw_loc_next)
22366     {
22367       switch (loc->dw_loc_opc)
22368         {
22369         case DW_OP_GNU_regval_type:
22370         case DW_OP_GNU_deref_type:
22371           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
22372           break;
22373         case DW_OP_GNU_convert:
22374         case DW_OP_GNU_reinterpret:
22375           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
22376             continue;
22377           /* FALLTHRU */
22378         case DW_OP_GNU_const_type:
22379           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
22380           break;
22381         case DW_OP_GNU_entry_value:
22382           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
22383           continue;
22384         default:
22385           continue;
22386         }
22387       gcc_assert (base_type->die_parent == comp_unit_die ());
22388       if (base_type->die_mark)
22389         base_type->die_mark++;
22390       else
22391         {
22392           base_types.safe_push (base_type);
22393           base_type->die_mark = 1;
22394         }
22395     }
22396 }
22397
22398 /* Comparison function for sorting marked base types.  */
22399
22400 static int
22401 base_type_cmp (const void *x, const void *y)
22402 {
22403   dw_die_ref dx = *(const dw_die_ref *) x;
22404   dw_die_ref dy = *(const dw_die_ref *) y;
22405   unsigned int byte_size1, byte_size2;
22406   unsigned int encoding1, encoding2;
22407   if (dx->die_mark > dy->die_mark)
22408     return -1;
22409   if (dx->die_mark < dy->die_mark)
22410     return 1;
22411   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
22412   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
22413   if (byte_size1 < byte_size2)
22414     return 1;
22415   if (byte_size1 > byte_size2)
22416     return -1;
22417   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
22418   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
22419   if (encoding1 < encoding2)
22420     return 1;
22421   if (encoding1 > encoding2)
22422     return -1;
22423   return 0;
22424 }
22425
22426 /* Move base types marked by mark_base_types as early as possible
22427    in the CU, sorted by decreasing usage count both to make the
22428    uleb128 references as small as possible and to make sure they
22429    will have die_offset already computed by calc_die_sizes when
22430    sizes of typed stack loc ops is computed.  */
22431
22432 static void
22433 move_marked_base_types (void)
22434 {
22435   unsigned int i;
22436   dw_die_ref base_type, die, c;
22437
22438   if (base_types.is_empty ())
22439     return;
22440
22441   /* Sort by decreasing usage count, they will be added again in that
22442      order later on.  */
22443   base_types.qsort (base_type_cmp);
22444   die = comp_unit_die ();
22445   c = die->die_child;
22446   do
22447     {
22448       dw_die_ref prev = c;
22449       c = c->die_sib;
22450       while (c->die_mark)
22451         {
22452           remove_child_with_prev (c, prev);
22453           /* As base types got marked, there must be at least
22454              one node other than DW_TAG_base_type.  */
22455           gcc_assert (c != c->die_sib);
22456           c = c->die_sib;
22457         }
22458     }
22459   while (c != die->die_child);
22460   gcc_assert (die->die_child);
22461   c = die->die_child;
22462   for (i = 0; base_types.iterate (i, &base_type); i++)
22463     {
22464       base_type->die_mark = 0;
22465       base_type->die_sib = c->die_sib;
22466       c->die_sib = base_type;
22467       c = base_type;
22468     }
22469 }
22470
22471 /* Helper function for resolve_addr, attempt to resolve
22472    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22473    SYMBOL_REFs refer to variables emitted in the current CU.  */
22474
22475 static int
22476 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22477 {
22478   rtx rtl = *addr;
22479
22480   if (GET_CODE (rtl) == CONST_STRING)
22481     {
22482       size_t len = strlen (XSTR (rtl, 0)) + 1;
22483       tree t = build_string (len, XSTR (rtl, 0));
22484       tree tlen = size_int (len - 1);
22485       TREE_TYPE (t)
22486         = build_array_type (char_type_node, build_index_type (tlen));
22487       rtl = lookup_constant_def (t);
22488       if (!rtl || !MEM_P (rtl))
22489         return 1;
22490       rtl = XEXP (rtl, 0);
22491       vec_safe_push (used_rtx_array, rtl);
22492       *addr = rtl;
22493       return 0;
22494     }
22495
22496   if (GET_CODE (rtl) == SYMBOL_REF
22497       && SYMBOL_REF_DECL (rtl))
22498     {
22499       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
22500         {
22501           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
22502             return 1;
22503         }
22504       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22505         return 1;
22506     }
22507
22508   if (GET_CODE (rtl) == CONST
22509       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22510     return 1;
22511
22512   return 0;
22513 }
22514
22515 /* Helper function for resolve_addr, handle one location
22516    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22517    the location list couldn't be resolved.  */
22518
22519 static bool
22520 resolve_addr_in_expr (dw_loc_descr_ref loc)
22521 {
22522   dw_loc_descr_ref keep = NULL;
22523   for (; loc; loc = loc->dw_loc_next)
22524     switch (loc->dw_loc_opc)
22525       {
22526       case DW_OP_addr:
22527         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22528           return false;
22529         break;
22530       case DW_OP_GNU_addr_index:
22531       case DW_OP_GNU_const_index:
22532         {
22533           if ((loc->dw_loc_opc == DW_OP_GNU_addr_index
22534                || (loc->dw_loc_opc == DW_OP_GNU_const_index && loc->dtprel))
22535               && resolve_one_addr (&loc->dw_loc_oprnd1.val_entry->addr.rtl,
22536                                    NULL))
22537             return false;
22538         }
22539        break;
22540       case DW_OP_const4u:
22541       case DW_OP_const8u:
22542         if (loc->dtprel
22543             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22544           return false;
22545         break;
22546       case DW_OP_plus_uconst:
22547         if (size_of_loc_descr (loc)
22548             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
22549               + 1
22550             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
22551           {
22552             dw_loc_descr_ref repl
22553               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
22554             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
22555             add_loc_descr (&repl, loc->dw_loc_next);
22556             *loc = *repl;
22557           }
22558         break;
22559       case DW_OP_implicit_value:
22560         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22561             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
22562           return false;
22563         break;
22564       case DW_OP_GNU_implicit_pointer:
22565       case DW_OP_GNU_parameter_ref:
22566         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
22567           {
22568             dw_die_ref ref
22569               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
22570             if (ref == NULL)
22571               return false;
22572             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
22573             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
22574             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
22575           }
22576         break;
22577       case DW_OP_GNU_const_type:
22578       case DW_OP_GNU_regval_type:
22579       case DW_OP_GNU_deref_type:
22580       case DW_OP_GNU_convert:
22581       case DW_OP_GNU_reinterpret:
22582         while (loc->dw_loc_next
22583                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
22584           {
22585             dw_die_ref base1, base2;
22586             unsigned enc1, enc2, size1, size2;
22587             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22588                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22589               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
22590             else if (loc->dw_loc_oprnd1.val_class
22591                      == dw_val_class_unsigned_const)
22592               break;
22593             else
22594               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
22595             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
22596                 == dw_val_class_unsigned_const)
22597               break;
22598             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
22599             gcc_assert (base1->die_tag == DW_TAG_base_type
22600                         && base2->die_tag == DW_TAG_base_type);
22601             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
22602             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
22603             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
22604             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
22605             if (size1 == size2
22606                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
22607                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
22608                      && loc != keep)
22609                     || enc1 == enc2))
22610               {
22611                 /* Optimize away next DW_OP_GNU_convert after
22612                    adjusting LOC's base type die reference.  */
22613                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
22614                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
22615                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
22616                 else
22617                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
22618                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
22619                 continue;
22620               }
22621             /* Don't change integer DW_OP_GNU_convert after e.g. floating
22622                point typed stack entry.  */
22623             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
22624               keep = loc->dw_loc_next;
22625             break;
22626           }
22627         break;
22628       default:
22629         break;
22630       }
22631   return true;
22632 }
22633
22634 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22635    an address in .rodata section if the string literal is emitted there,
22636    or remove the containing location list or replace DW_AT_const_value
22637    with DW_AT_location and empty location expression, if it isn't found
22638    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22639    to something that has been emitted in the current CU.  */
22640
22641 static void
22642 resolve_addr (dw_die_ref die)
22643 {
22644   dw_die_ref c;
22645   dw_attr_ref a;
22646   dw_loc_list_ref *curr, *start, loc;
22647   unsigned ix;
22648
22649   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
22650     switch (AT_class (a))
22651       {
22652       case dw_val_class_loc_list:
22653         start = curr = AT_loc_list_ptr (a);
22654         loc = *curr;
22655         gcc_assert (loc);
22656         /* The same list can be referenced more than once.  See if we have
22657            already recorded the result from a previous pass.  */
22658         if (loc->replaced)
22659           *curr = loc->dw_loc_next;
22660         else if (!loc->resolved_addr)
22661           {
22662             /* As things stand, we do not expect or allow one die to
22663                reference a suffix of another die's location list chain.
22664                References must be identical or completely separate.
22665                There is therefore no need to cache the result of this
22666                pass on any list other than the first; doing so
22667                would lead to unnecessary writes.  */
22668             while (*curr)
22669               {
22670                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
22671                 if (!resolve_addr_in_expr ((*curr)->expr))
22672                   {
22673                     dw_loc_list_ref next = (*curr)->dw_loc_next;
22674                     dw_loc_descr_ref l = (*curr)->expr;
22675
22676                     if (next && (*curr)->ll_symbol)
22677                       {
22678                         gcc_assert (!next->ll_symbol);
22679                         next->ll_symbol = (*curr)->ll_symbol;
22680                       }
22681                     if (dwarf_split_debug_info)
22682                       remove_loc_list_addr_table_entries (l);
22683                     *curr = next;
22684                   }
22685                 else
22686                   {
22687                     mark_base_types ((*curr)->expr);
22688                     curr = &(*curr)->dw_loc_next;
22689                   }
22690               }
22691             if (loc == *start)
22692               loc->resolved_addr = 1;
22693             else
22694               {
22695                 loc->replaced = 1;
22696                 loc->dw_loc_next = *start;
22697               }
22698           }
22699         if (!*start)
22700           {
22701             remove_AT (die, a->dw_attr);
22702             ix--;
22703           }
22704         break;
22705       case dw_val_class_loc:
22706         {
22707           dw_loc_descr_ref l = AT_loc (a);
22708           /* For -gdwarf-2 don't attempt to optimize
22709              DW_AT_data_member_location containing
22710              DW_OP_plus_uconst - older consumers might
22711              rely on it being that op instead of a more complex,
22712              but shorter, location description.  */
22713           if ((dwarf_version > 2
22714                || a->dw_attr != DW_AT_data_member_location
22715                || l == NULL
22716                || l->dw_loc_opc != DW_OP_plus_uconst
22717                || l->dw_loc_next != NULL)
22718               && !resolve_addr_in_expr (l))
22719             {
22720               if (dwarf_split_debug_info)
22721                 remove_loc_list_addr_table_entries (l);
22722               remove_AT (die, a->dw_attr);
22723               ix--;
22724             }
22725           else
22726             mark_base_types (l);
22727         }
22728         break;
22729       case dw_val_class_addr:
22730         if (a->dw_attr == DW_AT_const_value
22731             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22732           {
22733             if (AT_index (a) != NOT_INDEXED)
22734               remove_addr_table_entry (a->dw_attr_val.val_entry);
22735             remove_AT (die, a->dw_attr);
22736             ix--;
22737           }
22738         if (die->die_tag == DW_TAG_GNU_call_site
22739             && a->dw_attr == DW_AT_abstract_origin)
22740           {
22741             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
22742             dw_die_ref tdie = lookup_decl_die (tdecl);
22743             if (tdie == NULL
22744                 && DECL_EXTERNAL (tdecl)
22745                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
22746               {
22747                 force_decl_die (tdecl);
22748                 tdie = lookup_decl_die (tdecl);
22749               }
22750             if (tdie)
22751               {
22752                 a->dw_attr_val.val_class = dw_val_class_die_ref;
22753                 a->dw_attr_val.v.val_die_ref.die = tdie;
22754                 a->dw_attr_val.v.val_die_ref.external = 0;
22755               }
22756             else
22757               {
22758                 if (AT_index (a) != NOT_INDEXED)
22759                   remove_addr_table_entry (a->dw_attr_val.val_entry);
22760                 remove_AT (die, a->dw_attr);
22761                 ix--;
22762               }
22763           }
22764         break;
22765       default:
22766         break;
22767       }
22768
22769   FOR_EACH_CHILD (die, c, resolve_addr (c));
22770 }
22771 \f
22772 /* Helper routines for optimize_location_lists.
22773    This pass tries to share identical local lists in .debug_loc
22774    section.  */
22775
22776 /* Iteratively hash operands of LOC opcode.  */
22777
22778 static inline hashval_t
22779 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
22780 {
22781   dw_val_ref val1 = &loc->dw_loc_oprnd1;
22782   dw_val_ref val2 = &loc->dw_loc_oprnd2;
22783
22784   switch (loc->dw_loc_opc)
22785     {
22786     case DW_OP_const4u:
22787     case DW_OP_const8u:
22788       if (loc->dtprel)
22789         goto hash_addr;
22790       /* FALLTHRU */
22791     case DW_OP_const1u:
22792     case DW_OP_const1s:
22793     case DW_OP_const2u:
22794     case DW_OP_const2s:
22795     case DW_OP_const4s:
22796     case DW_OP_const8s:
22797     case DW_OP_constu:
22798     case DW_OP_consts:
22799     case DW_OP_pick:
22800     case DW_OP_plus_uconst:
22801     case DW_OP_breg0:
22802     case DW_OP_breg1:
22803     case DW_OP_breg2:
22804     case DW_OP_breg3:
22805     case DW_OP_breg4:
22806     case DW_OP_breg5:
22807     case DW_OP_breg6:
22808     case DW_OP_breg7:
22809     case DW_OP_breg8:
22810     case DW_OP_breg9:
22811     case DW_OP_breg10:
22812     case DW_OP_breg11:
22813     case DW_OP_breg12:
22814     case DW_OP_breg13:
22815     case DW_OP_breg14:
22816     case DW_OP_breg15:
22817     case DW_OP_breg16:
22818     case DW_OP_breg17:
22819     case DW_OP_breg18:
22820     case DW_OP_breg19:
22821     case DW_OP_breg20:
22822     case DW_OP_breg21:
22823     case DW_OP_breg22:
22824     case DW_OP_breg23:
22825     case DW_OP_breg24:
22826     case DW_OP_breg25:
22827     case DW_OP_breg26:
22828     case DW_OP_breg27:
22829     case DW_OP_breg28:
22830     case DW_OP_breg29:
22831     case DW_OP_breg30:
22832     case DW_OP_breg31:
22833     case DW_OP_regx:
22834     case DW_OP_fbreg:
22835     case DW_OP_piece:
22836     case DW_OP_deref_size:
22837     case DW_OP_xderef_size:
22838       hash = iterative_hash_object (val1->v.val_int, hash);
22839       break;
22840     case DW_OP_skip:
22841     case DW_OP_bra:
22842       {
22843         int offset;
22844
22845         gcc_assert (val1->val_class == dw_val_class_loc);
22846         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
22847         hash = iterative_hash_object (offset, hash);
22848       }
22849       break;
22850     case DW_OP_implicit_value:
22851       hash = iterative_hash_object (val1->v.val_unsigned, hash);
22852       switch (val2->val_class)
22853         {
22854         case dw_val_class_const:
22855           hash = iterative_hash_object (val2->v.val_int, hash);
22856           break;
22857         case dw_val_class_vec:
22858           {
22859             unsigned int elt_size = val2->v.val_vec.elt_size;
22860             unsigned int len = val2->v.val_vec.length;
22861
22862             hash = iterative_hash_object (elt_size, hash);
22863             hash = iterative_hash_object (len, hash);
22864             hash = iterative_hash (val2->v.val_vec.array,
22865                                    len * elt_size, hash);
22866           }
22867           break;
22868         case dw_val_class_const_double:
22869           hash = iterative_hash_object (val2->v.val_double.low, hash);
22870           hash = iterative_hash_object (val2->v.val_double.high, hash);
22871           break;
22872         case dw_val_class_addr:
22873           hash = iterative_hash_rtx (val2->v.val_addr, hash);
22874           break;
22875         default:
22876           gcc_unreachable ();
22877         }
22878       break;
22879     case DW_OP_bregx:
22880     case DW_OP_bit_piece:
22881       hash = iterative_hash_object (val1->v.val_int, hash);
22882       hash = iterative_hash_object (val2->v.val_int, hash);
22883       break;
22884     case DW_OP_addr:
22885     hash_addr:
22886       if (loc->dtprel)
22887         {
22888           unsigned char dtprel = 0xd1;
22889           hash = iterative_hash_object (dtprel, hash);
22890         }
22891       hash = iterative_hash_rtx (val1->v.val_addr, hash);
22892       break;
22893     case DW_OP_GNU_addr_index:
22894     case DW_OP_GNU_const_index:
22895       {
22896         if (loc->dtprel)
22897           {
22898             unsigned char dtprel = 0xd1;
22899             hash = iterative_hash_object (dtprel, hash);
22900           }
22901         hash = iterative_hash_rtx (val1->val_entry->addr.rtl, hash);
22902       }
22903       break;
22904     case DW_OP_GNU_implicit_pointer:
22905       hash = iterative_hash_object (val2->v.val_int, hash);
22906       break;
22907     case DW_OP_GNU_entry_value:
22908       hash = hash_loc_operands (val1->v.val_loc, hash);
22909       break;
22910     case DW_OP_GNU_regval_type:
22911     case DW_OP_GNU_deref_type:
22912       {
22913         unsigned int byte_size
22914           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
22915         unsigned int encoding
22916           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
22917         hash = iterative_hash_object (val1->v.val_int, hash);
22918         hash = iterative_hash_object (byte_size, hash);
22919         hash = iterative_hash_object (encoding, hash);
22920       }
22921       break;
22922     case DW_OP_GNU_convert:
22923     case DW_OP_GNU_reinterpret:
22924       if (val1->val_class == dw_val_class_unsigned_const)
22925         {
22926           hash = iterative_hash_object (val1->v.val_unsigned, hash);
22927           break;
22928         }
22929       /* FALLTHRU */
22930     case DW_OP_GNU_const_type:
22931       {
22932         unsigned int byte_size
22933           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
22934         unsigned int encoding
22935           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
22936         hash = iterative_hash_object (byte_size, hash);
22937         hash = iterative_hash_object (encoding, hash);
22938         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
22939           break;
22940         hash = iterative_hash_object (val2->val_class, hash);
22941         switch (val2->val_class)
22942           {
22943           case dw_val_class_const:
22944             hash = iterative_hash_object (val2->v.val_int, hash);
22945             break;
22946           case dw_val_class_vec:
22947             {
22948               unsigned int elt_size = val2->v.val_vec.elt_size;
22949               unsigned int len = val2->v.val_vec.length;
22950
22951               hash = iterative_hash_object (elt_size, hash);
22952               hash = iterative_hash_object (len, hash);
22953               hash = iterative_hash (val2->v.val_vec.array,
22954                                      len * elt_size, hash);
22955             }
22956             break;
22957           case dw_val_class_const_double:
22958             hash = iterative_hash_object (val2->v.val_double.low, hash);
22959             hash = iterative_hash_object (val2->v.val_double.high, hash);
22960             break;
22961           default:
22962             gcc_unreachable ();
22963           }
22964       }
22965       break;
22966
22967     default:
22968       /* Other codes have no operands.  */
22969       break;
22970     }
22971   return hash;
22972 }
22973
22974 /* Iteratively hash the whole DWARF location expression LOC.  */
22975
22976 static inline hashval_t
22977 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22978 {
22979   dw_loc_descr_ref l;
22980   bool sizes_computed = false;
22981   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22982   size_of_locs (loc);
22983
22984   for (l = loc; l != NULL; l = l->dw_loc_next)
22985     {
22986       enum dwarf_location_atom opc = l->dw_loc_opc;
22987       hash = iterative_hash_object (opc, hash);
22988       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22989         {
22990           size_of_locs (loc);
22991           sizes_computed = true;
22992         }
22993       hash = hash_loc_operands (l, hash);
22994     }
22995   return hash;
22996 }
22997
22998 /* Compute hash of the whole location list LIST_HEAD.  */
22999
23000 static inline void
23001 hash_loc_list (dw_loc_list_ref list_head)
23002 {
23003   dw_loc_list_ref curr = list_head;
23004   hashval_t hash = 0;
23005
23006   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
23007     {
23008       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
23009       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
23010       if (curr->section)
23011         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
23012                                hash);
23013       hash = hash_locs (curr->expr, hash);
23014     }
23015   list_head->hash = hash;
23016 }
23017
23018 /* Return true if X and Y opcodes have the same operands.  */
23019
23020 static inline bool
23021 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
23022 {
23023   dw_val_ref valx1 = &x->dw_loc_oprnd1;
23024   dw_val_ref valx2 = &x->dw_loc_oprnd2;
23025   dw_val_ref valy1 = &y->dw_loc_oprnd1;
23026   dw_val_ref valy2 = &y->dw_loc_oprnd2;
23027
23028   switch (x->dw_loc_opc)
23029     {
23030     case DW_OP_const4u:
23031     case DW_OP_const8u:
23032       if (x->dtprel)
23033         goto hash_addr;
23034       /* FALLTHRU */
23035     case DW_OP_const1u:
23036     case DW_OP_const1s:
23037     case DW_OP_const2u:
23038     case DW_OP_const2s:
23039     case DW_OP_const4s:
23040     case DW_OP_const8s:
23041     case DW_OP_constu:
23042     case DW_OP_consts:
23043     case DW_OP_pick:
23044     case DW_OP_plus_uconst:
23045     case DW_OP_breg0:
23046     case DW_OP_breg1:
23047     case DW_OP_breg2:
23048     case DW_OP_breg3:
23049     case DW_OP_breg4:
23050     case DW_OP_breg5:
23051     case DW_OP_breg6:
23052     case DW_OP_breg7:
23053     case DW_OP_breg8:
23054     case DW_OP_breg9:
23055     case DW_OP_breg10:
23056     case DW_OP_breg11:
23057     case DW_OP_breg12:
23058     case DW_OP_breg13:
23059     case DW_OP_breg14:
23060     case DW_OP_breg15:
23061     case DW_OP_breg16:
23062     case DW_OP_breg17:
23063     case DW_OP_breg18:
23064     case DW_OP_breg19:
23065     case DW_OP_breg20:
23066     case DW_OP_breg21:
23067     case DW_OP_breg22:
23068     case DW_OP_breg23:
23069     case DW_OP_breg24:
23070     case DW_OP_breg25:
23071     case DW_OP_breg26:
23072     case DW_OP_breg27:
23073     case DW_OP_breg28:
23074     case DW_OP_breg29:
23075     case DW_OP_breg30:
23076     case DW_OP_breg31:
23077     case DW_OP_regx:
23078     case DW_OP_fbreg:
23079     case DW_OP_piece:
23080     case DW_OP_deref_size:
23081     case DW_OP_xderef_size:
23082       return valx1->v.val_int == valy1->v.val_int;
23083     case DW_OP_skip:
23084     case DW_OP_bra:
23085       /* If splitting debug info, the use of DW_OP_GNU_addr_index
23086         can cause irrelevant differences in dw_loc_addr.  */
23087       gcc_assert (valx1->val_class == dw_val_class_loc
23088                   && valy1->val_class == dw_val_class_loc
23089                   && (dwarf_split_debug_info
23090                       || x->dw_loc_addr == y->dw_loc_addr));
23091       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
23092     case DW_OP_implicit_value:
23093       if (valx1->v.val_unsigned != valy1->v.val_unsigned
23094           || valx2->val_class != valy2->val_class)
23095         return false;
23096       switch (valx2->val_class)
23097         {
23098         case dw_val_class_const:
23099           return valx2->v.val_int == valy2->v.val_int;
23100         case dw_val_class_vec:
23101           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23102                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23103                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23104                             valx2->v.val_vec.elt_size
23105                             * valx2->v.val_vec.length) == 0;
23106         case dw_val_class_const_double:
23107           return valx2->v.val_double.low == valy2->v.val_double.low
23108                  && valx2->v.val_double.high == valy2->v.val_double.high;
23109         case dw_val_class_addr:
23110           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
23111         default:
23112           gcc_unreachable ();
23113         }
23114     case DW_OP_bregx:
23115     case DW_OP_bit_piece:
23116       return valx1->v.val_int == valy1->v.val_int
23117              && valx2->v.val_int == valy2->v.val_int;
23118     case DW_OP_addr:
23119     hash_addr:
23120       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
23121     case DW_OP_GNU_addr_index:
23122     case DW_OP_GNU_const_index:
23123       {
23124         rtx ax1 = valx1->val_entry->addr.rtl;
23125         rtx ay1 = valy1->val_entry->addr.rtl;
23126         return rtx_equal_p (ax1, ay1);
23127       }
23128     case DW_OP_GNU_implicit_pointer:
23129       return valx1->val_class == dw_val_class_die_ref
23130              && valx1->val_class == valy1->val_class
23131              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
23132              && valx2->v.val_int == valy2->v.val_int;
23133     case DW_OP_GNU_entry_value:
23134       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
23135     case DW_OP_GNU_const_type:
23136       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
23137           || valx2->val_class != valy2->val_class)
23138         return false;
23139       switch (valx2->val_class)
23140         {
23141         case dw_val_class_const:
23142           return valx2->v.val_int == valy2->v.val_int;
23143         case dw_val_class_vec:
23144           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
23145                  && valx2->v.val_vec.length == valy2->v.val_vec.length
23146                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
23147                             valx2->v.val_vec.elt_size
23148                             * valx2->v.val_vec.length) == 0;
23149         case dw_val_class_const_double:
23150           return valx2->v.val_double.low == valy2->v.val_double.low
23151                  && valx2->v.val_double.high == valy2->v.val_double.high;
23152         default:
23153           gcc_unreachable ();
23154         }
23155     case DW_OP_GNU_regval_type:
23156     case DW_OP_GNU_deref_type:
23157       return valx1->v.val_int == valy1->v.val_int
23158              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
23159     case DW_OP_GNU_convert:
23160     case DW_OP_GNU_reinterpret:
23161       if (valx1->val_class != valy1->val_class)
23162         return false;
23163       if (valx1->val_class == dw_val_class_unsigned_const)
23164         return valx1->v.val_unsigned == valy1->v.val_unsigned;
23165       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23166     case DW_OP_GNU_parameter_ref:
23167       return valx1->val_class == dw_val_class_die_ref
23168              && valx1->val_class == valy1->val_class
23169              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
23170     default:
23171       /* Other codes have no operands.  */
23172       return true;
23173     }
23174 }
23175
23176 /* Return true if DWARF location expressions X and Y are the same.  */
23177
23178 static inline bool
23179 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
23180 {
23181   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
23182     if (x->dw_loc_opc != y->dw_loc_opc
23183         || x->dtprel != y->dtprel
23184         || !compare_loc_operands (x, y))
23185       break;
23186   return x == NULL && y == NULL;
23187 }
23188
23189 /* Return precomputed hash of location list X.  */
23190
23191 static hashval_t
23192 loc_list_hash (const void *x)
23193 {
23194   return ((const struct dw_loc_list_struct *) x)->hash;
23195 }
23196
23197 /* Return 1 if location lists X and Y are the same.  */
23198
23199 static int
23200 loc_list_eq (const void *x, const void *y)
23201 {
23202   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
23203   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
23204   if (a == b)
23205     return 1;
23206   if (a->hash != b->hash)
23207     return 0;
23208   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
23209     if (strcmp (a->begin, b->begin) != 0
23210         || strcmp (a->end, b->end) != 0
23211         || (a->section == NULL) != (b->section == NULL)
23212         || (a->section && strcmp (a->section, b->section) != 0)
23213         || !compare_locs (a->expr, b->expr))
23214       break;
23215   return a == NULL && b == NULL;
23216 }
23217
23218 /* Recursively optimize location lists referenced from DIE
23219    children and share them whenever possible.  */
23220
23221 static void
23222 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
23223 {
23224   dw_die_ref c;
23225   dw_attr_ref a;
23226   unsigned ix;
23227   void **slot;
23228
23229   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23230     if (AT_class (a) == dw_val_class_loc_list)
23231       {
23232         dw_loc_list_ref list = AT_loc_list (a);
23233         /* TODO: perform some optimizations here, before hashing
23234            it and storing into the hash table.  */
23235         hash_loc_list (list);
23236         slot = htab_find_slot_with_hash (htab, list, list->hash,
23237                                          INSERT);
23238         if (*slot == NULL)
23239           *slot = (void *) list;
23240         else
23241           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
23242       }
23243
23244   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
23245 }
23246
23247
23248 /* Recursively assign each location list a unique index into the debug_addr
23249    section.  */
23250
23251 static void
23252 index_location_lists (dw_die_ref die)
23253 {
23254   dw_die_ref c;
23255   dw_attr_ref a;
23256   unsigned ix;
23257
23258   FOR_EACH_VEC_SAFE_ELT (die->die_attr, ix, a)
23259     if (AT_class (a) == dw_val_class_loc_list)
23260       {
23261         dw_loc_list_ref list = AT_loc_list (a);
23262         dw_loc_list_ref curr;
23263         for (curr = list; curr != NULL; curr = curr->dw_loc_next)
23264           {
23265             /* Don't index an entry that has already been indexed
23266                or won't be output.  */
23267             if (curr->begin_entry != NULL
23268                 || (strcmp (curr->begin, curr->end) == 0 && !curr->force))
23269               continue;
23270
23271             curr->begin_entry
23272                 = add_addr_table_entry (xstrdup (curr->begin),
23273                                         ate_kind_label);
23274           }
23275       }
23276
23277   FOR_EACH_CHILD (die, c, index_location_lists (c));
23278 }
23279
23280 /* Optimize location lists referenced from DIE
23281    children and share them whenever possible.  */
23282
23283 static void
23284 optimize_location_lists (dw_die_ref die)
23285 {
23286   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
23287   optimize_location_lists_1 (die, htab);
23288   htab_delete (htab);
23289 }
23290 \f
23291 /* Output stuff that dwarf requires at the end of every file,
23292    and generate the DWARF-2 debugging info.  */
23293
23294 static void
23295 dwarf2out_finish (const char *filename)
23296 {
23297   limbo_die_node *node, *next_node;
23298   comdat_type_node *ctnode;
23299   htab_t comdat_type_table;
23300   unsigned int i;
23301   dw_die_ref main_comp_unit_die;
23302
23303   /* PCH might result in DW_AT_producer string being restored from the
23304      header compilation, so always fill it with empty string initially
23305      and overwrite only here.  */
23306   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
23307   producer_string = gen_producer_string ();
23308   producer->dw_attr_val.v.val_str->refcount--;
23309   producer->dw_attr_val.v.val_str = find_AT_string (producer_string);
23310
23311   gen_scheduled_generic_parms_dies ();
23312   gen_remaining_tmpl_value_param_die_attribute ();
23313
23314   /* Add the name for the main input file now.  We delayed this from
23315      dwarf2out_init to avoid complications with PCH.  */
23316   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
23317   if (!IS_ABSOLUTE_PATH (filename) || targetm.force_at_comp_dir)
23318     add_comp_dir_attribute (comp_unit_die ());
23319   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
23320     {
23321       bool p = false;
23322       htab_traverse (file_table, file_table_relative_p, &p);
23323       if (p)
23324         add_comp_dir_attribute (comp_unit_die ());
23325     }
23326
23327   if (deferred_locations_list)
23328     for (i = 0; i < deferred_locations_list->length (); i++)
23329       {
23330         add_location_or_const_value_attribute (
23331             (*deferred_locations_list)[i].die,
23332             (*deferred_locations_list)[i].variable,
23333             false,
23334             DW_AT_location);
23335       }
23336
23337   /* Traverse the limbo die list, and add parent/child links.  The only
23338      dies without parents that should be here are concrete instances of
23339      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
23340      For concrete instances, we can get the parent die from the abstract
23341      instance.  */
23342   for (node = limbo_die_list; node; node = next_node)
23343     {
23344       dw_die_ref die = node->die;
23345       next_node = node->next;
23346
23347       if (die->die_parent == NULL)
23348         {
23349           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
23350
23351           if (origin && origin->die_parent)
23352             add_child_die (origin->die_parent, die);
23353           else if (is_cu_die (die))
23354             ;
23355           else if (seen_error ())
23356             /* It's OK to be confused by errors in the input.  */
23357             add_child_die (comp_unit_die (), die);
23358           else
23359             {
23360               /* In certain situations, the lexical block containing a
23361                  nested function can be optimized away, which results
23362                  in the nested function die being orphaned.  Likewise
23363                  with the return type of that nested function.  Force
23364                  this to be a child of the containing function.
23365
23366                  It may happen that even the containing function got fully
23367                  inlined and optimized out.  In that case we are lost and
23368                  assign the empty child.  This should not be big issue as
23369                  the function is likely unreachable too.  */
23370               gcc_assert (node->created_for);
23371
23372               if (DECL_P (node->created_for))
23373                 origin = get_context_die (DECL_CONTEXT (node->created_for));
23374               else if (TYPE_P (node->created_for))
23375                 origin = scope_die_for (node->created_for, comp_unit_die ());
23376               else
23377                 origin = comp_unit_die ();
23378
23379               add_child_die (origin, die);
23380             }
23381         }
23382     }
23383
23384   limbo_die_list = NULL;
23385
23386 #if ENABLE_ASSERT_CHECKING
23387   {
23388     dw_die_ref die = comp_unit_die (), c;
23389     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
23390   }
23391 #endif
23392   resolve_addr (comp_unit_die ());
23393   move_marked_base_types ();
23394
23395   for (node = deferred_asm_name; node; node = node->next)
23396     {
23397       tree decl = node->created_for;
23398       /* When generating LTO bytecode we can not generate new assembler
23399          names at this point and all important decls got theirs via
23400          free-lang-data.  */
23401       if ((!flag_generate_lto || DECL_ASSEMBLER_NAME_SET_P (decl))
23402           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
23403         {
23404           add_linkage_attr (node->die, decl);
23405           move_linkage_attr (node->die);
23406         }
23407     }
23408
23409   deferred_asm_name = NULL;
23410
23411   /* Walk through the list of incomplete types again, trying once more to
23412      emit full debugging info for them.  */
23413   retry_incomplete_types ();
23414
23415   if (flag_eliminate_unused_debug_types)
23416     prune_unused_types ();
23417
23418   /* Generate separate COMDAT sections for type DIEs. */
23419   if (use_debug_types)
23420     {
23421       break_out_comdat_types (comp_unit_die ());
23422
23423       /* Each new type_unit DIE was added to the limbo die list when created.
23424          Since these have all been added to comdat_type_list, clear the
23425          limbo die list.  */
23426       limbo_die_list = NULL;
23427
23428       /* For each new comdat type unit, copy declarations for incomplete
23429          types to make the new unit self-contained (i.e., no direct
23430          references to the main compile unit).  */
23431       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23432         copy_decls_for_unworthy_types (ctnode->root_die);
23433       copy_decls_for_unworthy_types (comp_unit_die ());
23434
23435       /* In the process of copying declarations from one unit to another,
23436          we may have left some declarations behind that are no longer
23437          referenced.  Prune them.  */
23438       prune_unused_types ();
23439     }
23440
23441   /* Generate separate CUs for each of the include files we've seen.
23442      They will go into limbo_die_list.  */
23443   if (flag_eliminate_dwarf2_dups)
23444     break_out_includes (comp_unit_die ());
23445
23446   /* Traverse the DIE's and add add sibling attributes to those DIE's
23447      that have children.  */
23448   add_sibling_attributes (comp_unit_die ());
23449   for (node = limbo_die_list; node; node = node->next)
23450     add_sibling_attributes (node->die);
23451   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23452     add_sibling_attributes (ctnode->root_die);
23453
23454   /* When splitting DWARF info, we put some attributes in the
23455      skeleton compile_unit DIE that remains in the .o, while
23456      most attributes go in the DWO compile_unit_die.  */
23457   if (dwarf_split_debug_info)
23458     main_comp_unit_die = gen_compile_unit_die (NULL);
23459   else
23460     main_comp_unit_die = comp_unit_die ();
23461
23462   /* Output a terminator label for the .text section.  */
23463   switch_to_section (text_section);
23464   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
23465   if (cold_text_section)
23466     {
23467       switch_to_section (cold_text_section);
23468       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
23469     }
23470
23471   /* We can only use the low/high_pc attributes if all of the code was
23472      in .text.  */
23473   if (!have_multiple_function_sections 
23474       || (dwarf_version < 3 && dwarf_strict))
23475     {
23476       /* Don't add if the CU has no associated code.  */
23477       if (text_section_used)
23478         add_AT_low_high_pc (main_comp_unit_die, text_section_label,
23479                             text_end_label, true);
23480     }
23481   else
23482     {
23483       unsigned fde_idx;
23484       dw_fde_ref fde;
23485       bool range_list_added = false;
23486
23487       if (text_section_used)
23488         add_ranges_by_labels (main_comp_unit_die, text_section_label,
23489                               text_end_label, &range_list_added, true);
23490       if (cold_text_section_used)
23491         add_ranges_by_labels (main_comp_unit_die, cold_text_section_label,
23492                               cold_end_label, &range_list_added, true);
23493
23494       FOR_EACH_VEC_ELT (*fde_vec, fde_idx, fde)
23495         {
23496           if (DECL_IGNORED_P (fde->decl))
23497             continue;
23498           if (!fde->in_std_section)
23499             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_begin,
23500                                   fde->dw_fde_end, &range_list_added,
23501                                   true);
23502           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
23503             add_ranges_by_labels (main_comp_unit_die, fde->dw_fde_second_begin,
23504                                   fde->dw_fde_second_end, &range_list_added,
23505                                   true);
23506         }
23507
23508       if (range_list_added)
23509         {
23510           /* We need to give .debug_loc and .debug_ranges an appropriate
23511              "base address".  Use zero so that these addresses become
23512              absolute.  Historically, we've emitted the unexpected
23513              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
23514              Emit both to give time for other tools to adapt.  */
23515           add_AT_addr (main_comp_unit_die, DW_AT_low_pc, const0_rtx, true);
23516           if (! dwarf_strict && dwarf_version < 4)
23517             add_AT_addr (main_comp_unit_die, DW_AT_entry_pc, const0_rtx, true);
23518
23519           add_ranges (NULL);
23520         }
23521     }
23522
23523   if (debug_info_level >= DINFO_LEVEL_NORMAL)
23524     add_AT_lineptr (main_comp_unit_die, DW_AT_stmt_list,
23525                     debug_line_section_label);
23526
23527   if (have_macinfo)
23528     add_AT_macptr (comp_unit_die (),
23529                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
23530                    macinfo_section_label);
23531
23532   if (dwarf_split_debug_info && addr_index_table != NULL)
23533     {
23534       /* optimize_location_lists calculates the size of the lists,
23535          so index them first, and assign indices to the entries.
23536          Although optimize_location_lists will remove entries from
23537          the table, it only does so for duplicates, and therefore
23538          only reduces ref_counts to 1.  */
23539       unsigned int index = 0;
23540       index_location_lists (comp_unit_die ());
23541       htab_traverse_noresize (addr_index_table,
23542                               index_addr_table_entry, &index);
23543     }
23544   if (have_location_lists)
23545     optimize_location_lists (comp_unit_die ());
23546
23547   save_macinfo_strings ();
23548   if (dwarf_split_debug_info)
23549     {
23550       unsigned int index = 0;
23551       htab_traverse_noresize (debug_str_hash, index_string, &index);
23552     }
23553
23554   /* Output all of the compilation units.  We put the main one last so that
23555      the offsets are available to output_pubnames.  */
23556   for (node = limbo_die_list; node; node = node->next)
23557     output_comp_unit (node->die, 0);
23558
23559   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
23560   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
23561     {
23562       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
23563
23564       /* Don't output duplicate types.  */
23565       if (*slot != HTAB_EMPTY_ENTRY)
23566         continue;
23567
23568       /* Add a pointer to the line table for the main compilation unit
23569          so that the debugger can make sense of DW_AT_decl_file
23570          attributes.  */
23571       if (debug_info_level >= DINFO_LEVEL_NORMAL)
23572         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
23573                         (!dwarf_split_debug_info
23574                          ? debug_line_section_label
23575                          : debug_skeleton_line_section_label));
23576
23577       output_comdat_type_unit (ctnode);
23578       *slot = ctnode;
23579     }
23580   htab_delete (comdat_type_table);
23581
23582   /* The AT_pubnames attribute needs to go in all skeleton dies, including
23583      both the main_cu and all skeleton TUs.  Making this call unconditional
23584      would end up either adding a second copy of the AT_pubnames attribute, or
23585      requiring a special case in add_top_level_skeleton_die_attrs.  */
23586   if (!dwarf_split_debug_info)
23587     add_AT_pubnames (comp_unit_die ());
23588
23589   if (dwarf_split_debug_info)
23590     {
23591       int mark;
23592       unsigned char checksum[16];
23593       struct md5_ctx ctx;
23594
23595       /* Compute a checksum of the comp_unit to use as the dwo_id.  */
23596       md5_init_ctx (&ctx);
23597       mark = 0;
23598       die_checksum (comp_unit_die (), &ctx, &mark);
23599       unmark_all_dies (comp_unit_die ());
23600       md5_finish_ctx (&ctx, checksum);
23601
23602       /* Use the first 8 bytes of the checksum as the dwo_id,
23603         and add it to both comp-unit DIEs.  */
23604       add_AT_data8 (main_comp_unit_die, DW_AT_GNU_dwo_id, checksum);
23605       add_AT_data8 (comp_unit_die (), DW_AT_GNU_dwo_id, checksum);
23606
23607       /* Add the base offset of the ranges table to the skeleton
23608         comp-unit DIE.  */
23609       if (ranges_table_in_use)
23610         add_AT_lineptr (main_comp_unit_die, DW_AT_GNU_ranges_base,
23611                         ranges_section_label);
23612
23613       switch_to_section (debug_addr_section);
23614       ASM_OUTPUT_LABEL (asm_out_file, debug_addr_section_label);
23615       output_addr_table ();
23616     }
23617
23618   /* Output the main compilation unit if non-empty or if .debug_macinfo
23619      or .debug_macro will be emitted.  */
23620   output_comp_unit (comp_unit_die (), have_macinfo);
23621
23622   if (dwarf_split_debug_info && info_section_emitted)
23623     output_skeleton_debug_sections (main_comp_unit_die);
23624
23625   /* Output the abbreviation table.  */
23626   if (abbrev_die_table_in_use != 1)
23627     {
23628       switch_to_section (debug_abbrev_section);
23629       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
23630       output_abbrev_section ();
23631     }
23632
23633   /* Output location list section if necessary.  */
23634   if (have_location_lists)
23635     {
23636       /* Output the location lists info.  */
23637       switch_to_section (debug_loc_section);
23638       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
23639       output_location_lists (comp_unit_die ());
23640     }
23641
23642   /* Output public names and types tables if necessary.  */
23643   output_pubnames (pubname_table);
23644   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
23645      It shouldn't hurt to emit it always, since pure DWARF2 consumers
23646      simply won't look for the section.  */
23647   output_pubnames (pubtype_table);
23648
23649   /* Output the address range information if a CU (.debug_info section)
23650      was emitted.  We output an empty table even if we had no functions
23651      to put in it.  This because the consumer has no way to tell the
23652      difference between an empty table that we omitted and failure to
23653      generate a table that would have contained data.  */
23654   if (info_section_emitted)
23655     {
23656       unsigned long aranges_length = size_of_aranges ();
23657
23658       switch_to_section (debug_aranges_section);
23659       output_aranges (aranges_length);
23660     }
23661
23662   /* Output ranges section if necessary.  */
23663   if (ranges_table_in_use)
23664     {
23665       switch_to_section (debug_ranges_section);
23666       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
23667       output_ranges ();
23668     }
23669
23670   /* Have to end the macro section.  */
23671   if (have_macinfo)
23672     {
23673       switch_to_section (debug_macinfo_section);
23674       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
23675       output_macinfo ();
23676       dw2_asm_output_data (1, 0, "End compilation unit");
23677     }
23678
23679   /* Output the source line correspondence table.  We must do this
23680      even if there is no line information.  Otherwise, on an empty
23681      translation unit, we will generate a present, but empty,
23682      .debug_info section.  IRIX 6.5 `nm' will then complain when
23683      examining the file.  This is done late so that any filenames
23684      used by the debug_info section are marked as 'used'.  */
23685   switch_to_section (debug_line_section);
23686   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
23687   if (! DWARF2_ASM_LINE_DEBUG_INFO)
23688     output_line_info (false);
23689
23690   if (dwarf_split_debug_info && info_section_emitted)
23691     {
23692       switch_to_section (debug_skeleton_line_section);
23693       ASM_OUTPUT_LABEL (asm_out_file, debug_skeleton_line_section_label);
23694       output_line_info (true);
23695     }
23696
23697   /* If we emitted any indirect strings, output the string table too.  */
23698   if (debug_str_hash)
23699     output_indirect_strings ();
23700 }
23701
23702 #include "gt-dwarf2out.h"