OSDN Git Service

PR other/32998
[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 HOST_WIDE_INT 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_LINE_SECTION
3422 #define DEBUG_LINE_SECTION      ".debug_line"
3423 #endif
3424 #ifndef DEBUG_LOC_SECTION
3425 #define DEBUG_LOC_SECTION       ".debug_loc"
3426 #endif
3427 #ifndef DEBUG_PUBNAMES_SECTION
3428 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
3429 #endif
3430 #ifndef DEBUG_PUBTYPES_SECTION
3431 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
3432 #endif
3433 #ifndef DEBUG_STR_SECTION
3434 #define DEBUG_STR_SECTION       ".debug_str"
3435 #endif
3436 #ifndef DEBUG_RANGES_SECTION
3437 #define DEBUG_RANGES_SECTION    ".debug_ranges"
3438 #endif
3439
3440 /* Standard ELF section names for compiled code and data.  */
3441 #ifndef TEXT_SECTION_NAME
3442 #define TEXT_SECTION_NAME       ".text"
3443 #endif
3444
3445 /* Section flags for .debug_str section.  */
3446 #define DEBUG_STR_SECTION_FLAGS \
3447   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
3448    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
3449    : SECTION_DEBUG)
3450
3451 /* Labels we insert at beginning sections we can reference instead of
3452    the section names themselves.  */
3453
3454 #ifndef TEXT_SECTION_LABEL
3455 #define TEXT_SECTION_LABEL              "Ltext"
3456 #endif
3457 #ifndef COLD_TEXT_SECTION_LABEL
3458 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
3459 #endif
3460 #ifndef DEBUG_LINE_SECTION_LABEL
3461 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
3462 #endif
3463 #ifndef DEBUG_INFO_SECTION_LABEL
3464 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
3465 #endif
3466 #ifndef DEBUG_ABBREV_SECTION_LABEL
3467 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
3468 #endif
3469 #ifndef DEBUG_LOC_SECTION_LABEL
3470 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
3471 #endif
3472 #ifndef DEBUG_RANGES_SECTION_LABEL
3473 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
3474 #endif
3475 #ifndef DEBUG_MACINFO_SECTION_LABEL
3476 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
3477 #endif
3478
3479
3480 /* Definitions of defaults for formats and names of various special
3481    (artificial) labels which may be generated within this file (when the -g
3482    options is used and DWARF2_DEBUGGING_INFO is in effect.
3483    If necessary, these may be overridden from within the tm.h file, but
3484    typically, overriding these defaults is unnecessary.  */
3485
3486 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3487 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3488 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3489 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
3490 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3491 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3492 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3493 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3494 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
3495 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
3496
3497 #ifndef TEXT_END_LABEL
3498 #define TEXT_END_LABEL          "Letext"
3499 #endif
3500 #ifndef COLD_END_LABEL
3501 #define COLD_END_LABEL          "Letext_cold"
3502 #endif
3503 #ifndef BLOCK_BEGIN_LABEL
3504 #define BLOCK_BEGIN_LABEL       "LBB"
3505 #endif
3506 #ifndef BLOCK_END_LABEL
3507 #define BLOCK_END_LABEL         "LBE"
3508 #endif
3509 #ifndef LINE_CODE_LABEL
3510 #define LINE_CODE_LABEL         "LM"
3511 #endif
3512
3513 \f
3514 /* Return the root of the DIE's built for the current compilation unit.  */
3515 static dw_die_ref
3516 comp_unit_die (void)
3517 {
3518   if (!single_comp_unit_die)
3519     single_comp_unit_die = gen_compile_unit_die (NULL);
3520   return single_comp_unit_die;
3521 }
3522
3523 /* We allow a language front-end to designate a function that is to be
3524    called to "demangle" any name before it is put into a DIE.  */
3525
3526 static const char *(*demangle_name_func) (const char *);
3527
3528 void
3529 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
3530 {
3531   demangle_name_func = func;
3532 }
3533
3534 /* Test if rtl node points to a pseudo register.  */
3535
3536 static inline int
3537 is_pseudo_reg (const_rtx rtl)
3538 {
3539   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
3540           || (GET_CODE (rtl) == SUBREG
3541               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
3542 }
3543
3544 /* Return a reference to a type, with its const and volatile qualifiers
3545    removed.  */
3546
3547 static inline tree
3548 type_main_variant (tree type)
3549 {
3550   type = TYPE_MAIN_VARIANT (type);
3551
3552   /* ??? There really should be only one main variant among any group of
3553      variants of a given type (and all of the MAIN_VARIANT values for all
3554      members of the group should point to that one type) but sometimes the C
3555      front-end messes this up for array types, so we work around that bug
3556      here.  */
3557   if (TREE_CODE (type) == ARRAY_TYPE)
3558     while (type != TYPE_MAIN_VARIANT (type))
3559       type = TYPE_MAIN_VARIANT (type);
3560
3561   return type;
3562 }
3563
3564 /* Return nonzero if the given type node represents a tagged type.  */
3565
3566 static inline int
3567 is_tagged_type (const_tree type)
3568 {
3569   enum tree_code code = TREE_CODE (type);
3570
3571   return (code == RECORD_TYPE || code == UNION_TYPE
3572           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
3573 }
3574
3575 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
3576
3577 static void
3578 get_ref_die_offset_label (char *label, dw_die_ref ref)
3579 {
3580   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
3581 }
3582
3583 /* Return die_offset of a DIE reference to a base type.  */
3584
3585 static unsigned long int
3586 get_base_type_offset (dw_die_ref ref)
3587 {
3588   if (ref->die_offset)
3589     return ref->die_offset;
3590   if (comp_unit_die ()->die_abbrev)
3591     {
3592       calc_base_type_die_sizes ();
3593       gcc_assert (ref->die_offset);
3594     }
3595   return ref->die_offset;
3596 }
3597
3598 /* Return die_offset of a DIE reference other than base type.  */
3599
3600 static unsigned long int
3601 get_ref_die_offset (dw_die_ref ref)
3602 {
3603   gcc_assert (ref->die_offset);
3604   return ref->die_offset;
3605 }
3606
3607 /* Convert a DIE tag into its string name.  */
3608
3609 static const char *
3610 dwarf_tag_name (unsigned int tag)
3611 {
3612   switch (tag)
3613     {
3614     case DW_TAG_padding:
3615       return "DW_TAG_padding";
3616     case DW_TAG_array_type:
3617       return "DW_TAG_array_type";
3618     case DW_TAG_class_type:
3619       return "DW_TAG_class_type";
3620     case DW_TAG_entry_point:
3621       return "DW_TAG_entry_point";
3622     case DW_TAG_enumeration_type:
3623       return "DW_TAG_enumeration_type";
3624     case DW_TAG_formal_parameter:
3625       return "DW_TAG_formal_parameter";
3626     case DW_TAG_imported_declaration:
3627       return "DW_TAG_imported_declaration";
3628     case DW_TAG_label:
3629       return "DW_TAG_label";
3630     case DW_TAG_lexical_block:
3631       return "DW_TAG_lexical_block";
3632     case DW_TAG_member:
3633       return "DW_TAG_member";
3634     case DW_TAG_pointer_type:
3635       return "DW_TAG_pointer_type";
3636     case DW_TAG_reference_type:
3637       return "DW_TAG_reference_type";
3638     case DW_TAG_compile_unit:
3639       return "DW_TAG_compile_unit";
3640     case DW_TAG_string_type:
3641       return "DW_TAG_string_type";
3642     case DW_TAG_structure_type:
3643       return "DW_TAG_structure_type";
3644     case DW_TAG_subroutine_type:
3645       return "DW_TAG_subroutine_type";
3646     case DW_TAG_typedef:
3647       return "DW_TAG_typedef";
3648     case DW_TAG_union_type:
3649       return "DW_TAG_union_type";
3650     case DW_TAG_unspecified_parameters:
3651       return "DW_TAG_unspecified_parameters";
3652     case DW_TAG_variant:
3653       return "DW_TAG_variant";
3654     case DW_TAG_common_block:
3655       return "DW_TAG_common_block";
3656     case DW_TAG_common_inclusion:
3657       return "DW_TAG_common_inclusion";
3658     case DW_TAG_inheritance:
3659       return "DW_TAG_inheritance";
3660     case DW_TAG_inlined_subroutine:
3661       return "DW_TAG_inlined_subroutine";
3662     case DW_TAG_module:
3663       return "DW_TAG_module";
3664     case DW_TAG_ptr_to_member_type:
3665       return "DW_TAG_ptr_to_member_type";
3666     case DW_TAG_set_type:
3667       return "DW_TAG_set_type";
3668     case DW_TAG_subrange_type:
3669       return "DW_TAG_subrange_type";
3670     case DW_TAG_with_stmt:
3671       return "DW_TAG_with_stmt";
3672     case DW_TAG_access_declaration:
3673       return "DW_TAG_access_declaration";
3674     case DW_TAG_base_type:
3675       return "DW_TAG_base_type";
3676     case DW_TAG_catch_block:
3677       return "DW_TAG_catch_block";
3678     case DW_TAG_const_type:
3679       return "DW_TAG_const_type";
3680     case DW_TAG_constant:
3681       return "DW_TAG_constant";
3682     case DW_TAG_enumerator:
3683       return "DW_TAG_enumerator";
3684     case DW_TAG_file_type:
3685       return "DW_TAG_file_type";
3686     case DW_TAG_friend:
3687       return "DW_TAG_friend";
3688     case DW_TAG_namelist:
3689       return "DW_TAG_namelist";
3690     case DW_TAG_namelist_item:
3691       return "DW_TAG_namelist_item";
3692     case DW_TAG_packed_type:
3693       return "DW_TAG_packed_type";
3694     case DW_TAG_subprogram:
3695       return "DW_TAG_subprogram";
3696     case DW_TAG_template_type_param:
3697       return "DW_TAG_template_type_param";
3698     case DW_TAG_template_value_param:
3699       return "DW_TAG_template_value_param";
3700     case DW_TAG_thrown_type:
3701       return "DW_TAG_thrown_type";
3702     case DW_TAG_try_block:
3703       return "DW_TAG_try_block";
3704     case DW_TAG_variant_part:
3705       return "DW_TAG_variant_part";
3706     case DW_TAG_variable:
3707       return "DW_TAG_variable";
3708     case DW_TAG_volatile_type:
3709       return "DW_TAG_volatile_type";
3710     case DW_TAG_dwarf_procedure:
3711       return "DW_TAG_dwarf_procedure";
3712     case DW_TAG_restrict_type:
3713       return "DW_TAG_restrict_type";
3714     case DW_TAG_interface_type:
3715       return "DW_TAG_interface_type";
3716     case DW_TAG_namespace:
3717       return "DW_TAG_namespace";
3718     case DW_TAG_imported_module:
3719       return "DW_TAG_imported_module";
3720     case DW_TAG_unspecified_type:
3721       return "DW_TAG_unspecified_type";
3722     case DW_TAG_partial_unit:
3723       return "DW_TAG_partial_unit";
3724     case DW_TAG_imported_unit:
3725       return "DW_TAG_imported_unit";
3726     case DW_TAG_condition:
3727       return "DW_TAG_condition";
3728     case DW_TAG_shared_type:
3729       return "DW_TAG_shared_type";
3730     case DW_TAG_type_unit:
3731       return "DW_TAG_type_unit";
3732     case DW_TAG_rvalue_reference_type:
3733       return "DW_TAG_rvalue_reference_type";
3734     case DW_TAG_template_alias:
3735       return "DW_TAG_template_alias";
3736     case DW_TAG_GNU_template_parameter_pack:
3737       return "DW_TAG_GNU_template_parameter_pack";
3738     case DW_TAG_GNU_formal_parameter_pack:
3739       return "DW_TAG_GNU_formal_parameter_pack";
3740     case DW_TAG_MIPS_loop:
3741       return "DW_TAG_MIPS_loop";
3742     case DW_TAG_format_label:
3743       return "DW_TAG_format_label";
3744     case DW_TAG_function_template:
3745       return "DW_TAG_function_template";
3746     case DW_TAG_class_template:
3747       return "DW_TAG_class_template";
3748     case DW_TAG_GNU_BINCL:
3749       return "DW_TAG_GNU_BINCL";
3750     case DW_TAG_GNU_EINCL:
3751       return "DW_TAG_GNU_EINCL";
3752     case DW_TAG_GNU_template_template_param:
3753       return "DW_TAG_GNU_template_template_param";
3754     case DW_TAG_GNU_call_site:
3755       return "DW_TAG_GNU_call_site";
3756     case DW_TAG_GNU_call_site_parameter:
3757       return "DW_TAG_GNU_call_site_parameter";
3758     default:
3759       return "DW_TAG_<unknown>";
3760     }
3761 }
3762
3763 /* Convert a DWARF attribute code into its string name.  */
3764
3765 static const char *
3766 dwarf_attr_name (unsigned int attr)
3767 {
3768   switch (attr)
3769     {
3770     case DW_AT_sibling:
3771       return "DW_AT_sibling";
3772     case DW_AT_location:
3773       return "DW_AT_location";
3774     case DW_AT_name:
3775       return "DW_AT_name";
3776     case DW_AT_ordering:
3777       return "DW_AT_ordering";
3778     case DW_AT_subscr_data:
3779       return "DW_AT_subscr_data";
3780     case DW_AT_byte_size:
3781       return "DW_AT_byte_size";
3782     case DW_AT_bit_offset:
3783       return "DW_AT_bit_offset";
3784     case DW_AT_bit_size:
3785       return "DW_AT_bit_size";
3786     case DW_AT_element_list:
3787       return "DW_AT_element_list";
3788     case DW_AT_stmt_list:
3789       return "DW_AT_stmt_list";
3790     case DW_AT_low_pc:
3791       return "DW_AT_low_pc";
3792     case DW_AT_high_pc:
3793       return "DW_AT_high_pc";
3794     case DW_AT_language:
3795       return "DW_AT_language";
3796     case DW_AT_member:
3797       return "DW_AT_member";
3798     case DW_AT_discr:
3799       return "DW_AT_discr";
3800     case DW_AT_discr_value:
3801       return "DW_AT_discr_value";
3802     case DW_AT_visibility:
3803       return "DW_AT_visibility";
3804     case DW_AT_import:
3805       return "DW_AT_import";
3806     case DW_AT_string_length:
3807       return "DW_AT_string_length";
3808     case DW_AT_common_reference:
3809       return "DW_AT_common_reference";
3810     case DW_AT_comp_dir:
3811       return "DW_AT_comp_dir";
3812     case DW_AT_const_value:
3813       return "DW_AT_const_value";
3814     case DW_AT_containing_type:
3815       return "DW_AT_containing_type";
3816     case DW_AT_default_value:
3817       return "DW_AT_default_value";
3818     case DW_AT_inline:
3819       return "DW_AT_inline";
3820     case DW_AT_is_optional:
3821       return "DW_AT_is_optional";
3822     case DW_AT_lower_bound:
3823       return "DW_AT_lower_bound";
3824     case DW_AT_producer:
3825       return "DW_AT_producer";
3826     case DW_AT_prototyped:
3827       return "DW_AT_prototyped";
3828     case DW_AT_return_addr:
3829       return "DW_AT_return_addr";
3830     case DW_AT_start_scope:
3831       return "DW_AT_start_scope";
3832     case DW_AT_bit_stride:
3833       return "DW_AT_bit_stride";
3834     case DW_AT_upper_bound:
3835       return "DW_AT_upper_bound";
3836     case DW_AT_abstract_origin:
3837       return "DW_AT_abstract_origin";
3838     case DW_AT_accessibility:
3839       return "DW_AT_accessibility";
3840     case DW_AT_address_class:
3841       return "DW_AT_address_class";
3842     case DW_AT_artificial:
3843       return "DW_AT_artificial";
3844     case DW_AT_base_types:
3845       return "DW_AT_base_types";
3846     case DW_AT_calling_convention:
3847       return "DW_AT_calling_convention";
3848     case DW_AT_count:
3849       return "DW_AT_count";
3850     case DW_AT_data_member_location:
3851       return "DW_AT_data_member_location";
3852     case DW_AT_decl_column:
3853       return "DW_AT_decl_column";
3854     case DW_AT_decl_file:
3855       return "DW_AT_decl_file";
3856     case DW_AT_decl_line:
3857       return "DW_AT_decl_line";
3858     case DW_AT_declaration:
3859       return "DW_AT_declaration";
3860     case DW_AT_discr_list:
3861       return "DW_AT_discr_list";
3862     case DW_AT_encoding:
3863       return "DW_AT_encoding";
3864     case DW_AT_external:
3865       return "DW_AT_external";
3866     case DW_AT_explicit:
3867       return "DW_AT_explicit";
3868     case DW_AT_frame_base:
3869       return "DW_AT_frame_base";
3870     case DW_AT_friend:
3871       return "DW_AT_friend";
3872     case DW_AT_identifier_case:
3873       return "DW_AT_identifier_case";
3874     case DW_AT_macro_info:
3875       return "DW_AT_macro_info";
3876     case DW_AT_namelist_items:
3877       return "DW_AT_namelist_items";
3878     case DW_AT_priority:
3879       return "DW_AT_priority";
3880     case DW_AT_segment:
3881       return "DW_AT_segment";
3882     case DW_AT_specification:
3883       return "DW_AT_specification";
3884     case DW_AT_static_link:
3885       return "DW_AT_static_link";
3886     case DW_AT_type:
3887       return "DW_AT_type";
3888     case DW_AT_use_location:
3889       return "DW_AT_use_location";
3890     case DW_AT_variable_parameter:
3891       return "DW_AT_variable_parameter";
3892     case DW_AT_virtuality:
3893       return "DW_AT_virtuality";
3894     case DW_AT_vtable_elem_location:
3895       return "DW_AT_vtable_elem_location";
3896
3897     case DW_AT_allocated:
3898       return "DW_AT_allocated";
3899     case DW_AT_associated:
3900       return "DW_AT_associated";
3901     case DW_AT_data_location:
3902       return "DW_AT_data_location";
3903     case DW_AT_byte_stride:
3904       return "DW_AT_byte_stride";
3905     case DW_AT_entry_pc:
3906       return "DW_AT_entry_pc";
3907     case DW_AT_use_UTF8:
3908       return "DW_AT_use_UTF8";
3909     case DW_AT_extension:
3910       return "DW_AT_extension";
3911     case DW_AT_ranges:
3912       return "DW_AT_ranges";
3913     case DW_AT_trampoline:
3914       return "DW_AT_trampoline";
3915     case DW_AT_call_column:
3916       return "DW_AT_call_column";
3917     case DW_AT_call_file:
3918       return "DW_AT_call_file";
3919     case DW_AT_call_line:
3920       return "DW_AT_call_line";
3921     case DW_AT_object_pointer:
3922       return "DW_AT_object_pointer";
3923
3924     case DW_AT_signature:
3925       return "DW_AT_signature";
3926     case DW_AT_main_subprogram:
3927       return "DW_AT_main_subprogram";
3928     case DW_AT_data_bit_offset:
3929       return "DW_AT_data_bit_offset";
3930     case DW_AT_const_expr:
3931       return "DW_AT_const_expr";
3932     case DW_AT_enum_class:
3933       return "DW_AT_enum_class";
3934     case DW_AT_linkage_name:
3935       return "DW_AT_linkage_name";
3936
3937     case DW_AT_MIPS_fde:
3938       return "DW_AT_MIPS_fde";
3939     case DW_AT_MIPS_loop_begin:
3940       return "DW_AT_MIPS_loop_begin";
3941     case DW_AT_MIPS_tail_loop_begin:
3942       return "DW_AT_MIPS_tail_loop_begin";
3943     case DW_AT_MIPS_epilog_begin:
3944       return "DW_AT_MIPS_epilog_begin";
3945 #if VMS_DEBUGGING_INFO
3946     case DW_AT_HP_prologue:
3947       return "DW_AT_HP_prologue";
3948 #else
3949     case DW_AT_MIPS_loop_unroll_factor:
3950       return "DW_AT_MIPS_loop_unroll_factor";
3951 #endif
3952     case DW_AT_MIPS_software_pipeline_depth:
3953       return "DW_AT_MIPS_software_pipeline_depth";
3954     case DW_AT_MIPS_linkage_name:
3955       return "DW_AT_MIPS_linkage_name";
3956 #if VMS_DEBUGGING_INFO
3957     case DW_AT_HP_epilogue:
3958       return "DW_AT_HP_epilogue";
3959 #else
3960     case DW_AT_MIPS_stride:
3961       return "DW_AT_MIPS_stride";
3962 #endif
3963     case DW_AT_MIPS_abstract_name:
3964       return "DW_AT_MIPS_abstract_name";
3965     case DW_AT_MIPS_clone_origin:
3966       return "DW_AT_MIPS_clone_origin";
3967     case DW_AT_MIPS_has_inlines:
3968       return "DW_AT_MIPS_has_inlines";
3969
3970     case DW_AT_sf_names:
3971       return "DW_AT_sf_names";
3972     case DW_AT_src_info:
3973       return "DW_AT_src_info";
3974     case DW_AT_mac_info:
3975       return "DW_AT_mac_info";
3976     case DW_AT_src_coords:
3977       return "DW_AT_src_coords";
3978     case DW_AT_body_begin:
3979       return "DW_AT_body_begin";
3980     case DW_AT_body_end:
3981       return "DW_AT_body_end";
3982
3983     case DW_AT_GNU_vector:
3984       return "DW_AT_GNU_vector";
3985     case DW_AT_GNU_guarded_by:
3986       return "DW_AT_GNU_guarded_by";
3987     case DW_AT_GNU_pt_guarded_by:
3988       return "DW_AT_GNU_pt_guarded_by";
3989     case DW_AT_GNU_guarded:
3990       return "DW_AT_GNU_guarded";
3991     case DW_AT_GNU_pt_guarded:
3992       return "DW_AT_GNU_pt_guarded";
3993     case DW_AT_GNU_locks_excluded:
3994       return "DW_AT_GNU_locks_excluded";
3995     case DW_AT_GNU_exclusive_locks_required:
3996       return "DW_AT_GNU_exclusive_locks_required";
3997     case DW_AT_GNU_shared_locks_required:
3998       return "DW_AT_GNU_shared_locks_required";
3999     case DW_AT_GNU_odr_signature:
4000       return "DW_AT_GNU_odr_signature";
4001     case DW_AT_GNU_template_name:
4002       return "DW_AT_GNU_template_name";
4003     case DW_AT_GNU_call_site_value:
4004       return "DW_AT_GNU_call_site_value";
4005     case DW_AT_GNU_call_site_data_value:
4006       return "DW_AT_GNU_call_site_data_value";
4007     case DW_AT_GNU_call_site_target:
4008       return "DW_AT_GNU_call_site_target";
4009     case DW_AT_GNU_call_site_target_clobbered:
4010       return "DW_AT_GNU_call_site_target_clobbered";
4011     case DW_AT_GNU_tail_call:
4012       return "DW_AT_GNU_tail_call";
4013     case DW_AT_GNU_all_tail_call_sites:
4014       return "DW_AT_GNU_all_tail_call_sites";
4015     case DW_AT_GNU_all_call_sites:
4016       return "DW_AT_GNU_all_call_sites";
4017     case DW_AT_GNU_all_source_call_sites:
4018       return "DW_AT_GNU_all_source_call_sites";
4019
4020     case DW_AT_GNAT_descriptive_type:
4021       return "DW_AT_GNAT_descriptive_type";
4022
4023     case DW_AT_VMS_rtnbeg_pd_address:
4024       return "DW_AT_VMS_rtnbeg_pd_address";
4025
4026     default:
4027       return "DW_AT_<unknown>";
4028     }
4029 }
4030
4031 /* Convert a DWARF value form code into its string name.  */
4032
4033 static const char *
4034 dwarf_form_name (unsigned int form)
4035 {
4036   switch (form)
4037     {
4038     case DW_FORM_addr:
4039       return "DW_FORM_addr";
4040     case DW_FORM_block2:
4041       return "DW_FORM_block2";
4042     case DW_FORM_block4:
4043       return "DW_FORM_block4";
4044     case DW_FORM_data2:
4045       return "DW_FORM_data2";
4046     case DW_FORM_data4:
4047       return "DW_FORM_data4";
4048     case DW_FORM_data8:
4049       return "DW_FORM_data8";
4050     case DW_FORM_string:
4051       return "DW_FORM_string";
4052     case DW_FORM_block:
4053       return "DW_FORM_block";
4054     case DW_FORM_block1:
4055       return "DW_FORM_block1";
4056     case DW_FORM_data1:
4057       return "DW_FORM_data1";
4058     case DW_FORM_flag:
4059       return "DW_FORM_flag";
4060     case DW_FORM_sdata:
4061       return "DW_FORM_sdata";
4062     case DW_FORM_strp:
4063       return "DW_FORM_strp";
4064     case DW_FORM_udata:
4065       return "DW_FORM_udata";
4066     case DW_FORM_ref_addr:
4067       return "DW_FORM_ref_addr";
4068     case DW_FORM_ref1:
4069       return "DW_FORM_ref1";
4070     case DW_FORM_ref2:
4071       return "DW_FORM_ref2";
4072     case DW_FORM_ref4:
4073       return "DW_FORM_ref4";
4074     case DW_FORM_ref8:
4075       return "DW_FORM_ref8";
4076     case DW_FORM_ref_udata:
4077       return "DW_FORM_ref_udata";
4078     case DW_FORM_indirect:
4079       return "DW_FORM_indirect";
4080     case DW_FORM_sec_offset:
4081       return "DW_FORM_sec_offset";
4082     case DW_FORM_exprloc:
4083       return "DW_FORM_exprloc";
4084     case DW_FORM_flag_present:
4085       return "DW_FORM_flag_present";
4086     case DW_FORM_ref_sig8:
4087       return "DW_FORM_ref_sig8";
4088     default:
4089       return "DW_FORM_<unknown>";
4090     }
4091 }
4092 \f
4093 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4094    instance of an inlined instance of a decl which is local to an inline
4095    function, so we have to trace all of the way back through the origin chain
4096    to find out what sort of node actually served as the original seed for the
4097    given block.  */
4098
4099 static tree
4100 decl_ultimate_origin (const_tree decl)
4101 {
4102   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4103     return NULL_TREE;
4104
4105   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4106      nodes in the function to point to themselves; ignore that if
4107      we're trying to output the abstract instance of this function.  */
4108   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4109     return NULL_TREE;
4110
4111   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4112      most distant ancestor, this should never happen.  */
4113   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4114
4115   return DECL_ABSTRACT_ORIGIN (decl);
4116 }
4117
4118 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4119    of a virtual function may refer to a base class, so we check the 'this'
4120    parameter.  */
4121
4122 static tree
4123 decl_class_context (tree decl)
4124 {
4125   tree context = NULL_TREE;
4126
4127   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4128     context = DECL_CONTEXT (decl);
4129   else
4130     context = TYPE_MAIN_VARIANT
4131       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4132
4133   if (context && !TYPE_P (context))
4134     context = NULL_TREE;
4135
4136   return context;
4137 }
4138 \f
4139 /* Add an attribute/value pair to a DIE.  */
4140
4141 static inline void
4142 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4143 {
4144   /* Maybe this should be an assert?  */
4145   if (die == NULL)
4146     return;
4147
4148   if (die->die_attr == NULL)
4149     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4150   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4151 }
4152
4153 static inline enum dw_val_class
4154 AT_class (dw_attr_ref a)
4155 {
4156   return a->dw_attr_val.val_class;
4157 }
4158
4159 /* Add a flag value attribute to a DIE.  */
4160
4161 static inline void
4162 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4163 {
4164   dw_attr_node attr;
4165
4166   attr.dw_attr = attr_kind;
4167   attr.dw_attr_val.val_class = dw_val_class_flag;
4168   attr.dw_attr_val.v.val_flag = flag;
4169   add_dwarf_attr (die, &attr);
4170 }
4171
4172 static inline unsigned
4173 AT_flag (dw_attr_ref a)
4174 {
4175   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4176   return a->dw_attr_val.v.val_flag;
4177 }
4178
4179 /* Add a signed integer attribute value to a DIE.  */
4180
4181 static inline void
4182 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4183 {
4184   dw_attr_node attr;
4185
4186   attr.dw_attr = attr_kind;
4187   attr.dw_attr_val.val_class = dw_val_class_const;
4188   attr.dw_attr_val.v.val_int = int_val;
4189   add_dwarf_attr (die, &attr);
4190 }
4191
4192 static inline HOST_WIDE_INT
4193 AT_int (dw_attr_ref a)
4194 {
4195   gcc_assert (a && AT_class (a) == dw_val_class_const);
4196   return a->dw_attr_val.v.val_int;
4197 }
4198
4199 /* Add an unsigned integer attribute value to a DIE.  */
4200
4201 static inline void
4202 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4203                  unsigned HOST_WIDE_INT unsigned_val)
4204 {
4205   dw_attr_node attr;
4206
4207   attr.dw_attr = attr_kind;
4208   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4209   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4210   add_dwarf_attr (die, &attr);
4211 }
4212
4213 static inline unsigned HOST_WIDE_INT
4214 AT_unsigned (dw_attr_ref a)
4215 {
4216   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4217   return a->dw_attr_val.v.val_unsigned;
4218 }
4219
4220 /* Add an unsigned double integer attribute value to a DIE.  */
4221
4222 static inline void
4223 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4224                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4225 {
4226   dw_attr_node attr;
4227
4228   attr.dw_attr = attr_kind;
4229   attr.dw_attr_val.val_class = dw_val_class_const_double;
4230   attr.dw_attr_val.v.val_double.high = high;
4231   attr.dw_attr_val.v.val_double.low = low;
4232   add_dwarf_attr (die, &attr);
4233 }
4234
4235 /* Add a floating point attribute value to a DIE and return it.  */
4236
4237 static inline void
4238 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4239             unsigned int length, unsigned int elt_size, unsigned char *array)
4240 {
4241   dw_attr_node attr;
4242
4243   attr.dw_attr = attr_kind;
4244   attr.dw_attr_val.val_class = dw_val_class_vec;
4245   attr.dw_attr_val.v.val_vec.length = length;
4246   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4247   attr.dw_attr_val.v.val_vec.array = array;
4248   add_dwarf_attr (die, &attr);
4249 }
4250
4251 /* Add an 8-byte data attribute value to a DIE.  */
4252
4253 static inline void
4254 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4255               unsigned char data8[8])
4256 {
4257   dw_attr_node attr;
4258
4259   attr.dw_attr = attr_kind;
4260   attr.dw_attr_val.val_class = dw_val_class_data8;
4261   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4262   add_dwarf_attr (die, &attr);
4263 }
4264
4265 /* Hash and equality functions for debug_str_hash.  */
4266
4267 static hashval_t
4268 debug_str_do_hash (const void *x)
4269 {
4270   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4271 }
4272
4273 static int
4274 debug_str_eq (const void *x1, const void *x2)
4275 {
4276   return strcmp ((((const struct indirect_string_node *)x1)->str),
4277                  (const char *)x2) == 0;
4278 }
4279
4280 /* Add STR to the indirect string hash table.  */
4281
4282 static struct indirect_string_node *
4283 find_AT_string (const char *str)
4284 {
4285   struct indirect_string_node *node;
4286   void **slot;
4287
4288   if (! debug_str_hash)
4289     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4290                                       debug_str_eq, NULL);
4291
4292   slot = htab_find_slot_with_hash (debug_str_hash, str,
4293                                    htab_hash_string (str), INSERT);
4294   if (*slot == NULL)
4295     {
4296       node = ggc_alloc_cleared_indirect_string_node ();
4297       node->str = ggc_strdup (str);
4298       *slot = node;
4299     }
4300   else
4301     node = (struct indirect_string_node *) *slot;
4302
4303   node->refcount++;
4304   return node;
4305 }
4306
4307 /* Add a string attribute value to a DIE.  */
4308
4309 static inline void
4310 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4311 {
4312   dw_attr_node attr;
4313   struct indirect_string_node *node;
4314
4315   node = find_AT_string (str);
4316
4317   attr.dw_attr = attr_kind;
4318   attr.dw_attr_val.val_class = dw_val_class_str;
4319   attr.dw_attr_val.v.val_str = node;
4320   add_dwarf_attr (die, &attr);
4321 }
4322
4323 static inline const char *
4324 AT_string (dw_attr_ref a)
4325 {
4326   gcc_assert (a && AT_class (a) == dw_val_class_str);
4327   return a->dw_attr_val.v.val_str->str;
4328 }
4329
4330 /* Find out whether a string should be output inline in DIE
4331    or out-of-line in .debug_str section.  */
4332
4333 static enum dwarf_form
4334 AT_string_form (dw_attr_ref a)
4335 {
4336   struct indirect_string_node *node;
4337   unsigned int len;
4338   char label[32];
4339
4340   gcc_assert (a && AT_class (a) == dw_val_class_str);
4341
4342   node = a->dw_attr_val.v.val_str;
4343   if (node->form)
4344     return node->form;
4345
4346   len = strlen (node->str) + 1;
4347
4348   /* If the string is shorter or equal to the size of the reference, it is
4349      always better to put it inline.  */
4350   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
4351     return node->form = DW_FORM_string;
4352
4353   /* If we cannot expect the linker to merge strings in .debug_str
4354      section, only put it into .debug_str if it is worth even in this
4355      single module.  */
4356   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
4357       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
4358       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
4359     return node->form = DW_FORM_string;
4360
4361   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
4362   ++dw2_string_counter;
4363   node->label = xstrdup (label);
4364
4365   return node->form = DW_FORM_strp;
4366 }
4367
4368 /* Add a DIE reference attribute value to a DIE.  */
4369
4370 static inline void
4371 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
4372 {
4373   dw_attr_node attr;
4374
4375 #ifdef ENABLE_CHECKING
4376   gcc_assert (targ_die != NULL);
4377 #else
4378   /* With LTO we can end up trying to reference something we didn't create
4379      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
4380   if (targ_die == NULL)
4381     return;
4382 #endif
4383
4384   attr.dw_attr = attr_kind;
4385   attr.dw_attr_val.val_class = dw_val_class_die_ref;
4386   attr.dw_attr_val.v.val_die_ref.die = targ_die;
4387   attr.dw_attr_val.v.val_die_ref.external = 0;
4388   add_dwarf_attr (die, &attr);
4389 }
4390
4391 /* Add an AT_specification attribute to a DIE, and also make the back
4392    pointer from the specification to the definition.  */
4393
4394 static inline void
4395 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
4396 {
4397   add_AT_die_ref (die, DW_AT_specification, targ_die);
4398   gcc_assert (!targ_die->die_definition);
4399   targ_die->die_definition = die;
4400 }
4401
4402 static inline dw_die_ref
4403 AT_ref (dw_attr_ref a)
4404 {
4405   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4406   return a->dw_attr_val.v.val_die_ref.die;
4407 }
4408
4409 static inline int
4410 AT_ref_external (dw_attr_ref a)
4411 {
4412   if (a && AT_class (a) == dw_val_class_die_ref)
4413     return a->dw_attr_val.v.val_die_ref.external;
4414
4415   return 0;
4416 }
4417
4418 static inline void
4419 set_AT_ref_external (dw_attr_ref a, int i)
4420 {
4421   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
4422   a->dw_attr_val.v.val_die_ref.external = i;
4423 }
4424
4425 /* Add an FDE reference attribute value to a DIE.  */
4426
4427 static inline void
4428 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
4429 {
4430   dw_attr_node attr;
4431
4432   attr.dw_attr = attr_kind;
4433   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
4434   attr.dw_attr_val.v.val_fde_index = targ_fde;
4435   add_dwarf_attr (die, &attr);
4436 }
4437
4438 /* Add a location description attribute value to a DIE.  */
4439
4440 static inline void
4441 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
4442 {
4443   dw_attr_node attr;
4444
4445   attr.dw_attr = attr_kind;
4446   attr.dw_attr_val.val_class = dw_val_class_loc;
4447   attr.dw_attr_val.v.val_loc = loc;
4448   add_dwarf_attr (die, &attr);
4449 }
4450
4451 static inline dw_loc_descr_ref
4452 AT_loc (dw_attr_ref a)
4453 {
4454   gcc_assert (a && AT_class (a) == dw_val_class_loc);
4455   return a->dw_attr_val.v.val_loc;
4456 }
4457
4458 static inline void
4459 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
4460 {
4461   dw_attr_node attr;
4462
4463   attr.dw_attr = attr_kind;
4464   attr.dw_attr_val.val_class = dw_val_class_loc_list;
4465   attr.dw_attr_val.v.val_loc_list = loc_list;
4466   add_dwarf_attr (die, &attr);
4467   have_location_lists = true;
4468 }
4469
4470 static inline dw_loc_list_ref
4471 AT_loc_list (dw_attr_ref a)
4472 {
4473   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4474   return a->dw_attr_val.v.val_loc_list;
4475 }
4476
4477 static inline dw_loc_list_ref *
4478 AT_loc_list_ptr (dw_attr_ref a)
4479 {
4480   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
4481   return &a->dw_attr_val.v.val_loc_list;
4482 }
4483
4484 /* Add an address constant attribute value to a DIE.  */
4485
4486 static inline void
4487 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
4488 {
4489   dw_attr_node attr;
4490
4491   attr.dw_attr = attr_kind;
4492   attr.dw_attr_val.val_class = dw_val_class_addr;
4493   attr.dw_attr_val.v.val_addr = addr;
4494   add_dwarf_attr (die, &attr);
4495 }
4496
4497 /* Get the RTX from to an address DIE attribute.  */
4498
4499 static inline rtx
4500 AT_addr (dw_attr_ref a)
4501 {
4502   gcc_assert (a && AT_class (a) == dw_val_class_addr);
4503   return a->dw_attr_val.v.val_addr;
4504 }
4505
4506 /* Add a file attribute value to a DIE.  */
4507
4508 static inline void
4509 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
4510              struct dwarf_file_data *fd)
4511 {
4512   dw_attr_node attr;
4513
4514   attr.dw_attr = attr_kind;
4515   attr.dw_attr_val.val_class = dw_val_class_file;
4516   attr.dw_attr_val.v.val_file = fd;
4517   add_dwarf_attr (die, &attr);
4518 }
4519
4520 /* Get the dwarf_file_data from a file DIE attribute.  */
4521
4522 static inline struct dwarf_file_data *
4523 AT_file (dw_attr_ref a)
4524 {
4525   gcc_assert (a && AT_class (a) == dw_val_class_file);
4526   return a->dw_attr_val.v.val_file;
4527 }
4528
4529 /* Add a vms delta attribute value to a DIE.  */
4530
4531 static inline void
4532 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
4533                   const char *lbl1, const char *lbl2)
4534 {
4535   dw_attr_node attr;
4536
4537   attr.dw_attr = attr_kind;
4538   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
4539   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
4540   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
4541   add_dwarf_attr (die, &attr);
4542 }
4543
4544 /* Add a label identifier attribute value to a DIE.  */
4545
4546 static inline void
4547 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
4548 {
4549   dw_attr_node attr;
4550
4551   attr.dw_attr = attr_kind;
4552   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
4553   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
4554   add_dwarf_attr (die, &attr);
4555 }
4556
4557 /* Add a section offset attribute value to a DIE, an offset into the
4558    debug_line section.  */
4559
4560 static inline void
4561 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4562                 const char *label)
4563 {
4564   dw_attr_node attr;
4565
4566   attr.dw_attr = attr_kind;
4567   attr.dw_attr_val.val_class = dw_val_class_lineptr;
4568   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4569   add_dwarf_attr (die, &attr);
4570 }
4571
4572 /* Add a section offset attribute value to a DIE, an offset into the
4573    debug_macinfo section.  */
4574
4575 static inline void
4576 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
4577                const char *label)
4578 {
4579   dw_attr_node attr;
4580
4581   attr.dw_attr = attr_kind;
4582   attr.dw_attr_val.val_class = dw_val_class_macptr;
4583   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
4584   add_dwarf_attr (die, &attr);
4585 }
4586
4587 /* Add an offset attribute value to a DIE.  */
4588
4589 static inline void
4590 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
4591                unsigned HOST_WIDE_INT offset)
4592 {
4593   dw_attr_node attr;
4594
4595   attr.dw_attr = attr_kind;
4596   attr.dw_attr_val.val_class = dw_val_class_offset;
4597   attr.dw_attr_val.v.val_offset = offset;
4598   add_dwarf_attr (die, &attr);
4599 }
4600
4601 /* Add an range_list attribute value to a DIE.  */
4602
4603 static void
4604 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
4605                    long unsigned int offset)
4606 {
4607   dw_attr_node attr;
4608
4609   attr.dw_attr = attr_kind;
4610   attr.dw_attr_val.val_class = dw_val_class_range_list;
4611   attr.dw_attr_val.v.val_offset = offset;
4612   add_dwarf_attr (die, &attr);
4613 }
4614
4615 /* Return the start label of a delta attribute.  */
4616
4617 static inline const char *
4618 AT_vms_delta1 (dw_attr_ref a)
4619 {
4620   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4621   return a->dw_attr_val.v.val_vms_delta.lbl1;
4622 }
4623
4624 /* Return the end label of a delta attribute.  */
4625
4626 static inline const char *
4627 AT_vms_delta2 (dw_attr_ref a)
4628 {
4629   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
4630   return a->dw_attr_val.v.val_vms_delta.lbl2;
4631 }
4632
4633 static inline const char *
4634 AT_lbl (dw_attr_ref a)
4635 {
4636   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
4637                     || AT_class (a) == dw_val_class_lineptr
4638                     || AT_class (a) == dw_val_class_macptr));
4639   return a->dw_attr_val.v.val_lbl_id;
4640 }
4641
4642 /* Get the attribute of type attr_kind.  */
4643
4644 static dw_attr_ref
4645 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4646 {
4647   dw_attr_ref a;
4648   unsigned ix;
4649   dw_die_ref spec = NULL;
4650
4651   if (! die)
4652     return NULL;
4653
4654   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4655     if (a->dw_attr == attr_kind)
4656       return a;
4657     else if (a->dw_attr == DW_AT_specification
4658              || a->dw_attr == DW_AT_abstract_origin)
4659       spec = AT_ref (a);
4660
4661   if (spec)
4662     return get_AT (spec, attr_kind);
4663
4664   return NULL;
4665 }
4666
4667 /* Return the "low pc" attribute value, typically associated with a subprogram
4668    DIE.  Return null if the "low pc" attribute is either not present, or if it
4669    cannot be represented as an assembler label identifier.  */
4670
4671 static inline const char *
4672 get_AT_low_pc (dw_die_ref die)
4673 {
4674   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
4675
4676   return a ? AT_lbl (a) : NULL;
4677 }
4678
4679 /* Return the "high pc" attribute value, typically associated with a subprogram
4680    DIE.  Return null if the "high pc" attribute is either not present, or if it
4681    cannot be represented as an assembler label identifier.  */
4682
4683 static inline const char *
4684 get_AT_hi_pc (dw_die_ref die)
4685 {
4686   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
4687
4688   return a ? AT_lbl (a) : NULL;
4689 }
4690
4691 /* Return the value of the string attribute designated by ATTR_KIND, or
4692    NULL if it is not present.  */
4693
4694 static inline const char *
4695 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
4696 {
4697   dw_attr_ref a = get_AT (die, attr_kind);
4698
4699   return a ? AT_string (a) : NULL;
4700 }
4701
4702 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
4703    if it is not present.  */
4704
4705 static inline int
4706 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
4707 {
4708   dw_attr_ref a = get_AT (die, attr_kind);
4709
4710   return a ? AT_flag (a) : 0;
4711 }
4712
4713 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
4714    if it is not present.  */
4715
4716 static inline unsigned
4717 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
4718 {
4719   dw_attr_ref a = get_AT (die, attr_kind);
4720
4721   return a ? AT_unsigned (a) : 0;
4722 }
4723
4724 static inline dw_die_ref
4725 get_AT_ref (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_ref (a) : NULL;
4730 }
4731
4732 static inline struct dwarf_file_data *
4733 get_AT_file (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_file (a) : NULL;
4738 }
4739
4740 /* Return TRUE if the language is C++.  */
4741
4742 static inline bool
4743 is_cxx (void)
4744 {
4745   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4746
4747   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
4748 }
4749
4750 /* Return TRUE if the language is Fortran.  */
4751
4752 static inline bool
4753 is_fortran (void)
4754 {
4755   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4756
4757   return (lang == DW_LANG_Fortran77
4758           || lang == DW_LANG_Fortran90
4759           || lang == DW_LANG_Fortran95);
4760 }
4761
4762 /* Return TRUE if the language is Ada.  */
4763
4764 static inline bool
4765 is_ada (void)
4766 {
4767   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
4768
4769   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
4770 }
4771
4772 /* Remove the specified attribute if present.  */
4773
4774 static void
4775 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
4776 {
4777   dw_attr_ref a;
4778   unsigned ix;
4779
4780   if (! die)
4781     return;
4782
4783   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
4784     if (a->dw_attr == attr_kind)
4785       {
4786         if (AT_class (a) == dw_val_class_str)
4787           if (a->dw_attr_val.v.val_str->refcount)
4788             a->dw_attr_val.v.val_str->refcount--;
4789
4790         /* VEC_ordered_remove should help reduce the number of abbrevs
4791            that are needed.  */
4792         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
4793         return;
4794       }
4795 }
4796
4797 /* Remove CHILD from its parent.  PREV must have the property that
4798    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
4799
4800 static void
4801 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
4802 {
4803   gcc_assert (child->die_parent == prev->die_parent);
4804   gcc_assert (prev->die_sib == child);
4805   if (prev == child)
4806     {
4807       gcc_assert (child->die_parent->die_child == child);
4808       prev = NULL;
4809     }
4810   else
4811     prev->die_sib = child->die_sib;
4812   if (child->die_parent->die_child == child)
4813     child->die_parent->die_child = prev;
4814 }
4815
4816 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
4817    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
4818
4819 static void
4820 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
4821 {
4822   dw_die_ref parent = old_child->die_parent;
4823
4824   gcc_assert (parent == prev->die_parent);
4825   gcc_assert (prev->die_sib == old_child);
4826
4827   new_child->die_parent = parent;
4828   if (prev == old_child)
4829     {
4830       gcc_assert (parent->die_child == old_child);
4831       new_child->die_sib = new_child;
4832     }
4833   else
4834     {
4835       prev->die_sib = new_child;
4836       new_child->die_sib = old_child->die_sib;
4837     }
4838   if (old_child->die_parent->die_child == old_child)
4839     old_child->die_parent->die_child = new_child;
4840 }
4841
4842 /* Move all children from OLD_PARENT to NEW_PARENT.  */
4843
4844 static void
4845 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
4846 {
4847   dw_die_ref c;
4848   new_parent->die_child = old_parent->die_child;
4849   old_parent->die_child = NULL;
4850   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
4851 }
4852
4853 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
4854    matches TAG.  */
4855
4856 static void
4857 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
4858 {
4859   dw_die_ref c;
4860
4861   c = die->die_child;
4862   if (c) do {
4863     dw_die_ref prev = c;
4864     c = c->die_sib;
4865     while (c->die_tag == tag)
4866       {
4867         remove_child_with_prev (c, prev);
4868         /* Might have removed every child.  */
4869         if (c == c->die_sib)
4870           return;
4871         c = c->die_sib;
4872       }
4873   } while (c != die->die_child);
4874 }
4875
4876 /* Add a CHILD_DIE as the last child of DIE.  */
4877
4878 static void
4879 add_child_die (dw_die_ref die, dw_die_ref child_die)
4880 {
4881   /* FIXME this should probably be an assert.  */
4882   if (! die || ! child_die)
4883     return;
4884   gcc_assert (die != child_die);
4885
4886   child_die->die_parent = die;
4887   if (die->die_child)
4888     {
4889       child_die->die_sib = die->die_child->die_sib;
4890       die->die_child->die_sib = child_die;
4891     }
4892   else
4893     child_die->die_sib = child_die;
4894   die->die_child = child_die;
4895 }
4896
4897 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
4898    is the specification, to the end of PARENT's list of children.
4899    This is done by removing and re-adding it.  */
4900
4901 static void
4902 splice_child_die (dw_die_ref parent, dw_die_ref child)
4903 {
4904   dw_die_ref p;
4905
4906   /* We want the declaration DIE from inside the class, not the
4907      specification DIE at toplevel.  */
4908   if (child->die_parent != parent)
4909     {
4910       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
4911
4912       if (tmp)
4913         child = tmp;
4914     }
4915
4916   gcc_assert (child->die_parent == parent
4917               || (child->die_parent
4918                   == get_AT_ref (parent, DW_AT_specification)));
4919
4920   for (p = child->die_parent->die_child; ; p = p->die_sib)
4921     if (p->die_sib == child)
4922       {
4923         remove_child_with_prev (child, p);
4924         break;
4925       }
4926
4927   add_child_die (parent, child);
4928 }
4929
4930 /* Return a pointer to a newly created DIE node.  */
4931
4932 static inline dw_die_ref
4933 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
4934 {
4935   dw_die_ref die = ggc_alloc_cleared_die_node ();
4936
4937   die->die_tag = tag_value;
4938
4939   if (parent_die != NULL)
4940     add_child_die (parent_die, die);
4941   else
4942     {
4943       limbo_die_node *limbo_node;
4944
4945       limbo_node = ggc_alloc_cleared_limbo_die_node ();
4946       limbo_node->die = die;
4947       limbo_node->created_for = t;
4948       limbo_node->next = limbo_die_list;
4949       limbo_die_list = limbo_node;
4950     }
4951
4952   return die;
4953 }
4954
4955 /* Return the DIE associated with the given type specifier.  */
4956
4957 static inline dw_die_ref
4958 lookup_type_die (tree type)
4959 {
4960   return TYPE_SYMTAB_DIE (type);
4961 }
4962
4963 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
4964    anonymous type named by the typedef TYPE_DIE, return the DIE of the
4965    anonymous type instead the one of the naming typedef.  */
4966
4967 static inline dw_die_ref
4968 strip_naming_typedef (tree type, dw_die_ref type_die)
4969 {
4970   if (type
4971       && TREE_CODE (type) == RECORD_TYPE
4972       && type_die
4973       && type_die->die_tag == DW_TAG_typedef
4974       && is_naming_typedef_decl (TYPE_NAME (type)))
4975     type_die = get_AT_ref (type_die, DW_AT_type);
4976   return type_die;
4977 }
4978
4979 /* Like lookup_type_die, but if type is an anonymous type named by a
4980    typedef[1], return the DIE of the anonymous type instead the one of
4981    the naming typedef.  This is because in gen_typedef_die, we did
4982    equate the anonymous struct named by the typedef with the DIE of
4983    the naming typedef. So by default, lookup_type_die on an anonymous
4984    struct yields the DIE of the naming typedef.
4985
4986    [1]: Read the comment of is_naming_typedef_decl to learn about what
4987    a naming typedef is.  */
4988
4989 static inline dw_die_ref
4990 lookup_type_die_strip_naming_typedef (tree type)
4991 {
4992   dw_die_ref die = lookup_type_die (type);
4993   return strip_naming_typedef (type, die);
4994 }
4995
4996 /* Equate a DIE to a given type specifier.  */
4997
4998 static inline void
4999 equate_type_number_to_die (tree type, dw_die_ref type_die)
5000 {
5001   TYPE_SYMTAB_DIE (type) = type_die;
5002 }
5003
5004 /* Returns a hash value for X (which really is a die_struct).  */
5005
5006 static hashval_t
5007 decl_die_table_hash (const void *x)
5008 {
5009   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5010 }
5011
5012 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5013
5014 static int
5015 decl_die_table_eq (const void *x, const void *y)
5016 {
5017   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5018 }
5019
5020 /* Return the DIE associated with a given declaration.  */
5021
5022 static inline dw_die_ref
5023 lookup_decl_die (tree decl)
5024 {
5025   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5026 }
5027
5028 /* Returns a hash value for X (which really is a var_loc_list).  */
5029
5030 static hashval_t
5031 decl_loc_table_hash (const void *x)
5032 {
5033   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5034 }
5035
5036 /* Return nonzero if decl_id of var_loc_list X is the same as
5037    UID of decl *Y.  */
5038
5039 static int
5040 decl_loc_table_eq (const void *x, const void *y)
5041 {
5042   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5043 }
5044
5045 /* Return the var_loc list associated with a given declaration.  */
5046
5047 static inline var_loc_list *
5048 lookup_decl_loc (const_tree decl)
5049 {
5050   if (!decl_loc_table)
5051     return NULL;
5052   return (var_loc_list *)
5053     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5054 }
5055
5056 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5057
5058 static hashval_t
5059 cached_dw_loc_list_table_hash (const void *x)
5060 {
5061   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5062 }
5063
5064 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5065    UID of decl *Y.  */
5066
5067 static int
5068 cached_dw_loc_list_table_eq (const void *x, const void *y)
5069 {
5070   return (((const cached_dw_loc_list *) x)->decl_id
5071           == DECL_UID ((const_tree) y));
5072 }
5073
5074 /* Equate a DIE to a particular declaration.  */
5075
5076 static void
5077 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5078 {
5079   unsigned int decl_id = DECL_UID (decl);
5080   void **slot;
5081
5082   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5083   *slot = decl_die;
5084   decl_die->decl_id = decl_id;
5085 }
5086
5087 /* Return how many bits covers PIECE EXPR_LIST.  */
5088
5089 static int
5090 decl_piece_bitsize (rtx piece)
5091 {
5092   int ret = (int) GET_MODE (piece);
5093   if (ret)
5094     return ret;
5095   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5096               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5097   return INTVAL (XEXP (XEXP (piece, 0), 0));
5098 }
5099
5100 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5101
5102 static rtx *
5103 decl_piece_varloc_ptr (rtx piece)
5104 {
5105   if ((int) GET_MODE (piece))
5106     return &XEXP (piece, 0);
5107   else
5108     return &XEXP (XEXP (piece, 0), 1);
5109 }
5110
5111 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5112    Next is the chain of following piece nodes.  */
5113
5114 static rtx
5115 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5116 {
5117   if (bitsize <= (int) MAX_MACHINE_MODE)
5118     return alloc_EXPR_LIST (bitsize, loc_note, next);
5119   else
5120     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5121                                                GEN_INT (bitsize),
5122                                                loc_note), next);
5123 }
5124
5125 /* Return rtx that should be stored into loc field for
5126    LOC_NOTE and BITPOS/BITSIZE.  */
5127
5128 static rtx
5129 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5130                       HOST_WIDE_INT bitsize)
5131 {
5132   if (bitsize != -1)
5133     {
5134       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5135       if (bitpos != 0)
5136         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5137     }
5138   return loc_note;
5139 }
5140
5141 /* This function either modifies location piece list *DEST in
5142    place (if SRC and INNER is NULL), or copies location piece list
5143    *SRC to *DEST while modifying it.  Location BITPOS is modified
5144    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5145    not copied and if needed some padding around it is added.
5146    When modifying in place, DEST should point to EXPR_LIST where
5147    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5148    to the start of the whole list and INNER points to the EXPR_LIST
5149    where earlier pieces cover PIECE_BITPOS bits.  */
5150
5151 static void
5152 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5153                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5154                    HOST_WIDE_INT bitsize, rtx loc_note)
5155 {
5156   int diff;
5157   bool copy = inner != NULL;
5158
5159   if (copy)
5160     {
5161       /* First copy all nodes preceeding the current bitpos.  */
5162       while (src != inner)
5163         {
5164           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5165                                    decl_piece_bitsize (*src), NULL_RTX);
5166           dest = &XEXP (*dest, 1);
5167           src = &XEXP (*src, 1);
5168         }
5169     }
5170   /* Add padding if needed.  */
5171   if (bitpos != piece_bitpos)
5172     {
5173       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5174                                copy ? NULL_RTX : *dest);
5175       dest = &XEXP (*dest, 1);
5176     }
5177   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5178     {
5179       gcc_assert (!copy);
5180       /* A piece with correct bitpos and bitsize already exist,
5181          just update the location for it and return.  */
5182       *decl_piece_varloc_ptr (*dest) = loc_note;
5183       return;
5184     }
5185   /* Add the piece that changed.  */
5186   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5187   dest = &XEXP (*dest, 1);
5188   /* Skip over pieces that overlap it.  */
5189   diff = bitpos - piece_bitpos + bitsize;
5190   if (!copy)
5191     src = dest;
5192   while (diff > 0 && *src)
5193     {
5194       rtx piece = *src;
5195       diff -= decl_piece_bitsize (piece);
5196       if (copy)
5197         src = &XEXP (piece, 1);
5198       else
5199         {
5200           *src = XEXP (piece, 1);
5201           free_EXPR_LIST_node (piece);
5202         }
5203     }
5204   /* Add padding if needed.  */
5205   if (diff < 0 && *src)
5206     {
5207       if (!copy)
5208         dest = src;
5209       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5210       dest = &XEXP (*dest, 1);
5211     }
5212   if (!copy)
5213     return;
5214   /* Finally copy all nodes following it.  */
5215   while (*src)
5216     {
5217       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5218                                decl_piece_bitsize (*src), NULL_RTX);
5219       dest = &XEXP (*dest, 1);
5220       src = &XEXP (*src, 1);
5221     }
5222 }
5223
5224 /* Add a variable location node to the linked list for DECL.  */
5225
5226 static struct var_loc_node *
5227 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5228 {
5229   unsigned int decl_id;
5230   var_loc_list *temp;
5231   void **slot;
5232   struct var_loc_node *loc = NULL;
5233   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5234
5235   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5236     {
5237       tree realdecl = DECL_DEBUG_EXPR (decl);
5238       if (realdecl && handled_component_p (realdecl))
5239         {
5240           HOST_WIDE_INT maxsize;
5241           tree innerdecl;
5242           innerdecl
5243             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5244           if (!DECL_P (innerdecl)
5245               || DECL_IGNORED_P (innerdecl)
5246               || TREE_STATIC (innerdecl)
5247               || bitsize <= 0
5248               || bitpos + bitsize > 256
5249               || bitsize != maxsize)
5250             return NULL;
5251           decl = innerdecl;
5252         }
5253     }
5254
5255   decl_id = DECL_UID (decl);
5256   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5257   if (*slot == NULL)
5258     {
5259       temp = ggc_alloc_cleared_var_loc_list ();
5260       temp->decl_id = decl_id;
5261       *slot = temp;
5262     }
5263   else
5264     temp = (var_loc_list *) *slot;
5265
5266   /* For PARM_DECLs try to keep around the original incoming value,
5267      even if that means we'll emit a zero-range .debug_loc entry.  */
5268   if (temp->last
5269       && temp->first == temp->last
5270       && TREE_CODE (decl) == PARM_DECL
5271       && GET_CODE (temp->first->loc) == NOTE
5272       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5273       && DECL_INCOMING_RTL (decl)
5274       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5275       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5276          == GET_CODE (DECL_INCOMING_RTL (decl))
5277       && prev_real_insn (temp->first->loc) == NULL_RTX
5278       && (bitsize != -1
5279           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5280                            NOTE_VAR_LOCATION_LOC (loc_note))
5281           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5282               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5283     {
5284       loc = ggc_alloc_cleared_var_loc_node ();
5285       temp->first->next = loc;
5286       temp->last = loc;
5287       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5288     }
5289   else if (temp->last)
5290     {
5291       struct var_loc_node *last = temp->last, *unused = NULL;
5292       rtx *piece_loc = NULL, last_loc_note;
5293       int piece_bitpos = 0;
5294       if (last->next)
5295         {
5296           last = last->next;
5297           gcc_assert (last->next == NULL);
5298         }
5299       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5300         {
5301           piece_loc = &last->loc;
5302           do
5303             {
5304               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5305               if (piece_bitpos + cur_bitsize > bitpos)
5306                 break;
5307               piece_bitpos += cur_bitsize;
5308               piece_loc = &XEXP (*piece_loc, 1);
5309             }
5310           while (*piece_loc);
5311         }
5312       /* TEMP->LAST here is either pointer to the last but one or
5313          last element in the chained list, LAST is pointer to the
5314          last element.  */
5315       if (label && strcmp (last->label, label) == 0)
5316         {
5317           /* For SRA optimized variables if there weren't any real
5318              insns since last note, just modify the last node.  */
5319           if (piece_loc != NULL)
5320             {
5321               adjust_piece_list (piece_loc, NULL, NULL,
5322                                  bitpos, piece_bitpos, bitsize, loc_note);
5323               return NULL;
5324             }
5325           /* If the last note doesn't cover any instructions, remove it.  */
5326           if (temp->last != last)
5327             {
5328               temp->last->next = NULL;
5329               unused = last;
5330               last = temp->last;
5331               gcc_assert (strcmp (last->label, label) != 0);
5332             }
5333           else
5334             {
5335               gcc_assert (temp->first == temp->last
5336                           || (temp->first->next == temp->last
5337                               && TREE_CODE (decl) == PARM_DECL));
5338               memset (temp->last, '\0', sizeof (*temp->last));
5339               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
5340               return temp->last;
5341             }
5342         }
5343       if (bitsize == -1 && NOTE_P (last->loc))
5344         last_loc_note = last->loc;
5345       else if (piece_loc != NULL
5346                && *piece_loc != NULL_RTX
5347                && piece_bitpos == bitpos
5348                && decl_piece_bitsize (*piece_loc) == bitsize)
5349         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
5350       else
5351         last_loc_note = NULL_RTX;
5352       /* If the current location is the same as the end of the list,
5353          and either both or neither of the locations is uninitialized,
5354          we have nothing to do.  */
5355       if (last_loc_note == NULL_RTX
5356           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
5357                             NOTE_VAR_LOCATION_LOC (loc_note)))
5358           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5359                != NOTE_VAR_LOCATION_STATUS (loc_note))
5360               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
5361                    == VAR_INIT_STATUS_UNINITIALIZED)
5362                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
5363                       == VAR_INIT_STATUS_UNINITIALIZED))))
5364         {
5365           /* Add LOC to the end of list and update LAST.  If the last
5366              element of the list has been removed above, reuse its
5367              memory for the new node, otherwise allocate a new one.  */
5368           if (unused)
5369             {
5370               loc = unused;
5371               memset (loc, '\0', sizeof (*loc));
5372             }
5373           else
5374             loc = ggc_alloc_cleared_var_loc_node ();
5375           if (bitsize == -1 || piece_loc == NULL)
5376             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5377           else
5378             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
5379                                bitpos, piece_bitpos, bitsize, loc_note);
5380           last->next = loc;
5381           /* Ensure TEMP->LAST will point either to the new last but one
5382              element of the chain, or to the last element in it.  */
5383           if (last != temp->last)
5384             temp->last = last;
5385         }
5386       else if (unused)
5387         ggc_free (unused);
5388     }
5389   else
5390     {
5391       loc = ggc_alloc_cleared_var_loc_node ();
5392       temp->first = loc;
5393       temp->last = loc;
5394       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5395     }
5396   return loc;
5397 }
5398 \f
5399 /* Keep track of the number of spaces used to indent the
5400    output of the debugging routines that print the structure of
5401    the DIE internal representation.  */
5402 static int print_indent;
5403
5404 /* Indent the line the number of spaces given by print_indent.  */
5405
5406 static inline void
5407 print_spaces (FILE *outfile)
5408 {
5409   fprintf (outfile, "%*s", print_indent, "");
5410 }
5411
5412 /* Print a type signature in hex.  */
5413
5414 static inline void
5415 print_signature (FILE *outfile, char *sig)
5416 {
5417   int i;
5418
5419   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
5420     fprintf (outfile, "%02x", sig[i] & 0xff);
5421 }
5422
5423 /* Print the information associated with a given DIE, and its children.
5424    This routine is a debugging aid only.  */
5425
5426 static void
5427 print_die (dw_die_ref die, FILE *outfile)
5428 {
5429   dw_attr_ref a;
5430   dw_die_ref c;
5431   unsigned ix;
5432
5433   print_spaces (outfile);
5434   fprintf (outfile, "DIE %4ld: %s (%p)\n",
5435            die->die_offset, dwarf_tag_name (die->die_tag),
5436            (void*) die);
5437   print_spaces (outfile);
5438   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5439   fprintf (outfile, " offset: %ld", die->die_offset);
5440   fprintf (outfile, " mark: %d\n", die->die_mark);
5441
5442   if (use_debug_types && die->die_id.die_type_node)
5443     {
5444       print_spaces (outfile);
5445       fprintf (outfile, "  signature: ");
5446       print_signature (outfile, die->die_id.die_type_node->signature);
5447       fprintf (outfile, "\n");
5448     }
5449
5450   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5451     {
5452       print_spaces (outfile);
5453       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5454
5455       switch (AT_class (a))
5456         {
5457         case dw_val_class_addr:
5458           fprintf (outfile, "address");
5459           break;
5460         case dw_val_class_offset:
5461           fprintf (outfile, "offset");
5462           break;
5463         case dw_val_class_loc:
5464           fprintf (outfile, "location descriptor");
5465           break;
5466         case dw_val_class_loc_list:
5467           fprintf (outfile, "location list -> label:%s",
5468                    AT_loc_list (a)->ll_symbol);
5469           break;
5470         case dw_val_class_range_list:
5471           fprintf (outfile, "range list");
5472           break;
5473         case dw_val_class_const:
5474           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5475           break;
5476         case dw_val_class_unsigned_const:
5477           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5478           break;
5479         case dw_val_class_const_double:
5480           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
5481                             HOST_WIDE_INT_PRINT_UNSIGNED")",
5482                    a->dw_attr_val.v.val_double.high,
5483                    a->dw_attr_val.v.val_double.low);
5484           break;
5485         case dw_val_class_vec:
5486           fprintf (outfile, "floating-point or vector constant");
5487           break;
5488         case dw_val_class_flag:
5489           fprintf (outfile, "%u", AT_flag (a));
5490           break;
5491         case dw_val_class_die_ref:
5492           if (AT_ref (a) != NULL)
5493             {
5494               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
5495                 {
5496                   fprintf (outfile, "die -> signature: ");
5497                   print_signature (outfile,
5498                                    AT_ref (a)->die_id.die_type_node->signature);
5499                 }
5500               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
5501                 fprintf (outfile, "die -> label: %s",
5502                          AT_ref (a)->die_id.die_symbol);
5503               else
5504                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5505               fprintf (outfile, " (%p)", (void *) AT_ref (a));
5506             }
5507           else
5508             fprintf (outfile, "die -> <null>");
5509           break;
5510         case dw_val_class_vms_delta:
5511           fprintf (outfile, "delta: @slotcount(%s-%s)",
5512                    AT_vms_delta2 (a), AT_vms_delta1 (a));
5513           break;
5514         case dw_val_class_lbl_id:
5515         case dw_val_class_lineptr:
5516         case dw_val_class_macptr:
5517           fprintf (outfile, "label: %s", AT_lbl (a));
5518           break;
5519         case dw_val_class_str:
5520           if (AT_string (a) != NULL)
5521             fprintf (outfile, "\"%s\"", AT_string (a));
5522           else
5523             fprintf (outfile, "<null>");
5524           break;
5525         case dw_val_class_file:
5526           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5527                    AT_file (a)->emitted_number);
5528           break;
5529         case dw_val_class_data8:
5530           {
5531             int i;
5532
5533             for (i = 0; i < 8; i++)
5534               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
5535             break;
5536           }
5537         default:
5538           break;
5539         }
5540
5541       fprintf (outfile, "\n");
5542     }
5543
5544   if (die->die_child != NULL)
5545     {
5546       print_indent += 4;
5547       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5548       print_indent -= 4;
5549     }
5550   if (print_indent == 0)
5551     fprintf (outfile, "\n");
5552 }
5553
5554 /* Print the information collected for a given DIE.  */
5555
5556 DEBUG_FUNCTION void
5557 debug_dwarf_die (dw_die_ref die)
5558 {
5559   print_die (die, stderr);
5560 }
5561
5562 /* Print all DWARF information collected for the compilation unit.
5563    This routine is a debugging aid only.  */
5564
5565 DEBUG_FUNCTION void
5566 debug_dwarf (void)
5567 {
5568   print_indent = 0;
5569   print_die (comp_unit_die (), stderr);
5570 }
5571 \f
5572 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
5573    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
5574    DIE that marks the start of the DIEs for this include file.  */
5575
5576 static dw_die_ref
5577 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
5578 {
5579   const char *filename = get_AT_string (bincl_die, DW_AT_name);
5580   dw_die_ref new_unit = gen_compile_unit_die (filename);
5581
5582   new_unit->die_sib = old_unit;
5583   return new_unit;
5584 }
5585
5586 /* Close an include-file CU and reopen the enclosing one.  */
5587
5588 static dw_die_ref
5589 pop_compile_unit (dw_die_ref old_unit)
5590 {
5591   dw_die_ref new_unit = old_unit->die_sib;
5592
5593   old_unit->die_sib = NULL;
5594   return new_unit;
5595 }
5596
5597 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5598 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
5599
5600 /* Calculate the checksum of a location expression.  */
5601
5602 static inline void
5603 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5604 {
5605   int tem;
5606
5607   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
5608   CHECKSUM (tem);
5609   CHECKSUM (loc->dw_loc_oprnd1);
5610   CHECKSUM (loc->dw_loc_oprnd2);
5611 }
5612
5613 /* Calculate the checksum of an attribute.  */
5614
5615 static void
5616 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
5617 {
5618   dw_loc_descr_ref loc;
5619   rtx r;
5620
5621   CHECKSUM (at->dw_attr);
5622
5623   /* We don't care that this was compiled with a different compiler
5624      snapshot; if the output is the same, that's what matters.  */
5625   if (at->dw_attr == DW_AT_producer)
5626     return;
5627
5628   switch (AT_class (at))
5629     {
5630     case dw_val_class_const:
5631       CHECKSUM (at->dw_attr_val.v.val_int);
5632       break;
5633     case dw_val_class_unsigned_const:
5634       CHECKSUM (at->dw_attr_val.v.val_unsigned);
5635       break;
5636     case dw_val_class_const_double:
5637       CHECKSUM (at->dw_attr_val.v.val_double);
5638       break;
5639     case dw_val_class_vec:
5640       CHECKSUM (at->dw_attr_val.v.val_vec);
5641       break;
5642     case dw_val_class_flag:
5643       CHECKSUM (at->dw_attr_val.v.val_flag);
5644       break;
5645     case dw_val_class_str:
5646       CHECKSUM_STRING (AT_string (at));
5647       break;
5648
5649     case dw_val_class_addr:
5650       r = AT_addr (at);
5651       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5652       CHECKSUM_STRING (XSTR (r, 0));
5653       break;
5654
5655     case dw_val_class_offset:
5656       CHECKSUM (at->dw_attr_val.v.val_offset);
5657       break;
5658
5659     case dw_val_class_loc:
5660       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5661         loc_checksum (loc, ctx);
5662       break;
5663
5664     case dw_val_class_die_ref:
5665       die_checksum (AT_ref (at), ctx, mark);
5666       break;
5667
5668     case dw_val_class_fde_ref:
5669     case dw_val_class_vms_delta:
5670     case dw_val_class_lbl_id:
5671     case dw_val_class_lineptr:
5672     case dw_val_class_macptr:
5673       break;
5674
5675     case dw_val_class_file:
5676       CHECKSUM_STRING (AT_file (at)->filename);
5677       break;
5678
5679     case dw_val_class_data8:
5680       CHECKSUM (at->dw_attr_val.v.val_data8);
5681       break;
5682
5683     default:
5684       break;
5685     }
5686 }
5687
5688 /* Calculate the checksum of a DIE.  */
5689
5690 static void
5691 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
5692 {
5693   dw_die_ref c;
5694   dw_attr_ref a;
5695   unsigned ix;
5696
5697   /* To avoid infinite recursion.  */
5698   if (die->die_mark)
5699     {
5700       CHECKSUM (die->die_mark);
5701       return;
5702     }
5703   die->die_mark = ++(*mark);
5704
5705   CHECKSUM (die->die_tag);
5706
5707   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5708     attr_checksum (a, ctx, mark);
5709
5710   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
5711 }
5712
5713 #undef CHECKSUM
5714 #undef CHECKSUM_STRING
5715
5716 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
5717 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
5718 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
5719 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
5720 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
5721 #define CHECKSUM_ATTR(FOO) \
5722   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
5723
5724 /* Calculate the checksum of a number in signed LEB128 format.  */
5725
5726 static void
5727 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
5728 {
5729   unsigned char byte;
5730   bool more;
5731
5732   while (1)
5733     {
5734       byte = (value & 0x7f);
5735       value >>= 7;
5736       more = !((value == 0 && (byte & 0x40) == 0)
5737                 || (value == -1 && (byte & 0x40) != 0));
5738       if (more)
5739         byte |= 0x80;
5740       CHECKSUM (byte);
5741       if (!more)
5742         break;
5743     }
5744 }
5745
5746 /* Calculate the checksum of a number in unsigned LEB128 format.  */
5747
5748 static void
5749 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
5750 {
5751   while (1)
5752     {
5753       unsigned char byte = (value & 0x7f);
5754       value >>= 7;
5755       if (value != 0)
5756         /* More bytes to follow.  */
5757         byte |= 0x80;
5758       CHECKSUM (byte);
5759       if (value == 0)
5760         break;
5761     }
5762 }
5763
5764 /* Checksum the context of the DIE.  This adds the names of any
5765    surrounding namespaces or structures to the checksum.  */
5766
5767 static void
5768 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
5769 {
5770   const char *name;
5771   dw_die_ref spec;
5772   int tag = die->die_tag;
5773
5774   if (tag != DW_TAG_namespace
5775       && tag != DW_TAG_structure_type
5776       && tag != DW_TAG_class_type)
5777     return;
5778
5779   name = get_AT_string (die, DW_AT_name);
5780
5781   spec = get_AT_ref (die, DW_AT_specification);
5782   if (spec != NULL)
5783     die = spec;
5784
5785   if (die->die_parent != NULL)
5786     checksum_die_context (die->die_parent, ctx);
5787
5788   CHECKSUM_ULEB128 ('C');
5789   CHECKSUM_ULEB128 (tag);
5790   if (name != NULL)
5791     CHECKSUM_STRING (name);
5792 }
5793
5794 /* Calculate the checksum of a location expression.  */
5795
5796 static inline void
5797 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
5798 {
5799   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
5800      were emitted as a DW_FORM_sdata instead of a location expression.  */
5801   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
5802     {
5803       CHECKSUM_ULEB128 (DW_FORM_sdata);
5804       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
5805       return;
5806     }
5807
5808   /* Otherwise, just checksum the raw location expression.  */
5809   while (loc != NULL)
5810     {
5811       CHECKSUM_ULEB128 (loc->dw_loc_opc);
5812       CHECKSUM (loc->dw_loc_oprnd1);
5813       CHECKSUM (loc->dw_loc_oprnd2);
5814       loc = loc->dw_loc_next;
5815     }
5816 }
5817
5818 /* Calculate the checksum of an attribute.  */
5819
5820 static void
5821 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
5822                        struct md5_ctx *ctx, int *mark)
5823 {
5824   dw_loc_descr_ref loc;
5825   rtx r;
5826
5827   if (AT_class (at) == dw_val_class_die_ref)
5828     {
5829       dw_die_ref target_die = AT_ref (at);
5830
5831       /* For pointer and reference types, we checksum only the (qualified)
5832          name of the target type (if there is a name).  For friend entries,
5833          we checksum only the (qualified) name of the target type or function.
5834          This allows the checksum to remain the same whether the target type
5835          is complete or not.  */
5836       if ((at->dw_attr == DW_AT_type
5837            && (tag == DW_TAG_pointer_type
5838                || tag == DW_TAG_reference_type
5839                || tag == DW_TAG_rvalue_reference_type
5840                || tag == DW_TAG_ptr_to_member_type))
5841           || (at->dw_attr == DW_AT_friend
5842               && tag == DW_TAG_friend))
5843         {
5844           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
5845
5846           if (name_attr != NULL)
5847             {
5848               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5849
5850               if (decl == NULL)
5851                 decl = target_die;
5852               CHECKSUM_ULEB128 ('N');
5853               CHECKSUM_ULEB128 (at->dw_attr);
5854               if (decl->die_parent != NULL)
5855                 checksum_die_context (decl->die_parent, ctx);
5856               CHECKSUM_ULEB128 ('E');
5857               CHECKSUM_STRING (AT_string (name_attr));
5858               return;
5859             }
5860         }
5861
5862       /* For all other references to another DIE, we check to see if the
5863          target DIE has already been visited.  If it has, we emit a
5864          backward reference; if not, we descend recursively.  */
5865       if (target_die->die_mark > 0)
5866         {
5867           CHECKSUM_ULEB128 ('R');
5868           CHECKSUM_ULEB128 (at->dw_attr);
5869           CHECKSUM_ULEB128 (target_die->die_mark);
5870         }
5871       else
5872         {
5873           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
5874
5875           if (decl == NULL)
5876             decl = target_die;
5877           target_die->die_mark = ++(*mark);
5878           CHECKSUM_ULEB128 ('T');
5879           CHECKSUM_ULEB128 (at->dw_attr);
5880           if (decl->die_parent != NULL)
5881             checksum_die_context (decl->die_parent, ctx);
5882           die_checksum_ordered (target_die, ctx, mark);
5883         }
5884       return;
5885     }
5886
5887   CHECKSUM_ULEB128 ('A');
5888   CHECKSUM_ULEB128 (at->dw_attr);
5889
5890   switch (AT_class (at))
5891     {
5892     case dw_val_class_const:
5893       CHECKSUM_ULEB128 (DW_FORM_sdata);
5894       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
5895       break;
5896
5897     case dw_val_class_unsigned_const:
5898       CHECKSUM_ULEB128 (DW_FORM_sdata);
5899       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
5900       break;
5901
5902     case dw_val_class_const_double:
5903       CHECKSUM_ULEB128 (DW_FORM_block);
5904       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
5905       CHECKSUM (at->dw_attr_val.v.val_double);
5906       break;
5907
5908     case dw_val_class_vec:
5909       CHECKSUM_ULEB128 (DW_FORM_block);
5910       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
5911       CHECKSUM (at->dw_attr_val.v.val_vec);
5912       break;
5913
5914     case dw_val_class_flag:
5915       CHECKSUM_ULEB128 (DW_FORM_flag);
5916       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
5917       break;
5918
5919     case dw_val_class_str:
5920       CHECKSUM_ULEB128 (DW_FORM_string);
5921       CHECKSUM_STRING (AT_string (at));
5922       break;
5923
5924     case dw_val_class_addr:
5925       r = AT_addr (at);
5926       gcc_assert (GET_CODE (r) == SYMBOL_REF);
5927       CHECKSUM_ULEB128 (DW_FORM_string);
5928       CHECKSUM_STRING (XSTR (r, 0));
5929       break;
5930
5931     case dw_val_class_offset:
5932       CHECKSUM_ULEB128 (DW_FORM_sdata);
5933       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
5934       break;
5935
5936     case dw_val_class_loc:
5937       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
5938         loc_checksum_ordered (loc, ctx);
5939       break;
5940
5941     case dw_val_class_fde_ref:
5942     case dw_val_class_lbl_id:
5943     case dw_val_class_lineptr:
5944     case dw_val_class_macptr:
5945       break;
5946
5947     case dw_val_class_file:
5948       CHECKSUM_ULEB128 (DW_FORM_string);
5949       CHECKSUM_STRING (AT_file (at)->filename);
5950       break;
5951
5952     case dw_val_class_data8:
5953       CHECKSUM (at->dw_attr_val.v.val_data8);
5954       break;
5955
5956     default:
5957       break;
5958     }
5959 }
5960
5961 struct checksum_attributes
5962 {
5963   dw_attr_ref at_name;
5964   dw_attr_ref at_type;
5965   dw_attr_ref at_friend;
5966   dw_attr_ref at_accessibility;
5967   dw_attr_ref at_address_class;
5968   dw_attr_ref at_allocated;
5969   dw_attr_ref at_artificial;
5970   dw_attr_ref at_associated;
5971   dw_attr_ref at_binary_scale;
5972   dw_attr_ref at_bit_offset;
5973   dw_attr_ref at_bit_size;
5974   dw_attr_ref at_bit_stride;
5975   dw_attr_ref at_byte_size;
5976   dw_attr_ref at_byte_stride;
5977   dw_attr_ref at_const_value;
5978   dw_attr_ref at_containing_type;
5979   dw_attr_ref at_count;
5980   dw_attr_ref at_data_location;
5981   dw_attr_ref at_data_member_location;
5982   dw_attr_ref at_decimal_scale;
5983   dw_attr_ref at_decimal_sign;
5984   dw_attr_ref at_default_value;
5985   dw_attr_ref at_digit_count;
5986   dw_attr_ref at_discr;
5987   dw_attr_ref at_discr_list;
5988   dw_attr_ref at_discr_value;
5989   dw_attr_ref at_encoding;
5990   dw_attr_ref at_endianity;
5991   dw_attr_ref at_explicit;
5992   dw_attr_ref at_is_optional;
5993   dw_attr_ref at_location;
5994   dw_attr_ref at_lower_bound;
5995   dw_attr_ref at_mutable;
5996   dw_attr_ref at_ordering;
5997   dw_attr_ref at_picture_string;
5998   dw_attr_ref at_prototyped;
5999   dw_attr_ref at_small;
6000   dw_attr_ref at_segment;
6001   dw_attr_ref at_string_length;
6002   dw_attr_ref at_threads_scaled;
6003   dw_attr_ref at_upper_bound;
6004   dw_attr_ref at_use_location;
6005   dw_attr_ref at_use_UTF8;
6006   dw_attr_ref at_variable_parameter;
6007   dw_attr_ref at_virtuality;
6008   dw_attr_ref at_visibility;
6009   dw_attr_ref at_vtable_elem_location;
6010 };
6011
6012 /* Collect the attributes that we will want to use for the checksum.  */
6013
6014 static void
6015 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6016 {
6017   dw_attr_ref a;
6018   unsigned ix;
6019
6020   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6021     {
6022       switch (a->dw_attr)
6023         {
6024         case DW_AT_name:
6025           attrs->at_name = a;
6026           break;
6027         case DW_AT_type:
6028           attrs->at_type = a;
6029           break;
6030         case DW_AT_friend:
6031           attrs->at_friend = a;
6032           break;
6033         case DW_AT_accessibility:
6034           attrs->at_accessibility = a;
6035           break;
6036         case DW_AT_address_class:
6037           attrs->at_address_class = a;
6038           break;
6039         case DW_AT_allocated:
6040           attrs->at_allocated = a;
6041           break;
6042         case DW_AT_artificial:
6043           attrs->at_artificial = a;
6044           break;
6045         case DW_AT_associated:
6046           attrs->at_associated = a;
6047           break;
6048         case DW_AT_binary_scale:
6049           attrs->at_binary_scale = a;
6050           break;
6051         case DW_AT_bit_offset:
6052           attrs->at_bit_offset = a;
6053           break;
6054         case DW_AT_bit_size:
6055           attrs->at_bit_size = a;
6056           break;
6057         case DW_AT_bit_stride:
6058           attrs->at_bit_stride = a;
6059           break;
6060         case DW_AT_byte_size:
6061           attrs->at_byte_size = a;
6062           break;
6063         case DW_AT_byte_stride:
6064           attrs->at_byte_stride = a;
6065           break;
6066         case DW_AT_const_value:
6067           attrs->at_const_value = a;
6068           break;
6069         case DW_AT_containing_type:
6070           attrs->at_containing_type = a;
6071           break;
6072         case DW_AT_count:
6073           attrs->at_count = a;
6074           break;
6075         case DW_AT_data_location:
6076           attrs->at_data_location = a;
6077           break;
6078         case DW_AT_data_member_location:
6079           attrs->at_data_member_location = a;
6080           break;
6081         case DW_AT_decimal_scale:
6082           attrs->at_decimal_scale = a;
6083           break;
6084         case DW_AT_decimal_sign:
6085           attrs->at_decimal_sign = a;
6086           break;
6087         case DW_AT_default_value:
6088           attrs->at_default_value = a;
6089           break;
6090         case DW_AT_digit_count:
6091           attrs->at_digit_count = a;
6092           break;
6093         case DW_AT_discr:
6094           attrs->at_discr = a;
6095           break;
6096         case DW_AT_discr_list:
6097           attrs->at_discr_list = a;
6098           break;
6099         case DW_AT_discr_value:
6100           attrs->at_discr_value = a;
6101           break;
6102         case DW_AT_encoding:
6103           attrs->at_encoding = a;
6104           break;
6105         case DW_AT_endianity:
6106           attrs->at_endianity = a;
6107           break;
6108         case DW_AT_explicit:
6109           attrs->at_explicit = a;
6110           break;
6111         case DW_AT_is_optional:
6112           attrs->at_is_optional = a;
6113           break;
6114         case DW_AT_location:
6115           attrs->at_location = a;
6116           break;
6117         case DW_AT_lower_bound:
6118           attrs->at_lower_bound = a;
6119           break;
6120         case DW_AT_mutable:
6121           attrs->at_mutable = a;
6122           break;
6123         case DW_AT_ordering:
6124           attrs->at_ordering = a;
6125           break;
6126         case DW_AT_picture_string:
6127           attrs->at_picture_string = a;
6128           break;
6129         case DW_AT_prototyped:
6130           attrs->at_prototyped = a;
6131           break;
6132         case DW_AT_small:
6133           attrs->at_small = a;
6134           break;
6135         case DW_AT_segment:
6136           attrs->at_segment = a;
6137           break;
6138         case DW_AT_string_length:
6139           attrs->at_string_length = a;
6140           break;
6141         case DW_AT_threads_scaled:
6142           attrs->at_threads_scaled = a;
6143           break;
6144         case DW_AT_upper_bound:
6145           attrs->at_upper_bound = a;
6146           break;
6147         case DW_AT_use_location:
6148           attrs->at_use_location = a;
6149           break;
6150         case DW_AT_use_UTF8:
6151           attrs->at_use_UTF8 = a;
6152           break;
6153         case DW_AT_variable_parameter:
6154           attrs->at_variable_parameter = a;
6155           break;
6156         case DW_AT_virtuality:
6157           attrs->at_virtuality = a;
6158           break;
6159         case DW_AT_visibility:
6160           attrs->at_visibility = a;
6161           break;
6162         case DW_AT_vtable_elem_location:
6163           attrs->at_vtable_elem_location = a;
6164           break;
6165         default:
6166           break;
6167         }
6168     }
6169 }
6170
6171 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6172
6173 static void
6174 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6175 {
6176   dw_die_ref c;
6177   dw_die_ref decl;
6178   struct checksum_attributes attrs;
6179
6180   CHECKSUM_ULEB128 ('D');
6181   CHECKSUM_ULEB128 (die->die_tag);
6182
6183   memset (&attrs, 0, sizeof (attrs));
6184
6185   decl = get_AT_ref (die, DW_AT_specification);
6186   if (decl != NULL)
6187     collect_checksum_attributes (&attrs, decl);
6188   collect_checksum_attributes (&attrs, die);
6189
6190   CHECKSUM_ATTR (attrs.at_name);
6191   CHECKSUM_ATTR (attrs.at_accessibility);
6192   CHECKSUM_ATTR (attrs.at_address_class);
6193   CHECKSUM_ATTR (attrs.at_allocated);
6194   CHECKSUM_ATTR (attrs.at_artificial);
6195   CHECKSUM_ATTR (attrs.at_associated);
6196   CHECKSUM_ATTR (attrs.at_binary_scale);
6197   CHECKSUM_ATTR (attrs.at_bit_offset);
6198   CHECKSUM_ATTR (attrs.at_bit_size);
6199   CHECKSUM_ATTR (attrs.at_bit_stride);
6200   CHECKSUM_ATTR (attrs.at_byte_size);
6201   CHECKSUM_ATTR (attrs.at_byte_stride);
6202   CHECKSUM_ATTR (attrs.at_const_value);
6203   CHECKSUM_ATTR (attrs.at_containing_type);
6204   CHECKSUM_ATTR (attrs.at_count);
6205   CHECKSUM_ATTR (attrs.at_data_location);
6206   CHECKSUM_ATTR (attrs.at_data_member_location);
6207   CHECKSUM_ATTR (attrs.at_decimal_scale);
6208   CHECKSUM_ATTR (attrs.at_decimal_sign);
6209   CHECKSUM_ATTR (attrs.at_default_value);
6210   CHECKSUM_ATTR (attrs.at_digit_count);
6211   CHECKSUM_ATTR (attrs.at_discr);
6212   CHECKSUM_ATTR (attrs.at_discr_list);
6213   CHECKSUM_ATTR (attrs.at_discr_value);
6214   CHECKSUM_ATTR (attrs.at_encoding);
6215   CHECKSUM_ATTR (attrs.at_endianity);
6216   CHECKSUM_ATTR (attrs.at_explicit);
6217   CHECKSUM_ATTR (attrs.at_is_optional);
6218   CHECKSUM_ATTR (attrs.at_location);
6219   CHECKSUM_ATTR (attrs.at_lower_bound);
6220   CHECKSUM_ATTR (attrs.at_mutable);
6221   CHECKSUM_ATTR (attrs.at_ordering);
6222   CHECKSUM_ATTR (attrs.at_picture_string);
6223   CHECKSUM_ATTR (attrs.at_prototyped);
6224   CHECKSUM_ATTR (attrs.at_small);
6225   CHECKSUM_ATTR (attrs.at_segment);
6226   CHECKSUM_ATTR (attrs.at_string_length);
6227   CHECKSUM_ATTR (attrs.at_threads_scaled);
6228   CHECKSUM_ATTR (attrs.at_upper_bound);
6229   CHECKSUM_ATTR (attrs.at_use_location);
6230   CHECKSUM_ATTR (attrs.at_use_UTF8);
6231   CHECKSUM_ATTR (attrs.at_variable_parameter);
6232   CHECKSUM_ATTR (attrs.at_virtuality);
6233   CHECKSUM_ATTR (attrs.at_visibility);
6234   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6235   CHECKSUM_ATTR (attrs.at_type);
6236   CHECKSUM_ATTR (attrs.at_friend);
6237
6238   /* Checksum the child DIEs, except for nested types and member functions.  */
6239   c = die->die_child;
6240   if (c) do {
6241     dw_attr_ref name_attr;
6242
6243     c = c->die_sib;
6244     name_attr = get_AT (c, DW_AT_name);
6245     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6246         && name_attr != NULL)
6247       {
6248         CHECKSUM_ULEB128 ('S');
6249         CHECKSUM_ULEB128 (c->die_tag);
6250         CHECKSUM_STRING (AT_string (name_attr));
6251       }
6252     else
6253       {
6254         /* Mark this DIE so it gets processed when unmarking.  */
6255         if (c->die_mark == 0)
6256           c->die_mark = -1;
6257         die_checksum_ordered (c, ctx, mark);
6258       }
6259   } while (c != die->die_child);
6260
6261   CHECKSUM_ULEB128 (0);
6262 }
6263
6264 #undef CHECKSUM
6265 #undef CHECKSUM_STRING
6266 #undef CHECKSUM_ATTR
6267 #undef CHECKSUM_LEB128
6268 #undef CHECKSUM_ULEB128
6269
6270 /* Generate the type signature for DIE.  This is computed by generating an
6271    MD5 checksum over the DIE's tag, its relevant attributes, and its
6272    children.  Attributes that are references to other DIEs are processed
6273    by recursion, using the MARK field to prevent infinite recursion.
6274    If the DIE is nested inside a namespace or another type, we also
6275    need to include that context in the signature.  The lower 64 bits
6276    of the resulting MD5 checksum comprise the signature.  */
6277
6278 static void
6279 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6280 {
6281   int mark;
6282   const char *name;
6283   unsigned char checksum[16];
6284   struct md5_ctx ctx;
6285   dw_die_ref decl;
6286
6287   name = get_AT_string (die, DW_AT_name);
6288   decl = get_AT_ref (die, DW_AT_specification);
6289
6290   /* First, compute a signature for just the type name (and its surrounding
6291      context, if any.  This is stored in the type unit DIE for link-time
6292      ODR (one-definition rule) checking.  */
6293
6294   if (is_cxx() && name != NULL)
6295     {
6296       md5_init_ctx (&ctx);
6297
6298       /* Checksum the names of surrounding namespaces and structures.  */
6299       if (decl != NULL && decl->die_parent != NULL)
6300         checksum_die_context (decl->die_parent, &ctx);
6301
6302       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6303       md5_process_bytes (name, strlen (name) + 1, &ctx);
6304       md5_finish_ctx (&ctx, checksum);
6305
6306       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6307     }
6308
6309   /* Next, compute the complete type signature.  */
6310
6311   md5_init_ctx (&ctx);
6312   mark = 1;
6313   die->die_mark = mark;
6314
6315   /* Checksum the names of surrounding namespaces and structures.  */
6316   if (decl != NULL && decl->die_parent != NULL)
6317     checksum_die_context (decl->die_parent, &ctx);
6318
6319   /* Checksum the DIE and its children.  */
6320   die_checksum_ordered (die, &ctx, &mark);
6321   unmark_all_dies (die);
6322   md5_finish_ctx (&ctx, checksum);
6323
6324   /* Store the signature in the type node and link the type DIE and the
6325      type node together.  */
6326   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
6327           DWARF_TYPE_SIGNATURE_SIZE);
6328   die->die_id.die_type_node = type_node;
6329   type_node->type_die = die;
6330
6331   /* If the DIE is a specification, link its declaration to the type node
6332      as well.  */
6333   if (decl != NULL)
6334     decl->die_id.die_type_node = type_node;
6335 }
6336
6337 /* Do the location expressions look same?  */
6338 static inline int
6339 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6340 {
6341   return loc1->dw_loc_opc == loc2->dw_loc_opc
6342          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6343          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6344 }
6345
6346 /* Do the values look the same?  */
6347 static int
6348 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6349 {
6350   dw_loc_descr_ref loc1, loc2;
6351   rtx r1, r2;
6352
6353   if (v1->val_class != v2->val_class)
6354     return 0;
6355
6356   switch (v1->val_class)
6357     {
6358     case dw_val_class_const:
6359       return v1->v.val_int == v2->v.val_int;
6360     case dw_val_class_unsigned_const:
6361       return v1->v.val_unsigned == v2->v.val_unsigned;
6362     case dw_val_class_const_double:
6363       return v1->v.val_double.high == v2->v.val_double.high
6364              && v1->v.val_double.low == v2->v.val_double.low;
6365     case dw_val_class_vec:
6366       if (v1->v.val_vec.length != v2->v.val_vec.length
6367           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6368         return 0;
6369       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6370                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6371         return 0;
6372       return 1;
6373     case dw_val_class_flag:
6374       return v1->v.val_flag == v2->v.val_flag;
6375     case dw_val_class_str:
6376       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6377
6378     case dw_val_class_addr:
6379       r1 = v1->v.val_addr;
6380       r2 = v2->v.val_addr;
6381       if (GET_CODE (r1) != GET_CODE (r2))
6382         return 0;
6383       return !rtx_equal_p (r1, r2);
6384
6385     case dw_val_class_offset:
6386       return v1->v.val_offset == v2->v.val_offset;
6387
6388     case dw_val_class_loc:
6389       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6390            loc1 && loc2;
6391            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6392         if (!same_loc_p (loc1, loc2, mark))
6393           return 0;
6394       return !loc1 && !loc2;
6395
6396     case dw_val_class_die_ref:
6397       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6398
6399     case dw_val_class_fde_ref:
6400     case dw_val_class_vms_delta:
6401     case dw_val_class_lbl_id:
6402     case dw_val_class_lineptr:
6403     case dw_val_class_macptr:
6404       return 1;
6405
6406     case dw_val_class_file:
6407       return v1->v.val_file == v2->v.val_file;
6408
6409     case dw_val_class_data8:
6410       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
6411
6412     default:
6413       return 1;
6414     }
6415 }
6416
6417 /* Do the attributes look the same?  */
6418
6419 static int
6420 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6421 {
6422   if (at1->dw_attr != at2->dw_attr)
6423     return 0;
6424
6425   /* We don't care that this was compiled with a different compiler
6426      snapshot; if the output is the same, that's what matters. */
6427   if (at1->dw_attr == DW_AT_producer)
6428     return 1;
6429
6430   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6431 }
6432
6433 /* Do the dies look the same?  */
6434
6435 static int
6436 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6437 {
6438   dw_die_ref c1, c2;
6439   dw_attr_ref a1;
6440   unsigned ix;
6441
6442   /* To avoid infinite recursion.  */
6443   if (die1->die_mark)
6444     return die1->die_mark == die2->die_mark;
6445   die1->die_mark = die2->die_mark = ++(*mark);
6446
6447   if (die1->die_tag != die2->die_tag)
6448     return 0;
6449
6450   if (VEC_length (dw_attr_node, die1->die_attr)
6451       != VEC_length (dw_attr_node, die2->die_attr))
6452     return 0;
6453
6454   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
6455     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6456       return 0;
6457
6458   c1 = die1->die_child;
6459   c2 = die2->die_child;
6460   if (! c1)
6461     {
6462       if (c2)
6463         return 0;
6464     }
6465   else
6466     for (;;)
6467       {
6468         if (!same_die_p (c1, c2, mark))
6469           return 0;
6470         c1 = c1->die_sib;
6471         c2 = c2->die_sib;
6472         if (c1 == die1->die_child)
6473           {
6474             if (c2 == die2->die_child)
6475               break;
6476             else
6477               return 0;
6478           }
6479     }
6480
6481   return 1;
6482 }
6483
6484 /* Do the dies look the same?  Wrapper around same_die_p.  */
6485
6486 static int
6487 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6488 {
6489   int mark = 0;
6490   int ret = same_die_p (die1, die2, &mark);
6491
6492   unmark_all_dies (die1);
6493   unmark_all_dies (die2);
6494
6495   return ret;
6496 }
6497
6498 /* The prefix to attach to symbols on DIEs in the current comdat debug
6499    info section.  */
6500 static char *comdat_symbol_id;
6501
6502 /* The index of the current symbol within the current comdat CU.  */
6503 static unsigned int comdat_symbol_number;
6504
6505 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6506    children, and set comdat_symbol_id accordingly.  */
6507
6508 static void
6509 compute_section_prefix (dw_die_ref unit_die)
6510 {
6511   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6512   const char *base = die_name ? lbasename (die_name) : "anonymous";
6513   char *name = XALLOCAVEC (char, strlen (base) + 64);
6514   char *p;
6515   int i, mark;
6516   unsigned char checksum[16];
6517   struct md5_ctx ctx;
6518
6519   /* Compute the checksum of the DIE, then append part of it as hex digits to
6520      the name filename of the unit.  */
6521
6522   md5_init_ctx (&ctx);
6523   mark = 0;
6524   die_checksum (unit_die, &ctx, &mark);
6525   unmark_all_dies (unit_die);
6526   md5_finish_ctx (&ctx, checksum);
6527
6528   sprintf (name, "%s.", base);
6529   clean_symbol_name (name);
6530
6531   p = name + strlen (name);
6532   for (i = 0; i < 4; i++)
6533     {
6534       sprintf (p, "%.2x", checksum[i]);
6535       p += 2;
6536     }
6537
6538   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
6539   comdat_symbol_number = 0;
6540 }
6541
6542 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6543
6544 static int
6545 is_type_die (dw_die_ref die)
6546 {
6547   switch (die->die_tag)
6548     {
6549     case DW_TAG_array_type:
6550     case DW_TAG_class_type:
6551     case DW_TAG_interface_type:
6552     case DW_TAG_enumeration_type:
6553     case DW_TAG_pointer_type:
6554     case DW_TAG_reference_type:
6555     case DW_TAG_rvalue_reference_type:
6556     case DW_TAG_string_type:
6557     case DW_TAG_structure_type:
6558     case DW_TAG_subroutine_type:
6559     case DW_TAG_union_type:
6560     case DW_TAG_ptr_to_member_type:
6561     case DW_TAG_set_type:
6562     case DW_TAG_subrange_type:
6563     case DW_TAG_base_type:
6564     case DW_TAG_const_type:
6565     case DW_TAG_file_type:
6566     case DW_TAG_packed_type:
6567     case DW_TAG_volatile_type:
6568     case DW_TAG_typedef:
6569       return 1;
6570     default:
6571       return 0;
6572     }
6573 }
6574
6575 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6576    Basically, we want to choose the bits that are likely to be shared between
6577    compilations (types) and leave out the bits that are specific to individual
6578    compilations (functions).  */
6579
6580 static int
6581 is_comdat_die (dw_die_ref c)
6582 {
6583   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6584      we do for stabs.  The advantage is a greater likelihood of sharing between
6585      objects that don't include headers in the same order (and therefore would
6586      put the base types in a different comdat).  jason 8/28/00 */
6587
6588   if (c->die_tag == DW_TAG_base_type)
6589     return 0;
6590
6591   if (c->die_tag == DW_TAG_pointer_type
6592       || c->die_tag == DW_TAG_reference_type
6593       || c->die_tag == DW_TAG_rvalue_reference_type
6594       || c->die_tag == DW_TAG_const_type
6595       || c->die_tag == DW_TAG_volatile_type)
6596     {
6597       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6598
6599       return t ? is_comdat_die (t) : 0;
6600     }
6601
6602   return is_type_die (c);
6603 }
6604
6605 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6606    compilation unit.  */
6607
6608 static int
6609 is_symbol_die (dw_die_ref c)
6610 {
6611   return (is_type_die (c)
6612           || is_declaration_die (c)
6613           || c->die_tag == DW_TAG_namespace
6614           || c->die_tag == DW_TAG_module);
6615 }
6616
6617 /* Returns true iff C is a compile-unit DIE.  */
6618
6619 static inline bool
6620 is_cu_die (dw_die_ref c)
6621 {
6622   return c && c->die_tag == DW_TAG_compile_unit;
6623 }
6624
6625 static char *
6626 gen_internal_sym (const char *prefix)
6627 {
6628   char buf[256];
6629
6630   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6631   return xstrdup (buf);
6632 }
6633
6634 /* Assign symbols to all worthy DIEs under DIE.  */
6635
6636 static void
6637 assign_symbol_names (dw_die_ref die)
6638 {
6639   dw_die_ref c;
6640
6641   if (is_symbol_die (die))
6642     {
6643       if (comdat_symbol_id)
6644         {
6645           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
6646
6647           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6648                    comdat_symbol_id, comdat_symbol_number++);
6649           die->die_id.die_symbol = xstrdup (p);
6650         }
6651       else
6652         die->die_id.die_symbol = gen_internal_sym ("LDIE");
6653     }
6654
6655   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6656 }
6657
6658 struct cu_hash_table_entry
6659 {
6660   dw_die_ref cu;
6661   unsigned min_comdat_num, max_comdat_num;
6662   struct cu_hash_table_entry *next;
6663 };
6664
6665 /* Routines to manipulate hash table of CUs.  */
6666 static hashval_t
6667 htab_cu_hash (const void *of)
6668 {
6669   const struct cu_hash_table_entry *const entry =
6670     (const struct cu_hash_table_entry *) of;
6671
6672   return htab_hash_string (entry->cu->die_id.die_symbol);
6673 }
6674
6675 static int
6676 htab_cu_eq (const void *of1, const void *of2)
6677 {
6678   const struct cu_hash_table_entry *const entry1 =
6679     (const struct cu_hash_table_entry *) of1;
6680   const struct die_struct *const entry2 = (const struct die_struct *) of2;
6681
6682   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
6683 }
6684
6685 static void
6686 htab_cu_del (void *what)
6687 {
6688   struct cu_hash_table_entry *next,
6689     *entry = (struct cu_hash_table_entry *) what;
6690
6691   while (entry)
6692     {
6693       next = entry->next;
6694       free (entry);
6695       entry = next;
6696     }
6697 }
6698
6699 /* Check whether we have already seen this CU and set up SYM_NUM
6700    accordingly.  */
6701 static int
6702 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6703 {
6704   struct cu_hash_table_entry dummy;
6705   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6706
6707   dummy.max_comdat_num = 0;
6708
6709   slot = (struct cu_hash_table_entry **)
6710     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6711         INSERT);
6712   entry = *slot;
6713
6714   for (; entry; last = entry, entry = entry->next)
6715     {
6716       if (same_die_p_wrap (cu, entry->cu))
6717         break;
6718     }
6719
6720   if (entry)
6721     {
6722       *sym_num = entry->min_comdat_num;
6723       return 1;
6724     }
6725
6726   entry = XCNEW (struct cu_hash_table_entry);
6727   entry->cu = cu;
6728   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6729   entry->next = *slot;
6730   *slot = entry;
6731
6732   return 0;
6733 }
6734
6735 /* Record SYM_NUM to record of CU in HTABLE.  */
6736 static void
6737 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6738 {
6739   struct cu_hash_table_entry **slot, *entry;
6740
6741   slot = (struct cu_hash_table_entry **)
6742     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
6743         NO_INSERT);
6744   entry = *slot;
6745
6746   entry->max_comdat_num = sym_num;
6747 }
6748
6749 /* Traverse the DIE (which is always comp_unit_die), and set up
6750    additional compilation units for each of the include files we see
6751    bracketed by BINCL/EINCL.  */
6752
6753 static void
6754 break_out_includes (dw_die_ref die)
6755 {
6756   dw_die_ref c;
6757   dw_die_ref unit = NULL;
6758   limbo_die_node *node, **pnode;
6759   htab_t cu_hash_table;
6760
6761   c = die->die_child;
6762   if (c) do {
6763     dw_die_ref prev = c;
6764     c = c->die_sib;
6765     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6766            || (unit && is_comdat_die (c)))
6767       {
6768         dw_die_ref next = c->die_sib;
6769
6770         /* This DIE is for a secondary CU; remove it from the main one.  */
6771         remove_child_with_prev (c, prev);
6772
6773         if (c->die_tag == DW_TAG_GNU_BINCL)
6774           unit = push_new_compile_unit (unit, c);
6775         else if (c->die_tag == DW_TAG_GNU_EINCL)
6776           unit = pop_compile_unit (unit);
6777         else
6778           add_child_die (unit, c);
6779         c = next;
6780         if (c == die->die_child)
6781           break;
6782       }
6783   } while (c != die->die_child);
6784
6785 #if 0
6786   /* We can only use this in debugging, since the frontend doesn't check
6787      to make sure that we leave every include file we enter.  */
6788   gcc_assert (!unit);
6789 #endif
6790
6791   assign_symbol_names (die);
6792   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6793   for (node = limbo_die_list, pnode = &limbo_die_list;
6794        node;
6795        node = node->next)
6796     {
6797       int is_dupl;
6798
6799       compute_section_prefix (node->die);
6800       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6801                         &comdat_symbol_number);
6802       assign_symbol_names (node->die);
6803       if (is_dupl)
6804         *pnode = node->next;
6805       else
6806         {
6807           pnode = &node->next;
6808           record_comdat_symbol_number (node->die, cu_hash_table,
6809                 comdat_symbol_number);
6810         }
6811     }
6812   htab_delete (cu_hash_table);
6813 }
6814
6815 /* Return non-zero if this DIE is a declaration.  */
6816
6817 static int
6818 is_declaration_die (dw_die_ref die)
6819 {
6820   dw_attr_ref a;
6821   unsigned ix;
6822
6823   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6824     if (a->dw_attr == DW_AT_declaration)
6825       return 1;
6826
6827   return 0;
6828 }
6829
6830 /* Return non-zero if this DIE is nested inside a subprogram.  */
6831
6832 static int
6833 is_nested_in_subprogram (dw_die_ref die)
6834 {
6835   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
6836
6837   if (decl == NULL)
6838     decl = die;
6839   return local_scope_p (decl);
6840 }
6841
6842 /* Return non-zero if this DIE contains a defining declaration of a
6843    subprogram.  */
6844
6845 static int
6846 contains_subprogram_definition (dw_die_ref die)
6847 {
6848   dw_die_ref c;
6849
6850   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
6851     return 1;
6852   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
6853   return 0;
6854 }
6855
6856 /* Return non-zero if this is a type DIE that should be moved to a
6857    COMDAT .debug_types section.  */
6858
6859 static int
6860 should_move_die_to_comdat (dw_die_ref die)
6861 {
6862   switch (die->die_tag)
6863     {
6864     case DW_TAG_class_type:
6865     case DW_TAG_structure_type:
6866     case DW_TAG_enumeration_type:
6867     case DW_TAG_union_type:
6868       /* Don't move declarations, inlined instances, or types nested in a
6869          subprogram.  */
6870       if (is_declaration_die (die)
6871           || get_AT (die, DW_AT_abstract_origin)
6872           || is_nested_in_subprogram (die))
6873         return 0;
6874       /* A type definition should never contain a subprogram definition.  */
6875       gcc_assert (!contains_subprogram_definition (die));
6876       return 1;
6877     case DW_TAG_array_type:
6878     case DW_TAG_interface_type:
6879     case DW_TAG_pointer_type:
6880     case DW_TAG_reference_type:
6881     case DW_TAG_rvalue_reference_type:
6882     case DW_TAG_string_type:
6883     case DW_TAG_subroutine_type:
6884     case DW_TAG_ptr_to_member_type:
6885     case DW_TAG_set_type:
6886     case DW_TAG_subrange_type:
6887     case DW_TAG_base_type:
6888     case DW_TAG_const_type:
6889     case DW_TAG_file_type:
6890     case DW_TAG_packed_type:
6891     case DW_TAG_volatile_type:
6892     case DW_TAG_typedef:
6893     default:
6894       return 0;
6895     }
6896 }
6897
6898 /* Make a clone of DIE.  */
6899
6900 static dw_die_ref
6901 clone_die (dw_die_ref die)
6902 {
6903   dw_die_ref clone;
6904   dw_attr_ref a;
6905   unsigned ix;
6906
6907   clone = ggc_alloc_cleared_die_node ();
6908   clone->die_tag = die->die_tag;
6909
6910   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6911     add_dwarf_attr (clone, a);
6912
6913   return clone;
6914 }
6915
6916 /* Make a clone of the tree rooted at DIE.  */
6917
6918 static dw_die_ref
6919 clone_tree (dw_die_ref die)
6920 {
6921   dw_die_ref c;
6922   dw_die_ref clone = clone_die (die);
6923
6924   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
6925
6926   return clone;
6927 }
6928
6929 /* Make a clone of DIE as a declaration.  */
6930
6931 static dw_die_ref
6932 clone_as_declaration (dw_die_ref die)
6933 {
6934   dw_die_ref clone;
6935   dw_die_ref decl;
6936   dw_attr_ref a;
6937   unsigned ix;
6938
6939   /* If the DIE is already a declaration, just clone it.  */
6940   if (is_declaration_die (die))
6941     return clone_die (die);
6942
6943   /* If the DIE is a specification, just clone its declaration DIE.  */
6944   decl = get_AT_ref (die, DW_AT_specification);
6945   if (decl != NULL)
6946     return clone_die (decl);
6947
6948   clone = ggc_alloc_cleared_die_node ();
6949   clone->die_tag = die->die_tag;
6950
6951   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6952     {
6953       /* We don't want to copy over all attributes.
6954          For example we don't want DW_AT_byte_size because otherwise we will no
6955          longer have a declaration and GDB will treat it as a definition.  */
6956
6957       switch (a->dw_attr)
6958         {
6959         case DW_AT_artificial:
6960         case DW_AT_containing_type:
6961         case DW_AT_external:
6962         case DW_AT_name:
6963         case DW_AT_type:
6964         case DW_AT_virtuality:
6965         case DW_AT_linkage_name:
6966         case DW_AT_MIPS_linkage_name:
6967           add_dwarf_attr (clone, a);
6968           break;
6969         case DW_AT_byte_size:
6970         default:
6971           break;
6972         }
6973     }
6974
6975   if (die->die_id.die_type_node)
6976     add_AT_die_ref (clone, DW_AT_signature, die);
6977
6978   add_AT_flag (clone, DW_AT_declaration, 1);
6979   return clone;
6980 }
6981
6982 /* Copy the declaration context to the new compile unit DIE.  This includes
6983    any surrounding namespace or type declarations.  If the DIE has an
6984    AT_specification attribute, it also includes attributes and children
6985    attached to the specification.  */
6986
6987 static void
6988 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
6989 {
6990   dw_die_ref decl;
6991   dw_die_ref new_decl;
6992
6993   decl = get_AT_ref (die, DW_AT_specification);
6994   if (decl == NULL)
6995     decl = die;
6996   else
6997     {
6998       unsigned ix;
6999       dw_die_ref c;
7000       dw_attr_ref a;
7001
7002       /* Copy the type node pointer from the new DIE to the original
7003          declaration DIE so we can forward references later.  */
7004       decl->die_id.die_type_node = die->die_id.die_type_node;
7005
7006       remove_AT (die, DW_AT_specification);
7007
7008       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7009         {
7010           if (a->dw_attr != DW_AT_name
7011               && a->dw_attr != DW_AT_declaration
7012               && a->dw_attr != DW_AT_external)
7013             add_dwarf_attr (die, a);
7014         }
7015
7016       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7017     }
7018
7019   if (decl->die_parent != NULL
7020       && decl->die_parent->die_tag != DW_TAG_compile_unit
7021       && decl->die_parent->die_tag != DW_TAG_type_unit)
7022     {
7023       new_decl = copy_ancestor_tree (unit, decl, NULL);
7024       if (new_decl != NULL)
7025         {
7026           remove_AT (new_decl, DW_AT_signature);
7027           add_AT_specification (die, new_decl);
7028         }
7029     }
7030 }
7031
7032 /* Generate the skeleton ancestor tree for the given NODE, then clone
7033    the DIE and add the clone into the tree.  */
7034
7035 static void
7036 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7037 {
7038   if (node->new_die != NULL)
7039     return;
7040
7041   node->new_die = clone_as_declaration (node->old_die);
7042
7043   if (node->parent != NULL)
7044     {
7045       generate_skeleton_ancestor_tree (node->parent);
7046       add_child_die (node->parent->new_die, node->new_die);
7047     }
7048 }
7049
7050 /* Generate a skeleton tree of DIEs containing any declarations that are
7051    found in the original tree.  We traverse the tree looking for declaration
7052    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7053
7054 static void
7055 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7056 {
7057   skeleton_chain_node node;
7058   dw_die_ref c;
7059   dw_die_ref first;
7060   dw_die_ref prev = NULL;
7061   dw_die_ref next = NULL;
7062
7063   node.parent = parent;
7064
7065   first = c = parent->old_die->die_child;
7066   if (c)
7067     next = c->die_sib;
7068   if (c) do {
7069     if (prev == NULL || prev->die_sib == c)
7070       prev = c;
7071     c = next;
7072     next = (c == first ? NULL : c->die_sib);
7073     node.old_die = c;
7074     node.new_die = NULL;
7075     if (is_declaration_die (c))
7076       {
7077         /* Clone the existing DIE, move the original to the skeleton
7078            tree (which is in the main CU), and put the clone, with
7079            all the original's children, where the original came from.  */
7080         dw_die_ref clone = clone_die (c);
7081         move_all_children (c, clone);
7082
7083         replace_child (c, clone, prev);
7084         generate_skeleton_ancestor_tree (parent);
7085         add_child_die (parent->new_die, c);
7086         node.new_die = c;
7087         c = clone;
7088       }
7089     generate_skeleton_bottom_up (&node);
7090   } while (next != NULL);
7091 }
7092
7093 /* Wrapper function for generate_skeleton_bottom_up.  */
7094
7095 static dw_die_ref
7096 generate_skeleton (dw_die_ref die)
7097 {
7098   skeleton_chain_node node;
7099
7100   node.old_die = die;
7101   node.new_die = NULL;
7102   node.parent = NULL;
7103
7104   /* If this type definition is nested inside another type,
7105      always leave at least a declaration in its place.  */
7106   if (die->die_parent != NULL && is_type_die (die->die_parent))
7107     node.new_die = clone_as_declaration (die);
7108
7109   generate_skeleton_bottom_up (&node);
7110   return node.new_die;
7111 }
7112
7113 /* Remove the DIE from its parent, possibly replacing it with a cloned
7114    declaration.  The original DIE will be moved to a new compile unit
7115    so that existing references to it follow it to the new location.  If
7116    any of the original DIE's descendants is a declaration, we need to
7117    replace the original DIE with a skeleton tree and move the
7118    declarations back into the skeleton tree.  */
7119
7120 static dw_die_ref
7121 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7122 {
7123   dw_die_ref skeleton;
7124
7125   skeleton = generate_skeleton (child);
7126   if (skeleton == NULL)
7127     remove_child_with_prev (child, prev);
7128   else
7129     {
7130       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7131       replace_child (child, skeleton, prev);
7132     }
7133
7134   return skeleton;
7135 }
7136
7137 /* Traverse the DIE and set up additional .debug_types sections for each
7138    type worthy of being placed in a COMDAT section.  */
7139
7140 static void
7141 break_out_comdat_types (dw_die_ref die)
7142 {
7143   dw_die_ref c;
7144   dw_die_ref first;
7145   dw_die_ref prev = NULL;
7146   dw_die_ref next = NULL;
7147   dw_die_ref unit = NULL;
7148
7149   first = c = die->die_child;
7150   if (c)
7151     next = c->die_sib;
7152   if (c) do {
7153     if (prev == NULL || prev->die_sib == c)
7154       prev = c;
7155     c = next;
7156     next = (c == first ? NULL : c->die_sib);
7157     if (should_move_die_to_comdat (c))
7158       {
7159         dw_die_ref replacement;
7160         comdat_type_node_ref type_node;
7161
7162         /* Create a new type unit DIE as the root for the new tree, and
7163            add it to the list of comdat types.  */
7164         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7165         add_AT_unsigned (unit, DW_AT_language,
7166                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7167         type_node = ggc_alloc_cleared_comdat_type_node ();
7168         type_node->root_die = unit;
7169         type_node->next = comdat_type_list;
7170         comdat_type_list = type_node;
7171
7172         /* Generate the type signature.  */
7173         generate_type_signature (c, type_node);
7174
7175         /* Copy the declaration context, attributes, and children of the
7176            declaration into the new compile unit DIE.  */
7177         copy_declaration_context (unit, c);
7178
7179         /* Remove this DIE from the main CU.  */
7180         replacement = remove_child_or_replace_with_skeleton (c, prev);
7181
7182         /* Break out nested types into their own type units.  */
7183         break_out_comdat_types (c);
7184
7185         /* Add the DIE to the new compunit.  */
7186         add_child_die (unit, c);
7187
7188         if (replacement != NULL)
7189           c = replacement;
7190       }
7191     else if (c->die_tag == DW_TAG_namespace
7192              || c->die_tag == DW_TAG_class_type
7193              || c->die_tag == DW_TAG_structure_type
7194              || c->die_tag == DW_TAG_union_type)
7195       {
7196         /* Look for nested types that can be broken out.  */
7197         break_out_comdat_types (c);
7198       }
7199   } while (next != NULL);
7200 }
7201
7202 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7203
7204 struct decl_table_entry
7205 {
7206   dw_die_ref orig;
7207   dw_die_ref copy;
7208 };
7209
7210 /* Routines to manipulate hash table of copied declarations.  */
7211
7212 static hashval_t
7213 htab_decl_hash (const void *of)
7214 {
7215   const struct decl_table_entry *const entry =
7216     (const struct decl_table_entry *) of;
7217
7218   return htab_hash_pointer (entry->orig);
7219 }
7220
7221 static int
7222 htab_decl_eq (const void *of1, const void *of2)
7223 {
7224   const struct decl_table_entry *const entry1 =
7225     (const struct decl_table_entry *) of1;
7226   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7227
7228   return entry1->orig == entry2;
7229 }
7230
7231 static void
7232 htab_decl_del (void *what)
7233 {
7234   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7235
7236   free (entry);
7237 }
7238
7239 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7240    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7241    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7242    to check if the ancestor has already been copied into UNIT.  */
7243
7244 static dw_die_ref
7245 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7246 {
7247   dw_die_ref parent = die->die_parent;
7248   dw_die_ref new_parent = unit;
7249   dw_die_ref copy;
7250   void **slot = NULL;
7251   struct decl_table_entry *entry = NULL;
7252
7253   if (decl_table)
7254     {
7255       /* Check if the entry has already been copied to UNIT.  */
7256       slot = htab_find_slot_with_hash (decl_table, die,
7257                                        htab_hash_pointer (die), INSERT);
7258       if (*slot != HTAB_EMPTY_ENTRY)
7259         {
7260           entry = (struct decl_table_entry *) *slot;
7261           return entry->copy;
7262         }
7263
7264       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7265       entry = XCNEW (struct decl_table_entry);
7266       entry->orig = die;
7267       entry->copy = NULL;
7268       *slot = entry;
7269     }
7270
7271   if (parent != NULL)
7272     {
7273       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7274       if (spec != NULL)
7275         parent = spec;
7276       if (parent->die_tag != DW_TAG_compile_unit
7277           && parent->die_tag != DW_TAG_type_unit)
7278         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7279     }
7280
7281   copy = clone_as_declaration (die);
7282   add_child_die (new_parent, copy);
7283
7284   if (decl_table != NULL)
7285     {
7286       /* Record the pointer to the copy.  */
7287       entry->copy = copy;
7288     }
7289
7290   return copy;
7291 }
7292
7293 /* Walk the DIE and its children, looking for references to incomplete
7294    or trivial types that are unmarked (i.e., that are not in the current
7295    type_unit).  */
7296
7297 static void
7298 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7299 {
7300   dw_die_ref c;
7301   dw_attr_ref a;
7302   unsigned ix;
7303
7304   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7305     {
7306       if (AT_class (a) == dw_val_class_die_ref)
7307         {
7308           dw_die_ref targ = AT_ref (a);
7309           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7310           void **slot;
7311           struct decl_table_entry *entry;
7312
7313           if (targ->die_mark != 0 || type_node != NULL)
7314             continue;
7315
7316           slot = htab_find_slot_with_hash (decl_table, targ,
7317                                            htab_hash_pointer (targ), INSERT);
7318
7319           if (*slot != HTAB_EMPTY_ENTRY)
7320             {
7321               /* TARG has already been copied, so we just need to
7322                  modify the reference to point to the copy.  */
7323               entry = (struct decl_table_entry *) *slot;
7324               a->dw_attr_val.v.val_die_ref.die = entry->copy;
7325             }
7326           else
7327             {
7328               dw_die_ref parent = unit;
7329               dw_die_ref copy = clone_tree (targ);
7330
7331               /* Make sure the cloned tree is marked as part of the
7332                  type unit.  */
7333               mark_dies (copy);
7334
7335               /* Record in DECL_TABLE that TARG has been copied.
7336                  Need to do this now, before the recursive call,
7337                  because DECL_TABLE may be expanded and SLOT
7338                  would no longer be a valid pointer.  */
7339               entry = XCNEW (struct decl_table_entry);
7340               entry->orig = targ;
7341               entry->copy = copy;
7342               *slot = entry;
7343
7344               /* If TARG has surrounding context, copy its ancestor tree
7345                  into the new type unit.  */
7346               if (targ->die_parent != NULL
7347                   && targ->die_parent->die_tag != DW_TAG_compile_unit
7348                   && targ->die_parent->die_tag != DW_TAG_type_unit)
7349                 parent = copy_ancestor_tree (unit, targ->die_parent,
7350                                              decl_table);
7351
7352               add_child_die (parent, copy);
7353               a->dw_attr_val.v.val_die_ref.die = copy;
7354
7355               /* Make sure the newly-copied DIE is walked.  If it was
7356                  installed in a previously-added context, it won't
7357                  get visited otherwise.  */
7358               if (parent != unit)
7359                 {
7360                   /* Find the highest point of the newly-added tree,
7361                      mark each node along the way, and walk from there.  */
7362                   parent->die_mark = 1;
7363                   while (parent->die_parent
7364                          && parent->die_parent->die_mark == 0)
7365                     {
7366                       parent = parent->die_parent;
7367                       parent->die_mark = 1;
7368                     }
7369                   copy_decls_walk (unit, parent, decl_table);
7370                 }
7371             }
7372         }
7373     }
7374
7375   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
7376 }
7377
7378 /* Copy declarations for "unworthy" types into the new comdat section.
7379    Incomplete types, modified types, and certain other types aren't broken
7380    out into comdat sections of their own, so they don't have a signature,
7381    and we need to copy the declaration into the same section so that we
7382    don't have an external reference.  */
7383
7384 static void
7385 copy_decls_for_unworthy_types (dw_die_ref unit)
7386 {
7387   htab_t decl_table;
7388
7389   mark_dies (unit);
7390   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
7391   copy_decls_walk (unit, unit, decl_table);
7392   htab_delete (decl_table);
7393   unmark_dies (unit);
7394 }
7395
7396 /* Traverse the DIE and add a sibling attribute if it may have the
7397    effect of speeding up access to siblings.  To save some space,
7398    avoid generating sibling attributes for DIE's without children.  */
7399
7400 static void
7401 add_sibling_attributes (dw_die_ref die)
7402 {
7403   dw_die_ref c;
7404
7405   if (! die->die_child)
7406     return;
7407
7408   if (die->die_parent && die != die->die_parent->die_child)
7409     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
7410
7411   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
7412 }
7413
7414 /* Output all location lists for the DIE and its children.  */
7415
7416 static void
7417 output_location_lists (dw_die_ref die)
7418 {
7419   dw_die_ref c;
7420   dw_attr_ref a;
7421   unsigned ix;
7422
7423   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7424     if (AT_class (a) == dw_val_class_loc_list)
7425       output_loc_list (AT_loc_list (a));
7426
7427   FOR_EACH_CHILD (die, c, output_location_lists (c));
7428 }
7429
7430 /* The format of each DIE (and its attribute value pairs) is encoded in an
7431    abbreviation table.  This routine builds the abbreviation table and assigns
7432    a unique abbreviation id for each abbreviation entry.  The children of each
7433    die are visited recursively.  */
7434
7435 static void
7436 build_abbrev_table (dw_die_ref die)
7437 {
7438   unsigned long abbrev_id;
7439   unsigned int n_alloc;
7440   dw_die_ref c;
7441   dw_attr_ref a;
7442   unsigned ix;
7443
7444   /* Scan the DIE references, and mark as external any that refer to
7445      DIEs from other CUs (i.e. those which are not marked).  */
7446   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7447     if (AT_class (a) == dw_val_class_die_ref
7448         && AT_ref (a)->die_mark == 0)
7449       {
7450         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
7451         set_AT_ref_external (a, 1);
7452       }
7453
7454   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7455     {
7456       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7457       dw_attr_ref die_a, abbrev_a;
7458       unsigned ix;
7459       bool ok = true;
7460
7461       if (abbrev->die_tag != die->die_tag)
7462         continue;
7463       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
7464         continue;
7465
7466       if (VEC_length (dw_attr_node, abbrev->die_attr)
7467           != VEC_length (dw_attr_node, die->die_attr))
7468         continue;
7469
7470       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
7471         {
7472           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
7473           if ((abbrev_a->dw_attr != die_a->dw_attr)
7474               || (value_format (abbrev_a) != value_format (die_a)))
7475             {
7476               ok = false;
7477               break;
7478             }
7479         }
7480       if (ok)
7481         break;
7482     }
7483
7484   if (abbrev_id >= abbrev_die_table_in_use)
7485     {
7486       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
7487         {
7488           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
7489           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
7490                                             n_alloc);
7491
7492           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
7493                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
7494           abbrev_die_table_allocated = n_alloc;
7495         }
7496
7497       ++abbrev_die_table_in_use;
7498       abbrev_die_table[abbrev_id] = die;
7499     }
7500
7501   die->die_abbrev = abbrev_id;
7502   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
7503 }
7504 \f
7505 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
7506
7507 static int
7508 constant_size (unsigned HOST_WIDE_INT value)
7509 {
7510   int log;
7511
7512   if (value == 0)
7513     log = 0;
7514   else
7515     log = floor_log2 (value);
7516
7517   log = log / 8;
7518   log = 1 << (floor_log2 (log) + 1);
7519
7520   return log;
7521 }
7522
7523 /* Return the size of a DIE as it is represented in the
7524    .debug_info section.  */
7525
7526 static unsigned long
7527 size_of_die (dw_die_ref die)
7528 {
7529   unsigned long size = 0;
7530   dw_attr_ref a;
7531   unsigned ix;
7532
7533   size += size_of_uleb128 (die->die_abbrev);
7534   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7535     {
7536       switch (AT_class (a))
7537         {
7538         case dw_val_class_addr:
7539           size += DWARF2_ADDR_SIZE;
7540           break;
7541         case dw_val_class_offset:
7542           size += DWARF_OFFSET_SIZE;
7543           break;
7544         case dw_val_class_loc:
7545           {
7546             unsigned long lsize = size_of_locs (AT_loc (a));
7547
7548             /* Block length.  */
7549             if (dwarf_version >= 4)
7550               size += size_of_uleb128 (lsize);
7551             else
7552               size += constant_size (lsize);
7553             size += lsize;
7554           }
7555           break;
7556         case dw_val_class_loc_list:
7557           size += DWARF_OFFSET_SIZE;
7558           break;
7559         case dw_val_class_range_list:
7560           size += DWARF_OFFSET_SIZE;
7561           break;
7562         case dw_val_class_const:
7563           size += size_of_sleb128 (AT_int (a));
7564           break;
7565         case dw_val_class_unsigned_const:
7566           size += constant_size (AT_unsigned (a));
7567           break;
7568         case dw_val_class_const_double:
7569           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
7570           if (HOST_BITS_PER_WIDE_INT >= 64)
7571             size++; /* block */
7572           break;
7573         case dw_val_class_vec:
7574           size += constant_size (a->dw_attr_val.v.val_vec.length
7575                                  * a->dw_attr_val.v.val_vec.elt_size)
7576                   + a->dw_attr_val.v.val_vec.length
7577                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
7578           break;
7579         case dw_val_class_flag:
7580           if (dwarf_version >= 4)
7581             /* Currently all add_AT_flag calls pass in 1 as last argument,
7582                so DW_FORM_flag_present can be used.  If that ever changes,
7583                we'll need to use DW_FORM_flag and have some optimization
7584                in build_abbrev_table that will change those to
7585                DW_FORM_flag_present if it is set to 1 in all DIEs using
7586                the same abbrev entry.  */
7587             gcc_assert (a->dw_attr_val.v.val_flag == 1);
7588           else
7589             size += 1;
7590           break;
7591         case dw_val_class_die_ref:
7592           if (AT_ref_external (a))
7593             {
7594               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
7595                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
7596                  is sized by target address length, whereas in DWARF3
7597                  it's always sized as an offset.  */
7598               if (use_debug_types)
7599                 size += DWARF_TYPE_SIGNATURE_SIZE;
7600               else if (dwarf_version == 2)
7601                 size += DWARF2_ADDR_SIZE;
7602               else
7603                 size += DWARF_OFFSET_SIZE;
7604             }
7605           else
7606             size += DWARF_OFFSET_SIZE;
7607           break;
7608         case dw_val_class_fde_ref:
7609           size += DWARF_OFFSET_SIZE;
7610           break;
7611         case dw_val_class_lbl_id:
7612           size += DWARF2_ADDR_SIZE;
7613           break;
7614         case dw_val_class_lineptr:
7615         case dw_val_class_macptr:
7616           size += DWARF_OFFSET_SIZE;
7617           break;
7618         case dw_val_class_str:
7619           if (AT_string_form (a) == DW_FORM_strp)
7620             size += DWARF_OFFSET_SIZE;
7621           else
7622             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
7623           break;
7624         case dw_val_class_file:
7625           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
7626           break;
7627         case dw_val_class_data8:
7628           size += 8;
7629           break;
7630         case dw_val_class_vms_delta:
7631           size += DWARF_OFFSET_SIZE;
7632           break;
7633         default:
7634           gcc_unreachable ();
7635         }
7636     }
7637
7638   return size;
7639 }
7640
7641 /* Size the debugging information associated with a given DIE.  Visits the
7642    DIE's children recursively.  Updates the global variable next_die_offset, on
7643    each time through.  Uses the current value of next_die_offset to update the
7644    die_offset field in each DIE.  */
7645
7646 static void
7647 calc_die_sizes (dw_die_ref die)
7648 {
7649   dw_die_ref c;
7650
7651   gcc_assert (die->die_offset == 0
7652               || (unsigned long int) die->die_offset == next_die_offset);
7653   die->die_offset = next_die_offset;
7654   next_die_offset += size_of_die (die);
7655
7656   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
7657
7658   if (die->die_child != NULL)
7659     /* Count the null byte used to terminate sibling lists.  */
7660     next_die_offset += 1;
7661 }
7662
7663 /* Size just the base type children at the start of the CU.
7664    This is needed because build_abbrev needs to size locs
7665    and sizing of type based stack ops needs to know die_offset
7666    values for the base types.  */
7667
7668 static void
7669 calc_base_type_die_sizes (void)
7670 {
7671   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7672   unsigned int i;
7673   dw_die_ref base_type;
7674 #if ENABLE_ASSERT_CHECKING
7675   dw_die_ref prev = comp_unit_die ()->die_child;
7676 #endif
7677
7678   die_offset += size_of_die (comp_unit_die ());
7679   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
7680     {
7681 #if ENABLE_ASSERT_CHECKING
7682       gcc_assert (base_type->die_offset == 0
7683                   && prev->die_sib == base_type
7684                   && base_type->die_child == NULL
7685                   && base_type->die_abbrev);
7686       prev = base_type;
7687 #endif
7688       base_type->die_offset = die_offset;
7689       die_offset += size_of_die (base_type);
7690     }
7691 }
7692
7693 /* Set the marks for a die and its children.  We do this so
7694    that we know whether or not a reference needs to use FORM_ref_addr; only
7695    DIEs in the same CU will be marked.  We used to clear out the offset
7696    and use that as the flag, but ran into ordering problems.  */
7697
7698 static void
7699 mark_dies (dw_die_ref die)
7700 {
7701   dw_die_ref c;
7702
7703   gcc_assert (!die->die_mark);
7704
7705   die->die_mark = 1;
7706   FOR_EACH_CHILD (die, c, mark_dies (c));
7707 }
7708
7709 /* Clear the marks for a die and its children.  */
7710
7711 static void
7712 unmark_dies (dw_die_ref die)
7713 {
7714   dw_die_ref c;
7715
7716   if (! use_debug_types)
7717     gcc_assert (die->die_mark);
7718
7719   die->die_mark = 0;
7720   FOR_EACH_CHILD (die, c, unmark_dies (c));
7721 }
7722
7723 /* Clear the marks for a die, its children and referred dies.  */
7724
7725 static void
7726 unmark_all_dies (dw_die_ref die)
7727 {
7728   dw_die_ref c;
7729   dw_attr_ref a;
7730   unsigned ix;
7731
7732   if (!die->die_mark)
7733     return;
7734   die->die_mark = 0;
7735
7736   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
7737
7738   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7739     if (AT_class (a) == dw_val_class_die_ref)
7740       unmark_all_dies (AT_ref (a));
7741 }
7742
7743 /* Return the size of the .debug_pubnames or .debug_pubtypes table
7744    generated for the compilation unit.  */
7745
7746 static unsigned long
7747 size_of_pubnames (VEC (pubname_entry, gc) * names)
7748 {
7749   unsigned long size;
7750   unsigned i;
7751   pubname_ref p;
7752
7753   size = DWARF_PUBNAMES_HEADER_SIZE;
7754   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
7755     if (names != pubtype_table
7756         || p->die->die_offset != 0
7757         || !flag_eliminate_unused_debug_types)
7758       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
7759
7760   size += DWARF_OFFSET_SIZE;
7761   return size;
7762 }
7763
7764 /* Return the size of the information in the .debug_aranges section.  */
7765
7766 static unsigned long
7767 size_of_aranges (void)
7768 {
7769   unsigned long size;
7770
7771   size = DWARF_ARANGES_HEADER_SIZE;
7772
7773   /* Count the address/length pair for this compilation unit.  */
7774   if (text_section_used)
7775     size += 2 * DWARF2_ADDR_SIZE;
7776   if (cold_text_section_used)
7777     size += 2 * DWARF2_ADDR_SIZE;
7778   if (have_multiple_function_sections)
7779     {
7780       unsigned fde_idx;
7781       dw_fde_ref fde;
7782
7783       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
7784         {
7785           if (!fde->in_std_section)
7786             size += 2 * DWARF2_ADDR_SIZE;
7787           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
7788             size += 2 * DWARF2_ADDR_SIZE;
7789         }
7790     }
7791
7792   /* Count the two zero words used to terminated the address range table.  */
7793   size += 2 * DWARF2_ADDR_SIZE;
7794   return size;
7795 }
7796 \f
7797 /* Select the encoding of an attribute value.  */
7798
7799 static enum dwarf_form
7800 value_format (dw_attr_ref a)
7801 {
7802   switch (a->dw_attr_val.val_class)
7803     {
7804     case dw_val_class_addr:
7805       /* Only very few attributes allow DW_FORM_addr.  */
7806       switch (a->dw_attr)
7807         {
7808         case DW_AT_low_pc:
7809         case DW_AT_high_pc:
7810         case DW_AT_entry_pc:
7811         case DW_AT_trampoline:
7812           return DW_FORM_addr;
7813         default:
7814           break;
7815         }
7816       switch (DWARF2_ADDR_SIZE)
7817         {
7818         case 1:
7819           return DW_FORM_data1;
7820         case 2:
7821           return DW_FORM_data2;
7822         case 4:
7823           return DW_FORM_data4;
7824         case 8:
7825           return DW_FORM_data8;
7826         default:
7827           gcc_unreachable ();
7828         }
7829     case dw_val_class_range_list:
7830     case dw_val_class_loc_list:
7831       if (dwarf_version >= 4)
7832         return DW_FORM_sec_offset;
7833       /* FALLTHRU */
7834     case dw_val_class_vms_delta:
7835     case dw_val_class_offset:
7836       switch (DWARF_OFFSET_SIZE)
7837         {
7838         case 4:
7839           return DW_FORM_data4;
7840         case 8:
7841           return DW_FORM_data8;
7842         default:
7843           gcc_unreachable ();
7844         }
7845     case dw_val_class_loc:
7846       if (dwarf_version >= 4)
7847         return DW_FORM_exprloc;
7848       switch (constant_size (size_of_locs (AT_loc (a))))
7849         {
7850         case 1:
7851           return DW_FORM_block1;
7852         case 2:
7853           return DW_FORM_block2;
7854         default:
7855           gcc_unreachable ();
7856         }
7857     case dw_val_class_const:
7858       return DW_FORM_sdata;
7859     case dw_val_class_unsigned_const:
7860       switch (constant_size (AT_unsigned (a)))
7861         {
7862         case 1:
7863           return DW_FORM_data1;
7864         case 2:
7865           return DW_FORM_data2;
7866         case 4:
7867           return DW_FORM_data4;
7868         case 8:
7869           return DW_FORM_data8;
7870         default:
7871           gcc_unreachable ();
7872         }
7873     case dw_val_class_const_double:
7874       switch (HOST_BITS_PER_WIDE_INT)
7875         {
7876         case 8:
7877           return DW_FORM_data2;
7878         case 16:
7879           return DW_FORM_data4;
7880         case 32:
7881           return DW_FORM_data8;
7882         case 64:
7883         default:
7884           return DW_FORM_block1;
7885         }
7886     case dw_val_class_vec:
7887       switch (constant_size (a->dw_attr_val.v.val_vec.length
7888                              * a->dw_attr_val.v.val_vec.elt_size))
7889         {
7890         case 1:
7891           return DW_FORM_block1;
7892         case 2:
7893           return DW_FORM_block2;
7894         case 4:
7895           return DW_FORM_block4;
7896         default:
7897           gcc_unreachable ();
7898         }
7899     case dw_val_class_flag:
7900       if (dwarf_version >= 4)
7901         {
7902           /* Currently all add_AT_flag calls pass in 1 as last argument,
7903              so DW_FORM_flag_present can be used.  If that ever changes,
7904              we'll need to use DW_FORM_flag and have some optimization
7905              in build_abbrev_table that will change those to
7906              DW_FORM_flag_present if it is set to 1 in all DIEs using
7907              the same abbrev entry.  */
7908           gcc_assert (a->dw_attr_val.v.val_flag == 1);
7909           return DW_FORM_flag_present;
7910         }
7911       return DW_FORM_flag;
7912     case dw_val_class_die_ref:
7913       if (AT_ref_external (a))
7914         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
7915       else
7916         return DW_FORM_ref;
7917     case dw_val_class_fde_ref:
7918       return DW_FORM_data;
7919     case dw_val_class_lbl_id:
7920       return DW_FORM_addr;
7921     case dw_val_class_lineptr:
7922     case dw_val_class_macptr:
7923       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
7924     case dw_val_class_str:
7925       return AT_string_form (a);
7926     case dw_val_class_file:
7927       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7928         {
7929         case 1:
7930           return DW_FORM_data1;
7931         case 2:
7932           return DW_FORM_data2;
7933         case 4:
7934           return DW_FORM_data4;
7935         default:
7936           gcc_unreachable ();
7937         }
7938
7939     case dw_val_class_data8:
7940       return DW_FORM_data8;
7941
7942     default:
7943       gcc_unreachable ();
7944     }
7945 }
7946
7947 /* Output the encoding of an attribute value.  */
7948
7949 static void
7950 output_value_format (dw_attr_ref a)
7951 {
7952   enum dwarf_form form = value_format (a);
7953
7954   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7955 }
7956
7957 /* Output the .debug_abbrev section which defines the DIE abbreviation
7958    table.  */
7959
7960 static void
7961 output_abbrev_section (void)
7962 {
7963   unsigned long abbrev_id;
7964
7965   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7966     {
7967       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7968       unsigned ix;
7969       dw_attr_ref a_attr;
7970
7971       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7972       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7973                                    dwarf_tag_name (abbrev->die_tag));
7974
7975       if (abbrev->die_child != NULL)
7976         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7977       else
7978         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7979
7980       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7981            ix++)
7982         {
7983           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7984                                        dwarf_attr_name (a_attr->dw_attr));
7985           output_value_format (a_attr);
7986         }
7987
7988       dw2_asm_output_data (1, 0, NULL);
7989       dw2_asm_output_data (1, 0, NULL);
7990     }
7991
7992   /* Terminate the table.  */
7993   dw2_asm_output_data (1, 0, NULL);
7994 }
7995
7996 /* Output a symbol we can use to refer to this DIE from another CU.  */
7997
7998 static inline void
7999 output_die_symbol (dw_die_ref die)
8000 {
8001   char *sym = die->die_id.die_symbol;
8002
8003   if (sym == 0)
8004     return;
8005
8006   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8007     /* We make these global, not weak; if the target doesn't support
8008        .linkonce, it doesn't support combining the sections, so debugging
8009        will break.  */
8010     targetm.asm_out.globalize_label (asm_out_file, sym);
8011
8012   ASM_OUTPUT_LABEL (asm_out_file, sym);
8013 }
8014
8015 /* Return a new location list, given the begin and end range, and the
8016    expression.  */
8017
8018 static inline dw_loc_list_ref
8019 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8020               const char *section)
8021 {
8022   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8023
8024   retlist->begin = begin;
8025   retlist->end = end;
8026   retlist->expr = expr;
8027   retlist->section = section;
8028
8029   return retlist;
8030 }
8031
8032 /* Generate a new internal symbol for this location list node, if it
8033    hasn't got one yet.  */
8034
8035 static inline void
8036 gen_llsym (dw_loc_list_ref list)
8037 {
8038   gcc_assert (!list->ll_symbol);
8039   list->ll_symbol = gen_internal_sym ("LLST");
8040 }
8041
8042 /* Output the location list given to us.  */
8043
8044 static void
8045 output_loc_list (dw_loc_list_ref list_head)
8046 {
8047   dw_loc_list_ref curr = list_head;
8048
8049   if (list_head->emitted)
8050     return;
8051   list_head->emitted = true;
8052
8053   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8054
8055   /* Walk the location list, and output each range + expression.  */
8056   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8057     {
8058       unsigned long size;
8059       /* Don't output an entry that starts and ends at the same address.  */
8060       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8061         continue;
8062       if (!have_multiple_function_sections)
8063         {
8064           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8065                                 "Location list begin address (%s)",
8066                                 list_head->ll_symbol);
8067           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8068                                 "Location list end address (%s)",
8069                                 list_head->ll_symbol);
8070         }
8071       else
8072         {
8073           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8074                                "Location list begin address (%s)",
8075                                list_head->ll_symbol);
8076           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8077                                "Location list end address (%s)",
8078                                list_head->ll_symbol);
8079         }
8080       size = size_of_locs (curr->expr);
8081
8082       /* Output the block length for this list of location operations.  */
8083       gcc_assert (size <= 0xffff);
8084       dw2_asm_output_data (2, size, "%s", "Location expression size");
8085
8086       output_loc_sequence (curr->expr, -1);
8087     }
8088
8089   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8090                        "Location list terminator begin (%s)",
8091                        list_head->ll_symbol);
8092   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8093                        "Location list terminator end (%s)",
8094                        list_head->ll_symbol);
8095 }
8096
8097 /* Output a type signature.  */
8098
8099 static inline void
8100 output_signature (const char *sig, const char *name)
8101 {
8102   int i;
8103
8104   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8105     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8106 }
8107
8108 /* Output the DIE and its attributes.  Called recursively to generate
8109    the definitions of each child DIE.  */
8110
8111 static void
8112 output_die (dw_die_ref die)
8113 {
8114   dw_attr_ref a;
8115   dw_die_ref c;
8116   unsigned long size;
8117   unsigned ix;
8118
8119   /* If someone in another CU might refer to us, set up a symbol for
8120      them to point to.  */
8121   if (! use_debug_types && die->die_id.die_symbol)
8122     output_die_symbol (die);
8123
8124   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8125                                (unsigned long)die->die_offset,
8126                                dwarf_tag_name (die->die_tag));
8127
8128   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8129     {
8130       const char *name = dwarf_attr_name (a->dw_attr);
8131
8132       switch (AT_class (a))
8133         {
8134         case dw_val_class_addr:
8135           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8136           break;
8137
8138         case dw_val_class_offset:
8139           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8140                                "%s", name);
8141           break;
8142
8143         case dw_val_class_range_list:
8144           {
8145             char *p = strchr (ranges_section_label, '\0');
8146
8147             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8148                      a->dw_attr_val.v.val_offset);
8149             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8150                                    debug_ranges_section, "%s", name);
8151             *p = '\0';
8152           }
8153           break;
8154
8155         case dw_val_class_loc:
8156           size = size_of_locs (AT_loc (a));
8157
8158           /* Output the block length for this list of location operations.  */
8159           if (dwarf_version >= 4)
8160             dw2_asm_output_data_uleb128 (size, "%s", name);
8161           else
8162             dw2_asm_output_data (constant_size (size), size, "%s", name);
8163
8164           output_loc_sequence (AT_loc (a), -1);
8165           break;
8166
8167         case dw_val_class_const:
8168           /* ??? It would be slightly more efficient to use a scheme like is
8169              used for unsigned constants below, but gdb 4.x does not sign
8170              extend.  Gdb 5.x does sign extend.  */
8171           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8172           break;
8173
8174         case dw_val_class_unsigned_const:
8175           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8176                                AT_unsigned (a), "%s", name);
8177           break;
8178
8179         case dw_val_class_const_double:
8180           {
8181             unsigned HOST_WIDE_INT first, second;
8182
8183             if (HOST_BITS_PER_WIDE_INT >= 64)
8184               dw2_asm_output_data (1,
8185                                    2 * HOST_BITS_PER_WIDE_INT
8186                                    / HOST_BITS_PER_CHAR,
8187                                    NULL);
8188
8189             if (WORDS_BIG_ENDIAN)
8190               {
8191                 first = a->dw_attr_val.v.val_double.high;
8192                 second = a->dw_attr_val.v.val_double.low;
8193               }
8194             else
8195               {
8196                 first = a->dw_attr_val.v.val_double.low;
8197                 second = a->dw_attr_val.v.val_double.high;
8198               }
8199
8200             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8201                                  first, name);
8202             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8203                                  second, NULL);
8204           }
8205           break;
8206
8207         case dw_val_class_vec:
8208           {
8209             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8210             unsigned int len = a->dw_attr_val.v.val_vec.length;
8211             unsigned int i;
8212             unsigned char *p;
8213
8214             dw2_asm_output_data (constant_size (len * elt_size),
8215                                  len * elt_size, "%s", name);
8216             if (elt_size > sizeof (HOST_WIDE_INT))
8217               {
8218                 elt_size /= 2;
8219                 len *= 2;
8220               }
8221             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8222                  i < len;
8223                  i++, p += elt_size)
8224               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8225                                    "fp or vector constant word %u", i);
8226             break;
8227           }
8228
8229         case dw_val_class_flag:
8230           if (dwarf_version >= 4)
8231             {
8232               /* Currently all add_AT_flag calls pass in 1 as last argument,
8233                  so DW_FORM_flag_present can be used.  If that ever changes,
8234                  we'll need to use DW_FORM_flag and have some optimization
8235                  in build_abbrev_table that will change those to
8236                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8237                  the same abbrev entry.  */
8238               gcc_assert (AT_flag (a) == 1);
8239               if (flag_debug_asm)
8240                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8241                          ASM_COMMENT_START, name);
8242               break;
8243             }
8244           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8245           break;
8246
8247         case dw_val_class_loc_list:
8248           {
8249             char *sym = AT_loc_list (a)->ll_symbol;
8250
8251             gcc_assert (sym);
8252             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8253                                    "%s", name);
8254           }
8255           break;
8256
8257         case dw_val_class_die_ref:
8258           if (AT_ref_external (a))
8259             {
8260               if (use_debug_types)
8261                 {
8262                   comdat_type_node_ref type_node =
8263                     AT_ref (a)->die_id.die_type_node;
8264
8265                   gcc_assert (type_node);
8266                   output_signature (type_node->signature, name);
8267                 }
8268               else
8269                 {
8270                   char *sym = AT_ref (a)->die_id.die_symbol;
8271                   int size;
8272
8273                   gcc_assert (sym);
8274                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8275                      length, whereas in DWARF3 it's always sized as an
8276                      offset.  */
8277                   if (dwarf_version == 2)
8278                     size = DWARF2_ADDR_SIZE;
8279                   else
8280                     size = DWARF_OFFSET_SIZE;
8281                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8282                                          name);
8283                 }
8284             }
8285           else
8286             {
8287               gcc_assert (AT_ref (a)->die_offset);
8288               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8289                                    "%s", name);
8290             }
8291           break;
8292
8293         case dw_val_class_fde_ref:
8294           {
8295             char l1[20];
8296
8297             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8298                                          a->dw_attr_val.v.val_fde_index * 2);
8299             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8300                                    "%s", name);
8301           }
8302           break;
8303
8304         case dw_val_class_vms_delta:
8305           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8306                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8307                                     "%s", name);
8308           break;
8309
8310         case dw_val_class_lbl_id:
8311           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8312           break;
8313
8314         case dw_val_class_lineptr:
8315           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8316                                  debug_line_section, "%s", name);
8317           break;
8318
8319         case dw_val_class_macptr:
8320           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8321                                  debug_macinfo_section, "%s", name);
8322           break;
8323
8324         case dw_val_class_str:
8325           if (AT_string_form (a) == DW_FORM_strp)
8326             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
8327                                    a->dw_attr_val.v.val_str->label,
8328                                    debug_str_section,
8329                                    "%s: \"%s\"", name, AT_string (a));
8330           else
8331             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
8332           break;
8333
8334         case dw_val_class_file:
8335           {
8336             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
8337
8338             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
8339                                  a->dw_attr_val.v.val_file->filename);
8340             break;
8341           }
8342
8343         case dw_val_class_data8:
8344           {
8345             int i;
8346
8347             for (i = 0; i < 8; i++)
8348               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
8349                                    i == 0 ? "%s" : NULL, name);
8350             break;
8351           }
8352
8353         default:
8354           gcc_unreachable ();
8355         }
8356     }
8357
8358   FOR_EACH_CHILD (die, c, output_die (c));
8359
8360   /* Add null byte to terminate sibling list.  */
8361   if (die->die_child != NULL)
8362     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
8363                          (unsigned long) die->die_offset);
8364 }
8365
8366 /* Output the compilation unit that appears at the beginning of the
8367    .debug_info section, and precedes the DIE descriptions.  */
8368
8369 static void
8370 output_compilation_unit_header (void)
8371 {
8372   int ver = dwarf_version;
8373
8374   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8375     dw2_asm_output_data (4, 0xffffffff,
8376       "Initial length escape value indicating 64-bit DWARF extension");
8377   dw2_asm_output_data (DWARF_OFFSET_SIZE,
8378                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
8379                        "Length of Compilation Unit Info");
8380   dw2_asm_output_data (2, ver, "DWARF version number");
8381   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
8382                          debug_abbrev_section,
8383                          "Offset Into Abbrev. Section");
8384   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
8385 }
8386
8387 /* Output the compilation unit DIE and its children.  */
8388
8389 static void
8390 output_comp_unit (dw_die_ref die, int output_if_empty)
8391 {
8392   const char *secname;
8393   char *oldsym, *tmp;
8394
8395   /* Unless we are outputting main CU, we may throw away empty ones.  */
8396   if (!output_if_empty && die->die_child == NULL)
8397     return;
8398
8399   /* Even if there are no children of this DIE, we must output the information
8400      about the compilation unit.  Otherwise, on an empty translation unit, we
8401      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
8402      will then complain when examining the file.  First mark all the DIEs in
8403      this CU so we know which get local refs.  */
8404   mark_dies (die);
8405
8406   build_abbrev_table (die);
8407
8408   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8409   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8410   calc_die_sizes (die);
8411
8412   oldsym = die->die_id.die_symbol;
8413   if (oldsym)
8414     {
8415       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
8416
8417       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
8418       secname = tmp;
8419       die->die_id.die_symbol = NULL;
8420       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8421     }
8422   else
8423     {
8424       switch_to_section (debug_info_section);
8425       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
8426       info_section_emitted = true;
8427     }
8428
8429   /* Output debugging information.  */
8430   output_compilation_unit_header ();
8431   output_die (die);
8432
8433   /* Leave the marks on the main CU, so we can check them in
8434      output_pubnames.  */
8435   if (oldsym)
8436     {
8437       unmark_dies (die);
8438       die->die_id.die_symbol = oldsym;
8439     }
8440 }
8441
8442 /* Output a comdat type unit DIE and its children.  */
8443
8444 static void
8445 output_comdat_type_unit (comdat_type_node *node)
8446 {
8447   const char *secname;
8448   char *tmp;
8449   int i;
8450 #if defined (OBJECT_FORMAT_ELF)
8451   tree comdat_key;
8452 #endif
8453
8454   /* First mark all the DIEs in this CU so we know which get local refs.  */
8455   mark_dies (node->root_die);
8456
8457   build_abbrev_table (node->root_die);
8458
8459   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
8460   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
8461   calc_die_sizes (node->root_die);
8462
8463 #if defined (OBJECT_FORMAT_ELF)
8464   secname = ".debug_types";
8465   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8466   sprintf (tmp, "wt.");
8467   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8468     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
8469   comdat_key = get_identifier (tmp);
8470   targetm.asm_out.named_section (secname,
8471                                  SECTION_DEBUG | SECTION_LINKONCE,
8472                                  comdat_key);
8473 #else
8474   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
8475   sprintf (tmp, ".gnu.linkonce.wt.");
8476   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8477     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
8478   secname = tmp;
8479   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
8480 #endif
8481
8482   /* Output debugging information.  */
8483   output_compilation_unit_header ();
8484   output_signature (node->signature, "Type Signature");
8485   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
8486                        "Offset to Type DIE");
8487   output_die (node->root_die);
8488
8489   unmark_dies (node->root_die);
8490 }
8491
8492 /* Return the DWARF2/3 pubname associated with a decl.  */
8493
8494 static const char *
8495 dwarf2_name (tree decl, int scope)
8496 {
8497   if (DECL_NAMELESS (decl))
8498     return NULL;
8499   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
8500 }
8501
8502 /* Add a new entry to .debug_pubnames if appropriate.  */
8503
8504 static void
8505 add_pubname_string (const char *str, dw_die_ref die)
8506 {
8507   if (targetm.want_debug_pub_sections)
8508     {
8509       pubname_entry e;
8510
8511       e.die = die;
8512       e.name = xstrdup (str);
8513       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
8514     }
8515 }
8516
8517 static void
8518 add_pubname (tree decl, dw_die_ref die)
8519 {
8520   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
8521     {
8522       const char *name = dwarf2_name (decl, 1);
8523       if (name)
8524         add_pubname_string (name, die);
8525     }
8526 }
8527
8528 /* Add a new entry to .debug_pubtypes if appropriate.  */
8529
8530 static void
8531 add_pubtype (tree decl, dw_die_ref die)
8532 {
8533   pubname_entry e;
8534
8535   if (!targetm.want_debug_pub_sections)
8536     return;
8537
8538   e.name = NULL;
8539   if ((TREE_PUBLIC (decl)
8540        || is_cu_die (die->die_parent))
8541       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
8542     {
8543       e.die = die;
8544       if (TYPE_P (decl))
8545         {
8546           if (TYPE_NAME (decl))
8547             {
8548               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
8549                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
8550               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
8551                        && DECL_NAME (TYPE_NAME (decl)))
8552                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
8553               else
8554                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
8555             }
8556         }
8557       else
8558         {
8559           e.name = dwarf2_name (decl, 1);
8560           if (e.name)
8561             e.name = xstrdup (e.name);
8562         }
8563
8564       /* If we don't have a name for the type, there's no point in adding
8565          it to the table.  */
8566       if (e.name && e.name[0] != '\0')
8567         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
8568     }
8569 }
8570
8571 /* Output the public names table used to speed up access to externally
8572    visible names; or the public types table used to find type definitions.  */
8573
8574 static void
8575 output_pubnames (VEC (pubname_entry, gc) * names)
8576 {
8577   unsigned i;
8578   unsigned long pubnames_length = size_of_pubnames (names);
8579   pubname_ref pub;
8580
8581   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8582     dw2_asm_output_data (4, 0xffffffff,
8583       "Initial length escape value indicating 64-bit DWARF extension");
8584   if (names == pubname_table)
8585     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8586                          "Length of Public Names Info");
8587   else
8588     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
8589                          "Length of Public Type Names Info");
8590   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
8591   dw2_asm_output_data (2, 2, "DWARF Version");
8592   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8593                          debug_info_section,
8594                          "Offset of Compilation Unit Info");
8595   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
8596                        "Compilation Unit Length");
8597
8598   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
8599     {
8600       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
8601       if (names == pubname_table)
8602         gcc_assert (pub->die->die_mark);
8603
8604       if (names != pubtype_table
8605           || pub->die->die_offset != 0
8606           || !flag_eliminate_unused_debug_types)
8607         {
8608           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
8609                                "DIE offset");
8610
8611           dw2_asm_output_nstring (pub->name, -1, "external name");
8612         }
8613     }
8614
8615   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
8616 }
8617
8618 /* Output the information that goes into the .debug_aranges table.
8619    Namely, define the beginning and ending address range of the
8620    text section generated for this compilation unit.  */
8621
8622 static void
8623 output_aranges (unsigned long aranges_length)
8624 {
8625   unsigned i;
8626
8627   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8628     dw2_asm_output_data (4, 0xffffffff,
8629       "Initial length escape value indicating 64-bit DWARF extension");
8630   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
8631                        "Length of Address Ranges Info");
8632   /* Version number for aranges is still 2, even in DWARF3.  */
8633   dw2_asm_output_data (2, 2, "DWARF Version");
8634   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
8635                          debug_info_section,
8636                          "Offset of Compilation Unit Info");
8637   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
8638   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
8639
8640   /* We need to align to twice the pointer size here.  */
8641   if (DWARF_ARANGES_PAD_SIZE)
8642     {
8643       /* Pad using a 2 byte words so that padding is correct for any
8644          pointer size.  */
8645       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
8646                            2 * DWARF2_ADDR_SIZE);
8647       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
8648         dw2_asm_output_data (2, 0, NULL);
8649     }
8650
8651   /* It is necessary not to output these entries if the sections were
8652      not used; if the sections were not used, the length will be 0 and
8653      the address may end up as 0 if the section is discarded by ld
8654      --gc-sections, leaving an invalid (0, 0) entry that can be
8655      confused with the terminator.  */
8656   if (text_section_used)
8657     {
8658       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
8659       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
8660                             text_section_label, "Length");
8661     }
8662   if (cold_text_section_used)
8663     {
8664       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
8665                            "Address");
8666       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
8667                             cold_text_section_label, "Length");
8668     }
8669
8670   if (have_multiple_function_sections)
8671     {
8672       unsigned fde_idx;
8673       dw_fde_ref fde;
8674
8675       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
8676         {
8677           if (!fde->in_std_section)
8678             {
8679               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
8680                                    "Address");
8681               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
8682                                     fde->dw_fde_begin, "Length");
8683             }
8684           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8685             {
8686               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
8687                                    "Address");
8688               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
8689                                     fde->dw_fde_second_begin, "Length");
8690             }
8691         }
8692     }
8693
8694   /* Output the terminator words.  */
8695   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8696   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8697 }
8698
8699 /* Add a new entry to .debug_ranges.  Return the offset at which it
8700    was placed.  */
8701
8702 static unsigned int
8703 add_ranges_num (int num)
8704 {
8705   unsigned int in_use = ranges_table_in_use;
8706
8707   if (in_use == ranges_table_allocated)
8708     {
8709       ranges_table_allocated += RANGES_TABLE_INCREMENT;
8710       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
8711                                     ranges_table_allocated);
8712       memset (ranges_table + ranges_table_in_use, 0,
8713               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
8714     }
8715
8716   ranges_table[in_use].num = num;
8717   ranges_table_in_use = in_use + 1;
8718
8719   return in_use * 2 * DWARF2_ADDR_SIZE;
8720 }
8721
8722 /* Add a new entry to .debug_ranges corresponding to a block, or a
8723    range terminator if BLOCK is NULL.  */
8724
8725 static unsigned int
8726 add_ranges (const_tree block)
8727 {
8728   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
8729 }
8730
8731 /* Add a new entry to .debug_ranges corresponding to a pair of
8732    labels.  */
8733
8734 static void
8735 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
8736                       bool *added)
8737 {
8738   unsigned int in_use = ranges_by_label_in_use;
8739   unsigned int offset;
8740
8741   if (in_use == ranges_by_label_allocated)
8742     {
8743       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
8744       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
8745                                        ranges_by_label,
8746                                        ranges_by_label_allocated);
8747       memset (ranges_by_label + ranges_by_label_in_use, 0,
8748               RANGES_TABLE_INCREMENT
8749               * sizeof (struct dw_ranges_by_label_struct));
8750     }
8751
8752   ranges_by_label[in_use].begin = begin;
8753   ranges_by_label[in_use].end = end;
8754   ranges_by_label_in_use = in_use + 1;
8755
8756   offset = add_ranges_num (-(int)in_use - 1);
8757   if (!*added)
8758     {
8759       add_AT_range_list (die, DW_AT_ranges, offset);
8760       *added = true;
8761     }
8762 }
8763
8764 static void
8765 output_ranges (void)
8766 {
8767   unsigned i;
8768   static const char *const start_fmt = "Offset %#x";
8769   const char *fmt = start_fmt;
8770
8771   for (i = 0; i < ranges_table_in_use; i++)
8772     {
8773       int block_num = ranges_table[i].num;
8774
8775       if (block_num > 0)
8776         {
8777           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
8778           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
8779
8780           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
8781           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
8782
8783           /* If all code is in the text section, then the compilation
8784              unit base address defaults to DW_AT_low_pc, which is the
8785              base of the text section.  */
8786           if (!have_multiple_function_sections)
8787             {
8788               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
8789                                     text_section_label,
8790                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8791               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
8792                                     text_section_label, NULL);
8793             }
8794
8795           /* Otherwise, the compilation unit base address is zero,
8796              which allows us to use absolute addresses, and not worry
8797              about whether the target supports cross-section
8798              arithmetic.  */
8799           else
8800             {
8801               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
8802                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8803               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
8804             }
8805
8806           fmt = NULL;
8807         }
8808
8809       /* Negative block_num stands for an index into ranges_by_label.  */
8810       else if (block_num < 0)
8811         {
8812           int lab_idx = - block_num - 1;
8813
8814           if (!have_multiple_function_sections)
8815             {
8816               gcc_unreachable ();
8817 #if 0
8818               /* If we ever use add_ranges_by_labels () for a single
8819                  function section, all we have to do is to take out
8820                  the #if 0 above.  */
8821               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8822                                     ranges_by_label[lab_idx].begin,
8823                                     text_section_label,
8824                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
8825               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
8826                                     ranges_by_label[lab_idx].end,
8827                                     text_section_label, NULL);
8828 #endif
8829             }
8830           else
8831             {
8832               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8833                                    ranges_by_label[lab_idx].begin,
8834                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
8835               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
8836                                    ranges_by_label[lab_idx].end,
8837                                    NULL);
8838             }
8839         }
8840       else
8841         {
8842           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8843           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
8844           fmt = start_fmt;
8845         }
8846     }
8847 }
8848
8849 /* Data structure containing information about input files.  */
8850 struct file_info
8851 {
8852   const char *path;     /* Complete file name.  */
8853   const char *fname;    /* File name part.  */
8854   int length;           /* Length of entire string.  */
8855   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
8856   int dir_idx;          /* Index in directory table.  */
8857 };
8858
8859 /* Data structure containing information about directories with source
8860    files.  */
8861 struct dir_info
8862 {
8863   const char *path;     /* Path including directory name.  */
8864   int length;           /* Path length.  */
8865   int prefix;           /* Index of directory entry which is a prefix.  */
8866   int count;            /* Number of files in this directory.  */
8867   int dir_idx;          /* Index of directory used as base.  */
8868 };
8869
8870 /* Callback function for file_info comparison.  We sort by looking at
8871    the directories in the path.  */
8872
8873 static int
8874 file_info_cmp (const void *p1, const void *p2)
8875 {
8876   const struct file_info *const s1 = (const struct file_info *) p1;
8877   const struct file_info *const s2 = (const struct file_info *) p2;
8878   const unsigned char *cp1;
8879   const unsigned char *cp2;
8880
8881   /* Take care of file names without directories.  We need to make sure that
8882      we return consistent values to qsort since some will get confused if
8883      we return the same value when identical operands are passed in opposite
8884      orders.  So if neither has a directory, return 0 and otherwise return
8885      1 or -1 depending on which one has the directory.  */
8886   if ((s1->path == s1->fname || s2->path == s2->fname))
8887     return (s2->path == s2->fname) - (s1->path == s1->fname);
8888
8889   cp1 = (const unsigned char *) s1->path;
8890   cp2 = (const unsigned char *) s2->path;
8891
8892   while (1)
8893     {
8894       ++cp1;
8895       ++cp2;
8896       /* Reached the end of the first path?  If so, handle like above.  */
8897       if ((cp1 == (const unsigned char *) s1->fname)
8898           || (cp2 == (const unsigned char *) s2->fname))
8899         return ((cp2 == (const unsigned char *) s2->fname)
8900                 - (cp1 == (const unsigned char *) s1->fname));
8901
8902       /* Character of current path component the same?  */
8903       else if (*cp1 != *cp2)
8904         return *cp1 - *cp2;
8905     }
8906 }
8907
8908 struct file_name_acquire_data
8909 {
8910   struct file_info *files;
8911   int used_files;
8912   int max_files;
8913 };
8914
8915 /* Traversal function for the hash table.  */
8916
8917 static int
8918 file_name_acquire (void ** slot, void *data)
8919 {
8920   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
8921   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
8922   struct file_info *fi;
8923   const char *f;
8924
8925   gcc_assert (fnad->max_files >= d->emitted_number);
8926
8927   if (! d->emitted_number)
8928     return 1;
8929
8930   gcc_assert (fnad->max_files != fnad->used_files);
8931
8932   fi = fnad->files + fnad->used_files++;
8933
8934   /* Skip all leading "./".  */
8935   f = d->filename;
8936   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
8937     f += 2;
8938
8939   /* Create a new array entry.  */
8940   fi->path = f;
8941   fi->length = strlen (f);
8942   fi->file_idx = d;
8943
8944   /* Search for the file name part.  */
8945   f = strrchr (f, DIR_SEPARATOR);
8946 #if defined (DIR_SEPARATOR_2)
8947   {
8948     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
8949
8950     if (g != NULL)
8951       {
8952         if (f == NULL || f < g)
8953           f = g;
8954       }
8955   }
8956 #endif
8957
8958   fi->fname = f == NULL ? fi->path : f + 1;
8959   return 1;
8960 }
8961
8962 /* Output the directory table and the file name table.  We try to minimize
8963    the total amount of memory needed.  A heuristic is used to avoid large
8964    slowdowns with many input files.  */
8965
8966 static void
8967 output_file_names (void)
8968 {
8969   struct file_name_acquire_data fnad;
8970   int numfiles;
8971   struct file_info *files;
8972   struct dir_info *dirs;
8973   int *saved;
8974   int *savehere;
8975   int *backmap;
8976   int ndirs;
8977   int idx_offset;
8978   int i;
8979
8980   if (!last_emitted_file)
8981     {
8982       dw2_asm_output_data (1, 0, "End directory table");
8983       dw2_asm_output_data (1, 0, "End file name table");
8984       return;
8985     }
8986
8987   numfiles = last_emitted_file->emitted_number;
8988
8989   /* Allocate the various arrays we need.  */
8990   files = XALLOCAVEC (struct file_info, numfiles);
8991   dirs = XALLOCAVEC (struct dir_info, numfiles);
8992
8993   fnad.files = files;
8994   fnad.used_files = 0;
8995   fnad.max_files = numfiles;
8996   htab_traverse (file_table, file_name_acquire, &fnad);
8997   gcc_assert (fnad.used_files == fnad.max_files);
8998
8999   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9000
9001   /* Find all the different directories used.  */
9002   dirs[0].path = files[0].path;
9003   dirs[0].length = files[0].fname - files[0].path;
9004   dirs[0].prefix = -1;
9005   dirs[0].count = 1;
9006   dirs[0].dir_idx = 0;
9007   files[0].dir_idx = 0;
9008   ndirs = 1;
9009
9010   for (i = 1; i < numfiles; i++)
9011     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9012         && memcmp (dirs[ndirs - 1].path, files[i].path,
9013                    dirs[ndirs - 1].length) == 0)
9014       {
9015         /* Same directory as last entry.  */
9016         files[i].dir_idx = ndirs - 1;
9017         ++dirs[ndirs - 1].count;
9018       }
9019     else
9020       {
9021         int j;
9022
9023         /* This is a new directory.  */
9024         dirs[ndirs].path = files[i].path;
9025         dirs[ndirs].length = files[i].fname - files[i].path;
9026         dirs[ndirs].count = 1;
9027         dirs[ndirs].dir_idx = ndirs;
9028         files[i].dir_idx = ndirs;
9029
9030         /* Search for a prefix.  */
9031         dirs[ndirs].prefix = -1;
9032         for (j = 0; j < ndirs; j++)
9033           if (dirs[j].length < dirs[ndirs].length
9034               && dirs[j].length > 1
9035               && (dirs[ndirs].prefix == -1
9036                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9037               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9038             dirs[ndirs].prefix = j;
9039
9040         ++ndirs;
9041       }
9042
9043   /* Now to the actual work.  We have to find a subset of the directories which
9044      allow expressing the file name using references to the directory table
9045      with the least amount of characters.  We do not do an exhaustive search
9046      where we would have to check out every combination of every single
9047      possible prefix.  Instead we use a heuristic which provides nearly optimal
9048      results in most cases and never is much off.  */
9049   saved = XALLOCAVEC (int, ndirs);
9050   savehere = XALLOCAVEC (int, ndirs);
9051
9052   memset (saved, '\0', ndirs * sizeof (saved[0]));
9053   for (i = 0; i < ndirs; i++)
9054     {
9055       int j;
9056       int total;
9057
9058       /* We can always save some space for the current directory.  But this
9059          does not mean it will be enough to justify adding the directory.  */
9060       savehere[i] = dirs[i].length;
9061       total = (savehere[i] - saved[i]) * dirs[i].count;
9062
9063       for (j = i + 1; j < ndirs; j++)
9064         {
9065           savehere[j] = 0;
9066           if (saved[j] < dirs[i].length)
9067             {
9068               /* Determine whether the dirs[i] path is a prefix of the
9069                  dirs[j] path.  */
9070               int k;
9071
9072               k = dirs[j].prefix;
9073               while (k != -1 && k != (int) i)
9074                 k = dirs[k].prefix;
9075
9076               if (k == (int) i)
9077                 {
9078                   /* Yes it is.  We can possibly save some memory by
9079                      writing the filenames in dirs[j] relative to
9080                      dirs[i].  */
9081                   savehere[j] = dirs[i].length;
9082                   total += (savehere[j] - saved[j]) * dirs[j].count;
9083                 }
9084             }
9085         }
9086
9087       /* Check whether we can save enough to justify adding the dirs[i]
9088          directory.  */
9089       if (total > dirs[i].length + 1)
9090         {
9091           /* It's worthwhile adding.  */
9092           for (j = i; j < ndirs; j++)
9093             if (savehere[j] > 0)
9094               {
9095                 /* Remember how much we saved for this directory so far.  */
9096                 saved[j] = savehere[j];
9097
9098                 /* Remember the prefix directory.  */
9099                 dirs[j].dir_idx = i;
9100               }
9101         }
9102     }
9103
9104   /* Emit the directory name table.  */
9105   idx_offset = dirs[0].length > 0 ? 1 : 0;
9106   for (i = 1 - idx_offset; i < ndirs; i++)
9107     dw2_asm_output_nstring (dirs[i].path,
9108                             dirs[i].length
9109                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9110                             "Directory Entry: %#x", i + idx_offset);
9111
9112   dw2_asm_output_data (1, 0, "End directory table");
9113
9114   /* We have to emit them in the order of emitted_number since that's
9115      used in the debug info generation.  To do this efficiently we
9116      generate a back-mapping of the indices first.  */
9117   backmap = XALLOCAVEC (int, numfiles);
9118   for (i = 0; i < numfiles; i++)
9119     backmap[files[i].file_idx->emitted_number - 1] = i;
9120
9121   /* Now write all the file names.  */
9122   for (i = 0; i < numfiles; i++)
9123     {
9124       int file_idx = backmap[i];
9125       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9126
9127 #ifdef VMS_DEBUGGING_INFO
9128 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9129
9130       /* Setting these fields can lead to debugger miscomparisons,
9131          but VMS Debug requires them to be set correctly.  */
9132
9133       int ver;
9134       long long cdt;
9135       long siz;
9136       int maxfilelen = strlen (files[file_idx].path)
9137                                + dirs[dir_idx].length
9138                                + MAX_VMS_VERSION_LEN + 1;
9139       char *filebuf = XALLOCAVEC (char, maxfilelen);
9140
9141       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9142       snprintf (filebuf, maxfilelen, "%s;%d",
9143                 files[file_idx].path + dirs[dir_idx].length, ver);
9144
9145       dw2_asm_output_nstring
9146         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9147
9148       /* Include directory index.  */
9149       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9150
9151       /* Modification time.  */
9152       dw2_asm_output_data_uleb128
9153         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9154           ? cdt : 0,
9155          NULL);
9156
9157       /* File length in bytes.  */
9158       dw2_asm_output_data_uleb128
9159         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9160           ? siz : 0,
9161          NULL);
9162 #else
9163       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9164                               "File Entry: %#x", (unsigned) i + 1);
9165
9166       /* Include directory index.  */
9167       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9168
9169       /* Modification time.  */
9170       dw2_asm_output_data_uleb128 (0, NULL);
9171
9172       /* File length in bytes.  */
9173       dw2_asm_output_data_uleb128 (0, NULL);
9174 #endif /* VMS_DEBUGGING_INFO */
9175     }
9176
9177   dw2_asm_output_data (1, 0, "End file name table");
9178 }
9179
9180
9181 /* Output one line number table into the .debug_line section.  */
9182
9183 static void
9184 output_one_line_info_table (dw_line_info_table *table)
9185 {
9186   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9187   unsigned int current_line = 1;
9188   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9189   dw_line_info_entry *ent;
9190   size_t i;
9191
9192   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9193     {
9194       switch (ent->opcode)
9195         {
9196         case LI_set_address:
9197           /* ??? Unfortunately, we have little choice here currently, and
9198              must always use the most general form.  GCC does not know the
9199              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9200              ports do have length attributes which will give an upper bound
9201              on the address range.  We could perhaps use length attributes
9202              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9203           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9204
9205           /* This can handle any delta.  This takes
9206              4+DWARF2_ADDR_SIZE bytes.  */
9207           dw2_asm_output_data (1, 0, "set address %s", line_label);
9208           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9209           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9210           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9211           break;
9212
9213         case LI_set_line:
9214           if (ent->val == current_line)
9215             {
9216               /* We still need to start a new row, so output a copy insn.  */
9217               dw2_asm_output_data (1, DW_LNS_copy,
9218                                    "copy line %u", current_line);
9219             }
9220           else
9221             {
9222               int line_offset = ent->val - current_line;
9223               int line_delta = line_offset - DWARF_LINE_BASE;
9224
9225               current_line = ent->val;
9226               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9227                 {
9228                   /* This can handle deltas from -10 to 234, using the current
9229                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9230                      This takes 1 byte.  */
9231                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9232                                        "line %u", current_line);
9233                 }
9234               else
9235                 {
9236                   /* This can handle any delta.  This takes at least 4 bytes,
9237                      depending on the value being encoded.  */
9238                   dw2_asm_output_data (1, DW_LNS_advance_line,
9239                                        "advance to line %u", current_line);
9240                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9241                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9242                 }
9243             }
9244           break;
9245
9246         case LI_set_file:
9247           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9248           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9249           break;
9250
9251         case LI_set_column:
9252           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9253           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9254           break;
9255
9256         case LI_negate_stmt:
9257           current_is_stmt = !current_is_stmt;
9258           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9259                                "is_stmt %d", current_is_stmt);
9260           break;
9261
9262         case LI_set_prologue_end:
9263           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9264                                "set prologue end");
9265           break;
9266           
9267         case LI_set_epilogue_begin:
9268           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9269                                "set epilogue begin");
9270           break;
9271
9272         case LI_set_discriminator:
9273           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9274           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9275           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9276           dw2_asm_output_data_uleb128 (ent->val, NULL);
9277           break;
9278         }
9279     }
9280
9281   /* Emit debug info for the address of the end of the table.  */
9282   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9283   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9284   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9285   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9286
9287   dw2_asm_output_data (1, 0, "end sequence");
9288   dw2_asm_output_data_uleb128 (1, NULL);
9289   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9290 }
9291
9292 /* Output the source line number correspondence information.  This
9293    information goes into the .debug_line section.  */
9294
9295 static void
9296 output_line_info (void)
9297 {
9298   char l1[20], l2[20], p1[20], p2[20];
9299   int ver = dwarf_version;
9300   bool saw_one = false;
9301   int opc;
9302
9303   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9304   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9305   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9306   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9307
9308   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9309     dw2_asm_output_data (4, 0xffffffff,
9310       "Initial length escape value indicating 64-bit DWARF extension");
9311   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9312                         "Length of Source Line Info");
9313   ASM_OUTPUT_LABEL (asm_out_file, l1);
9314
9315   dw2_asm_output_data (2, ver, "DWARF Version");
9316   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9317   ASM_OUTPUT_LABEL (asm_out_file, p1);
9318
9319   /* Define the architecture-dependent minimum instruction length (in bytes).
9320      In this implementation of DWARF, this field is used for information
9321      purposes only.  Since GCC generates assembly language, we have no
9322      a priori knowledge of how many instruction bytes are generated for each
9323      source line, and therefore can use only the DW_LNE_set_address and
9324      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
9325      this as '1', which is "correct enough" for all architectures,
9326      and don't let the target override.  */
9327   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
9328
9329   if (ver >= 4)
9330     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
9331                          "Maximum Operations Per Instruction");
9332   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9333                        "Default is_stmt_start flag");
9334   dw2_asm_output_data (1, DWARF_LINE_BASE,
9335                        "Line Base Value (Special Opcodes)");
9336   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9337                        "Line Range Value (Special Opcodes)");
9338   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9339                        "Special Opcode Base");
9340
9341   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9342     {
9343       int n_op_args;
9344       switch (opc)
9345         {
9346         case DW_LNS_advance_pc:
9347         case DW_LNS_advance_line:
9348         case DW_LNS_set_file:
9349         case DW_LNS_set_column:
9350         case DW_LNS_fixed_advance_pc:
9351         case DW_LNS_set_isa:
9352           n_op_args = 1;
9353           break;
9354         default:
9355           n_op_args = 0;
9356           break;
9357         }
9358
9359       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
9360                            opc, n_op_args);
9361     }
9362
9363   /* Write out the information about the files we use.  */
9364   output_file_names ();
9365   ASM_OUTPUT_LABEL (asm_out_file, p2);
9366
9367   if (separate_line_info)
9368     {
9369       dw_line_info_table *table;
9370       size_t i;
9371
9372       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
9373         if (table->in_use)
9374           {
9375             output_one_line_info_table (table);
9376             saw_one = true;
9377           }
9378     }
9379   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
9380     {
9381       output_one_line_info_table (cold_text_section_line_info);
9382       saw_one = true;
9383     }
9384
9385   /* ??? Some Darwin linkers crash on a .debug_line section with no
9386      sequences.  Further, merely a DW_LNE_end_sequence entry is not
9387      sufficient -- the address column must also be initialized.
9388      Make sure to output at least one set_address/end_sequence pair,
9389      choosing .text since that section is always present.  */
9390   if (text_section_line_info->in_use || !saw_one)
9391     output_one_line_info_table (text_section_line_info);
9392
9393   /* Output the marker for the end of the line number info.  */
9394   ASM_OUTPUT_LABEL (asm_out_file, l2);
9395 }
9396 \f
9397 /* Given a pointer to a tree node for some base type, return a pointer to
9398    a DIE that describes the given type.
9399
9400    This routine must only be called for GCC type nodes that correspond to
9401    Dwarf base (fundamental) types.  */
9402
9403 static dw_die_ref
9404 base_type_die (tree type)
9405 {
9406   dw_die_ref base_type_result;
9407   enum dwarf_type encoding;
9408
9409   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
9410     return 0;
9411
9412   /* If this is a subtype that should not be emitted as a subrange type,
9413      use the base type.  See subrange_type_for_debug_p.  */
9414   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
9415     type = TREE_TYPE (type);
9416
9417   switch (TREE_CODE (type))
9418     {
9419     case INTEGER_TYPE:
9420       if ((dwarf_version >= 4 || !dwarf_strict)
9421           && TYPE_NAME (type)
9422           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
9423           && DECL_IS_BUILTIN (TYPE_NAME (type))
9424           && DECL_NAME (TYPE_NAME (type)))
9425         {
9426           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
9427           if (strcmp (name, "char16_t") == 0
9428               || strcmp (name, "char32_t") == 0)
9429             {
9430               encoding = DW_ATE_UTF;
9431               break;
9432             }
9433         }
9434       if (TYPE_STRING_FLAG (type))
9435         {
9436           if (TYPE_UNSIGNED (type))
9437             encoding = DW_ATE_unsigned_char;
9438           else
9439             encoding = DW_ATE_signed_char;
9440         }
9441       else if (TYPE_UNSIGNED (type))
9442         encoding = DW_ATE_unsigned;
9443       else
9444         encoding = DW_ATE_signed;
9445       break;
9446
9447     case REAL_TYPE:
9448       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
9449         {
9450           if (dwarf_version >= 3 || !dwarf_strict)
9451             encoding = DW_ATE_decimal_float;
9452           else
9453             encoding = DW_ATE_lo_user;
9454         }
9455       else
9456         encoding = DW_ATE_float;
9457       break;
9458
9459     case FIXED_POINT_TYPE:
9460       if (!(dwarf_version >= 3 || !dwarf_strict))
9461         encoding = DW_ATE_lo_user;
9462       else if (TYPE_UNSIGNED (type))
9463         encoding = DW_ATE_unsigned_fixed;
9464       else
9465         encoding = DW_ATE_signed_fixed;
9466       break;
9467
9468       /* Dwarf2 doesn't know anything about complex ints, so use
9469          a user defined type for it.  */
9470     case COMPLEX_TYPE:
9471       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
9472         encoding = DW_ATE_complex_float;
9473       else
9474         encoding = DW_ATE_lo_user;
9475       break;
9476
9477     case BOOLEAN_TYPE:
9478       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
9479       encoding = DW_ATE_boolean;
9480       break;
9481
9482     default:
9483       /* No other TREE_CODEs are Dwarf fundamental types.  */
9484       gcc_unreachable ();
9485     }
9486
9487   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
9488
9489   add_AT_unsigned (base_type_result, DW_AT_byte_size,
9490                    int_size_in_bytes (type));
9491   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
9492
9493   return base_type_result;
9494 }
9495
9496 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
9497    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
9498
9499 static inline int
9500 is_base_type (tree type)
9501 {
9502   switch (TREE_CODE (type))
9503     {
9504     case ERROR_MARK:
9505     case VOID_TYPE:
9506     case INTEGER_TYPE:
9507     case REAL_TYPE:
9508     case FIXED_POINT_TYPE:
9509     case COMPLEX_TYPE:
9510     case BOOLEAN_TYPE:
9511       return 1;
9512
9513     case ARRAY_TYPE:
9514     case RECORD_TYPE:
9515     case UNION_TYPE:
9516     case QUAL_UNION_TYPE:
9517     case ENUMERAL_TYPE:
9518     case FUNCTION_TYPE:
9519     case METHOD_TYPE:
9520     case POINTER_TYPE:
9521     case REFERENCE_TYPE:
9522     case NULLPTR_TYPE:
9523     case OFFSET_TYPE:
9524     case LANG_TYPE:
9525     case VECTOR_TYPE:
9526       return 0;
9527
9528     default:
9529       gcc_unreachable ();
9530     }
9531
9532   return 0;
9533 }
9534
9535 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
9536    node, return the size in bits for the type if it is a constant, or else
9537    return the alignment for the type if the type's size is not constant, or
9538    else return BITS_PER_WORD if the type actually turns out to be an
9539    ERROR_MARK node.  */
9540
9541 static inline unsigned HOST_WIDE_INT
9542 simple_type_size_in_bits (const_tree type)
9543 {
9544   if (TREE_CODE (type) == ERROR_MARK)
9545     return BITS_PER_WORD;
9546   else if (TYPE_SIZE (type) == NULL_TREE)
9547     return 0;
9548   else if (host_integerp (TYPE_SIZE (type), 1))
9549     return tree_low_cst (TYPE_SIZE (type), 1);
9550   else
9551     return TYPE_ALIGN (type);
9552 }
9553
9554 /* Similarly, but return a double_int instead of UHWI.  */
9555
9556 static inline double_int
9557 double_int_type_size_in_bits (const_tree type)
9558 {
9559   if (TREE_CODE (type) == ERROR_MARK)
9560     return uhwi_to_double_int (BITS_PER_WORD);
9561   else if (TYPE_SIZE (type) == NULL_TREE)
9562     return double_int_zero;
9563   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
9564     return tree_to_double_int (TYPE_SIZE (type));
9565   else
9566     return uhwi_to_double_int (TYPE_ALIGN (type));
9567 }
9568
9569 /*  Given a pointer to a tree node for a subrange type, return a pointer
9570     to a DIE that describes the given type.  */
9571
9572 static dw_die_ref
9573 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
9574 {
9575   dw_die_ref subrange_die;
9576   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
9577
9578   if (context_die == NULL)
9579     context_die = comp_unit_die ();
9580
9581   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
9582
9583   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
9584     {
9585       /* The size of the subrange type and its base type do not match,
9586          so we need to generate a size attribute for the subrange type.  */
9587       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
9588     }
9589
9590   if (low)
9591     add_bound_info (subrange_die, DW_AT_lower_bound, low);
9592   if (high)
9593     add_bound_info (subrange_die, DW_AT_upper_bound, high);
9594
9595   return subrange_die;
9596 }
9597
9598 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
9599    entry that chains various modifiers in front of the given type.  */
9600
9601 static dw_die_ref
9602 modified_type_die (tree type, int is_const_type, int is_volatile_type,
9603                    dw_die_ref context_die)
9604 {
9605   enum tree_code code = TREE_CODE (type);
9606   dw_die_ref mod_type_die;
9607   dw_die_ref sub_die = NULL;
9608   tree item_type = NULL;
9609   tree qualified_type;
9610   tree name, low, high;
9611
9612   if (code == ERROR_MARK)
9613     return NULL;
9614
9615   /* See if we already have the appropriately qualified variant of
9616      this type.  */
9617   qualified_type
9618     = get_qualified_type (type,
9619                           ((is_const_type ? TYPE_QUAL_CONST : 0)
9620                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
9621
9622   if (qualified_type == sizetype
9623       && TYPE_NAME (qualified_type)
9624       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
9625     {
9626       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
9627
9628       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
9629                            && TYPE_PRECISION (t)
9630                            == TYPE_PRECISION (qualified_type)
9631                            && TYPE_UNSIGNED (t)
9632                            == TYPE_UNSIGNED (qualified_type));
9633       qualified_type = t;
9634     }
9635
9636   /* If we do, then we can just use its DIE, if it exists.  */
9637   if (qualified_type)
9638     {
9639       mod_type_die = lookup_type_die (qualified_type);
9640       if (mod_type_die)
9641         return mod_type_die;
9642     }
9643
9644   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
9645
9646   /* Handle C typedef types.  */
9647   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
9648       && !DECL_ARTIFICIAL (name))
9649     {
9650       tree dtype = TREE_TYPE (name);
9651
9652       if (qualified_type == dtype)
9653         {
9654           /* For a named type, use the typedef.  */
9655           gen_type_die (qualified_type, context_die);
9656           return lookup_type_die (qualified_type);
9657         }
9658       else if (is_const_type < TYPE_READONLY (dtype)
9659                || is_volatile_type < TYPE_VOLATILE (dtype)
9660                || (is_const_type <= TYPE_READONLY (dtype)
9661                    && is_volatile_type <= TYPE_VOLATILE (dtype)
9662                    && DECL_ORIGINAL_TYPE (name) != type))
9663         /* cv-unqualified version of named type.  Just use the unnamed
9664            type to which it refers.  */
9665         return modified_type_die (DECL_ORIGINAL_TYPE (name),
9666                                   is_const_type, is_volatile_type,
9667                                   context_die);
9668       /* Else cv-qualified version of named type; fall through.  */
9669     }
9670
9671   if (is_const_type
9672       /* If both is_const_type and is_volatile_type, prefer the path
9673          which leads to a qualified type.  */
9674       && (!is_volatile_type
9675           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
9676           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
9677     {
9678       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
9679       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
9680     }
9681   else if (is_volatile_type)
9682     {
9683       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
9684       sub_die = modified_type_die (type, is_const_type, 0, context_die);
9685     }
9686   else if (code == POINTER_TYPE)
9687     {
9688       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
9689       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9690                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9691       item_type = TREE_TYPE (type);
9692       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9693         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9694                          TYPE_ADDR_SPACE (item_type));
9695     }
9696   else if (code == REFERENCE_TYPE)
9697     {
9698       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
9699         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
9700                                 type);
9701       else
9702         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
9703       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
9704                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
9705       item_type = TREE_TYPE (type);
9706       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
9707         add_AT_unsigned (mod_type_die, DW_AT_address_class,
9708                          TYPE_ADDR_SPACE (item_type));
9709     }
9710   else if (code == INTEGER_TYPE
9711            && TREE_TYPE (type) != NULL_TREE
9712            && subrange_type_for_debug_p (type, &low, &high))
9713     {
9714       mod_type_die = subrange_type_die (type, low, high, context_die);
9715       item_type = TREE_TYPE (type);
9716     }
9717   else if (is_base_type (type))
9718     mod_type_die = base_type_die (type);
9719   else
9720     {
9721       gen_type_die (type, context_die);
9722
9723       /* We have to get the type_main_variant here (and pass that to the
9724          `lookup_type_die' routine) because the ..._TYPE node we have
9725          might simply be a *copy* of some original type node (where the
9726          copy was created to help us keep track of typedef names) and
9727          that copy might have a different TYPE_UID from the original
9728          ..._TYPE node.  */
9729       if (TREE_CODE (type) != VECTOR_TYPE)
9730         return lookup_type_die (type_main_variant (type));
9731       else
9732         /* Vectors have the debugging information in the type,
9733            not the main variant.  */
9734         return lookup_type_die (type);
9735     }
9736
9737   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
9738      don't output a DW_TAG_typedef, since there isn't one in the
9739      user's program; just attach a DW_AT_name to the type.
9740      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
9741      if the base type already has the same name.  */
9742   if (name
9743       && ((TREE_CODE (name) != TYPE_DECL
9744            && (qualified_type == TYPE_MAIN_VARIANT (type)
9745                || (!is_const_type && !is_volatile_type)))
9746           || (TREE_CODE (name) == TYPE_DECL
9747               && TREE_TYPE (name) == qualified_type
9748               && DECL_NAME (name))))
9749     {
9750       if (TREE_CODE (name) == TYPE_DECL)
9751         /* Could just call add_name_and_src_coords_attributes here,
9752            but since this is a builtin type it doesn't have any
9753            useful source coordinates anyway.  */
9754         name = DECL_NAME (name);
9755       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
9756       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
9757       if (TYPE_ARTIFICIAL (type))
9758         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
9759     }
9760   /* This probably indicates a bug.  */
9761   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
9762     add_name_attribute (mod_type_die, "__unknown__");
9763
9764   if (qualified_type)
9765     equate_type_number_to_die (qualified_type, mod_type_die);
9766
9767   if (item_type)
9768     /* We must do this after the equate_type_number_to_die call, in case
9769        this is a recursive type.  This ensures that the modified_type_die
9770        recursion will terminate even if the type is recursive.  Recursive
9771        types are possible in Ada.  */
9772     sub_die = modified_type_die (item_type,
9773                                  TYPE_READONLY (item_type),
9774                                  TYPE_VOLATILE (item_type),
9775                                  context_die);
9776
9777   if (sub_die != NULL)
9778     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
9779
9780   return mod_type_die;
9781 }
9782
9783 /* Generate DIEs for the generic parameters of T.
9784    T must be either a generic type or a generic function.
9785    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
9786
9787 static void
9788 gen_generic_params_dies (tree t)
9789 {
9790   tree parms, args;
9791   int parms_num, i;
9792   dw_die_ref die = NULL;
9793
9794   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
9795     return;
9796
9797   if (TYPE_P (t))
9798     die = lookup_type_die (t);
9799   else if (DECL_P (t))
9800     die = lookup_decl_die (t);
9801
9802   gcc_assert (die);
9803
9804   parms = lang_hooks.get_innermost_generic_parms (t);
9805   if (!parms)
9806     /* T has no generic parameter. It means T is neither a generic type
9807        or function. End of story.  */
9808     return;
9809
9810   parms_num = TREE_VEC_LENGTH (parms);
9811   args = lang_hooks.get_innermost_generic_args (t);
9812   for (i = 0; i < parms_num; i++)
9813     {
9814       tree parm, arg, arg_pack_elems;
9815
9816       parm = TREE_VEC_ELT (parms, i);
9817       arg = TREE_VEC_ELT (args, i);
9818       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
9819       gcc_assert (parm && TREE_VALUE (parm) && arg);
9820
9821       if (parm && TREE_VALUE (parm) && arg)
9822         {
9823           /* If PARM represents a template parameter pack,
9824              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
9825              by DW_TAG_template_*_parameter DIEs for the argument
9826              pack elements of ARG. Note that ARG would then be
9827              an argument pack.  */
9828           if (arg_pack_elems)
9829             template_parameter_pack_die (TREE_VALUE (parm),
9830                                          arg_pack_elems,
9831                                          die);
9832           else
9833             generic_parameter_die (TREE_VALUE (parm), arg,
9834                                    true /* Emit DW_AT_name */, die);
9835         }
9836     }
9837 }
9838
9839 /* Create and return a DIE for PARM which should be
9840    the representation of a generic type parameter.
9841    For instance, in the C++ front end, PARM would be a template parameter.
9842    ARG is the argument to PARM.
9843    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
9844    name of the PARM.
9845    PARENT_DIE is the parent DIE which the new created DIE should be added to,
9846    as a child node.  */
9847
9848 static dw_die_ref
9849 generic_parameter_die (tree parm, tree arg,
9850                        bool emit_name_p,
9851                        dw_die_ref parent_die)
9852 {
9853   dw_die_ref tmpl_die = NULL;
9854   const char *name = NULL;
9855
9856   if (!parm || !DECL_NAME (parm) || !arg)
9857     return NULL;
9858
9859   /* We support non-type generic parameters and arguments,
9860      type generic parameters and arguments, as well as
9861      generic generic parameters (a.k.a. template template parameters in C++)
9862      and arguments.  */
9863   if (TREE_CODE (parm) == PARM_DECL)
9864     /* PARM is a nontype generic parameter  */
9865     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
9866   else if (TREE_CODE (parm) == TYPE_DECL)
9867     /* PARM is a type generic parameter.  */
9868     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
9869   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9870     /* PARM is a generic generic parameter.
9871        Its DIE is a GNU extension. It shall have a
9872        DW_AT_name attribute to represent the name of the template template
9873        parameter, and a DW_AT_GNU_template_name attribute to represent the
9874        name of the template template argument.  */
9875     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
9876                         parent_die, parm);
9877   else
9878     gcc_unreachable ();
9879
9880   if (tmpl_die)
9881     {
9882       tree tmpl_type;
9883
9884       /* If PARM is a generic parameter pack, it means we are
9885          emitting debug info for a template argument pack element.
9886          In other terms, ARG is a template argument pack element.
9887          In that case, we don't emit any DW_AT_name attribute for
9888          the die.  */
9889       if (emit_name_p)
9890         {
9891           name = IDENTIFIER_POINTER (DECL_NAME (parm));
9892           gcc_assert (name);
9893           add_AT_string (tmpl_die, DW_AT_name, name);
9894         }
9895
9896       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
9897         {
9898           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
9899              TMPL_DIE should have a child DW_AT_type attribute that is set
9900              to the type of the argument to PARM, which is ARG.
9901              If PARM is a type generic parameter, TMPL_DIE should have a
9902              child DW_AT_type that is set to ARG.  */
9903           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
9904           add_type_attribute (tmpl_die, tmpl_type, 0,
9905                               TREE_THIS_VOLATILE (tmpl_type),
9906                               parent_die);
9907         }
9908       else
9909         {
9910           /* So TMPL_DIE is a DIE representing a
9911              a generic generic template parameter, a.k.a template template
9912              parameter in C++ and arg is a template.  */
9913
9914           /* The DW_AT_GNU_template_name attribute of the DIE must be set
9915              to the name of the argument.  */
9916           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
9917           if (name)
9918             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
9919         }
9920
9921       if (TREE_CODE (parm) == PARM_DECL)
9922         /* So PARM is a non-type generic parameter.
9923            DWARF3 5.6.8 says we must set a DW_AT_const_value child
9924            attribute of TMPL_DIE which value represents the value
9925            of ARG.
9926            We must be careful here:
9927            The value of ARG might reference some function decls.
9928            We might currently be emitting debug info for a generic
9929            type and types are emitted before function decls, we don't
9930            know if the function decls referenced by ARG will actually be
9931            emitted after cgraph computations.
9932            So must defer the generation of the DW_AT_const_value to
9933            after cgraph is ready.  */
9934         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
9935     }
9936
9937   return tmpl_die;
9938 }
9939
9940 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
9941    PARM_PACK must be a template parameter pack. The returned DIE
9942    will be child DIE of PARENT_DIE.  */
9943
9944 static dw_die_ref
9945 template_parameter_pack_die (tree parm_pack,
9946                              tree parm_pack_args,
9947                              dw_die_ref parent_die)
9948 {
9949   dw_die_ref die;
9950   int j;
9951
9952   gcc_assert (parent_die && parm_pack);
9953
9954   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
9955   add_name_and_src_coords_attributes (die, parm_pack);
9956   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
9957     generic_parameter_die (parm_pack,
9958                            TREE_VEC_ELT (parm_pack_args, j),
9959                            false /* Don't emit DW_AT_name */,
9960                            die);
9961   return die;
9962 }
9963
9964 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
9965    an enumerated type.  */
9966
9967 static inline int
9968 type_is_enum (const_tree type)
9969 {
9970   return TREE_CODE (type) == ENUMERAL_TYPE;
9971 }
9972
9973 /* Return the DBX register number described by a given RTL node.  */
9974
9975 static unsigned int
9976 dbx_reg_number (const_rtx rtl)
9977 {
9978   unsigned regno = REGNO (rtl);
9979
9980   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
9981
9982 #ifdef LEAF_REG_REMAP
9983   if (current_function_uses_only_leaf_regs)
9984     {
9985       int leaf_reg = LEAF_REG_REMAP (regno);
9986       if (leaf_reg != -1)
9987         regno = (unsigned) leaf_reg;
9988     }
9989 #endif
9990
9991   return DBX_REGISTER_NUMBER (regno);
9992 }
9993
9994 /* Optionally add a DW_OP_piece term to a location description expression.
9995    DW_OP_piece is only added if the location description expression already
9996    doesn't end with DW_OP_piece.  */
9997
9998 static void
9999 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10000 {
10001   dw_loc_descr_ref loc;
10002
10003   if (*list_head != NULL)
10004     {
10005       /* Find the end of the chain.  */
10006       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10007         ;
10008
10009       if (loc->dw_loc_opc != DW_OP_piece)
10010         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10011     }
10012 }
10013
10014 /* Return a location descriptor that designates a machine register or
10015    zero if there is none.  */
10016
10017 static dw_loc_descr_ref
10018 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10019 {
10020   rtx regs;
10021
10022   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10023     return 0;
10024
10025   /* We only use "frame base" when we're sure we're talking about the
10026      post-prologue local stack frame.  We do this by *not* running
10027      register elimination until this point, and recognizing the special
10028      argument pointer and soft frame pointer rtx's.
10029      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10030   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10031       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10032     {
10033       dw_loc_descr_ref result = NULL;
10034
10035       if (dwarf_version >= 4 || !dwarf_strict)
10036         {
10037           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10038                                        initialized);
10039           if (result)
10040             add_loc_descr (&result,
10041                            new_loc_descr (DW_OP_stack_value, 0, 0));
10042         }
10043       return result;
10044     }
10045
10046   regs = targetm.dwarf_register_span (rtl);
10047
10048   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10049     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10050   else
10051     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10052 }
10053
10054 /* Return a location descriptor that designates a machine register for
10055    a given hard register number.  */
10056
10057 static dw_loc_descr_ref
10058 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10059 {
10060   dw_loc_descr_ref reg_loc_descr;
10061
10062   if (regno <= 31)
10063     reg_loc_descr
10064       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10065   else
10066     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10067
10068   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10069     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10070
10071   return reg_loc_descr;
10072 }
10073
10074 /* Given an RTL of a register, return a location descriptor that
10075    designates a value that spans more than one register.  */
10076
10077 static dw_loc_descr_ref
10078 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10079                              enum var_init_status initialized)
10080 {
10081   int nregs, size, i;
10082   unsigned reg;
10083   dw_loc_descr_ref loc_result = NULL;
10084
10085   reg = REGNO (rtl);
10086 #ifdef LEAF_REG_REMAP
10087   if (current_function_uses_only_leaf_regs)
10088     {
10089       int leaf_reg = LEAF_REG_REMAP (reg);
10090       if (leaf_reg != -1)
10091         reg = (unsigned) leaf_reg;
10092     }
10093 #endif
10094   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10095   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10096
10097   /* Simple, contiguous registers.  */
10098   if (regs == NULL_RTX)
10099     {
10100       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10101
10102       loc_result = NULL;
10103       while (nregs--)
10104         {
10105           dw_loc_descr_ref t;
10106
10107           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10108                                       VAR_INIT_STATUS_INITIALIZED);
10109           add_loc_descr (&loc_result, t);
10110           add_loc_descr_op_piece (&loc_result, size);
10111           ++reg;
10112         }
10113       return loc_result;
10114     }
10115
10116   /* Now onto stupid register sets in non contiguous locations.  */
10117
10118   gcc_assert (GET_CODE (regs) == PARALLEL);
10119
10120   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10121   loc_result = NULL;
10122
10123   for (i = 0; i < XVECLEN (regs, 0); ++i)
10124     {
10125       dw_loc_descr_ref t;
10126
10127       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10128                                   VAR_INIT_STATUS_INITIALIZED);
10129       add_loc_descr (&loc_result, t);
10130       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10131       add_loc_descr_op_piece (&loc_result, size);
10132     }
10133
10134   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10135     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10136   return loc_result;
10137 }
10138
10139 static unsigned long size_of_int_loc_descriptor (HOST_WIDE_INT);
10140
10141 /* Return a location descriptor that designates a constant i,
10142    as a compound operation from constant (i >> shift), constant shift
10143    and DW_OP_shl.  */
10144
10145 static dw_loc_descr_ref
10146 int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10147 {
10148   dw_loc_descr_ref ret = int_loc_descriptor (i >> shift);
10149   add_loc_descr (&ret, int_loc_descriptor (shift));
10150   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
10151   return ret;
10152 }
10153
10154 /* Return a location descriptor that designates a constant.  */
10155
10156 static dw_loc_descr_ref
10157 int_loc_descriptor (HOST_WIDE_INT i)
10158 {
10159   enum dwarf_location_atom op;
10160
10161   /* Pick the smallest representation of a constant, rather than just
10162      defaulting to the LEB encoding.  */
10163   if (i >= 0)
10164     {
10165       int clz = clz_hwi (i);
10166       int ctz = ctz_hwi (i);
10167       if (i <= 31)
10168         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10169       else if (i <= 0xff)
10170         op = DW_OP_const1u;
10171       else if (i <= 0xffff)
10172         op = DW_OP_const2u;
10173       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10174                && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10175         /* DW_OP_litX DW_OP_litY DW_OP_shl takes just 3 bytes and
10176            DW_OP_litX DW_OP_const1u Y DW_OP_shl takes just 4 bytes,
10177            while DW_OP_const4u is 5 bytes.  */
10178         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 5);
10179       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10180                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10181         /* DW_OP_const1u X DW_OP_litY DW_OP_shl takes just 4 bytes,
10182            while DW_OP_const4u is 5 bytes.  */
10183         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10184       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10185         op = DW_OP_const4u;
10186       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10187                && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10188         /* DW_OP_const1u X DW_OP_const1u Y DW_OP_shl takes just 5 bytes,
10189            while DW_OP_constu of constant >= 0x100000000 takes at least
10190            6 bytes.  */
10191         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 8);
10192       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10193                && clz + 16 + (size_of_uleb128 (i) > 5 ? 255 : 31)
10194                   >= HOST_BITS_PER_WIDE_INT)
10195         /* DW_OP_const2u X DW_OP_litY DW_OP_shl takes just 5 bytes,
10196            DW_OP_const2u X DW_OP_const1u Y DW_OP_shl takes 6 bytes,
10197            while DW_OP_constu takes in this case at least 6 bytes.  */
10198         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 16);
10199       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10200                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10201                && size_of_uleb128 (i) > 6)
10202         /* DW_OP_const4u X DW_OP_litY DW_OP_shl takes just 7 bytes.  */
10203         return int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT - clz - 32);
10204       else
10205         op = DW_OP_constu;
10206     }
10207   else
10208     {
10209       if (i >= -0x80)
10210         op = DW_OP_const1s;
10211       else if (i >= -0x8000)
10212         op = DW_OP_const2s;
10213       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10214         {
10215           if (size_of_int_loc_descriptor (i) < 5)
10216             {
10217               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10218               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10219               return ret;
10220             }
10221           op = DW_OP_const4s;
10222         }
10223       else
10224         {
10225           if (size_of_int_loc_descriptor (i)
10226               < (unsigned long) 1 + size_of_sleb128 (i))
10227             {
10228               dw_loc_descr_ref ret = int_loc_descriptor (-i);
10229               add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
10230               return ret;
10231             }
10232           op = DW_OP_consts;
10233         }
10234     }
10235
10236   return new_loc_descr (op, i, 0);
10237 }
10238
10239 /* Return size_of_locs (int_shift_loc_descriptor (i, shift))
10240    without actually allocating it.  */
10241
10242 static unsigned long
10243 size_of_int_shift_loc_descriptor (HOST_WIDE_INT i, int shift)
10244 {
10245   return size_of_int_loc_descriptor (i >> shift)
10246          + size_of_int_loc_descriptor (shift)
10247          + 1;
10248 }
10249
10250 /* Return size_of_locs (int_loc_descriptor (i)) without
10251    actually allocating it.  */
10252
10253 static unsigned long
10254 size_of_int_loc_descriptor (HOST_WIDE_INT i)
10255 {
10256   unsigned long s;
10257
10258   if (i >= 0)
10259     {
10260       int clz, ctz;
10261       if (i <= 31)
10262         return 1;
10263       else if (i <= 0xff)
10264         return 2;
10265       else if (i <= 0xffff)
10266         return 3;
10267       clz = clz_hwi (i);
10268       ctz = ctz_hwi (i);
10269       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 5
10270           && clz + 5 + 255 >= HOST_BITS_PER_WIDE_INT)
10271         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10272                                                     - clz - 5);
10273       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10274                && clz + 8 + 31 >= HOST_BITS_PER_WIDE_INT)
10275         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10276                                                     - clz - 8);
10277       else if (HOST_BITS_PER_WIDE_INT == 32 || i <= 0xffffffff)
10278         return 5;
10279       s = size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10280       if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 8
10281           && clz + 8 + 255 >= HOST_BITS_PER_WIDE_INT)
10282         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10283                                                     - clz - 8);
10284       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 16
10285                && clz + 16 + (s > 5 ? 255 : 31) >= HOST_BITS_PER_WIDE_INT)
10286         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10287                                                     - clz - 16);
10288       else if (clz + ctz >= HOST_BITS_PER_WIDE_INT - 32
10289                && clz + 32 + 31 >= HOST_BITS_PER_WIDE_INT
10290                && s > 6)
10291         return size_of_int_shift_loc_descriptor (i, HOST_BITS_PER_WIDE_INT
10292                                                     - clz - 32);
10293       else
10294         return 1 + s;
10295     }
10296   else
10297     {
10298       if (i >= -0x80)
10299         return 2;
10300       else if (i >= -0x8000)
10301         return 3;
10302       else if (HOST_BITS_PER_WIDE_INT == 32 || i >= -0x80000000)
10303         {
10304           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10305             {
10306               s = size_of_int_loc_descriptor (-i) + 1;
10307               if (s < 5)
10308                 return s;
10309             }
10310           return 5;
10311         }
10312       else
10313         {
10314           unsigned long r = 1 + size_of_sleb128 (i);
10315           if (-(unsigned HOST_WIDE_INT) i != (unsigned HOST_WIDE_INT) i)
10316             {
10317               s = size_of_int_loc_descriptor (-i) + 1;
10318               if (s < r)
10319                 return s;
10320             }
10321           return r;
10322         }
10323     }
10324 }
10325
10326 /* Return loc description representing "address" of integer value.
10327    This can appear only as toplevel expression.  */
10328
10329 static dw_loc_descr_ref
10330 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10331 {
10332   int litsize;
10333   dw_loc_descr_ref loc_result = NULL;
10334
10335   if (!(dwarf_version >= 4 || !dwarf_strict))
10336     return NULL;
10337
10338   litsize = size_of_int_loc_descriptor (i);
10339   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10340      is more compact.  For DW_OP_stack_value we need:
10341      litsize + 1 (DW_OP_stack_value)
10342      and for DW_OP_implicit_value:
10343      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10344   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10345     {
10346       loc_result = int_loc_descriptor (i);
10347       add_loc_descr (&loc_result,
10348                      new_loc_descr (DW_OP_stack_value, 0, 0));
10349       return loc_result;
10350     }
10351
10352   loc_result = new_loc_descr (DW_OP_implicit_value,
10353                               size, 0);
10354   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10355   loc_result->dw_loc_oprnd2.v.val_int = i;
10356   return loc_result;
10357 }
10358
10359 /* Return a location descriptor that designates a base+offset location.  */
10360
10361 static dw_loc_descr_ref
10362 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10363                  enum var_init_status initialized)
10364 {
10365   unsigned int regno;
10366   dw_loc_descr_ref result;
10367   dw_fde_ref fde = cfun->fde;
10368
10369   /* We only use "frame base" when we're sure we're talking about the
10370      post-prologue local stack frame.  We do this by *not* running
10371      register elimination until this point, and recognizing the special
10372      argument pointer and soft frame pointer rtx's.  */
10373   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10374     {
10375       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10376
10377       if (elim != reg)
10378         {
10379           if (GET_CODE (elim) == PLUS)
10380             {
10381               offset += INTVAL (XEXP (elim, 1));
10382               elim = XEXP (elim, 0);
10383             }
10384           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10385                        && (elim == hard_frame_pointer_rtx
10386                            || elim == stack_pointer_rtx))
10387                       || elim == (frame_pointer_needed
10388                                   ? hard_frame_pointer_rtx
10389                                   : stack_pointer_rtx));
10390
10391           /* If drap register is used to align stack, use frame
10392              pointer + offset to access stack variables.  If stack
10393              is aligned without drap, use stack pointer + offset to
10394              access stack variables.  */
10395           if (crtl->stack_realign_tried
10396               && reg == frame_pointer_rtx)
10397             {
10398               int base_reg
10399                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10400                                       ? HARD_FRAME_POINTER_REGNUM
10401                                       : REGNO (elim));
10402               return new_reg_loc_descr (base_reg, offset);
10403             }
10404
10405           gcc_assert (frame_pointer_fb_offset_valid);
10406           offset += frame_pointer_fb_offset;
10407           return new_loc_descr (DW_OP_fbreg, offset, 0);
10408         }
10409     }
10410
10411   regno = DWARF_FRAME_REGNUM (REGNO (reg));
10412
10413   if (!optimize && fde
10414       && (fde->drap_reg == regno || fde->vdrap_reg == regno))
10415     {
10416       /* Use cfa+offset to represent the location of arguments passed
10417          on the stack when drap is used to align stack.
10418          Only do this when not optimizing, for optimized code var-tracking
10419          is supposed to track where the arguments live and the register
10420          used as vdrap or drap in some spot might be used for something
10421          else in other part of the routine.  */
10422       return new_loc_descr (DW_OP_fbreg, offset, 0);
10423     }
10424
10425   if (regno <= 31)
10426     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10427                             offset, 0);
10428   else
10429     result = new_loc_descr (DW_OP_bregx, regno, offset);
10430
10431   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10432     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10433
10434   return result;
10435 }
10436
10437 /* Return true if this RTL expression describes a base+offset calculation.  */
10438
10439 static inline int
10440 is_based_loc (const_rtx rtl)
10441 {
10442   return (GET_CODE (rtl) == PLUS
10443           && ((REG_P (XEXP (rtl, 0))
10444                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10445                && CONST_INT_P (XEXP (rtl, 1)))));
10446 }
10447
10448 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10449    failed.  */
10450
10451 static dw_loc_descr_ref
10452 tls_mem_loc_descriptor (rtx mem)
10453 {
10454   tree base;
10455   dw_loc_descr_ref loc_result;
10456
10457   if (MEM_EXPR (mem) == NULL_TREE || !MEM_OFFSET_KNOWN_P (mem))
10458     return NULL;
10459
10460   base = get_base_address (MEM_EXPR (mem));
10461   if (base == NULL
10462       || TREE_CODE (base) != VAR_DECL
10463       || !DECL_THREAD_LOCAL_P (base))
10464     return NULL;
10465
10466   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
10467   if (loc_result == NULL)
10468     return NULL;
10469
10470   if (MEM_OFFSET (mem))
10471     loc_descr_plus_const (&loc_result, MEM_OFFSET (mem));
10472
10473   return loc_result;
10474 }
10475
10476 /* Output debug info about reason why we failed to expand expression as dwarf
10477    expression.  */
10478
10479 static void
10480 expansion_failed (tree expr, rtx rtl, char const *reason)
10481 {
10482   if (dump_file && (dump_flags & TDF_DETAILS))
10483     {
10484       fprintf (dump_file, "Failed to expand as dwarf: ");
10485       if (expr)
10486         print_generic_expr (dump_file, expr, dump_flags);
10487       if (rtl)
10488         {
10489           fprintf (dump_file, "\n");
10490           print_rtl (dump_file, rtl);
10491         }
10492       fprintf (dump_file, "\nReason: %s\n", reason);
10493     }
10494 }
10495
10496 /* Helper function for const_ok_for_output, called either directly
10497    or via for_each_rtx.  */
10498
10499 static int
10500 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
10501 {
10502   rtx rtl = *rtlp;
10503
10504   if (GET_CODE (rtl) == UNSPEC)
10505     {
10506       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
10507          we can't express it in the debug info.  */
10508 #ifdef ENABLE_CHECKING
10509       /* Don't complain about TLS UNSPECs, those are just too hard to
10510          delegitimize.  */
10511       if (XVECLEN (rtl, 0) != 1
10512           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
10513           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
10514           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
10515           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
10516         inform (current_function_decl
10517                 ? DECL_SOURCE_LOCATION (current_function_decl)
10518                 : UNKNOWN_LOCATION,
10519 #if NUM_UNSPEC_VALUES > 0
10520                 "non-delegitimized UNSPEC %s (%d) found in variable location",
10521                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
10522                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
10523                 XINT (rtl, 1));
10524 #else
10525                 "non-delegitimized UNSPEC %d found in variable location",
10526                 XINT (rtl, 1));
10527 #endif
10528 #endif
10529       expansion_failed (NULL_TREE, rtl,
10530                         "UNSPEC hasn't been delegitimized.\n");
10531       return 1;
10532     }
10533
10534   if (GET_CODE (rtl) != SYMBOL_REF)
10535     return 0;
10536
10537   if (CONSTANT_POOL_ADDRESS_P (rtl))
10538     {
10539       bool marked;
10540       get_pool_constant_mark (rtl, &marked);
10541       /* If all references to this pool constant were optimized away,
10542          it was not output and thus we can't represent it.  */
10543       if (!marked)
10544         {
10545           expansion_failed (NULL_TREE, rtl,
10546                             "Constant was removed from constant pool.\n");
10547           return 1;
10548         }
10549     }
10550
10551   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
10552     return 1;
10553
10554   /* Avoid references to external symbols in debug info, on several targets
10555      the linker might even refuse to link when linking a shared library,
10556      and in many other cases the relocations for .debug_info/.debug_loc are
10557      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
10558      to be defined within the same shared library or executable are fine.  */
10559   if (SYMBOL_REF_EXTERNAL_P (rtl))
10560     {
10561       tree decl = SYMBOL_REF_DECL (rtl);
10562
10563       if (decl == NULL || !targetm.binds_local_p (decl))
10564         {
10565           expansion_failed (NULL_TREE, rtl,
10566                             "Symbol not defined in current TU.\n");
10567           return 1;
10568         }
10569     }
10570
10571   return 0;
10572 }
10573
10574 /* Return true if constant RTL can be emitted in DW_OP_addr or
10575    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
10576    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
10577
10578 static bool
10579 const_ok_for_output (rtx rtl)
10580 {
10581   if (GET_CODE (rtl) == SYMBOL_REF)
10582     return const_ok_for_output_1 (&rtl, NULL) == 0;
10583
10584   if (GET_CODE (rtl) == CONST)
10585     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
10586
10587   return true;
10588 }
10589
10590 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
10591    if possible, NULL otherwise.  */
10592
10593 static dw_die_ref
10594 base_type_for_mode (enum machine_mode mode, bool unsignedp)
10595 {
10596   dw_die_ref type_die;
10597   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10598
10599   if (type == NULL)
10600     return NULL;
10601   switch (TREE_CODE (type))
10602     {
10603     case INTEGER_TYPE:
10604     case REAL_TYPE:
10605       break;
10606     default:
10607       return NULL;
10608     }
10609   type_die = lookup_type_die (type);
10610   if (!type_die)
10611     type_die = modified_type_die (type, false, false, comp_unit_die ());
10612   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
10613     return NULL;
10614   return type_die;
10615 }
10616
10617 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
10618    type matching MODE, or, if MODE is narrower than or as wide as
10619    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
10620    possible.  */
10621
10622 static dw_loc_descr_ref
10623 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
10624 {
10625   enum machine_mode outer_mode = mode;
10626   dw_die_ref type_die;
10627   dw_loc_descr_ref cvt;
10628
10629   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
10630     {
10631       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
10632       return op;
10633     }
10634   type_die = base_type_for_mode (outer_mode, 1);
10635   if (type_die == NULL)
10636     return NULL;
10637   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10638   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10639   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10640   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10641   add_loc_descr (&op, cvt);
10642   return op;
10643 }
10644
10645 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
10646
10647 static dw_loc_descr_ref
10648 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
10649                         dw_loc_descr_ref op1)
10650 {
10651   dw_loc_descr_ref ret = op0;
10652   add_loc_descr (&ret, op1);
10653   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10654   if (STORE_FLAG_VALUE != 1)
10655     {
10656       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
10657       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
10658     }
10659   return ret;
10660 }
10661
10662 /* Return location descriptor for signed comparison OP RTL.  */
10663
10664 static dw_loc_descr_ref
10665 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10666                          enum machine_mode mem_mode)
10667 {
10668   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10669   dw_loc_descr_ref op0, op1;
10670   int shift;
10671
10672   if (op_mode == VOIDmode)
10673     op_mode = GET_MODE (XEXP (rtl, 1));
10674   if (op_mode == VOIDmode)
10675     return NULL;
10676
10677   if (dwarf_strict
10678       && (GET_MODE_CLASS (op_mode) != MODE_INT
10679           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
10680     return NULL;
10681
10682   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10683                             VAR_INIT_STATUS_INITIALIZED);
10684   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10685                             VAR_INIT_STATUS_INITIALIZED);
10686
10687   if (op0 == NULL || op1 == NULL)
10688     return NULL;
10689
10690   if (GET_MODE_CLASS (op_mode) != MODE_INT
10691       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10692     return compare_loc_descriptor (op, op0, op1);
10693
10694   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10695     {
10696       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
10697       dw_loc_descr_ref cvt;
10698
10699       if (type_die == NULL)
10700         return NULL;
10701       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10702       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10703       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10704       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10705       add_loc_descr (&op0, cvt);
10706       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10707       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10708       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10709       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10710       add_loc_descr (&op1, cvt);
10711       return compare_loc_descriptor (op, op0, op1);
10712     }
10713
10714   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
10715   /* For eq/ne, if the operands are known to be zero-extended,
10716      there is no need to do the fancy shifting up.  */
10717   if (op == DW_OP_eq || op == DW_OP_ne)
10718     {
10719       dw_loc_descr_ref last0, last1;
10720       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10721         ;
10722       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10723         ;
10724       /* deref_size zero extends, and for constants we can check
10725          whether they are zero extended or not.  */
10726       if (((last0->dw_loc_opc == DW_OP_deref_size
10727             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10728            || (CONST_INT_P (XEXP (rtl, 0))
10729                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
10730                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
10731           && ((last1->dw_loc_opc == DW_OP_deref_size
10732                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
10733               || (CONST_INT_P (XEXP (rtl, 1))
10734                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
10735                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
10736         return compare_loc_descriptor (op, op0, op1);
10737
10738       /* EQ/NE comparison against constant in narrower type than
10739          DWARF2_ADDR_SIZE can be performed either as
10740          DW_OP_const1u <shift> DW_OP_shl DW_OP_const* <cst << shift>
10741          DW_OP_{eq,ne}
10742          or
10743          DW_OP_const*u <mode_mask> DW_OP_and DW_OP_const* <cst & mode_mask>
10744          DW_OP_{eq,ne}.  Pick whatever is shorter.  */
10745       if (CONST_INT_P (XEXP (rtl, 1))
10746           && GET_MODE_BITSIZE (op_mode) < HOST_BITS_PER_WIDE_INT
10747           && (size_of_int_loc_descriptor (shift) + 1
10748               + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift)
10749               >= size_of_int_loc_descriptor (GET_MODE_MASK (op_mode)) + 1
10750                  + size_of_int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10751                                                & GET_MODE_MASK (op_mode))))
10752         {
10753           add_loc_descr (&op0, int_loc_descriptor (GET_MODE_MASK (op_mode)));
10754           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10755           op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1))
10756                                     & GET_MODE_MASK (op_mode));
10757           return compare_loc_descriptor (op, op0, op1);
10758         }
10759     }
10760   add_loc_descr (&op0, int_loc_descriptor (shift));
10761   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10762   if (CONST_INT_P (XEXP (rtl, 1)))
10763     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
10764   else
10765     {
10766       add_loc_descr (&op1, int_loc_descriptor (shift));
10767       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10768     }
10769   return compare_loc_descriptor (op, op0, op1);
10770 }
10771
10772 /* Return location descriptor for unsigned comparison OP RTL.  */
10773
10774 static dw_loc_descr_ref
10775 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
10776                          enum machine_mode mem_mode)
10777 {
10778   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
10779   dw_loc_descr_ref op0, op1;
10780
10781   if (op_mode == VOIDmode)
10782     op_mode = GET_MODE (XEXP (rtl, 1));
10783   if (op_mode == VOIDmode)
10784     return NULL;
10785   if (GET_MODE_CLASS (op_mode) != MODE_INT)
10786     return NULL;
10787
10788   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
10789     return NULL;
10790
10791   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
10792                             VAR_INIT_STATUS_INITIALIZED);
10793   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
10794                             VAR_INIT_STATUS_INITIALIZED);
10795
10796   if (op0 == NULL || op1 == NULL)
10797     return NULL;
10798
10799   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
10800     {
10801       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
10802       dw_loc_descr_ref last0, last1;
10803       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
10804         ;
10805       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
10806         ;
10807       if (CONST_INT_P (XEXP (rtl, 0)))
10808         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
10809       /* deref_size zero extends, so no need to mask it again.  */
10810       else if (last0->dw_loc_opc != DW_OP_deref_size
10811                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10812         {
10813           add_loc_descr (&op0, int_loc_descriptor (mask));
10814           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10815         }
10816       if (CONST_INT_P (XEXP (rtl, 1)))
10817         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
10818       /* deref_size zero extends, so no need to mask it again.  */
10819       else if (last1->dw_loc_opc != DW_OP_deref_size
10820                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
10821         {
10822           add_loc_descr (&op1, int_loc_descriptor (mask));
10823           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10824         }
10825     }
10826   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
10827     {
10828       HOST_WIDE_INT bias = 1;
10829       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10830       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10831       if (CONST_INT_P (XEXP (rtl, 1)))
10832         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
10833                                   + INTVAL (XEXP (rtl, 1)));
10834       else
10835         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
10836                                             bias, 0));
10837     }
10838   return compare_loc_descriptor (op, op0, op1);
10839 }
10840
10841 /* Return location descriptor for {U,S}{MIN,MAX}.  */
10842
10843 static dw_loc_descr_ref
10844 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
10845                        enum machine_mode mem_mode)
10846 {
10847   enum dwarf_location_atom op;
10848   dw_loc_descr_ref op0, op1, ret;
10849   dw_loc_descr_ref bra_node, drop_node;
10850
10851   if (dwarf_strict
10852       && (GET_MODE_CLASS (mode) != MODE_INT
10853           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
10854     return NULL;
10855
10856   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10857                             VAR_INIT_STATUS_INITIALIZED);
10858   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10859                             VAR_INIT_STATUS_INITIALIZED);
10860
10861   if (op0 == NULL || op1 == NULL)
10862     return NULL;
10863
10864   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
10865   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
10866   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
10867   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
10868     {
10869       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10870         {
10871           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10872           add_loc_descr (&op0, int_loc_descriptor (mask));
10873           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
10874           add_loc_descr (&op1, int_loc_descriptor (mask));
10875           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
10876         }
10877       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
10878         {
10879           HOST_WIDE_INT bias = 1;
10880           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
10881           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10882           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
10883         }
10884     }
10885   else if (GET_MODE_CLASS (mode) == MODE_INT
10886            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
10887     {
10888       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
10889       add_loc_descr (&op0, int_loc_descriptor (shift));
10890       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
10891       add_loc_descr (&op1, int_loc_descriptor (shift));
10892       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
10893     }
10894   else if (GET_MODE_CLASS (mode) == MODE_INT
10895            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10896     {
10897       dw_die_ref type_die = base_type_for_mode (mode, 0);
10898       dw_loc_descr_ref cvt;
10899       if (type_die == NULL)
10900         return NULL;
10901       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10902       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10903       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10904       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10905       add_loc_descr (&op0, cvt);
10906       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10907       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10908       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10909       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10910       add_loc_descr (&op1, cvt);
10911     }
10912
10913   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
10914     op = DW_OP_lt;
10915   else
10916     op = DW_OP_gt;
10917   ret = op0;
10918   add_loc_descr (&ret, op1);
10919   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
10920   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
10921   add_loc_descr (&ret, bra_node);
10922   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
10923   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
10924   add_loc_descr (&ret, drop_node);
10925   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
10926   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
10927   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
10928       && GET_MODE_CLASS (mode) == MODE_INT
10929       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
10930     ret = convert_descriptor_to_mode (mode, ret);
10931   return ret;
10932 }
10933
10934 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
10935    but after converting arguments to type_die, afterwards
10936    convert back to unsigned.  */
10937
10938 static dw_loc_descr_ref
10939 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
10940              enum machine_mode mode, enum machine_mode mem_mode)
10941 {
10942   dw_loc_descr_ref cvt, op0, op1;
10943
10944   if (type_die == NULL)
10945     return NULL;
10946   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
10947                             VAR_INIT_STATUS_INITIALIZED);
10948   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
10949                             VAR_INIT_STATUS_INITIALIZED);
10950   if (op0 == NULL || op1 == NULL)
10951     return NULL;
10952   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10953   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10954   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10955   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10956   add_loc_descr (&op0, cvt);
10957   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
10958   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
10959   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
10960   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
10961   add_loc_descr (&op1, cvt);
10962   add_loc_descr (&op0, op1);
10963   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
10964   return convert_descriptor_to_mode (mode, op0);
10965 }
10966
10967 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
10968    const0 is DW_OP_lit0 or corresponding typed constant,
10969    const1 is DW_OP_lit1 or corresponding typed constant
10970    and constMSB is constant with just the MSB bit set
10971    for the mode):
10972        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
10973    L1: const0 DW_OP_swap
10974    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
10975        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
10976    L3: DW_OP_drop
10977    L4: DW_OP_nop
10978
10979    CTZ is similar:
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 const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
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    FFS is similar:
10988        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
10989    L1: const1 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 static dw_loc_descr_ref
10996 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
10997                     enum machine_mode mem_mode)
10998 {
10999   dw_loc_descr_ref op0, ret, tmp;
11000   HOST_WIDE_INT valv;
11001   dw_loc_descr_ref l1jump, l1label;
11002   dw_loc_descr_ref l2jump, l2label;
11003   dw_loc_descr_ref l3jump, l3label;
11004   dw_loc_descr_ref l4jump, l4label;
11005   rtx msb;
11006
11007   if (GET_MODE_CLASS (mode) != MODE_INT
11008       || GET_MODE (XEXP (rtl, 0)) != mode
11009       || (GET_CODE (rtl) == CLZ
11010           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11011     return NULL;
11012
11013   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11014                             VAR_INIT_STATUS_INITIALIZED);
11015   if (op0 == NULL)
11016     return NULL;
11017   ret = op0;
11018   if (GET_CODE (rtl) == CLZ)
11019     {
11020       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11021         valv = GET_MODE_BITSIZE (mode);
11022     }
11023   else if (GET_CODE (rtl) == FFS)
11024     valv = 0;
11025   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11026     valv = GET_MODE_BITSIZE (mode);
11027   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11028   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11029   add_loc_descr (&ret, l1jump);
11030   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11031   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11032                             VAR_INIT_STATUS_INITIALIZED);
11033   if (tmp == NULL)
11034     return NULL;
11035   add_loc_descr (&ret, tmp);
11036   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11037   add_loc_descr (&ret, l4jump);
11038   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11039                                 ? const1_rtx : const0_rtx,
11040                                 mode, mem_mode,
11041                                 VAR_INIT_STATUS_INITIALIZED);
11042   if (l1label == NULL)
11043     return NULL;
11044   add_loc_descr (&ret, l1label);
11045   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11046   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11047   add_loc_descr (&ret, l2label);
11048   if (GET_CODE (rtl) != CLZ)
11049     msb = const1_rtx;
11050   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11051     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11052                    << (GET_MODE_BITSIZE (mode) - 1));
11053   else
11054     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11055                                   << (GET_MODE_BITSIZE (mode)
11056                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11057   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11058     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11059                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11060                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11061   else
11062     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11063                               VAR_INIT_STATUS_INITIALIZED);
11064   if (tmp == NULL)
11065     return NULL;
11066   add_loc_descr (&ret, tmp);
11067   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11068   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11069   add_loc_descr (&ret, l3jump);
11070   tmp = mem_loc_descriptor (const1_rtx, 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 (GET_CODE (rtl) == CLZ
11076                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11077   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11078   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11079   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11080   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11081   add_loc_descr (&ret, l2jump);
11082   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11083   add_loc_descr (&ret, l3label);
11084   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11085   add_loc_descr (&ret, l4label);
11086   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11087   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11088   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11089   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11090   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11091   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11092   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11093   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11094   return ret;
11095 }
11096
11097 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11098    const1 is DW_OP_lit1 or corresponding typed constant):
11099        const0 DW_OP_swap
11100    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11101        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11102    L2: DW_OP_drop
11103
11104    PARITY is similar:
11105    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11106        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11107    L2: DW_OP_drop  */
11108
11109 static dw_loc_descr_ref
11110 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11111                          enum machine_mode mem_mode)
11112 {
11113   dw_loc_descr_ref op0, ret, tmp;
11114   dw_loc_descr_ref l1jump, l1label;
11115   dw_loc_descr_ref l2jump, l2label;
11116
11117   if (GET_MODE_CLASS (mode) != MODE_INT
11118       || GET_MODE (XEXP (rtl, 0)) != mode)
11119     return NULL;
11120
11121   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11122                             VAR_INIT_STATUS_INITIALIZED);
11123   if (op0 == NULL)
11124     return NULL;
11125   ret = op0;
11126   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11127                             VAR_INIT_STATUS_INITIALIZED);
11128   if (tmp == NULL)
11129     return NULL;
11130   add_loc_descr (&ret, tmp);
11131   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11132   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11133   add_loc_descr (&ret, l1label);
11134   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11135   add_loc_descr (&ret, l2jump);
11136   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11137   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11138   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11139                             VAR_INIT_STATUS_INITIALIZED);
11140   if (tmp == NULL)
11141     return NULL;
11142   add_loc_descr (&ret, tmp);
11143   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11144   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11145                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11146   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11147   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11148                             VAR_INIT_STATUS_INITIALIZED);
11149   add_loc_descr (&ret, tmp);
11150   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11151   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11152   add_loc_descr (&ret, l1jump);
11153   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11154   add_loc_descr (&ret, l2label);
11155   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11156   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11157   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11158   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11159   return ret;
11160 }
11161
11162 /* BSWAP (constS is initial shift count, either 56 or 24):
11163        constS const0
11164    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11165        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11166        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11167        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11168    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11169
11170 static dw_loc_descr_ref
11171 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11172                       enum machine_mode mem_mode)
11173 {
11174   dw_loc_descr_ref op0, ret, tmp;
11175   dw_loc_descr_ref l1jump, l1label;
11176   dw_loc_descr_ref l2jump, l2label;
11177
11178   if (GET_MODE_CLASS (mode) != MODE_INT
11179       || BITS_PER_UNIT != 8
11180       || (GET_MODE_BITSIZE (mode) != 32
11181           &&  GET_MODE_BITSIZE (mode) != 64))
11182     return NULL;
11183
11184   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11185                             VAR_INIT_STATUS_INITIALIZED);
11186   if (op0 == NULL)
11187     return NULL;
11188
11189   ret = op0;
11190   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11191                             mode, mem_mode,
11192                             VAR_INIT_STATUS_INITIALIZED);
11193   if (tmp == NULL)
11194     return NULL;
11195   add_loc_descr (&ret, tmp);
11196   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11197                             VAR_INIT_STATUS_INITIALIZED);
11198   if (tmp == NULL)
11199     return NULL;
11200   add_loc_descr (&ret, tmp);
11201   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11202   add_loc_descr (&ret, l1label);
11203   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11204                             mode, mem_mode,
11205                             VAR_INIT_STATUS_INITIALIZED);
11206   add_loc_descr (&ret, tmp);
11207   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11208   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11209   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11210   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11211                             VAR_INIT_STATUS_INITIALIZED);
11212   if (tmp == NULL)
11213     return NULL;
11214   add_loc_descr (&ret, tmp);
11215   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11216   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11217   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11218   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11219   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11220   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11221   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11222                             VAR_INIT_STATUS_INITIALIZED);
11223   add_loc_descr (&ret, tmp);
11224   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11225   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11226   add_loc_descr (&ret, l2jump);
11227   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11228                             VAR_INIT_STATUS_INITIALIZED);
11229   add_loc_descr (&ret, tmp);
11230   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11231   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11232   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11233   add_loc_descr (&ret, l1jump);
11234   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11235   add_loc_descr (&ret, l2label);
11236   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11237   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11238   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11239   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11240   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11241   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11242   return ret;
11243 }
11244
11245 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11246    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11247    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11248    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11249
11250    ROTATERT is similar:
11251    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11252    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11253    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11254
11255 static dw_loc_descr_ref
11256 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11257                        enum machine_mode mem_mode)
11258 {
11259   rtx rtlop1 = XEXP (rtl, 1);
11260   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11261   int i;
11262
11263   if (GET_MODE_CLASS (mode) != MODE_INT)
11264     return NULL;
11265
11266   if (GET_MODE (rtlop1) != VOIDmode
11267       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11268     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11269   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11270                             VAR_INIT_STATUS_INITIALIZED);
11271   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11272                             VAR_INIT_STATUS_INITIALIZED);
11273   if (op0 == NULL || op1 == NULL)
11274     return NULL;
11275   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11276     for (i = 0; i < 2; i++)
11277       {
11278         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11279           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11280                                         mode, mem_mode,
11281                                         VAR_INIT_STATUS_INITIALIZED);
11282         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11283           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11284                                    ? DW_OP_const4u
11285                                    : HOST_BITS_PER_WIDE_INT == 64
11286                                    ? DW_OP_const8u : DW_OP_constu,
11287                                    GET_MODE_MASK (mode), 0);
11288         else
11289           mask[i] = NULL;
11290         if (mask[i] == NULL)
11291           return NULL;
11292         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11293       }
11294   ret = op0;
11295   add_loc_descr (&ret, op1);
11296   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11297   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11298   if (GET_CODE (rtl) == ROTATERT)
11299     {
11300       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11301       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11302                                           GET_MODE_BITSIZE (mode), 0));
11303     }
11304   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11305   if (mask[0] != NULL)
11306     add_loc_descr (&ret, mask[0]);
11307   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11308   if (mask[1] != NULL)
11309     {
11310       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11311       add_loc_descr (&ret, mask[1]);
11312       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11313     }
11314   if (GET_CODE (rtl) == ROTATE)
11315     {
11316       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11317       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11318                                           GET_MODE_BITSIZE (mode), 0));
11319     }
11320   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11321   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11322   return ret;
11323 }
11324
11325 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11326    for DEBUG_PARAMETER_REF RTL.  */
11327
11328 static dw_loc_descr_ref
11329 parameter_ref_descriptor (rtx rtl)
11330 {
11331   dw_loc_descr_ref ret;
11332   dw_die_ref ref;
11333
11334   if (dwarf_strict)
11335     return NULL;
11336   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11337   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11338   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11339   if (ref)
11340     {
11341       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11342       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11343       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11344     }
11345   else
11346     {
11347       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11348       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11349     }
11350   return ret;
11351 }
11352
11353 /* Helper function to get mode of MEM's address.  */
11354
11355 enum machine_mode
11356 get_address_mode (rtx mem)
11357 {
11358   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11359   if (mode != VOIDmode)
11360     return mode;
11361   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11362 }
11363
11364 /* The following routine converts the RTL for a variable or parameter
11365    (resident in memory) into an equivalent Dwarf representation of a
11366    mechanism for getting the address of that same variable onto the top of a
11367    hypothetical "address evaluation" stack.
11368
11369    When creating memory location descriptors, we are effectively transforming
11370    the RTL for a memory-resident object into its Dwarf postfix expression
11371    equivalent.  This routine recursively descends an RTL tree, turning
11372    it into Dwarf postfix code as it goes.
11373
11374    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11375
11376    MEM_MODE is the mode of the memory reference, needed to handle some
11377    autoincrement addressing modes.
11378
11379    Return 0 if we can't represent the location.  */
11380
11381 dw_loc_descr_ref
11382 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11383                     enum machine_mode mem_mode,
11384                     enum var_init_status initialized)
11385 {
11386   dw_loc_descr_ref mem_loc_result = NULL;
11387   enum dwarf_location_atom op;
11388   dw_loc_descr_ref op0, op1;
11389
11390   if (mode == VOIDmode)
11391     mode = GET_MODE (rtl);
11392
11393   /* Note that for a dynamically sized array, the location we will generate a
11394      description of here will be the lowest numbered location which is
11395      actually within the array.  That's *not* necessarily the same as the
11396      zeroth element of the array.  */
11397
11398   rtl = targetm.delegitimize_address (rtl);
11399
11400   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11401     return NULL;
11402
11403   switch (GET_CODE (rtl))
11404     {
11405     case POST_INC:
11406     case POST_DEC:
11407     case POST_MODIFY:
11408       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11409
11410     case SUBREG:
11411       /* The case of a subreg may arise when we have a local (register)
11412          variable or a formal (register) parameter which doesn't quite fill
11413          up an entire register.  For now, just assume that it is
11414          legitimate to make the Dwarf info refer to the whole register which
11415          contains the given subreg.  */
11416       if (!subreg_lowpart_p (rtl))
11417         break;
11418       if (GET_MODE_CLASS (mode) == MODE_INT
11419           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11420           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11421 #ifdef POINTERS_EXTEND_UNSIGNED
11422               || (mode == Pmode && mem_mode != VOIDmode)
11423 #endif
11424              )
11425           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11426         {
11427           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11428                                                GET_MODE (SUBREG_REG (rtl)),
11429                                                mem_mode, initialized);
11430           break;
11431         }
11432       if (dwarf_strict)
11433         break;
11434       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11435         break;
11436       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11437           && (GET_MODE_CLASS (mode) != MODE_INT
11438               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11439         break;
11440       else
11441         {
11442           dw_die_ref type_die;
11443           dw_loc_descr_ref cvt;
11444
11445           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11446                                                GET_MODE (SUBREG_REG (rtl)),
11447                                                mem_mode, initialized);
11448           if (mem_loc_result == NULL)
11449             break;
11450           type_die = base_type_for_mode (mode,
11451                                          GET_MODE_CLASS (mode) == MODE_INT);
11452           if (type_die == NULL)
11453             {
11454               mem_loc_result = NULL;
11455               break;
11456             }
11457           if (GET_MODE_SIZE (mode)
11458               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11459             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11460           else
11461             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11462           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11463           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11464           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11465           add_loc_descr (&mem_loc_result, cvt);
11466         }
11467       break;
11468
11469     case REG:
11470       if (GET_MODE_CLASS (mode) != MODE_INT
11471           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11472 #ifdef POINTERS_EXTEND_UNSIGNED
11473               && (mode != Pmode || mem_mode == VOIDmode)
11474 #endif
11475               ))
11476         {
11477           dw_die_ref type_die;
11478
11479           if (dwarf_strict)
11480             break;
11481           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
11482             break;
11483           type_die = base_type_for_mode (mode,
11484                                          GET_MODE_CLASS (mode) == MODE_INT);
11485           if (type_die == NULL)
11486             break;
11487           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
11488                                           dbx_reg_number (rtl), 0);
11489           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11490           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11491           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
11492           break;
11493         }
11494       /* Whenever a register number forms a part of the description of the
11495          method for calculating the (dynamic) address of a memory resident
11496          object, DWARF rules require the register number be referred to as
11497          a "base register".  This distinction is not based in any way upon
11498          what category of register the hardware believes the given register
11499          belongs to.  This is strictly DWARF terminology we're dealing with
11500          here. Note that in cases where the location of a memory-resident
11501          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11502          OP_CONST (0)) the actual DWARF location descriptor that we generate
11503          may just be OP_BASEREG (basereg).  This may look deceptively like
11504          the object in question was allocated to a register (rather than in
11505          memory) so DWARF consumers need to be aware of the subtle
11506          distinction between OP_REG and OP_BASEREG.  */
11507       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11508         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11509       else if (stack_realign_drap
11510                && crtl->drap_reg
11511                && crtl->args.internal_arg_pointer == rtl
11512                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11513         {
11514           /* If RTL is internal_arg_pointer, which has been optimized
11515              out, use DRAP instead.  */
11516           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11517                                             VAR_INIT_STATUS_INITIALIZED);
11518         }
11519       break;
11520
11521     case SIGN_EXTEND:
11522     case ZERO_EXTEND:
11523       if (GET_MODE_CLASS (mode) != MODE_INT)
11524         break;
11525       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
11526                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
11527       if (op0 == 0)
11528         break;
11529       else if (GET_CODE (rtl) == ZERO_EXTEND
11530                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11531                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11532                   < HOST_BITS_PER_WIDE_INT
11533                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
11534                   to expand zero extend as two shifts instead of
11535                   masking.  */
11536                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
11537         {
11538           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
11539           mem_loc_result = op0;
11540           add_loc_descr (&mem_loc_result,
11541                          int_loc_descriptor (GET_MODE_MASK (imode)));
11542           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
11543         }
11544       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11545         {
11546           int shift = DWARF2_ADDR_SIZE
11547                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11548           shift *= BITS_PER_UNIT;
11549           if (GET_CODE (rtl) == SIGN_EXTEND)
11550             op = DW_OP_shra;
11551           else
11552             op = DW_OP_shr;
11553           mem_loc_result = op0;
11554           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11555           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11556           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11557           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11558         }
11559       else if (!dwarf_strict)
11560         {
11561           dw_die_ref type_die1, type_die2;
11562           dw_loc_descr_ref cvt;
11563
11564           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
11565                                           GET_CODE (rtl) == ZERO_EXTEND);
11566           if (type_die1 == NULL)
11567             break;
11568           type_die2 = base_type_for_mode (mode, 1);
11569           if (type_die2 == NULL)
11570             break;
11571           mem_loc_result = op0;
11572           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11573           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11574           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
11575           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11576           add_loc_descr (&mem_loc_result, cvt);
11577           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11578           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11579           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
11580           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11581           add_loc_descr (&mem_loc_result, cvt);
11582         }
11583       break;
11584
11585     case MEM:
11586       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
11587                                            get_address_mode (rtl), mode,
11588                                            VAR_INIT_STATUS_INITIALIZED);
11589       if (mem_loc_result == NULL)
11590         mem_loc_result = tls_mem_loc_descriptor (rtl);
11591       if (mem_loc_result != 0)
11592         {
11593           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11594               || GET_MODE_CLASS (mode) != MODE_INT)
11595             {
11596               dw_die_ref type_die;
11597               dw_loc_descr_ref deref;
11598
11599               if (dwarf_strict)
11600                 return NULL;
11601               type_die
11602                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
11603               if (type_die == NULL)
11604                 return NULL;
11605               deref = new_loc_descr (DW_OP_GNU_deref_type,
11606                                      GET_MODE_SIZE (mode), 0);
11607               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
11608               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
11609               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
11610               add_loc_descr (&mem_loc_result, deref);
11611             }
11612           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11613             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11614           else
11615             add_loc_descr (&mem_loc_result,
11616                            new_loc_descr (DW_OP_deref_size,
11617                                           GET_MODE_SIZE (mode), 0));
11618         }
11619       else
11620         {
11621           rtx new_rtl = avoid_constant_pool_reference (rtl);
11622           if (new_rtl != rtl)
11623             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
11624         }
11625       break;
11626
11627     case LO_SUM:
11628       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
11629
11630     case LABEL_REF:
11631       /* Some ports can transform a symbol ref into a label ref, because
11632          the symbol ref is too far away and has to be dumped into a constant
11633          pool.  */
11634     case CONST:
11635     case SYMBOL_REF:
11636       if (GET_MODE_CLASS (mode) != MODE_INT
11637           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
11638 #ifdef POINTERS_EXTEND_UNSIGNED
11639               && (mode != Pmode || mem_mode == VOIDmode)
11640 #endif
11641               ))
11642         break;
11643       if (GET_CODE (rtl) == SYMBOL_REF
11644           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11645         {
11646           dw_loc_descr_ref temp;
11647
11648           /* If this is not defined, we have no way to emit the data.  */
11649           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11650             break;
11651
11652           /* We used to emit DW_OP_addr here, but that's wrong, since
11653              DW_OP_addr should be relocated by the debug info consumer,
11654              while DW_OP_GNU_push_tls_address operand should not.  */
11655           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
11656                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
11657           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11658           temp->dw_loc_oprnd1.v.val_addr = rtl;
11659           temp->dtprel = true;
11660
11661           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11662           add_loc_descr (&mem_loc_result, temp);
11663
11664           break;
11665         }
11666
11667       if (!const_ok_for_output (rtl))
11668         break;
11669
11670     symref:
11671       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11672       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11673       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11674       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11675       break;
11676
11677     case CONCAT:
11678     case CONCATN:
11679     case VAR_LOCATION:
11680     case DEBUG_IMPLICIT_PTR:
11681       expansion_failed (NULL_TREE, rtl,
11682                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11683       return 0;
11684
11685     case ENTRY_VALUE:
11686       if (dwarf_strict)
11687         return NULL;
11688       if (REG_P (ENTRY_VALUE_EXP (rtl)))
11689         {
11690           if (GET_MODE_CLASS (mode) != MODE_INT
11691               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11692             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11693                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11694           else
11695             op0
11696               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
11697                                         VAR_INIT_STATUS_INITIALIZED);
11698         }
11699       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
11700                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
11701         {
11702           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
11703                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11704           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
11705             return NULL;
11706         }
11707       else
11708         gcc_unreachable ();
11709       if (op0 == NULL)
11710         return NULL;
11711       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
11712       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
11713       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
11714       break;
11715
11716     case DEBUG_PARAMETER_REF:
11717       mem_loc_result = parameter_ref_descriptor (rtl);
11718       break;
11719
11720     case PRE_MODIFY:
11721       /* Extract the PLUS expression nested inside and fall into
11722          PLUS code below.  */
11723       rtl = XEXP (rtl, 1);
11724       goto plus;
11725
11726     case PRE_INC:
11727     case PRE_DEC:
11728       /* Turn these into a PLUS expression and fall into the PLUS code
11729          below.  */
11730       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
11731                           GEN_INT (GET_CODE (rtl) == PRE_INC
11732                                    ? GET_MODE_UNIT_SIZE (mem_mode)
11733                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
11734
11735       /* ... fall through ...  */
11736
11737     case PLUS:
11738     plus:
11739       if (is_based_loc (rtl)
11740           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11741           && GET_MODE_CLASS (mode) == MODE_INT)
11742         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11743                                           INTVAL (XEXP (rtl, 1)),
11744                                           VAR_INIT_STATUS_INITIALIZED);
11745       else
11746         {
11747           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11748                                                VAR_INIT_STATUS_INITIALIZED);
11749           if (mem_loc_result == 0)
11750             break;
11751
11752           if (CONST_INT_P (XEXP (rtl, 1))
11753               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11754             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11755           else
11756             {
11757               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11758                                         VAR_INIT_STATUS_INITIALIZED);
11759               if (op1 == 0)
11760                 break;
11761               add_loc_descr (&mem_loc_result, op1);
11762               add_loc_descr (&mem_loc_result,
11763                              new_loc_descr (DW_OP_plus, 0, 0));
11764             }
11765         }
11766       break;
11767
11768     /* If a pseudo-reg is optimized away, it is possible for it to
11769        be replaced with a MEM containing a multiply or shift.  */
11770     case MINUS:
11771       op = DW_OP_minus;
11772       goto do_binop;
11773
11774     case MULT:
11775       op = DW_OP_mul;
11776       goto do_binop;
11777
11778     case DIV:
11779       if (!dwarf_strict
11780           && GET_MODE_CLASS (mode) == MODE_INT
11781           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11782         {
11783           mem_loc_result = typed_binop (DW_OP_div, rtl,
11784                                         base_type_for_mode (mode, 0),
11785                                         mode, mem_mode);
11786           break;
11787         }
11788       op = DW_OP_div;
11789       goto do_binop;
11790
11791     case UMOD:
11792       op = DW_OP_mod;
11793       goto do_binop;
11794
11795     case ASHIFT:
11796       op = DW_OP_shl;
11797       goto do_shift;
11798
11799     case ASHIFTRT:
11800       op = DW_OP_shra;
11801       goto do_shift;
11802
11803     case LSHIFTRT:
11804       op = DW_OP_shr;
11805       goto do_shift;
11806
11807     do_shift:
11808       if (GET_MODE_CLASS (mode) != MODE_INT)
11809         break;
11810       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11811                                 VAR_INIT_STATUS_INITIALIZED);
11812       {
11813         rtx rtlop1 = XEXP (rtl, 1);
11814         if (GET_MODE (rtlop1) != VOIDmode
11815             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
11816                < GET_MODE_BITSIZE (mode))
11817           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11818         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11819                                   VAR_INIT_STATUS_INITIALIZED);
11820       }
11821
11822       if (op0 == 0 || op1 == 0)
11823         break;
11824
11825       mem_loc_result = op0;
11826       add_loc_descr (&mem_loc_result, op1);
11827       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11828       break;
11829
11830     case AND:
11831       op = DW_OP_and;
11832       goto do_binop;
11833
11834     case IOR:
11835       op = DW_OP_or;
11836       goto do_binop;
11837
11838     case XOR:
11839       op = DW_OP_xor;
11840       goto do_binop;
11841
11842     do_binop:
11843       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11844                                 VAR_INIT_STATUS_INITIALIZED);
11845       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11846                                 VAR_INIT_STATUS_INITIALIZED);
11847
11848       if (op0 == 0 || op1 == 0)
11849         break;
11850
11851       mem_loc_result = op0;
11852       add_loc_descr (&mem_loc_result, op1);
11853       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11854       break;
11855
11856     case MOD:
11857       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
11858         {
11859           mem_loc_result = typed_binop (DW_OP_mod, rtl,
11860                                         base_type_for_mode (mode, 0),
11861                                         mode, mem_mode);
11862           break;
11863         }
11864
11865       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11866                                 VAR_INIT_STATUS_INITIALIZED);
11867       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11868                                 VAR_INIT_STATUS_INITIALIZED);
11869
11870       if (op0 == 0 || op1 == 0)
11871         break;
11872
11873       mem_loc_result = op0;
11874       add_loc_descr (&mem_loc_result, op1);
11875       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11876       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
11877       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
11878       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11879       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
11880       break;
11881
11882     case UDIV:
11883       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
11884         {
11885           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
11886             {
11887               op = DW_OP_div;
11888               goto do_binop;
11889             }
11890           mem_loc_result = typed_binop (DW_OP_div, rtl,
11891                                         base_type_for_mode (mode, 1),
11892                                         mode, mem_mode);
11893         }
11894       break;
11895
11896     case NOT:
11897       op = DW_OP_not;
11898       goto do_unop;
11899
11900     case ABS:
11901       op = DW_OP_abs;
11902       goto do_unop;
11903
11904     case NEG:
11905       op = DW_OP_neg;
11906       goto do_unop;
11907
11908     do_unop:
11909       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11910                                 VAR_INIT_STATUS_INITIALIZED);
11911
11912       if (op0 == 0)
11913         break;
11914
11915       mem_loc_result = op0;
11916       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11917       break;
11918
11919     case CONST_INT:
11920       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11921 #ifdef POINTERS_EXTEND_UNSIGNED
11922           || (mode == Pmode
11923               && mem_mode != VOIDmode
11924               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
11925 #endif
11926           )
11927         {
11928           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11929           break;
11930         }
11931       if (!dwarf_strict
11932           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
11933               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
11934         {
11935           dw_die_ref type_die = base_type_for_mode (mode, 1);
11936           enum machine_mode amode;
11937           if (type_die == NULL)
11938             return NULL;
11939           amode = mode_for_size (DWARF2_ADDR_SIZE * BITS_PER_UNIT,
11940                                  MODE_INT, 0);
11941           if (INTVAL (rtl) >= 0
11942               && amode != BLKmode
11943               && trunc_int_for_mode (INTVAL (rtl), amode) == INTVAL (rtl)
11944               /* const DW_OP_GNU_convert <XXX> vs.
11945                  DW_OP_GNU_const_type <XXX, 1, const>.  */
11946               && size_of_int_loc_descriptor (INTVAL (rtl)) + 1 + 1
11947                  < (unsigned long) 1 + 1 + 1 + GET_MODE_SIZE (mode))
11948             {
11949               mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11950               op0 = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11951               op0->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11952               op0->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11953               op0->dw_loc_oprnd1.v.val_die_ref.external = 0;
11954               add_loc_descr (&mem_loc_result, op0);
11955               return mem_loc_result;
11956             }
11957           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
11958                                           INTVAL (rtl));
11959           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11960           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11961           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11962           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11963             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11964           else
11965             {
11966               mem_loc_result->dw_loc_oprnd2.val_class
11967                 = dw_val_class_const_double;
11968               mem_loc_result->dw_loc_oprnd2.v.val_double
11969                 = shwi_to_double_int (INTVAL (rtl));
11970             }
11971         }
11972       break;
11973
11974     case CONST_DOUBLE:
11975       if (!dwarf_strict)
11976         {
11977           dw_die_ref type_die;
11978
11979           /* Note that a CONST_DOUBLE rtx could represent either an integer
11980              or a floating-point constant.  A CONST_DOUBLE is used whenever
11981              the constant requires more than one word in order to be
11982              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11983           if (mode == VOIDmode
11984               || (GET_MODE (rtl) == VOIDmode
11985                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
11986             break;
11987           type_die = base_type_for_mode (mode,
11988                                          GET_MODE_CLASS (mode) == MODE_INT);
11989           if (type_die == NULL)
11990             return NULL;
11991           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
11992           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11993           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11994           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
11995           if (SCALAR_FLOAT_MODE_P (mode))
11996             {
11997               unsigned int length = GET_MODE_SIZE (mode);
11998               unsigned char *array
11999                   = (unsigned char*) ggc_alloc_atomic (length);
12000
12001               insert_float (rtl, array);
12002               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12003               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12004               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12005               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12006             }
12007           else
12008             {
12009               mem_loc_result->dw_loc_oprnd2.val_class
12010                 = dw_val_class_const_double;
12011               mem_loc_result->dw_loc_oprnd2.v.val_double
12012                 = rtx_to_double_int (rtl);
12013             }
12014         }
12015       break;
12016
12017     case EQ:
12018       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12019       break;
12020
12021     case GE:
12022       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12023       break;
12024
12025     case GT:
12026       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12027       break;
12028
12029     case LE:
12030       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12031       break;
12032
12033     case LT:
12034       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12035       break;
12036
12037     case NE:
12038       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12039       break;
12040
12041     case GEU:
12042       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12043       break;
12044
12045     case GTU:
12046       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12047       break;
12048
12049     case LEU:
12050       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12051       break;
12052
12053     case LTU:
12054       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12055       break;
12056
12057     case UMIN:
12058     case UMAX:
12059       if (GET_MODE_CLASS (mode) != MODE_INT)
12060         break;
12061       /* FALLTHRU */
12062     case SMIN:
12063     case SMAX:
12064       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12065       break;
12066
12067     case ZERO_EXTRACT:
12068     case SIGN_EXTRACT:
12069       if (CONST_INT_P (XEXP (rtl, 1))
12070           && CONST_INT_P (XEXP (rtl, 2))
12071           && ((unsigned) INTVAL (XEXP (rtl, 1))
12072               + (unsigned) INTVAL (XEXP (rtl, 2))
12073               <= GET_MODE_BITSIZE (mode))
12074           && GET_MODE_CLASS (mode) == MODE_INT
12075           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12076           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12077         {
12078           int shift, size;
12079           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12080                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12081           if (op0 == 0)
12082             break;
12083           if (GET_CODE (rtl) == SIGN_EXTRACT)
12084             op = DW_OP_shra;
12085           else
12086             op = DW_OP_shr;
12087           mem_loc_result = op0;
12088           size = INTVAL (XEXP (rtl, 1));
12089           shift = INTVAL (XEXP (rtl, 2));
12090           if (BITS_BIG_ENDIAN)
12091             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12092                     - shift - size;
12093           if (shift + size != (int) DWARF2_ADDR_SIZE)
12094             {
12095               add_loc_descr (&mem_loc_result,
12096                              int_loc_descriptor (DWARF2_ADDR_SIZE
12097                                                  - shift - size));
12098               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12099             }
12100           if (size != (int) DWARF2_ADDR_SIZE)
12101             {
12102               add_loc_descr (&mem_loc_result,
12103                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12104               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12105             }
12106         }
12107       break;
12108
12109     case IF_THEN_ELSE:
12110       {
12111         dw_loc_descr_ref op2, bra_node, drop_node;
12112         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12113                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12114                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12115                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12116         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12117                                   VAR_INIT_STATUS_INITIALIZED);
12118         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12119                                   VAR_INIT_STATUS_INITIALIZED);
12120         if (op0 == NULL || op1 == NULL || op2 == NULL)
12121           break;
12122
12123         mem_loc_result = op1;
12124         add_loc_descr (&mem_loc_result, op2);
12125         add_loc_descr (&mem_loc_result, op0);
12126         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12127         add_loc_descr (&mem_loc_result, bra_node);
12128         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12129         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12130         add_loc_descr (&mem_loc_result, drop_node);
12131         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12132         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12133       }
12134       break;
12135
12136     case FLOAT_EXTEND:
12137     case FLOAT_TRUNCATE:
12138     case FLOAT:
12139     case UNSIGNED_FLOAT:
12140     case FIX:
12141     case UNSIGNED_FIX:
12142       if (!dwarf_strict)
12143         {
12144           dw_die_ref type_die;
12145           dw_loc_descr_ref cvt;
12146
12147           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12148                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12149           if (op0 == NULL)
12150             break;
12151           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12152               && (GET_CODE (rtl) == FLOAT
12153                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12154                      <= DWARF2_ADDR_SIZE))
12155             {
12156               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12157                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12158               if (type_die == NULL)
12159                 break;
12160               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12161               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12162               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12163               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12164               add_loc_descr (&op0, cvt);
12165             }
12166           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12167           if (type_die == NULL)
12168             break;
12169           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12170           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12171           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12172           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12173           add_loc_descr (&op0, cvt);
12174           if (GET_MODE_CLASS (mode) == MODE_INT
12175               && (GET_CODE (rtl) == FIX
12176                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12177             {
12178               op0 = convert_descriptor_to_mode (mode, op0);
12179               if (op0 == NULL)
12180                 break;
12181             }
12182           mem_loc_result = op0;
12183         }
12184       break;
12185
12186     case CLZ:
12187     case CTZ:
12188     case FFS:
12189       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12190       break;
12191
12192     case POPCOUNT:
12193     case PARITY:
12194       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12195       break;
12196
12197     case BSWAP:
12198       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12199       break;
12200
12201     case ROTATE:
12202     case ROTATERT:
12203       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12204       break;
12205
12206     case COMPARE:
12207     case TRUNCATE:
12208       /* In theory, we could implement the above.  */
12209       /* DWARF cannot represent the unsigned compare operations
12210          natively.  */
12211     case SS_MULT:
12212     case US_MULT:
12213     case SS_DIV:
12214     case US_DIV:
12215     case SS_PLUS:
12216     case US_PLUS:
12217     case SS_MINUS:
12218     case US_MINUS:
12219     case SS_NEG:
12220     case US_NEG:
12221     case SS_ABS:
12222     case SS_ASHIFT:
12223     case US_ASHIFT:
12224     case SS_TRUNCATE:
12225     case US_TRUNCATE:
12226     case UNORDERED:
12227     case ORDERED:
12228     case UNEQ:
12229     case UNGE:
12230     case UNGT:
12231     case UNLE:
12232     case UNLT:
12233     case LTGT:
12234     case FRACT_CONVERT:
12235     case UNSIGNED_FRACT_CONVERT:
12236     case SAT_FRACT:
12237     case UNSIGNED_SAT_FRACT:
12238     case SQRT:
12239     case ASM_OPERANDS:
12240     case VEC_MERGE:
12241     case VEC_SELECT:
12242     case VEC_CONCAT:
12243     case VEC_DUPLICATE:
12244     case UNSPEC:
12245     case HIGH:
12246     case FMA:
12247     case STRICT_LOW_PART:
12248     case CONST_VECTOR:
12249     case CONST_FIXED:
12250     case CLRSB:
12251       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12252          can't express it in the debug info.  This can happen e.g. with some
12253          TLS UNSPECs.  */
12254       break;
12255
12256     case CONST_STRING:
12257       resolve_one_addr (&rtl, NULL);
12258       goto symref;
12259
12260     default:
12261 #ifdef ENABLE_CHECKING
12262       print_rtl (stderr, rtl);
12263       gcc_unreachable ();
12264 #else
12265       break;
12266 #endif
12267     }
12268
12269   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12270     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12271
12272   return mem_loc_result;
12273 }
12274
12275 /* Return a descriptor that describes the concatenation of two locations.
12276    This is typically a complex variable.  */
12277
12278 static dw_loc_descr_ref
12279 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12280 {
12281   dw_loc_descr_ref cc_loc_result = NULL;
12282   dw_loc_descr_ref x0_ref
12283     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12284   dw_loc_descr_ref x1_ref
12285     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12286
12287   if (x0_ref == 0 || x1_ref == 0)
12288     return 0;
12289
12290   cc_loc_result = x0_ref;
12291   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12292
12293   add_loc_descr (&cc_loc_result, x1_ref);
12294   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12295
12296   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12297     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12298
12299   return cc_loc_result;
12300 }
12301
12302 /* Return a descriptor that describes the concatenation of N
12303    locations.  */
12304
12305 static dw_loc_descr_ref
12306 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12307 {
12308   unsigned int i;
12309   dw_loc_descr_ref cc_loc_result = NULL;
12310   unsigned int n = XVECLEN (concatn, 0);
12311
12312   for (i = 0; i < n; ++i)
12313     {
12314       dw_loc_descr_ref ref;
12315       rtx x = XVECEXP (concatn, 0, i);
12316
12317       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12318       if (ref == NULL)
12319         return NULL;
12320
12321       add_loc_descr (&cc_loc_result, ref);
12322       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12323     }
12324
12325   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12326     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12327
12328   return cc_loc_result;
12329 }
12330
12331 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12332    for DEBUG_IMPLICIT_PTR RTL.  */
12333
12334 static dw_loc_descr_ref
12335 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12336 {
12337   dw_loc_descr_ref ret;
12338   dw_die_ref ref;
12339
12340   if (dwarf_strict)
12341     return NULL;
12342   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12343               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12344               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12345   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12346   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12347   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12348   if (ref)
12349     {
12350       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12351       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12352       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12353     }
12354   else
12355     {
12356       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12357       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12358     }
12359   return ret;
12360 }
12361
12362 /* Output a proper Dwarf location descriptor for a variable or parameter
12363    which is either allocated in a register or in a memory location.  For a
12364    register, we just generate an OP_REG and the register number.  For a
12365    memory location we provide a Dwarf postfix expression describing how to
12366    generate the (dynamic) address of the object onto the address stack.
12367
12368    MODE is mode of the decl if this loc_descriptor is going to be used in
12369    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12370    allowed, VOIDmode otherwise.
12371
12372    If we don't know how to describe it, return 0.  */
12373
12374 static dw_loc_descr_ref
12375 loc_descriptor (rtx rtl, enum machine_mode mode,
12376                 enum var_init_status initialized)
12377 {
12378   dw_loc_descr_ref loc_result = NULL;
12379
12380   switch (GET_CODE (rtl))
12381     {
12382     case SUBREG:
12383       /* The case of a subreg may arise when we have a local (register)
12384          variable or a formal (register) parameter which doesn't quite fill
12385          up an entire register.  For now, just assume that it is
12386          legitimate to make the Dwarf info refer to the whole register which
12387          contains the given subreg.  */
12388       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12389         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12390       else
12391         goto do_default;
12392       break;
12393
12394     case REG:
12395       loc_result = reg_loc_descriptor (rtl, initialized);
12396       break;
12397
12398     case MEM:
12399       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12400                                        GET_MODE (rtl), initialized);
12401       if (loc_result == NULL)
12402         loc_result = tls_mem_loc_descriptor (rtl);
12403       if (loc_result == NULL)
12404         {
12405           rtx new_rtl = avoid_constant_pool_reference (rtl);
12406           if (new_rtl != rtl)
12407             loc_result = loc_descriptor (new_rtl, mode, initialized);
12408         }
12409       break;
12410
12411     case CONCAT:
12412       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12413                                           initialized);
12414       break;
12415
12416     case CONCATN:
12417       loc_result = concatn_loc_descriptor (rtl, initialized);
12418       break;
12419
12420     case VAR_LOCATION:
12421       /* Single part.  */
12422       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12423         {
12424           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12425           if (GET_CODE (loc) == EXPR_LIST)
12426             loc = XEXP (loc, 0);
12427           loc_result = loc_descriptor (loc, mode, initialized);
12428           break;
12429         }
12430
12431       rtl = XEXP (rtl, 1);
12432       /* FALLTHRU */
12433
12434     case PARALLEL:
12435       {
12436         rtvec par_elems = XVEC (rtl, 0);
12437         int num_elem = GET_NUM_ELEM (par_elems);
12438         enum machine_mode mode;
12439         int i;
12440
12441         /* Create the first one, so we have something to add to.  */
12442         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12443                                      VOIDmode, initialized);
12444         if (loc_result == NULL)
12445           return NULL;
12446         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12447         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12448         for (i = 1; i < num_elem; i++)
12449           {
12450             dw_loc_descr_ref temp;
12451
12452             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12453                                    VOIDmode, initialized);
12454             if (temp == NULL)
12455               return NULL;
12456             add_loc_descr (&loc_result, temp);
12457             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12458             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12459           }
12460       }
12461       break;
12462
12463     case CONST_INT:
12464       if (mode != VOIDmode && mode != BLKmode)
12465         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12466                                                     INTVAL (rtl));
12467       break;
12468
12469     case CONST_DOUBLE:
12470       if (mode == VOIDmode)
12471         mode = GET_MODE (rtl);
12472
12473       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12474         {
12475           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12476
12477           /* Note that a CONST_DOUBLE rtx could represent either an integer
12478              or a floating-point constant.  A CONST_DOUBLE is used whenever
12479              the constant requires more than one word in order to be
12480              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12481           loc_result = new_loc_descr (DW_OP_implicit_value,
12482                                       GET_MODE_SIZE (mode), 0);
12483           if (SCALAR_FLOAT_MODE_P (mode))
12484             {
12485               unsigned int length = GET_MODE_SIZE (mode);
12486               unsigned char *array
12487                   = (unsigned char*) ggc_alloc_atomic (length);
12488
12489               insert_float (rtl, array);
12490               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12491               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12492               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12493               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12494             }
12495           else
12496             {
12497               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
12498               loc_result->dw_loc_oprnd2.v.val_double
12499                 = rtx_to_double_int (rtl);
12500             }
12501         }
12502       break;
12503
12504     case CONST_VECTOR:
12505       if (mode == VOIDmode)
12506         mode = GET_MODE (rtl);
12507
12508       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12509         {
12510           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
12511           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12512           unsigned char *array = (unsigned char *)
12513             ggc_alloc_atomic (length * elt_size);
12514           unsigned int i;
12515           unsigned char *p;
12516
12517           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12518           switch (GET_MODE_CLASS (mode))
12519             {
12520             case MODE_VECTOR_INT:
12521               for (i = 0, p = array; i < length; i++, p += elt_size)
12522                 {
12523                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12524                   double_int val = rtx_to_double_int (elt);
12525
12526                   if (elt_size <= sizeof (HOST_WIDE_INT))
12527                     insert_int (double_int_to_shwi (val), elt_size, p);
12528                   else
12529                     {
12530                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12531                       insert_double (val, p);
12532                     }
12533                 }
12534               break;
12535
12536             case MODE_VECTOR_FLOAT:
12537               for (i = 0, p = array; i < length; i++, p += elt_size)
12538                 {
12539                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12540                   insert_float (elt, p);
12541                 }
12542               break;
12543
12544             default:
12545               gcc_unreachable ();
12546             }
12547
12548           loc_result = new_loc_descr (DW_OP_implicit_value,
12549                                       length * elt_size, 0);
12550           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12551           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12552           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12553           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12554         }
12555       break;
12556
12557     case CONST:
12558       if (mode == VOIDmode
12559           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12560           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12561           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12562         {
12563           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12564           break;
12565         }
12566       /* FALLTHROUGH */
12567     case SYMBOL_REF:
12568       if (!const_ok_for_output (rtl))
12569         break;
12570     case LABEL_REF:
12571       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12572           && (dwarf_version >= 4 || !dwarf_strict))
12573         {
12574           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12575           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12576           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12577           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
12578           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12579         }
12580       break;
12581
12582     case DEBUG_IMPLICIT_PTR:
12583       loc_result = implicit_ptr_descriptor (rtl, 0);
12584       break;
12585
12586     case PLUS:
12587       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
12588           && CONST_INT_P (XEXP (rtl, 1)))
12589         {
12590           loc_result
12591             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
12592           break;
12593         }
12594       /* FALLTHRU */
12595     do_default:
12596     default:
12597       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12598            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12599            && dwarf_version >= 4)
12600           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
12601         {
12602           /* Value expression.  */
12603           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
12604           if (loc_result)
12605             add_loc_descr (&loc_result,
12606                            new_loc_descr (DW_OP_stack_value, 0, 0));
12607         }
12608       break;
12609     }
12610
12611   return loc_result;
12612 }
12613
12614 /* We need to figure out what section we should use as the base for the
12615    address ranges where a given location is valid.
12616    1. If this particular DECL has a section associated with it, use that.
12617    2. If this function has a section associated with it, use that.
12618    3. Otherwise, use the text section.
12619    XXX: If you split a variable across multiple sections, we won't notice.  */
12620
12621 static const char *
12622 secname_for_decl (const_tree decl)
12623 {
12624   const char *secname;
12625
12626   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12627     {
12628       tree sectree = DECL_SECTION_NAME (decl);
12629       secname = TREE_STRING_POINTER (sectree);
12630     }
12631   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12632     {
12633       tree sectree = DECL_SECTION_NAME (current_function_decl);
12634       secname = TREE_STRING_POINTER (sectree);
12635     }
12636   else if (cfun && in_cold_section_p)
12637     secname = crtl->subsections.cold_section_label;
12638   else
12639     secname = text_section_label;
12640
12641   return secname;
12642 }
12643
12644 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12645
12646 static bool
12647 decl_by_reference_p (tree decl)
12648 {
12649   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12650            || TREE_CODE (decl) == VAR_DECL)
12651           && DECL_BY_REFERENCE (decl));
12652 }
12653
12654 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12655    for VARLOC.  */
12656
12657 static dw_loc_descr_ref
12658 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
12659                enum var_init_status initialized)
12660 {
12661   int have_address = 0;
12662   dw_loc_descr_ref descr;
12663   enum machine_mode mode;
12664
12665   if (want_address != 2)
12666     {
12667       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12668       /* Single part.  */
12669       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12670         {
12671           varloc = PAT_VAR_LOCATION_LOC (varloc);
12672           if (GET_CODE (varloc) == EXPR_LIST)
12673             varloc = XEXP (varloc, 0);
12674           mode = GET_MODE (varloc);
12675           if (MEM_P (varloc))
12676             {
12677               rtx addr = XEXP (varloc, 0);
12678               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
12679                                           mode, initialized);
12680               if (descr)
12681                 have_address = 1;
12682               else
12683                 {
12684                   rtx x = avoid_constant_pool_reference (varloc);
12685                   if (x != varloc)
12686                     descr = mem_loc_descriptor (x, mode, VOIDmode,
12687                                                 initialized);
12688                 }
12689             }
12690           else
12691             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
12692         }
12693       else
12694         return 0;
12695     }
12696   else
12697     {
12698       if (GET_CODE (varloc) == VAR_LOCATION)
12699         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
12700       else
12701         mode = DECL_MODE (loc);
12702       descr = loc_descriptor (varloc, mode, initialized);
12703       have_address = 1;
12704     }
12705
12706   if (!descr)
12707     return 0;
12708
12709   if (want_address == 2 && !have_address
12710       && (dwarf_version >= 4 || !dwarf_strict))
12711     {
12712       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
12713         {
12714           expansion_failed (loc, NULL_RTX,
12715                             "DWARF address size mismatch");
12716           return 0;
12717         }
12718       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
12719       have_address = 1;
12720     }
12721   /* Show if we can't fill the request for an address.  */
12722   if (want_address && !have_address)
12723     {
12724       expansion_failed (loc, NULL_RTX,
12725                         "Want address and only have value");
12726       return 0;
12727     }
12728
12729   /* If we've got an address and don't want one, dereference.  */
12730   if (!want_address && have_address)
12731     {
12732       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12733       enum dwarf_location_atom op;
12734
12735       if (size > DWARF2_ADDR_SIZE || size == -1)
12736         {
12737           expansion_failed (loc, NULL_RTX,
12738                             "DWARF address size mismatch");
12739           return 0;
12740         }
12741       else if (size == DWARF2_ADDR_SIZE)
12742         op = DW_OP_deref;
12743       else
12744         op = DW_OP_deref_size;
12745
12746       add_loc_descr (&descr, new_loc_descr (op, size, 0));
12747     }
12748
12749   return descr;
12750 }
12751
12752 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
12753    if it is not possible.  */
12754
12755 static dw_loc_descr_ref
12756 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
12757 {
12758   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
12759     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
12760   else if (dwarf_version >= 3 || !dwarf_strict)
12761     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
12762   else
12763     return NULL;
12764 }
12765
12766 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
12767    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
12768
12769 static dw_loc_descr_ref
12770 dw_sra_loc_expr (tree decl, rtx loc)
12771 {
12772   rtx p;
12773   unsigned int padsize = 0;
12774   dw_loc_descr_ref descr, *descr_tail;
12775   unsigned HOST_WIDE_INT decl_size;
12776   rtx varloc;
12777   enum var_init_status initialized;
12778
12779   if (DECL_SIZE (decl) == NULL
12780       || !host_integerp (DECL_SIZE (decl), 1))
12781     return NULL;
12782
12783   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
12784   descr = NULL;
12785   descr_tail = &descr;
12786
12787   for (p = loc; p; p = XEXP (p, 1))
12788     {
12789       unsigned int bitsize = decl_piece_bitsize (p);
12790       rtx loc_note = *decl_piece_varloc_ptr (p);
12791       dw_loc_descr_ref cur_descr;
12792       dw_loc_descr_ref *tail, last = NULL;
12793       unsigned int opsize = 0;
12794
12795       if (loc_note == NULL_RTX
12796           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
12797         {
12798           padsize += bitsize;
12799           continue;
12800         }
12801       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
12802       varloc = NOTE_VAR_LOCATION (loc_note);
12803       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
12804       if (cur_descr == NULL)
12805         {
12806           padsize += bitsize;
12807           continue;
12808         }
12809
12810       /* Check that cur_descr either doesn't use
12811          DW_OP_*piece operations, or their sum is equal
12812          to bitsize.  Otherwise we can't embed it.  */
12813       for (tail = &cur_descr; *tail != NULL;
12814            tail = &(*tail)->dw_loc_next)
12815         if ((*tail)->dw_loc_opc == DW_OP_piece)
12816           {
12817             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
12818                       * BITS_PER_UNIT;
12819             last = *tail;
12820           }
12821         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
12822           {
12823             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
12824             last = *tail;
12825           }
12826
12827       if (last != NULL && opsize != bitsize)
12828         {
12829           padsize += bitsize;
12830           continue;
12831         }
12832
12833       /* If there is a hole, add DW_OP_*piece after empty DWARF
12834          expression, which means that those bits are optimized out.  */
12835       if (padsize)
12836         {
12837           if (padsize > decl_size)
12838             return NULL;
12839           decl_size -= padsize;
12840           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
12841           if (*descr_tail == NULL)
12842             return NULL;
12843           descr_tail = &(*descr_tail)->dw_loc_next;
12844           padsize = 0;
12845         }
12846       *descr_tail = cur_descr;
12847       descr_tail = tail;
12848       if (bitsize > decl_size)
12849         return NULL;
12850       decl_size -= bitsize;
12851       if (last == NULL)
12852         {
12853           HOST_WIDE_INT offset = 0;
12854           if (GET_CODE (varloc) == VAR_LOCATION
12855               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
12856             {
12857               varloc = PAT_VAR_LOCATION_LOC (varloc);
12858               if (GET_CODE (varloc) == EXPR_LIST)
12859                 varloc = XEXP (varloc, 0);
12860             }
12861           do 
12862             {
12863               if (GET_CODE (varloc) == CONST
12864                   || GET_CODE (varloc) == SIGN_EXTEND
12865                   || GET_CODE (varloc) == ZERO_EXTEND)
12866                 varloc = XEXP (varloc, 0);
12867               else if (GET_CODE (varloc) == SUBREG)
12868                 varloc = SUBREG_REG (varloc);
12869               else
12870                 break;
12871             }
12872           while (1);
12873           /* DW_OP_bit_size offset should be zero for register
12874              or implicit location descriptions and empty location
12875              descriptions, but for memory addresses needs big endian
12876              adjustment.  */
12877           if (MEM_P (varloc))
12878             {
12879               unsigned HOST_WIDE_INT memsize
12880                 = MEM_SIZE (varloc) * BITS_PER_UNIT;
12881               if (memsize != bitsize)
12882                 {
12883                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
12884                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
12885                     return NULL;
12886                   if (memsize < bitsize)
12887                     return NULL;
12888                   if (BITS_BIG_ENDIAN)
12889                     offset = memsize - bitsize;
12890                 }
12891             }
12892
12893           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
12894           if (*descr_tail == NULL)
12895             return NULL;
12896           descr_tail = &(*descr_tail)->dw_loc_next;
12897         }
12898     }
12899
12900   /* If there were any non-empty expressions, add padding till the end of
12901      the decl.  */
12902   if (descr != NULL && decl_size != 0)
12903     {
12904       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
12905       if (*descr_tail == NULL)
12906         return NULL;
12907     }
12908   return descr;
12909 }
12910
12911 /* Return the dwarf representation of the location list LOC_LIST of
12912    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
12913    function.  */
12914
12915 static dw_loc_list_ref
12916 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
12917 {
12918   const char *endname, *secname;
12919   rtx varloc;
12920   enum var_init_status initialized;
12921   struct var_loc_node *node;
12922   dw_loc_descr_ref descr;
12923   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12924   dw_loc_list_ref list = NULL;
12925   dw_loc_list_ref *listp = &list;
12926
12927   /* Now that we know what section we are using for a base,
12928      actually construct the list of locations.
12929      The first location information is what is passed to the
12930      function that creates the location list, and the remaining
12931      locations just get added on to that list.
12932      Note that we only know the start address for a location
12933      (IE location changes), so to build the range, we use
12934      the range [current location start, next location start].
12935      This means we have to special case the last node, and generate
12936      a range of [last location start, end of function label].  */
12937
12938   secname = secname_for_decl (decl);
12939
12940   for (node = loc_list->first; node; node = node->next)
12941     if (GET_CODE (node->loc) == EXPR_LIST
12942         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
12943       {
12944         if (GET_CODE (node->loc) == EXPR_LIST)
12945           {
12946             /* This requires DW_OP_{,bit_}piece, which is not usable
12947                inside DWARF expressions.  */
12948             if (want_address != 2)
12949               continue;
12950             descr = dw_sra_loc_expr (decl, node->loc);
12951             if (descr == NULL)
12952               continue;
12953           }
12954         else
12955           {
12956             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
12957             varloc = NOTE_VAR_LOCATION (node->loc);
12958             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
12959           }
12960         if (descr)
12961           {
12962             bool range_across_switch = false;
12963             /* If section switch happens in between node->label
12964                and node->next->label (or end of function) and
12965                we can't emit it as a single entry list,
12966                emit two ranges, first one ending at the end
12967                of first partition and second one starting at the
12968                beginning of second partition.  */
12969             if (node == loc_list->last_before_switch
12970                 && (node != loc_list->first || loc_list->first->next)
12971                 && current_function_decl)
12972               {
12973                 endname = cfun->fde->dw_fde_end;
12974                 range_across_switch = true;
12975               }
12976             /* The variable has a location between NODE->LABEL and
12977                NODE->NEXT->LABEL.  */
12978             else if (node->next)
12979               endname = node->next->label;
12980             /* If the variable has a location at the last label
12981                it keeps its location until the end of function.  */
12982             else if (!current_function_decl)
12983               endname = text_end_label;
12984             else
12985               {
12986                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12987                                              current_function_funcdef_no);
12988                 endname = ggc_strdup (label_id);
12989               }
12990
12991             *listp = new_loc_list (descr, node->label, endname, secname);
12992             if (TREE_CODE (decl) == PARM_DECL
12993                 && node == loc_list->first
12994                 && GET_CODE (node->loc) == NOTE
12995                 && strcmp (node->label, endname) == 0)
12996               (*listp)->force = true;
12997             listp = &(*listp)->dw_loc_next;
12998
12999             if (range_across_switch)
13000               {
13001                 if (GET_CODE (node->loc) == EXPR_LIST)
13002                   descr = dw_sra_loc_expr (decl, node->loc);
13003                 else
13004                   {
13005                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13006                     varloc = NOTE_VAR_LOCATION (node->loc);
13007                     descr = dw_loc_list_1 (decl, varloc, want_address,
13008                                            initialized);
13009                   }
13010                 gcc_assert (descr);
13011                 /* The variable has a location between NODE->LABEL and
13012                    NODE->NEXT->LABEL.  */
13013                 if (node->next)
13014                   endname = node->next->label;
13015                 else
13016                   endname = cfun->fde->dw_fde_second_end;
13017                 *listp = new_loc_list (descr,
13018                                        cfun->fde->dw_fde_second_begin,
13019                                        endname, secname);
13020                 listp = &(*listp)->dw_loc_next;
13021               }
13022           }
13023       }
13024
13025   /* Try to avoid the overhead of a location list emitting a location
13026      expression instead, but only if we didn't have more than one
13027      location entry in the first place.  If some entries were not
13028      representable, we don't want to pretend a single entry that was
13029      applies to the entire scope in which the variable is
13030      available.  */
13031   if (list && loc_list->first->next)
13032     gen_llsym (list);
13033
13034   return list;
13035 }
13036
13037 /* Return if the loc_list has only single element and thus can be represented
13038    as location description.   */
13039
13040 static bool
13041 single_element_loc_list_p (dw_loc_list_ref list)
13042 {
13043   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13044   return !list->ll_symbol;
13045 }
13046
13047 /* To each location in list LIST add loc descr REF.  */
13048
13049 static void
13050 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13051 {
13052   dw_loc_descr_ref copy;
13053   add_loc_descr (&list->expr, ref);
13054   list = list->dw_loc_next;
13055   while (list)
13056     {
13057       copy = ggc_alloc_dw_loc_descr_node ();
13058       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13059       add_loc_descr (&list->expr, copy);
13060       while (copy->dw_loc_next)
13061         {
13062           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13063           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13064           copy->dw_loc_next = new_copy;
13065           copy = new_copy;
13066         }
13067       list = list->dw_loc_next;
13068     }
13069 }
13070
13071 /* Given two lists RET and LIST
13072    produce location list that is result of adding expression in LIST
13073    to expression in RET on each possition in program.
13074    Might be destructive on both RET and LIST.
13075
13076    TODO: We handle only simple cases of RET or LIST having at most one
13077    element. General case would inolve sorting the lists in program order
13078    and merging them that will need some additional work.
13079    Adding that will improve quality of debug info especially for SRA-ed
13080    structures.  */
13081
13082 static void
13083 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13084 {
13085   if (!list)
13086     return;
13087   if (!*ret)
13088     {
13089       *ret = list;
13090       return;
13091     }
13092   if (!list->dw_loc_next)
13093     {
13094       add_loc_descr_to_each (*ret, list->expr);
13095       return;
13096     }
13097   if (!(*ret)->dw_loc_next)
13098     {
13099       add_loc_descr_to_each (list, (*ret)->expr);
13100       *ret = list;
13101       return;
13102     }
13103   expansion_failed (NULL_TREE, NULL_RTX,
13104                     "Don't know how to merge two non-trivial"
13105                     " location lists.\n");
13106   *ret = NULL;
13107   return;
13108 }
13109
13110 /* LOC is constant expression.  Try a luck, look it up in constant
13111    pool and return its loc_descr of its address.  */
13112
13113 static dw_loc_descr_ref
13114 cst_pool_loc_descr (tree loc)
13115 {
13116   /* Get an RTL for this, if something has been emitted.  */
13117   rtx rtl = lookup_constant_def (loc);
13118
13119   if (!rtl || !MEM_P (rtl))
13120     {
13121       gcc_assert (!rtl);
13122       return 0;
13123     }
13124   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13125
13126   /* TODO: We might get more coverage if we was actually delaying expansion
13127      of all expressions till end of compilation when constant pools are fully
13128      populated.  */
13129   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13130     {
13131       expansion_failed (loc, NULL_RTX,
13132                         "CST value in contant pool but not marked.");
13133       return 0;
13134     }
13135   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13136                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13137 }
13138
13139 /* Return dw_loc_list representing address of addr_expr LOC
13140    by looking for innder INDIRECT_REF expression and turing it
13141    into simple arithmetics.  */
13142
13143 static dw_loc_list_ref
13144 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13145 {
13146   tree obj, offset;
13147   HOST_WIDE_INT bitsize, bitpos, bytepos;
13148   enum machine_mode mode;
13149   int volatilep;
13150   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13151   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13152
13153   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13154                              &bitsize, &bitpos, &offset, &mode,
13155                              &unsignedp, &volatilep, false);
13156   STRIP_NOPS (obj);
13157   if (bitpos % BITS_PER_UNIT)
13158     {
13159       expansion_failed (loc, NULL_RTX, "bitfield access");
13160       return 0;
13161     }
13162   if (!INDIRECT_REF_P (obj))
13163     {
13164       expansion_failed (obj,
13165                         NULL_RTX, "no indirect ref in inner refrence");
13166       return 0;
13167     }
13168   if (!offset && !bitpos)
13169     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13170   else if (toplev
13171            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13172            && (dwarf_version >= 4 || !dwarf_strict))
13173     {
13174       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13175       if (!list_ret)
13176         return 0;
13177       if (offset)
13178         {
13179           /* Variable offset.  */
13180           list_ret1 = loc_list_from_tree (offset, 0);
13181           if (list_ret1 == 0)
13182             return 0;
13183           add_loc_list (&list_ret, list_ret1);
13184           if (!list_ret)
13185             return 0;
13186           add_loc_descr_to_each (list_ret,
13187                                  new_loc_descr (DW_OP_plus, 0, 0));
13188         }
13189       bytepos = bitpos / BITS_PER_UNIT;
13190       if (bytepos > 0)
13191         add_loc_descr_to_each (list_ret,
13192                                new_loc_descr (DW_OP_plus_uconst,
13193                                               bytepos, 0));
13194       else if (bytepos < 0)
13195         loc_list_plus_const (list_ret, bytepos);
13196       add_loc_descr_to_each (list_ret,
13197                              new_loc_descr (DW_OP_stack_value, 0, 0));
13198     }
13199   return list_ret;
13200 }
13201
13202
13203 /* Generate Dwarf location list representing LOC.
13204    If WANT_ADDRESS is false, expression computing LOC will be computed
13205    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13206    if WANT_ADDRESS is 2, expression computing address useable in location
13207      will be returned (i.e. DW_OP_reg can be used
13208      to refer to register values).  */
13209
13210 static dw_loc_list_ref
13211 loc_list_from_tree (tree loc, int want_address)
13212 {
13213   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13214   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13215   int have_address = 0;
13216   enum dwarf_location_atom op;
13217
13218   /* ??? Most of the time we do not take proper care for sign/zero
13219      extending the values properly.  Hopefully this won't be a real
13220      problem...  */
13221
13222   switch (TREE_CODE (loc))
13223     {
13224     case ERROR_MARK:
13225       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13226       return 0;
13227
13228     case PLACEHOLDER_EXPR:
13229       /* This case involves extracting fields from an object to determine the
13230          position of other fields.  We don't try to encode this here.  The
13231          only user of this is Ada, which encodes the needed information using
13232          the names of types.  */
13233       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13234       return 0;
13235
13236     case CALL_EXPR:
13237       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13238       /* There are no opcodes for these operations.  */
13239       return 0;
13240
13241     case PREINCREMENT_EXPR:
13242     case PREDECREMENT_EXPR:
13243     case POSTINCREMENT_EXPR:
13244     case POSTDECREMENT_EXPR:
13245       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13246       /* There are no opcodes for these operations.  */
13247       return 0;
13248
13249     case ADDR_EXPR:
13250       /* If we already want an address, see if there is INDIRECT_REF inside
13251          e.g. for &this->field.  */
13252       if (want_address)
13253         {
13254           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13255                        (loc, want_address == 2);
13256           if (list_ret)
13257             have_address = 1;
13258           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13259                    && (ret = cst_pool_loc_descr (loc)))
13260             have_address = 1;
13261         }
13262         /* Otherwise, process the argument and look for the address.  */
13263       if (!list_ret && !ret)
13264         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13265       else
13266         {
13267           if (want_address)
13268             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13269           return NULL;
13270         }
13271       break;
13272
13273     case VAR_DECL:
13274       if (DECL_THREAD_LOCAL_P (loc))
13275         {
13276           rtx rtl;
13277           enum dwarf_location_atom first_op;
13278           enum dwarf_location_atom second_op;
13279           bool dtprel = false;
13280
13281           if (targetm.have_tls)
13282             {
13283               /* If this is not defined, we have no way to emit the
13284                  data.  */
13285               if (!targetm.asm_out.output_dwarf_dtprel)
13286                 return 0;
13287
13288                /* The way DW_OP_GNU_push_tls_address is specified, we
13289                   can only look up addresses of objects in the current
13290                   module.  We used DW_OP_addr as first op, but that's
13291                   wrong, because DW_OP_addr is relocated by the debug
13292                   info consumer, while DW_OP_GNU_push_tls_address
13293                   operand shouldn't be.  */
13294               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13295                 return 0;
13296               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13297               dtprel = true;
13298               second_op = DW_OP_GNU_push_tls_address;
13299             }
13300           else
13301             {
13302               if (!targetm.emutls.debug_form_tls_address
13303                   || !(dwarf_version >= 3 || !dwarf_strict))
13304                 return 0;
13305               /* We stuffed the control variable into the DECL_VALUE_EXPR
13306                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13307                  no longer appear in gimple code.  We used the control
13308                  variable in specific so that we could pick it up here.  */
13309               loc = DECL_VALUE_EXPR (loc);
13310               first_op = DW_OP_addr;
13311               second_op = DW_OP_form_tls_address;
13312             }
13313
13314           rtl = rtl_for_decl_location (loc);
13315           if (rtl == NULL_RTX)
13316             return 0;
13317
13318           if (!MEM_P (rtl))
13319             return 0;
13320           rtl = XEXP (rtl, 0);
13321           if (! CONSTANT_P (rtl))
13322             return 0;
13323
13324           ret = new_loc_descr (first_op, 0, 0);
13325           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13326           ret->dw_loc_oprnd1.v.val_addr = rtl;
13327           ret->dtprel = dtprel;
13328
13329           ret1 = new_loc_descr (second_op, 0, 0);
13330           add_loc_descr (&ret, ret1);
13331
13332           have_address = 1;
13333           break;
13334         }
13335       /* FALLTHRU */
13336
13337     case PARM_DECL:
13338     case RESULT_DECL:
13339       if (DECL_HAS_VALUE_EXPR_P (loc))
13340         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13341                                    want_address);
13342       /* FALLTHRU */
13343
13344     case FUNCTION_DECL:
13345       {
13346         rtx rtl;
13347         var_loc_list *loc_list = lookup_decl_loc (loc);
13348
13349         if (loc_list && loc_list->first)
13350           {
13351             list_ret = dw_loc_list (loc_list, loc, want_address);
13352             have_address = want_address != 0;
13353             break;
13354           }
13355         rtl = rtl_for_decl_location (loc);
13356         if (rtl == NULL_RTX)
13357           {
13358             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13359             return 0;
13360           }
13361         else if (CONST_INT_P (rtl))
13362           {
13363             HOST_WIDE_INT val = INTVAL (rtl);
13364             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13365               val &= GET_MODE_MASK (DECL_MODE (loc));
13366             ret = int_loc_descriptor (val);
13367           }
13368         else if (GET_CODE (rtl) == CONST_STRING)
13369           {
13370             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13371             return 0;
13372           }
13373         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13374           {
13375             ret = new_loc_descr (DW_OP_addr, 0, 0);
13376             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13377             ret->dw_loc_oprnd1.v.val_addr = rtl;
13378           }
13379         else
13380           {
13381             enum machine_mode mode, mem_mode;
13382
13383             /* Certain constructs can only be represented at top-level.  */
13384             if (want_address == 2)
13385               {
13386                 ret = loc_descriptor (rtl, VOIDmode,
13387                                       VAR_INIT_STATUS_INITIALIZED);
13388                 have_address = 1;
13389               }
13390             else
13391               {
13392                 mode = GET_MODE (rtl);
13393                 mem_mode = VOIDmode;
13394                 if (MEM_P (rtl))
13395                   {
13396                     mem_mode = mode;
13397                     mode = get_address_mode (rtl);
13398                     rtl = XEXP (rtl, 0);
13399                     have_address = 1;
13400                   }
13401                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13402                                           VAR_INIT_STATUS_INITIALIZED);
13403               }
13404             if (!ret)
13405               expansion_failed (loc, rtl,
13406                                 "failed to produce loc descriptor for rtl");
13407           }
13408       }
13409       break;
13410
13411     case MEM_REF:
13412       /* ??? FIXME.  */
13413       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13414         return 0;
13415       /* Fallthru.  */
13416     case INDIRECT_REF:
13417       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13418       have_address = 1;
13419       break;
13420
13421     case COMPOUND_EXPR:
13422       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13423
13424     CASE_CONVERT:
13425     case VIEW_CONVERT_EXPR:
13426     case SAVE_EXPR:
13427     case MODIFY_EXPR:
13428       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13429
13430     case COMPONENT_REF:
13431     case BIT_FIELD_REF:
13432     case ARRAY_REF:
13433     case ARRAY_RANGE_REF:
13434     case REALPART_EXPR:
13435     case IMAGPART_EXPR:
13436       {
13437         tree obj, offset;
13438         HOST_WIDE_INT bitsize, bitpos, bytepos;
13439         enum machine_mode mode;
13440         int volatilep;
13441         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13442
13443         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13444                                    &unsignedp, &volatilep, false);
13445
13446         gcc_assert (obj != loc);
13447
13448         list_ret = loc_list_from_tree (obj,
13449                                        want_address == 2
13450                                        && !bitpos && !offset ? 2 : 1);
13451         /* TODO: We can extract value of the small expression via shifting even
13452            for nonzero bitpos.  */
13453         if (list_ret == 0)
13454           return 0;
13455         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13456           {
13457             expansion_failed (loc, NULL_RTX,
13458                               "bitfield access");
13459             return 0;
13460           }
13461
13462         if (offset != NULL_TREE)
13463           {
13464             /* Variable offset.  */
13465             list_ret1 = loc_list_from_tree (offset, 0);
13466             if (list_ret1 == 0)
13467               return 0;
13468             add_loc_list (&list_ret, list_ret1);
13469             if (!list_ret)
13470               return 0;
13471             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13472           }
13473
13474         bytepos = bitpos / BITS_PER_UNIT;
13475         if (bytepos > 0)
13476           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13477         else if (bytepos < 0)
13478           loc_list_plus_const (list_ret, bytepos);
13479
13480         have_address = 1;
13481         break;
13482       }
13483
13484     case INTEGER_CST:
13485       if ((want_address || !host_integerp (loc, 0))
13486           && (ret = cst_pool_loc_descr (loc)))
13487         have_address = 1;
13488       else if (want_address == 2
13489                && host_integerp (loc, 0)
13490                && (ret = address_of_int_loc_descriptor
13491                            (int_size_in_bytes (TREE_TYPE (loc)),
13492                             tree_low_cst (loc, 0))))
13493         have_address = 1;
13494       else if (host_integerp (loc, 0))
13495         ret = int_loc_descriptor (tree_low_cst (loc, 0));
13496       else
13497         {
13498           expansion_failed (loc, NULL_RTX,
13499                             "Integer operand is not host integer");
13500           return 0;
13501         }
13502       break;
13503
13504     case CONSTRUCTOR:
13505     case REAL_CST:
13506     case STRING_CST:
13507     case COMPLEX_CST:
13508       if ((ret = cst_pool_loc_descr (loc)))
13509         have_address = 1;
13510       else
13511       /* We can construct small constants here using int_loc_descriptor.  */
13512         expansion_failed (loc, NULL_RTX,
13513                           "constructor or constant not in constant pool");
13514       break;
13515
13516     case TRUTH_AND_EXPR:
13517     case TRUTH_ANDIF_EXPR:
13518     case BIT_AND_EXPR:
13519       op = DW_OP_and;
13520       goto do_binop;
13521
13522     case TRUTH_XOR_EXPR:
13523     case BIT_XOR_EXPR:
13524       op = DW_OP_xor;
13525       goto do_binop;
13526
13527     case TRUTH_OR_EXPR:
13528     case TRUTH_ORIF_EXPR:
13529     case BIT_IOR_EXPR:
13530       op = DW_OP_or;
13531       goto do_binop;
13532
13533     case FLOOR_DIV_EXPR:
13534     case CEIL_DIV_EXPR:
13535     case ROUND_DIV_EXPR:
13536     case TRUNC_DIV_EXPR:
13537       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13538         return 0;
13539       op = DW_OP_div;
13540       goto do_binop;
13541
13542     case MINUS_EXPR:
13543       op = DW_OP_minus;
13544       goto do_binop;
13545
13546     case FLOOR_MOD_EXPR:
13547     case CEIL_MOD_EXPR:
13548     case ROUND_MOD_EXPR:
13549     case TRUNC_MOD_EXPR:
13550       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13551         {
13552           op = DW_OP_mod;
13553           goto do_binop;
13554         }
13555       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13556       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13557       if (list_ret == 0 || list_ret1 == 0)
13558         return 0;
13559
13560       add_loc_list (&list_ret, list_ret1);
13561       if (list_ret == 0)
13562         return 0;
13563       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13564       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
13565       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
13566       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
13567       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
13568       break;
13569
13570     case MULT_EXPR:
13571       op = DW_OP_mul;
13572       goto do_binop;
13573
13574     case LSHIFT_EXPR:
13575       op = DW_OP_shl;
13576       goto do_binop;
13577
13578     case RSHIFT_EXPR:
13579       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
13580       goto do_binop;
13581
13582     case POINTER_PLUS_EXPR:
13583     case PLUS_EXPR:
13584       if (host_integerp (TREE_OPERAND (loc, 1), 0))
13585         {
13586           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13587           if (list_ret == 0)
13588             return 0;
13589
13590           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
13591           break;
13592         }
13593
13594       op = DW_OP_plus;
13595       goto do_binop;
13596
13597     case LE_EXPR:
13598       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13599         return 0;
13600
13601       op = DW_OP_le;
13602       goto do_binop;
13603
13604     case GE_EXPR:
13605       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13606         return 0;
13607
13608       op = DW_OP_ge;
13609       goto do_binop;
13610
13611     case LT_EXPR:
13612       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13613         return 0;
13614
13615       op = DW_OP_lt;
13616       goto do_binop;
13617
13618     case GT_EXPR:
13619       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
13620         return 0;
13621
13622       op = DW_OP_gt;
13623       goto do_binop;
13624
13625     case EQ_EXPR:
13626       op = DW_OP_eq;
13627       goto do_binop;
13628
13629     case NE_EXPR:
13630       op = DW_OP_ne;
13631       goto do_binop;
13632
13633     do_binop:
13634       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13635       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
13636       if (list_ret == 0 || list_ret1 == 0)
13637         return 0;
13638
13639       add_loc_list (&list_ret, list_ret1);
13640       if (list_ret == 0)
13641         return 0;
13642       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13643       break;
13644
13645     case TRUTH_NOT_EXPR:
13646     case BIT_NOT_EXPR:
13647       op = DW_OP_not;
13648       goto do_unop;
13649
13650     case ABS_EXPR:
13651       op = DW_OP_abs;
13652       goto do_unop;
13653
13654     case NEGATE_EXPR:
13655       op = DW_OP_neg;
13656       goto do_unop;
13657
13658     do_unop:
13659       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13660       if (list_ret == 0)
13661         return 0;
13662
13663       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
13664       break;
13665
13666     case MIN_EXPR:
13667     case MAX_EXPR:
13668       {
13669         const enum tree_code code =
13670           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
13671
13672         loc = build3 (COND_EXPR, TREE_TYPE (loc),
13673                       build2 (code, integer_type_node,
13674                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
13675                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
13676       }
13677
13678       /* ... fall through ...  */
13679
13680     case COND_EXPR:
13681       {
13682         dw_loc_descr_ref lhs
13683           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
13684         dw_loc_list_ref rhs
13685           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
13686         dw_loc_descr_ref bra_node, jump_node, tmp;
13687
13688         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13689         if (list_ret == 0 || lhs == 0 || rhs == 0)
13690           return 0;
13691
13692         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
13693         add_loc_descr_to_each (list_ret, bra_node);
13694
13695         add_loc_list (&list_ret, rhs);
13696         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
13697         add_loc_descr_to_each (list_ret, jump_node);
13698
13699         add_loc_descr_to_each (list_ret, lhs);
13700         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13701         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
13702
13703         /* ??? Need a node to point the skip at.  Use a nop.  */
13704         tmp = new_loc_descr (DW_OP_nop, 0, 0);
13705         add_loc_descr_to_each (list_ret, tmp);
13706         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
13707         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
13708       }
13709       break;
13710
13711     case FIX_TRUNC_EXPR:
13712       return 0;
13713
13714     default:
13715       /* Leave front-end specific codes as simply unknown.  This comes
13716          up, for instance, with the C STMT_EXPR.  */
13717       if ((unsigned int) TREE_CODE (loc)
13718           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
13719         {
13720           expansion_failed (loc, NULL_RTX,
13721                             "language specific tree node");
13722           return 0;
13723         }
13724
13725 #ifdef ENABLE_CHECKING
13726       /* Otherwise this is a generic code; we should just lists all of
13727          these explicitly.  We forgot one.  */
13728       gcc_unreachable ();
13729 #else
13730       /* In a release build, we want to degrade gracefully: better to
13731          generate incomplete debugging information than to crash.  */
13732       return NULL;
13733 #endif
13734     }
13735
13736   if (!ret && !list_ret)
13737     return 0;
13738
13739   if (want_address == 2 && !have_address
13740       && (dwarf_version >= 4 || !dwarf_strict))
13741     {
13742       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13743         {
13744           expansion_failed (loc, NULL_RTX,
13745                             "DWARF address size mismatch");
13746           return 0;
13747         }
13748       if (ret)
13749         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13750       else
13751         add_loc_descr_to_each (list_ret,
13752                                new_loc_descr (DW_OP_stack_value, 0, 0));
13753       have_address = 1;
13754     }
13755   /* Show if we can't fill the request for an address.  */
13756   if (want_address && !have_address)
13757     {
13758       expansion_failed (loc, NULL_RTX,
13759                         "Want address and only have value");
13760       return 0;
13761     }
13762
13763   gcc_assert (!ret || !list_ret);
13764
13765   /* If we've got an address and don't want one, dereference.  */
13766   if (!want_address && have_address)
13767     {
13768       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13769
13770       if (size > DWARF2_ADDR_SIZE || size == -1)
13771         {
13772           expansion_failed (loc, NULL_RTX,
13773                             "DWARF address size mismatch");
13774           return 0;
13775         }
13776       else if (size == DWARF2_ADDR_SIZE)
13777         op = DW_OP_deref;
13778       else
13779         op = DW_OP_deref_size;
13780
13781       if (ret)
13782         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13783       else
13784         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13785     }
13786   if (ret)
13787     list_ret = new_loc_list (ret, NULL, NULL, NULL);
13788
13789   return list_ret;
13790 }
13791
13792 /* Same as above but return only single location expression.  */
13793 static dw_loc_descr_ref
13794 loc_descriptor_from_tree (tree loc, int want_address)
13795 {
13796   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13797   if (!ret)
13798     return NULL;
13799   if (ret->dw_loc_next)
13800     {
13801       expansion_failed (loc, NULL_RTX,
13802                         "Location list where only loc descriptor needed");
13803       return NULL;
13804     }
13805   return ret->expr;
13806 }
13807
13808 /* Given a value, round it up to the lowest multiple of `boundary'
13809    which is not less than the value itself.  */
13810
13811 static inline HOST_WIDE_INT
13812 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13813 {
13814   return (((value + boundary - 1) / boundary) * boundary);
13815 }
13816
13817 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13818    pointer to the declared type for the relevant field variable, or return
13819    `integer_type_node' if the given node turns out to be an
13820    ERROR_MARK node.  */
13821
13822 static inline tree
13823 field_type (const_tree decl)
13824 {
13825   tree type;
13826
13827   if (TREE_CODE (decl) == ERROR_MARK)
13828     return integer_type_node;
13829
13830   type = DECL_BIT_FIELD_TYPE (decl);
13831   if (type == NULL_TREE)
13832     type = TREE_TYPE (decl);
13833
13834   return type;
13835 }
13836
13837 /* Given a pointer to a tree node, return the alignment in bits for
13838    it, or else return BITS_PER_WORD if the node actually turns out to
13839    be an ERROR_MARK node.  */
13840
13841 static inline unsigned
13842 simple_type_align_in_bits (const_tree type)
13843 {
13844   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13845 }
13846
13847 static inline unsigned
13848 simple_decl_align_in_bits (const_tree decl)
13849 {
13850   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13851 }
13852
13853 /* Return the result of rounding T up to ALIGN.  */
13854
13855 static inline double_int
13856 round_up_to_align (double_int t, unsigned int align)
13857 {
13858   double_int alignd = uhwi_to_double_int (align);
13859   t = double_int_add (t, alignd);
13860   t = double_int_add (t, double_int_minus_one);
13861   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
13862   t = double_int_mul (t, alignd);
13863   return t;
13864 }
13865
13866 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13867    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13868    or return 0 if we are unable to determine what that offset is, either
13869    because the argument turns out to be a pointer to an ERROR_MARK node, or
13870    because the offset is actually variable.  (We can't handle the latter case
13871    just yet).  */
13872
13873 static HOST_WIDE_INT
13874 field_byte_offset (const_tree decl)
13875 {
13876   double_int object_offset_in_bits;
13877   double_int object_offset_in_bytes;
13878   double_int bitpos_int;
13879
13880   if (TREE_CODE (decl) == ERROR_MARK)
13881     return 0;
13882
13883   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13884
13885   /* We cannot yet cope with fields whose positions are variable, so
13886      for now, when we see such things, we simply return 0.  Someday, we may
13887      be able to handle such cases, but it will be damn difficult.  */
13888   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
13889     return 0;
13890
13891   bitpos_int = tree_to_double_int (bit_position (decl));
13892
13893 #ifdef PCC_BITFIELD_TYPE_MATTERS
13894   if (PCC_BITFIELD_TYPE_MATTERS)
13895     {
13896       tree type;
13897       tree field_size_tree;
13898       double_int deepest_bitpos;
13899       double_int field_size_in_bits;
13900       unsigned int type_align_in_bits;
13901       unsigned int decl_align_in_bits;
13902       double_int type_size_in_bits;
13903
13904       type = field_type (decl);
13905       type_size_in_bits = double_int_type_size_in_bits (type);
13906       type_align_in_bits = simple_type_align_in_bits (type);
13907
13908       field_size_tree = DECL_SIZE (decl);
13909
13910       /* The size could be unspecified if there was an error, or for
13911          a flexible array member.  */
13912       if (!field_size_tree)
13913         field_size_tree = bitsize_zero_node;
13914
13915       /* If the size of the field is not constant, use the type size.  */
13916       if (TREE_CODE (field_size_tree) == INTEGER_CST)
13917         field_size_in_bits = tree_to_double_int (field_size_tree);
13918       else
13919         field_size_in_bits = type_size_in_bits;
13920
13921       decl_align_in_bits = simple_decl_align_in_bits (decl);
13922
13923       /* The GCC front-end doesn't make any attempt to keep track of the
13924          starting bit offset (relative to the start of the containing
13925          structure type) of the hypothetical "containing object" for a
13926          bit-field.  Thus, when computing the byte offset value for the
13927          start of the "containing object" of a bit-field, we must deduce
13928          this information on our own. This can be rather tricky to do in
13929          some cases.  For example, handling the following structure type
13930          definition when compiling for an i386/i486 target (which only
13931          aligns long long's to 32-bit boundaries) can be very tricky:
13932
13933          struct S { int field1; long long field2:31; };
13934
13935          Fortunately, there is a simple rule-of-thumb which can be used
13936          in such cases.  When compiling for an i386/i486, GCC will
13937          allocate 8 bytes for the structure shown above.  It decides to
13938          do this based upon one simple rule for bit-field allocation.
13939          GCC allocates each "containing object" for each bit-field at
13940          the first (i.e. lowest addressed) legitimate alignment boundary
13941          (based upon the required minimum alignment for the declared
13942          type of the field) which it can possibly use, subject to the
13943          condition that there is still enough available space remaining
13944          in the containing object (when allocated at the selected point)
13945          to fully accommodate all of the bits of the bit-field itself.
13946
13947          This simple rule makes it obvious why GCC allocates 8 bytes for
13948          each object of the structure type shown above.  When looking
13949          for a place to allocate the "containing object" for `field2',
13950          the compiler simply tries to allocate a 64-bit "containing
13951          object" at each successive 32-bit boundary (starting at zero)
13952          until it finds a place to allocate that 64- bit field such that
13953          at least 31 contiguous (and previously unallocated) bits remain
13954          within that selected 64 bit field.  (As it turns out, for the
13955          example above, the compiler finds it is OK to allocate the
13956          "containing object" 64-bit field at bit-offset zero within the
13957          structure type.)
13958
13959          Here we attempt to work backwards from the limited set of facts
13960          we're given, and we try to deduce from those facts, where GCC
13961          must have believed that the containing object started (within
13962          the structure type). The value we deduce is then used (by the
13963          callers of this routine) to generate DW_AT_location and
13964          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13965          the case of DW_AT_location, regular fields as well).  */
13966
13967       /* Figure out the bit-distance from the start of the structure to
13968          the "deepest" bit of the bit-field.  */
13969       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
13970
13971       /* This is the tricky part.  Use some fancy footwork to deduce
13972          where the lowest addressed bit of the containing object must
13973          be.  */
13974       object_offset_in_bits
13975         = double_int_sub (deepest_bitpos, type_size_in_bits);
13976
13977       /* Round up to type_align by default.  This works best for
13978          bitfields.  */
13979       object_offset_in_bits
13980         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13981
13982       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
13983         {
13984           object_offset_in_bits
13985             = double_int_sub (deepest_bitpos, type_size_in_bits);
13986
13987           /* Round up to decl_align instead.  */
13988           object_offset_in_bits
13989             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13990         }
13991     }
13992   else
13993 #endif /* PCC_BITFIELD_TYPE_MATTERS */
13994     object_offset_in_bits = bitpos_int;
13995
13996   object_offset_in_bytes
13997     = double_int_div (object_offset_in_bits,
13998                       uhwi_to_double_int (BITS_PER_UNIT), true,
13999                       TRUNC_DIV_EXPR);
14000   return double_int_to_shwi (object_offset_in_bytes);
14001 }
14002 \f
14003 /* The following routines define various Dwarf attributes and any data
14004    associated with them.  */
14005
14006 /* Add a location description attribute value to a DIE.
14007
14008    This emits location attributes suitable for whole variables and
14009    whole parameters.  Note that the location attributes for struct fields are
14010    generated by the routine `data_member_location_attribute' below.  */
14011
14012 static inline void
14013 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14014                              dw_loc_list_ref descr)
14015 {
14016   if (descr == 0)
14017     return;
14018   if (single_element_loc_list_p (descr))
14019     add_AT_loc (die, attr_kind, descr->expr);
14020   else
14021     add_AT_loc_list (die, attr_kind, descr);
14022 }
14023
14024 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14025
14026 static void
14027 add_accessibility_attribute (dw_die_ref die, tree decl)
14028 {
14029   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14030      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14031      the default has always been DW_ACCESS_public.  */
14032   if (TREE_PROTECTED (decl))
14033     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14034   else if (TREE_PRIVATE (decl))
14035     {
14036       if (dwarf_version == 2
14037           || die->die_parent == NULL
14038           || die->die_parent->die_tag != DW_TAG_class_type)
14039         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14040     }
14041   else if (dwarf_version > 2
14042            && die->die_parent
14043            && die->die_parent->die_tag == DW_TAG_class_type)
14044     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14045 }
14046
14047 /* Attach the specialized form of location attribute used for data members of
14048    struct and union types.  In the special case of a FIELD_DECL node which
14049    represents a bit-field, the "offset" part of this special location
14050    descriptor must indicate the distance in bytes from the lowest-addressed
14051    byte of the containing struct or union type to the lowest-addressed byte of
14052    the "containing object" for the bit-field.  (See the `field_byte_offset'
14053    function above).
14054
14055    For any given bit-field, the "containing object" is a hypothetical object
14056    (of some integral or enum type) within which the given bit-field lives.  The
14057    type of this hypothetical "containing object" is always the same as the
14058    declared type of the individual bit-field itself (for GCC anyway... the
14059    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14060    bytes) of the hypothetical "containing object" which will be given in the
14061    DW_AT_byte_size attribute for this bit-field.  (See the
14062    `byte_size_attribute' function below.)  It is also used when calculating the
14063    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14064    function below.)  */
14065
14066 static void
14067 add_data_member_location_attribute (dw_die_ref die, tree decl)
14068 {
14069   HOST_WIDE_INT offset;
14070   dw_loc_descr_ref loc_descr = 0;
14071
14072   if (TREE_CODE (decl) == TREE_BINFO)
14073     {
14074       /* We're working on the TAG_inheritance for a base class.  */
14075       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14076         {
14077           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14078              aren't at a fixed offset from all (sub)objects of the same
14079              type.  We need to extract the appropriate offset from our
14080              vtable.  The following dwarf expression means
14081
14082                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14083
14084              This is specific to the V3 ABI, of course.  */
14085
14086           dw_loc_descr_ref tmp;
14087
14088           /* Make a copy of the object address.  */
14089           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14090           add_loc_descr (&loc_descr, tmp);
14091
14092           /* Extract the vtable address.  */
14093           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14094           add_loc_descr (&loc_descr, tmp);
14095
14096           /* Calculate the address of the offset.  */
14097           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14098           gcc_assert (offset < 0);
14099
14100           tmp = int_loc_descriptor (-offset);
14101           add_loc_descr (&loc_descr, tmp);
14102           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14103           add_loc_descr (&loc_descr, tmp);
14104
14105           /* Extract the offset.  */
14106           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14107           add_loc_descr (&loc_descr, tmp);
14108
14109           /* Add it to the object address.  */
14110           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14111           add_loc_descr (&loc_descr, tmp);
14112         }
14113       else
14114         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14115     }
14116   else
14117     offset = field_byte_offset (decl);
14118
14119   if (! loc_descr)
14120     {
14121       if (dwarf_version > 2)
14122         {
14123           /* Don't need to output a location expression, just the constant. */
14124           if (offset < 0)
14125             add_AT_int (die, DW_AT_data_member_location, offset);
14126           else
14127             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14128           return;
14129         }
14130       else
14131         {
14132           enum dwarf_location_atom op;
14133
14134           /* The DWARF2 standard says that we should assume that the structure
14135              address is already on the stack, so we can specify a structure
14136              field address by using DW_OP_plus_uconst.  */
14137
14138 #ifdef MIPS_DEBUGGING_INFO
14139           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14140              operator correctly.  It works only if we leave the offset on the
14141              stack.  */
14142           op = DW_OP_constu;
14143 #else
14144           op = DW_OP_plus_uconst;
14145 #endif
14146
14147           loc_descr = new_loc_descr (op, offset, 0);
14148         }
14149     }
14150
14151   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14152 }
14153
14154 /* Writes integer values to dw_vec_const array.  */
14155
14156 static void
14157 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14158 {
14159   while (size != 0)
14160     {
14161       *dest++ = val & 0xff;
14162       val >>= 8;
14163       --size;
14164     }
14165 }
14166
14167 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14168
14169 static HOST_WIDE_INT
14170 extract_int (const unsigned char *src, unsigned int size)
14171 {
14172   HOST_WIDE_INT val = 0;
14173
14174   src += size;
14175   while (size != 0)
14176     {
14177       val <<= 8;
14178       val |= *--src & 0xff;
14179       --size;
14180     }
14181   return val;
14182 }
14183
14184 /* Writes double_int values to dw_vec_const array.  */
14185
14186 static void
14187 insert_double (double_int val, unsigned char *dest)
14188 {
14189   unsigned char *p0 = dest;
14190   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14191
14192   if (WORDS_BIG_ENDIAN)
14193     {
14194       p0 = p1;
14195       p1 = dest;
14196     }
14197
14198   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14199   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14200 }
14201
14202 /* Writes floating point values to dw_vec_const array.  */
14203
14204 static void
14205 insert_float (const_rtx rtl, unsigned char *array)
14206 {
14207   REAL_VALUE_TYPE rv;
14208   long val[4];
14209   int i;
14210
14211   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14212   real_to_target (val, &rv, GET_MODE (rtl));
14213
14214   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14215   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14216     {
14217       insert_int (val[i], 4, array);
14218       array += 4;
14219     }
14220 }
14221
14222 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14223    does not have a "location" either in memory or in a register.  These
14224    things can arise in GNU C when a constant is passed as an actual parameter
14225    to an inlined function.  They can also arise in C++ where declared
14226    constants do not necessarily get memory "homes".  */
14227
14228 static bool
14229 add_const_value_attribute (dw_die_ref die, rtx rtl)
14230 {
14231   switch (GET_CODE (rtl))
14232     {
14233     case CONST_INT:
14234       {
14235         HOST_WIDE_INT val = INTVAL (rtl);
14236
14237         if (val < 0)
14238           add_AT_int (die, DW_AT_const_value, val);
14239         else
14240           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14241       }
14242       return true;
14243
14244     case CONST_DOUBLE:
14245       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14246          floating-point constant.  A CONST_DOUBLE is used whenever the
14247          constant requires more than one word in order to be adequately
14248          represented.  */
14249       {
14250         enum machine_mode mode = GET_MODE (rtl);
14251
14252         if (SCALAR_FLOAT_MODE_P (mode))
14253           {
14254             unsigned int length = GET_MODE_SIZE (mode);
14255             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14256
14257             insert_float (rtl, array);
14258             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14259           }
14260         else
14261           add_AT_double (die, DW_AT_const_value,
14262                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14263       }
14264       return true;
14265
14266     case CONST_VECTOR:
14267       {
14268         enum machine_mode mode = GET_MODE (rtl);
14269         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14270         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14271         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14272           (length * elt_size);
14273         unsigned int i;
14274         unsigned char *p;
14275
14276         switch (GET_MODE_CLASS (mode))
14277           {
14278           case MODE_VECTOR_INT:
14279             for (i = 0, p = array; i < length; i++, p += elt_size)
14280               {
14281                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14282                 double_int val = rtx_to_double_int (elt);
14283
14284                 if (elt_size <= sizeof (HOST_WIDE_INT))
14285                   insert_int (double_int_to_shwi (val), elt_size, p);
14286                 else
14287                   {
14288                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14289                     insert_double (val, p);
14290                   }
14291               }
14292             break;
14293
14294           case MODE_VECTOR_FLOAT:
14295             for (i = 0, p = array; i < length; i++, p += elt_size)
14296               {
14297                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14298                 insert_float (elt, p);
14299               }
14300             break;
14301
14302           default:
14303             gcc_unreachable ();
14304           }
14305
14306         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14307       }
14308       return true;
14309
14310     case CONST_STRING:
14311       if (dwarf_version >= 4 || !dwarf_strict)
14312         {
14313           dw_loc_descr_ref loc_result;
14314           resolve_one_addr (&rtl, NULL);
14315         rtl_addr:
14316           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14317           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14318           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14319           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14320           add_AT_loc (die, DW_AT_location, loc_result);
14321           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14322           return true;
14323         }
14324       return false;
14325
14326     case CONST:
14327       if (CONSTANT_P (XEXP (rtl, 0)))
14328         return add_const_value_attribute (die, XEXP (rtl, 0));
14329       /* FALLTHROUGH */
14330     case SYMBOL_REF:
14331       if (!const_ok_for_output (rtl))
14332         return false;
14333     case LABEL_REF:
14334       if (dwarf_version >= 4 || !dwarf_strict)
14335         goto rtl_addr;
14336       return false;
14337
14338     case PLUS:
14339       /* In cases where an inlined instance of an inline function is passed
14340          the address of an `auto' variable (which is local to the caller) we
14341          can get a situation where the DECL_RTL of the artificial local
14342          variable (for the inlining) which acts as a stand-in for the
14343          corresponding formal parameter (of the inline function) will look
14344          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14345          exactly a compile-time constant expression, but it isn't the address
14346          of the (artificial) local variable either.  Rather, it represents the
14347          *value* which the artificial local variable always has during its
14348          lifetime.  We currently have no way to represent such quasi-constant
14349          values in Dwarf, so for now we just punt and generate nothing.  */
14350       return false;
14351
14352     case HIGH:
14353     case CONST_FIXED:
14354       return false;
14355
14356     case MEM:
14357       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14358           && MEM_READONLY_P (rtl)
14359           && GET_MODE (rtl) == BLKmode)
14360         {
14361           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14362           return true;
14363         }
14364       return false;
14365
14366     default:
14367       /* No other kinds of rtx should be possible here.  */
14368       gcc_unreachable ();
14369     }
14370   return false;
14371 }
14372
14373 /* Determine whether the evaluation of EXPR references any variables
14374    or functions which aren't otherwise used (and therefore may not be
14375    output).  */
14376 static tree
14377 reference_to_unused (tree * tp, int * walk_subtrees,
14378                      void * data ATTRIBUTE_UNUSED)
14379 {
14380   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14381     *walk_subtrees = 0;
14382
14383   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14384       && ! TREE_ASM_WRITTEN (*tp))
14385     return *tp;
14386   /* ???  The C++ FE emits debug information for using decls, so
14387      putting gcc_unreachable here falls over.  See PR31899.  For now
14388      be conservative.  */
14389   else if (!cgraph_global_info_ready
14390            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14391     return *tp;
14392   else if (TREE_CODE (*tp) == VAR_DECL)
14393     {
14394       struct varpool_node *node = varpool_get_node (*tp);
14395       if (!node || !node->needed)
14396         return *tp;
14397     }
14398   else if (TREE_CODE (*tp) == FUNCTION_DECL
14399            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14400     {
14401       /* The call graph machinery must have finished analyzing,
14402          optimizing and gimplifying the CU by now.
14403          So if *TP has no call graph node associated
14404          to it, it means *TP will not be emitted.  */
14405       if (!cgraph_get_node (*tp))
14406         return *tp;
14407     }
14408   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14409     return *tp;
14410
14411   return NULL_TREE;
14412 }
14413
14414 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14415    for use in a later add_const_value_attribute call.  */
14416
14417 static rtx
14418 rtl_for_decl_init (tree init, tree type)
14419 {
14420   rtx rtl = NULL_RTX;
14421
14422   STRIP_NOPS (init);
14423
14424   /* If a variable is initialized with a string constant without embedded
14425      zeros, build CONST_STRING.  */
14426   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14427     {
14428       tree enttype = TREE_TYPE (type);
14429       tree domain = TYPE_DOMAIN (type);
14430       enum machine_mode mode = TYPE_MODE (enttype);
14431
14432       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14433           && domain
14434           && integer_zerop (TYPE_MIN_VALUE (domain))
14435           && compare_tree_int (TYPE_MAX_VALUE (domain),
14436                                TREE_STRING_LENGTH (init) - 1) == 0
14437           && ((size_t) TREE_STRING_LENGTH (init)
14438               == strlen (TREE_STRING_POINTER (init)) + 1))
14439         {
14440           rtl = gen_rtx_CONST_STRING (VOIDmode,
14441                                       ggc_strdup (TREE_STRING_POINTER (init)));
14442           rtl = gen_rtx_MEM (BLKmode, rtl);
14443           MEM_READONLY_P (rtl) = 1;
14444         }
14445     }
14446   /* Other aggregates, and complex values, could be represented using
14447      CONCAT: FIXME!  */
14448   else if (AGGREGATE_TYPE_P (type)
14449            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14450                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14451            || TREE_CODE (type) == COMPLEX_TYPE)
14452     ;
14453   /* Vectors only work if their mode is supported by the target.
14454      FIXME: generic vectors ought to work too.  */
14455   else if (TREE_CODE (type) == VECTOR_TYPE
14456            && !VECTOR_MODE_P (TYPE_MODE (type)))
14457     ;
14458   /* If the initializer is something that we know will expand into an
14459      immediate RTL constant, expand it now.  We must be careful not to
14460      reference variables which won't be output.  */
14461   else if (initializer_constant_valid_p (init, type)
14462            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14463     {
14464       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14465          possible.  */
14466       if (TREE_CODE (type) == VECTOR_TYPE)
14467         switch (TREE_CODE (init))
14468           {
14469           case VECTOR_CST:
14470             break;
14471           case CONSTRUCTOR:
14472             if (TREE_CONSTANT (init))
14473               {
14474                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14475                 bool constant_p = true;
14476                 tree value;
14477                 unsigned HOST_WIDE_INT ix;
14478
14479                 /* Even when ctor is constant, it might contain non-*_CST
14480                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14481                    belong into VECTOR_CST nodes.  */
14482                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
14483                   if (!CONSTANT_CLASS_P (value))
14484                     {
14485                       constant_p = false;
14486                       break;
14487                     }
14488
14489                 if (constant_p)
14490                   {
14491                     init = build_vector_from_ctor (type, elts);
14492                     break;
14493                   }
14494               }
14495             /* FALLTHRU */
14496
14497           default:
14498             return NULL;
14499           }
14500
14501       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
14502
14503       /* If expand_expr returns a MEM, it wasn't immediate.  */
14504       gcc_assert (!rtl || !MEM_P (rtl));
14505     }
14506
14507   return rtl;
14508 }
14509
14510 /* Generate RTL for the variable DECL to represent its location.  */
14511
14512 static rtx
14513 rtl_for_decl_location (tree decl)
14514 {
14515   rtx rtl;
14516
14517   /* Here we have to decide where we are going to say the parameter "lives"
14518      (as far as the debugger is concerned).  We only have a couple of
14519      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
14520
14521      DECL_RTL normally indicates where the parameter lives during most of the
14522      activation of the function.  If optimization is enabled however, this
14523      could be either NULL or else a pseudo-reg.  Both of those cases indicate
14524      that the parameter doesn't really live anywhere (as far as the code
14525      generation parts of GCC are concerned) during most of the function's
14526      activation.  That will happen (for example) if the parameter is never
14527      referenced within the function.
14528
14529      We could just generate a location descriptor here for all non-NULL
14530      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
14531      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
14532      where DECL_RTL is NULL or is a pseudo-reg.
14533
14534      Note however that we can only get away with using DECL_INCOMING_RTL as
14535      a backup substitute for DECL_RTL in certain limited cases.  In cases
14536      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
14537      we can be sure that the parameter was passed using the same type as it is
14538      declared to have within the function, and that its DECL_INCOMING_RTL
14539      points us to a place where a value of that type is passed.
14540
14541      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
14542      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
14543      because in these cases DECL_INCOMING_RTL points us to a value of some
14544      type which is *different* from the type of the parameter itself.  Thus,
14545      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
14546      such cases, the debugger would end up (for example) trying to fetch a
14547      `float' from a place which actually contains the first part of a
14548      `double'.  That would lead to really incorrect and confusing
14549      output at debug-time.
14550
14551      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
14552      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
14553      are a couple of exceptions however.  On little-endian machines we can
14554      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
14555      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
14556      an integral type that is smaller than TREE_TYPE (decl). These cases arise
14557      when (on a little-endian machine) a non-prototyped function has a
14558      parameter declared to be of type `short' or `char'.  In such cases,
14559      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
14560      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
14561      passed `int' value.  If the debugger then uses that address to fetch
14562      a `short' or a `char' (on a little-endian machine) the result will be
14563      the correct data, so we allow for such exceptional cases below.
14564
14565      Note that our goal here is to describe the place where the given formal
14566      parameter lives during most of the function's activation (i.e. between the
14567      end of the prologue and the start of the epilogue).  We'll do that as best
14568      as we can. Note however that if the given formal parameter is modified
14569      sometime during the execution of the function, then a stack backtrace (at
14570      debug-time) will show the function as having been called with the *new*
14571      value rather than the value which was originally passed in.  This happens
14572      rarely enough that it is not a major problem, but it *is* a problem, and
14573      I'd like to fix it.
14574
14575      A future version of dwarf2out.c may generate two additional attributes for
14576      any given DW_TAG_formal_parameter DIE which will describe the "passed
14577      type" and the "passed location" for the given formal parameter in addition
14578      to the attributes we now generate to indicate the "declared type" and the
14579      "active location" for each parameter.  This additional set of attributes
14580      could be used by debuggers for stack backtraces. Separately, note that
14581      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
14582      This happens (for example) for inlined-instances of inline function formal
14583      parameters which are never referenced.  This really shouldn't be
14584      happening.  All PARM_DECL nodes should get valid non-NULL
14585      DECL_INCOMING_RTL values.  FIXME.  */
14586
14587   /* Use DECL_RTL as the "location" unless we find something better.  */
14588   rtl = DECL_RTL_IF_SET (decl);
14589
14590   /* When generating abstract instances, ignore everything except
14591      constants, symbols living in memory, and symbols living in
14592      fixed registers.  */
14593   if (! reload_completed)
14594     {
14595       if (rtl
14596           && (CONSTANT_P (rtl)
14597               || (MEM_P (rtl)
14598                   && CONSTANT_P (XEXP (rtl, 0)))
14599               || (REG_P (rtl)
14600                   && TREE_CODE (decl) == VAR_DECL
14601                   && TREE_STATIC (decl))))
14602         {
14603           rtl = targetm.delegitimize_address (rtl);
14604           return rtl;
14605         }
14606       rtl = NULL_RTX;
14607     }
14608   else if (TREE_CODE (decl) == PARM_DECL)
14609     {
14610       if (rtl == NULL_RTX
14611           || is_pseudo_reg (rtl)
14612           || (MEM_P (rtl)
14613               && is_pseudo_reg (XEXP (rtl, 0))
14614               && DECL_INCOMING_RTL (decl)
14615               && MEM_P (DECL_INCOMING_RTL (decl))
14616               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
14617         {
14618           tree declared_type = TREE_TYPE (decl);
14619           tree passed_type = DECL_ARG_TYPE (decl);
14620           enum machine_mode dmode = TYPE_MODE (declared_type);
14621           enum machine_mode pmode = TYPE_MODE (passed_type);
14622
14623           /* This decl represents a formal parameter which was optimized out.
14624              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
14625              all cases where (rtl == NULL_RTX) just below.  */
14626           if (dmode == pmode)
14627             rtl = DECL_INCOMING_RTL (decl);
14628           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
14629                    && SCALAR_INT_MODE_P (dmode)
14630                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
14631                    && DECL_INCOMING_RTL (decl))
14632             {
14633               rtx inc = DECL_INCOMING_RTL (decl);
14634               if (REG_P (inc))
14635                 rtl = inc;
14636               else if (MEM_P (inc))
14637                 {
14638                   if (BYTES_BIG_ENDIAN)
14639                     rtl = adjust_address_nv (inc, dmode,
14640                                              GET_MODE_SIZE (pmode)
14641                                              - GET_MODE_SIZE (dmode));
14642                   else
14643                     rtl = inc;
14644                 }
14645             }
14646         }
14647
14648       /* If the parm was passed in registers, but lives on the stack, then
14649          make a big endian correction if the mode of the type of the
14650          parameter is not the same as the mode of the rtl.  */
14651       /* ??? This is the same series of checks that are made in dbxout.c before
14652          we reach the big endian correction code there.  It isn't clear if all
14653          of these checks are necessary here, but keeping them all is the safe
14654          thing to do.  */
14655       else if (MEM_P (rtl)
14656                && XEXP (rtl, 0) != const0_rtx
14657                && ! CONSTANT_P (XEXP (rtl, 0))
14658                /* Not passed in memory.  */
14659                && !MEM_P (DECL_INCOMING_RTL (decl))
14660                /* Not passed by invisible reference.  */
14661                && (!REG_P (XEXP (rtl, 0))
14662                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
14663                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
14664 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
14665                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
14666 #endif
14667                      )
14668                /* Big endian correction check.  */
14669                && BYTES_BIG_ENDIAN
14670                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
14671                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
14672                    < UNITS_PER_WORD))
14673         {
14674           int offset = (UNITS_PER_WORD
14675                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
14676
14677           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14678                              plus_constant (XEXP (rtl, 0), offset));
14679         }
14680     }
14681   else if (TREE_CODE (decl) == VAR_DECL
14682            && rtl
14683            && MEM_P (rtl)
14684            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
14685            && BYTES_BIG_ENDIAN)
14686     {
14687       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
14688       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
14689
14690       /* If a variable is declared "register" yet is smaller than
14691          a register, then if we store the variable to memory, it
14692          looks like we're storing a register-sized value, when in
14693          fact we are not.  We need to adjust the offset of the
14694          storage location to reflect the actual value's bytes,
14695          else gdb will not be able to display it.  */
14696       if (rsize > dsize)
14697         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
14698                            plus_constant (XEXP (rtl, 0), rsize-dsize));
14699     }
14700
14701   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
14702      and will have been substituted directly into all expressions that use it.
14703      C does not have such a concept, but C++ and other languages do.  */
14704   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
14705     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
14706
14707   if (rtl)
14708     rtl = targetm.delegitimize_address (rtl);
14709
14710   /* If we don't look past the constant pool, we risk emitting a
14711      reference to a constant pool entry that isn't referenced from
14712      code, and thus is not emitted.  */
14713   if (rtl)
14714     rtl = avoid_constant_pool_reference (rtl);
14715
14716   /* Try harder to get a rtl.  If this symbol ends up not being emitted
14717      in the current CU, resolve_addr will remove the expression referencing
14718      it.  */
14719   if (rtl == NULL_RTX
14720       && TREE_CODE (decl) == VAR_DECL
14721       && !DECL_EXTERNAL (decl)
14722       && TREE_STATIC (decl)
14723       && DECL_NAME (decl)
14724       && !DECL_HARD_REGISTER (decl)
14725       && DECL_MODE (decl) != VOIDmode)
14726     {
14727       rtl = make_decl_rtl_for_debug (decl);
14728       if (!MEM_P (rtl)
14729           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
14730           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
14731         rtl = NULL_RTX;
14732     }
14733
14734   return rtl;
14735 }
14736
14737 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
14738    returned.  If so, the decl for the COMMON block is returned, and the
14739    value is the offset into the common block for the symbol.  */
14740
14741 static tree
14742 fortran_common (tree decl, HOST_WIDE_INT *value)
14743 {
14744   tree val_expr, cvar;
14745   enum machine_mode mode;
14746   HOST_WIDE_INT bitsize, bitpos;
14747   tree offset;
14748   int volatilep = 0, unsignedp = 0;
14749
14750   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
14751      it does not have a value (the offset into the common area), or if it
14752      is thread local (as opposed to global) then it isn't common, and shouldn't
14753      be handled as such.  */
14754   if (TREE_CODE (decl) != VAR_DECL
14755       || !TREE_STATIC (decl)
14756       || !DECL_HAS_VALUE_EXPR_P (decl)
14757       || !is_fortran ())
14758     return NULL_TREE;
14759
14760   val_expr = DECL_VALUE_EXPR (decl);
14761   if (TREE_CODE (val_expr) != COMPONENT_REF)
14762     return NULL_TREE;
14763
14764   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
14765                               &mode, &unsignedp, &volatilep, true);
14766
14767   if (cvar == NULL_TREE
14768       || TREE_CODE (cvar) != VAR_DECL
14769       || DECL_ARTIFICIAL (cvar)
14770       || !TREE_PUBLIC (cvar))
14771     return NULL_TREE;
14772
14773   *value = 0;
14774   if (offset != NULL)
14775     {
14776       if (!host_integerp (offset, 0))
14777         return NULL_TREE;
14778       *value = tree_low_cst (offset, 0);
14779     }
14780   if (bitpos != 0)
14781     *value += bitpos / BITS_PER_UNIT;
14782
14783   return cvar;
14784 }
14785
14786 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
14787    data attribute for a variable or a parameter.  We generate the
14788    DW_AT_const_value attribute only in those cases where the given variable
14789    or parameter does not have a true "location" either in memory or in a
14790    register.  This can happen (for example) when a constant is passed as an
14791    actual argument in a call to an inline function.  (It's possible that
14792    these things can crop up in other ways also.)  Note that one type of
14793    constant value which can be passed into an inlined function is a constant
14794    pointer.  This can happen for example if an actual argument in an inlined
14795    function call evaluates to a compile-time constant address.
14796
14797    CACHE_P is true if it is worth caching the location list for DECL,
14798    so that future calls can reuse it rather than regenerate it from scratch.
14799    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
14800    since we will need to refer to them each time the function is inlined.  */
14801
14802 static bool
14803 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
14804                                        enum dwarf_attribute attr)
14805 {
14806   rtx rtl;
14807   dw_loc_list_ref list;
14808   var_loc_list *loc_list;
14809   cached_dw_loc_list *cache;
14810   void **slot;
14811
14812   if (TREE_CODE (decl) == ERROR_MARK)
14813     return false;
14814
14815   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14816               || TREE_CODE (decl) == RESULT_DECL);
14817
14818   /* Try to get some constant RTL for this decl, and use that as the value of
14819      the location.  */
14820
14821   rtl = rtl_for_decl_location (decl);
14822   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14823       && add_const_value_attribute (die, rtl))
14824     return true;
14825
14826   /* See if we have single element location list that is equivalent to
14827      a constant value.  That way we are better to use add_const_value_attribute
14828      rather than expanding constant value equivalent.  */
14829   loc_list = lookup_decl_loc (decl);
14830   if (loc_list
14831       && loc_list->first
14832       && loc_list->first->next == NULL
14833       && NOTE_P (loc_list->first->loc)
14834       && NOTE_VAR_LOCATION (loc_list->first->loc)
14835       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
14836     {
14837       struct var_loc_node *node;
14838
14839       node = loc_list->first;
14840       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
14841       if (GET_CODE (rtl) == EXPR_LIST)
14842         rtl = XEXP (rtl, 0);
14843       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14844           && add_const_value_attribute (die, rtl))
14845          return true;
14846     }
14847   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
14848      list several times.  See if we've already cached the contents.  */
14849   list = NULL;
14850   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
14851     cache_p = false;
14852   if (cache_p)
14853     {
14854       cache = (cached_dw_loc_list *)
14855         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
14856       if (cache)
14857         list = cache->loc_list;
14858     }
14859   if (list == NULL)
14860     {
14861       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14862       /* It is usually worth caching this result if the decl is from
14863          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
14864       if (cache_p && list && list->dw_loc_next)
14865         {
14866           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
14867                                            DECL_UID (decl), INSERT);
14868           cache = ggc_alloc_cleared_cached_dw_loc_list ();
14869           cache->decl_id = DECL_UID (decl);
14870           cache->loc_list = list;
14871           *slot = cache;
14872         }
14873     }
14874   if (list)
14875     {
14876       add_AT_location_description (die, attr, list);
14877       return true;
14878     }
14879   /* None of that worked, so it must not really have a location;
14880      try adding a constant value attribute from the DECL_INITIAL.  */
14881   return tree_add_const_value_attribute_for_decl (die, decl);
14882 }
14883
14884 /* Add VARIABLE and DIE into deferred locations list.  */
14885
14886 static void
14887 defer_location (tree variable, dw_die_ref die)
14888 {
14889   deferred_locations entry;
14890   entry.variable = variable;
14891   entry.die = die;
14892   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14893 }
14894
14895 /* Helper function for tree_add_const_value_attribute.  Natively encode
14896    initializer INIT into an array.  Return true if successful.  */
14897
14898 static bool
14899 native_encode_initializer (tree init, unsigned char *array, int size)
14900 {
14901   tree type;
14902
14903   if (init == NULL_TREE)
14904     return false;
14905
14906   STRIP_NOPS (init);
14907   switch (TREE_CODE (init))
14908     {
14909     case STRING_CST:
14910       type = TREE_TYPE (init);
14911       if (TREE_CODE (type) == ARRAY_TYPE)
14912         {
14913           tree enttype = TREE_TYPE (type);
14914           enum machine_mode mode = TYPE_MODE (enttype);
14915
14916           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14917             return false;
14918           if (int_size_in_bytes (type) != size)
14919             return false;
14920           if (size > TREE_STRING_LENGTH (init))
14921             {
14922               memcpy (array, TREE_STRING_POINTER (init),
14923                       TREE_STRING_LENGTH (init));
14924               memset (array + TREE_STRING_LENGTH (init),
14925                       '\0', size - TREE_STRING_LENGTH (init));
14926             }
14927           else
14928             memcpy (array, TREE_STRING_POINTER (init), size);
14929           return true;
14930         }
14931       return false;
14932     case CONSTRUCTOR:
14933       type = TREE_TYPE (init);
14934       if (int_size_in_bytes (type) != size)
14935         return false;
14936       if (TREE_CODE (type) == ARRAY_TYPE)
14937         {
14938           HOST_WIDE_INT min_index;
14939           unsigned HOST_WIDE_INT cnt;
14940           int curpos = 0, fieldsize;
14941           constructor_elt *ce;
14942
14943           if (TYPE_DOMAIN (type) == NULL_TREE
14944               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14945             return false;
14946
14947           fieldsize = int_size_in_bytes (TREE_TYPE (type));
14948           if (fieldsize <= 0)
14949             return false;
14950
14951           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14952           memset (array, '\0', size);
14953           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
14954             {
14955               tree val = ce->value;
14956               tree index = ce->index;
14957               int pos = curpos;
14958               if (index && TREE_CODE (index) == RANGE_EXPR)
14959                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14960                       * fieldsize;
14961               else if (index)
14962                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14963
14964               if (val)
14965                 {
14966                   STRIP_NOPS (val);
14967                   if (!native_encode_initializer (val, array + pos, fieldsize))
14968                     return false;
14969                 }
14970               curpos = pos + fieldsize;
14971               if (index && TREE_CODE (index) == RANGE_EXPR)
14972                 {
14973                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14974                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14975                   while (count-- > 0)
14976                     {
14977                       if (val)
14978                         memcpy (array + curpos, array + pos, fieldsize);
14979                       curpos += fieldsize;
14980                     }
14981                 }
14982               gcc_assert (curpos <= size);
14983             }
14984           return true;
14985         }
14986       else if (TREE_CODE (type) == RECORD_TYPE
14987                || TREE_CODE (type) == UNION_TYPE)
14988         {
14989           tree field = NULL_TREE;
14990           unsigned HOST_WIDE_INT cnt;
14991           constructor_elt *ce;
14992
14993           if (int_size_in_bytes (type) != size)
14994             return false;
14995
14996           if (TREE_CODE (type) == RECORD_TYPE)
14997             field = TYPE_FIELDS (type);
14998
14999           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15000             {
15001               tree val = ce->value;
15002               int pos, fieldsize;
15003
15004               if (ce->index != 0)
15005                 field = ce->index;
15006
15007               if (val)
15008                 STRIP_NOPS (val);
15009
15010               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15011                 return false;
15012
15013               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15014                   && TYPE_DOMAIN (TREE_TYPE (field))
15015                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15016                 return false;
15017               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15018                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15019                 return false;
15020               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15021               pos = int_byte_position (field);
15022               gcc_assert (pos + fieldsize <= size);
15023               if (val
15024                   && !native_encode_initializer (val, array + pos, fieldsize))
15025                 return false;
15026             }
15027           return true;
15028         }
15029       return false;
15030     case VIEW_CONVERT_EXPR:
15031     case NON_LVALUE_EXPR:
15032       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15033     default:
15034       return native_encode_expr (init, array, size) == size;
15035     }
15036 }
15037
15038 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15039    attribute is the const value T.  */
15040
15041 static bool
15042 tree_add_const_value_attribute (dw_die_ref die, tree t)
15043 {
15044   tree init;
15045   tree type = TREE_TYPE (t);
15046   rtx rtl;
15047
15048   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15049     return false;
15050
15051   init = t;
15052   gcc_assert (!DECL_P (init));
15053
15054   rtl = rtl_for_decl_init (init, type);
15055   if (rtl)
15056     return add_const_value_attribute (die, rtl);
15057   /* If the host and target are sane, try harder.  */
15058   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15059            && initializer_constant_valid_p (init, type))
15060     {
15061       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15062       if (size > 0 && (int) size == size)
15063         {
15064           unsigned char *array = (unsigned char *)
15065             ggc_alloc_cleared_atomic (size);
15066
15067           if (native_encode_initializer (init, array, size))
15068             {
15069               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15070               return true;
15071             }
15072         }
15073     }
15074   return false;
15075 }
15076
15077 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15078    attribute is the const value of T, where T is an integral constant
15079    variable with static storage duration
15080    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15081
15082 static bool
15083 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15084 {
15085
15086   if (!decl
15087       || (TREE_CODE (decl) != VAR_DECL
15088           && TREE_CODE (decl) != CONST_DECL)
15089       || (TREE_CODE (decl) == VAR_DECL
15090           && !TREE_STATIC (decl)))
15091     return false;
15092
15093     if (TREE_READONLY (decl)
15094         && ! TREE_THIS_VOLATILE (decl)
15095         && DECL_INITIAL (decl))
15096       /* OK */;
15097     else
15098       return false;
15099
15100   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15101   if (get_AT (var_die, DW_AT_const_value))
15102     return false;
15103
15104   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15105 }
15106
15107 /* Convert the CFI instructions for the current function into a
15108    location list.  This is used for DW_AT_frame_base when we targeting
15109    a dwarf2 consumer that does not support the dwarf3
15110    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15111    expressions.  */
15112
15113 static dw_loc_list_ref
15114 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15115 {
15116   int ix;
15117   dw_fde_ref fde;
15118   dw_loc_list_ref list, *list_tail;
15119   dw_cfi_ref cfi;
15120   dw_cfa_location last_cfa, next_cfa;
15121   const char *start_label, *last_label, *section;
15122   dw_cfa_location remember;
15123
15124   fde = cfun->fde;
15125   gcc_assert (fde != NULL);
15126
15127   section = secname_for_decl (current_function_decl);
15128   list_tail = &list;
15129   list = NULL;
15130
15131   memset (&next_cfa, 0, sizeof (next_cfa));
15132   next_cfa.reg = INVALID_REGNUM;
15133   remember = next_cfa;
15134
15135   start_label = fde->dw_fde_begin;
15136
15137   /* ??? Bald assumption that the CIE opcode list does not contain
15138      advance opcodes.  */
15139   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15140     lookup_cfa_1 (cfi, &next_cfa, &remember);
15141
15142   last_cfa = next_cfa;
15143   last_label = start_label;
15144
15145   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15146     {
15147       /* If the first partition contained no CFI adjustments, the
15148          CIE opcodes apply to the whole first partition.  */
15149       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15150                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15151       list_tail =&(*list_tail)->dw_loc_next;
15152       start_label = last_label = fde->dw_fde_second_begin;
15153     }
15154
15155   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15156     {
15157       switch (cfi->dw_cfi_opc)
15158         {
15159         case DW_CFA_set_loc:
15160         case DW_CFA_advance_loc1:
15161         case DW_CFA_advance_loc2:
15162         case DW_CFA_advance_loc4:
15163           if (!cfa_equal_p (&last_cfa, &next_cfa))
15164             {
15165               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15166                                          start_label, last_label, section);
15167
15168               list_tail = &(*list_tail)->dw_loc_next;
15169               last_cfa = next_cfa;
15170               start_label = last_label;
15171             }
15172           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15173           break;
15174
15175         case DW_CFA_advance_loc:
15176           /* The encoding is complex enough that we should never emit this.  */
15177           gcc_unreachable ();
15178
15179         default:
15180           lookup_cfa_1 (cfi, &next_cfa, &remember);
15181           break;
15182         }
15183       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15184         {
15185           if (!cfa_equal_p (&last_cfa, &next_cfa))
15186             {
15187               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15188                                          start_label, last_label, section);
15189
15190               list_tail = &(*list_tail)->dw_loc_next;
15191               last_cfa = next_cfa;
15192               start_label = last_label;
15193             }
15194           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15195                                      start_label, fde->dw_fde_end, section);
15196           list_tail = &(*list_tail)->dw_loc_next;
15197           start_label = last_label = fde->dw_fde_second_begin;
15198         }
15199     }
15200
15201   if (!cfa_equal_p (&last_cfa, &next_cfa))
15202     {
15203       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15204                                  start_label, last_label, section);
15205       list_tail = &(*list_tail)->dw_loc_next;
15206       start_label = last_label;
15207     }
15208
15209   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15210                              start_label,
15211                              fde->dw_fde_second_begin
15212                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15213                              section);
15214
15215   if (list && list->dw_loc_next)
15216     gen_llsym (list);
15217
15218   return list;
15219 }
15220
15221 /* Compute a displacement from the "steady-state frame pointer" to the
15222    frame base (often the same as the CFA), and store it in
15223    frame_pointer_fb_offset.  OFFSET is added to the displacement
15224    before the latter is negated.  */
15225
15226 static void
15227 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15228 {
15229   rtx reg, elim;
15230
15231 #ifdef FRAME_POINTER_CFA_OFFSET
15232   reg = frame_pointer_rtx;
15233   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15234 #else
15235   reg = arg_pointer_rtx;
15236   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15237 #endif
15238
15239   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15240   if (GET_CODE (elim) == PLUS)
15241     {
15242       offset += INTVAL (XEXP (elim, 1));
15243       elim = XEXP (elim, 0);
15244     }
15245
15246   frame_pointer_fb_offset = -offset;
15247
15248   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15249      in which to eliminate.  This is because it's stack pointer isn't 
15250      directly accessible as a register within the ISA.  To work around
15251      this, assume that while we cannot provide a proper value for
15252      frame_pointer_fb_offset, we won't need one either.  */
15253   frame_pointer_fb_offset_valid
15254     = ((SUPPORTS_STACK_ALIGNMENT
15255         && (elim == hard_frame_pointer_rtx
15256             || elim == stack_pointer_rtx))
15257        || elim == (frame_pointer_needed
15258                    ? hard_frame_pointer_rtx
15259                    : stack_pointer_rtx));
15260 }
15261
15262 /* Generate a DW_AT_name attribute given some string value to be included as
15263    the value of the attribute.  */
15264
15265 static void
15266 add_name_attribute (dw_die_ref die, const char *name_string)
15267 {
15268   if (name_string != NULL && *name_string != 0)
15269     {
15270       if (demangle_name_func)
15271         name_string = (*demangle_name_func) (name_string);
15272
15273       add_AT_string (die, DW_AT_name, name_string);
15274     }
15275 }
15276
15277 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15278    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15279    of TYPE accordingly.
15280
15281    ??? This is a temporary measure until after we're able to generate
15282    regular DWARF for the complex Ada type system.  */
15283
15284 static void 
15285 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15286                                      dw_die_ref context_die)
15287 {
15288   tree dtype;
15289   dw_die_ref dtype_die;
15290
15291   if (!lang_hooks.types.descriptive_type)
15292     return;
15293
15294   dtype = lang_hooks.types.descriptive_type (type);
15295   if (!dtype)
15296     return;
15297
15298   dtype_die = lookup_type_die (dtype);
15299   if (!dtype_die)
15300     {
15301       gen_type_die (dtype, context_die);
15302       dtype_die = lookup_type_die (dtype);
15303       gcc_assert (dtype_die);
15304     }
15305
15306   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15307 }
15308
15309 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15310
15311 static void
15312 add_comp_dir_attribute (dw_die_ref die)
15313 {
15314   const char *wd = get_src_pwd ();
15315   char *wd1;
15316
15317   if (wd == NULL)
15318     return;
15319
15320   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15321     {
15322       int wdlen;
15323
15324       wdlen = strlen (wd);
15325       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15326       strcpy (wd1, wd);
15327       wd1 [wdlen] = DIR_SEPARATOR;
15328       wd1 [wdlen + 1] = 0;
15329       wd = wd1;
15330     }
15331
15332     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15333 }
15334
15335 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15336    default.  */
15337
15338 static int
15339 lower_bound_default (void)
15340 {
15341   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15342     {
15343     case DW_LANG_C:
15344     case DW_LANG_C89:
15345     case DW_LANG_C99:
15346     case DW_LANG_C_plus_plus:
15347     case DW_LANG_ObjC:
15348     case DW_LANG_ObjC_plus_plus:
15349     case DW_LANG_Java:
15350       return 0;
15351     case DW_LANG_Fortran77:
15352     case DW_LANG_Fortran90:
15353     case DW_LANG_Fortran95:
15354       return 1;
15355     case DW_LANG_UPC:
15356     case DW_LANG_D:
15357     case DW_LANG_Python:
15358       return dwarf_version >= 4 ? 0 : -1;
15359     case DW_LANG_Ada95:
15360     case DW_LANG_Ada83:
15361     case DW_LANG_Cobol74:
15362     case DW_LANG_Cobol85:
15363     case DW_LANG_Pascal83:
15364     case DW_LANG_Modula2:
15365     case DW_LANG_PLI:
15366       return dwarf_version >= 4 ? 1 : -1;
15367     default:
15368       return -1;
15369     }
15370 }
15371
15372 /* Given a tree node describing an array bound (either lower or upper) output
15373    a representation for that bound.  */
15374
15375 static void
15376 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15377 {
15378   switch (TREE_CODE (bound))
15379     {
15380     case ERROR_MARK:
15381       return;
15382
15383     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15384     case INTEGER_CST:
15385       {
15386         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15387         int dflt;
15388
15389         /* Use the default if possible.  */
15390         if (bound_attr == DW_AT_lower_bound
15391             && host_integerp (bound, 0)
15392             && (dflt = lower_bound_default ()) != -1
15393             && tree_low_cst (bound, 0) == dflt)
15394           ;
15395
15396         /* Otherwise represent the bound as an unsigned value with the
15397            precision of its type.  The precision and signedness of the
15398            type will be necessary to re-interpret it unambiguously.  */
15399         else if (prec < HOST_BITS_PER_WIDE_INT)
15400           {
15401             unsigned HOST_WIDE_INT mask
15402               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15403             add_AT_unsigned (subrange_die, bound_attr,
15404                              TREE_INT_CST_LOW (bound) & mask);
15405           }
15406         else if (prec == HOST_BITS_PER_WIDE_INT
15407                  || TREE_INT_CST_HIGH (bound) == 0)
15408           add_AT_unsigned (subrange_die, bound_attr,
15409                            TREE_INT_CST_LOW (bound));
15410         else
15411           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15412                          TREE_INT_CST_LOW (bound));
15413       }
15414       break;
15415
15416     CASE_CONVERT:
15417     case VIEW_CONVERT_EXPR:
15418       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15419       break;
15420
15421     case SAVE_EXPR:
15422       break;
15423
15424     case VAR_DECL:
15425     case PARM_DECL:
15426     case RESULT_DECL:
15427       {
15428         dw_die_ref decl_die = lookup_decl_die (bound);
15429
15430         /* ??? Can this happen, or should the variable have been bound
15431            first?  Probably it can, since I imagine that we try to create
15432            the types of parameters in the order in which they exist in
15433            the list, and won't have created a forward reference to a
15434            later parameter.  */
15435         if (decl_die != NULL)
15436           {
15437             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15438             break;
15439           }
15440       }
15441       /* FALLTHRU */
15442
15443     default:
15444       {
15445         /* Otherwise try to create a stack operation procedure to
15446            evaluate the value of the array bound.  */
15447
15448         dw_die_ref ctx, decl_die;
15449         dw_loc_list_ref list;
15450
15451         list = loc_list_from_tree (bound, 2);
15452         if (list == NULL || single_element_loc_list_p (list))
15453           {
15454             /* If DW_AT_*bound is not a reference nor constant, it is
15455                a DWARF expression rather than location description.
15456                For that loc_list_from_tree (bound, 0) is needed.
15457                If that fails to give a single element list,
15458                fall back to outputting this as a reference anyway.  */
15459             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15460             if (list2 && single_element_loc_list_p (list2))
15461               {
15462                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15463                 break;
15464               }
15465           }
15466         if (list == NULL)
15467           break;
15468
15469         if (current_function_decl == 0)
15470           ctx = comp_unit_die ();
15471         else
15472           ctx = lookup_decl_die (current_function_decl);
15473
15474         decl_die = new_die (DW_TAG_variable, ctx, bound);
15475         add_AT_flag (decl_die, DW_AT_artificial, 1);
15476         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15477         add_AT_location_description (decl_die, DW_AT_location, list);
15478         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15479         break;
15480       }
15481     }
15482 }
15483
15484 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
15485    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
15486    Note that the block of subscript information for an array type also
15487    includes information about the element type of the given array type.  */
15488
15489 static void
15490 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
15491 {
15492   unsigned dimension_number;
15493   tree lower, upper;
15494   dw_die_ref subrange_die;
15495
15496   for (dimension_number = 0;
15497        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
15498        type = TREE_TYPE (type), dimension_number++)
15499     {
15500       tree domain = TYPE_DOMAIN (type);
15501
15502       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
15503         break;
15504
15505       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
15506          and (in GNU C only) variable bounds.  Handle all three forms
15507          here.  */
15508       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
15509       if (domain)
15510         {
15511           /* We have an array type with specified bounds.  */
15512           lower = TYPE_MIN_VALUE (domain);
15513           upper = TYPE_MAX_VALUE (domain);
15514
15515           /* Define the index type.  */
15516           if (TREE_TYPE (domain))
15517             {
15518               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
15519                  TREE_TYPE field.  We can't emit debug info for this
15520                  because it is an unnamed integral type.  */
15521               if (TREE_CODE (domain) == INTEGER_TYPE
15522                   && TYPE_NAME (domain) == NULL_TREE
15523                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
15524                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
15525                 ;
15526               else
15527                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
15528                                     type_die);
15529             }
15530
15531           /* ??? If upper is NULL, the array has unspecified length,
15532              but it does have a lower bound.  This happens with Fortran
15533                dimension arr(N:*)
15534              Since the debugger is definitely going to need to know N
15535              to produce useful results, go ahead and output the lower
15536              bound solo, and hope the debugger can cope.  */
15537
15538           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
15539           if (upper)
15540             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
15541         }
15542
15543       /* Otherwise we have an array type with an unspecified length.  The
15544          DWARF-2 spec does not say how to handle this; let's just leave out the
15545          bounds.  */
15546     }
15547 }
15548
15549 static void
15550 add_byte_size_attribute (dw_die_ref die, tree tree_node)
15551 {
15552   unsigned size;
15553
15554   switch (TREE_CODE (tree_node))
15555     {
15556     case ERROR_MARK:
15557       size = 0;
15558       break;
15559     case ENUMERAL_TYPE:
15560     case RECORD_TYPE:
15561     case UNION_TYPE:
15562     case QUAL_UNION_TYPE:
15563       size = int_size_in_bytes (tree_node);
15564       break;
15565     case FIELD_DECL:
15566       /* For a data member of a struct or union, the DW_AT_byte_size is
15567          generally given as the number of bytes normally allocated for an
15568          object of the *declared* type of the member itself.  This is true
15569          even for bit-fields.  */
15570       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
15571       break;
15572     default:
15573       gcc_unreachable ();
15574     }
15575
15576   /* Note that `size' might be -1 when we get to this point.  If it is, that
15577      indicates that the byte size of the entity in question is variable.  We
15578      have no good way of expressing this fact in Dwarf at the present time,
15579      so just let the -1 pass on through.  */
15580   add_AT_unsigned (die, DW_AT_byte_size, size);
15581 }
15582
15583 /* For a FIELD_DECL node which represents a bit-field, output an attribute
15584    which specifies the distance in bits from the highest order bit of the
15585    "containing object" for the bit-field to the highest order bit of the
15586    bit-field itself.
15587
15588    For any given bit-field, the "containing object" is a hypothetical object
15589    (of some integral or enum type) within which the given bit-field lives.  The
15590    type of this hypothetical "containing object" is always the same as the
15591    declared type of the individual bit-field itself.  The determination of the
15592    exact location of the "containing object" for a bit-field is rather
15593    complicated.  It's handled by the `field_byte_offset' function (above).
15594
15595    Note that it is the size (in bytes) of the hypothetical "containing object"
15596    which will be given in the DW_AT_byte_size attribute for this bit-field.
15597    (See `byte_size_attribute' above).  */
15598
15599 static inline void
15600 add_bit_offset_attribute (dw_die_ref die, tree decl)
15601 {
15602   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
15603   tree type = DECL_BIT_FIELD_TYPE (decl);
15604   HOST_WIDE_INT bitpos_int;
15605   HOST_WIDE_INT highest_order_object_bit_offset;
15606   HOST_WIDE_INT highest_order_field_bit_offset;
15607   HOST_WIDE_INT bit_offset;
15608
15609   /* Must be a field and a bit field.  */
15610   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
15611
15612   /* We can't yet handle bit-fields whose offsets are variable, so if we
15613      encounter such things, just return without generating any attribute
15614      whatsoever.  Likewise for variable or too large size.  */
15615   if (! host_integerp (bit_position (decl), 0)
15616       || ! host_integerp (DECL_SIZE (decl), 1))
15617     return;
15618
15619   bitpos_int = int_bit_position (decl);
15620
15621   /* Note that the bit offset is always the distance (in bits) from the
15622      highest-order bit of the "containing object" to the highest-order bit of
15623      the bit-field itself.  Since the "high-order end" of any object or field
15624      is different on big-endian and little-endian machines, the computation
15625      below must take account of these differences.  */
15626   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
15627   highest_order_field_bit_offset = bitpos_int;
15628
15629   if (! BYTES_BIG_ENDIAN)
15630     {
15631       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
15632       highest_order_object_bit_offset += simple_type_size_in_bits (type);
15633     }
15634
15635   bit_offset
15636     = (! BYTES_BIG_ENDIAN
15637        ? highest_order_object_bit_offset - highest_order_field_bit_offset
15638        : highest_order_field_bit_offset - highest_order_object_bit_offset);
15639
15640   if (bit_offset < 0)
15641     add_AT_int (die, DW_AT_bit_offset, bit_offset);
15642   else
15643     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
15644 }
15645
15646 /* For a FIELD_DECL node which represents a bit field, output an attribute
15647    which specifies the length in bits of the given field.  */
15648
15649 static inline void
15650 add_bit_size_attribute (dw_die_ref die, tree decl)
15651 {
15652   /* Must be a field and a bit field.  */
15653   gcc_assert (TREE_CODE (decl) == FIELD_DECL
15654               && DECL_BIT_FIELD_TYPE (decl));
15655
15656   if (host_integerp (DECL_SIZE (decl), 1))
15657     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
15658 }
15659
15660 /* If the compiled language is ANSI C, then add a 'prototyped'
15661    attribute, if arg types are given for the parameters of a function.  */
15662
15663 static inline void
15664 add_prototyped_attribute (dw_die_ref die, tree func_type)
15665 {
15666   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
15667       && prototype_p (func_type))
15668     add_AT_flag (die, DW_AT_prototyped, 1);
15669 }
15670
15671 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
15672    by looking in either the type declaration or object declaration
15673    equate table.  */
15674
15675 static inline dw_die_ref
15676 add_abstract_origin_attribute (dw_die_ref die, tree origin)
15677 {
15678   dw_die_ref origin_die = NULL;
15679
15680   if (TREE_CODE (origin) != FUNCTION_DECL)
15681     {
15682       /* We may have gotten separated from the block for the inlined
15683          function, if we're in an exception handler or some such; make
15684          sure that the abstract function has been written out.
15685
15686          Doing this for nested functions is wrong, however; functions are
15687          distinct units, and our context might not even be inline.  */
15688       tree fn = origin;
15689
15690       if (TYPE_P (fn))
15691         fn = TYPE_STUB_DECL (fn);
15692
15693       fn = decl_function_context (fn);
15694       if (fn)
15695         dwarf2out_abstract_function (fn);
15696     }
15697
15698   if (DECL_P (origin))
15699     origin_die = lookup_decl_die (origin);
15700   else if (TYPE_P (origin))
15701     origin_die = lookup_type_die (origin);
15702
15703   /* XXX: Functions that are never lowered don't always have correct block
15704      trees (in the case of java, they simply have no block tree, in some other
15705      languages).  For these functions, there is nothing we can really do to
15706      output correct debug info for inlined functions in all cases.  Rather
15707      than die, we'll just produce deficient debug info now, in that we will
15708      have variables without a proper abstract origin.  In the future, when all
15709      functions are lowered, we should re-add a gcc_assert (origin_die)
15710      here.  */
15711
15712   if (origin_die)
15713     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
15714   return origin_die;
15715 }
15716
15717 /* We do not currently support the pure_virtual attribute.  */
15718
15719 static inline void
15720 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
15721 {
15722   if (DECL_VINDEX (func_decl))
15723     {
15724       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
15725
15726       if (host_integerp (DECL_VINDEX (func_decl), 0))
15727         add_AT_loc (die, DW_AT_vtable_elem_location,
15728                     new_loc_descr (DW_OP_constu,
15729                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
15730                                    0));
15731
15732       /* GNU extension: Record what type this method came from originally.  */
15733       if (debug_info_level > DINFO_LEVEL_TERSE
15734           && DECL_CONTEXT (func_decl))
15735         add_AT_die_ref (die, DW_AT_containing_type,
15736                         lookup_type_die (DECL_CONTEXT (func_decl)));
15737     }
15738 }
15739 \f
15740 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
15741    given decl.  This used to be a vendor extension until after DWARF 4
15742    standardized it.  */
15743
15744 static void
15745 add_linkage_attr (dw_die_ref die, tree decl)
15746 {
15747   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
15748
15749   /* Mimic what assemble_name_raw does with a leading '*'.  */
15750   if (name[0] == '*')
15751     name = &name[1];
15752
15753   if (dwarf_version >= 4)
15754     add_AT_string (die, DW_AT_linkage_name, name);
15755   else
15756     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
15757 }
15758
15759 /* Add source coordinate attributes for the given decl.  */
15760
15761 static void
15762 add_src_coords_attributes (dw_die_ref die, tree decl)
15763 {
15764   expanded_location s;
15765
15766   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
15767     return;
15768   s = expand_location (DECL_SOURCE_LOCATION (decl));
15769   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
15770   add_AT_unsigned (die, DW_AT_decl_line, s.line);
15771 }
15772
15773 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
15774
15775 static void
15776 add_linkage_name (dw_die_ref die, tree decl)
15777 {
15778   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
15779        && TREE_PUBLIC (decl)
15780        && !DECL_ABSTRACT (decl)
15781        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
15782        && die->die_tag != DW_TAG_member)
15783     {
15784       /* Defer until we have an assembler name set.  */
15785       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
15786         {
15787           limbo_die_node *asm_name;
15788
15789           asm_name = ggc_alloc_cleared_limbo_die_node ();
15790           asm_name->die = die;
15791           asm_name->created_for = decl;
15792           asm_name->next = deferred_asm_name;
15793           deferred_asm_name = asm_name;
15794         }
15795       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
15796         add_linkage_attr (die, decl);
15797     }
15798 }
15799
15800 /* Add a DW_AT_name attribute and source coordinate attribute for the
15801    given decl, but only if it actually has a name.  */
15802
15803 static void
15804 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
15805 {
15806   tree decl_name;
15807
15808   decl_name = DECL_NAME (decl);
15809   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
15810     {
15811       const char *name = dwarf2_name (decl, 0);
15812       if (name)
15813         add_name_attribute (die, name);
15814       if (! DECL_ARTIFICIAL (decl))
15815         add_src_coords_attributes (die, decl);
15816
15817       add_linkage_name (die, decl);
15818     }
15819
15820 #ifdef VMS_DEBUGGING_INFO
15821   /* Get the function's name, as described by its RTL.  This may be different
15822      from the DECL_NAME name used in the source file.  */
15823   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
15824     {
15825       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
15826                    XEXP (DECL_RTL (decl), 0));
15827       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
15828     }
15829 #endif /* VMS_DEBUGGING_INFO */
15830 }
15831
15832 #ifdef VMS_DEBUGGING_INFO
15833 /* Output the debug main pointer die for VMS */
15834
15835 void
15836 dwarf2out_vms_debug_main_pointer (void)
15837 {
15838   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15839   dw_die_ref die;
15840
15841   /* Allocate the VMS debug main subprogram die.  */
15842   die = ggc_alloc_cleared_die_node ();
15843   die->die_tag = DW_TAG_subprogram;
15844   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
15845   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
15846                                current_function_funcdef_no);
15847   add_AT_lbl_id (die, DW_AT_entry_pc, label);
15848
15849   /* Make it the first child of comp_unit_die ().  */
15850   die->die_parent = comp_unit_die ();
15851   if (comp_unit_die ()->die_child)
15852     {
15853       die->die_sib = comp_unit_die ()->die_child->die_sib;
15854       comp_unit_die ()->die_child->die_sib = die;
15855     }
15856   else
15857     {
15858       die->die_sib = die;
15859       comp_unit_die ()->die_child = die;
15860     }
15861 }
15862 #endif /* VMS_DEBUGGING_INFO */
15863
15864 /* Push a new declaration scope.  */
15865
15866 static void
15867 push_decl_scope (tree scope)
15868 {
15869   VEC_safe_push (tree, gc, decl_scope_table, scope);
15870 }
15871
15872 /* Pop a declaration scope.  */
15873
15874 static inline void
15875 pop_decl_scope (void)
15876 {
15877   VEC_pop (tree, decl_scope_table);
15878 }
15879
15880 /* Return the DIE for the scope that immediately contains this type.
15881    Non-named types get global scope.  Named types nested in other
15882    types get their containing scope if it's open, or global scope
15883    otherwise.  All other types (i.e. function-local named types) get
15884    the current active scope.  */
15885
15886 static dw_die_ref
15887 scope_die_for (tree t, dw_die_ref context_die)
15888 {
15889   dw_die_ref scope_die = NULL;
15890   tree containing_scope;
15891   int i;
15892
15893   /* Non-types always go in the current scope.  */
15894   gcc_assert (TYPE_P (t));
15895
15896   containing_scope = TYPE_CONTEXT (t);
15897
15898   /* Use the containing namespace if it was passed in (for a declaration).  */
15899   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
15900     {
15901       if (context_die == lookup_decl_die (containing_scope))
15902         /* OK */;
15903       else
15904         containing_scope = NULL_TREE;
15905     }
15906
15907   /* Ignore function type "scopes" from the C frontend.  They mean that
15908      a tagged type is local to a parmlist of a function declarator, but
15909      that isn't useful to DWARF.  */
15910   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
15911     containing_scope = NULL_TREE;
15912
15913   if (SCOPE_FILE_SCOPE_P (containing_scope))
15914     scope_die = comp_unit_die ();
15915   else if (TYPE_P (containing_scope))
15916     {
15917       /* For types, we can just look up the appropriate DIE.  But
15918          first we check to see if we're in the middle of emitting it
15919          so we know where the new DIE should go.  */
15920       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
15921         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
15922           break;
15923
15924       if (i < 0)
15925         {
15926           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
15927                       || TREE_ASM_WRITTEN (containing_scope));
15928           /*We are not in the middle of emitting the type
15929             CONTAINING_SCOPE. Let's see if it's emitted already.  */
15930           scope_die = lookup_type_die (containing_scope);
15931
15932           /* If none of the current dies are suitable, we get file scope.  */
15933           if (scope_die == NULL)
15934             scope_die = comp_unit_die ();
15935         }
15936       else
15937         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
15938     }
15939   else
15940     scope_die = context_die;
15941
15942   return scope_die;
15943 }
15944
15945 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
15946
15947 static inline int
15948 local_scope_p (dw_die_ref context_die)
15949 {
15950   for (; context_die; context_die = context_die->die_parent)
15951     if (context_die->die_tag == DW_TAG_inlined_subroutine
15952         || context_die->die_tag == DW_TAG_subprogram)
15953       return 1;
15954
15955   return 0;
15956 }
15957
15958 /* Returns nonzero if CONTEXT_DIE is a class.  */
15959
15960 static inline int
15961 class_scope_p (dw_die_ref context_die)
15962 {
15963   return (context_die
15964           && (context_die->die_tag == DW_TAG_structure_type
15965               || context_die->die_tag == DW_TAG_class_type
15966               || context_die->die_tag == DW_TAG_interface_type
15967               || context_die->die_tag == DW_TAG_union_type));
15968 }
15969
15970 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
15971    whether or not to treat a DIE in this context as a declaration.  */
15972
15973 static inline int
15974 class_or_namespace_scope_p (dw_die_ref context_die)
15975 {
15976   return (class_scope_p (context_die)
15977           || (context_die && context_die->die_tag == DW_TAG_namespace));
15978 }
15979
15980 /* Many forms of DIEs require a "type description" attribute.  This
15981    routine locates the proper "type descriptor" die for the type given
15982    by 'type', and adds a DW_AT_type attribute below the given die.  */
15983
15984 static void
15985 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
15986                     int decl_volatile, dw_die_ref context_die)
15987 {
15988   enum tree_code code  = TREE_CODE (type);
15989   dw_die_ref type_die  = NULL;
15990
15991   /* ??? If this type is an unnamed subrange type of an integral, floating-point
15992      or fixed-point type, use the inner type.  This is because we have no
15993      support for unnamed types in base_type_die.  This can happen if this is
15994      an Ada subrange type.  Correct solution is emit a subrange type die.  */
15995   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
15996       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
15997     type = TREE_TYPE (type), code = TREE_CODE (type);
15998
15999   if (code == ERROR_MARK
16000       /* Handle a special case.  For functions whose return type is void, we
16001          generate *no* type attribute.  (Note that no object may have type
16002          `void', so this only applies to function return types).  */
16003       || code == VOID_TYPE)
16004     return;
16005
16006   type_die = modified_type_die (type,
16007                                 decl_const || TYPE_READONLY (type),
16008                                 decl_volatile || TYPE_VOLATILE (type),
16009                                 context_die);
16010
16011   if (type_die != NULL)
16012     add_AT_die_ref (object_die, DW_AT_type, type_die);
16013 }
16014
16015 /* Given an object die, add the calling convention attribute for the
16016    function call type.  */
16017 static void
16018 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16019 {
16020   enum dwarf_calling_convention value = DW_CC_normal;
16021
16022   value = ((enum dwarf_calling_convention)
16023            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16024
16025   if (is_fortran ()
16026       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16027     {
16028       /* DWARF 2 doesn't provide a way to identify a program's source-level
16029         entry point.  DW_AT_calling_convention attributes are only meant
16030         to describe functions' calling conventions.  However, lacking a
16031         better way to signal the Fortran main program, we used this for 
16032         a long time, following existing custom.  Now, DWARF 4 has 
16033         DW_AT_main_subprogram, which we add below, but some tools still
16034         rely on the old way, which we thus keep.  */
16035       value = DW_CC_program;
16036
16037       if (dwarf_version >= 4 || !dwarf_strict)
16038         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16039     }
16040
16041   /* Only add the attribute if the backend requests it, and
16042      is not DW_CC_normal.  */
16043   if (value && (value != DW_CC_normal))
16044     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16045 }
16046
16047 /* Given a tree pointer to a struct, class, union, or enum type node, return
16048    a pointer to the (string) tag name for the given type, or zero if the type
16049    was declared without a tag.  */
16050
16051 static const char *
16052 type_tag (const_tree type)
16053 {
16054   const char *name = 0;
16055
16056   if (TYPE_NAME (type) != 0)
16057     {
16058       tree t = 0;
16059
16060       /* Find the IDENTIFIER_NODE for the type name.  */
16061       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16062           && !TYPE_NAMELESS (type))
16063         t = TYPE_NAME (type);
16064
16065       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16066          a TYPE_DECL node, regardless of whether or not a `typedef' was
16067          involved.  */
16068       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16069                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16070         {
16071           /* We want to be extra verbose.  Don't call dwarf_name if
16072              DECL_NAME isn't set.  The default hook for decl_printable_name
16073              doesn't like that, and in this context it's correct to return
16074              0, instead of "<anonymous>" or the like.  */
16075           if (DECL_NAME (TYPE_NAME (type))
16076               && !DECL_NAMELESS (TYPE_NAME (type)))
16077             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16078         }
16079
16080       /* Now get the name as a string, or invent one.  */
16081       if (!name && t != 0)
16082         name = IDENTIFIER_POINTER (t);
16083     }
16084
16085   return (name == 0 || *name == '\0') ? 0 : name;
16086 }
16087
16088 /* Return the type associated with a data member, make a special check
16089    for bit field types.  */
16090
16091 static inline tree
16092 member_declared_type (const_tree member)
16093 {
16094   return (DECL_BIT_FIELD_TYPE (member)
16095           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16096 }
16097
16098 /* Get the decl's label, as described by its RTL. This may be different
16099    from the DECL_NAME name used in the source file.  */
16100
16101 #if 0
16102 static const char *
16103 decl_start_label (tree decl)
16104 {
16105   rtx x;
16106   const char *fnname;
16107
16108   x = DECL_RTL (decl);
16109   gcc_assert (MEM_P (x));
16110
16111   x = XEXP (x, 0);
16112   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16113
16114   fnname = XSTR (x, 0);
16115   return fnname;
16116 }
16117 #endif
16118 \f
16119 /* These routines generate the internal representation of the DIE's for
16120    the compilation unit.  Debugging information is collected by walking
16121    the declaration trees passed in from dwarf2out_decl().  */
16122
16123 static void
16124 gen_array_type_die (tree type, dw_die_ref context_die)
16125 {
16126   dw_die_ref scope_die = scope_die_for (type, context_die);
16127   dw_die_ref array_die;
16128
16129   /* GNU compilers represent multidimensional array types as sequences of one
16130      dimensional array types whose element types are themselves array types.
16131      We sometimes squish that down to a single array_type DIE with multiple
16132      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16133      say that we are allowed to do this kind of compression in C, because
16134      there is no difference between an array of arrays and a multidimensional
16135      array.  We don't do this for Ada to remain as close as possible to the
16136      actual representation, which is especially important against the language
16137      flexibilty wrt arrays of variable size.  */
16138
16139   bool collapse_nested_arrays = !is_ada ();
16140   tree element_type;
16141
16142   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16143      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16144   if (TYPE_STRING_FLAG (type)
16145       && TREE_CODE (type) == ARRAY_TYPE
16146       && is_fortran ()
16147       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16148     {
16149       HOST_WIDE_INT size;
16150
16151       array_die = new_die (DW_TAG_string_type, scope_die, type);
16152       add_name_attribute (array_die, type_tag (type));
16153       equate_type_number_to_die (type, array_die);
16154       size = int_size_in_bytes (type);
16155       if (size >= 0)
16156         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16157       else if (TYPE_DOMAIN (type) != NULL_TREE
16158                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16159                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16160         {
16161           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16162           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16163
16164           size = int_size_in_bytes (TREE_TYPE (szdecl));
16165           if (loc && size > 0)
16166             {
16167               add_AT_location_description (array_die, DW_AT_string_length, loc);
16168               if (size != DWARF2_ADDR_SIZE)
16169                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16170             }
16171         }
16172       return;
16173     }
16174
16175   /* ??? The SGI dwarf reader fails for array of array of enum types
16176      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16177      array type comes before the outer array type.  We thus call gen_type_die
16178      before we new_die and must prevent nested array types collapsing for this
16179      target.  */
16180
16181 #ifdef MIPS_DEBUGGING_INFO
16182   gen_type_die (TREE_TYPE (type), context_die);
16183   collapse_nested_arrays = false;
16184 #endif
16185
16186   array_die = new_die (DW_TAG_array_type, scope_die, type);
16187   add_name_attribute (array_die, type_tag (type));
16188   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16189   if (TYPE_ARTIFICIAL (type))
16190     add_AT_flag (array_die, DW_AT_artificial, 1);
16191   equate_type_number_to_die (type, array_die);
16192
16193   if (TREE_CODE (type) == VECTOR_TYPE)
16194     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16195
16196   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16197   if (is_fortran ()
16198       && TREE_CODE (type) == ARRAY_TYPE
16199       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16200       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16201     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16202
16203 #if 0
16204   /* We default the array ordering.  SDB will probably do
16205      the right things even if DW_AT_ordering is not present.  It's not even
16206      an issue until we start to get into multidimensional arrays anyway.  If
16207      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16208      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16209      and when we find out that we need to put these in, we will only do so
16210      for multidimensional arrays.  */
16211   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16212 #endif
16213
16214 #ifdef MIPS_DEBUGGING_INFO
16215   /* The SGI compilers handle arrays of unknown bound by setting
16216      AT_declaration and not emitting any subrange DIEs.  */
16217   if (TREE_CODE (type) == ARRAY_TYPE
16218       && ! TYPE_DOMAIN (type))
16219     add_AT_flag (array_die, DW_AT_declaration, 1);
16220   else
16221 #endif
16222   if (TREE_CODE (type) == VECTOR_TYPE)
16223     {
16224       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16225       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16226       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16227       add_bound_info (subrange_die, DW_AT_upper_bound,
16228                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16229     }
16230   else
16231     add_subscript_info (array_die, type, collapse_nested_arrays);
16232
16233   /* Add representation of the type of the elements of this array type and
16234      emit the corresponding DIE if we haven't done it already.  */
16235   element_type = TREE_TYPE (type);
16236   if (collapse_nested_arrays)
16237     while (TREE_CODE (element_type) == ARRAY_TYPE)
16238       {
16239         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16240           break;
16241         element_type = TREE_TYPE (element_type);
16242       }
16243
16244 #ifndef MIPS_DEBUGGING_INFO
16245   gen_type_die (element_type, context_die);
16246 #endif
16247
16248   add_type_attribute (array_die, element_type, 0, 0, context_die);
16249
16250   if (get_AT (array_die, DW_AT_name))
16251     add_pubtype (type, array_die);
16252 }
16253
16254 static dw_loc_descr_ref
16255 descr_info_loc (tree val, tree base_decl)
16256 {
16257   HOST_WIDE_INT size;
16258   dw_loc_descr_ref loc, loc2;
16259   enum dwarf_location_atom op;
16260
16261   if (val == base_decl)
16262     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16263
16264   switch (TREE_CODE (val))
16265     {
16266     CASE_CONVERT:
16267       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16268     case VAR_DECL:
16269       return loc_descriptor_from_tree (val, 0);
16270     case INTEGER_CST:
16271       if (host_integerp (val, 0))
16272         return int_loc_descriptor (tree_low_cst (val, 0));
16273       break;
16274     case INDIRECT_REF:
16275       size = int_size_in_bytes (TREE_TYPE (val));
16276       if (size < 0)
16277         break;
16278       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16279       if (!loc)
16280         break;
16281       if (size == DWARF2_ADDR_SIZE)
16282         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16283       else
16284         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16285       return loc;
16286     case POINTER_PLUS_EXPR:
16287     case PLUS_EXPR:
16288       if (host_integerp (TREE_OPERAND (val, 1), 1)
16289           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16290              < 16384)
16291         {
16292           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16293           if (!loc)
16294             break;
16295           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16296         }
16297       else
16298         {
16299           op = DW_OP_plus;
16300         do_binop:
16301           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16302           if (!loc)
16303             break;
16304           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16305           if (!loc2)
16306             break;
16307           add_loc_descr (&loc, loc2);
16308           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16309         }
16310       return loc;
16311     case MINUS_EXPR:
16312       op = DW_OP_minus;
16313       goto do_binop;
16314     case MULT_EXPR:
16315       op = DW_OP_mul;
16316       goto do_binop;
16317     case EQ_EXPR:
16318       op = DW_OP_eq;
16319       goto do_binop;
16320     case NE_EXPR:
16321       op = DW_OP_ne;
16322       goto do_binop;
16323     default:
16324       break;
16325     }
16326   return NULL;
16327 }
16328
16329 static void
16330 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16331                       tree val, tree base_decl)
16332 {
16333   dw_loc_descr_ref loc;
16334
16335   if (host_integerp (val, 0))
16336     {
16337       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16338       return;
16339     }
16340
16341   loc = descr_info_loc (val, base_decl);
16342   if (!loc)
16343     return;
16344
16345   add_AT_loc (die, attr, loc);
16346 }
16347
16348 /* This routine generates DIE for array with hidden descriptor, details
16349    are filled into *info by a langhook.  */
16350
16351 static void
16352 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16353                           dw_die_ref context_die)
16354 {
16355   dw_die_ref scope_die = scope_die_for (type, context_die);
16356   dw_die_ref array_die;
16357   int dim;
16358
16359   array_die = new_die (DW_TAG_array_type, scope_die, type);
16360   add_name_attribute (array_die, type_tag (type));
16361   equate_type_number_to_die (type, array_die);
16362
16363   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16364   if (is_fortran ()
16365       && info->ndimensions >= 2)
16366     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16367
16368   if (info->data_location)
16369     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16370                           info->base_decl);
16371   if (info->associated)
16372     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16373                           info->base_decl);
16374   if (info->allocated)
16375     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16376                           info->base_decl);
16377
16378   for (dim = 0; dim < info->ndimensions; dim++)
16379     {
16380       dw_die_ref subrange_die
16381         = new_die (DW_TAG_subrange_type, array_die, NULL);
16382
16383       if (info->dimen[dim].lower_bound)
16384         {
16385           /* If it is the default value, omit it.  */
16386           int dflt;
16387
16388           if (host_integerp (info->dimen[dim].lower_bound, 0)
16389               && (dflt = lower_bound_default ()) != -1
16390               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16391             ;
16392           else
16393             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16394                                   info->dimen[dim].lower_bound,
16395                                   info->base_decl);
16396         }
16397       if (info->dimen[dim].upper_bound)
16398         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16399                               info->dimen[dim].upper_bound,
16400                               info->base_decl);
16401       if (info->dimen[dim].stride)
16402         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16403                               info->dimen[dim].stride,
16404                               info->base_decl);
16405     }
16406
16407   gen_type_die (info->element_type, context_die);
16408   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16409
16410   if (get_AT (array_die, DW_AT_name))
16411     add_pubtype (type, array_die);
16412 }
16413
16414 #if 0
16415 static void
16416 gen_entry_point_die (tree decl, dw_die_ref context_die)
16417 {
16418   tree origin = decl_ultimate_origin (decl);
16419   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16420
16421   if (origin != NULL)
16422     add_abstract_origin_attribute (decl_die, origin);
16423   else
16424     {
16425       add_name_and_src_coords_attributes (decl_die, decl);
16426       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16427                           0, 0, context_die);
16428     }
16429
16430   if (DECL_ABSTRACT (decl))
16431     equate_decl_number_to_die (decl, decl_die);
16432   else
16433     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16434 }
16435 #endif
16436
16437 /* Walk through the list of incomplete types again, trying once more to
16438    emit full debugging info for them.  */
16439
16440 static void
16441 retry_incomplete_types (void)
16442 {
16443   int i;
16444
16445   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16446     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16447                                   DINFO_USAGE_DIR_USE))
16448       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16449 }
16450
16451 /* Determine what tag to use for a record type.  */
16452
16453 static enum dwarf_tag
16454 record_type_tag (tree type)
16455 {
16456   if (! lang_hooks.types.classify_record)
16457     return DW_TAG_structure_type;
16458
16459   switch (lang_hooks.types.classify_record (type))
16460     {
16461     case RECORD_IS_STRUCT:
16462       return DW_TAG_structure_type;
16463
16464     case RECORD_IS_CLASS:
16465       return DW_TAG_class_type;
16466
16467     case RECORD_IS_INTERFACE:
16468       if (dwarf_version >= 3 || !dwarf_strict)
16469         return DW_TAG_interface_type;
16470       return DW_TAG_structure_type;
16471
16472     default:
16473       gcc_unreachable ();
16474     }
16475 }
16476
16477 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16478    include all of the information about the enumeration values also. Each
16479    enumerated type name/value is listed as a child of the enumerated type
16480    DIE.  */
16481
16482 static dw_die_ref
16483 gen_enumeration_type_die (tree type, dw_die_ref context_die)
16484 {
16485   dw_die_ref type_die = lookup_type_die (type);
16486
16487   if (type_die == NULL)
16488     {
16489       type_die = new_die (DW_TAG_enumeration_type,
16490                           scope_die_for (type, context_die), type);
16491       equate_type_number_to_die (type, type_die);
16492       add_name_attribute (type_die, type_tag (type));
16493       add_gnat_descriptive_type_attribute (type_die, type, context_die);
16494       if (TYPE_ARTIFICIAL (type))
16495         add_AT_flag (type_die, DW_AT_artificial, 1);
16496       if (dwarf_version >= 4 || !dwarf_strict)
16497         {
16498           if (ENUM_IS_SCOPED (type))
16499             add_AT_flag (type_die, DW_AT_enum_class, 1);
16500           if (ENUM_IS_OPAQUE (type))
16501             add_AT_flag (type_die, DW_AT_declaration, 1);
16502         }
16503     }
16504   else if (! TYPE_SIZE (type))
16505     return type_die;
16506   else
16507     remove_AT (type_die, DW_AT_declaration);
16508
16509   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
16510      given enum type is incomplete, do not generate the DW_AT_byte_size
16511      attribute or the DW_AT_element_list attribute.  */
16512   if (TYPE_SIZE (type))
16513     {
16514       tree link;
16515
16516       TREE_ASM_WRITTEN (type) = 1;
16517       add_byte_size_attribute (type_die, type);
16518       if (TYPE_STUB_DECL (type) != NULL_TREE)
16519         {
16520           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16521           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
16522         }
16523
16524       /* If the first reference to this type was as the return type of an
16525          inline function, then it may not have a parent.  Fix this now.  */
16526       if (type_die->die_parent == NULL)
16527         add_child_die (scope_die_for (type, context_die), type_die);
16528
16529       for (link = TYPE_VALUES (type);
16530            link != NULL; link = TREE_CHAIN (link))
16531         {
16532           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
16533           tree value = TREE_VALUE (link);
16534
16535           add_name_attribute (enum_die,
16536                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
16537
16538           if (TREE_CODE (value) == CONST_DECL)
16539             value = DECL_INITIAL (value);
16540
16541           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
16542             /* DWARF2 does not provide a way of indicating whether or
16543                not enumeration constants are signed or unsigned.  GDB
16544                always assumes the values are signed, so we output all
16545                values as if they were signed.  That means that
16546                enumeration constants with very large unsigned values
16547                will appear to have negative values in the debugger.  */
16548             add_AT_int (enum_die, DW_AT_const_value,
16549                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
16550         }
16551     }
16552   else
16553     add_AT_flag (type_die, DW_AT_declaration, 1);
16554
16555   if (get_AT (type_die, DW_AT_name))
16556     add_pubtype (type, type_die);
16557
16558   return type_die;
16559 }
16560
16561 /* Generate a DIE to represent either a real live formal parameter decl or to
16562    represent just the type of some formal parameter position in some function
16563    type.
16564
16565    Note that this routine is a bit unusual because its argument may be a
16566    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
16567    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
16568    node.  If it's the former then this function is being called to output a
16569    DIE to represent a formal parameter object (or some inlining thereof).  If
16570    it's the latter, then this function is only being called to output a
16571    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
16572    argument type of some subprogram type.
16573    If EMIT_NAME_P is true, name and source coordinate attributes
16574    are emitted.  */
16575
16576 static dw_die_ref
16577 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
16578                           dw_die_ref context_die)
16579 {
16580   tree node_or_origin = node ? node : origin;
16581   tree ultimate_origin;
16582   dw_die_ref parm_die
16583     = new_die (DW_TAG_formal_parameter, context_die, node);
16584
16585   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
16586     {
16587     case tcc_declaration:
16588       ultimate_origin = decl_ultimate_origin (node_or_origin);
16589       if (node || ultimate_origin)
16590         origin = ultimate_origin;
16591       if (origin != NULL)
16592         add_abstract_origin_attribute (parm_die, origin);
16593       else if (emit_name_p)
16594         add_name_and_src_coords_attributes (parm_die, node);
16595       if (origin == NULL
16596           || (! DECL_ABSTRACT (node_or_origin)
16597               && variably_modified_type_p (TREE_TYPE (node_or_origin),
16598                                            decl_function_context
16599                                                             (node_or_origin))))
16600         {
16601           tree type = TREE_TYPE (node_or_origin);
16602           if (decl_by_reference_p (node_or_origin))
16603             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
16604                                 context_die);
16605           else
16606             add_type_attribute (parm_die, type,
16607                                 TREE_READONLY (node_or_origin),
16608                                 TREE_THIS_VOLATILE (node_or_origin),
16609                                 context_die);
16610         }
16611       if (origin == NULL && DECL_ARTIFICIAL (node))
16612         add_AT_flag (parm_die, DW_AT_artificial, 1);
16613
16614       if (node && node != origin)
16615         equate_decl_number_to_die (node, parm_die);
16616       if (! DECL_ABSTRACT (node_or_origin))
16617         add_location_or_const_value_attribute (parm_die, node_or_origin,
16618                                                node == NULL, DW_AT_location);
16619
16620       break;
16621
16622     case tcc_type:
16623       /* We were called with some kind of a ..._TYPE node.  */
16624       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
16625       break;
16626
16627     default:
16628       gcc_unreachable ();
16629     }
16630
16631   return parm_die;
16632 }
16633
16634 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
16635    children DW_TAG_formal_parameter DIEs representing the arguments of the
16636    parameter pack.
16637
16638    PARM_PACK must be a function parameter pack.
16639    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
16640    must point to the subsequent arguments of the function PACK_ARG belongs to.
16641    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
16642    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
16643    following the last one for which a DIE was generated.  */
16644
16645 static dw_die_ref
16646 gen_formal_parameter_pack_die  (tree parm_pack,
16647                                 tree pack_arg,
16648                                 dw_die_ref subr_die,
16649                                 tree *next_arg)
16650 {
16651   tree arg;
16652   dw_die_ref parm_pack_die;
16653
16654   gcc_assert (parm_pack
16655               && lang_hooks.function_parameter_pack_p (parm_pack)
16656               && subr_die);
16657
16658   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
16659   add_src_coords_attributes (parm_pack_die, parm_pack);
16660
16661   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
16662     {
16663       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
16664                                                                  parm_pack))
16665         break;
16666       gen_formal_parameter_die (arg, NULL,
16667                                 false /* Don't emit name attribute.  */,
16668                                 parm_pack_die);
16669     }
16670   if (next_arg)
16671     *next_arg = arg;
16672   return parm_pack_die;
16673 }
16674
16675 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
16676    at the end of an (ANSI prototyped) formal parameters list.  */
16677
16678 static void
16679 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
16680 {
16681   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
16682 }
16683
16684 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
16685    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
16686    parameters as specified in some function type specification (except for
16687    those which appear as part of a function *definition*).  */
16688
16689 static void
16690 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
16691 {
16692   tree link;
16693   tree formal_type = NULL;
16694   tree first_parm_type;
16695   tree arg;
16696
16697   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
16698     {
16699       arg = DECL_ARGUMENTS (function_or_method_type);
16700       function_or_method_type = TREE_TYPE (function_or_method_type);
16701     }
16702   else
16703     arg = NULL_TREE;
16704
16705   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
16706
16707   /* Make our first pass over the list of formal parameter types and output a
16708      DW_TAG_formal_parameter DIE for each one.  */
16709   for (link = first_parm_type; link; )
16710     {
16711       dw_die_ref parm_die;
16712
16713       formal_type = TREE_VALUE (link);
16714       if (formal_type == void_type_node)
16715         break;
16716
16717       /* Output a (nameless) DIE to represent the formal parameter itself.  */
16718       parm_die = gen_formal_parameter_die (formal_type, NULL,
16719                                            true /* Emit name attribute.  */,
16720                                            context_die);
16721       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
16722           && link == first_parm_type)
16723         {
16724           add_AT_flag (parm_die, DW_AT_artificial, 1);
16725           if (dwarf_version >= 3 || !dwarf_strict)
16726             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
16727         }
16728       else if (arg && DECL_ARTIFICIAL (arg))
16729         add_AT_flag (parm_die, DW_AT_artificial, 1);
16730
16731       link = TREE_CHAIN (link);
16732       if (arg)
16733         arg = DECL_CHAIN (arg);
16734     }
16735
16736   /* If this function type has an ellipsis, add a
16737      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
16738   if (formal_type != void_type_node)
16739     gen_unspecified_parameters_die (function_or_method_type, context_die);
16740
16741   /* Make our second (and final) pass over the list of formal parameter types
16742      and output DIEs to represent those types (as necessary).  */
16743   for (link = TYPE_ARG_TYPES (function_or_method_type);
16744        link && TREE_VALUE (link);
16745        link = TREE_CHAIN (link))
16746     gen_type_die (TREE_VALUE (link), context_die);
16747 }
16748
16749 /* We want to generate the DIE for TYPE so that we can generate the
16750    die for MEMBER, which has been defined; we will need to refer back
16751    to the member declaration nested within TYPE.  If we're trying to
16752    generate minimal debug info for TYPE, processing TYPE won't do the
16753    trick; we need to attach the member declaration by hand.  */
16754
16755 static void
16756 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
16757 {
16758   gen_type_die (type, context_die);
16759
16760   /* If we're trying to avoid duplicate debug info, we may not have
16761      emitted the member decl for this function.  Emit it now.  */
16762   if (TYPE_STUB_DECL (type)
16763       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
16764       && ! lookup_decl_die (member))
16765     {
16766       dw_die_ref type_die;
16767       gcc_assert (!decl_ultimate_origin (member));
16768
16769       push_decl_scope (type);
16770       type_die = lookup_type_die_strip_naming_typedef (type);
16771       if (TREE_CODE (member) == FUNCTION_DECL)
16772         gen_subprogram_die (member, type_die);
16773       else if (TREE_CODE (member) == FIELD_DECL)
16774         {
16775           /* Ignore the nameless fields that are used to skip bits but handle
16776              C++ anonymous unions and structs.  */
16777           if (DECL_NAME (member) != NULL_TREE
16778               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
16779               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
16780             {
16781               gen_type_die (member_declared_type (member), type_die);
16782               gen_field_die (member, type_die);
16783             }
16784         }
16785       else
16786         gen_variable_die (member, NULL_TREE, type_die);
16787
16788       pop_decl_scope ();
16789     }
16790 }
16791
16792 /* Generate the DWARF2 info for the "abstract" instance of a function which we
16793    may later generate inlined and/or out-of-line instances of.  */
16794
16795 static void
16796 dwarf2out_abstract_function (tree decl)
16797 {
16798   dw_die_ref old_die;
16799   tree save_fn;
16800   tree context;
16801   int was_abstract;
16802   htab_t old_decl_loc_table;
16803   htab_t old_cached_dw_loc_list_table;
16804   int old_call_site_count, old_tail_call_site_count;
16805   struct call_arg_loc_node *old_call_arg_locations;
16806
16807   /* Make sure we have the actual abstract inline, not a clone.  */
16808   decl = DECL_ORIGIN (decl);
16809
16810   old_die = lookup_decl_die (decl);
16811   if (old_die && get_AT (old_die, DW_AT_inline))
16812     /* We've already generated the abstract instance.  */
16813     return;
16814
16815   /* We can be called while recursively when seeing block defining inlined subroutine
16816      DIE.  Be sure to not clobber the outer location table nor use it or we would
16817      get locations in abstract instantces.  */
16818   old_decl_loc_table = decl_loc_table;
16819   decl_loc_table = NULL;
16820   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
16821   cached_dw_loc_list_table = NULL;
16822   old_call_arg_locations = call_arg_locations;
16823   call_arg_locations = NULL;
16824   old_call_site_count = call_site_count;
16825   call_site_count = -1;
16826   old_tail_call_site_count = tail_call_site_count;
16827   tail_call_site_count = -1;
16828
16829   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
16830      we don't get confused by DECL_ABSTRACT.  */
16831   if (debug_info_level > DINFO_LEVEL_TERSE)
16832     {
16833       context = decl_class_context (decl);
16834       if (context)
16835         gen_type_die_for_member
16836           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
16837     }
16838
16839   /* Pretend we've just finished compiling this function.  */
16840   save_fn = current_function_decl;
16841   current_function_decl = decl;
16842   push_cfun (DECL_STRUCT_FUNCTION (decl));
16843
16844   was_abstract = DECL_ABSTRACT (decl);
16845   set_decl_abstract_flags (decl, 1);
16846   dwarf2out_decl (decl);
16847   if (! was_abstract)
16848     set_decl_abstract_flags (decl, 0);
16849
16850   current_function_decl = save_fn;
16851   decl_loc_table = old_decl_loc_table;
16852   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
16853   call_arg_locations = old_call_arg_locations;
16854   call_site_count = old_call_site_count;
16855   tail_call_site_count = old_tail_call_site_count;
16856   pop_cfun ();
16857 }
16858
16859 /* Helper function of premark_used_types() which gets called through
16860    htab_traverse.
16861
16862    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16863    marked as unused by prune_unused_types.  */
16864
16865 static int
16866 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
16867 {
16868   tree type;
16869   dw_die_ref die;
16870
16871   type = (tree) *slot;
16872   die = lookup_type_die (type);
16873   if (die != NULL)
16874     die->die_perennial_p = 1;
16875   return 1;
16876 }
16877
16878 /* Helper function of premark_types_used_by_global_vars which gets called
16879    through htab_traverse.
16880
16881    Marks the DIE of a given type in *SLOT as perennial, so it never gets
16882    marked as unused by prune_unused_types. The DIE of the type is marked
16883    only if the global variable using the type will actually be emitted.  */
16884
16885 static int
16886 premark_types_used_by_global_vars_helper (void **slot,
16887                                           void *data ATTRIBUTE_UNUSED)
16888 {
16889   struct types_used_by_vars_entry *entry;
16890   dw_die_ref die;
16891
16892   entry = (struct types_used_by_vars_entry *) *slot;
16893   gcc_assert (entry->type != NULL
16894               && entry->var_decl != NULL);
16895   die = lookup_type_die (entry->type);
16896   if (die)
16897     {
16898       /* Ask cgraph if the global variable really is to be emitted.
16899          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
16900       struct varpool_node *node = varpool_get_node (entry->var_decl);
16901       if (node && node->needed)
16902         {
16903           die->die_perennial_p = 1;
16904           /* Keep the parent DIEs as well.  */
16905           while ((die = die->die_parent) && die->die_perennial_p == 0)
16906             die->die_perennial_p = 1;
16907         }
16908     }
16909   return 1;
16910 }
16911
16912 /* Mark all members of used_types_hash as perennial.  */
16913
16914 static void
16915 premark_used_types (void)
16916 {
16917   if (cfun && cfun->used_types_hash)
16918     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
16919 }
16920
16921 /* Mark all members of types_used_by_vars_entry as perennial.  */
16922
16923 static void
16924 premark_types_used_by_global_vars (void)
16925 {
16926   if (types_used_by_vars_hash)
16927     htab_traverse (types_used_by_vars_hash,
16928                    premark_types_used_by_global_vars_helper, NULL);
16929 }
16930
16931 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
16932    for CA_LOC call arg loc node.  */
16933
16934 static dw_die_ref
16935 gen_call_site_die (tree decl, dw_die_ref subr_die,
16936                    struct call_arg_loc_node *ca_loc)
16937 {
16938   dw_die_ref stmt_die = NULL, die;
16939   tree block = ca_loc->block;
16940
16941   while (block
16942          && block != DECL_INITIAL (decl)
16943          && TREE_CODE (block) == BLOCK)
16944     {
16945       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
16946         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
16947       if (stmt_die)
16948         break;
16949       block = BLOCK_SUPERCONTEXT (block);
16950     }
16951   if (stmt_die == NULL)
16952     stmt_die = subr_die;
16953   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
16954   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
16955   if (ca_loc->tail_call_p)
16956     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
16957   if (ca_loc->symbol_ref)
16958     {
16959       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
16960       if (tdie)
16961         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
16962       else
16963         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
16964     }
16965   return die;
16966 }
16967
16968 /* Generate a DIE to represent a declared function (either file-scope or
16969    block-local).  */
16970
16971 static void
16972 gen_subprogram_die (tree decl, dw_die_ref context_die)
16973 {
16974   tree origin = decl_ultimate_origin (decl);
16975   dw_die_ref subr_die;
16976   tree outer_scope;
16977   dw_die_ref old_die = lookup_decl_die (decl);
16978   int declaration = (current_function_decl != decl
16979                      || class_or_namespace_scope_p (context_die));
16980
16981   premark_used_types ();
16982
16983   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
16984      started to generate the abstract instance of an inline, decided to output
16985      its containing class, and proceeded to emit the declaration of the inline
16986      from the member list for the class.  If so, DECLARATION takes priority;
16987      we'll get back to the abstract instance when done with the class.  */
16988
16989   /* The class-scope declaration DIE must be the primary DIE.  */
16990   if (origin && declaration && class_or_namespace_scope_p (context_die))
16991     {
16992       origin = NULL;
16993       gcc_assert (!old_die);
16994     }
16995
16996   /* Now that the C++ front end lazily declares artificial member fns, we
16997      might need to retrofit the declaration into its class.  */
16998   if (!declaration && !origin && !old_die
16999       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17000       && !class_or_namespace_scope_p (context_die)
17001       && debug_info_level > DINFO_LEVEL_TERSE)
17002     old_die = force_decl_die (decl);
17003
17004   if (origin != NULL)
17005     {
17006       gcc_assert (!declaration || local_scope_p (context_die));
17007
17008       /* Fixup die_parent for the abstract instance of a nested
17009          inline function.  */
17010       if (old_die && old_die->die_parent == NULL)
17011         add_child_die (context_die, old_die);
17012
17013       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17014       add_abstract_origin_attribute (subr_die, origin);
17015       /*  This is where the actual code for a cloned function is.
17016           Let's emit linkage name attribute for it.  This helps
17017           debuggers to e.g, set breakpoints into
17018           constructors/destructors when the user asks "break
17019           K::K".  */
17020       add_linkage_name (subr_die, decl);
17021     }
17022   else if (old_die)
17023     {
17024       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17025       struct dwarf_file_data * file_index = lookup_filename (s.file);
17026
17027       if (!get_AT_flag (old_die, DW_AT_declaration)
17028           /* We can have a normal definition following an inline one in the
17029              case of redefinition of GNU C extern inlines.
17030              It seems reasonable to use AT_specification in this case.  */
17031           && !get_AT (old_die, DW_AT_inline))
17032         {
17033           /* Detect and ignore this case, where we are trying to output
17034              something we have already output.  */
17035           return;
17036         }
17037
17038       /* If the definition comes from the same place as the declaration,
17039          maybe use the old DIE.  We always want the DIE for this function
17040          that has the *_pc attributes to be under comp_unit_die so the
17041          debugger can find it.  We also need to do this for abstract
17042          instances of inlines, since the spec requires the out-of-line copy
17043          to have the same parent.  For local class methods, this doesn't
17044          apply; we just use the old DIE.  */
17045       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17046           && (DECL_ARTIFICIAL (decl)
17047               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17048                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17049                       == (unsigned) s.line))))
17050         {
17051           subr_die = old_die;
17052
17053           /* Clear out the declaration attribute and the formal parameters.
17054              Do not remove all children, because it is possible that this
17055              declaration die was forced using force_decl_die(). In such
17056              cases die that forced declaration die (e.g. TAG_imported_module)
17057              is one of the children that we do not want to remove.  */
17058           remove_AT (subr_die, DW_AT_declaration);
17059           remove_AT (subr_die, DW_AT_object_pointer);
17060           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17061         }
17062       else
17063         {
17064           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17065           add_AT_specification (subr_die, old_die);
17066           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17067             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17068           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17069             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17070         }
17071     }
17072   else
17073     {
17074       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17075
17076       if (TREE_PUBLIC (decl))
17077         add_AT_flag (subr_die, DW_AT_external, 1);
17078
17079       add_name_and_src_coords_attributes (subr_die, decl);
17080       if (debug_info_level > DINFO_LEVEL_TERSE)
17081         {
17082           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17083           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17084                               0, 0, context_die);
17085         }
17086
17087       add_pure_or_virtual_attribute (subr_die, decl);
17088       if (DECL_ARTIFICIAL (decl))
17089         add_AT_flag (subr_die, DW_AT_artificial, 1);
17090
17091       add_accessibility_attribute (subr_die, decl);
17092     }
17093
17094   if (declaration)
17095     {
17096       if (!old_die || !get_AT (old_die, DW_AT_inline))
17097         {
17098           add_AT_flag (subr_die, DW_AT_declaration, 1);
17099
17100           /* If this is an explicit function declaration then generate
17101              a DW_AT_explicit attribute.  */
17102           if (lang_hooks.decls.function_decl_explicit_p (decl)
17103               && (dwarf_version >= 3 || !dwarf_strict))
17104             add_AT_flag (subr_die, DW_AT_explicit, 1);
17105
17106           /* The first time we see a member function, it is in the context of
17107              the class to which it belongs.  We make sure of this by emitting
17108              the class first.  The next time is the definition, which is
17109              handled above.  The two may come from the same source text.
17110
17111              Note that force_decl_die() forces function declaration die. It is
17112              later reused to represent definition.  */
17113           equate_decl_number_to_die (decl, subr_die);
17114         }
17115     }
17116   else if (DECL_ABSTRACT (decl))
17117     {
17118       if (DECL_DECLARED_INLINE_P (decl))
17119         {
17120           if (cgraph_function_possibly_inlined_p (decl))
17121             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17122           else
17123             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17124         }
17125       else
17126         {
17127           if (cgraph_function_possibly_inlined_p (decl))
17128             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17129           else
17130             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17131         }
17132
17133       if (DECL_DECLARED_INLINE_P (decl)
17134           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17135         add_AT_flag (subr_die, DW_AT_artificial, 1);
17136
17137       equate_decl_number_to_die (decl, subr_die);
17138     }
17139   else if (!DECL_EXTERNAL (decl))
17140     {
17141       HOST_WIDE_INT cfa_fb_offset;
17142
17143       if (!old_die || !get_AT (old_die, DW_AT_inline))
17144         equate_decl_number_to_die (decl, subr_die);
17145
17146       if (!flag_reorder_blocks_and_partition)
17147         {
17148           dw_fde_ref fde = cfun->fde;
17149           if (fde->dw_fde_begin)
17150             {
17151               /* We have already generated the labels.  */
17152               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17153               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17154             }
17155           else
17156             {
17157               /* Create start/end labels and add the range.  */
17158               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17159               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17160                                            current_function_funcdef_no);
17161               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17162               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17163                                            current_function_funcdef_no);
17164               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17165             }
17166
17167 #if VMS_DEBUGGING_INFO
17168       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17169          Section 2.3 Prologue and Epilogue Attributes:
17170          When a breakpoint is set on entry to a function, it is generally
17171          desirable for execution to be suspended, not on the very first
17172          instruction of the function, but rather at a point after the
17173          function's frame has been set up, after any language defined local
17174          declaration processing has been completed, and before execution of
17175          the first statement of the function begins. Debuggers generally
17176          cannot properly determine where this point is.  Similarly for a
17177          breakpoint set on exit from a function. The prologue and epilogue
17178          attributes allow a compiler to communicate the location(s) to use.  */
17179
17180       {
17181         if (fde->dw_fde_vms_end_prologue)
17182           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17183             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17184
17185         if (fde->dw_fde_vms_begin_epilogue)
17186           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17187             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17188       }
17189 #endif
17190
17191           add_pubname (decl, subr_die);
17192         }
17193       else
17194         {
17195           /* Generate pubnames entries for the split function code ranges.  */
17196           dw_fde_ref fde = cfun->fde;
17197
17198           if (fde->dw_fde_second_begin)
17199             {
17200               if (dwarf_version >= 3 || !dwarf_strict)
17201                 {
17202                   /* We should use ranges for non-contiguous code section 
17203                      addresses.  Use the actual code range for the initial
17204                      section, since the HOT/COLD labels might precede an 
17205                      alignment offset.  */
17206                   bool range_list_added = false;
17207                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17208                                         fde->dw_fde_end, &range_list_added);
17209                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17210                                         fde->dw_fde_second_end,
17211                                         &range_list_added);
17212                   add_pubname (decl, subr_die);
17213                   if (range_list_added)
17214                     add_ranges (NULL);
17215                 }
17216               else
17217                 {
17218                   /* There is no real support in DW2 for this .. so we make
17219                      a work-around.  First, emit the pub name for the segment
17220                      containing the function label.  Then make and emit a
17221                      simplified subprogram DIE for the second segment with the
17222                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17223                      linkage name for the second die so that gdb will find both
17224                      sections when given "b foo".  */
17225                   const char *name = NULL;
17226                   tree decl_name = DECL_NAME (decl);
17227                   dw_die_ref seg_die;
17228
17229                   /* Do the 'primary' section.   */
17230                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17231                                  fde->dw_fde_begin);
17232                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17233                                  fde->dw_fde_end);
17234                   /* Add it.   */
17235                   add_pubname (decl, subr_die);
17236
17237                   /* Build a minimal DIE for the secondary section.  */
17238                   seg_die = new_die (DW_TAG_subprogram,
17239                                      subr_die->die_parent, decl);
17240
17241                   if (TREE_PUBLIC (decl))
17242                     add_AT_flag (seg_die, DW_AT_external, 1);
17243
17244                   if (decl_name != NULL 
17245                       && IDENTIFIER_POINTER (decl_name) != NULL)
17246                     {
17247                       name = dwarf2_name (decl, 1);
17248                       if (! DECL_ARTIFICIAL (decl))
17249                         add_src_coords_attributes (seg_die, decl);
17250
17251                       add_linkage_name (seg_die, decl);
17252                     }
17253                   gcc_assert (name != NULL);
17254                   add_pure_or_virtual_attribute (seg_die, decl);
17255                   if (DECL_ARTIFICIAL (decl))
17256                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17257
17258                   name = concat ("__second_sect_of_", name, NULL); 
17259                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17260                                  fde->dw_fde_second_begin);
17261                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17262                                  fde->dw_fde_second_end);
17263                   add_name_attribute (seg_die, name);
17264                   add_pubname_string (name, seg_die);
17265                 }
17266             }
17267           else
17268             {
17269               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17270               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17271               add_pubname (decl, subr_die);
17272             }
17273         }
17274
17275 #ifdef MIPS_DEBUGGING_INFO
17276       /* Add a reference to the FDE for this routine.  */
17277       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, cfun->fde->fde_index);
17278 #endif
17279
17280       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17281
17282       /* We define the "frame base" as the function's CFA.  This is more
17283          convenient for several reasons: (1) It's stable across the prologue
17284          and epilogue, which makes it better than just a frame pointer,
17285          (2) With dwarf3, there exists a one-byte encoding that allows us
17286          to reference the .debug_frame data by proxy, but failing that,
17287          (3) We can at least reuse the code inspection and interpretation
17288          code that determines the CFA position at various points in the
17289          function.  */
17290       if (dwarf_version >= 3)
17291         {
17292           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17293           add_AT_loc (subr_die, DW_AT_frame_base, op);
17294         }
17295       else
17296         {
17297           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17298           if (list->dw_loc_next)
17299             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17300           else
17301             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17302         }
17303
17304       /* Compute a displacement from the "steady-state frame pointer" to
17305          the CFA.  The former is what all stack slots and argument slots
17306          will reference in the rtl; the later is what we've told the
17307          debugger about.  We'll need to adjust all frame_base references
17308          by this displacement.  */
17309       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17310
17311       if (cfun->static_chain_decl)
17312         add_AT_location_description (subr_die, DW_AT_static_link,
17313                  loc_list_from_tree (cfun->static_chain_decl, 2));
17314     }
17315
17316   /* Generate child dies for template paramaters.  */
17317   if (debug_info_level > DINFO_LEVEL_TERSE)
17318     gen_generic_params_dies (decl);
17319
17320   /* Now output descriptions of the arguments for this function. This gets
17321      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17322      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17323      `...' at the end of the formal parameter list.  In order to find out if
17324      there was a trailing ellipsis or not, we must instead look at the type
17325      associated with the FUNCTION_DECL.  This will be a node of type
17326      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17327      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17328      an ellipsis at the end.  */
17329
17330   /* In the case where we are describing a mere function declaration, all we
17331      need to do here (and all we *can* do here) is to describe the *types* of
17332      its formal parameters.  */
17333   if (debug_info_level <= DINFO_LEVEL_TERSE)
17334     ;
17335   else if (declaration)
17336     gen_formal_types_die (decl, subr_die);
17337   else
17338     {
17339       /* Generate DIEs to represent all known formal parameters.  */
17340       tree parm = DECL_ARGUMENTS (decl);
17341       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17342       tree generic_decl_parm = generic_decl
17343                                 ? DECL_ARGUMENTS (generic_decl)
17344                                 : NULL;
17345
17346       /* Now we want to walk the list of parameters of the function and
17347          emit their relevant DIEs.
17348
17349          We consider the case of DECL being an instance of a generic function
17350          as well as it being a normal function.
17351
17352          If DECL is an instance of a generic function we walk the
17353          parameters of the generic function declaration _and_ the parameters of
17354          DECL itself. This is useful because we want to emit specific DIEs for
17355          function parameter packs and those are declared as part of the
17356          generic function declaration. In that particular case,
17357          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17358          That DIE has children DIEs representing the set of arguments
17359          of the pack. Note that the set of pack arguments can be empty.
17360          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17361          children DIE.
17362
17363          Otherwise, we just consider the parameters of DECL.  */
17364       while (generic_decl_parm || parm)
17365         {
17366           if (generic_decl_parm
17367               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17368             gen_formal_parameter_pack_die (generic_decl_parm,
17369                                            parm, subr_die,
17370                                            &parm);
17371           else if (parm)
17372             {
17373               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17374
17375               if (parm == DECL_ARGUMENTS (decl)
17376                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17377                   && parm_die
17378                   && (dwarf_version >= 3 || !dwarf_strict))
17379                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17380
17381               parm = DECL_CHAIN (parm);
17382             }
17383
17384           if (generic_decl_parm)
17385             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17386         }
17387
17388       /* Decide whether we need an unspecified_parameters DIE at the end.
17389          There are 2 more cases to do this for: 1) the ansi ... declaration -
17390          this is detectable when the end of the arg list is not a
17391          void_type_node 2) an unprototyped function declaration (not a
17392          definition).  This just means that we have no info about the
17393          parameters at all.  */
17394       if (prototype_p (TREE_TYPE (decl)))
17395         {
17396           /* This is the prototyped case, check for....  */
17397           if (stdarg_p (TREE_TYPE (decl)))
17398             gen_unspecified_parameters_die (decl, subr_die);
17399         }
17400       else if (DECL_INITIAL (decl) == NULL_TREE)
17401         gen_unspecified_parameters_die (decl, subr_die);
17402     }
17403
17404   /* Output Dwarf info for all of the stuff within the body of the function
17405      (if it has one - it may be just a declaration).  */
17406   outer_scope = DECL_INITIAL (decl);
17407
17408   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17409      a function.  This BLOCK actually represents the outermost binding contour
17410      for the function, i.e. the contour in which the function's formal
17411      parameters and labels get declared. Curiously, it appears that the front
17412      end doesn't actually put the PARM_DECL nodes for the current function onto
17413      the BLOCK_VARS list for this outer scope, but are strung off of the
17414      DECL_ARGUMENTS list for the function instead.
17415
17416      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17417      the LABEL_DECL nodes for the function however, and we output DWARF info
17418      for those in decls_for_scope.  Just within the `outer_scope' there will be
17419      a BLOCK node representing the function's outermost pair of curly braces,
17420      and any blocks used for the base and member initializers of a C++
17421      constructor function.  */
17422   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17423     {
17424       int call_site_note_count = 0;
17425       int tail_call_site_note_count = 0;
17426
17427       /* Emit a DW_TAG_variable DIE for a named return value.  */
17428       if (DECL_NAME (DECL_RESULT (decl)))
17429         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17430
17431       current_function_has_inlines = 0;
17432       decls_for_scope (outer_scope, subr_die, 0);
17433
17434       if (call_arg_locations && !dwarf_strict)
17435         {
17436           struct call_arg_loc_node *ca_loc;
17437           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17438             {
17439               dw_die_ref die = NULL;
17440               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17441               rtx arg, next_arg;
17442
17443               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17444                    arg; arg = next_arg)
17445                 {
17446                   dw_loc_descr_ref reg, val;
17447                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17448                   dw_die_ref cdie, tdie = NULL;
17449
17450                   next_arg = XEXP (arg, 1);
17451                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17452                       && next_arg
17453                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17454                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17455                       && REGNO (XEXP (XEXP (arg, 0), 0))
17456                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17457                     next_arg = XEXP (next_arg, 1);
17458                   if (mode == VOIDmode)
17459                     {
17460                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17461                       if (mode == VOIDmode)
17462                         mode = GET_MODE (XEXP (arg, 0));
17463                     }
17464                   if (mode == VOIDmode || mode == BLKmode)
17465                     continue;
17466                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17467                     {
17468                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17469                       tloc = XEXP (XEXP (arg, 0), 1);
17470                       continue;
17471                     }
17472                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17473                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17474                     {
17475                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17476                       tlocc = XEXP (XEXP (arg, 0), 1);
17477                       continue;
17478                     }
17479                   reg = NULL;
17480                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17481                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
17482                                               VAR_INIT_STATUS_INITIALIZED);
17483                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
17484                     {
17485                       rtx mem = XEXP (XEXP (arg, 0), 0);
17486                       reg = mem_loc_descriptor (XEXP (mem, 0),
17487                                                 get_address_mode (mem),
17488                                                 GET_MODE (mem),
17489                                                 VAR_INIT_STATUS_INITIALIZED);
17490                     }
17491                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
17492                            == DEBUG_PARAMETER_REF)
17493                     {
17494                       tree tdecl
17495                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
17496                       tdie = lookup_decl_die (tdecl);
17497                       if (tdie == NULL)
17498                         continue;
17499                     }
17500                   else
17501                     continue;
17502                   if (reg == NULL
17503                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
17504                          != DEBUG_PARAMETER_REF)
17505                     continue;
17506                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
17507                                             VOIDmode,
17508                                             VAR_INIT_STATUS_INITIALIZED);
17509                   if (val == NULL)
17510                     continue;
17511                   if (die == NULL)
17512                     die = gen_call_site_die (decl, subr_die, ca_loc);
17513                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
17514                                   NULL_TREE);
17515                   if (reg != NULL)
17516                     add_AT_loc (cdie, DW_AT_location, reg);
17517                   else if (tdie != NULL)
17518                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
17519                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
17520                   if (next_arg != XEXP (arg, 1))
17521                     {
17522                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
17523                       if (mode == VOIDmode)
17524                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
17525                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
17526                                                             0), 1),
17527                                                 mode, VOIDmode,
17528                                                 VAR_INIT_STATUS_INITIALIZED);
17529                       if (val != NULL)
17530                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
17531                     }
17532                 }
17533               if (die == NULL
17534                   && (ca_loc->symbol_ref || tloc))
17535                 die = gen_call_site_die (decl, subr_die, ca_loc);
17536               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
17537                 {
17538                   dw_loc_descr_ref tval = NULL;
17539
17540                   if (tloc != NULL_RTX)
17541                     tval = mem_loc_descriptor (tloc,
17542                                                GET_MODE (tloc) == VOIDmode
17543                                                ? Pmode : GET_MODE (tloc),
17544                                                VOIDmode,
17545                                                VAR_INIT_STATUS_INITIALIZED);
17546                   if (tval)
17547                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
17548                   else if (tlocc != NULL_RTX)
17549                     {
17550                       tval = mem_loc_descriptor (tlocc,
17551                                                  GET_MODE (tlocc) == VOIDmode
17552                                                  ? Pmode : GET_MODE (tlocc),
17553                                                  VOIDmode,
17554                                                  VAR_INIT_STATUS_INITIALIZED);
17555                       if (tval)
17556                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
17557                                     tval);
17558                     }
17559                 }
17560               if (die != NULL)
17561                 {
17562                   call_site_note_count++;
17563                   if (ca_loc->tail_call_p)
17564                     tail_call_site_note_count++;
17565                 }
17566             }
17567         }
17568       call_arg_locations = NULL;
17569       call_arg_loc_last = NULL;
17570       if (tail_call_site_count >= 0
17571           && tail_call_site_count == tail_call_site_note_count
17572           && !dwarf_strict)
17573         {
17574           if (call_site_count >= 0
17575               && call_site_count == call_site_note_count)
17576             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
17577           else
17578             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
17579         }
17580       call_site_count = -1;
17581       tail_call_site_count = -1;
17582     }
17583   /* Add the calling convention attribute if requested.  */
17584   add_calling_convention_attribute (subr_die, decl);
17585
17586 }
17587
17588 /* Returns a hash value for X (which really is a die_struct).  */
17589
17590 static hashval_t
17591 common_block_die_table_hash (const void *x)
17592 {
17593   const_dw_die_ref d = (const_dw_die_ref) x;
17594   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
17595 }
17596
17597 /* Return nonzero if decl_id and die_parent of die_struct X is the same
17598    as decl_id and die_parent of die_struct Y.  */
17599
17600 static int
17601 common_block_die_table_eq (const void *x, const void *y)
17602 {
17603   const_dw_die_ref d = (const_dw_die_ref) x;
17604   const_dw_die_ref e = (const_dw_die_ref) y;
17605   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
17606 }
17607
17608 /* Generate a DIE to represent a declared data object.
17609    Either DECL or ORIGIN must be non-null.  */
17610
17611 static void
17612 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
17613 {
17614   HOST_WIDE_INT off;
17615   tree com_decl;
17616   tree decl_or_origin = decl ? decl : origin;
17617   tree ultimate_origin;
17618   dw_die_ref var_die;
17619   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
17620   dw_die_ref origin_die;
17621   bool declaration = (DECL_EXTERNAL (decl_or_origin)
17622                       || class_or_namespace_scope_p (context_die));
17623   bool specialization_p = false;
17624
17625   ultimate_origin = decl_ultimate_origin (decl_or_origin);
17626   if (decl || ultimate_origin)
17627     origin = ultimate_origin;
17628   com_decl = fortran_common (decl_or_origin, &off);
17629
17630   /* Symbol in common gets emitted as a child of the common block, in the form
17631      of a data member.  */
17632   if (com_decl)
17633     {
17634       dw_die_ref com_die;
17635       dw_loc_list_ref loc;
17636       die_node com_die_arg;
17637
17638       var_die = lookup_decl_die (decl_or_origin);
17639       if (var_die)
17640         {
17641           if (get_AT (var_die, DW_AT_location) == NULL)
17642             {
17643               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
17644               if (loc)
17645                 {
17646                   if (off)
17647                     {
17648                       /* Optimize the common case.  */
17649                       if (single_element_loc_list_p (loc)
17650                           && loc->expr->dw_loc_opc == DW_OP_addr
17651                           && loc->expr->dw_loc_next == NULL
17652                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
17653                              == SYMBOL_REF)
17654                         loc->expr->dw_loc_oprnd1.v.val_addr
17655                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17656                         else
17657                           loc_list_plus_const (loc, off);
17658                     }
17659                   add_AT_location_description (var_die, DW_AT_location, loc);
17660                   remove_AT (var_die, DW_AT_declaration);
17661                 }
17662             }
17663           return;
17664         }
17665
17666       if (common_block_die_table == NULL)
17667         common_block_die_table
17668           = htab_create_ggc (10, common_block_die_table_hash,
17669                              common_block_die_table_eq, NULL);
17670
17671       com_die_arg.decl_id = DECL_UID (com_decl);
17672       com_die_arg.die_parent = context_die;
17673       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
17674       loc = loc_list_from_tree (com_decl, 2);
17675       if (com_die == NULL)
17676         {
17677           const char *cnam
17678             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
17679           void **slot;
17680
17681           com_die = new_die (DW_TAG_common_block, context_die, decl);
17682           add_name_and_src_coords_attributes (com_die, com_decl);
17683           if (loc)
17684             {
17685               add_AT_location_description (com_die, DW_AT_location, loc);
17686               /* Avoid sharing the same loc descriptor between
17687                  DW_TAG_common_block and DW_TAG_variable.  */
17688               loc = loc_list_from_tree (com_decl, 2);
17689             }
17690           else if (DECL_EXTERNAL (decl))
17691             add_AT_flag (com_die, DW_AT_declaration, 1);
17692           add_pubname_string (cnam, com_die); /* ??? needed? */
17693           com_die->decl_id = DECL_UID (com_decl);
17694           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
17695           *slot = (void *) com_die;
17696         }
17697       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
17698         {
17699           add_AT_location_description (com_die, DW_AT_location, loc);
17700           loc = loc_list_from_tree (com_decl, 2);
17701           remove_AT (com_die, DW_AT_declaration);
17702         }
17703       var_die = new_die (DW_TAG_variable, com_die, decl);
17704       add_name_and_src_coords_attributes (var_die, decl);
17705       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
17706                           TREE_THIS_VOLATILE (decl), context_die);
17707       add_AT_flag (var_die, DW_AT_external, 1);
17708       if (loc)
17709         {
17710           if (off)
17711             {
17712               /* Optimize the common case.  */
17713               if (single_element_loc_list_p (loc)
17714                   && loc->expr->dw_loc_opc == DW_OP_addr
17715                   && loc->expr->dw_loc_next == NULL
17716                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
17717                 loc->expr->dw_loc_oprnd1.v.val_addr
17718                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
17719               else
17720                 loc_list_plus_const (loc, off);
17721             }
17722           add_AT_location_description (var_die, DW_AT_location, loc);
17723         }
17724       else if (DECL_EXTERNAL (decl))
17725         add_AT_flag (var_die, DW_AT_declaration, 1);
17726       equate_decl_number_to_die (decl, var_die);
17727       return;
17728     }
17729
17730   /* If the compiler emitted a definition for the DECL declaration
17731      and if we already emitted a DIE for it, don't emit a second
17732      DIE for it again. Allow re-declarations of DECLs that are
17733      inside functions, though.  */
17734   if (old_die && declaration && !local_scope_p (context_die))
17735     return;
17736
17737   /* For static data members, the declaration in the class is supposed
17738      to have DW_TAG_member tag; the specification should still be
17739      DW_TAG_variable referencing the DW_TAG_member DIE.  */
17740   if (declaration && class_scope_p (context_die))
17741     var_die = new_die (DW_TAG_member, context_die, decl);
17742   else
17743     var_die = new_die (DW_TAG_variable, context_die, decl);
17744
17745   origin_die = NULL;
17746   if (origin != NULL)
17747     origin_die = add_abstract_origin_attribute (var_die, origin);
17748
17749   /* Loop unrolling can create multiple blocks that refer to the same
17750      static variable, so we must test for the DW_AT_declaration flag.
17751
17752      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
17753      copy decls and set the DECL_ABSTRACT flag on them instead of
17754      sharing them.
17755
17756      ??? Duplicated blocks have been rewritten to use .debug_ranges.
17757
17758      ??? The declare_in_namespace support causes us to get two DIEs for one
17759      variable, both of which are declarations.  We want to avoid considering
17760      one to be a specification, so we must test that this DIE is not a
17761      declaration.  */
17762   else if (old_die && TREE_STATIC (decl) && ! declaration
17763            && get_AT_flag (old_die, DW_AT_declaration) == 1)
17764     {
17765       /* This is a definition of a C++ class level static.  */
17766       add_AT_specification (var_die, old_die);
17767       specialization_p = true;
17768       if (DECL_NAME (decl))
17769         {
17770           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17771           struct dwarf_file_data * file_index = lookup_filename (s.file);
17772
17773           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17774             add_AT_file (var_die, DW_AT_decl_file, file_index);
17775
17776           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17777             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
17778
17779           if (old_die->die_tag == DW_TAG_member)
17780             add_linkage_name (var_die, decl);
17781         }
17782     }
17783   else
17784     add_name_and_src_coords_attributes (var_die, decl);
17785
17786   if ((origin == NULL && !specialization_p)
17787       || (origin != NULL
17788           && !DECL_ABSTRACT (decl_or_origin)
17789           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
17790                                        decl_function_context
17791                                                         (decl_or_origin))))
17792     {
17793       tree type = TREE_TYPE (decl_or_origin);
17794
17795       if (decl_by_reference_p (decl_or_origin))
17796         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
17797       else
17798         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
17799                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
17800     }
17801
17802   if (origin == NULL && !specialization_p)
17803     {
17804       if (TREE_PUBLIC (decl))
17805         add_AT_flag (var_die, DW_AT_external, 1);
17806
17807       if (DECL_ARTIFICIAL (decl))
17808         add_AT_flag (var_die, DW_AT_artificial, 1);
17809
17810       add_accessibility_attribute (var_die, decl);
17811     }
17812
17813   if (declaration)
17814     add_AT_flag (var_die, DW_AT_declaration, 1);
17815
17816   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
17817     equate_decl_number_to_die (decl, var_die);
17818
17819   if (! declaration
17820       && (! DECL_ABSTRACT (decl_or_origin)
17821           /* Local static vars are shared between all clones/inlines,
17822              so emit DW_AT_location on the abstract DIE if DECL_RTL is
17823              already set.  */
17824           || (TREE_CODE (decl_or_origin) == VAR_DECL
17825               && TREE_STATIC (decl_or_origin)
17826               && DECL_RTL_SET_P (decl_or_origin)))
17827       /* When abstract origin already has DW_AT_location attribute, no need
17828          to add it again.  */
17829       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
17830     {
17831       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
17832           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
17833         defer_location (decl_or_origin, var_die);
17834       else
17835         add_location_or_const_value_attribute (var_die, decl_or_origin,
17836                                                decl == NULL, DW_AT_location);
17837       add_pubname (decl_or_origin, var_die);
17838     }
17839   else
17840     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
17841 }
17842
17843 /* Generate a DIE to represent a named constant.  */
17844
17845 static void
17846 gen_const_die (tree decl, dw_die_ref context_die)
17847 {
17848   dw_die_ref const_die;
17849   tree type = TREE_TYPE (decl);
17850
17851   const_die = new_die (DW_TAG_constant, context_die, decl);
17852   add_name_and_src_coords_attributes (const_die, decl);
17853   add_type_attribute (const_die, type, 1, 0, context_die);
17854   if (TREE_PUBLIC (decl))
17855     add_AT_flag (const_die, DW_AT_external, 1);
17856   if (DECL_ARTIFICIAL (decl))
17857     add_AT_flag (const_die, DW_AT_artificial, 1);
17858   tree_add_const_value_attribute_for_decl (const_die, decl);
17859 }
17860
17861 /* Generate a DIE to represent a label identifier.  */
17862
17863 static void
17864 gen_label_die (tree decl, dw_die_ref context_die)
17865 {
17866   tree origin = decl_ultimate_origin (decl);
17867   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
17868   rtx insn;
17869   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17870
17871   if (origin != NULL)
17872     add_abstract_origin_attribute (lbl_die, origin);
17873   else
17874     add_name_and_src_coords_attributes (lbl_die, decl);
17875
17876   if (DECL_ABSTRACT (decl))
17877     equate_decl_number_to_die (decl, lbl_die);
17878   else
17879     {
17880       insn = DECL_RTL_IF_SET (decl);
17881
17882       /* Deleted labels are programmer specified labels which have been
17883          eliminated because of various optimizations.  We still emit them
17884          here so that it is possible to put breakpoints on them.  */
17885       if (insn
17886           && (LABEL_P (insn)
17887               || ((NOTE_P (insn)
17888                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
17889         {
17890           /* When optimization is enabled (via -O) some parts of the compiler
17891              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
17892              represent source-level labels which were explicitly declared by
17893              the user.  This really shouldn't be happening though, so catch
17894              it if it ever does happen.  */
17895           gcc_assert (!INSN_DELETED_P (insn));
17896
17897           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
17898           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
17899         }
17900     }
17901 }
17902
17903 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
17904    attributes to the DIE for a block STMT, to describe where the inlined
17905    function was called from.  This is similar to add_src_coords_attributes.  */
17906
17907 static inline void
17908 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
17909 {
17910   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
17911
17912   if (dwarf_version >= 3 || !dwarf_strict)
17913     {
17914       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
17915       add_AT_unsigned (die, DW_AT_call_line, s.line);
17916     }
17917 }
17918
17919
17920 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
17921    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
17922
17923 static inline void
17924 add_high_low_attributes (tree stmt, dw_die_ref die)
17925 {
17926   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17927
17928   if (BLOCK_FRAGMENT_CHAIN (stmt)
17929       && (dwarf_version >= 3 || !dwarf_strict))
17930     {
17931       tree chain;
17932
17933       if (inlined_function_outer_scope_p (stmt))
17934         {
17935           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17936                                        BLOCK_NUMBER (stmt));
17937           add_AT_lbl_id (die, DW_AT_entry_pc, label);
17938         }
17939
17940       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
17941
17942       chain = BLOCK_FRAGMENT_CHAIN (stmt);
17943       do
17944         {
17945           add_ranges (chain);
17946           chain = BLOCK_FRAGMENT_CHAIN (chain);
17947         }
17948       while (chain);
17949       add_ranges (NULL);
17950     }
17951   else
17952     {
17953       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
17954                                    BLOCK_NUMBER (stmt));
17955       add_AT_lbl_id (die, DW_AT_low_pc, label);
17956       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
17957                                    BLOCK_NUMBER (stmt));
17958       add_AT_lbl_id (die, DW_AT_high_pc, label);
17959     }
17960 }
17961
17962 /* Generate a DIE for a lexical block.  */
17963
17964 static void
17965 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
17966 {
17967   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
17968
17969   if (call_arg_locations)
17970     {
17971       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
17972         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
17973                                BLOCK_NUMBER (stmt) + 1);
17974       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
17975     }
17976
17977   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
17978     add_high_low_attributes (stmt, stmt_die);
17979
17980   decls_for_scope (stmt, stmt_die, depth);
17981 }
17982
17983 /* Generate a DIE for an inlined subprogram.  */
17984
17985 static void
17986 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
17987 {
17988   tree decl;
17989
17990   /* The instance of function that is effectively being inlined shall not
17991      be abstract.  */
17992   gcc_assert (! BLOCK_ABSTRACT (stmt));
17993
17994   decl = block_ultimate_origin (stmt);
17995
17996   /* Emit info for the abstract instance first, if we haven't yet.  We
17997      must emit this even if the block is abstract, otherwise when we
17998      emit the block below (or elsewhere), we may end up trying to emit
17999      a die whose origin die hasn't been emitted, and crashing.  */
18000   dwarf2out_abstract_function (decl);
18001
18002   if (! BLOCK_ABSTRACT (stmt))
18003     {
18004       dw_die_ref subr_die
18005         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18006
18007       if (call_arg_locations)
18008         {
18009           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18010             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18011                                    BLOCK_NUMBER (stmt) + 1);
18012           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18013         }
18014       add_abstract_origin_attribute (subr_die, decl);
18015       if (TREE_ASM_WRITTEN (stmt))
18016         add_high_low_attributes (stmt, subr_die);
18017       add_call_src_coords_attributes (stmt, subr_die);
18018
18019       decls_for_scope (stmt, subr_die, depth);
18020       current_function_has_inlines = 1;
18021     }
18022 }
18023
18024 /* Generate a DIE for a field in a record, or structure.  */
18025
18026 static void
18027 gen_field_die (tree decl, dw_die_ref context_die)
18028 {
18029   dw_die_ref decl_die;
18030
18031   if (TREE_TYPE (decl) == error_mark_node)
18032     return;
18033
18034   decl_die = new_die (DW_TAG_member, context_die, decl);
18035   add_name_and_src_coords_attributes (decl_die, decl);
18036   add_type_attribute (decl_die, member_declared_type (decl),
18037                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18038                       context_die);
18039
18040   if (DECL_BIT_FIELD_TYPE (decl))
18041     {
18042       add_byte_size_attribute (decl_die, decl);
18043       add_bit_size_attribute (decl_die, decl);
18044       add_bit_offset_attribute (decl_die, decl);
18045     }
18046
18047   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18048     add_data_member_location_attribute (decl_die, decl);
18049
18050   if (DECL_ARTIFICIAL (decl))
18051     add_AT_flag (decl_die, DW_AT_artificial, 1);
18052
18053   add_accessibility_attribute (decl_die, decl);
18054
18055   /* Equate decl number to die, so that we can look up this decl later on.  */
18056   equate_decl_number_to_die (decl, decl_die);
18057 }
18058
18059 #if 0
18060 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18061    Use modified_type_die instead.
18062    We keep this code here just in case these types of DIEs may be needed to
18063    represent certain things in other languages (e.g. Pascal) someday.  */
18064
18065 static void
18066 gen_pointer_type_die (tree type, dw_die_ref context_die)
18067 {
18068   dw_die_ref ptr_die
18069     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18070
18071   equate_type_number_to_die (type, ptr_die);
18072   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18073   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18074 }
18075
18076 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18077    Use modified_type_die instead.
18078    We keep this code here just in case these types of DIEs may be needed to
18079    represent certain things in other languages (e.g. Pascal) someday.  */
18080
18081 static void
18082 gen_reference_type_die (tree type, dw_die_ref context_die)
18083 {
18084   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18085
18086   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18087     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18088   else
18089     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18090
18091   equate_type_number_to_die (type, ref_die);
18092   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18093   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18094 }
18095 #endif
18096
18097 /* Generate a DIE for a pointer to a member type.  */
18098
18099 static void
18100 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18101 {
18102   dw_die_ref ptr_die
18103     = new_die (DW_TAG_ptr_to_member_type,
18104                scope_die_for (type, context_die), type);
18105
18106   equate_type_number_to_die (type, ptr_die);
18107   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18108                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18109   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18110 }
18111
18112 typedef const char *dchar_p; /* For DEF_VEC_P.  */
18113 DEF_VEC_P(dchar_p);
18114 DEF_VEC_ALLOC_P(dchar_p,heap);
18115
18116 static char *producer_string;
18117
18118 /* Return a heap allocated producer string including command line options
18119    if -grecord-gcc-switches.  */
18120
18121 static char *
18122 gen_producer_string (void)
18123 {
18124   size_t j;
18125   VEC(dchar_p, heap) *switches = NULL;
18126   const char *language_string = lang_hooks.name;
18127   char *producer, *tail;
18128   const char *p;
18129   size_t len = dwarf_record_gcc_switches ? 0 : 3;
18130   size_t plen = strlen (language_string) + 1 + strlen (version_string);
18131
18132   for (j = 1; dwarf_record_gcc_switches && j < save_decoded_options_count; j++)
18133     switch (save_decoded_options[j].opt_index)
18134       {
18135       case OPT_o:
18136       case OPT_d:
18137       case OPT_dumpbase:
18138       case OPT_dumpdir:
18139       case OPT_auxbase:
18140       case OPT_auxbase_strip:
18141       case OPT_quiet:
18142       case OPT_version:
18143       case OPT_v:
18144       case OPT_w:
18145       case OPT_L:
18146       case OPT_D:
18147       case OPT_I:
18148       case OPT_U:
18149       case OPT_SPECIAL_unknown:
18150       case OPT_SPECIAL_ignore:
18151       case OPT_SPECIAL_program_name:
18152       case OPT_SPECIAL_input_file:
18153       case OPT_grecord_gcc_switches:
18154       case OPT_gno_record_gcc_switches:
18155       case OPT__output_pch_:
18156       case OPT_fdiagnostics_show_location_:
18157       case OPT_fdiagnostics_show_option:
18158       case OPT____:
18159       case OPT__sysroot_:
18160       case OPT_nostdinc:
18161       case OPT_nostdinc__:
18162         /* Ignore these.  */
18163         continue;
18164       default:
18165         gcc_checking_assert (save_decoded_options[j].canonical_option[0][0]
18166                              == '-');
18167         switch (save_decoded_options[j].canonical_option[0][1])
18168           {
18169           case 'M':
18170           case 'i':
18171           case 'W':
18172             continue;
18173           case 'f':
18174             if (strncmp (save_decoded_options[j].canonical_option[0] + 2,
18175                          "dump", 4) == 0)
18176               continue;
18177             break;
18178           default:
18179             break;
18180           }
18181         VEC_safe_push (dchar_p, heap, switches,
18182                        save_decoded_options[j].orig_option_with_args_text);
18183         len += strlen (save_decoded_options[j].orig_option_with_args_text) + 1;
18184         break;
18185       }
18186
18187   producer = XNEWVEC (char, plen + 1 + len + 1);
18188   tail = producer;
18189   sprintf (tail, "%s %s", language_string, version_string);
18190   tail += plen;
18191
18192   if (!dwarf_record_gcc_switches)
18193     {
18194 #ifdef MIPS_DEBUGGING_INFO
18195       /* The MIPS/SGI compilers place the 'cc' command line options in the
18196          producer string.  The SGI debugger looks for -g, -g1, -g2, or -g3;
18197          if they do not appear in the producer string, the debugger reaches
18198          the conclusion that the object file is stripped and has no debugging
18199          information.  To get the MIPS/SGI debugger to believe that there is
18200          debugging information in the object file, we add a -g to the producer
18201          string.  */
18202       if (debug_info_level > DINFO_LEVEL_TERSE)
18203         {
18204           memcpy (tail, " -g", 3);
18205           tail += 3;
18206         }
18207 #endif
18208     }
18209
18210   FOR_EACH_VEC_ELT (dchar_p, switches, j, p)
18211     {
18212       len = strlen (p);
18213       *tail = ' ';
18214       memcpy (tail + 1, p, len);
18215       tail += len + 1;
18216     }
18217
18218   *tail = '\0';
18219   VEC_free (dchar_p, heap, switches);
18220   return producer;
18221 }
18222
18223 /* Generate the DIE for the compilation unit.  */
18224
18225 static dw_die_ref
18226 gen_compile_unit_die (const char *filename)
18227 {
18228   dw_die_ref die;
18229   const char *language_string = lang_hooks.name;
18230   int language;
18231
18232   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18233
18234   if (filename)
18235     {
18236       add_name_attribute (die, filename);
18237       /* Don't add cwd for <built-in>.  */
18238       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18239         add_comp_dir_attribute (die);
18240     }
18241
18242   if (producer_string == NULL)
18243     producer_string = gen_producer_string ();
18244   add_AT_string (die, DW_AT_producer, producer_string);
18245
18246   /* If our producer is LTO try to figure out a common language to use
18247      from the global list of translation units.  */
18248   if (strcmp (language_string, "GNU GIMPLE") == 0)
18249     {
18250       unsigned i;
18251       tree t;
18252       const char *common_lang = NULL;
18253
18254       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18255         {
18256           if (!TRANSLATION_UNIT_LANGUAGE (t))
18257             continue;
18258           if (!common_lang)
18259             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18260           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18261             ;
18262           else if (strncmp (common_lang, "GNU C", 5) == 0
18263                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18264             /* Mixing C and C++ is ok, use C++ in that case.  */
18265             common_lang = "GNU C++";
18266           else
18267             {
18268               /* Fall back to C.  */
18269               common_lang = NULL;
18270               break;
18271             }
18272         }
18273
18274       if (common_lang)
18275         language_string = common_lang;
18276     }
18277
18278   language = DW_LANG_C89;
18279   if (strcmp (language_string, "GNU C++") == 0)
18280     language = DW_LANG_C_plus_plus;
18281   else if (strcmp (language_string, "GNU F77") == 0)
18282     language = DW_LANG_Fortran77;
18283   else if (strcmp (language_string, "GNU Pascal") == 0)
18284     language = DW_LANG_Pascal83;
18285   else if (dwarf_version >= 3 || !dwarf_strict)
18286     {
18287       if (strcmp (language_string, "GNU Ada") == 0)
18288         language = DW_LANG_Ada95;
18289       else if (strcmp (language_string, "GNU Fortran") == 0)
18290         language = DW_LANG_Fortran95;
18291       else if (strcmp (language_string, "GNU Java") == 0)
18292         language = DW_LANG_Java;
18293       else if (strcmp (language_string, "GNU Objective-C") == 0)
18294         language = DW_LANG_ObjC;
18295       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18296         language = DW_LANG_ObjC_plus_plus;
18297     }
18298
18299   add_AT_unsigned (die, DW_AT_language, language);
18300
18301   switch (language)
18302     {
18303     case DW_LANG_Fortran77:
18304     case DW_LANG_Fortran90:
18305     case DW_LANG_Fortran95:
18306       /* Fortran has case insensitive identifiers and the front-end
18307          lowercases everything.  */
18308       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18309       break;
18310     default:
18311       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18312       break;
18313     }
18314   return die;
18315 }
18316
18317 /* Generate the DIE for a base class.  */
18318
18319 static void
18320 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18321 {
18322   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18323
18324   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18325   add_data_member_location_attribute (die, binfo);
18326
18327   if (BINFO_VIRTUAL_P (binfo))
18328     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18329
18330   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18331      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18332      the default has always been DW_ACCESS_private.  */
18333   if (access == access_public_node)
18334     {
18335       if (dwarf_version == 2
18336           || context_die->die_tag == DW_TAG_class_type)
18337       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18338     }
18339   else if (access == access_protected_node)
18340     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18341   else if (dwarf_version > 2
18342            && context_die->die_tag != DW_TAG_class_type)
18343     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18344 }
18345
18346 /* Generate a DIE for a class member.  */
18347
18348 static void
18349 gen_member_die (tree type, dw_die_ref context_die)
18350 {
18351   tree member;
18352   tree binfo = TYPE_BINFO (type);
18353   dw_die_ref child;
18354
18355   /* If this is not an incomplete type, output descriptions of each of its
18356      members. Note that as we output the DIEs necessary to represent the
18357      members of this record or union type, we will also be trying to output
18358      DIEs to represent the *types* of those members. However the `type'
18359      function (above) will specifically avoid generating type DIEs for member
18360      types *within* the list of member DIEs for this (containing) type except
18361      for those types (of members) which are explicitly marked as also being
18362      members of this (containing) type themselves.  The g++ front- end can
18363      force any given type to be treated as a member of some other (containing)
18364      type by setting the TYPE_CONTEXT of the given (member) type to point to
18365      the TREE node representing the appropriate (containing) type.  */
18366
18367   /* First output info about the base classes.  */
18368   if (binfo)
18369     {
18370       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18371       int i;
18372       tree base;
18373
18374       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18375         gen_inheritance_die (base,
18376                              (accesses ? VEC_index (tree, accesses, i)
18377                               : access_public_node), context_die);
18378     }
18379
18380   /* Now output info about the data members and type members.  */
18381   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18382     {
18383       /* If we thought we were generating minimal debug info for TYPE
18384          and then changed our minds, some of the member declarations
18385          may have already been defined.  Don't define them again, but
18386          do put them in the right order.  */
18387
18388       child = lookup_decl_die (member);
18389       if (child)
18390         splice_child_die (context_die, child);
18391       else
18392         gen_decl_die (member, NULL, context_die);
18393     }
18394
18395   /* Now output info about the function members (if any).  */
18396   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18397     {
18398       /* Don't include clones in the member list.  */
18399       if (DECL_ABSTRACT_ORIGIN (member))
18400         continue;
18401
18402       child = lookup_decl_die (member);
18403       if (child)
18404         splice_child_die (context_die, child);
18405       else
18406         gen_decl_die (member, NULL, context_die);
18407     }
18408 }
18409
18410 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18411    is set, we pretend that the type was never defined, so we only get the
18412    member DIEs needed by later specification DIEs.  */
18413
18414 static void
18415 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18416                                 enum debug_info_usage usage)
18417 {
18418   dw_die_ref type_die = lookup_type_die (type);
18419   dw_die_ref scope_die = 0;
18420   int nested = 0;
18421   int complete = (TYPE_SIZE (type)
18422                   && (! TYPE_STUB_DECL (type)
18423                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18424   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18425   complete = complete && should_emit_struct_debug (type, usage);
18426
18427   if (type_die && ! complete)
18428     return;
18429
18430   if (TYPE_CONTEXT (type) != NULL_TREE
18431       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18432           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18433     nested = 1;
18434
18435   scope_die = scope_die_for (type, context_die);
18436
18437   if (! type_die || (nested && is_cu_die (scope_die)))
18438     /* First occurrence of type or toplevel definition of nested class.  */
18439     {
18440       dw_die_ref old_die = type_die;
18441
18442       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18443                           ? record_type_tag (type) : DW_TAG_union_type,
18444                           scope_die, type);
18445       equate_type_number_to_die (type, type_die);
18446       if (old_die)
18447         add_AT_specification (type_die, old_die);
18448       else
18449         {
18450           add_name_attribute (type_die, type_tag (type));
18451           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18452           if (TYPE_ARTIFICIAL (type))
18453             add_AT_flag (type_die, DW_AT_artificial, 1);
18454         }
18455     }
18456   else
18457     remove_AT (type_die, DW_AT_declaration);
18458
18459   /* Generate child dies for template paramaters.  */
18460   if (debug_info_level > DINFO_LEVEL_TERSE
18461       && COMPLETE_TYPE_P (type))
18462     schedule_generic_params_dies_gen (type);
18463
18464   /* If this type has been completed, then give it a byte_size attribute and
18465      then give a list of members.  */
18466   if (complete && !ns_decl)
18467     {
18468       /* Prevent infinite recursion in cases where the type of some member of
18469          this type is expressed in terms of this type itself.  */
18470       TREE_ASM_WRITTEN (type) = 1;
18471       add_byte_size_attribute (type_die, type);
18472       if (TYPE_STUB_DECL (type) != NULL_TREE)
18473         {
18474           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18475           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18476         }
18477
18478       /* If the first reference to this type was as the return type of an
18479          inline function, then it may not have a parent.  Fix this now.  */
18480       if (type_die->die_parent == NULL)
18481         add_child_die (scope_die, type_die);
18482
18483       push_decl_scope (type);
18484       gen_member_die (type, type_die);
18485       pop_decl_scope ();
18486
18487       /* GNU extension: Record what type our vtable lives in.  */
18488       if (TYPE_VFIELD (type))
18489         {
18490           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18491
18492           gen_type_die (vtype, context_die);
18493           add_AT_die_ref (type_die, DW_AT_containing_type,
18494                           lookup_type_die (vtype));
18495         }
18496     }
18497   else
18498     {
18499       add_AT_flag (type_die, DW_AT_declaration, 1);
18500
18501       /* We don't need to do this for function-local types.  */
18502       if (TYPE_STUB_DECL (type)
18503           && ! decl_function_context (TYPE_STUB_DECL (type)))
18504         VEC_safe_push (tree, gc, incomplete_types, type);
18505     }
18506
18507   if (get_AT (type_die, DW_AT_name))
18508     add_pubtype (type, type_die);
18509 }
18510
18511 /* Generate a DIE for a subroutine _type_.  */
18512
18513 static void
18514 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18515 {
18516   tree return_type = TREE_TYPE (type);
18517   dw_die_ref subr_die
18518     = new_die (DW_TAG_subroutine_type,
18519                scope_die_for (type, context_die), type);
18520
18521   equate_type_number_to_die (type, subr_die);
18522   add_prototyped_attribute (subr_die, type);
18523   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18524   gen_formal_types_die (type, subr_die);
18525
18526   if (get_AT (subr_die, DW_AT_name))
18527     add_pubtype (type, subr_die);
18528 }
18529
18530 /* Generate a DIE for a type definition.  */
18531
18532 static void
18533 gen_typedef_die (tree decl, dw_die_ref context_die)
18534 {
18535   dw_die_ref type_die;
18536   tree origin;
18537
18538   if (TREE_ASM_WRITTEN (decl))
18539     return;
18540
18541   TREE_ASM_WRITTEN (decl) = 1;
18542   type_die = new_die (DW_TAG_typedef, context_die, decl);
18543   origin = decl_ultimate_origin (decl);
18544   if (origin != NULL)
18545     add_abstract_origin_attribute (type_die, origin);
18546   else
18547     {
18548       tree type;
18549
18550       add_name_and_src_coords_attributes (type_die, decl);
18551       if (DECL_ORIGINAL_TYPE (decl))
18552         {
18553           type = DECL_ORIGINAL_TYPE (decl);
18554
18555           gcc_assert (type != TREE_TYPE (decl));
18556           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18557         }
18558       else
18559         {
18560           type = TREE_TYPE (decl);
18561
18562           if (is_naming_typedef_decl (TYPE_NAME (type)))
18563             {
18564               /* Here, we are in the case of decl being a typedef naming
18565                  an anonymous type, e.g:
18566                      typedef struct {...} foo;
18567                  In that case TREE_TYPE (decl) is not a typedef variant
18568                  type and TYPE_NAME of the anonymous type is set to the
18569                  TYPE_DECL of the typedef. This construct is emitted by
18570                  the C++ FE.
18571
18572                  TYPE is the anonymous struct named by the typedef
18573                  DECL. As we need the DW_AT_type attribute of the
18574                  DW_TAG_typedef to point to the DIE of TYPE, let's
18575                  generate that DIE right away. add_type_attribute
18576                  called below will then pick (via lookup_type_die) that
18577                  anonymous struct DIE.  */
18578               if (!TREE_ASM_WRITTEN (type))
18579                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18580
18581               /* This is a GNU Extension.  We are adding a
18582                  DW_AT_linkage_name attribute to the DIE of the
18583                  anonymous struct TYPE.  The value of that attribute
18584                  is the name of the typedef decl naming the anonymous
18585                  struct.  This greatly eases the work of consumers of
18586                  this debug info.  */
18587               add_linkage_attr (lookup_type_die (type), decl);
18588             }
18589         }
18590
18591       add_type_attribute (type_die, type, TREE_READONLY (decl),
18592                           TREE_THIS_VOLATILE (decl), context_die);
18593
18594       if (is_naming_typedef_decl (decl))
18595         /* We want that all subsequent calls to lookup_type_die with
18596            TYPE in argument yield the DW_TAG_typedef we have just
18597            created.  */
18598         equate_type_number_to_die (type, type_die);
18599
18600       add_accessibility_attribute (type_die, decl);
18601     }
18602
18603   if (DECL_ABSTRACT (decl))
18604     equate_decl_number_to_die (decl, type_die);
18605
18606   if (get_AT (type_die, DW_AT_name))
18607     add_pubtype (decl, type_die);
18608 }
18609
18610 /* Generate a DIE for a struct, class, enum or union type.  */
18611
18612 static void
18613 gen_tagged_type_die (tree type,
18614                      dw_die_ref context_die,
18615                      enum debug_info_usage usage)
18616 {
18617   int need_pop;
18618
18619   if (type == NULL_TREE
18620       || !is_tagged_type (type))
18621     return;
18622
18623   /* If this is a nested type whose containing class hasn't been written
18624      out yet, writing it out will cover this one, too.  This does not apply
18625      to instantiations of member class templates; they need to be added to
18626      the containing class as they are generated.  FIXME: This hurts the
18627      idea of combining type decls from multiple TUs, since we can't predict
18628      what set of template instantiations we'll get.  */
18629   if (TYPE_CONTEXT (type)
18630       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18631       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
18632     {
18633       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
18634
18635       if (TREE_ASM_WRITTEN (type))
18636         return;
18637
18638       /* If that failed, attach ourselves to the stub.  */
18639       push_decl_scope (TYPE_CONTEXT (type));
18640       context_die = lookup_type_die (TYPE_CONTEXT (type));
18641       need_pop = 1;
18642     }
18643   else if (TYPE_CONTEXT (type) != NULL_TREE
18644            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
18645     {
18646       /* If this type is local to a function that hasn't been written
18647          out yet, use a NULL context for now; it will be fixed up in
18648          decls_for_scope.  */
18649       context_die = lookup_decl_die (TYPE_CONTEXT (type));
18650       /* A declaration DIE doesn't count; nested types need to go in the
18651          specification.  */
18652       if (context_die && is_declaration_die (context_die))
18653         context_die = NULL;
18654       need_pop = 0;
18655     }
18656   else
18657     {
18658       context_die = declare_in_namespace (type, context_die);
18659       need_pop = 0;
18660     }
18661
18662   if (TREE_CODE (type) == ENUMERAL_TYPE)
18663     {
18664       /* This might have been written out by the call to
18665          declare_in_namespace.  */
18666       if (!TREE_ASM_WRITTEN (type))
18667         gen_enumeration_type_die (type, context_die);
18668     }
18669   else
18670     gen_struct_or_union_type_die (type, context_die, usage);
18671
18672   if (need_pop)
18673     pop_decl_scope ();
18674
18675   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
18676      it up if it is ever completed.  gen_*_type_die will set it for us
18677      when appropriate.  */
18678 }
18679
18680 /* Generate a type description DIE.  */
18681
18682 static void
18683 gen_type_die_with_usage (tree type, dw_die_ref context_die,
18684                          enum debug_info_usage usage)
18685 {
18686   struct array_descr_info info;
18687
18688   if (type == NULL_TREE || type == error_mark_node)
18689     return;
18690
18691   if (TYPE_NAME (type) != NULL_TREE
18692       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
18693       && is_redundant_typedef (TYPE_NAME (type))
18694       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
18695     /* The DECL of this type is a typedef we don't want to emit debug
18696        info for but we want debug info for its underlying typedef.
18697        This can happen for e.g, the injected-class-name of a C++
18698        type.  */
18699     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
18700
18701   /* If TYPE is a typedef type variant, let's generate debug info
18702      for the parent typedef which TYPE is a type of.  */
18703   if (typedef_variant_p (type))
18704     {
18705       if (TREE_ASM_WRITTEN (type))
18706         return;
18707
18708       /* Prevent broken recursion; we can't hand off to the same type.  */
18709       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
18710
18711       /* Use the DIE of the containing namespace as the parent DIE of
18712          the type description DIE we want to generate.  */
18713       if (DECL_CONTEXT (TYPE_NAME (type))
18714           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18715         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18716
18717       TREE_ASM_WRITTEN (type) = 1;
18718
18719       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18720       return;
18721     }
18722
18723   /* If type is an anonymous tagged type named by a typedef, let's
18724      generate debug info for the typedef.  */
18725   if (is_naming_typedef_decl (TYPE_NAME (type)))
18726     {
18727       /* Use the DIE of the containing namespace as the parent DIE of
18728          the type description DIE we want to generate.  */
18729       if (DECL_CONTEXT (TYPE_NAME (type))
18730           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
18731         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
18732       
18733       gen_decl_die (TYPE_NAME (type), NULL, context_die);
18734       return;
18735     }
18736
18737   /* If this is an array type with hidden descriptor, handle it first.  */
18738   if (!TREE_ASM_WRITTEN (type)
18739       && lang_hooks.types.get_array_descr_info
18740       && lang_hooks.types.get_array_descr_info (type, &info)
18741       && (dwarf_version >= 3 || !dwarf_strict))
18742     {
18743       gen_descr_array_type_die (type, &info, context_die);
18744       TREE_ASM_WRITTEN (type) = 1;
18745       return;
18746     }
18747
18748   /* We are going to output a DIE to represent the unqualified version
18749      of this type (i.e. without any const or volatile qualifiers) so
18750      get the main variant (i.e. the unqualified version) of this type
18751      now.  (Vectors are special because the debugging info is in the
18752      cloned type itself).  */
18753   if (TREE_CODE (type) != VECTOR_TYPE)
18754     type = type_main_variant (type);
18755
18756   if (TREE_ASM_WRITTEN (type))
18757     return;
18758
18759   switch (TREE_CODE (type))
18760     {
18761     case ERROR_MARK:
18762       break;
18763
18764     case POINTER_TYPE:
18765     case REFERENCE_TYPE:
18766       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
18767          ensures that the gen_type_die recursion will terminate even if the
18768          type is recursive.  Recursive types are possible in Ada.  */
18769       /* ??? We could perhaps do this for all types before the switch
18770          statement.  */
18771       TREE_ASM_WRITTEN (type) = 1;
18772
18773       /* For these types, all that is required is that we output a DIE (or a
18774          set of DIEs) to represent the "basis" type.  */
18775       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18776                                 DINFO_USAGE_IND_USE);
18777       break;
18778
18779     case OFFSET_TYPE:
18780       /* This code is used for C++ pointer-to-data-member types.
18781          Output a description of the relevant class type.  */
18782       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
18783                                         DINFO_USAGE_IND_USE);
18784
18785       /* Output a description of the type of the object pointed to.  */
18786       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18787                                         DINFO_USAGE_IND_USE);
18788
18789       /* Now output a DIE to represent this pointer-to-data-member type
18790          itself.  */
18791       gen_ptr_to_mbr_type_die (type, context_die);
18792       break;
18793
18794     case FUNCTION_TYPE:
18795       /* Force out return type (in case it wasn't forced out already).  */
18796       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18797                                         DINFO_USAGE_DIR_USE);
18798       gen_subroutine_type_die (type, context_die);
18799       break;
18800
18801     case METHOD_TYPE:
18802       /* Force out return type (in case it wasn't forced out already).  */
18803       gen_type_die_with_usage (TREE_TYPE (type), context_die,
18804                                         DINFO_USAGE_DIR_USE);
18805       gen_subroutine_type_die (type, context_die);
18806       break;
18807
18808     case ARRAY_TYPE:
18809       gen_array_type_die (type, context_die);
18810       break;
18811
18812     case VECTOR_TYPE:
18813       gen_array_type_die (type, context_die);
18814       break;
18815
18816     case ENUMERAL_TYPE:
18817     case RECORD_TYPE:
18818     case UNION_TYPE:
18819     case QUAL_UNION_TYPE:
18820       gen_tagged_type_die (type, context_die, usage);
18821       return;
18822
18823     case VOID_TYPE:
18824     case INTEGER_TYPE:
18825     case REAL_TYPE:
18826     case FIXED_POINT_TYPE:
18827     case COMPLEX_TYPE:
18828     case BOOLEAN_TYPE:
18829       /* No DIEs needed for fundamental types.  */
18830       break;
18831
18832     case NULLPTR_TYPE:
18833     case LANG_TYPE:
18834       /* Just use DW_TAG_unspecified_type.  */
18835       {
18836         dw_die_ref type_die = lookup_type_die (type);
18837         if (type_die == NULL)
18838           {
18839             tree name = TYPE_NAME (type);
18840             if (TREE_CODE (name) == TYPE_DECL)
18841               name = DECL_NAME (name);
18842             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
18843             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
18844             equate_type_number_to_die (type, type_die);
18845           }
18846       }
18847       break;
18848
18849     default:
18850       gcc_unreachable ();
18851     }
18852
18853   TREE_ASM_WRITTEN (type) = 1;
18854 }
18855
18856 static void
18857 gen_type_die (tree type, dw_die_ref context_die)
18858 {
18859   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
18860 }
18861
18862 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
18863    things which are local to the given block.  */
18864
18865 static void
18866 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
18867 {
18868   int must_output_die = 0;
18869   bool inlined_func;
18870
18871   /* Ignore blocks that are NULL.  */
18872   if (stmt == NULL_TREE)
18873     return;
18874
18875   inlined_func = inlined_function_outer_scope_p (stmt);
18876
18877   /* If the block is one fragment of a non-contiguous block, do not
18878      process the variables, since they will have been done by the
18879      origin block.  Do process subblocks.  */
18880   if (BLOCK_FRAGMENT_ORIGIN (stmt))
18881     {
18882       tree sub;
18883
18884       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
18885         gen_block_die (sub, context_die, depth + 1);
18886
18887       return;
18888     }
18889
18890   /* Determine if we need to output any Dwarf DIEs at all to represent this
18891      block.  */
18892   if (inlined_func)
18893     /* The outer scopes for inlinings *must* always be represented.  We
18894        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
18895     must_output_die = 1;
18896   else
18897     {
18898       /* Determine if this block directly contains any "significant"
18899          local declarations which we will need to output DIEs for.  */
18900       if (debug_info_level > DINFO_LEVEL_TERSE)
18901         /* We are not in terse mode so *any* local declaration counts
18902            as being a "significant" one.  */
18903         must_output_die = ((BLOCK_VARS (stmt) != NULL
18904                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
18905                            && (TREE_USED (stmt)
18906                                || TREE_ASM_WRITTEN (stmt)
18907                                || BLOCK_ABSTRACT (stmt)));
18908       else if ((TREE_USED (stmt)
18909                 || TREE_ASM_WRITTEN (stmt)
18910                 || BLOCK_ABSTRACT (stmt))
18911                && !dwarf2out_ignore_block (stmt))
18912         must_output_die = 1;
18913     }
18914
18915   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
18916      DIE for any block which contains no significant local declarations at
18917      all.  Rather, in such cases we just call `decls_for_scope' so that any
18918      needed Dwarf info for any sub-blocks will get properly generated. Note
18919      that in terse mode, our definition of what constitutes a "significant"
18920      local declaration gets restricted to include only inlined function
18921      instances and local (nested) function definitions.  */
18922   if (must_output_die)
18923     {
18924       if (inlined_func)
18925         {
18926           /* If STMT block is abstract, that means we have been called
18927              indirectly from dwarf2out_abstract_function.
18928              That function rightfully marks the descendent blocks (of
18929              the abstract function it is dealing with) as being abstract,
18930              precisely to prevent us from emitting any
18931              DW_TAG_inlined_subroutine DIE as a descendent
18932              of an abstract function instance. So in that case, we should
18933              not call gen_inlined_subroutine_die.
18934
18935              Later though, when cgraph asks dwarf2out to emit info
18936              for the concrete instance of the function decl into which
18937              the concrete instance of STMT got inlined, the later will lead
18938              to the generation of a DW_TAG_inlined_subroutine DIE.  */
18939           if (! BLOCK_ABSTRACT (stmt))
18940             gen_inlined_subroutine_die (stmt, context_die, depth);
18941         }
18942       else
18943         gen_lexical_block_die (stmt, context_die, depth);
18944     }
18945   else
18946     decls_for_scope (stmt, context_die, depth);
18947 }
18948
18949 /* Process variable DECL (or variable with origin ORIGIN) within
18950    block STMT and add it to CONTEXT_DIE.  */
18951 static void
18952 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
18953 {
18954   dw_die_ref die;
18955   tree decl_or_origin = decl ? decl : origin;
18956
18957   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
18958     die = lookup_decl_die (decl_or_origin);
18959   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
18960            && TYPE_DECL_IS_STUB (decl_or_origin))
18961     die = lookup_type_die (TREE_TYPE (decl_or_origin));
18962   else
18963     die = NULL;
18964
18965   if (die != NULL && die->die_parent == NULL)
18966     add_child_die (context_die, die);
18967   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
18968     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
18969                                          stmt, context_die);
18970   else
18971     gen_decl_die (decl, origin, context_die);
18972 }
18973
18974 /* Generate all of the decls declared within a given scope and (recursively)
18975    all of its sub-blocks.  */
18976
18977 static void
18978 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
18979 {
18980   tree decl;
18981   unsigned int i;
18982   tree subblocks;
18983
18984   /* Ignore NULL blocks.  */
18985   if (stmt == NULL_TREE)
18986     return;
18987
18988   /* Output the DIEs to represent all of the data objects and typedefs
18989      declared directly within this block but not within any nested
18990      sub-blocks.  Also, nested function and tag DIEs have been
18991      generated with a parent of NULL; fix that up now.  */
18992   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
18993     process_scope_var (stmt, decl, NULL_TREE, context_die);
18994   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
18995     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
18996                        context_die);
18997
18998   /* If we're at -g1, we're not interested in subblocks.  */
18999   if (debug_info_level <= DINFO_LEVEL_TERSE)
19000     return;
19001
19002   /* Output the DIEs to represent all sub-blocks (and the items declared
19003      therein) of this block.  */
19004   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19005        subblocks != NULL;
19006        subblocks = BLOCK_CHAIN (subblocks))
19007     gen_block_die (subblocks, context_die, depth + 1);
19008 }
19009
19010 /* Is this a typedef we can avoid emitting?  */
19011
19012 static inline int
19013 is_redundant_typedef (const_tree decl)
19014 {
19015   if (TYPE_DECL_IS_STUB (decl))
19016     return 1;
19017
19018   if (DECL_ARTIFICIAL (decl)
19019       && DECL_CONTEXT (decl)
19020       && is_tagged_type (DECL_CONTEXT (decl))
19021       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19022       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19023     /* Also ignore the artificial member typedef for the class name.  */
19024     return 1;
19025
19026   return 0;
19027 }
19028
19029 /* Return TRUE if TYPE is a typedef that names a type for linkage
19030    purposes. This kind of typedefs is produced by the C++ FE for
19031    constructs like:
19032
19033    typedef struct {...} foo;
19034
19035    In that case, there is no typedef variant type produced for foo.
19036    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19037    struct type.  */
19038
19039 static bool
19040 is_naming_typedef_decl (const_tree decl)
19041 {
19042   if (decl == NULL_TREE
19043       || TREE_CODE (decl) != TYPE_DECL
19044       || !is_tagged_type (TREE_TYPE (decl))
19045       || DECL_IS_BUILTIN (decl)
19046       || is_redundant_typedef (decl)
19047       /* It looks like Ada produces TYPE_DECLs that are very similar
19048          to C++ naming typedefs but that have different
19049          semantics. Let's be specific to c++ for now.  */
19050       || !is_cxx ())
19051     return FALSE;
19052
19053   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19054           && TYPE_NAME (TREE_TYPE (decl)) == decl
19055           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19056               != TYPE_NAME (TREE_TYPE (decl))));
19057 }
19058
19059 /* Returns the DIE for a context.  */
19060
19061 static inline dw_die_ref
19062 get_context_die (tree context)
19063 {
19064   if (context)
19065     {
19066       /* Find die that represents this context.  */
19067       if (TYPE_P (context))
19068         {
19069           context = TYPE_MAIN_VARIANT (context);
19070           return strip_naming_typedef (context, force_type_die (context));
19071         }
19072       else
19073         return force_decl_die (context);
19074     }
19075   return comp_unit_die ();
19076 }
19077
19078 /* Returns the DIE for decl.  A DIE will always be returned.  */
19079
19080 static dw_die_ref
19081 force_decl_die (tree decl)
19082 {
19083   dw_die_ref decl_die;
19084   unsigned saved_external_flag;
19085   tree save_fn = NULL_TREE;
19086   decl_die = lookup_decl_die (decl);
19087   if (!decl_die)
19088     {
19089       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19090
19091       decl_die = lookup_decl_die (decl);
19092       if (decl_die)
19093         return decl_die;
19094
19095       switch (TREE_CODE (decl))
19096         {
19097         case FUNCTION_DECL:
19098           /* Clear current_function_decl, so that gen_subprogram_die thinks
19099              that this is a declaration. At this point, we just want to force
19100              declaration die.  */
19101           save_fn = current_function_decl;
19102           current_function_decl = NULL_TREE;
19103           gen_subprogram_die (decl, context_die);
19104           current_function_decl = save_fn;
19105           break;
19106
19107         case VAR_DECL:
19108           /* Set external flag to force declaration die. Restore it after
19109            gen_decl_die() call.  */
19110           saved_external_flag = DECL_EXTERNAL (decl);
19111           DECL_EXTERNAL (decl) = 1;
19112           gen_decl_die (decl, NULL, context_die);
19113           DECL_EXTERNAL (decl) = saved_external_flag;
19114           break;
19115
19116         case NAMESPACE_DECL:
19117           if (dwarf_version >= 3 || !dwarf_strict)
19118             dwarf2out_decl (decl);
19119           else
19120             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19121             decl_die = comp_unit_die ();
19122           break;
19123
19124         case TRANSLATION_UNIT_DECL:
19125           decl_die = comp_unit_die ();
19126           break;
19127
19128         default:
19129           gcc_unreachable ();
19130         }
19131
19132       /* We should be able to find the DIE now.  */
19133       if (!decl_die)
19134         decl_die = lookup_decl_die (decl);
19135       gcc_assert (decl_die);
19136     }
19137
19138   return decl_die;
19139 }
19140
19141 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19142    always returned.  */
19143
19144 static dw_die_ref
19145 force_type_die (tree type)
19146 {
19147   dw_die_ref type_die;
19148
19149   type_die = lookup_type_die (type);
19150   if (!type_die)
19151     {
19152       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19153
19154       type_die = modified_type_die (type, TYPE_READONLY (type),
19155                                     TYPE_VOLATILE (type), context_die);
19156       gcc_assert (type_die);
19157     }
19158   return type_die;
19159 }
19160
19161 /* Force out any required namespaces to be able to output DECL,
19162    and return the new context_die for it, if it's changed.  */
19163
19164 static dw_die_ref
19165 setup_namespace_context (tree thing, dw_die_ref context_die)
19166 {
19167   tree context = (DECL_P (thing)
19168                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19169   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19170     /* Force out the namespace.  */
19171     context_die = force_decl_die (context);
19172
19173   return context_die;
19174 }
19175
19176 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19177    type) within its namespace, if appropriate.
19178
19179    For compatibility with older debuggers, namespace DIEs only contain
19180    declarations; all definitions are emitted at CU scope.  */
19181
19182 static dw_die_ref
19183 declare_in_namespace (tree thing, dw_die_ref context_die)
19184 {
19185   dw_die_ref ns_context;
19186
19187   if (debug_info_level <= DINFO_LEVEL_TERSE)
19188     return context_die;
19189
19190   /* If this decl is from an inlined function, then don't try to emit it in its
19191      namespace, as we will get confused.  It would have already been emitted
19192      when the abstract instance of the inline function was emitted anyways.  */
19193   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19194     return context_die;
19195
19196   ns_context = setup_namespace_context (thing, context_die);
19197
19198   if (ns_context != context_die)
19199     {
19200       if (is_fortran ())
19201         return ns_context;
19202       if (DECL_P (thing))
19203         gen_decl_die (thing, NULL, ns_context);
19204       else
19205         gen_type_die (thing, ns_context);
19206     }
19207   return context_die;
19208 }
19209
19210 /* Generate a DIE for a namespace or namespace alias.  */
19211
19212 static void
19213 gen_namespace_die (tree decl, dw_die_ref context_die)
19214 {
19215   dw_die_ref namespace_die;
19216
19217   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19218      they are an alias of.  */
19219   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19220     {
19221       /* Output a real namespace or module.  */
19222       context_die = setup_namespace_context (decl, comp_unit_die ());
19223       namespace_die = new_die (is_fortran ()
19224                                ? DW_TAG_module : DW_TAG_namespace,
19225                                context_die, decl);
19226       /* For Fortran modules defined in different CU don't add src coords.  */
19227       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19228         {
19229           const char *name = dwarf2_name (decl, 0);
19230           if (name)
19231             add_name_attribute (namespace_die, name);
19232         }
19233       else
19234         add_name_and_src_coords_attributes (namespace_die, decl);
19235       if (DECL_EXTERNAL (decl))
19236         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19237       equate_decl_number_to_die (decl, namespace_die);
19238     }
19239   else
19240     {
19241       /* Output a namespace alias.  */
19242
19243       /* Force out the namespace we are an alias of, if necessary.  */
19244       dw_die_ref origin_die
19245         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19246
19247       if (DECL_FILE_SCOPE_P (decl)
19248           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19249         context_die = setup_namespace_context (decl, comp_unit_die ());
19250       /* Now create the namespace alias DIE.  */
19251       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19252       add_name_and_src_coords_attributes (namespace_die, decl);
19253       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19254       equate_decl_number_to_die (decl, namespace_die);
19255     }
19256 }
19257
19258 /* Generate Dwarf debug information for a decl described by DECL.
19259    The return value is currently only meaningful for PARM_DECLs,
19260    for all other decls it returns NULL.  */
19261
19262 static dw_die_ref
19263 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19264 {
19265   tree decl_or_origin = decl ? decl : origin;
19266   tree class_origin = NULL, ultimate_origin;
19267
19268   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19269     return NULL;
19270
19271   switch (TREE_CODE (decl_or_origin))
19272     {
19273     case ERROR_MARK:
19274       break;
19275
19276     case CONST_DECL:
19277       if (!is_fortran () && !is_ada ())
19278         {
19279           /* The individual enumerators of an enum type get output when we output
19280              the Dwarf representation of the relevant enum type itself.  */
19281           break;
19282         }
19283
19284       /* Emit its type.  */
19285       gen_type_die (TREE_TYPE (decl), context_die);
19286
19287       /* And its containing namespace.  */
19288       context_die = declare_in_namespace (decl, context_die);
19289
19290       gen_const_die (decl, context_die);
19291       break;
19292
19293     case FUNCTION_DECL:
19294       /* Don't output any DIEs to represent mere function declarations,
19295          unless they are class members or explicit block externs.  */
19296       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19297           && DECL_FILE_SCOPE_P (decl_or_origin)
19298           && (current_function_decl == NULL_TREE
19299               || DECL_ARTIFICIAL (decl_or_origin)))
19300         break;
19301
19302 #if 0
19303       /* FIXME */
19304       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19305          on local redeclarations of global functions.  That seems broken.  */
19306       if (current_function_decl != decl)
19307         /* This is only a declaration.  */;
19308 #endif
19309
19310       /* If we're emitting a clone, emit info for the abstract instance.  */
19311       if (origin || DECL_ORIGIN (decl) != decl)
19312         dwarf2out_abstract_function (origin
19313                                      ? DECL_ORIGIN (origin)
19314                                      : DECL_ABSTRACT_ORIGIN (decl));
19315
19316       /* If we're emitting an out-of-line copy of an inline function,
19317          emit info for the abstract instance and set up to refer to it.  */
19318       else if (cgraph_function_possibly_inlined_p (decl)
19319                && ! DECL_ABSTRACT (decl)
19320                && ! class_or_namespace_scope_p (context_die)
19321                /* dwarf2out_abstract_function won't emit a die if this is just
19322                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19323                   that case, because that works only if we have a die.  */
19324                && DECL_INITIAL (decl) != NULL_TREE)
19325         {
19326           dwarf2out_abstract_function (decl);
19327           set_decl_origin_self (decl);
19328         }
19329
19330       /* Otherwise we're emitting the primary DIE for this decl.  */
19331       else if (debug_info_level > DINFO_LEVEL_TERSE)
19332         {
19333           /* Before we describe the FUNCTION_DECL itself, make sure that we
19334              have its containing type.  */
19335           if (!origin)
19336             origin = decl_class_context (decl);
19337           if (origin != NULL_TREE)
19338             gen_type_die (origin, context_die);
19339
19340           /* And its return type.  */
19341           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19342
19343           /* And its virtual context.  */
19344           if (DECL_VINDEX (decl) != NULL_TREE)
19345             gen_type_die (DECL_CONTEXT (decl), context_die);
19346
19347           /* Make sure we have a member DIE for decl.  */
19348           if (origin != NULL_TREE)
19349             gen_type_die_for_member (origin, decl, context_die);
19350
19351           /* And its containing namespace.  */
19352           context_die = declare_in_namespace (decl, context_die);
19353         }
19354
19355       /* Now output a DIE to represent the function itself.  */
19356       if (decl)
19357         gen_subprogram_die (decl, context_die);
19358       break;
19359
19360     case TYPE_DECL:
19361       /* If we are in terse mode, don't generate any DIEs to represent any
19362          actual typedefs.  */
19363       if (debug_info_level <= DINFO_LEVEL_TERSE)
19364         break;
19365
19366       /* In the special case of a TYPE_DECL node representing the declaration
19367          of some type tag, if the given TYPE_DECL is marked as having been
19368          instantiated from some other (original) TYPE_DECL node (e.g. one which
19369          was generated within the original definition of an inline function) we
19370          used to generate a special (abbreviated) DW_TAG_structure_type,
19371          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19372          should be actually referencing those DIEs, as variable DIEs with that
19373          type would be emitted already in the abstract origin, so it was always
19374          removed during unused type prunning.  Don't add anything in this
19375          case.  */
19376       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19377         break;
19378
19379       if (is_redundant_typedef (decl))
19380         gen_type_die (TREE_TYPE (decl), context_die);
19381       else
19382         /* Output a DIE to represent the typedef itself.  */
19383         gen_typedef_die (decl, context_die);
19384       break;
19385
19386     case LABEL_DECL:
19387       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19388         gen_label_die (decl, context_die);
19389       break;
19390
19391     case VAR_DECL:
19392     case RESULT_DECL:
19393       /* If we are in terse mode, don't generate any DIEs to represent any
19394          variable declarations or definitions.  */
19395       if (debug_info_level <= DINFO_LEVEL_TERSE)
19396         break;
19397
19398       /* Output any DIEs that are needed to specify the type of this data
19399          object.  */
19400       if (decl_by_reference_p (decl_or_origin))
19401         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19402       else
19403         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19404
19405       /* And its containing type.  */
19406       class_origin = decl_class_context (decl_or_origin);
19407       if (class_origin != NULL_TREE)
19408         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19409
19410       /* And its containing namespace.  */
19411       context_die = declare_in_namespace (decl_or_origin, context_die);
19412
19413       /* Now output the DIE to represent the data object itself.  This gets
19414          complicated because of the possibility that the VAR_DECL really
19415          represents an inlined instance of a formal parameter for an inline
19416          function.  */
19417       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19418       if (ultimate_origin != NULL_TREE
19419           && TREE_CODE (ultimate_origin) == PARM_DECL)
19420         gen_formal_parameter_die (decl, origin,
19421                                   true /* Emit name attribute.  */,
19422                                   context_die);
19423       else
19424         gen_variable_die (decl, origin, context_die);
19425       break;
19426
19427     case FIELD_DECL:
19428       /* Ignore the nameless fields that are used to skip bits but handle C++
19429          anonymous unions and structs.  */
19430       if (DECL_NAME (decl) != NULL_TREE
19431           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19432           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19433         {
19434           gen_type_die (member_declared_type (decl), context_die);
19435           gen_field_die (decl, context_die);
19436         }
19437       break;
19438
19439     case PARM_DECL:
19440       if (DECL_BY_REFERENCE (decl_or_origin))
19441         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19442       else
19443         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19444       return gen_formal_parameter_die (decl, origin,
19445                                        true /* Emit name attribute.  */,
19446                                        context_die);
19447
19448     case NAMESPACE_DECL:
19449     case IMPORTED_DECL:
19450       if (dwarf_version >= 3 || !dwarf_strict)
19451         gen_namespace_die (decl, context_die);
19452       break;
19453
19454     default:
19455       /* Probably some frontend-internal decl.  Assume we don't care.  */
19456       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19457       break;
19458     }
19459
19460   return NULL;
19461 }
19462 \f
19463 /* Output debug information for global decl DECL.  Called from toplev.c after
19464    compilation proper has finished.  */
19465
19466 static void
19467 dwarf2out_global_decl (tree decl)
19468 {
19469   /* Output DWARF2 information for file-scope tentative data object
19470      declarations, file-scope (extern) function declarations (which
19471      had no corresponding body) and file-scope tagged type declarations
19472      and definitions which have not yet been forced out.  */
19473   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19474     dwarf2out_decl (decl);
19475 }
19476
19477 /* Output debug information for type decl DECL.  Called from toplev.c
19478    and from language front ends (to record built-in types).  */
19479 static void
19480 dwarf2out_type_decl (tree decl, int local)
19481 {
19482   if (!local)
19483     dwarf2out_decl (decl);
19484 }
19485
19486 /* Output debug information for imported module or decl DECL.
19487    NAME is non-NULL name in the lexical block if the decl has been renamed.
19488    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19489    that DECL belongs to.
19490    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19491 static void
19492 dwarf2out_imported_module_or_decl_1 (tree decl,
19493                                      tree name,
19494                                      tree lexical_block,
19495                                      dw_die_ref lexical_block_die)
19496 {
19497   expanded_location xloc;
19498   dw_die_ref imported_die = NULL;
19499   dw_die_ref at_import_die;
19500
19501   if (TREE_CODE (decl) == IMPORTED_DECL)
19502     {
19503       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19504       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19505       gcc_assert (decl);
19506     }
19507   else
19508     xloc = expand_location (input_location);
19509
19510   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19511     {
19512       at_import_die = force_type_die (TREE_TYPE (decl));
19513       /* For namespace N { typedef void T; } using N::T; base_type_die
19514          returns NULL, but DW_TAG_imported_declaration requires
19515          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19516       if (!at_import_die)
19517         {
19518           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19519           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19520           at_import_die = lookup_type_die (TREE_TYPE (decl));
19521           gcc_assert (at_import_die);
19522         }
19523     }
19524   else
19525     {
19526       at_import_die = lookup_decl_die (decl);
19527       if (!at_import_die)
19528         {
19529           /* If we're trying to avoid duplicate debug info, we may not have
19530              emitted the member decl for this field.  Emit it now.  */
19531           if (TREE_CODE (decl) == FIELD_DECL)
19532             {
19533               tree type = DECL_CONTEXT (decl);
19534
19535               if (TYPE_CONTEXT (type)
19536                   && TYPE_P (TYPE_CONTEXT (type))
19537                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19538                                                 DINFO_USAGE_DIR_USE))
19539                 return;
19540               gen_type_die_for_member (type, decl,
19541                                        get_context_die (TYPE_CONTEXT (type)));
19542             }
19543           at_import_die = force_decl_die (decl);
19544         }
19545     }
19546
19547   if (TREE_CODE (decl) == NAMESPACE_DECL)
19548     {
19549       if (dwarf_version >= 3 || !dwarf_strict)
19550         imported_die = new_die (DW_TAG_imported_module,
19551                                 lexical_block_die,
19552                                 lexical_block);
19553       else
19554         return;
19555     }
19556   else
19557     imported_die = new_die (DW_TAG_imported_declaration,
19558                             lexical_block_die,
19559                             lexical_block);
19560
19561   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19562   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19563   if (name)
19564     add_AT_string (imported_die, DW_AT_name,
19565                    IDENTIFIER_POINTER (name));
19566   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19567 }
19568
19569 /* Output debug information for imported module or decl DECL.
19570    NAME is non-NULL name in context if the decl has been renamed.
19571    CHILD is true if decl is one of the renamed decls as part of
19572    importing whole module.  */
19573
19574 static void
19575 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19576                                    bool child)
19577 {
19578   /* dw_die_ref at_import_die;  */
19579   dw_die_ref scope_die;
19580
19581   if (debug_info_level <= DINFO_LEVEL_TERSE)
19582     return;
19583
19584   gcc_assert (decl);
19585
19586   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
19587      We need decl DIE for reference and scope die. First, get DIE for the decl
19588      itself.  */
19589
19590   /* Get the scope die for decl context. Use comp_unit_die for global module
19591      or decl. If die is not found for non globals, force new die.  */
19592   if (context
19593       && TYPE_P (context)
19594       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
19595     return;
19596
19597   if (!(dwarf_version >= 3 || !dwarf_strict))
19598     return;
19599
19600   scope_die = get_context_die (context);
19601
19602   if (child)
19603     {
19604       gcc_assert (scope_die->die_child);
19605       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
19606       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
19607       scope_die = scope_die->die_child;
19608     }
19609
19610   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
19611   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
19612
19613 }
19614
19615 /* Write the debugging output for DECL.  */
19616
19617 void
19618 dwarf2out_decl (tree decl)
19619 {
19620   dw_die_ref context_die = comp_unit_die ();
19621
19622   switch (TREE_CODE (decl))
19623     {
19624     case ERROR_MARK:
19625       return;
19626
19627     case FUNCTION_DECL:
19628       /* What we would really like to do here is to filter out all mere
19629          file-scope declarations of file-scope functions which are never
19630          referenced later within this translation unit (and keep all of ones
19631          that *are* referenced later on) but we aren't clairvoyant, so we have
19632          no idea which functions will be referenced in the future (i.e. later
19633          on within the current translation unit). So here we just ignore all
19634          file-scope function declarations which are not also definitions.  If
19635          and when the debugger needs to know something about these functions,
19636          it will have to hunt around and find the DWARF information associated
19637          with the definition of the function.
19638
19639          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
19640          nodes represent definitions and which ones represent mere
19641          declarations.  We have to check DECL_INITIAL instead. That's because
19642          the C front-end supports some weird semantics for "extern inline"
19643          function definitions.  These can get inlined within the current
19644          translation unit (and thus, we need to generate Dwarf info for their
19645          abstract instances so that the Dwarf info for the concrete inlined
19646          instances can have something to refer to) but the compiler never
19647          generates any out-of-lines instances of such things (despite the fact
19648          that they *are* definitions).
19649
19650          The important point is that the C front-end marks these "extern
19651          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
19652          them anyway. Note that the C++ front-end also plays some similar games
19653          for inline function definitions appearing within include files which
19654          also contain `#pragma interface' pragmas.  */
19655       if (DECL_INITIAL (decl) == NULL_TREE)
19656         return;
19657
19658       /* If we're a nested function, initially use a parent of NULL; if we're
19659          a plain function, this will be fixed up in decls_for_scope.  If
19660          we're a method, it will be ignored, since we already have a DIE.  */
19661       if (decl_function_context (decl)
19662           /* But if we're in terse mode, we don't care about scope.  */
19663           && debug_info_level > DINFO_LEVEL_TERSE)
19664         context_die = NULL;
19665       break;
19666
19667     case VAR_DECL:
19668       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
19669          declaration and if the declaration was never even referenced from
19670          within this entire compilation unit.  We suppress these DIEs in
19671          order to save space in the .debug section (by eliminating entries
19672          which are probably useless).  Note that we must not suppress
19673          block-local extern declarations (whether used or not) because that
19674          would screw-up the debugger's name lookup mechanism and cause it to
19675          miss things which really ought to be in scope at a given point.  */
19676       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
19677         return;
19678
19679       /* For local statics lookup proper context die.  */
19680       if (TREE_STATIC (decl) && decl_function_context (decl))
19681         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19682
19683       /* If we are in terse mode, don't generate any DIEs to represent any
19684          variable declarations or definitions.  */
19685       if (debug_info_level <= DINFO_LEVEL_TERSE)
19686         return;
19687       break;
19688
19689     case CONST_DECL:
19690       if (debug_info_level <= DINFO_LEVEL_TERSE)
19691         return;
19692       if (!is_fortran () && !is_ada ())
19693         return;
19694       if (TREE_STATIC (decl) && decl_function_context (decl))
19695         context_die = lookup_decl_die (DECL_CONTEXT (decl));
19696       break;
19697
19698     case NAMESPACE_DECL:
19699     case IMPORTED_DECL:
19700       if (debug_info_level <= DINFO_LEVEL_TERSE)
19701         return;
19702       if (lookup_decl_die (decl) != NULL)
19703         return;
19704       break;
19705
19706     case TYPE_DECL:
19707       /* Don't emit stubs for types unless they are needed by other DIEs.  */
19708       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
19709         return;
19710
19711       /* Don't bother trying to generate any DIEs to represent any of the
19712          normal built-in types for the language we are compiling.  */
19713       if (DECL_IS_BUILTIN (decl))
19714         return;
19715
19716       /* If we are in terse mode, don't generate any DIEs for types.  */
19717       if (debug_info_level <= DINFO_LEVEL_TERSE)
19718         return;
19719
19720       /* If we're a function-scope tag, initially use a parent of NULL;
19721          this will be fixed up in decls_for_scope.  */
19722       if (decl_function_context (decl))
19723         context_die = NULL;
19724
19725       break;
19726
19727     default:
19728       return;
19729     }
19730
19731   gen_decl_die (decl, NULL, context_die);
19732 }
19733
19734 /* Write the debugging output for DECL.  */
19735
19736 static void
19737 dwarf2out_function_decl (tree decl)
19738 {
19739   dwarf2out_decl (decl);
19740   call_arg_locations = NULL;
19741   call_arg_loc_last = NULL;
19742   call_site_count = -1;
19743   tail_call_site_count = -1;
19744   VEC_free (dw_die_ref, heap, block_map);
19745   htab_empty (decl_loc_table);
19746   htab_empty (cached_dw_loc_list_table);
19747 }
19748
19749 /* Output a marker (i.e. a label) for the beginning of the generated code for
19750    a lexical block.  */
19751
19752 static void
19753 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
19754                        unsigned int blocknum)
19755 {
19756   switch_to_section (current_function_section ());
19757   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
19758 }
19759
19760 /* Output a marker (i.e. a label) for the end of the generated code for a
19761    lexical block.  */
19762
19763 static void
19764 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
19765 {
19766   switch_to_section (current_function_section ());
19767   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
19768 }
19769
19770 /* Returns nonzero if it is appropriate not to emit any debugging
19771    information for BLOCK, because it doesn't contain any instructions.
19772
19773    Don't allow this for blocks with nested functions or local classes
19774    as we would end up with orphans, and in the presence of scheduling
19775    we may end up calling them anyway.  */
19776
19777 static bool
19778 dwarf2out_ignore_block (const_tree block)
19779 {
19780   tree decl;
19781   unsigned int i;
19782
19783   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
19784     if (TREE_CODE (decl) == FUNCTION_DECL
19785         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19786       return 0;
19787   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
19788     {
19789       decl = BLOCK_NONLOCALIZED_VAR (block, i);
19790       if (TREE_CODE (decl) == FUNCTION_DECL
19791           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
19792       return 0;
19793     }
19794
19795   return 1;
19796 }
19797
19798 /* Hash table routines for file_hash.  */
19799
19800 static int
19801 file_table_eq (const void *p1_p, const void *p2_p)
19802 {
19803   const struct dwarf_file_data *const p1 =
19804     (const struct dwarf_file_data *) p1_p;
19805   const char *const p2 = (const char *) p2_p;
19806   return filename_cmp (p1->filename, p2) == 0;
19807 }
19808
19809 static hashval_t
19810 file_table_hash (const void *p_p)
19811 {
19812   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
19813   return htab_hash_string (p->filename);
19814 }
19815
19816 /* Lookup FILE_NAME (in the list of filenames that we know about here in
19817    dwarf2out.c) and return its "index".  The index of each (known) filename is
19818    just a unique number which is associated with only that one filename.  We
19819    need such numbers for the sake of generating labels (in the .debug_sfnames
19820    section) and references to those files numbers (in the .debug_srcinfo
19821    and.debug_macinfo sections).  If the filename given as an argument is not
19822    found in our current list, add it to the list and assign it the next
19823    available unique index number.  In order to speed up searches, we remember
19824    the index of the filename was looked up last.  This handles the majority of
19825    all searches.  */
19826
19827 static struct dwarf_file_data *
19828 lookup_filename (const char *file_name)
19829 {
19830   void ** slot;
19831   struct dwarf_file_data * created;
19832
19833   /* Check to see if the file name that was searched on the previous
19834      call matches this file name.  If so, return the index.  */
19835   if (file_table_last_lookup
19836       && (file_name == file_table_last_lookup->filename
19837           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
19838     return file_table_last_lookup;
19839
19840   /* Didn't match the previous lookup, search the table.  */
19841   slot = htab_find_slot_with_hash (file_table, file_name,
19842                                    htab_hash_string (file_name), INSERT);
19843   if (*slot)
19844     return (struct dwarf_file_data *) *slot;
19845
19846   created = ggc_alloc_dwarf_file_data ();
19847   created->filename = file_name;
19848   created->emitted_number = 0;
19849   *slot = created;
19850   return created;
19851 }
19852
19853 /* If the assembler will construct the file table, then translate the compiler
19854    internal file table number into the assembler file table number, and emit
19855    a .file directive if we haven't already emitted one yet.  The file table
19856    numbers are different because we prune debug info for unused variables and
19857    types, which may include filenames.  */
19858
19859 static int
19860 maybe_emit_file (struct dwarf_file_data * fd)
19861 {
19862   if (! fd->emitted_number)
19863     {
19864       if (last_emitted_file)
19865         fd->emitted_number = last_emitted_file->emitted_number + 1;
19866       else
19867         fd->emitted_number = 1;
19868       last_emitted_file = fd;
19869
19870       if (DWARF2_ASM_LINE_DEBUG_INFO)
19871         {
19872           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
19873           output_quoted_string (asm_out_file,
19874                                 remap_debug_filename (fd->filename));
19875           fputc ('\n', asm_out_file);
19876         }
19877     }
19878
19879   return fd->emitted_number;
19880 }
19881
19882 /* Schedule generation of a DW_AT_const_value attribute to DIE.
19883    That generation should happen after function debug info has been
19884    generated. The value of the attribute is the constant value of ARG.  */
19885
19886 static void
19887 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
19888 {
19889   die_arg_entry entry;
19890
19891   if (!die || !arg)
19892     return;
19893
19894   if (!tmpl_value_parm_die_table)
19895     tmpl_value_parm_die_table
19896       = VEC_alloc (die_arg_entry, gc, 32);
19897
19898   entry.die = die;
19899   entry.arg = arg;
19900   VEC_safe_push (die_arg_entry, gc,
19901                  tmpl_value_parm_die_table,
19902                  &entry);
19903 }
19904
19905 /* Return TRUE if T is an instance of generic type, FALSE
19906    otherwise.  */
19907
19908 static bool
19909 generic_type_p (tree t)
19910 {
19911   if (t == NULL_TREE || !TYPE_P (t))
19912     return false;
19913   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
19914 }
19915
19916 /* Schedule the generation of the generic parameter dies for the
19917   instance of generic type T. The proper generation itself is later
19918   done by gen_scheduled_generic_parms_dies. */
19919
19920 static void
19921 schedule_generic_params_dies_gen (tree t)
19922 {
19923   if (!generic_type_p (t))
19924     return;
19925
19926   if (generic_type_instances == NULL)
19927     generic_type_instances = VEC_alloc (tree, gc, 256);
19928
19929   VEC_safe_push (tree, gc, generic_type_instances, t);
19930 }
19931
19932 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
19933    by append_entry_to_tmpl_value_parm_die_table. This function must
19934    be called after function DIEs have been generated.  */
19935
19936 static void
19937 gen_remaining_tmpl_value_param_die_attribute (void)
19938 {
19939   if (tmpl_value_parm_die_table)
19940     {
19941       unsigned i;
19942       die_arg_entry *e;
19943
19944       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
19945         tree_add_const_value_attribute (e->die, e->arg);
19946     }
19947 }
19948
19949 /* Generate generic parameters DIEs for instances of generic types
19950    that have been previously scheduled by
19951    schedule_generic_params_dies_gen. This function must be called
19952    after all the types of the CU have been laid out.  */
19953
19954 static void
19955 gen_scheduled_generic_parms_dies (void)
19956 {
19957   unsigned i;
19958   tree t;
19959
19960   if (generic_type_instances == NULL)
19961     return;
19962   
19963   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
19964     gen_generic_params_dies (t);
19965 }
19966
19967
19968 /* Replace DW_AT_name for the decl with name.  */
19969
19970 static void
19971 dwarf2out_set_name (tree decl, tree name)
19972 {
19973   dw_die_ref die;
19974   dw_attr_ref attr;
19975   const char *dname;
19976
19977   die = TYPE_SYMTAB_DIE (decl);
19978   if (!die)
19979     return;
19980
19981   dname = dwarf2_name (name, 0);
19982   if (!dname)
19983     return;
19984
19985   attr = get_AT (die, DW_AT_name);
19986   if (attr)
19987     {
19988       struct indirect_string_node *node;
19989
19990       node = find_AT_string (dname);
19991       /* replace the string.  */
19992       attr->dw_attr_val.v.val_str = node;
19993     }
19994
19995   else
19996     add_name_attribute (die, dname);
19997 }
19998
19999 /* Called by the final INSN scan whenever we see a var location.  We
20000    use it to drop labels in the right places, and throw the location in
20001    our lookup table.  */
20002
20003 static void
20004 dwarf2out_var_location (rtx loc_note)
20005 {
20006   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20007   struct var_loc_node *newloc;
20008   rtx next_real;
20009   static const char *last_label;
20010   static const char *last_postcall_label;
20011   static bool last_in_cold_section_p;
20012   tree decl;
20013   bool var_loc_p;
20014
20015   if (!NOTE_P (loc_note))
20016     {
20017       if (CALL_P (loc_note))
20018         {
20019           call_site_count++;
20020           if (SIBLING_CALL_P (loc_note))
20021             tail_call_site_count++;
20022         }
20023       return;
20024     }
20025
20026   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20027   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20028     return;
20029
20030   next_real = next_real_insn (loc_note);
20031
20032   /* If there are no instructions which would be affected by this note,
20033      don't do anything.  */
20034   if (var_loc_p
20035       && next_real == NULL_RTX
20036       && !NOTE_DURING_CALL_P (loc_note))
20037     return;
20038
20039   if (next_real == NULL_RTX)
20040     next_real = get_last_insn ();
20041
20042   /* If there were any real insns between note we processed last time
20043      and this note (or if it is the first note), clear
20044      last_{,postcall_}label so that they are not reused this time.  */
20045   if (last_var_location_insn == NULL_RTX
20046       || last_var_location_insn != next_real
20047       || last_in_cold_section_p != in_cold_section_p)
20048     {
20049       last_label = NULL;
20050       last_postcall_label = NULL;
20051     }
20052
20053   if (var_loc_p)
20054     {
20055       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20056       newloc = add_var_loc_to_decl (decl, loc_note,
20057                                     NOTE_DURING_CALL_P (loc_note)
20058                                     ? last_postcall_label : last_label);
20059       if (newloc == NULL)
20060         return;
20061     }
20062   else
20063     {
20064       decl = NULL_TREE;
20065       newloc = NULL;
20066     }
20067
20068   /* If there were no real insns between note we processed last time
20069      and this note, use the label we emitted last time.  Otherwise
20070      create a new label and emit it.  */
20071   if (last_label == NULL)
20072     {
20073       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20074       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20075       loclabel_num++;
20076       last_label = ggc_strdup (loclabel);
20077     }
20078
20079   if (!var_loc_p)
20080     {
20081       struct call_arg_loc_node *ca_loc
20082         = ggc_alloc_cleared_call_arg_loc_node ();
20083       rtx prev = prev_real_insn (loc_note), x;
20084       ca_loc->call_arg_loc_note = loc_note;
20085       ca_loc->next = NULL;
20086       ca_loc->label = last_label;
20087       gcc_assert (prev
20088                   && (CALL_P (prev)
20089                       || (NONJUMP_INSN_P (prev)
20090                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20091                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20092       if (!CALL_P (prev))
20093         prev = XVECEXP (PATTERN (prev), 0, 0);
20094       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20095       x = PATTERN (prev);
20096       if (GET_CODE (x) == PARALLEL)
20097         x = XVECEXP (x, 0, 0);
20098       if (GET_CODE (x) == SET)
20099         x = SET_SRC (x);
20100       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20101         {
20102           x = XEXP (XEXP (x, 0), 0);
20103           if (GET_CODE (x) == SYMBOL_REF
20104               && SYMBOL_REF_DECL (x)
20105               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20106             ca_loc->symbol_ref = x;
20107         }
20108       ca_loc->block = insn_scope (prev);
20109       if (call_arg_locations)
20110         call_arg_loc_last->next = ca_loc;
20111       else
20112         call_arg_locations = ca_loc;
20113       call_arg_loc_last = ca_loc;
20114     }
20115   else if (!NOTE_DURING_CALL_P (loc_note))
20116     newloc->label = last_label;
20117   else
20118     {
20119       if (!last_postcall_label)
20120         {
20121           sprintf (loclabel, "%s-1", last_label);
20122           last_postcall_label = ggc_strdup (loclabel);
20123         }
20124       newloc->label = last_postcall_label;
20125     }
20126
20127   last_var_location_insn = next_real;
20128   last_in_cold_section_p = in_cold_section_p;
20129 }
20130
20131 /* Note in one location list that text section has changed.  */
20132
20133 static int
20134 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20135 {
20136   var_loc_list *list = (var_loc_list *) *slot;
20137   if (list->first)
20138     list->last_before_switch
20139       = list->last->next ? list->last->next : list->last;
20140   return 1;
20141 }
20142
20143 /* Note in all location lists that text section has changed.  */
20144
20145 static void
20146 var_location_switch_text_section (void)
20147 {
20148   if (decl_loc_table == NULL)
20149     return;
20150
20151   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20152 }
20153
20154 /* Create a new line number table.  */
20155
20156 static dw_line_info_table *
20157 new_line_info_table (void)
20158 {
20159   dw_line_info_table *table;
20160
20161   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20162   table->file_num = 1;
20163   table->line_num = 1;
20164   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20165
20166   return table;
20167 }
20168
20169 /* Lookup the "current" table into which we emit line info, so
20170    that we don't have to do it for every source line.  */
20171
20172 static void
20173 set_cur_line_info_table (section *sec)
20174 {
20175   dw_line_info_table *table;
20176
20177   if (sec == text_section)
20178     table = text_section_line_info;
20179   else if (sec == cold_text_section)
20180     {
20181       table = cold_text_section_line_info;
20182       if (!table)
20183         {
20184           cold_text_section_line_info = table = new_line_info_table ();
20185           table->end_label = cold_end_label;
20186         }
20187     }
20188   else
20189     {
20190       const char *end_label;
20191
20192       if (flag_reorder_blocks_and_partition)
20193         {
20194           if (in_cold_section_p)
20195             end_label = crtl->subsections.cold_section_end_label;
20196           else
20197             end_label = crtl->subsections.hot_section_end_label;
20198         }
20199       else
20200         {
20201           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20202           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20203                                        current_function_funcdef_no);
20204           end_label = ggc_strdup (label);
20205         }
20206
20207       table = new_line_info_table ();
20208       table->end_label = end_label;
20209
20210       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20211     }
20212
20213   cur_line_info_table = table;
20214 }
20215
20216
20217 /* We need to reset the locations at the beginning of each
20218    function. We can't do this in the end_function hook, because the
20219    declarations that use the locations won't have been output when
20220    that hook is called.  Also compute have_multiple_function_sections here.  */
20221
20222 static void
20223 dwarf2out_begin_function (tree fun)
20224 {
20225   section *sec = function_section (fun);
20226
20227   if (sec != text_section)
20228     have_multiple_function_sections = true;
20229
20230   if (flag_reorder_blocks_and_partition && !cold_text_section)
20231     {
20232       gcc_assert (current_function_decl == fun);
20233       cold_text_section = unlikely_text_section ();
20234       switch_to_section (cold_text_section);
20235       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20236       switch_to_section (sec);
20237     }
20238
20239   dwarf2out_note_section_used ();
20240   call_site_count = 0;
20241   tail_call_site_count = 0;
20242
20243   set_cur_line_info_table (sec);
20244 }
20245
20246 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20247
20248 static void
20249 push_dw_line_info_entry (dw_line_info_table *table,
20250                          enum dw_line_info_opcode opcode, unsigned int val)
20251 {
20252   dw_line_info_entry e;
20253   e.opcode = opcode;
20254   e.val = val;
20255   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20256 }
20257
20258 /* Output a label to mark the beginning of a source code line entry
20259    and record information relating to this source line, in
20260    'line_info_table' for later output of the .debug_line section.  */
20261 /* ??? The discriminator parameter ought to be unsigned.  */
20262
20263 static void
20264 dwarf2out_source_line (unsigned int line, const char *filename,
20265                        int discriminator, bool is_stmt)
20266 {
20267   unsigned int file_num;
20268   dw_line_info_table *table;
20269
20270   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20271     return;
20272
20273   /* The discriminator column was added in dwarf4.  Simplify the below
20274      by simply removing it if we're not supposed to output it.  */
20275   if (dwarf_version < 4 && dwarf_strict)
20276     discriminator = 0;
20277
20278   table = cur_line_info_table;
20279   file_num = maybe_emit_file (lookup_filename (filename));
20280
20281   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20282      the debugger has used the second (possibly duplicate) line number
20283      at the beginning of the function to mark the end of the prologue.
20284      We could eliminate any other duplicates within the function.  For
20285      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20286      that second line number entry.  */
20287   /* Recall that this end-of-prologue indication is *not* the same thing
20288      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20289      to which the hook corresponds, follows the last insn that was 
20290      emitted by gen_prologue.  What we need is to preceed the first insn
20291      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20292      insn that corresponds to something the user wrote.  These may be
20293      very different locations once scheduling is enabled.  */
20294
20295   if (0 && file_num == table->file_num
20296       && line == table->line_num
20297       && discriminator == table->discrim_num
20298       && is_stmt == table->is_stmt)
20299     return;
20300
20301   switch_to_section (current_function_section ());
20302
20303   /* If requested, emit something human-readable.  */
20304   if (flag_debug_asm)
20305     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20306
20307   if (DWARF2_ASM_LINE_DEBUG_INFO)
20308     {
20309       /* Emit the .loc directive understood by GNU as.  */
20310       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20311       if (is_stmt != table->is_stmt)
20312         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20313       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20314         fprintf (asm_out_file, " discriminator %d", discriminator);
20315       fputc ('\n', asm_out_file);
20316     }
20317   else
20318     {
20319       unsigned int label_num = ++line_info_label_num;
20320
20321       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20322
20323       push_dw_line_info_entry (table, LI_set_address, label_num);
20324       if (file_num != table->file_num)
20325         push_dw_line_info_entry (table, LI_set_file, file_num);
20326       if (discriminator != table->discrim_num)
20327         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20328       if (is_stmt != table->is_stmt)
20329         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20330       push_dw_line_info_entry (table, LI_set_line, line);
20331     }
20332
20333   table->file_num = file_num;
20334   table->line_num = line;
20335   table->discrim_num = discriminator;
20336   table->is_stmt = is_stmt;
20337   table->in_use = true;
20338 }
20339
20340 /* Record the beginning of a new source file.  */
20341
20342 static void
20343 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20344 {
20345   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20346     {
20347       /* Record the beginning of the file for break_out_includes.  */
20348       dw_die_ref bincl_die;
20349
20350       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20351       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20352     }
20353
20354   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20355     {
20356       macinfo_entry e;
20357       e.code = DW_MACINFO_start_file;
20358       e.lineno = lineno;
20359       e.info = xstrdup (filename);
20360       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20361     }
20362 }
20363
20364 /* Record the end of a source file.  */
20365
20366 static void
20367 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20368 {
20369   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20370     /* Record the end of the file for break_out_includes.  */
20371     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20372
20373   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20374     {
20375       macinfo_entry e;
20376       e.code = DW_MACINFO_end_file;
20377       e.lineno = lineno;
20378       e.info = NULL;
20379       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20380     }
20381 }
20382
20383 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20384    the tail part of the directive line, i.e. the part which is past the
20385    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20386
20387 static void
20388 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20389                   const char *buffer ATTRIBUTE_UNUSED)
20390 {
20391   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20392     {
20393       macinfo_entry e;
20394       e.code = DW_MACINFO_define;
20395       e.lineno = lineno;
20396       e.info = xstrdup (buffer);;
20397       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20398     }
20399 }
20400
20401 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20402    the tail part of the directive line, i.e. the part which is past the
20403    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20404
20405 static void
20406 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20407                  const char *buffer ATTRIBUTE_UNUSED)
20408 {
20409   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20410     {
20411       macinfo_entry e;
20412       e.code = DW_MACINFO_undef;
20413       e.lineno = lineno;
20414       e.info = xstrdup (buffer);;
20415       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20416     }
20417 }
20418
20419 static void
20420 output_macinfo (void)
20421 {
20422   unsigned i;
20423   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20424   macinfo_entry *ref;
20425
20426   if (! length)
20427     return;
20428
20429   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20430     {
20431       switch (ref->code)
20432         {
20433           case DW_MACINFO_start_file:
20434             {
20435               int file_num = maybe_emit_file (lookup_filename (ref->info));
20436               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20437               dw2_asm_output_data_uleb128 
20438                         (ref->lineno, "Included from line number %lu", 
20439                                                 (unsigned long)ref->lineno);
20440               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20441             }
20442             break;
20443           case DW_MACINFO_end_file:
20444             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20445             break;
20446           case DW_MACINFO_define:
20447             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
20448             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20449                                                 (unsigned long)ref->lineno);
20450             dw2_asm_output_nstring (ref->info, -1, "The macro");
20451             break;
20452           case DW_MACINFO_undef:
20453             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
20454             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20455                                                 (unsigned long)ref->lineno);
20456             dw2_asm_output_nstring (ref->info, -1, "The macro");
20457             break;
20458           default:
20459            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20460              ASM_COMMENT_START, (unsigned long)ref->code);
20461           break;
20462         }
20463     }
20464 }
20465
20466 /* Set up for Dwarf output at the start of compilation.  */
20467
20468 static void
20469 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20470 {
20471   /* Allocate the file_table.  */
20472   file_table = htab_create_ggc (50, file_table_hash,
20473                                 file_table_eq, NULL);
20474
20475   /* Allocate the decl_die_table.  */
20476   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20477                                     decl_die_table_eq, NULL);
20478
20479   /* Allocate the decl_loc_table.  */
20480   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20481                                     decl_loc_table_eq, NULL);
20482
20483   /* Allocate the cached_dw_loc_list_table.  */
20484   cached_dw_loc_list_table
20485     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20486                        cached_dw_loc_list_table_eq, NULL);
20487
20488   /* Allocate the initial hunk of the decl_scope_table.  */
20489   decl_scope_table = VEC_alloc (tree, gc, 256);
20490
20491   /* Allocate the initial hunk of the abbrev_die_table.  */
20492   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20493     (ABBREV_DIE_TABLE_INCREMENT);
20494   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20495   /* Zero-th entry is allocated, but unused.  */
20496   abbrev_die_table_in_use = 1;
20497
20498   /* Allocate the pubtypes and pubnames vectors.  */
20499   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20500   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20501
20502   incomplete_types = VEC_alloc (tree, gc, 64);
20503
20504   used_rtx_array = VEC_alloc (rtx, gc, 32);
20505
20506   debug_info_section = get_section (DEBUG_INFO_SECTION,
20507                                     SECTION_DEBUG, NULL);
20508   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20509                                       SECTION_DEBUG, NULL);
20510   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20511                                        SECTION_DEBUG, NULL);
20512   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20513                                        SECTION_DEBUG, NULL);
20514   debug_line_section = get_section (DEBUG_LINE_SECTION,
20515                                     SECTION_DEBUG, NULL);
20516   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20517                                    SECTION_DEBUG, NULL);
20518   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20519                                         SECTION_DEBUG, NULL);
20520   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20521                                         SECTION_DEBUG, NULL);
20522   debug_str_section = get_section (DEBUG_STR_SECTION,
20523                                    DEBUG_STR_SECTION_FLAGS, NULL);
20524   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20525                                       SECTION_DEBUG, NULL);
20526   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20527                                      SECTION_DEBUG, NULL);
20528
20529   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20530   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20531                                DEBUG_ABBREV_SECTION_LABEL, 0);
20532   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20533   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20534                                COLD_TEXT_SECTION_LABEL, 0);
20535   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20536
20537   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20538                                DEBUG_INFO_SECTION_LABEL, 0);
20539   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20540                                DEBUG_LINE_SECTION_LABEL, 0);
20541   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20542                                DEBUG_RANGES_SECTION_LABEL, 0);
20543   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20544                                DEBUG_MACINFO_SECTION_LABEL, 0);
20545
20546   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20547     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20548
20549   switch_to_section (text_section);
20550   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20551
20552   /* Make sure the line number table for .text always exists.  */
20553   text_section_line_info = new_line_info_table ();
20554   text_section_line_info->end_label = text_end_label;
20555 }
20556
20557 /* Called before cgraph_optimize starts outputtting functions, variables
20558    and toplevel asms into assembly.  */
20559
20560 static void
20561 dwarf2out_assembly_start (void)
20562 {
20563   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20564       && dwarf2out_do_cfi_asm ()
20565       && (!(flag_unwind_tables || flag_exceptions)
20566           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20567     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20568 }
20569
20570 /* A helper function for dwarf2out_finish called through
20571    htab_traverse.  Emit one queued .debug_str string.  */
20572
20573 static int
20574 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20575 {
20576   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20577
20578   if (node->form == DW_FORM_strp)
20579     {
20580       switch_to_section (debug_str_section);
20581       ASM_OUTPUT_LABEL (asm_out_file, node->label);
20582       assemble_string (node->str, strlen (node->str) + 1);
20583     }
20584
20585   return 1;
20586 }
20587
20588 #if ENABLE_ASSERT_CHECKING
20589 /* Verify that all marks are clear.  */
20590
20591 static void
20592 verify_marks_clear (dw_die_ref die)
20593 {
20594   dw_die_ref c;
20595
20596   gcc_assert (! die->die_mark);
20597   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
20598 }
20599 #endif /* ENABLE_ASSERT_CHECKING */
20600
20601 /* Clear the marks for a die and its children.
20602    Be cool if the mark isn't set.  */
20603
20604 static void
20605 prune_unmark_dies (dw_die_ref die)
20606 {
20607   dw_die_ref c;
20608
20609   if (die->die_mark)
20610     die->die_mark = 0;
20611   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
20612 }
20613
20614 /* Given DIE that we're marking as used, find any other dies
20615    it references as attributes and mark them as used.  */
20616
20617 static void
20618 prune_unused_types_walk_attribs (dw_die_ref die)
20619 {
20620   dw_attr_ref a;
20621   unsigned ix;
20622
20623   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20624     {
20625       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
20626         {
20627           /* A reference to another DIE.
20628              Make sure that it will get emitted.
20629              If it was broken out into a comdat group, don't follow it.  */
20630           if (! use_debug_types
20631               || a->dw_attr == DW_AT_specification
20632               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
20633             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
20634         }
20635       /* Set the string's refcount to 0 so that prune_unused_types_mark
20636          accounts properly for it.  */
20637       if (AT_class (a) == dw_val_class_str)
20638         a->dw_attr_val.v.val_str->refcount = 0;
20639     }
20640 }
20641
20642 /* Mark the generic parameters and arguments children DIEs of DIE.  */
20643
20644 static void
20645 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
20646 {
20647   dw_die_ref c;
20648
20649   if (die == NULL || die->die_child == NULL)
20650     return;
20651   c = die->die_child;
20652   do
20653     {
20654       switch (c->die_tag)
20655         {
20656         case DW_TAG_template_type_param:
20657         case DW_TAG_template_value_param:
20658         case DW_TAG_GNU_template_template_param:
20659         case DW_TAG_GNU_template_parameter_pack:
20660           prune_unused_types_mark (c, 1);
20661           break;
20662         default:
20663           break;
20664         }
20665       c = c->die_sib;
20666     } while (c && c != die->die_child);
20667 }
20668
20669 /* Mark DIE as being used.  If DOKIDS is true, then walk down
20670    to DIE's children.  */
20671
20672 static void
20673 prune_unused_types_mark (dw_die_ref die, int dokids)
20674 {
20675   dw_die_ref c;
20676
20677   if (die->die_mark == 0)
20678     {
20679       /* We haven't done this node yet.  Mark it as used.  */
20680       die->die_mark = 1;
20681       /* If this is the DIE of a generic type instantiation,
20682          mark the children DIEs that describe its generic parms and
20683          args.  */
20684       prune_unused_types_mark_generic_parms_dies (die);
20685
20686       /* We also have to mark its parents as used.
20687          (But we don't want to mark our parents' kids due to this.)  */
20688       if (die->die_parent)
20689         prune_unused_types_mark (die->die_parent, 0);
20690
20691       /* Mark any referenced nodes.  */
20692       prune_unused_types_walk_attribs (die);
20693
20694       /* If this node is a specification,
20695          also mark the definition, if it exists.  */
20696       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
20697         prune_unused_types_mark (die->die_definition, 1);
20698     }
20699
20700   if (dokids && die->die_mark != 2)
20701     {
20702       /* We need to walk the children, but haven't done so yet.
20703          Remember that we've walked the kids.  */
20704       die->die_mark = 2;
20705
20706       /* If this is an array type, we need to make sure our
20707          kids get marked, even if they're types.  If we're
20708          breaking out types into comdat sections, do this
20709          for all type definitions.  */
20710       if (die->die_tag == DW_TAG_array_type
20711           || (use_debug_types
20712               && is_type_die (die) && ! is_declaration_die (die)))
20713         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
20714       else
20715         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20716     }
20717 }
20718
20719 /* For local classes, look if any static member functions were emitted
20720    and if so, mark them.  */
20721
20722 static void
20723 prune_unused_types_walk_local_classes (dw_die_ref die)
20724 {
20725   dw_die_ref c;
20726
20727   if (die->die_mark == 2)
20728     return;
20729
20730   switch (die->die_tag)
20731     {
20732     case DW_TAG_structure_type:
20733     case DW_TAG_union_type:
20734     case DW_TAG_class_type:
20735       break;
20736
20737     case DW_TAG_subprogram:
20738       if (!get_AT_flag (die, DW_AT_declaration)
20739           || die->die_definition != NULL)
20740         prune_unused_types_mark (die, 1);
20741       return;
20742
20743     default:
20744       return;
20745     }
20746
20747   /* Mark children.  */
20748   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
20749 }
20750
20751 /* Walk the tree DIE and mark types that we actually use.  */
20752
20753 static void
20754 prune_unused_types_walk (dw_die_ref die)
20755 {
20756   dw_die_ref c;
20757
20758   /* Don't do anything if this node is already marked and
20759      children have been marked as well.  */
20760   if (die->die_mark == 2)
20761     return;
20762
20763   switch (die->die_tag)
20764     {
20765     case DW_TAG_structure_type:
20766     case DW_TAG_union_type:
20767     case DW_TAG_class_type:
20768       if (die->die_perennial_p)
20769         break;
20770
20771       for (c = die->die_parent; c; c = c->die_parent)
20772         if (c->die_tag == DW_TAG_subprogram)
20773           break;
20774
20775       /* Finding used static member functions inside of classes
20776          is needed just for local classes, because for other classes
20777          static member function DIEs with DW_AT_specification
20778          are emitted outside of the DW_TAG_*_type.  If we ever change
20779          it, we'd need to call this even for non-local classes.  */
20780       if (c)
20781         prune_unused_types_walk_local_classes (die);
20782
20783       /* It's a type node --- don't mark it.  */
20784       return;
20785
20786     case DW_TAG_const_type:
20787     case DW_TAG_packed_type:
20788     case DW_TAG_pointer_type:
20789     case DW_TAG_reference_type:
20790     case DW_TAG_rvalue_reference_type:
20791     case DW_TAG_volatile_type:
20792     case DW_TAG_typedef:
20793     case DW_TAG_array_type:
20794     case DW_TAG_interface_type:
20795     case DW_TAG_friend:
20796     case DW_TAG_variant_part:
20797     case DW_TAG_enumeration_type:
20798     case DW_TAG_subroutine_type:
20799     case DW_TAG_string_type:
20800     case DW_TAG_set_type:
20801     case DW_TAG_subrange_type:
20802     case DW_TAG_ptr_to_member_type:
20803     case DW_TAG_file_type:
20804       if (die->die_perennial_p)
20805         break;
20806
20807       /* It's a type node --- don't mark it.  */
20808       return;
20809
20810     default:
20811       /* Mark everything else.  */
20812       break;
20813   }
20814
20815   if (die->die_mark == 0)
20816     {
20817       die->die_mark = 1;
20818
20819       /* Now, mark any dies referenced from here.  */
20820       prune_unused_types_walk_attribs (die);
20821     }
20822
20823   die->die_mark = 2;
20824
20825   /* Mark children.  */
20826   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
20827 }
20828
20829 /* Increment the string counts on strings referred to from DIE's
20830    attributes.  */
20831
20832 static void
20833 prune_unused_types_update_strings (dw_die_ref die)
20834 {
20835   dw_attr_ref a;
20836   unsigned ix;
20837
20838   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
20839     if (AT_class (a) == dw_val_class_str)
20840       {
20841         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
20842         s->refcount++;
20843         /* Avoid unnecessarily putting strings that are used less than
20844            twice in the hash table.  */
20845         if (s->refcount
20846             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
20847           {
20848             void ** slot;
20849             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
20850                                              htab_hash_string (s->str),
20851                                              INSERT);
20852             gcc_assert (*slot == NULL);
20853             *slot = s;
20854           }
20855       }
20856 }
20857
20858 /* Remove from the tree DIE any dies that aren't marked.  */
20859
20860 static void
20861 prune_unused_types_prune (dw_die_ref die)
20862 {
20863   dw_die_ref c;
20864
20865   gcc_assert (die->die_mark);
20866   prune_unused_types_update_strings (die);
20867
20868   if (! die->die_child)
20869     return;
20870
20871   c = die->die_child;
20872   do {
20873     dw_die_ref prev = c;
20874     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
20875       if (c == die->die_child)
20876         {
20877           /* No marked children between 'prev' and the end of the list.  */
20878           if (prev == c)
20879             /* No marked children at all.  */
20880             die->die_child = NULL;
20881           else
20882             {
20883               prev->die_sib = c->die_sib;
20884               die->die_child = prev;
20885             }
20886           return;
20887         }
20888
20889     if (c != prev->die_sib)
20890       prev->die_sib = c;
20891     prune_unused_types_prune (c);
20892   } while (c != die->die_child);
20893 }
20894
20895 /* Remove dies representing declarations that we never use.  */
20896
20897 static void
20898 prune_unused_types (void)
20899 {
20900   unsigned int i;
20901   limbo_die_node *node;
20902   comdat_type_node *ctnode;
20903   pubname_ref pub;
20904   dw_die_ref base_type;
20905
20906 #if ENABLE_ASSERT_CHECKING
20907   /* All the marks should already be clear.  */
20908   verify_marks_clear (comp_unit_die ());
20909   for (node = limbo_die_list; node; node = node->next)
20910     verify_marks_clear (node->die);
20911   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20912     verify_marks_clear (ctnode->root_die);
20913 #endif /* ENABLE_ASSERT_CHECKING */
20914
20915   /* Mark types that are used in global variables.  */
20916   premark_types_used_by_global_vars ();
20917
20918   /* Set the mark on nodes that are actually used.  */
20919   prune_unused_types_walk (comp_unit_die ());
20920   for (node = limbo_die_list; node; node = node->next)
20921     prune_unused_types_walk (node->die);
20922   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20923     {
20924       prune_unused_types_walk (ctnode->root_die);
20925       prune_unused_types_mark (ctnode->type_die, 1);
20926     }
20927
20928   /* Also set the mark on nodes referenced from the
20929      pubname_table.  */
20930   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
20931     prune_unused_types_mark (pub->die, 1);
20932   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
20933     prune_unused_types_mark (base_type, 1);
20934
20935   if (debug_str_hash)
20936     htab_empty (debug_str_hash);
20937   prune_unused_types_prune (comp_unit_die ());
20938   for (node = limbo_die_list; node; node = node->next)
20939     prune_unused_types_prune (node->die);
20940   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20941     prune_unused_types_prune (ctnode->root_die);
20942
20943   /* Leave the marks clear.  */
20944   prune_unmark_dies (comp_unit_die ());
20945   for (node = limbo_die_list; node; node = node->next)
20946     prune_unmark_dies (node->die);
20947   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
20948     prune_unmark_dies (ctnode->root_die);
20949 }
20950
20951 /* Set the parameter to true if there are any relative pathnames in
20952    the file table.  */
20953 static int
20954 file_table_relative_p (void ** slot, void *param)
20955 {
20956   bool *p = (bool *) param;
20957   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
20958   if (!IS_ABSOLUTE_PATH (d->filename))
20959     {
20960       *p = true;
20961       return 0;
20962     }
20963   return 1;
20964 }
20965
20966 /* Routines to manipulate hash table of comdat type units.  */
20967
20968 static hashval_t
20969 htab_ct_hash (const void *of)
20970 {
20971   hashval_t h;
20972   const comdat_type_node *const type_node = (const comdat_type_node *) of;
20973
20974   memcpy (&h, type_node->signature, sizeof (h));
20975   return h;
20976 }
20977
20978 static int
20979 htab_ct_eq (const void *of1, const void *of2)
20980 {
20981   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
20982   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
20983
20984   return (! memcmp (type_node_1->signature, type_node_2->signature,
20985                     DWARF_TYPE_SIGNATURE_SIZE));
20986 }
20987
20988 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
20989    to the location it would have been added, should we know its
20990    DECL_ASSEMBLER_NAME when we added other attributes.  This will
20991    probably improve compactness of debug info, removing equivalent
20992    abbrevs, and hide any differences caused by deferring the
20993    computation of the assembler name, triggered by e.g. PCH.  */
20994
20995 static inline void
20996 move_linkage_attr (dw_die_ref die)
20997 {
20998   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
20999   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21000
21001   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21002               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21003
21004   while (--ix > 0)
21005     {
21006       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21007
21008       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21009         break;
21010     }
21011
21012   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21013     {
21014       VEC_pop (dw_attr_node, die->die_attr);
21015       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21016     }
21017 }
21018
21019 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21020    referenced from typed stack ops and count how often they are used.  */
21021
21022 static void
21023 mark_base_types (dw_loc_descr_ref loc)
21024 {
21025   dw_die_ref base_type = NULL;
21026
21027   for (; loc; loc = loc->dw_loc_next)
21028     {
21029       switch (loc->dw_loc_opc)
21030         {
21031         case DW_OP_GNU_regval_type:
21032         case DW_OP_GNU_deref_type:
21033           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21034           break;
21035         case DW_OP_GNU_convert:
21036         case DW_OP_GNU_reinterpret:
21037           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21038             continue;
21039           /* FALLTHRU */
21040         case DW_OP_GNU_const_type:
21041           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21042           break;
21043         case DW_OP_GNU_entry_value:
21044           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21045           continue;
21046         default:
21047           continue;
21048         }
21049       gcc_assert (base_type->die_parent == comp_unit_die ());
21050       if (base_type->die_mark)
21051         base_type->die_mark++;
21052       else
21053         {
21054           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21055           base_type->die_mark = 1;
21056         }
21057     }
21058 }
21059
21060 /* Comparison function for sorting marked base types.  */
21061
21062 static int
21063 base_type_cmp (const void *x, const void *y)
21064 {
21065   dw_die_ref dx = *(const dw_die_ref *) x;
21066   dw_die_ref dy = *(const dw_die_ref *) y;
21067   unsigned int byte_size1, byte_size2;
21068   unsigned int encoding1, encoding2;
21069   if (dx->die_mark > dy->die_mark)
21070     return -1;
21071   if (dx->die_mark < dy->die_mark)
21072     return 1;
21073   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21074   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21075   if (byte_size1 < byte_size2)
21076     return 1;
21077   if (byte_size1 > byte_size2)
21078     return -1;
21079   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21080   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21081   if (encoding1 < encoding2)
21082     return 1;
21083   if (encoding1 > encoding2)
21084     return -1;
21085   return 0;
21086 }
21087
21088 /* Move base types marked by mark_base_types as early as possible
21089    in the CU, sorted by decreasing usage count both to make the
21090    uleb128 references as small as possible and to make sure they
21091    will have die_offset already computed by calc_die_sizes when
21092    sizes of typed stack loc ops is computed.  */
21093
21094 static void
21095 move_marked_base_types (void)
21096 {
21097   unsigned int i;
21098   dw_die_ref base_type, die, c;
21099
21100   if (VEC_empty (dw_die_ref, base_types))
21101     return;
21102
21103   /* Sort by decreasing usage count, they will be added again in that
21104      order later on.  */
21105   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21106   die = comp_unit_die ();
21107   c = die->die_child;
21108   do
21109     {
21110       dw_die_ref prev = c;
21111       c = c->die_sib;
21112       while (c->die_mark)
21113         {
21114           remove_child_with_prev (c, prev);
21115           /* As base types got marked, there must be at least
21116              one node other than DW_TAG_base_type.  */
21117           gcc_assert (c != c->die_sib);
21118           c = c->die_sib;
21119         }
21120     }
21121   while (c != die->die_child);
21122   gcc_assert (die->die_child);
21123   c = die->die_child;
21124   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21125     {
21126       base_type->die_mark = 0;
21127       base_type->die_sib = c->die_sib;
21128       c->die_sib = base_type;
21129       c = base_type;
21130     }
21131 }
21132
21133 /* Helper function for resolve_addr, attempt to resolve
21134    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21135    SYMBOL_REFs refer to variables emitted in the current CU.  */
21136
21137 static int
21138 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21139 {
21140   rtx rtl = *addr;
21141
21142   if (GET_CODE (rtl) == CONST_STRING)
21143     {
21144       size_t len = strlen (XSTR (rtl, 0)) + 1;
21145       tree t = build_string (len, XSTR (rtl, 0));
21146       tree tlen = size_int (len - 1);
21147       TREE_TYPE (t)
21148         = build_array_type (char_type_node, build_index_type (tlen));
21149       rtl = lookup_constant_def (t);
21150       if (!rtl || !MEM_P (rtl))
21151         return 1;
21152       rtl = XEXP (rtl, 0);
21153       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21154       *addr = rtl;
21155       return 0;
21156     }
21157
21158   if (GET_CODE (rtl) == SYMBOL_REF
21159       && SYMBOL_REF_DECL (rtl))
21160     {
21161       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21162         {
21163           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21164             return 1;
21165         }
21166       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21167         return 1;
21168     }
21169
21170   if (GET_CODE (rtl) == CONST
21171       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21172     return 1;
21173
21174   return 0;
21175 }
21176
21177 /* Helper function for resolve_addr, handle one location
21178    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21179    the location list couldn't be resolved.  */
21180
21181 static bool
21182 resolve_addr_in_expr (dw_loc_descr_ref loc)
21183 {
21184   dw_loc_descr_ref keep = NULL;
21185   for (; loc; loc = loc->dw_loc_next)
21186     switch (loc->dw_loc_opc)
21187       {
21188       case DW_OP_addr:
21189         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21190           return false;
21191         break;
21192       case DW_OP_const4u:
21193       case DW_OP_const8u:
21194         if (loc->dtprel
21195             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21196           return false;
21197         break;
21198       case DW_OP_plus_uconst:
21199         if (size_of_loc_descr (loc)
21200             > size_of_int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned)
21201               + 1
21202             && loc->dw_loc_oprnd1.v.val_unsigned > 0)
21203           {
21204             dw_loc_descr_ref repl
21205               = int_loc_descriptor (loc->dw_loc_oprnd1.v.val_unsigned);
21206             add_loc_descr (&repl, new_loc_descr (DW_OP_plus, 0, 0));
21207             add_loc_descr (&repl, loc->dw_loc_next);
21208             *loc = *repl;
21209           }
21210         break;
21211       case DW_OP_implicit_value:
21212         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21213             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21214           return false;
21215         break;
21216       case DW_OP_GNU_implicit_pointer:
21217       case DW_OP_GNU_parameter_ref:
21218         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21219           {
21220             dw_die_ref ref
21221               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21222             if (ref == NULL)
21223               return false;
21224             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21225             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21226             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21227           }
21228         break;
21229       case DW_OP_GNU_const_type:
21230       case DW_OP_GNU_regval_type:
21231       case DW_OP_GNU_deref_type:
21232       case DW_OP_GNU_convert:
21233       case DW_OP_GNU_reinterpret:
21234         while (loc->dw_loc_next
21235                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21236           {
21237             dw_die_ref base1, base2;
21238             unsigned enc1, enc2, size1, size2;
21239             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21240                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21241               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21242             else if (loc->dw_loc_oprnd1.val_class
21243                      == dw_val_class_unsigned_const)
21244               break;
21245             else
21246               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21247             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21248                 == dw_val_class_unsigned_const)
21249               break;
21250             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21251             gcc_assert (base1->die_tag == DW_TAG_base_type
21252                         && base2->die_tag == DW_TAG_base_type);
21253             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21254             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21255             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21256             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21257             if (size1 == size2
21258                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21259                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21260                      && loc != keep)
21261                     || enc1 == enc2))
21262               {
21263                 /* Optimize away next DW_OP_GNU_convert after
21264                    adjusting LOC's base type die reference.  */
21265                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21266                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21267                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21268                 else
21269                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21270                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21271                 continue;
21272               }
21273             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21274                point typed stack entry.  */
21275             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21276               keep = loc->dw_loc_next;
21277             break;
21278           }
21279         break;
21280       default:
21281         break;
21282       }
21283   return true;
21284 }
21285
21286 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21287    an address in .rodata section if the string literal is emitted there,
21288    or remove the containing location list or replace DW_AT_const_value
21289    with DW_AT_location and empty location expression, if it isn't found
21290    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21291    to something that has been emitted in the current CU.  */
21292
21293 static void
21294 resolve_addr (dw_die_ref die)
21295 {
21296   dw_die_ref c;
21297   dw_attr_ref a;
21298   dw_loc_list_ref *curr, *start, loc;
21299   unsigned ix;
21300
21301   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21302     switch (AT_class (a))
21303       {
21304       case dw_val_class_loc_list:
21305         start = curr = AT_loc_list_ptr (a);
21306         loc = *curr;
21307         gcc_assert (loc);
21308         /* The same list can be referenced more than once.  See if we have
21309            already recorded the result from a previous pass.  */
21310         if (loc->replaced)
21311           *curr = loc->dw_loc_next;
21312         else if (!loc->resolved_addr)
21313           {
21314             /* As things stand, we do not expect or allow one die to
21315                reference a suffix of another die's location list chain.
21316                References must be identical or completely separate.
21317                There is therefore no need to cache the result of this
21318                pass on any list other than the first; doing so
21319                would lead to unnecessary writes.  */
21320             while (*curr)
21321               {
21322                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21323                 if (!resolve_addr_in_expr ((*curr)->expr))
21324                   {
21325                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21326                     if (next && (*curr)->ll_symbol)
21327                       {
21328                         gcc_assert (!next->ll_symbol);
21329                         next->ll_symbol = (*curr)->ll_symbol;
21330                       }
21331                     *curr = next;
21332                   }
21333                 else
21334                   {
21335                     mark_base_types ((*curr)->expr);
21336                     curr = &(*curr)->dw_loc_next;
21337                   }
21338               }
21339             if (loc == *start)
21340               loc->resolved_addr = 1;
21341             else
21342               {
21343                 loc->replaced = 1;
21344                 loc->dw_loc_next = *start;
21345               }
21346           }
21347         if (!*start)
21348           {
21349             remove_AT (die, a->dw_attr);
21350             ix--;
21351           }
21352         break;
21353       case dw_val_class_loc:
21354         if (!resolve_addr_in_expr (AT_loc (a)))
21355           {
21356             remove_AT (die, a->dw_attr);
21357             ix--;
21358           }
21359         else
21360           mark_base_types (AT_loc (a));
21361         break;
21362       case dw_val_class_addr:
21363         if (a->dw_attr == DW_AT_const_value
21364             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21365           {
21366             remove_AT (die, a->dw_attr);
21367             ix--;
21368           }
21369         if (die->die_tag == DW_TAG_GNU_call_site
21370             && a->dw_attr == DW_AT_abstract_origin)
21371           {
21372             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21373             dw_die_ref tdie = lookup_decl_die (tdecl);
21374             if (tdie == NULL
21375                 && DECL_EXTERNAL (tdecl)
21376                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21377               {
21378                 force_decl_die (tdecl);
21379                 tdie = lookup_decl_die (tdecl);
21380               }
21381             if (tdie)
21382               {
21383                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21384                 a->dw_attr_val.v.val_die_ref.die = tdie;
21385                 a->dw_attr_val.v.val_die_ref.external = 0;
21386               }
21387             else
21388               {
21389                 remove_AT (die, a->dw_attr);
21390                 ix--;
21391               }
21392           }
21393         break;
21394       default:
21395         break;
21396       }
21397
21398   FOR_EACH_CHILD (die, c, resolve_addr (c));
21399 }
21400 \f
21401 /* Helper routines for optimize_location_lists.
21402    This pass tries to share identical local lists in .debug_loc
21403    section.  */
21404
21405 /* Iteratively hash operands of LOC opcode.  */
21406
21407 static inline hashval_t
21408 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21409 {
21410   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21411   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21412
21413   switch (loc->dw_loc_opc)
21414     {
21415     case DW_OP_const4u:
21416     case DW_OP_const8u:
21417       if (loc->dtprel)
21418         goto hash_addr;
21419       /* FALLTHRU */
21420     case DW_OP_const1u:
21421     case DW_OP_const1s:
21422     case DW_OP_const2u:
21423     case DW_OP_const2s:
21424     case DW_OP_const4s:
21425     case DW_OP_const8s:
21426     case DW_OP_constu:
21427     case DW_OP_consts:
21428     case DW_OP_pick:
21429     case DW_OP_plus_uconst:
21430     case DW_OP_breg0:
21431     case DW_OP_breg1:
21432     case DW_OP_breg2:
21433     case DW_OP_breg3:
21434     case DW_OP_breg4:
21435     case DW_OP_breg5:
21436     case DW_OP_breg6:
21437     case DW_OP_breg7:
21438     case DW_OP_breg8:
21439     case DW_OP_breg9:
21440     case DW_OP_breg10:
21441     case DW_OP_breg11:
21442     case DW_OP_breg12:
21443     case DW_OP_breg13:
21444     case DW_OP_breg14:
21445     case DW_OP_breg15:
21446     case DW_OP_breg16:
21447     case DW_OP_breg17:
21448     case DW_OP_breg18:
21449     case DW_OP_breg19:
21450     case DW_OP_breg20:
21451     case DW_OP_breg21:
21452     case DW_OP_breg22:
21453     case DW_OP_breg23:
21454     case DW_OP_breg24:
21455     case DW_OP_breg25:
21456     case DW_OP_breg26:
21457     case DW_OP_breg27:
21458     case DW_OP_breg28:
21459     case DW_OP_breg29:
21460     case DW_OP_breg30:
21461     case DW_OP_breg31:
21462     case DW_OP_regx:
21463     case DW_OP_fbreg:
21464     case DW_OP_piece:
21465     case DW_OP_deref_size:
21466     case DW_OP_xderef_size:
21467       hash = iterative_hash_object (val1->v.val_int, hash);
21468       break;
21469     case DW_OP_skip:
21470     case DW_OP_bra:
21471       {
21472         int offset;
21473
21474         gcc_assert (val1->val_class == dw_val_class_loc);
21475         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21476         hash = iterative_hash_object (offset, hash);
21477       }
21478       break;
21479     case DW_OP_implicit_value:
21480       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21481       switch (val2->val_class)
21482         {
21483         case dw_val_class_const:
21484           hash = iterative_hash_object (val2->v.val_int, hash);
21485           break;
21486         case dw_val_class_vec:
21487           {
21488             unsigned int elt_size = val2->v.val_vec.elt_size;
21489             unsigned int len = val2->v.val_vec.length;
21490
21491             hash = iterative_hash_object (elt_size, hash);
21492             hash = iterative_hash_object (len, hash);
21493             hash = iterative_hash (val2->v.val_vec.array,
21494                                    len * elt_size, hash);
21495           }
21496           break;
21497         case dw_val_class_const_double:
21498           hash = iterative_hash_object (val2->v.val_double.low, hash);
21499           hash = iterative_hash_object (val2->v.val_double.high, hash);
21500           break;
21501         case dw_val_class_addr:
21502           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21503           break;
21504         default:
21505           gcc_unreachable ();
21506         }
21507       break;
21508     case DW_OP_bregx:
21509     case DW_OP_bit_piece:
21510       hash = iterative_hash_object (val1->v.val_int, hash);
21511       hash = iterative_hash_object (val2->v.val_int, hash);
21512       break;
21513     case DW_OP_addr:
21514     hash_addr:
21515       if (loc->dtprel)
21516         {
21517           unsigned char dtprel = 0xd1;
21518           hash = iterative_hash_object (dtprel, hash);
21519         }
21520       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21521       break;
21522     case DW_OP_GNU_implicit_pointer:
21523       hash = iterative_hash_object (val2->v.val_int, hash);
21524       break;
21525     case DW_OP_GNU_entry_value:
21526       hash = hash_loc_operands (val1->v.val_loc, hash);
21527       break;
21528     case DW_OP_GNU_regval_type:
21529     case DW_OP_GNU_deref_type:
21530       {
21531         unsigned int byte_size
21532           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21533         unsigned int encoding
21534           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21535         hash = iterative_hash_object (val1->v.val_int, hash);
21536         hash = iterative_hash_object (byte_size, hash);
21537         hash = iterative_hash_object (encoding, hash);
21538       }
21539       break;
21540     case DW_OP_GNU_convert:
21541     case DW_OP_GNU_reinterpret:
21542       if (val1->val_class == dw_val_class_unsigned_const)
21543         {
21544           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21545           break;
21546         }
21547       /* FALLTHRU */
21548     case DW_OP_GNU_const_type:
21549       {
21550         unsigned int byte_size
21551           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21552         unsigned int encoding
21553           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21554         hash = iterative_hash_object (byte_size, hash);
21555         hash = iterative_hash_object (encoding, hash);
21556         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21557           break;
21558         hash = iterative_hash_object (val2->val_class, hash);
21559         switch (val2->val_class)
21560           {
21561           case dw_val_class_const:
21562             hash = iterative_hash_object (val2->v.val_int, hash);
21563             break;
21564           case dw_val_class_vec:
21565             {
21566               unsigned int elt_size = val2->v.val_vec.elt_size;
21567               unsigned int len = val2->v.val_vec.length;
21568
21569               hash = iterative_hash_object (elt_size, hash);
21570               hash = iterative_hash_object (len, hash);
21571               hash = iterative_hash (val2->v.val_vec.array,
21572                                      len * elt_size, hash);
21573             }
21574             break;
21575           case dw_val_class_const_double:
21576             hash = iterative_hash_object (val2->v.val_double.low, hash);
21577             hash = iterative_hash_object (val2->v.val_double.high, hash);
21578             break;
21579           default:
21580             gcc_unreachable ();
21581           }
21582       }
21583       break;
21584
21585     default:
21586       /* Other codes have no operands.  */
21587       break;
21588     }
21589   return hash;
21590 }
21591
21592 /* Iteratively hash the whole DWARF location expression LOC.  */
21593
21594 static inline hashval_t
21595 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
21596 {
21597   dw_loc_descr_ref l;
21598   bool sizes_computed = false;
21599   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
21600   size_of_locs (loc);
21601
21602   for (l = loc; l != NULL; l = l->dw_loc_next)
21603     {
21604       enum dwarf_location_atom opc = l->dw_loc_opc;
21605       hash = iterative_hash_object (opc, hash);
21606       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
21607         {
21608           size_of_locs (loc);
21609           sizes_computed = true;
21610         }
21611       hash = hash_loc_operands (l, hash);
21612     }
21613   return hash;
21614 }
21615
21616 /* Compute hash of the whole location list LIST_HEAD.  */
21617
21618 static inline void
21619 hash_loc_list (dw_loc_list_ref list_head)
21620 {
21621   dw_loc_list_ref curr = list_head;
21622   hashval_t hash = 0;
21623
21624   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
21625     {
21626       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
21627       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
21628       if (curr->section)
21629         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
21630                                hash);
21631       hash = hash_locs (curr->expr, hash);
21632     }
21633   list_head->hash = hash;
21634 }
21635
21636 /* Return true if X and Y opcodes have the same operands.  */
21637
21638 static inline bool
21639 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
21640 {
21641   dw_val_ref valx1 = &x->dw_loc_oprnd1;
21642   dw_val_ref valx2 = &x->dw_loc_oprnd2;
21643   dw_val_ref valy1 = &y->dw_loc_oprnd1;
21644   dw_val_ref valy2 = &y->dw_loc_oprnd2;
21645
21646   switch (x->dw_loc_opc)
21647     {
21648     case DW_OP_const4u:
21649     case DW_OP_const8u:
21650       if (x->dtprel)
21651         goto hash_addr;
21652       /* FALLTHRU */
21653     case DW_OP_const1u:
21654     case DW_OP_const1s:
21655     case DW_OP_const2u:
21656     case DW_OP_const2s:
21657     case DW_OP_const4s:
21658     case DW_OP_const8s:
21659     case DW_OP_constu:
21660     case DW_OP_consts:
21661     case DW_OP_pick:
21662     case DW_OP_plus_uconst:
21663     case DW_OP_breg0:
21664     case DW_OP_breg1:
21665     case DW_OP_breg2:
21666     case DW_OP_breg3:
21667     case DW_OP_breg4:
21668     case DW_OP_breg5:
21669     case DW_OP_breg6:
21670     case DW_OP_breg7:
21671     case DW_OP_breg8:
21672     case DW_OP_breg9:
21673     case DW_OP_breg10:
21674     case DW_OP_breg11:
21675     case DW_OP_breg12:
21676     case DW_OP_breg13:
21677     case DW_OP_breg14:
21678     case DW_OP_breg15:
21679     case DW_OP_breg16:
21680     case DW_OP_breg17:
21681     case DW_OP_breg18:
21682     case DW_OP_breg19:
21683     case DW_OP_breg20:
21684     case DW_OP_breg21:
21685     case DW_OP_breg22:
21686     case DW_OP_breg23:
21687     case DW_OP_breg24:
21688     case DW_OP_breg25:
21689     case DW_OP_breg26:
21690     case DW_OP_breg27:
21691     case DW_OP_breg28:
21692     case DW_OP_breg29:
21693     case DW_OP_breg30:
21694     case DW_OP_breg31:
21695     case DW_OP_regx:
21696     case DW_OP_fbreg:
21697     case DW_OP_piece:
21698     case DW_OP_deref_size:
21699     case DW_OP_xderef_size:
21700       return valx1->v.val_int == valy1->v.val_int;
21701     case DW_OP_skip:
21702     case DW_OP_bra:
21703       gcc_assert (valx1->val_class == dw_val_class_loc
21704                   && valy1->val_class == dw_val_class_loc
21705                   && x->dw_loc_addr == y->dw_loc_addr);
21706       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
21707     case DW_OP_implicit_value:
21708       if (valx1->v.val_unsigned != valy1->v.val_unsigned
21709           || valx2->val_class != valy2->val_class)
21710         return false;
21711       switch (valx2->val_class)
21712         {
21713         case dw_val_class_const:
21714           return valx2->v.val_int == valy2->v.val_int;
21715         case dw_val_class_vec:
21716           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
21717                  && valx2->v.val_vec.length == valy2->v.val_vec.length
21718                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
21719                             valx2->v.val_vec.elt_size
21720                             * valx2->v.val_vec.length) == 0;
21721         case dw_val_class_const_double:
21722           return valx2->v.val_double.low == valy2->v.val_double.low
21723                  && valx2->v.val_double.high == valy2->v.val_double.high;
21724         case dw_val_class_addr:
21725           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
21726         default:
21727           gcc_unreachable ();
21728         }
21729     case DW_OP_bregx:
21730     case DW_OP_bit_piece:
21731       return valx1->v.val_int == valy1->v.val_int
21732              && valx2->v.val_int == valy2->v.val_int;
21733     case DW_OP_addr:
21734     hash_addr:
21735       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
21736     case DW_OP_GNU_implicit_pointer:
21737       return valx1->val_class == dw_val_class_die_ref
21738              && valx1->val_class == valy1->val_class
21739              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
21740              && valx2->v.val_int == valy2->v.val_int;
21741     case DW_OP_GNU_entry_value:
21742       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
21743     case DW_OP_GNU_const_type:
21744       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
21745           || valx2->val_class != valy2->val_class)
21746         return false;
21747       switch (valx2->val_class)
21748         {
21749         case dw_val_class_const:
21750           return valx2->v.val_int == valy2->v.val_int;
21751         case dw_val_class_vec:
21752           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
21753                  && valx2->v.val_vec.length == valy2->v.val_vec.length
21754                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
21755                             valx2->v.val_vec.elt_size
21756                             * valx2->v.val_vec.length) == 0;
21757         case dw_val_class_const_double:
21758           return valx2->v.val_double.low == valy2->v.val_double.low
21759                  && valx2->v.val_double.high == valy2->v.val_double.high;
21760         default:
21761           gcc_unreachable ();
21762         }
21763     case DW_OP_GNU_regval_type:
21764     case DW_OP_GNU_deref_type:
21765       return valx1->v.val_int == valy1->v.val_int
21766              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
21767     case DW_OP_GNU_convert:
21768     case DW_OP_GNU_reinterpret:
21769       if (valx1->val_class != valy1->val_class)
21770         return false;
21771       if (valx1->val_class == dw_val_class_unsigned_const)
21772         return valx1->v.val_unsigned == valy1->v.val_unsigned;
21773       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
21774     case DW_OP_GNU_parameter_ref:
21775       return valx1->val_class == dw_val_class_die_ref
21776              && valx1->val_class == valy1->val_class
21777              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
21778     default:
21779       /* Other codes have no operands.  */
21780       return true;
21781     }
21782 }
21783
21784 /* Return true if DWARF location expressions X and Y are the same.  */
21785
21786 static inline bool
21787 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
21788 {
21789   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
21790     if (x->dw_loc_opc != y->dw_loc_opc
21791         || x->dtprel != y->dtprel
21792         || !compare_loc_operands (x, y))
21793       break;
21794   return x == NULL && y == NULL;
21795 }
21796
21797 /* Return precomputed hash of location list X.  */
21798
21799 static hashval_t
21800 loc_list_hash (const void *x)
21801 {
21802   return ((const struct dw_loc_list_struct *) x)->hash;
21803 }
21804
21805 /* Return 1 if location lists X and Y are the same.  */
21806
21807 static int
21808 loc_list_eq (const void *x, const void *y)
21809 {
21810   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
21811   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
21812   if (a == b)
21813     return 1;
21814   if (a->hash != b->hash)
21815     return 0;
21816   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
21817     if (strcmp (a->begin, b->begin) != 0
21818         || strcmp (a->end, b->end) != 0
21819         || (a->section == NULL) != (b->section == NULL)
21820         || (a->section && strcmp (a->section, b->section) != 0)
21821         || !compare_locs (a->expr, b->expr))
21822       break;
21823   return a == NULL && b == NULL;
21824 }
21825
21826 /* Recursively optimize location lists referenced from DIE
21827    children and share them whenever possible.  */
21828
21829 static void
21830 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
21831 {
21832   dw_die_ref c;
21833   dw_attr_ref a;
21834   unsigned ix;
21835   void **slot;
21836
21837   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21838     if (AT_class (a) == dw_val_class_loc_list)
21839       {
21840         dw_loc_list_ref list = AT_loc_list (a);
21841         /* TODO: perform some optimizations here, before hashing
21842            it and storing into the hash table.  */
21843         hash_loc_list (list);
21844         slot = htab_find_slot_with_hash (htab, list, list->hash,
21845                                          INSERT);
21846         if (*slot == NULL)
21847           *slot = (void *) list;
21848         else
21849           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
21850       }
21851
21852   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
21853 }
21854
21855 /* Optimize location lists referenced from DIE
21856    children and share them whenever possible.  */
21857
21858 static void
21859 optimize_location_lists (dw_die_ref die)
21860 {
21861   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
21862   optimize_location_lists_1 (die, htab);
21863   htab_delete (htab);
21864 }
21865 \f
21866 /* Output stuff that dwarf requires at the end of every file,
21867    and generate the DWARF-2 debugging info.  */
21868
21869 static void
21870 dwarf2out_finish (const char *filename)
21871 {
21872   limbo_die_node *node, *next_node;
21873   comdat_type_node *ctnode;
21874   htab_t comdat_type_table;
21875   unsigned int i;
21876
21877   /* PCH might result in DW_AT_producer string being restored from the
21878      header compilation, fix it up if needed.  */
21879   dw_attr_ref producer = get_AT (comp_unit_die (), DW_AT_producer);
21880   if (strcmp (AT_string (producer), producer_string) != 0)
21881     {
21882       struct indirect_string_node *node = find_AT_string (producer_string);
21883       producer->dw_attr_val.v.val_str = node;
21884     }
21885
21886   gen_scheduled_generic_parms_dies ();
21887   gen_remaining_tmpl_value_param_die_attribute ();
21888
21889   /* Add the name for the main input file now.  We delayed this from
21890      dwarf2out_init to avoid complications with PCH.  */
21891   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
21892   if (!IS_ABSOLUTE_PATH (filename))
21893     add_comp_dir_attribute (comp_unit_die ());
21894   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
21895     {
21896       bool p = false;
21897       htab_traverse (file_table, file_table_relative_p, &p);
21898       if (p)
21899         add_comp_dir_attribute (comp_unit_die ());
21900     }
21901
21902   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
21903     {
21904       add_location_or_const_value_attribute (
21905         VEC_index (deferred_locations, deferred_locations_list, i)->die,
21906         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
21907         false,
21908         DW_AT_location);
21909     }
21910
21911   /* Traverse the limbo die list, and add parent/child links.  The only
21912      dies without parents that should be here are concrete instances of
21913      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
21914      For concrete instances, we can get the parent die from the abstract
21915      instance.  */
21916   for (node = limbo_die_list; node; node = next_node)
21917     {
21918       dw_die_ref die = node->die;
21919       next_node = node->next;
21920
21921       if (die->die_parent == NULL)
21922         {
21923           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
21924
21925           if (origin && origin->die_parent)
21926             add_child_die (origin->die_parent, die);
21927           else if (is_cu_die (die))
21928             ;
21929           else if (seen_error ())
21930             /* It's OK to be confused by errors in the input.  */
21931             add_child_die (comp_unit_die (), die);
21932           else
21933             {
21934               /* In certain situations, the lexical block containing a
21935                  nested function can be optimized away, which results
21936                  in the nested function die being orphaned.  Likewise
21937                  with the return type of that nested function.  Force
21938                  this to be a child of the containing function.
21939
21940                  It may happen that even the containing function got fully
21941                  inlined and optimized out.  In that case we are lost and
21942                  assign the empty child.  This should not be big issue as
21943                  the function is likely unreachable too.  */
21944               tree context = NULL_TREE;
21945
21946               gcc_assert (node->created_for);
21947
21948               if (DECL_P (node->created_for))
21949                 context = DECL_CONTEXT (node->created_for);
21950               else if (TYPE_P (node->created_for))
21951                 context = TYPE_CONTEXT (node->created_for);
21952
21953               gcc_assert (context
21954                           && (TREE_CODE (context) == FUNCTION_DECL
21955                               || TREE_CODE (context) == NAMESPACE_DECL));
21956
21957               origin = lookup_decl_die (context);
21958               if (origin)
21959                 add_child_die (origin, die);
21960               else
21961                 add_child_die (comp_unit_die (), die);
21962             }
21963         }
21964     }
21965
21966   limbo_die_list = NULL;
21967
21968 #if ENABLE_ASSERT_CHECKING
21969   {
21970     dw_die_ref die = comp_unit_die (), c;
21971     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
21972   }
21973 #endif
21974   resolve_addr (comp_unit_die ());
21975   move_marked_base_types ();
21976
21977   for (node = deferred_asm_name; node; node = node->next)
21978     {
21979       tree decl = node->created_for;
21980       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
21981         {
21982           add_linkage_attr (node->die, decl);
21983           move_linkage_attr (node->die);
21984         }
21985     }
21986
21987   deferred_asm_name = NULL;
21988
21989   /* Walk through the list of incomplete types again, trying once more to
21990      emit full debugging info for them.  */
21991   retry_incomplete_types ();
21992
21993   if (flag_eliminate_unused_debug_types)
21994     prune_unused_types ();
21995
21996   /* Generate separate CUs for each of the include files we've seen.
21997      They will go into limbo_die_list.  */
21998   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
21999     break_out_includes (comp_unit_die ());
22000
22001   /* Generate separate COMDAT sections for type DIEs. */
22002   if (use_debug_types)
22003     {
22004       break_out_comdat_types (comp_unit_die ());
22005
22006       /* Each new type_unit DIE was added to the limbo die list when created.
22007          Since these have all been added to comdat_type_list, clear the
22008          limbo die list.  */
22009       limbo_die_list = NULL;
22010
22011       /* For each new comdat type unit, copy declarations for incomplete
22012          types to make the new unit self-contained (i.e., no direct
22013          references to the main compile unit).  */
22014       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22015         copy_decls_for_unworthy_types (ctnode->root_die);
22016       copy_decls_for_unworthy_types (comp_unit_die ());
22017
22018       /* In the process of copying declarations from one unit to another,
22019          we may have left some declarations behind that are no longer
22020          referenced.  Prune them.  */
22021       prune_unused_types ();
22022     }
22023
22024   /* Traverse the DIE's and add add sibling attributes to those DIE's
22025      that have children.  */
22026   add_sibling_attributes (comp_unit_die ());
22027   for (node = limbo_die_list; node; node = node->next)
22028     add_sibling_attributes (node->die);
22029   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22030     add_sibling_attributes (ctnode->root_die);
22031
22032   /* Output a terminator label for the .text section.  */
22033   switch_to_section (text_section);
22034   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22035   if (cold_text_section)
22036     {
22037       switch_to_section (cold_text_section);
22038       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22039     }
22040
22041   /* We can only use the low/high_pc attributes if all of the code was
22042      in .text.  */
22043   if (!have_multiple_function_sections 
22044       || (dwarf_version < 3 && dwarf_strict))
22045     {
22046       /* Don't add if the CU has no associated code.  */
22047       if (text_section_used)
22048         {
22049           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22050           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22051         }
22052     }
22053   else
22054     {
22055       unsigned fde_idx;
22056       dw_fde_ref fde;
22057       bool range_list_added = false;
22058
22059       if (text_section_used)
22060         add_ranges_by_labels (comp_unit_die (), text_section_label,
22061                               text_end_label, &range_list_added);
22062       if (cold_text_section_used)
22063         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22064                               cold_end_label, &range_list_added);
22065
22066       FOR_EACH_VEC_ELT (dw_fde_ref, fde_vec, fde_idx, fde)
22067         {
22068           if (!fde->in_std_section)
22069             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22070                                   fde->dw_fde_end, &range_list_added);
22071           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22072             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22073                                   fde->dw_fde_second_end, &range_list_added);
22074         }
22075
22076       if (range_list_added)
22077         {
22078           /* We need to give .debug_loc and .debug_ranges an appropriate
22079              "base address".  Use zero so that these addresses become
22080              absolute.  Historically, we've emitted the unexpected
22081              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22082              Emit both to give time for other tools to adapt.  */
22083           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22084           if (! dwarf_strict && dwarf_version < 4)
22085             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22086
22087           add_ranges (NULL);
22088         }
22089     }
22090
22091   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22092     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22093                     debug_line_section_label);
22094
22095   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22096     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
22097
22098   if (have_location_lists)
22099     optimize_location_lists (comp_unit_die ());
22100
22101   /* Output all of the compilation units.  We put the main one last so that
22102      the offsets are available to output_pubnames.  */
22103   for (node = limbo_die_list; node; node = node->next)
22104     output_comp_unit (node->die, 0);
22105
22106   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22107   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22108     {
22109       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22110
22111       /* Don't output duplicate types.  */
22112       if (*slot != HTAB_EMPTY_ENTRY)
22113         continue;
22114
22115       /* Add a pointer to the line table for the main compilation unit
22116          so that the debugger can make sense of DW_AT_decl_file
22117          attributes.  */
22118       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22119         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22120                         debug_line_section_label);
22121
22122       output_comdat_type_unit (ctnode);
22123       *slot = ctnode;
22124     }
22125   htab_delete (comdat_type_table);
22126
22127   /* Output the main compilation unit if non-empty or if .debug_macinfo
22128      will be emitted.  */
22129   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22130
22131   /* Output the abbreviation table.  */
22132   if (abbrev_die_table_in_use != 1)
22133     {
22134       switch_to_section (debug_abbrev_section);
22135       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22136       output_abbrev_section ();
22137     }
22138
22139   /* Output location list section if necessary.  */
22140   if (have_location_lists)
22141     {
22142       /* Output the location lists info.  */
22143       switch_to_section (debug_loc_section);
22144       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22145                                    DEBUG_LOC_SECTION_LABEL, 0);
22146       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22147       output_location_lists (comp_unit_die ());
22148     }
22149
22150   /* Output public names table if necessary.  */
22151   if (!VEC_empty (pubname_entry, pubname_table))
22152     {
22153       gcc_assert (info_section_emitted);
22154       switch_to_section (debug_pubnames_section);
22155       output_pubnames (pubname_table);
22156     }
22157
22158   /* Output public types table if necessary.  */
22159   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22160      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22161      simply won't look for the section.  */
22162   if (!VEC_empty (pubname_entry, pubtype_table))
22163     {
22164       bool empty = false;
22165       
22166       if (flag_eliminate_unused_debug_types)
22167         {
22168           /* The pubtypes table might be emptied by pruning unused items.  */
22169           unsigned i;
22170           pubname_ref p;
22171           empty = true;
22172           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22173             if (p->die->die_offset != 0)
22174               {
22175                 empty = false;
22176                 break;
22177               }
22178         }
22179       if (!empty)
22180         {
22181           gcc_assert (info_section_emitted);
22182           switch_to_section (debug_pubtypes_section);
22183           output_pubnames (pubtype_table);
22184         }
22185     }
22186
22187   /* Output the address range information if a CU (.debug_info section)
22188      was emitted.  We output an empty table even if we had no functions
22189      to put in it.  This because the consumer has no way to tell the
22190      difference between an empty table that we omitted and failure to
22191      generate a table that would have contained data.  */
22192   if (info_section_emitted)
22193     {
22194       unsigned long aranges_length = size_of_aranges ();
22195
22196       switch_to_section (debug_aranges_section);
22197       output_aranges (aranges_length);
22198     }
22199
22200   /* Output ranges section if necessary.  */
22201   if (ranges_table_in_use)
22202     {
22203       switch_to_section (debug_ranges_section);
22204       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22205       output_ranges ();
22206     }
22207
22208   /* Output the source line correspondence table.  We must do this
22209      even if there is no line information.  Otherwise, on an empty
22210      translation unit, we will generate a present, but empty,
22211      .debug_info section.  IRIX 6.5 `nm' will then complain when
22212      examining the file.  This is done late so that any filenames
22213      used by the debug_info section are marked as 'used'.  */
22214   switch_to_section (debug_line_section);
22215   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22216   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22217     output_line_info ();
22218
22219   /* Have to end the macro section.  */
22220   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22221     {
22222       switch_to_section (debug_macinfo_section);
22223       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22224       if (!VEC_empty (macinfo_entry, macinfo_table))
22225         output_macinfo ();
22226       dw2_asm_output_data (1, 0, "End compilation unit");
22227     }
22228
22229   /* If we emitted any DW_FORM_strp form attribute, output the string
22230      table too.  */
22231   if (debug_str_hash)
22232     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22233 }
22234
22235 #include "gt-dwarf2out.h"