OSDN Git Service

* dwarf2.h (DW_AT_GNU_macros): New.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97 #include "opts.h"
98
99 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
100 static rtx last_var_location_insn;
101
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
104
105 /* Define this macro to be a nonzero value if the directory specifications
106     which are output in the debug info should end with a separator.  */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109    from generating indirect strings in DWARF2 debug information, for instance
110    if your target is stuck with an old version of GDB that is unable to
111    process them properly or uses VMS Debug.  */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
117
118 /* ??? Poison these here until it can be done generically.  They've been
119    totally replaced in this file; make sure it stays that way.  */
120 #undef DWARF2_UNWIND_INFO
121 #undef DWARF2_FRAME_INFO
122 #if (GCC_VERSION >= 3000)
123  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
124 #endif
125
126 /* The size of the target's pointer type.  */
127 #ifndef PTR_SIZE
128 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
129 #endif
130
131 /* Array of RTXes referenced by the debugging information, which therefore
132    must be kept around forever.  */
133 static GTY(()) VEC(rtx,gc) *used_rtx_array;
134
135 /* A pointer to the base of a list of incomplete types which might be
136    completed at some later time.  incomplete_types_list needs to be a
137    VEC(tree,gc) because we want to tell the garbage collector about
138    it.  */
139 static GTY(()) VEC(tree,gc) *incomplete_types;
140
141 /* A pointer to the base of a table of references to declaration
142    scopes.  This table is a display which tracks the nesting
143    of declaration scopes at the current scope and containing
144    scopes.  This table is used to find the proper place to
145    define type declaration DIE's.  */
146 static GTY(()) VEC(tree,gc) *decl_scope_table;
147
148 /* Pointers to various DWARF2 sections.  */
149 static GTY(()) section *debug_info_section;
150 static GTY(()) section *debug_abbrev_section;
151 static GTY(()) section *debug_aranges_section;
152 static GTY(()) section *debug_macinfo_section;
153 static GTY(()) section *debug_line_section;
154 static GTY(()) section *debug_loc_section;
155 static GTY(()) section *debug_pubnames_section;
156 static GTY(()) section *debug_pubtypes_section;
157 static GTY(()) section *debug_str_section;
158 static GTY(()) section *debug_ranges_section;
159 static GTY(()) section *debug_frame_section;
160
161 /* Maximum size (in bytes) of an artificially generated label.  */
162 #define MAX_ARTIFICIAL_LABEL_BYTES      30
163
164 /* According to the (draft) DWARF 3 specification, the initial length
165    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
166    bytes are 0xffffffff, followed by the length stored in the next 8
167    bytes.
168
169    However, the SGI/MIPS ABI uses an initial length which is equal to
170    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
171
172 #ifndef DWARF_INITIAL_LENGTH_SIZE
173 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
174 #endif
175
176 /* Round SIZE up to the nearest BOUNDARY.  */
177 #define DWARF_ROUND(SIZE,BOUNDARY) \
178   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
179
180 /* CIE identifier.  */
181 #if HOST_BITS_PER_WIDE_INT >= 64
182 #define DWARF_CIE_ID \
183   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
184 #else
185 #define DWARF_CIE_ID DW_CIE_ID
186 #endif
187
188 DEF_VEC_P (dw_fde_ref);
189 DEF_VEC_ALLOC_P (dw_fde_ref, gc);
190
191 /* A vector for a table that contains frame description
192    information for each routine.  */
193 static GTY(()) VEC(dw_fde_ref, gc) *fde_vec;
194
195 struct GTY(()) indirect_string_node {
196   const char *str;
197   unsigned int refcount;
198   enum dwarf_form form;
199   char *label;
200 };
201
202 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
203
204 static GTY(()) int dw2_string_counter;
205
206 /* True if the compilation unit places functions in more than one section.  */
207 static GTY(()) bool have_multiple_function_sections = false;
208
209 /* Whether the default text and cold text sections have been used at all.  */
210
211 static GTY(()) bool text_section_used = false;
212 static GTY(()) bool cold_text_section_used = false;
213
214 /* The default cold text section.  */
215 static GTY(()) section *cold_text_section;
216
217 /* Forward declarations for functions defined in this file.  */
218
219 static char *stripattributes (const char *);
220 static void output_call_frame_info (int);
221 static void dwarf2out_note_section_used (void);
222
223 /* Personality decl of current unit.  Used only when assembler does not support
224    personality CFI.  */
225 static GTY(()) rtx current_unit_personality;
226
227 /* Data and reference forms for relocatable data.  */
228 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
229 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
230
231 #ifndef DEBUG_FRAME_SECTION
232 #define DEBUG_FRAME_SECTION     ".debug_frame"
233 #endif
234
235 #ifndef FUNC_BEGIN_LABEL
236 #define FUNC_BEGIN_LABEL        "LFB"
237 #endif
238
239 #ifndef FUNC_END_LABEL
240 #define FUNC_END_LABEL          "LFE"
241 #endif
242
243 #ifndef PROLOGUE_END_LABEL
244 #define PROLOGUE_END_LABEL      "LPE"
245 #endif
246
247 #ifndef EPILOGUE_BEGIN_LABEL
248 #define EPILOGUE_BEGIN_LABEL    "LEB"
249 #endif
250
251 #ifndef FRAME_BEGIN_LABEL
252 #define FRAME_BEGIN_LABEL       "Lframe"
253 #endif
254 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
255 #define CIE_END_LABEL           "LECIE"
256 #define FDE_LABEL               "LSFDE"
257 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
258 #define FDE_END_LABEL           "LEFDE"
259 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
260 #define LINE_NUMBER_END_LABEL   "LELT"
261 #define LN_PROLOG_AS_LABEL      "LASLTP"
262 #define LN_PROLOG_END_LABEL     "LELTP"
263 #define DIE_LABEL_PREFIX        "DW"
264 \f
265 /* Match the base name of a file to the base name of a compilation unit. */
266
267 static int
268 matches_main_base (const char *path)
269 {
270   /* Cache the last query. */
271   static const char *last_path = NULL;
272   static int last_match = 0;
273   if (path != last_path)
274     {
275       const char *base;
276       int length = base_of_path (path, &base);
277       last_path = path;
278       last_match = (length == main_input_baselength
279                     && memcmp (base, main_input_basename, length) == 0);
280     }
281   return last_match;
282 }
283
284 #ifdef DEBUG_DEBUG_STRUCT
285
286 static int
287 dump_struct_debug (tree type, enum debug_info_usage usage,
288                    enum debug_struct_file criterion, int generic,
289                    int matches, int result)
290 {
291   /* Find the type name. */
292   tree type_decl = TYPE_STUB_DECL (type);
293   tree t = type_decl;
294   const char *name = 0;
295   if (TREE_CODE (t) == TYPE_DECL)
296     t = DECL_NAME (t);
297   if (t)
298     name = IDENTIFIER_POINTER (t);
299
300   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
301            criterion,
302            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
303            matches ? "bas" : "hdr",
304            generic ? "gen" : "ord",
305            usage == DINFO_USAGE_DFN ? ";" :
306              usage == DINFO_USAGE_DIR_USE ? "." : "*",
307            result,
308            (void*) type_decl, name);
309   return result;
310 }
311 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
312   dump_struct_debug (type, usage, criterion, generic, matches, result)
313
314 #else
315
316 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
317   (result)
318
319 #endif
320
321 static bool
322 should_emit_struct_debug (tree type, enum debug_info_usage usage)
323 {
324   enum debug_struct_file criterion;
325   tree type_decl;
326   bool generic = lang_hooks.types.generic_p (type);
327
328   if (generic)
329     criterion = debug_struct_generic[usage];
330   else
331     criterion = debug_struct_ordinary[usage];
332
333   if (criterion == DINFO_STRUCT_FILE_NONE)
334     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
335   if (criterion == DINFO_STRUCT_FILE_ANY)
336     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
337
338   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
339
340   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
341     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
342
343   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
344     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
345   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
346 }
347 \f
348 /* Return a pointer to a copy of the section string name S with all
349    attributes stripped off, and an asterisk prepended (for assemble_name).  */
350
351 static inline char *
352 stripattributes (const char *s)
353 {
354   char *stripped = XNEWVEC (char, strlen (s) + 2);
355   char *p = stripped;
356
357   *p++ = '*';
358
359   while (*s && *s != ',')
360     *p++ = *s++;
361
362   *p = '\0';
363   return stripped;
364 }
365
366 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
367    switch to the data section instead, and write out a synthetic start label
368    for collect2 the first time around.  */
369
370 static void
371 switch_to_eh_frame_section (bool back)
372 {
373   tree label;
374
375 #ifdef EH_FRAME_SECTION_NAME
376   if (eh_frame_section == 0)
377     {
378       int flags;
379
380       if (EH_TABLES_CAN_BE_READ_ONLY)
381         {
382           int fde_encoding;
383           int per_encoding;
384           int lsda_encoding;
385
386           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
387                                                        /*global=*/0);
388           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
389                                                        /*global=*/1);
390           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
391                                                         /*global=*/0);
392           flags = ((! flag_pic
393                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
394                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
395                         && (per_encoding & 0x70) != DW_EH_PE_absptr
396                         && (per_encoding & 0x70) != DW_EH_PE_aligned
397                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
398                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
399                    ? 0 : SECTION_WRITE);
400         }
401       else
402         flags = SECTION_WRITE;
403       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
404     }
405 #endif /* EH_FRAME_SECTION_NAME */
406
407   if (eh_frame_section)
408     switch_to_section (eh_frame_section);
409   else
410     {
411       /* We have no special eh_frame section.  Put the information in
412          the data section and emit special labels to guide collect2.  */
413       switch_to_section (data_section);
414
415       if (!back)
416         {
417           label = get_file_function_name ("F");
418           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
419           targetm.asm_out.globalize_label (asm_out_file,
420                                            IDENTIFIER_POINTER (label));
421           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
422         }
423     }
424 }
425
426 /* Switch [BACK] to the eh or debug frame table section, depending on
427    FOR_EH.  */
428
429 static void
430 switch_to_frame_table_section (int for_eh, bool back)
431 {
432   if (for_eh)
433     switch_to_eh_frame_section (back);
434   else
435     {
436       if (!debug_frame_section)
437         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
438                                            SECTION_DEBUG, NULL);
439       switch_to_section (debug_frame_section);
440     }
441 }
442
443 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
444
445 enum dw_cfi_oprnd_type
446 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
447 {
448   switch (cfi)
449     {
450     case DW_CFA_nop:
451     case DW_CFA_GNU_window_save:
452     case DW_CFA_remember_state:
453     case DW_CFA_restore_state:
454       return dw_cfi_oprnd_unused;
455
456     case DW_CFA_set_loc:
457     case DW_CFA_advance_loc1:
458     case DW_CFA_advance_loc2:
459     case DW_CFA_advance_loc4:
460     case DW_CFA_MIPS_advance_loc8:
461       return dw_cfi_oprnd_addr;
462
463     case DW_CFA_offset:
464     case DW_CFA_offset_extended:
465     case DW_CFA_def_cfa:
466     case DW_CFA_offset_extended_sf:
467     case DW_CFA_def_cfa_sf:
468     case DW_CFA_restore:
469     case DW_CFA_restore_extended:
470     case DW_CFA_undefined:
471     case DW_CFA_same_value:
472     case DW_CFA_def_cfa_register:
473     case DW_CFA_register:
474     case DW_CFA_expression:
475       return dw_cfi_oprnd_reg_num;
476
477     case DW_CFA_def_cfa_offset:
478     case DW_CFA_GNU_args_size:
479     case DW_CFA_def_cfa_offset_sf:
480       return dw_cfi_oprnd_offset;
481
482     case DW_CFA_def_cfa_expression:
483       return dw_cfi_oprnd_loc;
484
485     default:
486       gcc_unreachable ();
487     }
488 }
489
490 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
491
492 enum dw_cfi_oprnd_type
493 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
494 {
495   switch (cfi)
496     {
497     case DW_CFA_def_cfa:
498     case DW_CFA_def_cfa_sf:
499     case DW_CFA_offset:
500     case DW_CFA_offset_extended_sf:
501     case DW_CFA_offset_extended:
502       return dw_cfi_oprnd_offset;
503
504     case DW_CFA_register:
505       return dw_cfi_oprnd_reg_num;
506
507     case DW_CFA_expression:
508       return dw_cfi_oprnd_loc;
509
510     default:
511       return dw_cfi_oprnd_unused;
512     }
513 }
514
515 /* Output one FDE.  */
516
517 static void
518 output_fde (dw_fde_ref fde, bool for_eh, bool second,
519             char *section_start_label, int fde_encoding, char *augmentation,
520             bool any_lsda_needed, int lsda_encoding)
521 {
522   int ix;
523   const char *begin, *end;
524   static unsigned int j;
525   char l1[20], l2[20];
526   dw_cfi_ref cfi;
527
528   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
529                                      /* empty */ 0);
530   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
531                                   for_eh + j);
532   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
533   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
534   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
535     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
536                          " indicating 64-bit DWARF extension");
537   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
538                         "FDE Length");
539   ASM_OUTPUT_LABEL (asm_out_file, l1);
540
541   if (for_eh)
542     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
543   else
544     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
545                            debug_frame_section, "FDE CIE offset");
546
547   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
548   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
549
550   if (for_eh)
551     {
552       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
553       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
554       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
555                                        "FDE initial location");
556       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
557                             end, begin, "FDE address range");
558     }
559   else
560     {
561       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
562       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
563     }
564
565   if (augmentation[0])
566     {
567       if (any_lsda_needed)
568         {
569           int size = size_of_encoded_value (lsda_encoding);
570
571           if (lsda_encoding == DW_EH_PE_aligned)
572             {
573               int offset = (  4         /* Length */
574                             + 4         /* CIE offset */
575                             + 2 * size_of_encoded_value (fde_encoding)
576                             + 1         /* Augmentation size */ );
577               int pad = -offset & (PTR_SIZE - 1);
578
579               size += pad;
580               gcc_assert (size_of_uleb128 (size) == 1);
581             }
582
583           dw2_asm_output_data_uleb128 (size, "Augmentation size");
584
585           if (fde->uses_eh_lsda)
586             {
587               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
588                                            fde->funcdef_number);
589               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
590                                                gen_rtx_SYMBOL_REF (Pmode, l1),
591                                                false,
592                                                "Language Specific Data Area");
593             }
594           else
595             {
596               if (lsda_encoding == DW_EH_PE_aligned)
597                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
598               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
599                                    "Language Specific Data Area (none)");
600             }
601         }
602       else
603         dw2_asm_output_data_uleb128 (0, "Augmentation size");
604     }
605
606   /* Loop through the Call Frame Instructions associated with
607      this FDE.  */
608   fde->dw_fde_current_label = begin;
609   if (fde->dw_fde_second_begin == NULL)
610     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
611       output_cfi (cfi, fde, for_eh);
612   else if (!second)
613     {
614       if (fde->dw_fde_switch_cfi_index > 0)
615         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
616           {
617             if (ix == fde->dw_fde_switch_cfi_index)
618               break;
619             output_cfi (cfi, fde, for_eh);
620           }
621     }
622   else
623     {
624       int i, from = 0;
625       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
626
627       if (fde->dw_fde_switch_cfi_index > 0)
628         {
629           from = fde->dw_fde_switch_cfi_index;
630           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
631         }
632       for (i = from; i < until; i++)
633         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
634                     fde, for_eh);
635     }
636
637   /* If we are to emit a ref/link from function bodies to their frame tables,
638      do it now.  This is typically performed to make sure that tables
639      associated with functions are dragged with them and not discarded in
640      garbage collecting links. We need to do this on a per function basis to
641      cope with -ffunction-sections.  */
642
643 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
644   /* Switch to the function section, emit the ref to the tables, and
645      switch *back* into the table section.  */
646   switch_to_section (function_section (fde->decl));
647   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
648   switch_to_frame_table_section (for_eh, true);
649 #endif
650
651   /* Pad the FDE out to an address sized boundary.  */
652   ASM_OUTPUT_ALIGN (asm_out_file,
653                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
654   ASM_OUTPUT_LABEL (asm_out_file, l2);
655
656   j += 2;
657 }
658
659 /* Return true if frame description entry FDE is needed for EH.  */
660
661 static bool
662 fde_needed_for_eh_p (dw_fde_ref fde)
663 {
664   if (flag_asynchronous_unwind_tables)
665     return true;
666
667   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
668     return true;
669
670   if (fde->uses_eh_lsda)
671     return true;
672
673   /* If exceptions are enabled, we have collected nothrow info.  */
674   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
675     return false;
676
677   return true;
678 }
679
680 /* Output the call frame information used to record information
681    that relates to calculating the frame pointer, and records the
682    location of saved registers.  */
683
684 static void
685 output_call_frame_info (int for_eh)
686 {
687   unsigned int i;
688   dw_fde_ref fde;
689   dw_cfi_ref cfi;
690   char l1[20], l2[20], section_start_label[20];
691   bool any_lsda_needed = false;
692   char augmentation[6];
693   int augmentation_size;
694   int fde_encoding = DW_EH_PE_absptr;
695   int per_encoding = DW_EH_PE_absptr;
696   int lsda_encoding = DW_EH_PE_absptr;
697   int return_reg;
698   rtx personality = NULL;
699   int dw_cie_version;
700
701   /* Don't emit a CIE if there won't be any FDEs.  */
702   if (fde_vec == NULL)
703     return;
704
705   /* Nothing to do if the assembler's doing it all.  */
706   if (dwarf2out_do_cfi_asm ())
707     return;
708
709   /* If we don't have any functions we'll want to unwind out of, don't emit
710      any EH unwind information.  If we make FDEs linkonce, we may have to
711      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
712      want to avoid having an FDE kept around when the function it refers to
713      is discarded.  Example where this matters: a primary function template
714      in C++ requires EH information, an explicit specialization doesn't.  */
715   if (for_eh)
716     {
717       bool any_eh_needed = false;
718
719       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
720         {
721           if (fde->uses_eh_lsda)
722             any_eh_needed = any_lsda_needed = true;
723           else if (fde_needed_for_eh_p (fde))
724             any_eh_needed = true;
725           else if (TARGET_USES_WEAK_UNWIND_INFO)
726             targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, 1, 1);
727         }
728
729       if (!any_eh_needed)
730         return;
731     }
732
733   /* We're going to be generating comments, so turn on app.  */
734   if (flag_debug_asm)
735     app_enable ();
736
737   /* Switch to the proper frame section, first time.  */
738   switch_to_frame_table_section (for_eh, false);
739
740   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
741   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
742
743   /* Output the CIE.  */
744   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
745   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
746   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
747     dw2_asm_output_data (4, 0xffffffff,
748       "Initial length escape value indicating 64-bit DWARF extension");
749   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
750                         "Length of Common Information Entry");
751   ASM_OUTPUT_LABEL (asm_out_file, l1);
752
753   /* Now that the CIE pointer is PC-relative for EH,
754      use 0 to identify the CIE.  */
755   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
756                        (for_eh ? 0 : DWARF_CIE_ID),
757                        "CIE Identifier Tag");
758
759   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
760      use CIE version 1, unless that would produce incorrect results
761      due to overflowing the return register column.  */
762   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
763   dw_cie_version = 1;
764   if (return_reg >= 256 || dwarf_version > 2)
765     dw_cie_version = 3;
766   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
767
768   augmentation[0] = 0;
769   augmentation_size = 0;
770
771   personality = current_unit_personality;
772   if (for_eh)
773     {
774       char *p;
775
776       /* Augmentation:
777          z      Indicates that a uleb128 is present to size the
778                 augmentation section.
779          L      Indicates the encoding (and thus presence) of
780                 an LSDA pointer in the FDE augmentation.
781          R      Indicates a non-default pointer encoding for
782                 FDE code pointers.
783          P      Indicates the presence of an encoding + language
784                 personality routine in the CIE augmentation.  */
785
786       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
787       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
788       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
789
790       p = augmentation + 1;
791       if (personality)
792         {
793           *p++ = 'P';
794           augmentation_size += 1 + size_of_encoded_value (per_encoding);
795           assemble_external_libcall (personality);
796         }
797       if (any_lsda_needed)
798         {
799           *p++ = 'L';
800           augmentation_size += 1;
801         }
802       if (fde_encoding != DW_EH_PE_absptr)
803         {
804           *p++ = 'R';
805           augmentation_size += 1;
806         }
807       if (p > augmentation + 1)
808         {
809           augmentation[0] = 'z';
810           *p = '\0';
811         }
812
813       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
814       if (personality && per_encoding == DW_EH_PE_aligned)
815         {
816           int offset = (  4             /* Length */
817                         + 4             /* CIE Id */
818                         + 1             /* CIE version */
819                         + strlen (augmentation) + 1     /* Augmentation */
820                         + size_of_uleb128 (1)           /* Code alignment */
821                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
822                         + 1             /* RA column */
823                         + 1             /* Augmentation size */
824                         + 1             /* Personality encoding */ );
825           int pad = -offset & (PTR_SIZE - 1);
826
827           augmentation_size += pad;
828
829           /* Augmentations should be small, so there's scarce need to
830              iterate for a solution.  Die if we exceed one uleb128 byte.  */
831           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
832         }
833     }
834
835   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
836   if (dw_cie_version >= 4)
837     {
838       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
839       dw2_asm_output_data (1, 0, "CIE Segment Size");
840     }
841   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
842   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
843                                "CIE Data Alignment Factor");
844
845   if (dw_cie_version == 1)
846     dw2_asm_output_data (1, return_reg, "CIE RA Column");
847   else
848     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
849
850   if (augmentation[0])
851     {
852       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
853       if (personality)
854         {
855           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
856                                eh_data_format_name (per_encoding));
857           dw2_asm_output_encoded_addr_rtx (per_encoding,
858                                            personality,
859                                            true, NULL);
860         }
861
862       if (any_lsda_needed)
863         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
864                              eh_data_format_name (lsda_encoding));
865
866       if (fde_encoding != DW_EH_PE_absptr)
867         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
868                              eh_data_format_name (fde_encoding));
869     }
870
871   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
872     output_cfi (cfi, NULL, for_eh);
873
874   /* Pad the CIE out to an address sized boundary.  */
875   ASM_OUTPUT_ALIGN (asm_out_file,
876                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
877   ASM_OUTPUT_LABEL (asm_out_file, l2);
878
879   /* Loop through all of the FDE's.  */
880   FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, i, fde)
881     {
882       unsigned int k;
883
884       /* Don't emit EH unwind info for leaf functions that don't need it.  */
885       if (for_eh && !fde_needed_for_eh_p (fde))
886         continue;
887
888       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
889         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
890                     augmentation, any_lsda_needed, lsda_encoding);
891     }
892
893   if (for_eh && targetm.terminate_dw2_eh_frame_info)
894     dw2_asm_output_data (4, 0, "End of Table");
895 #ifdef MIPS_DEBUGGING_INFO
896   /* Work around Irix 6 assembler bug whereby labels at the end of a section
897      get a value of 0.  Putting .align 0 after the label fixes it.  */
898   ASM_OUTPUT_ALIGN (asm_out_file, 0);
899 #endif
900
901   /* Turn off app to make assembly quicker.  */
902   if (flag_debug_asm)
903     app_disable ();
904 }
905
906 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
907
908 static void
909 dwarf2out_do_cfi_startproc (bool second)
910 {
911   int enc;
912   rtx ref;
913   rtx personality = get_personality_function (current_function_decl);
914
915   fprintf (asm_out_file, "\t.cfi_startproc\n");
916
917   if (personality)
918     {
919       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
920       ref = personality;
921
922       /* ??? The GAS support isn't entirely consistent.  We have to
923          handle indirect support ourselves, but PC-relative is done
924          in the assembler.  Further, the assembler can't handle any
925          of the weirder relocation types.  */
926       if (enc & DW_EH_PE_indirect)
927         ref = dw2_force_const_mem (ref, true);
928
929       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
930       output_addr_const (asm_out_file, ref);
931       fputc ('\n', asm_out_file);
932     }
933
934   if (crtl->uses_eh_lsda)
935     {
936       char lab[20];
937
938       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
939       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
940                                    current_function_funcdef_no);
941       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
942       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
943
944       if (enc & DW_EH_PE_indirect)
945         ref = dw2_force_const_mem (ref, true);
946
947       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
948       output_addr_const (asm_out_file, ref);
949       fputc ('\n', asm_out_file);
950     }
951 }
952
953 /* Allocate CURRENT_FDE.  Immediately initialize all we can, noting that
954    this allocation may be done before pass_final.  */
955
956 dw_fde_ref
957 dwarf2out_alloc_current_fde (void)
958 {
959   dw_fde_ref fde;
960
961   fde = ggc_alloc_cleared_dw_fde_node ();
962   fde->decl = current_function_decl;
963   fde->funcdef_number = current_function_funcdef_no;
964   fde->fde_index = VEC_length (dw_fde_ref, fde_vec);
965   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
966   fde->uses_eh_lsda = crtl->uses_eh_lsda;
967   fde->nothrow = crtl->nothrow;
968   fde->drap_reg = INVALID_REGNUM;
969   fde->vdrap_reg = INVALID_REGNUM;
970
971   /* Record the FDE associated with this function.  */
972   cfun->fde = fde;
973   VEC_safe_push (dw_fde_ref, gc, fde_vec, fde);
974
975   return fde;
976 }
977
978 /* Output a marker (i.e. a label) for the beginning of a function, before
979    the prologue.  */
980
981 void
982 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
983                           const char *file ATTRIBUTE_UNUSED)
984 {
985   char label[MAX_ARTIFICIAL_LABEL_BYTES];
986   char * dup_label;
987   dw_fde_ref fde;
988   section *fnsec;
989   bool do_frame;
990
991   current_function_func_begin_label = NULL;
992
993   do_frame = dwarf2out_do_frame ();
994
995   /* ??? current_function_func_begin_label is also used by except.c for
996      call-site information.  We must emit this label if it might be used.  */
997   if (!do_frame
998       && (!flag_exceptions
999           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
1000     return;
1001
1002   fnsec = function_section (current_function_decl);
1003   switch_to_section (fnsec);
1004   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1005                                current_function_funcdef_no);
1006   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1007                           current_function_funcdef_no);
1008   dup_label = xstrdup (label);
1009   current_function_func_begin_label = dup_label;
1010
1011   /* We can elide the fde allocation if we're not emitting debug info.  */
1012   if (!do_frame)
1013     return;
1014
1015   /* Cater to the various TARGET_ASM_OUTPUT_MI_THUNK implementations that
1016      emit insns as rtx but bypass the bulk of rest_of_compilation, which
1017      would include pass_dwarf2_frame.  If we've not created the FDE yet,
1018      do so now.  */
1019   fde = cfun->fde;
1020   if (fde == NULL)
1021     fde = dwarf2out_alloc_current_fde ();
1022
1023   /* Initialize the bits of CURRENT_FDE that were not available earlier.  */
1024   fde->dw_fde_begin = dup_label;
1025   fde->dw_fde_current_label = dup_label;
1026   fde->in_std_section = (fnsec == text_section
1027                          || (cold_text_section && fnsec == cold_text_section));
1028
1029   /* We only want to output line number information for the genuine dwarf2
1030      prologue case, not the eh frame case.  */
1031 #ifdef DWARF2_DEBUGGING_INFO
1032   if (file)
1033     dwarf2out_source_line (line, file, 0, true);
1034 #endif
1035
1036   if (dwarf2out_do_cfi_asm ())
1037     dwarf2out_do_cfi_startproc (false);
1038   else
1039     {
1040       rtx personality = get_personality_function (current_function_decl);
1041       if (!current_unit_personality)
1042         current_unit_personality = personality;
1043
1044       /* We cannot keep a current personality per function as without CFI
1045          asm, at the point where we emit the CFI data, there is no current
1046          function anymore.  */
1047       if (personality && current_unit_personality != personality)
1048         sorry ("multiple EH personalities are supported only with assemblers "
1049                "supporting .cfi_personality directive");
1050     }
1051 }
1052
1053 /* Output a marker (i.e. a label) for the end of the generated code
1054    for a function prologue.  This gets called *after* the prologue code has
1055    been generated.  */
1056
1057 void
1058 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1059                         const char *file ATTRIBUTE_UNUSED)
1060 {
1061   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1062
1063   /* Output a label to mark the endpoint of the code generated for this
1064      function.  */
1065   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1066                                current_function_funcdef_no);
1067   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1068                           current_function_funcdef_no);
1069   cfun->fde->dw_fde_vms_end_prologue = xstrdup (label);
1070 }
1071
1072 /* Output a marker (i.e. a label) for the beginning of the generated code
1073    for a function epilogue.  This gets called *before* the prologue code has
1074    been generated.  */
1075
1076 void
1077 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1078                           const char *file ATTRIBUTE_UNUSED)
1079 {
1080   dw_fde_ref fde = cfun->fde;
1081   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1082
1083   if (fde->dw_fde_vms_begin_epilogue)
1084     return;
1085
1086   /* Output a label to mark the endpoint of the code generated for this
1087      function.  */
1088   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1089                                current_function_funcdef_no);
1090   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1091                           current_function_funcdef_no);
1092   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1093 }
1094
1095 /* Output a marker (i.e. a label) for the absolute end of the generated code
1096    for a function definition.  This gets called *after* the epilogue code has
1097    been generated.  */
1098
1099 void
1100 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1101                         const char *file ATTRIBUTE_UNUSED)
1102 {
1103   dw_fde_ref fde;
1104   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1105
1106   last_var_location_insn = NULL_RTX;
1107
1108   if (dwarf2out_do_cfi_asm ())
1109     fprintf (asm_out_file, "\t.cfi_endproc\n");
1110
1111   /* Output a label to mark the endpoint of the code generated for this
1112      function.  */
1113   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1114                                current_function_funcdef_no);
1115   ASM_OUTPUT_LABEL (asm_out_file, label);
1116   fde = cfun->fde;
1117   gcc_assert (fde != NULL);
1118   if (fde->dw_fde_second_begin == NULL)
1119     fde->dw_fde_end = xstrdup (label);
1120 }
1121
1122 void
1123 dwarf2out_frame_finish (void)
1124 {
1125   /* Output call frame information.  */
1126   if (targetm.debug_unwind_info () == UI_DWARF2)
1127     output_call_frame_info (0);
1128
1129   /* Output another copy for the unwinder.  */
1130   if ((flag_unwind_tables || flag_exceptions)
1131       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1132     output_call_frame_info (1);
1133 }
1134
1135 /* Note that the current function section is being used for code.  */
1136
1137 static void
1138 dwarf2out_note_section_used (void)
1139 {
1140   section *sec = current_function_section ();
1141   if (sec == text_section)
1142     text_section_used = true;
1143   else if (sec == cold_text_section)
1144     cold_text_section_used = true;
1145 }
1146
1147 static void var_location_switch_text_section (void);
1148 static void set_cur_line_info_table (section *);
1149
1150 void
1151 dwarf2out_switch_text_section (void)
1152 {
1153   section *sect;
1154   dw_fde_ref fde = cfun->fde;
1155
1156   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1157
1158   if (!in_cold_section_p)
1159     {
1160       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1161       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1162       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1163     }
1164   else
1165     {
1166       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1167       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1168       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1169     }
1170   have_multiple_function_sections = true;
1171
1172   /* There is no need to mark used sections when not debugging.  */
1173   if (cold_text_section != NULL)
1174     dwarf2out_note_section_used ();
1175
1176   if (dwarf2out_do_cfi_asm ())
1177     fprintf (asm_out_file, "\t.cfi_endproc\n");
1178
1179   /* Now do the real section switch.  */
1180   sect = current_function_section ();
1181   switch_to_section (sect);
1182
1183   fde->second_in_std_section
1184     = (sect == text_section
1185        || (cold_text_section && sect == cold_text_section));
1186
1187   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
1188
1189   if (dwarf2out_do_cfi_asm ())
1190     {
1191       dwarf2out_do_cfi_startproc (true);
1192       /* As this is a different FDE, insert all current CFI instructions
1193          again.  */
1194       output_cfis (fde->dw_fde_cfi, fde->dw_fde_switch_cfi_index,
1195                    true, fde, true);
1196     }
1197
1198   var_location_switch_text_section ();
1199
1200   set_cur_line_info_table (sect);
1201 }
1202 \f
1203 /* And now, the subset of the debugging information support code necessary
1204    for emitting location expressions.  */
1205
1206 /* Data about a single source file.  */
1207 struct GTY(()) dwarf_file_data {
1208   const char * filename;
1209   int emitted_number;
1210 };
1211
1212 typedef struct GTY(()) deferred_locations_struct
1213 {
1214   tree variable;
1215   dw_die_ref die;
1216 } deferred_locations;
1217
1218 DEF_VEC_O(deferred_locations);
1219 DEF_VEC_ALLOC_O(deferred_locations,gc);
1220
1221 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1222
1223 DEF_VEC_P(dw_die_ref);
1224 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1225
1226 /* Location lists are ranges + location descriptions for that range,
1227    so you can track variables that are in different places over
1228    their entire life.  */
1229 typedef struct GTY(()) dw_loc_list_struct {
1230   dw_loc_list_ref dw_loc_next;
1231   const char *begin; /* Label for begin address of range */
1232   const char *end;  /* Label for end address of range */
1233   char *ll_symbol; /* Label for beginning of location list.
1234                       Only on head of list */
1235   const char *section; /* Section this loclist is relative to */
1236   dw_loc_descr_ref expr;
1237   hashval_t hash;
1238   /* True if all addresses in this and subsequent lists are known to be
1239      resolved.  */
1240   bool resolved_addr;
1241   /* True if this list has been replaced by dw_loc_next.  */
1242   bool replaced;
1243   bool emitted;
1244   /* True if the range should be emitted even if begin and end
1245      are the same.  */
1246   bool force;
1247 } dw_loc_list_node;
1248
1249 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1250
1251 /* Convert a DWARF stack opcode into its string name.  */
1252
1253 static const char *
1254 dwarf_stack_op_name (unsigned int op)
1255 {
1256   switch (op)
1257     {
1258     case DW_OP_addr:
1259       return "DW_OP_addr";
1260     case DW_OP_deref:
1261       return "DW_OP_deref";
1262     case DW_OP_const1u:
1263       return "DW_OP_const1u";
1264     case DW_OP_const1s:
1265       return "DW_OP_const1s";
1266     case DW_OP_const2u:
1267       return "DW_OP_const2u";
1268     case DW_OP_const2s:
1269       return "DW_OP_const2s";
1270     case DW_OP_const4u:
1271       return "DW_OP_const4u";
1272     case DW_OP_const4s:
1273       return "DW_OP_const4s";
1274     case DW_OP_const8u:
1275       return "DW_OP_const8u";
1276     case DW_OP_const8s:
1277       return "DW_OP_const8s";
1278     case DW_OP_constu:
1279       return "DW_OP_constu";
1280     case DW_OP_consts:
1281       return "DW_OP_consts";
1282     case DW_OP_dup:
1283       return "DW_OP_dup";
1284     case DW_OP_drop:
1285       return "DW_OP_drop";
1286     case DW_OP_over:
1287       return "DW_OP_over";
1288     case DW_OP_pick:
1289       return "DW_OP_pick";
1290     case DW_OP_swap:
1291       return "DW_OP_swap";
1292     case DW_OP_rot:
1293       return "DW_OP_rot";
1294     case DW_OP_xderef:
1295       return "DW_OP_xderef";
1296     case DW_OP_abs:
1297       return "DW_OP_abs";
1298     case DW_OP_and:
1299       return "DW_OP_and";
1300     case DW_OP_div:
1301       return "DW_OP_div";
1302     case DW_OP_minus:
1303       return "DW_OP_minus";
1304     case DW_OP_mod:
1305       return "DW_OP_mod";
1306     case DW_OP_mul:
1307       return "DW_OP_mul";
1308     case DW_OP_neg:
1309       return "DW_OP_neg";
1310     case DW_OP_not:
1311       return "DW_OP_not";
1312     case DW_OP_or:
1313       return "DW_OP_or";
1314     case DW_OP_plus:
1315       return "DW_OP_plus";
1316     case DW_OP_plus_uconst:
1317       return "DW_OP_plus_uconst";
1318     case DW_OP_shl:
1319       return "DW_OP_shl";
1320     case DW_OP_shr:
1321       return "DW_OP_shr";
1322     case DW_OP_shra:
1323       return "DW_OP_shra";
1324     case DW_OP_xor:
1325       return "DW_OP_xor";
1326     case DW_OP_bra:
1327       return "DW_OP_bra";
1328     case DW_OP_eq:
1329       return "DW_OP_eq";
1330     case DW_OP_ge:
1331       return "DW_OP_ge";
1332     case DW_OP_gt:
1333       return "DW_OP_gt";
1334     case DW_OP_le:
1335       return "DW_OP_le";
1336     case DW_OP_lt:
1337       return "DW_OP_lt";
1338     case DW_OP_ne:
1339       return "DW_OP_ne";
1340     case DW_OP_skip:
1341       return "DW_OP_skip";
1342     case DW_OP_lit0:
1343       return "DW_OP_lit0";
1344     case DW_OP_lit1:
1345       return "DW_OP_lit1";
1346     case DW_OP_lit2:
1347       return "DW_OP_lit2";
1348     case DW_OP_lit3:
1349       return "DW_OP_lit3";
1350     case DW_OP_lit4:
1351       return "DW_OP_lit4";
1352     case DW_OP_lit5:
1353       return "DW_OP_lit5";
1354     case DW_OP_lit6:
1355       return "DW_OP_lit6";
1356     case DW_OP_lit7:
1357       return "DW_OP_lit7";
1358     case DW_OP_lit8:
1359       return "DW_OP_lit8";
1360     case DW_OP_lit9:
1361       return "DW_OP_lit9";
1362     case DW_OP_lit10:
1363       return "DW_OP_lit10";
1364     case DW_OP_lit11:
1365       return "DW_OP_lit11";
1366     case DW_OP_lit12:
1367       return "DW_OP_lit12";
1368     case DW_OP_lit13:
1369       return "DW_OP_lit13";
1370     case DW_OP_lit14:
1371       return "DW_OP_lit14";
1372     case DW_OP_lit15:
1373       return "DW_OP_lit15";
1374     case DW_OP_lit16:
1375       return "DW_OP_lit16";
1376     case DW_OP_lit17:
1377       return "DW_OP_lit17";
1378     case DW_OP_lit18:
1379       return "DW_OP_lit18";
1380     case DW_OP_lit19:
1381       return "DW_OP_lit19";
1382     case DW_OP_lit20:
1383       return "DW_OP_lit20";
1384     case DW_OP_lit21:
1385       return "DW_OP_lit21";
1386     case DW_OP_lit22:
1387       return "DW_OP_lit22";
1388     case DW_OP_lit23:
1389       return "DW_OP_lit23";
1390     case DW_OP_lit24:
1391       return "DW_OP_lit24";
1392     case DW_OP_lit25:
1393       return "DW_OP_lit25";
1394     case DW_OP_lit26:
1395       return "DW_OP_lit26";
1396     case DW_OP_lit27:
1397       return "DW_OP_lit27";
1398     case DW_OP_lit28:
1399       return "DW_OP_lit28";
1400     case DW_OP_lit29:
1401       return "DW_OP_lit29";
1402     case DW_OP_lit30:
1403       return "DW_OP_lit30";
1404     case DW_OP_lit31:
1405       return "DW_OP_lit31";
1406     case DW_OP_reg0:
1407       return "DW_OP_reg0";
1408     case DW_OP_reg1:
1409       return "DW_OP_reg1";
1410     case DW_OP_reg2:
1411       return "DW_OP_reg2";
1412     case DW_OP_reg3:
1413       return "DW_OP_reg3";
1414     case DW_OP_reg4:
1415       return "DW_OP_reg4";
1416     case DW_OP_reg5:
1417       return "DW_OP_reg5";
1418     case DW_OP_reg6:
1419       return "DW_OP_reg6";
1420     case DW_OP_reg7:
1421       return "DW_OP_reg7";
1422     case DW_OP_reg8:
1423       return "DW_OP_reg8";
1424     case DW_OP_reg9:
1425       return "DW_OP_reg9";
1426     case DW_OP_reg10:
1427       return "DW_OP_reg10";
1428     case DW_OP_reg11:
1429       return "DW_OP_reg11";
1430     case DW_OP_reg12:
1431       return "DW_OP_reg12";
1432     case DW_OP_reg13:
1433       return "DW_OP_reg13";
1434     case DW_OP_reg14:
1435       return "DW_OP_reg14";
1436     case DW_OP_reg15:
1437       return "DW_OP_reg15";
1438     case DW_OP_reg16:
1439       return "DW_OP_reg16";
1440     case DW_OP_reg17:
1441       return "DW_OP_reg17";
1442     case DW_OP_reg18:
1443       return "DW_OP_reg18";
1444     case DW_OP_reg19:
1445       return "DW_OP_reg19";
1446     case DW_OP_reg20:
1447       return "DW_OP_reg20";
1448     case DW_OP_reg21:
1449       return "DW_OP_reg21";
1450     case DW_OP_reg22:
1451       return "DW_OP_reg22";
1452     case DW_OP_reg23:
1453       return "DW_OP_reg23";
1454     case DW_OP_reg24:
1455       return "DW_OP_reg24";
1456     case DW_OP_reg25:
1457       return "DW_OP_reg25";
1458     case DW_OP_reg26:
1459       return "DW_OP_reg26";
1460     case DW_OP_reg27:
1461       return "DW_OP_reg27";
1462     case DW_OP_reg28:
1463       return "DW_OP_reg28";
1464     case DW_OP_reg29:
1465       return "DW_OP_reg29";
1466     case DW_OP_reg30:
1467       return "DW_OP_reg30";
1468     case DW_OP_reg31:
1469       return "DW_OP_reg31";
1470     case DW_OP_breg0:
1471       return "DW_OP_breg0";
1472     case DW_OP_breg1:
1473       return "DW_OP_breg1";
1474     case DW_OP_breg2:
1475       return "DW_OP_breg2";
1476     case DW_OP_breg3:
1477       return "DW_OP_breg3";
1478     case DW_OP_breg4:
1479       return "DW_OP_breg4";
1480     case DW_OP_breg5:
1481       return "DW_OP_breg5";
1482     case DW_OP_breg6:
1483       return "DW_OP_breg6";
1484     case DW_OP_breg7:
1485       return "DW_OP_breg7";
1486     case DW_OP_breg8:
1487       return "DW_OP_breg8";
1488     case DW_OP_breg9:
1489       return "DW_OP_breg9";
1490     case DW_OP_breg10:
1491       return "DW_OP_breg10";
1492     case DW_OP_breg11:
1493       return "DW_OP_breg11";
1494     case DW_OP_breg12:
1495       return "DW_OP_breg12";
1496     case DW_OP_breg13:
1497       return "DW_OP_breg13";
1498     case DW_OP_breg14:
1499       return "DW_OP_breg14";
1500     case DW_OP_breg15:
1501       return "DW_OP_breg15";
1502     case DW_OP_breg16:
1503       return "DW_OP_breg16";
1504     case DW_OP_breg17:
1505       return "DW_OP_breg17";
1506     case DW_OP_breg18:
1507       return "DW_OP_breg18";
1508     case DW_OP_breg19:
1509       return "DW_OP_breg19";
1510     case DW_OP_breg20:
1511       return "DW_OP_breg20";
1512     case DW_OP_breg21:
1513       return "DW_OP_breg21";
1514     case DW_OP_breg22:
1515       return "DW_OP_breg22";
1516     case DW_OP_breg23:
1517       return "DW_OP_breg23";
1518     case DW_OP_breg24:
1519       return "DW_OP_breg24";
1520     case DW_OP_breg25:
1521       return "DW_OP_breg25";
1522     case DW_OP_breg26:
1523       return "DW_OP_breg26";
1524     case DW_OP_breg27:
1525       return "DW_OP_breg27";
1526     case DW_OP_breg28:
1527       return "DW_OP_breg28";
1528     case DW_OP_breg29:
1529       return "DW_OP_breg29";
1530     case DW_OP_breg30:
1531       return "DW_OP_breg30";
1532     case DW_OP_breg31:
1533       return "DW_OP_breg31";
1534     case DW_OP_regx:
1535       return "DW_OP_regx";
1536     case DW_OP_fbreg:
1537       return "DW_OP_fbreg";
1538     case DW_OP_bregx:
1539       return "DW_OP_bregx";
1540     case DW_OP_piece:
1541       return "DW_OP_piece";
1542     case DW_OP_deref_size:
1543       return "DW_OP_deref_size";
1544     case DW_OP_xderef_size:
1545       return "DW_OP_xderef_size";
1546     case DW_OP_nop:
1547       return "DW_OP_nop";
1548
1549     case DW_OP_push_object_address:
1550       return "DW_OP_push_object_address";
1551     case DW_OP_call2:
1552       return "DW_OP_call2";
1553     case DW_OP_call4:
1554       return "DW_OP_call4";
1555     case DW_OP_call_ref:
1556       return "DW_OP_call_ref";
1557     case DW_OP_implicit_value:
1558       return "DW_OP_implicit_value";
1559     case DW_OP_stack_value:
1560       return "DW_OP_stack_value";
1561     case DW_OP_form_tls_address:
1562       return "DW_OP_form_tls_address";
1563     case DW_OP_call_frame_cfa:
1564       return "DW_OP_call_frame_cfa";
1565     case DW_OP_bit_piece:
1566       return "DW_OP_bit_piece";
1567
1568     case DW_OP_GNU_push_tls_address:
1569       return "DW_OP_GNU_push_tls_address";
1570     case DW_OP_GNU_uninit:
1571       return "DW_OP_GNU_uninit";
1572     case DW_OP_GNU_encoded_addr:
1573       return "DW_OP_GNU_encoded_addr";
1574     case DW_OP_GNU_implicit_pointer:
1575       return "DW_OP_GNU_implicit_pointer";
1576     case DW_OP_GNU_entry_value:
1577       return "DW_OP_GNU_entry_value";
1578     case DW_OP_GNU_const_type:
1579       return "DW_OP_GNU_const_type";
1580     case DW_OP_GNU_regval_type:
1581       return "DW_OP_GNU_regval_type";
1582     case DW_OP_GNU_deref_type:
1583       return "DW_OP_GNU_deref_type";
1584     case DW_OP_GNU_convert:
1585       return "DW_OP_GNU_convert";
1586     case DW_OP_GNU_reinterpret:
1587       return "DW_OP_GNU_reinterpret";
1588     case DW_OP_GNU_parameter_ref:
1589       return "DW_OP_GNU_parameter_ref";
1590
1591     default:
1592       return "OP_<unknown>";
1593     }
1594 }
1595
1596 /* Return a pointer to a newly allocated location description.  Location
1597    descriptions are simple expression terms that can be strung
1598    together to form more complicated location (address) descriptions.  */
1599
1600 static inline dw_loc_descr_ref
1601 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
1602                unsigned HOST_WIDE_INT oprnd2)
1603 {
1604   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
1605
1606   descr->dw_loc_opc = op;
1607   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
1608   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
1609   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
1610   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
1611
1612   return descr;
1613 }
1614
1615 /* Return a pointer to a newly allocated location description for
1616    REG and OFFSET.  */
1617
1618 static inline dw_loc_descr_ref
1619 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
1620 {
1621   if (reg <= 31)
1622     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
1623                           offset, 0);
1624   else
1625     return new_loc_descr (DW_OP_bregx, reg, offset);
1626 }
1627
1628 /* Add a location description term to a location description expression.  */
1629
1630 static inline void
1631 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
1632 {
1633   dw_loc_descr_ref *d;
1634
1635   /* Find the end of the chain.  */
1636   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
1637     ;
1638
1639   *d = descr;
1640 }
1641
1642 /* Add a constant OFFSET to a location expression.  */
1643
1644 static void
1645 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
1646 {
1647   dw_loc_descr_ref loc;
1648   HOST_WIDE_INT *p;
1649
1650   gcc_assert (*list_head != NULL);
1651
1652   if (!offset)
1653     return;
1654
1655   /* Find the end of the chain.  */
1656   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
1657     ;
1658
1659   p = NULL;
1660   if (loc->dw_loc_opc == DW_OP_fbreg
1661       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
1662     p = &loc->dw_loc_oprnd1.v.val_int;
1663   else if (loc->dw_loc_opc == DW_OP_bregx)
1664     p = &loc->dw_loc_oprnd2.v.val_int;
1665
1666   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
1667      offset.  Don't optimize if an signed integer overflow would happen.  */
1668   if (p != NULL
1669       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
1670           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
1671     *p += offset;
1672
1673   else if (offset > 0)
1674     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
1675
1676   else
1677     {
1678       loc->dw_loc_next = int_loc_descriptor (-offset);
1679       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
1680     }
1681 }
1682
1683 /* Add a constant OFFSET to a location list.  */
1684
1685 static void
1686 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
1687 {
1688   dw_loc_list_ref d;
1689   for (d = list_head; d != NULL; d = d->dw_loc_next)
1690     loc_descr_plus_const (&d->expr, offset);
1691 }
1692
1693 #define DWARF_REF_SIZE  \
1694   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
1695
1696 static unsigned long int get_base_type_offset (dw_die_ref);
1697
1698 /* Return the size of a location descriptor.  */
1699
1700 static unsigned long
1701 size_of_loc_descr (dw_loc_descr_ref loc)
1702 {
1703   unsigned long size = 1;
1704
1705   switch (loc->dw_loc_opc)
1706     {
1707     case DW_OP_addr:
1708       size += DWARF2_ADDR_SIZE;
1709       break;
1710     case DW_OP_const1u:
1711     case DW_OP_const1s:
1712       size += 1;
1713       break;
1714     case DW_OP_const2u:
1715     case DW_OP_const2s:
1716       size += 2;
1717       break;
1718     case DW_OP_const4u:
1719     case DW_OP_const4s:
1720       size += 4;
1721       break;
1722     case DW_OP_const8u:
1723     case DW_OP_const8s:
1724       size += 8;
1725       break;
1726     case DW_OP_constu:
1727       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1728       break;
1729     case DW_OP_consts:
1730       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1731       break;
1732     case DW_OP_pick:
1733       size += 1;
1734       break;
1735     case DW_OP_plus_uconst:
1736       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1737       break;
1738     case DW_OP_skip:
1739     case DW_OP_bra:
1740       size += 2;
1741       break;
1742     case DW_OP_breg0:
1743     case DW_OP_breg1:
1744     case DW_OP_breg2:
1745     case DW_OP_breg3:
1746     case DW_OP_breg4:
1747     case DW_OP_breg5:
1748     case DW_OP_breg6:
1749     case DW_OP_breg7:
1750     case DW_OP_breg8:
1751     case DW_OP_breg9:
1752     case DW_OP_breg10:
1753     case DW_OP_breg11:
1754     case DW_OP_breg12:
1755     case DW_OP_breg13:
1756     case DW_OP_breg14:
1757     case DW_OP_breg15:
1758     case DW_OP_breg16:
1759     case DW_OP_breg17:
1760     case DW_OP_breg18:
1761     case DW_OP_breg19:
1762     case DW_OP_breg20:
1763     case DW_OP_breg21:
1764     case DW_OP_breg22:
1765     case DW_OP_breg23:
1766     case DW_OP_breg24:
1767     case DW_OP_breg25:
1768     case DW_OP_breg26:
1769     case DW_OP_breg27:
1770     case DW_OP_breg28:
1771     case DW_OP_breg29:
1772     case DW_OP_breg30:
1773     case DW_OP_breg31:
1774       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1775       break;
1776     case DW_OP_regx:
1777       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1778       break;
1779     case DW_OP_fbreg:
1780       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
1781       break;
1782     case DW_OP_bregx:
1783       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1784       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1785       break;
1786     case DW_OP_piece:
1787       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1788       break;
1789     case DW_OP_bit_piece:
1790       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1791       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
1792       break;
1793     case DW_OP_deref_size:
1794     case DW_OP_xderef_size:
1795       size += 1;
1796       break;
1797     case DW_OP_call2:
1798       size += 2;
1799       break;
1800     case DW_OP_call4:
1801       size += 4;
1802       break;
1803     case DW_OP_call_ref:
1804       size += DWARF_REF_SIZE;
1805       break;
1806     case DW_OP_implicit_value:
1807       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1808               + loc->dw_loc_oprnd1.v.val_unsigned;
1809       break;
1810     case DW_OP_GNU_implicit_pointer:
1811       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
1812       break;
1813     case DW_OP_GNU_entry_value:
1814       {
1815         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
1816         size += size_of_uleb128 (op_size) + op_size;
1817         break;
1818       }
1819     case DW_OP_GNU_const_type:
1820       {
1821         unsigned long o
1822           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1823         size += size_of_uleb128 (o) + 1;
1824         switch (loc->dw_loc_oprnd2.val_class)
1825           {
1826           case dw_val_class_vec:
1827             size += loc->dw_loc_oprnd2.v.val_vec.length
1828                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
1829             break;
1830           case dw_val_class_const:
1831             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1832             break;
1833           case dw_val_class_const_double:
1834             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
1835             break;
1836           default:
1837             gcc_unreachable ();
1838           }
1839         break;
1840       }
1841     case DW_OP_GNU_regval_type:
1842       {
1843         unsigned long o
1844           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1845         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
1846                 + size_of_uleb128 (o);
1847       }
1848       break;
1849     case DW_OP_GNU_deref_type:
1850       {
1851         unsigned long o
1852           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
1853         size += 1 + size_of_uleb128 (o);
1854       }
1855       break;
1856     case DW_OP_GNU_convert:
1857     case DW_OP_GNU_reinterpret:
1858       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
1859         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
1860       else
1861         {
1862           unsigned long o
1863             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
1864           size += size_of_uleb128 (o);
1865         }
1866       break;
1867     case DW_OP_GNU_parameter_ref:
1868       size += 4;
1869       break;
1870     default:
1871       break;
1872     }
1873
1874   return size;
1875 }
1876
1877 /* Return the size of a series of location descriptors.  */
1878
1879 unsigned long
1880 size_of_locs (dw_loc_descr_ref loc)
1881 {
1882   dw_loc_descr_ref l;
1883   unsigned long size;
1884
1885   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
1886      field, to avoid writing to a PCH file.  */
1887   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1888     {
1889       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
1890         break;
1891       size += size_of_loc_descr (l);
1892     }
1893   if (! l)
1894     return size;
1895
1896   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
1897     {
1898       l->dw_loc_addr = size;
1899       size += size_of_loc_descr (l);
1900     }
1901
1902   return size;
1903 }
1904
1905 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
1906 static void get_ref_die_offset_label (char *, dw_die_ref);
1907 static unsigned long int get_ref_die_offset (dw_die_ref);
1908
1909 /* Output location description stack opcode's operands (if any).
1910    The for_eh_or_skip parameter controls whether register numbers are
1911    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
1912    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
1913    info).  This should be suppressed for the cases that have not been converted
1914    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
1915
1916 static void
1917 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
1918 {
1919   dw_val_ref val1 = &loc->dw_loc_oprnd1;
1920   dw_val_ref val2 = &loc->dw_loc_oprnd2;
1921
1922   switch (loc->dw_loc_opc)
1923     {
1924 #ifdef DWARF2_DEBUGGING_INFO
1925     case DW_OP_const2u:
1926     case DW_OP_const2s:
1927       dw2_asm_output_data (2, val1->v.val_int, NULL);
1928       break;
1929     case DW_OP_const4u:
1930       if (loc->dtprel)
1931         {
1932           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1933           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
1934                                                val1->v.val_addr);
1935           fputc ('\n', asm_out_file);
1936           break;
1937         }
1938       /* FALLTHRU */
1939     case DW_OP_const4s:
1940       dw2_asm_output_data (4, val1->v.val_int, NULL);
1941       break;
1942     case DW_OP_const8u:
1943       if (loc->dtprel)
1944         {
1945           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
1946           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
1947                                                val1->v.val_addr);
1948           fputc ('\n', asm_out_file);
1949           break;
1950         }
1951       /* FALLTHRU */
1952     case DW_OP_const8s:
1953       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
1954       dw2_asm_output_data (8, val1->v.val_int, NULL);
1955       break;
1956     case DW_OP_skip:
1957     case DW_OP_bra:
1958       {
1959         int offset;
1960
1961         gcc_assert (val1->val_class == dw_val_class_loc);
1962         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
1963
1964         dw2_asm_output_data (2, offset, NULL);
1965       }
1966       break;
1967     case DW_OP_implicit_value:
1968       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
1969       switch (val2->val_class)
1970         {
1971         case dw_val_class_const:
1972           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
1973           break;
1974         case dw_val_class_vec:
1975           {
1976             unsigned int elt_size = val2->v.val_vec.elt_size;
1977             unsigned int len = val2->v.val_vec.length;
1978             unsigned int i;
1979             unsigned char *p;
1980
1981             if (elt_size > sizeof (HOST_WIDE_INT))
1982               {
1983                 elt_size /= 2;
1984                 len *= 2;
1985               }
1986             for (i = 0, p = val2->v.val_vec.array;
1987                  i < len;
1988                  i++, p += elt_size)
1989               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
1990                                    "fp or vector constant word %u", i);
1991           }
1992           break;
1993         case dw_val_class_const_double:
1994           {
1995             unsigned HOST_WIDE_INT first, second;
1996
1997             if (WORDS_BIG_ENDIAN)
1998               {
1999                 first = val2->v.val_double.high;
2000                 second = val2->v.val_double.low;
2001               }
2002             else
2003               {
2004                 first = val2->v.val_double.low;
2005                 second = val2->v.val_double.high;
2006               }
2007             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2008                                  first, NULL);
2009             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2010                                  second, NULL);
2011           }
2012           break;
2013         case dw_val_class_addr:
2014           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2015           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2016           break;
2017         default:
2018           gcc_unreachable ();
2019         }
2020       break;
2021 #else
2022     case DW_OP_const2u:
2023     case DW_OP_const2s:
2024     case DW_OP_const4u:
2025     case DW_OP_const4s:
2026     case DW_OP_const8u:
2027     case DW_OP_const8s:
2028     case DW_OP_skip:
2029     case DW_OP_bra:
2030     case DW_OP_implicit_value:
2031       /* We currently don't make any attempt to make sure these are
2032          aligned properly like we do for the main unwind info, so
2033          don't support emitting things larger than a byte if we're
2034          only doing unwinding.  */
2035       gcc_unreachable ();
2036 #endif
2037     case DW_OP_const1u:
2038     case DW_OP_const1s:
2039       dw2_asm_output_data (1, val1->v.val_int, NULL);
2040       break;
2041     case DW_OP_constu:
2042       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2043       break;
2044     case DW_OP_consts:
2045       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2046       break;
2047     case DW_OP_pick:
2048       dw2_asm_output_data (1, val1->v.val_int, NULL);
2049       break;
2050     case DW_OP_plus_uconst:
2051       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2052       break;
2053     case DW_OP_breg0:
2054     case DW_OP_breg1:
2055     case DW_OP_breg2:
2056     case DW_OP_breg3:
2057     case DW_OP_breg4:
2058     case DW_OP_breg5:
2059     case DW_OP_breg6:
2060     case DW_OP_breg7:
2061     case DW_OP_breg8:
2062     case DW_OP_breg9:
2063     case DW_OP_breg10:
2064     case DW_OP_breg11:
2065     case DW_OP_breg12:
2066     case DW_OP_breg13:
2067     case DW_OP_breg14:
2068     case DW_OP_breg15:
2069     case DW_OP_breg16:
2070     case DW_OP_breg17:
2071     case DW_OP_breg18:
2072     case DW_OP_breg19:
2073     case DW_OP_breg20:
2074     case DW_OP_breg21:
2075     case DW_OP_breg22:
2076     case DW_OP_breg23:
2077     case DW_OP_breg24:
2078     case DW_OP_breg25:
2079     case DW_OP_breg26:
2080     case DW_OP_breg27:
2081     case DW_OP_breg28:
2082     case DW_OP_breg29:
2083     case DW_OP_breg30:
2084     case DW_OP_breg31:
2085       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2086       break;
2087     case DW_OP_regx:
2088       {
2089         unsigned r = val1->v.val_unsigned;
2090         if (for_eh_or_skip >= 0)
2091           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2092         gcc_assert (size_of_uleb128 (r) 
2093                     == size_of_uleb128 (val1->v.val_unsigned));
2094         dw2_asm_output_data_uleb128 (r, NULL);  
2095       }
2096       break;
2097     case DW_OP_fbreg:
2098       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2099       break;
2100     case DW_OP_bregx:
2101       {
2102         unsigned r = val1->v.val_unsigned;
2103         if (for_eh_or_skip >= 0)
2104           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2105         gcc_assert (size_of_uleb128 (r) 
2106                     == size_of_uleb128 (val1->v.val_unsigned));
2107         dw2_asm_output_data_uleb128 (r, NULL);  
2108         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2109       }
2110       break;
2111     case DW_OP_piece:
2112       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2113       break;
2114     case DW_OP_bit_piece:
2115       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2116       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2117       break;
2118     case DW_OP_deref_size:
2119     case DW_OP_xderef_size:
2120       dw2_asm_output_data (1, val1->v.val_int, NULL);
2121       break;
2122
2123     case DW_OP_addr:
2124       if (loc->dtprel)
2125         {
2126           if (targetm.asm_out.output_dwarf_dtprel)
2127             {
2128               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2129                                                    DWARF2_ADDR_SIZE,
2130                                                    val1->v.val_addr);
2131               fputc ('\n', asm_out_file);
2132             }
2133           else
2134             gcc_unreachable ();
2135         }
2136       else
2137         {
2138 #ifdef DWARF2_DEBUGGING_INFO
2139           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2140 #else
2141           gcc_unreachable ();
2142 #endif
2143         }
2144       break;
2145
2146     case DW_OP_GNU_implicit_pointer:
2147       {
2148         char label[MAX_ARTIFICIAL_LABEL_BYTES
2149                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2150         gcc_assert (val1->val_class == dw_val_class_die_ref);
2151         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2152         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2153         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2154       }
2155       break;
2156
2157     case DW_OP_GNU_entry_value:
2158       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2159       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2160       break;
2161
2162     case DW_OP_GNU_const_type:
2163       {
2164         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2165         gcc_assert (o);
2166         dw2_asm_output_data_uleb128 (o, NULL);
2167         switch (val2->val_class)
2168           {
2169           case dw_val_class_const:
2170             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2171             dw2_asm_output_data (1, l, NULL);
2172             dw2_asm_output_data (l, val2->v.val_int, NULL);
2173             break;
2174           case dw_val_class_vec:
2175             {
2176               unsigned int elt_size = val2->v.val_vec.elt_size;
2177               unsigned int len = val2->v.val_vec.length;
2178               unsigned int i;
2179               unsigned char *p;
2180
2181               l = len * elt_size;
2182               dw2_asm_output_data (1, l, NULL);
2183               if (elt_size > sizeof (HOST_WIDE_INT))
2184                 {
2185                   elt_size /= 2;
2186                   len *= 2;
2187                 }
2188               for (i = 0, p = val2->v.val_vec.array;
2189                    i < len;
2190                    i++, p += elt_size)
2191                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2192                                      "fp or vector constant word %u", i);
2193             }
2194             break;
2195           case dw_val_class_const_double:
2196             {
2197               unsigned HOST_WIDE_INT first, second;
2198               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2199
2200               dw2_asm_output_data (1, 2 * l, NULL);
2201               if (WORDS_BIG_ENDIAN)
2202                 {
2203                   first = val2->v.val_double.high;
2204                   second = val2->v.val_double.low;
2205                 }
2206               else
2207                 {
2208                   first = val2->v.val_double.low;
2209                   second = val2->v.val_double.high;
2210                 }
2211               dw2_asm_output_data (l, first, NULL);
2212               dw2_asm_output_data (l, second, NULL);
2213             }
2214             break;
2215           default:
2216             gcc_unreachable ();
2217           }
2218       }
2219       break;
2220     case DW_OP_GNU_regval_type:
2221       {
2222         unsigned r = val1->v.val_unsigned;
2223         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2224         gcc_assert (o);
2225         if (for_eh_or_skip >= 0)
2226           {
2227             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2228             gcc_assert (size_of_uleb128 (r)
2229                         == size_of_uleb128 (val1->v.val_unsigned));
2230           }
2231         dw2_asm_output_data_uleb128 (r, NULL);
2232         dw2_asm_output_data_uleb128 (o, NULL);
2233       }
2234       break;
2235     case DW_OP_GNU_deref_type:
2236       {
2237         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2238         gcc_assert (o);
2239         dw2_asm_output_data (1, val1->v.val_int, NULL);
2240         dw2_asm_output_data_uleb128 (o, NULL);
2241       }
2242       break;
2243     case DW_OP_GNU_convert:
2244     case DW_OP_GNU_reinterpret:
2245       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2246         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2247       else
2248         {
2249           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2250           gcc_assert (o);
2251           dw2_asm_output_data_uleb128 (o, NULL);
2252         }
2253       break;
2254
2255     case DW_OP_GNU_parameter_ref:
2256       {
2257         unsigned long o;
2258         gcc_assert (val1->val_class == dw_val_class_die_ref);
2259         o = get_ref_die_offset (val1->v.val_die_ref.die);
2260         dw2_asm_output_data (4, o, NULL);
2261       }
2262       break;
2263
2264     default:
2265       /* Other codes have no operands.  */
2266       break;
2267     }
2268 }
2269
2270 /* Output a sequence of location operations.  
2271    The for_eh_or_skip parameter controls whether register numbers are
2272    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2273    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2274    info).  This should be suppressed for the cases that have not been converted
2275    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2276
2277 void
2278 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2279 {
2280   for (; loc != NULL; loc = loc->dw_loc_next)
2281     {
2282       enum dwarf_location_atom opc = loc->dw_loc_opc;
2283       /* Output the opcode.  */
2284       if (for_eh_or_skip >= 0 
2285           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2286         {
2287           unsigned r = (opc - DW_OP_breg0);
2288           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2289           gcc_assert (r <= 31);
2290           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2291         }
2292       else if (for_eh_or_skip >= 0 
2293                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2294         {
2295           unsigned r = (opc - DW_OP_reg0);
2296           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2297           gcc_assert (r <= 31);
2298           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2299         }
2300
2301       dw2_asm_output_data (1, opc,
2302                              "%s", dwarf_stack_op_name (opc));
2303
2304       /* Output the operand(s) (if any).  */
2305       output_loc_operands (loc, for_eh_or_skip);
2306     }
2307 }
2308
2309 /* Output location description stack opcode's operands (if any).
2310    The output is single bytes on a line, suitable for .cfi_escape.  */
2311
2312 static void
2313 output_loc_operands_raw (dw_loc_descr_ref loc)
2314 {
2315   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2316   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2317
2318   switch (loc->dw_loc_opc)
2319     {
2320     case DW_OP_addr:
2321     case DW_OP_implicit_value:
2322       /* We cannot output addresses in .cfi_escape, only bytes.  */
2323       gcc_unreachable ();
2324
2325     case DW_OP_const1u:
2326     case DW_OP_const1s:
2327     case DW_OP_pick:
2328     case DW_OP_deref_size:
2329     case DW_OP_xderef_size:
2330       fputc (',', asm_out_file);
2331       dw2_asm_output_data_raw (1, val1->v.val_int);
2332       break;
2333
2334     case DW_OP_const2u:
2335     case DW_OP_const2s:
2336       fputc (',', asm_out_file);
2337       dw2_asm_output_data_raw (2, val1->v.val_int);
2338       break;
2339
2340     case DW_OP_const4u:
2341     case DW_OP_const4s:
2342       fputc (',', asm_out_file);
2343       dw2_asm_output_data_raw (4, val1->v.val_int);
2344       break;
2345
2346     case DW_OP_const8u:
2347     case DW_OP_const8s:
2348       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2349       fputc (',', asm_out_file);
2350       dw2_asm_output_data_raw (8, val1->v.val_int);
2351       break;
2352
2353     case DW_OP_skip:
2354     case DW_OP_bra:
2355       {
2356         int offset;
2357
2358         gcc_assert (val1->val_class == dw_val_class_loc);
2359         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2360
2361         fputc (',', asm_out_file);
2362         dw2_asm_output_data_raw (2, offset);
2363       }
2364       break;
2365
2366     case DW_OP_regx:
2367       {
2368         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2369         gcc_assert (size_of_uleb128 (r) 
2370                     == size_of_uleb128 (val1->v.val_unsigned));
2371         fputc (',', asm_out_file);
2372         dw2_asm_output_data_uleb128_raw (r);
2373       }
2374       break;
2375       
2376     case DW_OP_constu:
2377     case DW_OP_plus_uconst:
2378     case DW_OP_piece:
2379       fputc (',', asm_out_file);
2380       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2381       break;
2382
2383     case DW_OP_bit_piece:
2384       fputc (',', asm_out_file);
2385       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
2386       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
2387       break;
2388
2389     case DW_OP_consts:
2390     case DW_OP_breg0:
2391     case DW_OP_breg1:
2392     case DW_OP_breg2:
2393     case DW_OP_breg3:
2394     case DW_OP_breg4:
2395     case DW_OP_breg5:
2396     case DW_OP_breg6:
2397     case DW_OP_breg7:
2398     case DW_OP_breg8:
2399     case DW_OP_breg9:
2400     case DW_OP_breg10:
2401     case DW_OP_breg11:
2402     case DW_OP_breg12:
2403     case DW_OP_breg13:
2404     case DW_OP_breg14:
2405     case DW_OP_breg15:
2406     case DW_OP_breg16:
2407     case DW_OP_breg17:
2408     case DW_OP_breg18:
2409     case DW_OP_breg19:
2410     case DW_OP_breg20:
2411     case DW_OP_breg21:
2412     case DW_OP_breg22:
2413     case DW_OP_breg23:
2414     case DW_OP_breg24:
2415     case DW_OP_breg25:
2416     case DW_OP_breg26:
2417     case DW_OP_breg27:
2418     case DW_OP_breg28:
2419     case DW_OP_breg29:
2420     case DW_OP_breg30:
2421     case DW_OP_breg31:
2422     case DW_OP_fbreg:
2423       fputc (',', asm_out_file);
2424       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
2425       break;
2426
2427     case DW_OP_bregx:
2428       {
2429         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2430         gcc_assert (size_of_uleb128 (r) 
2431                     == size_of_uleb128 (val1->v.val_unsigned));
2432         fputc (',', asm_out_file);
2433         dw2_asm_output_data_uleb128_raw (r);
2434         fputc (',', asm_out_file);
2435         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
2436       }
2437       break;
2438
2439     case DW_OP_GNU_implicit_pointer:
2440     case DW_OP_GNU_entry_value:
2441     case DW_OP_GNU_const_type:
2442     case DW_OP_GNU_regval_type:
2443     case DW_OP_GNU_deref_type:
2444     case DW_OP_GNU_convert:
2445     case DW_OP_GNU_reinterpret:
2446     case DW_OP_GNU_parameter_ref:
2447       gcc_unreachable ();
2448       break;
2449
2450     default:
2451       /* Other codes have no operands.  */
2452       break;
2453     }
2454 }
2455
2456 void
2457 output_loc_sequence_raw (dw_loc_descr_ref loc)
2458 {
2459   while (1)
2460     {
2461       enum dwarf_location_atom opc = loc->dw_loc_opc;
2462       /* Output the opcode.  */
2463       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2464         {
2465           unsigned r = (opc - DW_OP_breg0);
2466           r = DWARF2_FRAME_REG_OUT (r, 1);
2467           gcc_assert (r <= 31);
2468           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2469         }
2470       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2471         {
2472           unsigned r = (opc - DW_OP_reg0);
2473           r = DWARF2_FRAME_REG_OUT (r, 1);
2474           gcc_assert (r <= 31);
2475           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2476         }
2477       /* Output the opcode.  */
2478       fprintf (asm_out_file, "%#x", opc);
2479       output_loc_operands_raw (loc);
2480
2481       if (!loc->dw_loc_next)
2482         break;
2483       loc = loc->dw_loc_next;
2484
2485       fputc (',', asm_out_file);
2486     }
2487 }
2488
2489 /* This function builds a dwarf location descriptor sequence from a
2490    dw_cfa_location, adding the given OFFSET to the result of the
2491    expression.  */
2492
2493 struct dw_loc_descr_struct *
2494 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
2495 {
2496   struct dw_loc_descr_struct *head, *tmp;
2497
2498   offset += cfa->offset;
2499
2500   if (cfa->indirect)
2501     {
2502       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
2503       head->dw_loc_oprnd1.val_class = dw_val_class_const;
2504       tmp = new_loc_descr (DW_OP_deref, 0, 0);
2505       add_loc_descr (&head, tmp);
2506       if (offset != 0)
2507         {
2508           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2509           add_loc_descr (&head, tmp);
2510         }
2511     }
2512   else
2513     head = new_reg_loc_descr (cfa->reg, offset);
2514
2515   return head;
2516 }
2517
2518 /* This function builds a dwarf location descriptor sequence for
2519    the address at OFFSET from the CFA when stack is aligned to
2520    ALIGNMENT byte.  */
2521
2522 struct dw_loc_descr_struct *
2523 build_cfa_aligned_loc (dw_cfa_location *cfa,
2524                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
2525 {
2526   struct dw_loc_descr_struct *head;
2527   unsigned int dwarf_fp
2528     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
2529
2530   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
2531   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
2532     {
2533       head = new_reg_loc_descr (dwarf_fp, 0);
2534       add_loc_descr (&head, int_loc_descriptor (alignment));
2535       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
2536       loc_descr_plus_const (&head, offset);
2537     }
2538   else
2539     head = new_reg_loc_descr (dwarf_fp, offset);
2540   return head;
2541 }
2542 \f
2543 /* And now, the support for symbolic debugging information.  */
2544
2545 /* .debug_str support.  */
2546 static int output_indirect_string (void **, void *);
2547
2548 static void dwarf2out_init (const char *);
2549 static void dwarf2out_finish (const char *);
2550 static void dwarf2out_assembly_start (void);
2551 static void dwarf2out_define (unsigned int, const char *);
2552 static void dwarf2out_undef (unsigned int, const char *);
2553 static void dwarf2out_start_source_file (unsigned, const char *);
2554 static void dwarf2out_end_source_file (unsigned);
2555 static void dwarf2out_function_decl (tree);
2556 static void dwarf2out_begin_block (unsigned, unsigned);
2557 static void dwarf2out_end_block (unsigned, unsigned);
2558 static bool dwarf2out_ignore_block (const_tree);
2559 static void dwarf2out_global_decl (tree);
2560 static void dwarf2out_type_decl (tree, int);
2561 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
2562 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
2563                                                  dw_die_ref);
2564 static void dwarf2out_abstract_function (tree);
2565 static void dwarf2out_var_location (rtx);
2566 static void dwarf2out_begin_function (tree);
2567 static void dwarf2out_set_name (tree, tree);
2568
2569 /* The debug hooks structure.  */
2570
2571 const struct gcc_debug_hooks dwarf2_debug_hooks =
2572 {
2573   dwarf2out_init,
2574   dwarf2out_finish,
2575   dwarf2out_assembly_start,
2576   dwarf2out_define,
2577   dwarf2out_undef,
2578   dwarf2out_start_source_file,
2579   dwarf2out_end_source_file,
2580   dwarf2out_begin_block,
2581   dwarf2out_end_block,
2582   dwarf2out_ignore_block,
2583   dwarf2out_source_line,
2584   dwarf2out_begin_prologue,
2585 #if VMS_DEBUGGING_INFO
2586   dwarf2out_vms_end_prologue,
2587   dwarf2out_vms_begin_epilogue,
2588 #else
2589   debug_nothing_int_charstar,
2590   debug_nothing_int_charstar,
2591 #endif
2592   dwarf2out_end_epilogue,
2593   dwarf2out_begin_function,
2594   debug_nothing_int,            /* end_function */
2595   dwarf2out_function_decl,      /* function_decl */
2596   dwarf2out_global_decl,
2597   dwarf2out_type_decl,          /* type_decl */
2598   dwarf2out_imported_module_or_decl,
2599   debug_nothing_tree,           /* deferred_inline_function */
2600   /* The DWARF 2 backend tries to reduce debugging bloat by not
2601      emitting the abstract description of inline functions until
2602      something tries to reference them.  */
2603   dwarf2out_abstract_function,  /* outlining_inline_function */
2604   debug_nothing_rtx,            /* label */
2605   debug_nothing_int,            /* handle_pch */
2606   dwarf2out_var_location,
2607   dwarf2out_switch_text_section,
2608   dwarf2out_set_name,
2609   1,                            /* start_end_main_source_file */
2610   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
2611 };
2612 \f
2613 /* NOTE: In the comments in this file, many references are made to
2614    "Debugging Information Entries".  This term is abbreviated as `DIE'
2615    throughout the remainder of this file.  */
2616
2617 /* An internal representation of the DWARF output is built, and then
2618    walked to generate the DWARF debugging info.  The walk of the internal
2619    representation is done after the entire program has been compiled.
2620    The types below are used to describe the internal representation.  */
2621
2622 /* Whether to put type DIEs into their own section .debug_types instead
2623    of making them part of the .debug_info section.  Only supported for
2624    Dwarf V4 or higher and the user didn't disable them through
2625    -fno-debug-types-section.  It is more efficient to put them in a
2626    separate comdat sections since the linker will then be able to
2627    remove duplicates.  But not all tools support .debug_types sections
2628    yet.  */
2629
2630 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
2631
2632 /* Various DIE's use offsets relative to the beginning of the
2633    .debug_info section to refer to each other.  */
2634
2635 typedef long int dw_offset;
2636
2637 /* Define typedefs here to avoid circular dependencies.  */
2638
2639 typedef struct dw_attr_struct *dw_attr_ref;
2640 typedef struct dw_line_info_struct *dw_line_info_ref;
2641 typedef struct pubname_struct *pubname_ref;
2642 typedef struct dw_ranges_struct *dw_ranges_ref;
2643 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
2644 typedef struct comdat_type_struct *comdat_type_node_ref;
2645
2646 /* The entries in the line_info table more-or-less mirror the opcodes
2647    that are used in the real dwarf line table.  Arrays of these entries
2648    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
2649    supported.  */
2650
2651 enum dw_line_info_opcode {
2652   /* Emit DW_LNE_set_address; the operand is the label index.  */
2653   LI_set_address,
2654
2655   /* Emit a row to the matrix with the given line.  This may be done
2656      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
2657      special opcodes.  */
2658   LI_set_line,
2659
2660   /* Emit a DW_LNS_set_file.  */
2661   LI_set_file,
2662
2663   /* Emit a DW_LNS_set_column.  */
2664   LI_set_column,
2665
2666   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
2667   LI_negate_stmt,
2668
2669   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
2670   LI_set_prologue_end,
2671   LI_set_epilogue_begin,
2672
2673   /* Emit a DW_LNE_set_discriminator.  */
2674   LI_set_discriminator
2675 };
2676
2677 typedef struct GTY(()) dw_line_info_struct {
2678   enum dw_line_info_opcode opcode;
2679   unsigned int val;
2680 } dw_line_info_entry;
2681
2682 DEF_VEC_O(dw_line_info_entry);
2683 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
2684
2685 typedef struct GTY(()) dw_line_info_table_struct {
2686   /* The label that marks the end of this section.  */
2687   const char *end_label;
2688
2689   /* The values for the last row of the matrix, as collected in the table.
2690      These are used to minimize the changes to the next row.  */
2691   unsigned int file_num;
2692   unsigned int line_num;
2693   unsigned int column_num;
2694   int discrim_num;
2695   bool is_stmt;
2696   bool in_use;
2697
2698   VEC(dw_line_info_entry, gc) *entries;
2699 } dw_line_info_table;
2700
2701 typedef dw_line_info_table *dw_line_info_table_p;
2702
2703 DEF_VEC_P(dw_line_info_table_p);
2704 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
2705
2706 /* Each DIE attribute has a field specifying the attribute kind,
2707    a link to the next attribute in the chain, and an attribute value.
2708    Attributes are typically linked below the DIE they modify.  */
2709
2710 typedef struct GTY(()) dw_attr_struct {
2711   enum dwarf_attribute dw_attr;
2712   dw_val_node dw_attr_val;
2713 }
2714 dw_attr_node;
2715
2716 DEF_VEC_O(dw_attr_node);
2717 DEF_VEC_ALLOC_O(dw_attr_node,gc);
2718
2719 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
2720    The children of each node form a circular list linked by
2721    die_sib.  die_child points to the node *before* the "first" child node.  */
2722
2723 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
2724   union die_symbol_or_type_node
2725     {
2726       char * GTY ((tag ("0"))) die_symbol;
2727       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
2728     }
2729   GTY ((desc ("use_debug_types"))) die_id;
2730   VEC(dw_attr_node,gc) * die_attr;
2731   dw_die_ref die_parent;
2732   dw_die_ref die_child;
2733   dw_die_ref die_sib;
2734   dw_die_ref die_definition; /* ref from a specification to its definition */
2735   dw_offset die_offset;
2736   unsigned long die_abbrev;
2737   int die_mark;
2738   /* Die is used and must not be pruned as unused.  */
2739   int die_perennial_p;
2740   unsigned int decl_id;
2741   enum dwarf_tag die_tag;
2742 }
2743 die_node;
2744
2745 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
2746 #define FOR_EACH_CHILD(die, c, expr) do {       \
2747   c = die->die_child;                           \
2748   if (c) do {                                   \
2749     c = c->die_sib;                             \
2750     expr;                                       \
2751   } while (c != die->die_child);                \
2752 } while (0)
2753
2754 /* The pubname structure */
2755
2756 typedef struct GTY(()) pubname_struct {
2757   dw_die_ref die;
2758   const char *name;
2759 }
2760 pubname_entry;
2761
2762 DEF_VEC_O(pubname_entry);
2763 DEF_VEC_ALLOC_O(pubname_entry, gc);
2764
2765 struct GTY(()) dw_ranges_struct {
2766   /* If this is positive, it's a block number, otherwise it's a
2767      bitwise-negated index into dw_ranges_by_label.  */
2768   int num;
2769 };
2770
2771 /* A structure to hold a macinfo entry.  */
2772
2773 typedef struct GTY(()) macinfo_struct {
2774   unsigned char code;
2775   unsigned HOST_WIDE_INT lineno;
2776   const char *info;
2777 }
2778 macinfo_entry;
2779
2780 DEF_VEC_O(macinfo_entry);
2781 DEF_VEC_ALLOC_O(macinfo_entry, gc);
2782
2783 struct GTY(()) dw_ranges_by_label_struct {
2784   const char *begin;
2785   const char *end;
2786 };
2787
2788 /* The comdat type node structure.  */
2789 typedef struct GTY(()) comdat_type_struct
2790 {
2791   dw_die_ref root_die;
2792   dw_die_ref type_die;
2793   char signature[DWARF_TYPE_SIGNATURE_SIZE];
2794   struct comdat_type_struct *next;
2795 }
2796 comdat_type_node;
2797
2798 /* The limbo die list structure.  */
2799 typedef struct GTY(()) limbo_die_struct {
2800   dw_die_ref die;
2801   tree created_for;
2802   struct limbo_die_struct *next;
2803 }
2804 limbo_die_node;
2805
2806 typedef struct skeleton_chain_struct
2807 {
2808   dw_die_ref old_die;
2809   dw_die_ref new_die;
2810   struct skeleton_chain_struct *parent;
2811 }
2812 skeleton_chain_node;
2813
2814 /* Define a macro which returns nonzero for a TYPE_DECL which was
2815    implicitly generated for a type.
2816
2817    Note that, unlike the C front-end (which generates a NULL named
2818    TYPE_DECL node for each complete tagged type, each array type,
2819    and each function type node created) the C++ front-end generates
2820    a _named_ TYPE_DECL node for each tagged type node created.
2821    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
2822    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
2823    front-end, but for each type, tagged or not.  */
2824
2825 #define TYPE_DECL_IS_STUB(decl)                         \
2826   (DECL_NAME (decl) == NULL_TREE                        \
2827    || (DECL_ARTIFICIAL (decl)                           \
2828        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
2829            /* This is necessary for stub decls that     \
2830               appear in nested inline functions.  */    \
2831            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
2832                && (decl_ultimate_origin (decl)          \
2833                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
2834
2835 /* Information concerning the compilation unit's programming
2836    language, and compiler version.  */
2837
2838 /* Fixed size portion of the DWARF compilation unit header.  */
2839 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
2840   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
2841
2842 /* Fixed size portion of the DWARF comdat type unit header.  */
2843 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
2844   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
2845    + DWARF_OFFSET_SIZE)
2846
2847 /* Fixed size portion of public names info.  */
2848 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
2849
2850 /* Fixed size portion of the address range info.  */
2851 #define DWARF_ARANGES_HEADER_SIZE                                       \
2852   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
2853                 DWARF2_ADDR_SIZE * 2)                                   \
2854    - DWARF_INITIAL_LENGTH_SIZE)
2855
2856 /* Size of padding portion in the address range info.  It must be
2857    aligned to twice the pointer size.  */
2858 #define DWARF_ARANGES_PAD_SIZE \
2859   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
2860                 DWARF2_ADDR_SIZE * 2)                              \
2861    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
2862
2863 /* Use assembler line directives if available.  */
2864 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
2865 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
2866 #define DWARF2_ASM_LINE_DEBUG_INFO 1
2867 #else
2868 #define DWARF2_ASM_LINE_DEBUG_INFO 0
2869 #endif
2870 #endif
2871
2872 /* Minimum line offset in a special line info. opcode.
2873    This value was chosen to give a reasonable range of values.  */
2874 #define DWARF_LINE_BASE  -10
2875
2876 /* First special line opcode - leave room for the standard opcodes.  */
2877 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
2878
2879 /* Range of line offsets in a special line info. opcode.  */
2880 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
2881
2882 /* Flag that indicates the initial value of the is_stmt_start flag.
2883    In the present implementation, we do not mark any lines as
2884    the beginning of a source statement, because that information
2885    is not made available by the GCC front-end.  */
2886 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
2887
2888 /* Maximum number of operations per instruction bundle.  */
2889 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
2890 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
2891 #endif
2892
2893 /* This location is used by calc_die_sizes() to keep track
2894    the offset of each DIE within the .debug_info section.  */
2895 static unsigned long next_die_offset;
2896
2897 /* Record the root of the DIE's built for the current compilation unit.  */
2898 static GTY(()) dw_die_ref single_comp_unit_die;
2899
2900 /* A list of type DIEs that have been separated into comdat sections.  */
2901 static GTY(()) comdat_type_node *comdat_type_list;
2902
2903 /* A list of DIEs with a NULL parent waiting to be relocated.  */
2904 static GTY(()) limbo_die_node *limbo_die_list;
2905
2906 /* A list of DIEs for which we may have to generate
2907    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
2908 static GTY(()) limbo_die_node *deferred_asm_name;
2909
2910 /* Filenames referenced by this compilation unit.  */
2911 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
2912
2913 /* A hash table of references to DIE's that describe declarations.
2914    The key is a DECL_UID() which is a unique number identifying each decl.  */
2915 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
2916
2917 /* A hash table of references to DIE's that describe COMMON blocks.
2918    The key is DECL_UID() ^ die_parent.  */
2919 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
2920
2921 typedef struct GTY(()) die_arg_entry_struct {
2922     dw_die_ref die;
2923     tree arg;
2924 } die_arg_entry;
2925
2926 DEF_VEC_O(die_arg_entry);
2927 DEF_VEC_ALLOC_O(die_arg_entry,gc);
2928
2929 /* Node of the variable location list.  */
2930 struct GTY ((chain_next ("%h.next"))) var_loc_node {
2931   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
2932      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
2933      in mode of the EXPR_LIST node and first EXPR_LIST operand
2934      is either NOTE_INSN_VAR_LOCATION for a piece with a known
2935      location or NULL for padding.  For larger bitsizes,
2936      mode is 0 and first operand is a CONCAT with bitsize
2937      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
2938      NULL as second operand.  */
2939   rtx GTY (()) loc;
2940   const char * GTY (()) label;
2941   struct var_loc_node * GTY (()) next;
2942 };
2943
2944 /* Variable location list.  */
2945 struct GTY (()) var_loc_list_def {
2946   struct var_loc_node * GTY (()) first;
2947
2948   /* Pointer to the last but one or last element of the
2949      chained list.  If the list is empty, both first and
2950      last are NULL, if the list contains just one node
2951      or the last node certainly is not redundant, it points
2952      to the last node, otherwise points to the last but one.
2953      Do not mark it for GC because it is marked through the chain.  */
2954   struct var_loc_node * GTY ((skip ("%h"))) last;
2955
2956   /* Pointer to the last element before section switch,
2957      if NULL, either sections weren't switched or first
2958      is after section switch.  */
2959   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
2960
2961   /* DECL_UID of the variable decl.  */
2962   unsigned int decl_id;
2963 };
2964 typedef struct var_loc_list_def var_loc_list;
2965
2966 /* Call argument location list.  */
2967 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
2968   rtx GTY (()) call_arg_loc_note;
2969   const char * GTY (()) label;
2970   tree GTY (()) block;
2971   bool tail_call_p;
2972   rtx GTY (()) symbol_ref;
2973   struct call_arg_loc_node * GTY (()) next;
2974 };
2975
2976
2977 /* Table of decl location linked lists.  */
2978 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
2979
2980 /* Head and tail of call_arg_loc chain.  */
2981 static GTY (()) struct call_arg_loc_node *call_arg_locations;
2982 static struct call_arg_loc_node *call_arg_loc_last;
2983
2984 /* Number of call sites in the current function.  */
2985 static int call_site_count = -1;
2986 /* Number of tail call sites in the current function.  */
2987 static int tail_call_site_count = -1;
2988
2989 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
2990    DIEs.  */
2991 static VEC (dw_die_ref, heap) *block_map;
2992
2993 /* A cached location list.  */
2994 struct GTY (()) cached_dw_loc_list_def {
2995   /* The DECL_UID of the decl that this entry describes.  */
2996   unsigned int decl_id;
2997
2998   /* The cached location list.  */
2999   dw_loc_list_ref loc_list;
3000 };
3001 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3002
3003 /* Table of cached location lists.  */
3004 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3005
3006 /* A pointer to the base of a list of references to DIE's that
3007    are uniquely identified by their tag, presence/absence of
3008    children DIE's, and list of attribute/value pairs.  */
3009 static GTY((length ("abbrev_die_table_allocated")))
3010   dw_die_ref *abbrev_die_table;
3011
3012 /* Number of elements currently allocated for abbrev_die_table.  */
3013 static GTY(()) unsigned abbrev_die_table_allocated;
3014
3015 /* Number of elements in type_die_table currently in use.  */
3016 static GTY(()) unsigned abbrev_die_table_in_use;
3017
3018 /* Size (in elements) of increments by which we may expand the
3019    abbrev_die_table.  */
3020 #define ABBREV_DIE_TABLE_INCREMENT 256
3021
3022 /* A global counter for generating labels for line number data.  */
3023 static unsigned int line_info_label_num;
3024
3025 /* The current table to which we should emit line number information
3026    for the current function.  This will be set up at the beginning of
3027    assembly for the function.  */
3028 static dw_line_info_table *cur_line_info_table;
3029
3030 /* The two default tables of line number info.  */
3031 static GTY(()) dw_line_info_table *text_section_line_info;
3032 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3033
3034 /* The set of all non-default tables of line number info.  */
3035 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3036
3037 /* A flag to tell pubnames/types export if there is an info section to
3038    refer to.  */
3039 static bool info_section_emitted;
3040
3041 /* A pointer to the base of a table that contains a list of publicly
3042    accessible names.  */
3043 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3044
3045 /* A pointer to the base of a table that contains a list of publicly
3046    accessible types.  */
3047 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3048
3049 /* A pointer to the base of a table that contains a list of macro
3050    defines/undefines (and file start/end markers).  */
3051 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3052
3053 /* Array of dies for which we should generate .debug_ranges info.  */
3054 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3055
3056 /* Number of elements currently allocated for ranges_table.  */
3057 static GTY(()) unsigned ranges_table_allocated;
3058
3059 /* Number of elements in ranges_table currently in use.  */
3060 static GTY(()) unsigned ranges_table_in_use;
3061
3062 /* Array of pairs of labels referenced in ranges_table.  */
3063 static GTY ((length ("ranges_by_label_allocated")))
3064      dw_ranges_by_label_ref ranges_by_label;
3065
3066 /* Number of elements currently allocated for ranges_by_label.  */
3067 static GTY(()) unsigned ranges_by_label_allocated;
3068
3069 /* Number of elements in ranges_by_label currently in use.  */
3070 static GTY(()) unsigned ranges_by_label_in_use;
3071
3072 /* Size (in elements) of increments by which we may expand the
3073    ranges_table.  */
3074 #define RANGES_TABLE_INCREMENT 64
3075
3076 /* Whether we have location lists that need outputting */
3077 static GTY(()) bool have_location_lists;
3078
3079 /* Unique label counter.  */
3080 static GTY(()) unsigned int loclabel_num;
3081
3082 /* Unique label counter for point-of-call tables.  */
3083 static GTY(()) unsigned int poc_label_num;
3084
3085 /* Record whether the function being analyzed contains inlined functions.  */
3086 static int current_function_has_inlines;
3087
3088 /* The last file entry emitted by maybe_emit_file().  */
3089 static GTY(()) struct dwarf_file_data * last_emitted_file;
3090
3091 /* Number of internal labels generated by gen_internal_sym().  */
3092 static GTY(()) int label_num;
3093
3094 /* Cached result of previous call to lookup_filename.  */
3095 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3096
3097 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3098
3099 /* Instances of generic types for which we need to generate debug
3100    info that describe their generic parameters and arguments. That
3101    generation needs to happen once all types are properly laid out so
3102    we do it at the end of compilation.  */
3103 static GTY(()) VEC(tree,gc) *generic_type_instances;
3104
3105 /* Offset from the "steady-state frame pointer" to the frame base,
3106    within the current function.  */
3107 static HOST_WIDE_INT frame_pointer_fb_offset;
3108 static bool frame_pointer_fb_offset_valid;
3109
3110 static VEC (dw_die_ref, heap) *base_types;
3111
3112 /* Forward declarations for functions defined in this file.  */
3113
3114 static int is_pseudo_reg (const_rtx);
3115 static tree type_main_variant (tree);
3116 static int is_tagged_type (const_tree);
3117 static const char *dwarf_tag_name (unsigned);
3118 static const char *dwarf_attr_name (unsigned);
3119 static const char *dwarf_form_name (unsigned);
3120 static tree decl_ultimate_origin (const_tree);
3121 static tree decl_class_context (tree);
3122 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3123 static inline enum dw_val_class AT_class (dw_attr_ref);
3124 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3125 static inline unsigned AT_flag (dw_attr_ref);
3126 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3127 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3128 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3129 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3130 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3131                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3132 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3133                                unsigned int, unsigned char *);
3134 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3135 static hashval_t debug_str_do_hash (const void *);
3136 static int debug_str_eq (const void *, const void *);
3137 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3138 static inline const char *AT_string (dw_attr_ref);
3139 static enum dwarf_form AT_string_form (dw_attr_ref);
3140 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3141 static void add_AT_specification (dw_die_ref, dw_die_ref);
3142 static inline dw_die_ref AT_ref (dw_attr_ref);
3143 static inline int AT_ref_external (dw_attr_ref);
3144 static inline void set_AT_ref_external (dw_attr_ref, int);
3145 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3146 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3147 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3148 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3149                              dw_loc_list_ref);
3150 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3151 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3152 static inline rtx AT_addr (dw_attr_ref);
3153 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3154 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3155 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3156 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3157                            unsigned HOST_WIDE_INT);
3158 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3159                                unsigned long);
3160 static inline const char *AT_lbl (dw_attr_ref);
3161 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3162 static const char *get_AT_low_pc (dw_die_ref);
3163 static const char *get_AT_hi_pc (dw_die_ref);
3164 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3165 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3166 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3167 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3168 static bool is_cxx (void);
3169 static bool is_fortran (void);
3170 static bool is_ada (void);
3171 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3172 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3173 static void add_child_die (dw_die_ref, dw_die_ref);
3174 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3175 static dw_die_ref lookup_type_die (tree);
3176 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3177 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3178 static void equate_type_number_to_die (tree, dw_die_ref);
3179 static hashval_t decl_die_table_hash (const void *);
3180 static int decl_die_table_eq (const void *, const void *);
3181 static dw_die_ref lookup_decl_die (tree);
3182 static hashval_t common_block_die_table_hash (const void *);
3183 static int common_block_die_table_eq (const void *, const void *);
3184 static hashval_t decl_loc_table_hash (const void *);
3185 static int decl_loc_table_eq (const void *, const void *);
3186 static var_loc_list *lookup_decl_loc (const_tree);
3187 static void equate_decl_number_to_die (tree, dw_die_ref);
3188 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3189 static void print_spaces (FILE *);
3190 static void print_die (dw_die_ref, FILE *);
3191 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3192 static dw_die_ref pop_compile_unit (dw_die_ref);
3193 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3194 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3195 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3196 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3197 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3198 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3199 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3200                                    struct md5_ctx *, int *);
3201 struct checksum_attributes;
3202 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3203 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3204 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3205 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3206 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3207 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3208 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3209 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3210 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3211 static void compute_section_prefix (dw_die_ref);
3212 static int is_type_die (dw_die_ref);
3213 static int is_comdat_die (dw_die_ref);
3214 static int is_symbol_die (dw_die_ref);
3215 static void assign_symbol_names (dw_die_ref);
3216 static void break_out_includes (dw_die_ref);
3217 static int is_declaration_die (dw_die_ref);
3218 static int should_move_die_to_comdat (dw_die_ref);
3219 static dw_die_ref clone_as_declaration (dw_die_ref);
3220 static dw_die_ref clone_die (dw_die_ref);
3221 static dw_die_ref clone_tree (dw_die_ref);
3222 static void copy_declaration_context (dw_die_ref, dw_die_ref);
3223 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3224 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3225 static dw_die_ref generate_skeleton (dw_die_ref);
3226 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3227                                                          dw_die_ref);
3228 static void break_out_comdat_types (dw_die_ref);
3229 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3230 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3231 static void copy_decls_for_unworthy_types (dw_die_ref);
3232
3233 static hashval_t htab_cu_hash (const void *);
3234 static int htab_cu_eq (const void *, const void *);
3235 static void htab_cu_del (void *);
3236 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3237 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3238 static void add_sibling_attributes (dw_die_ref);
3239 static void build_abbrev_table (dw_die_ref);
3240 static void output_location_lists (dw_die_ref);
3241 static int constant_size (unsigned HOST_WIDE_INT);
3242 static unsigned long size_of_die (dw_die_ref);
3243 static void calc_die_sizes (dw_die_ref);
3244 static void calc_base_type_die_sizes (void);
3245 static void mark_dies (dw_die_ref);
3246 static void unmark_dies (dw_die_ref);
3247 static void unmark_all_dies (dw_die_ref);
3248 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3249 static unsigned long size_of_aranges (void);
3250 static enum dwarf_form value_format (dw_attr_ref);
3251 static void output_value_format (dw_attr_ref);
3252 static void output_abbrev_section (void);
3253 static void output_die_symbol (dw_die_ref);
3254 static void output_die (dw_die_ref);
3255 static void output_compilation_unit_header (void);
3256 static void output_comp_unit (dw_die_ref, int);
3257 static void output_comdat_type_unit (comdat_type_node *);
3258 static const char *dwarf2_name (tree, int);
3259 static void add_pubname (tree, dw_die_ref);
3260 static void add_pubname_string (const char *, dw_die_ref);
3261 static void add_pubtype (tree, dw_die_ref);
3262 static void output_pubnames (VEC (pubname_entry,gc) *);
3263 static void output_aranges (unsigned long);
3264 static unsigned int add_ranges_num (int);
3265 static unsigned int add_ranges (const_tree);
3266 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3267                                   bool *);
3268 static void output_ranges (void);
3269 static dw_line_info_table *new_line_info_table (void);
3270 static void output_line_info (void);
3271 static void output_file_names (void);
3272 static dw_die_ref base_type_die (tree);
3273 static int is_base_type (tree);
3274 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3275 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3276 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3277 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3278 static int type_is_enum (const_tree);
3279 static unsigned int dbx_reg_number (const_rtx);
3280 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3281 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3282 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3283                                                 enum var_init_status);
3284 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3285                                                      enum var_init_status);
3286 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3287                                          enum var_init_status);
3288 static int is_based_loc (const_rtx);
3289 static int resolve_one_addr (rtx *, void *);
3290 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3291                                                enum var_init_status);
3292 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3293                                         enum var_init_status);
3294 static dw_loc_list_ref loc_list_from_tree (tree, int);
3295 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3296 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3297 static tree field_type (const_tree);
3298 static unsigned int simple_type_align_in_bits (const_tree);
3299 static unsigned int simple_decl_align_in_bits (const_tree);
3300 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3301 static HOST_WIDE_INT field_byte_offset (const_tree);
3302 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3303                                          dw_loc_list_ref);
3304 static void add_data_member_location_attribute (dw_die_ref, tree);
3305 static bool add_const_value_attribute (dw_die_ref, rtx);
3306 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3307 static void insert_double (double_int, unsigned char *);
3308 static void insert_float (const_rtx, unsigned char *);
3309 static rtx rtl_for_decl_location (tree);
3310 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3311                                                    enum dwarf_attribute);
3312 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3313 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3314 static void add_name_attribute (dw_die_ref, const char *);
3315 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3316 static void add_comp_dir_attribute (dw_die_ref);
3317 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3318 static void add_subscript_info (dw_die_ref, tree, bool);
3319 static void add_byte_size_attribute (dw_die_ref, tree);
3320 static void add_bit_offset_attribute (dw_die_ref, tree);
3321 static void add_bit_size_attribute (dw_die_ref, tree);
3322 static void add_prototyped_attribute (dw_die_ref, tree);
3323 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
3324 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
3325 static void add_src_coords_attributes (dw_die_ref, tree);
3326 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
3327 static void push_decl_scope (tree);
3328 static void pop_decl_scope (void);
3329 static dw_die_ref scope_die_for (tree, dw_die_ref);
3330 static inline int local_scope_p (dw_die_ref);
3331 static inline int class_scope_p (dw_die_ref);
3332 static inline int class_or_namespace_scope_p (dw_die_ref);
3333 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
3334 static void add_calling_convention_attribute (dw_die_ref, tree);
3335 static const char *type_tag (const_tree);
3336 static tree member_declared_type (const_tree);
3337 #if 0
3338 static const char *decl_start_label (tree);
3339 #endif
3340 static void gen_array_type_die (tree, dw_die_ref);
3341 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
3342 #if 0
3343 static void gen_entry_point_die (tree, dw_die_ref);
3344 #endif
3345 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
3346 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
3347 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
3348 static void gen_unspecified_parameters_die (tree, dw_die_ref);
3349 static void gen_formal_types_die (tree, dw_die_ref);
3350 static void gen_subprogram_die (tree, dw_die_ref);
3351 static void gen_variable_die (tree, tree, dw_die_ref);
3352 static void gen_const_die (tree, dw_die_ref);
3353 static void gen_label_die (tree, dw_die_ref);
3354 static void gen_lexical_block_die (tree, dw_die_ref, int);
3355 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
3356 static void gen_field_die (tree, dw_die_ref);
3357 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
3358 static dw_die_ref gen_compile_unit_die (const char *);
3359 static void gen_inheritance_die (tree, tree, dw_die_ref);
3360 static void gen_member_die (tree, dw_die_ref);
3361 static void gen_struct_or_union_type_die (tree, dw_die_ref,
3362                                                 enum debug_info_usage);
3363 static void gen_subroutine_type_die (tree, dw_die_ref);
3364 static void gen_typedef_die (tree, dw_die_ref);
3365 static void gen_type_die (tree, dw_die_ref);
3366 static void gen_block_die (tree, dw_die_ref, int);
3367 static void decls_for_scope (tree, dw_die_ref, int);
3368 static inline int is_redundant_typedef (const_tree);
3369 static bool is_naming_typedef_decl (const_tree);
3370 static inline dw_die_ref get_context_die (tree);
3371 static void gen_namespace_die (tree, dw_die_ref);
3372 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
3373 static dw_die_ref force_decl_die (tree);
3374 static dw_die_ref force_type_die (tree);
3375 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
3376 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
3377 static struct dwarf_file_data * lookup_filename (const char *);
3378 static void retry_incomplete_types (void);
3379 static void gen_type_die_for_member (tree, tree, dw_die_ref);
3380 static void gen_generic_params_dies (tree);
3381 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
3382 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
3383 static void splice_child_die (dw_die_ref, dw_die_ref);
3384 static int file_info_cmp (const void *, const void *);
3385 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
3386                                      const char *, const char *);
3387 static void output_loc_list (dw_loc_list_ref);
3388 static char *gen_internal_sym (const char *);
3389
3390 static void prune_unmark_dies (dw_die_ref);
3391 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
3392 static void prune_unused_types_mark (dw_die_ref, int);
3393 static void prune_unused_types_walk (dw_die_ref);
3394 static void prune_unused_types_walk_attribs (dw_die_ref);
3395 static void prune_unused_types_prune (dw_die_ref);
3396 static void prune_unused_types (void);
3397 static int maybe_emit_file (struct dwarf_file_data *fd);
3398 static inline const char *AT_vms_delta1 (dw_attr_ref);
3399 static inline const char *AT_vms_delta2 (dw_attr_ref);
3400 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
3401                                      const char *, const char *);
3402 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
3403 static void gen_remaining_tmpl_value_param_die_attribute (void);
3404 static bool generic_type_p (tree);
3405 static void schedule_generic_params_dies_gen (tree t);
3406 static void gen_scheduled_generic_parms_dies (void);
3407
3408 /* Section names used to hold DWARF debugging information.  */
3409 #ifndef DEBUG_INFO_SECTION
3410 #define DEBUG_INFO_SECTION      ".debug_info"
3411 #endif
3412 #ifndef DEBUG_ABBREV_SECTION
3413 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
3414 #endif
3415 #ifndef DEBUG_ARANGES_SECTION
3416 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
3417 #endif
3418 #ifndef DEBUG_MACINFO_SECTION
3419 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
3420 #endif
3421 #ifndef DEBUG_MACRO_SECTION
3422 #define DEBUG_MACRO_SECTION     ".debug_macro"
3423 #endif
3424 #ifndef DEBUG_LINE_SECTION
3425 #define DEBUG_LINE_SECTION      ".debug_line"
3426 #endif
3427 #ifndef DEBUG_LOC_SECTION
3428 #define DEBUG_LOC_SECTION       ".debug_loc"
3429 #endif
3430 #ifndef DEBUG_PUBNAMES_SECTION
3431 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3432 #endif
3433 #ifndef DEBUG_PUBTYPES_SECTION
3434 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3435 #endif
3436 #ifndef DEBUG_STR_SECTION
3437 #define DEBUG_STR_SECTION       ".debug_str"
3438 #endif
3439 #ifndef DEBUG_RANGES_SECTION
3440 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3441 #endif
3442
3443 /* Standard ELF section names for compiled code and data.  */
3444 #ifndef TEXT_SECTION_NAME
3445 #define TEXT_SECTION_NAME       ".text"
3446 #endif
3447
3448 /* Section flags for .debug_str section.  */
3449 #define DEBUG_STR_SECTION_FLAGS \
3450   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3451    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3452    : SECTION_DEBUG)
3453
3454 /* Labels we insert at beginning sections we can reference instead of
3455    the section names themselves.  */
3456
3457 #ifndef TEXT_SECTION_LABEL
3458 #define TEXT_SECTION_LABEL              "Ltext"
3459 #endif
3460 #ifndef COLD_TEXT_SECTION_LABEL
3461 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3462 #endif
3463 #ifndef DEBUG_LINE_SECTION_LABEL
3464 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3465 #endif
3466 #ifndef DEBUG_INFO_SECTION_LABEL
3467 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3468 #endif
3469 #ifndef DEBUG_ABBREV_SECTION_LABEL
3470 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3471 #endif
3472 #ifndef DEBUG_LOC_SECTION_LABEL
3473 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3474 #endif
3475 #ifndef DEBUG_RANGES_SECTION_LABEL
3476 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3477 #endif
3478 #ifndef DEBUG_MACINFO_SECTION_LABEL
3479 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3480 #endif
3481 #ifndef DEBUG_MACRO_SECTION_LABEL
3482 #define DEBUG_MACRO_SECTION_LABEL       "Ldebug_macro"
3483 #endif
3484
3485
3486 /* Definitions of defaults for formats and names of various special
3487    (artificial) labels which may be generated within this file (when the -g
3488    options is used and DWARF2_DEBUGGING_INFO is in effect.
3489    If necessary, these may be overridden from within the tm.h file, but
3490    typically, overriding these defaults is unnecessary.  */
3491
3492 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3493 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3494 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3495 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3496 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3497 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3498 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3499 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3500 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3501 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3502
3503 #ifndef TEXT_END_LABEL
3504 #define TEXT_END_LABEL          "Letext"
3505 #endif
3506 #ifndef COLD_END_LABEL
3507 #define COLD_END_LABEL          "Letext_cold"
3508 #endif
3509 #ifndef BLOCK_BEGIN_LABEL
3510 #define BLOCK_BEGIN_LABEL       "LBB"
3511 #endif
3512 #ifndef BLOCK_END_LABEL
3513 #define BLOCK_END_LABEL         "LBE"
3514 #endif
3515 #ifndef LINE_CODE_LABEL
3516 #define LINE_CODE_LABEL         "LM"
3517 #endif
3518
3519 \f
3520 /* Return the root of the DIE's built for the current compilation unit.  */
3521 static dw_die_ref
3522 comp_unit_die (void)
3523 {
3524   if (!single_comp_unit_die)
3525     single_comp_unit_die = gen_compile_unit_die (NULL);
3526   return single_comp_unit_die;
3527 }
3528
3529 /* We allow a language front-end to designate a function that is to be
3530    called to "demangle" any name before it is put into a DIE.  */
3531
3532 static const char *(*demangle_name_func) (const char *);
3533
3534 void
3535 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3536 {
3537   demangle_name_func = func;
3538 }
3539
3540 /* Test if rtl node points to a pseudo register.  */
3541
3542 static inline int
3543 is_pseudo_reg (const_rtx rtl)
3544 {
3545   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3546           || (GET_CODE (rtl) == SUBREG
3547               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3548 }
3549
3550 /* Return a reference to a type, with its const and volatile qualifiers
3551    removed.  */
3552
3553 static inline tree
3554 type_main_variant (tree type)
3555 {
3556   type = TYPE_MAIN_VARIANT (type);
3557
3558   /* ??? There really should be only one main variant among any group of
3559      variants of a given type (and all of the MAIN_VARIANT values for all
3560      members of the group should point to that one type) but sometimes the C
3561      front-end messes this up for array types, so we work around that bug
3562      here.  */
3563   if (TREE_CODE (type) == ARRAY_TYPE)
3564     while (type != TYPE_MAIN_VARIANT (type))
3565       type = TYPE_MAIN_VARIANT (type);
3566
3567   return type;
3568 }
3569
3570 /* Return nonzero if the given type node represents a tagged type.  */
3571
3572 static inline int
3573 is_tagged_type (const_tree type)
3574 {
3575   enum tree_code code = TREE_CODE (type);
3576
3577   return (code == RECORD_TYPE || code == UNION_TYPE
3578           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3579 }
3580
3581 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3582
3583 static void
3584 get_ref_die_offset_label (char *label, dw_die_ref ref)
3585 {
3586   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3587 }
3588
3589 /* Return die_offset of a DIE reference to a base type.  */
3590
3591 static unsigned long int
3592 get_base_type_offset (dw_die_ref ref)
3593 {
3594   if (ref->die_offset)
3595     return ref->die_offset;
3596   if (comp_unit_die ()->die_abbrev)
3597     {
3598       calc_base_type_die_sizes ();
3599       gcc_assert (ref->die_offset);
3600     }
3601   return ref->die_offset;
3602 }
3603
3604 /* Return die_offset of a DIE reference other than base type.  */
3605
3606 static unsigned long int
3607 get_ref_die_offset (dw_die_ref ref)
3608 {
3609   gcc_assert (ref->die_offset);
3610   return ref->die_offset;
3611 }
3612
3613 /* Convert a DIE tag into its string name.  */
3614
3615 static const char *
3616 dwarf_tag_name (unsigned int tag)
3617 {
3618   switch (tag)
3619     {
3620     case DW_TAG_padding:
3621       return "DW_TAG_padding";
3622     case DW_TAG_array_type:
3623       return "DW_TAG_array_type";
3624     case DW_TAG_class_type:
3625       return "DW_TAG_class_type";
3626     case DW_TAG_entry_point:
3627       return "DW_TAG_entry_point";
3628     case DW_TAG_enumeration_type:
3629       return "DW_TAG_enumeration_type";
3630     case DW_TAG_formal_parameter:
3631       return "DW_TAG_formal_parameter";
3632     case DW_TAG_imported_declaration:
3633       return "DW_TAG_imported_declaration";
3634     case DW_TAG_label:
3635       return "DW_TAG_label";
3636     case DW_TAG_lexical_block:
3637       return "DW_TAG_lexical_block";
3638     case DW_TAG_member:
3639       return "DW_TAG_member";
3640     case DW_TAG_pointer_type:
3641       return "DW_TAG_pointer_type";
3642     case DW_TAG_reference_type:
3643       return "DW_TAG_reference_type";
3644     case DW_TAG_compile_unit:
3645       return "DW_TAG_compile_unit";
3646     case DW_TAG_string_type:
3647       return "DW_TAG_string_type";
3648     case DW_TAG_structure_type:
3649       return "DW_TAG_structure_type";
3650     case DW_TAG_subroutine_type:
3651       return "DW_TAG_subroutine_type";
3652     case DW_TAG_typedef:
3653       return "DW_TAG_typedef";
3654     case DW_TAG_union_type:
3655       return "DW_TAG_union_type";
3656     case DW_TAG_unspecified_parameters:
3657       return "DW_TAG_unspecified_parameters";
3658     case DW_TAG_variant:
3659       return "DW_TAG_variant";
3660     case DW_TAG_common_block:
3661       return "DW_TAG_common_block";
3662     case DW_TAG_common_inclusion:
3663       return "DW_TAG_common_inclusion";
3664     case DW_TAG_inheritance:
3665       return "DW_TAG_inheritance";
3666     case DW_TAG_inlined_subroutine:
3667       return "DW_TAG_inlined_subroutine";
3668     case DW_TAG_module:
3669       return "DW_TAG_module";
3670     case DW_TAG_ptr_to_member_type:
3671       return "DW_TAG_ptr_to_member_type";
3672     case DW_TAG_set_type:
3673       return "DW_TAG_set_type";
3674     case DW_TAG_subrange_type:
3675       return "DW_TAG_subrange_type";
3676     case DW_TAG_with_stmt:
3677       return "DW_TAG_with_stmt";
3678     case DW_TAG_access_declaration:
3679       return "DW_TAG_access_declaration";
3680     case DW_TAG_base_type:
3681       return "DW_TAG_base_type";
3682     case DW_TAG_catch_block:
3683       return "DW_TAG_catch_block";
3684     case DW_TAG_const_type:
3685       return "DW_TAG_const_type";
3686     case DW_TAG_constant:
3687       return "DW_TAG_constant";
3688     case DW_TAG_enumerator:
3689       return "DW_TAG_enumerator";
3690     case DW_TAG_file_type:
3691       return "DW_TAG_file_type";
3692     case DW_TAG_friend:
3693       return "DW_TAG_friend";
3694     case DW_TAG_namelist:
3695       return "DW_TAG_namelist";
3696     case DW_TAG_namelist_item:
3697       return "DW_TAG_namelist_item";
3698     case DW_TAG_packed_type:
3699       return "DW_TAG_packed_type";
3700     case DW_TAG_subprogram:
3701       return "DW_TAG_subprogram";
3702     case DW_TAG_template_type_param:
3703       return "DW_TAG_template_type_param";
3704     case DW_TAG_template_value_param:
3705       return "DW_TAG_template_value_param";
3706     case DW_TAG_thrown_type:
3707       return "DW_TAG_thrown_type";
3708     case DW_TAG_try_block:
3709       return "DW_TAG_try_block";
3710     case DW_TAG_variant_part:
3711       return "DW_TAG_variant_part";
3712     case DW_TAG_variable:
3713       return "DW_TAG_variable";
3714     case DW_TAG_volatile_type:
3715       return "DW_TAG_volatile_type";
3716     case DW_TAG_dwarf_procedure:
3717       return "DW_TAG_dwarf_procedure";
3718     case DW_TAG_restrict_type:
3719       return "DW_TAG_restrict_type";
3720     case DW_TAG_interface_type:
3721       return "DW_TAG_interface_type";
3722     case DW_TAG_namespace:
3723       return "DW_TAG_namespace";
3724     case DW_TAG_imported_module:
3725       return "DW_TAG_imported_module";
3726     case DW_TAG_unspecified_type:
3727       return "DW_TAG_unspecified_type";
3728     case DW_TAG_partial_unit:
3729       return "DW_TAG_partial_unit";
3730     case DW_TAG_imported_unit:
3731       return "DW_TAG_imported_unit";
3732     case DW_TAG_condition:
3733       return "DW_TAG_condition";
3734     case DW_TAG_shared_type:
3735       return "DW_TAG_shared_type";
3736     case DW_TAG_type_unit:
3737       return "DW_TAG_type_unit";
3738     case DW_TAG_rvalue_reference_type:
3739       return "DW_TAG_rvalue_reference_type";
3740     case DW_TAG_template_alias:
3741       return "DW_TAG_template_alias";
3742     case DW_TAG_GNU_template_parameter_pack:
3743       return "DW_TAG_GNU_template_parameter_pack";
3744     case DW_TAG_GNU_formal_parameter_pack:
3745       return "DW_TAG_GNU_formal_parameter_pack";
3746     case DW_TAG_MIPS_loop:
3747       return "DW_TAG_MIPS_loop";
3748     case DW_TAG_format_label:
3749       return "DW_TAG_format_label";
3750     case DW_TAG_function_template:
3751       return "DW_TAG_function_template";
3752     case DW_TAG_class_template:
3753       return "DW_TAG_class_template";
3754     case DW_TAG_GNU_BINCL:
3755       return "DW_TAG_GNU_BINCL";
3756     case DW_TAG_GNU_EINCL:
3757       return "DW_TAG_GNU_EINCL";
3758     case DW_TAG_GNU_template_template_param:
3759       return "DW_TAG_GNU_template_template_param";
3760     case DW_TAG_GNU_call_site:
3761       return "DW_TAG_GNU_call_site";
3762     case DW_TAG_GNU_call_site_parameter:
3763       return "DW_TAG_GNU_call_site_parameter";
3764     default:
3765       return "DW_TAG_<unknown>";
3766     }
3767 }
3768
3769 /* Convert a DWARF attribute code into its string name.  */
3770
3771 static const char *
3772 dwarf_attr_name (unsigned int attr)
3773 {
3774   switch (attr)
3775     {
3776     case DW_AT_sibling:
3777       return "DW_AT_sibling";
3778     case DW_AT_location:
3779       return "DW_AT_location";
3780     case DW_AT_name:
3781       return "DW_AT_name";
3782     case DW_AT_ordering:
3783       return "DW_AT_ordering";
3784     case DW_AT_subscr_data:
3785       return "DW_AT_subscr_data";
3786     case DW_AT_byte_size:
3787       return "DW_AT_byte_size";
3788     case DW_AT_bit_offset:
3789       return "DW_AT_bit_offset";
3790     case DW_AT_bit_size:
3791       return "DW_AT_bit_size";
3792     case DW_AT_element_list:
3793       return "DW_AT_element_list";
3794     case DW_AT_stmt_list:
3795       return "DW_AT_stmt_list";
3796     case DW_AT_low_pc:
3797       return "DW_AT_low_pc";
3798     case DW_AT_high_pc:
3799       return "DW_AT_high_pc";
3800     case DW_AT_language:
3801       return "DW_AT_language";
3802     case DW_AT_member:
3803       return "DW_AT_member";
3804     case DW_AT_discr:
3805       return "DW_AT_discr";
3806     case DW_AT_discr_value:
3807       return "DW_AT_discr_value";
3808     case DW_AT_visibility:
3809       return "DW_AT_visibility";
3810     case DW_AT_import:
3811       return "DW_AT_import";
3812     case DW_AT_string_length:
3813       return "DW_AT_string_length";
3814     case DW_AT_common_reference:
3815       return "DW_AT_common_reference";
3816     case DW_AT_comp_dir:
3817       return "DW_AT_comp_dir";
3818     case DW_AT_const_value:
3819       return "DW_AT_const_value";
3820     case DW_AT_containing_type:
3821       return "DW_AT_containing_type";
3822     case DW_AT_default_value:
3823       return "DW_AT_default_value";
3824     case DW_AT_inline:
3825       return "DW_AT_inline";
3826     case DW_AT_is_optional:
3827       return "DW_AT_is_optional";
3828     case DW_AT_lower_bound:
3829       return "DW_AT_lower_bound";
3830     case DW_AT_producer:
3831       return "DW_AT_producer";
3832     case DW_AT_prototyped:
3833       return "DW_AT_prototyped";
3834     case DW_AT_return_addr:
3835       return "DW_AT_return_addr";
3836     case DW_AT_start_scope:
3837       return "DW_AT_start_scope";
3838     case DW_AT_bit_stride:
3839       return "DW_AT_bit_stride";
3840     case DW_AT_upper_bound:
3841       return "DW_AT_upper_bound";
3842     case DW_AT_abstract_origin:
3843       return "DW_AT_abstract_origin";
3844     case DW_AT_accessibility:
3845       return "DW_AT_accessibility";
3846     case DW_AT_address_class:
3847       return "DW_AT_address_class";
3848     case DW_AT_artificial:
3849       return "DW_AT_artificial";
3850     case DW_AT_base_types:
3851       return "DW_AT_base_types";
3852     case DW_AT_calling_convention:
3853       return "DW_AT_calling_convention";
3854     case DW_AT_count:
3855       return "DW_AT_count";
3856     case DW_AT_data_member_location:
3857       return "DW_AT_data_member_location";
3858     case DW_AT_decl_column:
3859       return "DW_AT_decl_column";
3860     case DW_AT_decl_file:
3861       return "DW_AT_decl_file";
3862     case DW_AT_decl_line:
3863       return "DW_AT_decl_line";
3864     case DW_AT_declaration:
3865       return "DW_AT_declaration";
3866     case DW_AT_discr_list:
3867       return "DW_AT_discr_list";
3868     case DW_AT_encoding:
3869       return "DW_AT_encoding";
3870     case DW_AT_external:
3871       return "DW_AT_external";
3872     case DW_AT_explicit:
3873       return "DW_AT_explicit";
3874     case DW_AT_frame_base:
3875       return "DW_AT_frame_base";
3876     case DW_AT_friend:
3877       return "DW_AT_friend";
3878     case DW_AT_identifier_case:
3879       return "DW_AT_identifier_case";
3880     case DW_AT_macro_info:
3881       return "DW_AT_macro_info";
3882     case DW_AT_namelist_items:
3883       return "DW_AT_namelist_items";
3884     case DW_AT_priority:
3885       return "DW_AT_priority";
3886     case DW_AT_segment:
3887       return "DW_AT_segment";
3888     case DW_AT_specification:
3889       return "DW_AT_specification";
3890     case DW_AT_static_link:
3891       return "DW_AT_static_link";
3892     case DW_AT_type:
3893       return "DW_AT_type";
3894     case DW_AT_use_location:
3895       return "DW_AT_use_location";
3896     case DW_AT_variable_parameter:
3897       return "DW_AT_variable_parameter";
3898     case DW_AT_virtuality:
3899       return "DW_AT_virtuality";
3900     case DW_AT_vtable_elem_location:
3901       return "DW_AT_vtable_elem_location";
3902
3903     case DW_AT_allocated:
3904       return "DW_AT_allocated";
3905     case DW_AT_associated:
3906       return "DW_AT_associated";
3907     case DW_AT_data_location:
3908       return "DW_AT_data_location";
3909     case DW_AT_byte_stride:
3910       return "DW_AT_byte_stride";
3911     case DW_AT_entry_pc:
3912       return "DW_AT_entry_pc";
3913     case DW_AT_use_UTF8:
3914       return "DW_AT_use_UTF8";
3915     case DW_AT_extension:
3916       return "DW_AT_extension";
3917     case DW_AT_ranges:
3918       return "DW_AT_ranges";
3919     case DW_AT_trampoline:
3920       return "DW_AT_trampoline";
3921     case DW_AT_call_column:
3922       return "DW_AT_call_column";
3923     case DW_AT_call_file:
3924       return "DW_AT_call_file";
3925     case DW_AT_call_line:
3926       return "DW_AT_call_line";
3927     case DW_AT_object_pointer:
3928       return "DW_AT_object_pointer";
3929
3930     case DW_AT_signature:
3931       return "DW_AT_signature";
3932     case DW_AT_main_subprogram:
3933       return "DW_AT_main_subprogram";
3934     case DW_AT_data_bit_offset:
3935       return "DW_AT_data_bit_offset";
3936     case DW_AT_const_expr:
3937       return "DW_AT_const_expr";
3938     case DW_AT_enum_class:
3939       return "DW_AT_enum_class";
3940     case DW_AT_linkage_name:
3941       return "DW_AT_linkage_name";
3942
3943     case DW_AT_MIPS_fde:
3944       return "DW_AT_MIPS_fde";
3945     case DW_AT_MIPS_loop_begin:
3946       return "DW_AT_MIPS_loop_begin";
3947     case DW_AT_MIPS_tail_loop_begin:
3948       return "DW_AT_MIPS_tail_loop_begin";
3949     case DW_AT_MIPS_epilog_begin:
3950       return "DW_AT_MIPS_epilog_begin";
3951 #if VMS_DEBUGGING_INFO
3952     case DW_AT_HP_prologue:
3953       return "DW_AT_HP_prologue";
3954 #else
3955     case DW_AT_MIPS_loop_unroll_factor:
3956       return "DW_AT_MIPS_loop_unroll_factor";
3957 #endif
3958     case DW_AT_MIPS_software_pipeline_depth:
3959       return "DW_AT_MIPS_software_pipeline_depth";
3960     case DW_AT_MIPS_linkage_name:
3961       return "DW_AT_MIPS_linkage_name";
3962 #if VMS_DEBUGGING_INFO
3963     case DW_AT_HP_epilogue:
3964       return "DW_AT_HP_epilogue";
3965 #else
3966     case DW_AT_MIPS_stride:
3967       return "DW_AT_MIPS_stride";
3968 #endif
3969     case DW_AT_MIPS_abstract_name:
3970       return "DW_AT_MIPS_abstract_name";
3971     case DW_AT_MIPS_clone_origin:
3972       return "DW_AT_MIPS_clone_origin";
3973     case DW_AT_MIPS_has_inlines:
3974       return "DW_AT_MIPS_has_inlines";
3975
3976     case DW_AT_sf_names:
3977       return "DW_AT_sf_names";
3978     case DW_AT_src_info:
3979       return "DW_AT_src_info";
3980     case DW_AT_mac_info:
3981       return "DW_AT_mac_info";
3982     case DW_AT_src_coords:
3983       return "DW_AT_src_coords";
3984     case DW_AT_body_begin:
3985       return "DW_AT_body_begin";
3986     case DW_AT_body_end:
3987       return "DW_AT_body_end";
3988
3989     case DW_AT_GNU_vector:
3990       return "DW_AT_GNU_vector";
3991     case DW_AT_GNU_guarded_by:
3992       return "DW_AT_GNU_guarded_by";
3993     case DW_AT_GNU_pt_guarded_by:
3994       return "DW_AT_GNU_pt_guarded_by";
3995     case DW_AT_GNU_guarded:
3996       return "DW_AT_GNU_guarded";
3997     case DW_AT_GNU_pt_guarded:
3998       return "DW_AT_GNU_pt_guarded";
3999     case DW_AT_GNU_locks_excluded:
4000       return "DW_AT_GNU_locks_excluded";
4001     case DW_AT_GNU_exclusive_locks_required:
4002       return "DW_AT_GNU_exclusive_locks_required";
4003     case DW_AT_GNU_shared_locks_required:
4004       return "DW_AT_GNU_shared_locks_required";
4005     case DW_AT_GNU_odr_signature:
4006       return "DW_AT_GNU_odr_signature";
4007     case DW_AT_GNU_template_name:
4008       return "DW_AT_GNU_template_name";
4009     case DW_AT_GNU_call_site_value:
4010       return "DW_AT_GNU_call_site_value";
4011     case DW_AT_GNU_call_site_data_value:
4012       return "DW_AT_GNU_call_site_data_value";
4013     case DW_AT_GNU_call_site_target:
4014       return "DW_AT_GNU_call_site_target";
4015     case DW_AT_GNU_call_site_target_clobbered:
4016       return "DW_AT_GNU_call_site_target_clobbered";
4017     case DW_AT_GNU_tail_call:
4018       return "DW_AT_GNU_tail_call";
4019     case DW_AT_GNU_all_tail_call_sites:
4020       return "DW_AT_GNU_all_tail_call_sites";
4021     case DW_AT_GNU_all_call_sites:
4022       return "DW_AT_GNU_all_call_sites";
4023     case DW_AT_GNU_all_source_call_sites:
4024       return "DW_AT_GNU_all_source_call_sites";
4025     case DW_AT_GNU_macros:
4026       return "DW_AT_GNU_macros";
4027
4028     case DW_AT_GNAT_descriptive_type:
4029       return "DW_AT_GNAT_descriptive_type";
4030
4031     case DW_AT_VMS_rtnbeg_pd_address:
4032       return "DW_AT_VMS_rtnbeg_pd_address";
4033
4034     default:
4035       return "DW_AT_<unknown>";
4036     }
4037 }
4038
4039 /* Convert a DWARF value form code into its string name.  */
4040
4041 static const char *
4042 dwarf_form_name (unsigned int form)
4043 {
4044   switch (form)
4045     {
4046     case DW_FORM_addr:
4047       return "DW_FORM_addr";
4048     case DW_FORM_block2:
4049       return "DW_FORM_block2";
4050     case DW_FORM_block4:
4051       return "DW_FORM_block4";
4052     case DW_FORM_data2:
4053       return "DW_FORM_data2";
4054     case DW_FORM_data4:
4055       return "DW_FORM_data4";
4056     case DW_FORM_data8:
4057       return "DW_FORM_data8";
4058     case DW_FORM_string:
4059       return "DW_FORM_string";
4060     case DW_FORM_block:
4061       return "DW_FORM_block";
4062     case DW_FORM_block1:
4063       return "DW_FORM_block1";
4064     case DW_FORM_data1:
4065       return "DW_FORM_data1";
4066     case DW_FORM_flag:
4067       return "DW_FORM_flag";
4068     case DW_FORM_sdata:
4069       return "DW_FORM_sdata";
4070     case DW_FORM_strp:
4071       return "DW_FORM_strp";
4072     case DW_FORM_udata:
4073       return "DW_FORM_udata";
4074     case DW_FORM_ref_addr:
4075       return "DW_FORM_ref_addr";
4076     case DW_FORM_ref1:
4077       return "DW_FORM_ref1";
4078     case DW_FORM_ref2:
4079       return "DW_FORM_ref2";
4080     case DW_FORM_ref4:
4081       return "DW_FORM_ref4";
4082     case DW_FORM_ref8:
4083       return "DW_FORM_ref8";
4084     case DW_FORM_ref_udata:
4085       return "DW_FORM_ref_udata";
4086     case DW_FORM_indirect:
4087       return "DW_FORM_indirect";
4088     case DW_FORM_sec_offset:
4089       return "DW_FORM_sec_offset";
4090     case DW_FORM_exprloc:
4091       return "DW_FORM_exprloc";
4092     case DW_FORM_flag_present:
4093       return "DW_FORM_flag_present";
4094     case DW_FORM_ref_sig8:
4095       return "DW_FORM_ref_sig8";
4096     default:
4097       return "DW_FORM_<unknown>";
4098     }
4099 }
4100 \f
4101 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4102    instance of an inlined instance of a decl which is local to an inline
4103    function, so we have to trace all of the way back through the origin chain
4104    to find out what sort of node actually served as the original seed for the
4105    given block.  */
4106
4107 static tree
4108 decl_ultimate_origin (const_tree decl)
4109 {
4110   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4111     return NULL_TREE;
4112
4113   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4114      nodes in the function to point to themselves; ignore that if
4115      we're trying to output the abstract instance of this function.  */
4116   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4117     return NULL_TREE;
4118
4119   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4120      most distant ancestor, this should never happen.  */
4121   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4122
4123   return DECL_ABSTRACT_ORIGIN (decl);
4124 }
4125
4126 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4127    of a virtual function may refer to a base class, so we check the 'this'
4128    parameter.  */
4129
4130 static tree
4131 decl_class_context (tree decl)
4132 {
4133   tree context = NULL_TREE;
4134
4135   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4136     context = DECL_CONTEXT (decl);
4137   else
4138     context = TYPE_MAIN_VARIANT
4139       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4140
4141   if (context && !TYPE_P (context))
4142     context = NULL_TREE;
4143
4144   return context;
4145 }
4146 \f
4147 /* Add an attribute/value pair to a DIE.  */
4148
4149 static inline void
4150 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4151 {
4152   /* Maybe this should be an assert?  */
4153   if (die == NULL)
4154     return;
4155
4156   if (die->die_attr == NULL)
4157     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4158   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4159 }
4160
4161 static inline enum dw_val_class
4162 AT_class (dw_attr_ref a)
4163 {
4164   return a->dw_attr_val.val_class;
4165 }
4166
4167 /* Add a flag value attribute to a DIE.  */
4168
4169 static inline void
4170 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4171 {
4172   dw_attr_node attr;
4173
4174   attr.dw_attr = attr_kind;
4175   attr.dw_attr_val.val_class = dw_val_class_flag;
4176   attr.dw_attr_val.v.val_flag = flag;
4177   add_dwarf_attr (die, &attr);
4178 }
4179
4180 static inline unsigned
4181 AT_flag (dw_attr_ref a)
4182 {
4183   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4184   return a->dw_attr_val.v.val_flag;
4185 }
4186
4187 /* Add a signed integer attribute value to a DIE.  */
4188
4189 static inline void
4190 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4191 {
4192   dw_attr_node attr;
4193
4194   attr.dw_attr = attr_kind;
4195   attr.dw_attr_val.val_class = dw_val_class_const;
4196   attr.dw_attr_val.v.val_int = int_val;
4197   add_dwarf_attr (die, &attr);
4198 }
4199
4200 static inline HOST_WIDE_INT
4201 AT_int (dw_attr_ref a)
4202 {
4203   gcc_assert (a && AT_class (a) == dw_val_class_const);
4204   return a->dw_attr_val.v.val_int;
4205 }
4206
4207 /* Add an unsigned integer attribute value to a DIE.  */
4208
4209 static inline void
4210 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4211                  unsigned HOST_WIDE_INT unsigned_val)
4212 {
4213   dw_attr_node attr;
4214
4215   attr.dw_attr = attr_kind;
4216   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4217   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4218   add_dwarf_attr (die, &attr);
4219 }
4220
4221 static inline unsigned HOST_WIDE_INT
4222 AT_unsigned (dw_attr_ref a)
4223 {
4224   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4225   return a->dw_attr_val.v.val_unsigned;
4226 }
4227
4228 /* Add an unsigned double integer attribute value to a DIE.  */
4229
4230 static inline void
4231 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4232                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4233 {
4234   dw_attr_node attr;
4235
4236   attr.dw_attr = attr_kind;
4237   attr.dw_attr_val.val_class = dw_val_class_const_double;
4238   attr.dw_attr_val.v.val_double.high = high;
4239   attr.dw_attr_val.v.val_double.low = low;
4240   add_dwarf_attr (die, &attr);
4241 }
4242
4243 /* Add a floating point attribute value to a DIE and return it.  */
4244
4245 static inline void
4246 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4247             unsigned int length, unsigned int elt_size, unsigned char *array)
4248 {
4249   dw_attr_node attr;
4250
4251   attr.dw_attr = attr_kind;
4252   attr.dw_attr_val.val_class = dw_val_class_vec;
4253   attr.dw_attr_val.v.val_vec.length = length;
4254   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4255   attr.dw_attr_val.v.val_vec.array = array;
4256   add_dwarf_attr (die, &attr);
4257 }
4258
4259 /* Add an 8-byte data attribute value to a DIE.  */
4260
4261 static inline void
4262 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4263               unsigned char data8[8])
4264 {
4265   dw_attr_node attr;
4266
4267   attr.dw_attr = attr_kind;
4268   attr.dw_attr_val.val_class = dw_val_class_data8;
4269   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4270   add_dwarf_attr (die, &attr);
4271 }
4272
4273 /* Hash and equality functions for debug_str_hash.  */
4274
4275 static hashval_t
4276 debug_str_do_hash (const void *x)
4277 {
4278   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4279 }
4280
4281 static int
4282 debug_str_eq (const void *x1, const void *x2)
4283 {
4284   return strcmp ((((const struct indirect_string_node *)x1)->str),
4285                  (const char *)x2) == 0;
4286 }
4287
4288 /* Add STR to the indirect string hash table.  */
4289
4290 static struct indirect_string_node *
4291 find_AT_string (const char *str)
4292 {
4293   struct indirect_string_node *node;
4294   void **slot;
4295
4296   if (! debug_str_hash)
4297     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4298                                       debug_str_eq, NULL);
4299
4300   slot = htab_find_slot_with_hash (debug_str_hash, str,
4301                                    htab_hash_string (str), INSERT);
4302   if (*slot == NULL)
4303     {
4304       node = ggc_alloc_cleared_indirect_string_node ();
4305       node->str = ggc_strdup (str);
4306       *slot = node;
4307     }
4308   else
4309     node = (struct indirect_string_node *) *slot;
4310
4311   node->refcount++;
4312   return node;
4313 }
4314
4315 /* Add a string attribute value to a DIE.  */
4316
4317 static inline void
4318 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4319 {
4320   dw_attr_node attr;
4321   struct indirect_string_node *node;
4322
4323   node = find_AT_string (str);
4324
4325   attr.dw_attr = attr_kind;
4326   attr.dw_attr_val.val_class = dw_val_class_str;
4327   attr.dw_attr_val.v.val_str = node;
4328   add_dwarf_attr (die, &attr);
4329 }
4330
4331 static inline const char *
4332 AT_string (dw_attr_ref a)
4333 {
4334   gcc_assert (a && AT_class (a) == dw_val_class_str);
4335   return a->dw_attr_val.v.val_str->str;
4336 }
4337
4338 /* Find out whether a string should be output inline in DIE
4339    or out-of-line in .debug_str section.  */
4340
4341 static enum dwarf_form
4342 AT_string_form (dw_attr_ref a)
4343 {
4344   struct indirect_string_node *node;
4345   unsigned int len;
4346   char label[32];
4347
4348   gcc_assert (a && AT_class (a) == dw_val_class_str);
4349
4350   node = a->dw_attr_val.v.val_str;
4351   if (node->form)
4352     return node->form;
4353
4354   len = strlen (node->str) + 1;
4355
4356   /* If the string is shorter or equal to the size of the reference, it is
4357      always better to put it inline.  */
4358   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4359     return node->form = DW_FORM_string;
4360
4361   /* If we cannot expect the linker to merge strings in .debug_str
4362      section, only put it into .debug_str if it is worth even in this
4363      single module.  */
4364   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4365       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4366       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4367     return node->form = DW_FORM_string;
4368
4369   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4370   ++dw2_string_counter;
4371   node->label = xstrdup (label);
4372
4373   return node->form = DW_FORM_strp;
4374 }
4375
4376 /* Add a DIE reference attribute value to a DIE.  */
4377
4378 static inline void
4379 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4380 {
4381   dw_attr_node attr;
4382
4383 #ifdef ENABLE_CHECKING
4384   gcc_assert (targ_die != NULL);
4385 #else
4386   /* With LTO we can end up trying to reference something we didn't create
4387      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4388   if (targ_die == NULL)
4389     return;
4390 #endif
4391
4392   attr.dw_attr = attr_kind;
4393   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4394   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4395   attr.dw_attr_val.v.val_die_ref.external = 0;
4396   add_dwarf_attr (die, &attr);
4397 }
4398
4399 /* Add an AT_specification attribute to a DIE, and also make the back
4400    pointer from the specification to the definition.  */
4401
4402 static inline void
4403 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4404 {
4405   add_AT_die_ref (die, DW_AT_specification, targ_die);
4406   gcc_assert (!targ_die->die_definition);
4407   targ_die->die_definition = die;
4408 }
4409
4410 static inline dw_die_ref
4411 AT_ref (dw_attr_ref a)
4412 {
4413   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4414   return a->dw_attr_val.v.val_die_ref.die;
4415 }
4416
4417 static inline int
4418 AT_ref_external (dw_attr_ref a)
4419 {
4420   if (a && AT_class (a) == dw_val_class_die_ref)
4421     return a->dw_attr_val.v.val_die_ref.external;
4422
4423   return 0;
4424 }
4425
4426 static inline void
4427 set_AT_ref_external (dw_attr_ref a, int i)
4428 {
4429   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4430   a->dw_attr_val.v.val_die_ref.external = i;
4431 }
4432
4433 /* Add an FDE reference attribute value to a DIE.  */
4434
4435 static inline void
4436 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4437 {
4438   dw_attr_node attr;
4439
4440   attr.dw_attr = attr_kind;
4441   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4442   attr.dw_attr_val.v.val_fde_index = targ_fde;
4443   add_dwarf_attr (die, &attr);
4444 }
4445
4446 /* Add a location description attribute value to a DIE.  */
4447
4448 static inline void
4449 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4450 {
4451   dw_attr_node attr;
4452
4453   attr.dw_attr = attr_kind;
4454   attr.dw_attr_val.val_class = dw_val_class_loc;
4455   attr.dw_attr_val.v.val_loc = loc;
4456   add_dwarf_attr (die, &attr);
4457 }
4458
4459 static inline dw_loc_descr_ref
4460 AT_loc (dw_attr_ref a)
4461 {
4462   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4463   return a->dw_attr_val.v.val_loc;
4464 }
4465
4466 static inline void
4467 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4468 {
4469   dw_attr_node attr;
4470
4471   attr.dw_attr = attr_kind;
4472   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4473   attr.dw_attr_val.v.val_loc_list = loc_list;
4474   add_dwarf_attr (die, &attr);
4475   have_location_lists = true;
4476 }
4477
4478 static inline dw_loc_list_ref
4479 AT_loc_list (dw_attr_ref a)
4480 {
4481   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4482   return a->dw_attr_val.v.val_loc_list;
4483 }
4484
4485 static inline dw_loc_list_ref *
4486 AT_loc_list_ptr (dw_attr_ref a)
4487 {
4488   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4489   return &a->dw_attr_val.v.val_loc_list;
4490 }
4491
4492 /* Add an address constant attribute value to a DIE.  */
4493
4494 static inline void
4495 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4496 {
4497   dw_attr_node attr;
4498
4499   attr.dw_attr = attr_kind;
4500   attr.dw_attr_val.val_class = dw_val_class_addr;
4501   attr.dw_attr_val.v.val_addr = addr;
4502   add_dwarf_attr (die, &attr);
4503 }
4504
4505 /* Get the RTX from to an address DIE attribute.  */
4506
4507 static inline rtx
4508 AT_addr (dw_attr_ref a)
4509 {
4510   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4511   return a->dw_attr_val.v.val_addr;
4512 }
4513
4514 /* Add a file attribute value to a DIE.  */
4515
4516 static inline void
4517 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4518              struct dwarf_file_data *fd)
4519 {
4520   dw_attr_node attr;
4521
4522   attr.dw_attr = attr_kind;
4523   attr.dw_attr_val.val_class = dw_val_class_file;
4524   attr.dw_attr_val.v.val_file = fd;
4525   add_dwarf_attr (die, &attr);
4526 }
4527
4528 /* Get the dwarf_file_data from a file DIE attribute.  */
4529
4530 static inline struct dwarf_file_data *
4531 AT_file (dw_attr_ref a)
4532 {
4533   gcc_assert (a && AT_class (a) == dw_val_class_file);
4534   return a->dw_attr_val.v.val_file;
4535 }
4536
4537 /* Add a vms delta attribute value to a DIE.  */
4538
4539 static inline void
4540 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4541                   const char *lbl1, const char *lbl2)
4542 {
4543   dw_attr_node attr;
4544
4545   attr.dw_attr = attr_kind;
4546   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4547   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4548   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4549   add_dwarf_attr (die, &attr);
4550 }
4551
4552 /* Add a label identifier attribute value to a DIE.  */
4553
4554 static inline void
4555 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4556 {
4557   dw_attr_node attr;
4558
4559   attr.dw_attr = attr_kind;
4560   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4561   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4562   add_dwarf_attr (die, &attr);
4563 }
4564
4565 /* Add a section offset attribute value to a DIE, an offset into the
4566    debug_line section.  */
4567
4568 static inline void
4569 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4570                 const char *label)
4571 {
4572   dw_attr_node attr;
4573
4574   attr.dw_attr = attr_kind;
4575   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4576   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4577   add_dwarf_attr (die, &attr);
4578 }
4579
4580 /* Add a section offset attribute value to a DIE, an offset into the
4581    debug_macinfo section.  */
4582
4583 static inline void
4584 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4585                const char *label)
4586 {
4587   dw_attr_node attr;
4588
4589   attr.dw_attr = attr_kind;
4590   attr.dw_attr_val.val_class = dw_val_class_macptr;
4591   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4592   add_dwarf_attr (die, &attr);
4593 }
4594
4595 /* Add an offset attribute value to a DIE.  */
4596
4597 static inline void
4598 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4599                unsigned HOST_WIDE_INT offset)
4600 {
4601   dw_attr_node attr;
4602
4603   attr.dw_attr = attr_kind;
4604   attr.dw_attr_val.val_class = dw_val_class_offset;
4605   attr.dw_attr_val.v.val_offset = offset;
4606   add_dwarf_attr (die, &attr);
4607 }
4608
4609 /* Add an range_list attribute value to a DIE.  */
4610
4611 static void
4612 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4613                    long unsigned int offset)
4614 {
4615   dw_attr_node attr;
4616
4617   attr.dw_attr = attr_kind;
4618   attr.dw_attr_val.val_class = dw_val_class_range_list;
4619   attr.dw_attr_val.v.val_offset = offset;
4620   add_dwarf_attr (die, &attr);
4621 }
4622
4623 /* Return the start label of a delta attribute.  */
4624
4625 static inline const char *
4626 AT_vms_delta1 (dw_attr_ref a)
4627 {
4628   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4629   return a->dw_attr_val.v.val_vms_delta.lbl1;
4630 }
4631
4632 /* Return the end label of a delta attribute.  */
4633
4634 static inline const char *
4635 AT_vms_delta2 (dw_attr_ref a)
4636 {
4637   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4638   return a->dw_attr_val.v.val_vms_delta.lbl2;
4639 }
4640
4641 static inline const char *
4642 AT_lbl (dw_attr_ref a)
4643 {
4644   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4645                     || AT_class (a) == dw_val_class_lineptr
4646                     || AT_class (a) == dw_val_class_macptr));
4647   return a->dw_attr_val.v.val_lbl_id;
4648 }
4649
4650 /* Get the attribute of type attr_kind.  */
4651
4652 static dw_attr_ref
4653 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4654 {
4655   dw_attr_ref a;
4656   unsigned ix;
4657   dw_die_ref spec = NULL;
4658
4659   if (! die)
4660     return NULL;
4661
4662   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4663     if (a->dw_attr == attr_kind)
4664       return a;
4665     else if (a->dw_attr == DW_AT_specification
4666              || a->dw_attr == DW_AT_abstract_origin)
4667       spec = AT_ref (a);
4668
4669   if (spec)
4670     return get_AT (spec, attr_kind);
4671
4672   return NULL;
4673 }
4674
4675 /* Return the "low pc" attribute value, typically associated with a subprogram
4676    DIE.  Return null if the "low pc" attribute is either not present, or if it
4677    cannot be represented as an assembler label identifier.  */
4678
4679 static inline const char *
4680 get_AT_low_pc (dw_die_ref die)
4681 {
4682   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4683
4684   return a ? AT_lbl (a) : NULL;
4685 }
4686
4687 /* Return the "high pc" attribute value, typically associated with a subprogram
4688    DIE.  Return null if the "high pc" attribute is either not present, or if it
4689    cannot be represented as an assembler label identifier.  */
4690
4691 static inline const char *
4692 get_AT_hi_pc (dw_die_ref die)
4693 {
4694   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4695
4696   return a ? AT_lbl (a) : NULL;
4697 }
4698
4699 /* Return the value of the string attribute designated by ATTR_KIND, or
4700    NULL if it is not present.  */
4701
4702 static inline const char *
4703 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4704 {
4705   dw_attr_ref a = get_AT (die, attr_kind);
4706
4707   return a ? AT_string (a) : NULL;
4708 }
4709
4710 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4711    if it is not present.  */
4712
4713 static inline int
4714 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4715 {
4716   dw_attr_ref a = get_AT (die, attr_kind);
4717
4718   return a ? AT_flag (a) : 0;
4719 }
4720
4721 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4722    if it is not present.  */
4723
4724 static inline unsigned
4725 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4726 {
4727   dw_attr_ref a = get_AT (die, attr_kind);
4728
4729   return a ? AT_unsigned (a) : 0;
4730 }
4731
4732 static inline dw_die_ref
4733 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
4734 {
4735   dw_attr_ref a = get_AT (die, attr_kind);
4736
4737   return a ? AT_ref (a) : NULL;
4738 }
4739
4740 static inline struct dwarf_file_data *
4741 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
4742 {
4743   dw_attr_ref a = get_AT (die, attr_kind);
4744
4745   return a ? AT_file (a) : NULL;
4746 }
4747
4748 /* Return TRUE if the language is C++.  */
4749
4750 static inline bool
4751 is_cxx (void)
4752 {
4753   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4754
4755   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4756 }
4757
4758 /* Return TRUE if the language is Fortran.  */
4759
4760 static inline bool
4761 is_fortran (void)
4762 {
4763   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4764
4765   return (lang == DW_LANG_Fortran77
4766           || lang == DW_LANG_Fortran90
4767           || lang == DW_LANG_Fortran95);
4768 }
4769
4770 /* Return TRUE if the language is Ada.  */
4771
4772 static inline bool
4773 is_ada (void)
4774 {
4775   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4776
4777   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4778 }
4779
4780 /* Remove the specified attribute if present.  */
4781
4782 static void
4783 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4784 {
4785   dw_attr_ref a;
4786   unsigned ix;
4787
4788   if (! die)
4789     return;
4790
4791   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4792     if (a->dw_attr == attr_kind)
4793       {
4794         if (AT_class (a) == dw_val_class_str)
4795           if (a->dw_attr_val.v.val_str->refcount)
4796             a->dw_attr_val.v.val_str->refcount--;
4797
4798         /* VEC_ordered_remove should help reduce the number of abbrevs
4799            that are needed.  */
4800         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4801         return;
4802       }
4803 }
4804
4805 /* Remove CHILD from its parent.  PREV must have the property that
4806    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4807
4808 static void
4809 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4810 {
4811   gcc_assert (child->die_parent == prev->die_parent);
4812   gcc_assert (prev->die_sib == child);
4813   if (prev == child)
4814     {
4815       gcc_assert (child->die_parent->die_child == child);
4816       prev = NULL;
4817     }
4818   else
4819     prev->die_sib = child->die_sib;
4820   if (child->die_parent->die_child == child)
4821     child->die_parent->die_child = prev;
4822 }
4823
4824 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4825    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4826
4827 static void
4828 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4829 {
4830   dw_die_ref parent = old_child->die_parent;
4831
4832   gcc_assert (parent == prev->die_parent);
4833   gcc_assert (prev->die_sib == old_child);
4834
4835   new_child->die_parent = parent;
4836   if (prev == old_child)
4837     {
4838       gcc_assert (parent->die_child == old_child);
4839       new_child->die_sib = new_child;
4840     }
4841   else
4842     {
4843       prev->die_sib = new_child;
4844       new_child->die_sib = old_child->die_sib;
4845     }
4846   if (old_child->die_parent->die_child == old_child)
4847     old_child->die_parent->die_child = new_child;
4848 }
4849
4850 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4851
4852 static void
4853 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4854 {
4855   dw_die_ref c;
4856   new_parent->die_child = old_parent->die_child;
4857   old_parent->die_child = NULL;
4858   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4859 }
4860
4861 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4862    matches TAG.  */
4863
4864 static void
4865 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4866 {
4867   dw_die_ref c;
4868
4869   c = die->die_child;
4870   if (c) do {
4871     dw_die_ref prev = c;
4872     c = c->die_sib;
4873     while (c->die_tag == tag)
4874       {
4875         remove_child_with_prev (c, prev);
4876         /* Might have removed every child.  */
4877         if (c == c->die_sib)
4878           return;
4879         c = c->die_sib;
4880       }
4881   } while (c != die->die_child);
4882 }
4883
4884 /* Add a CHILD_DIE as the last child of DIE.  */
4885
4886 static void
4887 add_child_die (dw_die_ref die, dw_die_ref child_die)
4888 {
4889   /* FIXME this should probably be an assert.  */
4890   if (! die || ! child_die)
4891     return;
4892   gcc_assert (die != child_die);
4893
4894   child_die->die_parent = die;
4895   if (die->die_child)
4896     {
4897       child_die->die_sib = die->die_child->die_sib;
4898       die->die_child->die_sib = child_die;
4899     }
4900   else
4901     child_die->die_sib = child_die;
4902   die->die_child = child_die;
4903 }
4904
4905 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4906    is the specification, to the end of PARENT's list of children.
4907    This is done by removing and re-adding it.  */
4908
4909 static void
4910 splice_child_die (dw_die_ref parent, dw_die_ref child)
4911 {
4912   dw_die_ref p;
4913
4914   /* We want the declaration DIE from inside the class, not the
4915      specification DIE at toplevel.  */
4916   if (child->die_parent != parent)
4917     {
4918       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4919
4920       if (tmp)
4921         child = tmp;
4922     }
4923
4924   gcc_assert (child->die_parent == parent
4925               || (child->die_parent
4926                   == get_AT_ref (parent, DW_AT_specification)));
4927
4928   for (p = child->die_parent->die_child; ; p = p->die_sib)
4929     if (p->die_sib == child)
4930       {
4931         remove_child_with_prev (child, p);
4932         break;
4933       }
4934
4935   add_child_die (parent, child);
4936 }
4937
4938 /* Return a pointer to a newly created DIE node.  */
4939
4940 static inline dw_die_ref
4941 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4942 {
4943   dw_die_ref die = ggc_alloc_cleared_die_node ();
4944
4945   die->die_tag = tag_value;
4946
4947   if (parent_die != NULL)
4948     add_child_die (parent_die, die);
4949   else
4950     {
4951       limbo_die_node *limbo_node;
4952
4953       limbo_node = ggc_alloc_cleared_limbo_die_node ();
4954       limbo_node->die = die;
4955       limbo_node->created_for = t;
4956       limbo_node->next = limbo_die_list;
4957       limbo_die_list = limbo_node;
4958     }
4959
4960   return die;
4961 }
4962
4963 /* Return the DIE associated with the given type specifier.  */
4964
4965 static inline dw_die_ref
4966 lookup_type_die (tree type)
4967 {
4968   return TYPE_SYMTAB_DIE (type);
4969 }
4970
4971 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4972    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4973    anonymous type instead the one of the naming typedef.  */
4974
4975 static inline dw_die_ref
4976 strip_naming_typedef (tree type, dw_die_ref type_die)
4977 {
4978   if (type
4979       && TREE_CODE (type) == RECORD_TYPE
4980       && type_die
4981       && type_die->die_tag == DW_TAG_typedef
4982       && is_naming_typedef_decl (TYPE_NAME (type)))
4983     type_die = get_AT_ref (type_die, DW_AT_type);
4984   return type_die;
4985 }
4986
4987 /* Like lookup_type_die, but if type is an anonymous type named by a
4988    typedef[1], return the DIE of the anonymous type instead the one of
4989    the naming typedef.  This is because in gen_typedef_die, we did
4990    equate the anonymous struct named by the typedef with the DIE of
4991    the naming typedef. So by default, lookup_type_die on an anonymous
4992    struct yields the DIE of the naming typedef.
4993
4994    [1]: Read the comment of is_naming_typedef_decl to learn about what
4995    a naming typedef is.  */
4996
4997 static inline dw_die_ref
4998 lookup_type_die_strip_naming_typedef (tree type)
4999 {
5000   dw_die_ref die = lookup_type_die (type);
5001   return strip_naming_typedef (type, die);
5002 }
5003
5004 /* Equate a DIE to a given type specifier.  */
5005
5006 static inline void
5007 equate_type_number_to_die (tree type, dw_die_ref type_die)
5008 {
5009   TYPE_SYMTAB_DIE (type) = type_die;
5010 }
5011
5012 /* Returns a hash value for X (which really is a die_struct).  */
5013
5014 static hashval_t
5015 decl_die_table_hash (const void *x)
5016 {
5017   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5018 }
5019
5020 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5021
5022 static int
5023 decl_die_table_eq (const void *x, const void *y)
5024 {
5025   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5026 }
5027
5028 /* Return the DIE associated with a given declaration.  */
5029
5030 static inline dw_die_ref
5031 lookup_decl_die (tree decl)
5032 {
5033   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5034 }
5035
5036 /* Returns a hash value for X (which really is a var_loc_list).  */
5037
5038 static hashval_t
5039 decl_loc_table_hash (const void *x)
5040 {
5041   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5042 }
5043
5044 /* Return nonzero if decl_id of var_loc_list X is the same as
5045    UID of decl *Y.  */
5046
5047 static int
5048 decl_loc_table_eq (const void *x, const void *y)
5049 {
5050   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5051 }
5052
5053 /* Return the var_loc list associated with a given declaration.  */
5054
5055 static inline var_loc_list *
5056 lookup_decl_loc (const_tree decl)
5057 {
5058   if (!decl_loc_table)
5059     return NULL;
5060   return (var_loc_list *)
5061     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5062 }
5063
5064 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5065
5066 static hashval_t
5067 cached_dw_loc_list_table_hash (const void *x)
5068 {
5069   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5070 }
5071
5072 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5073    UID of decl *Y.  */
5074
5075 static int
5076 cached_dw_loc_list_table_eq (const void *x, const void *y)
5077 {
5078   return (((const cached_dw_loc_list *) x)->decl_id
5079           == DECL_UID ((const_tree) y));
5080 }
5081
5082 /* Equate a DIE to a particular declaration.  */
5083
5084 static void
5085 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5086 {
5087   unsigned int decl_id = DECL_UID (decl);
5088   void **slot;
5089
5090   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5091   *slot = decl_die;
5092   decl_die->decl_id = decl_id;
5093 }
5094
5095 /* Return how many bits covers PIECE EXPR_LIST.  */
5096
5097 static int
5098 decl_piece_bitsize (rtx piece)
5099 {
5100   int ret = (int) GET_MODE (piece);
5101   if (ret)
5102     return ret;
5103   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5104               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5105   return INTVAL (XEXP (XEXP (piece, 0), 0));
5106 }
5107
5108 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5109
5110 static rtx *
5111 decl_piece_varloc_ptr (rtx piece)
5112 {
5113   if ((int) GET_MODE (piece))
5114     return &XEXP (piece, 0);
5115   else
5116     return &XEXP (XEXP (piece, 0), 1);
5117 }
5118
5119 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5120    Next is the chain of following piece nodes.  */
5121
5122 static rtx
5123 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5124 {
5125   if (bitsize <= (int) MAX_MACHINE_MODE)
5126     return alloc_EXPR_LIST (bitsize, loc_note, next);
5127   else
5128     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5129                                                GEN_INT (bitsize),
5130                                                loc_note), next);
5131 }
5132
5133 /* Return rtx that should be stored into loc field for
5134    LOC_NOTE and BITPOS/BITSIZE.  */
5135
5136 static rtx
5137 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5138                       HOST_WIDE_INT bitsize)
5139 {
5140   if (bitsize != -1)
5141     {
5142       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5143       if (bitpos != 0)
5144         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5145     }
5146   return loc_note;
5147 }
5148
5149 /* This function either modifies location piece list *DEST in
5150    place (if SRC and INNER is NULL), or copies location piece list
5151    *SRC to *DEST while modifying it.  Location BITPOS is modified
5152    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5153    not copied and if needed some padding around it is added.
5154    When modifying in place, DEST should point to EXPR_LIST where
5155    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5156    to the start of the whole list and INNER points to the EXPR_LIST
5157    where earlier pieces cover PIECE_BITPOS bits.  */
5158
5159 static void
5160 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5161                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5162                    HOST_WIDE_INT bitsize, rtx loc_note)
5163 {
5164   int diff;
5165   bool copy = inner != NULL;
5166
5167   if (copy)
5168     {
5169       /* First copy all nodes preceeding the current bitpos.  */
5170       while (src != inner)
5171         {
5172           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5173                                    decl_piece_bitsize (*src), NULL_RTX);
5174           dest = &XEXP (*dest, 1);
5175           src = &XEXP (*src, 1);
5176         }
5177     }
5178   /* Add padding if needed.  */
5179   if (bitpos != piece_bitpos)
5180     {
5181       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5182                                copy ? NULL_RTX : *dest);
5183       dest = &XEXP (*dest, 1);
5184     }
5185   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5186     {
5187       gcc_assert (!copy);
5188       /* A piece with correct bitpos and bitsize already exist,
5189          just update the location for it and return.  */
5190       *decl_piece_varloc_ptr (*dest) = loc_note;
5191       return;
5192     }
5193   /* Add the piece that changed.  */
5194   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5195   dest = &XEXP (*dest, 1);
5196   /* Skip over pieces that overlap it.  */
5197   diff = bitpos - piece_bitpos + bitsize;
5198   if (!copy)
5199     src = dest;
5200   while (diff > 0 && *src)
5201     {
5202       rtx piece = *src;
5203       diff -= decl_piece_bitsize (piece);
5204       if (copy)
5205         src = &XEXP (piece, 1);
5206       else
5207         {
5208           *src = XEXP (piece, 1);
5209           free_EXPR_LIST_node (piece);
5210         }
5211     }
5212   /* Add padding if needed.  */
5213   if (diff < 0 && *src)
5214     {
5215       if (!copy)
5216         dest = src;
5217       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5218       dest = &XEXP (*dest, 1);
5219     }
5220   if (!copy)
5221     return;
5222   /* Finally copy all nodes following it.  */
5223   while (*src)
5224     {
5225       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5226                                decl_piece_bitsize (*src), NULL_RTX);
5227       dest = &XEXP (*dest, 1);
5228       src = &XEXP (*src, 1);
5229     }
5230 }
5231
5232 /* Add a variable location node to the linked list for DECL.  */
5233
5234 static struct var_loc_node *
5235 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5236 {
5237   unsigned int decl_id;
5238   var_loc_list *temp;
5239   void **slot;
5240   struct var_loc_node *loc = NULL;
5241   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5242
5243   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5244     {
5245       tree realdecl = DECL_DEBUG_EXPR (decl);
5246       if (realdecl && handled_component_p (realdecl))
5247         {
5248           HOST_WIDE_INT maxsize;
5249           tree innerdecl;
5250           innerdecl
5251             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5252           if (!DECL_P (innerdecl)
5253               || DECL_IGNORED_P (innerdecl)
5254               || TREE_STATIC (innerdecl)
5255               || bitsize <= 0
5256               || bitpos + bitsize > 256
5257               || bitsize != maxsize)
5258             return NULL;
5259           decl = innerdecl;
5260         }
5261     }
5262
5263   decl_id = DECL_UID (decl);
5264   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5265   if (*slot == NULL)
5266     {
5267       temp = ggc_alloc_cleared_var_loc_list ();
5268       temp->decl_id = decl_id;
5269       *slot = temp;
5270     }
5271   else
5272     temp = (var_loc_list *) *slot;
5273
5274   /* For PARM_DECLs try to keep around the original incoming value,
5275      even if that means we'll emit a zero-range .debug_loc entry.  */
5276   if (temp->last
5277       && temp->first == temp->last
5278       && TREE_CODE (decl) == PARM_DECL
5279       && GET_CODE (temp->first->loc) == NOTE
5280       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5281       && DECL_INCOMING_RTL (decl)
5282       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5283       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5284          == GET_CODE (DECL_INCOMING_RTL (decl))
5285       && prev_real_insn (temp->first->loc) == NULL_RTX
5286       && (bitsize != -1
5287           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5288                            NOTE_VAR_LOCATION_LOC (loc_note))
5289           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5290               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5291     {
5292       loc = ggc_alloc_cleared_var_loc_node ();
5293       temp->first->next = loc;
5294       temp->last = loc;
5295       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5296     }
5297   else if (temp->last)
5298     {
5299       struct var_loc_node *last = temp->last, *unused = NULL;
5300       rtx *piece_loc = NULL, last_loc_note;
5301       int piece_bitpos = 0;
5302       if (last->next)
5303         {
5304           last = last->next;
5305           gcc_assert (last->next == NULL);
5306         }
5307       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5308         {
5309           piece_loc = &last->loc;
5310           do
5311             {
5312               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5313               if (piece_bitpos + cur_bitsize > bitpos)
5314                 break;
5315               piece_bitpos += cur_bitsize;
5316               piece_loc = &XEXP (*piece_loc, 1);
5317             }
5318           while (*piece_loc);
5319         }
5320       /* TEMP->LAST here is either pointer to the last but one or
5321          last element in the chained list, LAST is pointer to the
5322          last element.  */
5323       if (label && strcmp (last->label, label) == 0)
5324         {
5325           /* For SRA optimized variables if there weren't any real
5326              insns since last note, just modify the last node.  */
5327           if (piece_loc != NULL)
5328             {
5329               adjust_piece_list (piece_loc, NULL, NULL,
5330                                  bitpos, piece_bitpos, bitsize, loc_note);
5331               return NULL;
5332             }
5333           /* If the last note doesn't cover any instructions, remove it.  */
5334           if (temp->last != last)
5335             {
5336               temp->last->next = NULL;
5337               unused = last;
5338               last = temp->last;
5339               gcc_assert (strcmp (last->label, label) != 0);
5340             }
5341           else
5342             {
5343               gcc_assert (temp->first == temp->last
5344                           || (temp->first->next == temp->last
5345                               && TREE_CODE (decl) == PARM_DECL));
5346               memset (temp->last, '\0', sizeof (*temp->last));
5347               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5348               return temp->last;
5349             }
5350         }
5351       if (bitsize == -1 && NOTE_P (last->loc))
5352         last_loc_note = last->loc;
5353       else if (piece_loc != NULL
5354                && *piece_loc != NULL_RTX
5355                && piece_bitpos == bitpos
5356                && decl_piece_bitsize (*piece_loc) == bitsize)
5357         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5358       else
5359         last_loc_note = NULL_RTX;
5360       /* If the current location is the same as the end of the list,
5361          and either both or neither of the locations is uninitialized,
5362          we have nothing to do.  */
5363       if (last_loc_note == NULL_RTX
5364           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5365                             NOTE_VAR_LOCATION_LOC (loc_note)))
5366           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5367                != NOTE_VAR_LOCATION_STATUS (loc_note))
5368               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5369                    == VAR_INIT_STATUS_UNINITIALIZED)
5370                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5371                       == VAR_INIT_STATUS_UNINITIALIZED))))
5372         {
5373           /* Add LOC to the end of list and update LAST.  If the last
5374              element of the list has been removed above, reuse its
5375              memory for the new node, otherwise allocate a new one.  */
5376           if (unused)
5377             {
5378               loc = unused;
5379               memset (loc, '\0', sizeof (*loc));
5380             }
5381           else
5382             loc = ggc_alloc_cleared_var_loc_node ();
5383           if (bitsize == -1 || piece_loc == NULL)
5384             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5385           else
5386             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5387                                bitpos, piece_bitpos, bitsize, loc_note);
5388           last->next = loc;
5389           /* Ensure TEMP->LAST will point either to the new last but one
5390              element of the chain, or to the last element in it.  */
5391           if (last != temp->last)
5392             temp->last = last;
5393         }
5394       else if (unused)
5395         ggc_free (unused);
5396     }
5397   else
5398     {
5399       loc = ggc_alloc_cleared_var_loc_node ();
5400       temp->first = loc;
5401       temp->last = loc;
5402       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5403     }
5404   return loc;
5405 }
5406 \f
5407 /* Keep track of the number of spaces used to indent the
5408    output of the debugging routines that print the structure of
5409    the DIE internal representation.  */
5410 static int print_indent;
5411
5412 /* Indent the line the number of spaces given by print_indent.  */
5413
5414 static inline void
5415 print_spaces (FILE *outfile)
5416 {
5417   fprintf (outfile, "%*s", print_indent, "");
5418 }
5419
5420 /* Print a type signature in hex.  */
5421
5422 static inline void
5423 print_signature (FILE *outfile, char *sig)
5424 {
5425   int i;
5426
5427   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5428     fprintf (outfile, "%02x", sig[i] & 0xff);
5429 }
5430
5431 /* Print the information associated with a given DIE, and its children.
5432    This routine is a debugging aid only.  */
5433
5434 static void
5435 print_die (dw_die_ref die, FILE *outfile)
5436 {
5437   dw_attr_ref a;
5438   dw_die_ref c;
5439   unsigned ix;
5440
5441   print_spaces (outfile);
5442   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5443            die->die_offset, dwarf_tag_name (die->die_tag),
5444            (void*) die);
5445   print_spaces (outfile);
5446   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5447   fprintf (outfile, " offset: %ld", die->die_offset);
5448   fprintf (outfile, " mark: %d\n", die->die_mark);
5449
5450   if (use_debug_types && die->die_id.die_type_node)
5451     {
5452       print_spaces (outfile);
5453       fprintf (outfile, "  signature: ");
5454       print_signature (outfile, die->die_id.die_type_node->signature);
5455       fprintf (outfile, "\n");
5456     }
5457
5458   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5459     {
5460       print_spaces (outfile);
5461       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5462
5463       switch (AT_class (a))
5464         {
5465         case dw_val_class_addr:
5466           fprintf (outfile, "address");
5467           break;
5468         case dw_val_class_offset:
5469           fprintf (outfile, "offset");
5470           break;
5471         case dw_val_class_loc:
5472           fprintf (outfile, "location descriptor");
5473           break;
5474         case dw_val_class_loc_list:
5475           fprintf (outfile, "location list -> label:%s",
5476                    AT_loc_list (a)->ll_symbol);
5477           break;
5478         case dw_val_class_range_list:
5479           fprintf (outfile, "range list");
5480           break;
5481         case dw_val_class_const:
5482           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5483           break;
5484         case dw_val_class_unsigned_const:
5485           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5486           break;
5487         case dw_val_class_const_double:
5488           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5489                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5490                    a->dw_attr_val.v.val_double.high,
5491                    a->dw_attr_val.v.val_double.low);
5492           break;
5493         case dw_val_class_vec:
5494           fprintf (outfile, "floating-point or vector constant");
5495           break;
5496         case dw_val_class_flag:
5497           fprintf (outfile, "%u", AT_flag (a));
5498           break;
5499         case dw_val_class_die_ref:
5500           if (AT_ref (a) != NULL)
5501             {
5502               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5503                 {
5504                   fprintf (outfile, "die -> signature: ");
5505                   print_signature (outfile,
5506                                    AT_ref (a)->die_id.die_type_node->signature);
5507                 }
5508               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5509                 fprintf (outfile, "die -> label: %s",
5510                          AT_ref (a)->die_id.die_symbol);
5511               else
5512                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5513               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5514             }
5515           else
5516             fprintf (outfile, "die -> <null>");
5517           break;
5518         case dw_val_class_vms_delta:
5519           fprintf (outfile, "delta: @slotcount(%s-%s)",
5520                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5521           break;
5522         case dw_val_class_lbl_id:
5523         case dw_val_class_lineptr:
5524         case dw_val_class_macptr:
5525           fprintf (outfile, "label: %s", AT_lbl (a));
5526           break;
5527         case dw_val_class_str:
5528           if (AT_string (a) != NULL)
5529             fprintf (outfile, "\"%s\"", AT_string (a));
5530           else
5531             fprintf (outfile, "<null>");
5532           break;
5533         case dw_val_class_file:
5534           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5535                    AT_file (a)->emitted_number);
5536           break;
5537         case dw_val_class_data8:
5538           {
5539             int i;
5540
5541             for (i = 0; i < 8; i++)
5542               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5543             break;
5544           }
5545         default:
5546           break;
5547         }
5548
5549       fprintf (outfile, "\n");
5550     }
5551
5552   if (die->die_child != NULL)
5553     {
5554       print_indent += 4;
5555       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5556       print_indent -= 4;
5557     }
5558   if (print_indent == 0)
5559     fprintf (outfile, "\n");
5560 }
5561
5562 /* Print the information collected for a given DIE.  */
5563
5564 DEBUG_FUNCTION void
5565 debug_dwarf_die (dw_die_ref die)
5566 {
5567   print_die (die, stderr);
5568 }
5569
5570 /* Print all DWARF information collected for the compilation unit.
5571    This routine is a debugging aid only.  */
5572
5573 DEBUG_FUNCTION void
5574 debug_dwarf (void)
5575 {
5576   print_indent = 0;
5577   print_die (comp_unit_die (), stderr);
5578 }
5579 \f
5580 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5581    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5582    DIE that marks the start of the DIEs for this include file.  */
5583
5584 static dw_die_ref
5585 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5586 {
5587   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5588   dw_die_ref new_unit = gen_compile_unit_die (filename);
5589
5590   new_unit->die_sib = old_unit;
5591   return new_unit;
5592 }
5593
5594 /* Close an include-file CU and reopen the enclosing one.  */
5595
5596 static dw_die_ref
5597 pop_compile_unit (dw_die_ref old_unit)
5598 {
5599   dw_die_ref new_unit = old_unit->die_sib;
5600
5601   old_unit->die_sib = NULL;
5602   return new_unit;
5603 }
5604
5605 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5606 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5607
5608 /* Calculate the checksum of a location expression.  */
5609
5610 static inline void
5611 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5612 {
5613   int tem;
5614
5615   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5616   CHECKSUM (tem);
5617   CHECKSUM (loc->dw_loc_oprnd1);
5618   CHECKSUM (loc->dw_loc_oprnd2);
5619 }
5620
5621 /* Calculate the checksum of an attribute.  */
5622
5623 static void
5624 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5625 {
5626   dw_loc_descr_ref loc;
5627   rtx r;
5628
5629   CHECKSUM (at->dw_attr);
5630
5631   /* We don't care that this was compiled with a different compiler
5632      snapshot; if the output is the same, that's what matters.  */
5633   if (at->dw_attr == DW_AT_producer)
5634     return;
5635
5636   switch (AT_class (at))
5637     {
5638     case dw_val_class_const:
5639       CHECKSUM (at->dw_attr_val.v.val_int);
5640       break;
5641     case dw_val_class_unsigned_const:
5642       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5643       break;
5644     case dw_val_class_const_double:
5645       CHECKSUM (at->dw_attr_val.v.val_double);
5646       break;
5647     case dw_val_class_vec:
5648       CHECKSUM (at->dw_attr_val.v.val_vec);
5649       break;
5650     case dw_val_class_flag:
5651       CHECKSUM (at->dw_attr_val.v.val_flag);
5652       break;
5653     case dw_val_class_str:
5654       CHECKSUM_STRING (AT_string (at));
5655       break;
5656
5657     case dw_val_class_addr:
5658       r = AT_addr (at);
5659       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5660       CHECKSUM_STRING (XSTR (r, 0));
5661       break;
5662
5663     case dw_val_class_offset:
5664       CHECKSUM (at->dw_attr_val.v.val_offset);
5665       break;
5666
5667     case dw_val_class_loc:
5668       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5669         loc_checksum (loc, ctx);
5670       break;
5671
5672     case dw_val_class_die_ref:
5673       die_checksum (AT_ref (at), ctx, mark);
5674       break;
5675
5676     case dw_val_class_fde_ref:
5677     case dw_val_class_vms_delta:
5678     case dw_val_class_lbl_id:
5679     case dw_val_class_lineptr:
5680     case dw_val_class_macptr:
5681       break;
5682
5683     case dw_val_class_file:
5684       CHECKSUM_STRING (AT_file (at)->filename);
5685       break;
5686
5687     case dw_val_class_data8:
5688       CHECKSUM (at->dw_attr_val.v.val_data8);
5689       break;
5690
5691     default:
5692       break;
5693     }
5694 }
5695
5696 /* Calculate the checksum of a DIE.  */
5697
5698 static void
5699 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5700 {
5701   dw_die_ref c;
5702   dw_attr_ref a;
5703   unsigned ix;
5704
5705   /* To avoid infinite recursion.  */
5706   if (die->die_mark)
5707     {
5708       CHECKSUM (die->die_mark);
5709       return;
5710     }
5711   die->die_mark = ++(*mark);
5712
5713   CHECKSUM (die->die_tag);
5714
5715   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5716     attr_checksum (a, ctx, mark);
5717
5718   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5719 }
5720
5721 #undef CHECKSUM
5722 #undef CHECKSUM_STRING
5723
5724 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5725 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5726 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5727 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5728 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5729 #define CHECKSUM_ATTR(FOO) \
5730   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5731
5732 /* Calculate the checksum of a number in signed LEB128 format.  */
5733
5734 static void
5735 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5736 {
5737   unsigned char byte;
5738   bool more;
5739
5740   while (1)
5741     {
5742       byte = (value & 0x7f);
5743       value >>= 7;
5744       more = !((value == 0 && (byte & 0x40) == 0)
5745                 || (value == -1 && (byte & 0x40) != 0));
5746       if (more)
5747         byte |= 0x80;
5748       CHECKSUM (byte);
5749       if (!more)
5750         break;
5751     }
5752 }
5753
5754 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5755
5756 static void
5757 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5758 {
5759   while (1)
5760     {
5761       unsigned char byte = (value & 0x7f);
5762       value >>= 7;
5763       if (value != 0)
5764         /* More bytes to follow.  */
5765         byte |= 0x80;
5766       CHECKSUM (byte);
5767       if (value == 0)
5768         break;
5769     }
5770 }
5771
5772 /* Checksum the context of the DIE.  This adds the names of any
5773    surrounding namespaces or structures to the checksum.  */
5774
5775 static void
5776 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5777 {
5778   const char *name;
5779   dw_die_ref spec;
5780   int tag = die->die_tag;
5781
5782   if (tag != DW_TAG_namespace
5783       && tag != DW_TAG_structure_type
5784       && tag != DW_TAG_class_type)
5785     return;
5786
5787   name = get_AT_string (die, DW_AT_name);
5788
5789   spec = get_AT_ref (die, DW_AT_specification);
5790   if (spec != NULL)
5791     die = spec;
5792
5793   if (die->die_parent != NULL)
5794     checksum_die_context (die->die_parent, ctx);
5795
5796   CHECKSUM_ULEB128 ('C');
5797   CHECKSUM_ULEB128 (tag);
5798   if (name != NULL)
5799     CHECKSUM_STRING (name);
5800 }
5801
5802 /* Calculate the checksum of a location expression.  */
5803
5804 static inline void
5805 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5806 {
5807   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5808      were emitted as a DW_FORM_sdata instead of a location expression.  */
5809   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5810     {
5811       CHECKSUM_ULEB128 (DW_FORM_sdata);
5812       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5813       return;
5814     }
5815
5816   /* Otherwise, just checksum the raw location expression.  */
5817   while (loc != NULL)
5818     {
5819       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5820       CHECKSUM (loc->dw_loc_oprnd1);
5821       CHECKSUM (loc->dw_loc_oprnd2);
5822       loc = loc->dw_loc_next;
5823     }
5824 }
5825
5826 /* Calculate the checksum of an attribute.  */
5827
5828 static void
5829 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5830                        struct md5_ctx *ctx, int *mark)
5831 {
5832   dw_loc_descr_ref loc;
5833   rtx r;
5834
5835   if (AT_class (at) == dw_val_class_die_ref)
5836     {
5837       dw_die_ref target_die = AT_ref (at);
5838
5839       /* For pointer and reference types, we checksum only the (qualified)
5840          name of the target type (if there is a name).  For friend entries,
5841          we checksum only the (qualified) name of the target type or function.
5842          This allows the checksum to remain the same whether the target type
5843          is complete or not.  */
5844       if ((at->dw_attr == DW_AT_type
5845            && (tag == DW_TAG_pointer_type
5846                || tag == DW_TAG_reference_type
5847                || tag == DW_TAG_rvalue_reference_type
5848                || tag == DW_TAG_ptr_to_member_type))
5849           || (at->dw_attr == DW_AT_friend
5850               && tag == DW_TAG_friend))
5851         {
5852           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5853
5854           if (name_attr != NULL)
5855             {
5856               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5857
5858               if (decl == NULL)
5859                 decl = target_die;
5860               CHECKSUM_ULEB128 ('N');
5861               CHECKSUM_ULEB128 (at->dw_attr);
5862               if (decl->die_parent != NULL)
5863                 checksum_die_context (decl->die_parent, ctx);
5864               CHECKSUM_ULEB128 ('E');
5865               CHECKSUM_STRING (AT_string (name_attr));
5866               return;
5867             }
5868         }
5869
5870       /* For all other references to another DIE, we check to see if the
5871          target DIE has already been visited.  If it has, we emit a
5872          backward reference; if not, we descend recursively.  */
5873       if (target_die->die_mark > 0)
5874         {
5875           CHECKSUM_ULEB128 ('R');
5876           CHECKSUM_ULEB128 (at->dw_attr);
5877           CHECKSUM_ULEB128 (target_die->die_mark);
5878         }
5879       else
5880         {
5881           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5882
5883           if (decl == NULL)
5884             decl = target_die;
5885           target_die->die_mark = ++(*mark);
5886           CHECKSUM_ULEB128 ('T');
5887           CHECKSUM_ULEB128 (at->dw_attr);
5888           if (decl->die_parent != NULL)
5889             checksum_die_context (decl->die_parent, ctx);
5890           die_checksum_ordered (target_die, ctx, mark);
5891         }
5892       return;
5893     }
5894
5895   CHECKSUM_ULEB128 ('A');
5896   CHECKSUM_ULEB128 (at->dw_attr);
5897
5898   switch (AT_class (at))
5899     {
5900     case dw_val_class_const:
5901       CHECKSUM_ULEB128 (DW_FORM_sdata);
5902       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5903       break;
5904
5905     case dw_val_class_unsigned_const:
5906       CHECKSUM_ULEB128 (DW_FORM_sdata);
5907       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5908       break;
5909
5910     case dw_val_class_const_double:
5911       CHECKSUM_ULEB128 (DW_FORM_block);
5912       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5913       CHECKSUM (at->dw_attr_val.v.val_double);
5914       break;
5915
5916     case dw_val_class_vec:
5917       CHECKSUM_ULEB128 (DW_FORM_block);
5918       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5919       CHECKSUM (at->dw_attr_val.v.val_vec);
5920       break;
5921
5922     case dw_val_class_flag:
5923       CHECKSUM_ULEB128 (DW_FORM_flag);
5924       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5925       break;
5926
5927     case dw_val_class_str:
5928       CHECKSUM_ULEB128 (DW_FORM_string);
5929       CHECKSUM_STRING (AT_string (at));
5930       break;
5931
5932     case dw_val_class_addr:
5933       r = AT_addr (at);
5934       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5935       CHECKSUM_ULEB128 (DW_FORM_string);
5936       CHECKSUM_STRING (XSTR (r, 0));
5937       break;
5938
5939     case dw_val_class_offset:
5940       CHECKSUM_ULEB128 (DW_FORM_sdata);
5941       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5942       break;
5943
5944     case dw_val_class_loc:
5945       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5946         loc_checksum_ordered (loc, ctx);
5947       break;
5948
5949     case dw_val_class_fde_ref:
5950     case dw_val_class_lbl_id:
5951     case dw_val_class_lineptr:
5952     case dw_val_class_macptr:
5953       break;
5954
5955     case dw_val_class_file:
5956       CHECKSUM_ULEB128 (DW_FORM_string);
5957       CHECKSUM_STRING (AT_file (at)->filename);
5958       break;
5959
5960     case dw_val_class_data8:
5961       CHECKSUM (at->dw_attr_val.v.val_data8);
5962       break;
5963
5964     default:
5965       break;
5966     }
5967 }
5968
5969 struct checksum_attributes
5970 {
5971   dw_attr_ref at_name;
5972   dw_attr_ref at_type;
5973   dw_attr_ref at_friend;
5974   dw_attr_ref at_accessibility;
5975   dw_attr_ref at_address_class;
5976   dw_attr_ref at_allocated;
5977   dw_attr_ref at_artificial;
5978   dw_attr_ref at_associated;
5979   dw_attr_ref at_binary_scale;
5980   dw_attr_ref at_bit_offset;
5981   dw_attr_ref at_bit_size;
5982   dw_attr_ref at_bit_stride;
5983   dw_attr_ref at_byte_size;
5984   dw_attr_ref at_byte_stride;
5985   dw_attr_ref at_const_value;
5986   dw_attr_ref at_containing_type;
5987   dw_attr_ref at_count;
5988   dw_attr_ref at_data_location;
5989   dw_attr_ref at_data_member_location;
5990   dw_attr_ref at_decimal_scale;
5991   dw_attr_ref at_decimal_sign;
5992   dw_attr_ref at_default_value;
5993   dw_attr_ref at_digit_count;
5994   dw_attr_ref at_discr;
5995   dw_attr_ref at_discr_list;
5996   dw_attr_ref at_discr_value;
5997   dw_attr_ref at_encoding;
5998   dw_attr_ref at_endianity;
5999   dw_attr_ref at_explicit;
6000   dw_attr_ref at_is_optional;
6001   dw_attr_ref at_location;
6002   dw_attr_ref at_lower_bound;
6003   dw_attr_ref at_mutable;
6004   dw_attr_ref at_ordering;
6005   dw_attr_ref at_picture_string;
6006   dw_attr_ref at_prototyped;
6007   dw_attr_ref at_small;
6008   dw_attr_ref at_segment;
6009   dw_attr_ref at_string_length;
6010   dw_attr_ref at_threads_scaled;
6011   dw_attr_ref at_upper_bound;
6012   dw_attr_ref at_use_location;
6013   dw_attr_ref at_use_UTF8;
6014   dw_attr_ref at_variable_parameter;
6015   dw_attr_ref at_virtuality;
6016   dw_attr_ref at_visibility;
6017   dw_attr_ref at_vtable_elem_location;
6018 };
6019
6020 /* Collect the attributes that we will want to use for the checksum.  */
6021
6022 static void
6023 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6024 {
6025   dw_attr_ref a;
6026   unsigned ix;
6027
6028   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6029     {
6030       switch (a->dw_attr)
6031         {
6032         case DW_AT_name:
6033           attrs->at_name = a;
6034           break;
6035         case DW_AT_type:
6036           attrs->at_type = a;
6037           break;
6038         case DW_AT_friend:
6039           attrs->at_friend = a;
6040           break;
6041         case DW_AT_accessibility:
6042           attrs->at_accessibility = a;
6043           break;
6044         case DW_AT_address_class:
6045           attrs->at_address_class = a;
6046           break;
6047         case DW_AT_allocated:
6048           attrs->at_allocated = a;
6049           break;
6050         case DW_AT_artificial:
6051           attrs->at_artificial = a;
6052           break;
6053         case DW_AT_associated:
6054           attrs->at_associated = a;
6055           break;
6056         case DW_AT_binary_scale:
6057           attrs->at_binary_scale = a;
6058           break;
6059         case DW_AT_bit_offset:
6060           attrs->at_bit_offset = a;
6061           break;
6062         case DW_AT_bit_size:
6063           attrs->at_bit_size = a;
6064           break;
6065         case DW_AT_bit_stride:
6066           attrs->at_bit_stride = a;
6067           break;
6068         case DW_AT_byte_size:
6069           attrs->at_byte_size = a;
6070           break;
6071         case DW_AT_byte_stride:
6072           attrs->at_byte_stride = a;
6073           break;
6074         case DW_AT_const_value:
6075           attrs->at_const_value = a;
6076           break;
6077         case DW_AT_containing_type:
6078           attrs->at_containing_type = a;
6079           break;
6080         case DW_AT_count:
6081           attrs->at_count = a;
6082           break;
6083         case DW_AT_data_location:
6084           attrs->at_data_location = a;
6085           break;
6086         case DW_AT_data_member_location:
6087           attrs->at_data_member_location = a;
6088           break;
6089         case DW_AT_decimal_scale:
6090           attrs->at_decimal_scale = a;
6091           break;
6092         case DW_AT_decimal_sign:
6093           attrs->at_decimal_sign = a;
6094           break;
6095         case DW_AT_default_value:
6096           attrs->at_default_value = a;
6097           break;
6098         case DW_AT_digit_count:
6099           attrs->at_digit_count = a;
6100           break;
6101         case DW_AT_discr:
6102           attrs->at_discr = a;
6103           break;
6104         case DW_AT_discr_list:
6105           attrs->at_discr_list = a;
6106           break;
6107         case DW_AT_discr_value:
6108           attrs->at_discr_value = a;
6109           break;
6110         case DW_AT_encoding:
6111           attrs->at_encoding = a;
6112           break;
6113         case DW_AT_endianity:
6114           attrs->at_endianity = a;
6115           break;
6116         case DW_AT_explicit:
6117           attrs->at_explicit = a;
6118           break;
6119         case DW_AT_is_optional:
6120           attrs->at_is_optional = a;
6121           break;
6122         case DW_AT_location:
6123           attrs->at_location = a;
6124           break;
6125         case DW_AT_lower_bound:
6126           attrs->at_lower_bound = a;
6127           break;
6128         case DW_AT_mutable:
6129           attrs->at_mutable = a;
6130           break;
6131         case DW_AT_ordering:
6132           attrs->at_ordering = a;
6133           break;
6134         case DW_AT_picture_string:
6135           attrs->at_picture_string = a;
6136           break;
6137         case DW_AT_prototyped:
6138           attrs->at_prototyped = a;
6139           break;
6140         case DW_AT_small:
6141           attrs->at_small = a;
6142           break;
6143         case DW_AT_segment:
6144           attrs->at_segment = a;
6145           break;
6146         case DW_AT_string_length:
6147           attrs->at_string_length = a;
6148           break;
6149         case DW_AT_threads_scaled:
6150           attrs->at_threads_scaled = a;
6151           break;
6152         case DW_AT_upper_bound:
6153           attrs->at_upper_bound = a;
6154           break;
6155         case DW_AT_use_location:
6156           attrs->at_use_location = a;
6157           break;
6158         case DW_AT_use_UTF8:
6159           attrs->at_use_UTF8 = a;
6160           break;
6161         case DW_AT_variable_parameter:
6162           attrs->at_variable_parameter = a;
6163           break;
6164         case DW_AT_virtuality:
6165           attrs->at_virtuality = a;
6166           break;
6167         case DW_AT_visibility:
6168           attrs->at_visibility = a;
6169           break;
6170         case DW_AT_vtable_elem_location:
6171           attrs->at_vtable_elem_location = a;
6172           break;
6173         default:
6174           break;
6175         }
6176     }
6177 }
6178
6179 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6180
6181 static void
6182 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6183 {
6184   dw_die_ref c;
6185   dw_die_ref decl;
6186   struct checksum_attributes attrs;
6187
6188   CHECKSUM_ULEB128 ('D');
6189   CHECKSUM_ULEB128 (die->die_tag);
6190
6191   memset (&attrs, 0, sizeof (attrs));
6192
6193   decl = get_AT_ref (die, DW_AT_specification);
6194   if (decl != NULL)
6195     collect_checksum_attributes (&attrs, decl);
6196   collect_checksum_attributes (&attrs, die);
6197
6198   CHECKSUM_ATTR (attrs.at_name);
6199   CHECKSUM_ATTR (attrs.at_accessibility);
6200   CHECKSUM_ATTR (attrs.at_address_class);
6201   CHECKSUM_ATTR (attrs.at_allocated);
6202   CHECKSUM_ATTR (attrs.at_artificial);
6203   CHECKSUM_ATTR (attrs.at_associated);
6204   CHECKSUM_ATTR (attrs.at_binary_scale);
6205   CHECKSUM_ATTR (attrs.at_bit_offset);
6206   CHECKSUM_ATTR (attrs.at_bit_size);
6207   CHECKSUM_ATTR (attrs.at_bit_stride);
6208   CHECKSUM_ATTR (attrs.at_byte_size);
6209   CHECKSUM_ATTR (attrs.at_byte_stride);
6210   CHECKSUM_ATTR (attrs.at_const_value);
6211   CHECKSUM_ATTR (attrs.at_containing_type);
6212   CHECKSUM_ATTR (attrs.at_count);
6213   CHECKSUM_ATTR (attrs.at_data_location);
6214   CHECKSUM_ATTR (attrs.at_data_member_location);
6215   CHECKSUM_ATTR (attrs.at_decimal_scale);
6216   CHECKSUM_ATTR (attrs.at_decimal_sign);
6217   CHECKSUM_ATTR (attrs.at_default_value);
6218   CHECKSUM_ATTR (attrs.at_digit_count);
6219   CHECKSUM_ATTR (attrs.at_discr);
6220   CHECKSUM_ATTR (attrs.at_discr_list);
6221   CHECKSUM_ATTR (attrs.at_discr_value);
6222   CHECKSUM_ATTR (attrs.at_encoding);
6223   CHECKSUM_ATTR (attrs.at_endianity);
6224   CHECKSUM_ATTR (attrs.at_explicit);
6225   CHECKSUM_ATTR (attrs.at_is_optional);
6226   CHECKSUM_ATTR (attrs.at_location);
6227   CHECKSUM_ATTR (attrs.at_lower_bound);
6228   CHECKSUM_ATTR (attrs.at_mutable);
6229   CHECKSUM_ATTR (attrs.at_ordering);
6230   CHECKSUM_ATTR (attrs.at_picture_string);
6231   CHECKSUM_ATTR (attrs.at_prototyped);
6232   CHECKSUM_ATTR (attrs.at_small);
6233   CHECKSUM_ATTR (attrs.at_segment);
6234   CHECKSUM_ATTR (attrs.at_string_length);
6235   CHECKSUM_ATTR (attrs.at_threads_scaled);
6236   CHECKSUM_ATTR (attrs.at_upper_bound);
6237   CHECKSUM_ATTR (attrs.at_use_location);
6238   CHECKSUM_ATTR (attrs.at_use_UTF8);
6239   CHECKSUM_ATTR (attrs.at_variable_parameter);
6240   CHECKSUM_ATTR (attrs.at_virtuality);
6241   CHECKSUM_ATTR (attrs.at_visibility);
6242   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6243   CHECKSUM_ATTR (attrs.at_type);
6244   CHECKSUM_ATTR (attrs.at_friend);
6245
6246   /* Checksum the child DIEs, except for nested types and member functions.  */
6247   c = die->die_child;
6248   if (c) do {
6249     dw_attr_ref name_attr;
6250
6251     c = c->die_sib;
6252     name_attr = get_AT (c, DW_AT_name);
6253     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6254         && name_attr != NULL)
6255       {
6256         CHECKSUM_ULEB128 ('S');
6257         CHECKSUM_ULEB128 (c->die_tag);
6258         CHECKSUM_STRING (AT_string (name_attr));
6259       }
6260     else
6261       {
6262         /* Mark this DIE so it gets processed when unmarking.  */
6263         if (c->die_mark == 0)
6264           c->die_mark = -1;
6265         die_checksum_ordered (c, ctx, mark);
6266       }
6267   } while (c != die->die_child);
6268
6269   CHECKSUM_ULEB128 (0);
6270 }
6271
6272 #undef CHECKSUM
6273 #undef CHECKSUM_STRING
6274 #undef CHECKSUM_ATTR
6275 #undef CHECKSUM_LEB128
6276 #undef CHECKSUM_ULEB128
6277
6278 /* Generate the type signature for DIE.  This is computed by generating an
6279    MD5 checksum over the DIE's tag, its relevant attributes, and its
6280    children.  Attributes that are references to other DIEs are processed
6281    by recursion, using the MARK field to prevent infinite recursion.
6282    If the DIE is nested inside a namespace or another type, we also
6283    need to include that context in the signature.  The lower 64 bits
6284    of the resulting MD5 checksum comprise the signature.  */
6285
6286 static void
6287 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6288 {
6289   int mark;
6290   const char *name;
6291   unsigned char checksum[16];
6292   struct md5_ctx ctx;
6293   dw_die_ref decl;
6294
6295   name = get_AT_string (die, DW_AT_name);
6296   decl = get_AT_ref (die, DW_AT_specification);
6297
6298   /* First, compute a signature for just the type name (and its surrounding
6299      context, if any.  This is stored in the type unit DIE for link-time
6300      ODR (one-definition rule) checking.  */
6301
6302   if (is_cxx() && name != NULL)
6303     {
6304       md5_init_ctx (&ctx);
6305
6306       /* Checksum the names of surrounding namespaces and structures.  */
6307       if (decl != NULL && decl->die_parent != NULL)
6308         checksum_die_context (decl->die_parent, &ctx);
6309
6310       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6311       md5_process_bytes (name, strlen (name) + 1, &ctx);
6312       md5_finish_ctx (&ctx, checksum);
6313
6314       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6315     }
6316
6317   /* Next, compute the complete type signature.  */
6318
6319   md5_init_ctx (&ctx);
6320   mark = 1;
6321   die->die_mark = mark;
6322
6323   /* Checksum the names of surrounding namespaces and structures.  */
6324   if (decl != NULL && decl->die_parent != NULL)
6325     checksum_die_context (decl->die_parent, &ctx);
6326
6327   /* Checksum the DIE and its children.  */
6328   die_checksum_ordered (die, &ctx, &mark);
6329   unmark_all_dies (die);
6330   md5_finish_ctx (&ctx, checksum);
6331
6332   /* Store the signature in the type node and link the type DIE and the
6333      type node together.  */
6334   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6335           DWARF_TYPE_SIGNATURE_SIZE);
6336   die->die_id.die_type_node = type_node;
6337   type_node->type_die = die;
6338
6339   /* If the DIE is a specification, link its declaration to the type node
6340      as well.  */
6341   if (decl != NULL)
6342     decl->die_id.die_type_node = type_node;
6343 }
6344
6345 /* Do the location expressions look same?  */
6346 static inline int
6347 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6348 {
6349   return loc1->dw_loc_opc == loc2->dw_loc_opc
6350          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6351          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6352 }
6353
6354 /* Do the values look the same?  */
6355 static int
6356 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6357 {
6358   dw_loc_descr_ref loc1, loc2;
6359   rtx r1, r2;
6360
6361   if (v1->val_class != v2->val_class)
6362     return 0;
6363
6364   switch (v1->val_class)
6365     {
6366     case dw_val_class_const:
6367       return v1->v.val_int == v2->v.val_int;
6368     case dw_val_class_unsigned_const:
6369       return v1->v.val_unsigned == v2->v.val_unsigned;
6370     case dw_val_class_const_double:
6371       return v1->v.val_double.high == v2->v.val_double.high
6372              && v1->v.val_double.low == v2->v.val_double.low;
6373     case dw_val_class_vec:
6374       if (v1->v.val_vec.length != v2->v.val_vec.length
6375           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6376         return 0;
6377       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6378                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6379         return 0;
6380       return 1;
6381     case dw_val_class_flag:
6382       return v1->v.val_flag == v2->v.val_flag;
6383     case dw_val_class_str:
6384       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6385
6386     case dw_val_class_addr:
6387       r1 = v1->v.val_addr;
6388       r2 = v2->v.val_addr;
6389       if (GET_CODE (r1) != GET_CODE (r2))
6390         return 0;
6391       return !rtx_equal_p (r1, r2);
6392
6393     case dw_val_class_offset:
6394       return v1->v.val_offset == v2->v.val_offset;
6395
6396     case dw_val_class_loc:
6397       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6398            loc1 && loc2;
6399            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6400         if (!same_loc_p (loc1, loc2, mark))
6401           return 0;
6402       return !loc1 && !loc2;
6403
6404     case dw_val_class_die_ref:
6405       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6406
6407     case dw_val_class_fde_ref:
6408     case dw_val_class_vms_delta:
6409     case dw_val_class_lbl_id:
6410     case dw_val_class_lineptr:
6411     case dw_val_class_macptr:
6412       return 1;
6413
6414     case dw_val_class_file:
6415       return v1->v.val_file == v2->v.val_file;
6416
6417     case dw_val_class_data8:
6418       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6419
6420     default:
6421       return 1;
6422     }
6423 }
6424
6425 /* Do the attributes look the same?  */
6426
6427 static int
6428 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6429 {
6430   if (at1->dw_attr != at2->dw_attr)
6431     return 0;
6432
6433   /* We don't care that this was compiled with a different compiler
6434      snapshot; if the output is the same, that's what matters. */
6435   if (at1->dw_attr == DW_AT_producer)
6436     return 1;
6437
6438   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6439 }
6440
6441 /* Do the dies look the same?  */
6442
6443 static int
6444 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6445 {
6446   dw_die_ref c1, c2;
6447   dw_attr_ref a1;
6448   unsigned ix;
6449
6450   /* To avoid infinite recursion.  */
6451   if (die1->die_mark)
6452     return die1->die_mark == die2->die_mark;
6453   die1->die_mark = die2->die_mark = ++(*mark);
6454
6455   if (die1->die_tag != die2->die_tag)
6456     return 0;
6457
6458   if (VEC_length (dw_attr_node, die1->die_attr)
6459       != VEC_length (dw_attr_node, die2->die_attr))
6460     return 0;
6461
6462   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6463     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6464       return 0;
6465
6466   c1 = die1->die_child;
6467   c2 = die2->die_child;
6468   if (! c1)
6469     {
6470       if (c2)
6471         return 0;
6472     }
6473   else
6474     for (;;)
6475       {
6476         if (!same_die_p (c1, c2, mark))
6477           return 0;
6478         c1 = c1->die_sib;
6479         c2 = c2->die_sib;
6480         if (c1 == die1->die_child)
6481           {
6482             if (c2 == die2->die_child)
6483               break;
6484             else
6485               return 0;
6486           }
6487     }
6488
6489   return 1;
6490 }
6491
6492 /* Do the dies look the same?  Wrapper around same_die_p.  */
6493
6494 static int
6495 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6496 {
6497   int mark = 0;
6498   int ret = same_die_p (die1, die2, &mark);
6499
6500   unmark_all_dies (die1);
6501   unmark_all_dies (die2);
6502
6503   return ret;
6504 }
6505
6506 /* The prefix to attach to symbols on DIEs in the current comdat debug
6507    info section.  */
6508 static char *comdat_symbol_id;
6509
6510 /* The index of the current symbol within the current comdat CU.  */
6511 static unsigned int comdat_symbol_number;
6512
6513 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6514    children, and set comdat_symbol_id accordingly.  */
6515
6516 static void
6517 compute_section_prefix (dw_die_ref unit_die)
6518 {
6519   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6520   const char *base = die_name ? lbasename (die_name) : "anonymous";
6521   char *name = XALLOCAVEC (char, strlen (base) + 64);
6522   char *p;
6523   int i, mark;
6524   unsigned char checksum[16];
6525   struct md5_ctx ctx;
6526
6527   /* Compute the checksum of the DIE, then append part of it as hex digits to
6528      the name filename of the unit.  */
6529
6530   md5_init_ctx (&ctx);
6531   mark = 0;
6532   die_checksum (unit_die, &ctx, &mark);
6533   unmark_all_dies (unit_die);
6534   md5_finish_ctx (&ctx, checksum);
6535
6536   sprintf (name, "%s.", base);
6537   clean_symbol_name (name);
6538
6539   p = name + strlen (name);
6540   for (i = 0; i < 4; i++)
6541     {
6542       sprintf (p, "%.2x", checksum[i]);
6543       p += 2;
6544     }
6545
6546   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6547   comdat_symbol_number = 0;
6548 }
6549
6550 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6551
6552 static int
6553 is_type_die (dw_die_ref die)
6554 {
6555   switch (die->die_tag)
6556     {
6557     case DW_TAG_array_type:
6558     case DW_TAG_class_type:
6559     case DW_TAG_interface_type:
6560     case DW_TAG_enumeration_type:
6561     case DW_TAG_pointer_type:
6562     case DW_TAG_reference_type:
6563     case DW_TAG_rvalue_reference_type:
6564     case DW_TAG_string_type:
6565     case DW_TAG_structure_type:
6566     case DW_TAG_subroutine_type:
6567     case DW_TAG_union_type:
6568     case DW_TAG_ptr_to_member_type:
6569     case DW_TAG_set_type:
6570     case DW_TAG_subrange_type:
6571     case DW_TAG_base_type:
6572     case DW_TAG_const_type:
6573     case DW_TAG_file_type:
6574     case DW_TAG_packed_type:
6575     case DW_TAG_volatile_type:
6576     case DW_TAG_typedef:
6577       return 1;
6578     default:
6579       return 0;
6580     }
6581 }
6582
6583 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6584    Basically, we want to choose the bits that are likely to be shared between
6585    compilations (types) and leave out the bits that are specific to individual
6586    compilations (functions).  */
6587
6588 static int
6589 is_comdat_die (dw_die_ref c)
6590 {
6591   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6592      we do for stabs.  The advantage is a greater likelihood of sharing between
6593      objects that don't include headers in the same order (and therefore would
6594      put the base types in a different comdat).  jason 8/28/00 */
6595
6596   if (c->die_tag == DW_TAG_base_type)
6597     return 0;
6598
6599   if (c->die_tag == DW_TAG_pointer_type
6600       || c->die_tag == DW_TAG_reference_type
6601       || c->die_tag == DW_TAG_rvalue_reference_type
6602       || c->die_tag == DW_TAG_const_type
6603       || c->die_tag == DW_TAG_volatile_type)
6604     {
6605       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6606
6607       return t ? is_comdat_die (t) : 0;
6608     }
6609
6610   return is_type_die (c);
6611 }
6612
6613 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6614    compilation unit.  */
6615
6616 static int
6617 is_symbol_die (dw_die_ref c)
6618 {
6619   return (is_type_die (c)
6620           || is_declaration_die (c)
6621           || c->die_tag == DW_TAG_namespace
6622           || c->die_tag == DW_TAG_module);
6623 }
6624
6625 /* Returns true iff C is a compile-unit DIE.  */
6626
6627 static inline bool
6628 is_cu_die (dw_die_ref c)
6629 {
6630   return c && c->die_tag == DW_TAG_compile_unit;
6631 }
6632
6633 static char *
6634 gen_internal_sym (const char *prefix)
6635 {
6636   char buf[256];
6637
6638   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6639   return xstrdup (buf);
6640 }
6641
6642 /* Assign symbols to all worthy DIEs under DIE.  */
6643
6644 static void
6645 assign_symbol_names (dw_die_ref die)
6646 {
6647   dw_die_ref c;
6648
6649   if (is_symbol_die (die))
6650     {
6651       if (comdat_symbol_id)
6652         {
6653           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6654
6655           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6656                    comdat_symbol_id, comdat_symbol_number++);
6657           die->die_id.die_symbol = xstrdup (p);
6658         }
6659       else
6660         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6661     }
6662
6663   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6664 }
6665
6666 struct cu_hash_table_entry
6667 {
6668   dw_die_ref cu;
6669   unsigned min_comdat_num, max_comdat_num;
6670   struct cu_hash_table_entry *next;
6671 };
6672
6673 /* Routines to manipulate hash table of CUs.  */
6674 static hashval_t
6675 htab_cu_hash (const void *of)
6676 {
6677   const struct cu_hash_table_entry *const entry =
6678     (const struct cu_hash_table_entry *) of;
6679
6680   return htab_hash_string (entry->cu->die_id.die_symbol);
6681 }
6682
6683 static int
6684 htab_cu_eq (const void *of1, const void *of2)
6685 {
6686   const struct cu_hash_table_entry *const entry1 =
6687     (const struct cu_hash_table_entry *) of1;
6688   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6689
6690   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6691 }
6692
6693 static void
6694 htab_cu_del (void *what)
6695 {
6696   struct cu_hash_table_entry *next,
6697     *entry = (struct cu_hash_table_entry *) what;
6698
6699   while (entry)
6700     {
6701       next = entry->next;
6702       free (entry);
6703       entry = next;
6704     }
6705 }
6706
6707 /* Check whether we have already seen this CU and set up SYM_NUM
6708    accordingly.  */
6709 static int
6710 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6711 {
6712   struct cu_hash_table_entry dummy;
6713   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6714
6715   dummy.max_comdat_num = 0;
6716
6717   slot = (struct cu_hash_table_entry **)
6718     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6719         INSERT);
6720   entry = *slot;
6721
6722   for (; entry; last = entry, entry = entry->next)
6723     {
6724       if (same_die_p_wrap (cu, entry->cu))
6725         break;
6726     }
6727
6728   if (entry)
6729     {
6730       *sym_num = entry->min_comdat_num;
6731       return 1;
6732     }
6733
6734   entry = XCNEW (struct cu_hash_table_entry);
6735   entry->cu = cu;
6736   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6737   entry->next = *slot;
6738   *slot = entry;
6739
6740   return 0;
6741 }
6742
6743 /* Record SYM_NUM to record of CU in HTABLE.  */
6744 static void
6745 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6746 {
6747   struct cu_hash_table_entry **slot, *entry;
6748
6749   slot = (struct cu_hash_table_entry **)
6750     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6751         NO_INSERT);
6752   entry = *slot;
6753
6754   entry->max_comdat_num = sym_num;
6755 }
6756
6757 /* Traverse the DIE (which is always comp_unit_die), and set up
6758    additional compilation units for each of the include files we see
6759    bracketed by BINCL/EINCL.  */
6760
6761 static void
6762 break_out_includes (dw_die_ref die)
6763 {
6764   dw_die_ref c;
6765   dw_die_ref unit = NULL;
6766   limbo_die_node *node, **pnode;
6767   htab_t cu_hash_table;
6768
6769   c = die->die_child;
6770   if (c) do {
6771     dw_die_ref prev = c;
6772     c = c->die_sib;
6773     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6774            || (unit && is_comdat_die (c)))
6775       {
6776         dw_die_ref next = c->die_sib;
6777
6778         /* This DIE is for a secondary CU; remove it from the main one.  */
6779         remove_child_with_prev (c, prev);
6780
6781         if (c->die_tag == DW_TAG_GNU_BINCL)
6782           unit = push_new_compile_unit (unit, c);
6783         else if (c->die_tag == DW_TAG_GNU_EINCL)
6784           unit = pop_compile_unit (unit);
6785         else
6786           add_child_die (unit, c);
6787         c = next;
6788         if (c == die->die_child)
6789           break;
6790       }
6791   } while (c != die->die_child);
6792
6793 #if 0
6794   /* We can only use this in debugging, since the frontend doesn't check
6795      to make sure that we leave every include file we enter.  */
6796   gcc_assert (!unit);
6797 #endif
6798
6799   assign_symbol_names (die);
6800   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6801   for (node = limbo_die_list, pnode = &limbo_die_list;
6802        node;
6803        node = node->next)
6804     {
6805       int is_dupl;
6806
6807       compute_section_prefix (node->die);
6808       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6809                         &comdat_symbol_number);
6810       assign_symbol_names (node->die);
6811       if (is_dupl)
6812         *pnode = node->next;
6813       else
6814         {
6815           pnode = &node->next;
6816           record_comdat_symbol_number (node->die, cu_hash_table,
6817                 comdat_symbol_number);
6818         }
6819     }
6820   htab_delete (cu_hash_table);
6821 }
6822
6823 /* Return non-zero if this DIE is a declaration.  */
6824
6825 static int
6826 is_declaration_die (dw_die_ref die)
6827 {
6828   dw_attr_ref a;
6829   unsigned ix;
6830
6831   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6832     if (a->dw_attr == DW_AT_declaration)
6833       return 1;
6834
6835   return 0;
6836 }
6837
6838 /* Return non-zero if this DIE is nested inside a subprogram.  */
6839
6840 static int
6841 is_nested_in_subprogram (dw_die_ref die)
6842 {
6843   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6844
6845   if (decl == NULL)
6846     decl = die;
6847   return local_scope_p (decl);
6848 }
6849
6850 /* Return non-zero if this DIE contains a defining declaration of a
6851    subprogram.  */
6852
6853 static int
6854 contains_subprogram_definition (dw_die_ref die)
6855 {
6856   dw_die_ref c;
6857
6858   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6859     return 1;
6860   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6861   return 0;
6862 }
6863
6864 /* Return non-zero if this is a type DIE that should be moved to a
6865    COMDAT .debug_types section.  */
6866
6867 static int
6868 should_move_die_to_comdat (dw_die_ref die)
6869 {
6870   switch (die->die_tag)
6871     {
6872     case DW_TAG_class_type:
6873     case DW_TAG_structure_type:
6874     case DW_TAG_enumeration_type:
6875     case DW_TAG_union_type:
6876       /* Don't move declarations, inlined instances, or types nested in a
6877          subprogram.  */
6878       if (is_declaration_die (die)
6879           || get_AT (die, DW_AT_abstract_origin)
6880           || is_nested_in_subprogram (die))
6881         return 0;
6882       /* A type definition should never contain a subprogram definition.  */
6883       gcc_assert (!contains_subprogram_definition (die));
6884       return 1;
6885     case DW_TAG_array_type:
6886     case DW_TAG_interface_type:
6887     case DW_TAG_pointer_type:
6888     case DW_TAG_reference_type:
6889     case DW_TAG_rvalue_reference_type:
6890     case DW_TAG_string_type:
6891     case DW_TAG_subroutine_type:
6892     case DW_TAG_ptr_to_member_type:
6893     case DW_TAG_set_type:
6894     case DW_TAG_subrange_type:
6895     case DW_TAG_base_type:
6896     case DW_TAG_const_type:
6897     case DW_TAG_file_type:
6898     case DW_TAG_packed_type:
6899     case DW_TAG_volatile_type:
6900     case DW_TAG_typedef:
6901     default:
6902       return 0;
6903     }
6904 }
6905
6906 /* Make a clone of DIE.  */
6907
6908 static dw_die_ref
6909 clone_die (dw_die_ref die)
6910 {
6911   dw_die_ref clone;
6912   dw_attr_ref a;
6913   unsigned ix;
6914
6915   clone = ggc_alloc_cleared_die_node ();
6916   clone->die_tag = die->die_tag;
6917
6918   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6919     add_dwarf_attr (clone, a);
6920
6921   return clone;
6922 }
6923
6924 /* Make a clone of the tree rooted at DIE.  */
6925
6926 static dw_die_ref
6927 clone_tree (dw_die_ref die)
6928 {
6929   dw_die_ref c;
6930   dw_die_ref clone = clone_die (die);
6931
6932   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6933
6934   return clone;
6935 }
6936
6937 /* Make a clone of DIE as a declaration.  */
6938
6939 static dw_die_ref
6940 clone_as_declaration (dw_die_ref die)
6941 {
6942   dw_die_ref clone;
6943   dw_die_ref decl;
6944   dw_attr_ref a;
6945   unsigned ix;
6946
6947   /* If the DIE is already a declaration, just clone it.  */
6948   if (is_declaration_die (die))
6949     return clone_die (die);
6950
6951   /* If the DIE is a specification, just clone its declaration DIE.  */
6952   decl = get_AT_ref (die, DW_AT_specification);
6953   if (decl != NULL)
6954     return clone_die (decl);
6955
6956   clone = ggc_alloc_cleared_die_node ();
6957   clone->die_tag = die->die_tag;
6958
6959   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6960     {
6961       /* We don't want to copy over all attributes.
6962          For example we don't want DW_AT_byte_size because otherwise we will no
6963          longer have a declaration and GDB will treat it as a definition.  */
6964
6965       switch (a->dw_attr)
6966         {
6967         case DW_AT_artificial:
6968         case DW_AT_containing_type:
6969         case DW_AT_external:
6970         case DW_AT_name:
6971         case DW_AT_type:
6972         case DW_AT_virtuality:
6973         case DW_AT_linkage_name:
6974         case DW_AT_MIPS_linkage_name:
6975           add_dwarf_attr (clone, a);
6976           break;
6977         case DW_AT_byte_size:
6978         default:
6979           break;
6980         }
6981     }
6982
6983   if (die->die_id.die_type_node)
6984     add_AT_die_ref (clone, DW_AT_signature, die);
6985
6986   add_AT_flag (clone, DW_AT_declaration, 1);
6987   return clone;
6988 }
6989
6990 /* Copy the declaration context to the new compile unit DIE.  This includes
6991    any surrounding namespace or type declarations.  If the DIE has an
6992    AT_specification attribute, it also includes attributes and children
6993    attached to the specification.  */
6994
6995 static void
6996 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6997 {
6998   dw_die_ref decl;
6999   dw_die_ref new_decl;
7000
7001   decl = get_AT_ref (die, DW_AT_specification);
7002   if (decl == NULL)
7003     decl = die;
7004   else
7005     {
7006       unsigned ix;
7007       dw_die_ref c;
7008       dw_attr_ref a;
7009
7010       /* Copy the type node pointer from the new DIE to the original
7011          declaration DIE so we can forward references later.  */
7012       decl->die_id.die_type_node = die->die_id.die_type_node;
7013
7014       remove_AT (die, DW_AT_specification);
7015
7016       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7017         {
7018           if (a->dw_attr != DW_AT_name
7019               && a->dw_attr != DW_AT_declaration
7020               && a->dw_attr != DW_AT_external)
7021             add_dwarf_attr (die, a);
7022         }
7023
7024       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7025     }
7026
7027   if (decl->die_parent != NULL
7028       && decl->die_parent->die_tag != DW_TAG_compile_unit
7029       && decl->die_parent->die_tag != DW_TAG_type_unit)
7030     {
7031       new_decl = copy_ancestor_tree (unit, decl, NULL);
7032       if (new_decl != NULL)
7033         {
7034           remove_AT (new_decl, DW_AT_signature);
7035           add_AT_specification (die, new_decl);
7036         }
7037     }
7038 }
7039
7040 /* Generate the skeleton ancestor tree for the given NODE, then clone
7041    the DIE and add the clone into the tree.  */
7042
7043 static void
7044 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7045 {
7046   if (node->new_die != NULL)
7047     return;
7048
7049   node->new_die = clone_as_declaration (node->old_die);
7050
7051   if (node->parent != NULL)
7052     {
7053       generate_skeleton_ancestor_tree (node->parent);
7054       add_child_die (node->parent->new_die, node->new_die);
7055     }
7056 }
7057
7058 /* Generate a skeleton tree of DIEs containing any declarations that are
7059    found in the original tree.  We traverse the tree looking for declaration
7060    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7061
7062 static void
7063 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7064 {
7065   skeleton_chain_node node;
7066   dw_die_ref c;
7067   dw_die_ref first;
7068   dw_die_ref prev = NULL;
7069   dw_die_ref next = NULL;
7070
7071   node.parent = parent;
7072
7073   first = c = parent->old_die->die_child;
7074   if (c)
7075     next = c->die_sib;
7076   if (c) do {
7077     if (prev == NULL || prev->die_sib == c)
7078       prev = c;
7079     c = next;
7080     next = (c == first ? NULL : c->die_sib);
7081     node.old_die = c;
7082     node.new_die = NULL;
7083     if (is_declaration_die (c))
7084       {
7085         /* Clone the existing DIE, move the original to the skeleton
7086            tree (which is in the main CU), and put the clone, with
7087            all the original's children, where the original came from.  */
7088         dw_die_ref clone = clone_die (c);
7089         move_all_children (c, clone);
7090
7091         replace_child (c, clone, prev);
7092         generate_skeleton_ancestor_tree (parent);
7093         add_child_die (parent->new_die, c);
7094         node.new_die = c;
7095         c = clone;
7096       }
7097     generate_skeleton_bottom_up (&node);
7098   } while (next != NULL);
7099 }
7100
7101 /* Wrapper function for generate_skeleton_bottom_up.  */
7102
7103 static dw_die_ref
7104 generate_skeleton (dw_die_ref die)
7105 {
7106   skeleton_chain_node node;
7107
7108   node.old_die = die;
7109   node.new_die = NULL;
7110   node.parent = NULL;
7111
7112   /* If this type definition is nested inside another type,
7113      always leave at least a declaration in its place.  */
7114   if (die->die_parent != NULL && is_type_die (die->die_parent))
7115     node.new_die = clone_as_declaration (die);
7116
7117   generate_skeleton_bottom_up (&node);
7118   return node.new_die;
7119 }
7120
7121 /* Remove the DIE from its parent, possibly replacing it with a cloned
7122    declaration.  The original DIE will be moved to a new compile unit
7123    so that existing references to it follow it to the new location.  If
7124    any of the original DIE's descendants is a declaration, we need to
7125    replace the original DIE with a skeleton tree and move the
7126    declarations back into the skeleton tree.  */
7127
7128 static dw_die_ref
7129 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7130 {
7131   dw_die_ref skeleton;
7132
7133   skeleton = generate_skeleton (child);
7134   if (skeleton == NULL)
7135     remove_child_with_prev (child, prev);
7136   else
7137     {
7138       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7139       replace_child (child, skeleton, prev);
7140     }
7141
7142   return skeleton;
7143 }
7144
7145 /* Traverse the DIE and set up additional .debug_types sections for each
7146    type worthy of being placed in a COMDAT section.  */
7147
7148 static void
7149 break_out_comdat_types (dw_die_ref die)
7150 {
7151   dw_die_ref c;
7152   dw_die_ref first;
7153   dw_die_ref prev = NULL;
7154   dw_die_ref next = NULL;
7155   dw_die_ref unit = NULL;
7156
7157   first = c = die->die_child;
7158   if (c)
7159     next = c->die_sib;
7160   if (c) do {
7161     if (prev == NULL || prev->die_sib == c)
7162       prev = c;
7163     c = next;
7164     next = (c == first ? NULL : c->die_sib);
7165     if (should_move_die_to_comdat (c))
7166       {
7167         dw_die_ref replacement;
7168         comdat_type_node_ref type_node;
7169
7170         /* Create a new type unit DIE as the root for the new tree, and
7171            add it to the list of comdat types.  */
7172         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7173         add_AT_unsigned (unit, DW_AT_language,
7174                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7175         type_node = ggc_alloc_cleared_comdat_type_node ();
7176         type_node->root_die = unit;
7177         type_node->next = comdat_type_list;
7178         comdat_type_list = type_node;
7179
7180         /* Generate the type signature.  */
7181         generate_type_signature (c, type_node);
7182
7183         /* Copy the declaration context, attributes, and children of the
7184            declaration into the new compile unit DIE.  */
7185         copy_declaration_context (unit, c);
7186
7187         /* Remove this DIE from the main CU.  */
7188         replacement = remove_child_or_replace_with_skeleton (c, prev);
7189
7190         /* Break out nested types into their own type units.  */
7191         break_out_comdat_types (c);
7192
7193         /* Add the DIE to the new compunit.  */
7194         add_child_die (unit, c);
7195
7196         if (replacement != NULL)
7197           c = replacement;
7198       }
7199     else if (c->die_tag == DW_TAG_namespace
7200              || c->die_tag == DW_TAG_class_type
7201              || c->die_tag == DW_TAG_structure_type
7202              || c->die_tag == DW_TAG_union_type)
7203       {
7204         /* Look for nested types that can be broken out.  */
7205         break_out_comdat_types (c);
7206       }
7207   } while (next != NULL);
7208 }
7209
7210 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7211
7212 struct decl_table_entry
7213 {
7214   dw_die_ref orig;
7215   dw_die_ref copy;
7216 };
7217
7218 /* Routines to manipulate hash table of copied declarations.  */
7219
7220 static hashval_t
7221 htab_decl_hash (const void *of)
7222 {
7223   const struct decl_table_entry *const entry =
7224     (const struct decl_table_entry *) of;
7225
7226   return htab_hash_pointer (entry->orig);
7227 }
7228
7229 static int
7230 htab_decl_eq (const void *of1, const void *of2)
7231 {
7232   const struct decl_table_entry *const entry1 =
7233     (const struct decl_table_entry *) of1;
7234   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7235
7236   return entry1->orig == entry2;
7237 }
7238
7239 static void
7240 htab_decl_del (void *what)
7241 {
7242   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7243
7244   free (entry);
7245 }
7246
7247 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7248    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7249    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7250    to check if the ancestor has already been copied into UNIT.  */
7251
7252 static dw_die_ref
7253 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7254 {
7255   dw_die_ref parent = die->die_parent;
7256   dw_die_ref new_parent = unit;
7257   dw_die_ref copy;
7258   void **slot = NULL;
7259   struct decl_table_entry *entry = NULL;
7260
7261   if (decl_table)
7262     {
7263       /* Check if the entry has already been copied to UNIT.  */
7264       slot = htab_find_slot_with_hash (decl_table, die,
7265                                        htab_hash_pointer (die), INSERT);
7266       if (*slot != HTAB_EMPTY_ENTRY)
7267         {
7268           entry = (struct decl_table_entry *) *slot;
7269           return entry->copy;
7270         }
7271
7272       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7273       entry = XCNEW (struct decl_table_entry);
7274       entry->orig = die;
7275       entry->copy = NULL;
7276       *slot = entry;
7277     }
7278
7279   if (parent != NULL)
7280     {
7281       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7282       if (spec != NULL)
7283         parent = spec;
7284       if (parent->die_tag != DW_TAG_compile_unit
7285           && parent->die_tag != DW_TAG_type_unit)
7286         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7287     }
7288
7289   copy = clone_as_declaration (die);
7290   add_child_die (new_parent, copy);
7291
7292   if (decl_table != NULL)
7293     {
7294       /* Record the pointer to the copy.  */
7295       entry->copy = copy;
7296     }
7297
7298   return copy;
7299 }
7300
7301 /* Walk the DIE and its children, looking for references to incomplete
7302    or trivial types that are unmarked (i.e., that are not in the current
7303    type_unit).  */
7304
7305 static void
7306 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7307 {
7308   dw_die_ref c;
7309   dw_attr_ref a;
7310   unsigned ix;
7311
7312   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7313     {
7314       if (AT_class (a) == dw_val_class_die_ref)
7315         {
7316           dw_die_ref targ = AT_ref (a);
7317           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7318           void **slot;
7319           struct decl_table_entry *entry;
7320
7321           if (targ->die_mark != 0 || type_node != NULL)
7322             continue;
7323
7324           slot = htab_find_slot_with_hash (decl_table, targ,
7325                                            htab_hash_pointer (targ), INSERT);
7326
7327           if (*slot != HTAB_EMPTY_ENTRY)
7328             {
7329               /* TARG has already been copied, so we just need to
7330                  modify the reference to point to the copy.  */
7331               entry = (struct decl_table_entry *) *slot;
7332               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7333             }
7334           else
7335             {
7336               dw_die_ref parent = unit;
7337               dw_die_ref copy = clone_tree (targ);
7338
7339               /* Make sure the cloned tree is marked as part of the
7340                  type unit.  */
7341               mark_dies (copy);
7342
7343               /* Record in DECL_TABLE that TARG has been copied.
7344                  Need to do this now, before the recursive call,
7345                  because DECL_TABLE may be expanded and SLOT
7346                  would no longer be a valid pointer.  */
7347               entry = XCNEW (struct decl_table_entry);
7348               entry->orig = targ;
7349               entry->copy = copy;
7350               *slot = entry;
7351
7352               /* If TARG has surrounding context, copy its ancestor tree
7353                  into the new type unit.  */
7354               if (targ->die_parent != NULL
7355                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7356                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7357                 parent = copy_ancestor_tree (unit, targ->die_parent,
7358                                              decl_table);
7359
7360               add_child_die (parent, copy);
7361               a->dw_attr_val.v.val_die_ref.die = copy;
7362
7363               /* Make sure the newly-copied DIE is walked.  If it was
7364                  installed in a previously-added context, it won't
7365                  get visited otherwise.  */
7366               if (parent != unit)
7367                 {
7368                   /* Find the highest point of the newly-added tree,
7369                      mark each node along the way, and walk from there.  */
7370                   parent->die_mark = 1;
7371                   while (parent->die_parent
7372                          && parent->die_parent->die_mark == 0)
7373                     {
7374                       parent = parent->die_parent;
7375                       parent->die_mark = 1;
7376                     }
7377                   copy_decls_walk (unit, parent, decl_table);
7378                 }
7379             }
7380         }
7381     }
7382
7383   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7384 }
7385
7386 /* Copy declarations for "unworthy" types into the new comdat section.
7387    Incomplete types, modified types, and certain other types aren't broken
7388    out into comdat sections of their own, so they don't have a signature,
7389    and we need to copy the declaration into the same section so that we
7390    don't have an external reference.  */
7391
7392 static void
7393 copy_decls_for_unworthy_types (dw_die_ref unit)
7394 {
7395   htab_t decl_table;
7396
7397   mark_dies (unit);
7398   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7399   copy_decls_walk (unit, unit, decl_table);
7400   htab_delete (decl_table);
7401   unmark_dies (unit);
7402 }
7403
7404 /* Traverse the DIE and add a sibling attribute if it may have the
7405    effect of speeding up access to siblings.  To save some space,
7406    avoid generating sibling attributes for DIE's without children.  */
7407
7408 static void
7409 add_sibling_attributes (dw_die_ref die)
7410 {
7411   dw_die_ref c;
7412
7413   if (! die->die_child)
7414     return;
7415
7416   if (die->die_parent && die != die->die_parent->die_child)
7417     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7418
7419   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7420 }
7421
7422 /* Output all location lists for the DIE and its children.  */
7423
7424 static void
7425 output_location_lists (dw_die_ref die)
7426 {
7427   dw_die_ref c;
7428   dw_attr_ref a;
7429   unsigned ix;
7430
7431   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7432     if (AT_class (a) == dw_val_class_loc_list)
7433       output_loc_list (AT_loc_list (a));
7434
7435   FOR_EACH_CHILD (die, c, output_location_lists (c));
7436 }
7437
7438 /* The format of each DIE (and its attribute value pairs) is encoded in an
7439    abbreviation table.  This routine builds the abbreviation table and assigns
7440    a unique abbreviation id for each abbreviation entry.  The children of each
7441    die are visited recursively.  */
7442
7443 static void
7444 build_abbrev_table (dw_die_ref die)
7445 {
7446   unsigned long abbrev_id;
7447   unsigned int n_alloc;
7448   dw_die_ref c;
7449   dw_attr_ref a;
7450   unsigned ix;
7451
7452   /* Scan the DIE references, and mark as external any that refer to
7453      DIEs from other CUs (i.e. those which are not marked).  */
7454   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7455     if (AT_class (a) == dw_val_class_die_ref
7456         && AT_ref (a)->die_mark == 0)
7457       {
7458         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7459         set_AT_ref_external (a, 1);
7460       }
7461
7462   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7463     {
7464       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7465       dw_attr_ref die_a, abbrev_a;
7466       unsigned ix;
7467       bool ok = true;
7468
7469       if (abbrev->die_tag != die->die_tag)
7470         continue;
7471       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7472         continue;
7473
7474       if (VEC_length (dw_attr_node, abbrev->die_attr)
7475           != VEC_length (dw_attr_node, die->die_attr))
7476         continue;
7477
7478       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7479         {
7480           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7481           if ((abbrev_a->dw_attr != die_a->dw_attr)
7482               || (value_format (abbrev_a) != value_format (die_a)))
7483             {
7484               ok = false;
7485               break;
7486             }
7487         }
7488       if (ok)
7489         break;
7490     }
7491
7492   if (abbrev_id >= abbrev_die_table_in_use)
7493     {
7494       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7495         {
7496           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7497           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7498                                             n_alloc);
7499
7500           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7501                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7502           abbrev_die_table_allocated = n_alloc;
7503         }
7504
7505       ++abbrev_die_table_in_use;
7506       abbrev_die_table[abbrev_id] = die;
7507     }
7508
7509   die->die_abbrev = abbrev_id;
7510   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7511 }
7512 \f
7513 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7514
7515 static int
7516 constant_size (unsigned HOST_WIDE_INT value)
7517 {
7518   int log;
7519
7520   if (value == 0)
7521     log = 0;
7522   else
7523     log = floor_log2 (value);
7524
7525   log = log / 8;
7526   log = 1 << (floor_log2 (log) + 1);
7527
7528   return log;
7529 }
7530
7531 /* Return the size of a DIE as it is represented in the
7532    .debug_info section.  */
7533
7534 static unsigned long
7535 size_of_die (dw_die_ref die)
7536 {
7537   unsigned long size = 0;
7538   dw_attr_ref a;
7539   unsigned ix;
7540
7541   size += size_of_uleb128 (die->die_abbrev);
7542   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7543     {
7544       switch (AT_class (a))
7545         {
7546         case dw_val_class_addr:
7547           size += DWARF2_ADDR_SIZE;
7548           break;
7549         case dw_val_class_offset:
7550           size += DWARF_OFFSET_SIZE;
7551           break;
7552         case dw_val_class_loc:
7553           {
7554             unsigned long lsize = size_of_locs (AT_loc (a));
7555
7556             /* Block length.  */
7557             if (dwarf_version >= 4)
7558               size += size_of_uleb128 (lsize);
7559             else
7560               size += constant_size (lsize);
7561             size += lsize;
7562           }
7563           break;
7564         case dw_val_class_loc_list:
7565           size += DWARF_OFFSET_SIZE;
7566           break;
7567         case dw_val_class_range_list:
7568           size += DWARF_OFFSET_SIZE;
7569           break;
7570         case dw_val_class_const:
7571           size += size_of_sleb128 (AT_int (a));
7572           break;
7573         case dw_val_class_unsigned_const:
7574           size += constant_size (AT_unsigned (a));
7575           break;
7576         case dw_val_class_const_double:
7577           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7578           if (HOST_BITS_PER_WIDE_INT >= 64)
7579             size++; /* block */
7580           break;
7581         case dw_val_class_vec:
7582           size += constant_size (a->dw_attr_val.v.val_vec.length
7583                                  * a->dw_attr_val.v.val_vec.elt_size)
7584                   + a->dw_attr_val.v.val_vec.length
7585                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7586           break;
7587         case dw_val_class_flag:
7588           if (dwarf_version >= 4)
7589             /* Currently all add_AT_flag calls pass in 1 as last argument,
7590                so DW_FORM_flag_present can be used.  If that ever changes,
7591                we'll need to use DW_FORM_flag and have some optimization
7592                in build_abbrev_table that will change those to
7593                DW_FORM_flag_present if it is set to 1 in all DIEs using
7594                the same abbrev entry.  */
7595             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7596           else
7597             size += 1;
7598           break;
7599         case dw_val_class_die_ref:
7600           if (AT_ref_external (a))
7601             {
7602               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7603                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7604                  is sized by target address length, whereas in DWARF3
7605                  it's always sized as an offset.  */
7606               if (use_debug_types)
7607                 size += DWARF_TYPE_SIGNATURE_SIZE;
7608               else if (dwarf_version == 2)
7609                 size += DWARF2_ADDR_SIZE;
7610               else
7611                 size += DWARF_OFFSET_SIZE;
7612             }
7613           else
7614             size += DWARF_OFFSET_SIZE;
7615           break;
7616         case dw_val_class_fde_ref:
7617           size += DWARF_OFFSET_SIZE;
7618           break;
7619         case dw_val_class_lbl_id:
7620           size += DWARF2_ADDR_SIZE;
7621           break;
7622         case dw_val_class_lineptr:
7623         case dw_val_class_macptr:
7624           size += DWARF_OFFSET_SIZE;
7625           break;
7626         case dw_val_class_str:
7627           if (AT_string_form (a) == DW_FORM_strp)
7628             size += DWARF_OFFSET_SIZE;
7629           else
7630             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7631           break;
7632         case dw_val_class_file:
7633           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7634           break;
7635         case dw_val_class_data8:
7636           size += 8;
7637           break;
7638         case dw_val_class_vms_delta:
7639           size += DWARF_OFFSET_SIZE;
7640           break;
7641         default:
7642           gcc_unreachable ();
7643         }
7644     }
7645
7646   return size;
7647 }
7648
7649 /* Size the debugging information associated with a given DIE.  Visits the
7650    DIE's children recursively.  Updates the global variable next_die_offset, on
7651    each time through.  Uses the current value of next_die_offset to update the
7652    die_offset field in each DIE.  */
7653
7654 static void
7655 calc_die_sizes (dw_die_ref die)
7656 {
7657   dw_die_ref c;
7658
7659   gcc_assert (die->die_offset == 0
7660               || (unsigned long int) die->die_offset == next_die_offset);
7661   die->die_offset = next_die_offset;
7662   next_die_offset += size_of_die (die);
7663
7664   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7665
7666   if (die->die_child != NULL)
7667     /* Count the null byte used to terminate sibling lists.  */
7668     next_die_offset += 1;
7669 }
7670
7671 /* Size just the base type children at the start of the CU.
7672    This is needed because build_abbrev needs to size locs
7673    and sizing of type based stack ops needs to know die_offset
7674    values for the base types.  */
7675
7676 static void
7677 calc_base_type_die_sizes (void)
7678 {
7679   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7680   unsigned int i;
7681   dw_die_ref base_type;
7682 #if ENABLE_ASSERT_CHECKING
7683   dw_die_ref prev = comp_unit_die ()->die_child;
7684 #endif
7685
7686   die_offset += size_of_die (comp_unit_die ());
7687   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7688     {
7689 #if ENABLE_ASSERT_CHECKING
7690       gcc_assert (base_type->die_offset == 0
7691                   && prev->die_sib == base_type
7692                   && base_type->die_child == NULL
7693                   && base_type->die_abbrev);
7694       prev = base_type;
7695 #endif
7696       base_type->die_offset = die_offset;
7697       die_offset += size_of_die (base_type);
7698     }
7699 }
7700
7701 /* Set the marks for a die and its children.  We do this so
7702    that we know whether or not a reference needs to use FORM_ref_addr; only
7703    DIEs in the same CU will be marked.  We used to clear out the offset
7704    and use that as the flag, but ran into ordering problems.  */
7705
7706 static void
7707 mark_dies (dw_die_ref die)
7708 {
7709   dw_die_ref c;
7710
7711   gcc_assert (!die->die_mark);
7712
7713   die->die_mark = 1;
7714   FOR_EACH_CHILD (die, c, mark_dies (c));
7715 }
7716
7717 /* Clear the marks for a die and its children.  */
7718
7719 static void
7720 unmark_dies (dw_die_ref die)
7721 {
7722   dw_die_ref c;
7723
7724   if (! use_debug_types)
7725     gcc_assert (die->die_mark);
7726
7727   die->die_mark = 0;
7728   FOR_EACH_CHILD (die, c, unmark_dies (c));
7729 }
7730
7731 /* Clear the marks for a die, its children and referred dies.  */
7732
7733 static void
7734 unmark_all_dies (dw_die_ref die)
7735 {
7736   dw_die_ref c;
7737   dw_attr_ref a;
7738   unsigned ix;
7739
7740   if (!die->die_mark)
7741     return;
7742   die->die_mark = 0;
7743
7744   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7745
7746   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7747     if (AT_class (a) == dw_val_class_die_ref)
7748       unmark_all_dies (AT_ref (a));
7749 }
7750
7751 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7752    generated for the compilation unit.  */
7753
7754 static unsigned long
7755 size_of_pubnames (VEC (pubname_entry, gc) * names)
7756 {
7757   unsigned long size;
7758   unsigned i;
7759   pubname_ref p;
7760
7761   size = DWARF_PUBNAMES_HEADER_SIZE;
7762   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7763     if (names != pubtype_table
7764         || p->die->die_offset != 0
7765         || !flag_eliminate_unused_debug_types)
7766       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7767
7768   size += DWARF_OFFSET_SIZE;
7769   return size;
7770 }
7771
7772 /* Return the size of the information in the .debug_aranges section.  */
7773
7774 static unsigned long
7775 size_of_aranges (void)
7776 {
7777   unsigned long size;
7778
7779   size = DWARF_ARANGES_HEADER_SIZE;
7780
7781   /* Count the address/length pair for this compilation unit.  */
7782   if (text_section_used)
7783     size += 2 * DWARF2_ADDR_SIZE;
7784   if (cold_text_section_used)
7785     size += 2 * DWARF2_ADDR_SIZE;
7786   if (have_multiple_function_sections)
7787     {
7788       unsigned fde_idx;
7789       dw_fde_ref fde;
7790
7791       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7792         {
7793           if (!fde->in_std_section)
7794             size += 2 * DWARF2_ADDR_SIZE;
7795           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7796             size += 2 * DWARF2_ADDR_SIZE;
7797         }
7798     }
7799
7800   /* Count the two zero words used to terminated the address range table.  */
7801   size += 2 * DWARF2_ADDR_SIZE;
7802   return size;
7803 }
7804 \f
7805 /* Select the encoding of an attribute value.  */
7806
7807 static enum dwarf_form
7808 value_format (dw_attr_ref a)
7809 {
7810   switch (a->dw_attr_val.val_class)
7811     {
7812     case dw_val_class_addr:
7813       /* Only very few attributes allow DW_FORM_addr.  */
7814       switch (a->dw_attr)
7815         {
7816         case DW_AT_low_pc:
7817         case DW_AT_high_pc:
7818         case DW_AT_entry_pc:
7819         case DW_AT_trampoline:
7820           return DW_FORM_addr;
7821         default:
7822           break;
7823         }
7824       switch (DWARF2_ADDR_SIZE)
7825         {
7826         case 1:
7827           return DW_FORM_data1;
7828         case 2:
7829           return DW_FORM_data2;
7830         case 4:
7831           return DW_FORM_data4;
7832         case 8:
7833           return DW_FORM_data8;
7834         default:
7835           gcc_unreachable ();
7836         }
7837     case dw_val_class_range_list:
7838     case dw_val_class_loc_list:
7839       if (dwarf_version >= 4)
7840         return DW_FORM_sec_offset;
7841       /* FALLTHRU */
7842     case dw_val_class_vms_delta:
7843     case dw_val_class_offset:
7844       switch (DWARF_OFFSET_SIZE)
7845         {
7846         case 4:
7847           return DW_FORM_data4;
7848         case 8:
7849           return DW_FORM_data8;
7850         default:
7851           gcc_unreachable ();
7852         }
7853     case dw_val_class_loc:
7854       if (dwarf_version >= 4)
7855         return DW_FORM_exprloc;
7856       switch (constant_size (size_of_locs (AT_loc (a))))
7857         {
7858         case 1:
7859           return DW_FORM_block1;
7860         case 2:
7861           return DW_FORM_block2;
7862         default:
7863           gcc_unreachable ();
7864         }
7865     case dw_val_class_const:
7866       return DW_FORM_sdata;
7867     case dw_val_class_unsigned_const:
7868       switch (constant_size (AT_unsigned (a)))
7869         {
7870         case 1:
7871           return DW_FORM_data1;
7872         case 2:
7873           return DW_FORM_data2;
7874         case 4:
7875           return DW_FORM_data4;
7876         case 8:
7877           return DW_FORM_data8;
7878         default:
7879           gcc_unreachable ();
7880         }
7881     case dw_val_class_const_double:
7882       switch (HOST_BITS_PER_WIDE_INT)
7883         {
7884         case 8:
7885           return DW_FORM_data2;
7886         case 16:
7887           return DW_FORM_data4;
7888         case 32:
7889           return DW_FORM_data8;
7890         case 64:
7891         default:
7892           return DW_FORM_block1;
7893         }
7894     case dw_val_class_vec:
7895       switch (constant_size (a->dw_attr_val.v.val_vec.length
7896                              * a->dw_attr_val.v.val_vec.elt_size))
7897         {
7898         case 1:
7899           return DW_FORM_block1;
7900         case 2:
7901           return DW_FORM_block2;
7902         case 4:
7903           return DW_FORM_block4;
7904         default:
7905           gcc_unreachable ();
7906         }
7907     case dw_val_class_flag:
7908       if (dwarf_version >= 4)
7909         {
7910           /* Currently all add_AT_flag calls pass in 1 as last argument,
7911              so DW_FORM_flag_present can be used.  If that ever changes,
7912              we'll need to use DW_FORM_flag and have some optimization
7913              in build_abbrev_table that will change those to
7914              DW_FORM_flag_present if it is set to 1 in all DIEs using
7915              the same abbrev entry.  */
7916           gcc_assert (a->dw_attr_val.v.val_flag == 1);
7917           return DW_FORM_flag_present;
7918         }
7919       return DW_FORM_flag;
7920     case dw_val_class_die_ref:
7921       if (AT_ref_external (a))
7922         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7923       else
7924         return DW_FORM_ref;
7925     case dw_val_class_fde_ref:
7926       return DW_FORM_data;
7927     case dw_val_class_lbl_id:
7928       return DW_FORM_addr;
7929     case dw_val_class_lineptr:
7930     case dw_val_class_macptr:
7931       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7932     case dw_val_class_str:
7933       return AT_string_form (a);
7934     case dw_val_class_file:
7935       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7936         {
7937         case 1:
7938           return DW_FORM_data1;
7939         case 2:
7940           return DW_FORM_data2;
7941         case 4:
7942           return DW_FORM_data4;
7943         default:
7944           gcc_unreachable ();
7945         }
7946
7947     case dw_val_class_data8:
7948       return DW_FORM_data8;
7949
7950     default:
7951       gcc_unreachable ();
7952     }
7953 }
7954
7955 /* Output the encoding of an attribute value.  */
7956
7957 static void
7958 output_value_format (dw_attr_ref a)
7959 {
7960   enum dwarf_form form = value_format (a);
7961
7962   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7963 }
7964
7965 /* Output the .debug_abbrev section which defines the DIE abbreviation
7966    table.  */
7967
7968 static void
7969 output_abbrev_section (void)
7970 {
7971   unsigned long abbrev_id;
7972
7973   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7974     {
7975       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7976       unsigned ix;
7977       dw_attr_ref a_attr;
7978
7979       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7980       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7981                                    dwarf_tag_name (abbrev->die_tag));
7982
7983       if (abbrev->die_child != NULL)
7984         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7985       else
7986         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7987
7988       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7989            ix++)
7990         {
7991           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7992                                        dwarf_attr_name (a_attr->dw_attr));
7993           output_value_format (a_attr);
7994         }
7995
7996       dw2_asm_output_data (1, 0, NULL);
7997       dw2_asm_output_data (1, 0, NULL);
7998     }
7999
8000   /* Terminate the table.  */
8001   dw2_asm_output_data (1, 0, NULL);
8002 }
8003
8004 /* Output a symbol we can use to refer to this DIE from another CU.  */
8005
8006 static inline void
8007 output_die_symbol (dw_die_ref die)
8008 {
8009   char *sym = die->die_id.die_symbol;
8010
8011   if (sym == 0)
8012     return;
8013
8014   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8015     /* We make these global, not weak; if the target doesn't support
8016        .linkonce, it doesn't support combining the sections, so debugging
8017        will break.  */
8018     targetm.asm_out.globalize_label (asm_out_file, sym);
8019
8020   ASM_OUTPUT_LABEL (asm_out_file, sym);
8021 }
8022
8023 /* Return a new location list, given the begin and end range, and the
8024    expression.  */
8025
8026 static inline dw_loc_list_ref
8027 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8028               const char *section)
8029 {
8030   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8031
8032   retlist->begin = begin;
8033   retlist->end = end;
8034   retlist->expr = expr;
8035   retlist->section = section;
8036
8037   return retlist;
8038 }
8039
8040 /* Generate a new internal symbol for this location list node, if it
8041    hasn't got one yet.  */
8042
8043 static inline void
8044 gen_llsym (dw_loc_list_ref list)
8045 {
8046   gcc_assert (!list->ll_symbol);
8047   list->ll_symbol = gen_internal_sym ("LLST");
8048 }
8049
8050 /* Output the location list given to us.  */
8051
8052 static void
8053 output_loc_list (dw_loc_list_ref list_head)
8054 {
8055   dw_loc_list_ref curr = list_head;
8056
8057   if (list_head->emitted)
8058     return;
8059   list_head->emitted = true;
8060
8061   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8062
8063   /* Walk the location list, and output each range + expression.  */
8064   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8065     {
8066       unsigned long size;
8067       /* Don't output an entry that starts and ends at the same address.  */
8068       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8069         continue;
8070       if (!have_multiple_function_sections)
8071         {
8072           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8073                                 "Location list begin address (%s)",
8074                                 list_head->ll_symbol);
8075           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8076                                 "Location list end address (%s)",
8077                                 list_head->ll_symbol);
8078         }
8079       else
8080         {
8081           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8082                                "Location list begin address (%s)",
8083                                list_head->ll_symbol);
8084           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8085                                "Location list end address (%s)",
8086                                list_head->ll_symbol);
8087         }
8088       size = size_of_locs (curr->expr);
8089
8090       /* Output the block length for this list of location operations.  */
8091       gcc_assert (size <= 0xffff);
8092       dw2_asm_output_data (2, size, "%s", "Location expression size");
8093
8094       output_loc_sequence (curr->expr, -1);
8095     }
8096
8097   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8098                        "Location list terminator begin (%s)",
8099                        list_head->ll_symbol);
8100   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8101                        "Location list terminator end (%s)",
8102                        list_head->ll_symbol);
8103 }
8104
8105 /* Output a type signature.  */
8106
8107 static inline void
8108 output_signature (const char *sig, const char *name)
8109 {
8110   int i;
8111
8112   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8113     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8114 }
8115
8116 /* Output the DIE and its attributes.  Called recursively to generate
8117    the definitions of each child DIE.  */
8118
8119 static void
8120 output_die (dw_die_ref die)
8121 {
8122   dw_attr_ref a;
8123   dw_die_ref c;
8124   unsigned long size;
8125   unsigned ix;
8126
8127   /* If someone in another CU might refer to us, set up a symbol for
8128      them to point to.  */
8129   if (! use_debug_types && die->die_id.die_symbol)
8130     output_die_symbol (die);
8131
8132   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8133                                (unsigned long)die->die_offset,
8134                                dwarf_tag_name (die->die_tag));
8135
8136   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8137     {
8138       const char *name = dwarf_attr_name (a->dw_attr);
8139
8140       switch (AT_class (a))
8141         {
8142         case dw_val_class_addr:
8143           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8144           break;
8145
8146         case dw_val_class_offset:
8147           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8148                                "%s", name);
8149           break;
8150
8151         case dw_val_class_range_list:
8152           {
8153             char *p = strchr (ranges_section_label, '\0');
8154
8155             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8156                      a->dw_attr_val.v.val_offset);
8157             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8158                                    debug_ranges_section, "%s", name);
8159             *p = '\0';
8160           }
8161           break;
8162
8163         case dw_val_class_loc:
8164           size = size_of_locs (AT_loc (a));
8165
8166           /* Output the block length for this list of location operations.  */
8167           if (dwarf_version >= 4)
8168             dw2_asm_output_data_uleb128 (size, "%s", name);
8169           else
8170             dw2_asm_output_data (constant_size (size), size, "%s", name);
8171
8172           output_loc_sequence (AT_loc (a), -1);
8173           break;
8174
8175         case dw_val_class_const:
8176           /* ??? It would be slightly more efficient to use a scheme like is
8177              used for unsigned constants below, but gdb 4.x does not sign
8178              extend.  Gdb 5.x does sign extend.  */
8179           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8180           break;
8181
8182         case dw_val_class_unsigned_const:
8183           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8184                                AT_unsigned (a), "%s", name);
8185           break;
8186
8187         case dw_val_class_const_double:
8188           {
8189             unsigned HOST_WIDE_INT first, second;
8190
8191             if (HOST_BITS_PER_WIDE_INT >= 64)
8192               dw2_asm_output_data (1,
8193                                    2 * HOST_BITS_PER_WIDE_INT
8194                                    / HOST_BITS_PER_CHAR,
8195                                    NULL);
8196
8197             if (WORDS_BIG_ENDIAN)
8198               {
8199                 first = a->dw_attr_val.v.val_double.high;
8200                 second = a->dw_attr_val.v.val_double.low;
8201               }
8202             else
8203               {
8204                 first = a->dw_attr_val.v.val_double.low;
8205                 second = a->dw_attr_val.v.val_double.high;
8206               }
8207
8208             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8209                                  first, name);
8210             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8211                                  second, NULL);
8212           }
8213           break;
8214
8215         case dw_val_class_vec:
8216           {
8217             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8218             unsigned int len = a->dw_attr_val.v.val_vec.length;
8219             unsigned int i;
8220             unsigned char *p;
8221
8222             dw2_asm_output_data (constant_size (len * elt_size),
8223                                  len * elt_size, "%s", name);
8224             if (elt_size > sizeof (HOST_WIDE_INT))
8225               {
8226                 elt_size /= 2;
8227                 len *= 2;
8228               }
8229             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8230                  i < len;
8231                  i++, p += elt_size)
8232               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8233                                    "fp or vector constant word %u", i);
8234             break;
8235           }
8236
8237         case dw_val_class_flag:
8238           if (dwarf_version >= 4)
8239             {
8240               /* Currently all add_AT_flag calls pass in 1 as last argument,
8241                  so DW_FORM_flag_present can be used.  If that ever changes,
8242                  we'll need to use DW_FORM_flag and have some optimization
8243                  in build_abbrev_table that will change those to
8244                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8245                  the same abbrev entry.  */
8246               gcc_assert (AT_flag (a) == 1);
8247               if (flag_debug_asm)
8248                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8249                          ASM_COMMENT_START, name);
8250               break;
8251             }
8252           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8253           break;
8254
8255         case dw_val_class_loc_list:
8256           {
8257             char *sym = AT_loc_list (a)->ll_symbol;
8258
8259             gcc_assert (sym);
8260             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8261                                    "%s", name);
8262           }
8263           break;
8264
8265         case dw_val_class_die_ref:
8266           if (AT_ref_external (a))
8267             {
8268               if (use_debug_types)
8269                 {
8270                   comdat_type_node_ref type_node =
8271                     AT_ref (a)->die_id.die_type_node;
8272
8273                   gcc_assert (type_node);
8274                   output_signature (type_node->signature, name);
8275                 }
8276               else
8277                 {
8278                   char *sym = AT_ref (a)->die_id.die_symbol;
8279                   int size;
8280
8281                   gcc_assert (sym);
8282                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8283                      length, whereas in DWARF3 it's always sized as an
8284                      offset.  */
8285                   if (dwarf_version == 2)
8286                     size = DWARF2_ADDR_SIZE;
8287                   else
8288                     size = DWARF_OFFSET_SIZE;
8289                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8290                                          name);
8291                 }
8292             }
8293           else
8294             {
8295               gcc_assert (AT_ref (a)->die_offset);
8296               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8297                                    "%s", name);
8298             }
8299           break;
8300
8301         case dw_val_class_fde_ref:
8302           {
8303             char l1[20];
8304
8305             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8306                                          a->dw_attr_val.v.val_fde_index * 2);
8307             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8308                                    "%s", name);
8309           }
8310           break;
8311
8312         case dw_val_class_vms_delta:
8313           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8314                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8315                                     "%s", name);
8316           break;
8317
8318         case dw_val_class_lbl_id:
8319           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8320           break;
8321
8322         case dw_val_class_lineptr:
8323           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8324                                  debug_line_section, "%s", name);
8325           break;
8326
8327         case dw_val_class_macptr:
8328           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8329                                  debug_macinfo_section, "%s", name);
8330           break;
8331
8332         case dw_val_class_str:
8333           if (AT_string_form (a) == DW_FORM_strp)
8334             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8335                                    a->dw_attr_val.v.val_str->label,
8336                                    debug_str_section,
8337                                    "%s: \"%s\"", name, AT_string (a));
8338           else
8339             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8340           break;
8341
8342         case dw_val_class_file:
8343           {
8344             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8345
8346             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8347                                  a->dw_attr_val.v.val_file->filename);
8348             break;
8349           }
8350
8351         case dw_val_class_data8:
8352           {
8353             int i;
8354
8355             for (i = 0; i < 8; i++)
8356               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8357                                    i == 0 ? "%s" : NULL, name);
8358             break;
8359           }
8360
8361         default:
8362           gcc_unreachable ();
8363         }
8364     }
8365
8366   FOR_EACH_CHILD (die, c, output_die (c));
8367
8368   /* Add null byte to terminate sibling list.  */
8369   if (die->die_child != NULL)
8370     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8371                          (unsigned long) die->die_offset);
8372 }
8373
8374 /* Output the compilation unit that appears at the beginning of the
8375    .debug_info section, and precedes the DIE descriptions.  */
8376
8377 static void
8378 output_compilation_unit_header (void)
8379 {
8380   int ver = dwarf_version;
8381
8382   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8383     dw2_asm_output_data (4, 0xffffffff,
8384       "Initial length escape value indicating 64-bit DWARF extension");
8385   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8386                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8387                        "Length of Compilation Unit Info");
8388   dw2_asm_output_data (2, ver, "DWARF version number");
8389   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8390                          debug_abbrev_section,
8391                          "Offset Into Abbrev. Section");
8392   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8393 }
8394
8395 /* Output the compilation unit DIE and its children.  */
8396
8397 static void
8398 output_comp_unit (dw_die_ref die, int output_if_empty)
8399 {
8400   const char *secname;
8401   char *oldsym, *tmp;
8402
8403   /* Unless we are outputting main CU, we may throw away empty ones.  */
8404   if (!output_if_empty && die->die_child == NULL)
8405     return;
8406
8407   /* Even if there are no children of this DIE, we must output the information
8408      about the compilation unit.  Otherwise, on an empty translation unit, we
8409      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8410      will then complain when examining the file.  First mark all the DIEs in
8411      this CU so we know which get local refs.  */
8412   mark_dies (die);
8413
8414   build_abbrev_table (die);
8415
8416   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8417   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8418   calc_die_sizes (die);
8419
8420   oldsym = die->die_id.die_symbol;
8421   if (oldsym)
8422     {
8423       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8424
8425       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8426       secname = tmp;
8427       die->die_id.die_symbol = NULL;
8428       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8429     }
8430   else
8431     {
8432       switch_to_section (debug_info_section);
8433       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8434       info_section_emitted = true;
8435     }
8436
8437   /* Output debugging information.  */
8438   output_compilation_unit_header ();
8439   output_die (die);
8440
8441   /* Leave the marks on the main CU, so we can check them in
8442      output_pubnames.  */
8443   if (oldsym)
8444     {
8445       unmark_dies (die);
8446       die->die_id.die_symbol = oldsym;
8447     }
8448 }
8449
8450 /* Output a comdat type unit DIE and its children.  */
8451
8452 static void
8453 output_comdat_type_unit (comdat_type_node *node)
8454 {
8455   const char *secname;
8456   char *tmp;
8457   int i;
8458 #if defined (OBJECT_FORMAT_ELF)
8459   tree comdat_key;
8460 #endif
8461
8462   /* First mark all the DIEs in this CU so we know which get local refs.  */
8463   mark_dies (node->root_die);
8464
8465   build_abbrev_table (node->root_die);
8466
8467   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8468   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8469   calc_die_sizes (node->root_die);
8470
8471 #if defined (OBJECT_FORMAT_ELF)
8472   secname = ".debug_types";
8473   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8474   sprintf (tmp, "wt.");
8475   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8476     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8477   comdat_key = get_identifier (tmp);
8478   targetm.asm_out.named_section (secname,
8479                                  SECTION_DEBUG | SECTION_LINKONCE,
8480                                  comdat_key);
8481 #else
8482   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8483   sprintf (tmp, ".gnu.linkonce.wt.");
8484   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8485     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8486   secname = tmp;
8487   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8488 #endif
8489
8490   /* Output debugging information.  */
8491   output_compilation_unit_header ();
8492   output_signature (node->signature, "Type Signature");
8493   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8494                        "Offset to Type DIE");
8495   output_die (node->root_die);
8496
8497   unmark_dies (node->root_die);
8498 }
8499
8500 /* Return the DWARF2/3 pubname associated with a decl.  */
8501
8502 static const char *
8503 dwarf2_name (tree decl, int scope)
8504 {
8505   if (DECL_NAMELESS (decl))
8506     return NULL;
8507   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8508 }
8509
8510 /* Add a new entry to .debug_pubnames if appropriate.  */
8511
8512 static void
8513 add_pubname_string (const char *str, dw_die_ref die)
8514 {
8515   if (targetm.want_debug_pub_sections)
8516     {
8517       pubname_entry e;
8518
8519       e.die = die;
8520       e.name = xstrdup (str);
8521       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8522     }
8523 }
8524
8525 static void
8526 add_pubname (tree decl, dw_die_ref die)
8527 {
8528   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8529     {
8530       const char *name = dwarf2_name (decl, 1);
8531       if (name)
8532         add_pubname_string (name, die);
8533     }
8534 }
8535
8536 /* Add a new entry to .debug_pubtypes if appropriate.  */
8537
8538 static void
8539 add_pubtype (tree decl, dw_die_ref die)
8540 {
8541   pubname_entry e;
8542
8543   if (!targetm.want_debug_pub_sections)
8544     return;
8545
8546   e.name = NULL;
8547   if ((TREE_PUBLIC (decl)
8548        || is_cu_die (die->die_parent))
8549       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8550     {
8551       e.die = die;
8552       if (TYPE_P (decl))
8553         {
8554           if (TYPE_NAME (decl))
8555             {
8556               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8557                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8558               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8559                        && DECL_NAME (TYPE_NAME (decl)))
8560                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8561               else
8562                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8563             }
8564         }
8565       else
8566         {
8567           e.name = dwarf2_name (decl, 1);
8568           if (e.name)
8569             e.name = xstrdup (e.name);
8570         }
8571
8572       /* If we don't have a name for the type, there's no point in adding
8573          it to the table.  */
8574       if (e.name && e.name[0] != '\0')
8575         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8576     }
8577 }
8578
8579 /* Output the public names table used to speed up access to externally
8580    visible names; or the public types table used to find type definitions.  */
8581
8582 static void
8583 output_pubnames (VEC (pubname_entry, gc) * names)
8584 {
8585   unsigned i;
8586   unsigned long pubnames_length = size_of_pubnames (names);
8587   pubname_ref pub;
8588
8589   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8590     dw2_asm_output_data (4, 0xffffffff,
8591       "Initial length escape value indicating 64-bit DWARF extension");
8592   if (names == pubname_table)
8593     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8594                          "Length of Public Names Info");
8595   else
8596     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8597                          "Length of Public Type Names Info");
8598   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8599   dw2_asm_output_data (2, 2, "DWARF Version");
8600   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8601                          debug_info_section,
8602                          "Offset of Compilation Unit Info");
8603   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8604                        "Compilation Unit Length");
8605
8606   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8607     {
8608       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8609       if (names == pubname_table)
8610         gcc_assert (pub->die->die_mark);
8611
8612       if (names != pubtype_table
8613           || pub->die->die_offset != 0
8614           || !flag_eliminate_unused_debug_types)
8615         {
8616           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8617                                "DIE offset");
8618
8619           dw2_asm_output_nstring (pub->name, -1, "external name");
8620         }
8621     }
8622
8623   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8624 }
8625
8626 /* Output the information that goes into the .debug_aranges table.
8627    Namely, define the beginning and ending address range of the
8628    text section generated for this compilation unit.  */
8629
8630 static void
8631 output_aranges (unsigned long aranges_length)
8632 {
8633   unsigned i;
8634
8635   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8636     dw2_asm_output_data (4, 0xffffffff,
8637       "Initial length escape value indicating 64-bit DWARF extension");
8638   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8639                        "Length of Address Ranges Info");
8640   /* Version number for aranges is still 2, even in DWARF3.  */
8641   dw2_asm_output_data (2, 2, "DWARF Version");
8642   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8643                          debug_info_section,
8644                          "Offset of Compilation Unit Info");
8645   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8646   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8647
8648   /* We need to align to twice the pointer size here.  */
8649   if (DWARF_ARANGES_PAD_SIZE)
8650     {
8651       /* Pad using a 2 byte words so that padding is correct for any
8652          pointer size.  */
8653       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8654                            2 * DWARF2_ADDR_SIZE);
8655       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8656         dw2_asm_output_data (2, 0, NULL);
8657     }
8658
8659   /* It is necessary not to output these entries if the sections were
8660      not used; if the sections were not used, the length will be 0 and
8661      the address may end up as 0 if the section is discarded by ld
8662      --gc-sections, leaving an invalid (0, 0) entry that can be
8663      confused with the terminator.  */
8664   if (text_section_used)
8665     {
8666       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8667       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8668                             text_section_label, "Length");
8669     }
8670   if (cold_text_section_used)
8671     {
8672       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8673                            "Address");
8674       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8675                             cold_text_section_label, "Length");
8676     }
8677
8678   if (have_multiple_function_sections)
8679     {
8680       unsigned fde_idx;
8681       dw_fde_ref fde;
8682
8683       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8684         {
8685           if (!fde->in_std_section)
8686             {
8687               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8688                                    "Address");
8689               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8690                                     fde->dw_fde_begin, "Length");
8691             }
8692           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8693             {
8694               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8695                                    "Address");
8696               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8697                                     fde->dw_fde_second_begin, "Length");
8698             }
8699         }
8700     }
8701
8702   /* Output the terminator words.  */
8703   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8704   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8705 }
8706
8707 /* Add a new entry to .debug_ranges.  Return the offset at which it
8708    was placed.  */
8709
8710 static unsigned int
8711 add_ranges_num (int num)
8712 {
8713   unsigned int in_use = ranges_table_in_use;
8714
8715   if (in_use == ranges_table_allocated)
8716     {
8717       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8718       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8719                                     ranges_table_allocated);
8720       memset (ranges_table + ranges_table_in_use, 0,
8721               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8722     }
8723
8724   ranges_table[in_use].num = num;
8725   ranges_table_in_use = in_use + 1;
8726
8727   return in_use * 2 * DWARF2_ADDR_SIZE;
8728 }
8729
8730 /* Add a new entry to .debug_ranges corresponding to a block, or a
8731    range terminator if BLOCK is NULL.  */
8732
8733 static unsigned int
8734 add_ranges (const_tree block)
8735 {
8736   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8737 }
8738
8739 /* Add a new entry to .debug_ranges corresponding to a pair of
8740    labels.  */
8741
8742 static void
8743 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8744                       bool *added)
8745 {
8746   unsigned int in_use = ranges_by_label_in_use;
8747   unsigned int offset;
8748
8749   if (in_use == ranges_by_label_allocated)
8750     {
8751       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8752       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8753                                        ranges_by_label,
8754                                        ranges_by_label_allocated);
8755       memset (ranges_by_label + ranges_by_label_in_use, 0,
8756               RANGES_TABLE_INCREMENT
8757               * sizeof (struct dw_ranges_by_label_struct));
8758     }
8759
8760   ranges_by_label[in_use].begin = begin;
8761   ranges_by_label[in_use].end = end;
8762   ranges_by_label_in_use = in_use + 1;
8763
8764   offset = add_ranges_num (-(int)in_use - 1);
8765   if (!*added)
8766     {
8767       add_AT_range_list (die, DW_AT_ranges, offset);
8768       *added = true;
8769     }
8770 }
8771
8772 static void
8773 output_ranges (void)
8774 {
8775   unsigned i;
8776   static const char *const start_fmt = "Offset %#x";
8777   const char *fmt = start_fmt;
8778
8779   for (i = 0; i < ranges_table_in_use; i++)
8780     {
8781       int block_num = ranges_table[i].num;
8782
8783       if (block_num > 0)
8784         {
8785           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8786           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8787
8788           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8789           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8790
8791           /* If all code is in the text section, then the compilation
8792              unit base address defaults to DW_AT_low_pc, which is the
8793              base of the text section.  */
8794           if (!have_multiple_function_sections)
8795             {
8796               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8797                                     text_section_label,
8798                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8799               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8800                                     text_section_label, NULL);
8801             }
8802
8803           /* Otherwise, the compilation unit base address is zero,
8804              which allows us to use absolute addresses, and not worry
8805              about whether the target supports cross-section
8806              arithmetic.  */
8807           else
8808             {
8809               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8810                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8811               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8812             }
8813
8814           fmt = NULL;
8815         }
8816
8817       /* Negative block_num stands for an index into ranges_by_label.  */
8818       else if (block_num < 0)
8819         {
8820           int lab_idx = - block_num - 1;
8821
8822           if (!have_multiple_function_sections)
8823             {
8824               gcc_unreachable ();
8825 #if 0
8826               /* If we ever use add_ranges_by_labels () for a single
8827                  function section, all we have to do is to take out
8828                  the #if 0 above.  */
8829               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8830                                     ranges_by_label[lab_idx].begin,
8831                                     text_section_label,
8832                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8833               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8834                                     ranges_by_label[lab_idx].end,
8835                                     text_section_label, NULL);
8836 #endif
8837             }
8838           else
8839             {
8840               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8841                                    ranges_by_label[lab_idx].begin,
8842                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8843               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8844                                    ranges_by_label[lab_idx].end,
8845                                    NULL);
8846             }
8847         }
8848       else
8849         {
8850           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8851           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8852           fmt = start_fmt;
8853         }
8854     }
8855 }
8856
8857 /* Data structure containing information about input files.  */
8858 struct file_info
8859 {
8860   const char *path;     /* Complete file name.  */
8861   const char *fname;    /* File name part.  */
8862   int length;           /* Length of entire string.  */
8863   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8864   int dir_idx;          /* Index in directory table.  */
8865 };
8866
8867 /* Data structure containing information about directories with source
8868    files.  */
8869 struct dir_info
8870 {
8871   const char *path;     /* Path including directory name.  */
8872   int length;           /* Path length.  */
8873   int prefix;           /* Index of directory entry which is a prefix.  */
8874   int count;            /* Number of files in this directory.  */
8875   int dir_idx;          /* Index of directory used as base.  */
8876 };
8877
8878 /* Callback function for file_info comparison.  We sort by looking at
8879    the directories in the path.  */
8880
8881 static int
8882 file_info_cmp (const void *p1, const void *p2)
8883 {
8884   const struct file_info *const s1 = (const struct file_info *) p1;
8885   const struct file_info *const s2 = (const struct file_info *) p2;
8886   const unsigned char *cp1;
8887   const unsigned char *cp2;
8888
8889   /* Take care of file names without directories.  We need to make sure that
8890      we return consistent values to qsort since some will get confused if
8891      we return the same value when identical operands are passed in opposite
8892      orders.  So if neither has a directory, return 0 and otherwise return
8893      1 or -1 depending on which one has the directory.  */
8894   if ((s1->path == s1->fname || s2->path == s2->fname))
8895     return (s2->path == s2->fname) - (s1->path == s1->fname);
8896
8897   cp1 = (const unsigned char *) s1->path;
8898   cp2 = (const unsigned char *) s2->path;
8899
8900   while (1)
8901     {
8902       ++cp1;
8903       ++cp2;
8904       /* Reached the end of the first path?  If so, handle like above.  */
8905       if ((cp1 == (const unsigned char *) s1->fname)
8906           || (cp2 == (const unsigned char *) s2->fname))
8907         return ((cp2 == (const unsigned char *) s2->fname)
8908                 - (cp1 == (const unsigned char *) s1->fname));
8909
8910       /* Character of current path component the same?  */
8911       else if (*cp1 != *cp2)
8912         return *cp1 - *cp2;
8913     }
8914 }
8915
8916 struct file_name_acquire_data
8917 {
8918   struct file_info *files;
8919   int used_files;
8920   int max_files;
8921 };
8922
8923 /* Traversal function for the hash table.  */
8924
8925 static int
8926 file_name_acquire (void ** slot, void *data)
8927 {
8928   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8929   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8930   struct file_info *fi;
8931   const char *f;
8932
8933   gcc_assert (fnad->max_files >= d->emitted_number);
8934
8935   if (! d->emitted_number)
8936     return 1;
8937
8938   gcc_assert (fnad->max_files != fnad->used_files);
8939
8940   fi = fnad->files + fnad->used_files++;
8941
8942   /* Skip all leading "./".  */
8943   f = d->filename;
8944   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8945     f += 2;
8946
8947   /* Create a new array entry.  */
8948   fi->path = f;
8949   fi->length = strlen (f);
8950   fi->file_idx = d;
8951
8952   /* Search for the file name part.  */
8953   f = strrchr (f, DIR_SEPARATOR);
8954 #if defined (DIR_SEPARATOR_2)
8955   {
8956     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8957
8958     if (g != NULL)
8959       {
8960         if (f == NULL || f < g)
8961           f = g;
8962       }
8963   }
8964 #endif
8965
8966   fi->fname = f == NULL ? fi->path : f + 1;
8967   return 1;
8968 }
8969
8970 /* Output the directory table and the file name table.  We try to minimize
8971    the total amount of memory needed.  A heuristic is used to avoid large
8972    slowdowns with many input files.  */
8973
8974 static void
8975 output_file_names (void)
8976 {
8977   struct file_name_acquire_data fnad;
8978   int numfiles;
8979   struct file_info *files;
8980   struct dir_info *dirs;
8981   int *saved;
8982   int *savehere;
8983   int *backmap;
8984   int ndirs;
8985   int idx_offset;
8986   int i;
8987
8988   if (!last_emitted_file)
8989     {
8990       dw2_asm_output_data (1, 0, "End directory table");
8991       dw2_asm_output_data (1, 0, "End file name table");
8992       return;
8993     }
8994
8995   numfiles = last_emitted_file->emitted_number;
8996
8997   /* Allocate the various arrays we need.  */
8998   files = XALLOCAVEC (struct file_info, numfiles);
8999   dirs = XALLOCAVEC (struct dir_info, numfiles);
9000
9001   fnad.files = files;
9002   fnad.used_files = 0;
9003   fnad.max_files = numfiles;
9004   htab_traverse (file_table, file_name_acquire, &fnad);
9005   gcc_assert (fnad.used_files == fnad.max_files);
9006
9007   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9008
9009   /* Find all the different directories used.  */
9010   dirs[0].path = files[0].path;
9011   dirs[0].length = files[0].fname - files[0].path;
9012   dirs[0].prefix = -1;
9013   dirs[0].count = 1;
9014   dirs[0].dir_idx = 0;
9015   files[0].dir_idx = 0;
9016   ndirs = 1;
9017
9018   for (i = 1; i < numfiles; i++)
9019     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9020         && memcmp (dirs[ndirs - 1].path, files[i].path,
9021                    dirs[ndirs - 1].length) == 0)
9022       {
9023         /* Same directory as last entry.  */
9024         files[i].dir_idx = ndirs - 1;
9025         ++dirs[ndirs - 1].count;
9026       }
9027     else
9028       {
9029         int j;
9030
9031         /* This is a new directory.  */
9032         dirs[ndirs].path = files[i].path;
9033         dirs[ndirs].length = files[i].fname - files[i].path;
9034         dirs[ndirs].count = 1;
9035         dirs[ndirs].dir_idx = ndirs;
9036         files[i].dir_idx = ndirs;
9037
9038         /* Search for a prefix.  */
9039         dirs[ndirs].prefix = -1;
9040         for (j = 0; j < ndirs; j++)
9041           if (dirs[j].length < dirs[ndirs].length
9042               && dirs[j].length > 1
9043               && (dirs[ndirs].prefix == -1
9044                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9045               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9046             dirs[ndirs].prefix = j;
9047
9048         ++ndirs;
9049       }
9050
9051   /* Now to the actual work.  We have to find a subset of the directories which
9052      allow expressing the file name using references to the directory table
9053      with the least amount of characters.  We do not do an exhaustive search
9054      where we would have to check out every combination of every single
9055      possible prefix.  Instead we use a heuristic which provides nearly optimal
9056      results in most cases and never is much off.  */
9057   saved = XALLOCAVEC (int, ndirs);
9058   savehere = XALLOCAVEC (int, ndirs);
9059
9060   memset (saved, '\0', ndirs * sizeof (saved[0]));
9061   for (i = 0; i < ndirs; i++)
9062     {
9063       int j;
9064       int total;
9065
9066       /* We can always save some space for the current directory.  But this
9067          does not mean it will be enough to justify adding the directory.  */
9068       savehere[i] = dirs[i].length;
9069       total = (savehere[i] - saved[i]) * dirs[i].count;
9070
9071       for (j = i + 1; j < ndirs; j++)
9072         {
9073           savehere[j] = 0;
9074           if (saved[j] < dirs[i].length)
9075             {
9076               /* Determine whether the dirs[i] path is a prefix of the
9077                  dirs[j] path.  */
9078               int k;
9079
9080               k = dirs[j].prefix;
9081               while (k != -1 && k != (int) i)
9082                 k = dirs[k].prefix;
9083
9084               if (k == (int) i)
9085                 {
9086                   /* Yes it is.  We can possibly save some memory by
9087                      writing the filenames in dirs[j] relative to
9088                      dirs[i].  */
9089                   savehere[j] = dirs[i].length;
9090                   total += (savehere[j] - saved[j]) * dirs[j].count;
9091                 }
9092             }
9093         }
9094
9095       /* Check whether we can save enough to justify adding the dirs[i]
9096          directory.  */
9097       if (total > dirs[i].length + 1)
9098         {
9099           /* It's worthwhile adding.  */
9100           for (j = i; j < ndirs; j++)
9101             if (savehere[j] > 0)
9102               {
9103                 /* Remember how much we saved for this directory so far.  */
9104                 saved[j] = savehere[j];
9105
9106                 /* Remember the prefix directory.  */
9107                 dirs[j].dir_idx = i;
9108               }
9109         }
9110     }
9111
9112   /* Emit the directory name table.  */
9113   idx_offset = dirs[0].length > 0 ? 1 : 0;
9114   for (i = 1 - idx_offset; i < ndirs; i++)
9115     dw2_asm_output_nstring (dirs[i].path,
9116                             dirs[i].length
9117                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9118                             "Directory Entry: %#x", i + idx_offset);
9119
9120   dw2_asm_output_data (1, 0, "End directory table");
9121
9122   /* We have to emit them in the order of emitted_number since that's
9123      used in the debug info generation.  To do this efficiently we
9124      generate a back-mapping of the indices first.  */
9125   backmap = XALLOCAVEC (int, numfiles);
9126   for (i = 0; i < numfiles; i++)
9127     backmap[files[i].file_idx->emitted_number - 1] = i;
9128
9129   /* Now write all the file names.  */
9130   for (i = 0; i < numfiles; i++)
9131     {
9132       int file_idx = backmap[i];
9133       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9134
9135 #ifdef VMS_DEBUGGING_INFO
9136 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9137
9138       /* Setting these fields can lead to debugger miscomparisons,
9139          but VMS Debug requires them to be set correctly.  */
9140
9141       int ver;
9142       long long cdt;
9143       long siz;
9144       int maxfilelen = strlen (files[file_idx].path)
9145                                + dirs[dir_idx].length
9146                                + MAX_VMS_VERSION_LEN + 1;
9147       char *filebuf = XALLOCAVEC (char, maxfilelen);
9148
9149       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9150       snprintf (filebuf, maxfilelen, "%s;%d",
9151                 files[file_idx].path + dirs[dir_idx].length, ver);
9152
9153       dw2_asm_output_nstring
9154         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9155
9156       /* Include directory index.  */
9157       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9158
9159       /* Modification time.  */
9160       dw2_asm_output_data_uleb128
9161         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9162           ? cdt : 0,
9163          NULL);
9164
9165       /* File length in bytes.  */
9166       dw2_asm_output_data_uleb128
9167         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9168           ? siz : 0,
9169          NULL);
9170 #else
9171       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9172                               "File Entry: %#x", (unsigned) i + 1);
9173
9174       /* Include directory index.  */
9175       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9176
9177       /* Modification time.  */
9178       dw2_asm_output_data_uleb128 (0, NULL);
9179
9180       /* File length in bytes.  */
9181       dw2_asm_output_data_uleb128 (0, NULL);
9182 #endif /* VMS_DEBUGGING_INFO */
9183     }
9184
9185   dw2_asm_output_data (1, 0, "End file name table");
9186 }
9187
9188
9189 /* Output one line number table into the .debug_line section.  */
9190
9191 static void
9192 output_one_line_info_table (dw_line_info_table *table)
9193 {
9194   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9195   unsigned int current_line = 1;
9196   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9197   dw_line_info_entry *ent;
9198   size_t i;
9199
9200   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9201     {
9202       switch (ent->opcode)
9203         {
9204         case LI_set_address:
9205           /* ??? Unfortunately, we have little choice here currently, and
9206              must always use the most general form.  GCC does not know the
9207              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9208              ports do have length attributes which will give an upper bound
9209              on the address range.  We could perhaps use length attributes
9210              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9211           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9212
9213           /* This can handle any delta.  This takes
9214              4+DWARF2_ADDR_SIZE bytes.  */
9215           dw2_asm_output_data (1, 0, "set address %s", line_label);
9216           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9217           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9218           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9219           break;
9220
9221         case LI_set_line:
9222           if (ent->val == current_line)
9223             {
9224               /* We still need to start a new row, so output a copy insn.  */
9225               dw2_asm_output_data (1, DW_LNS_copy,
9226                                    "copy line %u", current_line);
9227             }
9228           else
9229             {
9230               int line_offset = ent->val - current_line;
9231               int line_delta = line_offset - DWARF_LINE_BASE;
9232
9233               current_line = ent->val;
9234               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9235                 {
9236                   /* This can handle deltas from -10 to 234, using the current
9237                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9238                      This takes 1 byte.  */
9239                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9240                                        "line %u", current_line);
9241                 }
9242               else
9243                 {
9244                   /* This can handle any delta.  This takes at least 4 bytes,
9245                      depending on the value being encoded.  */
9246                   dw2_asm_output_data (1, DW_LNS_advance_line,
9247                                        "advance to line %u", current_line);
9248                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9249                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9250                 }
9251             }
9252           break;
9253
9254         case LI_set_file:
9255           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9256           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9257           break;
9258
9259         case LI_set_column:
9260           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9261           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9262           break;
9263
9264         case LI_negate_stmt:
9265           current_is_stmt = !current_is_stmt;
9266           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9267                                "is_stmt %d", current_is_stmt);
9268           break;
9269
9270         case LI_set_prologue_end:
9271           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9272                                "set prologue end");
9273           break;
9274           
9275         case LI_set_epilogue_begin:
9276           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9277                                "set epilogue begin");
9278           break;
9279
9280         case LI_set_discriminator:
9281           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9282           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9283           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9284           dw2_asm_output_data_uleb128 (ent->val, NULL);
9285           break;
9286         }
9287     }
9288
9289   /* Emit debug info for the address of the end of the table.  */
9290   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9291   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9292   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9293   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9294
9295   dw2_asm_output_data (1, 0, "end sequence");
9296   dw2_asm_output_data_uleb128 (1, NULL);
9297   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9298 }
9299
9300 /* Output the source line number correspondence information.  This
9301    information goes into the .debug_line section.  */
9302
9303 static void
9304 output_line_info (void)
9305 {
9306   char l1[20], l2[20], p1[20], p2[20];
9307   int ver = dwarf_version;
9308   bool saw_one = false;
9309   int opc;
9310
9311   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9312   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9313   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9314   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9315
9316   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9317     dw2_asm_output_data (4, 0xffffffff,
9318       "Initial length escape value indicating 64-bit DWARF extension");
9319   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9320                         "Length of Source Line Info");
9321   ASM_OUTPUT_LABEL (asm_out_file, l1);
9322
9323   dw2_asm_output_data (2, ver, "DWARF Version");
9324   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9325   ASM_OUTPUT_LABEL (asm_out_file, p1);
9326
9327   /* Define the architecture-dependent minimum instruction length (in bytes).
9328      In this implementation of DWARF, this field is used for information
9329      purposes only.  Since GCC generates assembly language, we have no
9330      a priori knowledge of how many instruction bytes are generated for each
9331      source line, and therefore can use only the DW_LNE_set_address and
9332      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9333      this as '1', which is "correct enough" for all architectures,
9334      and don't let the target override.  */
9335   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9336
9337   if (ver >= 4)
9338     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9339                          "Maximum Operations Per Instruction");
9340   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9341                        "Default is_stmt_start flag");
9342   dw2_asm_output_data (1, DWARF_LINE_BASE,
9343                        "Line Base Value (Special Opcodes)");
9344   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9345                        "Line Range Value (Special Opcodes)");
9346   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9347                        "Special Opcode Base");
9348
9349   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9350     {
9351       int n_op_args;
9352       switch (opc)
9353         {
9354         case DW_LNS_advance_pc:
9355         case DW_LNS_advance_line:
9356         case DW_LNS_set_file:
9357         case DW_LNS_set_column:
9358         case DW_LNS_fixed_advance_pc:
9359         case DW_LNS_set_isa:
9360           n_op_args = 1;
9361           break;
9362         default:
9363           n_op_args = 0;
9364           break;
9365         }
9366
9367       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9368                            opc, n_op_args);
9369     }
9370
9371   /* Write out the information about the files we use.  */
9372   output_file_names ();
9373   ASM_OUTPUT_LABEL (asm_out_file, p2);
9374
9375   if (separate_line_info)
9376     {
9377       dw_line_info_table *table;
9378       size_t i;
9379
9380       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9381         if (table->in_use)
9382           {
9383             output_one_line_info_table (table);
9384             saw_one = true;
9385           }
9386     }
9387   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9388     {
9389       output_one_line_info_table (cold_text_section_line_info);
9390       saw_one = true;
9391     }
9392
9393   /* ??? Some Darwin linkers crash on a .debug_line section with no
9394      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9395      sufficient -- the address column must also be initialized.
9396      Make sure to output at least one set_address/end_sequence pair,
9397      choosing .text since that section is always present.  */
9398   if (text_section_line_info->in_use || !saw_one)
9399     output_one_line_info_table (text_section_line_info);
9400
9401   /* Output the marker for the end of the line number info.  */
9402   ASM_OUTPUT_LABEL (asm_out_file, l2);
9403 }
9404 \f
9405 /* Given a pointer to a tree node for some base type, return a pointer to
9406    a DIE that describes the given type.
9407
9408    This routine must only be called for GCC type nodes that correspond to
9409    Dwarf base (fundamental) types.  */
9410
9411 static dw_die_ref
9412 base_type_die (tree type)
9413 {
9414   dw_die_ref base_type_result;
9415   enum dwarf_type encoding;
9416
9417   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9418     return 0;
9419
9420   /* If this is a subtype that should not be emitted as a subrange type,
9421      use the base type.  See subrange_type_for_debug_p.  */
9422   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9423     type = TREE_TYPE (type);
9424
9425   switch (TREE_CODE (type))
9426     {
9427     case INTEGER_TYPE:
9428       if ((dwarf_version >= 4 || !dwarf_strict)
9429           && TYPE_NAME (type)
9430           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9431           && DECL_IS_BUILTIN (TYPE_NAME (type))
9432           && DECL_NAME (TYPE_NAME (type)))
9433         {
9434           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9435           if (strcmp (name, "char16_t") == 0
9436               || strcmp (name, "char32_t") == 0)
9437             {
9438               encoding = DW_ATE_UTF;
9439               break;
9440             }
9441         }
9442       if (TYPE_STRING_FLAG (type))
9443         {
9444           if (TYPE_UNSIGNED (type))
9445             encoding = DW_ATE_unsigned_char;
9446           else
9447             encoding = DW_ATE_signed_char;
9448         }
9449       else if (TYPE_UNSIGNED (type))
9450         encoding = DW_ATE_unsigned;
9451       else
9452         encoding = DW_ATE_signed;
9453       break;
9454
9455     case REAL_TYPE:
9456       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9457         {
9458           if (dwarf_version >= 3 || !dwarf_strict)
9459             encoding = DW_ATE_decimal_float;
9460           else
9461             encoding = DW_ATE_lo_user;
9462         }
9463       else
9464         encoding = DW_ATE_float;
9465       break;
9466
9467     case FIXED_POINT_TYPE:
9468       if (!(dwarf_version >= 3 || !dwarf_strict))
9469         encoding = DW_ATE_lo_user;
9470       else if (TYPE_UNSIGNED (type))
9471         encoding = DW_ATE_unsigned_fixed;
9472       else
9473         encoding = DW_ATE_signed_fixed;
9474       break;
9475
9476       /* Dwarf2 doesn't know anything about complex ints, so use
9477          a user defined type for it.  */
9478     case COMPLEX_TYPE:
9479       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9480         encoding = DW_ATE_complex_float;
9481       else
9482         encoding = DW_ATE_lo_user;
9483       break;
9484
9485     case BOOLEAN_TYPE:
9486       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9487       encoding = DW_ATE_boolean;
9488       break;
9489
9490     default:
9491       /* No other TREE_CODEs are Dwarf fundamental types.  */
9492       gcc_unreachable ();
9493     }
9494
9495   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9496
9497   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9498                    int_size_in_bytes (type));
9499   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9500
9501   return base_type_result;
9502 }
9503
9504 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9505    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9506
9507 static inline int
9508 is_base_type (tree type)
9509 {
9510   switch (TREE_CODE (type))
9511     {
9512     case ERROR_MARK:
9513     case VOID_TYPE:
9514     case INTEGER_TYPE:
9515     case REAL_TYPE:
9516     case FIXED_POINT_TYPE:
9517     case COMPLEX_TYPE:
9518     case BOOLEAN_TYPE:
9519       return 1;
9520
9521     case ARRAY_TYPE:
9522     case RECORD_TYPE:
9523     case UNION_TYPE:
9524     case QUAL_UNION_TYPE:
9525     case ENUMERAL_TYPE:
9526     case FUNCTION_TYPE:
9527     case METHOD_TYPE:
9528     case POINTER_TYPE:
9529     case REFERENCE_TYPE:
9530     case NULLPTR_TYPE:
9531     case OFFSET_TYPE:
9532     case LANG_TYPE:
9533     case VECTOR_TYPE:
9534       return 0;
9535
9536     default:
9537       gcc_unreachable ();
9538     }
9539
9540   return 0;
9541 }
9542
9543 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9544    node, return the size in bits for the type if it is a constant, or else
9545    return the alignment for the type if the type's size is not constant, or
9546    else return BITS_PER_WORD if the type actually turns out to be an
9547    ERROR_MARK node.  */
9548
9549 static inline unsigned HOST_WIDE_INT
9550 simple_type_size_in_bits (const_tree type)
9551 {
9552   if (TREE_CODE (type) == ERROR_MARK)
9553     return BITS_PER_WORD;
9554   else if (TYPE_SIZE (type) == NULL_TREE)
9555     return 0;
9556   else if (host_integerp (TYPE_SIZE (type), 1))
9557     return tree_low_cst (TYPE_SIZE (type), 1);
9558   else
9559     return TYPE_ALIGN (type);
9560 }
9561
9562 /* Similarly, but return a double_int instead of UHWI.  */
9563
9564 static inline double_int
9565 double_int_type_size_in_bits (const_tree type)
9566 {
9567   if (TREE_CODE (type) == ERROR_MARK)
9568     return uhwi_to_double_int (BITS_PER_WORD);
9569   else if (TYPE_SIZE (type) == NULL_TREE)
9570     return double_int_zero;
9571   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9572     return tree_to_double_int (TYPE_SIZE (type));
9573   else
9574     return uhwi_to_double_int (TYPE_ALIGN (type));
9575 }
9576
9577 /*  Given a pointer to a tree node for a subrange type, return a pointer
9578     to a DIE that describes the given type.  */
9579
9580 static dw_die_ref
9581 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9582 {
9583   dw_die_ref subrange_die;
9584   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9585
9586   if (context_die == NULL)
9587     context_die = comp_unit_die ();
9588
9589   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9590
9591   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9592     {
9593       /* The size of the subrange type and its base type do not match,
9594          so we need to generate a size attribute for the subrange type.  */
9595       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9596     }
9597
9598   if (low)
9599     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9600   if (high)
9601     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9602
9603   return subrange_die;
9604 }
9605
9606 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9607    entry that chains various modifiers in front of the given type.  */
9608
9609 static dw_die_ref
9610 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9611                    dw_die_ref context_die)
9612 {
9613   enum tree_code code = TREE_CODE (type);
9614   dw_die_ref mod_type_die;
9615   dw_die_ref sub_die = NULL;
9616   tree item_type = NULL;
9617   tree qualified_type;
9618   tree name, low, high;
9619
9620   if (code == ERROR_MARK)
9621     return NULL;
9622
9623   /* See if we already have the appropriately qualified variant of
9624      this type.  */
9625   qualified_type
9626     = get_qualified_type (type,
9627                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9628                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9629
9630   if (qualified_type == sizetype
9631       && TYPE_NAME (qualified_type)
9632       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9633     {
9634       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9635
9636       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9637                            && TYPE_PRECISION (t)
9638                            == TYPE_PRECISION (qualified_type)
9639                            && TYPE_UNSIGNED (t)
9640                            == TYPE_UNSIGNED (qualified_type));
9641       qualified_type = t;
9642     }
9643
9644   /* If we do, then we can just use its DIE, if it exists.  */
9645   if (qualified_type)
9646     {
9647       mod_type_die = lookup_type_die (qualified_type);
9648       if (mod_type_die)
9649         return mod_type_die;
9650     }
9651
9652   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9653
9654   /* Handle C typedef types.  */
9655   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9656       && !DECL_ARTIFICIAL (name))
9657     {
9658       tree dtype = TREE_TYPE (name);
9659
9660       if (qualified_type == dtype)
9661         {
9662           /* For a named type, use the typedef.  */
9663           gen_type_die (qualified_type, context_die);
9664           return lookup_type_die (qualified_type);
9665         }
9666       else if (is_const_type < TYPE_READONLY (dtype)
9667                || is_volatile_type < TYPE_VOLATILE (dtype)
9668                || (is_const_type <= TYPE_READONLY (dtype)
9669                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9670                    && DECL_ORIGINAL_TYPE (name) != type))
9671         /* cv-unqualified version of named type.  Just use the unnamed
9672            type to which it refers.  */
9673         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9674                                   is_const_type, is_volatile_type,
9675                                   context_die);
9676       /* Else cv-qualified version of named type; fall through.  */
9677     }
9678
9679   if (is_const_type
9680       /* If both is_const_type and is_volatile_type, prefer the path
9681          which leads to a qualified type.  */
9682       && (!is_volatile_type
9683           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9684           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9685     {
9686       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9687       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9688     }
9689   else if (is_volatile_type)
9690     {
9691       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9692       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9693     }
9694   else if (code == POINTER_TYPE)
9695     {
9696       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9697       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9698                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9699       item_type = TREE_TYPE (type);
9700       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9701         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9702                          TYPE_ADDR_SPACE (item_type));
9703     }
9704   else if (code == REFERENCE_TYPE)
9705     {
9706       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9707         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9708                                 type);
9709       else
9710         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9711       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9712                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9713       item_type = TREE_TYPE (type);
9714       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9715         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9716                          TYPE_ADDR_SPACE (item_type));
9717     }
9718   else if (code == INTEGER_TYPE
9719            && TREE_TYPE (type) != NULL_TREE
9720            && subrange_type_for_debug_p (type, &low, &high))
9721     {
9722       mod_type_die = subrange_type_die (type, low, high, context_die);
9723       item_type = TREE_TYPE (type);
9724     }
9725   else if (is_base_type (type))
9726     mod_type_die = base_type_die (type);
9727   else
9728     {
9729       gen_type_die (type, context_die);
9730
9731       /* We have to get the type_main_variant here (and pass that to the
9732          `lookup_type_die' routine) because the ..._TYPE node we have
9733          might simply be a *copy* of some original type node (where the
9734          copy was created to help us keep track of typedef names) and
9735          that copy might have a different TYPE_UID from the original
9736          ..._TYPE node.  */
9737       if (TREE_CODE (type) != VECTOR_TYPE)
9738         return lookup_type_die (type_main_variant (type));
9739       else
9740         /* Vectors have the debugging information in the type,
9741            not the main variant.  */
9742         return lookup_type_die (type);
9743     }
9744
9745   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9746      don't output a DW_TAG_typedef, since there isn't one in the
9747      user's program; just attach a DW_AT_name to the type.
9748      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9749      if the base type already has the same name.  */
9750   if (name
9751       && ((TREE_CODE (name) != TYPE_DECL
9752            && (qualified_type == TYPE_MAIN_VARIANT (type)
9753                || (!is_const_type && !is_volatile_type)))
9754           || (TREE_CODE (name) == TYPE_DECL
9755               && TREE_TYPE (name) == qualified_type
9756               && DECL_NAME (name))))
9757     {
9758       if (TREE_CODE (name) == TYPE_DECL)
9759         /* Could just call add_name_and_src_coords_attributes here,
9760            but since this is a builtin type it doesn't have any
9761            useful source coordinates anyway.  */
9762         name = DECL_NAME (name);
9763       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9764       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9765       if (TYPE_ARTIFICIAL (type))
9766         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9767     }
9768   /* This probably indicates a bug.  */
9769   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9770     add_name_attribute (mod_type_die, "__unknown__");
9771
9772   if (qualified_type)
9773     equate_type_number_to_die (qualified_type, mod_type_die);
9774
9775   if (item_type)
9776     /* We must do this after the equate_type_number_to_die call, in case
9777        this is a recursive type.  This ensures that the modified_type_die
9778        recursion will terminate even if the type is recursive.  Recursive
9779        types are possible in Ada.  */
9780     sub_die = modified_type_die (item_type,
9781                                  TYPE_READONLY (item_type),
9782                                  TYPE_VOLATILE (item_type),
9783                                  context_die);
9784
9785   if (sub_die != NULL)
9786     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9787
9788   return mod_type_die;
9789 }
9790
9791 /* Generate DIEs for the generic parameters of T.
9792    T must be either a generic type or a generic function.
9793    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9794
9795 static void
9796 gen_generic_params_dies (tree t)
9797 {
9798   tree parms, args;
9799   int parms_num, i;
9800   dw_die_ref die = NULL;
9801
9802   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9803     return;
9804
9805   if (TYPE_P (t))
9806     die = lookup_type_die (t);
9807   else if (DECL_P (t))
9808     die = lookup_decl_die (t);
9809
9810   gcc_assert (die);
9811
9812   parms = lang_hooks.get_innermost_generic_parms (t);
9813   if (!parms)
9814     /* T has no generic parameter. It means T is neither a generic type
9815        or function. End of story.  */
9816     return;
9817
9818   parms_num = TREE_VEC_LENGTH (parms);
9819   args = lang_hooks.get_innermost_generic_args (t);
9820   for (i = 0; i < parms_num; i++)
9821     {
9822       tree parm, arg, arg_pack_elems;
9823
9824       parm = TREE_VEC_ELT (parms, i);
9825       arg = TREE_VEC_ELT (args, i);
9826       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9827       gcc_assert (parm && TREE_VALUE (parm) && arg);
9828
9829       if (parm && TREE_VALUE (parm) && arg)
9830         {
9831           /* If PARM represents a template parameter pack,
9832              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9833              by DW_TAG_template_*_parameter DIEs for the argument
9834              pack elements of ARG. Note that ARG would then be
9835              an argument pack.  */
9836           if (arg_pack_elems)
9837             template_parameter_pack_die (TREE_VALUE (parm),
9838                                          arg_pack_elems,
9839                                          die);
9840           else
9841             generic_parameter_die (TREE_VALUE (parm), arg,
9842                                    true /* Emit DW_AT_name */, die);
9843         }
9844     }
9845 }
9846
9847 /* Create and return a DIE for PARM which should be
9848    the representation of a generic type parameter.
9849    For instance, in the C++ front end, PARM would be a template parameter.
9850    ARG is the argument to PARM.
9851    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9852    name of the PARM.
9853    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9854    as a child node.  */
9855
9856 static dw_die_ref
9857 generic_parameter_die (tree parm, tree arg,
9858                        bool emit_name_p,
9859                        dw_die_ref parent_die)
9860 {
9861   dw_die_ref tmpl_die = NULL;
9862   const char *name = NULL;
9863
9864   if (!parm || !DECL_NAME (parm) || !arg)
9865     return NULL;
9866
9867   /* We support non-type generic parameters and arguments,
9868      type generic parameters and arguments, as well as
9869      generic generic parameters (a.k.a. template template parameters in C++)
9870      and arguments.  */
9871   if (TREE_CODE (parm) == PARM_DECL)
9872     /* PARM is a nontype generic parameter  */
9873     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9874   else if (TREE_CODE (parm) == TYPE_DECL)
9875     /* PARM is a type generic parameter.  */
9876     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9877   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9878     /* PARM is a generic generic parameter.
9879        Its DIE is a GNU extension. It shall have a
9880        DW_AT_name attribute to represent the name of the template template
9881        parameter, and a DW_AT_GNU_template_name attribute to represent the
9882        name of the template template argument.  */
9883     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9884                         parent_die, parm);
9885   else
9886     gcc_unreachable ();
9887
9888   if (tmpl_die)
9889     {
9890       tree tmpl_type;
9891
9892       /* If PARM is a generic parameter pack, it means we are
9893          emitting debug info for a template argument pack element.
9894          In other terms, ARG is a template argument pack element.
9895          In that case, we don't emit any DW_AT_name attribute for
9896          the die.  */
9897       if (emit_name_p)
9898         {
9899           name = IDENTIFIER_POINTER (DECL_NAME (parm));
9900           gcc_assert (name);
9901           add_AT_string (tmpl_die, DW_AT_name, name);
9902         }
9903
9904       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9905         {
9906           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9907              TMPL_DIE should have a child DW_AT_type attribute that is set
9908              to the type of the argument to PARM, which is ARG.
9909              If PARM is a type generic parameter, TMPL_DIE should have a
9910              child DW_AT_type that is set to ARG.  */
9911           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9912           add_type_attribute (tmpl_die, tmpl_type, 0,
9913                               TREE_THIS_VOLATILE (tmpl_type),
9914                               parent_die);
9915         }
9916       else
9917         {
9918           /* So TMPL_DIE is a DIE representing a
9919              a generic generic template parameter, a.k.a template template
9920              parameter in C++ and arg is a template.  */
9921
9922           /* The DW_AT_GNU_template_name attribute of the DIE must be set
9923              to the name of the argument.  */
9924           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9925           if (name)
9926             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9927         }
9928
9929       if (TREE_CODE (parm) == PARM_DECL)
9930         /* So PARM is a non-type generic parameter.
9931            DWARF3 5.6.8 says we must set a DW_AT_const_value child
9932            attribute of TMPL_DIE which value represents the value
9933            of ARG.
9934            We must be careful here:
9935            The value of ARG might reference some function decls.
9936            We might currently be emitting debug info for a generic
9937            type and types are emitted before function decls, we don't
9938            know if the function decls referenced by ARG will actually be
9939            emitted after cgraph computations.
9940            So must defer the generation of the DW_AT_const_value to
9941            after cgraph is ready.  */
9942         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9943     }
9944
9945   return tmpl_die;
9946 }
9947
9948 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
9949    PARM_PACK must be a template parameter pack. The returned DIE
9950    will be child DIE of PARENT_DIE.  */
9951
9952 static dw_die_ref
9953 template_parameter_pack_die (tree parm_pack,
9954                              tree parm_pack_args,
9955                              dw_die_ref parent_die)
9956 {
9957   dw_die_ref die;
9958   int j;
9959
9960   gcc_assert (parent_die && parm_pack);
9961
9962   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9963   add_name_and_src_coords_attributes (die, parm_pack);
9964   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9965     generic_parameter_die (parm_pack,
9966                            TREE_VEC_ELT (parm_pack_args, j),
9967                            false /* Don't emit DW_AT_name */,
9968                            die);
9969   return die;
9970 }
9971
9972 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9973    an enumerated type.  */
9974
9975 static inline int
9976 type_is_enum (const_tree type)
9977 {
9978   return TREE_CODE (type) == ENUMERAL_TYPE;
9979 }
9980
9981 /* Return the DBX register number described by a given RTL node.  */
9982
9983 static unsigned int
9984 dbx_reg_number (const_rtx rtl)
9985 {
9986   unsigned regno = REGNO (rtl);
9987
9988   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9989
9990 #ifdef LEAF_REG_REMAP
9991   if (current_function_uses_only_leaf_regs)
9992     {
9993       int leaf_reg = LEAF_REG_REMAP (regno);
9994       if (leaf_reg != -1)
9995         regno = (unsigned) leaf_reg;
9996     }
9997 #endif
9998
9999   return DBX_REGISTER_NUMBER (regno);
10000 }
10001
10002 /* Optionally add a DW_OP_piece term to a location description expression.
10003    DW_OP_piece is only added if the location description expression already
10004    doesn't end with DW_OP_piece.  */
10005
10006 static void
10007 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10008 {
10009   dw_loc_descr_ref loc;
10010
10011   if (*list_head != NULL)
10012     {
10013       /* Find the end of the chain.  */
10014       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10015         ;
10016
10017       if (loc->dw_loc_opc != DW_OP_piece)
10018         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10019     }
10020 }
10021
10022 /* Return a location descriptor that designates a machine register or
10023    zero if there is none.  */
10024
10025 static dw_loc_descr_ref
10026 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10027 {
10028   rtx regs;
10029
10030   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10031     return 0;
10032
10033   /* We only use "frame base" when we're sure we're talking about the
10034      post-prologue local stack frame.  We do this by *not* running
10035      register elimination until this point, and recognizing the special
10036      argument pointer and soft frame pointer rtx's.
10037      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10038   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10039       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10040     {
10041       dw_loc_descr_ref result = NULL;
10042
10043       if (dwarf_version >= 4 || !dwarf_strict)
10044         {
10045           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10046                                        initialized);
10047           if (result)
10048             add_loc_descr (&result,
10049                            new_loc_descr (DW_OP_stack_value, 0, 0));
10050         }
10051       return result;
10052     }
10053
10054   regs = targetm.dwarf_register_span (rtl);
10055
10056   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10057     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10058   else
10059     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10060 }
10061
10062 /* Return a location descriptor that designates a machine register for
10063    a given hard register number.  */
10064
10065 static dw_loc_descr_ref
10066 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10067 {
10068   dw_loc_descr_ref reg_loc_descr;
10069
10070   if (regno <= 31)
10071     reg_loc_descr
10072       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10073   else
10074     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10075
10076   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10077     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10078
10079   return reg_loc_descr;
10080 }
10081
10082 /* Given an RTL of a register, return a location descriptor that
10083    designates a value that spans more than one register.  */
10084
10085 static dw_loc_descr_ref
10086 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10087                              enum var_init_status initialized)
10088 {
10089   int nregs, size, i;
10090   unsigned reg;
10091   dw_loc_descr_ref loc_result = NULL;
10092
10093   reg = REGNO (rtl);
10094 #ifdef LEAF_REG_REMAP
10095   if (current_function_uses_only_leaf_regs)
10096     {
10097       int leaf_reg = LEAF_REG_REMAP (reg);
10098       if (leaf_reg != -1)
10099         reg = (unsigned) leaf_reg;
10100     }
10101 #endif
10102   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10103   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10104
10105   /* Simple, contiguous registers.  */
10106   if (regs == NULL_RTX)
10107     {
10108       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10109
10110       loc_result = NULL;
10111       while (nregs--)
10112         {
10113           dw_loc_descr_ref t;
10114
10115           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10116                                       VAR_INIT_STATUS_INITIALIZED);
10117           add_loc_descr (&loc_result, t);
10118           add_loc_descr_op_piece (&loc_result, size);
10119           ++reg;
10120         }
10121       return loc_result;
10122     }
10123
10124   /* Now onto stupid register sets in non contiguous locations.  */
10125
10126   gcc_assert (GET_CODE (regs) == PARALLEL);
10127
10128   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10129   loc_result = NULL;
10130
10131   for (i = 0; i < XVECLEN (regs, 0); ++i)
10132     {
10133       dw_loc_descr_ref t;
10134
10135       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10136                                   VAR_INIT_STATUS_INITIALIZED);
10137       add_loc_descr (&loc_result, t);
10138       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10139       add_loc_descr_op_piece (&loc_result, size);
10140     }
10141
10142   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10143     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10144   return loc_result;
10145 }
10146
10147 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10148
10149 /* Return a location descriptor that designates a constant i,
10150    as a compound operation from constant (i >> shift), constant shift
10151    and DW_OP_shl.  */
10152
10153 static dw_loc_descr_ref
10154 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10155 {
10156   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10157   add_loc_descr (&ret, int_loc_descriptor (shift));
10158   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10159   return ret;
10160 }
10161
10162 /* Return a location descriptor that designates a constant.  */
10163
10164 static dw_loc_descr_ref
10165 int_loc_descriptor (HOST_WIDE_INT i)
10166 {
10167   enum dwarf_location_atom op;
10168
10169   /* Pick the smallest representation of a constant, rather than just
10170      defaulting to the LEB encoding.  */
10171   if (i >= 0)
10172     {
10173       int clz = clz_hwi (i);
10174       int ctz = ctz_hwi (i);
10175       if (i <= 31)
10176         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10177       else if (i <= 0xff)
10178         op = DW_OP_const1u;
10179       else if (i <= 0xffff)
10180         op = DW_OP_const2u;
10181       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10182                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10183         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10184            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10185            while DW_OP_const4u is 5 bytes.  */
10186         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10187       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10188                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10189         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10190            while DW_OP_const4u is 5 bytes.  */
10191         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10192       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10193         op = DW_OP_const4u;
10194       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10195                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10196         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10197            while DW_OP_constu of constant >= 0x100000000 takes at least
10198            6 bytes.  */
10199         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10200       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10201                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10202                   >= HOST_BITS_PER_WIDE_INT)
10203         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10204            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10205            while DW_OP_constu takes in this case at least 6 bytes.  */
10206         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10207       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10208                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10209                && size_of_uleb128 (i) > 6)
10210         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10211         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10212       else
10213         op = DW_OP_constu;
10214     }
10215   else
10216     {
10217       if (i >= -0x80)
10218         op = DW_OP_const1s;
10219       else if (i >= -0x8000)
10220         op = DW_OP_const2s;
10221       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10222         {
10223           if (size_of_int_loc_descriptor (i) < 5)
10224             {
10225               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10226               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10227               return ret;
10228             }
10229           op = DW_OP_const4s;
10230         }
10231       else
10232         {
10233           if (size_of_int_loc_descriptor (i)
10234               < (unsigned long) 1 + size_of_sleb128 (i))
10235             {
10236               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10237               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10238               return ret;
10239             }
10240           op = DW_OP_consts;
10241         }
10242     }
10243
10244   return new_loc_descr (op, i, 0);
10245 }
10246
10247 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10248    without actually allocating it.  */
10249
10250 static unsigned long
10251 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10252 {
10253   return size_of_int_loc_descriptor (i >> shift)
10254          + size_of_int_loc_descriptor (shift)
10255          + 1;
10256 }
10257
10258 /* Return size_of_locs (int_loc_descriptor (i)) without
10259    actually allocating it.  */
10260
10261 static unsigned long
10262 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10263 {
10264   unsigned long s;
10265
10266   if (i >= 0)
10267     {
10268       int clz, ctz;
10269       if (i <= 31)
10270         return 1;
10271       else if (i <= 0xff)
10272         return 2;
10273       else if (i <= 0xffff)
10274         return 3;
10275       clz = clz_hwi (i);
10276       ctz = ctz_hwi (i);
10277       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10278           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10279         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10280                                                     - clz - 5);
10281       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10282                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10283         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10284                                                     - clz - 8);
10285       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10286         return 5;
10287       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10288       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10289           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10290         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10291                                                     - clz - 8);
10292       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10293                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10294         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10295                                                     - clz - 16);
10296       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10297                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10298                && s > 6)
10299         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10300                                                     - clz - 32);
10301       else
10302         return 1 + s;
10303     }
10304   else
10305     {
10306       if (i >= -0x80)
10307         return 2;
10308       else if (i >= -0x8000)
10309         return 3;
10310       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10311         {
10312           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10313             {
10314               s = size_of_int_loc_descriptor (-i) + 1;
10315               if (s < 5)
10316                 return s;
10317             }
10318           return 5;
10319         }
10320       else
10321         {
10322           unsigned long r = 1 + size_of_sleb128 (i);
10323           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10324             {
10325               s = size_of_int_loc_descriptor (-i) + 1;
10326               if (s < r)
10327                 return s;
10328             }
10329           return r;
10330         }
10331     }
10332 }
10333
10334 /* Return loc description representing "address" of integer value.
10335    This can appear only as toplevel expression.  */
10336
10337 static dw_loc_descr_ref
10338 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10339 {
10340   int litsize;
10341   dw_loc_descr_ref loc_result = NULL;
10342
10343   if (!(dwarf_version >= 4 || !dwarf_strict))
10344     return NULL;
10345
10346   litsize = size_of_int_loc_descriptor (i);
10347   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10348      is more compact.  For DW_OP_stack_value we need:
10349      litsize + 1 (DW_OP_stack_value)
10350      and for DW_OP_implicit_value:
10351      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10352   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10353     {
10354       loc_result = int_loc_descriptor (i);
10355       add_loc_descr (&loc_result,
10356                      new_loc_descr (DW_OP_stack_value, 0, 0));
10357       return loc_result;
10358     }
10359
10360   loc_result = new_loc_descr (DW_OP_implicit_value,
10361                               size, 0);
10362   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10363   loc_result->dw_loc_oprnd2.v.val_int = i;
10364   return loc_result;
10365 }
10366
10367 /* Return a location descriptor that designates a base+offset location.  */
10368
10369 static dw_loc_descr_ref
10370 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10371                  enum var_init_status initialized)
10372 {
10373   unsigned int regno;
10374   dw_loc_descr_ref result;
10375   dw_fde_ref fde = cfun->fde;
10376
10377   /* We only use "frame base" when we're sure we're talking about the
10378      post-prologue local stack frame.  We do this by *not* running
10379      register elimination until this point, and recognizing the special
10380      argument pointer and soft frame pointer rtx's.  */
10381   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10382     {
10383       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10384
10385       if (elim != reg)
10386         {
10387           if (GET_CODE (elim) == PLUS)
10388             {
10389               offset += INTVAL (XEXP (elim, 1));
10390               elim = XEXP (elim, 0);
10391             }
10392           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10393                        && (elim == hard_frame_pointer_rtx
10394                            || elim == stack_pointer_rtx))
10395                       || elim == (frame_pointer_needed
10396                                   ? hard_frame_pointer_rtx
10397                                   : stack_pointer_rtx));
10398
10399           /* If drap register is used to align stack, use frame
10400              pointer + offset to access stack variables.  If stack
10401              is aligned without drap, use stack pointer + offset to
10402              access stack variables.  */
10403           if (crtl->stack_realign_tried
10404               && reg == frame_pointer_rtx)
10405             {
10406               int base_reg
10407                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10408                                       ? HARD_FRAME_POINTER_REGNUM
10409                                       : REGNO (elim));
10410               return new_reg_loc_descr (base_reg, offset);
10411             }
10412
10413           gcc_assert (frame_pointer_fb_offset_valid);
10414           offset += frame_pointer_fb_offset;
10415           return new_loc_descr (DW_OP_fbreg, offset, 0);
10416         }
10417     }
10418
10419   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10420
10421   if (!optimize && fde
10422       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10423     {
10424       /* Use cfa+offset to represent the location of arguments passed
10425          on the stack when drap is used to align stack.
10426          Only do this when not optimizing, for optimized code var-tracking
10427          is supposed to track where the arguments live and the register
10428          used as vdrap or drap in some spot might be used for something
10429          else in other part of the routine.  */
10430       return new_loc_descr (DW_OP_fbreg, offset, 0);
10431     }
10432
10433   if (regno <= 31)
10434     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10435                             offset, 0);
10436   else
10437     result = new_loc_descr (DW_OP_bregx, regno, offset);
10438
10439   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10440     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10441
10442   return result;
10443 }
10444
10445 /* Return true if this RTL expression describes a base+offset calculation.  */
10446
10447 static inline int
10448 is_based_loc (const_rtx rtl)
10449 {
10450   return (GET_CODE (rtl) == PLUS
10451           && ((REG_P (XEXP (rtl, 0))
10452                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10453                && CONST_INT_P (XEXP (rtl, 1)))));
10454 }
10455
10456 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10457    failed.  */
10458
10459 static dw_loc_descr_ref
10460 tls_mem_loc_descriptor (rtx mem)
10461 {
10462   tree base;
10463   dw_loc_descr_ref loc_result;
10464
10465   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10466     return NULL;
10467
10468   base = get_base_address (MEM_EXPR (mem));
10469   if (base == NULL
10470       || TREE_CODE (base) != VAR_DECL
10471       || !DECL_THREAD_LOCAL_P (base))
10472     return NULL;
10473
10474   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10475   if (loc_result == NULL)
10476     return NULL;
10477
10478   if (MEM_OFFSET (mem))
10479     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10480
10481   return loc_result;
10482 }
10483
10484 /* Output debug info about reason why we failed to expand expression as dwarf
10485    expression.  */
10486
10487 static void
10488 expansion_failed (tree expr, rtx rtl, char const *reason)
10489 {
10490   if (dump_file && (dump_flags & TDF_DETAILS))
10491     {
10492       fprintf (dump_file, "Failed to expand as dwarf: ");
10493       if (expr)
10494         print_generic_expr (dump_file, expr, dump_flags);
10495       if (rtl)
10496         {
10497           fprintf (dump_file, "\n");
10498           print_rtl (dump_file, rtl);
10499         }
10500       fprintf (dump_file, "\nReason: %s\n", reason);
10501     }
10502 }
10503
10504 /* Helper function for const_ok_for_output, called either directly
10505    or via for_each_rtx.  */
10506
10507 static int
10508 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10509 {
10510   rtx rtl = *rtlp;
10511
10512   if (GET_CODE (rtl) == UNSPEC)
10513     {
10514       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10515          we can't express it in the debug info.  */
10516 #ifdef ENABLE_CHECKING
10517       /* Don't complain about TLS UNSPECs, those are just too hard to
10518          delegitimize.  */
10519       if (XVECLEN (rtl, 0) != 1
10520           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10521           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10522           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10523           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10524         inform (current_function_decl
10525                 ? DECL_SOURCE_LOCATION (current_function_decl)
10526                 : UNKNOWN_LOCATION,
10527 #if NUM_UNSPEC_VALUES > 0
10528                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10529                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10530                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10531                 XINT (rtl, 1));
10532 #else
10533                 "non-delegitimized UNSPEC %d found in variable location",
10534                 XINT (rtl, 1));
10535 #endif
10536 #endif
10537       expansion_failed (NULL_TREE, rtl,
10538                         "UNSPEC hasn't been delegitimized.\n");
10539       return 1;
10540     }
10541
10542   if (GET_CODE (rtl) != SYMBOL_REF)
10543     return 0;
10544
10545   if (CONSTANT_POOL_ADDRESS_P (rtl))
10546     {
10547       bool marked;
10548       get_pool_constant_mark (rtl, &marked);
10549       /* If all references to this pool constant were optimized away,
10550          it was not output and thus we can't represent it.  */
10551       if (!marked)
10552         {
10553           expansion_failed (NULL_TREE, rtl,
10554                             "Constant was removed from constant pool.\n");
10555           return 1;
10556         }
10557     }
10558
10559   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10560     return 1;
10561
10562   /* Avoid references to external symbols in debug info, on several targets
10563      the linker might even refuse to link when linking a shared library,
10564      and in many other cases the relocations for .debug_info/.debug_loc are
10565      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10566      to be defined within the same shared library or executable are fine.  */
10567   if (SYMBOL_REF_EXTERNAL_P (rtl))
10568     {
10569       tree decl = SYMBOL_REF_DECL (rtl);
10570
10571       if (decl == NULL || !targetm.binds_local_p (decl))
10572         {
10573           expansion_failed (NULL_TREE, rtl,
10574                             "Symbol not defined in current TU.\n");
10575           return 1;
10576         }
10577     }
10578
10579   return 0;
10580 }
10581
10582 /* Return true if constant RTL can be emitted in DW_OP_addr or
10583    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10584    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10585
10586 static bool
10587 const_ok_for_output (rtx rtl)
10588 {
10589   if (GET_CODE (rtl) == SYMBOL_REF)
10590     return const_ok_for_output_1 (&rtl, NULL) == 0;
10591
10592   if (GET_CODE (rtl) == CONST)
10593     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10594
10595   return true;
10596 }
10597
10598 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10599    if possible, NULL otherwise.  */
10600
10601 static dw_die_ref
10602 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10603 {
10604   dw_die_ref type_die;
10605   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10606
10607   if (type == NULL)
10608     return NULL;
10609   switch (TREE_CODE (type))
10610     {
10611     case INTEGER_TYPE:
10612     case REAL_TYPE:
10613       break;
10614     default:
10615       return NULL;
10616     }
10617   type_die = lookup_type_die (type);
10618   if (!type_die)
10619     type_die = modified_type_die (type, false, false, comp_unit_die ());
10620   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10621     return NULL;
10622   return type_die;
10623 }
10624
10625 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10626    type matching MODE, or, if MODE is narrower than or as wide as
10627    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10628    possible.  */
10629
10630 static dw_loc_descr_ref
10631 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10632 {
10633   enum machine_mode outer_mode = mode;
10634   dw_die_ref type_die;
10635   dw_loc_descr_ref cvt;
10636
10637   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10638     {
10639       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10640       return op;
10641     }
10642   type_die = base_type_for_mode (outer_mode, 1);
10643   if (type_die == NULL)
10644     return NULL;
10645   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10646   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10647   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10648   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10649   add_loc_descr (&op, cvt);
10650   return op;
10651 }
10652
10653 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10654
10655 static dw_loc_descr_ref
10656 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10657                         dw_loc_descr_ref op1)
10658 {
10659   dw_loc_descr_ref ret = op0;
10660   add_loc_descr (&ret, op1);
10661   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10662   if (STORE_FLAG_VALUE != 1)
10663     {
10664       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10665       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10666     }
10667   return ret;
10668 }
10669
10670 /* Return location descriptor for signed comparison OP RTL.  */
10671
10672 static dw_loc_descr_ref
10673 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10674                          enum machine_mode mem_mode)
10675 {
10676   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10677   dw_loc_descr_ref op0, op1;
10678   int shift;
10679
10680   if (op_mode == VOIDmode)
10681     op_mode = GET_MODE (XEXP (rtl, 1));
10682   if (op_mode == VOIDmode)
10683     return NULL;
10684
10685   if (dwarf_strict
10686       && (GET_MODE_CLASS (op_mode) != MODE_INT
10687           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10688     return NULL;
10689
10690   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10691                             VAR_INIT_STATUS_INITIALIZED);
10692   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10693                             VAR_INIT_STATUS_INITIALIZED);
10694
10695   if (op0 == NULL || op1 == NULL)
10696     return NULL;
10697
10698   if (GET_MODE_CLASS (op_mode) != MODE_INT
10699       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10700     return compare_loc_descriptor (op, op0, op1);
10701
10702   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10703     {
10704       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10705       dw_loc_descr_ref cvt;
10706
10707       if (type_die == NULL)
10708         return NULL;
10709       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10710       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10711       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10712       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10713       add_loc_descr (&op0, cvt);
10714       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10715       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10716       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10717       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10718       add_loc_descr (&op1, cvt);
10719       return compare_loc_descriptor (op, op0, op1);
10720     }
10721
10722   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10723   /* For eq/ne, if the operands are known to be zero-extended,
10724      there is no need to do the fancy shifting up.  */
10725   if (op == DW_OP_eq || op == DW_OP_ne)
10726     {
10727       dw_loc_descr_ref last0, last1;
10728       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10729         ;
10730       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10731         ;
10732       /* deref_size zero extends, and for constants we can check
10733          whether they are zero extended or not.  */
10734       if (((last0->dw_loc_opc == DW_OP_deref_size
10735             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10736            || (CONST_INT_P (XEXP (rtl, 0))
10737                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10738                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10739           && ((last1->dw_loc_opc == DW_OP_deref_size
10740                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10741               || (CONST_INT_P (XEXP (rtl, 1))
10742                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10743                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10744         return compare_loc_descriptor (op, op0, op1);
10745
10746       /* EQ/NE comparison against constant in narrower type than
10747          DWARF2_ADDR_SIZE can be performed either as
10748          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10749          DW_OP_{eq,ne}
10750          or
10751          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10752          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10753       if (CONST_INT_P (XEXP (rtl, 1))
10754           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10755           && (size_of_int_loc_descriptor (shift) + 1
10756               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10757               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10758                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10759                                                & GET_MODE_MASK (op_mode))))
10760         {
10761           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10762           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10763           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10764                                     & GET_MODE_MASK (op_mode));
10765           return compare_loc_descriptor (op, op0, op1);
10766         }
10767     }
10768   add_loc_descr (&op0, int_loc_descriptor (shift));
10769   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10770   if (CONST_INT_P (XEXP (rtl, 1)))
10771     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10772   else
10773     {
10774       add_loc_descr (&op1, int_loc_descriptor (shift));
10775       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10776     }
10777   return compare_loc_descriptor (op, op0, op1);
10778 }
10779
10780 /* Return location descriptor for unsigned comparison OP RTL.  */
10781
10782 static dw_loc_descr_ref
10783 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10784                          enum machine_mode mem_mode)
10785 {
10786   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10787   dw_loc_descr_ref op0, op1;
10788
10789   if (op_mode == VOIDmode)
10790     op_mode = GET_MODE (XEXP (rtl, 1));
10791   if (op_mode == VOIDmode)
10792     return NULL;
10793   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10794     return NULL;
10795
10796   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10797     return NULL;
10798
10799   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10800                             VAR_INIT_STATUS_INITIALIZED);
10801   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10802                             VAR_INIT_STATUS_INITIALIZED);
10803
10804   if (op0 == NULL || op1 == NULL)
10805     return NULL;
10806
10807   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10808     {
10809       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10810       dw_loc_descr_ref last0, last1;
10811       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10812         ;
10813       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10814         ;
10815       if (CONST_INT_P (XEXP (rtl, 0)))
10816         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10817       /* deref_size zero extends, so no need to mask it again.  */
10818       else if (last0->dw_loc_opc != DW_OP_deref_size
10819                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10820         {
10821           add_loc_descr (&op0, int_loc_descriptor (mask));
10822           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10823         }
10824       if (CONST_INT_P (XEXP (rtl, 1)))
10825         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10826       /* deref_size zero extends, so no need to mask it again.  */
10827       else if (last1->dw_loc_opc != DW_OP_deref_size
10828                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10829         {
10830           add_loc_descr (&op1, int_loc_descriptor (mask));
10831           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10832         }
10833     }
10834   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10835     {
10836       HOST_WIDE_INT bias = 1;
10837       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10838       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10839       if (CONST_INT_P (XEXP (rtl, 1)))
10840         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10841                                   + INTVAL (XEXP (rtl, 1)));
10842       else
10843         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10844                                             bias, 0));
10845     }
10846   return compare_loc_descriptor (op, op0, op1);
10847 }
10848
10849 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10850
10851 static dw_loc_descr_ref
10852 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10853                        enum machine_mode mem_mode)
10854 {
10855   enum dwarf_location_atom op;
10856   dw_loc_descr_ref op0, op1, ret;
10857   dw_loc_descr_ref bra_node, drop_node;
10858
10859   if (dwarf_strict
10860       && (GET_MODE_CLASS (mode) != MODE_INT
10861           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10862     return NULL;
10863
10864   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10865                             VAR_INIT_STATUS_INITIALIZED);
10866   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10867                             VAR_INIT_STATUS_INITIALIZED);
10868
10869   if (op0 == NULL || op1 == NULL)
10870     return NULL;
10871
10872   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10873   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10874   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10875   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10876     {
10877       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10878         {
10879           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10880           add_loc_descr (&op0, int_loc_descriptor (mask));
10881           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10882           add_loc_descr (&op1, int_loc_descriptor (mask));
10883           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10884         }
10885       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10886         {
10887           HOST_WIDE_INT bias = 1;
10888           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10889           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10890           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10891         }
10892     }
10893   else if (GET_MODE_CLASS (mode) == MODE_INT
10894            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10895     {
10896       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10897       add_loc_descr (&op0, int_loc_descriptor (shift));
10898       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10899       add_loc_descr (&op1, int_loc_descriptor (shift));
10900       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10901     }
10902   else if (GET_MODE_CLASS (mode) == MODE_INT
10903            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10904     {
10905       dw_die_ref type_die = base_type_for_mode (mode, 0);
10906       dw_loc_descr_ref cvt;
10907       if (type_die == NULL)
10908         return NULL;
10909       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10910       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10911       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10912       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10913       add_loc_descr (&op0, cvt);
10914       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10915       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10916       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10917       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10918       add_loc_descr (&op1, cvt);
10919     }
10920
10921   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10922     op = DW_OP_lt;
10923   else
10924     op = DW_OP_gt;
10925   ret = op0;
10926   add_loc_descr (&ret, op1);
10927   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10928   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10929   add_loc_descr (&ret, bra_node);
10930   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10931   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10932   add_loc_descr (&ret, drop_node);
10933   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10934   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10935   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10936       && GET_MODE_CLASS (mode) == MODE_INT
10937       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10938     ret = convert_descriptor_to_mode (mode, ret);
10939   return ret;
10940 }
10941
10942 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
10943    but after converting arguments to type_die, afterwards
10944    convert back to unsigned.  */
10945
10946 static dw_loc_descr_ref
10947 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10948              enum machine_mode mode, enum machine_mode mem_mode)
10949 {
10950   dw_loc_descr_ref cvt, op0, op1;
10951
10952   if (type_die == NULL)
10953     return NULL;
10954   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10955                             VAR_INIT_STATUS_INITIALIZED);
10956   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10957                             VAR_INIT_STATUS_INITIALIZED);
10958   if (op0 == NULL || op1 == NULL)
10959     return NULL;
10960   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10961   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10962   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10963   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10964   add_loc_descr (&op0, cvt);
10965   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10966   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10967   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10968   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10969   add_loc_descr (&op1, cvt);
10970   add_loc_descr (&op0, op1);
10971   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10972   return convert_descriptor_to_mode (mode, op0);
10973 }
10974
10975 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10976    const0 is DW_OP_lit0 or corresponding typed constant,
10977    const1 is DW_OP_lit1 or corresponding typed constant
10978    and constMSB is constant with just the MSB bit set
10979    for the mode):
10980        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10981    L1: const0 DW_OP_swap
10982    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10983        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10984    L3: DW_OP_drop
10985    L4: DW_OP_nop
10986
10987    CTZ is similar:
10988        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10989    L1: const0 DW_OP_swap
10990    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10991        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10992    L3: DW_OP_drop
10993    L4: DW_OP_nop
10994
10995    FFS is similar:
10996        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10997    L1: const1 DW_OP_swap
10998    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
10999        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11000    L3: DW_OP_drop
11001    L4: DW_OP_nop  */
11002
11003 static dw_loc_descr_ref
11004 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11005                     enum machine_mode mem_mode)
11006 {
11007   dw_loc_descr_ref op0, ret, tmp;
11008   HOST_WIDE_INT valv;
11009   dw_loc_descr_ref l1jump, l1label;
11010   dw_loc_descr_ref l2jump, l2label;
11011   dw_loc_descr_ref l3jump, l3label;
11012   dw_loc_descr_ref l4jump, l4label;
11013   rtx msb;
11014
11015   if (GET_MODE_CLASS (mode) != MODE_INT
11016       || GET_MODE (XEXP (rtl, 0)) != mode
11017       || (GET_CODE (rtl) == CLZ
11018           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11019     return NULL;
11020
11021   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11022                             VAR_INIT_STATUS_INITIALIZED);
11023   if (op0 == NULL)
11024     return NULL;
11025   ret = op0;
11026   if (GET_CODE (rtl) == CLZ)
11027     {
11028       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11029         valv = GET_MODE_BITSIZE (mode);
11030     }
11031   else if (GET_CODE (rtl) == FFS)
11032     valv = 0;
11033   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11034     valv = GET_MODE_BITSIZE (mode);
11035   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11036   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11037   add_loc_descr (&ret, l1jump);
11038   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11039   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11040                             VAR_INIT_STATUS_INITIALIZED);
11041   if (tmp == NULL)
11042     return NULL;
11043   add_loc_descr (&ret, tmp);
11044   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11045   add_loc_descr (&ret, l4jump);
11046   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11047                                 ? const1_rtx : const0_rtx,
11048                                 mode, mem_mode,
11049                                 VAR_INIT_STATUS_INITIALIZED);
11050   if (l1label == NULL)
11051     return NULL;
11052   add_loc_descr (&ret, l1label);
11053   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11054   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11055   add_loc_descr (&ret, l2label);
11056   if (GET_CODE (rtl) != CLZ)
11057     msb = const1_rtx;
11058   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11059     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11060                    << (GET_MODE_BITSIZE (mode) - 1));
11061   else
11062     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11063                                   << (GET_MODE_BITSIZE (mode)
11064                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11065   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11066     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11067                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11068                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11069   else
11070     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11071                               VAR_INIT_STATUS_INITIALIZED);
11072   if (tmp == NULL)
11073     return NULL;
11074   add_loc_descr (&ret, tmp);
11075   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11076   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11077   add_loc_descr (&ret, l3jump);
11078   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11079                             VAR_INIT_STATUS_INITIALIZED);
11080   if (tmp == NULL)
11081     return NULL;
11082   add_loc_descr (&ret, tmp);
11083   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11084                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11085   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11086   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11087   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11088   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11089   add_loc_descr (&ret, l2jump);
11090   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11091   add_loc_descr (&ret, l3label);
11092   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11093   add_loc_descr (&ret, l4label);
11094   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11095   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11096   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11097   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11098   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11099   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11100   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11101   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11102   return ret;
11103 }
11104
11105 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11106    const1 is DW_OP_lit1 or corresponding typed constant):
11107        const0 DW_OP_swap
11108    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11109        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11110    L2: DW_OP_drop
11111
11112    PARITY is similar:
11113    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11114        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11115    L2: DW_OP_drop  */
11116
11117 static dw_loc_descr_ref
11118 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11119                          enum machine_mode mem_mode)
11120 {
11121   dw_loc_descr_ref op0, ret, tmp;
11122   dw_loc_descr_ref l1jump, l1label;
11123   dw_loc_descr_ref l2jump, l2label;
11124
11125   if (GET_MODE_CLASS (mode) != MODE_INT
11126       || GET_MODE (XEXP (rtl, 0)) != mode)
11127     return NULL;
11128
11129   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11130                             VAR_INIT_STATUS_INITIALIZED);
11131   if (op0 == NULL)
11132     return NULL;
11133   ret = op0;
11134   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11135                             VAR_INIT_STATUS_INITIALIZED);
11136   if (tmp == NULL)
11137     return NULL;
11138   add_loc_descr (&ret, tmp);
11139   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11140   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11141   add_loc_descr (&ret, l1label);
11142   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11143   add_loc_descr (&ret, l2jump);
11144   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11145   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11146   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11147                             VAR_INIT_STATUS_INITIALIZED);
11148   if (tmp == NULL)
11149     return NULL;
11150   add_loc_descr (&ret, tmp);
11151   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11152   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11153                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11154   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11155   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11156                             VAR_INIT_STATUS_INITIALIZED);
11157   add_loc_descr (&ret, tmp);
11158   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11159   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11160   add_loc_descr (&ret, l1jump);
11161   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11162   add_loc_descr (&ret, l2label);
11163   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11164   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11165   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11166   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11167   return ret;
11168 }
11169
11170 /* BSWAP (constS is initial shift count, either 56 or 24):
11171        constS const0
11172    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11173        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11174        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11175        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11176    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11177
11178 static dw_loc_descr_ref
11179 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11180                       enum machine_mode mem_mode)
11181 {
11182   dw_loc_descr_ref op0, ret, tmp;
11183   dw_loc_descr_ref l1jump, l1label;
11184   dw_loc_descr_ref l2jump, l2label;
11185
11186   if (GET_MODE_CLASS (mode) != MODE_INT
11187       || BITS_PER_UNIT != 8
11188       || (GET_MODE_BITSIZE (mode) != 32
11189           &&  GET_MODE_BITSIZE (mode) != 64))
11190     return NULL;
11191
11192   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11193                             VAR_INIT_STATUS_INITIALIZED);
11194   if (op0 == NULL)
11195     return NULL;
11196
11197   ret = op0;
11198   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11199                             mode, mem_mode,
11200                             VAR_INIT_STATUS_INITIALIZED);
11201   if (tmp == NULL)
11202     return NULL;
11203   add_loc_descr (&ret, tmp);
11204   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11205                             VAR_INIT_STATUS_INITIALIZED);
11206   if (tmp == NULL)
11207     return NULL;
11208   add_loc_descr (&ret, tmp);
11209   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11210   add_loc_descr (&ret, l1label);
11211   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11212                             mode, mem_mode,
11213                             VAR_INIT_STATUS_INITIALIZED);
11214   add_loc_descr (&ret, tmp);
11215   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11216   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11217   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11218   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11219                             VAR_INIT_STATUS_INITIALIZED);
11220   if (tmp == NULL)
11221     return NULL;
11222   add_loc_descr (&ret, tmp);
11223   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11224   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11225   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11226   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11227   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11228   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11229   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11230                             VAR_INIT_STATUS_INITIALIZED);
11231   add_loc_descr (&ret, tmp);
11232   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11233   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11234   add_loc_descr (&ret, l2jump);
11235   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11236                             VAR_INIT_STATUS_INITIALIZED);
11237   add_loc_descr (&ret, tmp);
11238   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11239   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11240   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11241   add_loc_descr (&ret, l1jump);
11242   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11243   add_loc_descr (&ret, l2label);
11244   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11245   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11246   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11247   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11248   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11249   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11250   return ret;
11251 }
11252
11253 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11254    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11255    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11256    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11257
11258    ROTATERT is similar:
11259    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11260    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11261    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11262
11263 static dw_loc_descr_ref
11264 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11265                        enum machine_mode mem_mode)
11266 {
11267   rtx rtlop1 = XEXP (rtl, 1);
11268   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11269   int i;
11270
11271   if (GET_MODE_CLASS (mode) != MODE_INT)
11272     return NULL;
11273
11274   if (GET_MODE (rtlop1) != VOIDmode
11275       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11276     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11277   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11278                             VAR_INIT_STATUS_INITIALIZED);
11279   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11280                             VAR_INIT_STATUS_INITIALIZED);
11281   if (op0 == NULL || op1 == NULL)
11282     return NULL;
11283   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11284     for (i = 0; i < 2; i++)
11285       {
11286         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11287           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11288                                         mode, mem_mode,
11289                                         VAR_INIT_STATUS_INITIALIZED);
11290         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11291           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11292                                    ? DW_OP_const4u
11293                                    : HOST_BITS_PER_WIDE_INT == 64
11294                                    ? DW_OP_const8u : DW_OP_constu,
11295                                    GET_MODE_MASK (mode), 0);
11296         else
11297           mask[i] = NULL;
11298         if (mask[i] == NULL)
11299           return NULL;
11300         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11301       }
11302   ret = op0;
11303   add_loc_descr (&ret, op1);
11304   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11305   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11306   if (GET_CODE (rtl) == ROTATERT)
11307     {
11308       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11309       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11310                                           GET_MODE_BITSIZE (mode), 0));
11311     }
11312   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11313   if (mask[0] != NULL)
11314     add_loc_descr (&ret, mask[0]);
11315   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11316   if (mask[1] != NULL)
11317     {
11318       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11319       add_loc_descr (&ret, mask[1]);
11320       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11321     }
11322   if (GET_CODE (rtl) == ROTATE)
11323     {
11324       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11325       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11326                                           GET_MODE_BITSIZE (mode), 0));
11327     }
11328   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11329   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11330   return ret;
11331 }
11332
11333 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11334    for DEBUG_PARAMETER_REF RTL.  */
11335
11336 static dw_loc_descr_ref
11337 parameter_ref_descriptor (rtx rtl)
11338 {
11339   dw_loc_descr_ref ret;
11340   dw_die_ref ref;
11341
11342   if (dwarf_strict)
11343     return NULL;
11344   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11345   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11346   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11347   if (ref)
11348     {
11349       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11350       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11351       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11352     }
11353   else
11354     {
11355       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11356       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11357     }
11358   return ret;
11359 }
11360
11361 /* Helper function to get mode of MEM's address.  */
11362
11363 enum machine_mode
11364 get_address_mode (rtx mem)
11365 {
11366   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11367   if (mode != VOIDmode)
11368     return mode;
11369   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11370 }
11371
11372 /* The following routine converts the RTL for a variable or parameter
11373    (resident in memory) into an equivalent Dwarf representation of a
11374    mechanism for getting the address of that same variable onto the top of a
11375    hypothetical "address evaluation" stack.
11376
11377    When creating memory location descriptors, we are effectively transforming
11378    the RTL for a memory-resident object into its Dwarf postfix expression
11379    equivalent.  This routine recursively descends an RTL tree, turning
11380    it into Dwarf postfix code as it goes.
11381
11382    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11383
11384    MEM_MODE is the mode of the memory reference, needed to handle some
11385    autoincrement addressing modes.
11386
11387    Return 0 if we can't represent the location.  */
11388
11389 dw_loc_descr_ref
11390 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11391                     enum machine_mode mem_mode,
11392                     enum var_init_status initialized)
11393 {
11394   dw_loc_descr_ref mem_loc_result = NULL;
11395   enum dwarf_location_atom op;
11396   dw_loc_descr_ref op0, op1;
11397
11398   if (mode == VOIDmode)
11399     mode = GET_MODE (rtl);
11400
11401   /* Note that for a dynamically sized array, the location we will generate a
11402      description of here will be the lowest numbered location which is
11403      actually within the array.  That's *not* necessarily the same as the
11404      zeroth element of the array.  */
11405
11406   rtl = targetm.delegitimize_address (rtl);
11407
11408   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11409     return NULL;
11410
11411   switch (GET_CODE (rtl))
11412     {
11413     case POST_INC:
11414     case POST_DEC:
11415     case POST_MODIFY:
11416       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11417
11418     case SUBREG:
11419       /* The case of a subreg may arise when we have a local (register)
11420          variable or a formal (register) parameter which doesn't quite fill
11421          up an entire register.  For now, just assume that it is
11422          legitimate to make the Dwarf info refer to the whole register which
11423          contains the given subreg.  */
11424       if (!subreg_lowpart_p (rtl))
11425         break;
11426       if (GET_MODE_CLASS (mode) == MODE_INT
11427           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11428           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11429 #ifdef POINTERS_EXTEND_UNSIGNED
11430               || (mode == Pmode && mem_mode != VOIDmode)
11431 #endif
11432              )
11433           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11434         {
11435           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11436                                                GET_MODE (SUBREG_REG (rtl)),
11437                                                mem_mode, initialized);
11438           break;
11439         }
11440       if (dwarf_strict)
11441         break;
11442       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11443         break;
11444       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11445           && (GET_MODE_CLASS (mode) != MODE_INT
11446               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11447         break;
11448       else
11449         {
11450           dw_die_ref type_die;
11451           dw_loc_descr_ref cvt;
11452
11453           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11454                                                GET_MODE (SUBREG_REG (rtl)),
11455                                                mem_mode, initialized);
11456           if (mem_loc_result == NULL)
11457             break;
11458           type_die = base_type_for_mode (mode,
11459                                          GET_MODE_CLASS (mode) == MODE_INT);
11460           if (type_die == NULL)
11461             {
11462               mem_loc_result = NULL;
11463               break;
11464             }
11465           if (GET_MODE_SIZE (mode)
11466               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11467             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11468           else
11469             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11470           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11471           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11472           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11473           add_loc_descr (&mem_loc_result, cvt);
11474         }
11475       break;
11476
11477     case REG:
11478       if (GET_MODE_CLASS (mode) != MODE_INT
11479           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11480 #ifdef POINTERS_EXTEND_UNSIGNED
11481               && (mode != Pmode || mem_mode == VOIDmode)
11482 #endif
11483               ))
11484         {
11485           dw_die_ref type_die;
11486
11487           if (dwarf_strict)
11488             break;
11489           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11490             break;
11491           type_die = base_type_for_mode (mode,
11492                                          GET_MODE_CLASS (mode) == MODE_INT);
11493           if (type_die == NULL)
11494             break;
11495           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11496                                           dbx_reg_number (rtl), 0);
11497           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11498           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11499           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11500           break;
11501         }
11502       /* Whenever a register number forms a part of the description of the
11503          method for calculating the (dynamic) address of a memory resident
11504          object, DWARF rules require the register number be referred to as
11505          a "base register".  This distinction is not based in any way upon
11506          what category of register the hardware believes the given register
11507          belongs to.  This is strictly DWARF terminology we're dealing with
11508          here. Note that in cases where the location of a memory-resident
11509          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11510          OP_CONST (0)) the actual DWARF location descriptor that we generate
11511          may just be OP_BASEREG (basereg).  This may look deceptively like
11512          the object in question was allocated to a register (rather than in
11513          memory) so DWARF consumers need to be aware of the subtle
11514          distinction between OP_REG and OP_BASEREG.  */
11515       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11516         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11517       else if (stack_realign_drap
11518                && crtl->drap_reg
11519                && crtl->args.internal_arg_pointer == rtl
11520                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11521         {
11522           /* If RTL is internal_arg_pointer, which has been optimized
11523              out, use DRAP instead.  */
11524           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11525                                             VAR_INIT_STATUS_INITIALIZED);
11526         }
11527       break;
11528
11529     case SIGN_EXTEND:
11530     case ZERO_EXTEND:
11531       if (GET_MODE_CLASS (mode) != MODE_INT)
11532         break;
11533       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11534                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11535       if (op0 == 0)
11536         break;
11537       else if (GET_CODE (rtl) == ZERO_EXTEND
11538                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11539                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11540                   < HOST_BITS_PER_WIDE_INT
11541                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11542                   to expand zero extend as two shifts instead of
11543                   masking.  */
11544                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11545         {
11546           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11547           mem_loc_result = op0;
11548           add_loc_descr (&mem_loc_result,
11549                          int_loc_descriptor (GET_MODE_MASK (imode)));
11550           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11551         }
11552       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11553         {
11554           int shift = DWARF2_ADDR_SIZE
11555                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11556           shift *= BITS_PER_UNIT;
11557           if (GET_CODE (rtl) == SIGN_EXTEND)
11558             op = DW_OP_shra;
11559           else
11560             op = DW_OP_shr;
11561           mem_loc_result = op0;
11562           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11563           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11564           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11565           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11566         }
11567       else if (!dwarf_strict)
11568         {
11569           dw_die_ref type_die1, type_die2;
11570           dw_loc_descr_ref cvt;
11571
11572           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11573                                           GET_CODE (rtl) == ZERO_EXTEND);
11574           if (type_die1 == NULL)
11575             break;
11576           type_die2 = base_type_for_mode (mode, 1);
11577           if (type_die2 == NULL)
11578             break;
11579           mem_loc_result = op0;
11580           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11581           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11582           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11583           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11584           add_loc_descr (&mem_loc_result, cvt);
11585           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11586           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11587           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11588           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11589           add_loc_descr (&mem_loc_result, cvt);
11590         }
11591       break;
11592
11593     case MEM:
11594       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11595                                            get_address_mode (rtl), mode,
11596                                            VAR_INIT_STATUS_INITIALIZED);
11597       if (mem_loc_result == NULL)
11598         mem_loc_result = tls_mem_loc_descriptor (rtl);
11599       if (mem_loc_result != 0)
11600         {
11601           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11602               || GET_MODE_CLASS (mode) != MODE_INT)
11603             {
11604               dw_die_ref type_die;
11605               dw_loc_descr_ref deref;
11606
11607               if (dwarf_strict)
11608                 return NULL;
11609               type_die
11610                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11611               if (type_die == NULL)
11612                 return NULL;
11613               deref = new_loc_descr (DW_OP_GNU_deref_type,
11614                                      GET_MODE_SIZE (mode), 0);
11615               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11616               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11617               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11618               add_loc_descr (&mem_loc_result, deref);
11619             }
11620           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11621             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11622           else
11623             add_loc_descr (&mem_loc_result,
11624                            new_loc_descr (DW_OP_deref_size,
11625                                           GET_MODE_SIZE (mode), 0));
11626         }
11627       else
11628         {
11629           rtx new_rtl = avoid_constant_pool_reference (rtl);
11630           if (new_rtl != rtl)
11631             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
11632         }
11633       break;
11634
11635     case LO_SUM:
11636       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11637
11638     case LABEL_REF:
11639       /* Some ports can transform a symbol ref into a label ref, because
11640          the symbol ref is too far away and has to be dumped into a constant
11641          pool.  */
11642     case CONST:
11643     case SYMBOL_REF:
11644       if (GET_MODE_CLASS (mode) != MODE_INT
11645           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11646 #ifdef POINTERS_EXTEND_UNSIGNED
11647               && (mode != Pmode || mem_mode == VOIDmode)
11648 #endif
11649               ))
11650         break;
11651       if (GET_CODE (rtl) == SYMBOL_REF
11652           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11653         {
11654           dw_loc_descr_ref temp;
11655
11656           /* If this is not defined, we have no way to emit the data.  */
11657           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11658             break;
11659
11660           /* We used to emit DW_OP_addr here, but that's wrong, since
11661              DW_OP_addr should be relocated by the debug info consumer,
11662              while DW_OP_GNU_push_tls_address operand should not.  */
11663           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11664                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11665           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11666           temp->dw_loc_oprnd1.v.val_addr = rtl;
11667           temp->dtprel = true;
11668
11669           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11670           add_loc_descr (&mem_loc_result, temp);
11671
11672           break;
11673         }
11674
11675       if (!const_ok_for_output (rtl))
11676         break;
11677
11678     symref:
11679       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11680       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11681       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11682       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11683       break;
11684
11685     case CONCAT:
11686     case CONCATN:
11687     case VAR_LOCATION:
11688     case DEBUG_IMPLICIT_PTR:
11689       expansion_failed (NULL_TREE, rtl,
11690                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11691       return 0;
11692
11693     case ENTRY_VALUE:
11694       if (dwarf_strict)
11695         return NULL;
11696       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11697         {
11698           if (GET_MODE_CLASS (mode) != MODE_INT
11699               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11700             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11701                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11702           else
11703             op0
11704               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11705                                         VAR_INIT_STATUS_INITIALIZED);
11706         }
11707       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11708                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11709         {
11710           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11711                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11712           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11713             return NULL;
11714         }
11715       else
11716         gcc_unreachable ();
11717       if (op0 == NULL)
11718         return NULL;
11719       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11720       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11721       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11722       break;
11723
11724     case DEBUG_PARAMETER_REF:
11725       mem_loc_result = parameter_ref_descriptor (rtl);
11726       break;
11727
11728     case PRE_MODIFY:
11729       /* Extract the PLUS expression nested inside and fall into
11730          PLUS code below.  */
11731       rtl = XEXP (rtl, 1);
11732       goto plus;
11733
11734     case PRE_INC:
11735     case PRE_DEC:
11736       /* Turn these into a PLUS expression and fall into the PLUS code
11737          below.  */
11738       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11739                           GEN_INT (GET_CODE (rtl) == PRE_INC
11740                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11741                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11742
11743       /* ... fall through ...  */
11744
11745     case PLUS:
11746     plus:
11747       if (is_based_loc (rtl)
11748           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11749           && GET_MODE_CLASS (mode) == MODE_INT)
11750         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11751                                           INTVAL (XEXP (rtl, 1)),
11752                                           VAR_INIT_STATUS_INITIALIZED);
11753       else
11754         {
11755           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11756                                                VAR_INIT_STATUS_INITIALIZED);
11757           if (mem_loc_result == 0)
11758             break;
11759
11760           if (CONST_INT_P (XEXP (rtl, 1))
11761               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11762             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11763           else
11764             {
11765               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11766                                         VAR_INIT_STATUS_INITIALIZED);
11767               if (op1 == 0)
11768                 break;
11769               add_loc_descr (&mem_loc_result, op1);
11770               add_loc_descr (&mem_loc_result,
11771                              new_loc_descr (DW_OP_plus, 0, 0));
11772             }
11773         }
11774       break;
11775
11776     /* If a pseudo-reg is optimized away, it is possible for it to
11777        be replaced with a MEM containing a multiply or shift.  */
11778     case MINUS:
11779       op = DW_OP_minus;
11780       goto do_binop;
11781
11782     case MULT:
11783       op = DW_OP_mul;
11784       goto do_binop;
11785
11786     case DIV:
11787       if (!dwarf_strict
11788           && GET_MODE_CLASS (mode) == MODE_INT
11789           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11790         {
11791           mem_loc_result = typed_binop (DW_OP_div, rtl,
11792                                         base_type_for_mode (mode, 0),
11793                                         mode, mem_mode);
11794           break;
11795         }
11796       op = DW_OP_div;
11797       goto do_binop;
11798
11799     case UMOD:
11800       op = DW_OP_mod;
11801       goto do_binop;
11802
11803     case ASHIFT:
11804       op = DW_OP_shl;
11805       goto do_shift;
11806
11807     case ASHIFTRT:
11808       op = DW_OP_shra;
11809       goto do_shift;
11810
11811     case LSHIFTRT:
11812       op = DW_OP_shr;
11813       goto do_shift;
11814
11815     do_shift:
11816       if (GET_MODE_CLASS (mode) != MODE_INT)
11817         break;
11818       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11819                                 VAR_INIT_STATUS_INITIALIZED);
11820       {
11821         rtx rtlop1 = XEXP (rtl, 1);
11822         if (GET_MODE (rtlop1) != VOIDmode
11823             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11824                < GET_MODE_BITSIZE (mode))
11825           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11826         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11827                                   VAR_INIT_STATUS_INITIALIZED);
11828       }
11829
11830       if (op0 == 0 || op1 == 0)
11831         break;
11832
11833       mem_loc_result = op0;
11834       add_loc_descr (&mem_loc_result, op1);
11835       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11836       break;
11837
11838     case AND:
11839       op = DW_OP_and;
11840       goto do_binop;
11841
11842     case IOR:
11843       op = DW_OP_or;
11844       goto do_binop;
11845
11846     case XOR:
11847       op = DW_OP_xor;
11848       goto do_binop;
11849
11850     do_binop:
11851       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11852                                 VAR_INIT_STATUS_INITIALIZED);
11853       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11854                                 VAR_INIT_STATUS_INITIALIZED);
11855
11856       if (op0 == 0 || op1 == 0)
11857         break;
11858
11859       mem_loc_result = op0;
11860       add_loc_descr (&mem_loc_result, op1);
11861       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11862       break;
11863
11864     case MOD:
11865       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11866         {
11867           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11868                                         base_type_for_mode (mode, 0),
11869                                         mode, mem_mode);
11870           break;
11871         }
11872
11873       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11874                                 VAR_INIT_STATUS_INITIALIZED);
11875       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11876                                 VAR_INIT_STATUS_INITIALIZED);
11877
11878       if (op0 == 0 || op1 == 0)
11879         break;
11880
11881       mem_loc_result = op0;
11882       add_loc_descr (&mem_loc_result, op1);
11883       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11884       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11885       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11886       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11887       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11888       break;
11889
11890     case UDIV:
11891       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11892         {
11893           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11894             {
11895               op = DW_OP_div;
11896               goto do_binop;
11897             }
11898           mem_loc_result = typed_binop (DW_OP_div, rtl,
11899                                         base_type_for_mode (mode, 1),
11900                                         mode, mem_mode);
11901         }
11902       break;
11903
11904     case NOT:
11905       op = DW_OP_not;
11906       goto do_unop;
11907
11908     case ABS:
11909       op = DW_OP_abs;
11910       goto do_unop;
11911
11912     case NEG:
11913       op = DW_OP_neg;
11914       goto do_unop;
11915
11916     do_unop:
11917       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11918                                 VAR_INIT_STATUS_INITIALIZED);
11919
11920       if (op0 == 0)
11921         break;
11922
11923       mem_loc_result = op0;
11924       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11925       break;
11926
11927     case CONST_INT:
11928       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11929 #ifdef POINTERS_EXTEND_UNSIGNED
11930           || (mode == Pmode
11931               && mem_mode != VOIDmode
11932               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11933 #endif
11934           )
11935         {
11936           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11937           break;
11938         }
11939       if (!dwarf_strict
11940           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11941               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
11942         {
11943           dw_die_ref type_die = base_type_for_mode (mode, 1);
11944           enum machine_mode amode;
11945           if (type_die == NULL)
11946             return NULL;
11947           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
11948                                  MODE_INT, 0);
11949           if (INTVAL (rtl) >= 0
11950               && amode != BLKmode
11951               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
11952               /* const DW_OP_GNU_convert <XXX> vs.
11953                  DW_OP_GNU_const_type <XXX, 1, const>.  */
11954               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
11955                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
11956             {
11957               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11958               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11959               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11960               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11961               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
11962               add_loc_descr (&mem_loc_result, op0);
11963               return mem_loc_result;
11964             }
11965           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11966                                           INTVAL (rtl));
11967           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11968           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11969           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11970           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11971             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11972           else
11973             {
11974               mem_loc_result->dw_loc_oprnd2.val_class
11975                 = dw_val_class_const_double;
11976               mem_loc_result->dw_loc_oprnd2.v.val_double
11977                 = shwi_to_double_int (INTVAL (rtl));
11978             }
11979         }
11980       break;
11981
11982     case CONST_DOUBLE:
11983       if (!dwarf_strict)
11984         {
11985           dw_die_ref type_die;
11986
11987           /* Note that a CONST_DOUBLE rtx could represent either an integer
11988              or a floating-point constant.  A CONST_DOUBLE is used whenever
11989              the constant requires more than one word in order to be
11990              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11991           if (mode == VOIDmode
11992               || (GET_MODE (rtl) == VOIDmode
11993                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
11994             break;
11995           type_die = base_type_for_mode (mode,
11996                                          GET_MODE_CLASS (mode) == MODE_INT);
11997           if (type_die == NULL)
11998             return NULL;
11999           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12000           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12001           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12002           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12003           if (SCALAR_FLOAT_MODE_P (mode))
12004             {
12005               unsigned int length = GET_MODE_SIZE (mode);
12006               unsigned char *array
12007                   = (unsigned char*) ggc_alloc_atomic (length);
12008
12009               insert_float (rtl, array);
12010               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12011               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12012               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12013               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12014             }
12015           else
12016             {
12017               mem_loc_result->dw_loc_oprnd2.val_class
12018                 = dw_val_class_const_double;
12019               mem_loc_result->dw_loc_oprnd2.v.val_double
12020                 = rtx_to_double_int (rtl);
12021             }
12022         }
12023       break;
12024
12025     case EQ:
12026       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12027       break;
12028
12029     case GE:
12030       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12031       break;
12032
12033     case GT:
12034       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12035       break;
12036
12037     case LE:
12038       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12039       break;
12040
12041     case LT:
12042       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12043       break;
12044
12045     case NE:
12046       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12047       break;
12048
12049     case GEU:
12050       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12051       break;
12052
12053     case GTU:
12054       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12055       break;
12056
12057     case LEU:
12058       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12059       break;
12060
12061     case LTU:
12062       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12063       break;
12064
12065     case UMIN:
12066     case UMAX:
12067       if (GET_MODE_CLASS (mode) != MODE_INT)
12068         break;
12069       /* FALLTHRU */
12070     case SMIN:
12071     case SMAX:
12072       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12073       break;
12074
12075     case ZERO_EXTRACT:
12076     case SIGN_EXTRACT:
12077       if (CONST_INT_P (XEXP (rtl, 1))
12078           && CONST_INT_P (XEXP (rtl, 2))
12079           && ((unsigned) INTVAL (XEXP (rtl, 1))
12080               + (unsigned) INTVAL (XEXP (rtl, 2))
12081               <= GET_MODE_BITSIZE (mode))
12082           && GET_MODE_CLASS (mode) == MODE_INT
12083           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12084           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12085         {
12086           int shift, size;
12087           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12088                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12089           if (op0 == 0)
12090             break;
12091           if (GET_CODE (rtl) == SIGN_EXTRACT)
12092             op = DW_OP_shra;
12093           else
12094             op = DW_OP_shr;
12095           mem_loc_result = op0;
12096           size = INTVAL (XEXP (rtl, 1));
12097           shift = INTVAL (XEXP (rtl, 2));
12098           if (BITS_BIG_ENDIAN)
12099             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12100                     - shift - size;
12101           if (shift + size != (int) DWARF2_ADDR_SIZE)
12102             {
12103               add_loc_descr (&mem_loc_result,
12104                              int_loc_descriptor (DWARF2_ADDR_SIZE
12105                                                  - shift - size));
12106               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12107             }
12108           if (size != (int) DWARF2_ADDR_SIZE)
12109             {
12110               add_loc_descr (&mem_loc_result,
12111                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12112               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12113             }
12114         }
12115       break;
12116
12117     case IF_THEN_ELSE:
12118       {
12119         dw_loc_descr_ref op2, bra_node, drop_node;
12120         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12121                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12122                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12123                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12124         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12125                                   VAR_INIT_STATUS_INITIALIZED);
12126         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12127                                   VAR_INIT_STATUS_INITIALIZED);
12128         if (op0 == NULL || op1 == NULL || op2 == NULL)
12129           break;
12130
12131         mem_loc_result = op1;
12132         add_loc_descr (&mem_loc_result, op2);
12133         add_loc_descr (&mem_loc_result, op0);
12134         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12135         add_loc_descr (&mem_loc_result, bra_node);
12136         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12137         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12138         add_loc_descr (&mem_loc_result, drop_node);
12139         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12140         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12141       }
12142       break;
12143
12144     case FLOAT_EXTEND:
12145     case FLOAT_TRUNCATE:
12146     case FLOAT:
12147     case UNSIGNED_FLOAT:
12148     case FIX:
12149     case UNSIGNED_FIX:
12150       if (!dwarf_strict)
12151         {
12152           dw_die_ref type_die;
12153           dw_loc_descr_ref cvt;
12154
12155           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12156                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12157           if (op0 == NULL)
12158             break;
12159           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12160               && (GET_CODE (rtl) == FLOAT
12161                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12162                      <= DWARF2_ADDR_SIZE))
12163             {
12164               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12165                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12166               if (type_die == NULL)
12167                 break;
12168               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12169               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12170               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12171               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12172               add_loc_descr (&op0, cvt);
12173             }
12174           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12175           if (type_die == NULL)
12176             break;
12177           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12178           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12179           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12180           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12181           add_loc_descr (&op0, cvt);
12182           if (GET_MODE_CLASS (mode) == MODE_INT
12183               && (GET_CODE (rtl) == FIX
12184                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12185             {
12186               op0 = convert_descriptor_to_mode (mode, op0);
12187               if (op0 == NULL)
12188                 break;
12189             }
12190           mem_loc_result = op0;
12191         }
12192       break;
12193
12194     case CLZ:
12195     case CTZ:
12196     case FFS:
12197       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12198       break;
12199
12200     case POPCOUNT:
12201     case PARITY:
12202       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12203       break;
12204
12205     case BSWAP:
12206       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12207       break;
12208
12209     case ROTATE:
12210     case ROTATERT:
12211       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12212       break;
12213
12214     case COMPARE:
12215     case TRUNCATE:
12216       /* In theory, we could implement the above.  */
12217       /* DWARF cannot represent the unsigned compare operations
12218          natively.  */
12219     case SS_MULT:
12220     case US_MULT:
12221     case SS_DIV:
12222     case US_DIV:
12223     case SS_PLUS:
12224     case US_PLUS:
12225     case SS_MINUS:
12226     case US_MINUS:
12227     case SS_NEG:
12228     case US_NEG:
12229     case SS_ABS:
12230     case SS_ASHIFT:
12231     case US_ASHIFT:
12232     case SS_TRUNCATE:
12233     case US_TRUNCATE:
12234     case UNORDERED:
12235     case ORDERED:
12236     case UNEQ:
12237     case UNGE:
12238     case UNGT:
12239     case UNLE:
12240     case UNLT:
12241     case LTGT:
12242     case FRACT_CONVERT:
12243     case UNSIGNED_FRACT_CONVERT:
12244     case SAT_FRACT:
12245     case UNSIGNED_SAT_FRACT:
12246     case SQRT:
12247     case ASM_OPERANDS:
12248     case VEC_MERGE:
12249     case VEC_SELECT:
12250     case VEC_CONCAT:
12251     case VEC_DUPLICATE:
12252     case UNSPEC:
12253     case HIGH:
12254     case FMA:
12255     case STRICT_LOW_PART:
12256     case CONST_VECTOR:
12257     case CONST_FIXED:
12258     case CLRSB:
12259       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12260          can't express it in the debug info.  This can happen e.g. with some
12261          TLS UNSPECs.  */
12262       break;
12263
12264     case CONST_STRING:
12265       resolve_one_addr (&rtl, NULL);
12266       goto symref;
12267
12268     default:
12269 #ifdef ENABLE_CHECKING
12270       print_rtl (stderr, rtl);
12271       gcc_unreachable ();
12272 #else
12273       break;
12274 #endif
12275     }
12276
12277   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12278     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12279
12280   return mem_loc_result;
12281 }
12282
12283 /* Return a descriptor that describes the concatenation of two locations.
12284    This is typically a complex variable.  */
12285
12286 static dw_loc_descr_ref
12287 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12288 {
12289   dw_loc_descr_ref cc_loc_result = NULL;
12290   dw_loc_descr_ref x0_ref
12291     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12292   dw_loc_descr_ref x1_ref
12293     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12294
12295   if (x0_ref == 0 || x1_ref == 0)
12296     return 0;
12297
12298   cc_loc_result = x0_ref;
12299   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12300
12301   add_loc_descr (&cc_loc_result, x1_ref);
12302   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12303
12304   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12305     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12306
12307   return cc_loc_result;
12308 }
12309
12310 /* Return a descriptor that describes the concatenation of N
12311    locations.  */
12312
12313 static dw_loc_descr_ref
12314 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12315 {
12316   unsigned int i;
12317   dw_loc_descr_ref cc_loc_result = NULL;
12318   unsigned int n = XVECLEN (concatn, 0);
12319
12320   for (i = 0; i < n; ++i)
12321     {
12322       dw_loc_descr_ref ref;
12323       rtx x = XVECEXP (concatn, 0, i);
12324
12325       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12326       if (ref == NULL)
12327         return NULL;
12328
12329       add_loc_descr (&cc_loc_result, ref);
12330       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12331     }
12332
12333   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12334     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12335
12336   return cc_loc_result;
12337 }
12338
12339 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12340    for DEBUG_IMPLICIT_PTR RTL.  */
12341
12342 static dw_loc_descr_ref
12343 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12344 {
12345   dw_loc_descr_ref ret;
12346   dw_die_ref ref;
12347
12348   if (dwarf_strict)
12349     return NULL;
12350   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12351               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12352               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12353   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12354   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12355   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12356   if (ref)
12357     {
12358       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12359       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12360       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12361     }
12362   else
12363     {
12364       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12365       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12366     }
12367   return ret;
12368 }
12369
12370 /* Output a proper Dwarf location descriptor for a variable or parameter
12371    which is either allocated in a register or in a memory location.  For a
12372    register, we just generate an OP_REG and the register number.  For a
12373    memory location we provide a Dwarf postfix expression describing how to
12374    generate the (dynamic) address of the object onto the address stack.
12375
12376    MODE is mode of the decl if this loc_descriptor is going to be used in
12377    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12378    allowed, VOIDmode otherwise.
12379
12380    If we don't know how to describe it, return 0.  */
12381
12382 static dw_loc_descr_ref
12383 loc_descriptor (rtx rtl, enum machine_mode mode,
12384                 enum var_init_status initialized)
12385 {
12386   dw_loc_descr_ref loc_result = NULL;
12387
12388   switch (GET_CODE (rtl))
12389     {
12390     case SUBREG:
12391       /* The case of a subreg may arise when we have a local (register)
12392          variable or a formal (register) parameter which doesn't quite fill
12393          up an entire register.  For now, just assume that it is
12394          legitimate to make the Dwarf info refer to the whole register which
12395          contains the given subreg.  */
12396       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12397         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12398       else
12399         goto do_default;
12400       break;
12401
12402     case REG:
12403       loc_result = reg_loc_descriptor (rtl, initialized);
12404       break;
12405
12406     case MEM:
12407       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12408                                        GET_MODE (rtl), initialized);
12409       if (loc_result == NULL)
12410         loc_result = tls_mem_loc_descriptor (rtl);
12411       if (loc_result == NULL)
12412         {
12413           rtx new_rtl = avoid_constant_pool_reference (rtl);
12414           if (new_rtl != rtl)
12415             loc_result = loc_descriptor (new_rtl, mode, initialized);
12416         }
12417       break;
12418
12419     case CONCAT:
12420       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12421                                           initialized);
12422       break;
12423
12424     case CONCATN:
12425       loc_result = concatn_loc_descriptor (rtl, initialized);
12426       break;
12427
12428     case VAR_LOCATION:
12429       /* Single part.  */
12430       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12431         {
12432           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12433           if (GET_CODE (loc) == EXPR_LIST)
12434             loc = XEXP (loc, 0);
12435           loc_result = loc_descriptor (loc, mode, initialized);
12436           break;
12437         }
12438
12439       rtl = XEXP (rtl, 1);
12440       /* FALLTHRU */
12441
12442     case PARALLEL:
12443       {
12444         rtvec par_elems = XVEC (rtl, 0);
12445         int num_elem = GET_NUM_ELEM (par_elems);
12446         enum machine_mode mode;
12447         int i;
12448
12449         /* Create the first one, so we have something to add to.  */
12450         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12451                                      VOIDmode, initialized);
12452         if (loc_result == NULL)
12453           return NULL;
12454         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12455         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12456         for (i = 1; i < num_elem; i++)
12457           {
12458             dw_loc_descr_ref temp;
12459
12460             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12461                                    VOIDmode, initialized);
12462             if (temp == NULL)
12463               return NULL;
12464             add_loc_descr (&loc_result, temp);
12465             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12466             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12467           }
12468       }
12469       break;
12470
12471     case CONST_INT:
12472       if (mode != VOIDmode && mode != BLKmode)
12473         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12474                                                     INTVAL (rtl));
12475       break;
12476
12477     case CONST_DOUBLE:
12478       if (mode == VOIDmode)
12479         mode = GET_MODE (rtl);
12480
12481       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12482         {
12483           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12484
12485           /* Note that a CONST_DOUBLE rtx could represent either an integer
12486              or a floating-point constant.  A CONST_DOUBLE is used whenever
12487              the constant requires more than one word in order to be
12488              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12489           loc_result = new_loc_descr (DW_OP_implicit_value,
12490                                       GET_MODE_SIZE (mode), 0);
12491           if (SCALAR_FLOAT_MODE_P (mode))
12492             {
12493               unsigned int length = GET_MODE_SIZE (mode);
12494               unsigned char *array
12495                   = (unsigned char*) ggc_alloc_atomic (length);
12496
12497               insert_float (rtl, array);
12498               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12499               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12500               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12501               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12502             }
12503           else
12504             {
12505               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12506               loc_result->dw_loc_oprnd2.v.val_double
12507                 = rtx_to_double_int (rtl);
12508             }
12509         }
12510       break;
12511
12512     case CONST_VECTOR:
12513       if (mode == VOIDmode)
12514         mode = GET_MODE (rtl);
12515
12516       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12517         {
12518           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12519           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12520           unsigned char *array = (unsigned char *)
12521             ggc_alloc_atomic (length * elt_size);
12522           unsigned int i;
12523           unsigned char *p;
12524
12525           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12526           switch (GET_MODE_CLASS (mode))
12527             {
12528             case MODE_VECTOR_INT:
12529               for (i = 0, p = array; i < length; i++, p += elt_size)
12530                 {
12531                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12532                   double_int val = rtx_to_double_int (elt);
12533
12534                   if (elt_size <= sizeof (HOST_WIDE_INT))
12535                     insert_int (double_int_to_shwi (val), elt_size, p);
12536                   else
12537                     {
12538                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12539                       insert_double (val, p);
12540                     }
12541                 }
12542               break;
12543
12544             case MODE_VECTOR_FLOAT:
12545               for (i = 0, p = array; i < length; i++, p += elt_size)
12546                 {
12547                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12548                   insert_float (elt, p);
12549                 }
12550               break;
12551
12552             default:
12553               gcc_unreachable ();
12554             }
12555
12556           loc_result = new_loc_descr (DW_OP_implicit_value,
12557                                       length * elt_size, 0);
12558           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12559           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12560           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12561           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12562         }
12563       break;
12564
12565     case CONST:
12566       if (mode == VOIDmode
12567           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12568           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12569           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12570         {
12571           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12572           break;
12573         }
12574       /* FALLTHROUGH */
12575     case SYMBOL_REF:
12576       if (!const_ok_for_output (rtl))
12577         break;
12578     case LABEL_REF:
12579       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12580           && (dwarf_version >= 4 || !dwarf_strict))
12581         {
12582           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12583           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12584           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12585           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12586           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12587         }
12588       break;
12589
12590     case DEBUG_IMPLICIT_PTR:
12591       loc_result = implicit_ptr_descriptor (rtl, 0);
12592       break;
12593
12594     case PLUS:
12595       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12596           && CONST_INT_P (XEXP (rtl, 1)))
12597         {
12598           loc_result
12599             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12600           break;
12601         }
12602       /* FALLTHRU */
12603     do_default:
12604     default:
12605       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12606            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12607            && dwarf_version >= 4)
12608           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12609         {
12610           /* Value expression.  */
12611           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12612           if (loc_result)
12613             add_loc_descr (&loc_result,
12614                            new_loc_descr (DW_OP_stack_value, 0, 0));
12615         }
12616       break;
12617     }
12618
12619   return loc_result;
12620 }
12621
12622 /* We need to figure out what section we should use as the base for the
12623    address ranges where a given location is valid.
12624    1. If this particular DECL has a section associated with it, use that.
12625    2. If this function has a section associated with it, use that.
12626    3. Otherwise, use the text section.
12627    XXX: If you split a variable across multiple sections, we won't notice.  */
12628
12629 static const char *
12630 secname_for_decl (const_tree decl)
12631 {
12632   const char *secname;
12633
12634   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12635     {
12636       tree sectree = DECL_SECTION_NAME (decl);
12637       secname = TREE_STRING_POINTER (sectree);
12638     }
12639   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12640     {
12641       tree sectree = DECL_SECTION_NAME (current_function_decl);
12642       secname = TREE_STRING_POINTER (sectree);
12643     }
12644   else if (cfun && in_cold_section_p)
12645     secname = crtl->subsections.cold_section_label;
12646   else
12647     secname = text_section_label;
12648
12649   return secname;
12650 }
12651
12652 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12653
12654 static bool
12655 decl_by_reference_p (tree decl)
12656 {
12657   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12658            || TREE_CODE (decl) == VAR_DECL)
12659           && DECL_BY_REFERENCE (decl));
12660 }
12661
12662 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12663    for VARLOC.  */
12664
12665 static dw_loc_descr_ref
12666 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12667                enum var_init_status initialized)
12668 {
12669   int have_address = 0;
12670   dw_loc_descr_ref descr;
12671   enum machine_mode mode;
12672
12673   if (want_address != 2)
12674     {
12675       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12676       /* Single part.  */
12677       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12678         {
12679           varloc = PAT_VAR_LOCATION_LOC (varloc);
12680           if (GET_CODE (varloc) == EXPR_LIST)
12681             varloc = XEXP (varloc, 0);
12682           mode = GET_MODE (varloc);
12683           if (MEM_P (varloc))
12684             {
12685               rtx addr = XEXP (varloc, 0);
12686               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12687                                           mode, initialized);
12688               if (descr)
12689                 have_address = 1;
12690               else
12691                 {
12692                   rtx x = avoid_constant_pool_reference (varloc);
12693                   if (x != varloc)
12694                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12695                                                 initialized);
12696                 }
12697             }
12698           else
12699             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12700         }
12701       else
12702         return 0;
12703     }
12704   else
12705     {
12706       if (GET_CODE (varloc) == VAR_LOCATION)
12707         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12708       else
12709         mode = DECL_MODE (loc);
12710       descr = loc_descriptor (varloc, mode, initialized);
12711       have_address = 1;
12712     }
12713
12714   if (!descr)
12715     return 0;
12716
12717   if (want_address == 2 && !have_address
12718       && (dwarf_version >= 4 || !dwarf_strict))
12719     {
12720       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12721         {
12722           expansion_failed (loc, NULL_RTX,
12723                             "DWARF address size mismatch");
12724           return 0;
12725         }
12726       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12727       have_address = 1;
12728     }
12729   /* Show if we can't fill the request for an address.  */
12730   if (want_address && !have_address)
12731     {
12732       expansion_failed (loc, NULL_RTX,
12733                         "Want address and only have value");
12734       return 0;
12735     }
12736
12737   /* If we've got an address and don't want one, dereference.  */
12738   if (!want_address && have_address)
12739     {
12740       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12741       enum dwarf_location_atom op;
12742
12743       if (size > DWARF2_ADDR_SIZE || size == -1)
12744         {
12745           expansion_failed (loc, NULL_RTX,
12746                             "DWARF address size mismatch");
12747           return 0;
12748         }
12749       else if (size == DWARF2_ADDR_SIZE)
12750         op = DW_OP_deref;
12751       else
12752         op = DW_OP_deref_size;
12753
12754       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12755     }
12756
12757   return descr;
12758 }
12759
12760 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12761    if it is not possible.  */
12762
12763 static dw_loc_descr_ref
12764 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12765 {
12766   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12767     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12768   else if (dwarf_version >= 3 || !dwarf_strict)
12769     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12770   else
12771     return NULL;
12772 }
12773
12774 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12775    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12776
12777 static dw_loc_descr_ref
12778 dw_sra_loc_expr (tree decl, rtx loc)
12779 {
12780   rtx p;
12781   unsigned int padsize = 0;
12782   dw_loc_descr_ref descr, *descr_tail;
12783   unsigned HOST_WIDE_INT decl_size;
12784   rtx varloc;
12785   enum var_init_status initialized;
12786
12787   if (DECL_SIZE (decl) == NULL
12788       || !host_integerp (DECL_SIZE (decl), 1))
12789     return NULL;
12790
12791   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12792   descr = NULL;
12793   descr_tail = &descr;
12794
12795   for (p = loc; p; p = XEXP (p, 1))
12796     {
12797       unsigned int bitsize = decl_piece_bitsize (p);
12798       rtx loc_note = *decl_piece_varloc_ptr (p);
12799       dw_loc_descr_ref cur_descr;
12800       dw_loc_descr_ref *tail, last = NULL;
12801       unsigned int opsize = 0;
12802
12803       if (loc_note == NULL_RTX
12804           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12805         {
12806           padsize += bitsize;
12807           continue;
12808         }
12809       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12810       varloc = NOTE_VAR_LOCATION (loc_note);
12811       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12812       if (cur_descr == NULL)
12813         {
12814           padsize += bitsize;
12815           continue;
12816         }
12817
12818       /* Check that cur_descr either doesn't use
12819          DW_OP_*piece operations, or their sum is equal
12820          to bitsize.  Otherwise we can't embed it.  */
12821       for (tail = &cur_descr; *tail != NULL;
12822            tail = &(*tail)->dw_loc_next)
12823         if ((*tail)->dw_loc_opc == DW_OP_piece)
12824           {
12825             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12826                       * BITS_PER_UNIT;
12827             last = *tail;
12828           }
12829         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12830           {
12831             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12832             last = *tail;
12833           }
12834
12835       if (last != NULL && opsize != bitsize)
12836         {
12837           padsize += bitsize;
12838           continue;
12839         }
12840
12841       /* If there is a hole, add DW_OP_*piece after empty DWARF
12842          expression, which means that those bits are optimized out.  */
12843       if (padsize)
12844         {
12845           if (padsize > decl_size)
12846             return NULL;
12847           decl_size -= padsize;
12848           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12849           if (*descr_tail == NULL)
12850             return NULL;
12851           descr_tail = &(*descr_tail)->dw_loc_next;
12852           padsize = 0;
12853         }
12854       *descr_tail = cur_descr;
12855       descr_tail = tail;
12856       if (bitsize > decl_size)
12857         return NULL;
12858       decl_size -= bitsize;
12859       if (last == NULL)
12860         {
12861           HOST_WIDE_INT offset = 0;
12862           if (GET_CODE (varloc) == VAR_LOCATION
12863               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12864             {
12865               varloc = PAT_VAR_LOCATION_LOC (varloc);
12866               if (GET_CODE (varloc) == EXPR_LIST)
12867                 varloc = XEXP (varloc, 0);
12868             }
12869           do 
12870             {
12871               if (GET_CODE (varloc) == CONST
12872                   || GET_CODE (varloc) == SIGN_EXTEND
12873                   || GET_CODE (varloc) == ZERO_EXTEND)
12874                 varloc = XEXP (varloc, 0);
12875               else if (GET_CODE (varloc) == SUBREG)
12876                 varloc = SUBREG_REG (varloc);
12877               else
12878                 break;
12879             }
12880           while (1);
12881           /* DW_OP_bit_size offset should be zero for register
12882              or implicit location descriptions and empty location
12883              descriptions, but for memory addresses needs big endian
12884              adjustment.  */
12885           if (MEM_P (varloc))
12886             {
12887               unsigned HOST_WIDE_INT memsize
12888                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12889               if (memsize != bitsize)
12890                 {
12891                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12892                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12893                     return NULL;
12894                   if (memsize < bitsize)
12895                     return NULL;
12896                   if (BITS_BIG_ENDIAN)
12897                     offset = memsize - bitsize;
12898                 }
12899             }
12900
12901           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12902           if (*descr_tail == NULL)
12903             return NULL;
12904           descr_tail = &(*descr_tail)->dw_loc_next;
12905         }
12906     }
12907
12908   /* If there were any non-empty expressions, add padding till the end of
12909      the decl.  */
12910   if (descr != NULL && decl_size != 0)
12911     {
12912       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12913       if (*descr_tail == NULL)
12914         return NULL;
12915     }
12916   return descr;
12917 }
12918
12919 /* Return the dwarf representation of the location list LOC_LIST of
12920    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
12921    function.  */
12922
12923 static dw_loc_list_ref
12924 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12925 {
12926   const char *endname, *secname;
12927   rtx varloc;
12928   enum var_init_status initialized;
12929   struct var_loc_node *node;
12930   dw_loc_descr_ref descr;
12931   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12932   dw_loc_list_ref list = NULL;
12933   dw_loc_list_ref *listp = &list;
12934
12935   /* Now that we know what section we are using for a base,
12936      actually construct the list of locations.
12937      The first location information is what is passed to the
12938      function that creates the location list, and the remaining
12939      locations just get added on to that list.
12940      Note that we only know the start address for a location
12941      (IE location changes), so to build the range, we use
12942      the range [current location start, next location start].
12943      This means we have to special case the last node, and generate
12944      a range of [last location start, end of function label].  */
12945
12946   secname = secname_for_decl (decl);
12947
12948   for (node = loc_list->first; node; node = node->next)
12949     if (GET_CODE (node->loc) == EXPR_LIST
12950         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12951       {
12952         if (GET_CODE (node->loc) == EXPR_LIST)
12953           {
12954             /* This requires DW_OP_{,bit_}piece, which is not usable
12955                inside DWARF expressions.  */
12956             if (want_address != 2)
12957               continue;
12958             descr = dw_sra_loc_expr (decl, node->loc);
12959             if (descr == NULL)
12960               continue;
12961           }
12962         else
12963           {
12964             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12965             varloc = NOTE_VAR_LOCATION (node->loc);
12966             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12967           }
12968         if (descr)
12969           {
12970             bool range_across_switch = false;
12971             /* If section switch happens in between node->label
12972                and node->next->label (or end of function) and
12973                we can't emit it as a single entry list,
12974                emit two ranges, first one ending at the end
12975                of first partition and second one starting at the
12976                beginning of second partition.  */
12977             if (node == loc_list->last_before_switch
12978                 && (node != loc_list->first || loc_list->first->next)
12979                 && current_function_decl)
12980               {
12981                 endname = cfun->fde->dw_fde_end;
12982                 range_across_switch = true;
12983               }
12984             /* The variable has a location between NODE->LABEL and
12985                NODE->NEXT->LABEL.  */
12986             else if (node->next)
12987               endname = node->next->label;
12988             /* If the variable has a location at the last label
12989                it keeps its location until the end of function.  */
12990             else if (!current_function_decl)
12991               endname = text_end_label;
12992             else
12993               {
12994                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12995                                              current_function_funcdef_no);
12996                 endname = ggc_strdup (label_id);
12997               }
12998
12999             *listp = new_loc_list (descr, node->label, endname, secname);
13000             if (TREE_CODE (decl) == PARM_DECL
13001                 && node == loc_list->first
13002                 && GET_CODE (node->loc) == NOTE
13003                 && strcmp (node->label, endname) == 0)
13004               (*listp)->force = true;
13005             listp = &(*listp)->dw_loc_next;
13006
13007             if (range_across_switch)
13008               {
13009                 if (GET_CODE (node->loc) == EXPR_LIST)
13010                   descr = dw_sra_loc_expr (decl, node->loc);
13011                 else
13012                   {
13013                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13014                     varloc = NOTE_VAR_LOCATION (node->loc);
13015                     descr = dw_loc_list_1 (decl, varloc, want_address,
13016                                            initialized);
13017                   }
13018                 gcc_assert (descr);
13019                 /* The variable has a location between NODE->LABEL and
13020                    NODE->NEXT->LABEL.  */
13021                 if (node->next)
13022                   endname = node->next->label;
13023                 else
13024                   endname = cfun->fde->dw_fde_second_end;
13025                 *listp = new_loc_list (descr,
13026                                        cfun->fde->dw_fde_second_begin,
13027                                        endname, secname);
13028                 listp = &(*listp)->dw_loc_next;
13029               }
13030           }
13031       }
13032
13033   /* Try to avoid the overhead of a location list emitting a location
13034      expression instead, but only if we didn't have more than one
13035      location entry in the first place.  If some entries were not
13036      representable, we don't want to pretend a single entry that was
13037      applies to the entire scope in which the variable is
13038      available.  */
13039   if (list && loc_list->first->next)
13040     gen_llsym (list);
13041
13042   return list;
13043 }
13044
13045 /* Return if the loc_list has only single element and thus can be represented
13046    as location description.   */
13047
13048 static bool
13049 single_element_loc_list_p (dw_loc_list_ref list)
13050 {
13051   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13052   return !list->ll_symbol;
13053 }
13054
13055 /* To each location in list LIST add loc descr REF.  */
13056
13057 static void
13058 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13059 {
13060   dw_loc_descr_ref copy;
13061   add_loc_descr (&list->expr, ref);
13062   list = list->dw_loc_next;
13063   while (list)
13064     {
13065       copy = ggc_alloc_dw_loc_descr_node ();
13066       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13067       add_loc_descr (&list->expr, copy);
13068       while (copy->dw_loc_next)
13069         {
13070           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13071           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13072           copy->dw_loc_next = new_copy;
13073           copy = new_copy;
13074         }
13075       list = list->dw_loc_next;
13076     }
13077 }
13078
13079 /* Given two lists RET and LIST
13080    produce location list that is result of adding expression in LIST
13081    to expression in RET on each possition in program.
13082    Might be destructive on both RET and LIST.
13083
13084    TODO: We handle only simple cases of RET or LIST having at most one
13085    element. General case would inolve sorting the lists in program order
13086    and merging them that will need some additional work.
13087    Adding that will improve quality of debug info especially for SRA-ed
13088    structures.  */
13089
13090 static void
13091 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13092 {
13093   if (!list)
13094     return;
13095   if (!*ret)
13096     {
13097       *ret = list;
13098       return;
13099     }
13100   if (!list->dw_loc_next)
13101     {
13102       add_loc_descr_to_each (*ret, list->expr);
13103       return;
13104     }
13105   if (!(*ret)->dw_loc_next)
13106     {
13107       add_loc_descr_to_each (list, (*ret)->expr);
13108       *ret = list;
13109       return;
13110     }
13111   expansion_failed (NULL_TREE, NULL_RTX,
13112                     "Don't know how to merge two non-trivial"
13113                     " location lists.\n");
13114   *ret = NULL;
13115   return;
13116 }
13117
13118 /* LOC is constant expression.  Try a luck, look it up in constant
13119    pool and return its loc_descr of its address.  */
13120
13121 static dw_loc_descr_ref
13122 cst_pool_loc_descr (tree loc)
13123 {
13124   /* Get an RTL for this, if something has been emitted.  */
13125   rtx rtl = lookup_constant_def (loc);
13126
13127   if (!rtl || !MEM_P (rtl))
13128     {
13129       gcc_assert (!rtl);
13130       return 0;
13131     }
13132   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13133
13134   /* TODO: We might get more coverage if we was actually delaying expansion
13135      of all expressions till end of compilation when constant pools are fully
13136      populated.  */
13137   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13138     {
13139       expansion_failed (loc, NULL_RTX,
13140                         "CST value in contant pool but not marked.");
13141       return 0;
13142     }
13143   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13144                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13145 }
13146
13147 /* Return dw_loc_list representing address of addr_expr LOC
13148    by looking for innder INDIRECT_REF expression and turing it
13149    into simple arithmetics.  */
13150
13151 static dw_loc_list_ref
13152 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13153 {
13154   tree obj, offset;
13155   HOST_WIDE_INT bitsize, bitpos, bytepos;
13156   enum machine_mode mode;
13157   int volatilep;
13158   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13159   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13160
13161   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13162                              &bitsize, &bitpos, &offset, &mode,
13163                              &unsignedp, &volatilep, false);
13164   STRIP_NOPS (obj);
13165   if (bitpos % BITS_PER_UNIT)
13166     {
13167       expansion_failed (loc, NULL_RTX, "bitfield access");
13168       return 0;
13169     }
13170   if (!INDIRECT_REF_P (obj))
13171     {
13172       expansion_failed (obj,
13173                         NULL_RTX, "no indirect ref in inner refrence");
13174       return 0;
13175     }
13176   if (!offset && !bitpos)
13177     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13178   else if (toplev
13179            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13180            && (dwarf_version >= 4 || !dwarf_strict))
13181     {
13182       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13183       if (!list_ret)
13184         return 0;
13185       if (offset)
13186         {
13187           /* Variable offset.  */
13188           list_ret1 = loc_list_from_tree (offset, 0);
13189           if (list_ret1 == 0)
13190             return 0;
13191           add_loc_list (&list_ret, list_ret1);
13192           if (!list_ret)
13193             return 0;
13194           add_loc_descr_to_each (list_ret,
13195                                  new_loc_descr (DW_OP_plus, 0, 0));
13196         }
13197       bytepos = bitpos / BITS_PER_UNIT;
13198       if (bytepos > 0)
13199         add_loc_descr_to_each (list_ret,
13200                                new_loc_descr (DW_OP_plus_uconst,
13201                                               bytepos, 0));
13202       else if (bytepos < 0)
13203         loc_list_plus_const (list_ret, bytepos);
13204       add_loc_descr_to_each (list_ret,
13205                              new_loc_descr (DW_OP_stack_value, 0, 0));
13206     }
13207   return list_ret;
13208 }
13209
13210
13211 /* Generate Dwarf location list representing LOC.
13212    If WANT_ADDRESS is false, expression computing LOC will be computed
13213    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13214    if WANT_ADDRESS is 2, expression computing address useable in location
13215      will be returned (i.e. DW_OP_reg can be used
13216      to refer to register values).  */
13217
13218 static dw_loc_list_ref
13219 loc_list_from_tree (tree loc, int want_address)
13220 {
13221   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13222   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13223   int have_address = 0;
13224   enum dwarf_location_atom op;
13225
13226   /* ??? Most of the time we do not take proper care for sign/zero
13227      extending the values properly.  Hopefully this won't be a real
13228      problem...  */
13229
13230   switch (TREE_CODE (loc))
13231     {
13232     case ERROR_MARK:
13233       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13234       return 0;
13235
13236     case PLACEHOLDER_EXPR:
13237       /* This case involves extracting fields from an object to determine the
13238          position of other fields.  We don't try to encode this here.  The
13239          only user of this is Ada, which encodes the needed information using
13240          the names of types.  */
13241       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13242       return 0;
13243
13244     case CALL_EXPR:
13245       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13246       /* There are no opcodes for these operations.  */
13247       return 0;
13248
13249     case PREINCREMENT_EXPR:
13250     case PREDECREMENT_EXPR:
13251     case POSTINCREMENT_EXPR:
13252     case POSTDECREMENT_EXPR:
13253       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13254       /* There are no opcodes for these operations.  */
13255       return 0;
13256
13257     case ADDR_EXPR:
13258       /* If we already want an address, see if there is INDIRECT_REF inside
13259          e.g. for &this->field.  */
13260       if (want_address)
13261         {
13262           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13263                        (loc, want_address == 2);
13264           if (list_ret)
13265             have_address = 1;
13266           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13267                    && (ret = cst_pool_loc_descr (loc)))
13268             have_address = 1;
13269         }
13270         /* Otherwise, process the argument and look for the address.  */
13271       if (!list_ret && !ret)
13272         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13273       else
13274         {
13275           if (want_address)
13276             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13277           return NULL;
13278         }
13279       break;
13280
13281     case VAR_DECL:
13282       if (DECL_THREAD_LOCAL_P (loc))
13283         {
13284           rtx rtl;
13285           enum dwarf_location_atom first_op;
13286           enum dwarf_location_atom second_op;
13287           bool dtprel = false;
13288
13289           if (targetm.have_tls)
13290             {
13291               /* If this is not defined, we have no way to emit the
13292                  data.  */
13293               if (!targetm.asm_out.output_dwarf_dtprel)
13294                 return 0;
13295
13296                /* The way DW_OP_GNU_push_tls_address is specified, we
13297                   can only look up addresses of objects in the current
13298                   module.  We used DW_OP_addr as first op, but that's
13299                   wrong, because DW_OP_addr is relocated by the debug
13300                   info consumer, while DW_OP_GNU_push_tls_address
13301                   operand shouldn't be.  */
13302               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13303                 return 0;
13304               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13305               dtprel = true;
13306               second_op = DW_OP_GNU_push_tls_address;
13307             }
13308           else
13309             {
13310               if (!targetm.emutls.debug_form_tls_address
13311                   || !(dwarf_version >= 3 || !dwarf_strict))
13312                 return 0;
13313               /* We stuffed the control variable into the DECL_VALUE_EXPR
13314                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13315                  no longer appear in gimple code.  We used the control
13316                  variable in specific so that we could pick it up here.  */
13317               loc = DECL_VALUE_EXPR (loc);
13318               first_op = DW_OP_addr;
13319               second_op = DW_OP_form_tls_address;
13320             }
13321
13322           rtl = rtl_for_decl_location (loc);
13323           if (rtl == NULL_RTX)
13324             return 0;
13325
13326           if (!MEM_P (rtl))
13327             return 0;
13328           rtl = XEXP (rtl, 0);
13329           if (! CONSTANT_P (rtl))
13330             return 0;
13331
13332           ret = new_loc_descr (first_op, 0, 0);
13333           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13334           ret->dw_loc_oprnd1.v.val_addr = rtl;
13335           ret->dtprel = dtprel;
13336
13337           ret1 = new_loc_descr (second_op, 0, 0);
13338           add_loc_descr (&ret, ret1);
13339
13340           have_address = 1;
13341           break;
13342         }
13343       /* FALLTHRU */
13344
13345     case PARM_DECL:
13346     case RESULT_DECL:
13347       if (DECL_HAS_VALUE_EXPR_P (loc))
13348         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13349                                    want_address);
13350       /* FALLTHRU */
13351
13352     case FUNCTION_DECL:
13353       {
13354         rtx rtl;
13355         var_loc_list *loc_list = lookup_decl_loc (loc);
13356
13357         if (loc_list && loc_list->first)
13358           {
13359             list_ret = dw_loc_list (loc_list, loc, want_address);
13360             have_address = want_address != 0;
13361             break;
13362           }
13363         rtl = rtl_for_decl_location (loc);
13364         if (rtl == NULL_RTX)
13365           {
13366             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13367             return 0;
13368           }
13369         else if (CONST_INT_P (rtl))
13370           {
13371             HOST_WIDE_INT val = INTVAL (rtl);
13372             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13373               val &= GET_MODE_MASK (DECL_MODE (loc));
13374             ret = int_loc_descriptor (val);
13375           }
13376         else if (GET_CODE (rtl) == CONST_STRING)
13377           {
13378             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13379             return 0;
13380           }
13381         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13382           {
13383             ret = new_loc_descr (DW_OP_addr, 0, 0);
13384             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13385             ret->dw_loc_oprnd1.v.val_addr = rtl;
13386           }
13387         else
13388           {
13389             enum machine_mode mode, mem_mode;
13390
13391             /* Certain constructs can only be represented at top-level.  */
13392             if (want_address == 2)
13393               {
13394                 ret = loc_descriptor (rtl, VOIDmode,
13395                                       VAR_INIT_STATUS_INITIALIZED);
13396                 have_address = 1;
13397               }
13398             else
13399               {
13400                 mode = GET_MODE (rtl);
13401                 mem_mode = VOIDmode;
13402                 if (MEM_P (rtl))
13403                   {
13404                     mem_mode = mode;
13405                     mode = get_address_mode (rtl);
13406                     rtl = XEXP (rtl, 0);
13407                     have_address = 1;
13408                   }
13409                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13410                                           VAR_INIT_STATUS_INITIALIZED);
13411               }
13412             if (!ret)
13413               expansion_failed (loc, rtl,
13414                                 "failed to produce loc descriptor for rtl");
13415           }
13416       }
13417       break;
13418
13419     case MEM_REF:
13420       /* ??? FIXME.  */
13421       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13422         return 0;
13423       /* Fallthru.  */
13424     case INDIRECT_REF:
13425       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13426       have_address = 1;
13427       break;
13428
13429     case COMPOUND_EXPR:
13430       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13431
13432     CASE_CONVERT:
13433     case VIEW_CONVERT_EXPR:
13434     case SAVE_EXPR:
13435     case MODIFY_EXPR:
13436       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13437
13438     case COMPONENT_REF:
13439     case BIT_FIELD_REF:
13440     case ARRAY_REF:
13441     case ARRAY_RANGE_REF:
13442     case REALPART_EXPR:
13443     case IMAGPART_EXPR:
13444       {
13445         tree obj, offset;
13446         HOST_WIDE_INT bitsize, bitpos, bytepos;
13447         enum machine_mode mode;
13448         int volatilep;
13449         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13450
13451         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13452                                    &unsignedp, &volatilep, false);
13453
13454         gcc_assert (obj != loc);
13455
13456         list_ret = loc_list_from_tree (obj,
13457                                        want_address == 2
13458                                        && !bitpos && !offset ? 2 : 1);
13459         /* TODO: We can extract value of the small expression via shifting even
13460            for nonzero bitpos.  */
13461         if (list_ret == 0)
13462           return 0;
13463         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13464           {
13465             expansion_failed (loc, NULL_RTX,
13466                               "bitfield access");
13467             return 0;
13468           }
13469
13470         if (offset != NULL_TREE)
13471           {
13472             /* Variable offset.  */
13473             list_ret1 = loc_list_from_tree (offset, 0);
13474             if (list_ret1 == 0)
13475               return 0;
13476             add_loc_list (&list_ret, list_ret1);
13477             if (!list_ret)
13478               return 0;
13479             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13480           }
13481
13482         bytepos = bitpos / BITS_PER_UNIT;
13483         if (bytepos > 0)
13484           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13485         else if (bytepos < 0)
13486           loc_list_plus_const (list_ret, bytepos);
13487
13488         have_address = 1;
13489         break;
13490       }
13491
13492     case INTEGER_CST:
13493       if ((want_address || !host_integerp (loc, 0))
13494           && (ret = cst_pool_loc_descr (loc)))
13495         have_address = 1;
13496       else if (want_address == 2
13497                && host_integerp (loc, 0)
13498                && (ret = address_of_int_loc_descriptor
13499                            (int_size_in_bytes (TREE_TYPE (loc)),
13500                             tree_low_cst (loc, 0))))
13501         have_address = 1;
13502       else if (host_integerp (loc, 0))
13503         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13504       else
13505         {
13506           expansion_failed (loc, NULL_RTX,
13507                             "Integer operand is not host integer");
13508           return 0;
13509         }
13510       break;
13511
13512     case CONSTRUCTOR:
13513     case REAL_CST:
13514     case STRING_CST:
13515     case COMPLEX_CST:
13516       if ((ret = cst_pool_loc_descr (loc)))
13517         have_address = 1;
13518       else
13519       /* We can construct small constants here using int_loc_descriptor.  */
13520         expansion_failed (loc, NULL_RTX,
13521                           "constructor or constant not in constant pool");
13522       break;
13523
13524     case TRUTH_AND_EXPR:
13525     case TRUTH_ANDIF_EXPR:
13526     case BIT_AND_EXPR:
13527       op = DW_OP_and;
13528       goto do_binop;
13529
13530     case TRUTH_XOR_EXPR:
13531     case BIT_XOR_EXPR:
13532       op = DW_OP_xor;
13533       goto do_binop;
13534
13535     case TRUTH_OR_EXPR:
13536     case TRUTH_ORIF_EXPR:
13537     case BIT_IOR_EXPR:
13538       op = DW_OP_or;
13539       goto do_binop;
13540
13541     case FLOOR_DIV_EXPR:
13542     case CEIL_DIV_EXPR:
13543     case ROUND_DIV_EXPR:
13544     case TRUNC_DIV_EXPR:
13545       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13546         return 0;
13547       op = DW_OP_div;
13548       goto do_binop;
13549
13550     case MINUS_EXPR:
13551       op = DW_OP_minus;
13552       goto do_binop;
13553
13554     case FLOOR_MOD_EXPR:
13555     case CEIL_MOD_EXPR:
13556     case ROUND_MOD_EXPR:
13557     case TRUNC_MOD_EXPR:
13558       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13559         {
13560           op = DW_OP_mod;
13561           goto do_binop;
13562         }
13563       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13564       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13565       if (list_ret == 0 || list_ret1 == 0)
13566         return 0;
13567
13568       add_loc_list (&list_ret, list_ret1);
13569       if (list_ret == 0)
13570         return 0;
13571       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13572       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13573       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13574       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13575       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13576       break;
13577
13578     case MULT_EXPR:
13579       op = DW_OP_mul;
13580       goto do_binop;
13581
13582     case LSHIFT_EXPR:
13583       op = DW_OP_shl;
13584       goto do_binop;
13585
13586     case RSHIFT_EXPR:
13587       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13588       goto do_binop;
13589
13590     case POINTER_PLUS_EXPR:
13591     case PLUS_EXPR:
13592       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13593         {
13594           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13595           if (list_ret == 0)
13596             return 0;
13597
13598           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13599           break;
13600         }
13601
13602       op = DW_OP_plus;
13603       goto do_binop;
13604
13605     case LE_EXPR:
13606       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13607         return 0;
13608
13609       op = DW_OP_le;
13610       goto do_binop;
13611
13612     case GE_EXPR:
13613       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13614         return 0;
13615
13616       op = DW_OP_ge;
13617       goto do_binop;
13618
13619     case LT_EXPR:
13620       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13621         return 0;
13622
13623       op = DW_OP_lt;
13624       goto do_binop;
13625
13626     case GT_EXPR:
13627       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13628         return 0;
13629
13630       op = DW_OP_gt;
13631       goto do_binop;
13632
13633     case EQ_EXPR:
13634       op = DW_OP_eq;
13635       goto do_binop;
13636
13637     case NE_EXPR:
13638       op = DW_OP_ne;
13639       goto do_binop;
13640
13641     do_binop:
13642       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13643       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13644       if (list_ret == 0 || list_ret1 == 0)
13645         return 0;
13646
13647       add_loc_list (&list_ret, list_ret1);
13648       if (list_ret == 0)
13649         return 0;
13650       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13651       break;
13652
13653     case TRUTH_NOT_EXPR:
13654     case BIT_NOT_EXPR:
13655       op = DW_OP_not;
13656       goto do_unop;
13657
13658     case ABS_EXPR:
13659       op = DW_OP_abs;
13660       goto do_unop;
13661
13662     case NEGATE_EXPR:
13663       op = DW_OP_neg;
13664       goto do_unop;
13665
13666     do_unop:
13667       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13668       if (list_ret == 0)
13669         return 0;
13670
13671       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13672       break;
13673
13674     case MIN_EXPR:
13675     case MAX_EXPR:
13676       {
13677         const enum tree_code code =
13678           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13679
13680         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13681                       build2 (code, integer_type_node,
13682                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13683                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13684       }
13685
13686       /* ... fall through ...  */
13687
13688     case COND_EXPR:
13689       {
13690         dw_loc_descr_ref lhs
13691           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13692         dw_loc_list_ref rhs
13693           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13694         dw_loc_descr_ref bra_node, jump_node, tmp;
13695
13696         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13697         if (list_ret == 0 || lhs == 0 || rhs == 0)
13698           return 0;
13699
13700         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13701         add_loc_descr_to_each (list_ret, bra_node);
13702
13703         add_loc_list (&list_ret, rhs);
13704         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13705         add_loc_descr_to_each (list_ret, jump_node);
13706
13707         add_loc_descr_to_each (list_ret, lhs);
13708         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13709         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13710
13711         /* ??? Need a node to point the skip at.  Use a nop.  */
13712         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13713         add_loc_descr_to_each (list_ret, tmp);
13714         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13715         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13716       }
13717       break;
13718
13719     case FIX_TRUNC_EXPR:
13720       return 0;
13721
13722     default:
13723       /* Leave front-end specific codes as simply unknown.  This comes
13724          up, for instance, with the C STMT_EXPR.  */
13725       if ((unsigned int) TREE_CODE (loc)
13726           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13727         {
13728           expansion_failed (loc, NULL_RTX,
13729                             "language specific tree node");
13730           return 0;
13731         }
13732
13733 #ifdef ENABLE_CHECKING
13734       /* Otherwise this is a generic code; we should just lists all of
13735          these explicitly.  We forgot one.  */
13736       gcc_unreachable ();
13737 #else
13738       /* In a release build, we want to degrade gracefully: better to
13739          generate incomplete debugging information than to crash.  */
13740       return NULL;
13741 #endif
13742     }
13743
13744   if (!ret && !list_ret)
13745     return 0;
13746
13747   if (want_address == 2 && !have_address
13748       && (dwarf_version >= 4 || !dwarf_strict))
13749     {
13750       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13751         {
13752           expansion_failed (loc, NULL_RTX,
13753                             "DWARF address size mismatch");
13754           return 0;
13755         }
13756       if (ret)
13757         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13758       else
13759         add_loc_descr_to_each (list_ret,
13760                                new_loc_descr (DW_OP_stack_value, 0, 0));
13761       have_address = 1;
13762     }
13763   /* Show if we can't fill the request for an address.  */
13764   if (want_address && !have_address)
13765     {
13766       expansion_failed (loc, NULL_RTX,
13767                         "Want address and only have value");
13768       return 0;
13769     }
13770
13771   gcc_assert (!ret || !list_ret);
13772
13773   /* If we've got an address and don't want one, dereference.  */
13774   if (!want_address && have_address)
13775     {
13776       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13777
13778       if (size > DWARF2_ADDR_SIZE || size == -1)
13779         {
13780           expansion_failed (loc, NULL_RTX,
13781                             "DWARF address size mismatch");
13782           return 0;
13783         }
13784       else if (size == DWARF2_ADDR_SIZE)
13785         op = DW_OP_deref;
13786       else
13787         op = DW_OP_deref_size;
13788
13789       if (ret)
13790         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13791       else
13792         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13793     }
13794   if (ret)
13795     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13796
13797   return list_ret;
13798 }
13799
13800 /* Same as above but return only single location expression.  */
13801 static dw_loc_descr_ref
13802 loc_descriptor_from_tree (tree loc, int want_address)
13803 {
13804   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13805   if (!ret)
13806     return NULL;
13807   if (ret->dw_loc_next)
13808     {
13809       expansion_failed (loc, NULL_RTX,
13810                         "Location list where only loc descriptor needed");
13811       return NULL;
13812     }
13813   return ret->expr;
13814 }
13815
13816 /* Given a value, round it up to the lowest multiple of `boundary'
13817    which is not less than the value itself.  */
13818
13819 static inline HOST_WIDE_INT
13820 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13821 {
13822   return (((value + boundary - 1) / boundary) * boundary);
13823 }
13824
13825 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13826    pointer to the declared type for the relevant field variable, or return
13827    `integer_type_node' if the given node turns out to be an
13828    ERROR_MARK node.  */
13829
13830 static inline tree
13831 field_type (const_tree decl)
13832 {
13833   tree type;
13834
13835   if (TREE_CODE (decl) == ERROR_MARK)
13836     return integer_type_node;
13837
13838   type = DECL_BIT_FIELD_TYPE (decl);
13839   if (type == NULL_TREE)
13840     type = TREE_TYPE (decl);
13841
13842   return type;
13843 }
13844
13845 /* Given a pointer to a tree node, return the alignment in bits for
13846    it, or else return BITS_PER_WORD if the node actually turns out to
13847    be an ERROR_MARK node.  */
13848
13849 static inline unsigned
13850 simple_type_align_in_bits (const_tree type)
13851 {
13852   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13853 }
13854
13855 static inline unsigned
13856 simple_decl_align_in_bits (const_tree decl)
13857 {
13858   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13859 }
13860
13861 /* Return the result of rounding T up to ALIGN.  */
13862
13863 static inline double_int
13864 round_up_to_align (double_int t, unsigned int align)
13865 {
13866   double_int alignd = uhwi_to_double_int (align);
13867   t = double_int_add (t, alignd);
13868   t = double_int_add (t, double_int_minus_one);
13869   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13870   t = double_int_mul (t, alignd);
13871   return t;
13872 }
13873
13874 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13875    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13876    or return 0 if we are unable to determine what that offset is, either
13877    because the argument turns out to be a pointer to an ERROR_MARK node, or
13878    because the offset is actually variable.  (We can't handle the latter case
13879    just yet).  */
13880
13881 static HOST_WIDE_INT
13882 field_byte_offset (const_tree decl)
13883 {
13884   double_int object_offset_in_bits;
13885   double_int object_offset_in_bytes;
13886   double_int bitpos_int;
13887
13888   if (TREE_CODE (decl) == ERROR_MARK)
13889     return 0;
13890
13891   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13892
13893   /* We cannot yet cope with fields whose positions are variable, so
13894      for now, when we see such things, we simply return 0.  Someday, we may
13895      be able to handle such cases, but it will be damn difficult.  */
13896   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13897     return 0;
13898
13899   bitpos_int = tree_to_double_int (bit_position (decl));
13900
13901 #ifdef PCC_BITFIELD_TYPE_MATTERS
13902   if (PCC_BITFIELD_TYPE_MATTERS)
13903     {
13904       tree type;
13905       tree field_size_tree;
13906       double_int deepest_bitpos;
13907       double_int field_size_in_bits;
13908       unsigned int type_align_in_bits;
13909       unsigned int decl_align_in_bits;
13910       double_int type_size_in_bits;
13911
13912       type = field_type (decl);
13913       type_size_in_bits = double_int_type_size_in_bits (type);
13914       type_align_in_bits = simple_type_align_in_bits (type);
13915
13916       field_size_tree = DECL_SIZE (decl);
13917
13918       /* The size could be unspecified if there was an error, or for
13919          a flexible array member.  */
13920       if (!field_size_tree)
13921         field_size_tree = bitsize_zero_node;
13922
13923       /* If the size of the field is not constant, use the type size.  */
13924       if (TREE_CODE (field_size_tree) == INTEGER_CST)
13925         field_size_in_bits = tree_to_double_int (field_size_tree);
13926       else
13927         field_size_in_bits = type_size_in_bits;
13928
13929       decl_align_in_bits = simple_decl_align_in_bits (decl);
13930
13931       /* The GCC front-end doesn't make any attempt to keep track of the
13932          starting bit offset (relative to the start of the containing
13933          structure type) of the hypothetical "containing object" for a
13934          bit-field.  Thus, when computing the byte offset value for the
13935          start of the "containing object" of a bit-field, we must deduce
13936          this information on our own. This can be rather tricky to do in
13937          some cases.  For example, handling the following structure type
13938          definition when compiling for an i386/i486 target (which only
13939          aligns long long's to 32-bit boundaries) can be very tricky:
13940
13941          struct S { int field1; long long field2:31; };
13942
13943          Fortunately, there is a simple rule-of-thumb which can be used
13944          in such cases.  When compiling for an i386/i486, GCC will
13945          allocate 8 bytes for the structure shown above.  It decides to
13946          do this based upon one simple rule for bit-field allocation.
13947          GCC allocates each "containing object" for each bit-field at
13948          the first (i.e. lowest addressed) legitimate alignment boundary
13949          (based upon the required minimum alignment for the declared
13950          type of the field) which it can possibly use, subject to the
13951          condition that there is still enough available space remaining
13952          in the containing object (when allocated at the selected point)
13953          to fully accommodate all of the bits of the bit-field itself.
13954
13955          This simple rule makes it obvious why GCC allocates 8 bytes for
13956          each object of the structure type shown above.  When looking
13957          for a place to allocate the "containing object" for `field2',
13958          the compiler simply tries to allocate a 64-bit "containing
13959          object" at each successive 32-bit boundary (starting at zero)
13960          until it finds a place to allocate that 64- bit field such that
13961          at least 31 contiguous (and previously unallocated) bits remain
13962          within that selected 64 bit field.  (As it turns out, for the
13963          example above, the compiler finds it is OK to allocate the
13964          "containing object" 64-bit field at bit-offset zero within the
13965          structure type.)
13966
13967          Here we attempt to work backwards from the limited set of facts
13968          we're given, and we try to deduce from those facts, where GCC
13969          must have believed that the containing object started (within
13970          the structure type). The value we deduce is then used (by the
13971          callers of this routine) to generate DW_AT_location and
13972          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13973          the case of DW_AT_location, regular fields as well).  */
13974
13975       /* Figure out the bit-distance from the start of the structure to
13976          the "deepest" bit of the bit-field.  */
13977       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
13978
13979       /* This is the tricky part.  Use some fancy footwork to deduce
13980          where the lowest addressed bit of the containing object must
13981          be.  */
13982       object_offset_in_bits
13983         = double_int_sub (deepest_bitpos, type_size_in_bits);
13984
13985       /* Round up to type_align by default.  This works best for
13986          bitfields.  */
13987       object_offset_in_bits
13988         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13989
13990       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
13991         {
13992           object_offset_in_bits
13993             = double_int_sub (deepest_bitpos, type_size_in_bits);
13994
13995           /* Round up to decl_align instead.  */
13996           object_offset_in_bits
13997             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13998         }
13999     }
14000   else
14001 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14002     object_offset_in_bits = bitpos_int;
14003
14004   object_offset_in_bytes
14005     = double_int_div (object_offset_in_bits,
14006                       uhwi_to_double_int (BITS_PER_UNIT), true,
14007                       TRUNC_DIV_EXPR);
14008   return double_int_to_shwi (object_offset_in_bytes);
14009 }
14010 \f
14011 /* The following routines define various Dwarf attributes and any data
14012    associated with them.  */
14013
14014 /* Add a location description attribute value to a DIE.
14015
14016    This emits location attributes suitable for whole variables and
14017    whole parameters.  Note that the location attributes for struct fields are
14018    generated by the routine `data_member_location_attribute' below.  */
14019
14020 static inline void
14021 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14022                              dw_loc_list_ref descr)
14023 {
14024   if (descr == 0)
14025     return;
14026   if (single_element_loc_list_p (descr))
14027     add_AT_loc (die, attr_kind, descr->expr);
14028   else
14029     add_AT_loc_list (die, attr_kind, descr);
14030 }
14031
14032 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14033
14034 static void
14035 add_accessibility_attribute (dw_die_ref die, tree decl)
14036 {
14037   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14038      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14039      the default has always been DW_ACCESS_public.  */
14040   if (TREE_PROTECTED (decl))
14041     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14042   else if (TREE_PRIVATE (decl))
14043     {
14044       if (dwarf_version == 2
14045           || die->die_parent == NULL
14046           || die->die_parent->die_tag != DW_TAG_class_type)
14047         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14048     }
14049   else if (dwarf_version > 2
14050            && die->die_parent
14051            && die->die_parent->die_tag == DW_TAG_class_type)
14052     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14053 }
14054
14055 /* Attach the specialized form of location attribute used for data members of
14056    struct and union types.  In the special case of a FIELD_DECL node which
14057    represents a bit-field, the "offset" part of this special location
14058    descriptor must indicate the distance in bytes from the lowest-addressed
14059    byte of the containing struct or union type to the lowest-addressed byte of
14060    the "containing object" for the bit-field.  (See the `field_byte_offset'
14061    function above).
14062
14063    For any given bit-field, the "containing object" is a hypothetical object
14064    (of some integral or enum type) within which the given bit-field lives.  The
14065    type of this hypothetical "containing object" is always the same as the
14066    declared type of the individual bit-field itself (for GCC anyway... the
14067    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14068    bytes) of the hypothetical "containing object" which will be given in the
14069    DW_AT_byte_size attribute for this bit-field.  (See the
14070    `byte_size_attribute' function below.)  It is also used when calculating the
14071    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14072    function below.)  */
14073
14074 static void
14075 add_data_member_location_attribute (dw_die_ref die, tree decl)
14076 {
14077   HOST_WIDE_INT offset;
14078   dw_loc_descr_ref loc_descr = 0;
14079
14080   if (TREE_CODE (decl) == TREE_BINFO)
14081     {
14082       /* We're working on the TAG_inheritance for a base class.  */
14083       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14084         {
14085           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14086              aren't at a fixed offset from all (sub)objects of the same
14087              type.  We need to extract the appropriate offset from our
14088              vtable.  The following dwarf expression means
14089
14090                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14091
14092              This is specific to the V3 ABI, of course.  */
14093
14094           dw_loc_descr_ref tmp;
14095
14096           /* Make a copy of the object address.  */
14097           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14098           add_loc_descr (&loc_descr, tmp);
14099
14100           /* Extract the vtable address.  */
14101           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14102           add_loc_descr (&loc_descr, tmp);
14103
14104           /* Calculate the address of the offset.  */
14105           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14106           gcc_assert (offset < 0);
14107
14108           tmp = int_loc_descriptor (-offset);
14109           add_loc_descr (&loc_descr, tmp);
14110           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14111           add_loc_descr (&loc_descr, tmp);
14112
14113           /* Extract the offset.  */
14114           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14115           add_loc_descr (&loc_descr, tmp);
14116
14117           /* Add it to the object address.  */
14118           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14119           add_loc_descr (&loc_descr, tmp);
14120         }
14121       else
14122         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14123     }
14124   else
14125     offset = field_byte_offset (decl);
14126
14127   if (! loc_descr)
14128     {
14129       if (dwarf_version > 2)
14130         {
14131           /* Don't need to output a location expression, just the constant. */
14132           if (offset < 0)
14133             add_AT_int (die, DW_AT_data_member_location, offset);
14134           else
14135             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14136           return;
14137         }
14138       else
14139         {
14140           enum dwarf_location_atom op;
14141
14142           /* The DWARF2 standard says that we should assume that the structure
14143              address is already on the stack, so we can specify a structure
14144              field address by using DW_OP_plus_uconst.  */
14145
14146 #ifdef MIPS_DEBUGGING_INFO
14147           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14148              operator correctly.  It works only if we leave the offset on the
14149              stack.  */
14150           op = DW_OP_constu;
14151 #else
14152           op = DW_OP_plus_uconst;
14153 #endif
14154
14155           loc_descr = new_loc_descr (op, offset, 0);
14156         }
14157     }
14158
14159   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14160 }
14161
14162 /* Writes integer values to dw_vec_const array.  */
14163
14164 static void
14165 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14166 {
14167   while (size != 0)
14168     {
14169       *dest++ = val & 0xff;
14170       val >>= 8;
14171       --size;
14172     }
14173 }
14174
14175 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14176
14177 static HOST_WIDE_INT
14178 extract_int (const unsigned char *src, unsigned int size)
14179 {
14180   HOST_WIDE_INT val = 0;
14181
14182   src += size;
14183   while (size != 0)
14184     {
14185       val <<= 8;
14186       val |= *--src & 0xff;
14187       --size;
14188     }
14189   return val;
14190 }
14191
14192 /* Writes double_int values to dw_vec_const array.  */
14193
14194 static void
14195 insert_double (double_int val, unsigned char *dest)
14196 {
14197   unsigned char *p0 = dest;
14198   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14199
14200   if (WORDS_BIG_ENDIAN)
14201     {
14202       p0 = p1;
14203       p1 = dest;
14204     }
14205
14206   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14207   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14208 }
14209
14210 /* Writes floating point values to dw_vec_const array.  */
14211
14212 static void
14213 insert_float (const_rtx rtl, unsigned char *array)
14214 {
14215   REAL_VALUE_TYPE rv;
14216   long val[4];
14217   int i;
14218
14219   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14220   real_to_target (val, &rv, GET_MODE (rtl));
14221
14222   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14223   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14224     {
14225       insert_int (val[i], 4, array);
14226       array += 4;
14227     }
14228 }
14229
14230 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14231    does not have a "location" either in memory or in a register.  These
14232    things can arise in GNU C when a constant is passed as an actual parameter
14233    to an inlined function.  They can also arise in C++ where declared
14234    constants do not necessarily get memory "homes".  */
14235
14236 static bool
14237 add_const_value_attribute (dw_die_ref die, rtx rtl)
14238 {
14239   switch (GET_CODE (rtl))
14240     {
14241     case CONST_INT:
14242       {
14243         HOST_WIDE_INT val = INTVAL (rtl);
14244
14245         if (val < 0)
14246           add_AT_int (die, DW_AT_const_value, val);
14247         else
14248           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14249       }
14250       return true;
14251
14252     case CONST_DOUBLE:
14253       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14254          floating-point constant.  A CONST_DOUBLE is used whenever the
14255          constant requires more than one word in order to be adequately
14256          represented.  */
14257       {
14258         enum machine_mode mode = GET_MODE (rtl);
14259
14260         if (SCALAR_FLOAT_MODE_P (mode))
14261           {
14262             unsigned int length = GET_MODE_SIZE (mode);
14263             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14264
14265             insert_float (rtl, array);
14266             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14267           }
14268         else
14269           add_AT_double (die, DW_AT_const_value,
14270                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14271       }
14272       return true;
14273
14274     case CONST_VECTOR:
14275       {
14276         enum machine_mode mode = GET_MODE (rtl);
14277         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14278         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14279         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14280           (length * elt_size);
14281         unsigned int i;
14282         unsigned char *p;
14283
14284         switch (GET_MODE_CLASS (mode))
14285           {
14286           case MODE_VECTOR_INT:
14287             for (i = 0, p = array; i < length; i++, p += elt_size)
14288               {
14289                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14290                 double_int val = rtx_to_double_int (elt);
14291
14292                 if (elt_size <= sizeof (HOST_WIDE_INT))
14293                   insert_int (double_int_to_shwi (val), elt_size, p);
14294                 else
14295                   {
14296                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14297                     insert_double (val, p);
14298                   }
14299               }
14300             break;
14301
14302           case MODE_VECTOR_FLOAT:
14303             for (i = 0, p = array; i < length; i++, p += elt_size)
14304               {
14305                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14306                 insert_float (elt, p);
14307               }
14308             break;
14309
14310           default:
14311             gcc_unreachable ();
14312           }
14313
14314         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14315       }
14316       return true;
14317
14318     case CONST_STRING:
14319       if (dwarf_version >= 4 || !dwarf_strict)
14320         {
14321           dw_loc_descr_ref loc_result;
14322           resolve_one_addr (&rtl, NULL);
14323         rtl_addr:
14324           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14325           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14326           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14327           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14328           add_AT_loc (die, DW_AT_location, loc_result);
14329           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14330           return true;
14331         }
14332       return false;
14333
14334     case CONST:
14335       if (CONSTANT_P (XEXP (rtl, 0)))
14336         return add_const_value_attribute (die, XEXP (rtl, 0));
14337       /* FALLTHROUGH */
14338     case SYMBOL_REF:
14339       if (!const_ok_for_output (rtl))
14340         return false;
14341     case LABEL_REF:
14342       if (dwarf_version >= 4 || !dwarf_strict)
14343         goto rtl_addr;
14344       return false;
14345
14346     case PLUS:
14347       /* In cases where an inlined instance of an inline function is passed
14348          the address of an `auto' variable (which is local to the caller) we
14349          can get a situation where the DECL_RTL of the artificial local
14350          variable (for the inlining) which acts as a stand-in for the
14351          corresponding formal parameter (of the inline function) will look
14352          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14353          exactly a compile-time constant expression, but it isn't the address
14354          of the (artificial) local variable either.  Rather, it represents the
14355          *value* which the artificial local variable always has during its
14356          lifetime.  We currently have no way to represent such quasi-constant
14357          values in Dwarf, so for now we just punt and generate nothing.  */
14358       return false;
14359
14360     case HIGH:
14361     case CONST_FIXED:
14362       return false;
14363
14364     case MEM:
14365       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14366           && MEM_READONLY_P (rtl)
14367           && GET_MODE (rtl) == BLKmode)
14368         {
14369           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14370           return true;
14371         }
14372       return false;
14373
14374     default:
14375       /* No other kinds of rtx should be possible here.  */
14376       gcc_unreachable ();
14377     }
14378   return false;
14379 }
14380
14381 /* Determine whether the evaluation of EXPR references any variables
14382    or functions which aren't otherwise used (and therefore may not be
14383    output).  */
14384 static tree
14385 reference_to_unused (tree * tp, int * walk_subtrees,
14386                      void * data ATTRIBUTE_UNUSED)
14387 {
14388   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14389     *walk_subtrees = 0;
14390
14391   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14392       && ! TREE_ASM_WRITTEN (*tp))
14393     return *tp;
14394   /* ???  The C++ FE emits debug information for using decls, so
14395      putting gcc_unreachable here falls over.  See PR31899.  For now
14396      be conservative.  */
14397   else if (!cgraph_global_info_ready
14398            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14399     return *tp;
14400   else if (TREE_CODE (*tp) == VAR_DECL)
14401     {
14402       struct varpool_node *node = varpool_get_node (*tp);
14403       if (!node || !node->needed)
14404         return *tp;
14405     }
14406   else if (TREE_CODE (*tp) == FUNCTION_DECL
14407            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14408     {
14409       /* The call graph machinery must have finished analyzing,
14410          optimizing and gimplifying the CU by now.
14411          So if *TP has no call graph node associated
14412          to it, it means *TP will not be emitted.  */
14413       if (!cgraph_get_node (*tp))
14414         return *tp;
14415     }
14416   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14417     return *tp;
14418
14419   return NULL_TREE;
14420 }
14421
14422 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14423    for use in a later add_const_value_attribute call.  */
14424
14425 static rtx
14426 rtl_for_decl_init (tree init, tree type)
14427 {
14428   rtx rtl = NULL_RTX;
14429
14430   STRIP_NOPS (init);
14431
14432   /* If a variable is initialized with a string constant without embedded
14433      zeros, build CONST_STRING.  */
14434   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14435     {
14436       tree enttype = TREE_TYPE (type);
14437       tree domain = TYPE_DOMAIN (type);
14438       enum machine_mode mode = TYPE_MODE (enttype);
14439
14440       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14441           && domain
14442           && integer_zerop (TYPE_MIN_VALUE (domain))
14443           && compare_tree_int (TYPE_MAX_VALUE (domain),
14444                                TREE_STRING_LENGTH (init) - 1) == 0
14445           && ((size_t) TREE_STRING_LENGTH (init)
14446               == strlen (TREE_STRING_POINTER (init)) + 1))
14447         {
14448           rtl = gen_rtx_CONST_STRING (VOIDmode,
14449                                       ggc_strdup (TREE_STRING_POINTER (init)));
14450           rtl = gen_rtx_MEM (BLKmode, rtl);
14451           MEM_READONLY_P (rtl) = 1;
14452         }
14453     }
14454   /* Other aggregates, and complex values, could be represented using
14455      CONCAT: FIXME!  */
14456   else if (AGGREGATE_TYPE_P (type)
14457            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14458                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14459            || TREE_CODE (type) == COMPLEX_TYPE)
14460     ;
14461   /* Vectors only work if their mode is supported by the target.
14462      FIXME: generic vectors ought to work too.  */
14463   else if (TREE_CODE (type) == VECTOR_TYPE
14464            && !VECTOR_MODE_P (TYPE_MODE (type)))
14465     ;
14466   /* If the initializer is something that we know will expand into an
14467      immediate RTL constant, expand it now.  We must be careful not to
14468      reference variables which won't be output.  */
14469   else if (initializer_constant_valid_p (init, type)
14470            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14471     {
14472       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14473          possible.  */
14474       if (TREE_CODE (type) == VECTOR_TYPE)
14475         switch (TREE_CODE (init))
14476           {
14477           case VECTOR_CST:
14478             break;
14479           case CONSTRUCTOR:
14480             if (TREE_CONSTANT (init))
14481               {
14482                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14483                 bool constant_p = true;
14484                 tree value;
14485                 unsigned HOST_WIDE_INT ix;
14486
14487                 /* Even when ctor is constant, it might contain non-*_CST
14488                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14489                    belong into VECTOR_CST nodes.  */
14490                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14491                   if (!CONSTANT_CLASS_P (value))
14492                     {
14493                       constant_p = false;
14494                       break;
14495                     }
14496
14497                 if (constant_p)
14498                   {
14499                     init = build_vector_from_ctor (type, elts);
14500                     break;
14501                   }
14502               }
14503             /* FALLTHRU */
14504
14505           default:
14506             return NULL;
14507           }
14508
14509       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14510
14511       /* If expand_expr returns a MEM, it wasn't immediate.  */
14512       gcc_assert (!rtl || !MEM_P (rtl));
14513     }
14514
14515   return rtl;
14516 }
14517
14518 /* Generate RTL for the variable DECL to represent its location.  */
14519
14520 static rtx
14521 rtl_for_decl_location (tree decl)
14522 {
14523   rtx rtl;
14524
14525   /* Here we have to decide where we are going to say the parameter "lives"
14526      (as far as the debugger is concerned).  We only have a couple of
14527      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14528
14529      DECL_RTL normally indicates where the parameter lives during most of the
14530      activation of the function.  If optimization is enabled however, this
14531      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14532      that the parameter doesn't really live anywhere (as far as the code
14533      generation parts of GCC are concerned) during most of the function's
14534      activation.  That will happen (for example) if the parameter is never
14535      referenced within the function.
14536
14537      We could just generate a location descriptor here for all non-NULL
14538      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14539      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14540      where DECL_RTL is NULL or is a pseudo-reg.
14541
14542      Note however that we can only get away with using DECL_INCOMING_RTL as
14543      a backup substitute for DECL_RTL in certain limited cases.  In cases
14544      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14545      we can be sure that the parameter was passed using the same type as it is
14546      declared to have within the function, and that its DECL_INCOMING_RTL
14547      points us to a place where a value of that type is passed.
14548
14549      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14550      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14551      because in these cases DECL_INCOMING_RTL points us to a value of some
14552      type which is *different* from the type of the parameter itself.  Thus,
14553      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14554      such cases, the debugger would end up (for example) trying to fetch a
14555      `float' from a place which actually contains the first part of a
14556      `double'.  That would lead to really incorrect and confusing
14557      output at debug-time.
14558
14559      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14560      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14561      are a couple of exceptions however.  On little-endian machines we can
14562      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14563      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14564      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14565      when (on a little-endian machine) a non-prototyped function has a
14566      parameter declared to be of type `short' or `char'.  In such cases,
14567      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14568      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14569      passed `int' value.  If the debugger then uses that address to fetch
14570      a `short' or a `char' (on a little-endian machine) the result will be
14571      the correct data, so we allow for such exceptional cases below.
14572
14573      Note that our goal here is to describe the place where the given formal
14574      parameter lives during most of the function's activation (i.e. between the
14575      end of the prologue and the start of the epilogue).  We'll do that as best
14576      as we can. Note however that if the given formal parameter is modified
14577      sometime during the execution of the function, then a stack backtrace (at
14578      debug-time) will show the function as having been called with the *new*
14579      value rather than the value which was originally passed in.  This happens
14580      rarely enough that it is not a major problem, but it *is* a problem, and
14581      I'd like to fix it.
14582
14583      A future version of dwarf2out.c may generate two additional attributes for
14584      any given DW_TAG_formal_parameter DIE which will describe the "passed
14585      type" and the "passed location" for the given formal parameter in addition
14586      to the attributes we now generate to indicate the "declared type" and the
14587      "active location" for each parameter.  This additional set of attributes
14588      could be used by debuggers for stack backtraces. Separately, note that
14589      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14590      This happens (for example) for inlined-instances of inline function formal
14591      parameters which are never referenced.  This really shouldn't be
14592      happening.  All PARM_DECL nodes should get valid non-NULL
14593      DECL_INCOMING_RTL values.  FIXME.  */
14594
14595   /* Use DECL_RTL as the "location" unless we find something better.  */
14596   rtl = DECL_RTL_IF_SET (decl);
14597
14598   /* When generating abstract instances, ignore everything except
14599      constants, symbols living in memory, and symbols living in
14600      fixed registers.  */
14601   if (! reload_completed)
14602     {
14603       if (rtl
14604           && (CONSTANT_P (rtl)
14605               || (MEM_P (rtl)
14606                   && CONSTANT_P (XEXP (rtl, 0)))
14607               || (REG_P (rtl)
14608                   && TREE_CODE (decl) == VAR_DECL
14609                   && TREE_STATIC (decl))))
14610         {
14611           rtl = targetm.delegitimize_address (rtl);
14612           return rtl;
14613         }
14614       rtl = NULL_RTX;
14615     }
14616   else if (TREE_CODE (decl) == PARM_DECL)
14617     {
14618       if (rtl == NULL_RTX
14619           || is_pseudo_reg (rtl)
14620           || (MEM_P (rtl)
14621               && is_pseudo_reg (XEXP (rtl, 0))
14622               && DECL_INCOMING_RTL (decl)
14623               && MEM_P (DECL_INCOMING_RTL (decl))
14624               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14625         {
14626           tree declared_type = TREE_TYPE (decl);
14627           tree passed_type = DECL_ARG_TYPE (decl);
14628           enum machine_mode dmode = TYPE_MODE (declared_type);
14629           enum machine_mode pmode = TYPE_MODE (passed_type);
14630
14631           /* This decl represents a formal parameter which was optimized out.
14632              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14633              all cases where (rtl == NULL_RTX) just below.  */
14634           if (dmode == pmode)
14635             rtl = DECL_INCOMING_RTL (decl);
14636           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14637                    && SCALAR_INT_MODE_P (dmode)
14638                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14639                    && DECL_INCOMING_RTL (decl))
14640             {
14641               rtx inc = DECL_INCOMING_RTL (decl);
14642               if (REG_P (inc))
14643                 rtl = inc;
14644               else if (MEM_P (inc))
14645                 {
14646                   if (BYTES_BIG_ENDIAN)
14647                     rtl = adjust_address_nv (inc, dmode,
14648                                              GET_MODE_SIZE (pmode)
14649                                              - GET_MODE_SIZE (dmode));
14650                   else
14651                     rtl = inc;
14652                 }
14653             }
14654         }
14655
14656       /* If the parm was passed in registers, but lives on the stack, then
14657          make a big endian correction if the mode of the type of the
14658          parameter is not the same as the mode of the rtl.  */
14659       /* ??? This is the same series of checks that are made in dbxout.c before
14660          we reach the big endian correction code there.  It isn't clear if all
14661          of these checks are necessary here, but keeping them all is the safe
14662          thing to do.  */
14663       else if (MEM_P (rtl)
14664                && XEXP (rtl, 0) != const0_rtx
14665                && ! CONSTANT_P (XEXP (rtl, 0))
14666                /* Not passed in memory.  */
14667                && !MEM_P (DECL_INCOMING_RTL (decl))
14668                /* Not passed by invisible reference.  */
14669                && (!REG_P (XEXP (rtl, 0))
14670                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14671                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14672 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14673                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14674 #endif
14675                      )
14676                /* Big endian correction check.  */
14677                && BYTES_BIG_ENDIAN
14678                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14679                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14680                    < UNITS_PER_WORD))
14681         {
14682           int offset = (UNITS_PER_WORD
14683                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14684
14685           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14686                              plus_constant (XEXP (rtl, 0), offset));
14687         }
14688     }
14689   else if (TREE_CODE (decl) == VAR_DECL
14690            && rtl
14691            && MEM_P (rtl)
14692            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14693            && BYTES_BIG_ENDIAN)
14694     {
14695       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14696       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14697
14698       /* If a variable is declared "register" yet is smaller than
14699          a register, then if we store the variable to memory, it
14700          looks like we're storing a register-sized value, when in
14701          fact we are not.  We need to adjust the offset of the
14702          storage location to reflect the actual value's bytes,
14703          else gdb will not be able to display it.  */
14704       if (rsize > dsize)
14705         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14706                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14707     }
14708
14709   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14710      and will have been substituted directly into all expressions that use it.
14711      C does not have such a concept, but C++ and other languages do.  */
14712   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14713     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14714
14715   if (rtl)
14716     rtl = targetm.delegitimize_address (rtl);
14717
14718   /* If we don't look past the constant pool, we risk emitting a
14719      reference to a constant pool entry that isn't referenced from
14720      code, and thus is not emitted.  */
14721   if (rtl)
14722     rtl = avoid_constant_pool_reference (rtl);
14723
14724   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14725      in the current CU, resolve_addr will remove the expression referencing
14726      it.  */
14727   if (rtl == NULL_RTX
14728       && TREE_CODE (decl) == VAR_DECL
14729       && !DECL_EXTERNAL (decl)
14730       && TREE_STATIC (decl)
14731       && DECL_NAME (decl)
14732       && !DECL_HARD_REGISTER (decl)
14733       && DECL_MODE (decl) != VOIDmode)
14734     {
14735       rtl = make_decl_rtl_for_debug (decl);
14736       if (!MEM_P (rtl)
14737           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14738           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14739         rtl = NULL_RTX;
14740     }
14741
14742   return rtl;
14743 }
14744
14745 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14746    returned.  If so, the decl for the COMMON block is returned, and the
14747    value is the offset into the common block for the symbol.  */
14748
14749 static tree
14750 fortran_common (tree decl, HOST_WIDE_INT *value)
14751 {
14752   tree val_expr, cvar;
14753   enum machine_mode mode;
14754   HOST_WIDE_INT bitsize, bitpos;
14755   tree offset;
14756   int volatilep = 0, unsignedp = 0;
14757
14758   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14759      it does not have a value (the offset into the common area), or if it
14760      is thread local (as opposed to global) then it isn't common, and shouldn't
14761      be handled as such.  */
14762   if (TREE_CODE (decl) != VAR_DECL
14763       || !TREE_STATIC (decl)
14764       || !DECL_HAS_VALUE_EXPR_P (decl)
14765       || !is_fortran ())
14766     return NULL_TREE;
14767
14768   val_expr = DECL_VALUE_EXPR (decl);
14769   if (TREE_CODE (val_expr) != COMPONENT_REF)
14770     return NULL_TREE;
14771
14772   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14773                               &mode, &unsignedp, &volatilep, true);
14774
14775   if (cvar == NULL_TREE
14776       || TREE_CODE (cvar) != VAR_DECL
14777       || DECL_ARTIFICIAL (cvar)
14778       || !TREE_PUBLIC (cvar))
14779     return NULL_TREE;
14780
14781   *value = 0;
14782   if (offset != NULL)
14783     {
14784       if (!host_integerp (offset, 0))
14785         return NULL_TREE;
14786       *value = tree_low_cst (offset, 0);
14787     }
14788   if (bitpos != 0)
14789     *value += bitpos / BITS_PER_UNIT;
14790
14791   return cvar;
14792 }
14793
14794 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14795    data attribute for a variable or a parameter.  We generate the
14796    DW_AT_const_value attribute only in those cases where the given variable
14797    or parameter does not have a true "location" either in memory or in a
14798    register.  This can happen (for example) when a constant is passed as an
14799    actual argument in a call to an inline function.  (It's possible that
14800    these things can crop up in other ways also.)  Note that one type of
14801    constant value which can be passed into an inlined function is a constant
14802    pointer.  This can happen for example if an actual argument in an inlined
14803    function call evaluates to a compile-time constant address.
14804
14805    CACHE_P is true if it is worth caching the location list for DECL,
14806    so that future calls can reuse it rather than regenerate it from scratch.
14807    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14808    since we will need to refer to them each time the function is inlined.  */
14809
14810 static bool
14811 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14812                                        enum dwarf_attribute attr)
14813 {
14814   rtx rtl;
14815   dw_loc_list_ref list;
14816   var_loc_list *loc_list;
14817   cached_dw_loc_list *cache;
14818   void **slot;
14819
14820   if (TREE_CODE (decl) == ERROR_MARK)
14821     return false;
14822
14823   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14824               || TREE_CODE (decl) == RESULT_DECL);
14825
14826   /* Try to get some constant RTL for this decl, and use that as the value of
14827      the location.  */
14828
14829   rtl = rtl_for_decl_location (decl);
14830   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14831       && add_const_value_attribute (die, rtl))
14832     return true;
14833
14834   /* See if we have single element location list that is equivalent to
14835      a constant value.  That way we are better to use add_const_value_attribute
14836      rather than expanding constant value equivalent.  */
14837   loc_list = lookup_decl_loc (decl);
14838   if (loc_list
14839       && loc_list->first
14840       && loc_list->first->next == NULL
14841       && NOTE_P (loc_list->first->loc)
14842       && NOTE_VAR_LOCATION (loc_list->first->loc)
14843       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14844     {
14845       struct var_loc_node *node;
14846
14847       node = loc_list->first;
14848       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14849       if (GET_CODE (rtl) == EXPR_LIST)
14850         rtl = XEXP (rtl, 0);
14851       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14852           && add_const_value_attribute (die, rtl))
14853          return true;
14854     }
14855   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14856      list several times.  See if we've already cached the contents.  */
14857   list = NULL;
14858   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14859     cache_p = false;
14860   if (cache_p)
14861     {
14862       cache = (cached_dw_loc_list *)
14863         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14864       if (cache)
14865         list = cache->loc_list;
14866     }
14867   if (list == NULL)
14868     {
14869       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14870       /* It is usually worth caching this result if the decl is from
14871          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14872       if (cache_p && list && list->dw_loc_next)
14873         {
14874           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14875                                            DECL_UID (decl), INSERT);
14876           cache = ggc_alloc_cleared_cached_dw_loc_list ();
14877           cache->decl_id = DECL_UID (decl);
14878           cache->loc_list = list;
14879           *slot = cache;
14880         }
14881     }
14882   if (list)
14883     {
14884       add_AT_location_description (die, attr, list);
14885       return true;
14886     }
14887   /* None of that worked, so it must not really have a location;
14888      try adding a constant value attribute from the DECL_INITIAL.  */
14889   return tree_add_const_value_attribute_for_decl (die, decl);
14890 }
14891
14892 /* Add VARIABLE and DIE into deferred locations list.  */
14893
14894 static void
14895 defer_location (tree variable, dw_die_ref die)
14896 {
14897   deferred_locations entry;
14898   entry.variable = variable;
14899   entry.die = die;
14900   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14901 }
14902
14903 /* Helper function for tree_add_const_value_attribute.  Natively encode
14904    initializer INIT into an array.  Return true if successful.  */
14905
14906 static bool
14907 native_encode_initializer (tree init, unsigned char *array, int size)
14908 {
14909   tree type;
14910
14911   if (init == NULL_TREE)
14912     return false;
14913
14914   STRIP_NOPS (init);
14915   switch (TREE_CODE (init))
14916     {
14917     case STRING_CST:
14918       type = TREE_TYPE (init);
14919       if (TREE_CODE (type) == ARRAY_TYPE)
14920         {
14921           tree enttype = TREE_TYPE (type);
14922           enum machine_mode mode = TYPE_MODE (enttype);
14923
14924           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14925             return false;
14926           if (int_size_in_bytes (type) != size)
14927             return false;
14928           if (size > TREE_STRING_LENGTH (init))
14929             {
14930               memcpy (array, TREE_STRING_POINTER (init),
14931                       TREE_STRING_LENGTH (init));
14932               memset (array + TREE_STRING_LENGTH (init),
14933                       '\0', size - TREE_STRING_LENGTH (init));
14934             }
14935           else
14936             memcpy (array, TREE_STRING_POINTER (init), size);
14937           return true;
14938         }
14939       return false;
14940     case CONSTRUCTOR:
14941       type = TREE_TYPE (init);
14942       if (int_size_in_bytes (type) != size)
14943         return false;
14944       if (TREE_CODE (type) == ARRAY_TYPE)
14945         {
14946           HOST_WIDE_INT min_index;
14947           unsigned HOST_WIDE_INT cnt;
14948           int curpos = 0, fieldsize;
14949           constructor_elt *ce;
14950
14951           if (TYPE_DOMAIN (type) == NULL_TREE
14952               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14953             return false;
14954
14955           fieldsize = int_size_in_bytes (TREE_TYPE (type));
14956           if (fieldsize <= 0)
14957             return false;
14958
14959           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14960           memset (array, '\0', size);
14961           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14962             {
14963               tree val = ce->value;
14964               tree index = ce->index;
14965               int pos = curpos;
14966               if (index && TREE_CODE (index) == RANGE_EXPR)
14967                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14968                       * fieldsize;
14969               else if (index)
14970                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14971
14972               if (val)
14973                 {
14974                   STRIP_NOPS (val);
14975                   if (!native_encode_initializer (val, array + pos, fieldsize))
14976                     return false;
14977                 }
14978               curpos = pos + fieldsize;
14979               if (index && TREE_CODE (index) == RANGE_EXPR)
14980                 {
14981                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14982                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14983                   while (count-- > 0)
14984                     {
14985                       if (val)
14986                         memcpy (array + curpos, array + pos, fieldsize);
14987                       curpos += fieldsize;
14988                     }
14989                 }
14990               gcc_assert (curpos <= size);
14991             }
14992           return true;
14993         }
14994       else if (TREE_CODE (type) == RECORD_TYPE
14995                || TREE_CODE (type) == UNION_TYPE)
14996         {
14997           tree field = NULL_TREE;
14998           unsigned HOST_WIDE_INT cnt;
14999           constructor_elt *ce;
15000
15001           if (int_size_in_bytes (type) != size)
15002             return false;
15003
15004           if (TREE_CODE (type) == RECORD_TYPE)
15005             field = TYPE_FIELDS (type);
15006
15007           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15008             {
15009               tree val = ce->value;
15010               int pos, fieldsize;
15011
15012               if (ce->index != 0)
15013                 field = ce->index;
15014
15015               if (val)
15016                 STRIP_NOPS (val);
15017
15018               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15019                 return false;
15020
15021               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15022                   && TYPE_DOMAIN (TREE_TYPE (field))
15023                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15024                 return false;
15025               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15026                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15027                 return false;
15028               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15029               pos = int_byte_position (field);
15030               gcc_assert (pos + fieldsize <= size);
15031               if (val
15032                   && !native_encode_initializer (val, array + pos, fieldsize))
15033                 return false;
15034             }
15035           return true;
15036         }
15037       return false;
15038     case VIEW_CONVERT_EXPR:
15039     case NON_LVALUE_EXPR:
15040       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15041     default:
15042       return native_encode_expr (init, array, size) == size;
15043     }
15044 }
15045
15046 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15047    attribute is the const value T.  */
15048
15049 static bool
15050 tree_add_const_value_attribute (dw_die_ref die, tree t)
15051 {
15052   tree init;
15053   tree type = TREE_TYPE (t);
15054   rtx rtl;
15055
15056   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15057     return false;
15058
15059   init = t;
15060   gcc_assert (!DECL_P (init));
15061
15062   rtl = rtl_for_decl_init (init, type);
15063   if (rtl)
15064     return add_const_value_attribute (die, rtl);
15065   /* If the host and target are sane, try harder.  */
15066   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15067            && initializer_constant_valid_p (init, type))
15068     {
15069       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15070       if (size > 0 && (int) size == size)
15071         {
15072           unsigned char *array = (unsigned char *)
15073             ggc_alloc_cleared_atomic (size);
15074
15075           if (native_encode_initializer (init, array, size))
15076             {
15077               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15078               return true;
15079             }
15080         }
15081     }
15082   return false;
15083 }
15084
15085 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15086    attribute is the const value of T, where T is an integral constant
15087    variable with static storage duration
15088    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15089
15090 static bool
15091 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15092 {
15093
15094   if (!decl
15095       || (TREE_CODE (decl) != VAR_DECL
15096           && TREE_CODE (decl) != CONST_DECL)
15097       || (TREE_CODE (decl) == VAR_DECL
15098           && !TREE_STATIC (decl)))
15099     return false;
15100
15101     if (TREE_READONLY (decl)
15102         && ! TREE_THIS_VOLATILE (decl)
15103         && DECL_INITIAL (decl))
15104       /* OK */;
15105     else
15106       return false;
15107
15108   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15109   if (get_AT (var_die, DW_AT_const_value))
15110     return false;
15111
15112   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15113 }
15114
15115 /* Convert the CFI instructions for the current function into a
15116    location list.  This is used for DW_AT_frame_base when we targeting
15117    a dwarf2 consumer that does not support the dwarf3
15118    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15119    expressions.  */
15120
15121 static dw_loc_list_ref
15122 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15123 {
15124   int ix;
15125   dw_fde_ref fde;
15126   dw_loc_list_ref list, *list_tail;
15127   dw_cfi_ref cfi;
15128   dw_cfa_location last_cfa, next_cfa;
15129   const char *start_label, *last_label, *section;
15130   dw_cfa_location remember;
15131
15132   fde = cfun->fde;
15133   gcc_assert (fde != NULL);
15134
15135   section = secname_for_decl (current_function_decl);
15136   list_tail = &list;
15137   list = NULL;
15138
15139   memset (&next_cfa, 0, sizeof (next_cfa));
15140   next_cfa.reg = INVALID_REGNUM;
15141   remember = next_cfa;
15142
15143   start_label = fde->dw_fde_begin;
15144
15145   /* ??? Bald assumption that the CIE opcode list does not contain
15146      advance opcodes.  */
15147   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15148     lookup_cfa_1 (cfi, &next_cfa, &remember);
15149
15150   last_cfa = next_cfa;
15151   last_label = start_label;
15152
15153   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15154     {
15155       /* If the first partition contained no CFI adjustments, the
15156          CIE opcodes apply to the whole first partition.  */
15157       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15158                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15159       list_tail =&(*list_tail)->dw_loc_next;
15160       start_label = last_label = fde->dw_fde_second_begin;
15161     }
15162
15163   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15164     {
15165       switch (cfi->dw_cfi_opc)
15166         {
15167         case DW_CFA_set_loc:
15168         case DW_CFA_advance_loc1:
15169         case DW_CFA_advance_loc2:
15170         case DW_CFA_advance_loc4:
15171           if (!cfa_equal_p (&last_cfa, &next_cfa))
15172             {
15173               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15174                                          start_label, last_label, section);
15175
15176               list_tail = &(*list_tail)->dw_loc_next;
15177               last_cfa = next_cfa;
15178               start_label = last_label;
15179             }
15180           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15181           break;
15182
15183         case DW_CFA_advance_loc:
15184           /* The encoding is complex enough that we should never emit this.  */
15185           gcc_unreachable ();
15186
15187         default:
15188           lookup_cfa_1 (cfi, &next_cfa, &remember);
15189           break;
15190         }
15191       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15192         {
15193           if (!cfa_equal_p (&last_cfa, &next_cfa))
15194             {
15195               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15196                                          start_label, last_label, section);
15197
15198               list_tail = &(*list_tail)->dw_loc_next;
15199               last_cfa = next_cfa;
15200               start_label = last_label;
15201             }
15202           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15203                                      start_label, fde->dw_fde_end, section);
15204           list_tail = &(*list_tail)->dw_loc_next;
15205           start_label = last_label = fde->dw_fde_second_begin;
15206         }
15207     }
15208
15209   if (!cfa_equal_p (&last_cfa, &next_cfa))
15210     {
15211       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15212                                  start_label, last_label, section);
15213       list_tail = &(*list_tail)->dw_loc_next;
15214       start_label = last_label;
15215     }
15216
15217   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15218                              start_label,
15219                              fde->dw_fde_second_begin
15220                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15221                              section);
15222
15223   if (list && list->dw_loc_next)
15224     gen_llsym (list);
15225
15226   return list;
15227 }
15228
15229 /* Compute a displacement from the "steady-state frame pointer" to the
15230    frame base (often the same as the CFA), and store it in
15231    frame_pointer_fb_offset.  OFFSET is added to the displacement
15232    before the latter is negated.  */
15233
15234 static void
15235 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15236 {
15237   rtx reg, elim;
15238
15239 #ifdef FRAME_POINTER_CFA_OFFSET
15240   reg = frame_pointer_rtx;
15241   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15242 #else
15243   reg = arg_pointer_rtx;
15244   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15245 #endif
15246
15247   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15248   if (GET_CODE (elim) == PLUS)
15249     {
15250       offset += INTVAL (XEXP (elim, 1));
15251       elim = XEXP (elim, 0);
15252     }
15253
15254   frame_pointer_fb_offset = -offset;
15255
15256   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15257      in which to eliminate.  This is because it's stack pointer isn't 
15258      directly accessible as a register within the ISA.  To work around
15259      this, assume that while we cannot provide a proper value for
15260      frame_pointer_fb_offset, we won't need one either.  */
15261   frame_pointer_fb_offset_valid
15262     = ((SUPPORTS_STACK_ALIGNMENT
15263         && (elim == hard_frame_pointer_rtx
15264             || elim == stack_pointer_rtx))
15265        || elim == (frame_pointer_needed
15266                    ? hard_frame_pointer_rtx
15267                    : stack_pointer_rtx));
15268 }
15269
15270 /* Generate a DW_AT_name attribute given some string value to be included as
15271    the value of the attribute.  */
15272
15273 static void
15274 add_name_attribute (dw_die_ref die, const char *name_string)
15275 {
15276   if (name_string != NULL && *name_string != 0)
15277     {
15278       if (demangle_name_func)
15279         name_string = (*demangle_name_func) (name_string);
15280
15281       add_AT_string (die, DW_AT_name, name_string);
15282     }
15283 }
15284
15285 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15286    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15287    of TYPE accordingly.
15288
15289    ??? This is a temporary measure until after we're able to generate
15290    regular DWARF for the complex Ada type system.  */
15291
15292 static void 
15293 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15294                                      dw_die_ref context_die)
15295 {
15296   tree dtype;
15297   dw_die_ref dtype_die;
15298
15299   if (!lang_hooks.types.descriptive_type)
15300     return;
15301
15302   dtype = lang_hooks.types.descriptive_type (type);
15303   if (!dtype)
15304     return;
15305
15306   dtype_die = lookup_type_die (dtype);
15307   if (!dtype_die)
15308     {
15309       gen_type_die (dtype, context_die);
15310       dtype_die = lookup_type_die (dtype);
15311       gcc_assert (dtype_die);
15312     }
15313
15314   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15315 }
15316
15317 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15318
15319 static void
15320 add_comp_dir_attribute (dw_die_ref die)
15321 {
15322   const char *wd = get_src_pwd ();
15323   char *wd1;
15324
15325   if (wd == NULL)
15326     return;
15327
15328   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15329     {
15330       int wdlen;
15331
15332       wdlen = strlen (wd);
15333       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15334       strcpy (wd1, wd);
15335       wd1 [wdlen] = DIR_SEPARATOR;
15336       wd1 [wdlen + 1] = 0;
15337       wd = wd1;
15338     }
15339
15340     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15341 }
15342
15343 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15344    default.  */
15345
15346 static int
15347 lower_bound_default (void)
15348 {
15349   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15350     {
15351     case DW_LANG_C:
15352     case DW_LANG_C89:
15353     case DW_LANG_C99:
15354     case DW_LANG_C_plus_plus:
15355     case DW_LANG_ObjC:
15356     case DW_LANG_ObjC_plus_plus:
15357     case DW_LANG_Java:
15358       return 0;
15359     case DW_LANG_Fortran77:
15360     case DW_LANG_Fortran90:
15361     case DW_LANG_Fortran95:
15362       return 1;
15363     case DW_LANG_UPC:
15364     case DW_LANG_D:
15365     case DW_LANG_Python:
15366       return dwarf_version >= 4 ? 0 : -1;
15367     case DW_LANG_Ada95:
15368     case DW_LANG_Ada83:
15369     case DW_LANG_Cobol74:
15370     case DW_LANG_Cobol85:
15371     case DW_LANG_Pascal83:
15372     case DW_LANG_Modula2:
15373     case DW_LANG_PLI:
15374       return dwarf_version >= 4 ? 1 : -1;
15375     default:
15376       return -1;
15377     }
15378 }
15379
15380 /* Given a tree node describing an array bound (either lower or upper) output
15381    a representation for that bound.  */
15382
15383 static void
15384 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15385 {
15386   switch (TREE_CODE (bound))
15387     {
15388     case ERROR_MARK:
15389       return;
15390
15391     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15392     case INTEGER_CST:
15393       {
15394         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15395         int dflt;
15396
15397         /* Use the default if possible.  */
15398         if (bound_attr == DW_AT_lower_bound
15399             && host_integerp (bound, 0)
15400             && (dflt = lower_bound_default ()) != -1
15401             && tree_low_cst (bound, 0) == dflt)
15402           ;
15403
15404         /* Otherwise represent the bound as an unsigned value with the
15405            precision of its type.  The precision and signedness of the
15406            type will be necessary to re-interpret it unambiguously.  */
15407         else if (prec < HOST_BITS_PER_WIDE_INT)
15408           {
15409             unsigned HOST_WIDE_INT mask
15410               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15411             add_AT_unsigned (subrange_die, bound_attr,
15412                              TREE_INT_CST_LOW (bound) & mask);
15413           }
15414         else if (prec == HOST_BITS_PER_WIDE_INT
15415                  || TREE_INT_CST_HIGH (bound) == 0)
15416           add_AT_unsigned (subrange_die, bound_attr,
15417                            TREE_INT_CST_LOW (bound));
15418         else
15419           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15420                          TREE_INT_CST_LOW (bound));
15421       }
15422       break;
15423
15424     CASE_CONVERT:
15425     case VIEW_CONVERT_EXPR:
15426       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15427       break;
15428
15429     case SAVE_EXPR:
15430       break;
15431
15432     case VAR_DECL:
15433     case PARM_DECL:
15434     case RESULT_DECL:
15435       {
15436         dw_die_ref decl_die = lookup_decl_die (bound);
15437
15438         /* ??? Can this happen, or should the variable have been bound
15439            first?  Probably it can, since I imagine that we try to create
15440            the types of parameters in the order in which they exist in
15441            the list, and won't have created a forward reference to a
15442            later parameter.  */
15443         if (decl_die != NULL)
15444           {
15445             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15446             break;
15447           }
15448       }
15449       /* FALLTHRU */
15450
15451     default:
15452       {
15453         /* Otherwise try to create a stack operation procedure to
15454            evaluate the value of the array bound.  */
15455
15456         dw_die_ref ctx, decl_die;
15457         dw_loc_list_ref list;
15458
15459         list = loc_list_from_tree (bound, 2);
15460         if (list == NULL || single_element_loc_list_p (list))
15461           {
15462             /* If DW_AT_*bound is not a reference nor constant, it is
15463                a DWARF expression rather than location description.
15464                For that loc_list_from_tree (bound, 0) is needed.
15465                If that fails to give a single element list,
15466                fall back to outputting this as a reference anyway.  */
15467             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15468             if (list2 && single_element_loc_list_p (list2))
15469               {
15470                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15471                 break;
15472               }
15473           }
15474         if (list == NULL)
15475           break;
15476
15477         if (current_function_decl == 0)
15478           ctx = comp_unit_die ();
15479         else
15480           ctx = lookup_decl_die (current_function_decl);
15481
15482         decl_die = new_die (DW_TAG_variable, ctx, bound);
15483         add_AT_flag (decl_die, DW_AT_artificial, 1);
15484         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15485         add_AT_location_description (decl_die, DW_AT_location, list);
15486         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15487         break;
15488       }
15489     }
15490 }
15491
15492 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15493    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15494    Note that the block of subscript information for an array type also
15495    includes information about the element type of the given array type.  */
15496
15497 static void
15498 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15499 {
15500   unsigned dimension_number;
15501   tree lower, upper;
15502   dw_die_ref subrange_die;
15503
15504   for (dimension_number = 0;
15505        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15506        type = TREE_TYPE (type), dimension_number++)
15507     {
15508       tree domain = TYPE_DOMAIN (type);
15509
15510       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15511         break;
15512
15513       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15514          and (in GNU C only) variable bounds.  Handle all three forms
15515          here.  */
15516       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15517       if (domain)
15518         {
15519           /* We have an array type with specified bounds.  */
15520           lower = TYPE_MIN_VALUE (domain);
15521           upper = TYPE_MAX_VALUE (domain);
15522
15523           /* Define the index type.  */
15524           if (TREE_TYPE (domain))
15525             {
15526               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15527                  TREE_TYPE field.  We can't emit debug info for this
15528                  because it is an unnamed integral type.  */
15529               if (TREE_CODE (domain) == INTEGER_TYPE
15530                   && TYPE_NAME (domain) == NULL_TREE
15531                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15532                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15533                 ;
15534               else
15535                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15536                                     type_die);
15537             }
15538
15539           /* ??? If upper is NULL, the array has unspecified length,
15540              but it does have a lower bound.  This happens with Fortran
15541                dimension arr(N:*)
15542              Since the debugger is definitely going to need to know N
15543              to produce useful results, go ahead and output the lower
15544              bound solo, and hope the debugger can cope.  */
15545
15546           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15547           if (upper)
15548             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15549         }
15550
15551       /* Otherwise we have an array type with an unspecified length.  The
15552          DWARF-2 spec does not say how to handle this; let's just leave out the
15553          bounds.  */
15554     }
15555 }
15556
15557 static void
15558 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15559 {
15560   unsigned size;
15561
15562   switch (TREE_CODE (tree_node))
15563     {
15564     case ERROR_MARK:
15565       size = 0;
15566       break;
15567     case ENUMERAL_TYPE:
15568     case RECORD_TYPE:
15569     case UNION_TYPE:
15570     case QUAL_UNION_TYPE:
15571       size = int_size_in_bytes (tree_node);
15572       break;
15573     case FIELD_DECL:
15574       /* For a data member of a struct or union, the DW_AT_byte_size is
15575          generally given as the number of bytes normally allocated for an
15576          object of the *declared* type of the member itself.  This is true
15577          even for bit-fields.  */
15578       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15579       break;
15580     default:
15581       gcc_unreachable ();
15582     }
15583
15584   /* Note that `size' might be -1 when we get to this point.  If it is, that
15585      indicates that the byte size of the entity in question is variable.  We
15586      have no good way of expressing this fact in Dwarf at the present time,
15587      so just let the -1 pass on through.  */
15588   add_AT_unsigned (die, DW_AT_byte_size, size);
15589 }
15590
15591 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15592    which specifies the distance in bits from the highest order bit of the
15593    "containing object" for the bit-field to the highest order bit of the
15594    bit-field itself.
15595
15596    For any given bit-field, the "containing object" is a hypothetical object
15597    (of some integral or enum type) within which the given bit-field lives.  The
15598    type of this hypothetical "containing object" is always the same as the
15599    declared type of the individual bit-field itself.  The determination of the
15600    exact location of the "containing object" for a bit-field is rather
15601    complicated.  It's handled by the `field_byte_offset' function (above).
15602
15603    Note that it is the size (in bytes) of the hypothetical "containing object"
15604    which will be given in the DW_AT_byte_size attribute for this bit-field.
15605    (See `byte_size_attribute' above).  */
15606
15607 static inline void
15608 add_bit_offset_attribute (dw_die_ref die, tree decl)
15609 {
15610   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15611   tree type = DECL_BIT_FIELD_TYPE (decl);
15612   HOST_WIDE_INT bitpos_int;
15613   HOST_WIDE_INT highest_order_object_bit_offset;
15614   HOST_WIDE_INT highest_order_field_bit_offset;
15615   HOST_WIDE_INT bit_offset;
15616
15617   /* Must be a field and a bit field.  */
15618   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15619
15620   /* We can't yet handle bit-fields whose offsets are variable, so if we
15621      encounter such things, just return without generating any attribute
15622      whatsoever.  Likewise for variable or too large size.  */
15623   if (! host_integerp (bit_position (decl), 0)
15624       || ! host_integerp (DECL_SIZE (decl), 1))
15625     return;
15626
15627   bitpos_int = int_bit_position (decl);
15628
15629   /* Note that the bit offset is always the distance (in bits) from the
15630      highest-order bit of the "containing object" to the highest-order bit of
15631      the bit-field itself.  Since the "high-order end" of any object or field
15632      is different on big-endian and little-endian machines, the computation
15633      below must take account of these differences.  */
15634   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15635   highest_order_field_bit_offset = bitpos_int;
15636
15637   if (! BYTES_BIG_ENDIAN)
15638     {
15639       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15640       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15641     }
15642
15643   bit_offset
15644     = (! BYTES_BIG_ENDIAN
15645        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15646        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15647
15648   if (bit_offset < 0)
15649     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15650   else
15651     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15652 }
15653
15654 /* For a FIELD_DECL node which represents a bit field, output an attribute
15655    which specifies the length in bits of the given field.  */
15656
15657 static inline void
15658 add_bit_size_attribute (dw_die_ref die, tree decl)
15659 {
15660   /* Must be a field and a bit field.  */
15661   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15662               && DECL_BIT_FIELD_TYPE (decl));
15663
15664   if (host_integerp (DECL_SIZE (decl), 1))
15665     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15666 }
15667
15668 /* If the compiled language is ANSI C, then add a 'prototyped'
15669    attribute, if arg types are given for the parameters of a function.  */
15670
15671 static inline void
15672 add_prototyped_attribute (dw_die_ref die, tree func_type)
15673 {
15674   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15675       && prototype_p (func_type))
15676     add_AT_flag (die, DW_AT_prototyped, 1);
15677 }
15678
15679 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15680    by looking in either the type declaration or object declaration
15681    equate table.  */
15682
15683 static inline dw_die_ref
15684 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15685 {
15686   dw_die_ref origin_die = NULL;
15687
15688   if (TREE_CODE (origin) != FUNCTION_DECL)
15689     {
15690       /* We may have gotten separated from the block for the inlined
15691          function, if we're in an exception handler or some such; make
15692          sure that the abstract function has been written out.
15693
15694          Doing this for nested functions is wrong, however; functions are
15695          distinct units, and our context might not even be inline.  */
15696       tree fn = origin;
15697
15698       if (TYPE_P (fn))
15699         fn = TYPE_STUB_DECL (fn);
15700
15701       fn = decl_function_context (fn);
15702       if (fn)
15703         dwarf2out_abstract_function (fn);
15704     }
15705
15706   if (DECL_P (origin))
15707     origin_die = lookup_decl_die (origin);
15708   else if (TYPE_P (origin))
15709     origin_die = lookup_type_die (origin);
15710
15711   /* XXX: Functions that are never lowered don't always have correct block
15712      trees (in the case of java, they simply have no block tree, in some other
15713      languages).  For these functions, there is nothing we can really do to
15714      output correct debug info for inlined functions in all cases.  Rather
15715      than die, we'll just produce deficient debug info now, in that we will
15716      have variables without a proper abstract origin.  In the future, when all
15717      functions are lowered, we should re-add a gcc_assert (origin_die)
15718      here.  */
15719
15720   if (origin_die)
15721     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15722   return origin_die;
15723 }
15724
15725 /* We do not currently support the pure_virtual attribute.  */
15726
15727 static inline void
15728 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15729 {
15730   if (DECL_VINDEX (func_decl))
15731     {
15732       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15733
15734       if (host_integerp (DECL_VINDEX (func_decl), 0))
15735         add_AT_loc (die, DW_AT_vtable_elem_location,
15736                     new_loc_descr (DW_OP_constu,
15737                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15738                                    0));
15739
15740       /* GNU extension: Record what type this method came from originally.  */
15741       if (debug_info_level > DINFO_LEVEL_TERSE
15742           && DECL_CONTEXT (func_decl))
15743         add_AT_die_ref (die, DW_AT_containing_type,
15744                         lookup_type_die (DECL_CONTEXT (func_decl)));
15745     }
15746 }
15747 \f
15748 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15749    given decl.  This used to be a vendor extension until after DWARF 4
15750    standardized it.  */
15751
15752 static void
15753 add_linkage_attr (dw_die_ref die, tree decl)
15754 {
15755   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15756
15757   /* Mimic what assemble_name_raw does with a leading '*'.  */
15758   if (name[0] == '*')
15759     name = &name[1];
15760
15761   if (dwarf_version >= 4)
15762     add_AT_string (die, DW_AT_linkage_name, name);
15763   else
15764     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15765 }
15766
15767 /* Add source coordinate attributes for the given decl.  */
15768
15769 static void
15770 add_src_coords_attributes (dw_die_ref die, tree decl)
15771 {
15772   expanded_location s;
15773
15774   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15775     return;
15776   s = expand_location (DECL_SOURCE_LOCATION (decl));
15777   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15778   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15779 }
15780
15781 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15782
15783 static void
15784 add_linkage_name (dw_die_ref die, tree decl)
15785 {
15786   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15787        && TREE_PUBLIC (decl)
15788        && !DECL_ABSTRACT (decl)
15789        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15790        && die->die_tag != DW_TAG_member)
15791     {
15792       /* Defer until we have an assembler name set.  */
15793       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15794         {
15795           limbo_die_node *asm_name;
15796
15797           asm_name = ggc_alloc_cleared_limbo_die_node ();
15798           asm_name->die = die;
15799           asm_name->created_for = decl;
15800           asm_name->next = deferred_asm_name;
15801           deferred_asm_name = asm_name;
15802         }
15803       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15804         add_linkage_attr (die, decl);
15805     }
15806 }
15807
15808 /* Add a DW_AT_name attribute and source coordinate attribute for the
15809    given decl, but only if it actually has a name.  */
15810
15811 static void
15812 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15813 {
15814   tree decl_name;
15815
15816   decl_name = DECL_NAME (decl);
15817   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15818     {
15819       const char *name = dwarf2_name (decl, 0);
15820       if (name)
15821         add_name_attribute (die, name);
15822       if (! DECL_ARTIFICIAL (decl))
15823         add_src_coords_attributes (die, decl);
15824
15825       add_linkage_name (die, decl);
15826     }
15827
15828 #ifdef VMS_DEBUGGING_INFO
15829   /* Get the function's name, as described by its RTL.  This may be different
15830      from the DECL_NAME name used in the source file.  */
15831   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15832     {
15833       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15834                    XEXP (DECL_RTL (decl), 0));
15835       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15836     }
15837 #endif /* VMS_DEBUGGING_INFO */
15838 }
15839
15840 #ifdef VMS_DEBUGGING_INFO
15841 /* Output the debug main pointer die for VMS */
15842
15843 void
15844 dwarf2out_vms_debug_main_pointer (void)
15845 {
15846   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15847   dw_die_ref die;
15848
15849   /* Allocate the VMS debug main subprogram die.  */
15850   die = ggc_alloc_cleared_die_node ();
15851   die->die_tag = DW_TAG_subprogram;
15852   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15853   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15854                                current_function_funcdef_no);
15855   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15856
15857   /* Make it the first child of comp_unit_die ().  */
15858   die->die_parent = comp_unit_die ();
15859   if (comp_unit_die ()->die_child)
15860     {
15861       die->die_sib = comp_unit_die ()->die_child->die_sib;
15862       comp_unit_die ()->die_child->die_sib = die;
15863     }
15864   else
15865     {
15866       die->die_sib = die;
15867       comp_unit_die ()->die_child = die;
15868     }
15869 }
15870 #endif /* VMS_DEBUGGING_INFO */
15871
15872 /* Push a new declaration scope.  */
15873
15874 static void
15875 push_decl_scope (tree scope)
15876 {
15877   VEC_safe_push (tree, gc, decl_scope_table, scope);
15878 }
15879
15880 /* Pop a declaration scope.  */
15881
15882 static inline void
15883 pop_decl_scope (void)
15884 {
15885   VEC_pop (tree, decl_scope_table);
15886 }
15887
15888 /* Return the DIE for the scope that immediately contains this type.
15889    Non-named types get global scope.  Named types nested in other
15890    types get their containing scope if it's open, or global scope
15891    otherwise.  All other types (i.e. function-local named types) get
15892    the current active scope.  */
15893
15894 static dw_die_ref
15895 scope_die_for (tree t, dw_die_ref context_die)
15896 {
15897   dw_die_ref scope_die = NULL;
15898   tree containing_scope;
15899   int i;
15900
15901   /* Non-types always go in the current scope.  */
15902   gcc_assert (TYPE_P (t));
15903
15904   containing_scope = TYPE_CONTEXT (t);
15905
15906   /* Use the containing namespace if it was passed in (for a declaration).  */
15907   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15908     {
15909       if (context_die == lookup_decl_die (containing_scope))
15910         /* OK */;
15911       else
15912         containing_scope = NULL_TREE;
15913     }
15914
15915   /* Ignore function type "scopes" from the C frontend.  They mean that
15916      a tagged type is local to a parmlist of a function declarator, but
15917      that isn't useful to DWARF.  */
15918   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15919     containing_scope = NULL_TREE;
15920
15921   if (SCOPE_FILE_SCOPE_P (containing_scope))
15922     scope_die = comp_unit_die ();
15923   else if (TYPE_P (containing_scope))
15924     {
15925       /* For types, we can just look up the appropriate DIE.  But
15926          first we check to see if we're in the middle of emitting it
15927          so we know where the new DIE should go.  */
15928       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
15929         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
15930           break;
15931
15932       if (i < 0)
15933         {
15934           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
15935                       || TREE_ASM_WRITTEN (containing_scope));
15936           /*We are not in the middle of emitting the type
15937             CONTAINING_SCOPE. Let's see if it's emitted already.  */
15938           scope_die = lookup_type_die (containing_scope);
15939
15940           /* If none of the current dies are suitable, we get file scope.  */
15941           if (scope_die == NULL)
15942             scope_die = comp_unit_die ();
15943         }
15944       else
15945         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15946     }
15947   else
15948     scope_die = context_die;
15949
15950   return scope_die;
15951 }
15952
15953 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
15954
15955 static inline int
15956 local_scope_p (dw_die_ref context_die)
15957 {
15958   for (; context_die; context_die = context_die->die_parent)
15959     if (context_die->die_tag == DW_TAG_inlined_subroutine
15960         || context_die->die_tag == DW_TAG_subprogram)
15961       return 1;
15962
15963   return 0;
15964 }
15965
15966 /* Returns nonzero if CONTEXT_DIE is a class.  */
15967
15968 static inline int
15969 class_scope_p (dw_die_ref context_die)
15970 {
15971   return (context_die
15972           && (context_die->die_tag == DW_TAG_structure_type
15973               || context_die->die_tag == DW_TAG_class_type
15974               || context_die->die_tag == DW_TAG_interface_type
15975               || context_die->die_tag == DW_TAG_union_type));
15976 }
15977
15978 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15979    whether or not to treat a DIE in this context as a declaration.  */
15980
15981 static inline int
15982 class_or_namespace_scope_p (dw_die_ref context_die)
15983 {
15984   return (class_scope_p (context_die)
15985           || (context_die && context_die->die_tag == DW_TAG_namespace));
15986 }
15987
15988 /* Many forms of DIEs require a "type description" attribute.  This
15989    routine locates the proper "type descriptor" die for the type given
15990    by 'type', and adds a DW_AT_type attribute below the given die.  */
15991
15992 static void
15993 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15994                     int decl_volatile, dw_die_ref context_die)
15995 {
15996   enum tree_code code  = TREE_CODE (type);
15997   dw_die_ref type_die  = NULL;
15998
15999   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16000      or fixed-point type, use the inner type.  This is because we have no
16001      support for unnamed types in base_type_die.  This can happen if this is
16002      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16003   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16004       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16005     type = TREE_TYPE (type), code = TREE_CODE (type);
16006
16007   if (code == ERROR_MARK
16008       /* Handle a special case.  For functions whose return type is void, we
16009          generate *no* type attribute.  (Note that no object may have type
16010          `void', so this only applies to function return types).  */
16011       || code == VOID_TYPE)
16012     return;
16013
16014   type_die = modified_type_die (type,
16015                                 decl_const || TYPE_READONLY (type),
16016                                 decl_volatile || TYPE_VOLATILE (type),
16017                                 context_die);
16018
16019   if (type_die != NULL)
16020     add_AT_die_ref (object_die, DW_AT_type, type_die);
16021 }
16022
16023 /* Given an object die, add the calling convention attribute for the
16024    function call type.  */
16025 static void
16026 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16027 {
16028   enum dwarf_calling_convention value = DW_CC_normal;
16029
16030   value = ((enum dwarf_calling_convention)
16031            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16032
16033   if (is_fortran ()
16034       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16035     {
16036       /* DWARF 2 doesn't provide a way to identify a program's source-level
16037         entry point.  DW_AT_calling_convention attributes are only meant
16038         to describe functions' calling conventions.  However, lacking a
16039         better way to signal the Fortran main program, we used this for 
16040         a long time, following existing custom.  Now, DWARF 4 has 
16041         DW_AT_main_subprogram, which we add below, but some tools still
16042         rely on the old way, which we thus keep.  */
16043       value = DW_CC_program;
16044
16045       if (dwarf_version >= 4 || !dwarf_strict)
16046         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16047     }
16048
16049   /* Only add the attribute if the backend requests it, and
16050      is not DW_CC_normal.  */
16051   if (value && (value != DW_CC_normal))
16052     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16053 }
16054
16055 /* Given a tree pointer to a struct, class, union, or enum type node, return
16056    a pointer to the (string) tag name for the given type, or zero if the type
16057    was declared without a tag.  */
16058
16059 static const char *
16060 type_tag (const_tree type)
16061 {
16062   const char *name = 0;
16063
16064   if (TYPE_NAME (type) != 0)
16065     {
16066       tree t = 0;
16067
16068       /* Find the IDENTIFIER_NODE for the type name.  */
16069       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16070           && !TYPE_NAMELESS (type))
16071         t = TYPE_NAME (type);
16072
16073       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16074          a TYPE_DECL node, regardless of whether or not a `typedef' was
16075          involved.  */
16076       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16077                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16078         {
16079           /* We want to be extra verbose.  Don't call dwarf_name if
16080              DECL_NAME isn't set.  The default hook for decl_printable_name
16081              doesn't like that, and in this context it's correct to return
16082              0, instead of "<anonymous>" or the like.  */
16083           if (DECL_NAME (TYPE_NAME (type))
16084               && !DECL_NAMELESS (TYPE_NAME (type)))
16085             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16086         }
16087
16088       /* Now get the name as a string, or invent one.  */
16089       if (!name && t != 0)
16090         name = IDENTIFIER_POINTER (t);
16091     }
16092
16093   return (name == 0 || *name == '\0') ? 0 : name;
16094 }
16095
16096 /* Return the type associated with a data member, make a special check
16097    for bit field types.  */
16098
16099 static inline tree
16100 member_declared_type (const_tree member)
16101 {
16102   return (DECL_BIT_FIELD_TYPE (member)
16103           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16104 }
16105
16106 /* Get the decl's label, as described by its RTL. This may be different
16107    from the DECL_NAME name used in the source file.  */
16108
16109 #if 0
16110 static const char *
16111 decl_start_label (tree decl)
16112 {
16113   rtx x;
16114   const char *fnname;
16115
16116   x = DECL_RTL (decl);
16117   gcc_assert (MEM_P (x));
16118
16119   x = XEXP (x, 0);
16120   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16121
16122   fnname = XSTR (x, 0);
16123   return fnname;
16124 }
16125 #endif
16126 \f
16127 /* These routines generate the internal representation of the DIE's for
16128    the compilation unit.  Debugging information is collected by walking
16129    the declaration trees passed in from dwarf2out_decl().  */
16130
16131 static void
16132 gen_array_type_die (tree type, dw_die_ref context_die)
16133 {
16134   dw_die_ref scope_die = scope_die_for (type, context_die);
16135   dw_die_ref array_die;
16136
16137   /* GNU compilers represent multidimensional array types as sequences of one
16138      dimensional array types whose element types are themselves array types.
16139      We sometimes squish that down to a single array_type DIE with multiple
16140      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16141      say that we are allowed to do this kind of compression in C, because
16142      there is no difference between an array of arrays and a multidimensional
16143      array.  We don't do this for Ada to remain as close as possible to the
16144      actual representation, which is especially important against the language
16145      flexibilty wrt arrays of variable size.  */
16146
16147   bool collapse_nested_arrays = !is_ada ();
16148   tree element_type;
16149
16150   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16151      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16152   if (TYPE_STRING_FLAG (type)
16153       && TREE_CODE (type) == ARRAY_TYPE
16154       && is_fortran ()
16155       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16156     {
16157       HOST_WIDE_INT size;
16158
16159       array_die = new_die (DW_TAG_string_type, scope_die, type);
16160       add_name_attribute (array_die, type_tag (type));
16161       equate_type_number_to_die (type, array_die);
16162       size = int_size_in_bytes (type);
16163       if (size >= 0)
16164         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16165       else if (TYPE_DOMAIN (type) != NULL_TREE
16166                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16167                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16168         {
16169           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16170           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16171
16172           size = int_size_in_bytes (TREE_TYPE (szdecl));
16173           if (loc && size > 0)
16174             {
16175               add_AT_location_description (array_die, DW_AT_string_length, loc);
16176               if (size != DWARF2_ADDR_SIZE)
16177                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16178             }
16179         }
16180       return;
16181     }
16182
16183   /* ??? The SGI dwarf reader fails for array of array of enum types
16184      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16185      array type comes before the outer array type.  We thus call gen_type_die
16186      before we new_die and must prevent nested array types collapsing for this
16187      target.  */
16188
16189 #ifdef MIPS_DEBUGGING_INFO
16190   gen_type_die (TREE_TYPE (type), context_die);
16191   collapse_nested_arrays = false;
16192 #endif
16193
16194   array_die = new_die (DW_TAG_array_type, scope_die, type);
16195   add_name_attribute (array_die, type_tag (type));
16196   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16197   if (TYPE_ARTIFICIAL (type))
16198     add_AT_flag (array_die, DW_AT_artificial, 1);
16199   equate_type_number_to_die (type, array_die);
16200
16201   if (TREE_CODE (type) == VECTOR_TYPE)
16202     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16203
16204   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16205   if (is_fortran ()
16206       && TREE_CODE (type) == ARRAY_TYPE
16207       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16208       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16209     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16210
16211 #if 0
16212   /* We default the array ordering.  SDB will probably do
16213      the right things even if DW_AT_ordering is not present.  It's not even
16214      an issue until we start to get into multidimensional arrays anyway.  If
16215      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16216      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16217      and when we find out that we need to put these in, we will only do so
16218      for multidimensional arrays.  */
16219   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16220 #endif
16221
16222 #ifdef MIPS_DEBUGGING_INFO
16223   /* The SGI compilers handle arrays of unknown bound by setting
16224      AT_declaration and not emitting any subrange DIEs.  */
16225   if (TREE_CODE (type) == ARRAY_TYPE
16226       && ! TYPE_DOMAIN (type))
16227     add_AT_flag (array_die, DW_AT_declaration, 1);
16228   else
16229 #endif
16230   if (TREE_CODE (type) == VECTOR_TYPE)
16231     {
16232       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16233       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16234       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16235       add_bound_info (subrange_die, DW_AT_upper_bound,
16236                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16237     }
16238   else
16239     add_subscript_info (array_die, type, collapse_nested_arrays);
16240
16241   /* Add representation of the type of the elements of this array type and
16242      emit the corresponding DIE if we haven't done it already.  */
16243   element_type = TREE_TYPE (type);
16244   if (collapse_nested_arrays)
16245     while (TREE_CODE (element_type) == ARRAY_TYPE)
16246       {
16247         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16248           break;
16249         element_type = TREE_TYPE (element_type);
16250       }
16251
16252 #ifndef MIPS_DEBUGGING_INFO
16253   gen_type_die (element_type, context_die);
16254 #endif
16255
16256   add_type_attribute (array_die, element_type, 0, 0, context_die);
16257
16258   if (get_AT (array_die, DW_AT_name))
16259     add_pubtype (type, array_die);
16260 }
16261
16262 static dw_loc_descr_ref
16263 descr_info_loc (tree val, tree base_decl)
16264 {
16265   HOST_WIDE_INT size;
16266   dw_loc_descr_ref loc, loc2;
16267   enum dwarf_location_atom op;
16268
16269   if (val == base_decl)
16270     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16271
16272   switch (TREE_CODE (val))
16273     {
16274     CASE_CONVERT:
16275       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16276     case VAR_DECL:
16277       return loc_descriptor_from_tree (val, 0);
16278     case INTEGER_CST:
16279       if (host_integerp (val, 0))
16280         return int_loc_descriptor (tree_low_cst (val, 0));
16281       break;
16282     case INDIRECT_REF:
16283       size = int_size_in_bytes (TREE_TYPE (val));
16284       if (size < 0)
16285         break;
16286       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16287       if (!loc)
16288         break;
16289       if (size == DWARF2_ADDR_SIZE)
16290         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16291       else
16292         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16293       return loc;
16294     case POINTER_PLUS_EXPR:
16295     case PLUS_EXPR:
16296       if (host_integerp (TREE_OPERAND (val, 1), 1)
16297           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16298              < 16384)
16299         {
16300           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16301           if (!loc)
16302             break;
16303           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16304         }
16305       else
16306         {
16307           op = DW_OP_plus;
16308         do_binop:
16309           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16310           if (!loc)
16311             break;
16312           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16313           if (!loc2)
16314             break;
16315           add_loc_descr (&loc, loc2);
16316           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16317         }
16318       return loc;
16319     case MINUS_EXPR:
16320       op = DW_OP_minus;
16321       goto do_binop;
16322     case MULT_EXPR:
16323       op = DW_OP_mul;
16324       goto do_binop;
16325     case EQ_EXPR:
16326       op = DW_OP_eq;
16327       goto do_binop;
16328     case NE_EXPR:
16329       op = DW_OP_ne;
16330       goto do_binop;
16331     default:
16332       break;
16333     }
16334   return NULL;
16335 }
16336
16337 static void
16338 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16339                       tree val, tree base_decl)
16340 {
16341   dw_loc_descr_ref loc;
16342
16343   if (host_integerp (val, 0))
16344     {
16345       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16346       return;
16347     }
16348
16349   loc = descr_info_loc (val, base_decl);
16350   if (!loc)
16351     return;
16352
16353   add_AT_loc (die, attr, loc);
16354 }
16355
16356 /* This routine generates DIE for array with hidden descriptor, details
16357    are filled into *info by a langhook.  */
16358
16359 static void
16360 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16361                           dw_die_ref context_die)
16362 {
16363   dw_die_ref scope_die = scope_die_for (type, context_die);
16364   dw_die_ref array_die;
16365   int dim;
16366
16367   array_die = new_die (DW_TAG_array_type, scope_die, type);
16368   add_name_attribute (array_die, type_tag (type));
16369   equate_type_number_to_die (type, array_die);
16370
16371   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16372   if (is_fortran ()
16373       && info->ndimensions >= 2)
16374     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16375
16376   if (info->data_location)
16377     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16378                           info->base_decl);
16379   if (info->associated)
16380     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16381                           info->base_decl);
16382   if (info->allocated)
16383     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16384                           info->base_decl);
16385
16386   for (dim = 0; dim < info->ndimensions; dim++)
16387     {
16388       dw_die_ref subrange_die
16389         = new_die (DW_TAG_subrange_type, array_die, NULL);
16390
16391       if (info->dimen[dim].lower_bound)
16392         {
16393           /* If it is the default value, omit it.  */
16394           int dflt;
16395
16396           if (host_integerp (info->dimen[dim].lower_bound, 0)
16397               && (dflt = lower_bound_default ()) != -1
16398               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16399             ;
16400           else
16401             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16402                                   info->dimen[dim].lower_bound,
16403                                   info->base_decl);
16404         }
16405       if (info->dimen[dim].upper_bound)
16406         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16407                               info->dimen[dim].upper_bound,
16408                               info->base_decl);
16409       if (info->dimen[dim].stride)
16410         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16411                               info->dimen[dim].stride,
16412                               info->base_decl);
16413     }
16414
16415   gen_type_die (info->element_type, context_die);
16416   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16417
16418   if (get_AT (array_die, DW_AT_name))
16419     add_pubtype (type, array_die);
16420 }
16421
16422 #if 0
16423 static void
16424 gen_entry_point_die (tree decl, dw_die_ref context_die)
16425 {
16426   tree origin = decl_ultimate_origin (decl);
16427   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16428
16429   if (origin != NULL)
16430     add_abstract_origin_attribute (decl_die, origin);
16431   else
16432     {
16433       add_name_and_src_coords_attributes (decl_die, decl);
16434       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16435                           0, 0, context_die);
16436     }
16437
16438   if (DECL_ABSTRACT (decl))
16439     equate_decl_number_to_die (decl, decl_die);
16440   else
16441     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16442 }
16443 #endif
16444
16445 /* Walk through the list of incomplete types again, trying once more to
16446    emit full debugging info for them.  */
16447
16448 static void
16449 retry_incomplete_types (void)
16450 {
16451   int i;
16452
16453   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16454     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16455                                   DINFO_USAGE_DIR_USE))
16456       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16457 }
16458
16459 /* Determine what tag to use for a record type.  */
16460
16461 static enum dwarf_tag
16462 record_type_tag (tree type)
16463 {
16464   if (! lang_hooks.types.classify_record)
16465     return DW_TAG_structure_type;
16466
16467   switch (lang_hooks.types.classify_record (type))
16468     {
16469     case RECORD_IS_STRUCT:
16470       return DW_TAG_structure_type;
16471
16472     case RECORD_IS_CLASS:
16473       return DW_TAG_class_type;
16474
16475     case RECORD_IS_INTERFACE:
16476       if (dwarf_version >= 3 || !dwarf_strict)
16477         return DW_TAG_interface_type;
16478       return DW_TAG_structure_type;
16479
16480     default:
16481       gcc_unreachable ();
16482     }
16483 }
16484
16485 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16486    include all of the information about the enumeration values also. Each
16487    enumerated type name/value is listed as a child of the enumerated type
16488    DIE.  */
16489
16490 static dw_die_ref
16491 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16492 {
16493   dw_die_ref type_die = lookup_type_die (type);
16494
16495   if (type_die == NULL)
16496     {
16497       type_die = new_die (DW_TAG_enumeration_type,
16498                           scope_die_for (type, context_die), type);
16499       equate_type_number_to_die (type, type_die);
16500       add_name_attribute (type_die, type_tag (type));
16501       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16502       if (TYPE_ARTIFICIAL (type))
16503         add_AT_flag (type_die, DW_AT_artificial, 1);
16504       if (dwarf_version >= 4 || !dwarf_strict)
16505         {
16506           if (ENUM_IS_SCOPED (type))
16507             add_AT_flag (type_die, DW_AT_enum_class, 1);
16508           if (ENUM_IS_OPAQUE (type))
16509             add_AT_flag (type_die, DW_AT_declaration, 1);
16510         }
16511     }
16512   else if (! TYPE_SIZE (type))
16513     return type_die;
16514   else
16515     remove_AT (type_die, DW_AT_declaration);
16516
16517   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16518      given enum type is incomplete, do not generate the DW_AT_byte_size
16519      attribute or the DW_AT_element_list attribute.  */
16520   if (TYPE_SIZE (type))
16521     {
16522       tree link;
16523
16524       TREE_ASM_WRITTEN (type) = 1;
16525       add_byte_size_attribute (type_die, type);
16526       if (TYPE_STUB_DECL (type) != NULL_TREE)
16527         {
16528           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16529           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16530         }
16531
16532       /* If the first reference to this type was as the return type of an
16533          inline function, then it may not have a parent.  Fix this now.  */
16534       if (type_die->die_parent == NULL)
16535         add_child_die (scope_die_for (type, context_die), type_die);
16536
16537       for (link = TYPE_VALUES (type);
16538            link != NULL; link = TREE_CHAIN (link))
16539         {
16540           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16541           tree value = TREE_VALUE (link);
16542
16543           add_name_attribute (enum_die,
16544                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16545
16546           if (TREE_CODE (value) == CONST_DECL)
16547             value = DECL_INITIAL (value);
16548
16549           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16550             /* DWARF2 does not provide a way of indicating whether or
16551                not enumeration constants are signed or unsigned.  GDB
16552                always assumes the values are signed, so we output all
16553                values as if they were signed.  That means that
16554                enumeration constants with very large unsigned values
16555                will appear to have negative values in the debugger.  */
16556             add_AT_int (enum_die, DW_AT_const_value,
16557                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16558         }
16559     }
16560   else
16561     add_AT_flag (type_die, DW_AT_declaration, 1);
16562
16563   if (get_AT (type_die, DW_AT_name))
16564     add_pubtype (type, type_die);
16565
16566   return type_die;
16567 }
16568
16569 /* Generate a DIE to represent either a real live formal parameter decl or to
16570    represent just the type of some formal parameter position in some function
16571    type.
16572
16573    Note that this routine is a bit unusual because its argument may be a
16574    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16575    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16576    node.  If it's the former then this function is being called to output a
16577    DIE to represent a formal parameter object (or some inlining thereof).  If
16578    it's the latter, then this function is only being called to output a
16579    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16580    argument type of some subprogram type.
16581    If EMIT_NAME_P is true, name and source coordinate attributes
16582    are emitted.  */
16583
16584 static dw_die_ref
16585 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16586                           dw_die_ref context_die)
16587 {
16588   tree node_or_origin = node ? node : origin;
16589   tree ultimate_origin;
16590   dw_die_ref parm_die
16591     = new_die (DW_TAG_formal_parameter, context_die, node);
16592
16593   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16594     {
16595     case tcc_declaration:
16596       ultimate_origin = decl_ultimate_origin (node_or_origin);
16597       if (node || ultimate_origin)
16598         origin = ultimate_origin;
16599       if (origin != NULL)
16600         add_abstract_origin_attribute (parm_die, origin);
16601       else if (emit_name_p)
16602         add_name_and_src_coords_attributes (parm_die, node);
16603       if (origin == NULL
16604           || (! DECL_ABSTRACT (node_or_origin)
16605               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16606                                            decl_function_context
16607                                                             (node_or_origin))))
16608         {
16609           tree type = TREE_TYPE (node_or_origin);
16610           if (decl_by_reference_p (node_or_origin))
16611             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16612                                 context_die);
16613           else
16614             add_type_attribute (parm_die, type,
16615                                 TREE_READONLY (node_or_origin),
16616                                 TREE_THIS_VOLATILE (node_or_origin),
16617                                 context_die);
16618         }
16619       if (origin == NULL && DECL_ARTIFICIAL (node))
16620         add_AT_flag (parm_die, DW_AT_artificial, 1);
16621
16622       if (node && node != origin)
16623         equate_decl_number_to_die (node, parm_die);
16624       if (! DECL_ABSTRACT (node_or_origin))
16625         add_location_or_const_value_attribute (parm_die, node_or_origin,
16626                                                node == NULL, DW_AT_location);
16627
16628       break;
16629
16630     case tcc_type:
16631       /* We were called with some kind of a ..._TYPE node.  */
16632       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16633       break;
16634
16635     default:
16636       gcc_unreachable ();
16637     }
16638
16639   return parm_die;
16640 }
16641
16642 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16643    children DW_TAG_formal_parameter DIEs representing the arguments of the
16644    parameter pack.
16645
16646    PARM_PACK must be a function parameter pack.
16647    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16648    must point to the subsequent arguments of the function PACK_ARG belongs to.
16649    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16650    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16651    following the last one for which a DIE was generated.  */
16652
16653 static dw_die_ref
16654 gen_formal_parameter_pack_die  (tree parm_pack,
16655                                 tree pack_arg,
16656                                 dw_die_ref subr_die,
16657                                 tree *next_arg)
16658 {
16659   tree arg;
16660   dw_die_ref parm_pack_die;
16661
16662   gcc_assert (parm_pack
16663               && lang_hooks.function_parameter_pack_p (parm_pack)
16664               && subr_die);
16665
16666   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16667   add_src_coords_attributes (parm_pack_die, parm_pack);
16668
16669   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16670     {
16671       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16672                                                                  parm_pack))
16673         break;
16674       gen_formal_parameter_die (arg, NULL,
16675                                 false /* Don't emit name attribute.  */,
16676                                 parm_pack_die);
16677     }
16678   if (next_arg)
16679     *next_arg = arg;
16680   return parm_pack_die;
16681 }
16682
16683 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16684    at the end of an (ANSI prototyped) formal parameters list.  */
16685
16686 static void
16687 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16688 {
16689   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16690 }
16691
16692 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16693    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16694    parameters as specified in some function type specification (except for
16695    those which appear as part of a function *definition*).  */
16696
16697 static void
16698 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16699 {
16700   tree link;
16701   tree formal_type = NULL;
16702   tree first_parm_type;
16703   tree arg;
16704
16705   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16706     {
16707       arg = DECL_ARGUMENTS (function_or_method_type);
16708       function_or_method_type = TREE_TYPE (function_or_method_type);
16709     }
16710   else
16711     arg = NULL_TREE;
16712
16713   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16714
16715   /* Make our first pass over the list of formal parameter types and output a
16716      DW_TAG_formal_parameter DIE for each one.  */
16717   for (link = first_parm_type; link; )
16718     {
16719       dw_die_ref parm_die;
16720
16721       formal_type = TREE_VALUE (link);
16722       if (formal_type == void_type_node)
16723         break;
16724
16725       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16726       parm_die = gen_formal_parameter_die (formal_type, NULL,
16727                                            true /* Emit name attribute.  */,
16728                                            context_die);
16729       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16730           && link == first_parm_type)
16731         {
16732           add_AT_flag (parm_die, DW_AT_artificial, 1);
16733           if (dwarf_version >= 3 || !dwarf_strict)
16734             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16735         }
16736       else if (arg && DECL_ARTIFICIAL (arg))
16737         add_AT_flag (parm_die, DW_AT_artificial, 1);
16738
16739       link = TREE_CHAIN (link);
16740       if (arg)
16741         arg = DECL_CHAIN (arg);
16742     }
16743
16744   /* If this function type has an ellipsis, add a
16745      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16746   if (formal_type != void_type_node)
16747     gen_unspecified_parameters_die (function_or_method_type, context_die);
16748
16749   /* Make our second (and final) pass over the list of formal parameter types
16750      and output DIEs to represent those types (as necessary).  */
16751   for (link = TYPE_ARG_TYPES (function_or_method_type);
16752        link && TREE_VALUE (link);
16753        link = TREE_CHAIN (link))
16754     gen_type_die (TREE_VALUE (link), context_die);
16755 }
16756
16757 /* We want to generate the DIE for TYPE so that we can generate the
16758    die for MEMBER, which has been defined; we will need to refer back
16759    to the member declaration nested within TYPE.  If we're trying to
16760    generate minimal debug info for TYPE, processing TYPE won't do the
16761    trick; we need to attach the member declaration by hand.  */
16762
16763 static void
16764 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16765 {
16766   gen_type_die (type, context_die);
16767
16768   /* If we're trying to avoid duplicate debug info, we may not have
16769      emitted the member decl for this function.  Emit it now.  */
16770   if (TYPE_STUB_DECL (type)
16771       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16772       && ! lookup_decl_die (member))
16773     {
16774       dw_die_ref type_die;
16775       gcc_assert (!decl_ultimate_origin (member));
16776
16777       push_decl_scope (type);
16778       type_die = lookup_type_die_strip_naming_typedef (type);
16779       if (TREE_CODE (member) == FUNCTION_DECL)
16780         gen_subprogram_die (member, type_die);
16781       else if (TREE_CODE (member) == FIELD_DECL)
16782         {
16783           /* Ignore the nameless fields that are used to skip bits but handle
16784              C++ anonymous unions and structs.  */
16785           if (DECL_NAME (member) != NULL_TREE
16786               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16787               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16788             {
16789               gen_type_die (member_declared_type (member), type_die);
16790               gen_field_die (member, type_die);
16791             }
16792         }
16793       else
16794         gen_variable_die (member, NULL_TREE, type_die);
16795
16796       pop_decl_scope ();
16797     }
16798 }
16799
16800 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16801    may later generate inlined and/or out-of-line instances of.  */
16802
16803 static void
16804 dwarf2out_abstract_function (tree decl)
16805 {
16806   dw_die_ref old_die;
16807   tree save_fn;
16808   tree context;
16809   int was_abstract;
16810   htab_t old_decl_loc_table;
16811   htab_t old_cached_dw_loc_list_table;
16812   int old_call_site_count, old_tail_call_site_count;
16813   struct call_arg_loc_node *old_call_arg_locations;
16814
16815   /* Make sure we have the actual abstract inline, not a clone.  */
16816   decl = DECL_ORIGIN (decl);
16817
16818   old_die = lookup_decl_die (decl);
16819   if (old_die && get_AT (old_die, DW_AT_inline))
16820     /* We've already generated the abstract instance.  */
16821     return;
16822
16823   /* We can be called while recursively when seeing block defining inlined subroutine
16824      DIE.  Be sure to not clobber the outer location table nor use it or we would
16825      get locations in abstract instantces.  */
16826   old_decl_loc_table = decl_loc_table;
16827   decl_loc_table = NULL;
16828   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16829   cached_dw_loc_list_table = NULL;
16830   old_call_arg_locations = call_arg_locations;
16831   call_arg_locations = NULL;
16832   old_call_site_count = call_site_count;
16833   call_site_count = -1;
16834   old_tail_call_site_count = tail_call_site_count;
16835   tail_call_site_count = -1;
16836
16837   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16838      we don't get confused by DECL_ABSTRACT.  */
16839   if (debug_info_level > DINFO_LEVEL_TERSE)
16840     {
16841       context = decl_class_context (decl);
16842       if (context)
16843         gen_type_die_for_member
16844           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16845     }
16846
16847   /* Pretend we've just finished compiling this function.  */
16848   save_fn = current_function_decl;
16849   current_function_decl = decl;
16850   push_cfun (DECL_STRUCT_FUNCTION (decl));
16851
16852   was_abstract = DECL_ABSTRACT (decl);
16853   set_decl_abstract_flags (decl, 1);
16854   dwarf2out_decl (decl);
16855   if (! was_abstract)
16856     set_decl_abstract_flags (decl, 0);
16857
16858   current_function_decl = save_fn;
16859   decl_loc_table = old_decl_loc_table;
16860   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16861   call_arg_locations = old_call_arg_locations;
16862   call_site_count = old_call_site_count;
16863   tail_call_site_count = old_tail_call_site_count;
16864   pop_cfun ();
16865 }
16866
16867 /* Helper function of premark_used_types() which gets called through
16868    htab_traverse.
16869
16870    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16871    marked as unused by prune_unused_types.  */
16872
16873 static int
16874 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16875 {
16876   tree type;
16877   dw_die_ref die;
16878
16879   type = (tree) *slot;
16880   die = lookup_type_die (type);
16881   if (die != NULL)
16882     die->die_perennial_p = 1;
16883   return 1;
16884 }
16885
16886 /* Helper function of premark_types_used_by_global_vars which gets called
16887    through htab_traverse.
16888
16889    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16890    marked as unused by prune_unused_types. The DIE of the type is marked
16891    only if the global variable using the type will actually be emitted.  */
16892
16893 static int
16894 premark_types_used_by_global_vars_helper (void **slot,
16895                                           void *data ATTRIBUTE_UNUSED)
16896 {
16897   struct types_used_by_vars_entry *entry;
16898   dw_die_ref die;
16899
16900   entry = (struct types_used_by_vars_entry *) *slot;
16901   gcc_assert (entry->type != NULL
16902               && entry->var_decl != NULL);
16903   die = lookup_type_die (entry->type);
16904   if (die)
16905     {
16906       /* Ask cgraph if the global variable really is to be emitted.
16907          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
16908       struct varpool_node *node = varpool_get_node (entry->var_decl);
16909       if (node && node->needed)
16910         {
16911           die->die_perennial_p = 1;
16912           /* Keep the parent DIEs as well.  */
16913           while ((die = die->die_parent) && die->die_perennial_p == 0)
16914             die->die_perennial_p = 1;
16915         }
16916     }
16917   return 1;
16918 }
16919
16920 /* Mark all members of used_types_hash as perennial.  */
16921
16922 static void
16923 premark_used_types (void)
16924 {
16925   if (cfun && cfun->used_types_hash)
16926     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
16927 }
16928
16929 /* Mark all members of types_used_by_vars_entry as perennial.  */
16930
16931 static void
16932 premark_types_used_by_global_vars (void)
16933 {
16934   if (types_used_by_vars_hash)
16935     htab_traverse (types_used_by_vars_hash,
16936                    premark_types_used_by_global_vars_helper, NULL);
16937 }
16938
16939 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16940    for CA_LOC call arg loc node.  */
16941
16942 static dw_die_ref
16943 gen_call_site_die (tree decl, dw_die_ref subr_die,
16944                    struct call_arg_loc_node *ca_loc)
16945 {
16946   dw_die_ref stmt_die = NULL, die;
16947   tree block = ca_loc->block;
16948
16949   while (block
16950          && block != DECL_INITIAL (decl)
16951          && TREE_CODE (block) == BLOCK)
16952     {
16953       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16954         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16955       if (stmt_die)
16956         break;
16957       block = BLOCK_SUPERCONTEXT (block);
16958     }
16959   if (stmt_die == NULL)
16960     stmt_die = subr_die;
16961   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16962   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16963   if (ca_loc->tail_call_p)
16964     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16965   if (ca_loc->symbol_ref)
16966     {
16967       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16968       if (tdie)
16969         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16970       else
16971         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16972     }
16973   return die;
16974 }
16975
16976 /* Generate a DIE to represent a declared function (either file-scope or
16977    block-local).  */
16978
16979 static void
16980 gen_subprogram_die (tree decl, dw_die_ref context_die)
16981 {
16982   tree origin = decl_ultimate_origin (decl);
16983   dw_die_ref subr_die;
16984   tree outer_scope;
16985   dw_die_ref old_die = lookup_decl_die (decl);
16986   int declaration = (current_function_decl != decl
16987                      || class_or_namespace_scope_p (context_die));
16988
16989   premark_used_types ();
16990
16991   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16992      started to generate the abstract instance of an inline, decided to output
16993      its containing class, and proceeded to emit the declaration of the inline
16994      from the member list for the class.  If so, DECLARATION takes priority;
16995      we'll get back to the abstract instance when done with the class.  */
16996
16997   /* The class-scope declaration DIE must be the primary DIE.  */
16998   if (origin && declaration && class_or_namespace_scope_p (context_die))
16999     {
17000       origin = NULL;
17001       gcc_assert (!old_die);
17002     }
17003
17004   /* Now that the C++ front end lazily declares artificial member fns, we
17005      might need to retrofit the declaration into its class.  */
17006   if (!declaration && !origin && !old_die
17007       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17008       && !class_or_namespace_scope_p (context_die)
17009       && debug_info_level > DINFO_LEVEL_TERSE)
17010     old_die = force_decl_die (decl);
17011
17012   if (origin != NULL)
17013     {
17014       gcc_assert (!declaration || local_scope_p (context_die));
17015
17016       /* Fixup die_parent for the abstract instance of a nested
17017          inline function.  */
17018       if (old_die && old_die->die_parent == NULL)
17019         add_child_die (context_die, old_die);
17020
17021       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17022       add_abstract_origin_attribute (subr_die, origin);
17023       /*  This is where the actual code for a cloned function is.
17024           Let's emit linkage name attribute for it.  This helps
17025           debuggers to e.g, set breakpoints into
17026           constructors/destructors when the user asks "break
17027           K::K".  */
17028       add_linkage_name (subr_die, decl);
17029     }
17030   else if (old_die)
17031     {
17032       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17033       struct dwarf_file_data * file_index = lookup_filename (s.file);
17034
17035       if (!get_AT_flag (old_die, DW_AT_declaration)
17036           /* We can have a normal definition following an inline one in the
17037              case of redefinition of GNU C extern inlines.
17038              It seems reasonable to use AT_specification in this case.  */
17039           && !get_AT (old_die, DW_AT_inline))
17040         {
17041           /* Detect and ignore this case, where we are trying to output
17042              something we have already output.  */
17043           return;
17044         }
17045
17046       /* If the definition comes from the same place as the declaration,
17047          maybe use the old DIE.  We always want the DIE for this function
17048          that has the *_pc attributes to be under comp_unit_die so the
17049          debugger can find it.  We also need to do this for abstract
17050          instances of inlines, since the spec requires the out-of-line copy
17051          to have the same parent.  For local class methods, this doesn't
17052          apply; we just use the old DIE.  */
17053       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17054           && (DECL_ARTIFICIAL (decl)
17055               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17056                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17057                       == (unsigned) s.line))))
17058         {
17059           subr_die = old_die;
17060
17061           /* Clear out the declaration attribute and the formal parameters.
17062              Do not remove all children, because it is possible that this
17063              declaration die was forced using force_decl_die(). In such
17064              cases die that forced declaration die (e.g. TAG_imported_module)
17065              is one of the children that we do not want to remove.  */
17066           remove_AT (subr_die, DW_AT_declaration);
17067           remove_AT (subr_die, DW_AT_object_pointer);
17068           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17069         }
17070       else
17071         {
17072           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17073           add_AT_specification (subr_die, old_die);
17074           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17075             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17076           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17077             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17078         }
17079     }
17080   else
17081     {
17082       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17083
17084       if (TREE_PUBLIC (decl))
17085         add_AT_flag (subr_die, DW_AT_external, 1);
17086
17087       add_name_and_src_coords_attributes (subr_die, decl);
17088       if (debug_info_level > DINFO_LEVEL_TERSE)
17089         {
17090           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17091           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17092                               0, 0, context_die);
17093         }
17094
17095       add_pure_or_virtual_attribute (subr_die, decl);
17096       if (DECL_ARTIFICIAL (decl))
17097         add_AT_flag (subr_die, DW_AT_artificial, 1);
17098
17099       add_accessibility_attribute (subr_die, decl);
17100     }
17101
17102   if (declaration)
17103     {
17104       if (!old_die || !get_AT (old_die, DW_AT_inline))
17105         {
17106           add_AT_flag (subr_die, DW_AT_declaration, 1);
17107
17108           /* If this is an explicit function declaration then generate
17109              a DW_AT_explicit attribute.  */
17110           if (lang_hooks.decls.function_decl_explicit_p (decl)
17111               && (dwarf_version >= 3 || !dwarf_strict))
17112             add_AT_flag (subr_die, DW_AT_explicit, 1);
17113
17114           /* The first time we see a member function, it is in the context of
17115              the class to which it belongs.  We make sure of this by emitting
17116              the class first.  The next time is the definition, which is
17117              handled above.  The two may come from the same source text.
17118
17119              Note that force_decl_die() forces function declaration die. It is
17120              later reused to represent definition.  */
17121           equate_decl_number_to_die (decl, subr_die);
17122         }
17123     }
17124   else if (DECL_ABSTRACT (decl))
17125     {
17126       if (DECL_DECLARED_INLINE_P (decl))
17127         {
17128           if (cgraph_function_possibly_inlined_p (decl))
17129             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17130           else
17131             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17132         }
17133       else
17134         {
17135           if (cgraph_function_possibly_inlined_p (decl))
17136             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17137           else
17138             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17139         }
17140
17141       if (DECL_DECLARED_INLINE_P (decl)
17142           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17143         add_AT_flag (subr_die, DW_AT_artificial, 1);
17144
17145       equate_decl_number_to_die (decl, subr_die);
17146     }
17147   else if (!DECL_EXTERNAL (decl))
17148     {
17149       HOST_WIDE_INT cfa_fb_offset;
17150
17151       if (!old_die || !get_AT (old_die, DW_AT_inline))
17152         equate_decl_number_to_die (decl, subr_die);
17153
17154       if (!flag_reorder_blocks_and_partition)
17155         {
17156           dw_fde_ref fde = cfun->fde;
17157           if (fde->dw_fde_begin)
17158             {
17159               /* We have already generated the labels.  */
17160               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17161               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17162             }
17163           else
17164             {
17165               /* Create start/end labels and add the range.  */
17166               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17167               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17168                                            current_function_funcdef_no);
17169               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17170               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17171                                            current_function_funcdef_no);
17172               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17173             }
17174
17175 #if VMS_DEBUGGING_INFO
17176       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17177          Section 2.3 Prologue and Epilogue Attributes:
17178          When a breakpoint is set on entry to a function, it is generally
17179          desirable for execution to be suspended, not on the very first
17180          instruction of the function, but rather at a point after the
17181          function's frame has been set up, after any language defined local
17182          declaration processing has been completed, and before execution of
17183          the first statement of the function begins. Debuggers generally
17184          cannot properly determine where this point is.  Similarly for a
17185          breakpoint set on exit from a function. The prologue and epilogue
17186          attributes allow a compiler to communicate the location(s) to use.  */
17187
17188       {
17189         if (fde->dw_fde_vms_end_prologue)
17190           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17191             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17192
17193         if (fde->dw_fde_vms_begin_epilogue)
17194           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17195             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17196       }
17197 #endif
17198
17199           add_pubname (decl, subr_die);
17200         }
17201       else
17202         {
17203           /* Generate pubnames entries for the split function code ranges.  */
17204           dw_fde_ref fde = cfun->fde;
17205
17206           if (fde->dw_fde_second_begin)
17207             {
17208               if (dwarf_version >= 3 || !dwarf_strict)
17209                 {
17210                   /* We should use ranges for non-contiguous code section 
17211                      addresses.  Use the actual code range for the initial
17212                      section, since the HOT/COLD labels might precede an 
17213                      alignment offset.  */
17214                   bool range_list_added = false;
17215                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17216                                         fde->dw_fde_end, &range_list_added);
17217                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17218                                         fde->dw_fde_second_end,
17219                                         &range_list_added);
17220                   add_pubname (decl, subr_die);
17221                   if (range_list_added)
17222                     add_ranges (NULL);
17223                 }
17224               else
17225                 {
17226                   /* There is no real support in DW2 for this .. so we make
17227                      a work-around.  First, emit the pub name for the segment
17228                      containing the function label.  Then make and emit a
17229                      simplified subprogram DIE for the second segment with the
17230                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17231                      linkage name for the second die so that gdb will find both
17232                      sections when given "b foo".  */
17233                   const char *name = NULL;
17234                   tree decl_name = DECL_NAME (decl);
17235                   dw_die_ref seg_die;
17236
17237                   /* Do the 'primary' section.   */
17238                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17239                                  fde->dw_fde_begin);
17240                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17241                                  fde->dw_fde_end);
17242                   /* Add it.   */
17243                   add_pubname (decl, subr_die);
17244
17245                   /* Build a minimal DIE for the secondary section.  */
17246                   seg_die = new_die (DW_TAG_subprogram,
17247                                      subr_die->die_parent, decl);
17248
17249                   if (TREE_PUBLIC (decl))
17250                     add_AT_flag (seg_die, DW_AT_external, 1);
17251
17252                   if (decl_name != NULL 
17253                       && IDENTIFIER_POINTER (decl_name) != NULL)
17254                     {
17255                       name = dwarf2_name (decl, 1);
17256                       if (! DECL_ARTIFICIAL (decl))
17257                         add_src_coords_attributes (seg_die, decl);
17258
17259                       add_linkage_name (seg_die, decl);
17260                     }
17261                   gcc_assert (name != NULL);
17262                   add_pure_or_virtual_attribute (seg_die, decl);
17263                   if (DECL_ARTIFICIAL (decl))
17264                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17265
17266                   name = concat ("__second_sect_of_", name, NULL); 
17267                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17268                                  fde->dw_fde_second_begin);
17269                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17270                                  fde->dw_fde_second_end);
17271                   add_name_attribute (seg_die, name);
17272                   add_pubname_string (name, seg_die);
17273                 }
17274             }
17275           else
17276             {
17277               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17278               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17279               add_pubname (decl, subr_die);
17280             }
17281         }
17282
17283 #ifdef MIPS_DEBUGGING_INFO
17284       /* Add a reference to the FDE for this routine.  */
17285       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17286 #endif
17287
17288       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17289
17290       /* We define the "frame base" as the function's CFA.  This is more
17291          convenient for several reasons: (1) It's stable across the prologue
17292          and epilogue, which makes it better than just a frame pointer,
17293          (2) With dwarf3, there exists a one-byte encoding that allows us
17294          to reference the .debug_frame data by proxy, but failing that,
17295          (3) We can at least reuse the code inspection and interpretation
17296          code that determines the CFA position at various points in the
17297          function.  */
17298       if (dwarf_version >= 3)
17299         {
17300           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17301           add_AT_loc (subr_die, DW_AT_frame_base, op);
17302         }
17303       else
17304         {
17305           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17306           if (list->dw_loc_next)
17307             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17308           else
17309             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17310         }
17311
17312       /* Compute a displacement from the "steady-state frame pointer" to
17313          the CFA.  The former is what all stack slots and argument slots
17314          will reference in the rtl; the later is what we've told the
17315          debugger about.  We'll need to adjust all frame_base references
17316          by this displacement.  */
17317       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17318
17319       if (cfun->static_chain_decl)
17320         add_AT_location_description (subr_die, DW_AT_static_link,
17321                  loc_list_from_tree (cfun->static_chain_decl, 2));
17322     }
17323
17324   /* Generate child dies for template paramaters.  */
17325   if (debug_info_level > DINFO_LEVEL_TERSE)
17326     gen_generic_params_dies (decl);
17327
17328   /* Now output descriptions of the arguments for this function. This gets
17329      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17330      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17331      `...' at the end of the formal parameter list.  In order to find out if
17332      there was a trailing ellipsis or not, we must instead look at the type
17333      associated with the FUNCTION_DECL.  This will be a node of type
17334      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17335      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17336      an ellipsis at the end.  */
17337
17338   /* In the case where we are describing a mere function declaration, all we
17339      need to do here (and all we *can* do here) is to describe the *types* of
17340      its formal parameters.  */
17341   if (debug_info_level <= DINFO_LEVEL_TERSE)
17342     ;
17343   else if (declaration)
17344     gen_formal_types_die (decl, subr_die);
17345   else
17346     {
17347       /* Generate DIEs to represent all known formal parameters.  */
17348       tree parm = DECL_ARGUMENTS (decl);
17349       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17350       tree generic_decl_parm = generic_decl
17351                                 ? DECL_ARGUMENTS (generic_decl)
17352                                 : NULL;
17353
17354       /* Now we want to walk the list of parameters of the function and
17355          emit their relevant DIEs.
17356
17357          We consider the case of DECL being an instance of a generic function
17358          as well as it being a normal function.
17359
17360          If DECL is an instance of a generic function we walk the
17361          parameters of the generic function declaration _and_ the parameters of
17362          DECL itself. This is useful because we want to emit specific DIEs for
17363          function parameter packs and those are declared as part of the
17364          generic function declaration. In that particular case,
17365          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17366          That DIE has children DIEs representing the set of arguments
17367          of the pack. Note that the set of pack arguments can be empty.
17368          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17369          children DIE.
17370
17371          Otherwise, we just consider the parameters of DECL.  */
17372       while (generic_decl_parm || parm)
17373         {
17374           if (generic_decl_parm
17375               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17376             gen_formal_parameter_pack_die (generic_decl_parm,
17377                                            parm, subr_die,
17378                                            &parm);
17379           else if (parm)
17380             {
17381               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17382
17383               if (parm == DECL_ARGUMENTS (decl)
17384                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17385                   && parm_die
17386                   && (dwarf_version >= 3 || !dwarf_strict))
17387                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17388
17389               parm = DECL_CHAIN (parm);
17390             }
17391
17392           if (generic_decl_parm)
17393             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17394         }
17395
17396       /* Decide whether we need an unspecified_parameters DIE at the end.
17397          There are 2 more cases to do this for: 1) the ansi ... declaration -
17398          this is detectable when the end of the arg list is not a
17399          void_type_node 2) an unprototyped function declaration (not a
17400          definition).  This just means that we have no info about the
17401          parameters at all.  */
17402       if (prototype_p (TREE_TYPE (decl)))
17403         {
17404           /* This is the prototyped case, check for....  */
17405           if (stdarg_p (TREE_TYPE (decl)))
17406             gen_unspecified_parameters_die (decl, subr_die);
17407         }
17408       else if (DECL_INITIAL (decl) == NULL_TREE)
17409         gen_unspecified_parameters_die (decl, subr_die);
17410     }
17411
17412   /* Output Dwarf info for all of the stuff within the body of the function
17413      (if it has one - it may be just a declaration).  */
17414   outer_scope = DECL_INITIAL (decl);
17415
17416   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17417      a function.  This BLOCK actually represents the outermost binding contour
17418      for the function, i.e. the contour in which the function's formal
17419      parameters and labels get declared. Curiously, it appears that the front
17420      end doesn't actually put the PARM_DECL nodes for the current function onto
17421      the BLOCK_VARS list for this outer scope, but are strung off of the
17422      DECL_ARGUMENTS list for the function instead.
17423
17424      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17425      the LABEL_DECL nodes for the function however, and we output DWARF info
17426      for those in decls_for_scope.  Just within the `outer_scope' there will be
17427      a BLOCK node representing the function's outermost pair of curly braces,
17428      and any blocks used for the base and member initializers of a C++
17429      constructor function.  */
17430   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17431     {
17432       int call_site_note_count = 0;
17433       int tail_call_site_note_count = 0;
17434
17435       /* Emit a DW_TAG_variable DIE for a named return value.  */
17436       if (DECL_NAME (DECL_RESULT (decl)))
17437         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17438
17439       current_function_has_inlines = 0;
17440       decls_for_scope (outer_scope, subr_die, 0);
17441
17442       if (call_arg_locations && !dwarf_strict)
17443         {
17444           struct call_arg_loc_node *ca_loc;
17445           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17446             {
17447               dw_die_ref die = NULL;
17448               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17449               rtx arg, next_arg;
17450
17451               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17452                    arg; arg = next_arg)
17453                 {
17454                   dw_loc_descr_ref reg, val;
17455                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17456                   dw_die_ref cdie, tdie = NULL;
17457
17458                   next_arg = XEXP (arg, 1);
17459                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17460                       && next_arg
17461                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17462                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17463                       && REGNO (XEXP (XEXP (arg, 0), 0))
17464                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17465                     next_arg = XEXP (next_arg, 1);
17466                   if (mode == VOIDmode)
17467                     {
17468                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17469                       if (mode == VOIDmode)
17470                         mode = GET_MODE (XEXP (arg, 0));
17471                     }
17472                   if (mode == VOIDmode || mode == BLKmode)
17473                     continue;
17474                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17475                     {
17476                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17477                       tloc = XEXP (XEXP (arg, 0), 1);
17478                       continue;
17479                     }
17480                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17481                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17482                     {
17483                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17484                       tlocc = XEXP (XEXP (arg, 0), 1);
17485                       continue;
17486                     }
17487                   reg = NULL;
17488                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17489                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17490                                               VAR_INIT_STATUS_INITIALIZED);
17491                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17492                     {
17493                       rtx mem = XEXP (XEXP (arg, 0), 0);
17494                       reg = mem_loc_descriptor (XEXP (mem, 0),
17495                                                 get_address_mode (mem),
17496                                                 GET_MODE (mem),
17497                                                 VAR_INIT_STATUS_INITIALIZED);
17498                     }
17499                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17500                            == DEBUG_PARAMETER_REF)
17501                     {
17502                       tree tdecl
17503                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17504                       tdie = lookup_decl_die (tdecl);
17505                       if (tdie == NULL)
17506                         continue;
17507                     }
17508                   else
17509                     continue;
17510                   if (reg == NULL
17511                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17512                          != DEBUG_PARAMETER_REF)
17513                     continue;
17514                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17515                                             VOIDmode,
17516                                             VAR_INIT_STATUS_INITIALIZED);
17517                   if (val == NULL)
17518                     continue;
17519                   if (die == NULL)
17520                     die = gen_call_site_die (decl, subr_die, ca_loc);
17521                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17522                                   NULL_TREE);
17523                   if (reg != NULL)
17524                     add_AT_loc (cdie, DW_AT_location, reg);
17525                   else if (tdie != NULL)
17526                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17527                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17528                   if (next_arg != XEXP (arg, 1))
17529                     {
17530                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17531                       if (mode == VOIDmode)
17532                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17533                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17534                                                             0), 1),
17535                                                 mode, VOIDmode,
17536                                                 VAR_INIT_STATUS_INITIALIZED);
17537                       if (val != NULL)
17538                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17539                     }
17540                 }
17541               if (die == NULL
17542                   && (ca_loc->symbol_ref || tloc))
17543                 die = gen_call_site_die (decl, subr_die, ca_loc);
17544               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17545                 {
17546                   dw_loc_descr_ref tval = NULL;
17547
17548                   if (tloc != NULL_RTX)
17549                     tval = mem_loc_descriptor (tloc,
17550                                                GET_MODE (tloc) == VOIDmode
17551                                                ? Pmode : GET_MODE (tloc),
17552                                                VOIDmode,
17553                                                VAR_INIT_STATUS_INITIALIZED);
17554                   if (tval)
17555                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17556                   else if (tlocc != NULL_RTX)
17557                     {
17558                       tval = mem_loc_descriptor (tlocc,
17559                                                  GET_MODE (tlocc) == VOIDmode
17560                                                  ? Pmode : GET_MODE (tlocc),
17561                                                  VOIDmode,
17562                                                  VAR_INIT_STATUS_INITIALIZED);
17563                       if (tval)
17564                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17565                                     tval);
17566                     }
17567                 }
17568               if (die != NULL)
17569                 {
17570                   call_site_note_count++;
17571                   if (ca_loc->tail_call_p)
17572                     tail_call_site_note_count++;
17573                 }
17574             }
17575         }
17576       call_arg_locations = NULL;
17577       call_arg_loc_last = NULL;
17578       if (tail_call_site_count >= 0
17579           && tail_call_site_count == tail_call_site_note_count
17580           && !dwarf_strict)
17581         {
17582           if (call_site_count >= 0
17583               && call_site_count == call_site_note_count)
17584             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17585           else
17586             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17587         }
17588       call_site_count = -1;
17589       tail_call_site_count = -1;
17590     }
17591   /* Add the calling convention attribute if requested.  */
17592   add_calling_convention_attribute (subr_die, decl);
17593
17594 }
17595
17596 /* Returns a hash value for X (which really is a die_struct).  */
17597
17598 static hashval_t
17599 common_block_die_table_hash (const void *x)
17600 {
17601   const_dw_die_ref d = (const_dw_die_ref) x;
17602   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17603 }
17604
17605 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17606    as decl_id and die_parent of die_struct Y.  */
17607
17608 static int
17609 common_block_die_table_eq (const void *x, const void *y)
17610 {
17611   const_dw_die_ref d = (const_dw_die_ref) x;
17612   const_dw_die_ref e = (const_dw_die_ref) y;
17613   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17614 }
17615
17616 /* Generate a DIE to represent a declared data object.
17617    Either DECL or ORIGIN must be non-null.  */
17618
17619 static void
17620 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17621 {
17622   HOST_WIDE_INT off;
17623   tree com_decl;
17624   tree decl_or_origin = decl ? decl : origin;
17625   tree ultimate_origin;
17626   dw_die_ref var_die;
17627   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17628   dw_die_ref origin_die;
17629   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17630                       || class_or_namespace_scope_p (context_die));
17631   bool specialization_p = false;
17632
17633   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17634   if (decl || ultimate_origin)
17635     origin = ultimate_origin;
17636   com_decl = fortran_common (decl_or_origin, &off);
17637
17638   /* Symbol in common gets emitted as a child of the common block, in the form
17639      of a data member.  */
17640   if (com_decl)
17641     {
17642       dw_die_ref com_die;
17643       dw_loc_list_ref loc;
17644       die_node com_die_arg;
17645
17646       var_die = lookup_decl_die (decl_or_origin);
17647       if (var_die)
17648         {
17649           if (get_AT (var_die, DW_AT_location) == NULL)
17650             {
17651               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17652               if (loc)
17653                 {
17654                   if (off)
17655                     {
17656                       /* Optimize the common case.  */
17657                       if (single_element_loc_list_p (loc)
17658                           && loc->expr->dw_loc_opc == DW_OP_addr
17659                           && loc->expr->dw_loc_next == NULL
17660                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17661                              == SYMBOL_REF)
17662                         loc->expr->dw_loc_oprnd1.v.val_addr
17663                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17664                         else
17665                           loc_list_plus_const (loc, off);
17666                     }
17667                   add_AT_location_description (var_die, DW_AT_location, loc);
17668                   remove_AT (var_die, DW_AT_declaration);
17669                 }
17670             }
17671           return;
17672         }
17673
17674       if (common_block_die_table == NULL)
17675         common_block_die_table
17676           = htab_create_ggc (10, common_block_die_table_hash,
17677                              common_block_die_table_eq, NULL);
17678
17679       com_die_arg.decl_id = DECL_UID (com_decl);
17680       com_die_arg.die_parent = context_die;
17681       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17682       loc = loc_list_from_tree (com_decl, 2);
17683       if (com_die == NULL)
17684         {
17685           const char *cnam
17686             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17687           void **slot;
17688
17689           com_die = new_die (DW_TAG_common_block, context_die, decl);
17690           add_name_and_src_coords_attributes (com_die, com_decl);
17691           if (loc)
17692             {
17693               add_AT_location_description (com_die, DW_AT_location, loc);
17694               /* Avoid sharing the same loc descriptor between
17695                  DW_TAG_common_block and DW_TAG_variable.  */
17696               loc = loc_list_from_tree (com_decl, 2);
17697             }
17698           else if (DECL_EXTERNAL (decl))
17699             add_AT_flag (com_die, DW_AT_declaration, 1);
17700           add_pubname_string (cnam, com_die); /* ??? needed? */
17701           com_die->decl_id = DECL_UID (com_decl);
17702           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17703           *slot = (void *) com_die;
17704         }
17705       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17706         {
17707           add_AT_location_description (com_die, DW_AT_location, loc);
17708           loc = loc_list_from_tree (com_decl, 2);
17709           remove_AT (com_die, DW_AT_declaration);
17710         }
17711       var_die = new_die (DW_TAG_variable, com_die, decl);
17712       add_name_and_src_coords_attributes (var_die, decl);
17713       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17714                           TREE_THIS_VOLATILE (decl), context_die);
17715       add_AT_flag (var_die, DW_AT_external, 1);
17716       if (loc)
17717         {
17718           if (off)
17719             {
17720               /* Optimize the common case.  */
17721               if (single_element_loc_list_p (loc)
17722                   && loc->expr->dw_loc_opc == DW_OP_addr
17723                   && loc->expr->dw_loc_next == NULL
17724                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17725                 loc->expr->dw_loc_oprnd1.v.val_addr
17726                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17727               else
17728                 loc_list_plus_const (loc, off);
17729             }
17730           add_AT_location_description (var_die, DW_AT_location, loc);
17731         }
17732       else if (DECL_EXTERNAL (decl))
17733         add_AT_flag (var_die, DW_AT_declaration, 1);
17734       equate_decl_number_to_die (decl, var_die);
17735       return;
17736     }
17737
17738   /* If the compiler emitted a definition for the DECL declaration
17739      and if we already emitted a DIE for it, don't emit a second
17740      DIE for it again. Allow re-declarations of DECLs that are
17741      inside functions, though.  */
17742   if (old_die && declaration && !local_scope_p (context_die))
17743     return;
17744
17745   /* For static data members, the declaration in the class is supposed
17746      to have DW_TAG_member tag; the specification should still be
17747      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17748   if (declaration && class_scope_p (context_die))
17749     var_die = new_die (DW_TAG_member, context_die, decl);
17750   else
17751     var_die = new_die (DW_TAG_variable, context_die, decl);
17752
17753   origin_die = NULL;
17754   if (origin != NULL)
17755     origin_die = add_abstract_origin_attribute (var_die, origin);
17756
17757   /* Loop unrolling can create multiple blocks that refer to the same
17758      static variable, so we must test for the DW_AT_declaration flag.
17759
17760      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17761      copy decls and set the DECL_ABSTRACT flag on them instead of
17762      sharing them.
17763
17764      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17765
17766      ??? The declare_in_namespace support causes us to get two DIEs for one
17767      variable, both of which are declarations.  We want to avoid considering
17768      one to be a specification, so we must test that this DIE is not a
17769      declaration.  */
17770   else if (old_die && TREE_STATIC (decl) && ! declaration
17771            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17772     {
17773       /* This is a definition of a C++ class level static.  */
17774       add_AT_specification (var_die, old_die);
17775       specialization_p = true;
17776       if (DECL_NAME (decl))
17777         {
17778           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17779           struct dwarf_file_data * file_index = lookup_filename (s.file);
17780
17781           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17782             add_AT_file (var_die, DW_AT_decl_file, file_index);
17783
17784           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17785             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17786
17787           if (old_die->die_tag == DW_TAG_member)
17788             add_linkage_name (var_die, decl);
17789         }
17790     }
17791   else
17792     add_name_and_src_coords_attributes (var_die, decl);
17793
17794   if ((origin == NULL && !specialization_p)
17795       || (origin != NULL
17796           && !DECL_ABSTRACT (decl_or_origin)
17797           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17798                                        decl_function_context
17799                                                         (decl_or_origin))))
17800     {
17801       tree type = TREE_TYPE (decl_or_origin);
17802
17803       if (decl_by_reference_p (decl_or_origin))
17804         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17805       else
17806         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17807                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17808     }
17809
17810   if (origin == NULL && !specialization_p)
17811     {
17812       if (TREE_PUBLIC (decl))
17813         add_AT_flag (var_die, DW_AT_external, 1);
17814
17815       if (DECL_ARTIFICIAL (decl))
17816         add_AT_flag (var_die, DW_AT_artificial, 1);
17817
17818       add_accessibility_attribute (var_die, decl);
17819     }
17820
17821   if (declaration)
17822     add_AT_flag (var_die, DW_AT_declaration, 1);
17823
17824   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17825     equate_decl_number_to_die (decl, var_die);
17826
17827   if (! declaration
17828       && (! DECL_ABSTRACT (decl_or_origin)
17829           /* Local static vars are shared between all clones/inlines,
17830              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17831              already set.  */
17832           || (TREE_CODE (decl_or_origin) == VAR_DECL
17833               && TREE_STATIC (decl_or_origin)
17834               && DECL_RTL_SET_P (decl_or_origin)))
17835       /* When abstract origin already has DW_AT_location attribute, no need
17836          to add it again.  */
17837       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17838     {
17839       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17840           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17841         defer_location (decl_or_origin, var_die);
17842       else
17843         add_location_or_const_value_attribute (var_die, decl_or_origin,
17844                                                decl == NULL, DW_AT_location);
17845       add_pubname (decl_or_origin, var_die);
17846     }
17847   else
17848     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17849 }
17850
17851 /* Generate a DIE to represent a named constant.  */
17852
17853 static void
17854 gen_const_die (tree decl, dw_die_ref context_die)
17855 {
17856   dw_die_ref const_die;
17857   tree type = TREE_TYPE (decl);
17858
17859   const_die = new_die (DW_TAG_constant, context_die, decl);
17860   add_name_and_src_coords_attributes (const_die, decl);
17861   add_type_attribute (const_die, type, 1, 0, context_die);
17862   if (TREE_PUBLIC (decl))
17863     add_AT_flag (const_die, DW_AT_external, 1);
17864   if (DECL_ARTIFICIAL (decl))
17865     add_AT_flag (const_die, DW_AT_artificial, 1);
17866   tree_add_const_value_attribute_for_decl (const_die, decl);
17867 }
17868
17869 /* Generate a DIE to represent a label identifier.  */
17870
17871 static void
17872 gen_label_die (tree decl, dw_die_ref context_die)
17873 {
17874   tree origin = decl_ultimate_origin (decl);
17875   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17876   rtx insn;
17877   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17878
17879   if (origin != NULL)
17880     add_abstract_origin_attribute (lbl_die, origin);
17881   else
17882     add_name_and_src_coords_attributes (lbl_die, decl);
17883
17884   if (DECL_ABSTRACT (decl))
17885     equate_decl_number_to_die (decl, lbl_die);
17886   else
17887     {
17888       insn = DECL_RTL_IF_SET (decl);
17889
17890       /* Deleted labels are programmer specified labels which have been
17891          eliminated because of various optimizations.  We still emit them
17892          here so that it is possible to put breakpoints on them.  */
17893       if (insn
17894           && (LABEL_P (insn)
17895               || ((NOTE_P (insn)
17896                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17897         {
17898           /* When optimization is enabled (via -O) some parts of the compiler
17899              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17900              represent source-level labels which were explicitly declared by
17901              the user.  This really shouldn't be happening though, so catch
17902              it if it ever does happen.  */
17903           gcc_assert (!INSN_DELETED_P (insn));
17904
17905           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17906           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17907         }
17908     }
17909 }
17910
17911 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
17912    attributes to the DIE for a block STMT, to describe where the inlined
17913    function was called from.  This is similar to add_src_coords_attributes.  */
17914
17915 static inline void
17916 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17917 {
17918   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17919
17920   if (dwarf_version >= 3 || !dwarf_strict)
17921     {
17922       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17923       add_AT_unsigned (die, DW_AT_call_line, s.line);
17924     }
17925 }
17926
17927
17928 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17929    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
17930
17931 static inline void
17932 add_high_low_attributes (tree stmt, dw_die_ref die)
17933 {
17934   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17935
17936   if (BLOCK_FRAGMENT_CHAIN (stmt)
17937       && (dwarf_version >= 3 || !dwarf_strict))
17938     {
17939       tree chain;
17940
17941       if (inlined_function_outer_scope_p (stmt))
17942         {
17943           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17944                                        BLOCK_NUMBER (stmt));
17945           add_AT_lbl_id (die, DW_AT_entry_pc, label);
17946         }
17947
17948       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17949
17950       chain = BLOCK_FRAGMENT_CHAIN (stmt);
17951       do
17952         {
17953           add_ranges (chain);
17954           chain = BLOCK_FRAGMENT_CHAIN (chain);
17955         }
17956       while (chain);
17957       add_ranges (NULL);
17958     }
17959   else
17960     {
17961       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17962                                    BLOCK_NUMBER (stmt));
17963       add_AT_lbl_id (die, DW_AT_low_pc, label);
17964       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
17965                                    BLOCK_NUMBER (stmt));
17966       add_AT_lbl_id (die, DW_AT_high_pc, label);
17967     }
17968 }
17969
17970 /* Generate a DIE for a lexical block.  */
17971
17972 static void
17973 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17974 {
17975   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17976
17977   if (call_arg_locations)
17978     {
17979       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17980         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17981                                BLOCK_NUMBER (stmt) + 1);
17982       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17983     }
17984
17985   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17986     add_high_low_attributes (stmt, stmt_die);
17987
17988   decls_for_scope (stmt, stmt_die, depth);
17989 }
17990
17991 /* Generate a DIE for an inlined subprogram.  */
17992
17993 static void
17994 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17995 {
17996   tree decl;
17997
17998   /* The instance of function that is effectively being inlined shall not
17999      be abstract.  */
18000   gcc_assert (! BLOCK_ABSTRACT (stmt));
18001
18002   decl = block_ultimate_origin (stmt);
18003
18004   /* Emit info for the abstract instance first, if we haven't yet.  We
18005      must emit this even if the block is abstract, otherwise when we
18006      emit the block below (or elsewhere), we may end up trying to emit
18007      a die whose origin die hasn't been emitted, and crashing.  */
18008   dwarf2out_abstract_function (decl);
18009
18010   if (! BLOCK_ABSTRACT (stmt))
18011     {
18012       dw_die_ref subr_die
18013         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18014
18015       if (call_arg_locations)
18016         {
18017           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18018             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18019                                    BLOCK_NUMBER (stmt) + 1);
18020           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18021         }
18022       add_abstract_origin_attribute (subr_die, decl);
18023       if (TREE_ASM_WRITTEN (stmt))
18024         add_high_low_attributes (stmt, subr_die);
18025       add_call_src_coords_attributes (stmt, subr_die);
18026
18027       decls_for_scope (stmt, subr_die, depth);
18028       current_function_has_inlines = 1;
18029     }
18030 }
18031
18032 /* Generate a DIE for a field in a record, or structure.  */
18033
18034 static void
18035 gen_field_die (tree decl, dw_die_ref context_die)
18036 {
18037   dw_die_ref decl_die;
18038
18039   if (TREE_TYPE (decl) == error_mark_node)
18040     return;
18041
18042   decl_die = new_die (DW_TAG_member, context_die, decl);
18043   add_name_and_src_coords_attributes (decl_die, decl);
18044   add_type_attribute (decl_die, member_declared_type (decl),
18045                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18046                       context_die);
18047
18048   if (DECL_BIT_FIELD_TYPE (decl))
18049     {
18050       add_byte_size_attribute (decl_die, decl);
18051       add_bit_size_attribute (decl_die, decl);
18052       add_bit_offset_attribute (decl_die, decl);
18053     }
18054
18055   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18056     add_data_member_location_attribute (decl_die, decl);
18057
18058   if (DECL_ARTIFICIAL (decl))
18059     add_AT_flag (decl_die, DW_AT_artificial, 1);
18060
18061   add_accessibility_attribute (decl_die, decl);
18062
18063   /* Equate decl number to die, so that we can look up this decl later on.  */
18064   equate_decl_number_to_die (decl, decl_die);
18065 }
18066
18067 #if 0
18068 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18069    Use modified_type_die instead.
18070    We keep this code here just in case these types of DIEs may be needed to
18071    represent certain things in other languages (e.g. Pascal) someday.  */
18072
18073 static void
18074 gen_pointer_type_die (tree type, dw_die_ref context_die)
18075 {
18076   dw_die_ref ptr_die
18077     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18078
18079   equate_type_number_to_die (type, ptr_die);
18080   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18081   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18082 }
18083
18084 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18085    Use modified_type_die instead.
18086    We keep this code here just in case these types of DIEs may be needed to
18087    represent certain things in other languages (e.g. Pascal) someday.  */
18088
18089 static void
18090 gen_reference_type_die (tree type, dw_die_ref context_die)
18091 {
18092   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18093
18094   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18095     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18096   else
18097     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18098
18099   equate_type_number_to_die (type, ref_die);
18100   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18101   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18102 }
18103 #endif
18104
18105 /* Generate a DIE for a pointer to a member type.  */
18106
18107 static void
18108 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18109 {
18110   dw_die_ref ptr_die
18111     = new_die (DW_TAG_ptr_to_member_type,
18112                scope_die_for (type, context_die), type);
18113
18114   equate_type_number_to_die (type, ptr_die);
18115   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18116                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18117   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18118 }
18119
18120 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18121 DEF_VEC_P(dchar_p);
18122 DEF_VEC_ALLOC_P(dchar_p,heap);
18123
18124 static char *producer_string;
18125
18126 /* Return a heap allocated producer string including command line options
18127    if -grecord-gcc-switches.  */
18128
18129 static char *
18130 gen_producer_string (void)
18131 {
18132   size_t j;
18133   VEC(dchar_p, heap) *switches = NULL;
18134   const char *language_string = lang_hooks.name;
18135   char *producer, *tail;
18136   const char *p;
18137   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18138   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18139
18140   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18141     switch (save_decoded_options[j].opt_index)
18142       {
18143       case OPT_o:
18144       case OPT_d:
18145       case OPT_dumpbase:
18146       case OPT_dumpdir:
18147       case OPT_auxbase:
18148       case OPT_auxbase_strip:
18149       case OPT_quiet:
18150       case OPT_version:
18151       case OPT_v:
18152       case OPT_w:
18153       case OPT_L:
18154       case OPT_D:
18155       case OPT_I:
18156       case OPT_U:
18157       case OPT_SPECIAL_unknown:
18158       case OPT_SPECIAL_ignore:
18159       case OPT_SPECIAL_program_name:
18160       case OPT_SPECIAL_input_file:
18161       case OPT_grecord_gcc_switches:
18162       case OPT_gno_record_gcc_switches:
18163       case OPT__output_pch_:
18164       case OPT_fdiagnostics_show_location_:
18165       case OPT_fdiagnostics_show_option:
18166       case OPT____:
18167       case OPT__sysroot_:
18168       case OPT_nostdinc:
18169       case OPT_nostdinc__:
18170         /* Ignore these.  */
18171         continue;
18172       default:
18173         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18174                              == '-');
18175         switch (save_decoded_options[j].canonical_option[0][1])
18176           {
18177           case 'M':
18178           case 'i':
18179           case 'W':
18180             continue;
18181           case 'f':
18182             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18183                          "dump", 4) == 0)
18184               continue;
18185             break;
18186           default:
18187             break;
18188           }
18189         VEC_safe_push (dchar_p, heap, switches,
18190                        save_decoded_options[j].orig_option_with_args_text);
18191         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18192         break;
18193       }
18194
18195   producer = XNEWVEC (char, plen + 1 + len + 1);
18196   tail = producer;
18197   sprintf (tail, "%s %s", language_string, version_string);
18198   tail += plen;
18199
18200   if (!dwarf_record_gcc_switches)
18201     {
18202 #ifdef MIPS_DEBUGGING_INFO
18203       /* The MIPS/SGI compilers place the 'cc' command line options in the
18204          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18205          if they do not appear in the producer string, the debugger reaches
18206          the conclusion that the object file is stripped and has no debugging
18207          information.  To get the MIPS/SGI debugger to believe that there is
18208          debugging information in the object file, we add a -g to the producer
18209          string.  */
18210       if (debug_info_level > DINFO_LEVEL_TERSE)
18211         {
18212           memcpy (tail, " -g", 3);
18213           tail += 3;
18214         }
18215 #endif
18216     }
18217
18218   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18219     {
18220       len = strlen (p);
18221       *tail = ' ';
18222       memcpy (tail + 1, p, len);
18223       tail += len + 1;
18224     }
18225
18226   *tail = '\0';
18227   VEC_free (dchar_p, heap, switches);
18228   return producer;
18229 }
18230
18231 /* Generate the DIE for the compilation unit.  */
18232
18233 static dw_die_ref
18234 gen_compile_unit_die (const char *filename)
18235 {
18236   dw_die_ref die;
18237   const char *language_string = lang_hooks.name;
18238   int language;
18239
18240   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18241
18242   if (filename)
18243     {
18244       add_name_attribute (die, filename);
18245       /* Don't add cwd for <built-in>.  */
18246       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18247         add_comp_dir_attribute (die);
18248     }
18249
18250   if (producer_string == NULL)
18251     producer_string = gen_producer_string ();
18252   add_AT_string (die, DW_AT_producer, producer_string);
18253
18254   /* If our producer is LTO try to figure out a common language to use
18255      from the global list of translation units.  */
18256   if (strcmp (language_string, "GNU GIMPLE") == 0)
18257     {
18258       unsigned i;
18259       tree t;
18260       const char *common_lang = NULL;
18261
18262       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18263         {
18264           if (!TRANSLATION_UNIT_LANGUAGE (t))
18265             continue;
18266           if (!common_lang)
18267             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18268           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18269             ;
18270           else if (strncmp (common_lang, "GNU C", 5) == 0
18271                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18272             /* Mixing C and C++ is ok, use C++ in that case.  */
18273             common_lang = "GNU C++";
18274           else
18275             {
18276               /* Fall back to C.  */
18277               common_lang = NULL;
18278               break;
18279             }
18280         }
18281
18282       if (common_lang)
18283         language_string = common_lang;
18284     }
18285
18286   language = DW_LANG_C89;
18287   if (strcmp (language_string, "GNU C++") == 0)
18288     language = DW_LANG_C_plus_plus;
18289   else if (strcmp (language_string, "GNU F77") == 0)
18290     language = DW_LANG_Fortran77;
18291   else if (strcmp (language_string, "GNU Pascal") == 0)
18292     language = DW_LANG_Pascal83;
18293   else if (dwarf_version >= 3 || !dwarf_strict)
18294     {
18295       if (strcmp (language_string, "GNU Ada") == 0)
18296         language = DW_LANG_Ada95;
18297       else if (strcmp (language_string, "GNU Fortran") == 0)
18298         language = DW_LANG_Fortran95;
18299       else if (strcmp (language_string, "GNU Java") == 0)
18300         language = DW_LANG_Java;
18301       else if (strcmp (language_string, "GNU Objective-C") == 0)
18302         language = DW_LANG_ObjC;
18303       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18304         language = DW_LANG_ObjC_plus_plus;
18305     }
18306
18307   add_AT_unsigned (die, DW_AT_language, language);
18308
18309   switch (language)
18310     {
18311     case DW_LANG_Fortran77:
18312     case DW_LANG_Fortran90:
18313     case DW_LANG_Fortran95:
18314       /* Fortran has case insensitive identifiers and the front-end
18315          lowercases everything.  */
18316       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18317       break;
18318     default:
18319       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18320       break;
18321     }
18322   return die;
18323 }
18324
18325 /* Generate the DIE for a base class.  */
18326
18327 static void
18328 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18329 {
18330   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18331
18332   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18333   add_data_member_location_attribute (die, binfo);
18334
18335   if (BINFO_VIRTUAL_P (binfo))
18336     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18337
18338   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18339      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18340      the default has always been DW_ACCESS_private.  */
18341   if (access == access_public_node)
18342     {
18343       if (dwarf_version == 2
18344           || context_die->die_tag == DW_TAG_class_type)
18345       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18346     }
18347   else if (access == access_protected_node)
18348     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18349   else if (dwarf_version > 2
18350            && context_die->die_tag != DW_TAG_class_type)
18351     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18352 }
18353
18354 /* Generate a DIE for a class member.  */
18355
18356 static void
18357 gen_member_die (tree type, dw_die_ref context_die)
18358 {
18359   tree member;
18360   tree binfo = TYPE_BINFO (type);
18361   dw_die_ref child;
18362
18363   /* If this is not an incomplete type, output descriptions of each of its
18364      members. Note that as we output the DIEs necessary to represent the
18365      members of this record or union type, we will also be trying to output
18366      DIEs to represent the *types* of those members. However the `type'
18367      function (above) will specifically avoid generating type DIEs for member
18368      types *within* the list of member DIEs for this (containing) type except
18369      for those types (of members) which are explicitly marked as also being
18370      members of this (containing) type themselves.  The g++ front- end can
18371      force any given type to be treated as a member of some other (containing)
18372      type by setting the TYPE_CONTEXT of the given (member) type to point to
18373      the TREE node representing the appropriate (containing) type.  */
18374
18375   /* First output info about the base classes.  */
18376   if (binfo)
18377     {
18378       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18379       int i;
18380       tree base;
18381
18382       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18383         gen_inheritance_die (base,
18384                              (accesses ? VEC_index (tree, accesses, i)
18385                               : access_public_node), context_die);
18386     }
18387
18388   /* Now output info about the data members and type members.  */
18389   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18390     {
18391       /* If we thought we were generating minimal debug info for TYPE
18392          and then changed our minds, some of the member declarations
18393          may have already been defined.  Don't define them again, but
18394          do put them in the right order.  */
18395
18396       child = lookup_decl_die (member);
18397       if (child)
18398         splice_child_die (context_die, child);
18399       else
18400         gen_decl_die (member, NULL, context_die);
18401     }
18402
18403   /* Now output info about the function members (if any).  */
18404   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18405     {
18406       /* Don't include clones in the member list.  */
18407       if (DECL_ABSTRACT_ORIGIN (member))
18408         continue;
18409
18410       child = lookup_decl_die (member);
18411       if (child)
18412         splice_child_die (context_die, child);
18413       else
18414         gen_decl_die (member, NULL, context_die);
18415     }
18416 }
18417
18418 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18419    is set, we pretend that the type was never defined, so we only get the
18420    member DIEs needed by later specification DIEs.  */
18421
18422 static void
18423 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18424                                 enum debug_info_usage usage)
18425 {
18426   dw_die_ref type_die = lookup_type_die (type);
18427   dw_die_ref scope_die = 0;
18428   int nested = 0;
18429   int complete = (TYPE_SIZE (type)
18430                   && (! TYPE_STUB_DECL (type)
18431                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18432   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18433   complete = complete && should_emit_struct_debug (type, usage);
18434
18435   if (type_die && ! complete)
18436     return;
18437
18438   if (TYPE_CONTEXT (type) != NULL_TREE
18439       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18440           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18441     nested = 1;
18442
18443   scope_die = scope_die_for (type, context_die);
18444
18445   if (! type_die || (nested && is_cu_die (scope_die)))
18446     /* First occurrence of type or toplevel definition of nested class.  */
18447     {
18448       dw_die_ref old_die = type_die;
18449
18450       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18451                           ? record_type_tag (type) : DW_TAG_union_type,
18452                           scope_die, type);
18453       equate_type_number_to_die (type, type_die);
18454       if (old_die)
18455         add_AT_specification (type_die, old_die);
18456       else
18457         {
18458           add_name_attribute (type_die, type_tag (type));
18459           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18460           if (TYPE_ARTIFICIAL (type))
18461             add_AT_flag (type_die, DW_AT_artificial, 1);
18462         }
18463     }
18464   else
18465     remove_AT (type_die, DW_AT_declaration);
18466
18467   /* Generate child dies for template paramaters.  */
18468   if (debug_info_level > DINFO_LEVEL_TERSE
18469       && COMPLETE_TYPE_P (type))
18470     schedule_generic_params_dies_gen (type);
18471
18472   /* If this type has been completed, then give it a byte_size attribute and
18473      then give a list of members.  */
18474   if (complete && !ns_decl)
18475     {
18476       /* Prevent infinite recursion in cases where the type of some member of
18477          this type is expressed in terms of this type itself.  */
18478       TREE_ASM_WRITTEN (type) = 1;
18479       add_byte_size_attribute (type_die, type);
18480       if (TYPE_STUB_DECL (type) != NULL_TREE)
18481         {
18482           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18483           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18484         }
18485
18486       /* If the first reference to this type was as the return type of an
18487          inline function, then it may not have a parent.  Fix this now.  */
18488       if (type_die->die_parent == NULL)
18489         add_child_die (scope_die, type_die);
18490
18491       push_decl_scope (type);
18492       gen_member_die (type, type_die);
18493       pop_decl_scope ();
18494
18495       /* GNU extension: Record what type our vtable lives in.  */
18496       if (TYPE_VFIELD (type))
18497         {
18498           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18499
18500           gen_type_die (vtype, context_die);
18501           add_AT_die_ref (type_die, DW_AT_containing_type,
18502                           lookup_type_die (vtype));
18503         }
18504     }
18505   else
18506     {
18507       add_AT_flag (type_die, DW_AT_declaration, 1);
18508
18509       /* We don't need to do this for function-local types.  */
18510       if (TYPE_STUB_DECL (type)
18511           && ! decl_function_context (TYPE_STUB_DECL (type)))
18512         VEC_safe_push (tree, gc, incomplete_types, type);
18513     }
18514
18515   if (get_AT (type_die, DW_AT_name))
18516     add_pubtype (type, type_die);
18517 }
18518
18519 /* Generate a DIE for a subroutine _type_.  */
18520
18521 static void
18522 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18523 {
18524   tree return_type = TREE_TYPE (type);
18525   dw_die_ref subr_die
18526     = new_die (DW_TAG_subroutine_type,
18527                scope_die_for (type, context_die), type);
18528
18529   equate_type_number_to_die (type, subr_die);
18530   add_prototyped_attribute (subr_die, type);
18531   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18532   gen_formal_types_die (type, subr_die);
18533
18534   if (get_AT (subr_die, DW_AT_name))
18535     add_pubtype (type, subr_die);
18536 }
18537
18538 /* Generate a DIE for a type definition.  */
18539
18540 static void
18541 gen_typedef_die (tree decl, dw_die_ref context_die)
18542 {
18543   dw_die_ref type_die;
18544   tree origin;
18545
18546   if (TREE_ASM_WRITTEN (decl))
18547     return;
18548
18549   TREE_ASM_WRITTEN (decl) = 1;
18550   type_die = new_die (DW_TAG_typedef, context_die, decl);
18551   origin = decl_ultimate_origin (decl);
18552   if (origin != NULL)
18553     add_abstract_origin_attribute (type_die, origin);
18554   else
18555     {
18556       tree type;
18557
18558       add_name_and_src_coords_attributes (type_die, decl);
18559       if (DECL_ORIGINAL_TYPE (decl))
18560         {
18561           type = DECL_ORIGINAL_TYPE (decl);
18562
18563           gcc_assert (type != TREE_TYPE (decl));
18564           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18565         }
18566       else
18567         {
18568           type = TREE_TYPE (decl);
18569
18570           if (is_naming_typedef_decl (TYPE_NAME (type)))
18571             {
18572               /* Here, we are in the case of decl being a typedef naming
18573                  an anonymous type, e.g:
18574                      typedef struct {...} foo;
18575                  In that case TREE_TYPE (decl) is not a typedef variant
18576                  type and TYPE_NAME of the anonymous type is set to the
18577                  TYPE_DECL of the typedef. This construct is emitted by
18578                  the C++ FE.
18579
18580                  TYPE is the anonymous struct named by the typedef
18581                  DECL. As we need the DW_AT_type attribute of the
18582                  DW_TAG_typedef to point to the DIE of TYPE, let's
18583                  generate that DIE right away. add_type_attribute
18584                  called below will then pick (via lookup_type_die) that
18585                  anonymous struct DIE.  */
18586               if (!TREE_ASM_WRITTEN (type))
18587                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18588
18589               /* This is a GNU Extension.  We are adding a
18590                  DW_AT_linkage_name attribute to the DIE of the
18591                  anonymous struct TYPE.  The value of that attribute
18592                  is the name of the typedef decl naming the anonymous
18593                  struct.  This greatly eases the work of consumers of
18594                  this debug info.  */
18595               add_linkage_attr (lookup_type_die (type), decl);
18596             }
18597         }
18598
18599       add_type_attribute (type_die, type, TREE_READONLY (decl),
18600                           TREE_THIS_VOLATILE (decl), context_die);
18601
18602       if (is_naming_typedef_decl (decl))
18603         /* We want that all subsequent calls to lookup_type_die with
18604            TYPE in argument yield the DW_TAG_typedef we have just
18605            created.  */
18606         equate_type_number_to_die (type, type_die);
18607
18608       add_accessibility_attribute (type_die, decl);
18609     }
18610
18611   if (DECL_ABSTRACT (decl))
18612     equate_decl_number_to_die (decl, type_die);
18613
18614   if (get_AT (type_die, DW_AT_name))
18615     add_pubtype (decl, type_die);
18616 }
18617
18618 /* Generate a DIE for a struct, class, enum or union type.  */
18619
18620 static void
18621 gen_tagged_type_die (tree type,
18622                      dw_die_ref context_die,
18623                      enum debug_info_usage usage)
18624 {
18625   int need_pop;
18626
18627   if (type == NULL_TREE
18628       || !is_tagged_type (type))
18629     return;
18630
18631   /* If this is a nested type whose containing class hasn't been written
18632      out yet, writing it out will cover this one, too.  This does not apply
18633      to instantiations of member class templates; they need to be added to
18634      the containing class as they are generated.  FIXME: This hurts the
18635      idea of combining type decls from multiple TUs, since we can't predict
18636      what set of template instantiations we'll get.  */
18637   if (TYPE_CONTEXT (type)
18638       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18639       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18640     {
18641       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18642
18643       if (TREE_ASM_WRITTEN (type))
18644         return;
18645
18646       /* If that failed, attach ourselves to the stub.  */
18647       push_decl_scope (TYPE_CONTEXT (type));
18648       context_die = lookup_type_die (TYPE_CONTEXT (type));
18649       need_pop = 1;
18650     }
18651   else if (TYPE_CONTEXT (type) != NULL_TREE
18652            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18653     {
18654       /* If this type is local to a function that hasn't been written
18655          out yet, use a NULL context for now; it will be fixed up in
18656          decls_for_scope.  */
18657       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18658       /* A declaration DIE doesn't count; nested types need to go in the
18659          specification.  */
18660       if (context_die && is_declaration_die (context_die))
18661         context_die = NULL;
18662       need_pop = 0;
18663     }
18664   else
18665     {
18666       context_die = declare_in_namespace (type, context_die);
18667       need_pop = 0;
18668     }
18669
18670   if (TREE_CODE (type) == ENUMERAL_TYPE)
18671     {
18672       /* This might have been written out by the call to
18673          declare_in_namespace.  */
18674       if (!TREE_ASM_WRITTEN (type))
18675         gen_enumeration_type_die (type, context_die);
18676     }
18677   else
18678     gen_struct_or_union_type_die (type, context_die, usage);
18679
18680   if (need_pop)
18681     pop_decl_scope ();
18682
18683   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18684      it up if it is ever completed.  gen_*_type_die will set it for us
18685      when appropriate.  */
18686 }
18687
18688 /* Generate a type description DIE.  */
18689
18690 static void
18691 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18692                          enum debug_info_usage usage)
18693 {
18694   struct array_descr_info info;
18695
18696   if (type == NULL_TREE || type == error_mark_node)
18697     return;
18698
18699   if (TYPE_NAME (type) != NULL_TREE
18700       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18701       && is_redundant_typedef (TYPE_NAME (type))
18702       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18703     /* The DECL of this type is a typedef we don't want to emit debug
18704        info for but we want debug info for its underlying typedef.
18705        This can happen for e.g, the injected-class-name of a C++
18706        type.  */
18707     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18708
18709   /* If TYPE is a typedef type variant, let's generate debug info
18710      for the parent typedef which TYPE is a type of.  */
18711   if (typedef_variant_p (type))
18712     {
18713       if (TREE_ASM_WRITTEN (type))
18714         return;
18715
18716       /* Prevent broken recursion; we can't hand off to the same type.  */
18717       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18718
18719       /* Use the DIE of the containing namespace as the parent DIE of
18720          the type description DIE we want to generate.  */
18721       if (DECL_CONTEXT (TYPE_NAME (type))
18722           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18723         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18724
18725       TREE_ASM_WRITTEN (type) = 1;
18726
18727       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18728       return;
18729     }
18730
18731   /* If type is an anonymous tagged type named by a typedef, let's
18732      generate debug info for the typedef.  */
18733   if (is_naming_typedef_decl (TYPE_NAME (type)))
18734     {
18735       /* Use the DIE of the containing namespace as the parent DIE of
18736          the type description DIE we want to generate.  */
18737       if (DECL_CONTEXT (TYPE_NAME (type))
18738           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18739         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18740       
18741       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18742       return;
18743     }
18744
18745   /* If this is an array type with hidden descriptor, handle it first.  */
18746   if (!TREE_ASM_WRITTEN (type)
18747       && lang_hooks.types.get_array_descr_info
18748       && lang_hooks.types.get_array_descr_info (type, &info)
18749       && (dwarf_version >= 3 || !dwarf_strict))
18750     {
18751       gen_descr_array_type_die (type, &info, context_die);
18752       TREE_ASM_WRITTEN (type) = 1;
18753       return;
18754     }
18755
18756   /* We are going to output a DIE to represent the unqualified version
18757      of this type (i.e. without any const or volatile qualifiers) so
18758      get the main variant (i.e. the unqualified version) of this type
18759      now.  (Vectors are special because the debugging info is in the
18760      cloned type itself).  */
18761   if (TREE_CODE (type) != VECTOR_TYPE)
18762     type = type_main_variant (type);
18763
18764   if (TREE_ASM_WRITTEN (type))
18765     return;
18766
18767   switch (TREE_CODE (type))
18768     {
18769     case ERROR_MARK:
18770       break;
18771
18772     case POINTER_TYPE:
18773     case REFERENCE_TYPE:
18774       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18775          ensures that the gen_type_die recursion will terminate even if the
18776          type is recursive.  Recursive types are possible in Ada.  */
18777       /* ??? We could perhaps do this for all types before the switch
18778          statement.  */
18779       TREE_ASM_WRITTEN (type) = 1;
18780
18781       /* For these types, all that is required is that we output a DIE (or a
18782          set of DIEs) to represent the "basis" type.  */
18783       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18784                                 DINFO_USAGE_IND_USE);
18785       break;
18786
18787     case OFFSET_TYPE:
18788       /* This code is used for C++ pointer-to-data-member types.
18789          Output a description of the relevant class type.  */
18790       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18791                                         DINFO_USAGE_IND_USE);
18792
18793       /* Output a description of the type of the object pointed to.  */
18794       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18795                                         DINFO_USAGE_IND_USE);
18796
18797       /* Now output a DIE to represent this pointer-to-data-member type
18798          itself.  */
18799       gen_ptr_to_mbr_type_die (type, context_die);
18800       break;
18801
18802     case FUNCTION_TYPE:
18803       /* Force out return type (in case it wasn't forced out already).  */
18804       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18805                                         DINFO_USAGE_DIR_USE);
18806       gen_subroutine_type_die (type, context_die);
18807       break;
18808
18809     case METHOD_TYPE:
18810       /* Force out return type (in case it wasn't forced out already).  */
18811       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18812                                         DINFO_USAGE_DIR_USE);
18813       gen_subroutine_type_die (type, context_die);
18814       break;
18815
18816     case ARRAY_TYPE:
18817       gen_array_type_die (type, context_die);
18818       break;
18819
18820     case VECTOR_TYPE:
18821       gen_array_type_die (type, context_die);
18822       break;
18823
18824     case ENUMERAL_TYPE:
18825     case RECORD_TYPE:
18826     case UNION_TYPE:
18827     case QUAL_UNION_TYPE:
18828       gen_tagged_type_die (type, context_die, usage);
18829       return;
18830
18831     case VOID_TYPE:
18832     case INTEGER_TYPE:
18833     case REAL_TYPE:
18834     case FIXED_POINT_TYPE:
18835     case COMPLEX_TYPE:
18836     case BOOLEAN_TYPE:
18837       /* No DIEs needed for fundamental types.  */
18838       break;
18839
18840     case NULLPTR_TYPE:
18841     case LANG_TYPE:
18842       /* Just use DW_TAG_unspecified_type.  */
18843       {
18844         dw_die_ref type_die = lookup_type_die (type);
18845         if (type_die == NULL)
18846           {
18847             tree name = TYPE_NAME (type);
18848             if (TREE_CODE (name) == TYPE_DECL)
18849               name = DECL_NAME (name);
18850             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18851             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18852             equate_type_number_to_die (type, type_die);
18853           }
18854       }
18855       break;
18856
18857     default:
18858       gcc_unreachable ();
18859     }
18860
18861   TREE_ASM_WRITTEN (type) = 1;
18862 }
18863
18864 static void
18865 gen_type_die (tree type, dw_die_ref context_die)
18866 {
18867   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18868 }
18869
18870 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18871    things which are local to the given block.  */
18872
18873 static void
18874 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18875 {
18876   int must_output_die = 0;
18877   bool inlined_func;
18878
18879   /* Ignore blocks that are NULL.  */
18880   if (stmt == NULL_TREE)
18881     return;
18882
18883   inlined_func = inlined_function_outer_scope_p (stmt);
18884
18885   /* If the block is one fragment of a non-contiguous block, do not
18886      process the variables, since they will have been done by the
18887      origin block.  Do process subblocks.  */
18888   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18889     {
18890       tree sub;
18891
18892       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18893         gen_block_die (sub, context_die, depth + 1);
18894
18895       return;
18896     }
18897
18898   /* Determine if we need to output any Dwarf DIEs at all to represent this
18899      block.  */
18900   if (inlined_func)
18901     /* The outer scopes for inlinings *must* always be represented.  We
18902        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18903     must_output_die = 1;
18904   else
18905     {
18906       /* Determine if this block directly contains any "significant"
18907          local declarations which we will need to output DIEs for.  */
18908       if (debug_info_level > DINFO_LEVEL_TERSE)
18909         /* We are not in terse mode so *any* local declaration counts
18910            as being a "significant" one.  */
18911         must_output_die = ((BLOCK_VARS (stmt) != NULL
18912                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18913                            && (TREE_USED (stmt)
18914                                || TREE_ASM_WRITTEN (stmt)
18915                                || BLOCK_ABSTRACT (stmt)));
18916       else if ((TREE_USED (stmt)
18917                 || TREE_ASM_WRITTEN (stmt)
18918                 || BLOCK_ABSTRACT (stmt))
18919                && !dwarf2out_ignore_block (stmt))
18920         must_output_die = 1;
18921     }
18922
18923   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18924      DIE for any block which contains no significant local declarations at
18925      all.  Rather, in such cases we just call `decls_for_scope' so that any
18926      needed Dwarf info for any sub-blocks will get properly generated. Note
18927      that in terse mode, our definition of what constitutes a "significant"
18928      local declaration gets restricted to include only inlined function
18929      instances and local (nested) function definitions.  */
18930   if (must_output_die)
18931     {
18932       if (inlined_func)
18933         {
18934           /* If STMT block is abstract, that means we have been called
18935              indirectly from dwarf2out_abstract_function.
18936              That function rightfully marks the descendent blocks (of
18937              the abstract function it is dealing with) as being abstract,
18938              precisely to prevent us from emitting any
18939              DW_TAG_inlined_subroutine DIE as a descendent
18940              of an abstract function instance. So in that case, we should
18941              not call gen_inlined_subroutine_die.
18942
18943              Later though, when cgraph asks dwarf2out to emit info
18944              for the concrete instance of the function decl into which
18945              the concrete instance of STMT got inlined, the later will lead
18946              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18947           if (! BLOCK_ABSTRACT (stmt))
18948             gen_inlined_subroutine_die (stmt, context_die, depth);
18949         }
18950       else
18951         gen_lexical_block_die (stmt, context_die, depth);
18952     }
18953   else
18954     decls_for_scope (stmt, context_die, depth);
18955 }
18956
18957 /* Process variable DECL (or variable with origin ORIGIN) within
18958    block STMT and add it to CONTEXT_DIE.  */
18959 static void
18960 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18961 {
18962   dw_die_ref die;
18963   tree decl_or_origin = decl ? decl : origin;
18964
18965   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18966     die = lookup_decl_die (decl_or_origin);
18967   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18968            && TYPE_DECL_IS_STUB (decl_or_origin))
18969     die = lookup_type_die (TREE_TYPE (decl_or_origin));
18970   else
18971     die = NULL;
18972
18973   if (die != NULL && die->die_parent == NULL)
18974     add_child_die (context_die, die);
18975   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18976     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18977                                          stmt, context_die);
18978   else
18979     gen_decl_die (decl, origin, context_die);
18980 }
18981
18982 /* Generate all of the decls declared within a given scope and (recursively)
18983    all of its sub-blocks.  */
18984
18985 static void
18986 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18987 {
18988   tree decl;
18989   unsigned int i;
18990   tree subblocks;
18991
18992   /* Ignore NULL blocks.  */
18993   if (stmt == NULL_TREE)
18994     return;
18995
18996   /* Output the DIEs to represent all of the data objects and typedefs
18997      declared directly within this block but not within any nested
18998      sub-blocks.  Also, nested function and tag DIEs have been
18999      generated with a parent of NULL; fix that up now.  */
19000   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19001     process_scope_var (stmt, decl, NULL_TREE, context_die);
19002   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19003     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19004                        context_die);
19005
19006   /* If we're at -g1, we're not interested in subblocks.  */
19007   if (debug_info_level <= DINFO_LEVEL_TERSE)
19008     return;
19009
19010   /* Output the DIEs to represent all sub-blocks (and the items declared
19011      therein) of this block.  */
19012   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19013        subblocks != NULL;
19014        subblocks = BLOCK_CHAIN (subblocks))
19015     gen_block_die (subblocks, context_die, depth + 1);
19016 }
19017
19018 /* Is this a typedef we can avoid emitting?  */
19019
19020 static inline int
19021 is_redundant_typedef (const_tree decl)
19022 {
19023   if (TYPE_DECL_IS_STUB (decl))
19024     return 1;
19025
19026   if (DECL_ARTIFICIAL (decl)
19027       && DECL_CONTEXT (decl)
19028       && is_tagged_type (DECL_CONTEXT (decl))
19029       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19030       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19031     /* Also ignore the artificial member typedef for the class name.  */
19032     return 1;
19033
19034   return 0;
19035 }
19036
19037 /* Return TRUE if TYPE is a typedef that names a type for linkage
19038    purposes. This kind of typedefs is produced by the C++ FE for
19039    constructs like:
19040
19041    typedef struct {...} foo;
19042
19043    In that case, there is no typedef variant type produced for foo.
19044    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19045    struct type.  */
19046
19047 static bool
19048 is_naming_typedef_decl (const_tree decl)
19049 {
19050   if (decl == NULL_TREE
19051       || TREE_CODE (decl) != TYPE_DECL
19052       || !is_tagged_type (TREE_TYPE (decl))
19053       || DECL_IS_BUILTIN (decl)
19054       || is_redundant_typedef (decl)
19055       /* It looks like Ada produces TYPE_DECLs that are very similar
19056          to C++ naming typedefs but that have different
19057          semantics. Let's be specific to c++ for now.  */
19058       || !is_cxx ())
19059     return FALSE;
19060
19061   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19062           && TYPE_NAME (TREE_TYPE (decl)) == decl
19063           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19064               != TYPE_NAME (TREE_TYPE (decl))));
19065 }
19066
19067 /* Returns the DIE for a context.  */
19068
19069 static inline dw_die_ref
19070 get_context_die (tree context)
19071 {
19072   if (context)
19073     {
19074       /* Find die that represents this context.  */
19075       if (TYPE_P (context))
19076         {
19077           context = TYPE_MAIN_VARIANT (context);
19078           return strip_naming_typedef (context, force_type_die (context));
19079         }
19080       else
19081         return force_decl_die (context);
19082     }
19083   return comp_unit_die ();
19084 }
19085
19086 /* Returns the DIE for decl.  A DIE will always be returned.  */
19087
19088 static dw_die_ref
19089 force_decl_die (tree decl)
19090 {
19091   dw_die_ref decl_die;
19092   unsigned saved_external_flag;
19093   tree save_fn = NULL_TREE;
19094   decl_die = lookup_decl_die (decl);
19095   if (!decl_die)
19096     {
19097       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19098
19099       decl_die = lookup_decl_die (decl);
19100       if (decl_die)
19101         return decl_die;
19102
19103       switch (TREE_CODE (decl))
19104         {
19105         case FUNCTION_DECL:
19106           /* Clear current_function_decl, so that gen_subprogram_die thinks
19107              that this is a declaration. At this point, we just want to force
19108              declaration die.  */
19109           save_fn = current_function_decl;
19110           current_function_decl = NULL_TREE;
19111           gen_subprogram_die (decl, context_die);
19112           current_function_decl = save_fn;
19113           break;
19114
19115         case VAR_DECL:
19116           /* Set external flag to force declaration die. Restore it after
19117            gen_decl_die() call.  */
19118           saved_external_flag = DECL_EXTERNAL (decl);
19119           DECL_EXTERNAL (decl) = 1;
19120           gen_decl_die (decl, NULL, context_die);
19121           DECL_EXTERNAL (decl) = saved_external_flag;
19122           break;
19123
19124         case NAMESPACE_DECL:
19125           if (dwarf_version >= 3 || !dwarf_strict)
19126             dwarf2out_decl (decl);
19127           else
19128             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19129             decl_die = comp_unit_die ();
19130           break;
19131
19132         case TRANSLATION_UNIT_DECL:
19133           decl_die = comp_unit_die ();
19134           break;
19135
19136         default:
19137           gcc_unreachable ();
19138         }
19139
19140       /* We should be able to find the DIE now.  */
19141       if (!decl_die)
19142         decl_die = lookup_decl_die (decl);
19143       gcc_assert (decl_die);
19144     }
19145
19146   return decl_die;
19147 }
19148
19149 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19150    always returned.  */
19151
19152 static dw_die_ref
19153 force_type_die (tree type)
19154 {
19155   dw_die_ref type_die;
19156
19157   type_die = lookup_type_die (type);
19158   if (!type_die)
19159     {
19160       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19161
19162       type_die = modified_type_die (type, TYPE_READONLY (type),
19163                                     TYPE_VOLATILE (type), context_die);
19164       gcc_assert (type_die);
19165     }
19166   return type_die;
19167 }
19168
19169 /* Force out any required namespaces to be able to output DECL,
19170    and return the new context_die for it, if it's changed.  */
19171
19172 static dw_die_ref
19173 setup_namespace_context (tree thing, dw_die_ref context_die)
19174 {
19175   tree context = (DECL_P (thing)
19176                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19177   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19178     /* Force out the namespace.  */
19179     context_die = force_decl_die (context);
19180
19181   return context_die;
19182 }
19183
19184 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19185    type) within its namespace, if appropriate.
19186
19187    For compatibility with older debuggers, namespace DIEs only contain
19188    declarations; all definitions are emitted at CU scope.  */
19189
19190 static dw_die_ref
19191 declare_in_namespace (tree thing, dw_die_ref context_die)
19192 {
19193   dw_die_ref ns_context;
19194
19195   if (debug_info_level <= DINFO_LEVEL_TERSE)
19196     return context_die;
19197
19198   /* If this decl is from an inlined function, then don't try to emit it in its
19199      namespace, as we will get confused.  It would have already been emitted
19200      when the abstract instance of the inline function was emitted anyways.  */
19201   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19202     return context_die;
19203
19204   ns_context = setup_namespace_context (thing, context_die);
19205
19206   if (ns_context != context_die)
19207     {
19208       if (is_fortran ())
19209         return ns_context;
19210       if (DECL_P (thing))
19211         gen_decl_die (thing, NULL, ns_context);
19212       else
19213         gen_type_die (thing, ns_context);
19214     }
19215   return context_die;
19216 }
19217
19218 /* Generate a DIE for a namespace or namespace alias.  */
19219
19220 static void
19221 gen_namespace_die (tree decl, dw_die_ref context_die)
19222 {
19223   dw_die_ref namespace_die;
19224
19225   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19226      they are an alias of.  */
19227   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19228     {
19229       /* Output a real namespace or module.  */
19230       context_die = setup_namespace_context (decl, comp_unit_die ());
19231       namespace_die = new_die (is_fortran ()
19232                                ? DW_TAG_module : DW_TAG_namespace,
19233                                context_die, decl);
19234       /* For Fortran modules defined in different CU don't add src coords.  */
19235       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19236         {
19237           const char *name = dwarf2_name (decl, 0);
19238           if (name)
19239             add_name_attribute (namespace_die, name);
19240         }
19241       else
19242         add_name_and_src_coords_attributes (namespace_die, decl);
19243       if (DECL_EXTERNAL (decl))
19244         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19245       equate_decl_number_to_die (decl, namespace_die);
19246     }
19247   else
19248     {
19249       /* Output a namespace alias.  */
19250
19251       /* Force out the namespace we are an alias of, if necessary.  */
19252       dw_die_ref origin_die
19253         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19254
19255       if (DECL_FILE_SCOPE_P (decl)
19256           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19257         context_die = setup_namespace_context (decl, comp_unit_die ());
19258       /* Now create the namespace alias DIE.  */
19259       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19260       add_name_and_src_coords_attributes (namespace_die, decl);
19261       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19262       equate_decl_number_to_die (decl, namespace_die);
19263     }
19264 }
19265
19266 /* Generate Dwarf debug information for a decl described by DECL.
19267    The return value is currently only meaningful for PARM_DECLs,
19268    for all other decls it returns NULL.  */
19269
19270 static dw_die_ref
19271 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19272 {
19273   tree decl_or_origin = decl ? decl : origin;
19274   tree class_origin = NULL, ultimate_origin;
19275
19276   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19277     return NULL;
19278
19279   switch (TREE_CODE (decl_or_origin))
19280     {
19281     case ERROR_MARK:
19282       break;
19283
19284     case CONST_DECL:
19285       if (!is_fortran () && !is_ada ())
19286         {
19287           /* The individual enumerators of an enum type get output when we output
19288              the Dwarf representation of the relevant enum type itself.  */
19289           break;
19290         }
19291
19292       /* Emit its type.  */
19293       gen_type_die (TREE_TYPE (decl), context_die);
19294
19295       /* And its containing namespace.  */
19296       context_die = declare_in_namespace (decl, context_die);
19297
19298       gen_const_die (decl, context_die);
19299       break;
19300
19301     case FUNCTION_DECL:
19302       /* Don't output any DIEs to represent mere function declarations,
19303          unless they are class members or explicit block externs.  */
19304       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19305           && DECL_FILE_SCOPE_P (decl_or_origin)
19306           && (current_function_decl == NULL_TREE
19307               || DECL_ARTIFICIAL (decl_or_origin)))
19308         break;
19309
19310 #if 0
19311       /* FIXME */
19312       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19313          on local redeclarations of global functions.  That seems broken.  */
19314       if (current_function_decl != decl)
19315         /* This is only a declaration.  */;
19316 #endif
19317
19318       /* If we're emitting a clone, emit info for the abstract instance.  */
19319       if (origin || DECL_ORIGIN (decl) != decl)
19320         dwarf2out_abstract_function (origin
19321                                      ? DECL_ORIGIN (origin)
19322                                      : DECL_ABSTRACT_ORIGIN (decl));
19323
19324       /* If we're emitting an out-of-line copy of an inline function,
19325          emit info for the abstract instance and set up to refer to it.  */
19326       else if (cgraph_function_possibly_inlined_p (decl)
19327                && ! DECL_ABSTRACT (decl)
19328                && ! class_or_namespace_scope_p (context_die)
19329                /* dwarf2out_abstract_function won't emit a die if this is just
19330                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19331                   that case, because that works only if we have a die.  */
19332                && DECL_INITIAL (decl) != NULL_TREE)
19333         {
19334           dwarf2out_abstract_function (decl);
19335           set_decl_origin_self (decl);
19336         }
19337
19338       /* Otherwise we're emitting the primary DIE for this decl.  */
19339       else if (debug_info_level > DINFO_LEVEL_TERSE)
19340         {
19341           /* Before we describe the FUNCTION_DECL itself, make sure that we
19342              have its containing type.  */
19343           if (!origin)
19344             origin = decl_class_context (decl);
19345           if (origin != NULL_TREE)
19346             gen_type_die (origin, context_die);
19347
19348           /* And its return type.  */
19349           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19350
19351           /* And its virtual context.  */
19352           if (DECL_VINDEX (decl) != NULL_TREE)
19353             gen_type_die (DECL_CONTEXT (decl), context_die);
19354
19355           /* Make sure we have a member DIE for decl.  */
19356           if (origin != NULL_TREE)
19357             gen_type_die_for_member (origin, decl, context_die);
19358
19359           /* And its containing namespace.  */
19360           context_die = declare_in_namespace (decl, context_die);
19361         }
19362
19363       /* Now output a DIE to represent the function itself.  */
19364       if (decl)
19365         gen_subprogram_die (decl, context_die);
19366       break;
19367
19368     case TYPE_DECL:
19369       /* If we are in terse mode, don't generate any DIEs to represent any
19370          actual typedefs.  */
19371       if (debug_info_level <= DINFO_LEVEL_TERSE)
19372         break;
19373
19374       /* In the special case of a TYPE_DECL node representing the declaration
19375          of some type tag, if the given TYPE_DECL is marked as having been
19376          instantiated from some other (original) TYPE_DECL node (e.g. one which
19377          was generated within the original definition of an inline function) we
19378          used to generate a special (abbreviated) DW_TAG_structure_type,
19379          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19380          should be actually referencing those DIEs, as variable DIEs with that
19381          type would be emitted already in the abstract origin, so it was always
19382          removed during unused type prunning.  Don't add anything in this
19383          case.  */
19384       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19385         break;
19386
19387       if (is_redundant_typedef (decl))
19388         gen_type_die (TREE_TYPE (decl), context_die);
19389       else
19390         /* Output a DIE to represent the typedef itself.  */
19391         gen_typedef_die (decl, context_die);
19392       break;
19393
19394     case LABEL_DECL:
19395       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19396         gen_label_die (decl, context_die);
19397       break;
19398
19399     case VAR_DECL:
19400     case RESULT_DECL:
19401       /* If we are in terse mode, don't generate any DIEs to represent any
19402          variable declarations or definitions.  */
19403       if (debug_info_level <= DINFO_LEVEL_TERSE)
19404         break;
19405
19406       /* Output any DIEs that are needed to specify the type of this data
19407          object.  */
19408       if (decl_by_reference_p (decl_or_origin))
19409         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19410       else
19411         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19412
19413       /* And its containing type.  */
19414       class_origin = decl_class_context (decl_or_origin);
19415       if (class_origin != NULL_TREE)
19416         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19417
19418       /* And its containing namespace.  */
19419       context_die = declare_in_namespace (decl_or_origin, context_die);
19420
19421       /* Now output the DIE to represent the data object itself.  This gets
19422          complicated because of the possibility that the VAR_DECL really
19423          represents an inlined instance of a formal parameter for an inline
19424          function.  */
19425       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19426       if (ultimate_origin != NULL_TREE
19427           && TREE_CODE (ultimate_origin) == PARM_DECL)
19428         gen_formal_parameter_die (decl, origin,
19429                                   true /* Emit name attribute.  */,
19430                                   context_die);
19431       else
19432         gen_variable_die (decl, origin, context_die);
19433       break;
19434
19435     case FIELD_DECL:
19436       /* Ignore the nameless fields that are used to skip bits but handle C++
19437          anonymous unions and structs.  */
19438       if (DECL_NAME (decl) != NULL_TREE
19439           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19440           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19441         {
19442           gen_type_die (member_declared_type (decl), context_die);
19443           gen_field_die (decl, context_die);
19444         }
19445       break;
19446
19447     case PARM_DECL:
19448       if (DECL_BY_REFERENCE (decl_or_origin))
19449         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19450       else
19451         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19452       return gen_formal_parameter_die (decl, origin,
19453                                        true /* Emit name attribute.  */,
19454                                        context_die);
19455
19456     case NAMESPACE_DECL:
19457     case IMPORTED_DECL:
19458       if (dwarf_version >= 3 || !dwarf_strict)
19459         gen_namespace_die (decl, context_die);
19460       break;
19461
19462     default:
19463       /* Probably some frontend-internal decl.  Assume we don't care.  */
19464       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19465       break;
19466     }
19467
19468   return NULL;
19469 }
19470 \f
19471 /* Output debug information for global decl DECL.  Called from toplev.c after
19472    compilation proper has finished.  */
19473
19474 static void
19475 dwarf2out_global_decl (tree decl)
19476 {
19477   /* Output DWARF2 information for file-scope tentative data object
19478      declarations, file-scope (extern) function declarations (which
19479      had no corresponding body) and file-scope tagged type declarations
19480      and definitions which have not yet been forced out.  */
19481   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19482     dwarf2out_decl (decl);
19483 }
19484
19485 /* Output debug information for type decl DECL.  Called from toplev.c
19486    and from language front ends (to record built-in types).  */
19487 static void
19488 dwarf2out_type_decl (tree decl, int local)
19489 {
19490   if (!local)
19491     dwarf2out_decl (decl);
19492 }
19493
19494 /* Output debug information for imported module or decl DECL.
19495    NAME is non-NULL name in the lexical block if the decl has been renamed.
19496    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19497    that DECL belongs to.
19498    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19499 static void
19500 dwarf2out_imported_module_or_decl_1 (tree decl,
19501                                      tree name,
19502                                      tree lexical_block,
19503                                      dw_die_ref lexical_block_die)
19504 {
19505   expanded_location xloc;
19506   dw_die_ref imported_die = NULL;
19507   dw_die_ref at_import_die;
19508
19509   if (TREE_CODE (decl) == IMPORTED_DECL)
19510     {
19511       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19512       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19513       gcc_assert (decl);
19514     }
19515   else
19516     xloc = expand_location (input_location);
19517
19518   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19519     {
19520       at_import_die = force_type_die (TREE_TYPE (decl));
19521       /* For namespace N { typedef void T; } using N::T; base_type_die
19522          returns NULL, but DW_TAG_imported_declaration requires
19523          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19524       if (!at_import_die)
19525         {
19526           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19527           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19528           at_import_die = lookup_type_die (TREE_TYPE (decl));
19529           gcc_assert (at_import_die);
19530         }
19531     }
19532   else
19533     {
19534       at_import_die = lookup_decl_die (decl);
19535       if (!at_import_die)
19536         {
19537           /* If we're trying to avoid duplicate debug info, we may not have
19538              emitted the member decl for this field.  Emit it now.  */
19539           if (TREE_CODE (decl) == FIELD_DECL)
19540             {
19541               tree type = DECL_CONTEXT (decl);
19542
19543               if (TYPE_CONTEXT (type)
19544                   && TYPE_P (TYPE_CONTEXT (type))
19545                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19546                                                 DINFO_USAGE_DIR_USE))
19547                 return;
19548               gen_type_die_for_member (type, decl,
19549                                        get_context_die (TYPE_CONTEXT (type)));
19550             }
19551           at_import_die = force_decl_die (decl);
19552         }
19553     }
19554
19555   if (TREE_CODE (decl) == NAMESPACE_DECL)
19556     {
19557       if (dwarf_version >= 3 || !dwarf_strict)
19558         imported_die = new_die (DW_TAG_imported_module,
19559                                 lexical_block_die,
19560                                 lexical_block);
19561       else
19562         return;
19563     }
19564   else
19565     imported_die = new_die (DW_TAG_imported_declaration,
19566                             lexical_block_die,
19567                             lexical_block);
19568
19569   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19570   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19571   if (name)
19572     add_AT_string (imported_die, DW_AT_name,
19573                    IDENTIFIER_POINTER (name));
19574   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19575 }
19576
19577 /* Output debug information for imported module or decl DECL.
19578    NAME is non-NULL name in context if the decl has been renamed.
19579    CHILD is true if decl is one of the renamed decls as part of
19580    importing whole module.  */
19581
19582 static void
19583 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19584                                    bool child)
19585 {
19586   /* dw_die_ref at_import_die;  */
19587   dw_die_ref scope_die;
19588
19589   if (debug_info_level <= DINFO_LEVEL_TERSE)
19590     return;
19591
19592   gcc_assert (decl);
19593
19594   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19595      We need decl DIE for reference and scope die. First, get DIE for the decl
19596      itself.  */
19597
19598   /* Get the scope die for decl context. Use comp_unit_die for global module
19599      or decl. If die is not found for non globals, force new die.  */
19600   if (context
19601       && TYPE_P (context)
19602       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19603     return;
19604
19605   if (!(dwarf_version >= 3 || !dwarf_strict))
19606     return;
19607
19608   scope_die = get_context_die (context);
19609
19610   if (child)
19611     {
19612       gcc_assert (scope_die->die_child);
19613       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19614       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19615       scope_die = scope_die->die_child;
19616     }
19617
19618   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19619   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19620
19621 }
19622
19623 /* Write the debugging output for DECL.  */
19624
19625 void
19626 dwarf2out_decl (tree decl)
19627 {
19628   dw_die_ref context_die = comp_unit_die ();
19629
19630   switch (TREE_CODE (decl))
19631     {
19632     case ERROR_MARK:
19633       return;
19634
19635     case FUNCTION_DECL:
19636       /* What we would really like to do here is to filter out all mere
19637          file-scope declarations of file-scope functions which are never
19638          referenced later within this translation unit (and keep all of ones
19639          that *are* referenced later on) but we aren't clairvoyant, so we have
19640          no idea which functions will be referenced in the future (i.e. later
19641          on within the current translation unit). So here we just ignore all
19642          file-scope function declarations which are not also definitions.  If
19643          and when the debugger needs to know something about these functions,
19644          it will have to hunt around and find the DWARF information associated
19645          with the definition of the function.
19646
19647          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19648          nodes represent definitions and which ones represent mere
19649          declarations.  We have to check DECL_INITIAL instead. That's because
19650          the C front-end supports some weird semantics for "extern inline"
19651          function definitions.  These can get inlined within the current
19652          translation unit (and thus, we need to generate Dwarf info for their
19653          abstract instances so that the Dwarf info for the concrete inlined
19654          instances can have something to refer to) but the compiler never
19655          generates any out-of-lines instances of such things (despite the fact
19656          that they *are* definitions).
19657
19658          The important point is that the C front-end marks these "extern
19659          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19660          them anyway. Note that the C++ front-end also plays some similar games
19661          for inline function definitions appearing within include files which
19662          also contain `#pragma interface' pragmas.  */
19663       if (DECL_INITIAL (decl) == NULL_TREE)
19664         return;
19665
19666       /* If we're a nested function, initially use a parent of NULL; if we're
19667          a plain function, this will be fixed up in decls_for_scope.  If
19668          we're a method, it will be ignored, since we already have a DIE.  */
19669       if (decl_function_context (decl)
19670           /* But if we're in terse mode, we don't care about scope.  */
19671           && debug_info_level > DINFO_LEVEL_TERSE)
19672         context_die = NULL;
19673       break;
19674
19675     case VAR_DECL:
19676       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19677          declaration and if the declaration was never even referenced from
19678          within this entire compilation unit.  We suppress these DIEs in
19679          order to save space in the .debug section (by eliminating entries
19680          which are probably useless).  Note that we must not suppress
19681          block-local extern declarations (whether used or not) because that
19682          would screw-up the debugger's name lookup mechanism and cause it to
19683          miss things which really ought to be in scope at a given point.  */
19684       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19685         return;
19686
19687       /* For local statics lookup proper context die.  */
19688       if (TREE_STATIC (decl) && decl_function_context (decl))
19689         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19690
19691       /* If we are in terse mode, don't generate any DIEs to represent any
19692          variable declarations or definitions.  */
19693       if (debug_info_level <= DINFO_LEVEL_TERSE)
19694         return;
19695       break;
19696
19697     case CONST_DECL:
19698       if (debug_info_level <= DINFO_LEVEL_TERSE)
19699         return;
19700       if (!is_fortran () && !is_ada ())
19701         return;
19702       if (TREE_STATIC (decl) && decl_function_context (decl))
19703         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19704       break;
19705
19706     case NAMESPACE_DECL:
19707     case IMPORTED_DECL:
19708       if (debug_info_level <= DINFO_LEVEL_TERSE)
19709         return;
19710       if (lookup_decl_die (decl) != NULL)
19711         return;
19712       break;
19713
19714     case TYPE_DECL:
19715       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19716       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19717         return;
19718
19719       /* Don't bother trying to generate any DIEs to represent any of the
19720          normal built-in types for the language we are compiling.  */
19721       if (DECL_IS_BUILTIN (decl))
19722         return;
19723
19724       /* If we are in terse mode, don't generate any DIEs for types.  */
19725       if (debug_info_level <= DINFO_LEVEL_TERSE)
19726         return;
19727
19728       /* If we're a function-scope tag, initially use a parent of NULL;
19729          this will be fixed up in decls_for_scope.  */
19730       if (decl_function_context (decl))
19731         context_die = NULL;
19732
19733       break;
19734
19735     default:
19736       return;
19737     }
19738
19739   gen_decl_die (decl, NULL, context_die);
19740 }
19741
19742 /* Write the debugging output for DECL.  */
19743
19744 static void
19745 dwarf2out_function_decl (tree decl)
19746 {
19747   dwarf2out_decl (decl);
19748   call_arg_locations = NULL;
19749   call_arg_loc_last = NULL;
19750   call_site_count = -1;
19751   tail_call_site_count = -1;
19752   VEC_free (dw_die_ref, heap, block_map);
19753   htab_empty (decl_loc_table);
19754   htab_empty (cached_dw_loc_list_table);
19755 }
19756
19757 /* Output a marker (i.e. a label) for the beginning of the generated code for
19758    a lexical block.  */
19759
19760 static void
19761 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19762                        unsigned int blocknum)
19763 {
19764   switch_to_section (current_function_section ());
19765   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19766 }
19767
19768 /* Output a marker (i.e. a label) for the end of the generated code for a
19769    lexical block.  */
19770
19771 static void
19772 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19773 {
19774   switch_to_section (current_function_section ());
19775   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19776 }
19777
19778 /* Returns nonzero if it is appropriate not to emit any debugging
19779    information for BLOCK, because it doesn't contain any instructions.
19780
19781    Don't allow this for blocks with nested functions or local classes
19782    as we would end up with orphans, and in the presence of scheduling
19783    we may end up calling them anyway.  */
19784
19785 static bool
19786 dwarf2out_ignore_block (const_tree block)
19787 {
19788   tree decl;
19789   unsigned int i;
19790
19791   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19792     if (TREE_CODE (decl) == FUNCTION_DECL
19793         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19794       return 0;
19795   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19796     {
19797       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19798       if (TREE_CODE (decl) == FUNCTION_DECL
19799           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19800       return 0;
19801     }
19802
19803   return 1;
19804 }
19805
19806 /* Hash table routines for file_hash.  */
19807
19808 static int
19809 file_table_eq (const void *p1_p, const void *p2_p)
19810 {
19811   const struct dwarf_file_data *const p1 =
19812     (const struct dwarf_file_data *) p1_p;
19813   const char *const p2 = (const char *) p2_p;
19814   return filename_cmp (p1->filename, p2) == 0;
19815 }
19816
19817 static hashval_t
19818 file_table_hash (const void *p_p)
19819 {
19820   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19821   return htab_hash_string (p->filename);
19822 }
19823
19824 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19825    dwarf2out.c) and return its "index".  The index of each (known) filename is
19826    just a unique number which is associated with only that one filename.  We
19827    need such numbers for the sake of generating labels (in the .debug_sfnames
19828    section) and references to those files numbers (in the .debug_srcinfo
19829    and.debug_macinfo sections).  If the filename given as an argument is not
19830    found in our current list, add it to the list and assign it the next
19831    available unique index number.  In order to speed up searches, we remember
19832    the index of the filename was looked up last.  This handles the majority of
19833    all searches.  */
19834
19835 static struct dwarf_file_data *
19836 lookup_filename (const char *file_name)
19837 {
19838   void ** slot;
19839   struct dwarf_file_data * created;
19840
19841   /* Check to see if the file name that was searched on the previous
19842      call matches this file name.  If so, return the index.  */
19843   if (file_table_last_lookup
19844       && (file_name == file_table_last_lookup->filename
19845           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19846     return file_table_last_lookup;
19847
19848   /* Didn't match the previous lookup, search the table.  */
19849   slot = htab_find_slot_with_hash (file_table, file_name,
19850                                    htab_hash_string (file_name), INSERT);
19851   if (*slot)
19852     return (struct dwarf_file_data *) *slot;
19853
19854   created = ggc_alloc_dwarf_file_data ();
19855   created->filename = file_name;
19856   created->emitted_number = 0;
19857   *slot = created;
19858   return created;
19859 }
19860
19861 /* If the assembler will construct the file table, then translate the compiler
19862    internal file table number into the assembler file table number, and emit
19863    a .file directive if we haven't already emitted one yet.  The file table
19864    numbers are different because we prune debug info for unused variables and
19865    types, which may include filenames.  */
19866
19867 static int
19868 maybe_emit_file (struct dwarf_file_data * fd)
19869 {
19870   if (! fd->emitted_number)
19871     {
19872       if (last_emitted_file)
19873         fd->emitted_number = last_emitted_file->emitted_number + 1;
19874       else
19875         fd->emitted_number = 1;
19876       last_emitted_file = fd;
19877
19878       if (DWARF2_ASM_LINE_DEBUG_INFO)
19879         {
19880           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19881           output_quoted_string (asm_out_file,
19882                                 remap_debug_filename (fd->filename));
19883           fputc ('\n', asm_out_file);
19884         }
19885     }
19886
19887   return fd->emitted_number;
19888 }
19889
19890 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19891    That generation should happen after function debug info has been
19892    generated. The value of the attribute is the constant value of ARG.  */
19893
19894 static void
19895 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19896 {
19897   die_arg_entry entry;
19898
19899   if (!die || !arg)
19900     return;
19901
19902   if (!tmpl_value_parm_die_table)
19903     tmpl_value_parm_die_table
19904       = VEC_alloc (die_arg_entry, gc, 32);
19905
19906   entry.die = die;
19907   entry.arg = arg;
19908   VEC_safe_push (die_arg_entry, gc,
19909                  tmpl_value_parm_die_table,
19910                  &entry);
19911 }
19912
19913 /* Return TRUE if T is an instance of generic type, FALSE
19914    otherwise.  */
19915
19916 static bool
19917 generic_type_p (tree t)
19918 {
19919   if (t == NULL_TREE || !TYPE_P (t))
19920     return false;
19921   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19922 }
19923
19924 /* Schedule the generation of the generic parameter dies for the
19925   instance of generic type T. The proper generation itself is later
19926   done by gen_scheduled_generic_parms_dies. */
19927
19928 static void
19929 schedule_generic_params_dies_gen (tree t)
19930 {
19931   if (!generic_type_p (t))
19932     return;
19933
19934   if (generic_type_instances == NULL)
19935     generic_type_instances = VEC_alloc (tree, gc, 256);
19936
19937   VEC_safe_push (tree, gc, generic_type_instances, t);
19938 }
19939
19940 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19941    by append_entry_to_tmpl_value_parm_die_table. This function must
19942    be called after function DIEs have been generated.  */
19943
19944 static void
19945 gen_remaining_tmpl_value_param_die_attribute (void)
19946 {
19947   if (tmpl_value_parm_die_table)
19948     {
19949       unsigned i;
19950       die_arg_entry *e;
19951
19952       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19953         tree_add_const_value_attribute (e->die, e->arg);
19954     }
19955 }
19956
19957 /* Generate generic parameters DIEs for instances of generic types
19958    that have been previously scheduled by
19959    schedule_generic_params_dies_gen. This function must be called
19960    after all the types of the CU have been laid out.  */
19961
19962 static void
19963 gen_scheduled_generic_parms_dies (void)
19964 {
19965   unsigned i;
19966   tree t;
19967
19968   if (generic_type_instances == NULL)
19969     return;
19970   
19971   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19972     gen_generic_params_dies (t);
19973 }
19974
19975
19976 /* Replace DW_AT_name for the decl with name.  */
19977
19978 static void
19979 dwarf2out_set_name (tree decl, tree name)
19980 {
19981   dw_die_ref die;
19982   dw_attr_ref attr;
19983   const char *dname;
19984
19985   die = TYPE_SYMTAB_DIE (decl);
19986   if (!die)
19987     return;
19988
19989   dname = dwarf2_name (name, 0);
19990   if (!dname)
19991     return;
19992
19993   attr = get_AT (die, DW_AT_name);
19994   if (attr)
19995     {
19996       struct indirect_string_node *node;
19997
19998       node = find_AT_string (dname);
19999       /* replace the string.  */
20000       attr->dw_attr_val.v.val_str = node;
20001     }
20002
20003   else
20004     add_name_attribute (die, dname);
20005 }
20006
20007 /* Called by the final INSN scan whenever we see a var location.  We
20008    use it to drop labels in the right places, and throw the location in
20009    our lookup table.  */
20010
20011 static void
20012 dwarf2out_var_location (rtx loc_note)
20013 {
20014   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20015   struct var_loc_node *newloc;
20016   rtx next_real;
20017   static const char *last_label;
20018   static const char *last_postcall_label;
20019   static bool last_in_cold_section_p;
20020   tree decl;
20021   bool var_loc_p;
20022
20023   if (!NOTE_P (loc_note))
20024     {
20025       if (CALL_P (loc_note))
20026         {
20027           call_site_count++;
20028           if (SIBLING_CALL_P (loc_note))
20029             tail_call_site_count++;
20030         }
20031       return;
20032     }
20033
20034   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20035   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20036     return;
20037
20038   next_real = next_real_insn (loc_note);
20039
20040   /* If there are no instructions which would be affected by this note,
20041      don't do anything.  */
20042   if (var_loc_p
20043       && next_real == NULL_RTX
20044       && !NOTE_DURING_CALL_P (loc_note))
20045     return;
20046
20047   if (next_real == NULL_RTX)
20048     next_real = get_last_insn ();
20049
20050   /* If there were any real insns between note we processed last time
20051      and this note (or if it is the first note), clear
20052      last_{,postcall_}label so that they are not reused this time.  */
20053   if (last_var_location_insn == NULL_RTX
20054       || last_var_location_insn != next_real
20055       || last_in_cold_section_p != in_cold_section_p)
20056     {
20057       last_label = NULL;
20058       last_postcall_label = NULL;
20059     }
20060
20061   if (var_loc_p)
20062     {
20063       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20064       newloc = add_var_loc_to_decl (decl, loc_note,
20065                                     NOTE_DURING_CALL_P (loc_note)
20066                                     ? last_postcall_label : last_label);
20067       if (newloc == NULL)
20068         return;
20069     }
20070   else
20071     {
20072       decl = NULL_TREE;
20073       newloc = NULL;
20074     }
20075
20076   /* If there were no real insns between note we processed last time
20077      and this note, use the label we emitted last time.  Otherwise
20078      create a new label and emit it.  */
20079   if (last_label == NULL)
20080     {
20081       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20082       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20083       loclabel_num++;
20084       last_label = ggc_strdup (loclabel);
20085     }
20086
20087   if (!var_loc_p)
20088     {
20089       struct call_arg_loc_node *ca_loc
20090         = ggc_alloc_cleared_call_arg_loc_node ();
20091       rtx prev = prev_real_insn (loc_note), x;
20092       ca_loc->call_arg_loc_note = loc_note;
20093       ca_loc->next = NULL;
20094       ca_loc->label = last_label;
20095       gcc_assert (prev
20096                   && (CALL_P (prev)
20097                       || (NONJUMP_INSN_P (prev)
20098                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20099                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20100       if (!CALL_P (prev))
20101         prev = XVECEXP (PATTERN (prev), 0, 0);
20102       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20103       x = PATTERN (prev);
20104       if (GET_CODE (x) == PARALLEL)
20105         x = XVECEXP (x, 0, 0);
20106       if (GET_CODE (x) == SET)
20107         x = SET_SRC (x);
20108       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20109         {
20110           x = XEXP (XEXP (x, 0), 0);
20111           if (GET_CODE (x) == SYMBOL_REF
20112               && SYMBOL_REF_DECL (x)
20113               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20114             ca_loc->symbol_ref = x;
20115         }
20116       ca_loc->block = insn_scope (prev);
20117       if (call_arg_locations)
20118         call_arg_loc_last->next = ca_loc;
20119       else
20120         call_arg_locations = ca_loc;
20121       call_arg_loc_last = ca_loc;
20122     }
20123   else if (!NOTE_DURING_CALL_P (loc_note))
20124     newloc->label = last_label;
20125   else
20126     {
20127       if (!last_postcall_label)
20128         {
20129           sprintf (loclabel, "%s-1", last_label);
20130           last_postcall_label = ggc_strdup (loclabel);
20131         }
20132       newloc->label = last_postcall_label;
20133     }
20134
20135   last_var_location_insn = next_real;
20136   last_in_cold_section_p = in_cold_section_p;
20137 }
20138
20139 /* Note in one location list that text section has changed.  */
20140
20141 static int
20142 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20143 {
20144   var_loc_list *list = (var_loc_list *) *slot;
20145   if (list->first)
20146     list->last_before_switch
20147       = list->last->next ? list->last->next : list->last;
20148   return 1;
20149 }
20150
20151 /* Note in all location lists that text section has changed.  */
20152
20153 static void
20154 var_location_switch_text_section (void)
20155 {
20156   if (decl_loc_table == NULL)
20157     return;
20158
20159   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20160 }
20161
20162 /* Create a new line number table.  */
20163
20164 static dw_line_info_table *
20165 new_line_info_table (void)
20166 {
20167   dw_line_info_table *table;
20168
20169   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20170   table->file_num = 1;
20171   table->line_num = 1;
20172   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20173
20174   return table;
20175 }
20176
20177 /* Lookup the "current" table into which we emit line info, so
20178    that we don't have to do it for every source line.  */
20179
20180 static void
20181 set_cur_line_info_table (section *sec)
20182 {
20183   dw_line_info_table *table;
20184
20185   if (sec == text_section)
20186     table = text_section_line_info;
20187   else if (sec == cold_text_section)
20188     {
20189       table = cold_text_section_line_info;
20190       if (!table)
20191         {
20192           cold_text_section_line_info = table = new_line_info_table ();
20193           table->end_label = cold_end_label;
20194         }
20195     }
20196   else
20197     {
20198       const char *end_label;
20199
20200       if (flag_reorder_blocks_and_partition)
20201         {
20202           if (in_cold_section_p)
20203             end_label = crtl->subsections.cold_section_end_label;
20204           else
20205             end_label = crtl->subsections.hot_section_end_label;
20206         }
20207       else
20208         {
20209           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20210           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20211                                        current_function_funcdef_no);
20212           end_label = ggc_strdup (label);
20213         }
20214
20215       table = new_line_info_table ();
20216       table->end_label = end_label;
20217
20218       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20219     }
20220
20221   cur_line_info_table = table;
20222 }
20223
20224
20225 /* We need to reset the locations at the beginning of each
20226    function. We can't do this in the end_function hook, because the
20227    declarations that use the locations won't have been output when
20228    that hook is called.  Also compute have_multiple_function_sections here.  */
20229
20230 static void
20231 dwarf2out_begin_function (tree fun)
20232 {
20233   section *sec = function_section (fun);
20234
20235   if (sec != text_section)
20236     have_multiple_function_sections = true;
20237
20238   if (flag_reorder_blocks_and_partition && !cold_text_section)
20239     {
20240       gcc_assert (current_function_decl == fun);
20241       cold_text_section = unlikely_text_section ();
20242       switch_to_section (cold_text_section);
20243       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20244       switch_to_section (sec);
20245     }
20246
20247   dwarf2out_note_section_used ();
20248   call_site_count = 0;
20249   tail_call_site_count = 0;
20250
20251   set_cur_line_info_table (sec);
20252 }
20253
20254 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20255
20256 static void
20257 push_dw_line_info_entry (dw_line_info_table *table,
20258                          enum dw_line_info_opcode opcode, unsigned int val)
20259 {
20260   dw_line_info_entry e;
20261   e.opcode = opcode;
20262   e.val = val;
20263   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20264 }
20265
20266 /* Output a label to mark the beginning of a source code line entry
20267    and record information relating to this source line, in
20268    'line_info_table' for later output of the .debug_line section.  */
20269 /* ??? The discriminator parameter ought to be unsigned.  */
20270
20271 static void
20272 dwarf2out_source_line (unsigned int line, const char *filename,
20273                        int discriminator, bool is_stmt)
20274 {
20275   unsigned int file_num;
20276   dw_line_info_table *table;
20277
20278   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20279     return;
20280
20281   /* The discriminator column was added in dwarf4.  Simplify the below
20282      by simply removing it if we're not supposed to output it.  */
20283   if (dwarf_version < 4 && dwarf_strict)
20284     discriminator = 0;
20285
20286   table = cur_line_info_table;
20287   file_num = maybe_emit_file (lookup_filename (filename));
20288
20289   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20290      the debugger has used the second (possibly duplicate) line number
20291      at the beginning of the function to mark the end of the prologue.
20292      We could eliminate any other duplicates within the function.  For
20293      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20294      that second line number entry.  */
20295   /* Recall that this end-of-prologue indication is *not* the same thing
20296      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20297      to which the hook corresponds, follows the last insn that was 
20298      emitted by gen_prologue.  What we need is to preceed the first insn
20299      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20300      insn that corresponds to something the user wrote.  These may be
20301      very different locations once scheduling is enabled.  */
20302
20303   if (0 && file_num == table->file_num
20304       && line == table->line_num
20305       && discriminator == table->discrim_num
20306       && is_stmt == table->is_stmt)
20307     return;
20308
20309   switch_to_section (current_function_section ());
20310
20311   /* If requested, emit something human-readable.  */
20312   if (flag_debug_asm)
20313     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20314
20315   if (DWARF2_ASM_LINE_DEBUG_INFO)
20316     {
20317       /* Emit the .loc directive understood by GNU as.  */
20318       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20319       if (is_stmt != table->is_stmt)
20320         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20321       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20322         fprintf (asm_out_file, " discriminator %d", discriminator);
20323       fputc ('\n', asm_out_file);
20324     }
20325   else
20326     {
20327       unsigned int label_num = ++line_info_label_num;
20328
20329       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20330
20331       push_dw_line_info_entry (table, LI_set_address, label_num);
20332       if (file_num != table->file_num)
20333         push_dw_line_info_entry (table, LI_set_file, file_num);
20334       if (discriminator != table->discrim_num)
20335         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20336       if (is_stmt != table->is_stmt)
20337         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20338       push_dw_line_info_entry (table, LI_set_line, line);
20339     }
20340
20341   table->file_num = file_num;
20342   table->line_num = line;
20343   table->discrim_num = discriminator;
20344   table->is_stmt = is_stmt;
20345   table->in_use = true;
20346 }
20347
20348 /* Record the beginning of a new source file.  */
20349
20350 static void
20351 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20352 {
20353   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20354     {
20355       /* Record the beginning of the file for break_out_includes.  */
20356       dw_die_ref bincl_die;
20357
20358       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20359       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20360     }
20361
20362   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20363     {
20364       macinfo_entry e;
20365       e.code = DW_MACINFO_start_file;
20366       e.lineno = lineno;
20367       e.info = xstrdup (filename);
20368       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20369     }
20370 }
20371
20372 /* Record the end of a source file.  */
20373
20374 static void
20375 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20376 {
20377   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20378     /* Record the end of the file for break_out_includes.  */
20379     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20380
20381   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20382     {
20383       macinfo_entry e;
20384       e.code = DW_MACINFO_end_file;
20385       e.lineno = lineno;
20386       e.info = NULL;
20387       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20388     }
20389 }
20390
20391 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20392    the tail part of the directive line, i.e. the part which is past the
20393    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20394
20395 static void
20396 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20397                   const char *buffer ATTRIBUTE_UNUSED)
20398 {
20399   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20400     {
20401       macinfo_entry e;
20402       /* Insert a dummy first entry to be able to optimize the whole
20403          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20404       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20405         {
20406           e.code = 0;
20407           e.lineno = 0;
20408           e.info = NULL;
20409           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20410         }
20411       e.code = DW_MACINFO_define;
20412       e.lineno = lineno;
20413       e.info = xstrdup (buffer);;
20414       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20415     }
20416 }
20417
20418 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20419    the tail part of the directive line, i.e. the part which is past the
20420    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20421
20422 static void
20423 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20424                  const char *buffer ATTRIBUTE_UNUSED)
20425 {
20426   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20427     {
20428       macinfo_entry e;
20429       /* Insert a dummy first entry to be able to optimize the whole
20430          predefined macro block using DW_MACRO_GNU_transparent_include.  */
20431       if (VEC_empty (macinfo_entry, macinfo_table) && lineno == 0)
20432         {
20433           e.code = 0;
20434           e.lineno = 0;
20435           e.info = NULL;
20436           VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20437         }
20438       e.code = DW_MACINFO_undef;
20439       e.lineno = lineno;
20440       e.info = xstrdup (buffer);
20441       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20442     }
20443 }
20444
20445 /* Routines to manipulate hash table of CUs.  */
20446
20447 static hashval_t
20448 htab_macinfo_hash (const void *of)
20449 {
20450   const macinfo_entry *const entry =
20451     (const macinfo_entry *) of;
20452
20453   return htab_hash_string (entry->info);
20454 }
20455
20456 static int
20457 htab_macinfo_eq (const void *of1, const void *of2)
20458 {
20459   const macinfo_entry *const entry1 = (const macinfo_entry *) of1;
20460   const macinfo_entry *const entry2 = (const macinfo_entry *) of2;
20461
20462   return !strcmp (entry1->info, entry2->info);
20463 }
20464
20465 /* Output a single .debug_macinfo entry.  */
20466
20467 static void
20468 output_macinfo_op (macinfo_entry *ref)
20469 {
20470   int file_num;
20471   size_t len;
20472   struct indirect_string_node *node;
20473   char label[MAX_ARTIFICIAL_LABEL_BYTES];
20474
20475   switch (ref->code)
20476     {
20477     case DW_MACINFO_start_file:
20478       file_num = maybe_emit_file (lookup_filename (ref->info));
20479       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20480       dw2_asm_output_data_uleb128 (ref->lineno,
20481                                    "Included from line number %lu", 
20482                                    (unsigned long) ref->lineno);
20483       dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20484       break;
20485     case DW_MACINFO_end_file:
20486       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20487       break;
20488     case DW_MACINFO_define:
20489     case DW_MACINFO_undef:
20490       len = strlen (ref->info) + 1;
20491       if (!dwarf_strict
20492           && len > DWARF_OFFSET_SIZE
20493           && !DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
20494           && (debug_str_section->common.flags & SECTION_MERGE) != 0)
20495         {
20496           ref->code = ref->code == DW_MACINFO_define
20497                       ? DW_MACRO_GNU_define_indirect
20498                       : DW_MACRO_GNU_undef_indirect;
20499           output_macinfo_op (ref);
20500           return;
20501         }
20502       dw2_asm_output_data (1, ref->code,
20503                            ref->code == DW_MACINFO_define
20504                            ? "Define macro" : "Undefine macro");
20505       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20506                                    (unsigned long) ref->lineno);
20507       dw2_asm_output_nstring (ref->info, -1, "The macro");
20508       break;
20509     case DW_MACRO_GNU_define_indirect:
20510     case DW_MACRO_GNU_undef_indirect:
20511       node = find_AT_string (ref->info);
20512       if (node->form != DW_FORM_strp)
20513         {
20514           char label[32];
20515           ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
20516           ++dw2_string_counter;
20517           node->label = xstrdup (label);
20518           node->form = DW_FORM_strp;
20519         }
20520       dw2_asm_output_data (1, ref->code,
20521                            ref->code == DW_MACRO_GNU_define_indirect
20522                            ? "Define macro indirect"
20523                            : "Undefine macro indirect");
20524       dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20525                                    (unsigned long) ref->lineno);
20526       dw2_asm_output_offset (DWARF_OFFSET_SIZE, node->label,
20527                              debug_str_section, "The macro: \"%s\"",
20528                              ref->info);
20529       break;
20530     case DW_MACRO_GNU_transparent_include:
20531       dw2_asm_output_data (1, ref->code, "Transparent include");
20532       ASM_GENERATE_INTERNAL_LABEL (label,
20533                                    DEBUG_MACRO_SECTION_LABEL, ref->lineno);
20534       dw2_asm_output_offset (DWARF_OFFSET_SIZE, label, NULL, NULL);
20535       break;
20536     default:
20537       fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20538                ASM_COMMENT_START, (unsigned long) ref->code);
20539       break;
20540     }
20541 }
20542
20543 /* Attempt to make a sequence of define/undef macinfo ops shareable with
20544    other compilation unit .debug_macinfo sections.  IDX is the first
20545    index of a define/undef, return the number of ops that should be
20546    emitted in a comdat .debug_macinfo section and emit
20547    a DW_MACRO_GNU_transparent_include entry referencing it.
20548    If the define/undef entry should be emitted normally, return 0.  */
20549
20550 static unsigned
20551 optimize_macinfo_range (unsigned int idx, VEC (macinfo_entry, gc) *files,
20552                         htab_t *macinfo_htab)
20553 {
20554   macinfo_entry *first, *second, *cur, *inc;
20555   char linebuf[sizeof (HOST_WIDE_INT) * 3 + 1];
20556   unsigned char checksum[16];
20557   struct md5_ctx ctx;
20558   char *grp_name, *tail;
20559   const char *base;
20560   unsigned int i, count, encoded_filename_len, linebuf_len;
20561   void **slot;
20562
20563   first = VEC_index (macinfo_entry, macinfo_table, idx);
20564   second = VEC_index (macinfo_entry, macinfo_table, idx + 1);
20565
20566   /* Optimize only if there are at least two consecutive define/undef ops,
20567      and either all of them are before first DW_MACINFO_start_file
20568      with lineno 0 (i.e. predefined macro block), or all of them are
20569      in some included header file.  */
20570   if (second->code != DW_MACINFO_define && second->code != DW_MACINFO_undef)
20571     return 0;
20572   if (VEC_empty (macinfo_entry, files))
20573     {
20574       if (first->lineno != 0 || second->lineno != 0)
20575         return 0;
20576     }
20577   else if (first->lineno == 0)
20578     return 0;
20579
20580   /* Find the last define/undef entry that can be grouped together
20581      with first and at the same time compute md5 checksum of their
20582      codes, linenumbers and strings.  */
20583   md5_init_ctx (&ctx);
20584   for (i = idx; VEC_iterate (macinfo_entry, macinfo_table, i, cur); i++)
20585     if (cur->code != DW_MACINFO_define && cur->code != DW_MACINFO_undef)
20586       break;
20587     else if (first->lineno == 0 && cur->lineno != 0)
20588       break;
20589     else
20590       {
20591         unsigned char code = cur->code;
20592         md5_process_bytes (&code, 1, &ctx);
20593         checksum_uleb128 (cur->lineno, &ctx);
20594         md5_process_bytes (cur->info, strlen (cur->info) + 1, &ctx);
20595       }
20596   md5_finish_ctx (&ctx, checksum);
20597   count = i - idx;
20598
20599   /* From the containing include filename (if any) pick up just
20600      usable characters from its basename.  */
20601   if (first->lineno == 0)
20602     base = "";
20603   else
20604     base = lbasename (VEC_last (macinfo_entry, files)->info);
20605   for (encoded_filename_len = 0, i = 0; base[i]; i++)
20606     if (ISIDNUM (base[i]) || base[i] == '.')
20607       encoded_filename_len++;
20608   /* Count . at the end.  */
20609   if (encoded_filename_len)
20610     encoded_filename_len++;
20611
20612   sprintf (linebuf, HOST_WIDE_INT_PRINT_UNSIGNED, first->lineno);
20613   linebuf_len = strlen (linebuf);
20614
20615   /* The group name format is: wmN.[<encoded filename>.]<lineno>.<md5sum>  */
20616   grp_name = XNEWVEC (char, 4 + encoded_filename_len + linebuf_len + 1
20617                       + 16 * 2 + 1);
20618   memcpy (grp_name, DWARF_OFFSET_SIZE == 4 ? "wm4." : "wm8.", 4);
20619   tail = grp_name + 4;
20620   if (encoded_filename_len)
20621     {
20622       for (i = 0; base[i]; i++)
20623         if (ISIDNUM (base[i]) || base[i] == '.')
20624           *tail++ = base[i];
20625       *tail++ = '.';
20626     }
20627   memcpy (tail, linebuf, linebuf_len);
20628   tail += linebuf_len;
20629   *tail++ = '.';
20630   for (i = 0; i < 16; i++)
20631     sprintf (tail + i * 2, "%02x", checksum[i] & 0xff);
20632
20633   /* Construct a macinfo_entry for DW_MACRO_GNU_transparent_include
20634      in the empty vector entry before the first define/undef.  */
20635   inc = VEC_index (macinfo_entry, macinfo_table, idx - 1);
20636   inc->code = DW_MACRO_GNU_transparent_include;
20637   inc->lineno = 0;
20638   inc->info = grp_name;
20639   if (*macinfo_htab == NULL)
20640     *macinfo_htab = htab_create (10, htab_macinfo_hash, htab_macinfo_eq, NULL);
20641   /* Avoid emitting duplicates.  */
20642   slot = htab_find_slot (*macinfo_htab, inc, INSERT);
20643   if (*slot != NULL)
20644     {
20645       free (CONST_CAST (char *, inc->info));
20646       inc->code = 0;
20647       inc->info = NULL;
20648       /* If such an entry has been used before, just emit
20649          a DW_MACRO_GNU_transparent_include op.  */
20650       inc = (macinfo_entry *) *slot;
20651       output_macinfo_op (inc);
20652       /* And clear all macinfo_entry in the range to avoid emitting them
20653          in the second pass.  */
20654       for (i = idx;
20655            VEC_iterate (macinfo_entry, macinfo_table, i, cur)
20656            && i < idx + count;
20657            i++)
20658         {
20659           cur->code = 0;
20660           free (CONST_CAST (char *, cur->info));
20661           cur->info = NULL;
20662         }
20663     }
20664   else
20665     {
20666       *slot = inc;
20667       inc->lineno = htab_elements (*macinfo_htab);
20668       output_macinfo_op (inc);
20669     }
20670   return count;
20671 }
20672
20673 /* Output macinfo section(s).  */
20674
20675 static void
20676 output_macinfo (void)
20677 {
20678   unsigned i;
20679   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20680   macinfo_entry *ref;
20681   VEC (macinfo_entry, gc) *files = NULL;
20682   htab_t macinfo_htab = NULL;
20683
20684   if (! length)
20685     return;
20686
20687   /* output_macinfo* uses these interchangeably.  */
20688   gcc_assert ((int) DW_MACINFO_define == (int) DW_MACRO_GNU_define
20689               && (int) DW_MACINFO_undef == (int) DW_MACRO_GNU_undef
20690               && (int) DW_MACINFO_start_file == (int) DW_MACRO_GNU_start_file
20691               && (int) DW_MACINFO_end_file == (int) DW_MACRO_GNU_end_file);
20692
20693   /* For .debug_macro emit the section header.  */
20694   if (!dwarf_strict)
20695     {
20696       dw2_asm_output_data (2, 4, "DWARF macro version number");
20697       if (DWARF_OFFSET_SIZE == 8)
20698         dw2_asm_output_data (1, 3, "Flags: 64-bit, lineptr present");
20699       else
20700         dw2_asm_output_data (1, 2, "Flags: 32-bit, lineptr present");
20701       dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_line_section_label,
20702                              debug_line_section, NULL);
20703     }
20704
20705   /* In the first loop, it emits the primary .debug_macinfo section
20706      and after each emitted op the macinfo_entry is cleared.
20707      If a longer range of define/undef ops can be optimized using
20708      DW_MACRO_GNU_transparent_include, the
20709      DW_MACRO_GNU_transparent_include op is emitted and kept in
20710      the vector before the first define/undef in the range and the
20711      whole range of define/undef ops is not emitted and kept.  */
20712   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20713     {
20714       switch (ref->code)
20715         {
20716         case DW_MACINFO_start_file:
20717           VEC_safe_push (macinfo_entry, gc, files, ref);
20718           break;
20719         case DW_MACINFO_end_file:
20720           if (!VEC_empty (macinfo_entry, files))
20721             {
20722               macinfo_entry *file = VEC_last (macinfo_entry, files);
20723               free (CONST_CAST (char *, file->info));
20724               VEC_pop (macinfo_entry, files);
20725             }
20726           break;
20727         case DW_MACINFO_define:
20728         case DW_MACINFO_undef:
20729           if (!dwarf_strict
20730               && HAVE_COMDAT_GROUP
20731               && VEC_length (macinfo_entry, files) != 1
20732               && i > 0
20733               && i + 1 < length
20734               && VEC_index (macinfo_entry, macinfo_table, i - 1)->code == 0)
20735             {
20736               unsigned count = optimize_macinfo_range (i, files, &macinfo_htab);
20737               if (count)
20738                 {
20739                   i += count - 1;
20740                   continue;
20741                 }
20742             }
20743           break;
20744         case 0:
20745           /* A dummy entry may be inserted at the beginning to be able
20746              to optimize the whole block of predefined macros.  */
20747           if (i == 0)
20748             continue;
20749         default:
20750           break;
20751         }
20752       output_macinfo_op (ref);
20753       /* For DW_MACINFO_start_file ref->info has been copied into files
20754          vector.  */
20755       if (ref->code != DW_MACINFO_start_file)
20756         free (CONST_CAST (char *, ref->info));
20757       ref->info = NULL;
20758       ref->code = 0;
20759     }
20760
20761   if (macinfo_htab == NULL)
20762     return;
20763
20764   htab_delete (macinfo_htab);
20765
20766   /* If any DW_MACRO_GNU_transparent_include were used, on those
20767      DW_MACRO_GNU_transparent_include entries terminate the
20768      current chain and switch to a new comdat .debug_macinfo
20769      section and emit the define/undef entries within it.  */
20770   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20771     switch (ref->code)
20772       {
20773       case 0:
20774         continue;
20775       case DW_MACRO_GNU_transparent_include:
20776         {
20777           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20778           tree comdat_key = get_identifier (ref->info);
20779           /* Terminate the previous .debug_macinfo section.  */
20780           dw2_asm_output_data (1, 0, "End compilation unit");
20781           targetm.asm_out.named_section (DEBUG_MACRO_SECTION,
20782                                          SECTION_DEBUG
20783                                          | SECTION_LINKONCE,
20784                                          comdat_key);
20785           ASM_GENERATE_INTERNAL_LABEL (label,
20786                                        DEBUG_MACRO_SECTION_LABEL,
20787                                        ref->lineno);
20788           ASM_OUTPUT_LABEL (asm_out_file, label);
20789           ref->code = 0;
20790           free (CONST_CAST (char *, ref->info));
20791           ref->info = NULL;
20792           dw2_asm_output_data (2, 4, "DWARF macro version number");
20793           if (DWARF_OFFSET_SIZE == 8)
20794             dw2_asm_output_data (1, 1, "Flags: 64-bit");
20795           else
20796             dw2_asm_output_data (1, 0, "Flags: 32-bit");
20797         }
20798         break;
20799       case DW_MACINFO_define:
20800       case DW_MACINFO_undef:
20801         output_macinfo_op (ref);
20802         ref->code = 0;
20803         free (CONST_CAST (char *, ref->info));
20804         ref->info = NULL;
20805         break;
20806       default:
20807         gcc_unreachable ();
20808       }
20809 }
20810
20811 /* Set up for Dwarf output at the start of compilation.  */
20812
20813 static void
20814 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20815 {
20816   /* Allocate the file_table.  */
20817   file_table = htab_create_ggc (50, file_table_hash,
20818                                 file_table_eq, NULL);
20819
20820   /* Allocate the decl_die_table.  */
20821   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20822                                     decl_die_table_eq, NULL);
20823
20824   /* Allocate the decl_loc_table.  */
20825   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20826                                     decl_loc_table_eq, NULL);
20827
20828   /* Allocate the cached_dw_loc_list_table.  */
20829   cached_dw_loc_list_table
20830     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20831                        cached_dw_loc_list_table_eq, NULL);
20832
20833   /* Allocate the initial hunk of the decl_scope_table.  */
20834   decl_scope_table = VEC_alloc (tree, gc, 256);
20835
20836   /* Allocate the initial hunk of the abbrev_die_table.  */
20837   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20838     (ABBREV_DIE_TABLE_INCREMENT);
20839   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20840   /* Zero-th entry is allocated, but unused.  */
20841   abbrev_die_table_in_use = 1;
20842
20843   /* Allocate the pubtypes and pubnames vectors.  */
20844   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20845   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20846
20847   incomplete_types = VEC_alloc (tree, gc, 64);
20848
20849   used_rtx_array = VEC_alloc (rtx, gc, 32);
20850
20851   debug_info_section = get_section (DEBUG_INFO_SECTION,
20852                                     SECTION_DEBUG, NULL);
20853   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20854                                       SECTION_DEBUG, NULL);
20855   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20856                                        SECTION_DEBUG, NULL);
20857   debug_macinfo_section = get_section (dwarf_strict
20858                                        ? DEBUG_MACINFO_SECTION
20859                                        : DEBUG_MACRO_SECTION,
20860                                        SECTION_DEBUG, NULL);
20861   debug_line_section = get_section (DEBUG_LINE_SECTION,
20862                                     SECTION_DEBUG, NULL);
20863   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20864                                    SECTION_DEBUG, NULL);
20865   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20866                                         SECTION_DEBUG, NULL);
20867   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20868                                         SECTION_DEBUG, NULL);
20869   debug_str_section = get_section (DEBUG_STR_SECTION,
20870                                    DEBUG_STR_SECTION_FLAGS, NULL);
20871   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20872                                       SECTION_DEBUG, NULL);
20873   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20874                                      SECTION_DEBUG, NULL);
20875
20876   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20877   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20878                                DEBUG_ABBREV_SECTION_LABEL, 0);
20879   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20880   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20881                                COLD_TEXT_SECTION_LABEL, 0);
20882   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20883
20884   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20885                                DEBUG_INFO_SECTION_LABEL, 0);
20886   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20887                                DEBUG_LINE_SECTION_LABEL, 0);
20888   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20889                                DEBUG_RANGES_SECTION_LABEL, 0);
20890   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20891                                dwarf_strict
20892                                ? DEBUG_MACINFO_SECTION_LABEL
20893                                : DEBUG_MACRO_SECTION_LABEL, 0);
20894
20895   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20896     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20897
20898   switch_to_section (text_section);
20899   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20900
20901   /* Make sure the line number table for .text always exists.  */
20902   text_section_line_info = new_line_info_table ();
20903   text_section_line_info->end_label = text_end_label;
20904 }
20905
20906 /* Called before cgraph_optimize starts outputtting functions, variables
20907    and toplevel asms into assembly.  */
20908
20909 static void
20910 dwarf2out_assembly_start (void)
20911 {
20912   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20913       && dwarf2out_do_cfi_asm ()
20914       && (!(flag_unwind_tables || flag_exceptions)
20915           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20916     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20917 }
20918
20919 /* A helper function for dwarf2out_finish called through
20920    htab_traverse.  Emit one queued .debug_str string.  */
20921
20922 static int
20923 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20924 {
20925   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20926
20927   if (node->form == DW_FORM_strp)
20928     {
20929       switch_to_section (debug_str_section);
20930       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20931       assemble_string (node->str, strlen (node->str) + 1);
20932     }
20933
20934   return 1;
20935 }
20936
20937 #if ENABLE_ASSERT_CHECKING
20938 /* Verify that all marks are clear.  */
20939
20940 static void
20941 verify_marks_clear (dw_die_ref die)
20942 {
20943   dw_die_ref c;
20944
20945   gcc_assert (! die->die_mark);
20946   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20947 }
20948 #endif /* ENABLE_ASSERT_CHECKING */
20949
20950 /* Clear the marks for a die and its children.
20951    Be cool if the mark isn't set.  */
20952
20953 static void
20954 prune_unmark_dies (dw_die_ref die)
20955 {
20956   dw_die_ref c;
20957
20958   if (die->die_mark)
20959     die->die_mark = 0;
20960   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20961 }
20962
20963 /* Given DIE that we're marking as used, find any other dies
20964    it references as attributes and mark them as used.  */
20965
20966 static void
20967 prune_unused_types_walk_attribs (dw_die_ref die)
20968 {
20969   dw_attr_ref a;
20970   unsigned ix;
20971
20972   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20973     {
20974       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20975         {
20976           /* A reference to another DIE.
20977              Make sure that it will get emitted.
20978              If it was broken out into a comdat group, don't follow it.  */
20979           if (! use_debug_types
20980               || a->dw_attr == DW_AT_specification
20981               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
20982             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20983         }
20984       /* Set the string's refcount to 0 so that prune_unused_types_mark
20985          accounts properly for it.  */
20986       if (AT_class (a) == dw_val_class_str)
20987         a->dw_attr_val.v.val_str->refcount = 0;
20988     }
20989 }
20990
20991 /* Mark the generic parameters and arguments children DIEs of DIE.  */
20992
20993 static void
20994 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
20995 {
20996   dw_die_ref c;
20997
20998   if (die == NULL || die->die_child == NULL)
20999     return;
21000   c = die->die_child;
21001   do
21002     {
21003       switch (c->die_tag)
21004         {
21005         case DW_TAG_template_type_param:
21006         case DW_TAG_template_value_param:
21007         case DW_TAG_GNU_template_template_param:
21008         case DW_TAG_GNU_template_parameter_pack:
21009           prune_unused_types_mark (c, 1);
21010           break;
21011         default:
21012           break;
21013         }
21014       c = c->die_sib;
21015     } while (c && c != die->die_child);
21016 }
21017
21018 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21019    to DIE's children.  */
21020
21021 static void
21022 prune_unused_types_mark (dw_die_ref die, int dokids)
21023 {
21024   dw_die_ref c;
21025
21026   if (die->die_mark == 0)
21027     {
21028       /* We haven't done this node yet.  Mark it as used.  */
21029       die->die_mark = 1;
21030       /* If this is the DIE of a generic type instantiation,
21031          mark the children DIEs that describe its generic parms and
21032          args.  */
21033       prune_unused_types_mark_generic_parms_dies (die);
21034
21035       /* We also have to mark its parents as used.
21036          (But we don't want to mark our parents' kids due to this.)  */
21037       if (die->die_parent)
21038         prune_unused_types_mark (die->die_parent, 0);
21039
21040       /* Mark any referenced nodes.  */
21041       prune_unused_types_walk_attribs (die);
21042
21043       /* If this node is a specification,
21044          also mark the definition, if it exists.  */
21045       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21046         prune_unused_types_mark (die->die_definition, 1);
21047     }
21048
21049   if (dokids && die->die_mark != 2)
21050     {
21051       /* We need to walk the children, but haven't done so yet.
21052          Remember that we've walked the kids.  */
21053       die->die_mark = 2;
21054
21055       /* If this is an array type, we need to make sure our
21056          kids get marked, even if they're types.  If we're
21057          breaking out types into comdat sections, do this
21058          for all type definitions.  */
21059       if (die->die_tag == DW_TAG_array_type
21060           || (use_debug_types
21061               && is_type_die (die) && ! is_declaration_die (die)))
21062         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21063       else
21064         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21065     }
21066 }
21067
21068 /* For local classes, look if any static member functions were emitted
21069    and if so, mark them.  */
21070
21071 static void
21072 prune_unused_types_walk_local_classes (dw_die_ref die)
21073 {
21074   dw_die_ref c;
21075
21076   if (die->die_mark == 2)
21077     return;
21078
21079   switch (die->die_tag)
21080     {
21081     case DW_TAG_structure_type:
21082     case DW_TAG_union_type:
21083     case DW_TAG_class_type:
21084       break;
21085
21086     case DW_TAG_subprogram:
21087       if (!get_AT_flag (die, DW_AT_declaration)
21088           || die->die_definition != NULL)
21089         prune_unused_types_mark (die, 1);
21090       return;
21091
21092     default:
21093       return;
21094     }
21095
21096   /* Mark children.  */
21097   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21098 }
21099
21100 /* Walk the tree DIE and mark types that we actually use.  */
21101
21102 static void
21103 prune_unused_types_walk (dw_die_ref die)
21104 {
21105   dw_die_ref c;
21106
21107   /* Don't do anything if this node is already marked and
21108      children have been marked as well.  */
21109   if (die->die_mark == 2)
21110     return;
21111
21112   switch (die->die_tag)
21113     {
21114     case DW_TAG_structure_type:
21115     case DW_TAG_union_type:
21116     case DW_TAG_class_type:
21117       if (die->die_perennial_p)
21118         break;
21119
21120       for (c = die->die_parent; c; c = c->die_parent)
21121         if (c->die_tag == DW_TAG_subprogram)
21122           break;
21123
21124       /* Finding used static member functions inside of classes
21125          is needed just for local classes, because for other classes
21126          static member function DIEs with DW_AT_specification
21127          are emitted outside of the DW_TAG_*_type.  If we ever change
21128          it, we'd need to call this even for non-local classes.  */
21129       if (c)
21130         prune_unused_types_walk_local_classes (die);
21131
21132       /* It's a type node --- don't mark it.  */
21133       return;
21134
21135     case DW_TAG_const_type:
21136     case DW_TAG_packed_type:
21137     case DW_TAG_pointer_type:
21138     case DW_TAG_reference_type:
21139     case DW_TAG_rvalue_reference_type:
21140     case DW_TAG_volatile_type:
21141     case DW_TAG_typedef:
21142     case DW_TAG_array_type:
21143     case DW_TAG_interface_type:
21144     case DW_TAG_friend:
21145     case DW_TAG_variant_part:
21146     case DW_TAG_enumeration_type:
21147     case DW_TAG_subroutine_type:
21148     case DW_TAG_string_type:
21149     case DW_TAG_set_type:
21150     case DW_TAG_subrange_type:
21151     case DW_TAG_ptr_to_member_type:
21152     case DW_TAG_file_type:
21153       if (die->die_perennial_p)
21154         break;
21155
21156       /* It's a type node --- don't mark it.  */
21157       return;
21158
21159     default:
21160       /* Mark everything else.  */
21161       break;
21162   }
21163
21164   if (die->die_mark == 0)
21165     {
21166       die->die_mark = 1;
21167
21168       /* Now, mark any dies referenced from here.  */
21169       prune_unused_types_walk_attribs (die);
21170     }
21171
21172   die->die_mark = 2;
21173
21174   /* Mark children.  */
21175   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21176 }
21177
21178 /* Increment the string counts on strings referred to from DIE's
21179    attributes.  */
21180
21181 static void
21182 prune_unused_types_update_strings (dw_die_ref die)
21183 {
21184   dw_attr_ref a;
21185   unsigned ix;
21186
21187   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21188     if (AT_class (a) == dw_val_class_str)
21189       {
21190         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21191         s->refcount++;
21192         /* Avoid unnecessarily putting strings that are used less than
21193            twice in the hash table.  */
21194         if (s->refcount
21195             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21196           {
21197             void ** slot;
21198             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21199                                              htab_hash_string (s->str),
21200                                              INSERT);
21201             gcc_assert (*slot == NULL);
21202             *slot = s;
21203           }
21204       }
21205 }
21206
21207 /* Remove from the tree DIE any dies that aren't marked.  */
21208
21209 static void
21210 prune_unused_types_prune (dw_die_ref die)
21211 {
21212   dw_die_ref c;
21213
21214   gcc_assert (die->die_mark);
21215   prune_unused_types_update_strings (die);
21216
21217   if (! die->die_child)
21218     return;
21219
21220   c = die->die_child;
21221   do {
21222     dw_die_ref prev = c;
21223     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21224       if (c == die->die_child)
21225         {
21226           /* No marked children between 'prev' and the end of the list.  */
21227           if (prev == c)
21228             /* No marked children at all.  */
21229             die->die_child = NULL;
21230           else
21231             {
21232               prev->die_sib = c->die_sib;
21233               die->die_child = prev;
21234             }
21235           return;
21236         }
21237
21238     if (c != prev->die_sib)
21239       prev->die_sib = c;
21240     prune_unused_types_prune (c);
21241   } while (c != die->die_child);
21242 }
21243
21244 /* Remove dies representing declarations that we never use.  */
21245
21246 static void
21247 prune_unused_types (void)
21248 {
21249   unsigned int i;
21250   limbo_die_node *node;
21251   comdat_type_node *ctnode;
21252   pubname_ref pub;
21253   dw_die_ref base_type;
21254
21255 #if ENABLE_ASSERT_CHECKING
21256   /* All the marks should already be clear.  */
21257   verify_marks_clear (comp_unit_die ());
21258   for (node = limbo_die_list; node; node = node->next)
21259     verify_marks_clear (node->die);
21260   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21261     verify_marks_clear (ctnode->root_die);
21262 #endif /* ENABLE_ASSERT_CHECKING */
21263
21264   /* Mark types that are used in global variables.  */
21265   premark_types_used_by_global_vars ();
21266
21267   /* Set the mark on nodes that are actually used.  */
21268   prune_unused_types_walk (comp_unit_die ());
21269   for (node = limbo_die_list; node; node = node->next)
21270     prune_unused_types_walk (node->die);
21271   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21272     {
21273       prune_unused_types_walk (ctnode->root_die);
21274       prune_unused_types_mark (ctnode->type_die, 1);
21275     }
21276
21277   /* Also set the mark on nodes referenced from the
21278      pubname_table.  */
21279   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21280     prune_unused_types_mark (pub->die, 1);
21281   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21282     prune_unused_types_mark (base_type, 1);
21283
21284   if (debug_str_hash)
21285     htab_empty (debug_str_hash);
21286   prune_unused_types_prune (comp_unit_die ());
21287   for (node = limbo_die_list; node; node = node->next)
21288     prune_unused_types_prune (node->die);
21289   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21290     prune_unused_types_prune (ctnode->root_die);
21291
21292   /* Leave the marks clear.  */
21293   prune_unmark_dies (comp_unit_die ());
21294   for (node = limbo_die_list; node; node = node->next)
21295     prune_unmark_dies (node->die);
21296   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21297     prune_unmark_dies (ctnode->root_die);
21298 }
21299
21300 /* Set the parameter to true if there are any relative pathnames in
21301    the file table.  */
21302 static int
21303 file_table_relative_p (void ** slot, void *param)
21304 {
21305   bool *p = (bool *) param;
21306   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21307   if (!IS_ABSOLUTE_PATH (d->filename))
21308     {
21309       *p = true;
21310       return 0;
21311     }
21312   return 1;
21313 }
21314
21315 /* Routines to manipulate hash table of comdat type units.  */
21316
21317 static hashval_t
21318 htab_ct_hash (const void *of)
21319 {
21320   hashval_t h;
21321   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21322
21323   memcpy (&h, type_node->signature, sizeof (h));
21324   return h;
21325 }
21326
21327 static int
21328 htab_ct_eq (const void *of1, const void *of2)
21329 {
21330   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21331   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21332
21333   return (! memcmp (type_node_1->signature, type_node_2->signature,
21334                     DWARF_TYPE_SIGNATURE_SIZE));
21335 }
21336
21337 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21338    to the location it would have been added, should we know its
21339    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21340    probably improve compactness of debug info, removing equivalent
21341    abbrevs, and hide any differences caused by deferring the
21342    computation of the assembler name, triggered by e.g. PCH.  */
21343
21344 static inline void
21345 move_linkage_attr (dw_die_ref die)
21346 {
21347   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21348   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21349
21350   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21351               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21352
21353   while (--ix > 0)
21354     {
21355       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21356
21357       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21358         break;
21359     }
21360
21361   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21362     {
21363       VEC_pop (dw_attr_node, die->die_attr);
21364       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21365     }
21366 }
21367
21368 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21369    referenced from typed stack ops and count how often they are used.  */
21370
21371 static void
21372 mark_base_types (dw_loc_descr_ref loc)
21373 {
21374   dw_die_ref base_type = NULL;
21375
21376   for (; loc; loc = loc->dw_loc_next)
21377     {
21378       switch (loc->dw_loc_opc)
21379         {
21380         case DW_OP_GNU_regval_type:
21381         case DW_OP_GNU_deref_type:
21382           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21383           break;
21384         case DW_OP_GNU_convert:
21385         case DW_OP_GNU_reinterpret:
21386           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21387             continue;
21388           /* FALLTHRU */
21389         case DW_OP_GNU_const_type:
21390           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21391           break;
21392         case DW_OP_GNU_entry_value:
21393           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21394           continue;
21395         default:
21396           continue;
21397         }
21398       gcc_assert (base_type->die_parent == comp_unit_die ());
21399       if (base_type->die_mark)
21400         base_type->die_mark++;
21401       else
21402         {
21403           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21404           base_type->die_mark = 1;
21405         }
21406     }
21407 }
21408
21409 /* Comparison function for sorting marked base types.  */
21410
21411 static int
21412 base_type_cmp (const void *x, const void *y)
21413 {
21414   dw_die_ref dx = *(const dw_die_ref *) x;
21415   dw_die_ref dy = *(const dw_die_ref *) y;
21416   unsigned int byte_size1, byte_size2;
21417   unsigned int encoding1, encoding2;
21418   if (dx->die_mark > dy->die_mark)
21419     return -1;
21420   if (dx->die_mark < dy->die_mark)
21421     return 1;
21422   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21423   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21424   if (byte_size1 < byte_size2)
21425     return 1;
21426   if (byte_size1 > byte_size2)
21427     return -1;
21428   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21429   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21430   if (encoding1 < encoding2)
21431     return 1;
21432   if (encoding1 > encoding2)
21433     return -1;
21434   return 0;
21435 }
21436
21437 /* Move base types marked by mark_base_types as early as possible
21438    in the CU, sorted by decreasing usage count both to make the
21439    uleb128 references as small as possible and to make sure they
21440    will have die_offset already computed by calc_die_sizes when
21441    sizes of typed stack loc ops is computed.  */
21442
21443 static void
21444 move_marked_base_types (void)
21445 {
21446   unsigned int i;
21447   dw_die_ref base_type, die, c;
21448
21449   if (VEC_empty (dw_die_ref, base_types))
21450     return;
21451
21452   /* Sort by decreasing usage count, they will be added again in that
21453      order later on.  */
21454   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21455   die = comp_unit_die ();
21456   c = die->die_child;
21457   do
21458     {
21459       dw_die_ref prev = c;
21460       c = c->die_sib;
21461       while (c->die_mark)
21462         {
21463           remove_child_with_prev (c, prev);
21464           /* As base types got marked, there must be at least
21465              one node other than DW_TAG_base_type.  */
21466           gcc_assert (c != c->die_sib);
21467           c = c->die_sib;
21468         }
21469     }
21470   while (c != die->die_child);
21471   gcc_assert (die->die_child);
21472   c = die->die_child;
21473   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21474     {
21475       base_type->die_mark = 0;
21476       base_type->die_sib = c->die_sib;
21477       c->die_sib = base_type;
21478       c = base_type;
21479     }
21480 }
21481
21482 /* Helper function for resolve_addr, attempt to resolve
21483    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21484    SYMBOL_REFs refer to variables emitted in the current CU.  */
21485
21486 static int
21487 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21488 {
21489   rtx rtl = *addr;
21490
21491   if (GET_CODE (rtl) == CONST_STRING)
21492     {
21493       size_t len = strlen (XSTR (rtl, 0)) + 1;
21494       tree t = build_string (len, XSTR (rtl, 0));
21495       tree tlen = size_int (len - 1);
21496       TREE_TYPE (t)
21497         = build_array_type (char_type_node, build_index_type (tlen));
21498       rtl = lookup_constant_def (t);
21499       if (!rtl || !MEM_P (rtl))
21500         return 1;
21501       rtl = XEXP (rtl, 0);
21502       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21503       *addr = rtl;
21504       return 0;
21505     }
21506
21507   if (GET_CODE (rtl) == SYMBOL_REF
21508       && SYMBOL_REF_DECL (rtl))
21509     {
21510       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21511         {
21512           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21513             return 1;
21514         }
21515       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21516         return 1;
21517     }
21518
21519   if (GET_CODE (rtl) == CONST
21520       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21521     return 1;
21522
21523   return 0;
21524 }
21525
21526 /* Helper function for resolve_addr, handle one location
21527    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21528    the location list couldn't be resolved.  */
21529
21530 static bool
21531 resolve_addr_in_expr (dw_loc_descr_ref loc)
21532 {
21533   dw_loc_descr_ref keep = NULL;
21534   for (; loc; loc = loc->dw_loc_next)
21535     switch (loc->dw_loc_opc)
21536       {
21537       case DW_OP_addr:
21538         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21539           return false;
21540         break;
21541       case DW_OP_const4u:
21542       case DW_OP_const8u:
21543         if (loc->dtprel
21544             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21545           return false;
21546         break;
21547       case DW_OP_plus_uconst:
21548         if (size_of_loc_descr (loc)
21549             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21550               + 1
21551             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21552           {
21553             dw_loc_descr_ref repl
21554               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21555             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21556             add_loc_descr (&repl, loc->dw_loc_next);
21557             *loc = *repl;
21558           }
21559         break;
21560       case DW_OP_implicit_value:
21561         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21562             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21563           return false;
21564         break;
21565       case DW_OP_GNU_implicit_pointer:
21566       case DW_OP_GNU_parameter_ref:
21567         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21568           {
21569             dw_die_ref ref
21570               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21571             if (ref == NULL)
21572               return false;
21573             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21574             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21575             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21576           }
21577         break;
21578       case DW_OP_GNU_const_type:
21579       case DW_OP_GNU_regval_type:
21580       case DW_OP_GNU_deref_type:
21581       case DW_OP_GNU_convert:
21582       case DW_OP_GNU_reinterpret:
21583         while (loc->dw_loc_next
21584                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21585           {
21586             dw_die_ref base1, base2;
21587             unsigned enc1, enc2, size1, size2;
21588             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21589                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21590               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21591             else if (loc->dw_loc_oprnd1.val_class
21592                      == dw_val_class_unsigned_const)
21593               break;
21594             else
21595               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21596             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21597                 == dw_val_class_unsigned_const)
21598               break;
21599             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21600             gcc_assert (base1->die_tag == DW_TAG_base_type
21601                         && base2->die_tag == DW_TAG_base_type);
21602             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21603             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21604             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21605             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21606             if (size1 == size2
21607                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21608                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21609                      && loc != keep)
21610                     || enc1 == enc2))
21611               {
21612                 /* Optimize away next DW_OP_GNU_convert after
21613                    adjusting LOC's base type die reference.  */
21614                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21615                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21616                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21617                 else
21618                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21619                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21620                 continue;
21621               }
21622             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21623                point typed stack entry.  */
21624             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21625               keep = loc->dw_loc_next;
21626             break;
21627           }
21628         break;
21629       default:
21630         break;
21631       }
21632   return true;
21633 }
21634
21635 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21636    an address in .rodata section if the string literal is emitted there,
21637    or remove the containing location list or replace DW_AT_const_value
21638    with DW_AT_location and empty location expression, if it isn't found
21639    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21640    to something that has been emitted in the current CU.  */
21641
21642 static void
21643 resolve_addr (dw_die_ref die)
21644 {
21645   dw_die_ref c;
21646   dw_attr_ref a;
21647   dw_loc_list_ref *curr, *start, loc;
21648   unsigned ix;
21649
21650   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21651     switch (AT_class (a))
21652       {
21653       case dw_val_class_loc_list:
21654         start = curr = AT_loc_list_ptr (a);
21655         loc = *curr;
21656         gcc_assert (loc);
21657         /* The same list can be referenced more than once.  See if we have
21658            already recorded the result from a previous pass.  */
21659         if (loc->replaced)
21660           *curr = loc->dw_loc_next;
21661         else if (!loc->resolved_addr)
21662           {
21663             /* As things stand, we do not expect or allow one die to
21664                reference a suffix of another die's location list chain.
21665                References must be identical or completely separate.
21666                There is therefore no need to cache the result of this
21667                pass on any list other than the first; doing so
21668                would lead to unnecessary writes.  */
21669             while (*curr)
21670               {
21671                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21672                 if (!resolve_addr_in_expr ((*curr)->expr))
21673                   {
21674                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21675                     if (next && (*curr)->ll_symbol)
21676                       {
21677                         gcc_assert (!next->ll_symbol);
21678                         next->ll_symbol = (*curr)->ll_symbol;
21679                       }
21680                     *curr = next;
21681                   }
21682                 else
21683                   {
21684                     mark_base_types ((*curr)->expr);
21685                     curr = &(*curr)->dw_loc_next;
21686                   }
21687               }
21688             if (loc == *start)
21689               loc->resolved_addr = 1;
21690             else
21691               {
21692                 loc->replaced = 1;
21693                 loc->dw_loc_next = *start;
21694               }
21695           }
21696         if (!*start)
21697           {
21698             remove_AT (die, a->dw_attr);
21699             ix--;
21700           }
21701         break;
21702       case dw_val_class_loc:
21703         if (!resolve_addr_in_expr (AT_loc (a)))
21704           {
21705             remove_AT (die, a->dw_attr);
21706             ix--;
21707           }
21708         else
21709           mark_base_types (AT_loc (a));
21710         break;
21711       case dw_val_class_addr:
21712         if (a->dw_attr == DW_AT_const_value
21713             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21714           {
21715             remove_AT (die, a->dw_attr);
21716             ix--;
21717           }
21718         if (die->die_tag == DW_TAG_GNU_call_site
21719             && a->dw_attr == DW_AT_abstract_origin)
21720           {
21721             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21722             dw_die_ref tdie = lookup_decl_die (tdecl);
21723             if (tdie == NULL
21724                 && DECL_EXTERNAL (tdecl)
21725                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21726               {
21727                 force_decl_die (tdecl);
21728                 tdie = lookup_decl_die (tdecl);
21729               }
21730             if (tdie)
21731               {
21732                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21733                 a->dw_attr_val.v.val_die_ref.die = tdie;
21734                 a->dw_attr_val.v.val_die_ref.external = 0;
21735               }
21736             else
21737               {
21738                 remove_AT (die, a->dw_attr);
21739                 ix--;
21740               }
21741           }
21742         break;
21743       default:
21744         break;
21745       }
21746
21747   FOR_EACH_CHILD (die, c, resolve_addr (c));
21748 }
21749 \f
21750 /* Helper routines for optimize_location_lists.
21751    This pass tries to share identical local lists in .debug_loc
21752    section.  */
21753
21754 /* Iteratively hash operands of LOC opcode.  */
21755
21756 static inline hashval_t
21757 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21758 {
21759   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21760   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21761
21762   switch (loc->dw_loc_opc)
21763     {
21764     case DW_OP_const4u:
21765     case DW_OP_const8u:
21766       if (loc->dtprel)
21767         goto hash_addr;
21768       /* FALLTHRU */
21769     case DW_OP_const1u:
21770     case DW_OP_const1s:
21771     case DW_OP_const2u:
21772     case DW_OP_const2s:
21773     case DW_OP_const4s:
21774     case DW_OP_const8s:
21775     case DW_OP_constu:
21776     case DW_OP_consts:
21777     case DW_OP_pick:
21778     case DW_OP_plus_uconst:
21779     case DW_OP_breg0:
21780     case DW_OP_breg1:
21781     case DW_OP_breg2:
21782     case DW_OP_breg3:
21783     case DW_OP_breg4:
21784     case DW_OP_breg5:
21785     case DW_OP_breg6:
21786     case DW_OP_breg7:
21787     case DW_OP_breg8:
21788     case DW_OP_breg9:
21789     case DW_OP_breg10:
21790     case DW_OP_breg11:
21791     case DW_OP_breg12:
21792     case DW_OP_breg13:
21793     case DW_OP_breg14:
21794     case DW_OP_breg15:
21795     case DW_OP_breg16:
21796     case DW_OP_breg17:
21797     case DW_OP_breg18:
21798     case DW_OP_breg19:
21799     case DW_OP_breg20:
21800     case DW_OP_breg21:
21801     case DW_OP_breg22:
21802     case DW_OP_breg23:
21803     case DW_OP_breg24:
21804     case DW_OP_breg25:
21805     case DW_OP_breg26:
21806     case DW_OP_breg27:
21807     case DW_OP_breg28:
21808     case DW_OP_breg29:
21809     case DW_OP_breg30:
21810     case DW_OP_breg31:
21811     case DW_OP_regx:
21812     case DW_OP_fbreg:
21813     case DW_OP_piece:
21814     case DW_OP_deref_size:
21815     case DW_OP_xderef_size:
21816       hash = iterative_hash_object (val1->v.val_int, hash);
21817       break;
21818     case DW_OP_skip:
21819     case DW_OP_bra:
21820       {
21821         int offset;
21822
21823         gcc_assert (val1->val_class == dw_val_class_loc);
21824         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21825         hash = iterative_hash_object (offset, hash);
21826       }
21827       break;
21828     case DW_OP_implicit_value:
21829       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21830       switch (val2->val_class)
21831         {
21832         case dw_val_class_const:
21833           hash = iterative_hash_object (val2->v.val_int, hash);
21834           break;
21835         case dw_val_class_vec:
21836           {
21837             unsigned int elt_size = val2->v.val_vec.elt_size;
21838             unsigned int len = val2->v.val_vec.length;
21839
21840             hash = iterative_hash_object (elt_size, hash);
21841             hash = iterative_hash_object (len, hash);
21842             hash = iterative_hash (val2->v.val_vec.array,
21843                                    len * elt_size, hash);
21844           }
21845           break;
21846         case dw_val_class_const_double:
21847           hash = iterative_hash_object (val2->v.val_double.low, hash);
21848           hash = iterative_hash_object (val2->v.val_double.high, hash);
21849           break;
21850         case dw_val_class_addr:
21851           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21852           break;
21853         default:
21854           gcc_unreachable ();
21855         }
21856       break;
21857     case DW_OP_bregx:
21858     case DW_OP_bit_piece:
21859       hash = iterative_hash_object (val1->v.val_int, hash);
21860       hash = iterative_hash_object (val2->v.val_int, hash);
21861       break;
21862     case DW_OP_addr:
21863     hash_addr:
21864       if (loc->dtprel)
21865         {
21866           unsigned char dtprel = 0xd1;
21867           hash = iterative_hash_object (dtprel, hash);
21868         }
21869       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21870       break;
21871     case DW_OP_GNU_implicit_pointer:
21872       hash = iterative_hash_object (val2->v.val_int, hash);
21873       break;
21874     case DW_OP_GNU_entry_value:
21875       hash = hash_loc_operands (val1->v.val_loc, hash);
21876       break;
21877     case DW_OP_GNU_regval_type:
21878     case DW_OP_GNU_deref_type:
21879       {
21880         unsigned int byte_size
21881           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21882         unsigned int encoding
21883           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21884         hash = iterative_hash_object (val1->v.val_int, hash);
21885         hash = iterative_hash_object (byte_size, hash);
21886         hash = iterative_hash_object (encoding, hash);
21887       }
21888       break;
21889     case DW_OP_GNU_convert:
21890     case DW_OP_GNU_reinterpret:
21891       if (val1->val_class == dw_val_class_unsigned_const)
21892         {
21893           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21894           break;
21895         }
21896       /* FALLTHRU */
21897     case DW_OP_GNU_const_type:
21898       {
21899         unsigned int byte_size
21900           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21901         unsigned int encoding
21902           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21903         hash = iterative_hash_object (byte_size, hash);
21904         hash = iterative_hash_object (encoding, hash);
21905         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21906           break;
21907         hash = iterative_hash_object (val2->val_class, hash);
21908         switch (val2->val_class)
21909           {
21910           case dw_val_class_const:
21911             hash = iterative_hash_object (val2->v.val_int, hash);
21912             break;
21913           case dw_val_class_vec:
21914             {
21915               unsigned int elt_size = val2->v.val_vec.elt_size;
21916               unsigned int len = val2->v.val_vec.length;
21917
21918               hash = iterative_hash_object (elt_size, hash);
21919               hash = iterative_hash_object (len, hash);
21920               hash = iterative_hash (val2->v.val_vec.array,
21921                                      len * elt_size, hash);
21922             }
21923             break;
21924           case dw_val_class_const_double:
21925             hash = iterative_hash_object (val2->v.val_double.low, hash);
21926             hash = iterative_hash_object (val2->v.val_double.high, hash);
21927             break;
21928           default:
21929             gcc_unreachable ();
21930           }
21931       }
21932       break;
21933
21934     default:
21935       /* Other codes have no operands.  */
21936       break;
21937     }
21938   return hash;
21939 }
21940
21941 /* Iteratively hash the whole DWARF location expression LOC.  */
21942
21943 static inline hashval_t
21944 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21945 {
21946   dw_loc_descr_ref l;
21947   bool sizes_computed = false;
21948   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
21949   size_of_locs (loc);
21950
21951   for (l = loc; l != NULL; l = l->dw_loc_next)
21952     {
21953       enum dwarf_location_atom opc = l->dw_loc_opc;
21954       hash = iterative_hash_object (opc, hash);
21955       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21956         {
21957           size_of_locs (loc);
21958           sizes_computed = true;
21959         }
21960       hash = hash_loc_operands (l, hash);
21961     }
21962   return hash;
21963 }
21964
21965 /* Compute hash of the whole location list LIST_HEAD.  */
21966
21967 static inline void
21968 hash_loc_list (dw_loc_list_ref list_head)
21969 {
21970   dw_loc_list_ref curr = list_head;
21971   hashval_t hash = 0;
21972
21973   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
21974     {
21975       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
21976       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
21977       if (curr->section)
21978         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
21979                                hash);
21980       hash = hash_locs (curr->expr, hash);
21981     }
21982   list_head->hash = hash;
21983 }
21984
21985 /* Return true if X and Y opcodes have the same operands.  */
21986
21987 static inline bool
21988 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
21989 {
21990   dw_val_ref valx1 = &x->dw_loc_oprnd1;
21991   dw_val_ref valx2 = &x->dw_loc_oprnd2;
21992   dw_val_ref valy1 = &y->dw_loc_oprnd1;
21993   dw_val_ref valy2 = &y->dw_loc_oprnd2;
21994
21995   switch (x->dw_loc_opc)
21996     {
21997     case DW_OP_const4u:
21998     case DW_OP_const8u:
21999       if (x->dtprel)
22000         goto hash_addr;
22001       /* FALLTHRU */
22002     case DW_OP_const1u:
22003     case DW_OP_const1s:
22004     case DW_OP_const2u:
22005     case DW_OP_const2s:
22006     case DW_OP_const4s:
22007     case DW_OP_const8s:
22008     case DW_OP_constu:
22009     case DW_OP_consts:
22010     case DW_OP_pick:
22011     case DW_OP_plus_uconst:
22012     case DW_OP_breg0:
22013     case DW_OP_breg1:
22014     case DW_OP_breg2:
22015     case DW_OP_breg3:
22016     case DW_OP_breg4:
22017     case DW_OP_breg5:
22018     case DW_OP_breg6:
22019     case DW_OP_breg7:
22020     case DW_OP_breg8:
22021     case DW_OP_breg9:
22022     case DW_OP_breg10:
22023     case DW_OP_breg11:
22024     case DW_OP_breg12:
22025     case DW_OP_breg13:
22026     case DW_OP_breg14:
22027     case DW_OP_breg15:
22028     case DW_OP_breg16:
22029     case DW_OP_breg17:
22030     case DW_OP_breg18:
22031     case DW_OP_breg19:
22032     case DW_OP_breg20:
22033     case DW_OP_breg21:
22034     case DW_OP_breg22:
22035     case DW_OP_breg23:
22036     case DW_OP_breg24:
22037     case DW_OP_breg25:
22038     case DW_OP_breg26:
22039     case DW_OP_breg27:
22040     case DW_OP_breg28:
22041     case DW_OP_breg29:
22042     case DW_OP_breg30:
22043     case DW_OP_breg31:
22044     case DW_OP_regx:
22045     case DW_OP_fbreg:
22046     case DW_OP_piece:
22047     case DW_OP_deref_size:
22048     case DW_OP_xderef_size:
22049       return valx1->v.val_int == valy1->v.val_int;
22050     case DW_OP_skip:
22051     case DW_OP_bra:
22052       gcc_assert (valx1->val_class == dw_val_class_loc
22053                   && valy1->val_class == dw_val_class_loc
22054                   && x->dw_loc_addr == y->dw_loc_addr);
22055       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22056     case DW_OP_implicit_value:
22057       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22058           || valx2->val_class != valy2->val_class)
22059         return false;
22060       switch (valx2->val_class)
22061         {
22062         case dw_val_class_const:
22063           return valx2->v.val_int == valy2->v.val_int;
22064         case dw_val_class_vec:
22065           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22066                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22067                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22068                             valx2->v.val_vec.elt_size
22069                             * valx2->v.val_vec.length) == 0;
22070         case dw_val_class_const_double:
22071           return valx2->v.val_double.low == valy2->v.val_double.low
22072                  && valx2->v.val_double.high == valy2->v.val_double.high;
22073         case dw_val_class_addr:
22074           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22075         default:
22076           gcc_unreachable ();
22077         }
22078     case DW_OP_bregx:
22079     case DW_OP_bit_piece:
22080       return valx1->v.val_int == valy1->v.val_int
22081              && valx2->v.val_int == valy2->v.val_int;
22082     case DW_OP_addr:
22083     hash_addr:
22084       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22085     case DW_OP_GNU_implicit_pointer:
22086       return valx1->val_class == dw_val_class_die_ref
22087              && valx1->val_class == valy1->val_class
22088              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22089              && valx2->v.val_int == valy2->v.val_int;
22090     case DW_OP_GNU_entry_value:
22091       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22092     case DW_OP_GNU_const_type:
22093       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22094           || valx2->val_class != valy2->val_class)
22095         return false;
22096       switch (valx2->val_class)
22097         {
22098         case dw_val_class_const:
22099           return valx2->v.val_int == valy2->v.val_int;
22100         case dw_val_class_vec:
22101           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22102                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22103                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22104                             valx2->v.val_vec.elt_size
22105                             * valx2->v.val_vec.length) == 0;
22106         case dw_val_class_const_double:
22107           return valx2->v.val_double.low == valy2->v.val_double.low
22108                  && valx2->v.val_double.high == valy2->v.val_double.high;
22109         default:
22110           gcc_unreachable ();
22111         }
22112     case DW_OP_GNU_regval_type:
22113     case DW_OP_GNU_deref_type:
22114       return valx1->v.val_int == valy1->v.val_int
22115              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22116     case DW_OP_GNU_convert:
22117     case DW_OP_GNU_reinterpret:
22118       if (valx1->val_class != valy1->val_class)
22119         return false;
22120       if (valx1->val_class == dw_val_class_unsigned_const)
22121         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22122       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22123     case DW_OP_GNU_parameter_ref:
22124       return valx1->val_class == dw_val_class_die_ref
22125              && valx1->val_class == valy1->val_class
22126              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22127     default:
22128       /* Other codes have no operands.  */
22129       return true;
22130     }
22131 }
22132
22133 /* Return true if DWARF location expressions X and Y are the same.  */
22134
22135 static inline bool
22136 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22137 {
22138   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22139     if (x->dw_loc_opc != y->dw_loc_opc
22140         || x->dtprel != y->dtprel
22141         || !compare_loc_operands (x, y))
22142       break;
22143   return x == NULL && y == NULL;
22144 }
22145
22146 /* Return precomputed hash of location list X.  */
22147
22148 static hashval_t
22149 loc_list_hash (const void *x)
22150 {
22151   return ((const struct dw_loc_list_struct *) x)->hash;
22152 }
22153
22154 /* Return 1 if location lists X and Y are the same.  */
22155
22156 static int
22157 loc_list_eq (const void *x, const void *y)
22158 {
22159   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22160   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22161   if (a == b)
22162     return 1;
22163   if (a->hash != b->hash)
22164     return 0;
22165   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22166     if (strcmp (a->begin, b->begin) != 0
22167         || strcmp (a->end, b->end) != 0
22168         || (a->section == NULL) != (b->section == NULL)
22169         || (a->section && strcmp (a->section, b->section) != 0)
22170         || !compare_locs (a->expr, b->expr))
22171       break;
22172   return a == NULL && b == NULL;
22173 }
22174
22175 /* Recursively optimize location lists referenced from DIE
22176    children and share them whenever possible.  */
22177
22178 static void
22179 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22180 {
22181   dw_die_ref c;
22182   dw_attr_ref a;
22183   unsigned ix;
22184   void **slot;
22185
22186   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22187     if (AT_class (a) == dw_val_class_loc_list)
22188       {
22189         dw_loc_list_ref list = AT_loc_list (a);
22190         /* TODO: perform some optimizations here, before hashing
22191            it and storing into the hash table.  */
22192         hash_loc_list (list);
22193         slot = htab_find_slot_with_hash (htab, list, list->hash,
22194                                          INSERT);
22195         if (*slot == NULL)
22196           *slot = (void *) list;
22197         else
22198           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22199       }
22200
22201   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22202 }
22203
22204 /* Optimize location lists referenced from DIE
22205    children and share them whenever possible.  */
22206
22207 static void
22208 optimize_location_lists (dw_die_ref die)
22209 {
22210   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22211   optimize_location_lists_1 (die, htab);
22212   htab_delete (htab);
22213 }
22214 \f
22215 /* Output stuff that dwarf requires at the end of every file,
22216    and generate the DWARF-2 debugging info.  */
22217
22218 static void
22219 dwarf2out_finish (const char *filename)
22220 {
22221   limbo_die_node *node, *next_node;
22222   comdat_type_node *ctnode;
22223   htab_t comdat_type_table;
22224   unsigned int i;
22225
22226   /* PCH might result in DW_AT_producer string being restored from the
22227      header compilation, fix it up if needed.  */
22228   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
22229   if (strcmp (AT_string (producer), producer_string) != 0)
22230     {
22231       struct indirect_string_node *node = find_AT_string (producer_string);
22232       producer->dw_attr_val.v.val_str = node;
22233     }
22234
22235   gen_scheduled_generic_parms_dies ();
22236   gen_remaining_tmpl_value_param_die_attribute ();
22237
22238   /* Add the name for the main input file now.  We delayed this from
22239      dwarf2out_init to avoid complications with PCH.  */
22240   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22241   if (!IS_ABSOLUTE_PATH (filename))
22242     add_comp_dir_attribute (comp_unit_die ());
22243   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22244     {
22245       bool p = false;
22246       htab_traverse (file_table, file_table_relative_p, &p);
22247       if (p)
22248         add_comp_dir_attribute (comp_unit_die ());
22249     }
22250
22251   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22252     {
22253       add_location_or_const_value_attribute (
22254         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22255         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22256         false,
22257         DW_AT_location);
22258     }
22259
22260   /* Traverse the limbo die list, and add parent/child links.  The only
22261      dies without parents that should be here are concrete instances of
22262      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22263      For concrete instances, we can get the parent die from the abstract
22264      instance.  */
22265   for (node = limbo_die_list; node; node = next_node)
22266     {
22267       dw_die_ref die = node->die;
22268       next_node = node->next;
22269
22270       if (die->die_parent == NULL)
22271         {
22272           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22273
22274           if (origin && origin->die_parent)
22275             add_child_die (origin->die_parent, die);
22276           else if (is_cu_die (die))
22277             ;
22278           else if (seen_error ())
22279             /* It's OK to be confused by errors in the input.  */
22280             add_child_die (comp_unit_die (), die);
22281           else
22282             {
22283               /* In certain situations, the lexical block containing a
22284                  nested function can be optimized away, which results
22285                  in the nested function die being orphaned.  Likewise
22286                  with the return type of that nested function.  Force
22287                  this to be a child of the containing function.
22288
22289                  It may happen that even the containing function got fully
22290                  inlined and optimized out.  In that case we are lost and
22291                  assign the empty child.  This should not be big issue as
22292                  the function is likely unreachable too.  */
22293               tree context = NULL_TREE;
22294
22295               gcc_assert (node->created_for);
22296
22297               if (DECL_P (node->created_for))
22298                 context = DECL_CONTEXT (node->created_for);
22299               else if (TYPE_P (node->created_for))
22300                 context = TYPE_CONTEXT (node->created_for);
22301
22302               gcc_assert (context
22303                           && (TREE_CODE (context) == FUNCTION_DECL
22304                               || TREE_CODE (context) == NAMESPACE_DECL));
22305
22306               origin = lookup_decl_die (context);
22307               if (origin)
22308                 add_child_die (origin, die);
22309               else
22310                 add_child_die (comp_unit_die (), die);
22311             }
22312         }
22313     }
22314
22315   limbo_die_list = NULL;
22316
22317 #if ENABLE_ASSERT_CHECKING
22318   {
22319     dw_die_ref die = comp_unit_die (), c;
22320     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22321   }
22322 #endif
22323   resolve_addr (comp_unit_die ());
22324   move_marked_base_types ();
22325
22326   for (node = deferred_asm_name; node; node = node->next)
22327     {
22328       tree decl = node->created_for;
22329       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22330         {
22331           add_linkage_attr (node->die, decl);
22332           move_linkage_attr (node->die);
22333         }
22334     }
22335
22336   deferred_asm_name = NULL;
22337
22338   /* Walk through the list of incomplete types again, trying once more to
22339      emit full debugging info for them.  */
22340   retry_incomplete_types ();
22341
22342   if (flag_eliminate_unused_debug_types)
22343     prune_unused_types ();
22344
22345   /* Generate separate CUs for each of the include files we've seen.
22346      They will go into limbo_die_list.  */
22347   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22348     break_out_includes (comp_unit_die ());
22349
22350   /* Generate separate COMDAT sections for type DIEs. */
22351   if (use_debug_types)
22352     {
22353       break_out_comdat_types (comp_unit_die ());
22354
22355       /* Each new type_unit DIE was added to the limbo die list when created.
22356          Since these have all been added to comdat_type_list, clear the
22357          limbo die list.  */
22358       limbo_die_list = NULL;
22359
22360       /* For each new comdat type unit, copy declarations for incomplete
22361          types to make the new unit self-contained (i.e., no direct
22362          references to the main compile unit).  */
22363       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22364         copy_decls_for_unworthy_types (ctnode->root_die);
22365       copy_decls_for_unworthy_types (comp_unit_die ());
22366
22367       /* In the process of copying declarations from one unit to another,
22368          we may have left some declarations behind that are no longer
22369          referenced.  Prune them.  */
22370       prune_unused_types ();
22371     }
22372
22373   /* Traverse the DIE's and add add sibling attributes to those DIE's
22374      that have children.  */
22375   add_sibling_attributes (comp_unit_die ());
22376   for (node = limbo_die_list; node; node = node->next)
22377     add_sibling_attributes (node->die);
22378   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22379     add_sibling_attributes (ctnode->root_die);
22380
22381   /* Output a terminator label for the .text section.  */
22382   switch_to_section (text_section);
22383   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22384   if (cold_text_section)
22385     {
22386       switch_to_section (cold_text_section);
22387       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22388     }
22389
22390   /* We can only use the low/high_pc attributes if all of the code was
22391      in .text.  */
22392   if (!have_multiple_function_sections 
22393       || (dwarf_version < 3 && dwarf_strict))
22394     {
22395       /* Don't add if the CU has no associated code.  */
22396       if (text_section_used)
22397         {
22398           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22399           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22400         }
22401     }
22402   else
22403     {
22404       unsigned fde_idx;
22405       dw_fde_ref fde;
22406       bool range_list_added = false;
22407
22408       if (text_section_used)
22409         add_ranges_by_labels (comp_unit_die (), text_section_label,
22410                               text_end_label, &range_list_added);
22411       if (cold_text_section_used)
22412         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22413                               cold_end_label, &range_list_added);
22414
22415       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22416         {
22417           if (!fde->in_std_section)
22418             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22419                                   fde->dw_fde_end, &range_list_added);
22420           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22421             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22422                                   fde->dw_fde_second_end, &range_list_added);
22423         }
22424
22425       if (range_list_added)
22426         {
22427           /* We need to give .debug_loc and .debug_ranges an appropriate
22428              "base address".  Use zero so that these addresses become
22429              absolute.  Historically, we've emitted the unexpected
22430              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22431              Emit both to give time for other tools to adapt.  */
22432           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22433           if (! dwarf_strict && dwarf_version < 4)
22434             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22435
22436           add_ranges (NULL);
22437         }
22438     }
22439
22440   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22441     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22442                     debug_line_section_label);
22443
22444   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22445     add_AT_macptr (comp_unit_die (),
22446                    dwarf_strict ? DW_AT_macro_info : DW_AT_GNU_macros,
22447                    macinfo_section_label);
22448
22449   if (have_location_lists)
22450     optimize_location_lists (comp_unit_die ());
22451
22452   /* Output all of the compilation units.  We put the main one last so that
22453      the offsets are available to output_pubnames.  */
22454   for (node = limbo_die_list; node; node = node->next)
22455     output_comp_unit (node->die, 0);
22456
22457   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22458   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22459     {
22460       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22461
22462       /* Don't output duplicate types.  */
22463       if (*slot != HTAB_EMPTY_ENTRY)
22464         continue;
22465
22466       /* Add a pointer to the line table for the main compilation unit
22467          so that the debugger can make sense of DW_AT_decl_file
22468          attributes.  */
22469       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22470         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22471                         debug_line_section_label);
22472
22473       output_comdat_type_unit (ctnode);
22474       *slot = ctnode;
22475     }
22476   htab_delete (comdat_type_table);
22477
22478   /* Output the main compilation unit if non-empty or if .debug_macinfo
22479      will be emitted.  */
22480   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22481
22482   /* Output the abbreviation table.  */
22483   if (abbrev_die_table_in_use != 1)
22484     {
22485       switch_to_section (debug_abbrev_section);
22486       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22487       output_abbrev_section ();
22488     }
22489
22490   /* Output location list section if necessary.  */
22491   if (have_location_lists)
22492     {
22493       /* Output the location lists info.  */
22494       switch_to_section (debug_loc_section);
22495       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22496                                    DEBUG_LOC_SECTION_LABEL, 0);
22497       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22498       output_location_lists (comp_unit_die ());
22499     }
22500
22501   /* Output public names table if necessary.  */
22502   if (!VEC_empty (pubname_entry, pubname_table))
22503     {
22504       gcc_assert (info_section_emitted);
22505       switch_to_section (debug_pubnames_section);
22506       output_pubnames (pubname_table);
22507     }
22508
22509   /* Output public types table if necessary.  */
22510   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22511      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22512      simply won't look for the section.  */
22513   if (!VEC_empty (pubname_entry, pubtype_table))
22514     {
22515       bool empty = false;
22516       
22517       if (flag_eliminate_unused_debug_types)
22518         {
22519           /* The pubtypes table might be emptied by pruning unused items.  */
22520           unsigned i;
22521           pubname_ref p;
22522           empty = true;
22523           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22524             if (p->die->die_offset != 0)
22525               {
22526                 empty = false;
22527                 break;
22528               }
22529         }
22530       if (!empty)
22531         {
22532           gcc_assert (info_section_emitted);
22533           switch_to_section (debug_pubtypes_section);
22534           output_pubnames (pubtype_table);
22535         }
22536     }
22537
22538   /* Output the address range information if a CU (.debug_info section)
22539      was emitted.  We output an empty table even if we had no functions
22540      to put in it.  This because the consumer has no way to tell the
22541      difference between an empty table that we omitted and failure to
22542      generate a table that would have contained data.  */
22543   if (info_section_emitted)
22544     {
22545       unsigned long aranges_length = size_of_aranges ();
22546
22547       switch_to_section (debug_aranges_section);
22548       output_aranges (aranges_length);
22549     }
22550
22551   /* Output ranges section if necessary.  */
22552   if (ranges_table_in_use)
22553     {
22554       switch_to_section (debug_ranges_section);
22555       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22556       output_ranges ();
22557     }
22558
22559   /* Output the source line correspondence table.  We must do this
22560      even if there is no line information.  Otherwise, on an empty
22561      translation unit, we will generate a present, but empty,
22562      .debug_info section.  IRIX 6.5 `nm' will then complain when
22563      examining the file.  This is done late so that any filenames
22564      used by the debug_info section are marked as 'used'.  */
22565   switch_to_section (debug_line_section);
22566   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22567   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22568     output_line_info ();
22569
22570   /* Have to end the macro section.  */
22571   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22572     {
22573       switch_to_section (debug_macinfo_section);
22574       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22575       if (!VEC_empty (macinfo_entry, macinfo_table))
22576         output_macinfo ();
22577       dw2_asm_output_data (1, 0, "End compilation unit");
22578     }
22579
22580   /* If we emitted any DW_FORM_strp form attribute, output the string
22581      table too.  */
22582   if (debug_str_hash)
22583     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22584 }
22585
22586 #include "gt-dwarf2out.h"