OSDN Git Service

dwarf2out: Emit NOTE_INSN_CFI_* both with and without cfi-asm.
[pf3gnuchains/gcc-fork.git] / gcc / dwarf2out.c
1 /* Output Dwarf2 format symbol table information from GCC.
2    Copyright (C) 1992, 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
3    2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5    Contributed by Gary Funck (gary@intrepid.com).
6    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
7    Extensively modified by Jason Merrill (jason@cygnus.com).
8
9 This file is part of GCC.
10
11 GCC is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 3, or (at your option) any later
14 version.
15
16 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with GCC; see the file COPYING3.  If not see
23 <http://www.gnu.org/licenses/>.  */
24
25 /* TODO: Emit .debug_line header even when there are no functions, since
26            the file numbers are used by .debug_info.  Alternately, leave
27            out locations for types and decls.
28          Avoid talking about ctors and op= for PODs.
29          Factor out common prologue sequences into multiple CIEs.  */
30
31 /* The first part of this file deals with the DWARF 2 frame unwind
32    information, which is also used by the GCC efficient exception handling
33    mechanism.  The second part, controlled only by an #ifdef
34    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
35    information.  */
36
37 /* DWARF2 Abbreviation Glossary:
38
39    CFA = Canonical Frame Address
40            a fixed address on the stack which identifies a call frame.
41            We define it to be the value of SP just before the call insn.
42            The CFA register and offset, which may change during the course
43            of the function, are used to calculate its value at runtime.
44
45    CFI = Call Frame Instruction
46            an instruction for the DWARF2 abstract machine
47
48    CIE = Common Information Entry
49            information describing information common to one or more FDEs
50
51    DIE = Debugging Information Entry
52
53    FDE = Frame Description Entry
54            information describing the stack call frame, in particular,
55            how to restore registers
56
57    DW_CFA_... = DWARF2 CFA call frame instruction
58    DW_TAG_... = DWARF2 DIE tag */
59
60 #include "config.h"
61 #include "system.h"
62 #include "coretypes.h"
63 #include "tm.h"
64 #include "tree.h"
65 #include "version.h"
66 #include "flags.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "ggc.h"
82 #include "md5.h"
83 #include "tm_p.h"
84 #include "diagnostic.h"
85 #include "tree-pretty-print.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "common/common-target.h"
89 #include "langhooks.h"
90 #include "hashtab.h"
91 #include "cgraph.h"
92 #include "input.h"
93 #include "gimple.h"
94 #include "tree-pass.h"
95 #include "tree-flow.h"
96 #include "cfglayout.h"
97
98 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
99 static rtx last_var_location_insn;
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 /* ??? Poison these here until it can be done generically.  They've been
118    totally replaced in this file; make sure it stays that way.  */
119 #undef DWARF2_UNWIND_INFO
120 #undef DWARF2_FRAME_INFO
121 #if (GCC_VERSION >= 3000)
122  #pragma GCC poison DWARF2_UNWIND_INFO DWARF2_FRAME_INFO
123 #endif
124
125 #ifndef INCOMING_RETURN_ADDR_RTX
126 #define INCOMING_RETURN_ADDR_RTX  (gcc_unreachable (), NULL_RTX)
127 #endif
128
129 /* Map register numbers held in the call frame info that gcc has
130    collected using DWARF_FRAME_REGNUM to those that should be output in
131    .debug_frame and .eh_frame.  */
132 #ifndef DWARF2_FRAME_REG_OUT
133 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
134 #endif
135
136 /* The size of the target's pointer type.  */
137 #ifndef PTR_SIZE
138 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
139 #endif
140
141 /* Array of RTXes referenced by the debugging information, which therefore
142    must be kept around forever.  */
143 static GTY(()) VEC(rtx,gc) *used_rtx_array;
144
145 /* A pointer to the base of a list of incomplete types which might be
146    completed at some later time.  incomplete_types_list needs to be a
147    VEC(tree,gc) because we want to tell the garbage collector about
148    it.  */
149 static GTY(()) VEC(tree,gc) *incomplete_types;
150
151 /* A pointer to the base of a table of references to declaration
152    scopes.  This table is a display which tracks the nesting
153    of declaration scopes at the current scope and containing
154    scopes.  This table is used to find the proper place to
155    define type declaration DIE's.  */
156 static GTY(()) VEC(tree,gc) *decl_scope_table;
157
158 /* Pointers to various DWARF2 sections.  */
159 static GTY(()) section *debug_info_section;
160 static GTY(()) section *debug_abbrev_section;
161 static GTY(()) section *debug_aranges_section;
162 static GTY(()) section *debug_macinfo_section;
163 static GTY(()) section *debug_line_section;
164 static GTY(()) section *debug_loc_section;
165 static GTY(()) section *debug_pubnames_section;
166 static GTY(()) section *debug_pubtypes_section;
167 static GTY(()) section *debug_str_section;
168 static GTY(()) section *debug_ranges_section;
169 static GTY(()) section *debug_frame_section;
170
171 /* How to start an assembler comment.  */
172 #ifndef ASM_COMMENT_START
173 #define ASM_COMMENT_START ";#"
174 #endif
175
176 /* Maximum size (in bytes) of an artificially generated label.  */
177 #define MAX_ARTIFICIAL_LABEL_BYTES      30
178
179 /* The size of addresses as they appear in the Dwarf 2 data.
180    Some architectures use word addresses to refer to code locations,
181    but Dwarf 2 info always uses byte addresses.  On such machines,
182    Dwarf 2 addresses need to be larger than the architecture's
183    pointers.  */
184 #ifndef DWARF2_ADDR_SIZE
185 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
186 #endif
187
188 /* The size in bytes of a DWARF field indicating an offset or length
189    relative to a debug info section, specified to be 4 bytes in the
190    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
191    as PTR_SIZE.  */
192
193 #ifndef DWARF_OFFSET_SIZE
194 #define DWARF_OFFSET_SIZE 4
195 #endif
196
197 /* The size in bytes of a DWARF 4 type signature.  */
198
199 #ifndef DWARF_TYPE_SIGNATURE_SIZE
200 #define DWARF_TYPE_SIGNATURE_SIZE 8
201 #endif
202
203 /* According to the (draft) DWARF 3 specification, the initial length
204    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
205    bytes are 0xffffffff, followed by the length stored in the next 8
206    bytes.
207
208    However, the SGI/MIPS ABI uses an initial length which is equal to
209    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
210
211 #ifndef DWARF_INITIAL_LENGTH_SIZE
212 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
213 #endif
214
215 /* Round SIZE up to the nearest BOUNDARY.  */
216 #define DWARF_ROUND(SIZE,BOUNDARY) \
217   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
218
219 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
220 #ifndef DWARF_CIE_DATA_ALIGNMENT
221 #ifdef STACK_GROWS_DOWNWARD
222 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
223 #else
224 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
225 #endif
226 #endif
227
228 /* CIE identifier.  */
229 #if HOST_BITS_PER_WIDE_INT >= 64
230 #define DWARF_CIE_ID \
231   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
232 #else
233 #define DWARF_CIE_ID DW_CIE_ID
234 #endif
235
236 /* A pointer to the base of a table that contains frame description
237    information for each routine.  */
238 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
239
240 /* Number of elements currently allocated for fde_table.  */
241 static GTY(()) unsigned fde_table_allocated;
242
243 /* Number of elements in fde_table currently in use.  */
244 static GTY(()) unsigned fde_table_in_use;
245
246 /* Size (in elements) of increments by which we may expand the
247    fde_table.  */
248 #define FDE_TABLE_INCREMENT 256
249
250 /* Get the current fde_table entry we should use.  */
251
252 dw_fde_ref
253 current_fde (void)
254 {
255   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
256 }
257
258 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
259    attribute that accelerates the lookup of the FDE associated
260    with the subprogram.  This variable holds the table index of the FDE
261    associated with the current function (body) definition.  */
262 static unsigned current_funcdef_fde;
263
264 struct GTY(()) indirect_string_node {
265   const char *str;
266   unsigned int refcount;
267   enum dwarf_form form;
268   char *label;
269 };
270
271 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
272
273 static GTY(()) int dw2_string_counter;
274
275 /* True if the compilation unit places functions in more than one section.  */
276 static GTY(()) bool have_multiple_function_sections = false;
277
278 /* Whether the default text and cold text sections have been used at all.  */
279
280 static GTY(()) bool text_section_used = false;
281 static GTY(()) bool cold_text_section_used = false;
282
283 /* The default cold text section.  */
284 static GTY(()) section *cold_text_section;
285
286 /* Forward declarations for functions defined in this file.  */
287
288 static char *stripattributes (const char *);
289 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
290 static void output_call_frame_info (int);
291 static void dwarf2out_note_section_used (void);
292
293 /* Support for complex CFA locations.  */
294 static void output_cfa_loc (dw_cfi_ref, int);
295 static void output_cfa_loc_raw (dw_cfi_ref);
296
297 /* Personality decl of current unit.  Used only when assembler does not support
298    personality CFI.  */
299 static GTY(()) rtx current_unit_personality;
300
301 /* How to start an assembler comment.  */
302 #ifndef ASM_COMMENT_START
303 #define ASM_COMMENT_START ";#"
304 #endif
305
306 /* Data and reference forms for relocatable data.  */
307 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
308 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
309
310 #ifndef DEBUG_FRAME_SECTION
311 #define DEBUG_FRAME_SECTION     ".debug_frame"
312 #endif
313
314 #ifndef FUNC_BEGIN_LABEL
315 #define FUNC_BEGIN_LABEL        "LFB"
316 #endif
317
318 #ifndef FUNC_END_LABEL
319 #define FUNC_END_LABEL          "LFE"
320 #endif
321
322 #ifndef PROLOGUE_END_LABEL
323 #define PROLOGUE_END_LABEL      "LPE"
324 #endif
325
326 #ifndef EPILOGUE_BEGIN_LABEL
327 #define EPILOGUE_BEGIN_LABEL    "LEB"
328 #endif
329
330 #ifndef FRAME_BEGIN_LABEL
331 #define FRAME_BEGIN_LABEL       "Lframe"
332 #endif
333 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
334 #define CIE_END_LABEL           "LECIE"
335 #define FDE_LABEL               "LSFDE"
336 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
337 #define FDE_END_LABEL           "LEFDE"
338 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
339 #define LINE_NUMBER_END_LABEL   "LELT"
340 #define LN_PROLOG_AS_LABEL      "LASLTP"
341 #define LN_PROLOG_END_LABEL     "LELTP"
342 #define DIE_LABEL_PREFIX        "DW"
343
344 /* The DWARF 2 CFA column which tracks the return address.  Normally this
345    is the column for PC, or the first column after all of the hard
346    registers.  */
347 #ifndef DWARF_FRAME_RETURN_COLUMN
348 #ifdef PC_REGNUM
349 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
350 #else
351 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
352 #endif
353 #endif
354
355 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
356    default, we just provide columns for all registers.  */
357 #ifndef DWARF_FRAME_REGNUM
358 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
359 #endif
360 \f
361 /* Match the base name of a file to the base name of a compilation unit. */
362
363 static int
364 matches_main_base (const char *path)
365 {
366   /* Cache the last query. */
367   static const char *last_path = NULL;
368   static int last_match = 0;
369   if (path != last_path)
370     {
371       const char *base;
372       int length = base_of_path (path, &base);
373       last_path = path;
374       last_match = (length == main_input_baselength
375                     && memcmp (base, main_input_basename, length) == 0);
376     }
377   return last_match;
378 }
379
380 #ifdef DEBUG_DEBUG_STRUCT
381
382 static int
383 dump_struct_debug (tree type, enum debug_info_usage usage,
384                    enum debug_struct_file criterion, int generic,
385                    int matches, int result)
386 {
387   /* Find the type name. */
388   tree type_decl = TYPE_STUB_DECL (type);
389   tree t = type_decl;
390   const char *name = 0;
391   if (TREE_CODE (t) == TYPE_DECL)
392     t = DECL_NAME (t);
393   if (t)
394     name = IDENTIFIER_POINTER (t);
395
396   fprintf (stderr, "    struct %d %s %s %s %s %d %p %s\n",
397            criterion,
398            DECL_IN_SYSTEM_HEADER (type_decl) ? "sys" : "usr",
399            matches ? "bas" : "hdr",
400            generic ? "gen" : "ord",
401            usage == DINFO_USAGE_DFN ? ";" :
402              usage == DINFO_USAGE_DIR_USE ? "." : "*",
403            result,
404            (void*) type_decl, name);
405   return result;
406 }
407 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
408   dump_struct_debug (type, usage, criterion, generic, matches, result)
409
410 #else
411
412 #define DUMP_GSTRUCT(type, usage, criterion, generic, matches, result) \
413   (result)
414
415 #endif
416
417 static bool
418 should_emit_struct_debug (tree type, enum debug_info_usage usage)
419 {
420   enum debug_struct_file criterion;
421   tree type_decl;
422   bool generic = lang_hooks.types.generic_p (type);
423
424   if (generic)
425     criterion = debug_struct_generic[usage];
426   else
427     criterion = debug_struct_ordinary[usage];
428
429   if (criterion == DINFO_STRUCT_FILE_NONE)
430     return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
431   if (criterion == DINFO_STRUCT_FILE_ANY)
432     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
433
434   type_decl = TYPE_STUB_DECL (TYPE_MAIN_VARIANT (type));
435
436   if (criterion == DINFO_STRUCT_FILE_SYS && DECL_IN_SYSTEM_HEADER (type_decl))
437     return DUMP_GSTRUCT (type, usage, criterion, generic, false, true);
438
439   if (matches_main_base (DECL_SOURCE_FILE (type_decl)))
440     return DUMP_GSTRUCT (type, usage, criterion, generic, true, true);
441   return DUMP_GSTRUCT (type, usage, criterion, generic, false, false);
442 }
443 \f
444 /* Return a pointer to a copy of the section string name S with all
445    attributes stripped off, and an asterisk prepended (for assemble_name).  */
446
447 static inline char *
448 stripattributes (const char *s)
449 {
450   char *stripped = XNEWVEC (char, strlen (s) + 2);
451   char *p = stripped;
452
453   *p++ = '*';
454
455   while (*s && *s != ',')
456     *p++ = *s++;
457
458   *p = '\0';
459   return stripped;
460 }
461
462 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
463
464 static inline HOST_WIDE_INT
465 div_data_align (HOST_WIDE_INT off)
466 {
467   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
468   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
469   return r;
470 }
471
472 /* Return true if we need a signed version of a given opcode
473    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
474
475 static inline bool
476 need_data_align_sf_opcode (HOST_WIDE_INT off)
477 {
478   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
479 }
480
481 /* Convert a DWARF call frame info. operation to its string name */
482
483 static const char *
484 dwarf_cfi_name (unsigned int cfi_opc)
485 {
486   switch (cfi_opc)
487     {
488     case DW_CFA_advance_loc:
489       return "DW_CFA_advance_loc";
490     case DW_CFA_offset:
491       return "DW_CFA_offset";
492     case DW_CFA_restore:
493       return "DW_CFA_restore";
494     case DW_CFA_nop:
495       return "DW_CFA_nop";
496     case DW_CFA_set_loc:
497       return "DW_CFA_set_loc";
498     case DW_CFA_advance_loc1:
499       return "DW_CFA_advance_loc1";
500     case DW_CFA_advance_loc2:
501       return "DW_CFA_advance_loc2";
502     case DW_CFA_advance_loc4:
503       return "DW_CFA_advance_loc4";
504     case DW_CFA_offset_extended:
505       return "DW_CFA_offset_extended";
506     case DW_CFA_restore_extended:
507       return "DW_CFA_restore_extended";
508     case DW_CFA_undefined:
509       return "DW_CFA_undefined";
510     case DW_CFA_same_value:
511       return "DW_CFA_same_value";
512     case DW_CFA_register:
513       return "DW_CFA_register";
514     case DW_CFA_remember_state:
515       return "DW_CFA_remember_state";
516     case DW_CFA_restore_state:
517       return "DW_CFA_restore_state";
518     case DW_CFA_def_cfa:
519       return "DW_CFA_def_cfa";
520     case DW_CFA_def_cfa_register:
521       return "DW_CFA_def_cfa_register";
522     case DW_CFA_def_cfa_offset:
523       return "DW_CFA_def_cfa_offset";
524
525     /* DWARF 3 */
526     case DW_CFA_def_cfa_expression:
527       return "DW_CFA_def_cfa_expression";
528     case DW_CFA_expression:
529       return "DW_CFA_expression";
530     case DW_CFA_offset_extended_sf:
531       return "DW_CFA_offset_extended_sf";
532     case DW_CFA_def_cfa_sf:
533       return "DW_CFA_def_cfa_sf";
534     case DW_CFA_def_cfa_offset_sf:
535       return "DW_CFA_def_cfa_offset_sf";
536
537     /* SGI/MIPS specific */
538     case DW_CFA_MIPS_advance_loc8:
539       return "DW_CFA_MIPS_advance_loc8";
540
541     /* GNU extensions */
542     case DW_CFA_GNU_window_save:
543       return "DW_CFA_GNU_window_save";
544     case DW_CFA_GNU_args_size:
545       return "DW_CFA_GNU_args_size";
546     case DW_CFA_GNU_negative_offset_extended:
547       return "DW_CFA_GNU_negative_offset_extended";
548
549     default:
550       return "DW_CFA_<unknown>";
551     }
552 }
553
554 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
555    switch to the data section instead, and write out a synthetic start label
556    for collect2 the first time around.  */
557
558 static void
559 switch_to_eh_frame_section (bool back)
560 {
561   tree label;
562
563 #ifdef EH_FRAME_SECTION_NAME
564   if (eh_frame_section == 0)
565     {
566       int flags;
567
568       if (EH_TABLES_CAN_BE_READ_ONLY)
569         {
570           int fde_encoding;
571           int per_encoding;
572           int lsda_encoding;
573
574           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
575                                                        /*global=*/0);
576           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
577                                                        /*global=*/1);
578           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
579                                                         /*global=*/0);
580           flags = ((! flag_pic
581                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
582                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
583                         && (per_encoding & 0x70) != DW_EH_PE_absptr
584                         && (per_encoding & 0x70) != DW_EH_PE_aligned
585                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
586                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
587                    ? 0 : SECTION_WRITE);
588         }
589       else
590         flags = SECTION_WRITE;
591       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
592     }
593 #endif /* EH_FRAME_SECTION_NAME */
594
595   if (eh_frame_section)
596     switch_to_section (eh_frame_section);
597   else
598     {
599       /* We have no special eh_frame section.  Put the information in
600          the data section and emit special labels to guide collect2.  */
601       switch_to_section (data_section);
602
603       if (!back)
604         {
605           label = get_file_function_name ("F");
606           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
607           targetm.asm_out.globalize_label (asm_out_file,
608                                            IDENTIFIER_POINTER (label));
609           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
610         }
611     }
612 }
613
614 /* Switch [BACK] to the eh or debug frame table section, depending on
615    FOR_EH.  */
616
617 static void
618 switch_to_frame_table_section (int for_eh, bool back)
619 {
620   if (for_eh)
621     switch_to_eh_frame_section (back);
622   else
623     {
624       if (!debug_frame_section)
625         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
626                                            SECTION_DEBUG, NULL);
627       switch_to_section (debug_frame_section);
628     }
629 }
630
631 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
632
633 enum dw_cfi_oprnd_type
634 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
635 {
636   switch (cfi)
637     {
638     case DW_CFA_nop:
639     case DW_CFA_GNU_window_save:
640     case DW_CFA_remember_state:
641     case DW_CFA_restore_state:
642       return dw_cfi_oprnd_unused;
643
644     case DW_CFA_set_loc:
645     case DW_CFA_advance_loc1:
646     case DW_CFA_advance_loc2:
647     case DW_CFA_advance_loc4:
648     case DW_CFA_MIPS_advance_loc8:
649       return dw_cfi_oprnd_addr;
650
651     case DW_CFA_offset:
652     case DW_CFA_offset_extended:
653     case DW_CFA_def_cfa:
654     case DW_CFA_offset_extended_sf:
655     case DW_CFA_def_cfa_sf:
656     case DW_CFA_restore:
657     case DW_CFA_restore_extended:
658     case DW_CFA_undefined:
659     case DW_CFA_same_value:
660     case DW_CFA_def_cfa_register:
661     case DW_CFA_register:
662     case DW_CFA_expression:
663       return dw_cfi_oprnd_reg_num;
664
665     case DW_CFA_def_cfa_offset:
666     case DW_CFA_GNU_args_size:
667     case DW_CFA_def_cfa_offset_sf:
668       return dw_cfi_oprnd_offset;
669
670     case DW_CFA_def_cfa_expression:
671       return dw_cfi_oprnd_loc;
672
673     default:
674       gcc_unreachable ();
675     }
676 }
677
678 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
679
680 enum dw_cfi_oprnd_type
681 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
682 {
683   switch (cfi)
684     {
685     case DW_CFA_def_cfa:
686     case DW_CFA_def_cfa_sf:
687     case DW_CFA_offset:
688     case DW_CFA_offset_extended_sf:
689     case DW_CFA_offset_extended:
690       return dw_cfi_oprnd_offset;
691
692     case DW_CFA_register:
693       return dw_cfi_oprnd_reg_num;
694
695     case DW_CFA_expression:
696       return dw_cfi_oprnd_loc;
697
698     default:
699       return dw_cfi_oprnd_unused;
700     }
701 }
702
703 /* Output a Call Frame Information opcode and its operand(s).  */
704
705 static void
706 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
707 {
708   unsigned long r;
709   HOST_WIDE_INT off;
710
711   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
712     dw2_asm_output_data (1, (cfi->dw_cfi_opc
713                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
714                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
715                          ((unsigned HOST_WIDE_INT)
716                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
717   else if (cfi->dw_cfi_opc == DW_CFA_offset)
718     {
719       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
720       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
721                            "DW_CFA_offset, column %#lx", r);
722       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
723       dw2_asm_output_data_uleb128 (off, NULL);
724     }
725   else if (cfi->dw_cfi_opc == DW_CFA_restore)
726     {
727       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
728       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
729                            "DW_CFA_restore, column %#lx", r);
730     }
731   else
732     {
733       dw2_asm_output_data (1, cfi->dw_cfi_opc,
734                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
735
736       switch (cfi->dw_cfi_opc)
737         {
738         case DW_CFA_set_loc:
739           if (for_eh)
740             dw2_asm_output_encoded_addr_rtx (
741                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
742                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
743                 false, NULL);
744           else
745             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
746                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
747           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
748           break;
749
750         case DW_CFA_advance_loc1:
751           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
752                                 fde->dw_fde_current_label, NULL);
753           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
754           break;
755
756         case DW_CFA_advance_loc2:
757           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
758                                 fde->dw_fde_current_label, NULL);
759           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
760           break;
761
762         case DW_CFA_advance_loc4:
763           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
764                                 fde->dw_fde_current_label, NULL);
765           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
766           break;
767
768         case DW_CFA_MIPS_advance_loc8:
769           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
770                                 fde->dw_fde_current_label, NULL);
771           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
772           break;
773
774         case DW_CFA_offset_extended:
775           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
776           dw2_asm_output_data_uleb128 (r, NULL);
777           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
778           dw2_asm_output_data_uleb128 (off, NULL);
779           break;
780
781         case DW_CFA_def_cfa:
782           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
783           dw2_asm_output_data_uleb128 (r, NULL);
784           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
785           break;
786
787         case DW_CFA_offset_extended_sf:
788           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
789           dw2_asm_output_data_uleb128 (r, NULL);
790           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
791           dw2_asm_output_data_sleb128 (off, NULL);
792           break;
793
794         case DW_CFA_def_cfa_sf:
795           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
796           dw2_asm_output_data_uleb128 (r, NULL);
797           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
798           dw2_asm_output_data_sleb128 (off, NULL);
799           break;
800
801         case DW_CFA_restore_extended:
802         case DW_CFA_undefined:
803         case DW_CFA_same_value:
804         case DW_CFA_def_cfa_register:
805           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
806           dw2_asm_output_data_uleb128 (r, NULL);
807           break;
808
809         case DW_CFA_register:
810           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
811           dw2_asm_output_data_uleb128 (r, NULL);
812           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
813           dw2_asm_output_data_uleb128 (r, NULL);
814           break;
815
816         case DW_CFA_def_cfa_offset:
817         case DW_CFA_GNU_args_size:
818           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
819           break;
820
821         case DW_CFA_def_cfa_offset_sf:
822           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
823           dw2_asm_output_data_sleb128 (off, NULL);
824           break;
825
826         case DW_CFA_GNU_window_save:
827           break;
828
829         case DW_CFA_def_cfa_expression:
830         case DW_CFA_expression:
831           output_cfa_loc (cfi, for_eh);
832           break;
833
834         case DW_CFA_GNU_negative_offset_extended:
835           /* Obsoleted by DW_CFA_offset_extended_sf.  */
836           gcc_unreachable ();
837
838         default:
839           break;
840         }
841     }
842 }
843
844 /* Similar, but do it via assembler directives instead.  */
845
846 static void
847 output_cfi_directive (dw_cfi_ref cfi)
848 {
849   unsigned long r, r2;
850
851   switch (cfi->dw_cfi_opc)
852     {
853     case DW_CFA_advance_loc:
854     case DW_CFA_advance_loc1:
855     case DW_CFA_advance_loc2:
856     case DW_CFA_advance_loc4:
857     case DW_CFA_MIPS_advance_loc8:
858     case DW_CFA_set_loc:
859       /* Should only be created by add_fde_cfi in a code path not
860          followed when emitting via directives.  The assembler is
861          going to take care of this for us.  */
862       gcc_unreachable ();
863
864     case DW_CFA_offset:
865     case DW_CFA_offset_extended:
866     case DW_CFA_offset_extended_sf:
867       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
868       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
869                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
870       break;
871
872     case DW_CFA_restore:
873     case DW_CFA_restore_extended:
874       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
875       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
876       break;
877
878     case DW_CFA_undefined:
879       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
880       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
881       break;
882
883     case DW_CFA_same_value:
884       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
885       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
886       break;
887
888     case DW_CFA_def_cfa:
889     case DW_CFA_def_cfa_sf:
890       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
891       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
892                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
893       break;
894
895     case DW_CFA_def_cfa_register:
896       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
897       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
898       break;
899
900     case DW_CFA_register:
901       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
902       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
903       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
904       break;
905
906     case DW_CFA_def_cfa_offset:
907     case DW_CFA_def_cfa_offset_sf:
908       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
909                HOST_WIDE_INT_PRINT_DEC"\n",
910                cfi->dw_cfi_oprnd1.dw_cfi_offset);
911       break;
912
913     case DW_CFA_remember_state:
914       fprintf (asm_out_file, "\t.cfi_remember_state\n");
915       break;
916     case DW_CFA_restore_state:
917       fprintf (asm_out_file, "\t.cfi_restore_state\n");
918       break;
919
920     case DW_CFA_GNU_args_size:
921       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
922       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
923       if (flag_debug_asm)
924         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
925                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
926       fputc ('\n', asm_out_file);
927       break;
928
929     case DW_CFA_GNU_window_save:
930       fprintf (asm_out_file, "\t.cfi_window_save\n");
931       break;
932
933     case DW_CFA_def_cfa_expression:
934     case DW_CFA_expression:
935       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
936       output_cfa_loc_raw (cfi);
937       fputc ('\n', asm_out_file);
938       break;
939
940     default:
941       gcc_unreachable ();
942     }
943 }
944
945 void
946 dwarf2out_emit_cfi (dw_cfi_ref cfi)
947 {
948   if (dwarf2out_do_cfi_asm ())
949     output_cfi_directive (cfi);
950 }
951
952 /* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
953    same state as after executing CFIs in CFI chain.  DO_CFI_ASM is
954    true if .cfi_* directives shall be emitted, false otherwise.  If it
955    is false, FDE and FOR_EH are the other arguments to pass to
956    output_cfi.  */
957
958 static void
959 output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
960              dw_fde_ref fde, bool for_eh)
961 {
962   int ix;
963   struct dw_cfi_struct cfi_buf;
964   dw_cfi_ref cfi2;
965   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
966   VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
967   unsigned int len, idx;
968
969   for (ix = 0; ix < upto + 1; ix++)
970     {
971       dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
972       switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
973         {
974         case DW_CFA_advance_loc:
975         case DW_CFA_advance_loc1:
976         case DW_CFA_advance_loc2:
977         case DW_CFA_advance_loc4:
978         case DW_CFA_MIPS_advance_loc8:
979         case DW_CFA_set_loc:
980           /* All advances should be ignored.  */
981           break;
982         case DW_CFA_remember_state:
983           {
984             dw_cfi_ref args_size = cfi_args_size;
985
986             /* Skip everything between .cfi_remember_state and
987                .cfi_restore_state.  */
988             ix++;
989             if (ix == upto)
990               goto flush_all;
991
992             for (; ix < upto; ix++)
993               {
994                 cfi2 = VEC_index (dw_cfi_ref, vec, ix);
995                 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
996                   break;
997                 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
998                   args_size = cfi2;
999                 else
1000                   gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
1001               }
1002
1003             cfi_args_size = args_size;
1004             break;
1005           }
1006         case DW_CFA_GNU_args_size:
1007           cfi_args_size = cfi;
1008           break;
1009         case DW_CFA_GNU_window_save:
1010           goto flush_all;
1011         case DW_CFA_offset:
1012         case DW_CFA_offset_extended:
1013         case DW_CFA_offset_extended_sf:
1014         case DW_CFA_restore:
1015         case DW_CFA_restore_extended:
1016         case DW_CFA_undefined:
1017         case DW_CFA_same_value:
1018         case DW_CFA_register:
1019         case DW_CFA_val_offset:
1020         case DW_CFA_val_offset_sf:
1021         case DW_CFA_expression:
1022         case DW_CFA_val_expression:
1023         case DW_CFA_GNU_negative_offset_extended:
1024           if (VEC_length (dw_cfi_ref, regs)
1025               <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
1026             VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
1027                                    cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
1028           VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1029                        cfi);
1030           break;
1031         case DW_CFA_def_cfa:
1032         case DW_CFA_def_cfa_sf:
1033         case DW_CFA_def_cfa_expression:
1034           cfi_cfa = cfi;
1035           cfi_cfa_offset = cfi;
1036           break;
1037         case DW_CFA_def_cfa_register:
1038           cfi_cfa = cfi;
1039           break;
1040         case DW_CFA_def_cfa_offset:
1041         case DW_CFA_def_cfa_offset_sf:
1042           cfi_cfa_offset = cfi;
1043           break;
1044         case DW_CFA_nop:
1045           gcc_assert (cfi == NULL);
1046         flush_all:
1047           len = VEC_length (dw_cfi_ref, regs);
1048           for (idx = 0; idx < len; idx++)
1049             {
1050               cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
1051               if (cfi2 != NULL
1052                   && cfi2->dw_cfi_opc != DW_CFA_restore
1053                   && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
1054                 {
1055                   if (do_cfi_asm)
1056                     output_cfi_directive (cfi2);
1057                   else
1058                     output_cfi (cfi2, fde, for_eh);
1059                 }
1060             }
1061           if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
1062             {
1063               gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
1064               cfi_buf = *cfi_cfa;
1065               switch (cfi_cfa_offset->dw_cfi_opc)
1066                 {
1067                 case DW_CFA_def_cfa_offset:
1068                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
1069                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
1070                   break;
1071                 case DW_CFA_def_cfa_offset_sf:
1072                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
1073                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
1074                   break;
1075                 case DW_CFA_def_cfa:
1076                 case DW_CFA_def_cfa_sf:
1077                   cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
1078                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
1079                   break;
1080                 default:
1081                   gcc_unreachable ();
1082                 }
1083               cfi_cfa = &cfi_buf;
1084             }
1085           else if (cfi_cfa_offset)
1086             cfi_cfa = cfi_cfa_offset;
1087           if (cfi_cfa)
1088             {
1089               if (do_cfi_asm)
1090                 output_cfi_directive (cfi_cfa);
1091               else
1092                 output_cfi (cfi_cfa, fde, for_eh);
1093             }
1094           cfi_cfa = NULL;
1095           cfi_cfa_offset = NULL;
1096           if (cfi_args_size
1097               && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
1098             {
1099               if (do_cfi_asm)
1100                 output_cfi_directive (cfi_args_size);
1101               else
1102                 output_cfi (cfi_args_size, fde, for_eh);
1103             }
1104           cfi_args_size = NULL;
1105           if (cfi == NULL)
1106             {
1107               VEC_free (dw_cfi_ref, heap, regs);
1108               return;
1109             }
1110           else if (do_cfi_asm)
1111             output_cfi_directive (cfi);
1112           else
1113             output_cfi (cfi, fde, for_eh);
1114           break;
1115         default:
1116           gcc_unreachable ();
1117         }
1118     }
1119 }
1120
1121 /* Like output_cfis, but emit all CFIs in the vector.  */
1122 static void
1123 output_all_cfis (cfi_vec vec, bool do_cfi_asm,
1124                  dw_fde_ref fde, bool for_eh)
1125 {
1126   output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
1127 }
1128
1129 /* Output one FDE.  */
1130
1131 static void
1132 output_fde (dw_fde_ref fde, bool for_eh, bool second,
1133             char *section_start_label, int fde_encoding, char *augmentation,
1134             bool any_lsda_needed, int lsda_encoding)
1135 {
1136   int ix;
1137   const char *begin, *end;
1138   static unsigned int j;
1139   char l1[20], l2[20];
1140   dw_cfi_ref cfi;
1141
1142   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
1143                                      /* empty */ 0);
1144   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
1145                                   for_eh + j);
1146   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
1147   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
1148   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
1149     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
1150                          " indicating 64-bit DWARF extension");
1151   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1152                         "FDE Length");
1153   ASM_OUTPUT_LABEL (asm_out_file, l1);
1154
1155   if (for_eh)
1156     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
1157   else
1158     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
1159                            debug_frame_section, "FDE CIE offset");
1160
1161   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
1162   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
1163
1164   if (for_eh)
1165     {
1166       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
1167       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
1168       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
1169                                        "FDE initial location");
1170       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
1171                             end, begin, "FDE address range");
1172     }
1173   else
1174     {
1175       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
1176       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
1177     }
1178
1179   if (augmentation[0])
1180     {
1181       if (any_lsda_needed)
1182         {
1183           int size = size_of_encoded_value (lsda_encoding);
1184
1185           if (lsda_encoding == DW_EH_PE_aligned)
1186             {
1187               int offset = (  4         /* Length */
1188                             + 4         /* CIE offset */
1189                             + 2 * size_of_encoded_value (fde_encoding)
1190                             + 1         /* Augmentation size */ );
1191               int pad = -offset & (PTR_SIZE - 1);
1192
1193               size += pad;
1194               gcc_assert (size_of_uleb128 (size) == 1);
1195             }
1196
1197           dw2_asm_output_data_uleb128 (size, "Augmentation size");
1198
1199           if (fde->uses_eh_lsda)
1200             {
1201               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
1202                                            fde->funcdef_number);
1203               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
1204                                                gen_rtx_SYMBOL_REF (Pmode, l1),
1205                                                false,
1206                                                "Language Specific Data Area");
1207             }
1208           else
1209             {
1210               if (lsda_encoding == DW_EH_PE_aligned)
1211                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
1212               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
1213                                    "Language Specific Data Area (none)");
1214             }
1215         }
1216       else
1217         dw2_asm_output_data_uleb128 (0, "Augmentation size");
1218     }
1219
1220   /* Loop through the Call Frame Instructions associated with
1221      this FDE.  */
1222   fde->dw_fde_current_label = begin;
1223   if (fde->dw_fde_second_begin == NULL)
1224     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1225       output_cfi (cfi, fde, for_eh);
1226   else if (!second)
1227     {
1228       if (fde->dw_fde_switch_cfi_index > 0)
1229         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1230           {
1231             if (ix == fde->dw_fde_switch_cfi_index)
1232               break;
1233             output_cfi (cfi, fde, for_eh);
1234           }
1235     }
1236   else
1237     {
1238       int i, from = 0;
1239       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
1240
1241       if (fde->dw_fde_switch_cfi_index > 0)
1242         {
1243           from = fde->dw_fde_switch_cfi_index;
1244           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
1245         }
1246       for (i = from; i < until; i++)
1247         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
1248                     fde, for_eh);
1249     }
1250
1251   /* If we are to emit a ref/link from function bodies to their frame tables,
1252      do it now.  This is typically performed to make sure that tables
1253      associated with functions are dragged with them and not discarded in
1254      garbage collecting links. We need to do this on a per function basis to
1255      cope with -ffunction-sections.  */
1256
1257 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
1258   /* Switch to the function section, emit the ref to the tables, and
1259      switch *back* into the table section.  */
1260   switch_to_section (function_section (fde->decl));
1261   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
1262   switch_to_frame_table_section (for_eh, true);
1263 #endif
1264
1265   /* Pad the FDE out to an address sized boundary.  */
1266   ASM_OUTPUT_ALIGN (asm_out_file,
1267                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
1268   ASM_OUTPUT_LABEL (asm_out_file, l2);
1269
1270   j += 2;
1271 }
1272
1273 /* Return true if frame description entry FDE is needed for EH.  */
1274
1275 static bool
1276 fde_needed_for_eh_p (dw_fde_ref fde)
1277 {
1278   if (flag_asynchronous_unwind_tables)
1279     return true;
1280
1281   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
1282     return true;
1283
1284   if (fde->uses_eh_lsda)
1285     return true;
1286
1287   /* If exceptions are enabled, we have collected nothrow info.  */
1288   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
1289     return false;
1290
1291   return true;
1292 }
1293
1294 /* Output the call frame information used to record information
1295    that relates to calculating the frame pointer, and records the
1296    location of saved registers.  */
1297
1298 static void
1299 output_call_frame_info (int for_eh)
1300 {
1301   unsigned int i;
1302   dw_fde_ref fde;
1303   dw_cfi_ref cfi;
1304   char l1[20], l2[20], section_start_label[20];
1305   bool any_lsda_needed = false;
1306   char augmentation[6];
1307   int augmentation_size;
1308   int fde_encoding = DW_EH_PE_absptr;
1309   int per_encoding = DW_EH_PE_absptr;
1310   int lsda_encoding = DW_EH_PE_absptr;
1311   int return_reg;
1312   rtx personality = NULL;
1313   int dw_cie_version;
1314
1315   /* Don't emit a CIE if there won't be any FDEs.  */
1316   if (fde_table_in_use == 0)
1317     return;
1318
1319   /* Nothing to do if the assembler's doing it all.  */
1320   if (dwarf2out_do_cfi_asm ())
1321     return;
1322
1323   /* If we don't have any functions we'll want to unwind out of, don't emit
1324      any EH unwind information.  If we make FDEs linkonce, we may have to
1325      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
1326      want to avoid having an FDE kept around when the function it refers to
1327      is discarded.  Example where this matters: a primary function template
1328      in C++ requires EH information, an explicit specialization doesn't.  */
1329   if (for_eh)
1330     {
1331       bool any_eh_needed = false;
1332
1333       for (i = 0; i < fde_table_in_use; i++)
1334         if (fde_table[i].uses_eh_lsda)
1335           any_eh_needed = any_lsda_needed = true;
1336         else if (fde_needed_for_eh_p (&fde_table[i]))
1337           any_eh_needed = true;
1338         else if (TARGET_USES_WEAK_UNWIND_INFO)
1339           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
1340                                              1, 1);
1341
1342       if (!any_eh_needed)
1343         return;
1344     }
1345
1346   /* We're going to be generating comments, so turn on app.  */
1347   if (flag_debug_asm)
1348     app_enable ();
1349
1350   /* Switch to the proper frame section, first time.  */
1351   switch_to_frame_table_section (for_eh, false);
1352
1353   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
1354   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
1355
1356   /* Output the CIE.  */
1357   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
1358   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
1359   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
1360     dw2_asm_output_data (4, 0xffffffff,
1361       "Initial length escape value indicating 64-bit DWARF extension");
1362   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1363                         "Length of Common Information Entry");
1364   ASM_OUTPUT_LABEL (asm_out_file, l1);
1365
1366   /* Now that the CIE pointer is PC-relative for EH,
1367      use 0 to identify the CIE.  */
1368   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
1369                        (for_eh ? 0 : DWARF_CIE_ID),
1370                        "CIE Identifier Tag");
1371
1372   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
1373      use CIE version 1, unless that would produce incorrect results
1374      due to overflowing the return register column.  */
1375   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
1376   dw_cie_version = 1;
1377   if (return_reg >= 256 || dwarf_version > 2)
1378     dw_cie_version = 3;
1379   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
1380
1381   augmentation[0] = 0;
1382   augmentation_size = 0;
1383
1384   personality = current_unit_personality;
1385   if (for_eh)
1386     {
1387       char *p;
1388
1389       /* Augmentation:
1390          z      Indicates that a uleb128 is present to size the
1391                 augmentation section.
1392          L      Indicates the encoding (and thus presence) of
1393                 an LSDA pointer in the FDE augmentation.
1394          R      Indicates a non-default pointer encoding for
1395                 FDE code pointers.
1396          P      Indicates the presence of an encoding + language
1397                 personality routine in the CIE augmentation.  */
1398
1399       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
1400       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
1401       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1402
1403       p = augmentation + 1;
1404       if (personality)
1405         {
1406           *p++ = 'P';
1407           augmentation_size += 1 + size_of_encoded_value (per_encoding);
1408           assemble_external_libcall (personality);
1409         }
1410       if (any_lsda_needed)
1411         {
1412           *p++ = 'L';
1413           augmentation_size += 1;
1414         }
1415       if (fde_encoding != DW_EH_PE_absptr)
1416         {
1417           *p++ = 'R';
1418           augmentation_size += 1;
1419         }
1420       if (p > augmentation + 1)
1421         {
1422           augmentation[0] = 'z';
1423           *p = '\0';
1424         }
1425
1426       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
1427       if (personality && per_encoding == DW_EH_PE_aligned)
1428         {
1429           int offset = (  4             /* Length */
1430                         + 4             /* CIE Id */
1431                         + 1             /* CIE version */
1432                         + strlen (augmentation) + 1     /* Augmentation */
1433                         + size_of_uleb128 (1)           /* Code alignment */
1434                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
1435                         + 1             /* RA column */
1436                         + 1             /* Augmentation size */
1437                         + 1             /* Personality encoding */ );
1438           int pad = -offset & (PTR_SIZE - 1);
1439
1440           augmentation_size += pad;
1441
1442           /* Augmentations should be small, so there's scarce need to
1443              iterate for a solution.  Die if we exceed one uleb128 byte.  */
1444           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
1445         }
1446     }
1447
1448   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
1449   if (dw_cie_version >= 4)
1450     {
1451       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
1452       dw2_asm_output_data (1, 0, "CIE Segment Size");
1453     }
1454   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
1455   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
1456                                "CIE Data Alignment Factor");
1457
1458   if (dw_cie_version == 1)
1459     dw2_asm_output_data (1, return_reg, "CIE RA Column");
1460   else
1461     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
1462
1463   if (augmentation[0])
1464     {
1465       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
1466       if (personality)
1467         {
1468           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
1469                                eh_data_format_name (per_encoding));
1470           dw2_asm_output_encoded_addr_rtx (per_encoding,
1471                                            personality,
1472                                            true, NULL);
1473         }
1474
1475       if (any_lsda_needed)
1476         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
1477                              eh_data_format_name (lsda_encoding));
1478
1479       if (fde_encoding != DW_EH_PE_absptr)
1480         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
1481                              eh_data_format_name (fde_encoding));
1482     }
1483
1484   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
1485     output_cfi (cfi, NULL, for_eh);
1486
1487   /* Pad the CIE out to an address sized boundary.  */
1488   ASM_OUTPUT_ALIGN (asm_out_file,
1489                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
1490   ASM_OUTPUT_LABEL (asm_out_file, l2);
1491
1492   /* Loop through all of the FDE's.  */
1493   for (i = 0; i < fde_table_in_use; i++)
1494     {
1495       unsigned int k;
1496       fde = &fde_table[i];
1497
1498       /* Don't emit EH unwind info for leaf functions that don't need it.  */
1499       if (for_eh && !fde_needed_for_eh_p (fde))
1500         continue;
1501
1502       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
1503         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
1504                     augmentation, any_lsda_needed, lsda_encoding);
1505     }
1506
1507   if (for_eh && targetm.terminate_dw2_eh_frame_info)
1508     dw2_asm_output_data (4, 0, "End of Table");
1509 #ifdef MIPS_DEBUGGING_INFO
1510   /* Work around Irix 6 assembler bug whereby labels at the end of a section
1511      get a value of 0.  Putting .align 0 after the label fixes it.  */
1512   ASM_OUTPUT_ALIGN (asm_out_file, 0);
1513 #endif
1514
1515   /* Turn off app to make assembly quicker.  */
1516   if (flag_debug_asm)
1517     app_disable ();
1518 }
1519
1520 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
1521
1522 static void
1523 dwarf2out_do_cfi_startproc (bool second)
1524 {
1525   int enc;
1526   rtx ref;
1527   rtx personality = get_personality_function (current_function_decl);
1528
1529   fprintf (asm_out_file, "\t.cfi_startproc\n");
1530
1531   if (personality)
1532     {
1533       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
1534       ref = personality;
1535
1536       /* ??? The GAS support isn't entirely consistent.  We have to
1537          handle indirect support ourselves, but PC-relative is done
1538          in the assembler.  Further, the assembler can't handle any
1539          of the weirder relocation types.  */
1540       if (enc & DW_EH_PE_indirect)
1541         ref = dw2_force_const_mem (ref, true);
1542
1543       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1544       output_addr_const (asm_out_file, ref);
1545       fputc ('\n', asm_out_file);
1546     }
1547
1548   if (crtl->uses_eh_lsda)
1549     {
1550       char lab[20];
1551
1552       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1553       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1554                                    current_function_funcdef_no);
1555       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1556       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1557
1558       if (enc & DW_EH_PE_indirect)
1559         ref = dw2_force_const_mem (ref, true);
1560
1561       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1562       output_addr_const (asm_out_file, ref);
1563       fputc ('\n', asm_out_file);
1564     }
1565 }
1566
1567 /* Output a marker (i.e. a label) for the beginning of a function, before
1568    the prologue.  */
1569
1570 void
1571 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1572                           const char *file ATTRIBUTE_UNUSED)
1573 {
1574   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1575   char * dup_label;
1576   dw_fde_ref fde;
1577   section *fnsec;
1578   bool do_frame;
1579
1580   current_function_func_begin_label = NULL;
1581
1582   do_frame = dwarf2out_do_frame ();
1583
1584   /* ??? current_function_func_begin_label is also used by except.c for
1585      call-site information.  We must emit this label if it might be used.  */
1586   if (!do_frame
1587       && (!flag_exceptions
1588           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
1589     return;
1590
1591   fnsec = function_section (current_function_decl);
1592   switch_to_section (fnsec);
1593   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1594                                current_function_funcdef_no);
1595   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1596                           current_function_funcdef_no);
1597   dup_label = xstrdup (label);
1598   current_function_func_begin_label = dup_label;
1599
1600   /* We can elide the fde allocation if we're not emitting debug info.  */
1601   if (!do_frame)
1602     return;
1603
1604   /* Expand the fde table if necessary.  */
1605   if (fde_table_in_use == fde_table_allocated)
1606     {
1607       fde_table_allocated += FDE_TABLE_INCREMENT;
1608       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
1609       memset (fde_table + fde_table_in_use, 0,
1610               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
1611     }
1612
1613   /* Record the FDE associated with this function.  */
1614   current_funcdef_fde = fde_table_in_use;
1615
1616   /* Add the new FDE at the end of the fde_table.  */
1617   fde = &fde_table[fde_table_in_use++];
1618   fde->decl = current_function_decl;
1619   fde->dw_fde_begin = dup_label;
1620   fde->dw_fde_end = NULL;
1621   fde->dw_fde_current_label = dup_label;
1622   fde->dw_fde_second_begin = NULL;
1623   fde->dw_fde_second_end = NULL;
1624   fde->dw_fde_vms_end_prologue = NULL;
1625   fde->dw_fde_vms_begin_epilogue = NULL;
1626   fde->dw_fde_cfi = VEC_alloc (dw_cfi_ref, gc, 20);
1627   fde->dw_fde_switch_cfi_index = 0;
1628   fde->funcdef_number = current_function_funcdef_no;
1629   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1630   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1631   fde->nothrow = crtl->nothrow;
1632   fde->drap_reg = INVALID_REGNUM;
1633   fde->vdrap_reg = INVALID_REGNUM;
1634   fde->in_std_section = (fnsec == text_section
1635                          || (cold_text_section && fnsec == cold_text_section));
1636   fde->second_in_std_section = 0;
1637
1638   dwarf2cfi_function_init ();
1639
1640   /* We only want to output line number information for the genuine dwarf2
1641      prologue case, not the eh frame case.  */
1642 #ifdef DWARF2_DEBUGGING_INFO
1643   if (file)
1644     dwarf2out_source_line (line, file, 0, true);
1645 #endif
1646
1647   if (dwarf2out_do_cfi_asm ())
1648     dwarf2out_do_cfi_startproc (false);
1649   else
1650     {
1651       rtx personality = get_personality_function (current_function_decl);
1652       if (!current_unit_personality)
1653         current_unit_personality = personality;
1654
1655       /* We cannot keep a current personality per function as without CFI
1656          asm, at the point where we emit the CFI data, there is no current
1657          function anymore.  */
1658       if (personality && current_unit_personality != personality)
1659         sorry ("multiple EH personalities are supported only with assemblers "
1660                "supporting .cfi_personality directive");
1661     }
1662 }
1663
1664 /* Output a marker (i.e. a label) for the end of the generated code
1665    for a function prologue.  This gets called *after* the prologue code has
1666    been generated.  */
1667
1668 void
1669 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1670                         const char *file ATTRIBUTE_UNUSED)
1671 {
1672   dw_fde_ref fde;
1673   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1674
1675   /* Output a label to mark the endpoint of the code generated for this
1676      function.  */
1677   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1678                                current_function_funcdef_no);
1679   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1680                           current_function_funcdef_no);
1681   fde = &fde_table[fde_table_in_use - 1];
1682   fde->dw_fde_vms_end_prologue = xstrdup (label);
1683 }
1684
1685 /* Output a marker (i.e. a label) for the beginning of the generated code
1686    for a function epilogue.  This gets called *before* the prologue code has
1687    been generated.  */
1688
1689 void
1690 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1691                           const char *file ATTRIBUTE_UNUSED)
1692 {
1693   dw_fde_ref fde;
1694   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1695
1696   fde = &fde_table[fde_table_in_use - 1];
1697   if (fde->dw_fde_vms_begin_epilogue)
1698     return;
1699
1700   /* Output a label to mark the endpoint of the code generated for this
1701      function.  */
1702   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1703                                current_function_funcdef_no);
1704   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1705                           current_function_funcdef_no);
1706   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1707 }
1708
1709 /* Output a marker (i.e. a label) for the absolute end of the generated code
1710    for a function definition.  This gets called *after* the epilogue code has
1711    been generated.  */
1712
1713 void
1714 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1715                         const char *file ATTRIBUTE_UNUSED)
1716 {
1717   dw_fde_ref fde;
1718   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1719
1720   last_var_location_insn = NULL_RTX;
1721
1722   if (dwarf2out_do_cfi_asm ())
1723     fprintf (asm_out_file, "\t.cfi_endproc\n");
1724
1725   /* Output a label to mark the endpoint of the code generated for this
1726      function.  */
1727   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1728                                current_function_funcdef_no);
1729   ASM_OUTPUT_LABEL (asm_out_file, label);
1730   fde = current_fde ();
1731   gcc_assert (fde != NULL);
1732   if (fde->dw_fde_second_begin == NULL)
1733     fde->dw_fde_end = xstrdup (label);
1734 }
1735
1736 void
1737 dwarf2out_frame_init (void)
1738 {
1739   /* Allocate the initial hunk of the fde_table.  */
1740   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
1741   fde_table_allocated = FDE_TABLE_INCREMENT;
1742   fde_table_in_use = 0;
1743
1744   dwarf2cfi_frame_init ();
1745 }
1746
1747 void
1748 dwarf2out_frame_finish (void)
1749 {
1750   /* Output call frame information.  */
1751   if (targetm.debug_unwind_info () == UI_DWARF2)
1752     output_call_frame_info (0);
1753
1754   /* Output another copy for the unwinder.  */
1755   if ((flag_unwind_tables || flag_exceptions)
1756       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1757     output_call_frame_info (1);
1758 }
1759
1760 /* Note that the current function section is being used for code.  */
1761
1762 static void
1763 dwarf2out_note_section_used (void)
1764 {
1765   section *sec = current_function_section ();
1766   if (sec == text_section)
1767     text_section_used = true;
1768   else if (sec == cold_text_section)
1769     cold_text_section_used = true;
1770 }
1771
1772 static void var_location_switch_text_section (void);
1773 static void set_cur_line_info_table (section *);
1774
1775 void
1776 dwarf2out_switch_text_section (void)
1777 {
1778   section *sect;
1779   dw_fde_ref fde = current_fde ();
1780
1781   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1782
1783   if (!in_cold_section_p)
1784     {
1785       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1786       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1787       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1788     }
1789   else
1790     {
1791       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1792       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1793       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1794     }
1795   have_multiple_function_sections = true;
1796
1797   /* There is no need to mark used sections when not debugging.  */
1798   if (cold_text_section != NULL)
1799     dwarf2out_note_section_used ();
1800
1801   if (dwarf2out_do_cfi_asm ())
1802     fprintf (asm_out_file, "\t.cfi_endproc\n");
1803
1804   /* Now do the real section switch.  */
1805   sect = current_function_section ();
1806   switch_to_section (sect);
1807
1808   fde->second_in_std_section
1809     = (sect == text_section
1810        || (cold_text_section && sect == cold_text_section));
1811
1812   if (dwarf2out_do_cfi_asm ())
1813     {
1814       dwarf2out_do_cfi_startproc (true);
1815       /* As this is a different FDE, insert all current CFI instructions
1816          again.  */
1817       output_all_cfis (fde->dw_fde_cfi, true, fde, true);
1818     }
1819   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
1820   var_location_switch_text_section ();
1821
1822   set_cur_line_info_table (sect);
1823 }
1824 \f
1825 /* And now, the subset of the debugging information support code necessary
1826    for emitting location expressions.  */
1827
1828 /* Data about a single source file.  */
1829 struct GTY(()) dwarf_file_data {
1830   const char * filename;
1831   int emitted_number;
1832 };
1833
1834 typedef struct GTY(()) deferred_locations_struct
1835 {
1836   tree variable;
1837   dw_die_ref die;
1838 } deferred_locations;
1839
1840 DEF_VEC_O(deferred_locations);
1841 DEF_VEC_ALLOC_O(deferred_locations,gc);
1842
1843 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1844
1845 DEF_VEC_P(dw_die_ref);
1846 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1847
1848 /* Location lists are ranges + location descriptions for that range,
1849    so you can track variables that are in different places over
1850    their entire life.  */
1851 typedef struct GTY(()) dw_loc_list_struct {
1852   dw_loc_list_ref dw_loc_next;
1853   const char *begin; /* Label for begin address of range */
1854   const char *end;  /* Label for end address of range */
1855   char *ll_symbol; /* Label for beginning of location list.
1856                       Only on head of list */
1857   const char *section; /* Section this loclist is relative to */
1858   dw_loc_descr_ref expr;
1859   hashval_t hash;
1860   /* True if all addresses in this and subsequent lists are known to be
1861      resolved.  */
1862   bool resolved_addr;
1863   /* True if this list has been replaced by dw_loc_next.  */
1864   bool replaced;
1865   bool emitted;
1866   /* True if the range should be emitted even if begin and end
1867      are the same.  */
1868   bool force;
1869 } dw_loc_list_node;
1870
1871 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1872
1873 /* Convert a DWARF stack opcode into its string name.  */
1874
1875 static const char *
1876 dwarf_stack_op_name (unsigned int op)
1877 {
1878   switch (op)
1879     {
1880     case DW_OP_addr:
1881       return "DW_OP_addr";
1882     case DW_OP_deref:
1883       return "DW_OP_deref";
1884     case DW_OP_const1u:
1885       return "DW_OP_const1u";
1886     case DW_OP_const1s:
1887       return "DW_OP_const1s";
1888     case DW_OP_const2u:
1889       return "DW_OP_const2u";
1890     case DW_OP_const2s:
1891       return "DW_OP_const2s";
1892     case DW_OP_const4u:
1893       return "DW_OP_const4u";
1894     case DW_OP_const4s:
1895       return "DW_OP_const4s";
1896     case DW_OP_const8u:
1897       return "DW_OP_const8u";
1898     case DW_OP_const8s:
1899       return "DW_OP_const8s";
1900     case DW_OP_constu:
1901       return "DW_OP_constu";
1902     case DW_OP_consts:
1903       return "DW_OP_consts";
1904     case DW_OP_dup:
1905       return "DW_OP_dup";
1906     case DW_OP_drop:
1907       return "DW_OP_drop";
1908     case DW_OP_over:
1909       return "DW_OP_over";
1910     case DW_OP_pick:
1911       return "DW_OP_pick";
1912     case DW_OP_swap:
1913       return "DW_OP_swap";
1914     case DW_OP_rot:
1915       return "DW_OP_rot";
1916     case DW_OP_xderef:
1917       return "DW_OP_xderef";
1918     case DW_OP_abs:
1919       return "DW_OP_abs";
1920     case DW_OP_and:
1921       return "DW_OP_and";
1922     case DW_OP_div:
1923       return "DW_OP_div";
1924     case DW_OP_minus:
1925       return "DW_OP_minus";
1926     case DW_OP_mod:
1927       return "DW_OP_mod";
1928     case DW_OP_mul:
1929       return "DW_OP_mul";
1930     case DW_OP_neg:
1931       return "DW_OP_neg";
1932     case DW_OP_not:
1933       return "DW_OP_not";
1934     case DW_OP_or:
1935       return "DW_OP_or";
1936     case DW_OP_plus:
1937       return "DW_OP_plus";
1938     case DW_OP_plus_uconst:
1939       return "DW_OP_plus_uconst";
1940     case DW_OP_shl:
1941       return "DW_OP_shl";
1942     case DW_OP_shr:
1943       return "DW_OP_shr";
1944     case DW_OP_shra:
1945       return "DW_OP_shra";
1946     case DW_OP_xor:
1947       return "DW_OP_xor";
1948     case DW_OP_bra:
1949       return "DW_OP_bra";
1950     case DW_OP_eq:
1951       return "DW_OP_eq";
1952     case DW_OP_ge:
1953       return "DW_OP_ge";
1954     case DW_OP_gt:
1955       return "DW_OP_gt";
1956     case DW_OP_le:
1957       return "DW_OP_le";
1958     case DW_OP_lt:
1959       return "DW_OP_lt";
1960     case DW_OP_ne:
1961       return "DW_OP_ne";
1962     case DW_OP_skip:
1963       return "DW_OP_skip";
1964     case DW_OP_lit0:
1965       return "DW_OP_lit0";
1966     case DW_OP_lit1:
1967       return "DW_OP_lit1";
1968     case DW_OP_lit2:
1969       return "DW_OP_lit2";
1970     case DW_OP_lit3:
1971       return "DW_OP_lit3";
1972     case DW_OP_lit4:
1973       return "DW_OP_lit4";
1974     case DW_OP_lit5:
1975       return "DW_OP_lit5";
1976     case DW_OP_lit6:
1977       return "DW_OP_lit6";
1978     case DW_OP_lit7:
1979       return "DW_OP_lit7";
1980     case DW_OP_lit8:
1981       return "DW_OP_lit8";
1982     case DW_OP_lit9:
1983       return "DW_OP_lit9";
1984     case DW_OP_lit10:
1985       return "DW_OP_lit10";
1986     case DW_OP_lit11:
1987       return "DW_OP_lit11";
1988     case DW_OP_lit12:
1989       return "DW_OP_lit12";
1990     case DW_OP_lit13:
1991       return "DW_OP_lit13";
1992     case DW_OP_lit14:
1993       return "DW_OP_lit14";
1994     case DW_OP_lit15:
1995       return "DW_OP_lit15";
1996     case DW_OP_lit16:
1997       return "DW_OP_lit16";
1998     case DW_OP_lit17:
1999       return "DW_OP_lit17";
2000     case DW_OP_lit18:
2001       return "DW_OP_lit18";
2002     case DW_OP_lit19:
2003       return "DW_OP_lit19";
2004     case DW_OP_lit20:
2005       return "DW_OP_lit20";
2006     case DW_OP_lit21:
2007       return "DW_OP_lit21";
2008     case DW_OP_lit22:
2009       return "DW_OP_lit22";
2010     case DW_OP_lit23:
2011       return "DW_OP_lit23";
2012     case DW_OP_lit24:
2013       return "DW_OP_lit24";
2014     case DW_OP_lit25:
2015       return "DW_OP_lit25";
2016     case DW_OP_lit26:
2017       return "DW_OP_lit26";
2018     case DW_OP_lit27:
2019       return "DW_OP_lit27";
2020     case DW_OP_lit28:
2021       return "DW_OP_lit28";
2022     case DW_OP_lit29:
2023       return "DW_OP_lit29";
2024     case DW_OP_lit30:
2025       return "DW_OP_lit30";
2026     case DW_OP_lit31:
2027       return "DW_OP_lit31";
2028     case DW_OP_reg0:
2029       return "DW_OP_reg0";
2030     case DW_OP_reg1:
2031       return "DW_OP_reg1";
2032     case DW_OP_reg2:
2033       return "DW_OP_reg2";
2034     case DW_OP_reg3:
2035       return "DW_OP_reg3";
2036     case DW_OP_reg4:
2037       return "DW_OP_reg4";
2038     case DW_OP_reg5:
2039       return "DW_OP_reg5";
2040     case DW_OP_reg6:
2041       return "DW_OP_reg6";
2042     case DW_OP_reg7:
2043       return "DW_OP_reg7";
2044     case DW_OP_reg8:
2045       return "DW_OP_reg8";
2046     case DW_OP_reg9:
2047       return "DW_OP_reg9";
2048     case DW_OP_reg10:
2049       return "DW_OP_reg10";
2050     case DW_OP_reg11:
2051       return "DW_OP_reg11";
2052     case DW_OP_reg12:
2053       return "DW_OP_reg12";
2054     case DW_OP_reg13:
2055       return "DW_OP_reg13";
2056     case DW_OP_reg14:
2057       return "DW_OP_reg14";
2058     case DW_OP_reg15:
2059       return "DW_OP_reg15";
2060     case DW_OP_reg16:
2061       return "DW_OP_reg16";
2062     case DW_OP_reg17:
2063       return "DW_OP_reg17";
2064     case DW_OP_reg18:
2065       return "DW_OP_reg18";
2066     case DW_OP_reg19:
2067       return "DW_OP_reg19";
2068     case DW_OP_reg20:
2069       return "DW_OP_reg20";
2070     case DW_OP_reg21:
2071       return "DW_OP_reg21";
2072     case DW_OP_reg22:
2073       return "DW_OP_reg22";
2074     case DW_OP_reg23:
2075       return "DW_OP_reg23";
2076     case DW_OP_reg24:
2077       return "DW_OP_reg24";
2078     case DW_OP_reg25:
2079       return "DW_OP_reg25";
2080     case DW_OP_reg26:
2081       return "DW_OP_reg26";
2082     case DW_OP_reg27:
2083       return "DW_OP_reg27";
2084     case DW_OP_reg28:
2085       return "DW_OP_reg28";
2086     case DW_OP_reg29:
2087       return "DW_OP_reg29";
2088     case DW_OP_reg30:
2089       return "DW_OP_reg30";
2090     case DW_OP_reg31:
2091       return "DW_OP_reg31";
2092     case DW_OP_breg0:
2093       return "DW_OP_breg0";
2094     case DW_OP_breg1:
2095       return "DW_OP_breg1";
2096     case DW_OP_breg2:
2097       return "DW_OP_breg2";
2098     case DW_OP_breg3:
2099       return "DW_OP_breg3";
2100     case DW_OP_breg4:
2101       return "DW_OP_breg4";
2102     case DW_OP_breg5:
2103       return "DW_OP_breg5";
2104     case DW_OP_breg6:
2105       return "DW_OP_breg6";
2106     case DW_OP_breg7:
2107       return "DW_OP_breg7";
2108     case DW_OP_breg8:
2109       return "DW_OP_breg8";
2110     case DW_OP_breg9:
2111       return "DW_OP_breg9";
2112     case DW_OP_breg10:
2113       return "DW_OP_breg10";
2114     case DW_OP_breg11:
2115       return "DW_OP_breg11";
2116     case DW_OP_breg12:
2117       return "DW_OP_breg12";
2118     case DW_OP_breg13:
2119       return "DW_OP_breg13";
2120     case DW_OP_breg14:
2121       return "DW_OP_breg14";
2122     case DW_OP_breg15:
2123       return "DW_OP_breg15";
2124     case DW_OP_breg16:
2125       return "DW_OP_breg16";
2126     case DW_OP_breg17:
2127       return "DW_OP_breg17";
2128     case DW_OP_breg18:
2129       return "DW_OP_breg18";
2130     case DW_OP_breg19:
2131       return "DW_OP_breg19";
2132     case DW_OP_breg20:
2133       return "DW_OP_breg20";
2134     case DW_OP_breg21:
2135       return "DW_OP_breg21";
2136     case DW_OP_breg22:
2137       return "DW_OP_breg22";
2138     case DW_OP_breg23:
2139       return "DW_OP_breg23";
2140     case DW_OP_breg24:
2141       return "DW_OP_breg24";
2142     case DW_OP_breg25:
2143       return "DW_OP_breg25";
2144     case DW_OP_breg26:
2145       return "DW_OP_breg26";
2146     case DW_OP_breg27:
2147       return "DW_OP_breg27";
2148     case DW_OP_breg28:
2149       return "DW_OP_breg28";
2150     case DW_OP_breg29:
2151       return "DW_OP_breg29";
2152     case DW_OP_breg30:
2153       return "DW_OP_breg30";
2154     case DW_OP_breg31:
2155       return "DW_OP_breg31";
2156     case DW_OP_regx:
2157       return "DW_OP_regx";
2158     case DW_OP_fbreg:
2159       return "DW_OP_fbreg";
2160     case DW_OP_bregx:
2161       return "DW_OP_bregx";
2162     case DW_OP_piece:
2163       return "DW_OP_piece";
2164     case DW_OP_deref_size:
2165       return "DW_OP_deref_size";
2166     case DW_OP_xderef_size:
2167       return "DW_OP_xderef_size";
2168     case DW_OP_nop:
2169       return "DW_OP_nop";
2170
2171     case DW_OP_push_object_address:
2172       return "DW_OP_push_object_address";
2173     case DW_OP_call2:
2174       return "DW_OP_call2";
2175     case DW_OP_call4:
2176       return "DW_OP_call4";
2177     case DW_OP_call_ref:
2178       return "DW_OP_call_ref";
2179     case DW_OP_implicit_value:
2180       return "DW_OP_implicit_value";
2181     case DW_OP_stack_value:
2182       return "DW_OP_stack_value";
2183     case DW_OP_form_tls_address:
2184       return "DW_OP_form_tls_address";
2185     case DW_OP_call_frame_cfa:
2186       return "DW_OP_call_frame_cfa";
2187     case DW_OP_bit_piece:
2188       return "DW_OP_bit_piece";
2189
2190     case DW_OP_GNU_push_tls_address:
2191       return "DW_OP_GNU_push_tls_address";
2192     case DW_OP_GNU_uninit:
2193       return "DW_OP_GNU_uninit";
2194     case DW_OP_GNU_encoded_addr:
2195       return "DW_OP_GNU_encoded_addr";
2196     case DW_OP_GNU_implicit_pointer:
2197       return "DW_OP_GNU_implicit_pointer";
2198     case DW_OP_GNU_entry_value:
2199       return "DW_OP_GNU_entry_value";
2200     case DW_OP_GNU_const_type:
2201       return "DW_OP_GNU_const_type";
2202     case DW_OP_GNU_regval_type:
2203       return "DW_OP_GNU_regval_type";
2204     case DW_OP_GNU_deref_type:
2205       return "DW_OP_GNU_deref_type";
2206     case DW_OP_GNU_convert:
2207       return "DW_OP_GNU_convert";
2208     case DW_OP_GNU_reinterpret:
2209       return "DW_OP_GNU_reinterpret";
2210     case DW_OP_GNU_parameter_ref:
2211       return "DW_OP_GNU_parameter_ref";
2212
2213     default:
2214       return "OP_<unknown>";
2215     }
2216 }
2217
2218 /* Return a pointer to a newly allocated location description.  Location
2219    descriptions are simple expression terms that can be strung
2220    together to form more complicated location (address) descriptions.  */
2221
2222 static inline dw_loc_descr_ref
2223 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2224                unsigned HOST_WIDE_INT oprnd2)
2225 {
2226   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
2227
2228   descr->dw_loc_opc = op;
2229   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2230   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2231   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2232   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2233
2234   return descr;
2235 }
2236
2237 /* Return a pointer to a newly allocated location description for
2238    REG and OFFSET.  */
2239
2240 static inline dw_loc_descr_ref
2241 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
2242 {
2243   if (reg <= 31)
2244     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
2245                           offset, 0);
2246   else
2247     return new_loc_descr (DW_OP_bregx, reg, offset);
2248 }
2249
2250 /* Add a location description term to a location description expression.  */
2251
2252 static inline void
2253 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2254 {
2255   dw_loc_descr_ref *d;
2256
2257   /* Find the end of the chain.  */
2258   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2259     ;
2260
2261   *d = descr;
2262 }
2263
2264 /* Add a constant OFFSET to a location expression.  */
2265
2266 static void
2267 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
2268 {
2269   dw_loc_descr_ref loc;
2270   HOST_WIDE_INT *p;
2271
2272   gcc_assert (*list_head != NULL);
2273
2274   if (!offset)
2275     return;
2276
2277   /* Find the end of the chain.  */
2278   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
2279     ;
2280
2281   p = NULL;
2282   if (loc->dw_loc_opc == DW_OP_fbreg
2283       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
2284     p = &loc->dw_loc_oprnd1.v.val_int;
2285   else if (loc->dw_loc_opc == DW_OP_bregx)
2286     p = &loc->dw_loc_oprnd2.v.val_int;
2287
2288   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
2289      offset.  Don't optimize if an signed integer overflow would happen.  */
2290   if (p != NULL
2291       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
2292           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
2293     *p += offset;
2294
2295   else if (offset > 0)
2296     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2297
2298   else
2299     {
2300       loc->dw_loc_next = int_loc_descriptor (-offset);
2301       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
2302     }
2303 }
2304
2305 /* Add a constant OFFSET to a location list.  */
2306
2307 static void
2308 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
2309 {
2310   dw_loc_list_ref d;
2311   for (d = list_head; d != NULL; d = d->dw_loc_next)
2312     loc_descr_plus_const (&d->expr, offset);
2313 }
2314
2315 #define DWARF_REF_SIZE  \
2316   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
2317
2318 static unsigned long size_of_locs (dw_loc_descr_ref);
2319 static unsigned long int get_base_type_offset (dw_die_ref);
2320
2321 /* Return the size of a location descriptor.  */
2322
2323 static unsigned long
2324 size_of_loc_descr (dw_loc_descr_ref loc)
2325 {
2326   unsigned long size = 1;
2327
2328   switch (loc->dw_loc_opc)
2329     {
2330     case DW_OP_addr:
2331       size += DWARF2_ADDR_SIZE;
2332       break;
2333     case DW_OP_const1u:
2334     case DW_OP_const1s:
2335       size += 1;
2336       break;
2337     case DW_OP_const2u:
2338     case DW_OP_const2s:
2339       size += 2;
2340       break;
2341     case DW_OP_const4u:
2342     case DW_OP_const4s:
2343       size += 4;
2344       break;
2345     case DW_OP_const8u:
2346     case DW_OP_const8s:
2347       size += 8;
2348       break;
2349     case DW_OP_constu:
2350       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2351       break;
2352     case DW_OP_consts:
2353       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2354       break;
2355     case DW_OP_pick:
2356       size += 1;
2357       break;
2358     case DW_OP_plus_uconst:
2359       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2360       break;
2361     case DW_OP_skip:
2362     case DW_OP_bra:
2363       size += 2;
2364       break;
2365     case DW_OP_breg0:
2366     case DW_OP_breg1:
2367     case DW_OP_breg2:
2368     case DW_OP_breg3:
2369     case DW_OP_breg4:
2370     case DW_OP_breg5:
2371     case DW_OP_breg6:
2372     case DW_OP_breg7:
2373     case DW_OP_breg8:
2374     case DW_OP_breg9:
2375     case DW_OP_breg10:
2376     case DW_OP_breg11:
2377     case DW_OP_breg12:
2378     case DW_OP_breg13:
2379     case DW_OP_breg14:
2380     case DW_OP_breg15:
2381     case DW_OP_breg16:
2382     case DW_OP_breg17:
2383     case DW_OP_breg18:
2384     case DW_OP_breg19:
2385     case DW_OP_breg20:
2386     case DW_OP_breg21:
2387     case DW_OP_breg22:
2388     case DW_OP_breg23:
2389     case DW_OP_breg24:
2390     case DW_OP_breg25:
2391     case DW_OP_breg26:
2392     case DW_OP_breg27:
2393     case DW_OP_breg28:
2394     case DW_OP_breg29:
2395     case DW_OP_breg30:
2396     case DW_OP_breg31:
2397       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2398       break;
2399     case DW_OP_regx:
2400       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2401       break;
2402     case DW_OP_fbreg:
2403       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2404       break;
2405     case DW_OP_bregx:
2406       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2407       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2408       break;
2409     case DW_OP_piece:
2410       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2411       break;
2412     case DW_OP_bit_piece:
2413       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2414       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
2415       break;
2416     case DW_OP_deref_size:
2417     case DW_OP_xderef_size:
2418       size += 1;
2419       break;
2420     case DW_OP_call2:
2421       size += 2;
2422       break;
2423     case DW_OP_call4:
2424       size += 4;
2425       break;
2426     case DW_OP_call_ref:
2427       size += DWARF_REF_SIZE;
2428       break;
2429     case DW_OP_implicit_value:
2430       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2431               + loc->dw_loc_oprnd1.v.val_unsigned;
2432       break;
2433     case DW_OP_GNU_implicit_pointer:
2434       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2435       break;
2436     case DW_OP_GNU_entry_value:
2437       {
2438         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
2439         size += size_of_uleb128 (op_size) + op_size;
2440         break;
2441       }
2442     case DW_OP_GNU_const_type:
2443       {
2444         unsigned long o
2445           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2446         size += size_of_uleb128 (o) + 1;
2447         switch (loc->dw_loc_oprnd2.val_class)
2448           {
2449           case dw_val_class_vec:
2450             size += loc->dw_loc_oprnd2.v.val_vec.length
2451                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
2452             break;
2453           case dw_val_class_const:
2454             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2455             break;
2456           case dw_val_class_const_double:
2457             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2458             break;
2459           default:
2460             gcc_unreachable ();
2461           }
2462         break;
2463       }
2464     case DW_OP_GNU_regval_type:
2465       {
2466         unsigned long o
2467           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2468         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2469                 + size_of_uleb128 (o);
2470       }
2471       break;
2472     case DW_OP_GNU_deref_type:
2473       {
2474         unsigned long o
2475           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2476         size += 1 + size_of_uleb128 (o);
2477       }
2478       break;
2479     case DW_OP_GNU_convert:
2480     case DW_OP_GNU_reinterpret:
2481       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2482         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2483       else
2484         {
2485           unsigned long o
2486             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2487           size += size_of_uleb128 (o);
2488         }
2489       break;
2490     case DW_OP_GNU_parameter_ref:
2491       size += 4;
2492       break;
2493     default:
2494       break;
2495     }
2496
2497   return size;
2498 }
2499
2500 /* Return the size of a series of location descriptors.  */
2501
2502 static unsigned long
2503 size_of_locs (dw_loc_descr_ref loc)
2504 {
2505   dw_loc_descr_ref l;
2506   unsigned long size;
2507
2508   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2509      field, to avoid writing to a PCH file.  */
2510   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2511     {
2512       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2513         break;
2514       size += size_of_loc_descr (l);
2515     }
2516   if (! l)
2517     return size;
2518
2519   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2520     {
2521       l->dw_loc_addr = size;
2522       size += size_of_loc_descr (l);
2523     }
2524
2525   return size;
2526 }
2527
2528 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2529 static void get_ref_die_offset_label (char *, dw_die_ref);
2530 static unsigned long int get_ref_die_offset (dw_die_ref);
2531 static void output_loc_sequence (dw_loc_descr_ref, int);
2532
2533 /* Output location description stack opcode's operands (if any).
2534    The for_eh_or_skip parameter controls whether register numbers are
2535    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2536    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2537    info).  This should be suppressed for the cases that have not been converted
2538    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2539
2540 static void
2541 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2542 {
2543   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2544   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2545
2546   switch (loc->dw_loc_opc)
2547     {
2548 #ifdef DWARF2_DEBUGGING_INFO
2549     case DW_OP_const2u:
2550     case DW_OP_const2s:
2551       dw2_asm_output_data (2, val1->v.val_int, NULL);
2552       break;
2553     case DW_OP_const4u:
2554       if (loc->dtprel)
2555         {
2556           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2557           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2558                                                val1->v.val_addr);
2559           fputc ('\n', asm_out_file);
2560           break;
2561         }
2562       /* FALLTHRU */
2563     case DW_OP_const4s:
2564       dw2_asm_output_data (4, val1->v.val_int, NULL);
2565       break;
2566     case DW_OP_const8u:
2567       if (loc->dtprel)
2568         {
2569           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2570           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2571                                                val1->v.val_addr);
2572           fputc ('\n', asm_out_file);
2573           break;
2574         }
2575       /* FALLTHRU */
2576     case DW_OP_const8s:
2577       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2578       dw2_asm_output_data (8, val1->v.val_int, NULL);
2579       break;
2580     case DW_OP_skip:
2581     case DW_OP_bra:
2582       {
2583         int offset;
2584
2585         gcc_assert (val1->val_class == dw_val_class_loc);
2586         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2587
2588         dw2_asm_output_data (2, offset, NULL);
2589       }
2590       break;
2591     case DW_OP_implicit_value:
2592       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2593       switch (val2->val_class)
2594         {
2595         case dw_val_class_const:
2596           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2597           break;
2598         case dw_val_class_vec:
2599           {
2600             unsigned int elt_size = val2->v.val_vec.elt_size;
2601             unsigned int len = val2->v.val_vec.length;
2602             unsigned int i;
2603             unsigned char *p;
2604
2605             if (elt_size > sizeof (HOST_WIDE_INT))
2606               {
2607                 elt_size /= 2;
2608                 len *= 2;
2609               }
2610             for (i = 0, p = val2->v.val_vec.array;
2611                  i < len;
2612                  i++, p += elt_size)
2613               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2614                                    "fp or vector constant word %u", i);
2615           }
2616           break;
2617         case dw_val_class_const_double:
2618           {
2619             unsigned HOST_WIDE_INT first, second;
2620
2621             if (WORDS_BIG_ENDIAN)
2622               {
2623                 first = val2->v.val_double.high;
2624                 second = val2->v.val_double.low;
2625               }
2626             else
2627               {
2628                 first = val2->v.val_double.low;
2629                 second = val2->v.val_double.high;
2630               }
2631             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2632                                  first, NULL);
2633             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2634                                  second, NULL);
2635           }
2636           break;
2637         case dw_val_class_addr:
2638           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2639           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2640           break;
2641         default:
2642           gcc_unreachable ();
2643         }
2644       break;
2645 #else
2646     case DW_OP_const2u:
2647     case DW_OP_const2s:
2648     case DW_OP_const4u:
2649     case DW_OP_const4s:
2650     case DW_OP_const8u:
2651     case DW_OP_const8s:
2652     case DW_OP_skip:
2653     case DW_OP_bra:
2654     case DW_OP_implicit_value:
2655       /* We currently don't make any attempt to make sure these are
2656          aligned properly like we do for the main unwind info, so
2657          don't support emitting things larger than a byte if we're
2658          only doing unwinding.  */
2659       gcc_unreachable ();
2660 #endif
2661     case DW_OP_const1u:
2662     case DW_OP_const1s:
2663       dw2_asm_output_data (1, val1->v.val_int, NULL);
2664       break;
2665     case DW_OP_constu:
2666       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2667       break;
2668     case DW_OP_consts:
2669       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2670       break;
2671     case DW_OP_pick:
2672       dw2_asm_output_data (1, val1->v.val_int, NULL);
2673       break;
2674     case DW_OP_plus_uconst:
2675       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2676       break;
2677     case DW_OP_breg0:
2678     case DW_OP_breg1:
2679     case DW_OP_breg2:
2680     case DW_OP_breg3:
2681     case DW_OP_breg4:
2682     case DW_OP_breg5:
2683     case DW_OP_breg6:
2684     case DW_OP_breg7:
2685     case DW_OP_breg8:
2686     case DW_OP_breg9:
2687     case DW_OP_breg10:
2688     case DW_OP_breg11:
2689     case DW_OP_breg12:
2690     case DW_OP_breg13:
2691     case DW_OP_breg14:
2692     case DW_OP_breg15:
2693     case DW_OP_breg16:
2694     case DW_OP_breg17:
2695     case DW_OP_breg18:
2696     case DW_OP_breg19:
2697     case DW_OP_breg20:
2698     case DW_OP_breg21:
2699     case DW_OP_breg22:
2700     case DW_OP_breg23:
2701     case DW_OP_breg24:
2702     case DW_OP_breg25:
2703     case DW_OP_breg26:
2704     case DW_OP_breg27:
2705     case DW_OP_breg28:
2706     case DW_OP_breg29:
2707     case DW_OP_breg30:
2708     case DW_OP_breg31:
2709       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2710       break;
2711     case DW_OP_regx:
2712       {
2713         unsigned r = val1->v.val_unsigned;
2714         if (for_eh_or_skip >= 0)
2715           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2716         gcc_assert (size_of_uleb128 (r) 
2717                     == size_of_uleb128 (val1->v.val_unsigned));
2718         dw2_asm_output_data_uleb128 (r, NULL);  
2719       }
2720       break;
2721     case DW_OP_fbreg:
2722       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2723       break;
2724     case DW_OP_bregx:
2725       {
2726         unsigned r = val1->v.val_unsigned;
2727         if (for_eh_or_skip >= 0)
2728           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2729         gcc_assert (size_of_uleb128 (r) 
2730                     == size_of_uleb128 (val1->v.val_unsigned));
2731         dw2_asm_output_data_uleb128 (r, NULL);  
2732         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2733       }
2734       break;
2735     case DW_OP_piece:
2736       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2737       break;
2738     case DW_OP_bit_piece:
2739       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2740       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2741       break;
2742     case DW_OP_deref_size:
2743     case DW_OP_xderef_size:
2744       dw2_asm_output_data (1, val1->v.val_int, NULL);
2745       break;
2746
2747     case DW_OP_addr:
2748       if (loc->dtprel)
2749         {
2750           if (targetm.asm_out.output_dwarf_dtprel)
2751             {
2752               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2753                                                    DWARF2_ADDR_SIZE,
2754                                                    val1->v.val_addr);
2755               fputc ('\n', asm_out_file);
2756             }
2757           else
2758             gcc_unreachable ();
2759         }
2760       else
2761         {
2762 #ifdef DWARF2_DEBUGGING_INFO
2763           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2764 #else
2765           gcc_unreachable ();
2766 #endif
2767         }
2768       break;
2769
2770     case DW_OP_GNU_implicit_pointer:
2771       {
2772         char label[MAX_ARTIFICIAL_LABEL_BYTES
2773                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2774         gcc_assert (val1->val_class == dw_val_class_die_ref);
2775         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2776         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2777         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2778       }
2779       break;
2780
2781     case DW_OP_GNU_entry_value:
2782       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2783       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2784       break;
2785
2786     case DW_OP_GNU_const_type:
2787       {
2788         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2789         gcc_assert (o);
2790         dw2_asm_output_data_uleb128 (o, NULL);
2791         switch (val2->val_class)
2792           {
2793           case dw_val_class_const:
2794             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2795             dw2_asm_output_data (1, l, NULL);
2796             dw2_asm_output_data (l, val2->v.val_int, NULL);
2797             break;
2798           case dw_val_class_vec:
2799             {
2800               unsigned int elt_size = val2->v.val_vec.elt_size;
2801               unsigned int len = val2->v.val_vec.length;
2802               unsigned int i;
2803               unsigned char *p;
2804
2805               l = len * elt_size;
2806               dw2_asm_output_data (1, l, NULL);
2807               if (elt_size > sizeof (HOST_WIDE_INT))
2808                 {
2809                   elt_size /= 2;
2810                   len *= 2;
2811                 }
2812               for (i = 0, p = val2->v.val_vec.array;
2813                    i < len;
2814                    i++, p += elt_size)
2815                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2816                                      "fp or vector constant word %u", i);
2817             }
2818             break;
2819           case dw_val_class_const_double:
2820             {
2821               unsigned HOST_WIDE_INT first, second;
2822               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2823
2824               dw2_asm_output_data (1, 2 * l, NULL);
2825               if (WORDS_BIG_ENDIAN)
2826                 {
2827                   first = val2->v.val_double.high;
2828                   second = val2->v.val_double.low;
2829                 }
2830               else
2831                 {
2832                   first = val2->v.val_double.low;
2833                   second = val2->v.val_double.high;
2834                 }
2835               dw2_asm_output_data (l, first, NULL);
2836               dw2_asm_output_data (l, second, NULL);
2837             }
2838             break;
2839           default:
2840             gcc_unreachable ();
2841           }
2842       }
2843       break;
2844     case DW_OP_GNU_regval_type:
2845       {
2846         unsigned r = val1->v.val_unsigned;
2847         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2848         gcc_assert (o);
2849         if (for_eh_or_skip >= 0)
2850           {
2851             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2852             gcc_assert (size_of_uleb128 (r)
2853                         == size_of_uleb128 (val1->v.val_unsigned));
2854           }
2855         dw2_asm_output_data_uleb128 (r, NULL);
2856         dw2_asm_output_data_uleb128 (o, NULL);
2857       }
2858       break;
2859     case DW_OP_GNU_deref_type:
2860       {
2861         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2862         gcc_assert (o);
2863         dw2_asm_output_data (1, val1->v.val_int, NULL);
2864         dw2_asm_output_data_uleb128 (o, NULL);
2865       }
2866       break;
2867     case DW_OP_GNU_convert:
2868     case DW_OP_GNU_reinterpret:
2869       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2870         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2871       else
2872         {
2873           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2874           gcc_assert (o);
2875           dw2_asm_output_data_uleb128 (o, NULL);
2876         }
2877       break;
2878
2879     case DW_OP_GNU_parameter_ref:
2880       {
2881         unsigned long o;
2882         gcc_assert (val1->val_class == dw_val_class_die_ref);
2883         o = get_ref_die_offset (val1->v.val_die_ref.die);
2884         dw2_asm_output_data (4, o, NULL);
2885       }
2886       break;
2887
2888     default:
2889       /* Other codes have no operands.  */
2890       break;
2891     }
2892 }
2893
2894 /* Output a sequence of location operations.  
2895    The for_eh_or_skip parameter controls whether register numbers are
2896    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2897    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2898    info).  This should be suppressed for the cases that have not been converted
2899    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2900
2901 static void
2902 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2903 {
2904   for (; loc != NULL; loc = loc->dw_loc_next)
2905     {
2906       enum dwarf_location_atom opc = loc->dw_loc_opc;
2907       /* Output the opcode.  */
2908       if (for_eh_or_skip >= 0 
2909           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2910         {
2911           unsigned r = (opc - DW_OP_breg0);
2912           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2913           gcc_assert (r <= 31);
2914           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2915         }
2916       else if (for_eh_or_skip >= 0 
2917                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2918         {
2919           unsigned r = (opc - DW_OP_reg0);
2920           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2921           gcc_assert (r <= 31);
2922           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2923         }
2924
2925       dw2_asm_output_data (1, opc,
2926                              "%s", dwarf_stack_op_name (opc));
2927
2928       /* Output the operand(s) (if any).  */
2929       output_loc_operands (loc, for_eh_or_skip);
2930     }
2931 }
2932
2933 /* Output location description stack opcode's operands (if any).
2934    The output is single bytes on a line, suitable for .cfi_escape.  */
2935
2936 static void
2937 output_loc_operands_raw (dw_loc_descr_ref loc)
2938 {
2939   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2940   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2941
2942   switch (loc->dw_loc_opc)
2943     {
2944     case DW_OP_addr:
2945     case DW_OP_implicit_value:
2946       /* We cannot output addresses in .cfi_escape, only bytes.  */
2947       gcc_unreachable ();
2948
2949     case DW_OP_const1u:
2950     case DW_OP_const1s:
2951     case DW_OP_pick:
2952     case DW_OP_deref_size:
2953     case DW_OP_xderef_size:
2954       fputc (',', asm_out_file);
2955       dw2_asm_output_data_raw (1, val1->v.val_int);
2956       break;
2957
2958     case DW_OP_const2u:
2959     case DW_OP_const2s:
2960       fputc (',', asm_out_file);
2961       dw2_asm_output_data_raw (2, val1->v.val_int);
2962       break;
2963
2964     case DW_OP_const4u:
2965     case DW_OP_const4s:
2966       fputc (',', asm_out_file);
2967       dw2_asm_output_data_raw (4, val1->v.val_int);
2968       break;
2969
2970     case DW_OP_const8u:
2971     case DW_OP_const8s:
2972       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2973       fputc (',', asm_out_file);
2974       dw2_asm_output_data_raw (8, val1->v.val_int);
2975       break;
2976
2977     case DW_OP_skip:
2978     case DW_OP_bra:
2979       {
2980         int offset;
2981
2982         gcc_assert (val1->val_class == dw_val_class_loc);
2983         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2984
2985         fputc (',', asm_out_file);
2986         dw2_asm_output_data_raw (2, offset);
2987       }
2988       break;
2989
2990     case DW_OP_regx:
2991       {
2992         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2993         gcc_assert (size_of_uleb128 (r) 
2994                     == size_of_uleb128 (val1->v.val_unsigned));
2995         fputc (',', asm_out_file);
2996         dw2_asm_output_data_uleb128_raw (r);
2997       }
2998       break;
2999       
3000     case DW_OP_constu:
3001     case DW_OP_plus_uconst:
3002     case DW_OP_piece:
3003       fputc (',', asm_out_file);
3004       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
3005       break;
3006
3007     case DW_OP_bit_piece:
3008       fputc (',', asm_out_file);
3009       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
3010       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
3011       break;
3012
3013     case DW_OP_consts:
3014     case DW_OP_breg0:
3015     case DW_OP_breg1:
3016     case DW_OP_breg2:
3017     case DW_OP_breg3:
3018     case DW_OP_breg4:
3019     case DW_OP_breg5:
3020     case DW_OP_breg6:
3021     case DW_OP_breg7:
3022     case DW_OP_breg8:
3023     case DW_OP_breg9:
3024     case DW_OP_breg10:
3025     case DW_OP_breg11:
3026     case DW_OP_breg12:
3027     case DW_OP_breg13:
3028     case DW_OP_breg14:
3029     case DW_OP_breg15:
3030     case DW_OP_breg16:
3031     case DW_OP_breg17:
3032     case DW_OP_breg18:
3033     case DW_OP_breg19:
3034     case DW_OP_breg20:
3035     case DW_OP_breg21:
3036     case DW_OP_breg22:
3037     case DW_OP_breg23:
3038     case DW_OP_breg24:
3039     case DW_OP_breg25:
3040     case DW_OP_breg26:
3041     case DW_OP_breg27:
3042     case DW_OP_breg28:
3043     case DW_OP_breg29:
3044     case DW_OP_breg30:
3045     case DW_OP_breg31:
3046     case DW_OP_fbreg:
3047       fputc (',', asm_out_file);
3048       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
3049       break;
3050
3051     case DW_OP_bregx:
3052       {
3053         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
3054         gcc_assert (size_of_uleb128 (r) 
3055                     == size_of_uleb128 (val1->v.val_unsigned));
3056         fputc (',', asm_out_file);
3057         dw2_asm_output_data_uleb128_raw (r);
3058         fputc (',', asm_out_file);
3059         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
3060       }
3061       break;
3062
3063     case DW_OP_GNU_implicit_pointer:
3064     case DW_OP_GNU_entry_value:
3065     case DW_OP_GNU_const_type:
3066     case DW_OP_GNU_regval_type:
3067     case DW_OP_GNU_deref_type:
3068     case DW_OP_GNU_convert:
3069     case DW_OP_GNU_reinterpret:
3070     case DW_OP_GNU_parameter_ref:
3071       gcc_unreachable ();
3072       break;
3073
3074     default:
3075       /* Other codes have no operands.  */
3076       break;
3077     }
3078 }
3079
3080 static void
3081 output_loc_sequence_raw (dw_loc_descr_ref loc)
3082 {
3083   while (1)
3084     {
3085       enum dwarf_location_atom opc = loc->dw_loc_opc;
3086       /* Output the opcode.  */
3087       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
3088         {
3089           unsigned r = (opc - DW_OP_breg0);
3090           r = DWARF2_FRAME_REG_OUT (r, 1);
3091           gcc_assert (r <= 31);
3092           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
3093         }
3094       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
3095         {
3096           unsigned r = (opc - DW_OP_reg0);
3097           r = DWARF2_FRAME_REG_OUT (r, 1);
3098           gcc_assert (r <= 31);
3099           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
3100         }
3101       /* Output the opcode.  */
3102       fprintf (asm_out_file, "%#x", opc);
3103       output_loc_operands_raw (loc);
3104
3105       if (!loc->dw_loc_next)
3106         break;
3107       loc = loc->dw_loc_next;
3108
3109       fputc (',', asm_out_file);
3110     }
3111 }
3112
3113 /* This routine will generate the correct assembly data for a location
3114    description based on a cfi entry with a complex address.  */
3115
3116 static void
3117 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
3118 {
3119   dw_loc_descr_ref loc;
3120   unsigned long size;
3121
3122   if (cfi->dw_cfi_opc == DW_CFA_expression)
3123     {
3124       unsigned r = 
3125         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3126       dw2_asm_output_data (1, r, NULL);
3127       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
3128     }
3129   else
3130     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3131
3132   /* Output the size of the block.  */
3133   size = size_of_locs (loc);
3134   dw2_asm_output_data_uleb128 (size, NULL);
3135
3136   /* Now output the operations themselves.  */
3137   output_loc_sequence (loc, for_eh);
3138 }
3139
3140 /* Similar, but used for .cfi_escape.  */
3141
3142 static void
3143 output_cfa_loc_raw (dw_cfi_ref cfi)
3144 {
3145   dw_loc_descr_ref loc;
3146   unsigned long size;
3147
3148   if (cfi->dw_cfi_opc == DW_CFA_expression)
3149     {
3150       unsigned r = 
3151         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3152       fprintf (asm_out_file, "%#x,", r);
3153       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
3154     }
3155   else
3156     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3157
3158   /* Output the size of the block.  */
3159   size = size_of_locs (loc);
3160   dw2_asm_output_data_uleb128_raw (size);
3161   fputc (',', asm_out_file);
3162
3163   /* Now output the operations themselves.  */
3164   output_loc_sequence_raw (loc);
3165 }
3166
3167 /* This function builds a dwarf location descriptor sequence from a
3168    dw_cfa_location, adding the given OFFSET to the result of the
3169    expression.  */
3170
3171 struct dw_loc_descr_struct *
3172 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3173 {
3174   struct dw_loc_descr_struct *head, *tmp;
3175
3176   offset += cfa->offset;
3177
3178   if (cfa->indirect)
3179     {
3180       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
3181       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3182       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3183       add_loc_descr (&head, tmp);
3184       if (offset != 0)
3185         {
3186           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3187           add_loc_descr (&head, tmp);
3188         }
3189     }
3190   else
3191     head = new_reg_loc_descr (cfa->reg, offset);
3192
3193   return head;
3194 }
3195
3196 /* This function builds a dwarf location descriptor sequence for
3197    the address at OFFSET from the CFA when stack is aligned to
3198    ALIGNMENT byte.  */
3199
3200 struct dw_loc_descr_struct *
3201 build_cfa_aligned_loc (dw_cfa_location *cfa,
3202                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
3203 {
3204   struct dw_loc_descr_struct *head;
3205   unsigned int dwarf_fp
3206     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
3207
3208   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
3209   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
3210     {
3211       head = new_reg_loc_descr (dwarf_fp, 0);
3212       add_loc_descr (&head, int_loc_descriptor (alignment));
3213       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
3214       loc_descr_plus_const (&head, offset);
3215     }
3216   else
3217     head = new_reg_loc_descr (dwarf_fp, offset);
3218   return head;
3219 }
3220 \f
3221 /* And now, the support for symbolic debugging information.  */
3222
3223 /* .debug_str support.  */
3224 static int output_indirect_string (void **, void *);
3225
3226 static void dwarf2out_init (const char *);
3227 static void dwarf2out_finish (const char *);
3228 static void dwarf2out_assembly_start (void);
3229 static void dwarf2out_define (unsigned int, const char *);
3230 static void dwarf2out_undef (unsigned int, const char *);
3231 static void dwarf2out_start_source_file (unsigned, const char *);
3232 static void dwarf2out_end_source_file (unsigned);
3233 static void dwarf2out_function_decl (tree);
3234 static void dwarf2out_begin_block (unsigned, unsigned);
3235 static void dwarf2out_end_block (unsigned, unsigned);
3236 static bool dwarf2out_ignore_block (const_tree);
3237 static void dwarf2out_global_decl (tree);
3238 static void dwarf2out_type_decl (tree, int);
3239 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
3240 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
3241                                                  dw_die_ref);
3242 static void dwarf2out_abstract_function (tree);
3243 static void dwarf2out_var_location (rtx);
3244 static void dwarf2out_begin_function (tree);
3245 static void dwarf2out_set_name (tree, tree);
3246
3247 /* The debug hooks structure.  */
3248
3249 const struct gcc_debug_hooks dwarf2_debug_hooks =
3250 {
3251   dwarf2out_init,
3252   dwarf2out_finish,
3253   dwarf2out_assembly_start,
3254   dwarf2out_define,
3255   dwarf2out_undef,
3256   dwarf2out_start_source_file,
3257   dwarf2out_end_source_file,
3258   dwarf2out_begin_block,
3259   dwarf2out_end_block,
3260   dwarf2out_ignore_block,
3261   dwarf2out_source_line,
3262   dwarf2out_begin_prologue,
3263 #if VMS_DEBUGGING_INFO
3264   dwarf2out_vms_end_prologue,
3265   dwarf2out_vms_begin_epilogue,
3266 #else
3267   debug_nothing_int_charstar,
3268   debug_nothing_int_charstar,
3269 #endif
3270   dwarf2out_end_epilogue,
3271   dwarf2out_begin_function,
3272   debug_nothing_int,            /* end_function */
3273   dwarf2out_function_decl,      /* function_decl */
3274   dwarf2out_global_decl,
3275   dwarf2out_type_decl,          /* type_decl */
3276   dwarf2out_imported_module_or_decl,
3277   debug_nothing_tree,           /* deferred_inline_function */
3278   /* The DWARF 2 backend tries to reduce debugging bloat by not
3279      emitting the abstract description of inline functions until
3280      something tries to reference them.  */
3281   dwarf2out_abstract_function,  /* outlining_inline_function */
3282   debug_nothing_rtx,            /* label */
3283   debug_nothing_int,            /* handle_pch */
3284   dwarf2out_var_location,
3285   dwarf2out_switch_text_section,
3286   dwarf2out_set_name,
3287   1,                            /* start_end_main_source_file */
3288   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
3289 };
3290 \f
3291 /* NOTE: In the comments in this file, many references are made to
3292    "Debugging Information Entries".  This term is abbreviated as `DIE'
3293    throughout the remainder of this file.  */
3294
3295 /* An internal representation of the DWARF output is built, and then
3296    walked to generate the DWARF debugging info.  The walk of the internal
3297    representation is done after the entire program has been compiled.
3298    The types below are used to describe the internal representation.  */
3299
3300 /* Whether to put type DIEs into their own section .debug_types instead
3301    of making them part of the .debug_info section.  Only supported for
3302    Dwarf V4 or higher and the user didn't disable them through
3303    -fno-debug-types-section.  It is more efficient to put them in a
3304    separate comdat sections since the linker will then be able to
3305    remove duplicates.  But not all tools support .debug_types sections
3306    yet.  */
3307
3308 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
3309
3310 /* Various DIE's use offsets relative to the beginning of the
3311    .debug_info section to refer to each other.  */
3312
3313 typedef long int dw_offset;
3314
3315 /* Define typedefs here to avoid circular dependencies.  */
3316
3317 typedef struct dw_attr_struct *dw_attr_ref;
3318 typedef struct dw_line_info_struct *dw_line_info_ref;
3319 typedef struct pubname_struct *pubname_ref;
3320 typedef struct dw_ranges_struct *dw_ranges_ref;
3321 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3322 typedef struct comdat_type_struct *comdat_type_node_ref;
3323
3324 /* The entries in the line_info table more-or-less mirror the opcodes
3325    that are used in the real dwarf line table.  Arrays of these entries
3326    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3327    supported.  */
3328
3329 enum dw_line_info_opcode {
3330   /* Emit DW_LNE_set_address; the operand is the label index.  */
3331   LI_set_address,
3332
3333   /* Emit a row to the matrix with the given line.  This may be done
3334      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3335      special opcodes.  */
3336   LI_set_line,
3337
3338   /* Emit a DW_LNS_set_file.  */
3339   LI_set_file,
3340
3341   /* Emit a DW_LNS_set_column.  */
3342   LI_set_column,
3343
3344   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
3345   LI_negate_stmt,
3346
3347   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
3348   LI_set_prologue_end,
3349   LI_set_epilogue_begin,
3350
3351   /* Emit a DW_LNE_set_discriminator.  */
3352   LI_set_discriminator
3353 };
3354
3355 typedef struct GTY(()) dw_line_info_struct {
3356   enum dw_line_info_opcode opcode;
3357   unsigned int val;
3358 } dw_line_info_entry;
3359
3360 DEF_VEC_O(dw_line_info_entry);
3361 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
3362
3363 typedef struct GTY(()) dw_line_info_table_struct {
3364   /* The label that marks the end of this section.  */
3365   const char *end_label;
3366
3367   /* The values for the last row of the matrix, as collected in the table.
3368      These are used to minimize the changes to the next row.  */
3369   unsigned int file_num;
3370   unsigned int line_num;
3371   unsigned int column_num;
3372   int discrim_num;
3373   bool is_stmt;
3374   bool in_use;
3375
3376   VEC(dw_line_info_entry, gc) *entries;
3377 } dw_line_info_table;
3378
3379 typedef dw_line_info_table *dw_line_info_table_p;
3380
3381 DEF_VEC_P(dw_line_info_table_p);
3382 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
3383
3384 /* Each DIE attribute has a field specifying the attribute kind,
3385    a link to the next attribute in the chain, and an attribute value.
3386    Attributes are typically linked below the DIE they modify.  */
3387
3388 typedef struct GTY(()) dw_attr_struct {
3389   enum dwarf_attribute dw_attr;
3390   dw_val_node dw_attr_val;
3391 }
3392 dw_attr_node;
3393
3394 DEF_VEC_O(dw_attr_node);
3395 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3396
3397 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3398    The children of each node form a circular list linked by
3399    die_sib.  die_child points to the node *before* the "first" child node.  */
3400
3401 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
3402   union die_symbol_or_type_node
3403     {
3404       char * GTY ((tag ("0"))) die_symbol;
3405       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
3406     }
3407   GTY ((desc ("use_debug_types"))) die_id;
3408   VEC(dw_attr_node,gc) * die_attr;
3409   dw_die_ref die_parent;
3410   dw_die_ref die_child;
3411   dw_die_ref die_sib;
3412   dw_die_ref die_definition; /* ref from a specification to its definition */
3413   dw_offset die_offset;
3414   unsigned long die_abbrev;
3415   int die_mark;
3416   /* Die is used and must not be pruned as unused.  */
3417   int die_perennial_p;
3418   unsigned int decl_id;
3419   enum dwarf_tag die_tag;
3420 }
3421 die_node;
3422
3423 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3424 #define FOR_EACH_CHILD(die, c, expr) do {       \
3425   c = die->die_child;                           \
3426   if (c) do {                                   \
3427     c = c->die_sib;                             \
3428     expr;                                       \
3429   } while (c != die->die_child);                \
3430 } while (0)
3431
3432 /* The pubname structure */
3433
3434 typedef struct GTY(()) pubname_struct {
3435   dw_die_ref die;
3436   const char *name;
3437 }
3438 pubname_entry;
3439
3440 DEF_VEC_O(pubname_entry);
3441 DEF_VEC_ALLOC_O(pubname_entry, gc);
3442
3443 struct GTY(()) dw_ranges_struct {
3444   /* If this is positive, it's a block number, otherwise it's a
3445      bitwise-negated index into dw_ranges_by_label.  */
3446   int num;
3447 };
3448
3449 /* A structure to hold a macinfo entry.  */
3450
3451 typedef struct GTY(()) macinfo_struct {
3452   unsigned HOST_WIDE_INT code;
3453   unsigned HOST_WIDE_INT lineno;
3454   const char *info;
3455 }
3456 macinfo_entry;
3457
3458 DEF_VEC_O(macinfo_entry);
3459 DEF_VEC_ALLOC_O(macinfo_entry, gc);
3460
3461 struct GTY(()) dw_ranges_by_label_struct {
3462   const char *begin;
3463   const char *end;
3464 };
3465
3466 /* The comdat type node structure.  */
3467 typedef struct GTY(()) comdat_type_struct
3468 {
3469   dw_die_ref root_die;
3470   dw_die_ref type_die;
3471   char signature[DWARF_TYPE_SIGNATURE_SIZE];
3472   struct comdat_type_struct *next;
3473 }
3474 comdat_type_node;
3475
3476 /* The limbo die list structure.  */
3477 typedef struct GTY(()) limbo_die_struct {
3478   dw_die_ref die;
3479   tree created_for;
3480   struct limbo_die_struct *next;
3481 }
3482 limbo_die_node;
3483
3484 typedef struct skeleton_chain_struct
3485 {
3486   dw_die_ref old_die;
3487   dw_die_ref new_die;
3488   struct skeleton_chain_struct *parent;
3489 }
3490 skeleton_chain_node;
3491
3492 /* How to start an assembler comment.  */
3493 #ifndef ASM_COMMENT_START
3494 #define ASM_COMMENT_START ";#"
3495 #endif
3496
3497 /* Define a macro which returns nonzero for a TYPE_DECL which was
3498    implicitly generated for a type.
3499
3500    Note that, unlike the C front-end (which generates a NULL named
3501    TYPE_DECL node for each complete tagged type, each array type,
3502    and each function type node created) the C++ front-end generates
3503    a _named_ TYPE_DECL node for each tagged type node created.
3504    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3505    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
3506    front-end, but for each type, tagged or not.  */
3507
3508 #define TYPE_DECL_IS_STUB(decl)                         \
3509   (DECL_NAME (decl) == NULL_TREE                        \
3510    || (DECL_ARTIFICIAL (decl)                           \
3511        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3512            /* This is necessary for stub decls that     \
3513               appear in nested inline functions.  */    \
3514            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3515                && (decl_ultimate_origin (decl)          \
3516                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3517
3518 /* Information concerning the compilation unit's programming
3519    language, and compiler version.  */
3520
3521 /* Fixed size portion of the DWARF compilation unit header.  */
3522 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3523   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3524
3525 /* Fixed size portion of the DWARF comdat type unit header.  */
3526 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3527   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
3528    + DWARF_OFFSET_SIZE)
3529
3530 /* Fixed size portion of public names info.  */
3531 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3532
3533 /* Fixed size portion of the address range info.  */
3534 #define DWARF_ARANGES_HEADER_SIZE                                       \
3535   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3536                 DWARF2_ADDR_SIZE * 2)                                   \
3537    - DWARF_INITIAL_LENGTH_SIZE)
3538
3539 /* Size of padding portion in the address range info.  It must be
3540    aligned to twice the pointer size.  */
3541 #define DWARF_ARANGES_PAD_SIZE \
3542   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3543                 DWARF2_ADDR_SIZE * 2)                              \
3544    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3545
3546 /* Use assembler line directives if available.  */
3547 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3548 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3549 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3550 #else
3551 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3552 #endif
3553 #endif
3554
3555 /* Minimum line offset in a special line info. opcode.
3556    This value was chosen to give a reasonable range of values.  */
3557 #define DWARF_LINE_BASE  -10
3558
3559 /* First special line opcode - leave room for the standard opcodes.  */
3560 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
3561
3562 /* Range of line offsets in a special line info. opcode.  */
3563 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3564
3565 /* Flag that indicates the initial value of the is_stmt_start flag.
3566    In the present implementation, we do not mark any lines as
3567    the beginning of a source statement, because that information
3568    is not made available by the GCC front-end.  */
3569 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3570
3571 /* Maximum number of operations per instruction bundle.  */
3572 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3573 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3574 #endif
3575
3576 /* This location is used by calc_die_sizes() to keep track
3577    the offset of each DIE within the .debug_info section.  */
3578 static unsigned long next_die_offset;
3579
3580 /* Record the root of the DIE's built for the current compilation unit.  */
3581 static GTY(()) dw_die_ref single_comp_unit_die;
3582
3583 /* A list of type DIEs that have been separated into comdat sections.  */
3584 static GTY(()) comdat_type_node *comdat_type_list;
3585
3586 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3587 static GTY(()) limbo_die_node *limbo_die_list;
3588
3589 /* A list of DIEs for which we may have to generate
3590    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
3591 static GTY(()) limbo_die_node *deferred_asm_name;
3592
3593 /* Filenames referenced by this compilation unit.  */
3594 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3595
3596 /* A hash table of references to DIE's that describe declarations.
3597    The key is a DECL_UID() which is a unique number identifying each decl.  */
3598 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3599
3600 /* A hash table of references to DIE's that describe COMMON blocks.
3601    The key is DECL_UID() ^ die_parent.  */
3602 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
3603
3604 typedef struct GTY(()) die_arg_entry_struct {
3605     dw_die_ref die;
3606     tree arg;
3607 } die_arg_entry;
3608
3609 DEF_VEC_O(die_arg_entry);
3610 DEF_VEC_ALLOC_O(die_arg_entry,gc);
3611
3612 /* Node of the variable location list.  */
3613 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3614   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3615      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
3616      in mode of the EXPR_LIST node and first EXPR_LIST operand
3617      is either NOTE_INSN_VAR_LOCATION for a piece with a known
3618      location or NULL for padding.  For larger bitsizes,
3619      mode is 0 and first operand is a CONCAT with bitsize
3620      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3621      NULL as second operand.  */
3622   rtx GTY (()) loc;
3623   const char * GTY (()) label;
3624   struct var_loc_node * GTY (()) next;
3625 };
3626
3627 /* Variable location list.  */
3628 struct GTY (()) var_loc_list_def {
3629   struct var_loc_node * GTY (()) first;
3630
3631   /* Pointer to the last but one or last element of the
3632      chained list.  If the list is empty, both first and
3633      last are NULL, if the list contains just one node
3634      or the last node certainly is not redundant, it points
3635      to the last node, otherwise points to the last but one.
3636      Do not mark it for GC because it is marked through the chain.  */
3637   struct var_loc_node * GTY ((skip ("%h"))) last;
3638
3639   /* Pointer to the last element before section switch,
3640      if NULL, either sections weren't switched or first
3641      is after section switch.  */
3642   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3643
3644   /* DECL_UID of the variable decl.  */
3645   unsigned int decl_id;
3646 };
3647 typedef struct var_loc_list_def var_loc_list;
3648
3649 /* Call argument location list.  */
3650 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3651   rtx GTY (()) call_arg_loc_note;
3652   const char * GTY (()) label;
3653   tree GTY (()) block;
3654   bool tail_call_p;
3655   rtx GTY (()) symbol_ref;
3656   struct call_arg_loc_node * GTY (()) next;
3657 };
3658
3659
3660 /* Table of decl location linked lists.  */
3661 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3662
3663 /* Head and tail of call_arg_loc chain.  */
3664 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3665 static struct call_arg_loc_node *call_arg_loc_last;
3666
3667 /* Number of call sites in the current function.  */
3668 static int call_site_count = -1;
3669 /* Number of tail call sites in the current function.  */
3670 static int tail_call_site_count = -1;
3671
3672 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
3673    DIEs.  */
3674 static VEC (dw_die_ref, heap) *block_map;
3675
3676 /* A cached location list.  */
3677 struct GTY (()) cached_dw_loc_list_def {
3678   /* The DECL_UID of the decl that this entry describes.  */
3679   unsigned int decl_id;
3680
3681   /* The cached location list.  */
3682   dw_loc_list_ref loc_list;
3683 };
3684 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3685
3686 /* Table of cached location lists.  */
3687 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3688
3689 /* A pointer to the base of a list of references to DIE's that
3690    are uniquely identified by their tag, presence/absence of
3691    children DIE's, and list of attribute/value pairs.  */
3692 static GTY((length ("abbrev_die_table_allocated")))
3693   dw_die_ref *abbrev_die_table;
3694
3695 /* Number of elements currently allocated for abbrev_die_table.  */
3696 static GTY(()) unsigned abbrev_die_table_allocated;
3697
3698 /* Number of elements in type_die_table currently in use.  */
3699 static GTY(()) unsigned abbrev_die_table_in_use;
3700
3701 /* Size (in elements) of increments by which we may expand the
3702    abbrev_die_table.  */
3703 #define ABBREV_DIE_TABLE_INCREMENT 256
3704
3705 /* A global counter for generating labels for line number data.  */
3706 static unsigned int line_info_label_num;
3707
3708 /* The current table to which we should emit line number information
3709    for the current function.  This will be set up at the beginning of
3710    assembly for the function.  */
3711 static dw_line_info_table *cur_line_info_table;
3712
3713 /* The two default tables of line number info.  */
3714 static GTY(()) dw_line_info_table *text_section_line_info;
3715 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3716
3717 /* The set of all non-default tables of line number info.  */
3718 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3719
3720 /* A flag to tell pubnames/types export if there is an info section to
3721    refer to.  */
3722 static bool info_section_emitted;
3723
3724 /* A pointer to the base of a table that contains a list of publicly
3725    accessible names.  */
3726 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3727
3728 /* A pointer to the base of a table that contains a list of publicly
3729    accessible types.  */
3730 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3731
3732 /* A pointer to the base of a table that contains a list of macro
3733    defines/undefines (and file start/end markers).  */
3734 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3735
3736 /* Array of dies for which we should generate .debug_ranges info.  */
3737 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3738
3739 /* Number of elements currently allocated for ranges_table.  */
3740 static GTY(()) unsigned ranges_table_allocated;
3741
3742 /* Number of elements in ranges_table currently in use.  */
3743 static GTY(()) unsigned ranges_table_in_use;
3744
3745 /* Array of pairs of labels referenced in ranges_table.  */
3746 static GTY ((length ("ranges_by_label_allocated")))
3747      dw_ranges_by_label_ref ranges_by_label;
3748
3749 /* Number of elements currently allocated for ranges_by_label.  */
3750 static GTY(()) unsigned ranges_by_label_allocated;
3751
3752 /* Number of elements in ranges_by_label currently in use.  */
3753 static GTY(()) unsigned ranges_by_label_in_use;
3754
3755 /* Size (in elements) of increments by which we may expand the
3756    ranges_table.  */
3757 #define RANGES_TABLE_INCREMENT 64
3758
3759 /* Whether we have location lists that need outputting */
3760 static GTY(()) bool have_location_lists;
3761
3762 /* Unique label counter.  */
3763 static GTY(()) unsigned int loclabel_num;
3764
3765 /* Unique label counter for point-of-call tables.  */
3766 static GTY(()) unsigned int poc_label_num;
3767
3768 /* Record whether the function being analyzed contains inlined functions.  */
3769 static int current_function_has_inlines;
3770
3771 /* The last file entry emitted by maybe_emit_file().  */
3772 static GTY(()) struct dwarf_file_data * last_emitted_file;
3773
3774 /* Number of internal labels generated by gen_internal_sym().  */
3775 static GTY(()) int label_num;
3776
3777 /* Cached result of previous call to lookup_filename.  */
3778 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3779
3780 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3781
3782 /* Instances of generic types for which we need to generate debug
3783    info that describe their generic parameters and arguments. That
3784    generation needs to happen once all types are properly laid out so
3785    we do it at the end of compilation.  */
3786 static GTY(()) VEC(tree,gc) *generic_type_instances;
3787
3788 /* Offset from the "steady-state frame pointer" to the frame base,
3789    within the current function.  */
3790 static HOST_WIDE_INT frame_pointer_fb_offset;
3791 static bool frame_pointer_fb_offset_valid;
3792
3793 static VEC (dw_die_ref, heap) *base_types;
3794
3795 /* Forward declarations for functions defined in this file.  */
3796
3797 static int is_pseudo_reg (const_rtx);
3798 static tree type_main_variant (tree);
3799 static int is_tagged_type (const_tree);
3800 static const char *dwarf_tag_name (unsigned);
3801 static const char *dwarf_attr_name (unsigned);
3802 static const char *dwarf_form_name (unsigned);
3803 static tree decl_ultimate_origin (const_tree);
3804 static tree decl_class_context (tree);
3805 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3806 static inline enum dw_val_class AT_class (dw_attr_ref);
3807 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3808 static inline unsigned AT_flag (dw_attr_ref);
3809 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3810 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3811 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3812 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3813 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3814                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3815 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3816                                unsigned int, unsigned char *);
3817 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3818 static hashval_t debug_str_do_hash (const void *);
3819 static int debug_str_eq (const void *, const void *);
3820 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3821 static inline const char *AT_string (dw_attr_ref);
3822 static enum dwarf_form AT_string_form (dw_attr_ref);
3823 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3824 static void add_AT_specification (dw_die_ref, dw_die_ref);
3825 static inline dw_die_ref AT_ref (dw_attr_ref);
3826 static inline int AT_ref_external (dw_attr_ref);
3827 static inline void set_AT_ref_external (dw_attr_ref, int);
3828 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3829 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3830 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3831 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3832                              dw_loc_list_ref);
3833 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3834 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3835 static inline rtx AT_addr (dw_attr_ref);
3836 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3837 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3838 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3839 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3840                            unsigned HOST_WIDE_INT);
3841 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3842                                unsigned long);
3843 static inline const char *AT_lbl (dw_attr_ref);
3844 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3845 static const char *get_AT_low_pc (dw_die_ref);
3846 static const char *get_AT_hi_pc (dw_die_ref);
3847 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3848 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3849 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3850 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3851 static bool is_cxx (void);
3852 static bool is_fortran (void);
3853 static bool is_ada (void);
3854 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3855 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3856 static void add_child_die (dw_die_ref, dw_die_ref);
3857 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3858 static dw_die_ref lookup_type_die (tree);
3859 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3860 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3861 static void equate_type_number_to_die (tree, dw_die_ref);
3862 static hashval_t decl_die_table_hash (const void *);
3863 static int decl_die_table_eq (const void *, const void *);
3864 static dw_die_ref lookup_decl_die (tree);
3865 static hashval_t common_block_die_table_hash (const void *);
3866 static int common_block_die_table_eq (const void *, const void *);
3867 static hashval_t decl_loc_table_hash (const void *);
3868 static int decl_loc_table_eq (const void *, const void *);
3869 static var_loc_list *lookup_decl_loc (const_tree);
3870 static void equate_decl_number_to_die (tree, dw_die_ref);
3871 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3872 static void print_spaces (FILE *);
3873 static void print_die (dw_die_ref, FILE *);
3874 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3875 static dw_die_ref pop_compile_unit (dw_die_ref);
3876 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3877 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3878 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3879 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3880 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3881 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3882 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3883                                    struct md5_ctx *, int *);
3884 struct checksum_attributes;
3885 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3886 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3887 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3888 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3889 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3890 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3891 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3892 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3893 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3894 static void compute_section_prefix (dw_die_ref);
3895 static int is_type_die (dw_die_ref);
3896 static int is_comdat_die (dw_die_ref);
3897 static int is_symbol_die (dw_die_ref);
3898 static void assign_symbol_names (dw_die_ref);
3899 static void break_out_includes (dw_die_ref);
3900 static int is_declaration_die (dw_die_ref);
3901 static int should_move_die_to_comdat (dw_die_ref);
3902 static dw_die_ref clone_as_declaration (dw_die_ref);
3903 static dw_die_ref clone_die (dw_die_ref);
3904 static dw_die_ref clone_tree (dw_die_ref);
3905 static void copy_declaration_context (dw_die_ref, dw_die_ref);
3906 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3907 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3908 static dw_die_ref generate_skeleton (dw_die_ref);
3909 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3910                                                          dw_die_ref);
3911 static void break_out_comdat_types (dw_die_ref);
3912 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3913 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3914 static void copy_decls_for_unworthy_types (dw_die_ref);
3915
3916 static hashval_t htab_cu_hash (const void *);
3917 static int htab_cu_eq (const void *, const void *);
3918 static void htab_cu_del (void *);
3919 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3920 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3921 static void add_sibling_attributes (dw_die_ref);
3922 static void build_abbrev_table (dw_die_ref);
3923 static void output_location_lists (dw_die_ref);
3924 static int constant_size (unsigned HOST_WIDE_INT);
3925 static unsigned long size_of_die (dw_die_ref);
3926 static void calc_die_sizes (dw_die_ref);
3927 static void calc_base_type_die_sizes (void);
3928 static void mark_dies (dw_die_ref);
3929 static void unmark_dies (dw_die_ref);
3930 static void unmark_all_dies (dw_die_ref);
3931 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3932 static unsigned long size_of_aranges (void);
3933 static enum dwarf_form value_format (dw_attr_ref);
3934 static void output_value_format (dw_attr_ref);
3935 static void output_abbrev_section (void);
3936 static void output_die_symbol (dw_die_ref);
3937 static void output_die (dw_die_ref);
3938 static void output_compilation_unit_header (void);
3939 static void output_comp_unit (dw_die_ref, int);
3940 static void output_comdat_type_unit (comdat_type_node *);
3941 static const char *dwarf2_name (tree, int);
3942 static void add_pubname (tree, dw_die_ref);
3943 static void add_pubname_string (const char *, dw_die_ref);
3944 static void add_pubtype (tree, dw_die_ref);
3945 static void output_pubnames (VEC (pubname_entry,gc) *);
3946 static void output_aranges (unsigned long);
3947 static unsigned int add_ranges_num (int);
3948 static unsigned int add_ranges (const_tree);
3949 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3950                                   bool *);
3951 static void output_ranges (void);
3952 static dw_line_info_table *new_line_info_table (void);
3953 static void output_line_info (void);
3954 static void output_file_names (void);
3955 static dw_die_ref base_type_die (tree);
3956 static int is_base_type (tree);
3957 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3958 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3959 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3960 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3961 static int type_is_enum (const_tree);
3962 static unsigned int dbx_reg_number (const_rtx);
3963 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3964 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3965 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3966                                                 enum var_init_status);
3967 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3968                                                      enum var_init_status);
3969 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3970                                          enum var_init_status);
3971 static int is_based_loc (const_rtx);
3972 static int resolve_one_addr (rtx *, void *);
3973 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3974                                                enum var_init_status);
3975 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3976                                         enum var_init_status);
3977 static dw_loc_list_ref loc_list_from_tree (tree, int);
3978 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3979 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3980 static tree field_type (const_tree);
3981 static unsigned int simple_type_align_in_bits (const_tree);
3982 static unsigned int simple_decl_align_in_bits (const_tree);
3983 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3984 static HOST_WIDE_INT field_byte_offset (const_tree);
3985 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3986                                          dw_loc_list_ref);
3987 static void add_data_member_location_attribute (dw_die_ref, tree);
3988 static bool add_const_value_attribute (dw_die_ref, rtx);
3989 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3990 static void insert_double (double_int, unsigned char *);
3991 static void insert_float (const_rtx, unsigned char *);
3992 static rtx rtl_for_decl_location (tree);
3993 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3994                                                    enum dwarf_attribute);
3995 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3996 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3997 static void add_name_attribute (dw_die_ref, const char *);
3998 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3999 static void add_comp_dir_attribute (dw_die_ref);
4000 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4001 static void add_subscript_info (dw_die_ref, tree, bool);
4002 static void add_byte_size_attribute (dw_die_ref, tree);
4003 static void add_bit_offset_attribute (dw_die_ref, tree);
4004 static void add_bit_size_attribute (dw_die_ref, tree);
4005 static void add_prototyped_attribute (dw_die_ref, tree);
4006 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
4007 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4008 static void add_src_coords_attributes (dw_die_ref, tree);
4009 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4010 static void push_decl_scope (tree);
4011 static void pop_decl_scope (void);
4012 static dw_die_ref scope_die_for (tree, dw_die_ref);
4013 static inline int local_scope_p (dw_die_ref);
4014 static inline int class_scope_p (dw_die_ref);
4015 static inline int class_or_namespace_scope_p (dw_die_ref);
4016 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4017 static void add_calling_convention_attribute (dw_die_ref, tree);
4018 static const char *type_tag (const_tree);
4019 static tree member_declared_type (const_tree);
4020 #if 0
4021 static const char *decl_start_label (tree);
4022 #endif
4023 static void gen_array_type_die (tree, dw_die_ref);
4024 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4025 #if 0
4026 static void gen_entry_point_die (tree, dw_die_ref);
4027 #endif
4028 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4029 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
4030 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
4031 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4032 static void gen_formal_types_die (tree, dw_die_ref);
4033 static void gen_subprogram_die (tree, dw_die_ref);
4034 static void gen_variable_die (tree, tree, dw_die_ref);
4035 static void gen_const_die (tree, dw_die_ref);
4036 static void gen_label_die (tree, dw_die_ref);
4037 static void gen_lexical_block_die (tree, dw_die_ref, int);
4038 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4039 static void gen_field_die (tree, dw_die_ref);
4040 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4041 static dw_die_ref gen_compile_unit_die (const char *);
4042 static void gen_inheritance_die (tree, tree, dw_die_ref);
4043 static void gen_member_die (tree, dw_die_ref);
4044 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4045                                                 enum debug_info_usage);
4046 static void gen_subroutine_type_die (tree, dw_die_ref);
4047 static void gen_typedef_die (tree, dw_die_ref);
4048 static void gen_type_die (tree, dw_die_ref);
4049 static void gen_block_die (tree, dw_die_ref, int);
4050 static void decls_for_scope (tree, dw_die_ref, int);
4051 static inline int is_redundant_typedef (const_tree);
4052 static bool is_naming_typedef_decl (const_tree);
4053 static inline dw_die_ref get_context_die (tree);
4054 static void gen_namespace_die (tree, dw_die_ref);
4055 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
4056 static dw_die_ref force_decl_die (tree);
4057 static dw_die_ref force_type_die (tree);
4058 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4059 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
4060 static struct dwarf_file_data * lookup_filename (const char *);
4061 static void retry_incomplete_types (void);
4062 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4063 static void gen_generic_params_dies (tree);
4064 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
4065 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
4066 static void splice_child_die (dw_die_ref, dw_die_ref);
4067 static int file_info_cmp (const void *, const void *);
4068 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4069                                      const char *, const char *);
4070 static void output_loc_list (dw_loc_list_ref);
4071 static char *gen_internal_sym (const char *);
4072
4073 static void prune_unmark_dies (dw_die_ref);
4074 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
4075 static void prune_unused_types_mark (dw_die_ref, int);
4076 static void prune_unused_types_walk (dw_die_ref);
4077 static void prune_unused_types_walk_attribs (dw_die_ref);
4078 static void prune_unused_types_prune (dw_die_ref);
4079 static void prune_unused_types (void);
4080 static int maybe_emit_file (struct dwarf_file_data *fd);
4081 static inline const char *AT_vms_delta1 (dw_attr_ref);
4082 static inline const char *AT_vms_delta2 (dw_attr_ref);
4083 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
4084                                      const char *, const char *);
4085 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
4086 static void gen_remaining_tmpl_value_param_die_attribute (void);
4087 static bool generic_type_p (tree);
4088 static void schedule_generic_params_dies_gen (tree t);
4089 static void gen_scheduled_generic_parms_dies (void);
4090
4091 /* Section names used to hold DWARF debugging information.  */
4092 #ifndef DEBUG_INFO_SECTION
4093 #define DEBUG_INFO_SECTION      ".debug_info"
4094 #endif
4095 #ifndef DEBUG_ABBREV_SECTION
4096 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4097 #endif
4098 #ifndef DEBUG_ARANGES_SECTION
4099 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4100 #endif
4101 #ifndef DEBUG_MACINFO_SECTION
4102 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4103 #endif
4104 #ifndef DEBUG_LINE_SECTION
4105 #define DEBUG_LINE_SECTION      ".debug_line"
4106 #endif
4107 #ifndef DEBUG_LOC_SECTION
4108 #define DEBUG_LOC_SECTION       ".debug_loc"
4109 #endif
4110 #ifndef DEBUG_PUBNAMES_SECTION
4111 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4112 #endif
4113 #ifndef DEBUG_PUBTYPES_SECTION
4114 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
4115 #endif
4116 #ifndef DEBUG_STR_SECTION
4117 #define DEBUG_STR_SECTION       ".debug_str"
4118 #endif
4119 #ifndef DEBUG_RANGES_SECTION
4120 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4121 #endif
4122
4123 /* Standard ELF section names for compiled code and data.  */
4124 #ifndef TEXT_SECTION_NAME
4125 #define TEXT_SECTION_NAME       ".text"
4126 #endif
4127
4128 /* Section flags for .debug_str section.  */
4129 #define DEBUG_STR_SECTION_FLAGS \
4130   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4131    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4132    : SECTION_DEBUG)
4133
4134 /* Labels we insert at beginning sections we can reference instead of
4135    the section names themselves.  */
4136
4137 #ifndef TEXT_SECTION_LABEL
4138 #define TEXT_SECTION_LABEL              "Ltext"
4139 #endif
4140 #ifndef COLD_TEXT_SECTION_LABEL
4141 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4142 #endif
4143 #ifndef DEBUG_LINE_SECTION_LABEL
4144 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4145 #endif
4146 #ifndef DEBUG_INFO_SECTION_LABEL
4147 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4148 #endif
4149 #ifndef DEBUG_ABBREV_SECTION_LABEL
4150 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4151 #endif
4152 #ifndef DEBUG_LOC_SECTION_LABEL
4153 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4154 #endif
4155 #ifndef DEBUG_RANGES_SECTION_LABEL
4156 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4157 #endif
4158 #ifndef DEBUG_MACINFO_SECTION_LABEL
4159 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4160 #endif
4161
4162
4163 /* Definitions of defaults for formats and names of various special
4164    (artificial) labels which may be generated within this file (when the -g
4165    options is used and DWARF2_DEBUGGING_INFO is in effect.
4166    If necessary, these may be overridden from within the tm.h file, but
4167    typically, overriding these defaults is unnecessary.  */
4168
4169 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4170 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4171 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4172 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4173 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4174 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4175 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4176 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4177 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4178 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4179
4180 #ifndef TEXT_END_LABEL
4181 #define TEXT_END_LABEL          "Letext"
4182 #endif
4183 #ifndef COLD_END_LABEL
4184 #define COLD_END_LABEL          "Letext_cold"
4185 #endif
4186 #ifndef BLOCK_BEGIN_LABEL
4187 #define BLOCK_BEGIN_LABEL       "LBB"
4188 #endif
4189 #ifndef BLOCK_END_LABEL
4190 #define BLOCK_END_LABEL         "LBE"
4191 #endif
4192 #ifndef LINE_CODE_LABEL
4193 #define LINE_CODE_LABEL         "LM"
4194 #endif
4195
4196 \f
4197 /* Return the root of the DIE's built for the current compilation unit.  */
4198 static dw_die_ref
4199 comp_unit_die (void)
4200 {
4201   if (!single_comp_unit_die)
4202     single_comp_unit_die = gen_compile_unit_die (NULL);
4203   return single_comp_unit_die;
4204 }
4205
4206 /* We allow a language front-end to designate a function that is to be
4207    called to "demangle" any name before it is put into a DIE.  */
4208
4209 static const char *(*demangle_name_func) (const char *);
4210
4211 void
4212 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4213 {
4214   demangle_name_func = func;
4215 }
4216
4217 /* Test if rtl node points to a pseudo register.  */
4218
4219 static inline int
4220 is_pseudo_reg (const_rtx rtl)
4221 {
4222   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4223           || (GET_CODE (rtl) == SUBREG
4224               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4225 }
4226
4227 /* Return a reference to a type, with its const and volatile qualifiers
4228    removed.  */
4229
4230 static inline tree
4231 type_main_variant (tree type)
4232 {
4233   type = TYPE_MAIN_VARIANT (type);
4234
4235   /* ??? There really should be only one main variant among any group of
4236      variants of a given type (and all of the MAIN_VARIANT values for all
4237      members of the group should point to that one type) but sometimes the C
4238      front-end messes this up for array types, so we work around that bug
4239      here.  */
4240   if (TREE_CODE (type) == ARRAY_TYPE)
4241     while (type != TYPE_MAIN_VARIANT (type))
4242       type = TYPE_MAIN_VARIANT (type);
4243
4244   return type;
4245 }
4246
4247 /* Return nonzero if the given type node represents a tagged type.  */
4248
4249 static inline int
4250 is_tagged_type (const_tree type)
4251 {
4252   enum tree_code code = TREE_CODE (type);
4253
4254   return (code == RECORD_TYPE || code == UNION_TYPE
4255           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4256 }
4257
4258 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
4259
4260 static void
4261 get_ref_die_offset_label (char *label, dw_die_ref ref)
4262 {
4263   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4264 }
4265
4266 /* Return die_offset of a DIE reference to a base type.  */
4267
4268 static unsigned long int
4269 get_base_type_offset (dw_die_ref ref)
4270 {
4271   if (ref->die_offset)
4272     return ref->die_offset;
4273   if (comp_unit_die ()->die_abbrev)
4274     {
4275       calc_base_type_die_sizes ();
4276       gcc_assert (ref->die_offset);
4277     }
4278   return ref->die_offset;
4279 }
4280
4281 /* Return die_offset of a DIE reference other than base type.  */
4282
4283 static unsigned long int
4284 get_ref_die_offset (dw_die_ref ref)
4285 {
4286   gcc_assert (ref->die_offset);
4287   return ref->die_offset;
4288 }
4289
4290 /* Convert a DIE tag into its string name.  */
4291
4292 static const char *
4293 dwarf_tag_name (unsigned int tag)
4294 {
4295   switch (tag)
4296     {
4297     case DW_TAG_padding:
4298       return "DW_TAG_padding";
4299     case DW_TAG_array_type:
4300       return "DW_TAG_array_type";
4301     case DW_TAG_class_type:
4302       return "DW_TAG_class_type";
4303     case DW_TAG_entry_point:
4304       return "DW_TAG_entry_point";
4305     case DW_TAG_enumeration_type:
4306       return "DW_TAG_enumeration_type";
4307     case DW_TAG_formal_parameter:
4308       return "DW_TAG_formal_parameter";
4309     case DW_TAG_imported_declaration:
4310       return "DW_TAG_imported_declaration";
4311     case DW_TAG_label:
4312       return "DW_TAG_label";
4313     case DW_TAG_lexical_block:
4314       return "DW_TAG_lexical_block";
4315     case DW_TAG_member:
4316       return "DW_TAG_member";
4317     case DW_TAG_pointer_type:
4318       return "DW_TAG_pointer_type";
4319     case DW_TAG_reference_type:
4320       return "DW_TAG_reference_type";
4321     case DW_TAG_compile_unit:
4322       return "DW_TAG_compile_unit";
4323     case DW_TAG_string_type:
4324       return "DW_TAG_string_type";
4325     case DW_TAG_structure_type:
4326       return "DW_TAG_structure_type";
4327     case DW_TAG_subroutine_type:
4328       return "DW_TAG_subroutine_type";
4329     case DW_TAG_typedef:
4330       return "DW_TAG_typedef";
4331     case DW_TAG_union_type:
4332       return "DW_TAG_union_type";
4333     case DW_TAG_unspecified_parameters:
4334       return "DW_TAG_unspecified_parameters";
4335     case DW_TAG_variant:
4336       return "DW_TAG_variant";
4337     case DW_TAG_common_block:
4338       return "DW_TAG_common_block";
4339     case DW_TAG_common_inclusion:
4340       return "DW_TAG_common_inclusion";
4341     case DW_TAG_inheritance:
4342       return "DW_TAG_inheritance";
4343     case DW_TAG_inlined_subroutine:
4344       return "DW_TAG_inlined_subroutine";
4345     case DW_TAG_module:
4346       return "DW_TAG_module";
4347     case DW_TAG_ptr_to_member_type:
4348       return "DW_TAG_ptr_to_member_type";
4349     case DW_TAG_set_type:
4350       return "DW_TAG_set_type";
4351     case DW_TAG_subrange_type:
4352       return "DW_TAG_subrange_type";
4353     case DW_TAG_with_stmt:
4354       return "DW_TAG_with_stmt";
4355     case DW_TAG_access_declaration:
4356       return "DW_TAG_access_declaration";
4357     case DW_TAG_base_type:
4358       return "DW_TAG_base_type";
4359     case DW_TAG_catch_block:
4360       return "DW_TAG_catch_block";
4361     case DW_TAG_const_type:
4362       return "DW_TAG_const_type";
4363     case DW_TAG_constant:
4364       return "DW_TAG_constant";
4365     case DW_TAG_enumerator:
4366       return "DW_TAG_enumerator";
4367     case DW_TAG_file_type:
4368       return "DW_TAG_file_type";
4369     case DW_TAG_friend:
4370       return "DW_TAG_friend";
4371     case DW_TAG_namelist:
4372       return "DW_TAG_namelist";
4373     case DW_TAG_namelist_item:
4374       return "DW_TAG_namelist_item";
4375     case DW_TAG_packed_type:
4376       return "DW_TAG_packed_type";
4377     case DW_TAG_subprogram:
4378       return "DW_TAG_subprogram";
4379     case DW_TAG_template_type_param:
4380       return "DW_TAG_template_type_param";
4381     case DW_TAG_template_value_param:
4382       return "DW_TAG_template_value_param";
4383     case DW_TAG_thrown_type:
4384       return "DW_TAG_thrown_type";
4385     case DW_TAG_try_block:
4386       return "DW_TAG_try_block";
4387     case DW_TAG_variant_part:
4388       return "DW_TAG_variant_part";
4389     case DW_TAG_variable:
4390       return "DW_TAG_variable";
4391     case DW_TAG_volatile_type:
4392       return "DW_TAG_volatile_type";
4393     case DW_TAG_dwarf_procedure:
4394       return "DW_TAG_dwarf_procedure";
4395     case DW_TAG_restrict_type:
4396       return "DW_TAG_restrict_type";
4397     case DW_TAG_interface_type:
4398       return "DW_TAG_interface_type";
4399     case DW_TAG_namespace:
4400       return "DW_TAG_namespace";
4401     case DW_TAG_imported_module:
4402       return "DW_TAG_imported_module";
4403     case DW_TAG_unspecified_type:
4404       return "DW_TAG_unspecified_type";
4405     case DW_TAG_partial_unit:
4406       return "DW_TAG_partial_unit";
4407     case DW_TAG_imported_unit:
4408       return "DW_TAG_imported_unit";
4409     case DW_TAG_condition:
4410       return "DW_TAG_condition";
4411     case DW_TAG_shared_type:
4412       return "DW_TAG_shared_type";
4413     case DW_TAG_type_unit:
4414       return "DW_TAG_type_unit";
4415     case DW_TAG_rvalue_reference_type:
4416       return "DW_TAG_rvalue_reference_type";
4417     case DW_TAG_template_alias:
4418       return "DW_TAG_template_alias";
4419     case DW_TAG_GNU_template_parameter_pack:
4420       return "DW_TAG_GNU_template_parameter_pack";
4421     case DW_TAG_GNU_formal_parameter_pack:
4422       return "DW_TAG_GNU_formal_parameter_pack";
4423     case DW_TAG_MIPS_loop:
4424       return "DW_TAG_MIPS_loop";
4425     case DW_TAG_format_label:
4426       return "DW_TAG_format_label";
4427     case DW_TAG_function_template:
4428       return "DW_TAG_function_template";
4429     case DW_TAG_class_template:
4430       return "DW_TAG_class_template";
4431     case DW_TAG_GNU_BINCL:
4432       return "DW_TAG_GNU_BINCL";
4433     case DW_TAG_GNU_EINCL:
4434       return "DW_TAG_GNU_EINCL";
4435     case DW_TAG_GNU_template_template_param:
4436       return "DW_TAG_GNU_template_template_param";
4437     case DW_TAG_GNU_call_site:
4438       return "DW_TAG_GNU_call_site";
4439     case DW_TAG_GNU_call_site_parameter:
4440       return "DW_TAG_GNU_call_site_parameter";
4441     default:
4442       return "DW_TAG_<unknown>";
4443     }
4444 }
4445
4446 /* Convert a DWARF attribute code into its string name.  */
4447
4448 static const char *
4449 dwarf_attr_name (unsigned int attr)
4450 {
4451   switch (attr)
4452     {
4453     case DW_AT_sibling:
4454       return "DW_AT_sibling";
4455     case DW_AT_location:
4456       return "DW_AT_location";
4457     case DW_AT_name:
4458       return "DW_AT_name";
4459     case DW_AT_ordering:
4460       return "DW_AT_ordering";
4461     case DW_AT_subscr_data:
4462       return "DW_AT_subscr_data";
4463     case DW_AT_byte_size:
4464       return "DW_AT_byte_size";
4465     case DW_AT_bit_offset:
4466       return "DW_AT_bit_offset";
4467     case DW_AT_bit_size:
4468       return "DW_AT_bit_size";
4469     case DW_AT_element_list:
4470       return "DW_AT_element_list";
4471     case DW_AT_stmt_list:
4472       return "DW_AT_stmt_list";
4473     case DW_AT_low_pc:
4474       return "DW_AT_low_pc";
4475     case DW_AT_high_pc:
4476       return "DW_AT_high_pc";
4477     case DW_AT_language:
4478       return "DW_AT_language";
4479     case DW_AT_member:
4480       return "DW_AT_member";
4481     case DW_AT_discr:
4482       return "DW_AT_discr";
4483     case DW_AT_discr_value:
4484       return "DW_AT_discr_value";
4485     case DW_AT_visibility:
4486       return "DW_AT_visibility";
4487     case DW_AT_import:
4488       return "DW_AT_import";
4489     case DW_AT_string_length:
4490       return "DW_AT_string_length";
4491     case DW_AT_common_reference:
4492       return "DW_AT_common_reference";
4493     case DW_AT_comp_dir:
4494       return "DW_AT_comp_dir";
4495     case DW_AT_const_value:
4496       return "DW_AT_const_value";
4497     case DW_AT_containing_type:
4498       return "DW_AT_containing_type";
4499     case DW_AT_default_value:
4500       return "DW_AT_default_value";
4501     case DW_AT_inline:
4502       return "DW_AT_inline";
4503     case DW_AT_is_optional:
4504       return "DW_AT_is_optional";
4505     case DW_AT_lower_bound:
4506       return "DW_AT_lower_bound";
4507     case DW_AT_producer:
4508       return "DW_AT_producer";
4509     case DW_AT_prototyped:
4510       return "DW_AT_prototyped";
4511     case DW_AT_return_addr:
4512       return "DW_AT_return_addr";
4513     case DW_AT_start_scope:
4514       return "DW_AT_start_scope";
4515     case DW_AT_bit_stride:
4516       return "DW_AT_bit_stride";
4517     case DW_AT_upper_bound:
4518       return "DW_AT_upper_bound";
4519     case DW_AT_abstract_origin:
4520       return "DW_AT_abstract_origin";
4521     case DW_AT_accessibility:
4522       return "DW_AT_accessibility";
4523     case DW_AT_address_class:
4524       return "DW_AT_address_class";
4525     case DW_AT_artificial:
4526       return "DW_AT_artificial";
4527     case DW_AT_base_types:
4528       return "DW_AT_base_types";
4529     case DW_AT_calling_convention:
4530       return "DW_AT_calling_convention";
4531     case DW_AT_count:
4532       return "DW_AT_count";
4533     case DW_AT_data_member_location:
4534       return "DW_AT_data_member_location";
4535     case DW_AT_decl_column:
4536       return "DW_AT_decl_column";
4537     case DW_AT_decl_file:
4538       return "DW_AT_decl_file";
4539     case DW_AT_decl_line:
4540       return "DW_AT_decl_line";
4541     case DW_AT_declaration:
4542       return "DW_AT_declaration";
4543     case DW_AT_discr_list:
4544       return "DW_AT_discr_list";
4545     case DW_AT_encoding:
4546       return "DW_AT_encoding";
4547     case DW_AT_external:
4548       return "DW_AT_external";
4549     case DW_AT_explicit:
4550       return "DW_AT_explicit";
4551     case DW_AT_frame_base:
4552       return "DW_AT_frame_base";
4553     case DW_AT_friend:
4554       return "DW_AT_friend";
4555     case DW_AT_identifier_case:
4556       return "DW_AT_identifier_case";
4557     case DW_AT_macro_info:
4558       return "DW_AT_macro_info";
4559     case DW_AT_namelist_items:
4560       return "DW_AT_namelist_items";
4561     case DW_AT_priority:
4562       return "DW_AT_priority";
4563     case DW_AT_segment:
4564       return "DW_AT_segment";
4565     case DW_AT_specification:
4566       return "DW_AT_specification";
4567     case DW_AT_static_link:
4568       return "DW_AT_static_link";
4569     case DW_AT_type:
4570       return "DW_AT_type";
4571     case DW_AT_use_location:
4572       return "DW_AT_use_location";
4573     case DW_AT_variable_parameter:
4574       return "DW_AT_variable_parameter";
4575     case DW_AT_virtuality:
4576       return "DW_AT_virtuality";
4577     case DW_AT_vtable_elem_location:
4578       return "DW_AT_vtable_elem_location";
4579
4580     case DW_AT_allocated:
4581       return "DW_AT_allocated";
4582     case DW_AT_associated:
4583       return "DW_AT_associated";
4584     case DW_AT_data_location:
4585       return "DW_AT_data_location";
4586     case DW_AT_byte_stride:
4587       return "DW_AT_byte_stride";
4588     case DW_AT_entry_pc:
4589       return "DW_AT_entry_pc";
4590     case DW_AT_use_UTF8:
4591       return "DW_AT_use_UTF8";
4592     case DW_AT_extension:
4593       return "DW_AT_extension";
4594     case DW_AT_ranges:
4595       return "DW_AT_ranges";
4596     case DW_AT_trampoline:
4597       return "DW_AT_trampoline";
4598     case DW_AT_call_column:
4599       return "DW_AT_call_column";
4600     case DW_AT_call_file:
4601       return "DW_AT_call_file";
4602     case DW_AT_call_line:
4603       return "DW_AT_call_line";
4604     case DW_AT_object_pointer:
4605       return "DW_AT_object_pointer";
4606
4607     case DW_AT_signature:
4608       return "DW_AT_signature";
4609     case DW_AT_main_subprogram:
4610       return "DW_AT_main_subprogram";
4611     case DW_AT_data_bit_offset:
4612       return "DW_AT_data_bit_offset";
4613     case DW_AT_const_expr:
4614       return "DW_AT_const_expr";
4615     case DW_AT_enum_class:
4616       return "DW_AT_enum_class";
4617     case DW_AT_linkage_name:
4618       return "DW_AT_linkage_name";
4619
4620     case DW_AT_MIPS_fde:
4621       return "DW_AT_MIPS_fde";
4622     case DW_AT_MIPS_loop_begin:
4623       return "DW_AT_MIPS_loop_begin";
4624     case DW_AT_MIPS_tail_loop_begin:
4625       return "DW_AT_MIPS_tail_loop_begin";
4626     case DW_AT_MIPS_epilog_begin:
4627       return "DW_AT_MIPS_epilog_begin";
4628 #if VMS_DEBUGGING_INFO
4629     case DW_AT_HP_prologue:
4630       return "DW_AT_HP_prologue";
4631 #else
4632     case DW_AT_MIPS_loop_unroll_factor:
4633       return "DW_AT_MIPS_loop_unroll_factor";
4634 #endif
4635     case DW_AT_MIPS_software_pipeline_depth:
4636       return "DW_AT_MIPS_software_pipeline_depth";
4637     case DW_AT_MIPS_linkage_name:
4638       return "DW_AT_MIPS_linkage_name";
4639 #if VMS_DEBUGGING_INFO
4640     case DW_AT_HP_epilogue:
4641       return "DW_AT_HP_epilogue";
4642 #else
4643     case DW_AT_MIPS_stride:
4644       return "DW_AT_MIPS_stride";
4645 #endif
4646     case DW_AT_MIPS_abstract_name:
4647       return "DW_AT_MIPS_abstract_name";
4648     case DW_AT_MIPS_clone_origin:
4649       return "DW_AT_MIPS_clone_origin";
4650     case DW_AT_MIPS_has_inlines:
4651       return "DW_AT_MIPS_has_inlines";
4652
4653     case DW_AT_sf_names:
4654       return "DW_AT_sf_names";
4655     case DW_AT_src_info:
4656       return "DW_AT_src_info";
4657     case DW_AT_mac_info:
4658       return "DW_AT_mac_info";
4659     case DW_AT_src_coords:
4660       return "DW_AT_src_coords";
4661     case DW_AT_body_begin:
4662       return "DW_AT_body_begin";
4663     case DW_AT_body_end:
4664       return "DW_AT_body_end";
4665
4666     case DW_AT_GNU_vector:
4667       return "DW_AT_GNU_vector";
4668     case DW_AT_GNU_guarded_by:
4669       return "DW_AT_GNU_guarded_by";
4670     case DW_AT_GNU_pt_guarded_by:
4671       return "DW_AT_GNU_pt_guarded_by";
4672     case DW_AT_GNU_guarded:
4673       return "DW_AT_GNU_guarded";
4674     case DW_AT_GNU_pt_guarded:
4675       return "DW_AT_GNU_pt_guarded";
4676     case DW_AT_GNU_locks_excluded:
4677       return "DW_AT_GNU_locks_excluded";
4678     case DW_AT_GNU_exclusive_locks_required:
4679       return "DW_AT_GNU_exclusive_locks_required";
4680     case DW_AT_GNU_shared_locks_required:
4681       return "DW_AT_GNU_shared_locks_required";
4682     case DW_AT_GNU_odr_signature:
4683       return "DW_AT_GNU_odr_signature";
4684     case DW_AT_GNU_template_name:
4685       return "DW_AT_GNU_template_name";
4686     case DW_AT_GNU_call_site_value:
4687       return "DW_AT_GNU_call_site_value";
4688     case DW_AT_GNU_call_site_data_value:
4689       return "DW_AT_GNU_call_site_data_value";
4690     case DW_AT_GNU_call_site_target:
4691       return "DW_AT_GNU_call_site_target";
4692     case DW_AT_GNU_call_site_target_clobbered:
4693       return "DW_AT_GNU_call_site_target_clobbered";
4694     case DW_AT_GNU_tail_call:
4695       return "DW_AT_GNU_tail_call";
4696     case DW_AT_GNU_all_tail_call_sites:
4697       return "DW_AT_GNU_all_tail_call_sites";
4698     case DW_AT_GNU_all_call_sites:
4699       return "DW_AT_GNU_all_call_sites";
4700     case DW_AT_GNU_all_source_call_sites:
4701       return "DW_AT_GNU_all_source_call_sites";
4702
4703     case DW_AT_GNAT_descriptive_type:
4704       return "DW_AT_GNAT_descriptive_type";
4705
4706     case DW_AT_VMS_rtnbeg_pd_address:
4707       return "DW_AT_VMS_rtnbeg_pd_address";
4708
4709     default:
4710       return "DW_AT_<unknown>";
4711     }
4712 }
4713
4714 /* Convert a DWARF value form code into its string name.  */
4715
4716 static const char *
4717 dwarf_form_name (unsigned int form)
4718 {
4719   switch (form)
4720     {
4721     case DW_FORM_addr:
4722       return "DW_FORM_addr";
4723     case DW_FORM_block2:
4724       return "DW_FORM_block2";
4725     case DW_FORM_block4:
4726       return "DW_FORM_block4";
4727     case DW_FORM_data2:
4728       return "DW_FORM_data2";
4729     case DW_FORM_data4:
4730       return "DW_FORM_data4";
4731     case DW_FORM_data8:
4732       return "DW_FORM_data8";
4733     case DW_FORM_string:
4734       return "DW_FORM_string";
4735     case DW_FORM_block:
4736       return "DW_FORM_block";
4737     case DW_FORM_block1:
4738       return "DW_FORM_block1";
4739     case DW_FORM_data1:
4740       return "DW_FORM_data1";
4741     case DW_FORM_flag:
4742       return "DW_FORM_flag";
4743     case DW_FORM_sdata:
4744       return "DW_FORM_sdata";
4745     case DW_FORM_strp:
4746       return "DW_FORM_strp";
4747     case DW_FORM_udata:
4748       return "DW_FORM_udata";
4749     case DW_FORM_ref_addr:
4750       return "DW_FORM_ref_addr";
4751     case DW_FORM_ref1:
4752       return "DW_FORM_ref1";
4753     case DW_FORM_ref2:
4754       return "DW_FORM_ref2";
4755     case DW_FORM_ref4:
4756       return "DW_FORM_ref4";
4757     case DW_FORM_ref8:
4758       return "DW_FORM_ref8";
4759     case DW_FORM_ref_udata:
4760       return "DW_FORM_ref_udata";
4761     case DW_FORM_indirect:
4762       return "DW_FORM_indirect";
4763     case DW_FORM_sec_offset:
4764       return "DW_FORM_sec_offset";
4765     case DW_FORM_exprloc:
4766       return "DW_FORM_exprloc";
4767     case DW_FORM_flag_present:
4768       return "DW_FORM_flag_present";
4769     case DW_FORM_ref_sig8:
4770       return "DW_FORM_ref_sig8";
4771     default:
4772       return "DW_FORM_<unknown>";
4773     }
4774 }
4775 \f
4776 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4777    instance of an inlined instance of a decl which is local to an inline
4778    function, so we have to trace all of the way back through the origin chain
4779    to find out what sort of node actually served as the original seed for the
4780    given block.  */
4781
4782 static tree
4783 decl_ultimate_origin (const_tree decl)
4784 {
4785   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4786     return NULL_TREE;
4787
4788   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4789      nodes in the function to point to themselves; ignore that if
4790      we're trying to output the abstract instance of this function.  */
4791   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4792     return NULL_TREE;
4793
4794   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4795      most distant ancestor, this should never happen.  */
4796   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4797
4798   return DECL_ABSTRACT_ORIGIN (decl);
4799 }
4800
4801 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4802    of a virtual function may refer to a base class, so we check the 'this'
4803    parameter.  */
4804
4805 static tree
4806 decl_class_context (tree decl)
4807 {
4808   tree context = NULL_TREE;
4809
4810   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4811     context = DECL_CONTEXT (decl);
4812   else
4813     context = TYPE_MAIN_VARIANT
4814       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4815
4816   if (context && !TYPE_P (context))
4817     context = NULL_TREE;
4818
4819   return context;
4820 }
4821 \f
4822 /* Add an attribute/value pair to a DIE.  */
4823
4824 static inline void
4825 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4826 {
4827   /* Maybe this should be an assert?  */
4828   if (die == NULL)
4829     return;
4830
4831   if (die->die_attr == NULL)
4832     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4833   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4834 }
4835
4836 static inline enum dw_val_class
4837 AT_class (dw_attr_ref a)
4838 {
4839   return a->dw_attr_val.val_class;
4840 }
4841
4842 /* Add a flag value attribute to a DIE.  */
4843
4844 static inline void
4845 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4846 {
4847   dw_attr_node attr;
4848
4849   attr.dw_attr = attr_kind;
4850   attr.dw_attr_val.val_class = dw_val_class_flag;
4851   attr.dw_attr_val.v.val_flag = flag;
4852   add_dwarf_attr (die, &attr);
4853 }
4854
4855 static inline unsigned
4856 AT_flag (dw_attr_ref a)
4857 {
4858   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4859   return a->dw_attr_val.v.val_flag;
4860 }
4861
4862 /* Add a signed integer attribute value to a DIE.  */
4863
4864 static inline void
4865 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4866 {
4867   dw_attr_node attr;
4868
4869   attr.dw_attr = attr_kind;
4870   attr.dw_attr_val.val_class = dw_val_class_const;
4871   attr.dw_attr_val.v.val_int = int_val;
4872   add_dwarf_attr (die, &attr);
4873 }
4874
4875 static inline HOST_WIDE_INT
4876 AT_int (dw_attr_ref a)
4877 {
4878   gcc_assert (a && AT_class (a) == dw_val_class_const);
4879   return a->dw_attr_val.v.val_int;
4880 }
4881
4882 /* Add an unsigned integer attribute value to a DIE.  */
4883
4884 static inline void
4885 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4886                  unsigned HOST_WIDE_INT unsigned_val)
4887 {
4888   dw_attr_node attr;
4889
4890   attr.dw_attr = attr_kind;
4891   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4892   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4893   add_dwarf_attr (die, &attr);
4894 }
4895
4896 static inline unsigned HOST_WIDE_INT
4897 AT_unsigned (dw_attr_ref a)
4898 {
4899   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4900   return a->dw_attr_val.v.val_unsigned;
4901 }
4902
4903 /* Add an unsigned double integer attribute value to a DIE.  */
4904
4905 static inline void
4906 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4907                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4908 {
4909   dw_attr_node attr;
4910
4911   attr.dw_attr = attr_kind;
4912   attr.dw_attr_val.val_class = dw_val_class_const_double;
4913   attr.dw_attr_val.v.val_double.high = high;
4914   attr.dw_attr_val.v.val_double.low = low;
4915   add_dwarf_attr (die, &attr);
4916 }
4917
4918 /* Add a floating point attribute value to a DIE and return it.  */
4919
4920 static inline void
4921 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4922             unsigned int length, unsigned int elt_size, unsigned char *array)
4923 {
4924   dw_attr_node attr;
4925
4926   attr.dw_attr = attr_kind;
4927   attr.dw_attr_val.val_class = dw_val_class_vec;
4928   attr.dw_attr_val.v.val_vec.length = length;
4929   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4930   attr.dw_attr_val.v.val_vec.array = array;
4931   add_dwarf_attr (die, &attr);
4932 }
4933
4934 /* Add an 8-byte data attribute value to a DIE.  */
4935
4936 static inline void
4937 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4938               unsigned char data8[8])
4939 {
4940   dw_attr_node attr;
4941
4942   attr.dw_attr = attr_kind;
4943   attr.dw_attr_val.val_class = dw_val_class_data8;
4944   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4945   add_dwarf_attr (die, &attr);
4946 }
4947
4948 /* Hash and equality functions for debug_str_hash.  */
4949
4950 static hashval_t
4951 debug_str_do_hash (const void *x)
4952 {
4953   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4954 }
4955
4956 static int
4957 debug_str_eq (const void *x1, const void *x2)
4958 {
4959   return strcmp ((((const struct indirect_string_node *)x1)->str),
4960                  (const char *)x2) == 0;
4961 }
4962
4963 /* Add STR to the indirect string hash table.  */
4964
4965 static struct indirect_string_node *
4966 find_AT_string (const char *str)
4967 {
4968   struct indirect_string_node *node;
4969   void **slot;
4970
4971   if (! debug_str_hash)
4972     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4973                                       debug_str_eq, NULL);
4974
4975   slot = htab_find_slot_with_hash (debug_str_hash, str,
4976                                    htab_hash_string (str), INSERT);
4977   if (*slot == NULL)
4978     {
4979       node = ggc_alloc_cleared_indirect_string_node ();
4980       node->str = ggc_strdup (str);
4981       *slot = node;
4982     }
4983   else
4984     node = (struct indirect_string_node *) *slot;
4985
4986   node->refcount++;
4987   return node;
4988 }
4989
4990 /* Add a string attribute value to a DIE.  */
4991
4992 static inline void
4993 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4994 {
4995   dw_attr_node attr;
4996   struct indirect_string_node *node;
4997
4998   node = find_AT_string (str);
4999
5000   attr.dw_attr = attr_kind;
5001   attr.dw_attr_val.val_class = dw_val_class_str;
5002   attr.dw_attr_val.v.val_str = node;
5003   add_dwarf_attr (die, &attr);
5004 }
5005
5006 static inline const char *
5007 AT_string (dw_attr_ref a)
5008 {
5009   gcc_assert (a && AT_class (a) == dw_val_class_str);
5010   return a->dw_attr_val.v.val_str->str;
5011 }
5012
5013 /* Find out whether a string should be output inline in DIE
5014    or out-of-line in .debug_str section.  */
5015
5016 static enum dwarf_form
5017 AT_string_form (dw_attr_ref a)
5018 {
5019   struct indirect_string_node *node;
5020   unsigned int len;
5021   char label[32];
5022
5023   gcc_assert (a && AT_class (a) == dw_val_class_str);
5024
5025   node = a->dw_attr_val.v.val_str;
5026   if (node->form)
5027     return node->form;
5028
5029   len = strlen (node->str) + 1;
5030
5031   /* If the string is shorter or equal to the size of the reference, it is
5032      always better to put it inline.  */
5033   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5034     return node->form = DW_FORM_string;
5035
5036   /* If we cannot expect the linker to merge strings in .debug_str
5037      section, only put it into .debug_str if it is worth even in this
5038      single module.  */
5039   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
5040       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
5041       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
5042     return node->form = DW_FORM_string;
5043
5044   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5045   ++dw2_string_counter;
5046   node->label = xstrdup (label);
5047
5048   return node->form = DW_FORM_strp;
5049 }
5050
5051 /* Add a DIE reference attribute value to a DIE.  */
5052
5053 static inline void
5054 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5055 {
5056   dw_attr_node attr;
5057
5058 #ifdef ENABLE_CHECKING
5059   gcc_assert (targ_die != NULL);
5060 #else
5061   /* With LTO we can end up trying to reference something we didn't create
5062      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
5063   if (targ_die == NULL)
5064     return;
5065 #endif
5066
5067   attr.dw_attr = attr_kind;
5068   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5069   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5070   attr.dw_attr_val.v.val_die_ref.external = 0;
5071   add_dwarf_attr (die, &attr);
5072 }
5073
5074 /* Add an AT_specification attribute to a DIE, and also make the back
5075    pointer from the specification to the definition.  */
5076
5077 static inline void
5078 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5079 {
5080   add_AT_die_ref (die, DW_AT_specification, targ_die);
5081   gcc_assert (!targ_die->die_definition);
5082   targ_die->die_definition = die;
5083 }
5084
5085 static inline dw_die_ref
5086 AT_ref (dw_attr_ref a)
5087 {
5088   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5089   return a->dw_attr_val.v.val_die_ref.die;
5090 }
5091
5092 static inline int
5093 AT_ref_external (dw_attr_ref a)
5094 {
5095   if (a && AT_class (a) == dw_val_class_die_ref)
5096     return a->dw_attr_val.v.val_die_ref.external;
5097
5098   return 0;
5099 }
5100
5101 static inline void
5102 set_AT_ref_external (dw_attr_ref a, int i)
5103 {
5104   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5105   a->dw_attr_val.v.val_die_ref.external = i;
5106 }
5107
5108 /* Add an FDE reference attribute value to a DIE.  */
5109
5110 static inline void
5111 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5112 {
5113   dw_attr_node attr;
5114
5115   attr.dw_attr = attr_kind;
5116   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5117   attr.dw_attr_val.v.val_fde_index = targ_fde;
5118   add_dwarf_attr (die, &attr);
5119 }
5120
5121 /* Add a location description attribute value to a DIE.  */
5122
5123 static inline void
5124 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5125 {
5126   dw_attr_node attr;
5127
5128   attr.dw_attr = attr_kind;
5129   attr.dw_attr_val.val_class = dw_val_class_loc;
5130   attr.dw_attr_val.v.val_loc = loc;
5131   add_dwarf_attr (die, &attr);
5132 }
5133
5134 static inline dw_loc_descr_ref
5135 AT_loc (dw_attr_ref a)
5136 {
5137   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5138   return a->dw_attr_val.v.val_loc;
5139 }
5140
5141 static inline void
5142 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5143 {
5144   dw_attr_node attr;
5145
5146   attr.dw_attr = attr_kind;
5147   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5148   attr.dw_attr_val.v.val_loc_list = loc_list;
5149   add_dwarf_attr (die, &attr);
5150   have_location_lists = true;
5151 }
5152
5153 static inline dw_loc_list_ref
5154 AT_loc_list (dw_attr_ref a)
5155 {
5156   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5157   return a->dw_attr_val.v.val_loc_list;
5158 }
5159
5160 static inline dw_loc_list_ref *
5161 AT_loc_list_ptr (dw_attr_ref a)
5162 {
5163   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5164   return &a->dw_attr_val.v.val_loc_list;
5165 }
5166
5167 /* Add an address constant attribute value to a DIE.  */
5168
5169 static inline void
5170 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5171 {
5172   dw_attr_node attr;
5173
5174   attr.dw_attr = attr_kind;
5175   attr.dw_attr_val.val_class = dw_val_class_addr;
5176   attr.dw_attr_val.v.val_addr = addr;
5177   add_dwarf_attr (die, &attr);
5178 }
5179
5180 /* Get the RTX from to an address DIE attribute.  */
5181
5182 static inline rtx
5183 AT_addr (dw_attr_ref a)
5184 {
5185   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5186   return a->dw_attr_val.v.val_addr;
5187 }
5188
5189 /* Add a file attribute value to a DIE.  */
5190
5191 static inline void
5192 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5193              struct dwarf_file_data *fd)
5194 {
5195   dw_attr_node attr;
5196
5197   attr.dw_attr = attr_kind;
5198   attr.dw_attr_val.val_class = dw_val_class_file;
5199   attr.dw_attr_val.v.val_file = fd;
5200   add_dwarf_attr (die, &attr);
5201 }
5202
5203 /* Get the dwarf_file_data from a file DIE attribute.  */
5204
5205 static inline struct dwarf_file_data *
5206 AT_file (dw_attr_ref a)
5207 {
5208   gcc_assert (a && AT_class (a) == dw_val_class_file);
5209   return a->dw_attr_val.v.val_file;
5210 }
5211
5212 /* Add a vms delta attribute value to a DIE.  */
5213
5214 static inline void
5215 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5216                   const char *lbl1, const char *lbl2)
5217 {
5218   dw_attr_node attr;
5219
5220   attr.dw_attr = attr_kind;
5221   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5222   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5223   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5224   add_dwarf_attr (die, &attr);
5225 }
5226
5227 /* Add a label identifier attribute value to a DIE.  */
5228
5229 static inline void
5230 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5231 {
5232   dw_attr_node attr;
5233
5234   attr.dw_attr = attr_kind;
5235   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5236   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5237   add_dwarf_attr (die, &attr);
5238 }
5239
5240 /* Add a section offset attribute value to a DIE, an offset into the
5241    debug_line section.  */
5242
5243 static inline void
5244 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5245                 const char *label)
5246 {
5247   dw_attr_node attr;
5248
5249   attr.dw_attr = attr_kind;
5250   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5251   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5252   add_dwarf_attr (die, &attr);
5253 }
5254
5255 /* Add a section offset attribute value to a DIE, an offset into the
5256    debug_macinfo section.  */
5257
5258 static inline void
5259 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5260                const char *label)
5261 {
5262   dw_attr_node attr;
5263
5264   attr.dw_attr = attr_kind;
5265   attr.dw_attr_val.val_class = dw_val_class_macptr;
5266   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5267   add_dwarf_attr (die, &attr);
5268 }
5269
5270 /* Add an offset attribute value to a DIE.  */
5271
5272 static inline void
5273 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5274                unsigned HOST_WIDE_INT offset)
5275 {
5276   dw_attr_node attr;
5277
5278   attr.dw_attr = attr_kind;
5279   attr.dw_attr_val.val_class = dw_val_class_offset;
5280   attr.dw_attr_val.v.val_offset = offset;
5281   add_dwarf_attr (die, &attr);
5282 }
5283
5284 /* Add an range_list attribute value to a DIE.  */
5285
5286 static void
5287 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5288                    long unsigned int offset)
5289 {
5290   dw_attr_node attr;
5291
5292   attr.dw_attr = attr_kind;
5293   attr.dw_attr_val.val_class = dw_val_class_range_list;
5294   attr.dw_attr_val.v.val_offset = offset;
5295   add_dwarf_attr (die, &attr);
5296 }
5297
5298 /* Return the start label of a delta attribute.  */
5299
5300 static inline const char *
5301 AT_vms_delta1 (dw_attr_ref a)
5302 {
5303   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5304   return a->dw_attr_val.v.val_vms_delta.lbl1;
5305 }
5306
5307 /* Return the end label of a delta attribute.  */
5308
5309 static inline const char *
5310 AT_vms_delta2 (dw_attr_ref a)
5311 {
5312   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5313   return a->dw_attr_val.v.val_vms_delta.lbl2;
5314 }
5315
5316 static inline const char *
5317 AT_lbl (dw_attr_ref a)
5318 {
5319   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5320                     || AT_class (a) == dw_val_class_lineptr
5321                     || AT_class (a) == dw_val_class_macptr));
5322   return a->dw_attr_val.v.val_lbl_id;
5323 }
5324
5325 /* Get the attribute of type attr_kind.  */
5326
5327 static dw_attr_ref
5328 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5329 {
5330   dw_attr_ref a;
5331   unsigned ix;
5332   dw_die_ref spec = NULL;
5333
5334   if (! die)
5335     return NULL;
5336
5337   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5338     if (a->dw_attr == attr_kind)
5339       return a;
5340     else if (a->dw_attr == DW_AT_specification
5341              || a->dw_attr == DW_AT_abstract_origin)
5342       spec = AT_ref (a);
5343
5344   if (spec)
5345     return get_AT (spec, attr_kind);
5346
5347   return NULL;
5348 }
5349
5350 /* Return the "low pc" attribute value, typically associated with a subprogram
5351    DIE.  Return null if the "low pc" attribute is either not present, or if it
5352    cannot be represented as an assembler label identifier.  */
5353
5354 static inline const char *
5355 get_AT_low_pc (dw_die_ref die)
5356 {
5357   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5358
5359   return a ? AT_lbl (a) : NULL;
5360 }
5361
5362 /* Return the "high pc" attribute value, typically associated with a subprogram
5363    DIE.  Return null if the "high pc" attribute is either not present, or if it
5364    cannot be represented as an assembler label identifier.  */
5365
5366 static inline const char *
5367 get_AT_hi_pc (dw_die_ref die)
5368 {
5369   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5370
5371   return a ? AT_lbl (a) : NULL;
5372 }
5373
5374 /* Return the value of the string attribute designated by ATTR_KIND, or
5375    NULL if it is not present.  */
5376
5377 static inline const char *
5378 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5379 {
5380   dw_attr_ref a = get_AT (die, attr_kind);
5381
5382   return a ? AT_string (a) : NULL;
5383 }
5384
5385 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5386    if it is not present.  */
5387
5388 static inline int
5389 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5390 {
5391   dw_attr_ref a = get_AT (die, attr_kind);
5392
5393   return a ? AT_flag (a) : 0;
5394 }
5395
5396 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5397    if it is not present.  */
5398
5399 static inline unsigned
5400 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5401 {
5402   dw_attr_ref a = get_AT (die, attr_kind);
5403
5404   return a ? AT_unsigned (a) : 0;
5405 }
5406
5407 static inline dw_die_ref
5408 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5409 {
5410   dw_attr_ref a = get_AT (die, attr_kind);
5411
5412   return a ? AT_ref (a) : NULL;
5413 }
5414
5415 static inline struct dwarf_file_data *
5416 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5417 {
5418   dw_attr_ref a = get_AT (die, attr_kind);
5419
5420   return a ? AT_file (a) : NULL;
5421 }
5422
5423 /* Return TRUE if the language is C++.  */
5424
5425 static inline bool
5426 is_cxx (void)
5427 {
5428   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5429
5430   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5431 }
5432
5433 /* Return TRUE if the language is Fortran.  */
5434
5435 static inline bool
5436 is_fortran (void)
5437 {
5438   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5439
5440   return (lang == DW_LANG_Fortran77
5441           || lang == DW_LANG_Fortran90
5442           || lang == DW_LANG_Fortran95);
5443 }
5444
5445 /* Return TRUE if the language is Ada.  */
5446
5447 static inline bool
5448 is_ada (void)
5449 {
5450   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5451
5452   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5453 }
5454
5455 /* Remove the specified attribute if present.  */
5456
5457 static void
5458 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5459 {
5460   dw_attr_ref a;
5461   unsigned ix;
5462
5463   if (! die)
5464     return;
5465
5466   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5467     if (a->dw_attr == attr_kind)
5468       {
5469         if (AT_class (a) == dw_val_class_str)
5470           if (a->dw_attr_val.v.val_str->refcount)
5471             a->dw_attr_val.v.val_str->refcount--;
5472
5473         /* VEC_ordered_remove should help reduce the number of abbrevs
5474            that are needed.  */
5475         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5476         return;
5477       }
5478 }
5479
5480 /* Remove CHILD from its parent.  PREV must have the property that
5481    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5482
5483 static void
5484 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5485 {
5486   gcc_assert (child->die_parent == prev->die_parent);
5487   gcc_assert (prev->die_sib == child);
5488   if (prev == child)
5489     {
5490       gcc_assert (child->die_parent->die_child == child);
5491       prev = NULL;
5492     }
5493   else
5494     prev->die_sib = child->die_sib;
5495   if (child->die_parent->die_child == child)
5496     child->die_parent->die_child = prev;
5497 }
5498
5499 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
5500    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
5501
5502 static void
5503 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5504 {
5505   dw_die_ref parent = old_child->die_parent;
5506
5507   gcc_assert (parent == prev->die_parent);
5508   gcc_assert (prev->die_sib == old_child);
5509
5510   new_child->die_parent = parent;
5511   if (prev == old_child)
5512     {
5513       gcc_assert (parent->die_child == old_child);
5514       new_child->die_sib = new_child;
5515     }
5516   else
5517     {
5518       prev->die_sib = new_child;
5519       new_child->die_sib = old_child->die_sib;
5520     }
5521   if (old_child->die_parent->die_child == old_child)
5522     old_child->die_parent->die_child = new_child;
5523 }
5524
5525 /* Move all children from OLD_PARENT to NEW_PARENT.  */
5526
5527 static void
5528 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5529 {
5530   dw_die_ref c;
5531   new_parent->die_child = old_parent->die_child;
5532   old_parent->die_child = NULL;
5533   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5534 }
5535
5536 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5537    matches TAG.  */
5538
5539 static void
5540 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5541 {
5542   dw_die_ref c;
5543
5544   c = die->die_child;
5545   if (c) do {
5546     dw_die_ref prev = c;
5547     c = c->die_sib;
5548     while (c->die_tag == tag)
5549       {
5550         remove_child_with_prev (c, prev);
5551         /* Might have removed every child.  */
5552         if (c == c->die_sib)
5553           return;
5554         c = c->die_sib;
5555       }
5556   } while (c != die->die_child);
5557 }
5558
5559 /* Add a CHILD_DIE as the last child of DIE.  */
5560
5561 static void
5562 add_child_die (dw_die_ref die, dw_die_ref child_die)
5563 {
5564   /* FIXME this should probably be an assert.  */
5565   if (! die || ! child_die)
5566     return;
5567   gcc_assert (die != child_die);
5568
5569   child_die->die_parent = die;
5570   if (die->die_child)
5571     {
5572       child_die->die_sib = die->die_child->die_sib;
5573       die->die_child->die_sib = child_die;
5574     }
5575   else
5576     child_die->die_sib = child_die;
5577   die->die_child = child_die;
5578 }
5579
5580 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5581    is the specification, to the end of PARENT's list of children.
5582    This is done by removing and re-adding it.  */
5583
5584 static void
5585 splice_child_die (dw_die_ref parent, dw_die_ref child)
5586 {
5587   dw_die_ref p;
5588
5589   /* We want the declaration DIE from inside the class, not the
5590      specification DIE at toplevel.  */
5591   if (child->die_parent != parent)
5592     {
5593       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5594
5595       if (tmp)
5596         child = tmp;
5597     }
5598
5599   gcc_assert (child->die_parent == parent
5600               || (child->die_parent
5601                   == get_AT_ref (parent, DW_AT_specification)));
5602
5603   for (p = child->die_parent->die_child; ; p = p->die_sib)
5604     if (p->die_sib == child)
5605       {
5606         remove_child_with_prev (child, p);
5607         break;
5608       }
5609
5610   add_child_die (parent, child);
5611 }
5612
5613 /* Return a pointer to a newly created DIE node.  */
5614
5615 static inline dw_die_ref
5616 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5617 {
5618   dw_die_ref die = ggc_alloc_cleared_die_node ();
5619
5620   die->die_tag = tag_value;
5621
5622   if (parent_die != NULL)
5623     add_child_die (parent_die, die);
5624   else
5625     {
5626       limbo_die_node *limbo_node;
5627
5628       limbo_node = ggc_alloc_cleared_limbo_die_node ();
5629       limbo_node->die = die;
5630       limbo_node->created_for = t;
5631       limbo_node->next = limbo_die_list;
5632       limbo_die_list = limbo_node;
5633     }
5634
5635   return die;
5636 }
5637
5638 /* Return the DIE associated with the given type specifier.  */
5639
5640 static inline dw_die_ref
5641 lookup_type_die (tree type)
5642 {
5643   return TYPE_SYMTAB_DIE (type);
5644 }
5645
5646 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5647    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5648    anonymous type instead the one of the naming typedef.  */
5649
5650 static inline dw_die_ref
5651 strip_naming_typedef (tree type, dw_die_ref type_die)
5652 {
5653   if (type
5654       && TREE_CODE (type) == RECORD_TYPE
5655       && type_die
5656       && type_die->die_tag == DW_TAG_typedef
5657       && is_naming_typedef_decl (TYPE_NAME (type)))
5658     type_die = get_AT_ref (type_die, DW_AT_type);
5659   return type_die;
5660 }
5661
5662 /* Like lookup_type_die, but if type is an anonymous type named by a
5663    typedef[1], return the DIE of the anonymous type instead the one of
5664    the naming typedef.  This is because in gen_typedef_die, we did
5665    equate the anonymous struct named by the typedef with the DIE of
5666    the naming typedef. So by default, lookup_type_die on an anonymous
5667    struct yields the DIE of the naming typedef.
5668
5669    [1]: Read the comment of is_naming_typedef_decl to learn about what
5670    a naming typedef is.  */
5671
5672 static inline dw_die_ref
5673 lookup_type_die_strip_naming_typedef (tree type)
5674 {
5675   dw_die_ref die = lookup_type_die (type);
5676   return strip_naming_typedef (type, die);
5677 }
5678
5679 /* Equate a DIE to a given type specifier.  */
5680
5681 static inline void
5682 equate_type_number_to_die (tree type, dw_die_ref type_die)
5683 {
5684   TYPE_SYMTAB_DIE (type) = type_die;
5685 }
5686
5687 /* Returns a hash value for X (which really is a die_struct).  */
5688
5689 static hashval_t
5690 decl_die_table_hash (const void *x)
5691 {
5692   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5693 }
5694
5695 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5696
5697 static int
5698 decl_die_table_eq (const void *x, const void *y)
5699 {
5700   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5701 }
5702
5703 /* Return the DIE associated with a given declaration.  */
5704
5705 static inline dw_die_ref
5706 lookup_decl_die (tree decl)
5707 {
5708   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5709 }
5710
5711 /* Returns a hash value for X (which really is a var_loc_list).  */
5712
5713 static hashval_t
5714 decl_loc_table_hash (const void *x)
5715 {
5716   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5717 }
5718
5719 /* Return nonzero if decl_id of var_loc_list X is the same as
5720    UID of decl *Y.  */
5721
5722 static int
5723 decl_loc_table_eq (const void *x, const void *y)
5724 {
5725   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5726 }
5727
5728 /* Return the var_loc list associated with a given declaration.  */
5729
5730 static inline var_loc_list *
5731 lookup_decl_loc (const_tree decl)
5732 {
5733   if (!decl_loc_table)
5734     return NULL;
5735   return (var_loc_list *)
5736     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5737 }
5738
5739 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5740
5741 static hashval_t
5742 cached_dw_loc_list_table_hash (const void *x)
5743 {
5744   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5745 }
5746
5747 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5748    UID of decl *Y.  */
5749
5750 static int
5751 cached_dw_loc_list_table_eq (const void *x, const void *y)
5752 {
5753   return (((const cached_dw_loc_list *) x)->decl_id
5754           == DECL_UID ((const_tree) y));
5755 }
5756
5757 /* Equate a DIE to a particular declaration.  */
5758
5759 static void
5760 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5761 {
5762   unsigned int decl_id = DECL_UID (decl);
5763   void **slot;
5764
5765   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5766   *slot = decl_die;
5767   decl_die->decl_id = decl_id;
5768 }
5769
5770 /* Return how many bits covers PIECE EXPR_LIST.  */
5771
5772 static int
5773 decl_piece_bitsize (rtx piece)
5774 {
5775   int ret = (int) GET_MODE (piece);
5776   if (ret)
5777     return ret;
5778   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5779               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5780   return INTVAL (XEXP (XEXP (piece, 0), 0));
5781 }
5782
5783 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5784
5785 static rtx *
5786 decl_piece_varloc_ptr (rtx piece)
5787 {
5788   if ((int) GET_MODE (piece))
5789     return &XEXP (piece, 0);
5790   else
5791     return &XEXP (XEXP (piece, 0), 1);
5792 }
5793
5794 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5795    Next is the chain of following piece nodes.  */
5796
5797 static rtx
5798 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5799 {
5800   if (bitsize <= (int) MAX_MACHINE_MODE)
5801     return alloc_EXPR_LIST (bitsize, loc_note, next);
5802   else
5803     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5804                                                GEN_INT (bitsize),
5805                                                loc_note), next);
5806 }
5807
5808 /* Return rtx that should be stored into loc field for
5809    LOC_NOTE and BITPOS/BITSIZE.  */
5810
5811 static rtx
5812 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5813                       HOST_WIDE_INT bitsize)
5814 {
5815   if (bitsize != -1)
5816     {
5817       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5818       if (bitpos != 0)
5819         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5820     }
5821   return loc_note;
5822 }
5823
5824 /* This function either modifies location piece list *DEST in
5825    place (if SRC and INNER is NULL), or copies location piece list
5826    *SRC to *DEST while modifying it.  Location BITPOS is modified
5827    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5828    not copied and if needed some padding around it is added.
5829    When modifying in place, DEST should point to EXPR_LIST where
5830    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5831    to the start of the whole list and INNER points to the EXPR_LIST
5832    where earlier pieces cover PIECE_BITPOS bits.  */
5833
5834 static void
5835 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5836                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5837                    HOST_WIDE_INT bitsize, rtx loc_note)
5838 {
5839   int diff;
5840   bool copy = inner != NULL;
5841
5842   if (copy)
5843     {
5844       /* First copy all nodes preceeding the current bitpos.  */
5845       while (src != inner)
5846         {
5847           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5848                                    decl_piece_bitsize (*src), NULL_RTX);
5849           dest = &XEXP (*dest, 1);
5850           src = &XEXP (*src, 1);
5851         }
5852     }
5853   /* Add padding if needed.  */
5854   if (bitpos != piece_bitpos)
5855     {
5856       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5857                                copy ? NULL_RTX : *dest);
5858       dest = &XEXP (*dest, 1);
5859     }
5860   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5861     {
5862       gcc_assert (!copy);
5863       /* A piece with correct bitpos and bitsize already exist,
5864          just update the location for it and return.  */
5865       *decl_piece_varloc_ptr (*dest) = loc_note;
5866       return;
5867     }
5868   /* Add the piece that changed.  */
5869   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5870   dest = &XEXP (*dest, 1);
5871   /* Skip over pieces that overlap it.  */
5872   diff = bitpos - piece_bitpos + bitsize;
5873   if (!copy)
5874     src = dest;
5875   while (diff > 0 && *src)
5876     {
5877       rtx piece = *src;
5878       diff -= decl_piece_bitsize (piece);
5879       if (copy)
5880         src = &XEXP (piece, 1);
5881       else
5882         {
5883           *src = XEXP (piece, 1);
5884           free_EXPR_LIST_node (piece);
5885         }
5886     }
5887   /* Add padding if needed.  */
5888   if (diff < 0 && *src)
5889     {
5890       if (!copy)
5891         dest = src;
5892       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5893       dest = &XEXP (*dest, 1);
5894     }
5895   if (!copy)
5896     return;
5897   /* Finally copy all nodes following it.  */
5898   while (*src)
5899     {
5900       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5901                                decl_piece_bitsize (*src), NULL_RTX);
5902       dest = &XEXP (*dest, 1);
5903       src = &XEXP (*src, 1);
5904     }
5905 }
5906
5907 /* Add a variable location node to the linked list for DECL.  */
5908
5909 static struct var_loc_node *
5910 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5911 {
5912   unsigned int decl_id;
5913   var_loc_list *temp;
5914   void **slot;
5915   struct var_loc_node *loc = NULL;
5916   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5917
5918   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5919     {
5920       tree realdecl = DECL_DEBUG_EXPR (decl);
5921       if (realdecl && handled_component_p (realdecl))
5922         {
5923           HOST_WIDE_INT maxsize;
5924           tree innerdecl;
5925           innerdecl
5926             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5927           if (!DECL_P (innerdecl)
5928               || DECL_IGNORED_P (innerdecl)
5929               || TREE_STATIC (innerdecl)
5930               || bitsize <= 0
5931               || bitpos + bitsize > 256
5932               || bitsize != maxsize)
5933             return NULL;
5934           decl = innerdecl;
5935         }
5936     }
5937
5938   decl_id = DECL_UID (decl);
5939   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5940   if (*slot == NULL)
5941     {
5942       temp = ggc_alloc_cleared_var_loc_list ();
5943       temp->decl_id = decl_id;
5944       *slot = temp;
5945     }
5946   else
5947     temp = (var_loc_list *) *slot;
5948
5949   /* For PARM_DECLs try to keep around the original incoming value,
5950      even if that means we'll emit a zero-range .debug_loc entry.  */
5951   if (temp->last
5952       && temp->first == temp->last
5953       && TREE_CODE (decl) == PARM_DECL
5954       && GET_CODE (temp->first->loc) == NOTE
5955       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5956       && DECL_INCOMING_RTL (decl)
5957       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5958       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5959          == GET_CODE (DECL_INCOMING_RTL (decl))
5960       && prev_real_insn (temp->first->loc) == NULL_RTX
5961       && (bitsize != -1
5962           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5963                            NOTE_VAR_LOCATION_LOC (loc_note))
5964           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5965               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5966     {
5967       loc = ggc_alloc_cleared_var_loc_node ();
5968       temp->first->next = loc;
5969       temp->last = loc;
5970       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5971     }
5972   else if (temp->last)
5973     {
5974       struct var_loc_node *last = temp->last, *unused = NULL;
5975       rtx *piece_loc = NULL, last_loc_note;
5976       int piece_bitpos = 0;
5977       if (last->next)
5978         {
5979           last = last->next;
5980           gcc_assert (last->next == NULL);
5981         }
5982       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5983         {
5984           piece_loc = &last->loc;
5985           do
5986             {
5987               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5988               if (piece_bitpos + cur_bitsize > bitpos)
5989                 break;
5990               piece_bitpos += cur_bitsize;
5991               piece_loc = &XEXP (*piece_loc, 1);
5992             }
5993           while (*piece_loc);
5994         }
5995       /* TEMP->LAST here is either pointer to the last but one or
5996          last element in the chained list, LAST is pointer to the
5997          last element.  */
5998       if (label && strcmp (last->label, label) == 0)
5999         {
6000           /* For SRA optimized variables if there weren't any real
6001              insns since last note, just modify the last node.  */
6002           if (piece_loc != NULL)
6003             {
6004               adjust_piece_list (piece_loc, NULL, NULL,
6005                                  bitpos, piece_bitpos, bitsize, loc_note);
6006               return NULL;
6007             }
6008           /* If the last note doesn't cover any instructions, remove it.  */
6009           if (temp->last != last)
6010             {
6011               temp->last->next = NULL;
6012               unused = last;
6013               last = temp->last;
6014               gcc_assert (strcmp (last->label, label) != 0);
6015             }
6016           else
6017             {
6018               gcc_assert (temp->first == temp->last
6019                           || (temp->first->next == temp->last
6020                               && TREE_CODE (decl) == PARM_DECL));
6021               memset (temp->last, '\0', sizeof (*temp->last));
6022               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6023               return temp->last;
6024             }
6025         }
6026       if (bitsize == -1 && NOTE_P (last->loc))
6027         last_loc_note = last->loc;
6028       else if (piece_loc != NULL
6029                && *piece_loc != NULL_RTX
6030                && piece_bitpos == bitpos
6031                && decl_piece_bitsize (*piece_loc) == bitsize)
6032         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6033       else
6034         last_loc_note = NULL_RTX;
6035       /* If the current location is the same as the end of the list,
6036          and either both or neither of the locations is uninitialized,
6037          we have nothing to do.  */
6038       if (last_loc_note == NULL_RTX
6039           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6040                             NOTE_VAR_LOCATION_LOC (loc_note)))
6041           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6042                != NOTE_VAR_LOCATION_STATUS (loc_note))
6043               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6044                    == VAR_INIT_STATUS_UNINITIALIZED)
6045                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
6046                       == VAR_INIT_STATUS_UNINITIALIZED))))
6047         {
6048           /* Add LOC to the end of list and update LAST.  If the last
6049              element of the list has been removed above, reuse its
6050              memory for the new node, otherwise allocate a new one.  */
6051           if (unused)
6052             {
6053               loc = unused;
6054               memset (loc, '\0', sizeof (*loc));
6055             }
6056           else
6057             loc = ggc_alloc_cleared_var_loc_node ();
6058           if (bitsize == -1 || piece_loc == NULL)
6059             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6060           else
6061             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6062                                bitpos, piece_bitpos, bitsize, loc_note);
6063           last->next = loc;
6064           /* Ensure TEMP->LAST will point either to the new last but one
6065              element of the chain, or to the last element in it.  */
6066           if (last != temp->last)
6067             temp->last = last;
6068         }
6069       else if (unused)
6070         ggc_free (unused);
6071     }
6072   else
6073     {
6074       loc = ggc_alloc_cleared_var_loc_node ();
6075       temp->first = loc;
6076       temp->last = loc;
6077       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6078     }
6079   return loc;
6080 }
6081 \f
6082 /* Keep track of the number of spaces used to indent the
6083    output of the debugging routines that print the structure of
6084    the DIE internal representation.  */
6085 static int print_indent;
6086
6087 /* Indent the line the number of spaces given by print_indent.  */
6088
6089 static inline void
6090 print_spaces (FILE *outfile)
6091 {
6092   fprintf (outfile, "%*s", print_indent, "");
6093 }
6094
6095 /* Print a type signature in hex.  */
6096
6097 static inline void
6098 print_signature (FILE *outfile, char *sig)
6099 {
6100   int i;
6101
6102   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6103     fprintf (outfile, "%02x", sig[i] & 0xff);
6104 }
6105
6106 /* Print the information associated with a given DIE, and its children.
6107    This routine is a debugging aid only.  */
6108
6109 static void
6110 print_die (dw_die_ref die, FILE *outfile)
6111 {
6112   dw_attr_ref a;
6113   dw_die_ref c;
6114   unsigned ix;
6115
6116   print_spaces (outfile);
6117   fprintf (outfile, "DIE %4ld: %s (%p)\n",
6118            die->die_offset, dwarf_tag_name (die->die_tag),
6119            (void*) die);
6120   print_spaces (outfile);
6121   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6122   fprintf (outfile, " offset: %ld", die->die_offset);
6123   fprintf (outfile, " mark: %d\n", die->die_mark);
6124
6125   if (use_debug_types && die->die_id.die_type_node)
6126     {
6127       print_spaces (outfile);
6128       fprintf (outfile, "  signature: ");
6129       print_signature (outfile, die->die_id.die_type_node->signature);
6130       fprintf (outfile, "\n");
6131     }
6132
6133   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6134     {
6135       print_spaces (outfile);
6136       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6137
6138       switch (AT_class (a))
6139         {
6140         case dw_val_class_addr:
6141           fprintf (outfile, "address");
6142           break;
6143         case dw_val_class_offset:
6144           fprintf (outfile, "offset");
6145           break;
6146         case dw_val_class_loc:
6147           fprintf (outfile, "location descriptor");
6148           break;
6149         case dw_val_class_loc_list:
6150           fprintf (outfile, "location list -> label:%s",
6151                    AT_loc_list (a)->ll_symbol);
6152           break;
6153         case dw_val_class_range_list:
6154           fprintf (outfile, "range list");
6155           break;
6156         case dw_val_class_const:
6157           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6158           break;
6159         case dw_val_class_unsigned_const:
6160           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6161           break;
6162         case dw_val_class_const_double:
6163           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
6164                             HOST_WIDE_INT_PRINT_UNSIGNED")",
6165                    a->dw_attr_val.v.val_double.high,
6166                    a->dw_attr_val.v.val_double.low);
6167           break;
6168         case dw_val_class_vec:
6169           fprintf (outfile, "floating-point or vector constant");
6170           break;
6171         case dw_val_class_flag:
6172           fprintf (outfile, "%u", AT_flag (a));
6173           break;
6174         case dw_val_class_die_ref:
6175           if (AT_ref (a) != NULL)
6176             {
6177               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
6178                 {
6179                   fprintf (outfile, "die -> signature: ");
6180                   print_signature (outfile,
6181                                    AT_ref (a)->die_id.die_type_node->signature);
6182                 }
6183               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
6184                 fprintf (outfile, "die -> label: %s",
6185                          AT_ref (a)->die_id.die_symbol);
6186               else
6187                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6188               fprintf (outfile, " (%p)", (void *) AT_ref (a));
6189             }
6190           else
6191             fprintf (outfile, "die -> <null>");
6192           break;
6193         case dw_val_class_vms_delta:
6194           fprintf (outfile, "delta: @slotcount(%s-%s)",
6195                    AT_vms_delta2 (a), AT_vms_delta1 (a));
6196           break;
6197         case dw_val_class_lbl_id:
6198         case dw_val_class_lineptr:
6199         case dw_val_class_macptr:
6200           fprintf (outfile, "label: %s", AT_lbl (a));
6201           break;
6202         case dw_val_class_str:
6203           if (AT_string (a) != NULL)
6204             fprintf (outfile, "\"%s\"", AT_string (a));
6205           else
6206             fprintf (outfile, "<null>");
6207           break;
6208         case dw_val_class_file:
6209           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6210                    AT_file (a)->emitted_number);
6211           break;
6212         case dw_val_class_data8:
6213           {
6214             int i;
6215
6216             for (i = 0; i < 8; i++)
6217               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
6218             break;
6219           }
6220         default:
6221           break;
6222         }
6223
6224       fprintf (outfile, "\n");
6225     }
6226
6227   if (die->die_child != NULL)
6228     {
6229       print_indent += 4;
6230       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6231       print_indent -= 4;
6232     }
6233   if (print_indent == 0)
6234     fprintf (outfile, "\n");
6235 }
6236
6237 /* Print the information collected for a given DIE.  */
6238
6239 DEBUG_FUNCTION void
6240 debug_dwarf_die (dw_die_ref die)
6241 {
6242   print_die (die, stderr);
6243 }
6244
6245 /* Print all DWARF information collected for the compilation unit.
6246    This routine is a debugging aid only.  */
6247
6248 DEBUG_FUNCTION void
6249 debug_dwarf (void)
6250 {
6251   print_indent = 0;
6252   print_die (comp_unit_die (), stderr);
6253 }
6254 \f
6255 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6256    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6257    DIE that marks the start of the DIEs for this include file.  */
6258
6259 static dw_die_ref
6260 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6261 {
6262   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6263   dw_die_ref new_unit = gen_compile_unit_die (filename);
6264
6265   new_unit->die_sib = old_unit;
6266   return new_unit;
6267 }
6268
6269 /* Close an include-file CU and reopen the enclosing one.  */
6270
6271 static dw_die_ref
6272 pop_compile_unit (dw_die_ref old_unit)
6273 {
6274   dw_die_ref new_unit = old_unit->die_sib;
6275
6276   old_unit->die_sib = NULL;
6277   return new_unit;
6278 }
6279
6280 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6281 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6282
6283 /* Calculate the checksum of a location expression.  */
6284
6285 static inline void
6286 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6287 {
6288   int tem;
6289
6290   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6291   CHECKSUM (tem);
6292   CHECKSUM (loc->dw_loc_oprnd1);
6293   CHECKSUM (loc->dw_loc_oprnd2);
6294 }
6295
6296 /* Calculate the checksum of an attribute.  */
6297
6298 static void
6299 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6300 {
6301   dw_loc_descr_ref loc;
6302   rtx r;
6303
6304   CHECKSUM (at->dw_attr);
6305
6306   /* We don't care that this was compiled with a different compiler
6307      snapshot; if the output is the same, that's what matters.  */
6308   if (at->dw_attr == DW_AT_producer)
6309     return;
6310
6311   switch (AT_class (at))
6312     {
6313     case dw_val_class_const:
6314       CHECKSUM (at->dw_attr_val.v.val_int);
6315       break;
6316     case dw_val_class_unsigned_const:
6317       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6318       break;
6319     case dw_val_class_const_double:
6320       CHECKSUM (at->dw_attr_val.v.val_double);
6321       break;
6322     case dw_val_class_vec:
6323       CHECKSUM (at->dw_attr_val.v.val_vec);
6324       break;
6325     case dw_val_class_flag:
6326       CHECKSUM (at->dw_attr_val.v.val_flag);
6327       break;
6328     case dw_val_class_str:
6329       CHECKSUM_STRING (AT_string (at));
6330       break;
6331
6332     case dw_val_class_addr:
6333       r = AT_addr (at);
6334       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6335       CHECKSUM_STRING (XSTR (r, 0));
6336       break;
6337
6338     case dw_val_class_offset:
6339       CHECKSUM (at->dw_attr_val.v.val_offset);
6340       break;
6341
6342     case dw_val_class_loc:
6343       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6344         loc_checksum (loc, ctx);
6345       break;
6346
6347     case dw_val_class_die_ref:
6348       die_checksum (AT_ref (at), ctx, mark);
6349       break;
6350
6351     case dw_val_class_fde_ref:
6352     case dw_val_class_vms_delta:
6353     case dw_val_class_lbl_id:
6354     case dw_val_class_lineptr:
6355     case dw_val_class_macptr:
6356       break;
6357
6358     case dw_val_class_file:
6359       CHECKSUM_STRING (AT_file (at)->filename);
6360       break;
6361
6362     case dw_val_class_data8:
6363       CHECKSUM (at->dw_attr_val.v.val_data8);
6364       break;
6365
6366     default:
6367       break;
6368     }
6369 }
6370
6371 /* Calculate the checksum of a DIE.  */
6372
6373 static void
6374 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6375 {
6376   dw_die_ref c;
6377   dw_attr_ref a;
6378   unsigned ix;
6379
6380   /* To avoid infinite recursion.  */
6381   if (die->die_mark)
6382     {
6383       CHECKSUM (die->die_mark);
6384       return;
6385     }
6386   die->die_mark = ++(*mark);
6387
6388   CHECKSUM (die->die_tag);
6389
6390   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6391     attr_checksum (a, ctx, mark);
6392
6393   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6394 }
6395
6396 #undef CHECKSUM
6397 #undef CHECKSUM_STRING
6398
6399 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
6400 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6401 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6402 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6403 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6404 #define CHECKSUM_ATTR(FOO) \
6405   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6406
6407 /* Calculate the checksum of a number in signed LEB128 format.  */
6408
6409 static void
6410 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6411 {
6412   unsigned char byte;
6413   bool more;
6414
6415   while (1)
6416     {
6417       byte = (value & 0x7f);
6418       value >>= 7;
6419       more = !((value == 0 && (byte & 0x40) == 0)
6420                 || (value == -1 && (byte & 0x40) != 0));
6421       if (more)
6422         byte |= 0x80;
6423       CHECKSUM (byte);
6424       if (!more)
6425         break;
6426     }
6427 }
6428
6429 /* Calculate the checksum of a number in unsigned LEB128 format.  */
6430
6431 static void
6432 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6433 {
6434   while (1)
6435     {
6436       unsigned char byte = (value & 0x7f);
6437       value >>= 7;
6438       if (value != 0)
6439         /* More bytes to follow.  */
6440         byte |= 0x80;
6441       CHECKSUM (byte);
6442       if (value == 0)
6443         break;
6444     }
6445 }
6446
6447 /* Checksum the context of the DIE.  This adds the names of any
6448    surrounding namespaces or structures to the checksum.  */
6449
6450 static void
6451 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6452 {
6453   const char *name;
6454   dw_die_ref spec;
6455   int tag = die->die_tag;
6456
6457   if (tag != DW_TAG_namespace
6458       && tag != DW_TAG_structure_type
6459       && tag != DW_TAG_class_type)
6460     return;
6461
6462   name = get_AT_string (die, DW_AT_name);
6463
6464   spec = get_AT_ref (die, DW_AT_specification);
6465   if (spec != NULL)
6466     die = spec;
6467
6468   if (die->die_parent != NULL)
6469     checksum_die_context (die->die_parent, ctx);
6470
6471   CHECKSUM_ULEB128 ('C');
6472   CHECKSUM_ULEB128 (tag);
6473   if (name != NULL)
6474     CHECKSUM_STRING (name);
6475 }
6476
6477 /* Calculate the checksum of a location expression.  */
6478
6479 static inline void
6480 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6481 {
6482   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6483      were emitted as a DW_FORM_sdata instead of a location expression.  */
6484   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6485     {
6486       CHECKSUM_ULEB128 (DW_FORM_sdata);
6487       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6488       return;
6489     }
6490
6491   /* Otherwise, just checksum the raw location expression.  */
6492   while (loc != NULL)
6493     {
6494       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6495       CHECKSUM (loc->dw_loc_oprnd1);
6496       CHECKSUM (loc->dw_loc_oprnd2);
6497       loc = loc->dw_loc_next;
6498     }
6499 }
6500
6501 /* Calculate the checksum of an attribute.  */
6502
6503 static void
6504 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6505                        struct md5_ctx *ctx, int *mark)
6506 {
6507   dw_loc_descr_ref loc;
6508   rtx r;
6509
6510   if (AT_class (at) == dw_val_class_die_ref)
6511     {
6512       dw_die_ref target_die = AT_ref (at);
6513
6514       /* For pointer and reference types, we checksum only the (qualified)
6515          name of the target type (if there is a name).  For friend entries,
6516          we checksum only the (qualified) name of the target type or function.
6517          This allows the checksum to remain the same whether the target type
6518          is complete or not.  */
6519       if ((at->dw_attr == DW_AT_type
6520            && (tag == DW_TAG_pointer_type
6521                || tag == DW_TAG_reference_type
6522                || tag == DW_TAG_rvalue_reference_type
6523                || tag == DW_TAG_ptr_to_member_type))
6524           || (at->dw_attr == DW_AT_friend
6525               && tag == DW_TAG_friend))
6526         {
6527           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6528
6529           if (name_attr != NULL)
6530             {
6531               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6532
6533               if (decl == NULL)
6534                 decl = target_die;
6535               CHECKSUM_ULEB128 ('N');
6536               CHECKSUM_ULEB128 (at->dw_attr);
6537               if (decl->die_parent != NULL)
6538                 checksum_die_context (decl->die_parent, ctx);
6539               CHECKSUM_ULEB128 ('E');
6540               CHECKSUM_STRING (AT_string (name_attr));
6541               return;
6542             }
6543         }
6544
6545       /* For all other references to another DIE, we check to see if the
6546          target DIE has already been visited.  If it has, we emit a
6547          backward reference; if not, we descend recursively.  */
6548       if (target_die->die_mark > 0)
6549         {
6550           CHECKSUM_ULEB128 ('R');
6551           CHECKSUM_ULEB128 (at->dw_attr);
6552           CHECKSUM_ULEB128 (target_die->die_mark);
6553         }
6554       else
6555         {
6556           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6557
6558           if (decl == NULL)
6559             decl = target_die;
6560           target_die->die_mark = ++(*mark);
6561           CHECKSUM_ULEB128 ('T');
6562           CHECKSUM_ULEB128 (at->dw_attr);
6563           if (decl->die_parent != NULL)
6564             checksum_die_context (decl->die_parent, ctx);
6565           die_checksum_ordered (target_die, ctx, mark);
6566         }
6567       return;
6568     }
6569
6570   CHECKSUM_ULEB128 ('A');
6571   CHECKSUM_ULEB128 (at->dw_attr);
6572
6573   switch (AT_class (at))
6574     {
6575     case dw_val_class_const:
6576       CHECKSUM_ULEB128 (DW_FORM_sdata);
6577       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6578       break;
6579
6580     case dw_val_class_unsigned_const:
6581       CHECKSUM_ULEB128 (DW_FORM_sdata);
6582       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6583       break;
6584
6585     case dw_val_class_const_double:
6586       CHECKSUM_ULEB128 (DW_FORM_block);
6587       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6588       CHECKSUM (at->dw_attr_val.v.val_double);
6589       break;
6590
6591     case dw_val_class_vec:
6592       CHECKSUM_ULEB128 (DW_FORM_block);
6593       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
6594       CHECKSUM (at->dw_attr_val.v.val_vec);
6595       break;
6596
6597     case dw_val_class_flag:
6598       CHECKSUM_ULEB128 (DW_FORM_flag);
6599       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6600       break;
6601
6602     case dw_val_class_str:
6603       CHECKSUM_ULEB128 (DW_FORM_string);
6604       CHECKSUM_STRING (AT_string (at));
6605       break;
6606
6607     case dw_val_class_addr:
6608       r = AT_addr (at);
6609       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6610       CHECKSUM_ULEB128 (DW_FORM_string);
6611       CHECKSUM_STRING (XSTR (r, 0));
6612       break;
6613
6614     case dw_val_class_offset:
6615       CHECKSUM_ULEB128 (DW_FORM_sdata);
6616       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6617       break;
6618
6619     case dw_val_class_loc:
6620       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6621         loc_checksum_ordered (loc, ctx);
6622       break;
6623
6624     case dw_val_class_fde_ref:
6625     case dw_val_class_lbl_id:
6626     case dw_val_class_lineptr:
6627     case dw_val_class_macptr:
6628       break;
6629
6630     case dw_val_class_file:
6631       CHECKSUM_ULEB128 (DW_FORM_string);
6632       CHECKSUM_STRING (AT_file (at)->filename);
6633       break;
6634
6635     case dw_val_class_data8:
6636       CHECKSUM (at->dw_attr_val.v.val_data8);
6637       break;
6638
6639     default:
6640       break;
6641     }
6642 }
6643
6644 struct checksum_attributes
6645 {
6646   dw_attr_ref at_name;
6647   dw_attr_ref at_type;
6648   dw_attr_ref at_friend;
6649   dw_attr_ref at_accessibility;
6650   dw_attr_ref at_address_class;
6651   dw_attr_ref at_allocated;
6652   dw_attr_ref at_artificial;
6653   dw_attr_ref at_associated;
6654   dw_attr_ref at_binary_scale;
6655   dw_attr_ref at_bit_offset;
6656   dw_attr_ref at_bit_size;
6657   dw_attr_ref at_bit_stride;
6658   dw_attr_ref at_byte_size;
6659   dw_attr_ref at_byte_stride;
6660   dw_attr_ref at_const_value;
6661   dw_attr_ref at_containing_type;
6662   dw_attr_ref at_count;
6663   dw_attr_ref at_data_location;
6664   dw_attr_ref at_data_member_location;
6665   dw_attr_ref at_decimal_scale;
6666   dw_attr_ref at_decimal_sign;
6667   dw_attr_ref at_default_value;
6668   dw_attr_ref at_digit_count;
6669   dw_attr_ref at_discr;
6670   dw_attr_ref at_discr_list;
6671   dw_attr_ref at_discr_value;
6672   dw_attr_ref at_encoding;
6673   dw_attr_ref at_endianity;
6674   dw_attr_ref at_explicit;
6675   dw_attr_ref at_is_optional;
6676   dw_attr_ref at_location;
6677   dw_attr_ref at_lower_bound;
6678   dw_attr_ref at_mutable;
6679   dw_attr_ref at_ordering;
6680   dw_attr_ref at_picture_string;
6681   dw_attr_ref at_prototyped;
6682   dw_attr_ref at_small;
6683   dw_attr_ref at_segment;
6684   dw_attr_ref at_string_length;
6685   dw_attr_ref at_threads_scaled;
6686   dw_attr_ref at_upper_bound;
6687   dw_attr_ref at_use_location;
6688   dw_attr_ref at_use_UTF8;
6689   dw_attr_ref at_variable_parameter;
6690   dw_attr_ref at_virtuality;
6691   dw_attr_ref at_visibility;
6692   dw_attr_ref at_vtable_elem_location;
6693 };
6694
6695 /* Collect the attributes that we will want to use for the checksum.  */
6696
6697 static void
6698 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6699 {
6700   dw_attr_ref a;
6701   unsigned ix;
6702
6703   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6704     {
6705       switch (a->dw_attr)
6706         {
6707         case DW_AT_name:
6708           attrs->at_name = a;
6709           break;
6710         case DW_AT_type:
6711           attrs->at_type = a;
6712           break;
6713         case DW_AT_friend:
6714           attrs->at_friend = a;
6715           break;
6716         case DW_AT_accessibility:
6717           attrs->at_accessibility = a;
6718           break;
6719         case DW_AT_address_class:
6720           attrs->at_address_class = a;
6721           break;
6722         case DW_AT_allocated:
6723           attrs->at_allocated = a;
6724           break;
6725         case DW_AT_artificial:
6726           attrs->at_artificial = a;
6727           break;
6728         case DW_AT_associated:
6729           attrs->at_associated = a;
6730           break;
6731         case DW_AT_binary_scale:
6732           attrs->at_binary_scale = a;
6733           break;
6734         case DW_AT_bit_offset:
6735           attrs->at_bit_offset = a;
6736           break;
6737         case DW_AT_bit_size:
6738           attrs->at_bit_size = a;
6739           break;
6740         case DW_AT_bit_stride:
6741           attrs->at_bit_stride = a;
6742           break;
6743         case DW_AT_byte_size:
6744           attrs->at_byte_size = a;
6745           break;
6746         case DW_AT_byte_stride:
6747           attrs->at_byte_stride = a;
6748           break;
6749         case DW_AT_const_value:
6750           attrs->at_const_value = a;
6751           break;
6752         case DW_AT_containing_type:
6753           attrs->at_containing_type = a;
6754           break;
6755         case DW_AT_count:
6756           attrs->at_count = a;
6757           break;
6758         case DW_AT_data_location:
6759           attrs->at_data_location = a;
6760           break;
6761         case DW_AT_data_member_location:
6762           attrs->at_data_member_location = a;
6763           break;
6764         case DW_AT_decimal_scale:
6765           attrs->at_decimal_scale = a;
6766           break;
6767         case DW_AT_decimal_sign:
6768           attrs->at_decimal_sign = a;
6769           break;
6770         case DW_AT_default_value:
6771           attrs->at_default_value = a;
6772           break;
6773         case DW_AT_digit_count:
6774           attrs->at_digit_count = a;
6775           break;
6776         case DW_AT_discr:
6777           attrs->at_discr = a;
6778           break;
6779         case DW_AT_discr_list:
6780           attrs->at_discr_list = a;
6781           break;
6782         case DW_AT_discr_value:
6783           attrs->at_discr_value = a;
6784           break;
6785         case DW_AT_encoding:
6786           attrs->at_encoding = a;
6787           break;
6788         case DW_AT_endianity:
6789           attrs->at_endianity = a;
6790           break;
6791         case DW_AT_explicit:
6792           attrs->at_explicit = a;
6793           break;
6794         case DW_AT_is_optional:
6795           attrs->at_is_optional = a;
6796           break;
6797         case DW_AT_location:
6798           attrs->at_location = a;
6799           break;
6800         case DW_AT_lower_bound:
6801           attrs->at_lower_bound = a;
6802           break;
6803         case DW_AT_mutable:
6804           attrs->at_mutable = a;
6805           break;
6806         case DW_AT_ordering:
6807           attrs->at_ordering = a;
6808           break;
6809         case DW_AT_picture_string:
6810           attrs->at_picture_string = a;
6811           break;
6812         case DW_AT_prototyped:
6813           attrs->at_prototyped = a;
6814           break;
6815         case DW_AT_small:
6816           attrs->at_small = a;
6817           break;
6818         case DW_AT_segment:
6819           attrs->at_segment = a;
6820           break;
6821         case DW_AT_string_length:
6822           attrs->at_string_length = a;
6823           break;
6824         case DW_AT_threads_scaled:
6825           attrs->at_threads_scaled = a;
6826           break;
6827         case DW_AT_upper_bound:
6828           attrs->at_upper_bound = a;
6829           break;
6830         case DW_AT_use_location:
6831           attrs->at_use_location = a;
6832           break;
6833         case DW_AT_use_UTF8:
6834           attrs->at_use_UTF8 = a;
6835           break;
6836         case DW_AT_variable_parameter:
6837           attrs->at_variable_parameter = a;
6838           break;
6839         case DW_AT_virtuality:
6840           attrs->at_virtuality = a;
6841           break;
6842         case DW_AT_visibility:
6843           attrs->at_visibility = a;
6844           break;
6845         case DW_AT_vtable_elem_location:
6846           attrs->at_vtable_elem_location = a;
6847           break;
6848         default:
6849           break;
6850         }
6851     }
6852 }
6853
6854 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6855
6856 static void
6857 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6858 {
6859   dw_die_ref c;
6860   dw_die_ref decl;
6861   struct checksum_attributes attrs;
6862
6863   CHECKSUM_ULEB128 ('D');
6864   CHECKSUM_ULEB128 (die->die_tag);
6865
6866   memset (&attrs, 0, sizeof (attrs));
6867
6868   decl = get_AT_ref (die, DW_AT_specification);
6869   if (decl != NULL)
6870     collect_checksum_attributes (&attrs, decl);
6871   collect_checksum_attributes (&attrs, die);
6872
6873   CHECKSUM_ATTR (attrs.at_name);
6874   CHECKSUM_ATTR (attrs.at_accessibility);
6875   CHECKSUM_ATTR (attrs.at_address_class);
6876   CHECKSUM_ATTR (attrs.at_allocated);
6877   CHECKSUM_ATTR (attrs.at_artificial);
6878   CHECKSUM_ATTR (attrs.at_associated);
6879   CHECKSUM_ATTR (attrs.at_binary_scale);
6880   CHECKSUM_ATTR (attrs.at_bit_offset);
6881   CHECKSUM_ATTR (attrs.at_bit_size);
6882   CHECKSUM_ATTR (attrs.at_bit_stride);
6883   CHECKSUM_ATTR (attrs.at_byte_size);
6884   CHECKSUM_ATTR (attrs.at_byte_stride);
6885   CHECKSUM_ATTR (attrs.at_const_value);
6886   CHECKSUM_ATTR (attrs.at_containing_type);
6887   CHECKSUM_ATTR (attrs.at_count);
6888   CHECKSUM_ATTR (attrs.at_data_location);
6889   CHECKSUM_ATTR (attrs.at_data_member_location);
6890   CHECKSUM_ATTR (attrs.at_decimal_scale);
6891   CHECKSUM_ATTR (attrs.at_decimal_sign);
6892   CHECKSUM_ATTR (attrs.at_default_value);
6893   CHECKSUM_ATTR (attrs.at_digit_count);
6894   CHECKSUM_ATTR (attrs.at_discr);
6895   CHECKSUM_ATTR (attrs.at_discr_list);
6896   CHECKSUM_ATTR (attrs.at_discr_value);
6897   CHECKSUM_ATTR (attrs.at_encoding);
6898   CHECKSUM_ATTR (attrs.at_endianity);
6899   CHECKSUM_ATTR (attrs.at_explicit);
6900   CHECKSUM_ATTR (attrs.at_is_optional);
6901   CHECKSUM_ATTR (attrs.at_location);
6902   CHECKSUM_ATTR (attrs.at_lower_bound);
6903   CHECKSUM_ATTR (attrs.at_mutable);
6904   CHECKSUM_ATTR (attrs.at_ordering);
6905   CHECKSUM_ATTR (attrs.at_picture_string);
6906   CHECKSUM_ATTR (attrs.at_prototyped);
6907   CHECKSUM_ATTR (attrs.at_small);
6908   CHECKSUM_ATTR (attrs.at_segment);
6909   CHECKSUM_ATTR (attrs.at_string_length);
6910   CHECKSUM_ATTR (attrs.at_threads_scaled);
6911   CHECKSUM_ATTR (attrs.at_upper_bound);
6912   CHECKSUM_ATTR (attrs.at_use_location);
6913   CHECKSUM_ATTR (attrs.at_use_UTF8);
6914   CHECKSUM_ATTR (attrs.at_variable_parameter);
6915   CHECKSUM_ATTR (attrs.at_virtuality);
6916   CHECKSUM_ATTR (attrs.at_visibility);
6917   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6918   CHECKSUM_ATTR (attrs.at_type);
6919   CHECKSUM_ATTR (attrs.at_friend);
6920
6921   /* Checksum the child DIEs, except for nested types and member functions.  */
6922   c = die->die_child;
6923   if (c) do {
6924     dw_attr_ref name_attr;
6925
6926     c = c->die_sib;
6927     name_attr = get_AT (c, DW_AT_name);
6928     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6929         && name_attr != NULL)
6930       {
6931         CHECKSUM_ULEB128 ('S');
6932         CHECKSUM_ULEB128 (c->die_tag);
6933         CHECKSUM_STRING (AT_string (name_attr));
6934       }
6935     else
6936       {
6937         /* Mark this DIE so it gets processed when unmarking.  */
6938         if (c->die_mark == 0)
6939           c->die_mark = -1;
6940         die_checksum_ordered (c, ctx, mark);
6941       }
6942   } while (c != die->die_child);
6943
6944   CHECKSUM_ULEB128 (0);
6945 }
6946
6947 #undef CHECKSUM
6948 #undef CHECKSUM_STRING
6949 #undef CHECKSUM_ATTR
6950 #undef CHECKSUM_LEB128
6951 #undef CHECKSUM_ULEB128
6952
6953 /* Generate the type signature for DIE.  This is computed by generating an
6954    MD5 checksum over the DIE's tag, its relevant attributes, and its
6955    children.  Attributes that are references to other DIEs are processed
6956    by recursion, using the MARK field to prevent infinite recursion.
6957    If the DIE is nested inside a namespace or another type, we also
6958    need to include that context in the signature.  The lower 64 bits
6959    of the resulting MD5 checksum comprise the signature.  */
6960
6961 static void
6962 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6963 {
6964   int mark;
6965   const char *name;
6966   unsigned char checksum[16];
6967   struct md5_ctx ctx;
6968   dw_die_ref decl;
6969
6970   name = get_AT_string (die, DW_AT_name);
6971   decl = get_AT_ref (die, DW_AT_specification);
6972
6973   /* First, compute a signature for just the type name (and its surrounding
6974      context, if any.  This is stored in the type unit DIE for link-time
6975      ODR (one-definition rule) checking.  */
6976
6977   if (is_cxx() && name != NULL)
6978     {
6979       md5_init_ctx (&ctx);
6980
6981       /* Checksum the names of surrounding namespaces and structures.  */
6982       if (decl != NULL && decl->die_parent != NULL)
6983         checksum_die_context (decl->die_parent, &ctx);
6984
6985       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6986       md5_process_bytes (name, strlen (name) + 1, &ctx);
6987       md5_finish_ctx (&ctx, checksum);
6988
6989       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6990     }
6991
6992   /* Next, compute the complete type signature.  */
6993
6994   md5_init_ctx (&ctx);
6995   mark = 1;
6996   die->die_mark = mark;
6997
6998   /* Checksum the names of surrounding namespaces and structures.  */
6999   if (decl != NULL && decl->die_parent != NULL)
7000     checksum_die_context (decl->die_parent, &ctx);
7001
7002   /* Checksum the DIE and its children.  */
7003   die_checksum_ordered (die, &ctx, &mark);
7004   unmark_all_dies (die);
7005   md5_finish_ctx (&ctx, checksum);
7006
7007   /* Store the signature in the type node and link the type DIE and the
7008      type node together.  */
7009   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7010           DWARF_TYPE_SIGNATURE_SIZE);
7011   die->die_id.die_type_node = type_node;
7012   type_node->type_die = die;
7013
7014   /* If the DIE is a specification, link its declaration to the type node
7015      as well.  */
7016   if (decl != NULL)
7017     decl->die_id.die_type_node = type_node;
7018 }
7019
7020 /* Do the location expressions look same?  */
7021 static inline int
7022 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7023 {
7024   return loc1->dw_loc_opc == loc2->dw_loc_opc
7025          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7026          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7027 }
7028
7029 /* Do the values look the same?  */
7030 static int
7031 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7032 {
7033   dw_loc_descr_ref loc1, loc2;
7034   rtx r1, r2;
7035
7036   if (v1->val_class != v2->val_class)
7037     return 0;
7038
7039   switch (v1->val_class)
7040     {
7041     case dw_val_class_const:
7042       return v1->v.val_int == v2->v.val_int;
7043     case dw_val_class_unsigned_const:
7044       return v1->v.val_unsigned == v2->v.val_unsigned;
7045     case dw_val_class_const_double:
7046       return v1->v.val_double.high == v2->v.val_double.high
7047              && v1->v.val_double.low == v2->v.val_double.low;
7048     case dw_val_class_vec:
7049       if (v1->v.val_vec.length != v2->v.val_vec.length
7050           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7051         return 0;
7052       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7053                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7054         return 0;
7055       return 1;
7056     case dw_val_class_flag:
7057       return v1->v.val_flag == v2->v.val_flag;
7058     case dw_val_class_str:
7059       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7060
7061     case dw_val_class_addr:
7062       r1 = v1->v.val_addr;
7063       r2 = v2->v.val_addr;
7064       if (GET_CODE (r1) != GET_CODE (r2))
7065         return 0;
7066       return !rtx_equal_p (r1, r2);
7067
7068     case dw_val_class_offset:
7069       return v1->v.val_offset == v2->v.val_offset;
7070
7071     case dw_val_class_loc:
7072       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7073            loc1 && loc2;
7074            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7075         if (!same_loc_p (loc1, loc2, mark))
7076           return 0;
7077       return !loc1 && !loc2;
7078
7079     case dw_val_class_die_ref:
7080       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7081
7082     case dw_val_class_fde_ref:
7083     case dw_val_class_vms_delta:
7084     case dw_val_class_lbl_id:
7085     case dw_val_class_lineptr:
7086     case dw_val_class_macptr:
7087       return 1;
7088
7089     case dw_val_class_file:
7090       return v1->v.val_file == v2->v.val_file;
7091
7092     case dw_val_class_data8:
7093       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7094
7095     default:
7096       return 1;
7097     }
7098 }
7099
7100 /* Do the attributes look the same?  */
7101
7102 static int
7103 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7104 {
7105   if (at1->dw_attr != at2->dw_attr)
7106     return 0;
7107
7108   /* We don't care that this was compiled with a different compiler
7109      snapshot; if the output is the same, that's what matters. */
7110   if (at1->dw_attr == DW_AT_producer)
7111     return 1;
7112
7113   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7114 }
7115
7116 /* Do the dies look the same?  */
7117
7118 static int
7119 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7120 {
7121   dw_die_ref c1, c2;
7122   dw_attr_ref a1;
7123   unsigned ix;
7124
7125   /* To avoid infinite recursion.  */
7126   if (die1->die_mark)
7127     return die1->die_mark == die2->die_mark;
7128   die1->die_mark = die2->die_mark = ++(*mark);
7129
7130   if (die1->die_tag != die2->die_tag)
7131     return 0;
7132
7133   if (VEC_length (dw_attr_node, die1->die_attr)
7134       != VEC_length (dw_attr_node, die2->die_attr))
7135     return 0;
7136
7137   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
7138     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7139       return 0;
7140
7141   c1 = die1->die_child;
7142   c2 = die2->die_child;
7143   if (! c1)
7144     {
7145       if (c2)
7146         return 0;
7147     }
7148   else
7149     for (;;)
7150       {
7151         if (!same_die_p (c1, c2, mark))
7152           return 0;
7153         c1 = c1->die_sib;
7154         c2 = c2->die_sib;
7155         if (c1 == die1->die_child)
7156           {
7157             if (c2 == die2->die_child)
7158               break;
7159             else
7160               return 0;
7161           }
7162     }
7163
7164   return 1;
7165 }
7166
7167 /* Do the dies look the same?  Wrapper around same_die_p.  */
7168
7169 static int
7170 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7171 {
7172   int mark = 0;
7173   int ret = same_die_p (die1, die2, &mark);
7174
7175   unmark_all_dies (die1);
7176   unmark_all_dies (die2);
7177
7178   return ret;
7179 }
7180
7181 /* The prefix to attach to symbols on DIEs in the current comdat debug
7182    info section.  */
7183 static char *comdat_symbol_id;
7184
7185 /* The index of the current symbol within the current comdat CU.  */
7186 static unsigned int comdat_symbol_number;
7187
7188 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7189    children, and set comdat_symbol_id accordingly.  */
7190
7191 static void
7192 compute_section_prefix (dw_die_ref unit_die)
7193 {
7194   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7195   const char *base = die_name ? lbasename (die_name) : "anonymous";
7196   char *name = XALLOCAVEC (char, strlen (base) + 64);
7197   char *p;
7198   int i, mark;
7199   unsigned char checksum[16];
7200   struct md5_ctx ctx;
7201
7202   /* Compute the checksum of the DIE, then append part of it as hex digits to
7203      the name filename of the unit.  */
7204
7205   md5_init_ctx (&ctx);
7206   mark = 0;
7207   die_checksum (unit_die, &ctx, &mark);
7208   unmark_all_dies (unit_die);
7209   md5_finish_ctx (&ctx, checksum);
7210
7211   sprintf (name, "%s.", base);
7212   clean_symbol_name (name);
7213
7214   p = name + strlen (name);
7215   for (i = 0; i < 4; i++)
7216     {
7217       sprintf (p, "%.2x", checksum[i]);
7218       p += 2;
7219     }
7220
7221   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7222   comdat_symbol_number = 0;
7223 }
7224
7225 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7226
7227 static int
7228 is_type_die (dw_die_ref die)
7229 {
7230   switch (die->die_tag)
7231     {
7232     case DW_TAG_array_type:
7233     case DW_TAG_class_type:
7234     case DW_TAG_interface_type:
7235     case DW_TAG_enumeration_type:
7236     case DW_TAG_pointer_type:
7237     case DW_TAG_reference_type:
7238     case DW_TAG_rvalue_reference_type:
7239     case DW_TAG_string_type:
7240     case DW_TAG_structure_type:
7241     case DW_TAG_subroutine_type:
7242     case DW_TAG_union_type:
7243     case DW_TAG_ptr_to_member_type:
7244     case DW_TAG_set_type:
7245     case DW_TAG_subrange_type:
7246     case DW_TAG_base_type:
7247     case DW_TAG_const_type:
7248     case DW_TAG_file_type:
7249     case DW_TAG_packed_type:
7250     case DW_TAG_volatile_type:
7251     case DW_TAG_typedef:
7252       return 1;
7253     default:
7254       return 0;
7255     }
7256 }
7257
7258 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7259    Basically, we want to choose the bits that are likely to be shared between
7260    compilations (types) and leave out the bits that are specific to individual
7261    compilations (functions).  */
7262
7263 static int
7264 is_comdat_die (dw_die_ref c)
7265 {
7266   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7267      we do for stabs.  The advantage is a greater likelihood of sharing between
7268      objects that don't include headers in the same order (and therefore would
7269      put the base types in a different comdat).  jason 8/28/00 */
7270
7271   if (c->die_tag == DW_TAG_base_type)
7272     return 0;
7273
7274   if (c->die_tag == DW_TAG_pointer_type
7275       || c->die_tag == DW_TAG_reference_type
7276       || c->die_tag == DW_TAG_rvalue_reference_type
7277       || c->die_tag == DW_TAG_const_type
7278       || c->die_tag == DW_TAG_volatile_type)
7279     {
7280       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7281
7282       return t ? is_comdat_die (t) : 0;
7283     }
7284
7285   return is_type_die (c);
7286 }
7287
7288 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7289    compilation unit.  */
7290
7291 static int
7292 is_symbol_die (dw_die_ref c)
7293 {
7294   return (is_type_die (c)
7295           || is_declaration_die (c)
7296           || c->die_tag == DW_TAG_namespace
7297           || c->die_tag == DW_TAG_module);
7298 }
7299
7300 /* Returns true iff C is a compile-unit DIE.  */
7301
7302 static inline bool
7303 is_cu_die (dw_die_ref c)
7304 {
7305   return c && c->die_tag == DW_TAG_compile_unit;
7306 }
7307
7308 static char *
7309 gen_internal_sym (const char *prefix)
7310 {
7311   char buf[256];
7312
7313   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7314   return xstrdup (buf);
7315 }
7316
7317 /* Assign symbols to all worthy DIEs under DIE.  */
7318
7319 static void
7320 assign_symbol_names (dw_die_ref die)
7321 {
7322   dw_die_ref c;
7323
7324   if (is_symbol_die (die))
7325     {
7326       if (comdat_symbol_id)
7327         {
7328           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7329
7330           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7331                    comdat_symbol_id, comdat_symbol_number++);
7332           die->die_id.die_symbol = xstrdup (p);
7333         }
7334       else
7335         die->die_id.die_symbol = gen_internal_sym ("LDIE");
7336     }
7337
7338   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7339 }
7340
7341 struct cu_hash_table_entry
7342 {
7343   dw_die_ref cu;
7344   unsigned min_comdat_num, max_comdat_num;
7345   struct cu_hash_table_entry *next;
7346 };
7347
7348 /* Routines to manipulate hash table of CUs.  */
7349 static hashval_t
7350 htab_cu_hash (const void *of)
7351 {
7352   const struct cu_hash_table_entry *const entry =
7353     (const struct cu_hash_table_entry *) of;
7354
7355   return htab_hash_string (entry->cu->die_id.die_symbol);
7356 }
7357
7358 static int
7359 htab_cu_eq (const void *of1, const void *of2)
7360 {
7361   const struct cu_hash_table_entry *const entry1 =
7362     (const struct cu_hash_table_entry *) of1;
7363   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7364
7365   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7366 }
7367
7368 static void
7369 htab_cu_del (void *what)
7370 {
7371   struct cu_hash_table_entry *next,
7372     *entry = (struct cu_hash_table_entry *) what;
7373
7374   while (entry)
7375     {
7376       next = entry->next;
7377       free (entry);
7378       entry = next;
7379     }
7380 }
7381
7382 /* Check whether we have already seen this CU and set up SYM_NUM
7383    accordingly.  */
7384 static int
7385 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7386 {
7387   struct cu_hash_table_entry dummy;
7388   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7389
7390   dummy.max_comdat_num = 0;
7391
7392   slot = (struct cu_hash_table_entry **)
7393     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
7394         INSERT);
7395   entry = *slot;
7396
7397   for (; entry; last = entry, entry = entry->next)
7398     {
7399       if (same_die_p_wrap (cu, entry->cu))
7400         break;
7401     }
7402
7403   if (entry)
7404     {
7405       *sym_num = entry->min_comdat_num;
7406       return 1;
7407     }
7408
7409   entry = XCNEW (struct cu_hash_table_entry);
7410   entry->cu = cu;
7411   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7412   entry->next = *slot;
7413   *slot = entry;
7414
7415   return 0;
7416 }
7417
7418 /* Record SYM_NUM to record of CU in HTABLE.  */
7419 static void
7420 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7421 {
7422   struct cu_hash_table_entry **slot, *entry;
7423
7424   slot = (struct cu_hash_table_entry **)
7425     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
7426         NO_INSERT);
7427   entry = *slot;
7428
7429   entry->max_comdat_num = sym_num;
7430 }
7431
7432 /* Traverse the DIE (which is always comp_unit_die), and set up
7433    additional compilation units for each of the include files we see
7434    bracketed by BINCL/EINCL.  */
7435
7436 static void
7437 break_out_includes (dw_die_ref die)
7438 {
7439   dw_die_ref c;
7440   dw_die_ref unit = NULL;
7441   limbo_die_node *node, **pnode;
7442   htab_t cu_hash_table;
7443
7444   c = die->die_child;
7445   if (c) do {
7446     dw_die_ref prev = c;
7447     c = c->die_sib;
7448     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7449            || (unit && is_comdat_die (c)))
7450       {
7451         dw_die_ref next = c->die_sib;
7452
7453         /* This DIE is for a secondary CU; remove it from the main one.  */
7454         remove_child_with_prev (c, prev);
7455
7456         if (c->die_tag == DW_TAG_GNU_BINCL)
7457           unit = push_new_compile_unit (unit, c);
7458         else if (c->die_tag == DW_TAG_GNU_EINCL)
7459           unit = pop_compile_unit (unit);
7460         else
7461           add_child_die (unit, c);
7462         c = next;
7463         if (c == die->die_child)
7464           break;
7465       }
7466   } while (c != die->die_child);
7467
7468 #if 0
7469   /* We can only use this in debugging, since the frontend doesn't check
7470      to make sure that we leave every include file we enter.  */
7471   gcc_assert (!unit);
7472 #endif
7473
7474   assign_symbol_names (die);
7475   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7476   for (node = limbo_die_list, pnode = &limbo_die_list;
7477        node;
7478        node = node->next)
7479     {
7480       int is_dupl;
7481
7482       compute_section_prefix (node->die);
7483       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7484                         &comdat_symbol_number);
7485       assign_symbol_names (node->die);
7486       if (is_dupl)
7487         *pnode = node->next;
7488       else
7489         {
7490           pnode = &node->next;
7491           record_comdat_symbol_number (node->die, cu_hash_table,
7492                 comdat_symbol_number);
7493         }
7494     }
7495   htab_delete (cu_hash_table);
7496 }
7497
7498 /* Return non-zero if this DIE is a declaration.  */
7499
7500 static int
7501 is_declaration_die (dw_die_ref die)
7502 {
7503   dw_attr_ref a;
7504   unsigned ix;
7505
7506   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7507     if (a->dw_attr == DW_AT_declaration)
7508       return 1;
7509
7510   return 0;
7511 }
7512
7513 /* Return non-zero if this DIE is nested inside a subprogram.  */
7514
7515 static int
7516 is_nested_in_subprogram (dw_die_ref die)
7517 {
7518   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7519
7520   if (decl == NULL)
7521     decl = die;
7522   return local_scope_p (decl);
7523 }
7524
7525 /* Return non-zero if this DIE contains a defining declaration of a
7526    subprogram.  */
7527
7528 static int
7529 contains_subprogram_definition (dw_die_ref die)
7530 {
7531   dw_die_ref c;
7532
7533   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7534     return 1;
7535   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
7536   return 0;
7537 }
7538
7539 /* Return non-zero if this is a type DIE that should be moved to a
7540    COMDAT .debug_types section.  */
7541
7542 static int
7543 should_move_die_to_comdat (dw_die_ref die)
7544 {
7545   switch (die->die_tag)
7546     {
7547     case DW_TAG_class_type:
7548     case DW_TAG_structure_type:
7549     case DW_TAG_enumeration_type:
7550     case DW_TAG_union_type:
7551       /* Don't move declarations, inlined instances, or types nested in a
7552          subprogram.  */
7553       if (is_declaration_die (die)
7554           || get_AT (die, DW_AT_abstract_origin)
7555           || is_nested_in_subprogram (die))
7556         return 0;
7557       /* A type definition should never contain a subprogram definition.  */
7558       gcc_assert (!contains_subprogram_definition (die));
7559       return 1;
7560     case DW_TAG_array_type:
7561     case DW_TAG_interface_type:
7562     case DW_TAG_pointer_type:
7563     case DW_TAG_reference_type:
7564     case DW_TAG_rvalue_reference_type:
7565     case DW_TAG_string_type:
7566     case DW_TAG_subroutine_type:
7567     case DW_TAG_ptr_to_member_type:
7568     case DW_TAG_set_type:
7569     case DW_TAG_subrange_type:
7570     case DW_TAG_base_type:
7571     case DW_TAG_const_type:
7572     case DW_TAG_file_type:
7573     case DW_TAG_packed_type:
7574     case DW_TAG_volatile_type:
7575     case DW_TAG_typedef:
7576     default:
7577       return 0;
7578     }
7579 }
7580
7581 /* Make a clone of DIE.  */
7582
7583 static dw_die_ref
7584 clone_die (dw_die_ref die)
7585 {
7586   dw_die_ref clone;
7587   dw_attr_ref a;
7588   unsigned ix;
7589
7590   clone = ggc_alloc_cleared_die_node ();
7591   clone->die_tag = die->die_tag;
7592
7593   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7594     add_dwarf_attr (clone, a);
7595
7596   return clone;
7597 }
7598
7599 /* Make a clone of the tree rooted at DIE.  */
7600
7601 static dw_die_ref
7602 clone_tree (dw_die_ref die)
7603 {
7604   dw_die_ref c;
7605   dw_die_ref clone = clone_die (die);
7606
7607   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
7608
7609   return clone;
7610 }
7611
7612 /* Make a clone of DIE as a declaration.  */
7613
7614 static dw_die_ref
7615 clone_as_declaration (dw_die_ref die)
7616 {
7617   dw_die_ref clone;
7618   dw_die_ref decl;
7619   dw_attr_ref a;
7620   unsigned ix;
7621
7622   /* If the DIE is already a declaration, just clone it.  */
7623   if (is_declaration_die (die))
7624     return clone_die (die);
7625
7626   /* If the DIE is a specification, just clone its declaration DIE.  */
7627   decl = get_AT_ref (die, DW_AT_specification);
7628   if (decl != NULL)
7629     return clone_die (decl);
7630
7631   clone = ggc_alloc_cleared_die_node ();
7632   clone->die_tag = die->die_tag;
7633
7634   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7635     {
7636       /* We don't want to copy over all attributes.
7637          For example we don't want DW_AT_byte_size because otherwise we will no
7638          longer have a declaration and GDB will treat it as a definition.  */
7639
7640       switch (a->dw_attr)
7641         {
7642         case DW_AT_artificial:
7643         case DW_AT_containing_type:
7644         case DW_AT_external:
7645         case DW_AT_name:
7646         case DW_AT_type:
7647         case DW_AT_virtuality:
7648         case DW_AT_linkage_name:
7649         case DW_AT_MIPS_linkage_name:
7650           add_dwarf_attr (clone, a);
7651           break;
7652         case DW_AT_byte_size:
7653         default:
7654           break;
7655         }
7656     }
7657
7658   if (die->die_id.die_type_node)
7659     add_AT_die_ref (clone, DW_AT_signature, die);
7660
7661   add_AT_flag (clone, DW_AT_declaration, 1);
7662   return clone;
7663 }
7664
7665 /* Copy the declaration context to the new compile unit DIE.  This includes
7666    any surrounding namespace or type declarations.  If the DIE has an
7667    AT_specification attribute, it also includes attributes and children
7668    attached to the specification.  */
7669
7670 static void
7671 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7672 {
7673   dw_die_ref decl;
7674   dw_die_ref new_decl;
7675
7676   decl = get_AT_ref (die, DW_AT_specification);
7677   if (decl == NULL)
7678     decl = die;
7679   else
7680     {
7681       unsigned ix;
7682       dw_die_ref c;
7683       dw_attr_ref a;
7684
7685       /* Copy the type node pointer from the new DIE to the original
7686          declaration DIE so we can forward references later.  */
7687       decl->die_id.die_type_node = die->die_id.die_type_node;
7688
7689       remove_AT (die, DW_AT_specification);
7690
7691       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7692         {
7693           if (a->dw_attr != DW_AT_name
7694               && a->dw_attr != DW_AT_declaration
7695               && a->dw_attr != DW_AT_external)
7696             add_dwarf_attr (die, a);
7697         }
7698
7699       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7700     }
7701
7702   if (decl->die_parent != NULL
7703       && decl->die_parent->die_tag != DW_TAG_compile_unit
7704       && decl->die_parent->die_tag != DW_TAG_type_unit)
7705     {
7706       new_decl = copy_ancestor_tree (unit, decl, NULL);
7707       if (new_decl != NULL)
7708         {
7709           remove_AT (new_decl, DW_AT_signature);
7710           add_AT_specification (die, new_decl);
7711         }
7712     }
7713 }
7714
7715 /* Generate the skeleton ancestor tree for the given NODE, then clone
7716    the DIE and add the clone into the tree.  */
7717
7718 static void
7719 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7720 {
7721   if (node->new_die != NULL)
7722     return;
7723
7724   node->new_die = clone_as_declaration (node->old_die);
7725
7726   if (node->parent != NULL)
7727     {
7728       generate_skeleton_ancestor_tree (node->parent);
7729       add_child_die (node->parent->new_die, node->new_die);
7730     }
7731 }
7732
7733 /* Generate a skeleton tree of DIEs containing any declarations that are
7734    found in the original tree.  We traverse the tree looking for declaration
7735    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7736
7737 static void
7738 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7739 {
7740   skeleton_chain_node node;
7741   dw_die_ref c;
7742   dw_die_ref first;
7743   dw_die_ref prev = NULL;
7744   dw_die_ref next = NULL;
7745
7746   node.parent = parent;
7747
7748   first = c = parent->old_die->die_child;
7749   if (c)
7750     next = c->die_sib;
7751   if (c) do {
7752     if (prev == NULL || prev->die_sib == c)
7753       prev = c;
7754     c = next;
7755     next = (c == first ? NULL : c->die_sib);
7756     node.old_die = c;
7757     node.new_die = NULL;
7758     if (is_declaration_die (c))
7759       {
7760         /* Clone the existing DIE, move the original to the skeleton
7761            tree (which is in the main CU), and put the clone, with
7762            all the original's children, where the original came from.  */
7763         dw_die_ref clone = clone_die (c);
7764         move_all_children (c, clone);
7765
7766         replace_child (c, clone, prev);
7767         generate_skeleton_ancestor_tree (parent);
7768         add_child_die (parent->new_die, c);
7769         node.new_die = c;
7770         c = clone;
7771       }
7772     generate_skeleton_bottom_up (&node);
7773   } while (next != NULL);
7774 }
7775
7776 /* Wrapper function for generate_skeleton_bottom_up.  */
7777
7778 static dw_die_ref
7779 generate_skeleton (dw_die_ref die)
7780 {
7781   skeleton_chain_node node;
7782
7783   node.old_die = die;
7784   node.new_die = NULL;
7785   node.parent = NULL;
7786
7787   /* If this type definition is nested inside another type,
7788      always leave at least a declaration in its place.  */
7789   if (die->die_parent != NULL && is_type_die (die->die_parent))
7790     node.new_die = clone_as_declaration (die);
7791
7792   generate_skeleton_bottom_up (&node);
7793   return node.new_die;
7794 }
7795
7796 /* Remove the DIE from its parent, possibly replacing it with a cloned
7797    declaration.  The original DIE will be moved to a new compile unit
7798    so that existing references to it follow it to the new location.  If
7799    any of the original DIE's descendants is a declaration, we need to
7800    replace the original DIE with a skeleton tree and move the
7801    declarations back into the skeleton tree.  */
7802
7803 static dw_die_ref
7804 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7805 {
7806   dw_die_ref skeleton;
7807
7808   skeleton = generate_skeleton (child);
7809   if (skeleton == NULL)
7810     remove_child_with_prev (child, prev);
7811   else
7812     {
7813       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7814       replace_child (child, skeleton, prev);
7815     }
7816
7817   return skeleton;
7818 }
7819
7820 /* Traverse the DIE and set up additional .debug_types sections for each
7821    type worthy of being placed in a COMDAT section.  */
7822
7823 static void
7824 break_out_comdat_types (dw_die_ref die)
7825 {
7826   dw_die_ref c;
7827   dw_die_ref first;
7828   dw_die_ref prev = NULL;
7829   dw_die_ref next = NULL;
7830   dw_die_ref unit = NULL;
7831
7832   first = c = die->die_child;
7833   if (c)
7834     next = c->die_sib;
7835   if (c) do {
7836     if (prev == NULL || prev->die_sib == c)
7837       prev = c;
7838     c = next;
7839     next = (c == first ? NULL : c->die_sib);
7840     if (should_move_die_to_comdat (c))
7841       {
7842         dw_die_ref replacement;
7843         comdat_type_node_ref type_node;
7844
7845         /* Create a new type unit DIE as the root for the new tree, and
7846            add it to the list of comdat types.  */
7847         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7848         add_AT_unsigned (unit, DW_AT_language,
7849                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7850         type_node = ggc_alloc_cleared_comdat_type_node ();
7851         type_node->root_die = unit;
7852         type_node->next = comdat_type_list;
7853         comdat_type_list = type_node;
7854
7855         /* Generate the type signature.  */
7856         generate_type_signature (c, type_node);
7857
7858         /* Copy the declaration context, attributes, and children of the
7859            declaration into the new compile unit DIE.  */
7860         copy_declaration_context (unit, c);
7861
7862         /* Remove this DIE from the main CU.  */
7863         replacement = remove_child_or_replace_with_skeleton (c, prev);
7864
7865         /* Break out nested types into their own type units.  */
7866         break_out_comdat_types (c);
7867
7868         /* Add the DIE to the new compunit.  */
7869         add_child_die (unit, c);
7870
7871         if (replacement != NULL)
7872           c = replacement;
7873       }
7874     else if (c->die_tag == DW_TAG_namespace
7875              || c->die_tag == DW_TAG_class_type
7876              || c->die_tag == DW_TAG_structure_type
7877              || c->die_tag == DW_TAG_union_type)
7878       {
7879         /* Look for nested types that can be broken out.  */
7880         break_out_comdat_types (c);
7881       }
7882   } while (next != NULL);
7883 }
7884
7885 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7886
7887 struct decl_table_entry
7888 {
7889   dw_die_ref orig;
7890   dw_die_ref copy;
7891 };
7892
7893 /* Routines to manipulate hash table of copied declarations.  */
7894
7895 static hashval_t
7896 htab_decl_hash (const void *of)
7897 {
7898   const struct decl_table_entry *const entry =
7899     (const struct decl_table_entry *) of;
7900
7901   return htab_hash_pointer (entry->orig);
7902 }
7903
7904 static int
7905 htab_decl_eq (const void *of1, const void *of2)
7906 {
7907   const struct decl_table_entry *const entry1 =
7908     (const struct decl_table_entry *) of1;
7909   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7910
7911   return entry1->orig == entry2;
7912 }
7913
7914 static void
7915 htab_decl_del (void *what)
7916 {
7917   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7918
7919   free (entry);
7920 }
7921
7922 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7923    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7924    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7925    to check if the ancestor has already been copied into UNIT.  */
7926
7927 static dw_die_ref
7928 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7929 {
7930   dw_die_ref parent = die->die_parent;
7931   dw_die_ref new_parent = unit;
7932   dw_die_ref copy;
7933   void **slot = NULL;
7934   struct decl_table_entry *entry = NULL;
7935
7936   if (decl_table)
7937     {
7938       /* Check if the entry has already been copied to UNIT.  */
7939       slot = htab_find_slot_with_hash (decl_table, die,
7940                                        htab_hash_pointer (die), INSERT);
7941       if (*slot != HTAB_EMPTY_ENTRY)
7942         {
7943           entry = (struct decl_table_entry *) *slot;
7944           return entry->copy;
7945         }
7946
7947       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7948       entry = XCNEW (struct decl_table_entry);
7949       entry->orig = die;
7950       entry->copy = NULL;
7951       *slot = entry;
7952     }
7953
7954   if (parent != NULL)
7955     {
7956       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7957       if (spec != NULL)
7958         parent = spec;
7959       if (parent->die_tag != DW_TAG_compile_unit
7960           && parent->die_tag != DW_TAG_type_unit)
7961         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7962     }
7963
7964   copy = clone_as_declaration (die);
7965   add_child_die (new_parent, copy);
7966
7967   if (decl_table != NULL)
7968     {
7969       /* Record the pointer to the copy.  */
7970       entry->copy = copy;
7971     }
7972
7973   return copy;
7974 }
7975
7976 /* Walk the DIE and its children, looking for references to incomplete
7977    or trivial types that are unmarked (i.e., that are not in the current
7978    type_unit).  */
7979
7980 static void
7981 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7982 {
7983   dw_die_ref c;
7984   dw_attr_ref a;
7985   unsigned ix;
7986
7987   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7988     {
7989       if (AT_class (a) == dw_val_class_die_ref)
7990         {
7991           dw_die_ref targ = AT_ref (a);
7992           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7993           void **slot;
7994           struct decl_table_entry *entry;
7995
7996           if (targ->die_mark != 0 || type_node != NULL)
7997             continue;
7998
7999           slot = htab_find_slot_with_hash (decl_table, targ,
8000                                            htab_hash_pointer (targ), INSERT);
8001
8002           if (*slot != HTAB_EMPTY_ENTRY)
8003             {
8004               /* TARG has already been copied, so we just need to
8005                  modify the reference to point to the copy.  */
8006               entry = (struct decl_table_entry *) *slot;
8007               a->dw_attr_val.v.val_die_ref.die = entry->copy;
8008             }
8009           else
8010             {
8011               dw_die_ref parent = unit;
8012               dw_die_ref copy = clone_tree (targ);
8013
8014               /* Make sure the cloned tree is marked as part of the
8015                  type unit.  */
8016               mark_dies (copy);
8017
8018               /* Record in DECL_TABLE that TARG has been copied.
8019                  Need to do this now, before the recursive call,
8020                  because DECL_TABLE may be expanded and SLOT
8021                  would no longer be a valid pointer.  */
8022               entry = XCNEW (struct decl_table_entry);
8023               entry->orig = targ;
8024               entry->copy = copy;
8025               *slot = entry;
8026
8027               /* If TARG has surrounding context, copy its ancestor tree
8028                  into the new type unit.  */
8029               if (targ->die_parent != NULL
8030                   && targ->die_parent->die_tag != DW_TAG_compile_unit
8031                   && targ->die_parent->die_tag != DW_TAG_type_unit)
8032                 parent = copy_ancestor_tree (unit, targ->die_parent,
8033                                              decl_table);
8034
8035               add_child_die (parent, copy);
8036               a->dw_attr_val.v.val_die_ref.die = copy;
8037
8038               /* Make sure the newly-copied DIE is walked.  If it was
8039                  installed in a previously-added context, it won't
8040                  get visited otherwise.  */
8041               if (parent != unit)
8042                 {
8043                   /* Find the highest point of the newly-added tree,
8044                      mark each node along the way, and walk from there.  */
8045                   parent->die_mark = 1;
8046                   while (parent->die_parent
8047                          && parent->die_parent->die_mark == 0)
8048                     {
8049                       parent = parent->die_parent;
8050                       parent->die_mark = 1;
8051                     }
8052                   copy_decls_walk (unit, parent, decl_table);
8053                 }
8054             }
8055         }
8056     }
8057
8058   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8059 }
8060
8061 /* Copy declarations for "unworthy" types into the new comdat section.
8062    Incomplete types, modified types, and certain other types aren't broken
8063    out into comdat sections of their own, so they don't have a signature,
8064    and we need to copy the declaration into the same section so that we
8065    don't have an external reference.  */
8066
8067 static void
8068 copy_decls_for_unworthy_types (dw_die_ref unit)
8069 {
8070   htab_t decl_table;
8071
8072   mark_dies (unit);
8073   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
8074   copy_decls_walk (unit, unit, decl_table);
8075   htab_delete (decl_table);
8076   unmark_dies (unit);
8077 }
8078
8079 /* Traverse the DIE and add a sibling attribute if it may have the
8080    effect of speeding up access to siblings.  To save some space,
8081    avoid generating sibling attributes for DIE's without children.  */
8082
8083 static void
8084 add_sibling_attributes (dw_die_ref die)
8085 {
8086   dw_die_ref c;
8087
8088   if (! die->die_child)
8089     return;
8090
8091   if (die->die_parent && die != die->die_parent->die_child)
8092     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8093
8094   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8095 }
8096
8097 /* Output all location lists for the DIE and its children.  */
8098
8099 static void
8100 output_location_lists (dw_die_ref die)
8101 {
8102   dw_die_ref c;
8103   dw_attr_ref a;
8104   unsigned ix;
8105
8106   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8107     if (AT_class (a) == dw_val_class_loc_list)
8108       output_loc_list (AT_loc_list (a));
8109
8110   FOR_EACH_CHILD (die, c, output_location_lists (c));
8111 }
8112
8113 /* The format of each DIE (and its attribute value pairs) is encoded in an
8114    abbreviation table.  This routine builds the abbreviation table and assigns
8115    a unique abbreviation id for each abbreviation entry.  The children of each
8116    die are visited recursively.  */
8117
8118 static void
8119 build_abbrev_table (dw_die_ref die)
8120 {
8121   unsigned long abbrev_id;
8122   unsigned int n_alloc;
8123   dw_die_ref c;
8124   dw_attr_ref a;
8125   unsigned ix;
8126
8127   /* Scan the DIE references, and mark as external any that refer to
8128      DIEs from other CUs (i.e. those which are not marked).  */
8129   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8130     if (AT_class (a) == dw_val_class_die_ref
8131         && AT_ref (a)->die_mark == 0)
8132       {
8133         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
8134         set_AT_ref_external (a, 1);
8135       }
8136
8137   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8138     {
8139       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8140       dw_attr_ref die_a, abbrev_a;
8141       unsigned ix;
8142       bool ok = true;
8143
8144       if (abbrev->die_tag != die->die_tag)
8145         continue;
8146       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8147         continue;
8148
8149       if (VEC_length (dw_attr_node, abbrev->die_attr)
8150           != VEC_length (dw_attr_node, die->die_attr))
8151         continue;
8152
8153       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
8154         {
8155           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8156           if ((abbrev_a->dw_attr != die_a->dw_attr)
8157               || (value_format (abbrev_a) != value_format (die_a)))
8158             {
8159               ok = false;
8160               break;
8161             }
8162         }
8163       if (ok)
8164         break;
8165     }
8166
8167   if (abbrev_id >= abbrev_die_table_in_use)
8168     {
8169       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8170         {
8171           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8172           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8173                                             n_alloc);
8174
8175           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8176                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8177           abbrev_die_table_allocated = n_alloc;
8178         }
8179
8180       ++abbrev_die_table_in_use;
8181       abbrev_die_table[abbrev_id] = die;
8182     }
8183
8184   die->die_abbrev = abbrev_id;
8185   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8186 }
8187 \f
8188 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8189
8190 static int
8191 constant_size (unsigned HOST_WIDE_INT value)
8192 {
8193   int log;
8194
8195   if (value == 0)
8196     log = 0;
8197   else
8198     log = floor_log2 (value);
8199
8200   log = log / 8;
8201   log = 1 << (floor_log2 (log) + 1);
8202
8203   return log;
8204 }
8205
8206 /* Return the size of a DIE as it is represented in the
8207    .debug_info section.  */
8208
8209 static unsigned long
8210 size_of_die (dw_die_ref die)
8211 {
8212   unsigned long size = 0;
8213   dw_attr_ref a;
8214   unsigned ix;
8215
8216   size += size_of_uleb128 (die->die_abbrev);
8217   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8218     {
8219       switch (AT_class (a))
8220         {
8221         case dw_val_class_addr:
8222           size += DWARF2_ADDR_SIZE;
8223           break;
8224         case dw_val_class_offset:
8225           size += DWARF_OFFSET_SIZE;
8226           break;
8227         case dw_val_class_loc:
8228           {
8229             unsigned long lsize = size_of_locs (AT_loc (a));
8230
8231             /* Block length.  */
8232             if (dwarf_version >= 4)
8233               size += size_of_uleb128 (lsize);
8234             else
8235               size += constant_size (lsize);
8236             size += lsize;
8237           }
8238           break;
8239         case dw_val_class_loc_list:
8240           size += DWARF_OFFSET_SIZE;
8241           break;
8242         case dw_val_class_range_list:
8243           size += DWARF_OFFSET_SIZE;
8244           break;
8245         case dw_val_class_const:
8246           size += size_of_sleb128 (AT_int (a));
8247           break;
8248         case dw_val_class_unsigned_const:
8249           size += constant_size (AT_unsigned (a));
8250           break;
8251         case dw_val_class_const_double:
8252           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8253           if (HOST_BITS_PER_WIDE_INT >= 64)
8254             size++; /* block */
8255           break;
8256         case dw_val_class_vec:
8257           size += constant_size (a->dw_attr_val.v.val_vec.length
8258                                  * a->dw_attr_val.v.val_vec.elt_size)
8259                   + a->dw_attr_val.v.val_vec.length
8260                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8261           break;
8262         case dw_val_class_flag:
8263           if (dwarf_version >= 4)
8264             /* Currently all add_AT_flag calls pass in 1 as last argument,
8265                so DW_FORM_flag_present can be used.  If that ever changes,
8266                we'll need to use DW_FORM_flag and have some optimization
8267                in build_abbrev_table that will change those to
8268                DW_FORM_flag_present if it is set to 1 in all DIEs using
8269                the same abbrev entry.  */
8270             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8271           else
8272             size += 1;
8273           break;
8274         case dw_val_class_die_ref:
8275           if (AT_ref_external (a))
8276             {
8277               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8278                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8279                  is sized by target address length, whereas in DWARF3
8280                  it's always sized as an offset.  */
8281               if (use_debug_types)
8282                 size += DWARF_TYPE_SIGNATURE_SIZE;
8283               else if (dwarf_version == 2)
8284                 size += DWARF2_ADDR_SIZE;
8285               else
8286                 size += DWARF_OFFSET_SIZE;
8287             }
8288           else
8289             size += DWARF_OFFSET_SIZE;
8290           break;
8291         case dw_val_class_fde_ref:
8292           size += DWARF_OFFSET_SIZE;
8293           break;
8294         case dw_val_class_lbl_id:
8295           size += DWARF2_ADDR_SIZE;
8296           break;
8297         case dw_val_class_lineptr:
8298         case dw_val_class_macptr:
8299           size += DWARF_OFFSET_SIZE;
8300           break;
8301         case dw_val_class_str:
8302           if (AT_string_form (a) == DW_FORM_strp)
8303             size += DWARF_OFFSET_SIZE;
8304           else
8305             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8306           break;
8307         case dw_val_class_file:
8308           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8309           break;
8310         case dw_val_class_data8:
8311           size += 8;
8312           break;
8313         case dw_val_class_vms_delta:
8314           size += DWARF_OFFSET_SIZE;
8315           break;
8316         default:
8317           gcc_unreachable ();
8318         }
8319     }
8320
8321   return size;
8322 }
8323
8324 /* Size the debugging information associated with a given DIE.  Visits the
8325    DIE's children recursively.  Updates the global variable next_die_offset, on
8326    each time through.  Uses the current value of next_die_offset to update the
8327    die_offset field in each DIE.  */
8328
8329 static void
8330 calc_die_sizes (dw_die_ref die)
8331 {
8332   dw_die_ref c;
8333
8334   gcc_assert (die->die_offset == 0
8335               || (unsigned long int) die->die_offset == next_die_offset);
8336   die->die_offset = next_die_offset;
8337   next_die_offset += size_of_die (die);
8338
8339   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8340
8341   if (die->die_child != NULL)
8342     /* Count the null byte used to terminate sibling lists.  */
8343     next_die_offset += 1;
8344 }
8345
8346 /* Size just the base type children at the start of the CU.
8347    This is needed because build_abbrev needs to size locs
8348    and sizing of type based stack ops needs to know die_offset
8349    values for the base types.  */
8350
8351 static void
8352 calc_base_type_die_sizes (void)
8353 {
8354   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8355   unsigned int i;
8356   dw_die_ref base_type;
8357 #if ENABLE_ASSERT_CHECKING
8358   dw_die_ref prev = comp_unit_die ()->die_child;
8359 #endif
8360
8361   die_offset += size_of_die (comp_unit_die ());
8362   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
8363     {
8364 #if ENABLE_ASSERT_CHECKING
8365       gcc_assert (base_type->die_offset == 0
8366                   && prev->die_sib == base_type
8367                   && base_type->die_child == NULL
8368                   && base_type->die_abbrev);
8369       prev = base_type;
8370 #endif
8371       base_type->die_offset = die_offset;
8372       die_offset += size_of_die (base_type);
8373     }
8374 }
8375
8376 /* Set the marks for a die and its children.  We do this so
8377    that we know whether or not a reference needs to use FORM_ref_addr; only
8378    DIEs in the same CU will be marked.  We used to clear out the offset
8379    and use that as the flag, but ran into ordering problems.  */
8380
8381 static void
8382 mark_dies (dw_die_ref die)
8383 {
8384   dw_die_ref c;
8385
8386   gcc_assert (!die->die_mark);
8387
8388   die->die_mark = 1;
8389   FOR_EACH_CHILD (die, c, mark_dies (c));
8390 }
8391
8392 /* Clear the marks for a die and its children.  */
8393
8394 static void
8395 unmark_dies (dw_die_ref die)
8396 {
8397   dw_die_ref c;
8398
8399   if (! use_debug_types)
8400     gcc_assert (die->die_mark);
8401
8402   die->die_mark = 0;
8403   FOR_EACH_CHILD (die, c, unmark_dies (c));
8404 }
8405
8406 /* Clear the marks for a die, its children and referred dies.  */
8407
8408 static void
8409 unmark_all_dies (dw_die_ref die)
8410 {
8411   dw_die_ref c;
8412   dw_attr_ref a;
8413   unsigned ix;
8414
8415   if (!die->die_mark)
8416     return;
8417   die->die_mark = 0;
8418
8419   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8420
8421   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8422     if (AT_class (a) == dw_val_class_die_ref)
8423       unmark_all_dies (AT_ref (a));
8424 }
8425
8426 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8427    generated for the compilation unit.  */
8428
8429 static unsigned long
8430 size_of_pubnames (VEC (pubname_entry, gc) * names)
8431 {
8432   unsigned long size;
8433   unsigned i;
8434   pubname_ref p;
8435
8436   size = DWARF_PUBNAMES_HEADER_SIZE;
8437   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
8438     if (names != pubtype_table
8439         || p->die->die_offset != 0
8440         || !flag_eliminate_unused_debug_types)
8441       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8442
8443   size += DWARF_OFFSET_SIZE;
8444   return size;
8445 }
8446
8447 /* Return the size of the information in the .debug_aranges section.  */
8448
8449 static unsigned long
8450 size_of_aranges (void)
8451 {
8452   unsigned long size;
8453
8454   size = DWARF_ARANGES_HEADER_SIZE;
8455
8456   /* Count the address/length pair for this compilation unit.  */
8457   if (text_section_used)
8458     size += 2 * DWARF2_ADDR_SIZE;
8459   if (cold_text_section_used)
8460     size += 2 * DWARF2_ADDR_SIZE;
8461   if (have_multiple_function_sections)
8462     {
8463       unsigned fde_idx = 0;
8464
8465       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
8466         {
8467           dw_fde_ref fde = &fde_table[fde_idx];
8468
8469           if (!fde->in_std_section)
8470             size += 2 * DWARF2_ADDR_SIZE;
8471           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8472             size += 2 * DWARF2_ADDR_SIZE;
8473         }
8474     }
8475
8476   /* Count the two zero words used to terminated the address range table.  */
8477   size += 2 * DWARF2_ADDR_SIZE;
8478   return size;
8479 }
8480 \f
8481 /* Select the encoding of an attribute value.  */
8482
8483 static enum dwarf_form
8484 value_format (dw_attr_ref a)
8485 {
8486   switch (a->dw_attr_val.val_class)
8487     {
8488     case dw_val_class_addr:
8489       /* Only very few attributes allow DW_FORM_addr.  */
8490       switch (a->dw_attr)
8491         {
8492         case DW_AT_low_pc:
8493         case DW_AT_high_pc:
8494         case DW_AT_entry_pc:
8495         case DW_AT_trampoline:
8496           return DW_FORM_addr;
8497         default:
8498           break;
8499         }
8500       switch (DWARF2_ADDR_SIZE)
8501         {
8502         case 1:
8503           return DW_FORM_data1;
8504         case 2:
8505           return DW_FORM_data2;
8506         case 4:
8507           return DW_FORM_data4;
8508         case 8:
8509           return DW_FORM_data8;
8510         default:
8511           gcc_unreachable ();
8512         }
8513     case dw_val_class_range_list:
8514     case dw_val_class_loc_list:
8515       if (dwarf_version >= 4)
8516         return DW_FORM_sec_offset;
8517       /* FALLTHRU */
8518     case dw_val_class_vms_delta:
8519     case dw_val_class_offset:
8520       switch (DWARF_OFFSET_SIZE)
8521         {
8522         case 4:
8523           return DW_FORM_data4;
8524         case 8:
8525           return DW_FORM_data8;
8526         default:
8527           gcc_unreachable ();
8528         }
8529     case dw_val_class_loc:
8530       if (dwarf_version >= 4)
8531         return DW_FORM_exprloc;
8532       switch (constant_size (size_of_locs (AT_loc (a))))
8533         {
8534         case 1:
8535           return DW_FORM_block1;
8536         case 2:
8537           return DW_FORM_block2;
8538         default:
8539           gcc_unreachable ();
8540         }
8541     case dw_val_class_const:
8542       return DW_FORM_sdata;
8543     case dw_val_class_unsigned_const:
8544       switch (constant_size (AT_unsigned (a)))
8545         {
8546         case 1:
8547           return DW_FORM_data1;
8548         case 2:
8549           return DW_FORM_data2;
8550         case 4:
8551           return DW_FORM_data4;
8552         case 8:
8553           return DW_FORM_data8;
8554         default:
8555           gcc_unreachable ();
8556         }
8557     case dw_val_class_const_double:
8558       switch (HOST_BITS_PER_WIDE_INT)
8559         {
8560         case 8:
8561           return DW_FORM_data2;
8562         case 16:
8563           return DW_FORM_data4;
8564         case 32:
8565           return DW_FORM_data8;
8566         case 64:
8567         default:
8568           return DW_FORM_block1;
8569         }
8570     case dw_val_class_vec:
8571       switch (constant_size (a->dw_attr_val.v.val_vec.length
8572                              * a->dw_attr_val.v.val_vec.elt_size))
8573         {
8574         case 1:
8575           return DW_FORM_block1;
8576         case 2:
8577           return DW_FORM_block2;
8578         case 4:
8579           return DW_FORM_block4;
8580         default:
8581           gcc_unreachable ();
8582         }
8583     case dw_val_class_flag:
8584       if (dwarf_version >= 4)
8585         {
8586           /* Currently all add_AT_flag calls pass in 1 as last argument,
8587              so DW_FORM_flag_present can be used.  If that ever changes,
8588              we'll need to use DW_FORM_flag and have some optimization
8589              in build_abbrev_table that will change those to
8590              DW_FORM_flag_present if it is set to 1 in all DIEs using
8591              the same abbrev entry.  */
8592           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8593           return DW_FORM_flag_present;
8594         }
8595       return DW_FORM_flag;
8596     case dw_val_class_die_ref:
8597       if (AT_ref_external (a))
8598         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8599       else
8600         return DW_FORM_ref;
8601     case dw_val_class_fde_ref:
8602       return DW_FORM_data;
8603     case dw_val_class_lbl_id:
8604       return DW_FORM_addr;
8605     case dw_val_class_lineptr:
8606     case dw_val_class_macptr:
8607       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8608     case dw_val_class_str:
8609       return AT_string_form (a);
8610     case dw_val_class_file:
8611       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8612         {
8613         case 1:
8614           return DW_FORM_data1;
8615         case 2:
8616           return DW_FORM_data2;
8617         case 4:
8618           return DW_FORM_data4;
8619         default:
8620           gcc_unreachable ();
8621         }
8622
8623     case dw_val_class_data8:
8624       return DW_FORM_data8;
8625
8626     default:
8627       gcc_unreachable ();
8628     }
8629 }
8630
8631 /* Output the encoding of an attribute value.  */
8632
8633 static void
8634 output_value_format (dw_attr_ref a)
8635 {
8636   enum dwarf_form form = value_format (a);
8637
8638   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8639 }
8640
8641 /* Output the .debug_abbrev section which defines the DIE abbreviation
8642    table.  */
8643
8644 static void
8645 output_abbrev_section (void)
8646 {
8647   unsigned long abbrev_id;
8648
8649   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8650     {
8651       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8652       unsigned ix;
8653       dw_attr_ref a_attr;
8654
8655       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8656       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8657                                    dwarf_tag_name (abbrev->die_tag));
8658
8659       if (abbrev->die_child != NULL)
8660         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8661       else
8662         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8663
8664       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8665            ix++)
8666         {
8667           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8668                                        dwarf_attr_name (a_attr->dw_attr));
8669           output_value_format (a_attr);
8670         }
8671
8672       dw2_asm_output_data (1, 0, NULL);
8673       dw2_asm_output_data (1, 0, NULL);
8674     }
8675
8676   /* Terminate the table.  */
8677   dw2_asm_output_data (1, 0, NULL);
8678 }
8679
8680 /* Output a symbol we can use to refer to this DIE from another CU.  */
8681
8682 static inline void
8683 output_die_symbol (dw_die_ref die)
8684 {
8685   char *sym = die->die_id.die_symbol;
8686
8687   if (sym == 0)
8688     return;
8689
8690   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8691     /* We make these global, not weak; if the target doesn't support
8692        .linkonce, it doesn't support combining the sections, so debugging
8693        will break.  */
8694     targetm.asm_out.globalize_label (asm_out_file, sym);
8695
8696   ASM_OUTPUT_LABEL (asm_out_file, sym);
8697 }
8698
8699 /* Return a new location list, given the begin and end range, and the
8700    expression.  */
8701
8702 static inline dw_loc_list_ref
8703 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8704               const char *section)
8705 {
8706   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8707
8708   retlist->begin = begin;
8709   retlist->end = end;
8710   retlist->expr = expr;
8711   retlist->section = section;
8712
8713   return retlist;
8714 }
8715
8716 /* Generate a new internal symbol for this location list node, if it
8717    hasn't got one yet.  */
8718
8719 static inline void
8720 gen_llsym (dw_loc_list_ref list)
8721 {
8722   gcc_assert (!list->ll_symbol);
8723   list->ll_symbol = gen_internal_sym ("LLST");
8724 }
8725
8726 /* Output the location list given to us.  */
8727
8728 static void
8729 output_loc_list (dw_loc_list_ref list_head)
8730 {
8731   dw_loc_list_ref curr = list_head;
8732
8733   if (list_head->emitted)
8734     return;
8735   list_head->emitted = true;
8736
8737   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8738
8739   /* Walk the location list, and output each range + expression.  */
8740   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8741     {
8742       unsigned long size;
8743       /* Don't output an entry that starts and ends at the same address.  */
8744       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8745         continue;
8746       if (!have_multiple_function_sections)
8747         {
8748           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8749                                 "Location list begin address (%s)",
8750                                 list_head->ll_symbol);
8751           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8752                                 "Location list end address (%s)",
8753                                 list_head->ll_symbol);
8754         }
8755       else
8756         {
8757           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8758                                "Location list begin address (%s)",
8759                                list_head->ll_symbol);
8760           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8761                                "Location list end address (%s)",
8762                                list_head->ll_symbol);
8763         }
8764       size = size_of_locs (curr->expr);
8765
8766       /* Output the block length for this list of location operations.  */
8767       gcc_assert (size <= 0xffff);
8768       dw2_asm_output_data (2, size, "%s", "Location expression size");
8769
8770       output_loc_sequence (curr->expr, -1);
8771     }
8772
8773   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8774                        "Location list terminator begin (%s)",
8775                        list_head->ll_symbol);
8776   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8777                        "Location list terminator end (%s)",
8778                        list_head->ll_symbol);
8779 }
8780
8781 /* Output a type signature.  */
8782
8783 static inline void
8784 output_signature (const char *sig, const char *name)
8785 {
8786   int i;
8787
8788   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8789     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8790 }
8791
8792 /* Output the DIE and its attributes.  Called recursively to generate
8793    the definitions of each child DIE.  */
8794
8795 static void
8796 output_die (dw_die_ref die)
8797 {
8798   dw_attr_ref a;
8799   dw_die_ref c;
8800   unsigned long size;
8801   unsigned ix;
8802
8803   /* If someone in another CU might refer to us, set up a symbol for
8804      them to point to.  */
8805   if (! use_debug_types && die->die_id.die_symbol)
8806     output_die_symbol (die);
8807
8808   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8809                                (unsigned long)die->die_offset,
8810                                dwarf_tag_name (die->die_tag));
8811
8812   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8813     {
8814       const char *name = dwarf_attr_name (a->dw_attr);
8815
8816       switch (AT_class (a))
8817         {
8818         case dw_val_class_addr:
8819           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8820           break;
8821
8822         case dw_val_class_offset:
8823           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8824                                "%s", name);
8825           break;
8826
8827         case dw_val_class_range_list:
8828           {
8829             char *p = strchr (ranges_section_label, '\0');
8830
8831             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8832                      a->dw_attr_val.v.val_offset);
8833             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8834                                    debug_ranges_section, "%s", name);
8835             *p = '\0';
8836           }
8837           break;
8838
8839         case dw_val_class_loc:
8840           size = size_of_locs (AT_loc (a));
8841
8842           /* Output the block length for this list of location operations.  */
8843           if (dwarf_version >= 4)
8844             dw2_asm_output_data_uleb128 (size, "%s", name);
8845           else
8846             dw2_asm_output_data (constant_size (size), size, "%s", name);
8847
8848           output_loc_sequence (AT_loc (a), -1);
8849           break;
8850
8851         case dw_val_class_const:
8852           /* ??? It would be slightly more efficient to use a scheme like is
8853              used for unsigned constants below, but gdb 4.x does not sign
8854              extend.  Gdb 5.x does sign extend.  */
8855           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8856           break;
8857
8858         case dw_val_class_unsigned_const:
8859           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8860                                AT_unsigned (a), "%s", name);
8861           break;
8862
8863         case dw_val_class_const_double:
8864           {
8865             unsigned HOST_WIDE_INT first, second;
8866
8867             if (HOST_BITS_PER_WIDE_INT >= 64)
8868               dw2_asm_output_data (1,
8869                                    2 * HOST_BITS_PER_WIDE_INT
8870                                    / HOST_BITS_PER_CHAR,
8871                                    NULL);
8872
8873             if (WORDS_BIG_ENDIAN)
8874               {
8875                 first = a->dw_attr_val.v.val_double.high;
8876                 second = a->dw_attr_val.v.val_double.low;
8877               }
8878             else
8879               {
8880                 first = a->dw_attr_val.v.val_double.low;
8881                 second = a->dw_attr_val.v.val_double.high;
8882               }
8883
8884             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8885                                  first, name);
8886             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8887                                  second, NULL);
8888           }
8889           break;
8890
8891         case dw_val_class_vec:
8892           {
8893             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8894             unsigned int len = a->dw_attr_val.v.val_vec.length;
8895             unsigned int i;
8896             unsigned char *p;
8897
8898             dw2_asm_output_data (constant_size (len * elt_size),
8899                                  len * elt_size, "%s", name);
8900             if (elt_size > sizeof (HOST_WIDE_INT))
8901               {
8902                 elt_size /= 2;
8903                 len *= 2;
8904               }
8905             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8906                  i < len;
8907                  i++, p += elt_size)
8908               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8909                                    "fp or vector constant word %u", i);
8910             break;
8911           }
8912
8913         case dw_val_class_flag:
8914           if (dwarf_version >= 4)
8915             {
8916               /* Currently all add_AT_flag calls pass in 1 as last argument,
8917                  so DW_FORM_flag_present can be used.  If that ever changes,
8918                  we'll need to use DW_FORM_flag and have some optimization
8919                  in build_abbrev_table that will change those to
8920                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8921                  the same abbrev entry.  */
8922               gcc_assert (AT_flag (a) == 1);
8923               if (flag_debug_asm)
8924                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8925                          ASM_COMMENT_START, name);
8926               break;
8927             }
8928           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8929           break;
8930
8931         case dw_val_class_loc_list:
8932           {
8933             char *sym = AT_loc_list (a)->ll_symbol;
8934
8935             gcc_assert (sym);
8936             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8937                                    "%s", name);
8938           }
8939           break;
8940
8941         case dw_val_class_die_ref:
8942           if (AT_ref_external (a))
8943             {
8944               if (use_debug_types)
8945                 {
8946                   comdat_type_node_ref type_node =
8947                     AT_ref (a)->die_id.die_type_node;
8948
8949                   gcc_assert (type_node);
8950                   output_signature (type_node->signature, name);
8951                 }
8952               else
8953                 {
8954                   char *sym = AT_ref (a)->die_id.die_symbol;
8955                   int size;
8956
8957                   gcc_assert (sym);
8958                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8959                      length, whereas in DWARF3 it's always sized as an
8960                      offset.  */
8961                   if (dwarf_version == 2)
8962                     size = DWARF2_ADDR_SIZE;
8963                   else
8964                     size = DWARF_OFFSET_SIZE;
8965                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8966                                          name);
8967                 }
8968             }
8969           else
8970             {
8971               gcc_assert (AT_ref (a)->die_offset);
8972               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8973                                    "%s", name);
8974             }
8975           break;
8976
8977         case dw_val_class_fde_ref:
8978           {
8979             char l1[20];
8980
8981             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8982                                          a->dw_attr_val.v.val_fde_index * 2);
8983             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8984                                    "%s", name);
8985           }
8986           break;
8987
8988         case dw_val_class_vms_delta:
8989           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8990                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8991                                     "%s", name);
8992           break;
8993
8994         case dw_val_class_lbl_id:
8995           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8996           break;
8997
8998         case dw_val_class_lineptr:
8999           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9000                                  debug_line_section, "%s", name);
9001           break;
9002
9003         case dw_val_class_macptr:
9004           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9005                                  debug_macinfo_section, "%s", name);
9006           break;
9007
9008         case dw_val_class_str:
9009           if (AT_string_form (a) == DW_FORM_strp)
9010             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9011                                    a->dw_attr_val.v.val_str->label,
9012                                    debug_str_section,
9013                                    "%s: \"%s\"", name, AT_string (a));
9014           else
9015             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9016           break;
9017
9018         case dw_val_class_file:
9019           {
9020             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9021
9022             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9023                                  a->dw_attr_val.v.val_file->filename);
9024             break;
9025           }
9026
9027         case dw_val_class_data8:
9028           {
9029             int i;
9030
9031             for (i = 0; i < 8; i++)
9032               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9033                                    i == 0 ? "%s" : NULL, name);
9034             break;
9035           }
9036
9037         default:
9038           gcc_unreachable ();
9039         }
9040     }
9041
9042   FOR_EACH_CHILD (die, c, output_die (c));
9043
9044   /* Add null byte to terminate sibling list.  */
9045   if (die->die_child != NULL)
9046     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9047                          (unsigned long) die->die_offset);
9048 }
9049
9050 /* Output the compilation unit that appears at the beginning of the
9051    .debug_info section, and precedes the DIE descriptions.  */
9052
9053 static void
9054 output_compilation_unit_header (void)
9055 {
9056   int ver = dwarf_version;
9057
9058   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9059     dw2_asm_output_data (4, 0xffffffff,
9060       "Initial length escape value indicating 64-bit DWARF extension");
9061   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9062                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9063                        "Length of Compilation Unit Info");
9064   dw2_asm_output_data (2, ver, "DWARF version number");
9065   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9066                          debug_abbrev_section,
9067                          "Offset Into Abbrev. Section");
9068   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9069 }
9070
9071 /* Output the compilation unit DIE and its children.  */
9072
9073 static void
9074 output_comp_unit (dw_die_ref die, int output_if_empty)
9075 {
9076   const char *secname;
9077   char *oldsym, *tmp;
9078
9079   /* Unless we are outputting main CU, we may throw away empty ones.  */
9080   if (!output_if_empty && die->die_child == NULL)
9081     return;
9082
9083   /* Even if there are no children of this DIE, we must output the information
9084      about the compilation unit.  Otherwise, on an empty translation unit, we
9085      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9086      will then complain when examining the file.  First mark all the DIEs in
9087      this CU so we know which get local refs.  */
9088   mark_dies (die);
9089
9090   build_abbrev_table (die);
9091
9092   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9093   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9094   calc_die_sizes (die);
9095
9096   oldsym = die->die_id.die_symbol;
9097   if (oldsym)
9098     {
9099       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9100
9101       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9102       secname = tmp;
9103       die->die_id.die_symbol = NULL;
9104       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9105     }
9106   else
9107     {
9108       switch_to_section (debug_info_section);
9109       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9110       info_section_emitted = true;
9111     }
9112
9113   /* Output debugging information.  */
9114   output_compilation_unit_header ();
9115   output_die (die);
9116
9117   /* Leave the marks on the main CU, so we can check them in
9118      output_pubnames.  */
9119   if (oldsym)
9120     {
9121       unmark_dies (die);
9122       die->die_id.die_symbol = oldsym;
9123     }
9124 }
9125
9126 /* Output a comdat type unit DIE and its children.  */
9127
9128 static void
9129 output_comdat_type_unit (comdat_type_node *node)
9130 {
9131   const char *secname;
9132   char *tmp;
9133   int i;
9134 #if defined (OBJECT_FORMAT_ELF)
9135   tree comdat_key;
9136 #endif
9137
9138   /* First mark all the DIEs in this CU so we know which get local refs.  */
9139   mark_dies (node->root_die);
9140
9141   build_abbrev_table (node->root_die);
9142
9143   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9144   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9145   calc_die_sizes (node->root_die);
9146
9147 #if defined (OBJECT_FORMAT_ELF)
9148   secname = ".debug_types";
9149   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9150   sprintf (tmp, "wt.");
9151   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9152     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9153   comdat_key = get_identifier (tmp);
9154   targetm.asm_out.named_section (secname,
9155                                  SECTION_DEBUG | SECTION_LINKONCE,
9156                                  comdat_key);
9157 #else
9158   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9159   sprintf (tmp, ".gnu.linkonce.wt.");
9160   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9161     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9162   secname = tmp;
9163   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9164 #endif
9165
9166   /* Output debugging information.  */
9167   output_compilation_unit_header ();
9168   output_signature (node->signature, "Type Signature");
9169   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9170                        "Offset to Type DIE");
9171   output_die (node->root_die);
9172
9173   unmark_dies (node->root_die);
9174 }
9175
9176 /* Return the DWARF2/3 pubname associated with a decl.  */
9177
9178 static const char *
9179 dwarf2_name (tree decl, int scope)
9180 {
9181   if (DECL_NAMELESS (decl))
9182     return NULL;
9183   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9184 }
9185
9186 /* Add a new entry to .debug_pubnames if appropriate.  */
9187
9188 static void
9189 add_pubname_string (const char *str, dw_die_ref die)
9190 {
9191   if (targetm.want_debug_pub_sections)
9192     {
9193       pubname_entry e;
9194
9195       e.die = die;
9196       e.name = xstrdup (str);
9197       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9198     }
9199 }
9200
9201 static void
9202 add_pubname (tree decl, dw_die_ref die)
9203 {
9204   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
9205     {
9206       const char *name = dwarf2_name (decl, 1);
9207       if (name)
9208         add_pubname_string (name, die);
9209     }
9210 }
9211
9212 /* Add a new entry to .debug_pubtypes if appropriate.  */
9213
9214 static void
9215 add_pubtype (tree decl, dw_die_ref die)
9216 {
9217   pubname_entry e;
9218
9219   if (!targetm.want_debug_pub_sections)
9220     return;
9221
9222   e.name = NULL;
9223   if ((TREE_PUBLIC (decl)
9224        || is_cu_die (die->die_parent))
9225       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9226     {
9227       e.die = die;
9228       if (TYPE_P (decl))
9229         {
9230           if (TYPE_NAME (decl))
9231             {
9232               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9233                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9234               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9235                        && DECL_NAME (TYPE_NAME (decl)))
9236                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9237               else
9238                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9239             }
9240         }
9241       else
9242         {
9243           e.name = dwarf2_name (decl, 1);
9244           if (e.name)
9245             e.name = xstrdup (e.name);
9246         }
9247
9248       /* If we don't have a name for the type, there's no point in adding
9249          it to the table.  */
9250       if (e.name && e.name[0] != '\0')
9251         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9252     }
9253 }
9254
9255 /* Output the public names table used to speed up access to externally
9256    visible names; or the public types table used to find type definitions.  */
9257
9258 static void
9259 output_pubnames (VEC (pubname_entry, gc) * names)
9260 {
9261   unsigned i;
9262   unsigned long pubnames_length = size_of_pubnames (names);
9263   pubname_ref pub;
9264
9265   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9266     dw2_asm_output_data (4, 0xffffffff,
9267       "Initial length escape value indicating 64-bit DWARF extension");
9268   if (names == pubname_table)
9269     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9270                          "Length of Public Names Info");
9271   else
9272     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9273                          "Length of Public Type Names Info");
9274   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9275   dw2_asm_output_data (2, 2, "DWARF Version");
9276   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9277                          debug_info_section,
9278                          "Offset of Compilation Unit Info");
9279   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9280                        "Compilation Unit Length");
9281
9282   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
9283     {
9284       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9285       if (names == pubname_table)
9286         gcc_assert (pub->die->die_mark);
9287
9288       if (names != pubtype_table
9289           || pub->die->die_offset != 0
9290           || !flag_eliminate_unused_debug_types)
9291         {
9292           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9293                                "DIE offset");
9294
9295           dw2_asm_output_nstring (pub->name, -1, "external name");
9296         }
9297     }
9298
9299   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9300 }
9301
9302 /* Output the information that goes into the .debug_aranges table.
9303    Namely, define the beginning and ending address range of the
9304    text section generated for this compilation unit.  */
9305
9306 static void
9307 output_aranges (unsigned long aranges_length)
9308 {
9309   unsigned i;
9310
9311   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9312     dw2_asm_output_data (4, 0xffffffff,
9313       "Initial length escape value indicating 64-bit DWARF extension");
9314   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9315                        "Length of Address Ranges Info");
9316   /* Version number for aranges is still 2, even in DWARF3.  */
9317   dw2_asm_output_data (2, 2, "DWARF Version");
9318   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9319                          debug_info_section,
9320                          "Offset of Compilation Unit Info");
9321   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9322   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9323
9324   /* We need to align to twice the pointer size here.  */
9325   if (DWARF_ARANGES_PAD_SIZE)
9326     {
9327       /* Pad using a 2 byte words so that padding is correct for any
9328          pointer size.  */
9329       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9330                            2 * DWARF2_ADDR_SIZE);
9331       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9332         dw2_asm_output_data (2, 0, NULL);
9333     }
9334
9335   /* It is necessary not to output these entries if the sections were
9336      not used; if the sections were not used, the length will be 0 and
9337      the address may end up as 0 if the section is discarded by ld
9338      --gc-sections, leaving an invalid (0, 0) entry that can be
9339      confused with the terminator.  */
9340   if (text_section_used)
9341     {
9342       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9343       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9344                             text_section_label, "Length");
9345     }
9346   if (cold_text_section_used)
9347     {
9348       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9349                            "Address");
9350       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9351                             cold_text_section_label, "Length");
9352     }
9353
9354   if (have_multiple_function_sections)
9355     {
9356       unsigned fde_idx = 0;
9357
9358       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
9359         {
9360           dw_fde_ref fde = &fde_table[fde_idx];
9361
9362           if (!fde->in_std_section)
9363             {
9364               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9365                                    "Address");
9366               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9367                                     fde->dw_fde_begin, "Length");
9368             }
9369           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9370             {
9371               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9372                                    "Address");
9373               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9374                                     fde->dw_fde_second_begin, "Length");
9375             }
9376         }
9377     }
9378
9379   /* Output the terminator words.  */
9380   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9381   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9382 }
9383
9384 /* Add a new entry to .debug_ranges.  Return the offset at which it
9385    was placed.  */
9386
9387 static unsigned int
9388 add_ranges_num (int num)
9389 {
9390   unsigned int in_use = ranges_table_in_use;
9391
9392   if (in_use == ranges_table_allocated)
9393     {
9394       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9395       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9396                                     ranges_table_allocated);
9397       memset (ranges_table + ranges_table_in_use, 0,
9398               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9399     }
9400
9401   ranges_table[in_use].num = num;
9402   ranges_table_in_use = in_use + 1;
9403
9404   return in_use * 2 * DWARF2_ADDR_SIZE;
9405 }
9406
9407 /* Add a new entry to .debug_ranges corresponding to a block, or a
9408    range terminator if BLOCK is NULL.  */
9409
9410 static unsigned int
9411 add_ranges (const_tree block)
9412 {
9413   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9414 }
9415
9416 /* Add a new entry to .debug_ranges corresponding to a pair of
9417    labels.  */
9418
9419 static void
9420 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9421                       bool *added)
9422 {
9423   unsigned int in_use = ranges_by_label_in_use;
9424   unsigned int offset;
9425
9426   if (in_use == ranges_by_label_allocated)
9427     {
9428       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9429       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9430                                        ranges_by_label,
9431                                        ranges_by_label_allocated);
9432       memset (ranges_by_label + ranges_by_label_in_use, 0,
9433               RANGES_TABLE_INCREMENT
9434               * sizeof (struct dw_ranges_by_label_struct));
9435     }
9436
9437   ranges_by_label[in_use].begin = begin;
9438   ranges_by_label[in_use].end = end;
9439   ranges_by_label_in_use = in_use + 1;
9440
9441   offset = add_ranges_num (-(int)in_use - 1);
9442   if (!*added)
9443     {
9444       add_AT_range_list (die, DW_AT_ranges, offset);
9445       *added = true;
9446     }
9447 }
9448
9449 static void
9450 output_ranges (void)
9451 {
9452   unsigned i;
9453   static const char *const start_fmt = "Offset %#x";
9454   const char *fmt = start_fmt;
9455
9456   for (i = 0; i < ranges_table_in_use; i++)
9457     {
9458       int block_num = ranges_table[i].num;
9459
9460       if (block_num > 0)
9461         {
9462           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9463           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9464
9465           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9466           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9467
9468           /* If all code is in the text section, then the compilation
9469              unit base address defaults to DW_AT_low_pc, which is the
9470              base of the text section.  */
9471           if (!have_multiple_function_sections)
9472             {
9473               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9474                                     text_section_label,
9475                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9476               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9477                                     text_section_label, NULL);
9478             }
9479
9480           /* Otherwise, the compilation unit base address is zero,
9481              which allows us to use absolute addresses, and not worry
9482              about whether the target supports cross-section
9483              arithmetic.  */
9484           else
9485             {
9486               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9487                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9488               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9489             }
9490
9491           fmt = NULL;
9492         }
9493
9494       /* Negative block_num stands for an index into ranges_by_label.  */
9495       else if (block_num < 0)
9496         {
9497           int lab_idx = - block_num - 1;
9498
9499           if (!have_multiple_function_sections)
9500             {
9501               gcc_unreachable ();
9502 #if 0
9503               /* If we ever use add_ranges_by_labels () for a single
9504                  function section, all we have to do is to take out
9505                  the #if 0 above.  */
9506               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9507                                     ranges_by_label[lab_idx].begin,
9508                                     text_section_label,
9509                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9510               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9511                                     ranges_by_label[lab_idx].end,
9512                                     text_section_label, NULL);
9513 #endif
9514             }
9515           else
9516             {
9517               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9518                                    ranges_by_label[lab_idx].begin,
9519                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9520               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9521                                    ranges_by_label[lab_idx].end,
9522                                    NULL);
9523             }
9524         }
9525       else
9526         {
9527           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9528           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9529           fmt = start_fmt;
9530         }
9531     }
9532 }
9533
9534 /* Data structure containing information about input files.  */
9535 struct file_info
9536 {
9537   const char *path;     /* Complete file name.  */
9538   const char *fname;    /* File name part.  */
9539   int length;           /* Length of entire string.  */
9540   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9541   int dir_idx;          /* Index in directory table.  */
9542 };
9543
9544 /* Data structure containing information about directories with source
9545    files.  */
9546 struct dir_info
9547 {
9548   const char *path;     /* Path including directory name.  */
9549   int length;           /* Path length.  */
9550   int prefix;           /* Index of directory entry which is a prefix.  */
9551   int count;            /* Number of files in this directory.  */
9552   int dir_idx;          /* Index of directory used as base.  */
9553 };
9554
9555 /* Callback function for file_info comparison.  We sort by looking at
9556    the directories in the path.  */
9557
9558 static int
9559 file_info_cmp (const void *p1, const void *p2)
9560 {
9561   const struct file_info *const s1 = (const struct file_info *) p1;
9562   const struct file_info *const s2 = (const struct file_info *) p2;
9563   const unsigned char *cp1;
9564   const unsigned char *cp2;
9565
9566   /* Take care of file names without directories.  We need to make sure that
9567      we return consistent values to qsort since some will get confused if
9568      we return the same value when identical operands are passed in opposite
9569      orders.  So if neither has a directory, return 0 and otherwise return
9570      1 or -1 depending on which one has the directory.  */
9571   if ((s1->path == s1->fname || s2->path == s2->fname))
9572     return (s2->path == s2->fname) - (s1->path == s1->fname);
9573
9574   cp1 = (const unsigned char *) s1->path;
9575   cp2 = (const unsigned char *) s2->path;
9576
9577   while (1)
9578     {
9579       ++cp1;
9580       ++cp2;
9581       /* Reached the end of the first path?  If so, handle like above.  */
9582       if ((cp1 == (const unsigned char *) s1->fname)
9583           || (cp2 == (const unsigned char *) s2->fname))
9584         return ((cp2 == (const unsigned char *) s2->fname)
9585                 - (cp1 == (const unsigned char *) s1->fname));
9586
9587       /* Character of current path component the same?  */
9588       else if (*cp1 != *cp2)
9589         return *cp1 - *cp2;
9590     }
9591 }
9592
9593 struct file_name_acquire_data
9594 {
9595   struct file_info *files;
9596   int used_files;
9597   int max_files;
9598 };
9599
9600 /* Traversal function for the hash table.  */
9601
9602 static int
9603 file_name_acquire (void ** slot, void *data)
9604 {
9605   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9606   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9607   struct file_info *fi;
9608   const char *f;
9609
9610   gcc_assert (fnad->max_files >= d->emitted_number);
9611
9612   if (! d->emitted_number)
9613     return 1;
9614
9615   gcc_assert (fnad->max_files != fnad->used_files);
9616
9617   fi = fnad->files + fnad->used_files++;
9618
9619   /* Skip all leading "./".  */
9620   f = d->filename;
9621   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9622     f += 2;
9623
9624   /* Create a new array entry.  */
9625   fi->path = f;
9626   fi->length = strlen (f);
9627   fi->file_idx = d;
9628
9629   /* Search for the file name part.  */
9630   f = strrchr (f, DIR_SEPARATOR);
9631 #if defined (DIR_SEPARATOR_2)
9632   {
9633     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9634
9635     if (g != NULL)
9636       {
9637         if (f == NULL || f < g)
9638           f = g;
9639       }
9640   }
9641 #endif
9642
9643   fi->fname = f == NULL ? fi->path : f + 1;
9644   return 1;
9645 }
9646
9647 /* Output the directory table and the file name table.  We try to minimize
9648    the total amount of memory needed.  A heuristic is used to avoid large
9649    slowdowns with many input files.  */
9650
9651 static void
9652 output_file_names (void)
9653 {
9654   struct file_name_acquire_data fnad;
9655   int numfiles;
9656   struct file_info *files;
9657   struct dir_info *dirs;
9658   int *saved;
9659   int *savehere;
9660   int *backmap;
9661   int ndirs;
9662   int idx_offset;
9663   int i;
9664
9665   if (!last_emitted_file)
9666     {
9667       dw2_asm_output_data (1, 0, "End directory table");
9668       dw2_asm_output_data (1, 0, "End file name table");
9669       return;
9670     }
9671
9672   numfiles = last_emitted_file->emitted_number;
9673
9674   /* Allocate the various arrays we need.  */
9675   files = XALLOCAVEC (struct file_info, numfiles);
9676   dirs = XALLOCAVEC (struct dir_info, numfiles);
9677
9678   fnad.files = files;
9679   fnad.used_files = 0;
9680   fnad.max_files = numfiles;
9681   htab_traverse (file_table, file_name_acquire, &fnad);
9682   gcc_assert (fnad.used_files == fnad.max_files);
9683
9684   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9685
9686   /* Find all the different directories used.  */
9687   dirs[0].path = files[0].path;
9688   dirs[0].length = files[0].fname - files[0].path;
9689   dirs[0].prefix = -1;
9690   dirs[0].count = 1;
9691   dirs[0].dir_idx = 0;
9692   files[0].dir_idx = 0;
9693   ndirs = 1;
9694
9695   for (i = 1; i < numfiles; i++)
9696     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9697         && memcmp (dirs[ndirs - 1].path, files[i].path,
9698                    dirs[ndirs - 1].length) == 0)
9699       {
9700         /* Same directory as last entry.  */
9701         files[i].dir_idx = ndirs - 1;
9702         ++dirs[ndirs - 1].count;
9703       }
9704     else
9705       {
9706         int j;
9707
9708         /* This is a new directory.  */
9709         dirs[ndirs].path = files[i].path;
9710         dirs[ndirs].length = files[i].fname - files[i].path;
9711         dirs[ndirs].count = 1;
9712         dirs[ndirs].dir_idx = ndirs;
9713         files[i].dir_idx = ndirs;
9714
9715         /* Search for a prefix.  */
9716         dirs[ndirs].prefix = -1;
9717         for (j = 0; j < ndirs; j++)
9718           if (dirs[j].length < dirs[ndirs].length
9719               && dirs[j].length > 1
9720               && (dirs[ndirs].prefix == -1
9721                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9722               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9723             dirs[ndirs].prefix = j;
9724
9725         ++ndirs;
9726       }
9727
9728   /* Now to the actual work.  We have to find a subset of the directories which
9729      allow expressing the file name using references to the directory table
9730      with the least amount of characters.  We do not do an exhaustive search
9731      where we would have to check out every combination of every single
9732      possible prefix.  Instead we use a heuristic which provides nearly optimal
9733      results in most cases and never is much off.  */
9734   saved = XALLOCAVEC (int, ndirs);
9735   savehere = XALLOCAVEC (int, ndirs);
9736
9737   memset (saved, '\0', ndirs * sizeof (saved[0]));
9738   for (i = 0; i < ndirs; i++)
9739     {
9740       int j;
9741       int total;
9742
9743       /* We can always save some space for the current directory.  But this
9744          does not mean it will be enough to justify adding the directory.  */
9745       savehere[i] = dirs[i].length;
9746       total = (savehere[i] - saved[i]) * dirs[i].count;
9747
9748       for (j = i + 1; j < ndirs; j++)
9749         {
9750           savehere[j] = 0;
9751           if (saved[j] < dirs[i].length)
9752             {
9753               /* Determine whether the dirs[i] path is a prefix of the
9754                  dirs[j] path.  */
9755               int k;
9756
9757               k = dirs[j].prefix;
9758               while (k != -1 && k != (int) i)
9759                 k = dirs[k].prefix;
9760
9761               if (k == (int) i)
9762                 {
9763                   /* Yes it is.  We can possibly save some memory by
9764                      writing the filenames in dirs[j] relative to
9765                      dirs[i].  */
9766                   savehere[j] = dirs[i].length;
9767                   total += (savehere[j] - saved[j]) * dirs[j].count;
9768                 }
9769             }
9770         }
9771
9772       /* Check whether we can save enough to justify adding the dirs[i]
9773          directory.  */
9774       if (total > dirs[i].length + 1)
9775         {
9776           /* It's worthwhile adding.  */
9777           for (j = i; j < ndirs; j++)
9778             if (savehere[j] > 0)
9779               {
9780                 /* Remember how much we saved for this directory so far.  */
9781                 saved[j] = savehere[j];
9782
9783                 /* Remember the prefix directory.  */
9784                 dirs[j].dir_idx = i;
9785               }
9786         }
9787     }
9788
9789   /* Emit the directory name table.  */
9790   idx_offset = dirs[0].length > 0 ? 1 : 0;
9791   for (i = 1 - idx_offset; i < ndirs; i++)
9792     dw2_asm_output_nstring (dirs[i].path,
9793                             dirs[i].length
9794                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9795                             "Directory Entry: %#x", i + idx_offset);
9796
9797   dw2_asm_output_data (1, 0, "End directory table");
9798
9799   /* We have to emit them in the order of emitted_number since that's
9800      used in the debug info generation.  To do this efficiently we
9801      generate a back-mapping of the indices first.  */
9802   backmap = XALLOCAVEC (int, numfiles);
9803   for (i = 0; i < numfiles; i++)
9804     backmap[files[i].file_idx->emitted_number - 1] = i;
9805
9806   /* Now write all the file names.  */
9807   for (i = 0; i < numfiles; i++)
9808     {
9809       int file_idx = backmap[i];
9810       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9811
9812 #ifdef VMS_DEBUGGING_INFO
9813 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9814
9815       /* Setting these fields can lead to debugger miscomparisons,
9816          but VMS Debug requires them to be set correctly.  */
9817
9818       int ver;
9819       long long cdt;
9820       long siz;
9821       int maxfilelen = strlen (files[file_idx].path)
9822                                + dirs[dir_idx].length
9823                                + MAX_VMS_VERSION_LEN + 1;
9824       char *filebuf = XALLOCAVEC (char, maxfilelen);
9825
9826       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9827       snprintf (filebuf, maxfilelen, "%s;%d",
9828                 files[file_idx].path + dirs[dir_idx].length, ver);
9829
9830       dw2_asm_output_nstring
9831         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9832
9833       /* Include directory index.  */
9834       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9835
9836       /* Modification time.  */
9837       dw2_asm_output_data_uleb128
9838         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9839           ? cdt : 0,
9840          NULL);
9841
9842       /* File length in bytes.  */
9843       dw2_asm_output_data_uleb128
9844         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9845           ? siz : 0,
9846          NULL);
9847 #else
9848       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9849                               "File Entry: %#x", (unsigned) i + 1);
9850
9851       /* Include directory index.  */
9852       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9853
9854       /* Modification time.  */
9855       dw2_asm_output_data_uleb128 (0, NULL);
9856
9857       /* File length in bytes.  */
9858       dw2_asm_output_data_uleb128 (0, NULL);
9859 #endif /* VMS_DEBUGGING_INFO */
9860     }
9861
9862   dw2_asm_output_data (1, 0, "End file name table");
9863 }
9864
9865
9866 /* Output one line number table into the .debug_line section.  */
9867
9868 static void
9869 output_one_line_info_table (dw_line_info_table *table)
9870 {
9871   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9872   unsigned int current_line = 1;
9873   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9874   dw_line_info_entry *ent;
9875   size_t i;
9876
9877   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9878     {
9879       switch (ent->opcode)
9880         {
9881         case LI_set_address:
9882           /* ??? Unfortunately, we have little choice here currently, and
9883              must always use the most general form.  GCC does not know the
9884              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9885              ports do have length attributes which will give an upper bound
9886              on the address range.  We could perhaps use length attributes
9887              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9888           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9889
9890           /* This can handle any delta.  This takes
9891              4+DWARF2_ADDR_SIZE bytes.  */
9892           dw2_asm_output_data (1, 0, "set address %s", line_label);
9893           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9894           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9895           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9896           break;
9897
9898         case LI_set_line:
9899           if (ent->val == current_line)
9900             {
9901               /* We still need to start a new row, so output a copy insn.  */
9902               dw2_asm_output_data (1, DW_LNS_copy,
9903                                    "copy line %u", current_line);
9904             }
9905           else
9906             {
9907               int line_offset = ent->val - current_line;
9908               int line_delta = line_offset - DWARF_LINE_BASE;
9909
9910               current_line = ent->val;
9911               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9912                 {
9913                   /* This can handle deltas from -10 to 234, using the current
9914                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9915                      This takes 1 byte.  */
9916                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9917                                        "line %u", current_line);
9918                 }
9919               else
9920                 {
9921                   /* This can handle any delta.  This takes at least 4 bytes,
9922                      depending on the value being encoded.  */
9923                   dw2_asm_output_data (1, DW_LNS_advance_line,
9924                                        "advance to line %u", current_line);
9925                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9926                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9927                 }
9928             }
9929           break;
9930
9931         case LI_set_file:
9932           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9933           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9934           break;
9935
9936         case LI_set_column:
9937           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9938           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9939           break;
9940
9941         case LI_negate_stmt:
9942           current_is_stmt = !current_is_stmt;
9943           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9944                                "is_stmt %d", current_is_stmt);
9945           break;
9946
9947         case LI_set_prologue_end:
9948           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9949                                "set prologue end");
9950           break;
9951           
9952         case LI_set_epilogue_begin:
9953           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9954                                "set epilogue begin");
9955           break;
9956
9957         case LI_set_discriminator:
9958           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9959           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9960           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9961           dw2_asm_output_data_uleb128 (ent->val, NULL);
9962           break;
9963         }
9964     }
9965
9966   /* Emit debug info for the address of the end of the table.  */
9967   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9968   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9969   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9970   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9971
9972   dw2_asm_output_data (1, 0, "end sequence");
9973   dw2_asm_output_data_uleb128 (1, NULL);
9974   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9975 }
9976
9977 /* Output the source line number correspondence information.  This
9978    information goes into the .debug_line section.  */
9979
9980 static void
9981 output_line_info (void)
9982 {
9983   char l1[20], l2[20], p1[20], p2[20];
9984   int ver = dwarf_version;
9985   bool saw_one = false;
9986   int opc;
9987
9988   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9989   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9990   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9991   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9992
9993   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9994     dw2_asm_output_data (4, 0xffffffff,
9995       "Initial length escape value indicating 64-bit DWARF extension");
9996   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9997                         "Length of Source Line Info");
9998   ASM_OUTPUT_LABEL (asm_out_file, l1);
9999
10000   dw2_asm_output_data (2, ver, "DWARF Version");
10001   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
10002   ASM_OUTPUT_LABEL (asm_out_file, p1);
10003
10004   /* Define the architecture-dependent minimum instruction length (in bytes).
10005      In this implementation of DWARF, this field is used for information
10006      purposes only.  Since GCC generates assembly language, we have no
10007      a priori knowledge of how many instruction bytes are generated for each
10008      source line, and therefore can use only the DW_LNE_set_address and
10009      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10010      this as '1', which is "correct enough" for all architectures,
10011      and don't let the target override.  */
10012   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10013
10014   if (ver >= 4)
10015     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10016                          "Maximum Operations Per Instruction");
10017   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10018                        "Default is_stmt_start flag");
10019   dw2_asm_output_data (1, DWARF_LINE_BASE,
10020                        "Line Base Value (Special Opcodes)");
10021   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10022                        "Line Range Value (Special Opcodes)");
10023   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10024                        "Special Opcode Base");
10025
10026   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10027     {
10028       int n_op_args;
10029       switch (opc)
10030         {
10031         case DW_LNS_advance_pc:
10032         case DW_LNS_advance_line:
10033         case DW_LNS_set_file:
10034         case DW_LNS_set_column:
10035         case DW_LNS_fixed_advance_pc:
10036         case DW_LNS_set_isa:
10037           n_op_args = 1;
10038           break;
10039         default:
10040           n_op_args = 0;
10041           break;
10042         }
10043
10044       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10045                            opc, n_op_args);
10046     }
10047
10048   /* Write out the information about the files we use.  */
10049   output_file_names ();
10050   ASM_OUTPUT_LABEL (asm_out_file, p2);
10051
10052   if (separate_line_info)
10053     {
10054       dw_line_info_table *table;
10055       size_t i;
10056
10057       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
10058         if (table->in_use)
10059           {
10060             output_one_line_info_table (table);
10061             saw_one = true;
10062           }
10063     }
10064   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10065     {
10066       output_one_line_info_table (cold_text_section_line_info);
10067       saw_one = true;
10068     }
10069
10070   /* ??? Some Darwin linkers crash on a .debug_line section with no
10071      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10072      sufficient -- the address column must also be initialized.
10073      Make sure to output at least one set_address/end_sequence pair,
10074      choosing .text since that section is always present.  */
10075   if (text_section_line_info->in_use || !saw_one)
10076     output_one_line_info_table (text_section_line_info);
10077
10078   /* Output the marker for the end of the line number info.  */
10079   ASM_OUTPUT_LABEL (asm_out_file, l2);
10080 }
10081 \f
10082 /* Given a pointer to a tree node for some base type, return a pointer to
10083    a DIE that describes the given type.
10084
10085    This routine must only be called for GCC type nodes that correspond to
10086    Dwarf base (fundamental) types.  */
10087
10088 static dw_die_ref
10089 base_type_die (tree type)
10090 {
10091   dw_die_ref base_type_result;
10092   enum dwarf_type encoding;
10093
10094   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10095     return 0;
10096
10097   /* If this is a subtype that should not be emitted as a subrange type,
10098      use the base type.  See subrange_type_for_debug_p.  */
10099   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10100     type = TREE_TYPE (type);
10101
10102   switch (TREE_CODE (type))
10103     {
10104     case INTEGER_TYPE:
10105       if ((dwarf_version >= 4 || !dwarf_strict)
10106           && TYPE_NAME (type)
10107           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10108           && DECL_IS_BUILTIN (TYPE_NAME (type))
10109           && DECL_NAME (TYPE_NAME (type)))
10110         {
10111           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10112           if (strcmp (name, "char16_t") == 0
10113               || strcmp (name, "char32_t") == 0)
10114             {
10115               encoding = DW_ATE_UTF;
10116               break;
10117             }
10118         }
10119       if (TYPE_STRING_FLAG (type))
10120         {
10121           if (TYPE_UNSIGNED (type))
10122             encoding = DW_ATE_unsigned_char;
10123           else
10124             encoding = DW_ATE_signed_char;
10125         }
10126       else if (TYPE_UNSIGNED (type))
10127         encoding = DW_ATE_unsigned;
10128       else
10129         encoding = DW_ATE_signed;
10130       break;
10131
10132     case REAL_TYPE:
10133       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10134         {
10135           if (dwarf_version >= 3 || !dwarf_strict)
10136             encoding = DW_ATE_decimal_float;
10137           else
10138             encoding = DW_ATE_lo_user;
10139         }
10140       else
10141         encoding = DW_ATE_float;
10142       break;
10143
10144     case FIXED_POINT_TYPE:
10145       if (!(dwarf_version >= 3 || !dwarf_strict))
10146         encoding = DW_ATE_lo_user;
10147       else if (TYPE_UNSIGNED (type))
10148         encoding = DW_ATE_unsigned_fixed;
10149       else
10150         encoding = DW_ATE_signed_fixed;
10151       break;
10152
10153       /* Dwarf2 doesn't know anything about complex ints, so use
10154          a user defined type for it.  */
10155     case COMPLEX_TYPE:
10156       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10157         encoding = DW_ATE_complex_float;
10158       else
10159         encoding = DW_ATE_lo_user;
10160       break;
10161
10162     case BOOLEAN_TYPE:
10163       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10164       encoding = DW_ATE_boolean;
10165       break;
10166
10167     default:
10168       /* No other TREE_CODEs are Dwarf fundamental types.  */
10169       gcc_unreachable ();
10170     }
10171
10172   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10173
10174   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10175                    int_size_in_bytes (type));
10176   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10177
10178   return base_type_result;
10179 }
10180
10181 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10182    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10183
10184 static inline int
10185 is_base_type (tree type)
10186 {
10187   switch (TREE_CODE (type))
10188     {
10189     case ERROR_MARK:
10190     case VOID_TYPE:
10191     case INTEGER_TYPE:
10192     case REAL_TYPE:
10193     case FIXED_POINT_TYPE:
10194     case COMPLEX_TYPE:
10195     case BOOLEAN_TYPE:
10196       return 1;
10197
10198     case ARRAY_TYPE:
10199     case RECORD_TYPE:
10200     case UNION_TYPE:
10201     case QUAL_UNION_TYPE:
10202     case ENUMERAL_TYPE:
10203     case FUNCTION_TYPE:
10204     case METHOD_TYPE:
10205     case POINTER_TYPE:
10206     case REFERENCE_TYPE:
10207     case NULLPTR_TYPE:
10208     case OFFSET_TYPE:
10209     case LANG_TYPE:
10210     case VECTOR_TYPE:
10211       return 0;
10212
10213     default:
10214       gcc_unreachable ();
10215     }
10216
10217   return 0;
10218 }
10219
10220 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10221    node, return the size in bits for the type if it is a constant, or else
10222    return the alignment for the type if the type's size is not constant, or
10223    else return BITS_PER_WORD if the type actually turns out to be an
10224    ERROR_MARK node.  */
10225
10226 static inline unsigned HOST_WIDE_INT
10227 simple_type_size_in_bits (const_tree type)
10228 {
10229   if (TREE_CODE (type) == ERROR_MARK)
10230     return BITS_PER_WORD;
10231   else if (TYPE_SIZE (type) == NULL_TREE)
10232     return 0;
10233   else if (host_integerp (TYPE_SIZE (type), 1))
10234     return tree_low_cst (TYPE_SIZE (type), 1);
10235   else
10236     return TYPE_ALIGN (type);
10237 }
10238
10239 /* Similarly, but return a double_int instead of UHWI.  */
10240
10241 static inline double_int
10242 double_int_type_size_in_bits (const_tree type)
10243 {
10244   if (TREE_CODE (type) == ERROR_MARK)
10245     return uhwi_to_double_int (BITS_PER_WORD);
10246   else if (TYPE_SIZE (type) == NULL_TREE)
10247     return double_int_zero;
10248   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10249     return tree_to_double_int (TYPE_SIZE (type));
10250   else
10251     return uhwi_to_double_int (TYPE_ALIGN (type));
10252 }
10253
10254 /*  Given a pointer to a tree node for a subrange type, return a pointer
10255     to a DIE that describes the given type.  */
10256
10257 static dw_die_ref
10258 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10259 {
10260   dw_die_ref subrange_die;
10261   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10262
10263   if (context_die == NULL)
10264     context_die = comp_unit_die ();
10265
10266   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10267
10268   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10269     {
10270       /* The size of the subrange type and its base type do not match,
10271          so we need to generate a size attribute for the subrange type.  */
10272       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10273     }
10274
10275   if (low)
10276     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10277   if (high)
10278     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10279
10280   return subrange_die;
10281 }
10282
10283 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10284    entry that chains various modifiers in front of the given type.  */
10285
10286 static dw_die_ref
10287 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10288                    dw_die_ref context_die)
10289 {
10290   enum tree_code code = TREE_CODE (type);
10291   dw_die_ref mod_type_die;
10292   dw_die_ref sub_die = NULL;
10293   tree item_type = NULL;
10294   tree qualified_type;
10295   tree name, low, high;
10296
10297   if (code == ERROR_MARK)
10298     return NULL;
10299
10300   /* See if we already have the appropriately qualified variant of
10301      this type.  */
10302   qualified_type
10303     = get_qualified_type (type,
10304                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10305                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10306
10307   if (qualified_type == sizetype
10308       && TYPE_NAME (qualified_type)
10309       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10310     {
10311       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10312
10313       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10314                            && TYPE_PRECISION (t)
10315                            == TYPE_PRECISION (qualified_type)
10316                            && TYPE_UNSIGNED (t)
10317                            == TYPE_UNSIGNED (qualified_type));
10318       qualified_type = t;
10319     }
10320
10321   /* If we do, then we can just use its DIE, if it exists.  */
10322   if (qualified_type)
10323     {
10324       mod_type_die = lookup_type_die (qualified_type);
10325       if (mod_type_die)
10326         return mod_type_die;
10327     }
10328
10329   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10330
10331   /* Handle C typedef types.  */
10332   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10333       && !DECL_ARTIFICIAL (name))
10334     {
10335       tree dtype = TREE_TYPE (name);
10336
10337       if (qualified_type == dtype)
10338         {
10339           /* For a named type, use the typedef.  */
10340           gen_type_die (qualified_type, context_die);
10341           return lookup_type_die (qualified_type);
10342         }
10343       else if (is_const_type < TYPE_READONLY (dtype)
10344                || is_volatile_type < TYPE_VOLATILE (dtype)
10345                || (is_const_type <= TYPE_READONLY (dtype)
10346                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10347                    && DECL_ORIGINAL_TYPE (name) != type))
10348         /* cv-unqualified version of named type.  Just use the unnamed
10349            type to which it refers.  */
10350         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10351                                   is_const_type, is_volatile_type,
10352                                   context_die);
10353       /* Else cv-qualified version of named type; fall through.  */
10354     }
10355
10356   if (is_const_type
10357       /* If both is_const_type and is_volatile_type, prefer the path
10358          which leads to a qualified type.  */
10359       && (!is_volatile_type
10360           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10361           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10362     {
10363       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
10364       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10365     }
10366   else if (is_volatile_type)
10367     {
10368       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
10369       sub_die = modified_type_die (type, is_const_type, 0, context_die);
10370     }
10371   else if (code == POINTER_TYPE)
10372     {
10373       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
10374       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10375                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10376       item_type = TREE_TYPE (type);
10377       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10378         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10379                          TYPE_ADDR_SPACE (item_type));
10380     }
10381   else if (code == REFERENCE_TYPE)
10382     {
10383       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10384         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
10385                                 type);
10386       else
10387         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
10388       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10389                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10390       item_type = TREE_TYPE (type);
10391       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10392         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10393                          TYPE_ADDR_SPACE (item_type));
10394     }
10395   else if (code == INTEGER_TYPE
10396            && TREE_TYPE (type) != NULL_TREE
10397            && subrange_type_for_debug_p (type, &low, &high))
10398     {
10399       mod_type_die = subrange_type_die (type, low, high, context_die);
10400       item_type = TREE_TYPE (type);
10401     }
10402   else if (is_base_type (type))
10403     mod_type_die = base_type_die (type);
10404   else
10405     {
10406       gen_type_die (type, context_die);
10407
10408       /* We have to get the type_main_variant here (and pass that to the
10409          `lookup_type_die' routine) because the ..._TYPE node we have
10410          might simply be a *copy* of some original type node (where the
10411          copy was created to help us keep track of typedef names) and
10412          that copy might have a different TYPE_UID from the original
10413          ..._TYPE node.  */
10414       if (TREE_CODE (type) != VECTOR_TYPE)
10415         return lookup_type_die (type_main_variant (type));
10416       else
10417         /* Vectors have the debugging information in the type,
10418            not the main variant.  */
10419         return lookup_type_die (type);
10420     }
10421
10422   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10423      don't output a DW_TAG_typedef, since there isn't one in the
10424      user's program; just attach a DW_AT_name to the type.
10425      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10426      if the base type already has the same name.  */
10427   if (name
10428       && ((TREE_CODE (name) != TYPE_DECL
10429            && (qualified_type == TYPE_MAIN_VARIANT (type)
10430                || (!is_const_type && !is_volatile_type)))
10431           || (TREE_CODE (name) == TYPE_DECL
10432               && TREE_TYPE (name) == qualified_type
10433               && DECL_NAME (name))))
10434     {
10435       if (TREE_CODE (name) == TYPE_DECL)
10436         /* Could just call add_name_and_src_coords_attributes here,
10437            but since this is a builtin type it doesn't have any
10438            useful source coordinates anyway.  */
10439         name = DECL_NAME (name);
10440       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10441       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10442       if (TYPE_ARTIFICIAL (type))
10443         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10444     }
10445   /* This probably indicates a bug.  */
10446   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10447     add_name_attribute (mod_type_die, "__unknown__");
10448
10449   if (qualified_type)
10450     equate_type_number_to_die (qualified_type, mod_type_die);
10451
10452   if (item_type)
10453     /* We must do this after the equate_type_number_to_die call, in case
10454        this is a recursive type.  This ensures that the modified_type_die
10455        recursion will terminate even if the type is recursive.  Recursive
10456        types are possible in Ada.  */
10457     sub_die = modified_type_die (item_type,
10458                                  TYPE_READONLY (item_type),
10459                                  TYPE_VOLATILE (item_type),
10460                                  context_die);
10461
10462   if (sub_die != NULL)
10463     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10464
10465   return mod_type_die;
10466 }
10467
10468 /* Generate DIEs for the generic parameters of T.
10469    T must be either a generic type or a generic function.
10470    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10471
10472 static void
10473 gen_generic_params_dies (tree t)
10474 {
10475   tree parms, args;
10476   int parms_num, i;
10477   dw_die_ref die = NULL;
10478
10479   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10480     return;
10481
10482   if (TYPE_P (t))
10483     die = lookup_type_die (t);
10484   else if (DECL_P (t))
10485     die = lookup_decl_die (t);
10486
10487   gcc_assert (die);
10488
10489   parms = lang_hooks.get_innermost_generic_parms (t);
10490   if (!parms)
10491     /* T has no generic parameter. It means T is neither a generic type
10492        or function. End of story.  */
10493     return;
10494
10495   parms_num = TREE_VEC_LENGTH (parms);
10496   args = lang_hooks.get_innermost_generic_args (t);
10497   for (i = 0; i < parms_num; i++)
10498     {
10499       tree parm, arg, arg_pack_elems;
10500
10501       parm = TREE_VEC_ELT (parms, i);
10502       arg = TREE_VEC_ELT (args, i);
10503       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10504       gcc_assert (parm && TREE_VALUE (parm) && arg);
10505
10506       if (parm && TREE_VALUE (parm) && arg)
10507         {
10508           /* If PARM represents a template parameter pack,
10509              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10510              by DW_TAG_template_*_parameter DIEs for the argument
10511              pack elements of ARG. Note that ARG would then be
10512              an argument pack.  */
10513           if (arg_pack_elems)
10514             template_parameter_pack_die (TREE_VALUE (parm),
10515                                          arg_pack_elems,
10516                                          die);
10517           else
10518             generic_parameter_die (TREE_VALUE (parm), arg,
10519                                    true /* Emit DW_AT_name */, die);
10520         }
10521     }
10522 }
10523
10524 /* Create and return a DIE for PARM which should be
10525    the representation of a generic type parameter.
10526    For instance, in the C++ front end, PARM would be a template parameter.
10527    ARG is the argument to PARM.
10528    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10529    name of the PARM.
10530    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10531    as a child node.  */
10532
10533 static dw_die_ref
10534 generic_parameter_die (tree parm, tree arg,
10535                        bool emit_name_p,
10536                        dw_die_ref parent_die)
10537 {
10538   dw_die_ref tmpl_die = NULL;
10539   const char *name = NULL;
10540
10541   if (!parm || !DECL_NAME (parm) || !arg)
10542     return NULL;
10543
10544   /* We support non-type generic parameters and arguments,
10545      type generic parameters and arguments, as well as
10546      generic generic parameters (a.k.a. template template parameters in C++)
10547      and arguments.  */
10548   if (TREE_CODE (parm) == PARM_DECL)
10549     /* PARM is a nontype generic parameter  */
10550     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10551   else if (TREE_CODE (parm) == TYPE_DECL)
10552     /* PARM is a type generic parameter.  */
10553     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10554   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10555     /* PARM is a generic generic parameter.
10556        Its DIE is a GNU extension. It shall have a
10557        DW_AT_name attribute to represent the name of the template template
10558        parameter, and a DW_AT_GNU_template_name attribute to represent the
10559        name of the template template argument.  */
10560     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10561                         parent_die, parm);
10562   else
10563     gcc_unreachable ();
10564
10565   if (tmpl_die)
10566     {
10567       tree tmpl_type;
10568
10569       /* If PARM is a generic parameter pack, it means we are
10570          emitting debug info for a template argument pack element.
10571          In other terms, ARG is a template argument pack element.
10572          In that case, we don't emit any DW_AT_name attribute for
10573          the die.  */
10574       if (emit_name_p)
10575         {
10576           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10577           gcc_assert (name);
10578           add_AT_string (tmpl_die, DW_AT_name, name);
10579         }
10580
10581       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10582         {
10583           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10584              TMPL_DIE should have a child DW_AT_type attribute that is set
10585              to the type of the argument to PARM, which is ARG.
10586              If PARM is a type generic parameter, TMPL_DIE should have a
10587              child DW_AT_type that is set to ARG.  */
10588           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10589           add_type_attribute (tmpl_die, tmpl_type, 0,
10590                               TREE_THIS_VOLATILE (tmpl_type),
10591                               parent_die);
10592         }
10593       else
10594         {
10595           /* So TMPL_DIE is a DIE representing a
10596              a generic generic template parameter, a.k.a template template
10597              parameter in C++ and arg is a template.  */
10598
10599           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10600              to the name of the argument.  */
10601           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10602           if (name)
10603             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10604         }
10605
10606       if (TREE_CODE (parm) == PARM_DECL)
10607         /* So PARM is a non-type generic parameter.
10608            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10609            attribute of TMPL_DIE which value represents the value
10610            of ARG.
10611            We must be careful here:
10612            The value of ARG might reference some function decls.
10613            We might currently be emitting debug info for a generic
10614            type and types are emitted before function decls, we don't
10615            know if the function decls referenced by ARG will actually be
10616            emitted after cgraph computations.
10617            So must defer the generation of the DW_AT_const_value to
10618            after cgraph is ready.  */
10619         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10620     }
10621
10622   return tmpl_die;
10623 }
10624
10625 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10626    PARM_PACK must be a template parameter pack. The returned DIE
10627    will be child DIE of PARENT_DIE.  */
10628
10629 static dw_die_ref
10630 template_parameter_pack_die (tree parm_pack,
10631                              tree parm_pack_args,
10632                              dw_die_ref parent_die)
10633 {
10634   dw_die_ref die;
10635   int j;
10636
10637   gcc_assert (parent_die && parm_pack);
10638
10639   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10640   add_name_and_src_coords_attributes (die, parm_pack);
10641   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10642     generic_parameter_die (parm_pack,
10643                            TREE_VEC_ELT (parm_pack_args, j),
10644                            false /* Don't emit DW_AT_name */,
10645                            die);
10646   return die;
10647 }
10648
10649 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10650    an enumerated type.  */
10651
10652 static inline int
10653 type_is_enum (const_tree type)
10654 {
10655   return TREE_CODE (type) == ENUMERAL_TYPE;
10656 }
10657
10658 /* Return the DBX register number described by a given RTL node.  */
10659
10660 static unsigned int
10661 dbx_reg_number (const_rtx rtl)
10662 {
10663   unsigned regno = REGNO (rtl);
10664
10665   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10666
10667 #ifdef LEAF_REG_REMAP
10668   if (current_function_uses_only_leaf_regs)
10669     {
10670       int leaf_reg = LEAF_REG_REMAP (regno);
10671       if (leaf_reg != -1)
10672         regno = (unsigned) leaf_reg;
10673     }
10674 #endif
10675
10676   return DBX_REGISTER_NUMBER (regno);
10677 }
10678
10679 /* Optionally add a DW_OP_piece term to a location description expression.
10680    DW_OP_piece is only added if the location description expression already
10681    doesn't end with DW_OP_piece.  */
10682
10683 static void
10684 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10685 {
10686   dw_loc_descr_ref loc;
10687
10688   if (*list_head != NULL)
10689     {
10690       /* Find the end of the chain.  */
10691       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10692         ;
10693
10694       if (loc->dw_loc_opc != DW_OP_piece)
10695         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10696     }
10697 }
10698
10699 /* Return a location descriptor that designates a machine register or
10700    zero if there is none.  */
10701
10702 static dw_loc_descr_ref
10703 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10704 {
10705   rtx regs;
10706
10707   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10708     return 0;
10709
10710   /* We only use "frame base" when we're sure we're talking about the
10711      post-prologue local stack frame.  We do this by *not* running
10712      register elimination until this point, and recognizing the special
10713      argument pointer and soft frame pointer rtx's.
10714      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10715   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10716       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10717     {
10718       dw_loc_descr_ref result = NULL;
10719
10720       if (dwarf_version >= 4 || !dwarf_strict)
10721         {
10722           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10723                                        initialized);
10724           if (result)
10725             add_loc_descr (&result,
10726                            new_loc_descr (DW_OP_stack_value, 0, 0));
10727         }
10728       return result;
10729     }
10730
10731   regs = targetm.dwarf_register_span (rtl);
10732
10733   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10734     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10735   else
10736     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10737 }
10738
10739 /* Return a location descriptor that designates a machine register for
10740    a given hard register number.  */
10741
10742 static dw_loc_descr_ref
10743 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10744 {
10745   dw_loc_descr_ref reg_loc_descr;
10746
10747   if (regno <= 31)
10748     reg_loc_descr
10749       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10750   else
10751     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10752
10753   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10754     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10755
10756   return reg_loc_descr;
10757 }
10758
10759 /* Given an RTL of a register, return a location descriptor that
10760    designates a value that spans more than one register.  */
10761
10762 static dw_loc_descr_ref
10763 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10764                              enum var_init_status initialized)
10765 {
10766   int nregs, size, i;
10767   unsigned reg;
10768   dw_loc_descr_ref loc_result = NULL;
10769
10770   reg = REGNO (rtl);
10771 #ifdef LEAF_REG_REMAP
10772   if (current_function_uses_only_leaf_regs)
10773     {
10774       int leaf_reg = LEAF_REG_REMAP (reg);
10775       if (leaf_reg != -1)
10776         reg = (unsigned) leaf_reg;
10777     }
10778 #endif
10779   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10780   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10781
10782   /* Simple, contiguous registers.  */
10783   if (regs == NULL_RTX)
10784     {
10785       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10786
10787       loc_result = NULL;
10788       while (nregs--)
10789         {
10790           dw_loc_descr_ref t;
10791
10792           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10793                                       VAR_INIT_STATUS_INITIALIZED);
10794           add_loc_descr (&loc_result, t);
10795           add_loc_descr_op_piece (&loc_result, size);
10796           ++reg;
10797         }
10798       return loc_result;
10799     }
10800
10801   /* Now onto stupid register sets in non contiguous locations.  */
10802
10803   gcc_assert (GET_CODE (regs) == PARALLEL);
10804
10805   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10806   loc_result = NULL;
10807
10808   for (i = 0; i < XVECLEN (regs, 0); ++i)
10809     {
10810       dw_loc_descr_ref t;
10811
10812       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10813                                   VAR_INIT_STATUS_INITIALIZED);
10814       add_loc_descr (&loc_result, t);
10815       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10816       add_loc_descr_op_piece (&loc_result, size);
10817     }
10818
10819   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10820     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10821   return loc_result;
10822 }
10823
10824 /* Return a location descriptor that designates a constant.  */
10825
10826 static dw_loc_descr_ref
10827 int_loc_descriptor (HOST_WIDE_INT i)
10828 {
10829   enum dwarf_location_atom op;
10830
10831   /* Pick the smallest representation of a constant, rather than just
10832      defaulting to the LEB encoding.  */
10833   if (i >= 0)
10834     {
10835       if (i <= 31)
10836         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10837       else if (i <= 0xff)
10838         op = DW_OP_const1u;
10839       else if (i <= 0xffff)
10840         op = DW_OP_const2u;
10841       else if (HOST_BITS_PER_WIDE_INT == 32
10842                || i <= 0xffffffff)
10843         op = DW_OP_const4u;
10844       else
10845         op = DW_OP_constu;
10846     }
10847   else
10848     {
10849       if (i >= -0x80)
10850         op = DW_OP_const1s;
10851       else if (i >= -0x8000)
10852         op = DW_OP_const2s;
10853       else if (HOST_BITS_PER_WIDE_INT == 32
10854                || i >= -0x80000000)
10855         op = DW_OP_const4s;
10856       else
10857         op = DW_OP_consts;
10858     }
10859
10860   return new_loc_descr (op, i, 0);
10861 }
10862
10863 /* Return loc description representing "address" of integer value.
10864    This can appear only as toplevel expression.  */
10865
10866 static dw_loc_descr_ref
10867 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10868 {
10869   int litsize;
10870   dw_loc_descr_ref loc_result = NULL;
10871
10872   if (!(dwarf_version >= 4 || !dwarf_strict))
10873     return NULL;
10874
10875   if (i >= 0)
10876     {
10877       if (i <= 31)
10878         litsize = 1;
10879       else if (i <= 0xff)
10880         litsize = 2;
10881       else if (i <= 0xffff)
10882         litsize = 3;
10883       else if (HOST_BITS_PER_WIDE_INT == 32
10884                || i <= 0xffffffff)
10885         litsize = 5;
10886       else
10887         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10888     }
10889   else
10890     {
10891       if (i >= -0x80)
10892         litsize = 2;
10893       else if (i >= -0x8000)
10894         litsize = 3;
10895       else if (HOST_BITS_PER_WIDE_INT == 32
10896                || i >= -0x80000000)
10897         litsize = 5;
10898       else
10899         litsize = 1 + size_of_sleb128 (i);
10900     }
10901   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10902      is more compact.  For DW_OP_stack_value we need:
10903      litsize + 1 (DW_OP_stack_value)
10904      and for DW_OP_implicit_value:
10905      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10906   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10907     {
10908       loc_result = int_loc_descriptor (i);
10909       add_loc_descr (&loc_result,
10910                      new_loc_descr (DW_OP_stack_value, 0, 0));
10911       return loc_result;
10912     }
10913
10914   loc_result = new_loc_descr (DW_OP_implicit_value,
10915                               size, 0);
10916   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10917   loc_result->dw_loc_oprnd2.v.val_int = i;
10918   return loc_result;
10919 }
10920
10921 /* Return a location descriptor that designates a base+offset location.  */
10922
10923 static dw_loc_descr_ref
10924 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10925                  enum var_init_status initialized)
10926 {
10927   unsigned int regno;
10928   dw_loc_descr_ref result;
10929   dw_fde_ref fde = current_fde ();
10930
10931   /* We only use "frame base" when we're sure we're talking about the
10932      post-prologue local stack frame.  We do this by *not* running
10933      register elimination until this point, and recognizing the special
10934      argument pointer and soft frame pointer rtx's.  */
10935   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10936     {
10937       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10938
10939       if (elim != reg)
10940         {
10941           if (GET_CODE (elim) == PLUS)
10942             {
10943               offset += INTVAL (XEXP (elim, 1));
10944               elim = XEXP (elim, 0);
10945             }
10946           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10947                        && (elim == hard_frame_pointer_rtx
10948                            || elim == stack_pointer_rtx))
10949                       || elim == (frame_pointer_needed
10950                                   ? hard_frame_pointer_rtx
10951                                   : stack_pointer_rtx));
10952
10953           /* If drap register is used to align stack, use frame
10954              pointer + offset to access stack variables.  If stack
10955              is aligned without drap, use stack pointer + offset to
10956              access stack variables.  */
10957           if (crtl->stack_realign_tried
10958               && reg == frame_pointer_rtx)
10959             {
10960               int base_reg
10961                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10962                                       ? HARD_FRAME_POINTER_REGNUM
10963                                       : REGNO (elim));
10964               return new_reg_loc_descr (base_reg, offset);
10965             }
10966
10967           gcc_assert (frame_pointer_fb_offset_valid);
10968           offset += frame_pointer_fb_offset;
10969           return new_loc_descr (DW_OP_fbreg, offset, 0);
10970         }
10971     }
10972   else if (!optimize
10973            && fde
10974            && (fde->drap_reg == REGNO (reg)
10975                || fde->vdrap_reg == REGNO (reg)))
10976     {
10977       /* Use cfa+offset to represent the location of arguments passed
10978          on the stack when drap is used to align stack.
10979          Only do this when not optimizing, for optimized code var-tracking
10980          is supposed to track where the arguments live and the register
10981          used as vdrap or drap in some spot might be used for something
10982          else in other part of the routine.  */
10983       return new_loc_descr (DW_OP_fbreg, offset, 0);
10984     }
10985
10986   regno = dbx_reg_number (reg);
10987   if (regno <= 31)
10988     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10989                             offset, 0);
10990   else
10991     result = new_loc_descr (DW_OP_bregx, regno, offset);
10992
10993   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10994     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10995
10996   return result;
10997 }
10998
10999 /* Return true if this RTL expression describes a base+offset calculation.  */
11000
11001 static inline int
11002 is_based_loc (const_rtx rtl)
11003 {
11004   return (GET_CODE (rtl) == PLUS
11005           && ((REG_P (XEXP (rtl, 0))
11006                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11007                && CONST_INT_P (XEXP (rtl, 1)))));
11008 }
11009
11010 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11011    failed.  */
11012
11013 static dw_loc_descr_ref
11014 tls_mem_loc_descriptor (rtx mem)
11015 {
11016   tree base;
11017   dw_loc_descr_ref loc_result;
11018
11019   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
11020     return NULL;
11021
11022   base = get_base_address (MEM_EXPR (mem));
11023   if (base == NULL
11024       || TREE_CODE (base) != VAR_DECL
11025       || !DECL_THREAD_LOCAL_P (base))
11026     return NULL;
11027
11028   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11029   if (loc_result == NULL)
11030     return NULL;
11031
11032   if (INTVAL (MEM_OFFSET (mem)))
11033     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
11034
11035   return loc_result;
11036 }
11037
11038 /* Output debug info about reason why we failed to expand expression as dwarf
11039    expression.  */
11040
11041 static void
11042 expansion_failed (tree expr, rtx rtl, char const *reason)
11043 {
11044   if (dump_file && (dump_flags & TDF_DETAILS))
11045     {
11046       fprintf (dump_file, "Failed to expand as dwarf: ");
11047       if (expr)
11048         print_generic_expr (dump_file, expr, dump_flags);
11049       if (rtl)
11050         {
11051           fprintf (dump_file, "\n");
11052           print_rtl (dump_file, rtl);
11053         }
11054       fprintf (dump_file, "\nReason: %s\n", reason);
11055     }
11056 }
11057
11058 /* Helper function for const_ok_for_output, called either directly
11059    or via for_each_rtx.  */
11060
11061 static int
11062 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11063 {
11064   rtx rtl = *rtlp;
11065
11066   if (GET_CODE (rtl) == UNSPEC)
11067     {
11068       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11069          we can't express it in the debug info.  */
11070 #ifdef ENABLE_CHECKING
11071       /* Don't complain about TLS UNSPECs, those are just too hard to
11072          delegitimize.  */
11073       if (XVECLEN (rtl, 0) != 1
11074           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11075           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
11076           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
11077           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
11078         inform (current_function_decl
11079                 ? DECL_SOURCE_LOCATION (current_function_decl)
11080                 : UNKNOWN_LOCATION,
11081 #if NUM_UNSPEC_VALUES > 0
11082                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11083                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11084                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11085                 XINT (rtl, 1));
11086 #else
11087                 "non-delegitimized UNSPEC %d found in variable location",
11088                 XINT (rtl, 1));
11089 #endif
11090 #endif
11091       expansion_failed (NULL_TREE, rtl,
11092                         "UNSPEC hasn't been delegitimized.\n");
11093       return 1;
11094     }
11095
11096   if (GET_CODE (rtl) != SYMBOL_REF)
11097     return 0;
11098
11099   if (CONSTANT_POOL_ADDRESS_P (rtl))
11100     {
11101       bool marked;
11102       get_pool_constant_mark (rtl, &marked);
11103       /* If all references to this pool constant were optimized away,
11104          it was not output and thus we can't represent it.  */
11105       if (!marked)
11106         {
11107           expansion_failed (NULL_TREE, rtl,
11108                             "Constant was removed from constant pool.\n");
11109           return 1;
11110         }
11111     }
11112
11113   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11114     return 1;
11115
11116   /* Avoid references to external symbols in debug info, on several targets
11117      the linker might even refuse to link when linking a shared library,
11118      and in many other cases the relocations for .debug_info/.debug_loc are
11119      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11120      to be defined within the same shared library or executable are fine.  */
11121   if (SYMBOL_REF_EXTERNAL_P (rtl))
11122     {
11123       tree decl = SYMBOL_REF_DECL (rtl);
11124
11125       if (decl == NULL || !targetm.binds_local_p (decl))
11126         {
11127           expansion_failed (NULL_TREE, rtl,
11128                             "Symbol not defined in current TU.\n");
11129           return 1;
11130         }
11131     }
11132
11133   return 0;
11134 }
11135
11136 /* Return true if constant RTL can be emitted in DW_OP_addr or
11137    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11138    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11139
11140 static bool
11141 const_ok_for_output (rtx rtl)
11142 {
11143   if (GET_CODE (rtl) == SYMBOL_REF)
11144     return const_ok_for_output_1 (&rtl, NULL) == 0;
11145
11146   if (GET_CODE (rtl) == CONST)
11147     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11148
11149   return true;
11150 }
11151
11152 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11153    if possible, NULL otherwise.  */
11154
11155 static dw_die_ref
11156 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11157 {
11158   dw_die_ref type_die;
11159   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11160
11161   if (type == NULL)
11162     return NULL;
11163   switch (TREE_CODE (type))
11164     {
11165     case INTEGER_TYPE:
11166     case REAL_TYPE:
11167       break;
11168     default:
11169       return NULL;
11170     }
11171   type_die = lookup_type_die (type);
11172   if (!type_die)
11173     type_die = modified_type_die (type, false, false, comp_unit_die ());
11174   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11175     return NULL;
11176   return type_die;
11177 }
11178
11179 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11180    type matching MODE, or, if MODE is narrower than or as wide as
11181    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11182    possible.  */
11183
11184 static dw_loc_descr_ref
11185 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11186 {
11187   enum machine_mode outer_mode = mode;
11188   dw_die_ref type_die;
11189   dw_loc_descr_ref cvt;
11190
11191   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11192     {
11193       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11194       return op;
11195     }
11196   type_die = base_type_for_mode (outer_mode, 1);
11197   if (type_die == NULL)
11198     return NULL;
11199   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11200   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11201   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11202   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11203   add_loc_descr (&op, cvt);
11204   return op;
11205 }
11206
11207 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11208
11209 static dw_loc_descr_ref
11210 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11211                         dw_loc_descr_ref op1)
11212 {
11213   dw_loc_descr_ref ret = op0;
11214   add_loc_descr (&ret, op1);
11215   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11216   if (STORE_FLAG_VALUE != 1)
11217     {
11218       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11219       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11220     }
11221   return ret;
11222 }
11223
11224 /* Return location descriptor for signed comparison OP RTL.  */
11225
11226 static dw_loc_descr_ref
11227 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11228                          enum machine_mode mem_mode)
11229 {
11230   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11231   dw_loc_descr_ref op0, op1;
11232   int shift;
11233
11234   if (op_mode == VOIDmode)
11235     op_mode = GET_MODE (XEXP (rtl, 1));
11236   if (op_mode == VOIDmode)
11237     return NULL;
11238
11239   if (dwarf_strict
11240       && (GET_MODE_CLASS (op_mode) != MODE_INT
11241           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11242     return NULL;
11243
11244   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11245                             VAR_INIT_STATUS_INITIALIZED);
11246   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11247                             VAR_INIT_STATUS_INITIALIZED);
11248
11249   if (op0 == NULL || op1 == NULL)
11250     return NULL;
11251
11252   if (GET_MODE_CLASS (op_mode) != MODE_INT
11253       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11254     return compare_loc_descriptor (op, op0, op1);
11255
11256   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11257     {
11258       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11259       dw_loc_descr_ref cvt;
11260
11261       if (type_die == NULL)
11262         return NULL;
11263       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11264       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11265       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11266       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11267       add_loc_descr (&op0, cvt);
11268       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11269       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11270       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11271       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11272       add_loc_descr (&op1, cvt);
11273       return compare_loc_descriptor (op, op0, op1);
11274     }
11275
11276   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11277   /* For eq/ne, if the operands are known to be zero-extended,
11278      there is no need to do the fancy shifting up.  */
11279   if (op == DW_OP_eq || op == DW_OP_ne)
11280     {
11281       dw_loc_descr_ref last0, last1;
11282       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11283         ;
11284       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11285         ;
11286       /* deref_size zero extends, and for constants we can check
11287          whether they are zero extended or not.  */
11288       if (((last0->dw_loc_opc == DW_OP_deref_size
11289             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11290            || (CONST_INT_P (XEXP (rtl, 0))
11291                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11292                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11293           && ((last1->dw_loc_opc == DW_OP_deref_size
11294                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11295               || (CONST_INT_P (XEXP (rtl, 1))
11296                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11297                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11298         return compare_loc_descriptor (op, op0, op1);
11299     }
11300   add_loc_descr (&op0, int_loc_descriptor (shift));
11301   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11302   if (CONST_INT_P (XEXP (rtl, 1)))
11303     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11304   else
11305     {
11306       add_loc_descr (&op1, int_loc_descriptor (shift));
11307       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11308     }
11309   return compare_loc_descriptor (op, op0, op1);
11310 }
11311
11312 /* Return location descriptor for unsigned comparison OP RTL.  */
11313
11314 static dw_loc_descr_ref
11315 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11316                          enum machine_mode mem_mode)
11317 {
11318   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11319   dw_loc_descr_ref op0, op1;
11320
11321   if (op_mode == VOIDmode)
11322     op_mode = GET_MODE (XEXP (rtl, 1));
11323   if (op_mode == VOIDmode)
11324     return NULL;
11325   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11326     return NULL;
11327
11328   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11329     return NULL;
11330
11331   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11332                             VAR_INIT_STATUS_INITIALIZED);
11333   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11334                             VAR_INIT_STATUS_INITIALIZED);
11335
11336   if (op0 == NULL || op1 == NULL)
11337     return NULL;
11338
11339   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11340     {
11341       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11342       dw_loc_descr_ref last0, last1;
11343       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11344         ;
11345       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11346         ;
11347       if (CONST_INT_P (XEXP (rtl, 0)))
11348         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11349       /* deref_size zero extends, so no need to mask it again.  */
11350       else if (last0->dw_loc_opc != DW_OP_deref_size
11351                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11352         {
11353           add_loc_descr (&op0, int_loc_descriptor (mask));
11354           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11355         }
11356       if (CONST_INT_P (XEXP (rtl, 1)))
11357         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11358       /* deref_size zero extends, so no need to mask it again.  */
11359       else if (last1->dw_loc_opc != DW_OP_deref_size
11360                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11361         {
11362           add_loc_descr (&op1, int_loc_descriptor (mask));
11363           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11364         }
11365     }
11366   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11367     {
11368       HOST_WIDE_INT bias = 1;
11369       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11370       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11371       if (CONST_INT_P (XEXP (rtl, 1)))
11372         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11373                                   + INTVAL (XEXP (rtl, 1)));
11374       else
11375         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11376                                             bias, 0));
11377     }
11378   return compare_loc_descriptor (op, op0, op1);
11379 }
11380
11381 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11382
11383 static dw_loc_descr_ref
11384 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11385                        enum machine_mode mem_mode)
11386 {
11387   enum dwarf_location_atom op;
11388   dw_loc_descr_ref op0, op1, ret;
11389   dw_loc_descr_ref bra_node, drop_node;
11390
11391   if (dwarf_strict
11392       && (GET_MODE_CLASS (mode) != MODE_INT
11393           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11394     return NULL;
11395
11396   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11397                             VAR_INIT_STATUS_INITIALIZED);
11398   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11399                             VAR_INIT_STATUS_INITIALIZED);
11400
11401   if (op0 == NULL || op1 == NULL)
11402     return NULL;
11403
11404   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11405   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11406   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11407   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11408     {
11409       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11410         {
11411           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11412           add_loc_descr (&op0, int_loc_descriptor (mask));
11413           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11414           add_loc_descr (&op1, int_loc_descriptor (mask));
11415           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11416         }
11417       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11418         {
11419           HOST_WIDE_INT bias = 1;
11420           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11421           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11422           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11423         }
11424     }
11425   else if (GET_MODE_CLASS (mode) == MODE_INT
11426            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11427     {
11428       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11429       add_loc_descr (&op0, int_loc_descriptor (shift));
11430       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11431       add_loc_descr (&op1, int_loc_descriptor (shift));
11432       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11433     }
11434   else if (GET_MODE_CLASS (mode) == MODE_INT
11435            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11436     {
11437       dw_die_ref type_die = base_type_for_mode (mode, 0);
11438       dw_loc_descr_ref cvt;
11439       if (type_die == NULL)
11440         return NULL;
11441       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11442       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11443       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11444       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11445       add_loc_descr (&op0, cvt);
11446       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11447       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11448       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11449       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11450       add_loc_descr (&op1, cvt);
11451     }
11452
11453   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11454     op = DW_OP_lt;
11455   else
11456     op = DW_OP_gt;
11457   ret = op0;
11458   add_loc_descr (&ret, op1);
11459   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11460   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11461   add_loc_descr (&ret, bra_node);
11462   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11463   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11464   add_loc_descr (&ret, drop_node);
11465   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11466   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11467   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11468       && GET_MODE_CLASS (mode) == MODE_INT
11469       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11470     ret = convert_descriptor_to_mode (mode, ret);
11471   return ret;
11472 }
11473
11474 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11475    but after converting arguments to type_die, afterwards
11476    convert back to unsigned.  */
11477
11478 static dw_loc_descr_ref
11479 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11480              enum machine_mode mode, enum machine_mode mem_mode)
11481 {
11482   dw_loc_descr_ref cvt, op0, op1;
11483
11484   if (type_die == NULL)
11485     return NULL;
11486   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11487                             VAR_INIT_STATUS_INITIALIZED);
11488   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11489                             VAR_INIT_STATUS_INITIALIZED);
11490   if (op0 == NULL || op1 == NULL)
11491     return NULL;
11492   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11493   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11494   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11495   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11496   add_loc_descr (&op0, cvt);
11497   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11498   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11499   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11500   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11501   add_loc_descr (&op1, cvt);
11502   add_loc_descr (&op0, op1);
11503   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11504   return convert_descriptor_to_mode (mode, op0);
11505 }
11506
11507 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11508    const0 is DW_OP_lit0 or corresponding typed constant,
11509    const1 is DW_OP_lit1 or corresponding typed constant
11510    and constMSB is constant with just the MSB bit set
11511    for the mode):
11512        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11513    L1: const0 DW_OP_swap
11514    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11515        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11516    L3: DW_OP_drop
11517    L4: DW_OP_nop
11518
11519    CTZ is similar:
11520        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11521    L1: const0 DW_OP_swap
11522    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11523        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11524    L3: DW_OP_drop
11525    L4: DW_OP_nop
11526
11527    FFS is similar:
11528        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11529    L1: const1 DW_OP_swap
11530    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11531        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11532    L3: DW_OP_drop
11533    L4: DW_OP_nop  */
11534
11535 static dw_loc_descr_ref
11536 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11537                     enum machine_mode mem_mode)
11538 {
11539   dw_loc_descr_ref op0, ret, tmp;
11540   HOST_WIDE_INT valv;
11541   dw_loc_descr_ref l1jump, l1label;
11542   dw_loc_descr_ref l2jump, l2label;
11543   dw_loc_descr_ref l3jump, l3label;
11544   dw_loc_descr_ref l4jump, l4label;
11545   rtx msb;
11546
11547   if (GET_MODE_CLASS (mode) != MODE_INT
11548       || GET_MODE (XEXP (rtl, 0)) != mode
11549       || (GET_CODE (rtl) == CLZ
11550           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11551     return NULL;
11552
11553   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11554                             VAR_INIT_STATUS_INITIALIZED);
11555   if (op0 == NULL)
11556     return NULL;
11557   ret = op0;
11558   if (GET_CODE (rtl) == CLZ)
11559     {
11560       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11561         valv = GET_MODE_BITSIZE (mode);
11562     }
11563   else if (GET_CODE (rtl) == FFS)
11564     valv = 0;
11565   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11566     valv = GET_MODE_BITSIZE (mode);
11567   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11568   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11569   add_loc_descr (&ret, l1jump);
11570   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11571   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11572                             VAR_INIT_STATUS_INITIALIZED);
11573   if (tmp == NULL)
11574     return NULL;
11575   add_loc_descr (&ret, tmp);
11576   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11577   add_loc_descr (&ret, l4jump);
11578   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11579                                 ? const1_rtx : const0_rtx,
11580                                 mode, mem_mode,
11581                                 VAR_INIT_STATUS_INITIALIZED);
11582   if (l1label == NULL)
11583     return NULL;
11584   add_loc_descr (&ret, l1label);
11585   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11586   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11587   add_loc_descr (&ret, l2label);
11588   if (GET_CODE (rtl) != CLZ)
11589     msb = const1_rtx;
11590   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11591     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11592                    << (GET_MODE_BITSIZE (mode) - 1));
11593   else
11594     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11595                                   << (GET_MODE_BITSIZE (mode)
11596                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11597   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11598     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11599                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11600                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11601   else
11602     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11603                               VAR_INIT_STATUS_INITIALIZED);
11604   if (tmp == NULL)
11605     return NULL;
11606   add_loc_descr (&ret, tmp);
11607   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11608   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11609   add_loc_descr (&ret, l3jump);
11610   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11611                             VAR_INIT_STATUS_INITIALIZED);
11612   if (tmp == NULL)
11613     return NULL;
11614   add_loc_descr (&ret, tmp);
11615   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11616                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11617   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11618   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11619   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11620   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11621   add_loc_descr (&ret, l2jump);
11622   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11623   add_loc_descr (&ret, l3label);
11624   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11625   add_loc_descr (&ret, l4label);
11626   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11627   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11628   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11629   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11630   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11631   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11632   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11633   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11634   return ret;
11635 }
11636
11637 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11638    const1 is DW_OP_lit1 or corresponding typed constant):
11639        const0 DW_OP_swap
11640    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11641        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11642    L2: DW_OP_drop
11643
11644    PARITY is similar:
11645    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11646        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11647    L2: DW_OP_drop  */
11648
11649 static dw_loc_descr_ref
11650 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11651                          enum machine_mode mem_mode)
11652 {
11653   dw_loc_descr_ref op0, ret, tmp;
11654   dw_loc_descr_ref l1jump, l1label;
11655   dw_loc_descr_ref l2jump, l2label;
11656
11657   if (GET_MODE_CLASS (mode) != MODE_INT
11658       || GET_MODE (XEXP (rtl, 0)) != mode)
11659     return NULL;
11660
11661   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11662                             VAR_INIT_STATUS_INITIALIZED);
11663   if (op0 == NULL)
11664     return NULL;
11665   ret = op0;
11666   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11667                             VAR_INIT_STATUS_INITIALIZED);
11668   if (tmp == NULL)
11669     return NULL;
11670   add_loc_descr (&ret, tmp);
11671   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11672   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11673   add_loc_descr (&ret, l1label);
11674   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11675   add_loc_descr (&ret, l2jump);
11676   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11677   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11678   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11679                             VAR_INIT_STATUS_INITIALIZED);
11680   if (tmp == NULL)
11681     return NULL;
11682   add_loc_descr (&ret, tmp);
11683   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11684   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11685                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11686   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11687   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11688                             VAR_INIT_STATUS_INITIALIZED);
11689   add_loc_descr (&ret, tmp);
11690   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11691   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11692   add_loc_descr (&ret, l1jump);
11693   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11694   add_loc_descr (&ret, l2label);
11695   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11696   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11697   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11698   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11699   return ret;
11700 }
11701
11702 /* BSWAP (constS is initial shift count, either 56 or 24):
11703        constS const0
11704    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11705        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11706        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11707        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11708    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11709
11710 static dw_loc_descr_ref
11711 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11712                       enum machine_mode mem_mode)
11713 {
11714   dw_loc_descr_ref op0, ret, tmp;
11715   dw_loc_descr_ref l1jump, l1label;
11716   dw_loc_descr_ref l2jump, l2label;
11717
11718   if (GET_MODE_CLASS (mode) != MODE_INT
11719       || BITS_PER_UNIT != 8
11720       || (GET_MODE_BITSIZE (mode) != 32
11721           &&  GET_MODE_BITSIZE (mode) != 64))
11722     return NULL;
11723
11724   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11725                             VAR_INIT_STATUS_INITIALIZED);
11726   if (op0 == NULL)
11727     return NULL;
11728
11729   ret = op0;
11730   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11731                             mode, mem_mode,
11732                             VAR_INIT_STATUS_INITIALIZED);
11733   if (tmp == NULL)
11734     return NULL;
11735   add_loc_descr (&ret, tmp);
11736   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11737                             VAR_INIT_STATUS_INITIALIZED);
11738   if (tmp == NULL)
11739     return NULL;
11740   add_loc_descr (&ret, tmp);
11741   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11742   add_loc_descr (&ret, l1label);
11743   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11744                             mode, mem_mode,
11745                             VAR_INIT_STATUS_INITIALIZED);
11746   add_loc_descr (&ret, tmp);
11747   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11748   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11749   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11750   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11751                             VAR_INIT_STATUS_INITIALIZED);
11752   if (tmp == NULL)
11753     return NULL;
11754   add_loc_descr (&ret, tmp);
11755   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11756   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11757   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11758   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11759   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11760   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11761   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11762                             VAR_INIT_STATUS_INITIALIZED);
11763   add_loc_descr (&ret, tmp);
11764   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11765   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11766   add_loc_descr (&ret, l2jump);
11767   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11768                             VAR_INIT_STATUS_INITIALIZED);
11769   add_loc_descr (&ret, tmp);
11770   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11771   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11772   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11773   add_loc_descr (&ret, l1jump);
11774   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11775   add_loc_descr (&ret, l2label);
11776   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11777   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11778   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11779   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11780   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11781   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11782   return ret;
11783 }
11784
11785 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11786    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11787    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11788    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11789
11790    ROTATERT is similar:
11791    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11792    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11793    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11794
11795 static dw_loc_descr_ref
11796 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11797                        enum machine_mode mem_mode)
11798 {
11799   rtx rtlop1 = XEXP (rtl, 1);
11800   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11801   int i;
11802
11803   if (GET_MODE_CLASS (mode) != MODE_INT)
11804     return NULL;
11805
11806   if (GET_MODE (rtlop1) != VOIDmode
11807       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11808     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11809   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11810                             VAR_INIT_STATUS_INITIALIZED);
11811   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11812                             VAR_INIT_STATUS_INITIALIZED);
11813   if (op0 == NULL || op1 == NULL)
11814     return NULL;
11815   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11816     for (i = 0; i < 2; i++)
11817       {
11818         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11819           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11820                                         mode, mem_mode,
11821                                         VAR_INIT_STATUS_INITIALIZED);
11822         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11823           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11824                                    ? DW_OP_const4u
11825                                    : HOST_BITS_PER_WIDE_INT == 64
11826                                    ? DW_OP_const8u : DW_OP_constu,
11827                                    GET_MODE_MASK (mode), 0);
11828         else
11829           mask[i] = NULL;
11830         if (mask[i] == NULL)
11831           return NULL;
11832         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11833       }
11834   ret = op0;
11835   add_loc_descr (&ret, op1);
11836   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11837   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11838   if (GET_CODE (rtl) == ROTATERT)
11839     {
11840       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11841       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11842                                           GET_MODE_BITSIZE (mode), 0));
11843     }
11844   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11845   if (mask[0] != NULL)
11846     add_loc_descr (&ret, mask[0]);
11847   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11848   if (mask[1] != NULL)
11849     {
11850       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11851       add_loc_descr (&ret, mask[1]);
11852       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11853     }
11854   if (GET_CODE (rtl) == ROTATE)
11855     {
11856       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11857       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11858                                           GET_MODE_BITSIZE (mode), 0));
11859     }
11860   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11861   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11862   return ret;
11863 }
11864
11865 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11866    for DEBUG_PARAMETER_REF RTL.  */
11867
11868 static dw_loc_descr_ref
11869 parameter_ref_descriptor (rtx rtl)
11870 {
11871   dw_loc_descr_ref ret;
11872   dw_die_ref ref;
11873
11874   if (dwarf_strict)
11875     return NULL;
11876   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11877   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11878   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11879   if (ref)
11880     {
11881       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11882       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11883       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11884     }
11885   else
11886     {
11887       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11888       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11889     }
11890   return ret;
11891 }
11892
11893 /* Helper function to get mode of MEM's address.  */
11894
11895 enum machine_mode
11896 get_address_mode (rtx mem)
11897 {
11898   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11899   if (mode != VOIDmode)
11900     return mode;
11901   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11902 }
11903
11904 /* The following routine converts the RTL for a variable or parameter
11905    (resident in memory) into an equivalent Dwarf representation of a
11906    mechanism for getting the address of that same variable onto the top of a
11907    hypothetical "address evaluation" stack.
11908
11909    When creating memory location descriptors, we are effectively transforming
11910    the RTL for a memory-resident object into its Dwarf postfix expression
11911    equivalent.  This routine recursively descends an RTL tree, turning
11912    it into Dwarf postfix code as it goes.
11913
11914    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11915
11916    MEM_MODE is the mode of the memory reference, needed to handle some
11917    autoincrement addressing modes.
11918
11919    Return 0 if we can't represent the location.  */
11920
11921 dw_loc_descr_ref
11922 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11923                     enum machine_mode mem_mode,
11924                     enum var_init_status initialized)
11925 {
11926   dw_loc_descr_ref mem_loc_result = NULL;
11927   enum dwarf_location_atom op;
11928   dw_loc_descr_ref op0, op1;
11929
11930   if (mode == VOIDmode)
11931     mode = GET_MODE (rtl);
11932
11933   /* Note that for a dynamically sized array, the location we will generate a
11934      description of here will be the lowest numbered location which is
11935      actually within the array.  That's *not* necessarily the same as the
11936      zeroth element of the array.  */
11937
11938   rtl = targetm.delegitimize_address (rtl);
11939
11940   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11941     return NULL;
11942
11943   switch (GET_CODE (rtl))
11944     {
11945     case POST_INC:
11946     case POST_DEC:
11947     case POST_MODIFY:
11948       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11949
11950     case SUBREG:
11951       /* The case of a subreg may arise when we have a local (register)
11952          variable or a formal (register) parameter which doesn't quite fill
11953          up an entire register.  For now, just assume that it is
11954          legitimate to make the Dwarf info refer to the whole register which
11955          contains the given subreg.  */
11956       if (!subreg_lowpart_p (rtl))
11957         break;
11958       if (GET_MODE_CLASS (mode) == MODE_INT
11959           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11960           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11961 #ifdef POINTERS_EXTEND_UNSIGNED
11962               || (mode == Pmode && mem_mode != VOIDmode)
11963 #endif
11964              )
11965           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11966         {
11967           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11968                                                GET_MODE (SUBREG_REG (rtl)),
11969                                                mem_mode, initialized);
11970           break;
11971         }
11972       if (dwarf_strict)
11973         break;
11974       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11975         break;
11976       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11977           && (GET_MODE_CLASS (mode) != MODE_INT
11978               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11979         break;
11980       else
11981         {
11982           dw_die_ref type_die;
11983           dw_loc_descr_ref cvt;
11984
11985           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11986                                                GET_MODE (SUBREG_REG (rtl)),
11987                                                mem_mode, initialized);
11988           if (mem_loc_result == NULL)
11989             break;
11990           type_die = base_type_for_mode (mode,
11991                                          GET_MODE_CLASS (mode) == MODE_INT);
11992           if (type_die == NULL)
11993             {
11994               mem_loc_result = NULL;
11995               break;
11996             }
11997           if (GET_MODE_SIZE (mode)
11998               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11999             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12000           else
12001             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
12002           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12003           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12004           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12005           add_loc_descr (&mem_loc_result, cvt);
12006         }
12007       break;
12008
12009     case REG:
12010       if (GET_MODE_CLASS (mode) != MODE_INT
12011           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12012 #ifdef POINTERS_EXTEND_UNSIGNED
12013               && (mode != Pmode || mem_mode == VOIDmode)
12014 #endif
12015               ))
12016         {
12017           dw_die_ref type_die;
12018
12019           if (dwarf_strict)
12020             break;
12021           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12022             break;
12023           type_die = base_type_for_mode (mode,
12024                                          GET_MODE_CLASS (mode) == MODE_INT);
12025           if (type_die == NULL)
12026             break;
12027           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12028                                           dbx_reg_number (rtl), 0);
12029           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12030           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12031           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12032           break;
12033         }
12034       /* Whenever a register number forms a part of the description of the
12035          method for calculating the (dynamic) address of a memory resident
12036          object, DWARF rules require the register number be referred to as
12037          a "base register".  This distinction is not based in any way upon
12038          what category of register the hardware believes the given register
12039          belongs to.  This is strictly DWARF terminology we're dealing with
12040          here. Note that in cases where the location of a memory-resident
12041          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12042          OP_CONST (0)) the actual DWARF location descriptor that we generate
12043          may just be OP_BASEREG (basereg).  This may look deceptively like
12044          the object in question was allocated to a register (rather than in
12045          memory) so DWARF consumers need to be aware of the subtle
12046          distinction between OP_REG and OP_BASEREG.  */
12047       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12048         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12049       else if (stack_realign_drap
12050                && crtl->drap_reg
12051                && crtl->args.internal_arg_pointer == rtl
12052                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12053         {
12054           /* If RTL is internal_arg_pointer, which has been optimized
12055              out, use DRAP instead.  */
12056           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12057                                             VAR_INIT_STATUS_INITIALIZED);
12058         }
12059       break;
12060
12061     case SIGN_EXTEND:
12062     case ZERO_EXTEND:
12063       if (GET_MODE_CLASS (mode) != MODE_INT)
12064         break;
12065       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12066                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12067       if (op0 == 0)
12068         break;
12069       else if (GET_CODE (rtl) == ZERO_EXTEND
12070                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12071                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12072                   < HOST_BITS_PER_WIDE_INT
12073                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12074                   to expand zero extend as two shifts instead of
12075                   masking.  */
12076                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12077         {
12078           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12079           mem_loc_result = op0;
12080           add_loc_descr (&mem_loc_result,
12081                          int_loc_descriptor (GET_MODE_MASK (imode)));
12082           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12083         }
12084       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12085         {
12086           int shift = DWARF2_ADDR_SIZE
12087                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12088           shift *= BITS_PER_UNIT;
12089           if (GET_CODE (rtl) == SIGN_EXTEND)
12090             op = DW_OP_shra;
12091           else
12092             op = DW_OP_shr;
12093           mem_loc_result = op0;
12094           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12095           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12096           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12097           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12098         }
12099       else if (!dwarf_strict)
12100         {
12101           dw_die_ref type_die1, type_die2;
12102           dw_loc_descr_ref cvt;
12103
12104           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12105                                           GET_CODE (rtl) == ZERO_EXTEND);
12106           if (type_die1 == NULL)
12107             break;
12108           type_die2 = base_type_for_mode (mode, 1);
12109           if (type_die2 == NULL)
12110             break;
12111           mem_loc_result = op0;
12112           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12113           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12114           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12115           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12116           add_loc_descr (&mem_loc_result, cvt);
12117           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12118           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12119           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12120           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12121           add_loc_descr (&mem_loc_result, cvt);
12122         }
12123       break;
12124
12125     case MEM:
12126       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12127                                            get_address_mode (rtl), mode,
12128                                            VAR_INIT_STATUS_INITIALIZED);
12129       if (mem_loc_result == NULL)
12130         mem_loc_result = tls_mem_loc_descriptor (rtl);
12131       if (mem_loc_result != 0)
12132         {
12133           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12134               || GET_MODE_CLASS (mode) != MODE_INT)
12135             {
12136               dw_die_ref type_die;
12137               dw_loc_descr_ref deref;
12138
12139               if (dwarf_strict)
12140                 return NULL;
12141               type_die
12142                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12143               if (type_die == NULL)
12144                 return NULL;
12145               deref = new_loc_descr (DW_OP_GNU_deref_type,
12146                                      GET_MODE_SIZE (mode), 0);
12147               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12148               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12149               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12150               add_loc_descr (&mem_loc_result, deref);
12151             }
12152           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12153             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12154           else
12155             add_loc_descr (&mem_loc_result,
12156                            new_loc_descr (DW_OP_deref_size,
12157                                           GET_MODE_SIZE (mode), 0));
12158         }
12159       else
12160         {
12161           rtx new_rtl = avoid_constant_pool_reference (rtl);
12162           if (new_rtl != rtl)
12163             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
12164         }
12165       break;
12166
12167     case LO_SUM:
12168       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12169
12170     case LABEL_REF:
12171       /* Some ports can transform a symbol ref into a label ref, because
12172          the symbol ref is too far away and has to be dumped into a constant
12173          pool.  */
12174     case CONST:
12175     case SYMBOL_REF:
12176       if (GET_MODE_CLASS (mode) != MODE_INT
12177           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12178 #ifdef POINTERS_EXTEND_UNSIGNED
12179               && (mode != Pmode || mem_mode == VOIDmode)
12180 #endif
12181               ))
12182         break;
12183       if (GET_CODE (rtl) == SYMBOL_REF
12184           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12185         {
12186           dw_loc_descr_ref temp;
12187
12188           /* If this is not defined, we have no way to emit the data.  */
12189           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12190             break;
12191
12192           /* We used to emit DW_OP_addr here, but that's wrong, since
12193              DW_OP_addr should be relocated by the debug info consumer,
12194              while DW_OP_GNU_push_tls_address operand should not.  */
12195           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
12196                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
12197           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
12198           temp->dw_loc_oprnd1.v.val_addr = rtl;
12199           temp->dtprel = true;
12200
12201           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12202           add_loc_descr (&mem_loc_result, temp);
12203
12204           break;
12205         }
12206
12207       if (!const_ok_for_output (rtl))
12208         break;
12209
12210     symref:
12211       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12212       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12213       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12214       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12215       break;
12216
12217     case CONCAT:
12218     case CONCATN:
12219     case VAR_LOCATION:
12220     case DEBUG_IMPLICIT_PTR:
12221       expansion_failed (NULL_TREE, rtl,
12222                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12223       return 0;
12224
12225     case ENTRY_VALUE:
12226       if (dwarf_strict)
12227         return NULL;
12228       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12229         {
12230           if (GET_MODE_CLASS (mode) != MODE_INT
12231               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12232             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12233                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12234           else
12235             op0
12236               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
12237                                         VAR_INIT_STATUS_INITIALIZED);
12238         }
12239       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12240                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12241         {
12242           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12243                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12244           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12245             return NULL;
12246         }
12247       else
12248         gcc_unreachable ();
12249       if (op0 == NULL)
12250         return NULL;
12251       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12252       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12253       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12254       break;
12255
12256     case DEBUG_PARAMETER_REF:
12257       mem_loc_result = parameter_ref_descriptor (rtl);
12258       break;
12259
12260     case PRE_MODIFY:
12261       /* Extract the PLUS expression nested inside and fall into
12262          PLUS code below.  */
12263       rtl = XEXP (rtl, 1);
12264       goto plus;
12265
12266     case PRE_INC:
12267     case PRE_DEC:
12268       /* Turn these into a PLUS expression and fall into the PLUS code
12269          below.  */
12270       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12271                           GEN_INT (GET_CODE (rtl) == PRE_INC
12272                                    ? GET_MODE_UNIT_SIZE (mem_mode)
12273                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
12274
12275       /* ... fall through ...  */
12276
12277     case PLUS:
12278     plus:
12279       if (is_based_loc (rtl)
12280           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12281           && GET_MODE_CLASS (mode) == MODE_INT)
12282         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12283                                           INTVAL (XEXP (rtl, 1)),
12284                                           VAR_INIT_STATUS_INITIALIZED);
12285       else
12286         {
12287           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12288                                                VAR_INIT_STATUS_INITIALIZED);
12289           if (mem_loc_result == 0)
12290             break;
12291
12292           if (CONST_INT_P (XEXP (rtl, 1))
12293               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12294             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12295           else
12296             {
12297               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12298                                         VAR_INIT_STATUS_INITIALIZED);
12299               if (op1 == 0)
12300                 break;
12301               add_loc_descr (&mem_loc_result, op1);
12302               add_loc_descr (&mem_loc_result,
12303                              new_loc_descr (DW_OP_plus, 0, 0));
12304             }
12305         }
12306       break;
12307
12308     /* If a pseudo-reg is optimized away, it is possible for it to
12309        be replaced with a MEM containing a multiply or shift.  */
12310     case MINUS:
12311       op = DW_OP_minus;
12312       goto do_binop;
12313
12314     case MULT:
12315       op = DW_OP_mul;
12316       goto do_binop;
12317
12318     case DIV:
12319       if (!dwarf_strict
12320           && GET_MODE_CLASS (mode) == MODE_INT
12321           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12322         {
12323           mem_loc_result = typed_binop (DW_OP_div, rtl,
12324                                         base_type_for_mode (mode, 0),
12325                                         mode, mem_mode);
12326           break;
12327         }
12328       op = DW_OP_div;
12329       goto do_binop;
12330
12331     case UMOD:
12332       op = DW_OP_mod;
12333       goto do_binop;
12334
12335     case ASHIFT:
12336       op = DW_OP_shl;
12337       goto do_shift;
12338
12339     case ASHIFTRT:
12340       op = DW_OP_shra;
12341       goto do_shift;
12342
12343     case LSHIFTRT:
12344       op = DW_OP_shr;
12345       goto do_shift;
12346
12347     do_shift:
12348       if (GET_MODE_CLASS (mode) != MODE_INT)
12349         break;
12350       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12351                                 VAR_INIT_STATUS_INITIALIZED);
12352       {
12353         rtx rtlop1 = XEXP (rtl, 1);
12354         if (GET_MODE (rtlop1) != VOIDmode
12355             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12356                < GET_MODE_BITSIZE (mode))
12357           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12358         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12359                                   VAR_INIT_STATUS_INITIALIZED);
12360       }
12361
12362       if (op0 == 0 || op1 == 0)
12363         break;
12364
12365       mem_loc_result = op0;
12366       add_loc_descr (&mem_loc_result, op1);
12367       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12368       break;
12369
12370     case AND:
12371       op = DW_OP_and;
12372       goto do_binop;
12373
12374     case IOR:
12375       op = DW_OP_or;
12376       goto do_binop;
12377
12378     case XOR:
12379       op = DW_OP_xor;
12380       goto do_binop;
12381
12382     do_binop:
12383       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12384                                 VAR_INIT_STATUS_INITIALIZED);
12385       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12386                                 VAR_INIT_STATUS_INITIALIZED);
12387
12388       if (op0 == 0 || op1 == 0)
12389         break;
12390
12391       mem_loc_result = op0;
12392       add_loc_descr (&mem_loc_result, op1);
12393       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12394       break;
12395
12396     case MOD:
12397       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12398         {
12399           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12400                                         base_type_for_mode (mode, 0),
12401                                         mode, mem_mode);
12402           break;
12403         }
12404
12405       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12406                                 VAR_INIT_STATUS_INITIALIZED);
12407       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12408                                 VAR_INIT_STATUS_INITIALIZED);
12409
12410       if (op0 == 0 || op1 == 0)
12411         break;
12412
12413       mem_loc_result = op0;
12414       add_loc_descr (&mem_loc_result, op1);
12415       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12416       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12417       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12418       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12419       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12420       break;
12421
12422     case UDIV:
12423       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12424         {
12425           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12426             {
12427               op = DW_OP_div;
12428               goto do_binop;
12429             }
12430           mem_loc_result = typed_binop (DW_OP_div, rtl,
12431                                         base_type_for_mode (mode, 1),
12432                                         mode, mem_mode);
12433         }
12434       break;
12435
12436     case NOT:
12437       op = DW_OP_not;
12438       goto do_unop;
12439
12440     case ABS:
12441       op = DW_OP_abs;
12442       goto do_unop;
12443
12444     case NEG:
12445       op = DW_OP_neg;
12446       goto do_unop;
12447
12448     do_unop:
12449       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12450                                 VAR_INIT_STATUS_INITIALIZED);
12451
12452       if (op0 == 0)
12453         break;
12454
12455       mem_loc_result = op0;
12456       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12457       break;
12458
12459     case CONST_INT:
12460       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12461 #ifdef POINTERS_EXTEND_UNSIGNED
12462           || (mode == Pmode
12463               && mem_mode != VOIDmode
12464               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12465 #endif
12466           )
12467         {
12468           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12469           break;
12470         }
12471       if (!dwarf_strict
12472           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12473               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12474         {
12475           dw_die_ref type_die = base_type_for_mode (mode, 1);
12476           if (type_die == NULL)
12477             return NULL;
12478           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12479                                           INTVAL (rtl));
12480           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12481           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12482           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12483           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12484             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12485           else
12486             {
12487               mem_loc_result->dw_loc_oprnd2.val_class
12488                 = dw_val_class_const_double;
12489               mem_loc_result->dw_loc_oprnd2.v.val_double
12490                 = shwi_to_double_int (INTVAL (rtl));
12491             }
12492         }
12493       break;
12494
12495     case CONST_DOUBLE:
12496       if (!dwarf_strict)
12497         {
12498           dw_die_ref type_die;
12499
12500           /* Note that a CONST_DOUBLE rtx could represent either an integer
12501              or a floating-point constant.  A CONST_DOUBLE is used whenever
12502              the constant requires more than one word in order to be
12503              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12504           if (mode == VOIDmode
12505               || (GET_MODE (rtl) == VOIDmode
12506                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12507             break;
12508           type_die = base_type_for_mode (mode,
12509                                          GET_MODE_CLASS (mode) == MODE_INT);
12510           if (type_die == NULL)
12511             return NULL;
12512           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12513           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12514           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12515           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12516           if (SCALAR_FLOAT_MODE_P (mode))
12517             {
12518               unsigned int length = GET_MODE_SIZE (mode);
12519               unsigned char *array
12520                   = (unsigned char*) ggc_alloc_atomic (length);
12521
12522               insert_float (rtl, array);
12523               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12524               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12525               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12526               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12527             }
12528           else
12529             {
12530               mem_loc_result->dw_loc_oprnd2.val_class
12531                 = dw_val_class_const_double;
12532               mem_loc_result->dw_loc_oprnd2.v.val_double
12533                 = rtx_to_double_int (rtl);
12534             }
12535         }
12536       break;
12537
12538     case EQ:
12539       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12540       break;
12541
12542     case GE:
12543       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12544       break;
12545
12546     case GT:
12547       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12548       break;
12549
12550     case LE:
12551       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12552       break;
12553
12554     case LT:
12555       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12556       break;
12557
12558     case NE:
12559       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12560       break;
12561
12562     case GEU:
12563       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12564       break;
12565
12566     case GTU:
12567       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12568       break;
12569
12570     case LEU:
12571       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12572       break;
12573
12574     case LTU:
12575       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12576       break;
12577
12578     case UMIN:
12579     case UMAX:
12580       if (GET_MODE_CLASS (mode) != MODE_INT)
12581         break;
12582       /* FALLTHRU */
12583     case SMIN:
12584     case SMAX:
12585       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12586       break;
12587
12588     case ZERO_EXTRACT:
12589     case SIGN_EXTRACT:
12590       if (CONST_INT_P (XEXP (rtl, 1))
12591           && CONST_INT_P (XEXP (rtl, 2))
12592           && ((unsigned) INTVAL (XEXP (rtl, 1))
12593               + (unsigned) INTVAL (XEXP (rtl, 2))
12594               <= GET_MODE_BITSIZE (mode))
12595           && GET_MODE_CLASS (mode) == MODE_INT
12596           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12597           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12598         {
12599           int shift, size;
12600           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12601                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12602           if (op0 == 0)
12603             break;
12604           if (GET_CODE (rtl) == SIGN_EXTRACT)
12605             op = DW_OP_shra;
12606           else
12607             op = DW_OP_shr;
12608           mem_loc_result = op0;
12609           size = INTVAL (XEXP (rtl, 1));
12610           shift = INTVAL (XEXP (rtl, 2));
12611           if (BITS_BIG_ENDIAN)
12612             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12613                     - shift - size;
12614           if (shift + size != (int) DWARF2_ADDR_SIZE)
12615             {
12616               add_loc_descr (&mem_loc_result,
12617                              int_loc_descriptor (DWARF2_ADDR_SIZE
12618                                                  - shift - size));
12619               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12620             }
12621           if (size != (int) DWARF2_ADDR_SIZE)
12622             {
12623               add_loc_descr (&mem_loc_result,
12624                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12625               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12626             }
12627         }
12628       break;
12629
12630     case IF_THEN_ELSE:
12631       {
12632         dw_loc_descr_ref op2, bra_node, drop_node;
12633         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12634                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12635                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12636                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12637         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12638                                   VAR_INIT_STATUS_INITIALIZED);
12639         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12640                                   VAR_INIT_STATUS_INITIALIZED);
12641         if (op0 == NULL || op1 == NULL || op2 == NULL)
12642           break;
12643
12644         mem_loc_result = op1;
12645         add_loc_descr (&mem_loc_result, op2);
12646         add_loc_descr (&mem_loc_result, op0);
12647         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12648         add_loc_descr (&mem_loc_result, bra_node);
12649         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12650         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12651         add_loc_descr (&mem_loc_result, drop_node);
12652         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12653         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12654       }
12655       break;
12656
12657     case FLOAT_EXTEND:
12658     case FLOAT_TRUNCATE:
12659     case FLOAT:
12660     case UNSIGNED_FLOAT:
12661     case FIX:
12662     case UNSIGNED_FIX:
12663       if (!dwarf_strict)
12664         {
12665           dw_die_ref type_die;
12666           dw_loc_descr_ref cvt;
12667
12668           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12669                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12670           if (op0 == NULL)
12671             break;
12672           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12673               && (GET_CODE (rtl) == FLOAT
12674                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12675                      <= DWARF2_ADDR_SIZE))
12676             {
12677               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12678                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12679               if (type_die == NULL)
12680                 break;
12681               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12682               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12683               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12684               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12685               add_loc_descr (&op0, cvt);
12686             }
12687           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12688           if (type_die == NULL)
12689             break;
12690           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12691           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12692           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12693           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12694           add_loc_descr (&op0, cvt);
12695           if (GET_MODE_CLASS (mode) == MODE_INT
12696               && (GET_CODE (rtl) == FIX
12697                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12698             {
12699               op0 = convert_descriptor_to_mode (mode, op0);
12700               if (op0 == NULL)
12701                 break;
12702             }
12703           mem_loc_result = op0;
12704         }
12705       break;
12706
12707     case CLZ:
12708     case CTZ:
12709     case FFS:
12710       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12711       break;
12712
12713     case POPCOUNT:
12714     case PARITY:
12715       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12716       break;
12717
12718     case BSWAP:
12719       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12720       break;
12721
12722     case ROTATE:
12723     case ROTATERT:
12724       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12725       break;
12726
12727     case COMPARE:
12728     case TRUNCATE:
12729       /* In theory, we could implement the above.  */
12730       /* DWARF cannot represent the unsigned compare operations
12731          natively.  */
12732     case SS_MULT:
12733     case US_MULT:
12734     case SS_DIV:
12735     case US_DIV:
12736     case SS_PLUS:
12737     case US_PLUS:
12738     case SS_MINUS:
12739     case US_MINUS:
12740     case SS_NEG:
12741     case US_NEG:
12742     case SS_ABS:
12743     case SS_ASHIFT:
12744     case US_ASHIFT:
12745     case SS_TRUNCATE:
12746     case US_TRUNCATE:
12747     case UNORDERED:
12748     case ORDERED:
12749     case UNEQ:
12750     case UNGE:
12751     case UNGT:
12752     case UNLE:
12753     case UNLT:
12754     case LTGT:
12755     case FRACT_CONVERT:
12756     case UNSIGNED_FRACT_CONVERT:
12757     case SAT_FRACT:
12758     case UNSIGNED_SAT_FRACT:
12759     case SQRT:
12760     case ASM_OPERANDS:
12761     case VEC_MERGE:
12762     case VEC_SELECT:
12763     case VEC_CONCAT:
12764     case VEC_DUPLICATE:
12765     case UNSPEC:
12766     case HIGH:
12767     case FMA:
12768     case STRICT_LOW_PART:
12769     case CONST_VECTOR:
12770     case CONST_FIXED:
12771     case CLRSB:
12772       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12773          can't express it in the debug info.  This can happen e.g. with some
12774          TLS UNSPECs.  */
12775       break;
12776
12777     case CONST_STRING:
12778       resolve_one_addr (&rtl, NULL);
12779       goto symref;
12780
12781     default:
12782 #ifdef ENABLE_CHECKING
12783       print_rtl (stderr, rtl);
12784       gcc_unreachable ();
12785 #else
12786       break;
12787 #endif
12788     }
12789
12790   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12791     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12792
12793   return mem_loc_result;
12794 }
12795
12796 /* Return a descriptor that describes the concatenation of two locations.
12797    This is typically a complex variable.  */
12798
12799 static dw_loc_descr_ref
12800 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12801 {
12802   dw_loc_descr_ref cc_loc_result = NULL;
12803   dw_loc_descr_ref x0_ref
12804     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12805   dw_loc_descr_ref x1_ref
12806     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12807
12808   if (x0_ref == 0 || x1_ref == 0)
12809     return 0;
12810
12811   cc_loc_result = x0_ref;
12812   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12813
12814   add_loc_descr (&cc_loc_result, x1_ref);
12815   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12816
12817   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12818     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12819
12820   return cc_loc_result;
12821 }
12822
12823 /* Return a descriptor that describes the concatenation of N
12824    locations.  */
12825
12826 static dw_loc_descr_ref
12827 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12828 {
12829   unsigned int i;
12830   dw_loc_descr_ref cc_loc_result = NULL;
12831   unsigned int n = XVECLEN (concatn, 0);
12832
12833   for (i = 0; i < n; ++i)
12834     {
12835       dw_loc_descr_ref ref;
12836       rtx x = XVECEXP (concatn, 0, i);
12837
12838       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12839       if (ref == NULL)
12840         return NULL;
12841
12842       add_loc_descr (&cc_loc_result, ref);
12843       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12844     }
12845
12846   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12847     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12848
12849   return cc_loc_result;
12850 }
12851
12852 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12853    for DEBUG_IMPLICIT_PTR RTL.  */
12854
12855 static dw_loc_descr_ref
12856 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12857 {
12858   dw_loc_descr_ref ret;
12859   dw_die_ref ref;
12860
12861   if (dwarf_strict)
12862     return NULL;
12863   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12864               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12865               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12866   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12867   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12868   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12869   if (ref)
12870     {
12871       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12872       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12873       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12874     }
12875   else
12876     {
12877       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12878       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12879     }
12880   return ret;
12881 }
12882
12883 /* Output a proper Dwarf location descriptor for a variable or parameter
12884    which is either allocated in a register or in a memory location.  For a
12885    register, we just generate an OP_REG and the register number.  For a
12886    memory location we provide a Dwarf postfix expression describing how to
12887    generate the (dynamic) address of the object onto the address stack.
12888
12889    MODE is mode of the decl if this loc_descriptor is going to be used in
12890    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12891    allowed, VOIDmode otherwise.
12892
12893    If we don't know how to describe it, return 0.  */
12894
12895 static dw_loc_descr_ref
12896 loc_descriptor (rtx rtl, enum machine_mode mode,
12897                 enum var_init_status initialized)
12898 {
12899   dw_loc_descr_ref loc_result = NULL;
12900
12901   switch (GET_CODE (rtl))
12902     {
12903     case SUBREG:
12904       /* The case of a subreg may arise when we have a local (register)
12905          variable or a formal (register) parameter which doesn't quite fill
12906          up an entire register.  For now, just assume that it is
12907          legitimate to make the Dwarf info refer to the whole register which
12908          contains the given subreg.  */
12909       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12910         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12911       else
12912         goto do_default;
12913       break;
12914
12915     case REG:
12916       loc_result = reg_loc_descriptor (rtl, initialized);
12917       break;
12918
12919     case MEM:
12920       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12921                                        GET_MODE (rtl), initialized);
12922       if (loc_result == NULL)
12923         loc_result = tls_mem_loc_descriptor (rtl);
12924       if (loc_result == NULL)
12925         {
12926           rtx new_rtl = avoid_constant_pool_reference (rtl);
12927           if (new_rtl != rtl)
12928             loc_result = loc_descriptor (new_rtl, mode, initialized);
12929         }
12930       break;
12931
12932     case CONCAT:
12933       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12934                                           initialized);
12935       break;
12936
12937     case CONCATN:
12938       loc_result = concatn_loc_descriptor (rtl, initialized);
12939       break;
12940
12941     case VAR_LOCATION:
12942       /* Single part.  */
12943       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12944         {
12945           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12946           if (GET_CODE (loc) == EXPR_LIST)
12947             loc = XEXP (loc, 0);
12948           loc_result = loc_descriptor (loc, mode, initialized);
12949           break;
12950         }
12951
12952       rtl = XEXP (rtl, 1);
12953       /* FALLTHRU */
12954
12955     case PARALLEL:
12956       {
12957         rtvec par_elems = XVEC (rtl, 0);
12958         int num_elem = GET_NUM_ELEM (par_elems);
12959         enum machine_mode mode;
12960         int i;
12961
12962         /* Create the first one, so we have something to add to.  */
12963         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12964                                      VOIDmode, initialized);
12965         if (loc_result == NULL)
12966           return NULL;
12967         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12968         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12969         for (i = 1; i < num_elem; i++)
12970           {
12971             dw_loc_descr_ref temp;
12972
12973             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12974                                    VOIDmode, initialized);
12975             if (temp == NULL)
12976               return NULL;
12977             add_loc_descr (&loc_result, temp);
12978             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12979             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12980           }
12981       }
12982       break;
12983
12984     case CONST_INT:
12985       if (mode != VOIDmode && mode != BLKmode)
12986         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12987                                                     INTVAL (rtl));
12988       break;
12989
12990     case CONST_DOUBLE:
12991       if (mode == VOIDmode)
12992         mode = GET_MODE (rtl);
12993
12994       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12995         {
12996           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12997
12998           /* Note that a CONST_DOUBLE rtx could represent either an integer
12999              or a floating-point constant.  A CONST_DOUBLE is used whenever
13000              the constant requires more than one word in order to be
13001              adequately represented.  We output CONST_DOUBLEs as blocks.  */
13002           loc_result = new_loc_descr (DW_OP_implicit_value,
13003                                       GET_MODE_SIZE (mode), 0);
13004           if (SCALAR_FLOAT_MODE_P (mode))
13005             {
13006               unsigned int length = GET_MODE_SIZE (mode);
13007               unsigned char *array
13008                   = (unsigned char*) ggc_alloc_atomic (length);
13009
13010               insert_float (rtl, array);
13011               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13012               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13013               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13014               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13015             }
13016           else
13017             {
13018               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13019               loc_result->dw_loc_oprnd2.v.val_double
13020                 = rtx_to_double_int (rtl);
13021             }
13022         }
13023       break;
13024
13025     case CONST_VECTOR:
13026       if (mode == VOIDmode)
13027         mode = GET_MODE (rtl);
13028
13029       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13030         {
13031           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13032           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13033           unsigned char *array = (unsigned char *)
13034             ggc_alloc_atomic (length * elt_size);
13035           unsigned int i;
13036           unsigned char *p;
13037
13038           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13039           switch (GET_MODE_CLASS (mode))
13040             {
13041             case MODE_VECTOR_INT:
13042               for (i = 0, p = array; i < length; i++, p += elt_size)
13043                 {
13044                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13045                   double_int val = rtx_to_double_int (elt);
13046
13047                   if (elt_size <= sizeof (HOST_WIDE_INT))
13048                     insert_int (double_int_to_shwi (val), elt_size, p);
13049                   else
13050                     {
13051                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13052                       insert_double (val, p);
13053                     }
13054                 }
13055               break;
13056
13057             case MODE_VECTOR_FLOAT:
13058               for (i = 0, p = array; i < length; i++, p += elt_size)
13059                 {
13060                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13061                   insert_float (elt, p);
13062                 }
13063               break;
13064
13065             default:
13066               gcc_unreachable ();
13067             }
13068
13069           loc_result = new_loc_descr (DW_OP_implicit_value,
13070                                       length * elt_size, 0);
13071           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13072           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13073           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13074           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13075         }
13076       break;
13077
13078     case CONST:
13079       if (mode == VOIDmode
13080           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
13081           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
13082           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13083         {
13084           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13085           break;
13086         }
13087       /* FALLTHROUGH */
13088     case SYMBOL_REF:
13089       if (!const_ok_for_output (rtl))
13090         break;
13091     case LABEL_REF:
13092       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13093           && (dwarf_version >= 4 || !dwarf_strict))
13094         {
13095           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13096           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13097           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13098           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13099           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13100         }
13101       break;
13102
13103     case DEBUG_IMPLICIT_PTR:
13104       loc_result = implicit_ptr_descriptor (rtl, 0);
13105       break;
13106
13107     case PLUS:
13108       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13109           && CONST_INT_P (XEXP (rtl, 1)))
13110         {
13111           loc_result
13112             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13113           break;
13114         }
13115       /* FALLTHRU */
13116     do_default:
13117     default:
13118       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13119            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13120            && dwarf_version >= 4)
13121           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13122         {
13123           /* Value expression.  */
13124           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13125           if (loc_result)
13126             add_loc_descr (&loc_result,
13127                            new_loc_descr (DW_OP_stack_value, 0, 0));
13128         }
13129       break;
13130     }
13131
13132   return loc_result;
13133 }
13134
13135 /* We need to figure out what section we should use as the base for the
13136    address ranges where a given location is valid.
13137    1. If this particular DECL has a section associated with it, use that.
13138    2. If this function has a section associated with it, use that.
13139    3. Otherwise, use the text section.
13140    XXX: If you split a variable across multiple sections, we won't notice.  */
13141
13142 static const char *
13143 secname_for_decl (const_tree decl)
13144 {
13145   const char *secname;
13146
13147   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13148     {
13149       tree sectree = DECL_SECTION_NAME (decl);
13150       secname = TREE_STRING_POINTER (sectree);
13151     }
13152   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13153     {
13154       tree sectree = DECL_SECTION_NAME (current_function_decl);
13155       secname = TREE_STRING_POINTER (sectree);
13156     }
13157   else if (cfun && in_cold_section_p)
13158     secname = crtl->subsections.cold_section_label;
13159   else
13160     secname = text_section_label;
13161
13162   return secname;
13163 }
13164
13165 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13166
13167 static bool
13168 decl_by_reference_p (tree decl)
13169 {
13170   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13171            || TREE_CODE (decl) == VAR_DECL)
13172           && DECL_BY_REFERENCE (decl));
13173 }
13174
13175 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13176    for VARLOC.  */
13177
13178 static dw_loc_descr_ref
13179 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13180                enum var_init_status initialized)
13181 {
13182   int have_address = 0;
13183   dw_loc_descr_ref descr;
13184   enum machine_mode mode;
13185
13186   if (want_address != 2)
13187     {
13188       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13189       /* Single part.  */
13190       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13191         {
13192           varloc = PAT_VAR_LOCATION_LOC (varloc);
13193           if (GET_CODE (varloc) == EXPR_LIST)
13194             varloc = XEXP (varloc, 0);
13195           mode = GET_MODE (varloc);
13196           if (MEM_P (varloc))
13197             {
13198               rtx addr = XEXP (varloc, 0);
13199               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13200                                           mode, initialized);
13201               if (descr)
13202                 have_address = 1;
13203               else
13204                 {
13205                   rtx x = avoid_constant_pool_reference (varloc);
13206                   if (x != varloc)
13207                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13208                                                 initialized);
13209                 }
13210             }
13211           else
13212             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13213         }
13214       else
13215         return 0;
13216     }
13217   else
13218     {
13219       if (GET_CODE (varloc) == VAR_LOCATION)
13220         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13221       else
13222         mode = DECL_MODE (loc);
13223       descr = loc_descriptor (varloc, mode, initialized);
13224       have_address = 1;
13225     }
13226
13227   if (!descr)
13228     return 0;
13229
13230   if (want_address == 2 && !have_address
13231       && (dwarf_version >= 4 || !dwarf_strict))
13232     {
13233       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13234         {
13235           expansion_failed (loc, NULL_RTX,
13236                             "DWARF address size mismatch");
13237           return 0;
13238         }
13239       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13240       have_address = 1;
13241     }
13242   /* Show if we can't fill the request for an address.  */
13243   if (want_address && !have_address)
13244     {
13245       expansion_failed (loc, NULL_RTX,
13246                         "Want address and only have value");
13247       return 0;
13248     }
13249
13250   /* If we've got an address and don't want one, dereference.  */
13251   if (!want_address && have_address)
13252     {
13253       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13254       enum dwarf_location_atom op;
13255
13256       if (size > DWARF2_ADDR_SIZE || size == -1)
13257         {
13258           expansion_failed (loc, NULL_RTX,
13259                             "DWARF address size mismatch");
13260           return 0;
13261         }
13262       else if (size == DWARF2_ADDR_SIZE)
13263         op = DW_OP_deref;
13264       else
13265         op = DW_OP_deref_size;
13266
13267       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13268     }
13269
13270   return descr;
13271 }
13272
13273 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13274    if it is not possible.  */
13275
13276 static dw_loc_descr_ref
13277 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13278 {
13279   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13280     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13281   else if (dwarf_version >= 3 || !dwarf_strict)
13282     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13283   else
13284     return NULL;
13285 }
13286
13287 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13288    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13289
13290 static dw_loc_descr_ref
13291 dw_sra_loc_expr (tree decl, rtx loc)
13292 {
13293   rtx p;
13294   unsigned int padsize = 0;
13295   dw_loc_descr_ref descr, *descr_tail;
13296   unsigned HOST_WIDE_INT decl_size;
13297   rtx varloc;
13298   enum var_init_status initialized;
13299
13300   if (DECL_SIZE (decl) == NULL
13301       || !host_integerp (DECL_SIZE (decl), 1))
13302     return NULL;
13303
13304   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13305   descr = NULL;
13306   descr_tail = &descr;
13307
13308   for (p = loc; p; p = XEXP (p, 1))
13309     {
13310       unsigned int bitsize = decl_piece_bitsize (p);
13311       rtx loc_note = *decl_piece_varloc_ptr (p);
13312       dw_loc_descr_ref cur_descr;
13313       dw_loc_descr_ref *tail, last = NULL;
13314       unsigned int opsize = 0;
13315
13316       if (loc_note == NULL_RTX
13317           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13318         {
13319           padsize += bitsize;
13320           continue;
13321         }
13322       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13323       varloc = NOTE_VAR_LOCATION (loc_note);
13324       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13325       if (cur_descr == NULL)
13326         {
13327           padsize += bitsize;
13328           continue;
13329         }
13330
13331       /* Check that cur_descr either doesn't use
13332          DW_OP_*piece operations, or their sum is equal
13333          to bitsize.  Otherwise we can't embed it.  */
13334       for (tail = &cur_descr; *tail != NULL;
13335            tail = &(*tail)->dw_loc_next)
13336         if ((*tail)->dw_loc_opc == DW_OP_piece)
13337           {
13338             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13339                       * BITS_PER_UNIT;
13340             last = *tail;
13341           }
13342         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13343           {
13344             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13345             last = *tail;
13346           }
13347
13348       if (last != NULL && opsize != bitsize)
13349         {
13350           padsize += bitsize;
13351           continue;
13352         }
13353
13354       /* If there is a hole, add DW_OP_*piece after empty DWARF
13355          expression, which means that those bits are optimized out.  */
13356       if (padsize)
13357         {
13358           if (padsize > decl_size)
13359             return NULL;
13360           decl_size -= padsize;
13361           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13362           if (*descr_tail == NULL)
13363             return NULL;
13364           descr_tail = &(*descr_tail)->dw_loc_next;
13365           padsize = 0;
13366         }
13367       *descr_tail = cur_descr;
13368       descr_tail = tail;
13369       if (bitsize > decl_size)
13370         return NULL;
13371       decl_size -= bitsize;
13372       if (last == NULL)
13373         {
13374           HOST_WIDE_INT offset = 0;
13375           if (GET_CODE (varloc) == VAR_LOCATION
13376               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13377             {
13378               varloc = PAT_VAR_LOCATION_LOC (varloc);
13379               if (GET_CODE (varloc) == EXPR_LIST)
13380                 varloc = XEXP (varloc, 0);
13381             }
13382           do 
13383             {
13384               if (GET_CODE (varloc) == CONST
13385                   || GET_CODE (varloc) == SIGN_EXTEND
13386                   || GET_CODE (varloc) == ZERO_EXTEND)
13387                 varloc = XEXP (varloc, 0);
13388               else if (GET_CODE (varloc) == SUBREG)
13389                 varloc = SUBREG_REG (varloc);
13390               else
13391                 break;
13392             }
13393           while (1);
13394           /* DW_OP_bit_size offset should be zero for register
13395              or implicit location descriptions and empty location
13396              descriptions, but for memory addresses needs big endian
13397              adjustment.  */
13398           if (MEM_P (varloc))
13399             {
13400               unsigned HOST_WIDE_INT memsize
13401                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
13402               if (memsize != bitsize)
13403                 {
13404                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13405                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13406                     return NULL;
13407                   if (memsize < bitsize)
13408                     return NULL;
13409                   if (BITS_BIG_ENDIAN)
13410                     offset = memsize - bitsize;
13411                 }
13412             }
13413
13414           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13415           if (*descr_tail == NULL)
13416             return NULL;
13417           descr_tail = &(*descr_tail)->dw_loc_next;
13418         }
13419     }
13420
13421   /* If there were any non-empty expressions, add padding till the end of
13422      the decl.  */
13423   if (descr != NULL && decl_size != 0)
13424     {
13425       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13426       if (*descr_tail == NULL)
13427         return NULL;
13428     }
13429   return descr;
13430 }
13431
13432 /* Return the dwarf representation of the location list LOC_LIST of
13433    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13434    function.  */
13435
13436 static dw_loc_list_ref
13437 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13438 {
13439   const char *endname, *secname;
13440   rtx varloc;
13441   enum var_init_status initialized;
13442   struct var_loc_node *node;
13443   dw_loc_descr_ref descr;
13444   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13445   dw_loc_list_ref list = NULL;
13446   dw_loc_list_ref *listp = &list;
13447
13448   /* Now that we know what section we are using for a base,
13449      actually construct the list of locations.
13450      The first location information is what is passed to the
13451      function that creates the location list, and the remaining
13452      locations just get added on to that list.
13453      Note that we only know the start address for a location
13454      (IE location changes), so to build the range, we use
13455      the range [current location start, next location start].
13456      This means we have to special case the last node, and generate
13457      a range of [last location start, end of function label].  */
13458
13459   secname = secname_for_decl (decl);
13460
13461   for (node = loc_list->first; node; node = node->next)
13462     if (GET_CODE (node->loc) == EXPR_LIST
13463         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13464       {
13465         if (GET_CODE (node->loc) == EXPR_LIST)
13466           {
13467             /* This requires DW_OP_{,bit_}piece, which is not usable
13468                inside DWARF expressions.  */
13469             if (want_address != 2)
13470               continue;
13471             descr = dw_sra_loc_expr (decl, node->loc);
13472             if (descr == NULL)
13473               continue;
13474           }
13475         else
13476           {
13477             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13478             varloc = NOTE_VAR_LOCATION (node->loc);
13479             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13480           }
13481         if (descr)
13482           {
13483             bool range_across_switch = false;
13484             /* If section switch happens in between node->label
13485                and node->next->label (or end of function) and
13486                we can't emit it as a single entry list,
13487                emit two ranges, first one ending at the end
13488                of first partition and second one starting at the
13489                beginning of second partition.  */
13490             if (node == loc_list->last_before_switch
13491                 && (node != loc_list->first || loc_list->first->next)
13492                 && current_function_decl)
13493               {
13494                 endname = current_fde ()->dw_fde_end;
13495                 range_across_switch = true;
13496               }
13497             /* The variable has a location between NODE->LABEL and
13498                NODE->NEXT->LABEL.  */
13499             else if (node->next)
13500               endname = node->next->label;
13501             /* If the variable has a location at the last label
13502                it keeps its location until the end of function.  */
13503             else if (!current_function_decl)
13504               endname = text_end_label;
13505             else
13506               {
13507                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13508                                              current_function_funcdef_no);
13509                 endname = ggc_strdup (label_id);
13510               }
13511
13512             *listp = new_loc_list (descr, node->label, endname, secname);
13513             if (TREE_CODE (decl) == PARM_DECL
13514                 && node == loc_list->first
13515                 && GET_CODE (node->loc) == NOTE
13516                 && strcmp (node->label, endname) == 0)
13517               (*listp)->force = true;
13518             listp = &(*listp)->dw_loc_next;
13519
13520             if (range_across_switch)
13521               {
13522                 if (GET_CODE (node->loc) == EXPR_LIST)
13523                   descr = dw_sra_loc_expr (decl, node->loc);
13524                 else
13525                   {
13526                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13527                     varloc = NOTE_VAR_LOCATION (node->loc);
13528                     descr = dw_loc_list_1 (decl, varloc, want_address,
13529                                            initialized);
13530                   }
13531                 gcc_assert (descr);
13532                 /* The variable has a location between NODE->LABEL and
13533                    NODE->NEXT->LABEL.  */
13534                 if (node->next)
13535                   endname = node->next->label;
13536                 else
13537                   endname = current_fde ()->dw_fde_second_end;
13538                 *listp = new_loc_list (descr,
13539                                        current_fde ()->dw_fde_second_begin,
13540                                        endname, secname);
13541                 listp = &(*listp)->dw_loc_next;
13542               }
13543           }
13544       }
13545
13546   /* Try to avoid the overhead of a location list emitting a location
13547      expression instead, but only if we didn't have more than one
13548      location entry in the first place.  If some entries were not
13549      representable, we don't want to pretend a single entry that was
13550      applies to the entire scope in which the variable is
13551      available.  */
13552   if (list && loc_list->first->next)
13553     gen_llsym (list);
13554
13555   return list;
13556 }
13557
13558 /* Return if the loc_list has only single element and thus can be represented
13559    as location description.   */
13560
13561 static bool
13562 single_element_loc_list_p (dw_loc_list_ref list)
13563 {
13564   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13565   return !list->ll_symbol;
13566 }
13567
13568 /* To each location in list LIST add loc descr REF.  */
13569
13570 static void
13571 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13572 {
13573   dw_loc_descr_ref copy;
13574   add_loc_descr (&list->expr, ref);
13575   list = list->dw_loc_next;
13576   while (list)
13577     {
13578       copy = ggc_alloc_dw_loc_descr_node ();
13579       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13580       add_loc_descr (&list->expr, copy);
13581       while (copy->dw_loc_next)
13582         {
13583           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13584           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13585           copy->dw_loc_next = new_copy;
13586           copy = new_copy;
13587         }
13588       list = list->dw_loc_next;
13589     }
13590 }
13591
13592 /* Given two lists RET and LIST
13593    produce location list that is result of adding expression in LIST
13594    to expression in RET on each possition in program.
13595    Might be destructive on both RET and LIST.
13596
13597    TODO: We handle only simple cases of RET or LIST having at most one
13598    element. General case would inolve sorting the lists in program order
13599    and merging them that will need some additional work.
13600    Adding that will improve quality of debug info especially for SRA-ed
13601    structures.  */
13602
13603 static void
13604 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13605 {
13606   if (!list)
13607     return;
13608   if (!*ret)
13609     {
13610       *ret = list;
13611       return;
13612     }
13613   if (!list->dw_loc_next)
13614     {
13615       add_loc_descr_to_each (*ret, list->expr);
13616       return;
13617     }
13618   if (!(*ret)->dw_loc_next)
13619     {
13620       add_loc_descr_to_each (list, (*ret)->expr);
13621       *ret = list;
13622       return;
13623     }
13624   expansion_failed (NULL_TREE, NULL_RTX,
13625                     "Don't know how to merge two non-trivial"
13626                     " location lists.\n");
13627   *ret = NULL;
13628   return;
13629 }
13630
13631 /* LOC is constant expression.  Try a luck, look it up in constant
13632    pool and return its loc_descr of its address.  */
13633
13634 static dw_loc_descr_ref
13635 cst_pool_loc_descr (tree loc)
13636 {
13637   /* Get an RTL for this, if something has been emitted.  */
13638   rtx rtl = lookup_constant_def (loc);
13639
13640   if (!rtl || !MEM_P (rtl))
13641     {
13642       gcc_assert (!rtl);
13643       return 0;
13644     }
13645   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13646
13647   /* TODO: We might get more coverage if we was actually delaying expansion
13648      of all expressions till end of compilation when constant pools are fully
13649      populated.  */
13650   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13651     {
13652       expansion_failed (loc, NULL_RTX,
13653                         "CST value in contant pool but not marked.");
13654       return 0;
13655     }
13656   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13657                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13658 }
13659
13660 /* Return dw_loc_list representing address of addr_expr LOC
13661    by looking for innder INDIRECT_REF expression and turing it
13662    into simple arithmetics.  */
13663
13664 static dw_loc_list_ref
13665 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13666 {
13667   tree obj, offset;
13668   HOST_WIDE_INT bitsize, bitpos, bytepos;
13669   enum machine_mode mode;
13670   int volatilep;
13671   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13672   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13673
13674   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13675                              &bitsize, &bitpos, &offset, &mode,
13676                              &unsignedp, &volatilep, false);
13677   STRIP_NOPS (obj);
13678   if (bitpos % BITS_PER_UNIT)
13679     {
13680       expansion_failed (loc, NULL_RTX, "bitfield access");
13681       return 0;
13682     }
13683   if (!INDIRECT_REF_P (obj))
13684     {
13685       expansion_failed (obj,
13686                         NULL_RTX, "no indirect ref in inner refrence");
13687       return 0;
13688     }
13689   if (!offset && !bitpos)
13690     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13691   else if (toplev
13692            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13693            && (dwarf_version >= 4 || !dwarf_strict))
13694     {
13695       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13696       if (!list_ret)
13697         return 0;
13698       if (offset)
13699         {
13700           /* Variable offset.  */
13701           list_ret1 = loc_list_from_tree (offset, 0);
13702           if (list_ret1 == 0)
13703             return 0;
13704           add_loc_list (&list_ret, list_ret1);
13705           if (!list_ret)
13706             return 0;
13707           add_loc_descr_to_each (list_ret,
13708                                  new_loc_descr (DW_OP_plus, 0, 0));
13709         }
13710       bytepos = bitpos / BITS_PER_UNIT;
13711       if (bytepos > 0)
13712         add_loc_descr_to_each (list_ret,
13713                                new_loc_descr (DW_OP_plus_uconst,
13714                                               bytepos, 0));
13715       else if (bytepos < 0)
13716         loc_list_plus_const (list_ret, bytepos);
13717       add_loc_descr_to_each (list_ret,
13718                              new_loc_descr (DW_OP_stack_value, 0, 0));
13719     }
13720   return list_ret;
13721 }
13722
13723
13724 /* Generate Dwarf location list representing LOC.
13725    If WANT_ADDRESS is false, expression computing LOC will be computed
13726    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13727    if WANT_ADDRESS is 2, expression computing address useable in location
13728      will be returned (i.e. DW_OP_reg can be used
13729      to refer to register values).  */
13730
13731 static dw_loc_list_ref
13732 loc_list_from_tree (tree loc, int want_address)
13733 {
13734   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13735   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13736   int have_address = 0;
13737   enum dwarf_location_atom op;
13738
13739   /* ??? Most of the time we do not take proper care for sign/zero
13740      extending the values properly.  Hopefully this won't be a real
13741      problem...  */
13742
13743   switch (TREE_CODE (loc))
13744     {
13745     case ERROR_MARK:
13746       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13747       return 0;
13748
13749     case PLACEHOLDER_EXPR:
13750       /* This case involves extracting fields from an object to determine the
13751          position of other fields.  We don't try to encode this here.  The
13752          only user of this is Ada, which encodes the needed information using
13753          the names of types.  */
13754       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13755       return 0;
13756
13757     case CALL_EXPR:
13758       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13759       /* There are no opcodes for these operations.  */
13760       return 0;
13761
13762     case PREINCREMENT_EXPR:
13763     case PREDECREMENT_EXPR:
13764     case POSTINCREMENT_EXPR:
13765     case POSTDECREMENT_EXPR:
13766       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13767       /* There are no opcodes for these operations.  */
13768       return 0;
13769
13770     case ADDR_EXPR:
13771       /* If we already want an address, see if there is INDIRECT_REF inside
13772          e.g. for &this->field.  */
13773       if (want_address)
13774         {
13775           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13776                        (loc, want_address == 2);
13777           if (list_ret)
13778             have_address = 1;
13779           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13780                    && (ret = cst_pool_loc_descr (loc)))
13781             have_address = 1;
13782         }
13783         /* Otherwise, process the argument and look for the address.  */
13784       if (!list_ret && !ret)
13785         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13786       else
13787         {
13788           if (want_address)
13789             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13790           return NULL;
13791         }
13792       break;
13793
13794     case VAR_DECL:
13795       if (DECL_THREAD_LOCAL_P (loc))
13796         {
13797           rtx rtl;
13798           enum dwarf_location_atom first_op;
13799           enum dwarf_location_atom second_op;
13800           bool dtprel = false;
13801
13802           if (targetm.have_tls)
13803             {
13804               /* If this is not defined, we have no way to emit the
13805                  data.  */
13806               if (!targetm.asm_out.output_dwarf_dtprel)
13807                 return 0;
13808
13809                /* The way DW_OP_GNU_push_tls_address is specified, we
13810                   can only look up addresses of objects in the current
13811                   module.  We used DW_OP_addr as first op, but that's
13812                   wrong, because DW_OP_addr is relocated by the debug
13813                   info consumer, while DW_OP_GNU_push_tls_address
13814                   operand shouldn't be.  */
13815               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13816                 return 0;
13817               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13818               dtprel = true;
13819               second_op = DW_OP_GNU_push_tls_address;
13820             }
13821           else
13822             {
13823               if (!targetm.emutls.debug_form_tls_address
13824                   || !(dwarf_version >= 3 || !dwarf_strict))
13825                 return 0;
13826               /* We stuffed the control variable into the DECL_VALUE_EXPR
13827                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13828                  no longer appear in gimple code.  We used the control
13829                  variable in specific so that we could pick it up here.  */
13830               loc = DECL_VALUE_EXPR (loc);
13831               first_op = DW_OP_addr;
13832               second_op = DW_OP_form_tls_address;
13833             }
13834
13835           rtl = rtl_for_decl_location (loc);
13836           if (rtl == NULL_RTX)
13837             return 0;
13838
13839           if (!MEM_P (rtl))
13840             return 0;
13841           rtl = XEXP (rtl, 0);
13842           if (! CONSTANT_P (rtl))
13843             return 0;
13844
13845           ret = new_loc_descr (first_op, 0, 0);
13846           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13847           ret->dw_loc_oprnd1.v.val_addr = rtl;
13848           ret->dtprel = dtprel;
13849
13850           ret1 = new_loc_descr (second_op, 0, 0);
13851           add_loc_descr (&ret, ret1);
13852
13853           have_address = 1;
13854           break;
13855         }
13856       /* FALLTHRU */
13857
13858     case PARM_DECL:
13859     case RESULT_DECL:
13860       if (DECL_HAS_VALUE_EXPR_P (loc))
13861         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13862                                    want_address);
13863       /* FALLTHRU */
13864
13865     case FUNCTION_DECL:
13866       {
13867         rtx rtl;
13868         var_loc_list *loc_list = lookup_decl_loc (loc);
13869
13870         if (loc_list && loc_list->first)
13871           {
13872             list_ret = dw_loc_list (loc_list, loc, want_address);
13873             have_address = want_address != 0;
13874             break;
13875           }
13876         rtl = rtl_for_decl_location (loc);
13877         if (rtl == NULL_RTX)
13878           {
13879             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13880             return 0;
13881           }
13882         else if (CONST_INT_P (rtl))
13883           {
13884             HOST_WIDE_INT val = INTVAL (rtl);
13885             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13886               val &= GET_MODE_MASK (DECL_MODE (loc));
13887             ret = int_loc_descriptor (val);
13888           }
13889         else if (GET_CODE (rtl) == CONST_STRING)
13890           {
13891             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13892             return 0;
13893           }
13894         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13895           {
13896             ret = new_loc_descr (DW_OP_addr, 0, 0);
13897             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13898             ret->dw_loc_oprnd1.v.val_addr = rtl;
13899           }
13900         else
13901           {
13902             enum machine_mode mode, mem_mode;
13903
13904             /* Certain constructs can only be represented at top-level.  */
13905             if (want_address == 2)
13906               {
13907                 ret = loc_descriptor (rtl, VOIDmode,
13908                                       VAR_INIT_STATUS_INITIALIZED);
13909                 have_address = 1;
13910               }
13911             else
13912               {
13913                 mode = GET_MODE (rtl);
13914                 mem_mode = VOIDmode;
13915                 if (MEM_P (rtl))
13916                   {
13917                     mem_mode = mode;
13918                     mode = get_address_mode (rtl);
13919                     rtl = XEXP (rtl, 0);
13920                     have_address = 1;
13921                   }
13922                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13923                                           VAR_INIT_STATUS_INITIALIZED);
13924               }
13925             if (!ret)
13926               expansion_failed (loc, rtl,
13927                                 "failed to produce loc descriptor for rtl");
13928           }
13929       }
13930       break;
13931
13932     case MEM_REF:
13933       /* ??? FIXME.  */
13934       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13935         return 0;
13936       /* Fallthru.  */
13937     case INDIRECT_REF:
13938       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13939       have_address = 1;
13940       break;
13941
13942     case COMPOUND_EXPR:
13943       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13944
13945     CASE_CONVERT:
13946     case VIEW_CONVERT_EXPR:
13947     case SAVE_EXPR:
13948     case MODIFY_EXPR:
13949       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13950
13951     case COMPONENT_REF:
13952     case BIT_FIELD_REF:
13953     case ARRAY_REF:
13954     case ARRAY_RANGE_REF:
13955     case REALPART_EXPR:
13956     case IMAGPART_EXPR:
13957       {
13958         tree obj, offset;
13959         HOST_WIDE_INT bitsize, bitpos, bytepos;
13960         enum machine_mode mode;
13961         int volatilep;
13962         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13963
13964         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13965                                    &unsignedp, &volatilep, false);
13966
13967         gcc_assert (obj != loc);
13968
13969         list_ret = loc_list_from_tree (obj,
13970                                        want_address == 2
13971                                        && !bitpos && !offset ? 2 : 1);
13972         /* TODO: We can extract value of the small expression via shifting even
13973            for nonzero bitpos.  */
13974         if (list_ret == 0)
13975           return 0;
13976         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13977           {
13978             expansion_failed (loc, NULL_RTX,
13979                               "bitfield access");
13980             return 0;
13981           }
13982
13983         if (offset != NULL_TREE)
13984           {
13985             /* Variable offset.  */
13986             list_ret1 = loc_list_from_tree (offset, 0);
13987             if (list_ret1 == 0)
13988               return 0;
13989             add_loc_list (&list_ret, list_ret1);
13990             if (!list_ret)
13991               return 0;
13992             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13993           }
13994
13995         bytepos = bitpos / BITS_PER_UNIT;
13996         if (bytepos > 0)
13997           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13998         else if (bytepos < 0)
13999           loc_list_plus_const (list_ret, bytepos);
14000
14001         have_address = 1;
14002         break;
14003       }
14004
14005     case INTEGER_CST:
14006       if ((want_address || !host_integerp (loc, 0))
14007           && (ret = cst_pool_loc_descr (loc)))
14008         have_address = 1;
14009       else if (want_address == 2
14010                && host_integerp (loc, 0)
14011                && (ret = address_of_int_loc_descriptor
14012                            (int_size_in_bytes (TREE_TYPE (loc)),
14013                             tree_low_cst (loc, 0))))
14014         have_address = 1;
14015       else if (host_integerp (loc, 0))
14016         ret = int_loc_descriptor (tree_low_cst (loc, 0));
14017       else
14018         {
14019           expansion_failed (loc, NULL_RTX,
14020                             "Integer operand is not host integer");
14021           return 0;
14022         }
14023       break;
14024
14025     case CONSTRUCTOR:
14026     case REAL_CST:
14027     case STRING_CST:
14028     case COMPLEX_CST:
14029       if ((ret = cst_pool_loc_descr (loc)))
14030         have_address = 1;
14031       else
14032       /* We can construct small constants here using int_loc_descriptor.  */
14033         expansion_failed (loc, NULL_RTX,
14034                           "constructor or constant not in constant pool");
14035       break;
14036
14037     case TRUTH_AND_EXPR:
14038     case TRUTH_ANDIF_EXPR:
14039     case BIT_AND_EXPR:
14040       op = DW_OP_and;
14041       goto do_binop;
14042
14043     case TRUTH_XOR_EXPR:
14044     case BIT_XOR_EXPR:
14045       op = DW_OP_xor;
14046       goto do_binop;
14047
14048     case TRUTH_OR_EXPR:
14049     case TRUTH_ORIF_EXPR:
14050     case BIT_IOR_EXPR:
14051       op = DW_OP_or;
14052       goto do_binop;
14053
14054     case FLOOR_DIV_EXPR:
14055     case CEIL_DIV_EXPR:
14056     case ROUND_DIV_EXPR:
14057     case TRUNC_DIV_EXPR:
14058       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14059         return 0;
14060       op = DW_OP_div;
14061       goto do_binop;
14062
14063     case MINUS_EXPR:
14064       op = DW_OP_minus;
14065       goto do_binop;
14066
14067     case FLOOR_MOD_EXPR:
14068     case CEIL_MOD_EXPR:
14069     case ROUND_MOD_EXPR:
14070     case TRUNC_MOD_EXPR:
14071       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14072         {
14073           op = DW_OP_mod;
14074           goto do_binop;
14075         }
14076       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14077       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14078       if (list_ret == 0 || list_ret1 == 0)
14079         return 0;
14080
14081       add_loc_list (&list_ret, list_ret1);
14082       if (list_ret == 0)
14083         return 0;
14084       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14085       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14086       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14087       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14088       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14089       break;
14090
14091     case MULT_EXPR:
14092       op = DW_OP_mul;
14093       goto do_binop;
14094
14095     case LSHIFT_EXPR:
14096       op = DW_OP_shl;
14097       goto do_binop;
14098
14099     case RSHIFT_EXPR:
14100       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14101       goto do_binop;
14102
14103     case POINTER_PLUS_EXPR:
14104     case PLUS_EXPR:
14105       if (host_integerp (TREE_OPERAND (loc, 1), 0))
14106         {
14107           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14108           if (list_ret == 0)
14109             return 0;
14110
14111           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14112           break;
14113         }
14114
14115       op = DW_OP_plus;
14116       goto do_binop;
14117
14118     case LE_EXPR:
14119       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14120         return 0;
14121
14122       op = DW_OP_le;
14123       goto do_binop;
14124
14125     case GE_EXPR:
14126       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14127         return 0;
14128
14129       op = DW_OP_ge;
14130       goto do_binop;
14131
14132     case LT_EXPR:
14133       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14134         return 0;
14135
14136       op = DW_OP_lt;
14137       goto do_binop;
14138
14139     case GT_EXPR:
14140       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14141         return 0;
14142
14143       op = DW_OP_gt;
14144       goto do_binop;
14145
14146     case EQ_EXPR:
14147       op = DW_OP_eq;
14148       goto do_binop;
14149
14150     case NE_EXPR:
14151       op = DW_OP_ne;
14152       goto do_binop;
14153
14154     do_binop:
14155       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14156       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14157       if (list_ret == 0 || list_ret1 == 0)
14158         return 0;
14159
14160       add_loc_list (&list_ret, list_ret1);
14161       if (list_ret == 0)
14162         return 0;
14163       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14164       break;
14165
14166     case TRUTH_NOT_EXPR:
14167     case BIT_NOT_EXPR:
14168       op = DW_OP_not;
14169       goto do_unop;
14170
14171     case ABS_EXPR:
14172       op = DW_OP_abs;
14173       goto do_unop;
14174
14175     case NEGATE_EXPR:
14176       op = DW_OP_neg;
14177       goto do_unop;
14178
14179     do_unop:
14180       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14181       if (list_ret == 0)
14182         return 0;
14183
14184       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14185       break;
14186
14187     case MIN_EXPR:
14188     case MAX_EXPR:
14189       {
14190         const enum tree_code code =
14191           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14192
14193         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14194                       build2 (code, integer_type_node,
14195                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14196                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14197       }
14198
14199       /* ... fall through ...  */
14200
14201     case COND_EXPR:
14202       {
14203         dw_loc_descr_ref lhs
14204           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14205         dw_loc_list_ref rhs
14206           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14207         dw_loc_descr_ref bra_node, jump_node, tmp;
14208
14209         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14210         if (list_ret == 0 || lhs == 0 || rhs == 0)
14211           return 0;
14212
14213         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14214         add_loc_descr_to_each (list_ret, bra_node);
14215
14216         add_loc_list (&list_ret, rhs);
14217         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14218         add_loc_descr_to_each (list_ret, jump_node);
14219
14220         add_loc_descr_to_each (list_ret, lhs);
14221         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14222         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14223
14224         /* ??? Need a node to point the skip at.  Use a nop.  */
14225         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14226         add_loc_descr_to_each (list_ret, tmp);
14227         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14228         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14229       }
14230       break;
14231
14232     case FIX_TRUNC_EXPR:
14233       return 0;
14234
14235     default:
14236       /* Leave front-end specific codes as simply unknown.  This comes
14237          up, for instance, with the C STMT_EXPR.  */
14238       if ((unsigned int) TREE_CODE (loc)
14239           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14240         {
14241           expansion_failed (loc, NULL_RTX,
14242                             "language specific tree node");
14243           return 0;
14244         }
14245
14246 #ifdef ENABLE_CHECKING
14247       /* Otherwise this is a generic code; we should just lists all of
14248          these explicitly.  We forgot one.  */
14249       gcc_unreachable ();
14250 #else
14251       /* In a release build, we want to degrade gracefully: better to
14252          generate incomplete debugging information than to crash.  */
14253       return NULL;
14254 #endif
14255     }
14256
14257   if (!ret && !list_ret)
14258     return 0;
14259
14260   if (want_address == 2 && !have_address
14261       && (dwarf_version >= 4 || !dwarf_strict))
14262     {
14263       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14264         {
14265           expansion_failed (loc, NULL_RTX,
14266                             "DWARF address size mismatch");
14267           return 0;
14268         }
14269       if (ret)
14270         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14271       else
14272         add_loc_descr_to_each (list_ret,
14273                                new_loc_descr (DW_OP_stack_value, 0, 0));
14274       have_address = 1;
14275     }
14276   /* Show if we can't fill the request for an address.  */
14277   if (want_address && !have_address)
14278     {
14279       expansion_failed (loc, NULL_RTX,
14280                         "Want address and only have value");
14281       return 0;
14282     }
14283
14284   gcc_assert (!ret || !list_ret);
14285
14286   /* If we've got an address and don't want one, dereference.  */
14287   if (!want_address && have_address)
14288     {
14289       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14290
14291       if (size > DWARF2_ADDR_SIZE || size == -1)
14292         {
14293           expansion_failed (loc, NULL_RTX,
14294                             "DWARF address size mismatch");
14295           return 0;
14296         }
14297       else if (size == DWARF2_ADDR_SIZE)
14298         op = DW_OP_deref;
14299       else
14300         op = DW_OP_deref_size;
14301
14302       if (ret)
14303         add_loc_descr (&ret, new_loc_descr (op, size, 0));
14304       else
14305         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14306     }
14307   if (ret)
14308     list_ret = new_loc_list (ret, NULL, NULL, NULL);
14309
14310   return list_ret;
14311 }
14312
14313 /* Same as above but return only single location expression.  */
14314 static dw_loc_descr_ref
14315 loc_descriptor_from_tree (tree loc, int want_address)
14316 {
14317   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14318   if (!ret)
14319     return NULL;
14320   if (ret->dw_loc_next)
14321     {
14322       expansion_failed (loc, NULL_RTX,
14323                         "Location list where only loc descriptor needed");
14324       return NULL;
14325     }
14326   return ret->expr;
14327 }
14328
14329 /* Given a value, round it up to the lowest multiple of `boundary'
14330    which is not less than the value itself.  */
14331
14332 static inline HOST_WIDE_INT
14333 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14334 {
14335   return (((value + boundary - 1) / boundary) * boundary);
14336 }
14337
14338 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14339    pointer to the declared type for the relevant field variable, or return
14340    `integer_type_node' if the given node turns out to be an
14341    ERROR_MARK node.  */
14342
14343 static inline tree
14344 field_type (const_tree decl)
14345 {
14346   tree type;
14347
14348   if (TREE_CODE (decl) == ERROR_MARK)
14349     return integer_type_node;
14350
14351   type = DECL_BIT_FIELD_TYPE (decl);
14352   if (type == NULL_TREE)
14353     type = TREE_TYPE (decl);
14354
14355   return type;
14356 }
14357
14358 /* Given a pointer to a tree node, return the alignment in bits for
14359    it, or else return BITS_PER_WORD if the node actually turns out to
14360    be an ERROR_MARK node.  */
14361
14362 static inline unsigned
14363 simple_type_align_in_bits (const_tree type)
14364 {
14365   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14366 }
14367
14368 static inline unsigned
14369 simple_decl_align_in_bits (const_tree decl)
14370 {
14371   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14372 }
14373
14374 /* Return the result of rounding T up to ALIGN.  */
14375
14376 static inline double_int
14377 round_up_to_align (double_int t, unsigned int align)
14378 {
14379   double_int alignd = uhwi_to_double_int (align);
14380   t = double_int_add (t, alignd);
14381   t = double_int_add (t, double_int_minus_one);
14382   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
14383   t = double_int_mul (t, alignd);
14384   return t;
14385 }
14386
14387 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14388    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14389    or return 0 if we are unable to determine what that offset is, either
14390    because the argument turns out to be a pointer to an ERROR_MARK node, or
14391    because the offset is actually variable.  (We can't handle the latter case
14392    just yet).  */
14393
14394 static HOST_WIDE_INT
14395 field_byte_offset (const_tree decl)
14396 {
14397   double_int object_offset_in_bits;
14398   double_int object_offset_in_bytes;
14399   double_int bitpos_int;
14400
14401   if (TREE_CODE (decl) == ERROR_MARK)
14402     return 0;
14403
14404   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14405
14406   /* We cannot yet cope with fields whose positions are variable, so
14407      for now, when we see such things, we simply return 0.  Someday, we may
14408      be able to handle such cases, but it will be damn difficult.  */
14409   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14410     return 0;
14411
14412   bitpos_int = tree_to_double_int (bit_position (decl));
14413
14414 #ifdef PCC_BITFIELD_TYPE_MATTERS
14415   if (PCC_BITFIELD_TYPE_MATTERS)
14416     {
14417       tree type;
14418       tree field_size_tree;
14419       double_int deepest_bitpos;
14420       double_int field_size_in_bits;
14421       unsigned int type_align_in_bits;
14422       unsigned int decl_align_in_bits;
14423       double_int type_size_in_bits;
14424
14425       type = field_type (decl);
14426       type_size_in_bits = double_int_type_size_in_bits (type);
14427       type_align_in_bits = simple_type_align_in_bits (type);
14428
14429       field_size_tree = DECL_SIZE (decl);
14430
14431       /* The size could be unspecified if there was an error, or for
14432          a flexible array member.  */
14433       if (!field_size_tree)
14434         field_size_tree = bitsize_zero_node;
14435
14436       /* If the size of the field is not constant, use the type size.  */
14437       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14438         field_size_in_bits = tree_to_double_int (field_size_tree);
14439       else
14440         field_size_in_bits = type_size_in_bits;
14441
14442       decl_align_in_bits = simple_decl_align_in_bits (decl);
14443
14444       /* The GCC front-end doesn't make any attempt to keep track of the
14445          starting bit offset (relative to the start of the containing
14446          structure type) of the hypothetical "containing object" for a
14447          bit-field.  Thus, when computing the byte offset value for the
14448          start of the "containing object" of a bit-field, we must deduce
14449          this information on our own. This can be rather tricky to do in
14450          some cases.  For example, handling the following structure type
14451          definition when compiling for an i386/i486 target (which only
14452          aligns long long's to 32-bit boundaries) can be very tricky:
14453
14454          struct S { int field1; long long field2:31; };
14455
14456          Fortunately, there is a simple rule-of-thumb which can be used
14457          in such cases.  When compiling for an i386/i486, GCC will
14458          allocate 8 bytes for the structure shown above.  It decides to
14459          do this based upon one simple rule for bit-field allocation.
14460          GCC allocates each "containing object" for each bit-field at
14461          the first (i.e. lowest addressed) legitimate alignment boundary
14462          (based upon the required minimum alignment for the declared
14463          type of the field) which it can possibly use, subject to the
14464          condition that there is still enough available space remaining
14465          in the containing object (when allocated at the selected point)
14466          to fully accommodate all of the bits of the bit-field itself.
14467
14468          This simple rule makes it obvious why GCC allocates 8 bytes for
14469          each object of the structure type shown above.  When looking
14470          for a place to allocate the "containing object" for `field2',
14471          the compiler simply tries to allocate a 64-bit "containing
14472          object" at each successive 32-bit boundary (starting at zero)
14473          until it finds a place to allocate that 64- bit field such that
14474          at least 31 contiguous (and previously unallocated) bits remain
14475          within that selected 64 bit field.  (As it turns out, for the
14476          example above, the compiler finds it is OK to allocate the
14477          "containing object" 64-bit field at bit-offset zero within the
14478          structure type.)
14479
14480          Here we attempt to work backwards from the limited set of facts
14481          we're given, and we try to deduce from those facts, where GCC
14482          must have believed that the containing object started (within
14483          the structure type). The value we deduce is then used (by the
14484          callers of this routine) to generate DW_AT_location and
14485          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14486          the case of DW_AT_location, regular fields as well).  */
14487
14488       /* Figure out the bit-distance from the start of the structure to
14489          the "deepest" bit of the bit-field.  */
14490       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14491
14492       /* This is the tricky part.  Use some fancy footwork to deduce
14493          where the lowest addressed bit of the containing object must
14494          be.  */
14495       object_offset_in_bits
14496         = double_int_sub (deepest_bitpos, type_size_in_bits);
14497
14498       /* Round up to type_align by default.  This works best for
14499          bitfields.  */
14500       object_offset_in_bits
14501         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14502
14503       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14504         {
14505           object_offset_in_bits
14506             = double_int_sub (deepest_bitpos, type_size_in_bits);
14507
14508           /* Round up to decl_align instead.  */
14509           object_offset_in_bits
14510             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14511         }
14512     }
14513   else
14514 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14515     object_offset_in_bits = bitpos_int;
14516
14517   object_offset_in_bytes
14518     = double_int_div (object_offset_in_bits,
14519                       uhwi_to_double_int (BITS_PER_UNIT), true,
14520                       TRUNC_DIV_EXPR);
14521   return double_int_to_shwi (object_offset_in_bytes);
14522 }
14523 \f
14524 /* The following routines define various Dwarf attributes and any data
14525    associated with them.  */
14526
14527 /* Add a location description attribute value to a DIE.
14528
14529    This emits location attributes suitable for whole variables and
14530    whole parameters.  Note that the location attributes for struct fields are
14531    generated by the routine `data_member_location_attribute' below.  */
14532
14533 static inline void
14534 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14535                              dw_loc_list_ref descr)
14536 {
14537   if (descr == 0)
14538     return;
14539   if (single_element_loc_list_p (descr))
14540     add_AT_loc (die, attr_kind, descr->expr);
14541   else
14542     add_AT_loc_list (die, attr_kind, descr);
14543 }
14544
14545 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14546
14547 static void
14548 add_accessibility_attribute (dw_die_ref die, tree decl)
14549 {
14550   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14551      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14552      the default has always been DW_ACCESS_public.  */
14553   if (TREE_PROTECTED (decl))
14554     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14555   else if (TREE_PRIVATE (decl))
14556     {
14557       if (dwarf_version == 2
14558           || die->die_parent == NULL
14559           || die->die_parent->die_tag != DW_TAG_class_type)
14560         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14561     }
14562   else if (dwarf_version > 2
14563            && die->die_parent
14564            && die->die_parent->die_tag == DW_TAG_class_type)
14565     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14566 }
14567
14568 /* Attach the specialized form of location attribute used for data members of
14569    struct and union types.  In the special case of a FIELD_DECL node which
14570    represents a bit-field, the "offset" part of this special location
14571    descriptor must indicate the distance in bytes from the lowest-addressed
14572    byte of the containing struct or union type to the lowest-addressed byte of
14573    the "containing object" for the bit-field.  (See the `field_byte_offset'
14574    function above).
14575
14576    For any given bit-field, the "containing object" is a hypothetical object
14577    (of some integral or enum type) within which the given bit-field lives.  The
14578    type of this hypothetical "containing object" is always the same as the
14579    declared type of the individual bit-field itself (for GCC anyway... the
14580    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14581    bytes) of the hypothetical "containing object" which will be given in the
14582    DW_AT_byte_size attribute for this bit-field.  (See the
14583    `byte_size_attribute' function below.)  It is also used when calculating the
14584    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14585    function below.)  */
14586
14587 static void
14588 add_data_member_location_attribute (dw_die_ref die, tree decl)
14589 {
14590   HOST_WIDE_INT offset;
14591   dw_loc_descr_ref loc_descr = 0;
14592
14593   if (TREE_CODE (decl) == TREE_BINFO)
14594     {
14595       /* We're working on the TAG_inheritance for a base class.  */
14596       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14597         {
14598           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14599              aren't at a fixed offset from all (sub)objects of the same
14600              type.  We need to extract the appropriate offset from our
14601              vtable.  The following dwarf expression means
14602
14603                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14604
14605              This is specific to the V3 ABI, of course.  */
14606
14607           dw_loc_descr_ref tmp;
14608
14609           /* Make a copy of the object address.  */
14610           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14611           add_loc_descr (&loc_descr, tmp);
14612
14613           /* Extract the vtable address.  */
14614           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14615           add_loc_descr (&loc_descr, tmp);
14616
14617           /* Calculate the address of the offset.  */
14618           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14619           gcc_assert (offset < 0);
14620
14621           tmp = int_loc_descriptor (-offset);
14622           add_loc_descr (&loc_descr, tmp);
14623           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14624           add_loc_descr (&loc_descr, tmp);
14625
14626           /* Extract the offset.  */
14627           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14628           add_loc_descr (&loc_descr, tmp);
14629
14630           /* Add it to the object address.  */
14631           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14632           add_loc_descr (&loc_descr, tmp);
14633         }
14634       else
14635         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14636     }
14637   else
14638     offset = field_byte_offset (decl);
14639
14640   if (! loc_descr)
14641     {
14642       if (dwarf_version > 2)
14643         {
14644           /* Don't need to output a location expression, just the constant. */
14645           if (offset < 0)
14646             add_AT_int (die, DW_AT_data_member_location, offset);
14647           else
14648             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14649           return;
14650         }
14651       else
14652         {
14653           enum dwarf_location_atom op;
14654
14655           /* The DWARF2 standard says that we should assume that the structure
14656              address is already on the stack, so we can specify a structure
14657              field address by using DW_OP_plus_uconst.  */
14658
14659 #ifdef MIPS_DEBUGGING_INFO
14660           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14661              operator correctly.  It works only if we leave the offset on the
14662              stack.  */
14663           op = DW_OP_constu;
14664 #else
14665           op = DW_OP_plus_uconst;
14666 #endif
14667
14668           loc_descr = new_loc_descr (op, offset, 0);
14669         }
14670     }
14671
14672   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14673 }
14674
14675 /* Writes integer values to dw_vec_const array.  */
14676
14677 static void
14678 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14679 {
14680   while (size != 0)
14681     {
14682       *dest++ = val & 0xff;
14683       val >>= 8;
14684       --size;
14685     }
14686 }
14687
14688 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14689
14690 static HOST_WIDE_INT
14691 extract_int (const unsigned char *src, unsigned int size)
14692 {
14693   HOST_WIDE_INT val = 0;
14694
14695   src += size;
14696   while (size != 0)
14697     {
14698       val <<= 8;
14699       val |= *--src & 0xff;
14700       --size;
14701     }
14702   return val;
14703 }
14704
14705 /* Writes double_int values to dw_vec_const array.  */
14706
14707 static void
14708 insert_double (double_int val, unsigned char *dest)
14709 {
14710   unsigned char *p0 = dest;
14711   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14712
14713   if (WORDS_BIG_ENDIAN)
14714     {
14715       p0 = p1;
14716       p1 = dest;
14717     }
14718
14719   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14720   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14721 }
14722
14723 /* Writes floating point values to dw_vec_const array.  */
14724
14725 static void
14726 insert_float (const_rtx rtl, unsigned char *array)
14727 {
14728   REAL_VALUE_TYPE rv;
14729   long val[4];
14730   int i;
14731
14732   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14733   real_to_target (val, &rv, GET_MODE (rtl));
14734
14735   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14736   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14737     {
14738       insert_int (val[i], 4, array);
14739       array += 4;
14740     }
14741 }
14742
14743 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14744    does not have a "location" either in memory or in a register.  These
14745    things can arise in GNU C when a constant is passed as an actual parameter
14746    to an inlined function.  They can also arise in C++ where declared
14747    constants do not necessarily get memory "homes".  */
14748
14749 static bool
14750 add_const_value_attribute (dw_die_ref die, rtx rtl)
14751 {
14752   switch (GET_CODE (rtl))
14753     {
14754     case CONST_INT:
14755       {
14756         HOST_WIDE_INT val = INTVAL (rtl);
14757
14758         if (val < 0)
14759           add_AT_int (die, DW_AT_const_value, val);
14760         else
14761           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14762       }
14763       return true;
14764
14765     case CONST_DOUBLE:
14766       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14767          floating-point constant.  A CONST_DOUBLE is used whenever the
14768          constant requires more than one word in order to be adequately
14769          represented.  */
14770       {
14771         enum machine_mode mode = GET_MODE (rtl);
14772
14773         if (SCALAR_FLOAT_MODE_P (mode))
14774           {
14775             unsigned int length = GET_MODE_SIZE (mode);
14776             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14777
14778             insert_float (rtl, array);
14779             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14780           }
14781         else
14782           add_AT_double (die, DW_AT_const_value,
14783                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14784       }
14785       return true;
14786
14787     case CONST_VECTOR:
14788       {
14789         enum machine_mode mode = GET_MODE (rtl);
14790         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14791         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14792         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14793           (length * elt_size);
14794         unsigned int i;
14795         unsigned char *p;
14796
14797         switch (GET_MODE_CLASS (mode))
14798           {
14799           case MODE_VECTOR_INT:
14800             for (i = 0, p = array; i < length; i++, p += elt_size)
14801               {
14802                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14803                 double_int val = rtx_to_double_int (elt);
14804
14805                 if (elt_size <= sizeof (HOST_WIDE_INT))
14806                   insert_int (double_int_to_shwi (val), elt_size, p);
14807                 else
14808                   {
14809                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14810                     insert_double (val, p);
14811                   }
14812               }
14813             break;
14814
14815           case MODE_VECTOR_FLOAT:
14816             for (i = 0, p = array; i < length; i++, p += elt_size)
14817               {
14818                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14819                 insert_float (elt, p);
14820               }
14821             break;
14822
14823           default:
14824             gcc_unreachable ();
14825           }
14826
14827         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14828       }
14829       return true;
14830
14831     case CONST_STRING:
14832       if (dwarf_version >= 4 || !dwarf_strict)
14833         {
14834           dw_loc_descr_ref loc_result;
14835           resolve_one_addr (&rtl, NULL);
14836         rtl_addr:
14837           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14838           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14839           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14840           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14841           add_AT_loc (die, DW_AT_location, loc_result);
14842           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14843           return true;
14844         }
14845       return false;
14846
14847     case CONST:
14848       if (CONSTANT_P (XEXP (rtl, 0)))
14849         return add_const_value_attribute (die, XEXP (rtl, 0));
14850       /* FALLTHROUGH */
14851     case SYMBOL_REF:
14852       if (!const_ok_for_output (rtl))
14853         return false;
14854     case LABEL_REF:
14855       if (dwarf_version >= 4 || !dwarf_strict)
14856         goto rtl_addr;
14857       return false;
14858
14859     case PLUS:
14860       /* In cases where an inlined instance of an inline function is passed
14861          the address of an `auto' variable (which is local to the caller) we
14862          can get a situation where the DECL_RTL of the artificial local
14863          variable (for the inlining) which acts as a stand-in for the
14864          corresponding formal parameter (of the inline function) will look
14865          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14866          exactly a compile-time constant expression, but it isn't the address
14867          of the (artificial) local variable either.  Rather, it represents the
14868          *value* which the artificial local variable always has during its
14869          lifetime.  We currently have no way to represent such quasi-constant
14870          values in Dwarf, so for now we just punt and generate nothing.  */
14871       return false;
14872
14873     case HIGH:
14874     case CONST_FIXED:
14875       return false;
14876
14877     case MEM:
14878       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14879           && MEM_READONLY_P (rtl)
14880           && GET_MODE (rtl) == BLKmode)
14881         {
14882           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14883           return true;
14884         }
14885       return false;
14886
14887     default:
14888       /* No other kinds of rtx should be possible here.  */
14889       gcc_unreachable ();
14890     }
14891   return false;
14892 }
14893
14894 /* Determine whether the evaluation of EXPR references any variables
14895    or functions which aren't otherwise used (and therefore may not be
14896    output).  */
14897 static tree
14898 reference_to_unused (tree * tp, int * walk_subtrees,
14899                      void * data ATTRIBUTE_UNUSED)
14900 {
14901   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14902     *walk_subtrees = 0;
14903
14904   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14905       && ! TREE_ASM_WRITTEN (*tp))
14906     return *tp;
14907   /* ???  The C++ FE emits debug information for using decls, so
14908      putting gcc_unreachable here falls over.  See PR31899.  For now
14909      be conservative.  */
14910   else if (!cgraph_global_info_ready
14911            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14912     return *tp;
14913   else if (TREE_CODE (*tp) == VAR_DECL)
14914     {
14915       struct varpool_node *node = varpool_get_node (*tp);
14916       if (!node || !node->needed)
14917         return *tp;
14918     }
14919   else if (TREE_CODE (*tp) == FUNCTION_DECL
14920            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14921     {
14922       /* The call graph machinery must have finished analyzing,
14923          optimizing and gimplifying the CU by now.
14924          So if *TP has no call graph node associated
14925          to it, it means *TP will not be emitted.  */
14926       if (!cgraph_get_node (*tp))
14927         return *tp;
14928     }
14929   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14930     return *tp;
14931
14932   return NULL_TREE;
14933 }
14934
14935 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14936    for use in a later add_const_value_attribute call.  */
14937
14938 static rtx
14939 rtl_for_decl_init (tree init, tree type)
14940 {
14941   rtx rtl = NULL_RTX;
14942
14943   STRIP_NOPS (init);
14944
14945   /* If a variable is initialized with a string constant without embedded
14946      zeros, build CONST_STRING.  */
14947   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14948     {
14949       tree enttype = TREE_TYPE (type);
14950       tree domain = TYPE_DOMAIN (type);
14951       enum machine_mode mode = TYPE_MODE (enttype);
14952
14953       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14954           && domain
14955           && integer_zerop (TYPE_MIN_VALUE (domain))
14956           && compare_tree_int (TYPE_MAX_VALUE (domain),
14957                                TREE_STRING_LENGTH (init) - 1) == 0
14958           && ((size_t) TREE_STRING_LENGTH (init)
14959               == strlen (TREE_STRING_POINTER (init)) + 1))
14960         {
14961           rtl = gen_rtx_CONST_STRING (VOIDmode,
14962                                       ggc_strdup (TREE_STRING_POINTER (init)));
14963           rtl = gen_rtx_MEM (BLKmode, rtl);
14964           MEM_READONLY_P (rtl) = 1;
14965         }
14966     }
14967   /* Other aggregates, and complex values, could be represented using
14968      CONCAT: FIXME!  */
14969   else if (AGGREGATE_TYPE_P (type)
14970            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14971                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14972            || TREE_CODE (type) == COMPLEX_TYPE)
14973     ;
14974   /* Vectors only work if their mode is supported by the target.
14975      FIXME: generic vectors ought to work too.  */
14976   else if (TREE_CODE (type) == VECTOR_TYPE
14977            && !VECTOR_MODE_P (TYPE_MODE (type)))
14978     ;
14979   /* If the initializer is something that we know will expand into an
14980      immediate RTL constant, expand it now.  We must be careful not to
14981      reference variables which won't be output.  */
14982   else if (initializer_constant_valid_p (init, type)
14983            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14984     {
14985       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14986          possible.  */
14987       if (TREE_CODE (type) == VECTOR_TYPE)
14988         switch (TREE_CODE (init))
14989           {
14990           case VECTOR_CST:
14991             break;
14992           case CONSTRUCTOR:
14993             if (TREE_CONSTANT (init))
14994               {
14995                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14996                 bool constant_p = true;
14997                 tree value;
14998                 unsigned HOST_WIDE_INT ix;
14999
15000                 /* Even when ctor is constant, it might contain non-*_CST
15001                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
15002                    belong into VECTOR_CST nodes.  */
15003                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15004                   if (!CONSTANT_CLASS_P (value))
15005                     {
15006                       constant_p = false;
15007                       break;
15008                     }
15009
15010                 if (constant_p)
15011                   {
15012                     init = build_vector_from_ctor (type, elts);
15013                     break;
15014                   }
15015               }
15016             /* FALLTHRU */
15017
15018           default:
15019             return NULL;
15020           }
15021
15022       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15023
15024       /* If expand_expr returns a MEM, it wasn't immediate.  */
15025       gcc_assert (!rtl || !MEM_P (rtl));
15026     }
15027
15028   return rtl;
15029 }
15030
15031 /* Generate RTL for the variable DECL to represent its location.  */
15032
15033 static rtx
15034 rtl_for_decl_location (tree decl)
15035 {
15036   rtx rtl;
15037
15038   /* Here we have to decide where we are going to say the parameter "lives"
15039      (as far as the debugger is concerned).  We only have a couple of
15040      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15041
15042      DECL_RTL normally indicates where the parameter lives during most of the
15043      activation of the function.  If optimization is enabled however, this
15044      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15045      that the parameter doesn't really live anywhere (as far as the code
15046      generation parts of GCC are concerned) during most of the function's
15047      activation.  That will happen (for example) if the parameter is never
15048      referenced within the function.
15049
15050      We could just generate a location descriptor here for all non-NULL
15051      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15052      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15053      where DECL_RTL is NULL or is a pseudo-reg.
15054
15055      Note however that we can only get away with using DECL_INCOMING_RTL as
15056      a backup substitute for DECL_RTL in certain limited cases.  In cases
15057      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15058      we can be sure that the parameter was passed using the same type as it is
15059      declared to have within the function, and that its DECL_INCOMING_RTL
15060      points us to a place where a value of that type is passed.
15061
15062      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15063      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15064      because in these cases DECL_INCOMING_RTL points us to a value of some
15065      type which is *different* from the type of the parameter itself.  Thus,
15066      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15067      such cases, the debugger would end up (for example) trying to fetch a
15068      `float' from a place which actually contains the first part of a
15069      `double'.  That would lead to really incorrect and confusing
15070      output at debug-time.
15071
15072      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15073      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15074      are a couple of exceptions however.  On little-endian machines we can
15075      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15076      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15077      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15078      when (on a little-endian machine) a non-prototyped function has a
15079      parameter declared to be of type `short' or `char'.  In such cases,
15080      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15081      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15082      passed `int' value.  If the debugger then uses that address to fetch
15083      a `short' or a `char' (on a little-endian machine) the result will be
15084      the correct data, so we allow for such exceptional cases below.
15085
15086      Note that our goal here is to describe the place where the given formal
15087      parameter lives during most of the function's activation (i.e. between the
15088      end of the prologue and the start of the epilogue).  We'll do that as best
15089      as we can. Note however that if the given formal parameter is modified
15090      sometime during the execution of the function, then a stack backtrace (at
15091      debug-time) will show the function as having been called with the *new*
15092      value rather than the value which was originally passed in.  This happens
15093      rarely enough that it is not a major problem, but it *is* a problem, and
15094      I'd like to fix it.
15095
15096      A future version of dwarf2out.c may generate two additional attributes for
15097      any given DW_TAG_formal_parameter DIE which will describe the "passed
15098      type" and the "passed location" for the given formal parameter in addition
15099      to the attributes we now generate to indicate the "declared type" and the
15100      "active location" for each parameter.  This additional set of attributes
15101      could be used by debuggers for stack backtraces. Separately, note that
15102      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15103      This happens (for example) for inlined-instances of inline function formal
15104      parameters which are never referenced.  This really shouldn't be
15105      happening.  All PARM_DECL nodes should get valid non-NULL
15106      DECL_INCOMING_RTL values.  FIXME.  */
15107
15108   /* Use DECL_RTL as the "location" unless we find something better.  */
15109   rtl = DECL_RTL_IF_SET (decl);
15110
15111   /* When generating abstract instances, ignore everything except
15112      constants, symbols living in memory, and symbols living in
15113      fixed registers.  */
15114   if (! reload_completed)
15115     {
15116       if (rtl
15117           && (CONSTANT_P (rtl)
15118               || (MEM_P (rtl)
15119                   && CONSTANT_P (XEXP (rtl, 0)))
15120               || (REG_P (rtl)
15121                   && TREE_CODE (decl) == VAR_DECL
15122                   && TREE_STATIC (decl))))
15123         {
15124           rtl = targetm.delegitimize_address (rtl);
15125           return rtl;
15126         }
15127       rtl = NULL_RTX;
15128     }
15129   else if (TREE_CODE (decl) == PARM_DECL)
15130     {
15131       if (rtl == NULL_RTX
15132           || is_pseudo_reg (rtl)
15133           || (MEM_P (rtl)
15134               && is_pseudo_reg (XEXP (rtl, 0))
15135               && DECL_INCOMING_RTL (decl)
15136               && MEM_P (DECL_INCOMING_RTL (decl))
15137               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15138         {
15139           tree declared_type = TREE_TYPE (decl);
15140           tree passed_type = DECL_ARG_TYPE (decl);
15141           enum machine_mode dmode = TYPE_MODE (declared_type);
15142           enum machine_mode pmode = TYPE_MODE (passed_type);
15143
15144           /* This decl represents a formal parameter which was optimized out.
15145              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15146              all cases where (rtl == NULL_RTX) just below.  */
15147           if (dmode == pmode)
15148             rtl = DECL_INCOMING_RTL (decl);
15149           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15150                    && SCALAR_INT_MODE_P (dmode)
15151                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15152                    && DECL_INCOMING_RTL (decl))
15153             {
15154               rtx inc = DECL_INCOMING_RTL (decl);
15155               if (REG_P (inc))
15156                 rtl = inc;
15157               else if (MEM_P (inc))
15158                 {
15159                   if (BYTES_BIG_ENDIAN)
15160                     rtl = adjust_address_nv (inc, dmode,
15161                                              GET_MODE_SIZE (pmode)
15162                                              - GET_MODE_SIZE (dmode));
15163                   else
15164                     rtl = inc;
15165                 }
15166             }
15167         }
15168
15169       /* If the parm was passed in registers, but lives on the stack, then
15170          make a big endian correction if the mode of the type of the
15171          parameter is not the same as the mode of the rtl.  */
15172       /* ??? This is the same series of checks that are made in dbxout.c before
15173          we reach the big endian correction code there.  It isn't clear if all
15174          of these checks are necessary here, but keeping them all is the safe
15175          thing to do.  */
15176       else if (MEM_P (rtl)
15177                && XEXP (rtl, 0) != const0_rtx
15178                && ! CONSTANT_P (XEXP (rtl, 0))
15179                /* Not passed in memory.  */
15180                && !MEM_P (DECL_INCOMING_RTL (decl))
15181                /* Not passed by invisible reference.  */
15182                && (!REG_P (XEXP (rtl, 0))
15183                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15184                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15185 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15186                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15187 #endif
15188                      )
15189                /* Big endian correction check.  */
15190                && BYTES_BIG_ENDIAN
15191                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15192                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15193                    < UNITS_PER_WORD))
15194         {
15195           int offset = (UNITS_PER_WORD
15196                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15197
15198           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15199                              plus_constant (XEXP (rtl, 0), offset));
15200         }
15201     }
15202   else if (TREE_CODE (decl) == VAR_DECL
15203            && rtl
15204            && MEM_P (rtl)
15205            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15206            && BYTES_BIG_ENDIAN)
15207     {
15208       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15209       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15210
15211       /* If a variable is declared "register" yet is smaller than
15212          a register, then if we store the variable to memory, it
15213          looks like we're storing a register-sized value, when in
15214          fact we are not.  We need to adjust the offset of the
15215          storage location to reflect the actual value's bytes,
15216          else gdb will not be able to display it.  */
15217       if (rsize > dsize)
15218         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15219                            plus_constant (XEXP (rtl, 0), rsize-dsize));
15220     }
15221
15222   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15223      and will have been substituted directly into all expressions that use it.
15224      C does not have such a concept, but C++ and other languages do.  */
15225   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15226     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15227
15228   if (rtl)
15229     rtl = targetm.delegitimize_address (rtl);
15230
15231   /* If we don't look past the constant pool, we risk emitting a
15232      reference to a constant pool entry that isn't referenced from
15233      code, and thus is not emitted.  */
15234   if (rtl)
15235     rtl = avoid_constant_pool_reference (rtl);
15236
15237   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15238      in the current CU, resolve_addr will remove the expression referencing
15239      it.  */
15240   if (rtl == NULL_RTX
15241       && TREE_CODE (decl) == VAR_DECL
15242       && !DECL_EXTERNAL (decl)
15243       && TREE_STATIC (decl)
15244       && DECL_NAME (decl)
15245       && !DECL_HARD_REGISTER (decl)
15246       && DECL_MODE (decl) != VOIDmode)
15247     {
15248       rtl = make_decl_rtl_for_debug (decl);
15249       if (!MEM_P (rtl)
15250           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15251           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15252         rtl = NULL_RTX;
15253     }
15254
15255   return rtl;
15256 }
15257
15258 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15259    returned.  If so, the decl for the COMMON block is returned, and the
15260    value is the offset into the common block for the symbol.  */
15261
15262 static tree
15263 fortran_common (tree decl, HOST_WIDE_INT *value)
15264 {
15265   tree val_expr, cvar;
15266   enum machine_mode mode;
15267   HOST_WIDE_INT bitsize, bitpos;
15268   tree offset;
15269   int volatilep = 0, unsignedp = 0;
15270
15271   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15272      it does not have a value (the offset into the common area), or if it
15273      is thread local (as opposed to global) then it isn't common, and shouldn't
15274      be handled as such.  */
15275   if (TREE_CODE (decl) != VAR_DECL
15276       || !TREE_STATIC (decl)
15277       || !DECL_HAS_VALUE_EXPR_P (decl)
15278       || !is_fortran ())
15279     return NULL_TREE;
15280
15281   val_expr = DECL_VALUE_EXPR (decl);
15282   if (TREE_CODE (val_expr) != COMPONENT_REF)
15283     return NULL_TREE;
15284
15285   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15286                               &mode, &unsignedp, &volatilep, true);
15287
15288   if (cvar == NULL_TREE
15289       || TREE_CODE (cvar) != VAR_DECL
15290       || DECL_ARTIFICIAL (cvar)
15291       || !TREE_PUBLIC (cvar))
15292     return NULL_TREE;
15293
15294   *value = 0;
15295   if (offset != NULL)
15296     {
15297       if (!host_integerp (offset, 0))
15298         return NULL_TREE;
15299       *value = tree_low_cst (offset, 0);
15300     }
15301   if (bitpos != 0)
15302     *value += bitpos / BITS_PER_UNIT;
15303
15304   return cvar;
15305 }
15306
15307 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15308    data attribute for a variable or a parameter.  We generate the
15309    DW_AT_const_value attribute only in those cases where the given variable
15310    or parameter does not have a true "location" either in memory or in a
15311    register.  This can happen (for example) when a constant is passed as an
15312    actual argument in a call to an inline function.  (It's possible that
15313    these things can crop up in other ways also.)  Note that one type of
15314    constant value which can be passed into an inlined function is a constant
15315    pointer.  This can happen for example if an actual argument in an inlined
15316    function call evaluates to a compile-time constant address.
15317
15318    CACHE_P is true if it is worth caching the location list for DECL,
15319    so that future calls can reuse it rather than regenerate it from scratch.
15320    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15321    since we will need to refer to them each time the function is inlined.  */
15322
15323 static bool
15324 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15325                                        enum dwarf_attribute attr)
15326 {
15327   rtx rtl;
15328   dw_loc_list_ref list;
15329   var_loc_list *loc_list;
15330   cached_dw_loc_list *cache;
15331   void **slot;
15332
15333   if (TREE_CODE (decl) == ERROR_MARK)
15334     return false;
15335
15336   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15337               || TREE_CODE (decl) == RESULT_DECL);
15338
15339   /* Try to get some constant RTL for this decl, and use that as the value of
15340      the location.  */
15341
15342   rtl = rtl_for_decl_location (decl);
15343   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15344       && add_const_value_attribute (die, rtl))
15345     return true;
15346
15347   /* See if we have single element location list that is equivalent to
15348      a constant value.  That way we are better to use add_const_value_attribute
15349      rather than expanding constant value equivalent.  */
15350   loc_list = lookup_decl_loc (decl);
15351   if (loc_list
15352       && loc_list->first
15353       && loc_list->first->next == NULL
15354       && NOTE_P (loc_list->first->loc)
15355       && NOTE_VAR_LOCATION (loc_list->first->loc)
15356       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15357     {
15358       struct var_loc_node *node;
15359
15360       node = loc_list->first;
15361       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15362       if (GET_CODE (rtl) == EXPR_LIST)
15363         rtl = XEXP (rtl, 0);
15364       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15365           && add_const_value_attribute (die, rtl))
15366          return true;
15367     }
15368   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15369      list several times.  See if we've already cached the contents.  */
15370   list = NULL;
15371   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15372     cache_p = false;
15373   if (cache_p)
15374     {
15375       cache = (cached_dw_loc_list *)
15376         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15377       if (cache)
15378         list = cache->loc_list;
15379     }
15380   if (list == NULL)
15381     {
15382       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15383       /* It is usually worth caching this result if the decl is from
15384          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
15385       if (cache_p && list && list->dw_loc_next)
15386         {
15387           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15388                                            DECL_UID (decl), INSERT);
15389           cache = ggc_alloc_cleared_cached_dw_loc_list ();
15390           cache->decl_id = DECL_UID (decl);
15391           cache->loc_list = list;
15392           *slot = cache;
15393         }
15394     }
15395   if (list)
15396     {
15397       add_AT_location_description (die, attr, list);
15398       return true;
15399     }
15400   /* None of that worked, so it must not really have a location;
15401      try adding a constant value attribute from the DECL_INITIAL.  */
15402   return tree_add_const_value_attribute_for_decl (die, decl);
15403 }
15404
15405 /* Add VARIABLE and DIE into deferred locations list.  */
15406
15407 static void
15408 defer_location (tree variable, dw_die_ref die)
15409 {
15410   deferred_locations entry;
15411   entry.variable = variable;
15412   entry.die = die;
15413   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15414 }
15415
15416 /* Helper function for tree_add_const_value_attribute.  Natively encode
15417    initializer INIT into an array.  Return true if successful.  */
15418
15419 static bool
15420 native_encode_initializer (tree init, unsigned char *array, int size)
15421 {
15422   tree type;
15423
15424   if (init == NULL_TREE)
15425     return false;
15426
15427   STRIP_NOPS (init);
15428   switch (TREE_CODE (init))
15429     {
15430     case STRING_CST:
15431       type = TREE_TYPE (init);
15432       if (TREE_CODE (type) == ARRAY_TYPE)
15433         {
15434           tree enttype = TREE_TYPE (type);
15435           enum machine_mode mode = TYPE_MODE (enttype);
15436
15437           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15438             return false;
15439           if (int_size_in_bytes (type) != size)
15440             return false;
15441           if (size > TREE_STRING_LENGTH (init))
15442             {
15443               memcpy (array, TREE_STRING_POINTER (init),
15444                       TREE_STRING_LENGTH (init));
15445               memset (array + TREE_STRING_LENGTH (init),
15446                       '\0', size - TREE_STRING_LENGTH (init));
15447             }
15448           else
15449             memcpy (array, TREE_STRING_POINTER (init), size);
15450           return true;
15451         }
15452       return false;
15453     case CONSTRUCTOR:
15454       type = TREE_TYPE (init);
15455       if (int_size_in_bytes (type) != size)
15456         return false;
15457       if (TREE_CODE (type) == ARRAY_TYPE)
15458         {
15459           HOST_WIDE_INT min_index;
15460           unsigned HOST_WIDE_INT cnt;
15461           int curpos = 0, fieldsize;
15462           constructor_elt *ce;
15463
15464           if (TYPE_DOMAIN (type) == NULL_TREE
15465               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15466             return false;
15467
15468           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15469           if (fieldsize <= 0)
15470             return false;
15471
15472           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15473           memset (array, '\0', size);
15474           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15475             {
15476               tree val = ce->value;
15477               tree index = ce->index;
15478               int pos = curpos;
15479               if (index && TREE_CODE (index) == RANGE_EXPR)
15480                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15481                       * fieldsize;
15482               else if (index)
15483                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15484
15485               if (val)
15486                 {
15487                   STRIP_NOPS (val);
15488                   if (!native_encode_initializer (val, array + pos, fieldsize))
15489                     return false;
15490                 }
15491               curpos = pos + fieldsize;
15492               if (index && TREE_CODE (index) == RANGE_EXPR)
15493                 {
15494                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15495                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15496                   while (count-- > 0)
15497                     {
15498                       if (val)
15499                         memcpy (array + curpos, array + pos, fieldsize);
15500                       curpos += fieldsize;
15501                     }
15502                 }
15503               gcc_assert (curpos <= size);
15504             }
15505           return true;
15506         }
15507       else if (TREE_CODE (type) == RECORD_TYPE
15508                || TREE_CODE (type) == UNION_TYPE)
15509         {
15510           tree field = NULL_TREE;
15511           unsigned HOST_WIDE_INT cnt;
15512           constructor_elt *ce;
15513
15514           if (int_size_in_bytes (type) != size)
15515             return false;
15516
15517           if (TREE_CODE (type) == RECORD_TYPE)
15518             field = TYPE_FIELDS (type);
15519
15520           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15521             {
15522               tree val = ce->value;
15523               int pos, fieldsize;
15524
15525               if (ce->index != 0)
15526                 field = ce->index;
15527
15528               if (val)
15529                 STRIP_NOPS (val);
15530
15531               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15532                 return false;
15533
15534               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15535                   && TYPE_DOMAIN (TREE_TYPE (field))
15536                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15537                 return false;
15538               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15539                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15540                 return false;
15541               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15542               pos = int_byte_position (field);
15543               gcc_assert (pos + fieldsize <= size);
15544               if (val
15545                   && !native_encode_initializer (val, array + pos, fieldsize))
15546                 return false;
15547             }
15548           return true;
15549         }
15550       return false;
15551     case VIEW_CONVERT_EXPR:
15552     case NON_LVALUE_EXPR:
15553       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15554     default:
15555       return native_encode_expr (init, array, size) == size;
15556     }
15557 }
15558
15559 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15560    attribute is the const value T.  */
15561
15562 static bool
15563 tree_add_const_value_attribute (dw_die_ref die, tree t)
15564 {
15565   tree init;
15566   tree type = TREE_TYPE (t);
15567   rtx rtl;
15568
15569   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15570     return false;
15571
15572   init = t;
15573   gcc_assert (!DECL_P (init));
15574
15575   rtl = rtl_for_decl_init (init, type);
15576   if (rtl)
15577     return add_const_value_attribute (die, rtl);
15578   /* If the host and target are sane, try harder.  */
15579   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15580            && initializer_constant_valid_p (init, type))
15581     {
15582       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15583       if (size > 0 && (int) size == size)
15584         {
15585           unsigned char *array = (unsigned char *)
15586             ggc_alloc_cleared_atomic (size);
15587
15588           if (native_encode_initializer (init, array, size))
15589             {
15590               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15591               return true;
15592             }
15593         }
15594     }
15595   return false;
15596 }
15597
15598 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15599    attribute is the const value of T, where T is an integral constant
15600    variable with static storage duration
15601    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15602
15603 static bool
15604 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15605 {
15606
15607   if (!decl
15608       || (TREE_CODE (decl) != VAR_DECL
15609           && TREE_CODE (decl) != CONST_DECL)
15610       || (TREE_CODE (decl) == VAR_DECL
15611           && !TREE_STATIC (decl)))
15612     return false;
15613
15614     if (TREE_READONLY (decl)
15615         && ! TREE_THIS_VOLATILE (decl)
15616         && DECL_INITIAL (decl))
15617       /* OK */;
15618     else
15619       return false;
15620
15621   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15622   if (get_AT (var_die, DW_AT_const_value))
15623     return false;
15624
15625   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15626 }
15627
15628 /* Convert the CFI instructions for the current function into a
15629    location list.  This is used for DW_AT_frame_base when we targeting
15630    a dwarf2 consumer that does not support the dwarf3
15631    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15632    expressions.  */
15633
15634 static dw_loc_list_ref
15635 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15636 {
15637   int ix;
15638   dw_fde_ref fde;
15639   dw_loc_list_ref list, *list_tail;
15640   dw_cfi_ref cfi;
15641   dw_cfa_location last_cfa, next_cfa;
15642   const char *start_label, *last_label, *section;
15643   dw_cfa_location remember;
15644
15645   fde = current_fde ();
15646   gcc_assert (fde != NULL);
15647
15648   section = secname_for_decl (current_function_decl);
15649   list_tail = &list;
15650   list = NULL;
15651
15652   memset (&next_cfa, 0, sizeof (next_cfa));
15653   next_cfa.reg = INVALID_REGNUM;
15654   remember = next_cfa;
15655
15656   start_label = fde->dw_fde_begin;
15657
15658   /* ??? Bald assumption that the CIE opcode list does not contain
15659      advance opcodes.  */
15660   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15661     lookup_cfa_1 (cfi, &next_cfa, &remember);
15662
15663   last_cfa = next_cfa;
15664   last_label = start_label;
15665
15666   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15667     {
15668       /* If the first partition contained no CFI adjustments, the
15669          CIE opcodes apply to the whole first partition.  */
15670       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15671                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15672       list_tail =&(*list_tail)->dw_loc_next;
15673       start_label = last_label = fde->dw_fde_second_begin;
15674     }
15675
15676   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15677     {
15678       switch (cfi->dw_cfi_opc)
15679         {
15680         case DW_CFA_set_loc:
15681         case DW_CFA_advance_loc1:
15682         case DW_CFA_advance_loc2:
15683         case DW_CFA_advance_loc4:
15684           if (!cfa_equal_p (&last_cfa, &next_cfa))
15685             {
15686               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15687                                          start_label, last_label, section);
15688
15689               list_tail = &(*list_tail)->dw_loc_next;
15690               last_cfa = next_cfa;
15691               start_label = last_label;
15692             }
15693           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15694           break;
15695
15696         case DW_CFA_advance_loc:
15697           /* The encoding is complex enough that we should never emit this.  */
15698           gcc_unreachable ();
15699
15700         default:
15701           lookup_cfa_1 (cfi, &next_cfa, &remember);
15702           break;
15703         }
15704       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15705         {
15706           if (!cfa_equal_p (&last_cfa, &next_cfa))
15707             {
15708               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15709                                          start_label, last_label, section);
15710
15711               list_tail = &(*list_tail)->dw_loc_next;
15712               last_cfa = next_cfa;
15713               start_label = last_label;
15714             }
15715           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15716                                      start_label, fde->dw_fde_end, section);
15717           list_tail = &(*list_tail)->dw_loc_next;
15718           start_label = last_label = fde->dw_fde_second_begin;
15719         }
15720     }
15721
15722   if (!cfa_equal_p (&last_cfa, &next_cfa))
15723     {
15724       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15725                                  start_label, last_label, section);
15726       list_tail = &(*list_tail)->dw_loc_next;
15727       start_label = last_label;
15728     }
15729
15730   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15731                              start_label,
15732                              fde->dw_fde_second_begin
15733                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15734                              section);
15735
15736   if (list && list->dw_loc_next)
15737     gen_llsym (list);
15738
15739   return list;
15740 }
15741
15742 /* Compute a displacement from the "steady-state frame pointer" to the
15743    frame base (often the same as the CFA), and store it in
15744    frame_pointer_fb_offset.  OFFSET is added to the displacement
15745    before the latter is negated.  */
15746
15747 static void
15748 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15749 {
15750   rtx reg, elim;
15751
15752 #ifdef FRAME_POINTER_CFA_OFFSET
15753   reg = frame_pointer_rtx;
15754   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15755 #else
15756   reg = arg_pointer_rtx;
15757   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15758 #endif
15759
15760   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15761   if (GET_CODE (elim) == PLUS)
15762     {
15763       offset += INTVAL (XEXP (elim, 1));
15764       elim = XEXP (elim, 0);
15765     }
15766
15767   frame_pointer_fb_offset = -offset;
15768
15769   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15770      in which to eliminate.  This is because it's stack pointer isn't 
15771      directly accessible as a register within the ISA.  To work around
15772      this, assume that while we cannot provide a proper value for
15773      frame_pointer_fb_offset, we won't need one either.  */
15774   frame_pointer_fb_offset_valid
15775     = ((SUPPORTS_STACK_ALIGNMENT
15776         && (elim == hard_frame_pointer_rtx
15777             || elim == stack_pointer_rtx))
15778        || elim == (frame_pointer_needed
15779                    ? hard_frame_pointer_rtx
15780                    : stack_pointer_rtx));
15781 }
15782
15783 /* Generate a DW_AT_name attribute given some string value to be included as
15784    the value of the attribute.  */
15785
15786 static void
15787 add_name_attribute (dw_die_ref die, const char *name_string)
15788 {
15789   if (name_string != NULL && *name_string != 0)
15790     {
15791       if (demangle_name_func)
15792         name_string = (*demangle_name_func) (name_string);
15793
15794       add_AT_string (die, DW_AT_name, name_string);
15795     }
15796 }
15797
15798 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15799    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15800    of TYPE accordingly.
15801
15802    ??? This is a temporary measure until after we're able to generate
15803    regular DWARF for the complex Ada type system.  */
15804
15805 static void 
15806 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15807                                      dw_die_ref context_die)
15808 {
15809   tree dtype;
15810   dw_die_ref dtype_die;
15811
15812   if (!lang_hooks.types.descriptive_type)
15813     return;
15814
15815   dtype = lang_hooks.types.descriptive_type (type);
15816   if (!dtype)
15817     return;
15818
15819   dtype_die = lookup_type_die (dtype);
15820   if (!dtype_die)
15821     {
15822       gen_type_die (dtype, context_die);
15823       dtype_die = lookup_type_die (dtype);
15824       gcc_assert (dtype_die);
15825     }
15826
15827   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15828 }
15829
15830 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15831
15832 static void
15833 add_comp_dir_attribute (dw_die_ref die)
15834 {
15835   const char *wd = get_src_pwd ();
15836   char *wd1;
15837
15838   if (wd == NULL)
15839     return;
15840
15841   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15842     {
15843       int wdlen;
15844
15845       wdlen = strlen (wd);
15846       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15847       strcpy (wd1, wd);
15848       wd1 [wdlen] = DIR_SEPARATOR;
15849       wd1 [wdlen + 1] = 0;
15850       wd = wd1;
15851     }
15852
15853     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15854 }
15855
15856 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15857    default.  */
15858
15859 static int
15860 lower_bound_default (void)
15861 {
15862   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15863     {
15864     case DW_LANG_C:
15865     case DW_LANG_C89:
15866     case DW_LANG_C99:
15867     case DW_LANG_C_plus_plus:
15868     case DW_LANG_ObjC:
15869     case DW_LANG_ObjC_plus_plus:
15870     case DW_LANG_Java:
15871       return 0;
15872     case DW_LANG_Fortran77:
15873     case DW_LANG_Fortran90:
15874     case DW_LANG_Fortran95:
15875       return 1;
15876     case DW_LANG_UPC:
15877     case DW_LANG_D:
15878     case DW_LANG_Python:
15879       return dwarf_version >= 4 ? 0 : -1;
15880     case DW_LANG_Ada95:
15881     case DW_LANG_Ada83:
15882     case DW_LANG_Cobol74:
15883     case DW_LANG_Cobol85:
15884     case DW_LANG_Pascal83:
15885     case DW_LANG_Modula2:
15886     case DW_LANG_PLI:
15887       return dwarf_version >= 4 ? 1 : -1;
15888     default:
15889       return -1;
15890     }
15891 }
15892
15893 /* Given a tree node describing an array bound (either lower or upper) output
15894    a representation for that bound.  */
15895
15896 static void
15897 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15898 {
15899   switch (TREE_CODE (bound))
15900     {
15901     case ERROR_MARK:
15902       return;
15903
15904     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15905     case INTEGER_CST:
15906       {
15907         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15908         int dflt;
15909
15910         /* Use the default if possible.  */
15911         if (bound_attr == DW_AT_lower_bound
15912             && host_integerp (bound, 0)
15913             && (dflt = lower_bound_default ()) != -1
15914             && tree_low_cst (bound, 0) == dflt)
15915           ;
15916
15917         /* Otherwise represent the bound as an unsigned value with the
15918            precision of its type.  The precision and signedness of the
15919            type will be necessary to re-interpret it unambiguously.  */
15920         else if (prec < HOST_BITS_PER_WIDE_INT)
15921           {
15922             unsigned HOST_WIDE_INT mask
15923               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15924             add_AT_unsigned (subrange_die, bound_attr,
15925                              TREE_INT_CST_LOW (bound) & mask);
15926           }
15927         else if (prec == HOST_BITS_PER_WIDE_INT
15928                  || TREE_INT_CST_HIGH (bound) == 0)
15929           add_AT_unsigned (subrange_die, bound_attr,
15930                            TREE_INT_CST_LOW (bound));
15931         else
15932           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15933                          TREE_INT_CST_LOW (bound));
15934       }
15935       break;
15936
15937     CASE_CONVERT:
15938     case VIEW_CONVERT_EXPR:
15939       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15940       break;
15941
15942     case SAVE_EXPR:
15943       break;
15944
15945     case VAR_DECL:
15946     case PARM_DECL:
15947     case RESULT_DECL:
15948       {
15949         dw_die_ref decl_die = lookup_decl_die (bound);
15950
15951         /* ??? Can this happen, or should the variable have been bound
15952            first?  Probably it can, since I imagine that we try to create
15953            the types of parameters in the order in which they exist in
15954            the list, and won't have created a forward reference to a
15955            later parameter.  */
15956         if (decl_die != NULL)
15957           {
15958             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15959             break;
15960           }
15961       }
15962       /* FALLTHRU */
15963
15964     default:
15965       {
15966         /* Otherwise try to create a stack operation procedure to
15967            evaluate the value of the array bound.  */
15968
15969         dw_die_ref ctx, decl_die;
15970         dw_loc_list_ref list;
15971
15972         list = loc_list_from_tree (bound, 2);
15973         if (list == NULL || single_element_loc_list_p (list))
15974           {
15975             /* If DW_AT_*bound is not a reference nor constant, it is
15976                a DWARF expression rather than location description.
15977                For that loc_list_from_tree (bound, 0) is needed.
15978                If that fails to give a single element list,
15979                fall back to outputting this as a reference anyway.  */
15980             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15981             if (list2 && single_element_loc_list_p (list2))
15982               {
15983                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15984                 break;
15985               }
15986           }
15987         if (list == NULL)
15988           break;
15989
15990         if (current_function_decl == 0)
15991           ctx = comp_unit_die ();
15992         else
15993           ctx = lookup_decl_die (current_function_decl);
15994
15995         decl_die = new_die (DW_TAG_variable, ctx, bound);
15996         add_AT_flag (decl_die, DW_AT_artificial, 1);
15997         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15998         add_AT_location_description (decl_die, DW_AT_location, list);
15999         add_AT_die_ref (subrange_die, bound_attr, decl_die);
16000         break;
16001       }
16002     }
16003 }
16004
16005 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16006    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16007    Note that the block of subscript information for an array type also
16008    includes information about the element type of the given array type.  */
16009
16010 static void
16011 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16012 {
16013   unsigned dimension_number;
16014   tree lower, upper;
16015   dw_die_ref subrange_die;
16016
16017   for (dimension_number = 0;
16018        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16019        type = TREE_TYPE (type), dimension_number++)
16020     {
16021       tree domain = TYPE_DOMAIN (type);
16022
16023       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16024         break;
16025
16026       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16027          and (in GNU C only) variable bounds.  Handle all three forms
16028          here.  */
16029       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16030       if (domain)
16031         {
16032           /* We have an array type with specified bounds.  */
16033           lower = TYPE_MIN_VALUE (domain);
16034           upper = TYPE_MAX_VALUE (domain);
16035
16036           /* Define the index type.  */
16037           if (TREE_TYPE (domain))
16038             {
16039               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16040                  TREE_TYPE field.  We can't emit debug info for this
16041                  because it is an unnamed integral type.  */
16042               if (TREE_CODE (domain) == INTEGER_TYPE
16043                   && TYPE_NAME (domain) == NULL_TREE
16044                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16045                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16046                 ;
16047               else
16048                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16049                                     type_die);
16050             }
16051
16052           /* ??? If upper is NULL, the array has unspecified length,
16053              but it does have a lower bound.  This happens with Fortran
16054                dimension arr(N:*)
16055              Since the debugger is definitely going to need to know N
16056              to produce useful results, go ahead and output the lower
16057              bound solo, and hope the debugger can cope.  */
16058
16059           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16060           if (upper)
16061             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16062         }
16063
16064       /* Otherwise we have an array type with an unspecified length.  The
16065          DWARF-2 spec does not say how to handle this; let's just leave out the
16066          bounds.  */
16067     }
16068 }
16069
16070 static void
16071 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16072 {
16073   unsigned size;
16074
16075   switch (TREE_CODE (tree_node))
16076     {
16077     case ERROR_MARK:
16078       size = 0;
16079       break;
16080     case ENUMERAL_TYPE:
16081     case RECORD_TYPE:
16082     case UNION_TYPE:
16083     case QUAL_UNION_TYPE:
16084       size = int_size_in_bytes (tree_node);
16085       break;
16086     case FIELD_DECL:
16087       /* For a data member of a struct or union, the DW_AT_byte_size is
16088          generally given as the number of bytes normally allocated for an
16089          object of the *declared* type of the member itself.  This is true
16090          even for bit-fields.  */
16091       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16092       break;
16093     default:
16094       gcc_unreachable ();
16095     }
16096
16097   /* Note that `size' might be -1 when we get to this point.  If it is, that
16098      indicates that the byte size of the entity in question is variable.  We
16099      have no good way of expressing this fact in Dwarf at the present time,
16100      so just let the -1 pass on through.  */
16101   add_AT_unsigned (die, DW_AT_byte_size, size);
16102 }
16103
16104 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16105    which specifies the distance in bits from the highest order bit of the
16106    "containing object" for the bit-field to the highest order bit of the
16107    bit-field itself.
16108
16109    For any given bit-field, the "containing object" is a hypothetical object
16110    (of some integral or enum type) within which the given bit-field lives.  The
16111    type of this hypothetical "containing object" is always the same as the
16112    declared type of the individual bit-field itself.  The determination of the
16113    exact location of the "containing object" for a bit-field is rather
16114    complicated.  It's handled by the `field_byte_offset' function (above).
16115
16116    Note that it is the size (in bytes) of the hypothetical "containing object"
16117    which will be given in the DW_AT_byte_size attribute for this bit-field.
16118    (See `byte_size_attribute' above).  */
16119
16120 static inline void
16121 add_bit_offset_attribute (dw_die_ref die, tree decl)
16122 {
16123   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16124   tree type = DECL_BIT_FIELD_TYPE (decl);
16125   HOST_WIDE_INT bitpos_int;
16126   HOST_WIDE_INT highest_order_object_bit_offset;
16127   HOST_WIDE_INT highest_order_field_bit_offset;
16128   HOST_WIDE_INT bit_offset;
16129
16130   /* Must be a field and a bit field.  */
16131   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16132
16133   /* We can't yet handle bit-fields whose offsets are variable, so if we
16134      encounter such things, just return without generating any attribute
16135      whatsoever.  Likewise for variable or too large size.  */
16136   if (! host_integerp (bit_position (decl), 0)
16137       || ! host_integerp (DECL_SIZE (decl), 1))
16138     return;
16139
16140   bitpos_int = int_bit_position (decl);
16141
16142   /* Note that the bit offset is always the distance (in bits) from the
16143      highest-order bit of the "containing object" to the highest-order bit of
16144      the bit-field itself.  Since the "high-order end" of any object or field
16145      is different on big-endian and little-endian machines, the computation
16146      below must take account of these differences.  */
16147   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16148   highest_order_field_bit_offset = bitpos_int;
16149
16150   if (! BYTES_BIG_ENDIAN)
16151     {
16152       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16153       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16154     }
16155
16156   bit_offset
16157     = (! BYTES_BIG_ENDIAN
16158        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16159        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16160
16161   if (bit_offset < 0)
16162     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16163   else
16164     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16165 }
16166
16167 /* For a FIELD_DECL node which represents a bit field, output an attribute
16168    which specifies the length in bits of the given field.  */
16169
16170 static inline void
16171 add_bit_size_attribute (dw_die_ref die, tree decl)
16172 {
16173   /* Must be a field and a bit field.  */
16174   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16175               && DECL_BIT_FIELD_TYPE (decl));
16176
16177   if (host_integerp (DECL_SIZE (decl), 1))
16178     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16179 }
16180
16181 /* If the compiled language is ANSI C, then add a 'prototyped'
16182    attribute, if arg types are given for the parameters of a function.  */
16183
16184 static inline void
16185 add_prototyped_attribute (dw_die_ref die, tree func_type)
16186 {
16187   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16188       && prototype_p (func_type))
16189     add_AT_flag (die, DW_AT_prototyped, 1);
16190 }
16191
16192 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16193    by looking in either the type declaration or object declaration
16194    equate table.  */
16195
16196 static inline dw_die_ref
16197 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16198 {
16199   dw_die_ref origin_die = NULL;
16200
16201   if (TREE_CODE (origin) != FUNCTION_DECL)
16202     {
16203       /* We may have gotten separated from the block for the inlined
16204          function, if we're in an exception handler or some such; make
16205          sure that the abstract function has been written out.
16206
16207          Doing this for nested functions is wrong, however; functions are
16208          distinct units, and our context might not even be inline.  */
16209       tree fn = origin;
16210
16211       if (TYPE_P (fn))
16212         fn = TYPE_STUB_DECL (fn);
16213
16214       fn = decl_function_context (fn);
16215       if (fn)
16216         dwarf2out_abstract_function (fn);
16217     }
16218
16219   if (DECL_P (origin))
16220     origin_die = lookup_decl_die (origin);
16221   else if (TYPE_P (origin))
16222     origin_die = lookup_type_die (origin);
16223
16224   /* XXX: Functions that are never lowered don't always have correct block
16225      trees (in the case of java, they simply have no block tree, in some other
16226      languages).  For these functions, there is nothing we can really do to
16227      output correct debug info for inlined functions in all cases.  Rather
16228      than die, we'll just produce deficient debug info now, in that we will
16229      have variables without a proper abstract origin.  In the future, when all
16230      functions are lowered, we should re-add a gcc_assert (origin_die)
16231      here.  */
16232
16233   if (origin_die)
16234     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16235   return origin_die;
16236 }
16237
16238 /* We do not currently support the pure_virtual attribute.  */
16239
16240 static inline void
16241 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16242 {
16243   if (DECL_VINDEX (func_decl))
16244     {
16245       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16246
16247       if (host_integerp (DECL_VINDEX (func_decl), 0))
16248         add_AT_loc (die, DW_AT_vtable_elem_location,
16249                     new_loc_descr (DW_OP_constu,
16250                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
16251                                    0));
16252
16253       /* GNU extension: Record what type this method came from originally.  */
16254       if (debug_info_level > DINFO_LEVEL_TERSE
16255           && DECL_CONTEXT (func_decl))
16256         add_AT_die_ref (die, DW_AT_containing_type,
16257                         lookup_type_die (DECL_CONTEXT (func_decl)));
16258     }
16259 }
16260 \f
16261 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16262    given decl.  This used to be a vendor extension until after DWARF 4
16263    standardized it.  */
16264
16265 static void
16266 add_linkage_attr (dw_die_ref die, tree decl)
16267 {
16268   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16269
16270   /* Mimic what assemble_name_raw does with a leading '*'.  */
16271   if (name[0] == '*')
16272     name = &name[1];
16273
16274   if (dwarf_version >= 4)
16275     add_AT_string (die, DW_AT_linkage_name, name);
16276   else
16277     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16278 }
16279
16280 /* Add source coordinate attributes for the given decl.  */
16281
16282 static void
16283 add_src_coords_attributes (dw_die_ref die, tree decl)
16284 {
16285   expanded_location s;
16286
16287   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
16288     return;
16289   s = expand_location (DECL_SOURCE_LOCATION (decl));
16290   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16291   add_AT_unsigned (die, DW_AT_decl_line, s.line);
16292 }
16293
16294 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
16295
16296 static void
16297 add_linkage_name (dw_die_ref die, tree decl)
16298 {
16299   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16300        && TREE_PUBLIC (decl)
16301        && !DECL_ABSTRACT (decl)
16302        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16303        && die->die_tag != DW_TAG_member)
16304     {
16305       /* Defer until we have an assembler name set.  */
16306       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16307         {
16308           limbo_die_node *asm_name;
16309
16310           asm_name = ggc_alloc_cleared_limbo_die_node ();
16311           asm_name->die = die;
16312           asm_name->created_for = decl;
16313           asm_name->next = deferred_asm_name;
16314           deferred_asm_name = asm_name;
16315         }
16316       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16317         add_linkage_attr (die, decl);
16318     }
16319 }
16320
16321 /* Add a DW_AT_name attribute and source coordinate attribute for the
16322    given decl, but only if it actually has a name.  */
16323
16324 static void
16325 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16326 {
16327   tree decl_name;
16328
16329   decl_name = DECL_NAME (decl);
16330   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16331     {
16332       const char *name = dwarf2_name (decl, 0);
16333       if (name)
16334         add_name_attribute (die, name);
16335       if (! DECL_ARTIFICIAL (decl))
16336         add_src_coords_attributes (die, decl);
16337
16338       add_linkage_name (die, decl);
16339     }
16340
16341 #ifdef VMS_DEBUGGING_INFO
16342   /* Get the function's name, as described by its RTL.  This may be different
16343      from the DECL_NAME name used in the source file.  */
16344   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16345     {
16346       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16347                    XEXP (DECL_RTL (decl), 0));
16348       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
16349     }
16350 #endif /* VMS_DEBUGGING_INFO */
16351 }
16352
16353 #ifdef VMS_DEBUGGING_INFO
16354 /* Output the debug main pointer die for VMS */
16355
16356 void
16357 dwarf2out_vms_debug_main_pointer (void)
16358 {
16359   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16360   dw_die_ref die;
16361
16362   /* Allocate the VMS debug main subprogram die.  */
16363   die = ggc_alloc_cleared_die_node ();
16364   die->die_tag = DW_TAG_subprogram;
16365   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16366   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16367                                current_function_funcdef_no);
16368   add_AT_lbl_id (die, DW_AT_entry_pc, label);
16369
16370   /* Make it the first child of comp_unit_die ().  */
16371   die->die_parent = comp_unit_die ();
16372   if (comp_unit_die ()->die_child)
16373     {
16374       die->die_sib = comp_unit_die ()->die_child->die_sib;
16375       comp_unit_die ()->die_child->die_sib = die;
16376     }
16377   else
16378     {
16379       die->die_sib = die;
16380       comp_unit_die ()->die_child = die;
16381     }
16382 }
16383 #endif /* VMS_DEBUGGING_INFO */
16384
16385 /* Push a new declaration scope.  */
16386
16387 static void
16388 push_decl_scope (tree scope)
16389 {
16390   VEC_safe_push (tree, gc, decl_scope_table, scope);
16391 }
16392
16393 /* Pop a declaration scope.  */
16394
16395 static inline void
16396 pop_decl_scope (void)
16397 {
16398   VEC_pop (tree, decl_scope_table);
16399 }
16400
16401 /* Return the DIE for the scope that immediately contains this type.
16402    Non-named types get global scope.  Named types nested in other
16403    types get their containing scope if it's open, or global scope
16404    otherwise.  All other types (i.e. function-local named types) get
16405    the current active scope.  */
16406
16407 static dw_die_ref
16408 scope_die_for (tree t, dw_die_ref context_die)
16409 {
16410   dw_die_ref scope_die = NULL;
16411   tree containing_scope;
16412   int i;
16413
16414   /* Non-types always go in the current scope.  */
16415   gcc_assert (TYPE_P (t));
16416
16417   containing_scope = TYPE_CONTEXT (t);
16418
16419   /* Use the containing namespace if it was passed in (for a declaration).  */
16420   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16421     {
16422       if (context_die == lookup_decl_die (containing_scope))
16423         /* OK */;
16424       else
16425         containing_scope = NULL_TREE;
16426     }
16427
16428   /* Ignore function type "scopes" from the C frontend.  They mean that
16429      a tagged type is local to a parmlist of a function declarator, but
16430      that isn't useful to DWARF.  */
16431   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16432     containing_scope = NULL_TREE;
16433
16434   if (SCOPE_FILE_SCOPE_P (containing_scope))
16435     scope_die = comp_unit_die ();
16436   else if (TYPE_P (containing_scope))
16437     {
16438       /* For types, we can just look up the appropriate DIE.  But
16439          first we check to see if we're in the middle of emitting it
16440          so we know where the new DIE should go.  */
16441       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16442         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16443           break;
16444
16445       if (i < 0)
16446         {
16447           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16448                       || TREE_ASM_WRITTEN (containing_scope));
16449           /*We are not in the middle of emitting the type
16450             CONTAINING_SCOPE. Let's see if it's emitted already.  */
16451           scope_die = lookup_type_die (containing_scope);
16452
16453           /* If none of the current dies are suitable, we get file scope.  */
16454           if (scope_die == NULL)
16455             scope_die = comp_unit_die ();
16456         }
16457       else
16458         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16459     }
16460   else
16461     scope_die = context_die;
16462
16463   return scope_die;
16464 }
16465
16466 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16467
16468 static inline int
16469 local_scope_p (dw_die_ref context_die)
16470 {
16471   for (; context_die; context_die = context_die->die_parent)
16472     if (context_die->die_tag == DW_TAG_inlined_subroutine
16473         || context_die->die_tag == DW_TAG_subprogram)
16474       return 1;
16475
16476   return 0;
16477 }
16478
16479 /* Returns nonzero if CONTEXT_DIE is a class.  */
16480
16481 static inline int
16482 class_scope_p (dw_die_ref context_die)
16483 {
16484   return (context_die
16485           && (context_die->die_tag == DW_TAG_structure_type
16486               || context_die->die_tag == DW_TAG_class_type
16487               || context_die->die_tag == DW_TAG_interface_type
16488               || context_die->die_tag == DW_TAG_union_type));
16489 }
16490
16491 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16492    whether or not to treat a DIE in this context as a declaration.  */
16493
16494 static inline int
16495 class_or_namespace_scope_p (dw_die_ref context_die)
16496 {
16497   return (class_scope_p (context_die)
16498           || (context_die && context_die->die_tag == DW_TAG_namespace));
16499 }
16500
16501 /* Many forms of DIEs require a "type description" attribute.  This
16502    routine locates the proper "type descriptor" die for the type given
16503    by 'type', and adds a DW_AT_type attribute below the given die.  */
16504
16505 static void
16506 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16507                     int decl_volatile, dw_die_ref context_die)
16508 {
16509   enum tree_code code  = TREE_CODE (type);
16510   dw_die_ref type_die  = NULL;
16511
16512   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16513      or fixed-point type, use the inner type.  This is because we have no
16514      support for unnamed types in base_type_die.  This can happen if this is
16515      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16516   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16517       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16518     type = TREE_TYPE (type), code = TREE_CODE (type);
16519
16520   if (code == ERROR_MARK
16521       /* Handle a special case.  For functions whose return type is void, we
16522          generate *no* type attribute.  (Note that no object may have type
16523          `void', so this only applies to function return types).  */
16524       || code == VOID_TYPE)
16525     return;
16526
16527   type_die = modified_type_die (type,
16528                                 decl_const || TYPE_READONLY (type),
16529                                 decl_volatile || TYPE_VOLATILE (type),
16530                                 context_die);
16531
16532   if (type_die != NULL)
16533     add_AT_die_ref (object_die, DW_AT_type, type_die);
16534 }
16535
16536 /* Given an object die, add the calling convention attribute for the
16537    function call type.  */
16538 static void
16539 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16540 {
16541   enum dwarf_calling_convention value = DW_CC_normal;
16542
16543   value = ((enum dwarf_calling_convention)
16544            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16545
16546   if (is_fortran ()
16547       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16548     {
16549       /* DWARF 2 doesn't provide a way to identify a program's source-level
16550         entry point.  DW_AT_calling_convention attributes are only meant
16551         to describe functions' calling conventions.  However, lacking a
16552         better way to signal the Fortran main program, we used this for 
16553         a long time, following existing custom.  Now, DWARF 4 has 
16554         DW_AT_main_subprogram, which we add below, but some tools still
16555         rely on the old way, which we thus keep.  */
16556       value = DW_CC_program;
16557
16558       if (dwarf_version >= 4 || !dwarf_strict)
16559         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16560     }
16561
16562   /* Only add the attribute if the backend requests it, and
16563      is not DW_CC_normal.  */
16564   if (value && (value != DW_CC_normal))
16565     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16566 }
16567
16568 /* Given a tree pointer to a struct, class, union, or enum type node, return
16569    a pointer to the (string) tag name for the given type, or zero if the type
16570    was declared without a tag.  */
16571
16572 static const char *
16573 type_tag (const_tree type)
16574 {
16575   const char *name = 0;
16576
16577   if (TYPE_NAME (type) != 0)
16578     {
16579       tree t = 0;
16580
16581       /* Find the IDENTIFIER_NODE for the type name.  */
16582       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16583           && !TYPE_NAMELESS (type))
16584         t = TYPE_NAME (type);
16585
16586       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16587          a TYPE_DECL node, regardless of whether or not a `typedef' was
16588          involved.  */
16589       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16590                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16591         {
16592           /* We want to be extra verbose.  Don't call dwarf_name if
16593              DECL_NAME isn't set.  The default hook for decl_printable_name
16594              doesn't like that, and in this context it's correct to return
16595              0, instead of "<anonymous>" or the like.  */
16596           if (DECL_NAME (TYPE_NAME (type))
16597               && !DECL_NAMELESS (TYPE_NAME (type)))
16598             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16599         }
16600
16601       /* Now get the name as a string, or invent one.  */
16602       if (!name && t != 0)
16603         name = IDENTIFIER_POINTER (t);
16604     }
16605
16606   return (name == 0 || *name == '\0') ? 0 : name;
16607 }
16608
16609 /* Return the type associated with a data member, make a special check
16610    for bit field types.  */
16611
16612 static inline tree
16613 member_declared_type (const_tree member)
16614 {
16615   return (DECL_BIT_FIELD_TYPE (member)
16616           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16617 }
16618
16619 /* Get the decl's label, as described by its RTL. This may be different
16620    from the DECL_NAME name used in the source file.  */
16621
16622 #if 0
16623 static const char *
16624 decl_start_label (tree decl)
16625 {
16626   rtx x;
16627   const char *fnname;
16628
16629   x = DECL_RTL (decl);
16630   gcc_assert (MEM_P (x));
16631
16632   x = XEXP (x, 0);
16633   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16634
16635   fnname = XSTR (x, 0);
16636   return fnname;
16637 }
16638 #endif
16639 \f
16640 /* These routines generate the internal representation of the DIE's for
16641    the compilation unit.  Debugging information is collected by walking
16642    the declaration trees passed in from dwarf2out_decl().  */
16643
16644 static void
16645 gen_array_type_die (tree type, dw_die_ref context_die)
16646 {
16647   dw_die_ref scope_die = scope_die_for (type, context_die);
16648   dw_die_ref array_die;
16649
16650   /* GNU compilers represent multidimensional array types as sequences of one
16651      dimensional array types whose element types are themselves array types.
16652      We sometimes squish that down to a single array_type DIE with multiple
16653      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16654      say that we are allowed to do this kind of compression in C, because
16655      there is no difference between an array of arrays and a multidimensional
16656      array.  We don't do this for Ada to remain as close as possible to the
16657      actual representation, which is especially important against the language
16658      flexibilty wrt arrays of variable size.  */
16659
16660   bool collapse_nested_arrays = !is_ada ();
16661   tree element_type;
16662
16663   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16664      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16665   if (TYPE_STRING_FLAG (type)
16666       && TREE_CODE (type) == ARRAY_TYPE
16667       && is_fortran ()
16668       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16669     {
16670       HOST_WIDE_INT size;
16671
16672       array_die = new_die (DW_TAG_string_type, scope_die, type);
16673       add_name_attribute (array_die, type_tag (type));
16674       equate_type_number_to_die (type, array_die);
16675       size = int_size_in_bytes (type);
16676       if (size >= 0)
16677         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16678       else if (TYPE_DOMAIN (type) != NULL_TREE
16679                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16680                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16681         {
16682           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16683           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16684
16685           size = int_size_in_bytes (TREE_TYPE (szdecl));
16686           if (loc && size > 0)
16687             {
16688               add_AT_location_description (array_die, DW_AT_string_length, loc);
16689               if (size != DWARF2_ADDR_SIZE)
16690                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16691             }
16692         }
16693       return;
16694     }
16695
16696   /* ??? The SGI dwarf reader fails for array of array of enum types
16697      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16698      array type comes before the outer array type.  We thus call gen_type_die
16699      before we new_die and must prevent nested array types collapsing for this
16700      target.  */
16701
16702 #ifdef MIPS_DEBUGGING_INFO
16703   gen_type_die (TREE_TYPE (type), context_die);
16704   collapse_nested_arrays = false;
16705 #endif
16706
16707   array_die = new_die (DW_TAG_array_type, scope_die, type);
16708   add_name_attribute (array_die, type_tag (type));
16709   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16710   if (TYPE_ARTIFICIAL (type))
16711     add_AT_flag (array_die, DW_AT_artificial, 1);
16712   equate_type_number_to_die (type, array_die);
16713
16714   if (TREE_CODE (type) == VECTOR_TYPE)
16715     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16716
16717   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16718   if (is_fortran ()
16719       && TREE_CODE (type) == ARRAY_TYPE
16720       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16721       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16722     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16723
16724 #if 0
16725   /* We default the array ordering.  SDB will probably do
16726      the right things even if DW_AT_ordering is not present.  It's not even
16727      an issue until we start to get into multidimensional arrays anyway.  If
16728      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16729      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16730      and when we find out that we need to put these in, we will only do so
16731      for multidimensional arrays.  */
16732   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16733 #endif
16734
16735 #ifdef MIPS_DEBUGGING_INFO
16736   /* The SGI compilers handle arrays of unknown bound by setting
16737      AT_declaration and not emitting any subrange DIEs.  */
16738   if (TREE_CODE (type) == ARRAY_TYPE
16739       && ! TYPE_DOMAIN (type))
16740     add_AT_flag (array_die, DW_AT_declaration, 1);
16741   else
16742 #endif
16743   if (TREE_CODE (type) == VECTOR_TYPE)
16744     {
16745       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16746       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16747       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16748       add_bound_info (subrange_die, DW_AT_upper_bound,
16749                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16750     }
16751   else
16752     add_subscript_info (array_die, type, collapse_nested_arrays);
16753
16754   /* Add representation of the type of the elements of this array type and
16755      emit the corresponding DIE if we haven't done it already.  */
16756   element_type = TREE_TYPE (type);
16757   if (collapse_nested_arrays)
16758     while (TREE_CODE (element_type) == ARRAY_TYPE)
16759       {
16760         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16761           break;
16762         element_type = TREE_TYPE (element_type);
16763       }
16764
16765 #ifndef MIPS_DEBUGGING_INFO
16766   gen_type_die (element_type, context_die);
16767 #endif
16768
16769   add_type_attribute (array_die, element_type, 0, 0, context_die);
16770
16771   if (get_AT (array_die, DW_AT_name))
16772     add_pubtype (type, array_die);
16773 }
16774
16775 static dw_loc_descr_ref
16776 descr_info_loc (tree val, tree base_decl)
16777 {
16778   HOST_WIDE_INT size;
16779   dw_loc_descr_ref loc, loc2;
16780   enum dwarf_location_atom op;
16781
16782   if (val == base_decl)
16783     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16784
16785   switch (TREE_CODE (val))
16786     {
16787     CASE_CONVERT:
16788       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16789     case VAR_DECL:
16790       return loc_descriptor_from_tree (val, 0);
16791     case INTEGER_CST:
16792       if (host_integerp (val, 0))
16793         return int_loc_descriptor (tree_low_cst (val, 0));
16794       break;
16795     case INDIRECT_REF:
16796       size = int_size_in_bytes (TREE_TYPE (val));
16797       if (size < 0)
16798         break;
16799       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16800       if (!loc)
16801         break;
16802       if (size == DWARF2_ADDR_SIZE)
16803         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16804       else
16805         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16806       return loc;
16807     case POINTER_PLUS_EXPR:
16808     case PLUS_EXPR:
16809       if (host_integerp (TREE_OPERAND (val, 1), 1)
16810           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16811              < 16384)
16812         {
16813           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16814           if (!loc)
16815             break;
16816           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16817         }
16818       else
16819         {
16820           op = DW_OP_plus;
16821         do_binop:
16822           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16823           if (!loc)
16824             break;
16825           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16826           if (!loc2)
16827             break;
16828           add_loc_descr (&loc, loc2);
16829           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16830         }
16831       return loc;
16832     case MINUS_EXPR:
16833       op = DW_OP_minus;
16834       goto do_binop;
16835     case MULT_EXPR:
16836       op = DW_OP_mul;
16837       goto do_binop;
16838     case EQ_EXPR:
16839       op = DW_OP_eq;
16840       goto do_binop;
16841     case NE_EXPR:
16842       op = DW_OP_ne;
16843       goto do_binop;
16844     default:
16845       break;
16846     }
16847   return NULL;
16848 }
16849
16850 static void
16851 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16852                       tree val, tree base_decl)
16853 {
16854   dw_loc_descr_ref loc;
16855
16856   if (host_integerp (val, 0))
16857     {
16858       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16859       return;
16860     }
16861
16862   loc = descr_info_loc (val, base_decl);
16863   if (!loc)
16864     return;
16865
16866   add_AT_loc (die, attr, loc);
16867 }
16868
16869 /* This routine generates DIE for array with hidden descriptor, details
16870    are filled into *info by a langhook.  */
16871
16872 static void
16873 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16874                           dw_die_ref context_die)
16875 {
16876   dw_die_ref scope_die = scope_die_for (type, context_die);
16877   dw_die_ref array_die;
16878   int dim;
16879
16880   array_die = new_die (DW_TAG_array_type, scope_die, type);
16881   add_name_attribute (array_die, type_tag (type));
16882   equate_type_number_to_die (type, array_die);
16883
16884   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16885   if (is_fortran ()
16886       && info->ndimensions >= 2)
16887     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16888
16889   if (info->data_location)
16890     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16891                           info->base_decl);
16892   if (info->associated)
16893     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16894                           info->base_decl);
16895   if (info->allocated)
16896     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16897                           info->base_decl);
16898
16899   for (dim = 0; dim < info->ndimensions; dim++)
16900     {
16901       dw_die_ref subrange_die
16902         = new_die (DW_TAG_subrange_type, array_die, NULL);
16903
16904       if (info->dimen[dim].lower_bound)
16905         {
16906           /* If it is the default value, omit it.  */
16907           int dflt;
16908
16909           if (host_integerp (info->dimen[dim].lower_bound, 0)
16910               && (dflt = lower_bound_default ()) != -1
16911               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16912             ;
16913           else
16914             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16915                                   info->dimen[dim].lower_bound,
16916                                   info->base_decl);
16917         }
16918       if (info->dimen[dim].upper_bound)
16919         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16920                               info->dimen[dim].upper_bound,
16921                               info->base_decl);
16922       if (info->dimen[dim].stride)
16923         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16924                               info->dimen[dim].stride,
16925                               info->base_decl);
16926     }
16927
16928   gen_type_die (info->element_type, context_die);
16929   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16930
16931   if (get_AT (array_die, DW_AT_name))
16932     add_pubtype (type, array_die);
16933 }
16934
16935 #if 0
16936 static void
16937 gen_entry_point_die (tree decl, dw_die_ref context_die)
16938 {
16939   tree origin = decl_ultimate_origin (decl);
16940   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16941
16942   if (origin != NULL)
16943     add_abstract_origin_attribute (decl_die, origin);
16944   else
16945     {
16946       add_name_and_src_coords_attributes (decl_die, decl);
16947       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16948                           0, 0, context_die);
16949     }
16950
16951   if (DECL_ABSTRACT (decl))
16952     equate_decl_number_to_die (decl, decl_die);
16953   else
16954     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16955 }
16956 #endif
16957
16958 /* Walk through the list of incomplete types again, trying once more to
16959    emit full debugging info for them.  */
16960
16961 static void
16962 retry_incomplete_types (void)
16963 {
16964   int i;
16965
16966   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16967     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16968                                   DINFO_USAGE_DIR_USE))
16969       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16970 }
16971
16972 /* Determine what tag to use for a record type.  */
16973
16974 static enum dwarf_tag
16975 record_type_tag (tree type)
16976 {
16977   if (! lang_hooks.types.classify_record)
16978     return DW_TAG_structure_type;
16979
16980   switch (lang_hooks.types.classify_record (type))
16981     {
16982     case RECORD_IS_STRUCT:
16983       return DW_TAG_structure_type;
16984
16985     case RECORD_IS_CLASS:
16986       return DW_TAG_class_type;
16987
16988     case RECORD_IS_INTERFACE:
16989       if (dwarf_version >= 3 || !dwarf_strict)
16990         return DW_TAG_interface_type;
16991       return DW_TAG_structure_type;
16992
16993     default:
16994       gcc_unreachable ();
16995     }
16996 }
16997
16998 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16999    include all of the information about the enumeration values also. Each
17000    enumerated type name/value is listed as a child of the enumerated type
17001    DIE.  */
17002
17003 static dw_die_ref
17004 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17005 {
17006   dw_die_ref type_die = lookup_type_die (type);
17007
17008   if (type_die == NULL)
17009     {
17010       type_die = new_die (DW_TAG_enumeration_type,
17011                           scope_die_for (type, context_die), type);
17012       equate_type_number_to_die (type, type_die);
17013       add_name_attribute (type_die, type_tag (type));
17014       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17015       if (TYPE_ARTIFICIAL (type))
17016         add_AT_flag (type_die, DW_AT_artificial, 1);
17017       if (dwarf_version >= 4 || !dwarf_strict)
17018         {
17019           if (ENUM_IS_SCOPED (type))
17020             add_AT_flag (type_die, DW_AT_enum_class, 1);
17021           if (ENUM_IS_OPAQUE (type))
17022             add_AT_flag (type_die, DW_AT_declaration, 1);
17023         }
17024     }
17025   else if (! TYPE_SIZE (type))
17026     return type_die;
17027   else
17028     remove_AT (type_die, DW_AT_declaration);
17029
17030   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17031      given enum type is incomplete, do not generate the DW_AT_byte_size
17032      attribute or the DW_AT_element_list attribute.  */
17033   if (TYPE_SIZE (type))
17034     {
17035       tree link;
17036
17037       TREE_ASM_WRITTEN (type) = 1;
17038       add_byte_size_attribute (type_die, type);
17039       if (TYPE_STUB_DECL (type) != NULL_TREE)
17040         {
17041           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17042           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17043         }
17044
17045       /* If the first reference to this type was as the return type of an
17046          inline function, then it may not have a parent.  Fix this now.  */
17047       if (type_die->die_parent == NULL)
17048         add_child_die (scope_die_for (type, context_die), type_die);
17049
17050       for (link = TYPE_VALUES (type);
17051            link != NULL; link = TREE_CHAIN (link))
17052         {
17053           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17054           tree value = TREE_VALUE (link);
17055
17056           add_name_attribute (enum_die,
17057                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17058
17059           if (TREE_CODE (value) == CONST_DECL)
17060             value = DECL_INITIAL (value);
17061
17062           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17063             /* DWARF2 does not provide a way of indicating whether or
17064                not enumeration constants are signed or unsigned.  GDB
17065                always assumes the values are signed, so we output all
17066                values as if they were signed.  That means that
17067                enumeration constants with very large unsigned values
17068                will appear to have negative values in the debugger.  */
17069             add_AT_int (enum_die, DW_AT_const_value,
17070                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17071         }
17072     }
17073   else
17074     add_AT_flag (type_die, DW_AT_declaration, 1);
17075
17076   if (get_AT (type_die, DW_AT_name))
17077     add_pubtype (type, type_die);
17078
17079   return type_die;
17080 }
17081
17082 /* Generate a DIE to represent either a real live formal parameter decl or to
17083    represent just the type of some formal parameter position in some function
17084    type.
17085
17086    Note that this routine is a bit unusual because its argument may be a
17087    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17088    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17089    node.  If it's the former then this function is being called to output a
17090    DIE to represent a formal parameter object (or some inlining thereof).  If
17091    it's the latter, then this function is only being called to output a
17092    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17093    argument type of some subprogram type.
17094    If EMIT_NAME_P is true, name and source coordinate attributes
17095    are emitted.  */
17096
17097 static dw_die_ref
17098 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17099                           dw_die_ref context_die)
17100 {
17101   tree node_or_origin = node ? node : origin;
17102   tree ultimate_origin;
17103   dw_die_ref parm_die
17104     = new_die (DW_TAG_formal_parameter, context_die, node);
17105
17106   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17107     {
17108     case tcc_declaration:
17109       ultimate_origin = decl_ultimate_origin (node_or_origin);
17110       if (node || ultimate_origin)
17111         origin = ultimate_origin;
17112       if (origin != NULL)
17113         add_abstract_origin_attribute (parm_die, origin);
17114       else if (emit_name_p)
17115         add_name_and_src_coords_attributes (parm_die, node);
17116       if (origin == NULL
17117           || (! DECL_ABSTRACT (node_or_origin)
17118               && variably_modified_type_p (TREE_TYPE (node_or_origin),
17119                                            decl_function_context
17120                                                             (node_or_origin))))
17121         {
17122           tree type = TREE_TYPE (node_or_origin);
17123           if (decl_by_reference_p (node_or_origin))
17124             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17125                                 context_die);
17126           else
17127             add_type_attribute (parm_die, type,
17128                                 TREE_READONLY (node_or_origin),
17129                                 TREE_THIS_VOLATILE (node_or_origin),
17130                                 context_die);
17131         }
17132       if (origin == NULL && DECL_ARTIFICIAL (node))
17133         add_AT_flag (parm_die, DW_AT_artificial, 1);
17134
17135       if (node && node != origin)
17136         equate_decl_number_to_die (node, parm_die);
17137       if (! DECL_ABSTRACT (node_or_origin))
17138         add_location_or_const_value_attribute (parm_die, node_or_origin,
17139                                                node == NULL, DW_AT_location);
17140
17141       break;
17142
17143     case tcc_type:
17144       /* We were called with some kind of a ..._TYPE node.  */
17145       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17146       break;
17147
17148     default:
17149       gcc_unreachable ();
17150     }
17151
17152   return parm_die;
17153 }
17154
17155 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17156    children DW_TAG_formal_parameter DIEs representing the arguments of the
17157    parameter pack.
17158
17159    PARM_PACK must be a function parameter pack.
17160    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17161    must point to the subsequent arguments of the function PACK_ARG belongs to.
17162    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17163    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17164    following the last one for which a DIE was generated.  */
17165
17166 static dw_die_ref
17167 gen_formal_parameter_pack_die  (tree parm_pack,
17168                                 tree pack_arg,
17169                                 dw_die_ref subr_die,
17170                                 tree *next_arg)
17171 {
17172   tree arg;
17173   dw_die_ref parm_pack_die;
17174
17175   gcc_assert (parm_pack
17176               && lang_hooks.function_parameter_pack_p (parm_pack)
17177               && subr_die);
17178
17179   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17180   add_src_coords_attributes (parm_pack_die, parm_pack);
17181
17182   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17183     {
17184       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17185                                                                  parm_pack))
17186         break;
17187       gen_formal_parameter_die (arg, NULL,
17188                                 false /* Don't emit name attribute.  */,
17189                                 parm_pack_die);
17190     }
17191   if (next_arg)
17192     *next_arg = arg;
17193   return parm_pack_die;
17194 }
17195
17196 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17197    at the end of an (ANSI prototyped) formal parameters list.  */
17198
17199 static void
17200 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17201 {
17202   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17203 }
17204
17205 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17206    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17207    parameters as specified in some function type specification (except for
17208    those which appear as part of a function *definition*).  */
17209
17210 static void
17211 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17212 {
17213   tree link;
17214   tree formal_type = NULL;
17215   tree first_parm_type;
17216   tree arg;
17217
17218   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17219     {
17220       arg = DECL_ARGUMENTS (function_or_method_type);
17221       function_or_method_type = TREE_TYPE (function_or_method_type);
17222     }
17223   else
17224     arg = NULL_TREE;
17225
17226   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17227
17228   /* Make our first pass over the list of formal parameter types and output a
17229      DW_TAG_formal_parameter DIE for each one.  */
17230   for (link = first_parm_type; link; )
17231     {
17232       dw_die_ref parm_die;
17233
17234       formal_type = TREE_VALUE (link);
17235       if (formal_type == void_type_node)
17236         break;
17237
17238       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17239       parm_die = gen_formal_parameter_die (formal_type, NULL,
17240                                            true /* Emit name attribute.  */,
17241                                            context_die);
17242       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17243           && link == first_parm_type)
17244         {
17245           add_AT_flag (parm_die, DW_AT_artificial, 1);
17246           if (dwarf_version >= 3 || !dwarf_strict)
17247             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17248         }
17249       else if (arg && DECL_ARTIFICIAL (arg))
17250         add_AT_flag (parm_die, DW_AT_artificial, 1);
17251
17252       link = TREE_CHAIN (link);
17253       if (arg)
17254         arg = DECL_CHAIN (arg);
17255     }
17256
17257   /* If this function type has an ellipsis, add a
17258      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17259   if (formal_type != void_type_node)
17260     gen_unspecified_parameters_die (function_or_method_type, context_die);
17261
17262   /* Make our second (and final) pass over the list of formal parameter types
17263      and output DIEs to represent those types (as necessary).  */
17264   for (link = TYPE_ARG_TYPES (function_or_method_type);
17265        link && TREE_VALUE (link);
17266        link = TREE_CHAIN (link))
17267     gen_type_die (TREE_VALUE (link), context_die);
17268 }
17269
17270 /* We want to generate the DIE for TYPE so that we can generate the
17271    die for MEMBER, which has been defined; we will need to refer back
17272    to the member declaration nested within TYPE.  If we're trying to
17273    generate minimal debug info for TYPE, processing TYPE won't do the
17274    trick; we need to attach the member declaration by hand.  */
17275
17276 static void
17277 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17278 {
17279   gen_type_die (type, context_die);
17280
17281   /* If we're trying to avoid duplicate debug info, we may not have
17282      emitted the member decl for this function.  Emit it now.  */
17283   if (TYPE_STUB_DECL (type)
17284       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17285       && ! lookup_decl_die (member))
17286     {
17287       dw_die_ref type_die;
17288       gcc_assert (!decl_ultimate_origin (member));
17289
17290       push_decl_scope (type);
17291       type_die = lookup_type_die_strip_naming_typedef (type);
17292       if (TREE_CODE (member) == FUNCTION_DECL)
17293         gen_subprogram_die (member, type_die);
17294       else if (TREE_CODE (member) == FIELD_DECL)
17295         {
17296           /* Ignore the nameless fields that are used to skip bits but handle
17297              C++ anonymous unions and structs.  */
17298           if (DECL_NAME (member) != NULL_TREE
17299               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17300               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17301             {
17302               gen_type_die (member_declared_type (member), type_die);
17303               gen_field_die (member, type_die);
17304             }
17305         }
17306       else
17307         gen_variable_die (member, NULL_TREE, type_die);
17308
17309       pop_decl_scope ();
17310     }
17311 }
17312
17313 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17314    may later generate inlined and/or out-of-line instances of.  */
17315
17316 static void
17317 dwarf2out_abstract_function (tree decl)
17318 {
17319   dw_die_ref old_die;
17320   tree save_fn;
17321   tree context;
17322   int was_abstract;
17323   htab_t old_decl_loc_table;
17324   htab_t old_cached_dw_loc_list_table;
17325   int old_call_site_count, old_tail_call_site_count;
17326   struct call_arg_loc_node *old_call_arg_locations;
17327
17328   /* Make sure we have the actual abstract inline, not a clone.  */
17329   decl = DECL_ORIGIN (decl);
17330
17331   old_die = lookup_decl_die (decl);
17332   if (old_die && get_AT (old_die, DW_AT_inline))
17333     /* We've already generated the abstract instance.  */
17334     return;
17335
17336   /* We can be called while recursively when seeing block defining inlined subroutine
17337      DIE.  Be sure to not clobber the outer location table nor use it or we would
17338      get locations in abstract instantces.  */
17339   old_decl_loc_table = decl_loc_table;
17340   decl_loc_table = NULL;
17341   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17342   cached_dw_loc_list_table = NULL;
17343   old_call_arg_locations = call_arg_locations;
17344   call_arg_locations = NULL;
17345   old_call_site_count = call_site_count;
17346   call_site_count = -1;
17347   old_tail_call_site_count = tail_call_site_count;
17348   tail_call_site_count = -1;
17349
17350   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17351      we don't get confused by DECL_ABSTRACT.  */
17352   if (debug_info_level > DINFO_LEVEL_TERSE)
17353     {
17354       context = decl_class_context (decl);
17355       if (context)
17356         gen_type_die_for_member
17357           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17358     }
17359
17360   /* Pretend we've just finished compiling this function.  */
17361   save_fn = current_function_decl;
17362   current_function_decl = decl;
17363   push_cfun (DECL_STRUCT_FUNCTION (decl));
17364
17365   was_abstract = DECL_ABSTRACT (decl);
17366   set_decl_abstract_flags (decl, 1);
17367   dwarf2out_decl (decl);
17368   if (! was_abstract)
17369     set_decl_abstract_flags (decl, 0);
17370
17371   current_function_decl = save_fn;
17372   decl_loc_table = old_decl_loc_table;
17373   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17374   call_arg_locations = old_call_arg_locations;
17375   call_site_count = old_call_site_count;
17376   tail_call_site_count = old_tail_call_site_count;
17377   pop_cfun ();
17378 }
17379
17380 /* Helper function of premark_used_types() which gets called through
17381    htab_traverse.
17382
17383    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17384    marked as unused by prune_unused_types.  */
17385
17386 static int
17387 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17388 {
17389   tree type;
17390   dw_die_ref die;
17391
17392   type = (tree) *slot;
17393   die = lookup_type_die (type);
17394   if (die != NULL)
17395     die->die_perennial_p = 1;
17396   return 1;
17397 }
17398
17399 /* Helper function of premark_types_used_by_global_vars which gets called
17400    through htab_traverse.
17401
17402    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17403    marked as unused by prune_unused_types. The DIE of the type is marked
17404    only if the global variable using the type will actually be emitted.  */
17405
17406 static int
17407 premark_types_used_by_global_vars_helper (void **slot,
17408                                           void *data ATTRIBUTE_UNUSED)
17409 {
17410   struct types_used_by_vars_entry *entry;
17411   dw_die_ref die;
17412
17413   entry = (struct types_used_by_vars_entry *) *slot;
17414   gcc_assert (entry->type != NULL
17415               && entry->var_decl != NULL);
17416   die = lookup_type_die (entry->type);
17417   if (die)
17418     {
17419       /* Ask cgraph if the global variable really is to be emitted.
17420          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17421       struct varpool_node *node = varpool_get_node (entry->var_decl);
17422       if (node && node->needed)
17423         {
17424           die->die_perennial_p = 1;
17425           /* Keep the parent DIEs as well.  */
17426           while ((die = die->die_parent) && die->die_perennial_p == 0)
17427             die->die_perennial_p = 1;
17428         }
17429     }
17430   return 1;
17431 }
17432
17433 /* Mark all members of used_types_hash as perennial.  */
17434
17435 static void
17436 premark_used_types (void)
17437 {
17438   if (cfun && cfun->used_types_hash)
17439     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17440 }
17441
17442 /* Mark all members of types_used_by_vars_entry as perennial.  */
17443
17444 static void
17445 premark_types_used_by_global_vars (void)
17446 {
17447   if (types_used_by_vars_hash)
17448     htab_traverse (types_used_by_vars_hash,
17449                    premark_types_used_by_global_vars_helper, NULL);
17450 }
17451
17452 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17453    for CA_LOC call arg loc node.  */
17454
17455 static dw_die_ref
17456 gen_call_site_die (tree decl, dw_die_ref subr_die,
17457                    struct call_arg_loc_node *ca_loc)
17458 {
17459   dw_die_ref stmt_die = NULL, die;
17460   tree block = ca_loc->block;
17461
17462   while (block
17463          && block != DECL_INITIAL (decl)
17464          && TREE_CODE (block) == BLOCK)
17465     {
17466       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17467         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17468       if (stmt_die)
17469         break;
17470       block = BLOCK_SUPERCONTEXT (block);
17471     }
17472   if (stmt_die == NULL)
17473     stmt_die = subr_die;
17474   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17475   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17476   if (ca_loc->tail_call_p)
17477     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17478   if (ca_loc->symbol_ref)
17479     {
17480       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17481       if (tdie)
17482         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17483       else
17484         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17485     }
17486   return die;
17487 }
17488
17489 /* Generate a DIE to represent a declared function (either file-scope or
17490    block-local).  */
17491
17492 static void
17493 gen_subprogram_die (tree decl, dw_die_ref context_die)
17494 {
17495   tree origin = decl_ultimate_origin (decl);
17496   dw_die_ref subr_die;
17497   tree outer_scope;
17498   dw_die_ref old_die = lookup_decl_die (decl);
17499   int declaration = (current_function_decl != decl
17500                      || class_or_namespace_scope_p (context_die));
17501
17502   premark_used_types ();
17503
17504   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17505      started to generate the abstract instance of an inline, decided to output
17506      its containing class, and proceeded to emit the declaration of the inline
17507      from the member list for the class.  If so, DECLARATION takes priority;
17508      we'll get back to the abstract instance when done with the class.  */
17509
17510   /* The class-scope declaration DIE must be the primary DIE.  */
17511   if (origin && declaration && class_or_namespace_scope_p (context_die))
17512     {
17513       origin = NULL;
17514       gcc_assert (!old_die);
17515     }
17516
17517   /* Now that the C++ front end lazily declares artificial member fns, we
17518      might need to retrofit the declaration into its class.  */
17519   if (!declaration && !origin && !old_die
17520       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17521       && !class_or_namespace_scope_p (context_die)
17522       && debug_info_level > DINFO_LEVEL_TERSE)
17523     old_die = force_decl_die (decl);
17524
17525   if (origin != NULL)
17526     {
17527       gcc_assert (!declaration || local_scope_p (context_die));
17528
17529       /* Fixup die_parent for the abstract instance of a nested
17530          inline function.  */
17531       if (old_die && old_die->die_parent == NULL)
17532         add_child_die (context_die, old_die);
17533
17534       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17535       add_abstract_origin_attribute (subr_die, origin);
17536       /*  This is where the actual code for a cloned function is.
17537           Let's emit linkage name attribute for it.  This helps
17538           debuggers to e.g, set breakpoints into
17539           constructors/destructors when the user asks "break
17540           K::K".  */
17541       add_linkage_name (subr_die, decl);
17542     }
17543   else if (old_die)
17544     {
17545       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17546       struct dwarf_file_data * file_index = lookup_filename (s.file);
17547
17548       if (!get_AT_flag (old_die, DW_AT_declaration)
17549           /* We can have a normal definition following an inline one in the
17550              case of redefinition of GNU C extern inlines.
17551              It seems reasonable to use AT_specification in this case.  */
17552           && !get_AT (old_die, DW_AT_inline))
17553         {
17554           /* Detect and ignore this case, where we are trying to output
17555              something we have already output.  */
17556           return;
17557         }
17558
17559       /* If the definition comes from the same place as the declaration,
17560          maybe use the old DIE.  We always want the DIE for this function
17561          that has the *_pc attributes to be under comp_unit_die so the
17562          debugger can find it.  We also need to do this for abstract
17563          instances of inlines, since the spec requires the out-of-line copy
17564          to have the same parent.  For local class methods, this doesn't
17565          apply; we just use the old DIE.  */
17566       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17567           && (DECL_ARTIFICIAL (decl)
17568               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17569                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17570                       == (unsigned) s.line))))
17571         {
17572           subr_die = old_die;
17573
17574           /* Clear out the declaration attribute and the formal parameters.
17575              Do not remove all children, because it is possible that this
17576              declaration die was forced using force_decl_die(). In such
17577              cases die that forced declaration die (e.g. TAG_imported_module)
17578              is one of the children that we do not want to remove.  */
17579           remove_AT (subr_die, DW_AT_declaration);
17580           remove_AT (subr_die, DW_AT_object_pointer);
17581           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17582         }
17583       else
17584         {
17585           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17586           add_AT_specification (subr_die, old_die);
17587           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17588             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17589           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17590             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17591         }
17592     }
17593   else
17594     {
17595       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17596
17597       if (TREE_PUBLIC (decl))
17598         add_AT_flag (subr_die, DW_AT_external, 1);
17599
17600       add_name_and_src_coords_attributes (subr_die, decl);
17601       if (debug_info_level > DINFO_LEVEL_TERSE)
17602         {
17603           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17604           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17605                               0, 0, context_die);
17606         }
17607
17608       add_pure_or_virtual_attribute (subr_die, decl);
17609       if (DECL_ARTIFICIAL (decl))
17610         add_AT_flag (subr_die, DW_AT_artificial, 1);
17611
17612       add_accessibility_attribute (subr_die, decl);
17613     }
17614
17615   if (declaration)
17616     {
17617       if (!old_die || !get_AT (old_die, DW_AT_inline))
17618         {
17619           add_AT_flag (subr_die, DW_AT_declaration, 1);
17620
17621           /* If this is an explicit function declaration then generate
17622              a DW_AT_explicit attribute.  */
17623           if (lang_hooks.decls.function_decl_explicit_p (decl)
17624               && (dwarf_version >= 3 || !dwarf_strict))
17625             add_AT_flag (subr_die, DW_AT_explicit, 1);
17626
17627           /* The first time we see a member function, it is in the context of
17628              the class to which it belongs.  We make sure of this by emitting
17629              the class first.  The next time is the definition, which is
17630              handled above.  The two may come from the same source text.
17631
17632              Note that force_decl_die() forces function declaration die. It is
17633              later reused to represent definition.  */
17634           equate_decl_number_to_die (decl, subr_die);
17635         }
17636     }
17637   else if (DECL_ABSTRACT (decl))
17638     {
17639       if (DECL_DECLARED_INLINE_P (decl))
17640         {
17641           if (cgraph_function_possibly_inlined_p (decl))
17642             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17643           else
17644             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17645         }
17646       else
17647         {
17648           if (cgraph_function_possibly_inlined_p (decl))
17649             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17650           else
17651             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17652         }
17653
17654       if (DECL_DECLARED_INLINE_P (decl)
17655           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17656         add_AT_flag (subr_die, DW_AT_artificial, 1);
17657
17658       equate_decl_number_to_die (decl, subr_die);
17659     }
17660   else if (!DECL_EXTERNAL (decl))
17661     {
17662       HOST_WIDE_INT cfa_fb_offset;
17663
17664       if (!old_die || !get_AT (old_die, DW_AT_inline))
17665         equate_decl_number_to_die (decl, subr_die);
17666
17667       if (!flag_reorder_blocks_and_partition)
17668         {
17669           dw_fde_ref fde = &fde_table[current_funcdef_fde];
17670           if (fde->dw_fde_begin)
17671             {
17672               /* We have already generated the labels.  */
17673               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17674               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17675             }
17676           else
17677             {
17678               /* Create start/end labels and add the range.  */
17679               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17680               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17681                                            current_function_funcdef_no);
17682               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17683               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17684                                            current_function_funcdef_no);
17685               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17686             }
17687
17688 #if VMS_DEBUGGING_INFO
17689       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17690          Section 2.3 Prologue and Epilogue Attributes:
17691          When a breakpoint is set on entry to a function, it is generally
17692          desirable for execution to be suspended, not on the very first
17693          instruction of the function, but rather at a point after the
17694          function's frame has been set up, after any language defined local
17695          declaration processing has been completed, and before execution of
17696          the first statement of the function begins. Debuggers generally
17697          cannot properly determine where this point is.  Similarly for a
17698          breakpoint set on exit from a function. The prologue and epilogue
17699          attributes allow a compiler to communicate the location(s) to use.  */
17700
17701       {
17702         if (fde->dw_fde_vms_end_prologue)
17703           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17704             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17705
17706         if (fde->dw_fde_vms_begin_epilogue)
17707           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17708             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17709       }
17710 #endif
17711
17712           add_pubname (decl, subr_die);
17713         }
17714       else
17715         {  /* Generate pubnames entries for the split function code
17716               ranges.  */
17717           dw_fde_ref fde = &fde_table[current_funcdef_fde];
17718
17719           if (fde->dw_fde_second_begin)
17720             {
17721               if (dwarf_version >= 3 || !dwarf_strict)
17722                 {
17723                   /* We should use ranges for non-contiguous code section 
17724                      addresses.  Use the actual code range for the initial
17725                      section, since the HOT/COLD labels might precede an 
17726                      alignment offset.  */
17727                   bool range_list_added = false;
17728                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17729                                         fde->dw_fde_end, &range_list_added);
17730                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17731                                         fde->dw_fde_second_end,
17732                                         &range_list_added);
17733                   add_pubname (decl, subr_die);
17734                   if (range_list_added)
17735                     add_ranges (NULL);
17736                 }
17737               else
17738                 {
17739                   /* There is no real support in DW2 for this .. so we make
17740                      a work-around.  First, emit the pub name for the segment
17741                      containing the function label.  Then make and emit a
17742                      simplified subprogram DIE for the second segment with the
17743                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17744                      linkage name for the second die so that gdb will find both
17745                      sections when given "b foo".  */
17746                   const char *name = NULL;
17747                   tree decl_name = DECL_NAME (decl);
17748                   dw_die_ref seg_die;
17749
17750                   /* Do the 'primary' section.   */
17751                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17752                                  fde->dw_fde_begin);
17753                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17754                                  fde->dw_fde_end);
17755                   /* Add it.   */
17756                   add_pubname (decl, subr_die);
17757
17758                   /* Build a minimal DIE for the secondary section.  */
17759                   seg_die = new_die (DW_TAG_subprogram,
17760                                      subr_die->die_parent, decl);
17761
17762                   if (TREE_PUBLIC (decl))
17763                     add_AT_flag (seg_die, DW_AT_external, 1);
17764
17765                   if (decl_name != NULL 
17766                       && IDENTIFIER_POINTER (decl_name) != NULL)
17767                     {
17768                       name = dwarf2_name (decl, 1);
17769                       if (! DECL_ARTIFICIAL (decl))
17770                         add_src_coords_attributes (seg_die, decl);
17771
17772                       add_linkage_name (seg_die, decl);
17773                     }
17774                   gcc_assert (name != NULL);
17775                   add_pure_or_virtual_attribute (seg_die, decl);
17776                   if (DECL_ARTIFICIAL (decl))
17777                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17778
17779                   name = concat ("__second_sect_of_", name, NULL); 
17780                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17781                                  fde->dw_fde_second_begin);
17782                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17783                                  fde->dw_fde_second_end);
17784                   add_name_attribute (seg_die, name);
17785                   add_pubname_string (name, seg_die);
17786                 }
17787             }
17788           else
17789             {
17790               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17791               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17792               add_pubname (decl, subr_die);
17793             }
17794         }
17795
17796 #ifdef MIPS_DEBUGGING_INFO
17797       /* Add a reference to the FDE for this routine.  */
17798       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
17799 #endif
17800
17801       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17802
17803       /* We define the "frame base" as the function's CFA.  This is more
17804          convenient for several reasons: (1) It's stable across the prologue
17805          and epilogue, which makes it better than just a frame pointer,
17806          (2) With dwarf3, there exists a one-byte encoding that allows us
17807          to reference the .debug_frame data by proxy, but failing that,
17808          (3) We can at least reuse the code inspection and interpretation
17809          code that determines the CFA position at various points in the
17810          function.  */
17811       if (dwarf_version >= 3)
17812         {
17813           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17814           add_AT_loc (subr_die, DW_AT_frame_base, op);
17815         }
17816       else
17817         {
17818           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17819           if (list->dw_loc_next)
17820             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17821           else
17822             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17823         }
17824
17825       /* Compute a displacement from the "steady-state frame pointer" to
17826          the CFA.  The former is what all stack slots and argument slots
17827          will reference in the rtl; the later is what we've told the
17828          debugger about.  We'll need to adjust all frame_base references
17829          by this displacement.  */
17830       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17831
17832       if (cfun->static_chain_decl)
17833         add_AT_location_description (subr_die, DW_AT_static_link,
17834                  loc_list_from_tree (cfun->static_chain_decl, 2));
17835     }
17836
17837   /* Generate child dies for template paramaters.  */
17838   if (debug_info_level > DINFO_LEVEL_TERSE)
17839     gen_generic_params_dies (decl);
17840
17841   /* Now output descriptions of the arguments for this function. This gets
17842      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17843      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17844      `...' at the end of the formal parameter list.  In order to find out if
17845      there was a trailing ellipsis or not, we must instead look at the type
17846      associated with the FUNCTION_DECL.  This will be a node of type
17847      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17848      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17849      an ellipsis at the end.  */
17850
17851   /* In the case where we are describing a mere function declaration, all we
17852      need to do here (and all we *can* do here) is to describe the *types* of
17853      its formal parameters.  */
17854   if (debug_info_level <= DINFO_LEVEL_TERSE)
17855     ;
17856   else if (declaration)
17857     gen_formal_types_die (decl, subr_die);
17858   else
17859     {
17860       /* Generate DIEs to represent all known formal parameters.  */
17861       tree parm = DECL_ARGUMENTS (decl);
17862       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17863       tree generic_decl_parm = generic_decl
17864                                 ? DECL_ARGUMENTS (generic_decl)
17865                                 : NULL;
17866
17867       /* Now we want to walk the list of parameters of the function and
17868          emit their relevant DIEs.
17869
17870          We consider the case of DECL being an instance of a generic function
17871          as well as it being a normal function.
17872
17873          If DECL is an instance of a generic function we walk the
17874          parameters of the generic function declaration _and_ the parameters of
17875          DECL itself. This is useful because we want to emit specific DIEs for
17876          function parameter packs and those are declared as part of the
17877          generic function declaration. In that particular case,
17878          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17879          That DIE has children DIEs representing the set of arguments
17880          of the pack. Note that the set of pack arguments can be empty.
17881          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17882          children DIE.
17883
17884          Otherwise, we just consider the parameters of DECL.  */
17885       while (generic_decl_parm || parm)
17886         {
17887           if (generic_decl_parm
17888               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17889             gen_formal_parameter_pack_die (generic_decl_parm,
17890                                            parm, subr_die,
17891                                            &parm);
17892           else if (parm)
17893             {
17894               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17895
17896               if (parm == DECL_ARGUMENTS (decl)
17897                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17898                   && parm_die
17899                   && (dwarf_version >= 3 || !dwarf_strict))
17900                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17901
17902               parm = DECL_CHAIN (parm);
17903             }
17904
17905           if (generic_decl_parm)
17906             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17907         }
17908
17909       /* Decide whether we need an unspecified_parameters DIE at the end.
17910          There are 2 more cases to do this for: 1) the ansi ... declaration -
17911          this is detectable when the end of the arg list is not a
17912          void_type_node 2) an unprototyped function declaration (not a
17913          definition).  This just means that we have no info about the
17914          parameters at all.  */
17915       if (prototype_p (TREE_TYPE (decl)))
17916         {
17917           /* This is the prototyped case, check for....  */
17918           if (stdarg_p (TREE_TYPE (decl)))
17919             gen_unspecified_parameters_die (decl, subr_die);
17920         }
17921       else if (DECL_INITIAL (decl) == NULL_TREE)
17922         gen_unspecified_parameters_die (decl, subr_die);
17923     }
17924
17925   /* Output Dwarf info for all of the stuff within the body of the function
17926      (if it has one - it may be just a declaration).  */
17927   outer_scope = DECL_INITIAL (decl);
17928
17929   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17930      a function.  This BLOCK actually represents the outermost binding contour
17931      for the function, i.e. the contour in which the function's formal
17932      parameters and labels get declared. Curiously, it appears that the front
17933      end doesn't actually put the PARM_DECL nodes for the current function onto
17934      the BLOCK_VARS list for this outer scope, but are strung off of the
17935      DECL_ARGUMENTS list for the function instead.
17936
17937      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17938      the LABEL_DECL nodes for the function however, and we output DWARF info
17939      for those in decls_for_scope.  Just within the `outer_scope' there will be
17940      a BLOCK node representing the function's outermost pair of curly braces,
17941      and any blocks used for the base and member initializers of a C++
17942      constructor function.  */
17943   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17944     {
17945       int call_site_note_count = 0;
17946       int tail_call_site_note_count = 0;
17947
17948       /* Emit a DW_TAG_variable DIE for a named return value.  */
17949       if (DECL_NAME (DECL_RESULT (decl)))
17950         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17951
17952       current_function_has_inlines = 0;
17953       decls_for_scope (outer_scope, subr_die, 0);
17954
17955       if (call_arg_locations && !dwarf_strict)
17956         {
17957           struct call_arg_loc_node *ca_loc;
17958           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17959             {
17960               dw_die_ref die = NULL;
17961               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17962               rtx arg, next_arg;
17963
17964               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17965                    arg; arg = next_arg)
17966                 {
17967                   dw_loc_descr_ref reg, val;
17968                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17969                   dw_die_ref cdie, tdie = NULL;
17970
17971                   next_arg = XEXP (arg, 1);
17972                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17973                       && next_arg
17974                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17975                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17976                       && REGNO (XEXP (XEXP (arg, 0), 0))
17977                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17978                     next_arg = XEXP (next_arg, 1);
17979                   if (mode == VOIDmode)
17980                     {
17981                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17982                       if (mode == VOIDmode)
17983                         mode = GET_MODE (XEXP (arg, 0));
17984                     }
17985                   if (mode == VOIDmode || mode == BLKmode)
17986                     continue;
17987                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17988                     {
17989                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17990                       tloc = XEXP (XEXP (arg, 0), 1);
17991                       continue;
17992                     }
17993                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17994                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17995                     {
17996                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17997                       tlocc = XEXP (XEXP (arg, 0), 1);
17998                       continue;
17999                     }
18000                   reg = NULL;
18001                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
18002                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18003                                               VAR_INIT_STATUS_INITIALIZED);
18004                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18005                     {
18006                       rtx mem = XEXP (XEXP (arg, 0), 0);
18007                       reg = mem_loc_descriptor (XEXP (mem, 0),
18008                                                 get_address_mode (mem),
18009                                                 GET_MODE (mem),
18010                                                 VAR_INIT_STATUS_INITIALIZED);
18011                     }
18012                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18013                            == DEBUG_PARAMETER_REF)
18014                     {
18015                       tree tdecl
18016                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18017                       tdie = lookup_decl_die (tdecl);
18018                       if (tdie == NULL)
18019                         continue;
18020                     }
18021                   else
18022                     continue;
18023                   if (reg == NULL
18024                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
18025                          != DEBUG_PARAMETER_REF)
18026                     continue;
18027                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18028                                             VOIDmode,
18029                                             VAR_INIT_STATUS_INITIALIZED);
18030                   if (val == NULL)
18031                     continue;
18032                   if (die == NULL)
18033                     die = gen_call_site_die (decl, subr_die, ca_loc);
18034                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18035                                   NULL_TREE);
18036                   if (reg != NULL)
18037                     add_AT_loc (cdie, DW_AT_location, reg);
18038                   else if (tdie != NULL)
18039                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18040                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18041                   if (next_arg != XEXP (arg, 1))
18042                     {
18043                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18044                       if (mode == VOIDmode)
18045                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18046                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18047                                                             0), 1),
18048                                                 mode, VOIDmode,
18049                                                 VAR_INIT_STATUS_INITIALIZED);
18050                       if (val != NULL)
18051                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18052                     }
18053                 }
18054               if (die == NULL
18055                   && (ca_loc->symbol_ref || tloc))
18056                 die = gen_call_site_die (decl, subr_die, ca_loc);
18057               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18058                 {
18059                   dw_loc_descr_ref tval = NULL;
18060
18061                   if (tloc != NULL_RTX)
18062                     tval = mem_loc_descriptor (tloc,
18063                                                GET_MODE (tloc) == VOIDmode
18064                                                ? Pmode : GET_MODE (tloc),
18065                                                VOIDmode,
18066                                                VAR_INIT_STATUS_INITIALIZED);
18067                   if (tval)
18068                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18069                   else if (tlocc != NULL_RTX)
18070                     {
18071                       tval = mem_loc_descriptor (tlocc,
18072                                                  GET_MODE (tlocc) == VOIDmode
18073                                                  ? Pmode : GET_MODE (tlocc),
18074                                                  VOIDmode,
18075                                                  VAR_INIT_STATUS_INITIALIZED);
18076                       if (tval)
18077                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18078                                     tval);
18079                     }
18080                 }
18081               if (die != NULL)
18082                 {
18083                   call_site_note_count++;
18084                   if (ca_loc->tail_call_p)
18085                     tail_call_site_note_count++;
18086                 }
18087             }
18088         }
18089       call_arg_locations = NULL;
18090       call_arg_loc_last = NULL;
18091       if (tail_call_site_count >= 0
18092           && tail_call_site_count == tail_call_site_note_count
18093           && !dwarf_strict)
18094         {
18095           if (call_site_count >= 0
18096               && call_site_count == call_site_note_count)
18097             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18098           else
18099             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18100         }
18101       call_site_count = -1;
18102       tail_call_site_count = -1;
18103     }
18104   /* Add the calling convention attribute if requested.  */
18105   add_calling_convention_attribute (subr_die, decl);
18106
18107 }
18108
18109 /* Returns a hash value for X (which really is a die_struct).  */
18110
18111 static hashval_t
18112 common_block_die_table_hash (const void *x)
18113 {
18114   const_dw_die_ref d = (const_dw_die_ref) x;
18115   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18116 }
18117
18118 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18119    as decl_id and die_parent of die_struct Y.  */
18120
18121 static int
18122 common_block_die_table_eq (const void *x, const void *y)
18123 {
18124   const_dw_die_ref d = (const_dw_die_ref) x;
18125   const_dw_die_ref e = (const_dw_die_ref) y;
18126   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18127 }
18128
18129 /* Generate a DIE to represent a declared data object.
18130    Either DECL or ORIGIN must be non-null.  */
18131
18132 static void
18133 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18134 {
18135   HOST_WIDE_INT off;
18136   tree com_decl;
18137   tree decl_or_origin = decl ? decl : origin;
18138   tree ultimate_origin;
18139   dw_die_ref var_die;
18140   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18141   dw_die_ref origin_die;
18142   bool declaration = (DECL_EXTERNAL (decl_or_origin)
18143                       || class_or_namespace_scope_p (context_die));
18144   bool specialization_p = false;
18145
18146   ultimate_origin = decl_ultimate_origin (decl_or_origin);
18147   if (decl || ultimate_origin)
18148     origin = ultimate_origin;
18149   com_decl = fortran_common (decl_or_origin, &off);
18150
18151   /* Symbol in common gets emitted as a child of the common block, in the form
18152      of a data member.  */
18153   if (com_decl)
18154     {
18155       dw_die_ref com_die;
18156       dw_loc_list_ref loc;
18157       die_node com_die_arg;
18158
18159       var_die = lookup_decl_die (decl_or_origin);
18160       if (var_die)
18161         {
18162           if (get_AT (var_die, DW_AT_location) == NULL)
18163             {
18164               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18165               if (loc)
18166                 {
18167                   if (off)
18168                     {
18169                       /* Optimize the common case.  */
18170                       if (single_element_loc_list_p (loc)
18171                           && loc->expr->dw_loc_opc == DW_OP_addr
18172                           && loc->expr->dw_loc_next == NULL
18173                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18174                              == SYMBOL_REF)
18175                         loc->expr->dw_loc_oprnd1.v.val_addr
18176                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18177                         else
18178                           loc_list_plus_const (loc, off);
18179                     }
18180                   add_AT_location_description (var_die, DW_AT_location, loc);
18181                   remove_AT (var_die, DW_AT_declaration);
18182                 }
18183             }
18184           return;
18185         }
18186
18187       if (common_block_die_table == NULL)
18188         common_block_die_table
18189           = htab_create_ggc (10, common_block_die_table_hash,
18190                              common_block_die_table_eq, NULL);
18191
18192       com_die_arg.decl_id = DECL_UID (com_decl);
18193       com_die_arg.die_parent = context_die;
18194       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18195       loc = loc_list_from_tree (com_decl, 2);
18196       if (com_die == NULL)
18197         {
18198           const char *cnam
18199             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18200           void **slot;
18201
18202           com_die = new_die (DW_TAG_common_block, context_die, decl);
18203           add_name_and_src_coords_attributes (com_die, com_decl);
18204           if (loc)
18205             {
18206               add_AT_location_description (com_die, DW_AT_location, loc);
18207               /* Avoid sharing the same loc descriptor between
18208                  DW_TAG_common_block and DW_TAG_variable.  */
18209               loc = loc_list_from_tree (com_decl, 2);
18210             }
18211           else if (DECL_EXTERNAL (decl))
18212             add_AT_flag (com_die, DW_AT_declaration, 1);
18213           add_pubname_string (cnam, com_die); /* ??? needed? */
18214           com_die->decl_id = DECL_UID (com_decl);
18215           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18216           *slot = (void *) com_die;
18217         }
18218       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18219         {
18220           add_AT_location_description (com_die, DW_AT_location, loc);
18221           loc = loc_list_from_tree (com_decl, 2);
18222           remove_AT (com_die, DW_AT_declaration);
18223         }
18224       var_die = new_die (DW_TAG_variable, com_die, decl);
18225       add_name_and_src_coords_attributes (var_die, decl);
18226       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18227                           TREE_THIS_VOLATILE (decl), context_die);
18228       add_AT_flag (var_die, DW_AT_external, 1);
18229       if (loc)
18230         {
18231           if (off)
18232             {
18233               /* Optimize the common case.  */
18234               if (single_element_loc_list_p (loc)
18235                   && loc->expr->dw_loc_opc == DW_OP_addr
18236                   && loc->expr->dw_loc_next == NULL
18237                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18238                 loc->expr->dw_loc_oprnd1.v.val_addr
18239                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18240               else
18241                 loc_list_plus_const (loc, off);
18242             }
18243           add_AT_location_description (var_die, DW_AT_location, loc);
18244         }
18245       else if (DECL_EXTERNAL (decl))
18246         add_AT_flag (var_die, DW_AT_declaration, 1);
18247       equate_decl_number_to_die (decl, var_die);
18248       return;
18249     }
18250
18251   /* If the compiler emitted a definition for the DECL declaration
18252      and if we already emitted a DIE for it, don't emit a second
18253      DIE for it again. Allow re-declarations of DECLs that are
18254      inside functions, though.  */
18255   if (old_die && declaration && !local_scope_p (context_die))
18256     return;
18257
18258   /* For static data members, the declaration in the class is supposed
18259      to have DW_TAG_member tag; the specification should still be
18260      DW_TAG_variable referencing the DW_TAG_member DIE.  */
18261   if (declaration && class_scope_p (context_die))
18262     var_die = new_die (DW_TAG_member, context_die, decl);
18263   else
18264     var_die = new_die (DW_TAG_variable, context_die, decl);
18265
18266   origin_die = NULL;
18267   if (origin != NULL)
18268     origin_die = add_abstract_origin_attribute (var_die, origin);
18269
18270   /* Loop unrolling can create multiple blocks that refer to the same
18271      static variable, so we must test for the DW_AT_declaration flag.
18272
18273      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18274      copy decls and set the DECL_ABSTRACT flag on them instead of
18275      sharing them.
18276
18277      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18278
18279      ??? The declare_in_namespace support causes us to get two DIEs for one
18280      variable, both of which are declarations.  We want to avoid considering
18281      one to be a specification, so we must test that this DIE is not a
18282      declaration.  */
18283   else if (old_die && TREE_STATIC (decl) && ! declaration
18284            && get_AT_flag (old_die, DW_AT_declaration) == 1)
18285     {
18286       /* This is a definition of a C++ class level static.  */
18287       add_AT_specification (var_die, old_die);
18288       specialization_p = true;
18289       if (DECL_NAME (decl))
18290         {
18291           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18292           struct dwarf_file_data * file_index = lookup_filename (s.file);
18293
18294           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18295             add_AT_file (var_die, DW_AT_decl_file, file_index);
18296
18297           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18298             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18299
18300           if (old_die->die_tag == DW_TAG_member)
18301             add_linkage_name (var_die, decl);
18302         }
18303     }
18304   else
18305     add_name_and_src_coords_attributes (var_die, decl);
18306
18307   if ((origin == NULL && !specialization_p)
18308       || (origin != NULL
18309           && !DECL_ABSTRACT (decl_or_origin)
18310           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18311                                        decl_function_context
18312                                                         (decl_or_origin))))
18313     {
18314       tree type = TREE_TYPE (decl_or_origin);
18315
18316       if (decl_by_reference_p (decl_or_origin))
18317         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18318       else
18319         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18320                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
18321     }
18322
18323   if (origin == NULL && !specialization_p)
18324     {
18325       if (TREE_PUBLIC (decl))
18326         add_AT_flag (var_die, DW_AT_external, 1);
18327
18328       if (DECL_ARTIFICIAL (decl))
18329         add_AT_flag (var_die, DW_AT_artificial, 1);
18330
18331       add_accessibility_attribute (var_die, decl);
18332     }
18333
18334   if (declaration)
18335     add_AT_flag (var_die, DW_AT_declaration, 1);
18336
18337   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18338     equate_decl_number_to_die (decl, var_die);
18339
18340   if (! declaration
18341       && (! DECL_ABSTRACT (decl_or_origin)
18342           /* Local static vars are shared between all clones/inlines,
18343              so emit DW_AT_location on the abstract DIE if DECL_RTL is
18344              already set.  */
18345           || (TREE_CODE (decl_or_origin) == VAR_DECL
18346               && TREE_STATIC (decl_or_origin)
18347               && DECL_RTL_SET_P (decl_or_origin)))
18348       /* When abstract origin already has DW_AT_location attribute, no need
18349          to add it again.  */
18350       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18351     {
18352       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18353           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18354         defer_location (decl_or_origin, var_die);
18355       else
18356         add_location_or_const_value_attribute (var_die, decl_or_origin,
18357                                                decl == NULL, DW_AT_location);
18358       add_pubname (decl_or_origin, var_die);
18359     }
18360   else
18361     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18362 }
18363
18364 /* Generate a DIE to represent a named constant.  */
18365
18366 static void
18367 gen_const_die (tree decl, dw_die_ref context_die)
18368 {
18369   dw_die_ref const_die;
18370   tree type = TREE_TYPE (decl);
18371
18372   const_die = new_die (DW_TAG_constant, context_die, decl);
18373   add_name_and_src_coords_attributes (const_die, decl);
18374   add_type_attribute (const_die, type, 1, 0, context_die);
18375   if (TREE_PUBLIC (decl))
18376     add_AT_flag (const_die, DW_AT_external, 1);
18377   if (DECL_ARTIFICIAL (decl))
18378     add_AT_flag (const_die, DW_AT_artificial, 1);
18379   tree_add_const_value_attribute_for_decl (const_die, decl);
18380 }
18381
18382 /* Generate a DIE to represent a label identifier.  */
18383
18384 static void
18385 gen_label_die (tree decl, dw_die_ref context_die)
18386 {
18387   tree origin = decl_ultimate_origin (decl);
18388   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18389   rtx insn;
18390   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18391
18392   if (origin != NULL)
18393     add_abstract_origin_attribute (lbl_die, origin);
18394   else
18395     add_name_and_src_coords_attributes (lbl_die, decl);
18396
18397   if (DECL_ABSTRACT (decl))
18398     equate_decl_number_to_die (decl, lbl_die);
18399   else
18400     {
18401       insn = DECL_RTL_IF_SET (decl);
18402
18403       /* Deleted labels are programmer specified labels which have been
18404          eliminated because of various optimizations.  We still emit them
18405          here so that it is possible to put breakpoints on them.  */
18406       if (insn
18407           && (LABEL_P (insn)
18408               || ((NOTE_P (insn)
18409                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18410         {
18411           /* When optimization is enabled (via -O) some parts of the compiler
18412              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18413              represent source-level labels which were explicitly declared by
18414              the user.  This really shouldn't be happening though, so catch
18415              it if it ever does happen.  */
18416           gcc_assert (!INSN_DELETED_P (insn));
18417
18418           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18419           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18420         }
18421     }
18422 }
18423
18424 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18425    attributes to the DIE for a block STMT, to describe where the inlined
18426    function was called from.  This is similar to add_src_coords_attributes.  */
18427
18428 static inline void
18429 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18430 {
18431   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18432
18433   if (dwarf_version >= 3 || !dwarf_strict)
18434     {
18435       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18436       add_AT_unsigned (die, DW_AT_call_line, s.line);
18437     }
18438 }
18439
18440
18441 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18442    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18443
18444 static inline void
18445 add_high_low_attributes (tree stmt, dw_die_ref die)
18446 {
18447   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18448
18449   if (BLOCK_FRAGMENT_CHAIN (stmt)
18450       && (dwarf_version >= 3 || !dwarf_strict))
18451     {
18452       tree chain;
18453
18454       if (inlined_function_outer_scope_p (stmt))
18455         {
18456           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18457                                        BLOCK_NUMBER (stmt));
18458           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18459         }
18460
18461       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18462
18463       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18464       do
18465         {
18466           add_ranges (chain);
18467           chain = BLOCK_FRAGMENT_CHAIN (chain);
18468         }
18469       while (chain);
18470       add_ranges (NULL);
18471     }
18472   else
18473     {
18474       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18475                                    BLOCK_NUMBER (stmt));
18476       add_AT_lbl_id (die, DW_AT_low_pc, label);
18477       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18478                                    BLOCK_NUMBER (stmt));
18479       add_AT_lbl_id (die, DW_AT_high_pc, label);
18480     }
18481 }
18482
18483 /* Generate a DIE for a lexical block.  */
18484
18485 static void
18486 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18487 {
18488   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18489
18490   if (call_arg_locations)
18491     {
18492       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18493         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18494                                BLOCK_NUMBER (stmt) + 1);
18495       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18496     }
18497
18498   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18499     add_high_low_attributes (stmt, stmt_die);
18500
18501   decls_for_scope (stmt, stmt_die, depth);
18502 }
18503
18504 /* Generate a DIE for an inlined subprogram.  */
18505
18506 static void
18507 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18508 {
18509   tree decl;
18510
18511   /* The instance of function that is effectively being inlined shall not
18512      be abstract.  */
18513   gcc_assert (! BLOCK_ABSTRACT (stmt));
18514
18515   decl = block_ultimate_origin (stmt);
18516
18517   /* Emit info for the abstract instance first, if we haven't yet.  We
18518      must emit this even if the block is abstract, otherwise when we
18519      emit the block below (or elsewhere), we may end up trying to emit
18520      a die whose origin die hasn't been emitted, and crashing.  */
18521   dwarf2out_abstract_function (decl);
18522
18523   if (! BLOCK_ABSTRACT (stmt))
18524     {
18525       dw_die_ref subr_die
18526         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18527
18528       if (call_arg_locations)
18529         {
18530           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18531             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18532                                    BLOCK_NUMBER (stmt) + 1);
18533           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18534         }
18535       add_abstract_origin_attribute (subr_die, decl);
18536       if (TREE_ASM_WRITTEN (stmt))
18537         add_high_low_attributes (stmt, subr_die);
18538       add_call_src_coords_attributes (stmt, subr_die);
18539
18540       decls_for_scope (stmt, subr_die, depth);
18541       current_function_has_inlines = 1;
18542     }
18543 }
18544
18545 /* Generate a DIE for a field in a record, or structure.  */
18546
18547 static void
18548 gen_field_die (tree decl, dw_die_ref context_die)
18549 {
18550   dw_die_ref decl_die;
18551
18552   if (TREE_TYPE (decl) == error_mark_node)
18553     return;
18554
18555   decl_die = new_die (DW_TAG_member, context_die, decl);
18556   add_name_and_src_coords_attributes (decl_die, decl);
18557   add_type_attribute (decl_die, member_declared_type (decl),
18558                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18559                       context_die);
18560
18561   if (DECL_BIT_FIELD_TYPE (decl))
18562     {
18563       add_byte_size_attribute (decl_die, decl);
18564       add_bit_size_attribute (decl_die, decl);
18565       add_bit_offset_attribute (decl_die, decl);
18566     }
18567
18568   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18569     add_data_member_location_attribute (decl_die, decl);
18570
18571   if (DECL_ARTIFICIAL (decl))
18572     add_AT_flag (decl_die, DW_AT_artificial, 1);
18573
18574   add_accessibility_attribute (decl_die, decl);
18575
18576   /* Equate decl number to die, so that we can look up this decl later on.  */
18577   equate_decl_number_to_die (decl, decl_die);
18578 }
18579
18580 #if 0
18581 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18582    Use modified_type_die instead.
18583    We keep this code here just in case these types of DIEs may be needed to
18584    represent certain things in other languages (e.g. Pascal) someday.  */
18585
18586 static void
18587 gen_pointer_type_die (tree type, dw_die_ref context_die)
18588 {
18589   dw_die_ref ptr_die
18590     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18591
18592   equate_type_number_to_die (type, ptr_die);
18593   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18594   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18595 }
18596
18597 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18598    Use modified_type_die instead.
18599    We keep this code here just in case these types of DIEs may be needed to
18600    represent certain things in other languages (e.g. Pascal) someday.  */
18601
18602 static void
18603 gen_reference_type_die (tree type, dw_die_ref context_die)
18604 {
18605   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18606
18607   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18608     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18609   else
18610     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18611
18612   equate_type_number_to_die (type, ref_die);
18613   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18614   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18615 }
18616 #endif
18617
18618 /* Generate a DIE for a pointer to a member type.  */
18619
18620 static void
18621 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18622 {
18623   dw_die_ref ptr_die
18624     = new_die (DW_TAG_ptr_to_member_type,
18625                scope_die_for (type, context_die), type);
18626
18627   equate_type_number_to_die (type, ptr_die);
18628   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18629                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18630   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18631 }
18632
18633 /* Generate the DIE for the compilation unit.  */
18634
18635 static dw_die_ref
18636 gen_compile_unit_die (const char *filename)
18637 {
18638   dw_die_ref die;
18639   char producer[250];
18640   const char *language_string = lang_hooks.name;
18641   int language;
18642
18643   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18644
18645   if (filename)
18646     {
18647       add_name_attribute (die, filename);
18648       /* Don't add cwd for <built-in>.  */
18649       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18650         add_comp_dir_attribute (die);
18651     }
18652
18653   sprintf (producer, "%s %s", language_string, version_string);
18654
18655 #ifdef MIPS_DEBUGGING_INFO
18656   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
18657      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
18658      not appear in the producer string, the debugger reaches the conclusion
18659      that the object file is stripped and has no debugging information.
18660      To get the MIPS/SGI debugger to believe that there is debugging
18661      information in the object file, we add a -g to the producer string.  */
18662   if (debug_info_level > DINFO_LEVEL_TERSE)
18663     strcat (producer, " -g");
18664 #endif
18665
18666   add_AT_string (die, DW_AT_producer, producer);
18667
18668   /* If our producer is LTO try to figure out a common language to use
18669      from the global list of translation units.  */
18670   if (strcmp (language_string, "GNU GIMPLE") == 0)
18671     {
18672       unsigned i;
18673       tree t;
18674       const char *common_lang = NULL;
18675
18676       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18677         {
18678           if (!TRANSLATION_UNIT_LANGUAGE (t))
18679             continue;
18680           if (!common_lang)
18681             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18682           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18683             ;
18684           else if (strncmp (common_lang, "GNU C", 5) == 0
18685                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18686             /* Mixing C and C++ is ok, use C++ in that case.  */
18687             common_lang = "GNU C++";
18688           else
18689             {
18690               /* Fall back to C.  */
18691               common_lang = NULL;
18692               break;
18693             }
18694         }
18695
18696       if (common_lang)
18697         language_string = common_lang;
18698     }
18699
18700   language = DW_LANG_C89;
18701   if (strcmp (language_string, "GNU C++") == 0)
18702     language = DW_LANG_C_plus_plus;
18703   else if (strcmp (language_string, "GNU F77") == 0)
18704     language = DW_LANG_Fortran77;
18705   else if (strcmp (language_string, "GNU Pascal") == 0)
18706     language = DW_LANG_Pascal83;
18707   else if (dwarf_version >= 3 || !dwarf_strict)
18708     {
18709       if (strcmp (language_string, "GNU Ada") == 0)
18710         language = DW_LANG_Ada95;
18711       else if (strcmp (language_string, "GNU Fortran") == 0)
18712         language = DW_LANG_Fortran95;
18713       else if (strcmp (language_string, "GNU Java") == 0)
18714         language = DW_LANG_Java;
18715       else if (strcmp (language_string, "GNU Objective-C") == 0)
18716         language = DW_LANG_ObjC;
18717       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18718         language = DW_LANG_ObjC_plus_plus;
18719     }
18720
18721   add_AT_unsigned (die, DW_AT_language, language);
18722
18723   switch (language)
18724     {
18725     case DW_LANG_Fortran77:
18726     case DW_LANG_Fortran90:
18727     case DW_LANG_Fortran95:
18728       /* Fortran has case insensitive identifiers and the front-end
18729          lowercases everything.  */
18730       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18731       break;
18732     default:
18733       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18734       break;
18735     }
18736   return die;
18737 }
18738
18739 /* Generate the DIE for a base class.  */
18740
18741 static void
18742 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18743 {
18744   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18745
18746   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18747   add_data_member_location_attribute (die, binfo);
18748
18749   if (BINFO_VIRTUAL_P (binfo))
18750     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18751
18752   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18753      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18754      the default has always been DW_ACCESS_private.  */
18755   if (access == access_public_node)
18756     {
18757       if (dwarf_version == 2
18758           || context_die->die_tag == DW_TAG_class_type)
18759       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18760     }
18761   else if (access == access_protected_node)
18762     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18763   else if (dwarf_version > 2
18764            && context_die->die_tag != DW_TAG_class_type)
18765     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18766 }
18767
18768 /* Generate a DIE for a class member.  */
18769
18770 static void
18771 gen_member_die (tree type, dw_die_ref context_die)
18772 {
18773   tree member;
18774   tree binfo = TYPE_BINFO (type);
18775   dw_die_ref child;
18776
18777   /* If this is not an incomplete type, output descriptions of each of its
18778      members. Note that as we output the DIEs necessary to represent the
18779      members of this record or union type, we will also be trying to output
18780      DIEs to represent the *types* of those members. However the `type'
18781      function (above) will specifically avoid generating type DIEs for member
18782      types *within* the list of member DIEs for this (containing) type except
18783      for those types (of members) which are explicitly marked as also being
18784      members of this (containing) type themselves.  The g++ front- end can
18785      force any given type to be treated as a member of some other (containing)
18786      type by setting the TYPE_CONTEXT of the given (member) type to point to
18787      the TREE node representing the appropriate (containing) type.  */
18788
18789   /* First output info about the base classes.  */
18790   if (binfo)
18791     {
18792       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18793       int i;
18794       tree base;
18795
18796       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18797         gen_inheritance_die (base,
18798                              (accesses ? VEC_index (tree, accesses, i)
18799                               : access_public_node), context_die);
18800     }
18801
18802   /* Now output info about the data members and type members.  */
18803   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18804     {
18805       /* If we thought we were generating minimal debug info for TYPE
18806          and then changed our minds, some of the member declarations
18807          may have already been defined.  Don't define them again, but
18808          do put them in the right order.  */
18809
18810       child = lookup_decl_die (member);
18811       if (child)
18812         splice_child_die (context_die, child);
18813       else
18814         gen_decl_die (member, NULL, context_die);
18815     }
18816
18817   /* Now output info about the function members (if any).  */
18818   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18819     {
18820       /* Don't include clones in the member list.  */
18821       if (DECL_ABSTRACT_ORIGIN (member))
18822         continue;
18823
18824       child = lookup_decl_die (member);
18825       if (child)
18826         splice_child_die (context_die, child);
18827       else
18828         gen_decl_die (member, NULL, context_die);
18829     }
18830 }
18831
18832 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18833    is set, we pretend that the type was never defined, so we only get the
18834    member DIEs needed by later specification DIEs.  */
18835
18836 static void
18837 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18838                                 enum debug_info_usage usage)
18839 {
18840   dw_die_ref type_die = lookup_type_die (type);
18841   dw_die_ref scope_die = 0;
18842   int nested = 0;
18843   int complete = (TYPE_SIZE (type)
18844                   && (! TYPE_STUB_DECL (type)
18845                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18846   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18847   complete = complete && should_emit_struct_debug (type, usage);
18848
18849   if (type_die && ! complete)
18850     return;
18851
18852   if (TYPE_CONTEXT (type) != NULL_TREE
18853       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18854           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18855     nested = 1;
18856
18857   scope_die = scope_die_for (type, context_die);
18858
18859   if (! type_die || (nested && is_cu_die (scope_die)))
18860     /* First occurrence of type or toplevel definition of nested class.  */
18861     {
18862       dw_die_ref old_die = type_die;
18863
18864       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18865                           ? record_type_tag (type) : DW_TAG_union_type,
18866                           scope_die, type);
18867       equate_type_number_to_die (type, type_die);
18868       if (old_die)
18869         add_AT_specification (type_die, old_die);
18870       else
18871         {
18872           add_name_attribute (type_die, type_tag (type));
18873           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18874           if (TYPE_ARTIFICIAL (type))
18875             add_AT_flag (type_die, DW_AT_artificial, 1);
18876         }
18877     }
18878   else
18879     remove_AT (type_die, DW_AT_declaration);
18880
18881   /* Generate child dies for template paramaters.  */
18882   if (debug_info_level > DINFO_LEVEL_TERSE
18883       && COMPLETE_TYPE_P (type))
18884     schedule_generic_params_dies_gen (type);
18885
18886   /* If this type has been completed, then give it a byte_size attribute and
18887      then give a list of members.  */
18888   if (complete && !ns_decl)
18889     {
18890       /* Prevent infinite recursion in cases where the type of some member of
18891          this type is expressed in terms of this type itself.  */
18892       TREE_ASM_WRITTEN (type) = 1;
18893       add_byte_size_attribute (type_die, type);
18894       if (TYPE_STUB_DECL (type) != NULL_TREE)
18895         {
18896           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18897           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18898         }
18899
18900       /* If the first reference to this type was as the return type of an
18901          inline function, then it may not have a parent.  Fix this now.  */
18902       if (type_die->die_parent == NULL)
18903         add_child_die (scope_die, type_die);
18904
18905       push_decl_scope (type);
18906       gen_member_die (type, type_die);
18907       pop_decl_scope ();
18908
18909       /* GNU extension: Record what type our vtable lives in.  */
18910       if (TYPE_VFIELD (type))
18911         {
18912           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18913
18914           gen_type_die (vtype, context_die);
18915           add_AT_die_ref (type_die, DW_AT_containing_type,
18916                           lookup_type_die (vtype));
18917         }
18918     }
18919   else
18920     {
18921       add_AT_flag (type_die, DW_AT_declaration, 1);
18922
18923       /* We don't need to do this for function-local types.  */
18924       if (TYPE_STUB_DECL (type)
18925           && ! decl_function_context (TYPE_STUB_DECL (type)))
18926         VEC_safe_push (tree, gc, incomplete_types, type);
18927     }
18928
18929   if (get_AT (type_die, DW_AT_name))
18930     add_pubtype (type, type_die);
18931 }
18932
18933 /* Generate a DIE for a subroutine _type_.  */
18934
18935 static void
18936 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18937 {
18938   tree return_type = TREE_TYPE (type);
18939   dw_die_ref subr_die
18940     = new_die (DW_TAG_subroutine_type,
18941                scope_die_for (type, context_die), type);
18942
18943   equate_type_number_to_die (type, subr_die);
18944   add_prototyped_attribute (subr_die, type);
18945   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18946   gen_formal_types_die (type, subr_die);
18947
18948   if (get_AT (subr_die, DW_AT_name))
18949     add_pubtype (type, subr_die);
18950 }
18951
18952 /* Generate a DIE for a type definition.  */
18953
18954 static void
18955 gen_typedef_die (tree decl, dw_die_ref context_die)
18956 {
18957   dw_die_ref type_die;
18958   tree origin;
18959
18960   if (TREE_ASM_WRITTEN (decl))
18961     return;
18962
18963   TREE_ASM_WRITTEN (decl) = 1;
18964   type_die = new_die (DW_TAG_typedef, context_die, decl);
18965   origin = decl_ultimate_origin (decl);
18966   if (origin != NULL)
18967     add_abstract_origin_attribute (type_die, origin);
18968   else
18969     {
18970       tree type;
18971
18972       add_name_and_src_coords_attributes (type_die, decl);
18973       if (DECL_ORIGINAL_TYPE (decl))
18974         {
18975           type = DECL_ORIGINAL_TYPE (decl);
18976
18977           gcc_assert (type != TREE_TYPE (decl));
18978           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18979         }
18980       else
18981         {
18982           type = TREE_TYPE (decl);
18983
18984           if (is_naming_typedef_decl (TYPE_NAME (type)))
18985             {
18986               /* Here, we are in the case of decl being a typedef naming
18987                  an anonymous type, e.g:
18988                      typedef struct {...} foo;
18989                  In that case TREE_TYPE (decl) is not a typedef variant
18990                  type and TYPE_NAME of the anonymous type is set to the
18991                  TYPE_DECL of the typedef. This construct is emitted by
18992                  the C++ FE.
18993
18994                  TYPE is the anonymous struct named by the typedef
18995                  DECL. As we need the DW_AT_type attribute of the
18996                  DW_TAG_typedef to point to the DIE of TYPE, let's
18997                  generate that DIE right away. add_type_attribute
18998                  called below will then pick (via lookup_type_die) that
18999                  anonymous struct DIE.  */
19000               if (!TREE_ASM_WRITTEN (type))
19001                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19002
19003               /* This is a GNU Extension.  We are adding a
19004                  DW_AT_linkage_name attribute to the DIE of the
19005                  anonymous struct TYPE.  The value of that attribute
19006                  is the name of the typedef decl naming the anonymous
19007                  struct.  This greatly eases the work of consumers of
19008                  this debug info.  */
19009               add_linkage_attr (lookup_type_die (type), decl);
19010             }
19011         }
19012
19013       add_type_attribute (type_die, type, TREE_READONLY (decl),
19014                           TREE_THIS_VOLATILE (decl), context_die);
19015
19016       if (is_naming_typedef_decl (decl))
19017         /* We want that all subsequent calls to lookup_type_die with
19018            TYPE in argument yield the DW_TAG_typedef we have just
19019            created.  */
19020         equate_type_number_to_die (type, type_die);
19021
19022       add_accessibility_attribute (type_die, decl);
19023     }
19024
19025   if (DECL_ABSTRACT (decl))
19026     equate_decl_number_to_die (decl, type_die);
19027
19028   if (get_AT (type_die, DW_AT_name))
19029     add_pubtype (decl, type_die);
19030 }
19031
19032 /* Generate a DIE for a struct, class, enum or union type.  */
19033
19034 static void
19035 gen_tagged_type_die (tree type,
19036                      dw_die_ref context_die,
19037                      enum debug_info_usage usage)
19038 {
19039   int need_pop;
19040
19041   if (type == NULL_TREE
19042       || !is_tagged_type (type))
19043     return;
19044
19045   /* If this is a nested type whose containing class hasn't been written
19046      out yet, writing it out will cover this one, too.  This does not apply
19047      to instantiations of member class templates; they need to be added to
19048      the containing class as they are generated.  FIXME: This hurts the
19049      idea of combining type decls from multiple TUs, since we can't predict
19050      what set of template instantiations we'll get.  */
19051   if (TYPE_CONTEXT (type)
19052       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19053       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19054     {
19055       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19056
19057       if (TREE_ASM_WRITTEN (type))
19058         return;
19059
19060       /* If that failed, attach ourselves to the stub.  */
19061       push_decl_scope (TYPE_CONTEXT (type));
19062       context_die = lookup_type_die (TYPE_CONTEXT (type));
19063       need_pop = 1;
19064     }
19065   else if (TYPE_CONTEXT (type) != NULL_TREE
19066            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19067     {
19068       /* If this type is local to a function that hasn't been written
19069          out yet, use a NULL context for now; it will be fixed up in
19070          decls_for_scope.  */
19071       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19072       /* A declaration DIE doesn't count; nested types need to go in the
19073          specification.  */
19074       if (context_die && is_declaration_die (context_die))
19075         context_die = NULL;
19076       need_pop = 0;
19077     }
19078   else
19079     {
19080       context_die = declare_in_namespace (type, context_die);
19081       need_pop = 0;
19082     }
19083
19084   if (TREE_CODE (type) == ENUMERAL_TYPE)
19085     {
19086       /* This might have been written out by the call to
19087          declare_in_namespace.  */
19088       if (!TREE_ASM_WRITTEN (type))
19089         gen_enumeration_type_die (type, context_die);
19090     }
19091   else
19092     gen_struct_or_union_type_die (type, context_die, usage);
19093
19094   if (need_pop)
19095     pop_decl_scope ();
19096
19097   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19098      it up if it is ever completed.  gen_*_type_die will set it for us
19099      when appropriate.  */
19100 }
19101
19102 /* Generate a type description DIE.  */
19103
19104 static void
19105 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19106                          enum debug_info_usage usage)
19107 {
19108   struct array_descr_info info;
19109
19110   if (type == NULL_TREE || type == error_mark_node)
19111     return;
19112
19113   if (TYPE_NAME (type) != NULL_TREE
19114       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19115       && is_redundant_typedef (TYPE_NAME (type))
19116       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19117     /* The DECL of this type is a typedef we don't want to emit debug
19118        info for but we want debug info for its underlying typedef.
19119        This can happen for e.g, the injected-class-name of a C++
19120        type.  */
19121     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19122
19123   /* If TYPE is a typedef type variant, let's generate debug info
19124      for the parent typedef which TYPE is a type of.  */
19125   if (typedef_variant_p (type))
19126     {
19127       if (TREE_ASM_WRITTEN (type))
19128         return;
19129
19130       /* Prevent broken recursion; we can't hand off to the same type.  */
19131       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19132
19133       /* Use the DIE of the containing namespace as the parent DIE of
19134          the type description DIE we want to generate.  */
19135       if (DECL_CONTEXT (TYPE_NAME (type))
19136           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19137         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19138
19139       TREE_ASM_WRITTEN (type) = 1;
19140
19141       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19142       return;
19143     }
19144
19145   /* If type is an anonymous tagged type named by a typedef, let's
19146      generate debug info for the typedef.  */
19147   if (is_naming_typedef_decl (TYPE_NAME (type)))
19148     {
19149       /* Use the DIE of the containing namespace as the parent DIE of
19150          the type description DIE we want to generate.  */
19151       if (DECL_CONTEXT (TYPE_NAME (type))
19152           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19153         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19154       
19155       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19156       return;
19157     }
19158
19159   /* If this is an array type with hidden descriptor, handle it first.  */
19160   if (!TREE_ASM_WRITTEN (type)
19161       && lang_hooks.types.get_array_descr_info
19162       && lang_hooks.types.get_array_descr_info (type, &info)
19163       && (dwarf_version >= 3 || !dwarf_strict))
19164     {
19165       gen_descr_array_type_die (type, &info, context_die);
19166       TREE_ASM_WRITTEN (type) = 1;
19167       return;
19168     }
19169
19170   /* We are going to output a DIE to represent the unqualified version
19171      of this type (i.e. without any const or volatile qualifiers) so
19172      get the main variant (i.e. the unqualified version) of this type
19173      now.  (Vectors are special because the debugging info is in the
19174      cloned type itself).  */
19175   if (TREE_CODE (type) != VECTOR_TYPE)
19176     type = type_main_variant (type);
19177
19178   if (TREE_ASM_WRITTEN (type))
19179     return;
19180
19181   switch (TREE_CODE (type))
19182     {
19183     case ERROR_MARK:
19184       break;
19185
19186     case POINTER_TYPE:
19187     case REFERENCE_TYPE:
19188       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19189          ensures that the gen_type_die recursion will terminate even if the
19190          type is recursive.  Recursive types are possible in Ada.  */
19191       /* ??? We could perhaps do this for all types before the switch
19192          statement.  */
19193       TREE_ASM_WRITTEN (type) = 1;
19194
19195       /* For these types, all that is required is that we output a DIE (or a
19196          set of DIEs) to represent the "basis" type.  */
19197       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19198                                 DINFO_USAGE_IND_USE);
19199       break;
19200
19201     case OFFSET_TYPE:
19202       /* This code is used for C++ pointer-to-data-member types.
19203          Output a description of the relevant class type.  */
19204       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19205                                         DINFO_USAGE_IND_USE);
19206
19207       /* Output a description of the type of the object pointed to.  */
19208       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19209                                         DINFO_USAGE_IND_USE);
19210
19211       /* Now output a DIE to represent this pointer-to-data-member type
19212          itself.  */
19213       gen_ptr_to_mbr_type_die (type, context_die);
19214       break;
19215
19216     case FUNCTION_TYPE:
19217       /* Force out return type (in case it wasn't forced out already).  */
19218       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19219                                         DINFO_USAGE_DIR_USE);
19220       gen_subroutine_type_die (type, context_die);
19221       break;
19222
19223     case METHOD_TYPE:
19224       /* Force out return type (in case it wasn't forced out already).  */
19225       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19226                                         DINFO_USAGE_DIR_USE);
19227       gen_subroutine_type_die (type, context_die);
19228       break;
19229
19230     case ARRAY_TYPE:
19231       gen_array_type_die (type, context_die);
19232       break;
19233
19234     case VECTOR_TYPE:
19235       gen_array_type_die (type, context_die);
19236       break;
19237
19238     case ENUMERAL_TYPE:
19239     case RECORD_TYPE:
19240     case UNION_TYPE:
19241     case QUAL_UNION_TYPE:
19242       gen_tagged_type_die (type, context_die, usage);
19243       return;
19244
19245     case VOID_TYPE:
19246     case INTEGER_TYPE:
19247     case REAL_TYPE:
19248     case FIXED_POINT_TYPE:
19249     case COMPLEX_TYPE:
19250     case BOOLEAN_TYPE:
19251       /* No DIEs needed for fundamental types.  */
19252       break;
19253
19254     case NULLPTR_TYPE:
19255     case LANG_TYPE:
19256       /* Just use DW_TAG_unspecified_type.  */
19257       {
19258         dw_die_ref type_die = lookup_type_die (type);
19259         if (type_die == NULL)
19260           {
19261             tree name = TYPE_NAME (type);
19262             if (TREE_CODE (name) == TYPE_DECL)
19263               name = DECL_NAME (name);
19264             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19265             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19266             equate_type_number_to_die (type, type_die);
19267           }
19268       }
19269       break;
19270
19271     default:
19272       gcc_unreachable ();
19273     }
19274
19275   TREE_ASM_WRITTEN (type) = 1;
19276 }
19277
19278 static void
19279 gen_type_die (tree type, dw_die_ref context_die)
19280 {
19281   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19282 }
19283
19284 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19285    things which are local to the given block.  */
19286
19287 static void
19288 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19289 {
19290   int must_output_die = 0;
19291   bool inlined_func;
19292
19293   /* Ignore blocks that are NULL.  */
19294   if (stmt == NULL_TREE)
19295     return;
19296
19297   inlined_func = inlined_function_outer_scope_p (stmt);
19298
19299   /* If the block is one fragment of a non-contiguous block, do not
19300      process the variables, since they will have been done by the
19301      origin block.  Do process subblocks.  */
19302   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19303     {
19304       tree sub;
19305
19306       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19307         gen_block_die (sub, context_die, depth + 1);
19308
19309       return;
19310     }
19311
19312   /* Determine if we need to output any Dwarf DIEs at all to represent this
19313      block.  */
19314   if (inlined_func)
19315     /* The outer scopes for inlinings *must* always be represented.  We
19316        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19317     must_output_die = 1;
19318   else
19319     {
19320       /* Determine if this block directly contains any "significant"
19321          local declarations which we will need to output DIEs for.  */
19322       if (debug_info_level > DINFO_LEVEL_TERSE)
19323         /* We are not in terse mode so *any* local declaration counts
19324            as being a "significant" one.  */
19325         must_output_die = ((BLOCK_VARS (stmt) != NULL
19326                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19327                            && (TREE_USED (stmt)
19328                                || TREE_ASM_WRITTEN (stmt)
19329                                || BLOCK_ABSTRACT (stmt)));
19330       else if ((TREE_USED (stmt)
19331                 || TREE_ASM_WRITTEN (stmt)
19332                 || BLOCK_ABSTRACT (stmt))
19333                && !dwarf2out_ignore_block (stmt))
19334         must_output_die = 1;
19335     }
19336
19337   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19338      DIE for any block which contains no significant local declarations at
19339      all.  Rather, in such cases we just call `decls_for_scope' so that any
19340      needed Dwarf info for any sub-blocks will get properly generated. Note
19341      that in terse mode, our definition of what constitutes a "significant"
19342      local declaration gets restricted to include only inlined function
19343      instances and local (nested) function definitions.  */
19344   if (must_output_die)
19345     {
19346       if (inlined_func)
19347         {
19348           /* If STMT block is abstract, that means we have been called
19349              indirectly from dwarf2out_abstract_function.
19350              That function rightfully marks the descendent blocks (of
19351              the abstract function it is dealing with) as being abstract,
19352              precisely to prevent us from emitting any
19353              DW_TAG_inlined_subroutine DIE as a descendent
19354              of an abstract function instance. So in that case, we should
19355              not call gen_inlined_subroutine_die.
19356
19357              Later though, when cgraph asks dwarf2out to emit info
19358              for the concrete instance of the function decl into which
19359              the concrete instance of STMT got inlined, the later will lead
19360              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19361           if (! BLOCK_ABSTRACT (stmt))
19362             gen_inlined_subroutine_die (stmt, context_die, depth);
19363         }
19364       else
19365         gen_lexical_block_die (stmt, context_die, depth);
19366     }
19367   else
19368     decls_for_scope (stmt, context_die, depth);
19369 }
19370
19371 /* Process variable DECL (or variable with origin ORIGIN) within
19372    block STMT and add it to CONTEXT_DIE.  */
19373 static void
19374 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19375 {
19376   dw_die_ref die;
19377   tree decl_or_origin = decl ? decl : origin;
19378
19379   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19380     die = lookup_decl_die (decl_or_origin);
19381   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19382            && TYPE_DECL_IS_STUB (decl_or_origin))
19383     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19384   else
19385     die = NULL;
19386
19387   if (die != NULL && die->die_parent == NULL)
19388     add_child_die (context_die, die);
19389   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19390     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19391                                          stmt, context_die);
19392   else
19393     gen_decl_die (decl, origin, context_die);
19394 }
19395
19396 /* Generate all of the decls declared within a given scope and (recursively)
19397    all of its sub-blocks.  */
19398
19399 static void
19400 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19401 {
19402   tree decl;
19403   unsigned int i;
19404   tree subblocks;
19405
19406   /* Ignore NULL blocks.  */
19407   if (stmt == NULL_TREE)
19408     return;
19409
19410   /* Output the DIEs to represent all of the data objects and typedefs
19411      declared directly within this block but not within any nested
19412      sub-blocks.  Also, nested function and tag DIEs have been
19413      generated with a parent of NULL; fix that up now.  */
19414   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19415     process_scope_var (stmt, decl, NULL_TREE, context_die);
19416   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19417     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19418                        context_die);
19419
19420   /* If we're at -g1, we're not interested in subblocks.  */
19421   if (debug_info_level <= DINFO_LEVEL_TERSE)
19422     return;
19423
19424   /* Output the DIEs to represent all sub-blocks (and the items declared
19425      therein) of this block.  */
19426   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19427        subblocks != NULL;
19428        subblocks = BLOCK_CHAIN (subblocks))
19429     gen_block_die (subblocks, context_die, depth + 1);
19430 }
19431
19432 /* Is this a typedef we can avoid emitting?  */
19433
19434 static inline int
19435 is_redundant_typedef (const_tree decl)
19436 {
19437   if (TYPE_DECL_IS_STUB (decl))
19438     return 1;
19439
19440   if (DECL_ARTIFICIAL (decl)
19441       && DECL_CONTEXT (decl)
19442       && is_tagged_type (DECL_CONTEXT (decl))
19443       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19444       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19445     /* Also ignore the artificial member typedef for the class name.  */
19446     return 1;
19447
19448   return 0;
19449 }
19450
19451 /* Return TRUE if TYPE is a typedef that names a type for linkage
19452    purposes. This kind of typedefs is produced by the C++ FE for
19453    constructs like:
19454
19455    typedef struct {...} foo;
19456
19457    In that case, there is no typedef variant type produced for foo.
19458    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19459    struct type.  */
19460
19461 static bool
19462 is_naming_typedef_decl (const_tree decl)
19463 {
19464   if (decl == NULL_TREE
19465       || TREE_CODE (decl) != TYPE_DECL
19466       || !is_tagged_type (TREE_TYPE (decl))
19467       || DECL_IS_BUILTIN (decl)
19468       || is_redundant_typedef (decl)
19469       /* It looks like Ada produces TYPE_DECLs that are very similar
19470          to C++ naming typedefs but that have different
19471          semantics. Let's be specific to c++ for now.  */
19472       || !is_cxx ())
19473     return FALSE;
19474
19475   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19476           && TYPE_NAME (TREE_TYPE (decl)) == decl
19477           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19478               != TYPE_NAME (TREE_TYPE (decl))));
19479 }
19480
19481 /* Returns the DIE for a context.  */
19482
19483 static inline dw_die_ref
19484 get_context_die (tree context)
19485 {
19486   if (context)
19487     {
19488       /* Find die that represents this context.  */
19489       if (TYPE_P (context))
19490         {
19491           context = TYPE_MAIN_VARIANT (context);
19492           return strip_naming_typedef (context, force_type_die (context));
19493         }
19494       else
19495         return force_decl_die (context);
19496     }
19497   return comp_unit_die ();
19498 }
19499
19500 /* Returns the DIE for decl.  A DIE will always be returned.  */
19501
19502 static dw_die_ref
19503 force_decl_die (tree decl)
19504 {
19505   dw_die_ref decl_die;
19506   unsigned saved_external_flag;
19507   tree save_fn = NULL_TREE;
19508   decl_die = lookup_decl_die (decl);
19509   if (!decl_die)
19510     {
19511       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19512
19513       decl_die = lookup_decl_die (decl);
19514       if (decl_die)
19515         return decl_die;
19516
19517       switch (TREE_CODE (decl))
19518         {
19519         case FUNCTION_DECL:
19520           /* Clear current_function_decl, so that gen_subprogram_die thinks
19521              that this is a declaration. At this point, we just want to force
19522              declaration die.  */
19523           save_fn = current_function_decl;
19524           current_function_decl = NULL_TREE;
19525           gen_subprogram_die (decl, context_die);
19526           current_function_decl = save_fn;
19527           break;
19528
19529         case VAR_DECL:
19530           /* Set external flag to force declaration die. Restore it after
19531            gen_decl_die() call.  */
19532           saved_external_flag = DECL_EXTERNAL (decl);
19533           DECL_EXTERNAL (decl) = 1;
19534           gen_decl_die (decl, NULL, context_die);
19535           DECL_EXTERNAL (decl) = saved_external_flag;
19536           break;
19537
19538         case NAMESPACE_DECL:
19539           if (dwarf_version >= 3 || !dwarf_strict)
19540             dwarf2out_decl (decl);
19541           else
19542             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19543             decl_die = comp_unit_die ();
19544           break;
19545
19546         case TRANSLATION_UNIT_DECL:
19547           decl_die = comp_unit_die ();
19548           break;
19549
19550         default:
19551           gcc_unreachable ();
19552         }
19553
19554       /* We should be able to find the DIE now.  */
19555       if (!decl_die)
19556         decl_die = lookup_decl_die (decl);
19557       gcc_assert (decl_die);
19558     }
19559
19560   return decl_die;
19561 }
19562
19563 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19564    always returned.  */
19565
19566 static dw_die_ref
19567 force_type_die (tree type)
19568 {
19569   dw_die_ref type_die;
19570
19571   type_die = lookup_type_die (type);
19572   if (!type_die)
19573     {
19574       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19575
19576       type_die = modified_type_die (type, TYPE_READONLY (type),
19577                                     TYPE_VOLATILE (type), context_die);
19578       gcc_assert (type_die);
19579     }
19580   return type_die;
19581 }
19582
19583 /* Force out any required namespaces to be able to output DECL,
19584    and return the new context_die for it, if it's changed.  */
19585
19586 static dw_die_ref
19587 setup_namespace_context (tree thing, dw_die_ref context_die)
19588 {
19589   tree context = (DECL_P (thing)
19590                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19591   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19592     /* Force out the namespace.  */
19593     context_die = force_decl_die (context);
19594
19595   return context_die;
19596 }
19597
19598 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19599    type) within its namespace, if appropriate.
19600
19601    For compatibility with older debuggers, namespace DIEs only contain
19602    declarations; all definitions are emitted at CU scope.  */
19603
19604 static dw_die_ref
19605 declare_in_namespace (tree thing, dw_die_ref context_die)
19606 {
19607   dw_die_ref ns_context;
19608
19609   if (debug_info_level <= DINFO_LEVEL_TERSE)
19610     return context_die;
19611
19612   /* If this decl is from an inlined function, then don't try to emit it in its
19613      namespace, as we will get confused.  It would have already been emitted
19614      when the abstract instance of the inline function was emitted anyways.  */
19615   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19616     return context_die;
19617
19618   ns_context = setup_namespace_context (thing, context_die);
19619
19620   if (ns_context != context_die)
19621     {
19622       if (is_fortran ())
19623         return ns_context;
19624       if (DECL_P (thing))
19625         gen_decl_die (thing, NULL, ns_context);
19626       else
19627         gen_type_die (thing, ns_context);
19628     }
19629   return context_die;
19630 }
19631
19632 /* Generate a DIE for a namespace or namespace alias.  */
19633
19634 static void
19635 gen_namespace_die (tree decl, dw_die_ref context_die)
19636 {
19637   dw_die_ref namespace_die;
19638
19639   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19640      they are an alias of.  */
19641   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19642     {
19643       /* Output a real namespace or module.  */
19644       context_die = setup_namespace_context (decl, comp_unit_die ());
19645       namespace_die = new_die (is_fortran ()
19646                                ? DW_TAG_module : DW_TAG_namespace,
19647                                context_die, decl);
19648       /* For Fortran modules defined in different CU don't add src coords.  */
19649       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19650         {
19651           const char *name = dwarf2_name (decl, 0);
19652           if (name)
19653             add_name_attribute (namespace_die, name);
19654         }
19655       else
19656         add_name_and_src_coords_attributes (namespace_die, decl);
19657       if (DECL_EXTERNAL (decl))
19658         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19659       equate_decl_number_to_die (decl, namespace_die);
19660     }
19661   else
19662     {
19663       /* Output a namespace alias.  */
19664
19665       /* Force out the namespace we are an alias of, if necessary.  */
19666       dw_die_ref origin_die
19667         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19668
19669       if (DECL_FILE_SCOPE_P (decl)
19670           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19671         context_die = setup_namespace_context (decl, comp_unit_die ());
19672       /* Now create the namespace alias DIE.  */
19673       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19674       add_name_and_src_coords_attributes (namespace_die, decl);
19675       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19676       equate_decl_number_to_die (decl, namespace_die);
19677     }
19678 }
19679
19680 /* Generate Dwarf debug information for a decl described by DECL.
19681    The return value is currently only meaningful for PARM_DECLs,
19682    for all other decls it returns NULL.  */
19683
19684 static dw_die_ref
19685 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19686 {
19687   tree decl_or_origin = decl ? decl : origin;
19688   tree class_origin = NULL, ultimate_origin;
19689
19690   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19691     return NULL;
19692
19693   switch (TREE_CODE (decl_or_origin))
19694     {
19695     case ERROR_MARK:
19696       break;
19697
19698     case CONST_DECL:
19699       if (!is_fortran () && !is_ada ())
19700         {
19701           /* The individual enumerators of an enum type get output when we output
19702              the Dwarf representation of the relevant enum type itself.  */
19703           break;
19704         }
19705
19706       /* Emit its type.  */
19707       gen_type_die (TREE_TYPE (decl), context_die);
19708
19709       /* And its containing namespace.  */
19710       context_die = declare_in_namespace (decl, context_die);
19711
19712       gen_const_die (decl, context_die);
19713       break;
19714
19715     case FUNCTION_DECL:
19716       /* Don't output any DIEs to represent mere function declarations,
19717          unless they are class members or explicit block externs.  */
19718       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19719           && DECL_FILE_SCOPE_P (decl_or_origin)
19720           && (current_function_decl == NULL_TREE
19721               || DECL_ARTIFICIAL (decl_or_origin)))
19722         break;
19723
19724 #if 0
19725       /* FIXME */
19726       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19727          on local redeclarations of global functions.  That seems broken.  */
19728       if (current_function_decl != decl)
19729         /* This is only a declaration.  */;
19730 #endif
19731
19732       /* If we're emitting a clone, emit info for the abstract instance.  */
19733       if (origin || DECL_ORIGIN (decl) != decl)
19734         dwarf2out_abstract_function (origin
19735                                      ? DECL_ORIGIN (origin)
19736                                      : DECL_ABSTRACT_ORIGIN (decl));
19737
19738       /* If we're emitting an out-of-line copy of an inline function,
19739          emit info for the abstract instance and set up to refer to it.  */
19740       else if (cgraph_function_possibly_inlined_p (decl)
19741                && ! DECL_ABSTRACT (decl)
19742                && ! class_or_namespace_scope_p (context_die)
19743                /* dwarf2out_abstract_function won't emit a die if this is just
19744                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19745                   that case, because that works only if we have a die.  */
19746                && DECL_INITIAL (decl) != NULL_TREE)
19747         {
19748           dwarf2out_abstract_function (decl);
19749           set_decl_origin_self (decl);
19750         }
19751
19752       /* Otherwise we're emitting the primary DIE for this decl.  */
19753       else if (debug_info_level > DINFO_LEVEL_TERSE)
19754         {
19755           /* Before we describe the FUNCTION_DECL itself, make sure that we
19756              have its containing type.  */
19757           if (!origin)
19758             origin = decl_class_context (decl);
19759           if (origin != NULL_TREE)
19760             gen_type_die (origin, context_die);
19761
19762           /* And its return type.  */
19763           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19764
19765           /* And its virtual context.  */
19766           if (DECL_VINDEX (decl) != NULL_TREE)
19767             gen_type_die (DECL_CONTEXT (decl), context_die);
19768
19769           /* Make sure we have a member DIE for decl.  */
19770           if (origin != NULL_TREE)
19771             gen_type_die_for_member (origin, decl, context_die);
19772
19773           /* And its containing namespace.  */
19774           context_die = declare_in_namespace (decl, context_die);
19775         }
19776
19777       /* Now output a DIE to represent the function itself.  */
19778       if (decl)
19779         gen_subprogram_die (decl, context_die);
19780       break;
19781
19782     case TYPE_DECL:
19783       /* If we are in terse mode, don't generate any DIEs to represent any
19784          actual typedefs.  */
19785       if (debug_info_level <= DINFO_LEVEL_TERSE)
19786         break;
19787
19788       /* In the special case of a TYPE_DECL node representing the declaration
19789          of some type tag, if the given TYPE_DECL is marked as having been
19790          instantiated from some other (original) TYPE_DECL node (e.g. one which
19791          was generated within the original definition of an inline function) we
19792          used to generate a special (abbreviated) DW_TAG_structure_type,
19793          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19794          should be actually referencing those DIEs, as variable DIEs with that
19795          type would be emitted already in the abstract origin, so it was always
19796          removed during unused type prunning.  Don't add anything in this
19797          case.  */
19798       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19799         break;
19800
19801       if (is_redundant_typedef (decl))
19802         gen_type_die (TREE_TYPE (decl), context_die);
19803       else
19804         /* Output a DIE to represent the typedef itself.  */
19805         gen_typedef_die (decl, context_die);
19806       break;
19807
19808     case LABEL_DECL:
19809       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19810         gen_label_die (decl, context_die);
19811       break;
19812
19813     case VAR_DECL:
19814     case RESULT_DECL:
19815       /* If we are in terse mode, don't generate any DIEs to represent any
19816          variable declarations or definitions.  */
19817       if (debug_info_level <= DINFO_LEVEL_TERSE)
19818         break;
19819
19820       /* Output any DIEs that are needed to specify the type of this data
19821          object.  */
19822       if (decl_by_reference_p (decl_or_origin))
19823         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19824       else
19825         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19826
19827       /* And its containing type.  */
19828       class_origin = decl_class_context (decl_or_origin);
19829       if (class_origin != NULL_TREE)
19830         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19831
19832       /* And its containing namespace.  */
19833       context_die = declare_in_namespace (decl_or_origin, context_die);
19834
19835       /* Now output the DIE to represent the data object itself.  This gets
19836          complicated because of the possibility that the VAR_DECL really
19837          represents an inlined instance of a formal parameter for an inline
19838          function.  */
19839       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19840       if (ultimate_origin != NULL_TREE
19841           && TREE_CODE (ultimate_origin) == PARM_DECL)
19842         gen_formal_parameter_die (decl, origin,
19843                                   true /* Emit name attribute.  */,
19844                                   context_die);
19845       else
19846         gen_variable_die (decl, origin, context_die);
19847       break;
19848
19849     case FIELD_DECL:
19850       /* Ignore the nameless fields that are used to skip bits but handle C++
19851          anonymous unions and structs.  */
19852       if (DECL_NAME (decl) != NULL_TREE
19853           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19854           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19855         {
19856           gen_type_die (member_declared_type (decl), context_die);
19857           gen_field_die (decl, context_die);
19858         }
19859       break;
19860
19861     case PARM_DECL:
19862       if (DECL_BY_REFERENCE (decl_or_origin))
19863         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19864       else
19865         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19866       return gen_formal_parameter_die (decl, origin,
19867                                        true /* Emit name attribute.  */,
19868                                        context_die);
19869
19870     case NAMESPACE_DECL:
19871     case IMPORTED_DECL:
19872       if (dwarf_version >= 3 || !dwarf_strict)
19873         gen_namespace_die (decl, context_die);
19874       break;
19875
19876     default:
19877       /* Probably some frontend-internal decl.  Assume we don't care.  */
19878       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19879       break;
19880     }
19881
19882   return NULL;
19883 }
19884 \f
19885 /* Output debug information for global decl DECL.  Called from toplev.c after
19886    compilation proper has finished.  */
19887
19888 static void
19889 dwarf2out_global_decl (tree decl)
19890 {
19891   /* Output DWARF2 information for file-scope tentative data object
19892      declarations, file-scope (extern) function declarations (which
19893      had no corresponding body) and file-scope tagged type declarations
19894      and definitions which have not yet been forced out.  */
19895   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19896     dwarf2out_decl (decl);
19897 }
19898
19899 /* Output debug information for type decl DECL.  Called from toplev.c
19900    and from language front ends (to record built-in types).  */
19901 static void
19902 dwarf2out_type_decl (tree decl, int local)
19903 {
19904   if (!local)
19905     dwarf2out_decl (decl);
19906 }
19907
19908 /* Output debug information for imported module or decl DECL.
19909    NAME is non-NULL name in the lexical block if the decl has been renamed.
19910    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19911    that DECL belongs to.
19912    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19913 static void
19914 dwarf2out_imported_module_or_decl_1 (tree decl,
19915                                      tree name,
19916                                      tree lexical_block,
19917                                      dw_die_ref lexical_block_die)
19918 {
19919   expanded_location xloc;
19920   dw_die_ref imported_die = NULL;
19921   dw_die_ref at_import_die;
19922
19923   if (TREE_CODE (decl) == IMPORTED_DECL)
19924     {
19925       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19926       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19927       gcc_assert (decl);
19928     }
19929   else
19930     xloc = expand_location (input_location);
19931
19932   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19933     {
19934       at_import_die = force_type_die (TREE_TYPE (decl));
19935       /* For namespace N { typedef void T; } using N::T; base_type_die
19936          returns NULL, but DW_TAG_imported_declaration requires
19937          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19938       if (!at_import_die)
19939         {
19940           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19941           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19942           at_import_die = lookup_type_die (TREE_TYPE (decl));
19943           gcc_assert (at_import_die);
19944         }
19945     }
19946   else
19947     {
19948       at_import_die = lookup_decl_die (decl);
19949       if (!at_import_die)
19950         {
19951           /* If we're trying to avoid duplicate debug info, we may not have
19952              emitted the member decl for this field.  Emit it now.  */
19953           if (TREE_CODE (decl) == FIELD_DECL)
19954             {
19955               tree type = DECL_CONTEXT (decl);
19956
19957               if (TYPE_CONTEXT (type)
19958                   && TYPE_P (TYPE_CONTEXT (type))
19959                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19960                                                 DINFO_USAGE_DIR_USE))
19961                 return;
19962               gen_type_die_for_member (type, decl,
19963                                        get_context_die (TYPE_CONTEXT (type)));
19964             }
19965           at_import_die = force_decl_die (decl);
19966         }
19967     }
19968
19969   if (TREE_CODE (decl) == NAMESPACE_DECL)
19970     {
19971       if (dwarf_version >= 3 || !dwarf_strict)
19972         imported_die = new_die (DW_TAG_imported_module,
19973                                 lexical_block_die,
19974                                 lexical_block);
19975       else
19976         return;
19977     }
19978   else
19979     imported_die = new_die (DW_TAG_imported_declaration,
19980                             lexical_block_die,
19981                             lexical_block);
19982
19983   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19984   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19985   if (name)
19986     add_AT_string (imported_die, DW_AT_name,
19987                    IDENTIFIER_POINTER (name));
19988   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19989 }
19990
19991 /* Output debug information for imported module or decl DECL.
19992    NAME is non-NULL name in context if the decl has been renamed.
19993    CHILD is true if decl is one of the renamed decls as part of
19994    importing whole module.  */
19995
19996 static void
19997 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19998                                    bool child)
19999 {
20000   /* dw_die_ref at_import_die;  */
20001   dw_die_ref scope_die;
20002
20003   if (debug_info_level <= DINFO_LEVEL_TERSE)
20004     return;
20005
20006   gcc_assert (decl);
20007
20008   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20009      We need decl DIE for reference and scope die. First, get DIE for the decl
20010      itself.  */
20011
20012   /* Get the scope die for decl context. Use comp_unit_die for global module
20013      or decl. If die is not found for non globals, force new die.  */
20014   if (context
20015       && TYPE_P (context)
20016       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20017     return;
20018
20019   if (!(dwarf_version >= 3 || !dwarf_strict))
20020     return;
20021
20022   scope_die = get_context_die (context);
20023
20024   if (child)
20025     {
20026       gcc_assert (scope_die->die_child);
20027       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20028       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20029       scope_die = scope_die->die_child;
20030     }
20031
20032   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20033   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20034
20035 }
20036
20037 /* Write the debugging output for DECL.  */
20038
20039 void
20040 dwarf2out_decl (tree decl)
20041 {
20042   dw_die_ref context_die = comp_unit_die ();
20043
20044   switch (TREE_CODE (decl))
20045     {
20046     case ERROR_MARK:
20047       return;
20048
20049     case FUNCTION_DECL:
20050       /* What we would really like to do here is to filter out all mere
20051          file-scope declarations of file-scope functions which are never
20052          referenced later within this translation unit (and keep all of ones
20053          that *are* referenced later on) but we aren't clairvoyant, so we have
20054          no idea which functions will be referenced in the future (i.e. later
20055          on within the current translation unit). So here we just ignore all
20056          file-scope function declarations which are not also definitions.  If
20057          and when the debugger needs to know something about these functions,
20058          it will have to hunt around and find the DWARF information associated
20059          with the definition of the function.
20060
20061          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20062          nodes represent definitions and which ones represent mere
20063          declarations.  We have to check DECL_INITIAL instead. That's because
20064          the C front-end supports some weird semantics for "extern inline"
20065          function definitions.  These can get inlined within the current
20066          translation unit (and thus, we need to generate Dwarf info for their
20067          abstract instances so that the Dwarf info for the concrete inlined
20068          instances can have something to refer to) but the compiler never
20069          generates any out-of-lines instances of such things (despite the fact
20070          that they *are* definitions).
20071
20072          The important point is that the C front-end marks these "extern
20073          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20074          them anyway. Note that the C++ front-end also plays some similar games
20075          for inline function definitions appearing within include files which
20076          also contain `#pragma interface' pragmas.  */
20077       if (DECL_INITIAL (decl) == NULL_TREE)
20078         return;
20079
20080       /* If we're a nested function, initially use a parent of NULL; if we're
20081          a plain function, this will be fixed up in decls_for_scope.  If
20082          we're a method, it will be ignored, since we already have a DIE.  */
20083       if (decl_function_context (decl)
20084           /* But if we're in terse mode, we don't care about scope.  */
20085           && debug_info_level > DINFO_LEVEL_TERSE)
20086         context_die = NULL;
20087       break;
20088
20089     case VAR_DECL:
20090       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20091          declaration and if the declaration was never even referenced from
20092          within this entire compilation unit.  We suppress these DIEs in
20093          order to save space in the .debug section (by eliminating entries
20094          which are probably useless).  Note that we must not suppress
20095          block-local extern declarations (whether used or not) because that
20096          would screw-up the debugger's name lookup mechanism and cause it to
20097          miss things which really ought to be in scope at a given point.  */
20098       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20099         return;
20100
20101       /* For local statics lookup proper context die.  */
20102       if (TREE_STATIC (decl) && decl_function_context (decl))
20103         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20104
20105       /* If we are in terse mode, don't generate any DIEs to represent any
20106          variable declarations or definitions.  */
20107       if (debug_info_level <= DINFO_LEVEL_TERSE)
20108         return;
20109       break;
20110
20111     case CONST_DECL:
20112       if (debug_info_level <= DINFO_LEVEL_TERSE)
20113         return;
20114       if (!is_fortran () && !is_ada ())
20115         return;
20116       if (TREE_STATIC (decl) && decl_function_context (decl))
20117         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20118       break;
20119
20120     case NAMESPACE_DECL:
20121     case IMPORTED_DECL:
20122       if (debug_info_level <= DINFO_LEVEL_TERSE)
20123         return;
20124       if (lookup_decl_die (decl) != NULL)
20125         return;
20126       break;
20127
20128     case TYPE_DECL:
20129       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20130       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20131         return;
20132
20133       /* Don't bother trying to generate any DIEs to represent any of the
20134          normal built-in types for the language we are compiling.  */
20135       if (DECL_IS_BUILTIN (decl))
20136         return;
20137
20138       /* If we are in terse mode, don't generate any DIEs for types.  */
20139       if (debug_info_level <= DINFO_LEVEL_TERSE)
20140         return;
20141
20142       /* If we're a function-scope tag, initially use a parent of NULL;
20143          this will be fixed up in decls_for_scope.  */
20144       if (decl_function_context (decl))
20145         context_die = NULL;
20146
20147       break;
20148
20149     default:
20150       return;
20151     }
20152
20153   gen_decl_die (decl, NULL, context_die);
20154 }
20155
20156 /* Write the debugging output for DECL.  */
20157
20158 static void
20159 dwarf2out_function_decl (tree decl)
20160 {
20161   dwarf2out_decl (decl);
20162   call_arg_locations = NULL;
20163   call_arg_loc_last = NULL;
20164   call_site_count = -1;
20165   tail_call_site_count = -1;
20166   VEC_free (dw_die_ref, heap, block_map);
20167   htab_empty (decl_loc_table);
20168   htab_empty (cached_dw_loc_list_table);
20169 }
20170
20171 /* Output a marker (i.e. a label) for the beginning of the generated code for
20172    a lexical block.  */
20173
20174 static void
20175 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20176                        unsigned int blocknum)
20177 {
20178   switch_to_section (current_function_section ());
20179   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20180 }
20181
20182 /* Output a marker (i.e. a label) for the end of the generated code for a
20183    lexical block.  */
20184
20185 static void
20186 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20187 {
20188   switch_to_section (current_function_section ());
20189   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20190 }
20191
20192 /* Returns nonzero if it is appropriate not to emit any debugging
20193    information for BLOCK, because it doesn't contain any instructions.
20194
20195    Don't allow this for blocks with nested functions or local classes
20196    as we would end up with orphans, and in the presence of scheduling
20197    we may end up calling them anyway.  */
20198
20199 static bool
20200 dwarf2out_ignore_block (const_tree block)
20201 {
20202   tree decl;
20203   unsigned int i;
20204
20205   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20206     if (TREE_CODE (decl) == FUNCTION_DECL
20207         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20208       return 0;
20209   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20210     {
20211       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20212       if (TREE_CODE (decl) == FUNCTION_DECL
20213           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20214       return 0;
20215     }
20216
20217   return 1;
20218 }
20219
20220 /* Hash table routines for file_hash.  */
20221
20222 static int
20223 file_table_eq (const void *p1_p, const void *p2_p)
20224 {
20225   const struct dwarf_file_data *const p1 =
20226     (const struct dwarf_file_data *) p1_p;
20227   const char *const p2 = (const char *) p2_p;
20228   return filename_cmp (p1->filename, p2) == 0;
20229 }
20230
20231 static hashval_t
20232 file_table_hash (const void *p_p)
20233 {
20234   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20235   return htab_hash_string (p->filename);
20236 }
20237
20238 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20239    dwarf2out.c) and return its "index".  The index of each (known) filename is
20240    just a unique number which is associated with only that one filename.  We
20241    need such numbers for the sake of generating labels (in the .debug_sfnames
20242    section) and references to those files numbers (in the .debug_srcinfo
20243    and.debug_macinfo sections).  If the filename given as an argument is not
20244    found in our current list, add it to the list and assign it the next
20245    available unique index number.  In order to speed up searches, we remember
20246    the index of the filename was looked up last.  This handles the majority of
20247    all searches.  */
20248
20249 static struct dwarf_file_data *
20250 lookup_filename (const char *file_name)
20251 {
20252   void ** slot;
20253   struct dwarf_file_data * created;
20254
20255   /* Check to see if the file name that was searched on the previous
20256      call matches this file name.  If so, return the index.  */
20257   if (file_table_last_lookup
20258       && (file_name == file_table_last_lookup->filename
20259           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20260     return file_table_last_lookup;
20261
20262   /* Didn't match the previous lookup, search the table.  */
20263   slot = htab_find_slot_with_hash (file_table, file_name,
20264                                    htab_hash_string (file_name), INSERT);
20265   if (*slot)
20266     return (struct dwarf_file_data *) *slot;
20267
20268   created = ggc_alloc_dwarf_file_data ();
20269   created->filename = file_name;
20270   created->emitted_number = 0;
20271   *slot = created;
20272   return created;
20273 }
20274
20275 /* If the assembler will construct the file table, then translate the compiler
20276    internal file table number into the assembler file table number, and emit
20277    a .file directive if we haven't already emitted one yet.  The file table
20278    numbers are different because we prune debug info for unused variables and
20279    types, which may include filenames.  */
20280
20281 static int
20282 maybe_emit_file (struct dwarf_file_data * fd)
20283 {
20284   if (! fd->emitted_number)
20285     {
20286       if (last_emitted_file)
20287         fd->emitted_number = last_emitted_file->emitted_number + 1;
20288       else
20289         fd->emitted_number = 1;
20290       last_emitted_file = fd;
20291
20292       if (DWARF2_ASM_LINE_DEBUG_INFO)
20293         {
20294           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20295           output_quoted_string (asm_out_file,
20296                                 remap_debug_filename (fd->filename));
20297           fputc ('\n', asm_out_file);
20298         }
20299     }
20300
20301   return fd->emitted_number;
20302 }
20303
20304 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20305    That generation should happen after function debug info has been
20306    generated. The value of the attribute is the constant value of ARG.  */
20307
20308 static void
20309 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20310 {
20311   die_arg_entry entry;
20312
20313   if (!die || !arg)
20314     return;
20315
20316   if (!tmpl_value_parm_die_table)
20317     tmpl_value_parm_die_table
20318       = VEC_alloc (die_arg_entry, gc, 32);
20319
20320   entry.die = die;
20321   entry.arg = arg;
20322   VEC_safe_push (die_arg_entry, gc,
20323                  tmpl_value_parm_die_table,
20324                  &entry);
20325 }
20326
20327 /* Return TRUE if T is an instance of generic type, FALSE
20328    otherwise.  */
20329
20330 static bool
20331 generic_type_p (tree t)
20332 {
20333   if (t == NULL_TREE || !TYPE_P (t))
20334     return false;
20335   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20336 }
20337
20338 /* Schedule the generation of the generic parameter dies for the
20339   instance of generic type T. The proper generation itself is later
20340   done by gen_scheduled_generic_parms_dies. */
20341
20342 static void
20343 schedule_generic_params_dies_gen (tree t)
20344 {
20345   if (!generic_type_p (t))
20346     return;
20347
20348   if (generic_type_instances == NULL)
20349     generic_type_instances = VEC_alloc (tree, gc, 256);
20350
20351   VEC_safe_push (tree, gc, generic_type_instances, t);
20352 }
20353
20354 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20355    by append_entry_to_tmpl_value_parm_die_table. This function must
20356    be called after function DIEs have been generated.  */
20357
20358 static void
20359 gen_remaining_tmpl_value_param_die_attribute (void)
20360 {
20361   if (tmpl_value_parm_die_table)
20362     {
20363       unsigned i;
20364       die_arg_entry *e;
20365
20366       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20367         tree_add_const_value_attribute (e->die, e->arg);
20368     }
20369 }
20370
20371 /* Generate generic parameters DIEs for instances of generic types
20372    that have been previously scheduled by
20373    schedule_generic_params_dies_gen. This function must be called
20374    after all the types of the CU have been laid out.  */
20375
20376 static void
20377 gen_scheduled_generic_parms_dies (void)
20378 {
20379   unsigned i;
20380   tree t;
20381
20382   if (generic_type_instances == NULL)
20383     return;
20384   
20385   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20386     gen_generic_params_dies (t);
20387 }
20388
20389
20390 /* Replace DW_AT_name for the decl with name.  */
20391
20392 static void
20393 dwarf2out_set_name (tree decl, tree name)
20394 {
20395   dw_die_ref die;
20396   dw_attr_ref attr;
20397   const char *dname;
20398
20399   die = TYPE_SYMTAB_DIE (decl);
20400   if (!die)
20401     return;
20402
20403   dname = dwarf2_name (name, 0);
20404   if (!dname)
20405     return;
20406
20407   attr = get_AT (die, DW_AT_name);
20408   if (attr)
20409     {
20410       struct indirect_string_node *node;
20411
20412       node = find_AT_string (dname);
20413       /* replace the string.  */
20414       attr->dw_attr_val.v.val_str = node;
20415     }
20416
20417   else
20418     add_name_attribute (die, dname);
20419 }
20420
20421 /* Called by the final INSN scan whenever we see a var location.  We
20422    use it to drop labels in the right places, and throw the location in
20423    our lookup table.  */
20424
20425 static void
20426 dwarf2out_var_location (rtx loc_note)
20427 {
20428   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20429   struct var_loc_node *newloc;
20430   rtx next_real;
20431   static const char *last_label;
20432   static const char *last_postcall_label;
20433   static bool last_in_cold_section_p;
20434   tree decl;
20435   bool var_loc_p;
20436
20437   if (!NOTE_P (loc_note))
20438     {
20439       if (CALL_P (loc_note))
20440         {
20441           call_site_count++;
20442           if (SIBLING_CALL_P (loc_note))
20443             tail_call_site_count++;
20444         }
20445       return;
20446     }
20447
20448   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20449   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20450     return;
20451
20452   next_real = next_real_insn (loc_note);
20453
20454   /* If there are no instructions which would be affected by this note,
20455      don't do anything.  */
20456   if (var_loc_p
20457       && next_real == NULL_RTX
20458       && !NOTE_DURING_CALL_P (loc_note))
20459     return;
20460
20461   if (next_real == NULL_RTX)
20462     next_real = get_last_insn ();
20463
20464   /* If there were any real insns between note we processed last time
20465      and this note (or if it is the first note), clear
20466      last_{,postcall_}label so that they are not reused this time.  */
20467   if (last_var_location_insn == NULL_RTX
20468       || last_var_location_insn != next_real
20469       || last_in_cold_section_p != in_cold_section_p)
20470     {
20471       last_label = NULL;
20472       last_postcall_label = NULL;
20473     }
20474
20475   if (var_loc_p)
20476     {
20477       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20478       newloc = add_var_loc_to_decl (decl, loc_note,
20479                                     NOTE_DURING_CALL_P (loc_note)
20480                                     ? last_postcall_label : last_label);
20481       if (newloc == NULL)
20482         return;
20483     }
20484   else
20485     {
20486       decl = NULL_TREE;
20487       newloc = NULL;
20488     }
20489
20490   /* If there were no real insns between note we processed last time
20491      and this note, use the label we emitted last time.  Otherwise
20492      create a new label and emit it.  */
20493   if (last_label == NULL)
20494     {
20495       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20496       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20497       loclabel_num++;
20498       last_label = ggc_strdup (loclabel);
20499     }
20500
20501   if (!var_loc_p)
20502     {
20503       struct call_arg_loc_node *ca_loc
20504         = ggc_alloc_cleared_call_arg_loc_node ();
20505       rtx prev = prev_real_insn (loc_note), x;
20506       ca_loc->call_arg_loc_note = loc_note;
20507       ca_loc->next = NULL;
20508       ca_loc->label = last_label;
20509       gcc_assert (prev
20510                   && (CALL_P (prev)
20511                       || (NONJUMP_INSN_P (prev)
20512                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20513                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20514       if (!CALL_P (prev))
20515         prev = XVECEXP (PATTERN (prev), 0, 0);
20516       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20517       x = PATTERN (prev);
20518       if (GET_CODE (x) == PARALLEL)
20519         x = XVECEXP (x, 0, 0);
20520       if (GET_CODE (x) == SET)
20521         x = SET_SRC (x);
20522       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20523         {
20524           x = XEXP (XEXP (x, 0), 0);
20525           if (GET_CODE (x) == SYMBOL_REF
20526               && SYMBOL_REF_DECL (x)
20527               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20528             ca_loc->symbol_ref = x;
20529         }
20530       ca_loc->block = insn_scope (prev);
20531       if (call_arg_locations)
20532         call_arg_loc_last->next = ca_loc;
20533       else
20534         call_arg_locations = ca_loc;
20535       call_arg_loc_last = ca_loc;
20536     }
20537   else if (!NOTE_DURING_CALL_P (loc_note))
20538     newloc->label = last_label;
20539   else
20540     {
20541       if (!last_postcall_label)
20542         {
20543           sprintf (loclabel, "%s-1", last_label);
20544           last_postcall_label = ggc_strdup (loclabel);
20545         }
20546       newloc->label = last_postcall_label;
20547     }
20548
20549   last_var_location_insn = next_real;
20550   last_in_cold_section_p = in_cold_section_p;
20551 }
20552
20553 /* Note in one location list that text section has changed.  */
20554
20555 static int
20556 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20557 {
20558   var_loc_list *list = (var_loc_list *) *slot;
20559   if (list->first)
20560     list->last_before_switch
20561       = list->last->next ? list->last->next : list->last;
20562   return 1;
20563 }
20564
20565 /* Note in all location lists that text section has changed.  */
20566
20567 static void
20568 var_location_switch_text_section (void)
20569 {
20570   if (decl_loc_table == NULL)
20571     return;
20572
20573   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20574 }
20575
20576 /* Create a new line number table.  */
20577
20578 static dw_line_info_table *
20579 new_line_info_table (void)
20580 {
20581   dw_line_info_table *table;
20582
20583   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20584   table->file_num = 1;
20585   table->line_num = 1;
20586   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20587
20588   return table;
20589 }
20590
20591 /* Lookup the "current" table into which we emit line info, so
20592    that we don't have to do it for every source line.  */
20593
20594 static void
20595 set_cur_line_info_table (section *sec)
20596 {
20597   dw_line_info_table *table;
20598
20599   if (sec == text_section)
20600     table = text_section_line_info;
20601   else if (sec == cold_text_section)
20602     {
20603       table = cold_text_section_line_info;
20604       if (!table)
20605         {
20606           cold_text_section_line_info = table = new_line_info_table ();
20607           table->end_label = cold_end_label;
20608         }
20609     }
20610   else
20611     {
20612       const char *end_label;
20613
20614       if (flag_reorder_blocks_and_partition)
20615         {
20616           if (in_cold_section_p)
20617             end_label = crtl->subsections.cold_section_end_label;
20618           else
20619             end_label = crtl->subsections.hot_section_end_label;
20620         }
20621       else
20622         {
20623           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20624           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20625                                        current_function_funcdef_no);
20626           end_label = ggc_strdup (label);
20627         }
20628
20629       table = new_line_info_table ();
20630       table->end_label = end_label;
20631
20632       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20633     }
20634
20635   cur_line_info_table = table;
20636 }
20637
20638
20639 /* We need to reset the locations at the beginning of each
20640    function. We can't do this in the end_function hook, because the
20641    declarations that use the locations won't have been output when
20642    that hook is called.  Also compute have_multiple_function_sections here.  */
20643
20644 static void
20645 dwarf2out_begin_function (tree fun)
20646 {
20647   section *sec = function_section (fun);
20648
20649   if (sec != text_section)
20650     have_multiple_function_sections = true;
20651
20652   if (flag_reorder_blocks_and_partition && !cold_text_section)
20653     {
20654       gcc_assert (current_function_decl == fun);
20655       cold_text_section = unlikely_text_section ();
20656       switch_to_section (cold_text_section);
20657       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20658       switch_to_section (sec);
20659     }
20660
20661   dwarf2out_note_section_used ();
20662   call_site_count = 0;
20663   tail_call_site_count = 0;
20664
20665   set_cur_line_info_table (sec);
20666 }
20667
20668 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20669
20670 static void
20671 push_dw_line_info_entry (dw_line_info_table *table,
20672                          enum dw_line_info_opcode opcode, unsigned int val)
20673 {
20674   dw_line_info_entry e;
20675   e.opcode = opcode;
20676   e.val = val;
20677   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20678 }
20679
20680 /* Output a label to mark the beginning of a source code line entry
20681    and record information relating to this source line, in
20682    'line_info_table' for later output of the .debug_line section.  */
20683 /* ??? The discriminator parameter ought to be unsigned.  */
20684
20685 static void
20686 dwarf2out_source_line (unsigned int line, const char *filename,
20687                        int discriminator, bool is_stmt)
20688 {
20689   unsigned int file_num;
20690   dw_line_info_table *table;
20691
20692   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20693     return;
20694
20695   /* The discriminator column was added in dwarf4.  Simplify the below
20696      by simply removing it if we're not supposed to output it.  */
20697   if (dwarf_version < 4 && dwarf_strict)
20698     discriminator = 0;
20699
20700   table = cur_line_info_table;
20701   file_num = maybe_emit_file (lookup_filename (filename));
20702
20703   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20704      the debugger has used the second (possibly duplicate) line number
20705      at the beginning of the function to mark the end of the prologue.
20706      We could eliminate any other duplicates within the function.  For
20707      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20708      that second line number entry.  */
20709   /* Recall that this end-of-prologue indication is *not* the same thing
20710      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20711      to which the hook corresponds, follows the last insn that was 
20712      emitted by gen_prologue.  What we need is to preceed the first insn
20713      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20714      insn that corresponds to something the user wrote.  These may be
20715      very different locations once scheduling is enabled.  */
20716
20717   if (0 && file_num == table->file_num
20718       && line == table->line_num
20719       && discriminator == table->discrim_num
20720       && is_stmt == table->is_stmt)
20721     return;
20722
20723   switch_to_section (current_function_section ());
20724
20725   /* If requested, emit something human-readable.  */
20726   if (flag_debug_asm)
20727     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20728
20729   if (DWARF2_ASM_LINE_DEBUG_INFO)
20730     {
20731       /* Emit the .loc directive understood by GNU as.  */
20732       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20733       if (is_stmt != table->is_stmt)
20734         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20735       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20736         fprintf (asm_out_file, " discriminator %d", discriminator);
20737       fputc ('\n', asm_out_file);
20738     }
20739   else
20740     {
20741       unsigned int label_num = ++line_info_label_num;
20742
20743       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20744
20745       push_dw_line_info_entry (table, LI_set_address, label_num);
20746       if (file_num != table->file_num)
20747         push_dw_line_info_entry (table, LI_set_file, file_num);
20748       if (discriminator != table->discrim_num)
20749         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20750       if (is_stmt != table->is_stmt)
20751         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20752       push_dw_line_info_entry (table, LI_set_line, line);
20753     }
20754
20755   table->file_num = file_num;
20756   table->line_num = line;
20757   table->discrim_num = discriminator;
20758   table->is_stmt = is_stmt;
20759   table->in_use = true;
20760 }
20761
20762 /* Record the beginning of a new source file.  */
20763
20764 static void
20765 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20766 {
20767   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20768     {
20769       /* Record the beginning of the file for break_out_includes.  */
20770       dw_die_ref bincl_die;
20771
20772       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20773       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20774     }
20775
20776   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20777     {
20778       macinfo_entry e;
20779       e.code = DW_MACINFO_start_file;
20780       e.lineno = lineno;
20781       e.info = xstrdup (filename);
20782       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20783     }
20784 }
20785
20786 /* Record the end of a source file.  */
20787
20788 static void
20789 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20790 {
20791   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20792     /* Record the end of the file for break_out_includes.  */
20793     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20794
20795   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20796     {
20797       macinfo_entry e;
20798       e.code = DW_MACINFO_end_file;
20799       e.lineno = lineno;
20800       e.info = NULL;
20801       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20802     }
20803 }
20804
20805 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20806    the tail part of the directive line, i.e. the part which is past the
20807    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20808
20809 static void
20810 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20811                   const char *buffer ATTRIBUTE_UNUSED)
20812 {
20813   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20814     {
20815       macinfo_entry e;
20816       e.code = DW_MACINFO_define;
20817       e.lineno = lineno;
20818       e.info = xstrdup (buffer);;
20819       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20820     }
20821 }
20822
20823 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20824    the tail part of the directive line, i.e. the part which is past the
20825    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20826
20827 static void
20828 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20829                  const char *buffer ATTRIBUTE_UNUSED)
20830 {
20831   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20832     {
20833       macinfo_entry e;
20834       e.code = DW_MACINFO_undef;
20835       e.lineno = lineno;
20836       e.info = xstrdup (buffer);;
20837       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20838     }
20839 }
20840
20841 static void
20842 output_macinfo (void)
20843 {
20844   unsigned i;
20845   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20846   macinfo_entry *ref;
20847
20848   if (! length)
20849     return;
20850
20851   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20852     {
20853       switch (ref->code)
20854         {
20855           case DW_MACINFO_start_file:
20856             {
20857               int file_num = maybe_emit_file (lookup_filename (ref->info));
20858               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20859               dw2_asm_output_data_uleb128 
20860                         (ref->lineno, "Included from line number %lu", 
20861                                                 (unsigned long)ref->lineno);
20862               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20863             }
20864             break;
20865           case DW_MACINFO_end_file:
20866             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20867             break;
20868           case DW_MACINFO_define:
20869             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
20870             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20871                                                 (unsigned long)ref->lineno);
20872             dw2_asm_output_nstring (ref->info, -1, "The macro");
20873             break;
20874           case DW_MACINFO_undef:
20875             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
20876             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20877                                                 (unsigned long)ref->lineno);
20878             dw2_asm_output_nstring (ref->info, -1, "The macro");
20879             break;
20880           default:
20881            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20882              ASM_COMMENT_START, (unsigned long)ref->code);
20883           break;
20884         }
20885     }
20886 }
20887
20888 /* Set up for Dwarf output at the start of compilation.  */
20889
20890 static void
20891 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20892 {
20893   /* Allocate the file_table.  */
20894   file_table = htab_create_ggc (50, file_table_hash,
20895                                 file_table_eq, NULL);
20896
20897   /* Allocate the decl_die_table.  */
20898   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20899                                     decl_die_table_eq, NULL);
20900
20901   /* Allocate the decl_loc_table.  */
20902   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20903                                     decl_loc_table_eq, NULL);
20904
20905   /* Allocate the cached_dw_loc_list_table.  */
20906   cached_dw_loc_list_table
20907     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20908                        cached_dw_loc_list_table_eq, NULL);
20909
20910   /* Allocate the initial hunk of the decl_scope_table.  */
20911   decl_scope_table = VEC_alloc (tree, gc, 256);
20912
20913   /* Allocate the initial hunk of the abbrev_die_table.  */
20914   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20915     (ABBREV_DIE_TABLE_INCREMENT);
20916   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20917   /* Zero-th entry is allocated, but unused.  */
20918   abbrev_die_table_in_use = 1;
20919
20920   /* Allocate the pubtypes and pubnames vectors.  */
20921   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20922   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20923
20924   incomplete_types = VEC_alloc (tree, gc, 64);
20925
20926   used_rtx_array = VEC_alloc (rtx, gc, 32);
20927
20928   debug_info_section = get_section (DEBUG_INFO_SECTION,
20929                                     SECTION_DEBUG, NULL);
20930   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20931                                       SECTION_DEBUG, NULL);
20932   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20933                                        SECTION_DEBUG, NULL);
20934   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20935                                        SECTION_DEBUG, NULL);
20936   debug_line_section = get_section (DEBUG_LINE_SECTION,
20937                                     SECTION_DEBUG, NULL);
20938   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20939                                    SECTION_DEBUG, NULL);
20940   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20941                                         SECTION_DEBUG, NULL);
20942   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20943                                         SECTION_DEBUG, NULL);
20944   debug_str_section = get_section (DEBUG_STR_SECTION,
20945                                    DEBUG_STR_SECTION_FLAGS, NULL);
20946   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20947                                       SECTION_DEBUG, NULL);
20948   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20949                                      SECTION_DEBUG, NULL);
20950
20951   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20952   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20953                                DEBUG_ABBREV_SECTION_LABEL, 0);
20954   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20955   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20956                                COLD_TEXT_SECTION_LABEL, 0);
20957   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20958
20959   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20960                                DEBUG_INFO_SECTION_LABEL, 0);
20961   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20962                                DEBUG_LINE_SECTION_LABEL, 0);
20963   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20964                                DEBUG_RANGES_SECTION_LABEL, 0);
20965   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20966                                DEBUG_MACINFO_SECTION_LABEL, 0);
20967
20968   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20969     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20970
20971   switch_to_section (text_section);
20972   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20973
20974   /* Make sure the line number table for .text always exists.  */
20975   text_section_line_info = new_line_info_table ();
20976   text_section_line_info->end_label = text_end_label;
20977 }
20978
20979 /* Called before cgraph_optimize starts outputtting functions, variables
20980    and toplevel asms into assembly.  */
20981
20982 static void
20983 dwarf2out_assembly_start (void)
20984 {
20985   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20986       && dwarf2out_do_cfi_asm ()
20987       && (!(flag_unwind_tables || flag_exceptions)
20988           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20989     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20990 }
20991
20992 /* A helper function for dwarf2out_finish called through
20993    htab_traverse.  Emit one queued .debug_str string.  */
20994
20995 static int
20996 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20997 {
20998   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20999
21000   if (node->form == DW_FORM_strp)
21001     {
21002       switch_to_section (debug_str_section);
21003       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21004       assemble_string (node->str, strlen (node->str) + 1);
21005     }
21006
21007   return 1;
21008 }
21009
21010 #if ENABLE_ASSERT_CHECKING
21011 /* Verify that all marks are clear.  */
21012
21013 static void
21014 verify_marks_clear (dw_die_ref die)
21015 {
21016   dw_die_ref c;
21017
21018   gcc_assert (! die->die_mark);
21019   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21020 }
21021 #endif /* ENABLE_ASSERT_CHECKING */
21022
21023 /* Clear the marks for a die and its children.
21024    Be cool if the mark isn't set.  */
21025
21026 static void
21027 prune_unmark_dies (dw_die_ref die)
21028 {
21029   dw_die_ref c;
21030
21031   if (die->die_mark)
21032     die->die_mark = 0;
21033   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21034 }
21035
21036 /* Given DIE that we're marking as used, find any other dies
21037    it references as attributes and mark them as used.  */
21038
21039 static void
21040 prune_unused_types_walk_attribs (dw_die_ref die)
21041 {
21042   dw_attr_ref a;
21043   unsigned ix;
21044
21045   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21046     {
21047       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21048         {
21049           /* A reference to another DIE.
21050              Make sure that it will get emitted.
21051              If it was broken out into a comdat group, don't follow it.  */
21052           if (! use_debug_types
21053               || a->dw_attr == DW_AT_specification
21054               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21055             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21056         }
21057       /* Set the string's refcount to 0 so that prune_unused_types_mark
21058          accounts properly for it.  */
21059       if (AT_class (a) == dw_val_class_str)
21060         a->dw_attr_val.v.val_str->refcount = 0;
21061     }
21062 }
21063
21064 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21065
21066 static void
21067 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21068 {
21069   dw_die_ref c;
21070
21071   if (die == NULL || die->die_child == NULL)
21072     return;
21073   c = die->die_child;
21074   do
21075     {
21076       switch (c->die_tag)
21077         {
21078         case DW_TAG_template_type_param:
21079         case DW_TAG_template_value_param:
21080         case DW_TAG_GNU_template_template_param:
21081         case DW_TAG_GNU_template_parameter_pack:
21082           prune_unused_types_mark (c, 1);
21083           break;
21084         default:
21085           break;
21086         }
21087       c = c->die_sib;
21088     } while (c && c != die->die_child);
21089 }
21090
21091 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21092    to DIE's children.  */
21093
21094 static void
21095 prune_unused_types_mark (dw_die_ref die, int dokids)
21096 {
21097   dw_die_ref c;
21098
21099   if (die->die_mark == 0)
21100     {
21101       /* We haven't done this node yet.  Mark it as used.  */
21102       die->die_mark = 1;
21103       /* If this is the DIE of a generic type instantiation,
21104          mark the children DIEs that describe its generic parms and
21105          args.  */
21106       prune_unused_types_mark_generic_parms_dies (die);
21107
21108       /* We also have to mark its parents as used.
21109          (But we don't want to mark our parents' kids due to this.)  */
21110       if (die->die_parent)
21111         prune_unused_types_mark (die->die_parent, 0);
21112
21113       /* Mark any referenced nodes.  */
21114       prune_unused_types_walk_attribs (die);
21115
21116       /* If this node is a specification,
21117          also mark the definition, if it exists.  */
21118       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21119         prune_unused_types_mark (die->die_definition, 1);
21120     }
21121
21122   if (dokids && die->die_mark != 2)
21123     {
21124       /* We need to walk the children, but haven't done so yet.
21125          Remember that we've walked the kids.  */
21126       die->die_mark = 2;
21127
21128       /* If this is an array type, we need to make sure our
21129          kids get marked, even if they're types.  If we're
21130          breaking out types into comdat sections, do this
21131          for all type definitions.  */
21132       if (die->die_tag == DW_TAG_array_type
21133           || (use_debug_types
21134               && is_type_die (die) && ! is_declaration_die (die)))
21135         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21136       else
21137         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21138     }
21139 }
21140
21141 /* For local classes, look if any static member functions were emitted
21142    and if so, mark them.  */
21143
21144 static void
21145 prune_unused_types_walk_local_classes (dw_die_ref die)
21146 {
21147   dw_die_ref c;
21148
21149   if (die->die_mark == 2)
21150     return;
21151
21152   switch (die->die_tag)
21153     {
21154     case DW_TAG_structure_type:
21155     case DW_TAG_union_type:
21156     case DW_TAG_class_type:
21157       break;
21158
21159     case DW_TAG_subprogram:
21160       if (!get_AT_flag (die, DW_AT_declaration)
21161           || die->die_definition != NULL)
21162         prune_unused_types_mark (die, 1);
21163       return;
21164
21165     default:
21166       return;
21167     }
21168
21169   /* Mark children.  */
21170   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21171 }
21172
21173 /* Walk the tree DIE and mark types that we actually use.  */
21174
21175 static void
21176 prune_unused_types_walk (dw_die_ref die)
21177 {
21178   dw_die_ref c;
21179
21180   /* Don't do anything if this node is already marked and
21181      children have been marked as well.  */
21182   if (die->die_mark == 2)
21183     return;
21184
21185   switch (die->die_tag)
21186     {
21187     case DW_TAG_structure_type:
21188     case DW_TAG_union_type:
21189     case DW_TAG_class_type:
21190       if (die->die_perennial_p)
21191         break;
21192
21193       for (c = die->die_parent; c; c = c->die_parent)
21194         if (c->die_tag == DW_TAG_subprogram)
21195           break;
21196
21197       /* Finding used static member functions inside of classes
21198          is needed just for local classes, because for other classes
21199          static member function DIEs with DW_AT_specification
21200          are emitted outside of the DW_TAG_*_type.  If we ever change
21201          it, we'd need to call this even for non-local classes.  */
21202       if (c)
21203         prune_unused_types_walk_local_classes (die);
21204
21205       /* It's a type node --- don't mark it.  */
21206       return;
21207
21208     case DW_TAG_const_type:
21209     case DW_TAG_packed_type:
21210     case DW_TAG_pointer_type:
21211     case DW_TAG_reference_type:
21212     case DW_TAG_rvalue_reference_type:
21213     case DW_TAG_volatile_type:
21214     case DW_TAG_typedef:
21215     case DW_TAG_array_type:
21216     case DW_TAG_interface_type:
21217     case DW_TAG_friend:
21218     case DW_TAG_variant_part:
21219     case DW_TAG_enumeration_type:
21220     case DW_TAG_subroutine_type:
21221     case DW_TAG_string_type:
21222     case DW_TAG_set_type:
21223     case DW_TAG_subrange_type:
21224     case DW_TAG_ptr_to_member_type:
21225     case DW_TAG_file_type:
21226       if (die->die_perennial_p)
21227         break;
21228
21229       /* It's a type node --- don't mark it.  */
21230       return;
21231
21232     default:
21233       /* Mark everything else.  */
21234       break;
21235   }
21236
21237   if (die->die_mark == 0)
21238     {
21239       die->die_mark = 1;
21240
21241       /* Now, mark any dies referenced from here.  */
21242       prune_unused_types_walk_attribs (die);
21243     }
21244
21245   die->die_mark = 2;
21246
21247   /* Mark children.  */
21248   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21249 }
21250
21251 /* Increment the string counts on strings referred to from DIE's
21252    attributes.  */
21253
21254 static void
21255 prune_unused_types_update_strings (dw_die_ref die)
21256 {
21257   dw_attr_ref a;
21258   unsigned ix;
21259
21260   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21261     if (AT_class (a) == dw_val_class_str)
21262       {
21263         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21264         s->refcount++;
21265         /* Avoid unnecessarily putting strings that are used less than
21266            twice in the hash table.  */
21267         if (s->refcount
21268             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21269           {
21270             void ** slot;
21271             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21272                                              htab_hash_string (s->str),
21273                                              INSERT);
21274             gcc_assert (*slot == NULL);
21275             *slot = s;
21276           }
21277       }
21278 }
21279
21280 /* Remove from the tree DIE any dies that aren't marked.  */
21281
21282 static void
21283 prune_unused_types_prune (dw_die_ref die)
21284 {
21285   dw_die_ref c;
21286
21287   gcc_assert (die->die_mark);
21288   prune_unused_types_update_strings (die);
21289
21290   if (! die->die_child)
21291     return;
21292
21293   c = die->die_child;
21294   do {
21295     dw_die_ref prev = c;
21296     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21297       if (c == die->die_child)
21298         {
21299           /* No marked children between 'prev' and the end of the list.  */
21300           if (prev == c)
21301             /* No marked children at all.  */
21302             die->die_child = NULL;
21303           else
21304             {
21305               prev->die_sib = c->die_sib;
21306               die->die_child = prev;
21307             }
21308           return;
21309         }
21310
21311     if (c != prev->die_sib)
21312       prev->die_sib = c;
21313     prune_unused_types_prune (c);
21314   } while (c != die->die_child);
21315 }
21316
21317 /* Remove dies representing declarations that we never use.  */
21318
21319 static void
21320 prune_unused_types (void)
21321 {
21322   unsigned int i;
21323   limbo_die_node *node;
21324   comdat_type_node *ctnode;
21325   pubname_ref pub;
21326   dw_die_ref base_type;
21327
21328 #if ENABLE_ASSERT_CHECKING
21329   /* All the marks should already be clear.  */
21330   verify_marks_clear (comp_unit_die ());
21331   for (node = limbo_die_list; node; node = node->next)
21332     verify_marks_clear (node->die);
21333   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21334     verify_marks_clear (ctnode->root_die);
21335 #endif /* ENABLE_ASSERT_CHECKING */
21336
21337   /* Mark types that are used in global variables.  */
21338   premark_types_used_by_global_vars ();
21339
21340   /* Set the mark on nodes that are actually used.  */
21341   prune_unused_types_walk (comp_unit_die ());
21342   for (node = limbo_die_list; node; node = node->next)
21343     prune_unused_types_walk (node->die);
21344   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21345     {
21346       prune_unused_types_walk (ctnode->root_die);
21347       prune_unused_types_mark (ctnode->type_die, 1);
21348     }
21349
21350   /* Also set the mark on nodes referenced from the
21351      pubname_table.  */
21352   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21353     prune_unused_types_mark (pub->die, 1);
21354   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21355     prune_unused_types_mark (base_type, 1);
21356
21357   if (debug_str_hash)
21358     htab_empty (debug_str_hash);
21359   prune_unused_types_prune (comp_unit_die ());
21360   for (node = limbo_die_list; node; node = node->next)
21361     prune_unused_types_prune (node->die);
21362   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21363     prune_unused_types_prune (ctnode->root_die);
21364
21365   /* Leave the marks clear.  */
21366   prune_unmark_dies (comp_unit_die ());
21367   for (node = limbo_die_list; node; node = node->next)
21368     prune_unmark_dies (node->die);
21369   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21370     prune_unmark_dies (ctnode->root_die);
21371 }
21372
21373 /* Set the parameter to true if there are any relative pathnames in
21374    the file table.  */
21375 static int
21376 file_table_relative_p (void ** slot, void *param)
21377 {
21378   bool *p = (bool *) param;
21379   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21380   if (!IS_ABSOLUTE_PATH (d->filename))
21381     {
21382       *p = true;
21383       return 0;
21384     }
21385   return 1;
21386 }
21387
21388 /* Routines to manipulate hash table of comdat type units.  */
21389
21390 static hashval_t
21391 htab_ct_hash (const void *of)
21392 {
21393   hashval_t h;
21394   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21395
21396   memcpy (&h, type_node->signature, sizeof (h));
21397   return h;
21398 }
21399
21400 static int
21401 htab_ct_eq (const void *of1, const void *of2)
21402 {
21403   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21404   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21405
21406   return (! memcmp (type_node_1->signature, type_node_2->signature,
21407                     DWARF_TYPE_SIGNATURE_SIZE));
21408 }
21409
21410 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21411    to the location it would have been added, should we know its
21412    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21413    probably improve compactness of debug info, removing equivalent
21414    abbrevs, and hide any differences caused by deferring the
21415    computation of the assembler name, triggered by e.g. PCH.  */
21416
21417 static inline void
21418 move_linkage_attr (dw_die_ref die)
21419 {
21420   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21421   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21422
21423   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21424               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21425
21426   while (--ix > 0)
21427     {
21428       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21429
21430       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21431         break;
21432     }
21433
21434   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21435     {
21436       VEC_pop (dw_attr_node, die->die_attr);
21437       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21438     }
21439 }
21440
21441 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21442    referenced from typed stack ops and count how often they are used.  */
21443
21444 static void
21445 mark_base_types (dw_loc_descr_ref loc)
21446 {
21447   dw_die_ref base_type = NULL;
21448
21449   for (; loc; loc = loc->dw_loc_next)
21450     {
21451       switch (loc->dw_loc_opc)
21452         {
21453         case DW_OP_GNU_regval_type:
21454         case DW_OP_GNU_deref_type:
21455           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21456           break;
21457         case DW_OP_GNU_convert:
21458         case DW_OP_GNU_reinterpret:
21459           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21460             continue;
21461           /* FALLTHRU */
21462         case DW_OP_GNU_const_type:
21463           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21464           break;
21465         case DW_OP_GNU_entry_value:
21466           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21467           continue;
21468         default:
21469           continue;
21470         }
21471       gcc_assert (base_type->die_parent == comp_unit_die ());
21472       if (base_type->die_mark)
21473         base_type->die_mark++;
21474       else
21475         {
21476           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21477           base_type->die_mark = 1;
21478         }
21479     }
21480 }
21481
21482 /* Comparison function for sorting marked base types.  */
21483
21484 static int
21485 base_type_cmp (const void *x, const void *y)
21486 {
21487   dw_die_ref dx = *(const dw_die_ref *) x;
21488   dw_die_ref dy = *(const dw_die_ref *) y;
21489   unsigned int byte_size1, byte_size2;
21490   unsigned int encoding1, encoding2;
21491   if (dx->die_mark > dy->die_mark)
21492     return -1;
21493   if (dx->die_mark < dy->die_mark)
21494     return 1;
21495   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21496   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21497   if (byte_size1 < byte_size2)
21498     return 1;
21499   if (byte_size1 > byte_size2)
21500     return -1;
21501   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21502   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21503   if (encoding1 < encoding2)
21504     return 1;
21505   if (encoding1 > encoding2)
21506     return -1;
21507   return 0;
21508 }
21509
21510 /* Move base types marked by mark_base_types as early as possible
21511    in the CU, sorted by decreasing usage count both to make the
21512    uleb128 references as small as possible and to make sure they
21513    will have die_offset already computed by calc_die_sizes when
21514    sizes of typed stack loc ops is computed.  */
21515
21516 static void
21517 move_marked_base_types (void)
21518 {
21519   unsigned int i;
21520   dw_die_ref base_type, die, c;
21521
21522   if (VEC_empty (dw_die_ref, base_types))
21523     return;
21524
21525   /* Sort by decreasing usage count, they will be added again in that
21526      order later on.  */
21527   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21528   die = comp_unit_die ();
21529   c = die->die_child;
21530   do
21531     {
21532       dw_die_ref prev = c;
21533       c = c->die_sib;
21534       while (c->die_mark)
21535         {
21536           remove_child_with_prev (c, prev);
21537           /* As base types got marked, there must be at least
21538              one node other than DW_TAG_base_type.  */
21539           gcc_assert (c != c->die_sib);
21540           c = c->die_sib;
21541         }
21542     }
21543   while (c != die->die_child);
21544   gcc_assert (die->die_child);
21545   c = die->die_child;
21546   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21547     {
21548       base_type->die_mark = 0;
21549       base_type->die_sib = c->die_sib;
21550       c->die_sib = base_type;
21551       c = base_type;
21552     }
21553 }
21554
21555 /* Helper function for resolve_addr, attempt to resolve
21556    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21557    SYMBOL_REFs refer to variables emitted in the current CU.  */
21558
21559 static int
21560 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21561 {
21562   rtx rtl = *addr;
21563
21564   if (GET_CODE (rtl) == CONST_STRING)
21565     {
21566       size_t len = strlen (XSTR (rtl, 0)) + 1;
21567       tree t = build_string (len, XSTR (rtl, 0));
21568       tree tlen = size_int (len - 1);
21569       TREE_TYPE (t)
21570         = build_array_type (char_type_node, build_index_type (tlen));
21571       rtl = lookup_constant_def (t);
21572       if (!rtl || !MEM_P (rtl))
21573         return 1;
21574       rtl = XEXP (rtl, 0);
21575       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21576       *addr = rtl;
21577       return 0;
21578     }
21579
21580   if (GET_CODE (rtl) == SYMBOL_REF
21581       && SYMBOL_REF_DECL (rtl))
21582     {
21583       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21584         {
21585           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21586             return 1;
21587         }
21588       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21589         return 1;
21590     }
21591
21592   if (GET_CODE (rtl) == CONST
21593       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21594     return 1;
21595
21596   return 0;
21597 }
21598
21599 /* Helper function for resolve_addr, handle one location
21600    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21601    the location list couldn't be resolved.  */
21602
21603 static bool
21604 resolve_addr_in_expr (dw_loc_descr_ref loc)
21605 {
21606   dw_loc_descr_ref keep = NULL;
21607   for (; loc; loc = loc->dw_loc_next)
21608     switch (loc->dw_loc_opc)
21609       {
21610       case DW_OP_addr:
21611         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21612           return false;
21613         break;
21614       case DW_OP_const4u:
21615       case DW_OP_const8u:
21616         if (loc->dtprel
21617             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21618           return false;
21619         break;
21620       case DW_OP_implicit_value:
21621         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21622             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21623           return false;
21624         break;
21625       case DW_OP_GNU_implicit_pointer:
21626       case DW_OP_GNU_parameter_ref:
21627         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21628           {
21629             dw_die_ref ref
21630               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21631             if (ref == NULL)
21632               return false;
21633             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21634             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21635             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21636           }
21637         break;
21638       case DW_OP_GNU_const_type:
21639       case DW_OP_GNU_regval_type:
21640       case DW_OP_GNU_deref_type:
21641       case DW_OP_GNU_convert:
21642       case DW_OP_GNU_reinterpret:
21643         while (loc->dw_loc_next
21644                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21645           {
21646             dw_die_ref base1, base2;
21647             unsigned enc1, enc2, size1, size2;
21648             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21649                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21650               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21651             else if (loc->dw_loc_oprnd1.val_class
21652                      == dw_val_class_unsigned_const)
21653               break;
21654             else
21655               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21656             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21657                 == dw_val_class_unsigned_const)
21658               break;
21659             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21660             gcc_assert (base1->die_tag == DW_TAG_base_type
21661                         && base2->die_tag == DW_TAG_base_type);
21662             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21663             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21664             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21665             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21666             if (size1 == size2
21667                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21668                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21669                      && loc != keep)
21670                     || enc1 == enc2))
21671               {
21672                 /* Optimize away next DW_OP_GNU_convert after
21673                    adjusting LOC's base type die reference.  */
21674                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21675                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21676                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21677                 else
21678                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21679                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21680                 continue;
21681               }
21682             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21683                point typed stack entry.  */
21684             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21685               keep = loc->dw_loc_next;
21686             break;
21687           }
21688         break;
21689       default:
21690         break;
21691       }
21692   return true;
21693 }
21694
21695 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21696    an address in .rodata section if the string literal is emitted there,
21697    or remove the containing location list or replace DW_AT_const_value
21698    with DW_AT_location and empty location expression, if it isn't found
21699    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21700    to something that has been emitted in the current CU.  */
21701
21702 static void
21703 resolve_addr (dw_die_ref die)
21704 {
21705   dw_die_ref c;
21706   dw_attr_ref a;
21707   dw_loc_list_ref *curr, *start, loc;
21708   unsigned ix;
21709
21710   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21711     switch (AT_class (a))
21712       {
21713       case dw_val_class_loc_list:
21714         start = curr = AT_loc_list_ptr (a);
21715         loc = *curr;
21716         gcc_assert (loc);
21717         /* The same list can be referenced more than once.  See if we have
21718            already recorded the result from a previous pass.  */
21719         if (loc->replaced)
21720           *curr = loc->dw_loc_next;
21721         else if (!loc->resolved_addr)
21722           {
21723             /* As things stand, we do not expect or allow one die to
21724                reference a suffix of another die's location list chain.
21725                References must be identical or completely separate.
21726                There is therefore no need to cache the result of this
21727                pass on any list other than the first; doing so
21728                would lead to unnecessary writes.  */
21729             while (*curr)
21730               {
21731                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21732                 if (!resolve_addr_in_expr ((*curr)->expr))
21733                   {
21734                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21735                     if (next && (*curr)->ll_symbol)
21736                       {
21737                         gcc_assert (!next->ll_symbol);
21738                         next->ll_symbol = (*curr)->ll_symbol;
21739                       }
21740                     *curr = next;
21741                   }
21742                 else
21743                   {
21744                     mark_base_types ((*curr)->expr);
21745                     curr = &(*curr)->dw_loc_next;
21746                   }
21747               }
21748             if (loc == *start)
21749               loc->resolved_addr = 1;
21750             else
21751               {
21752                 loc->replaced = 1;
21753                 loc->dw_loc_next = *start;
21754               }
21755           }
21756         if (!*start)
21757           {
21758             remove_AT (die, a->dw_attr);
21759             ix--;
21760           }
21761         break;
21762       case dw_val_class_loc:
21763         if (!resolve_addr_in_expr (AT_loc (a)))
21764           {
21765             remove_AT (die, a->dw_attr);
21766             ix--;
21767           }
21768         else
21769           mark_base_types (AT_loc (a));
21770         break;
21771       case dw_val_class_addr:
21772         if (a->dw_attr == DW_AT_const_value
21773             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21774           {
21775             remove_AT (die, a->dw_attr);
21776             ix--;
21777           }
21778         if (die->die_tag == DW_TAG_GNU_call_site
21779             && a->dw_attr == DW_AT_abstract_origin)
21780           {
21781             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21782             dw_die_ref tdie = lookup_decl_die (tdecl);
21783             if (tdie == NULL
21784                 && DECL_EXTERNAL (tdecl)
21785                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21786               {
21787                 force_decl_die (tdecl);
21788                 tdie = lookup_decl_die (tdecl);
21789               }
21790             if (tdie)
21791               {
21792                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21793                 a->dw_attr_val.v.val_die_ref.die = tdie;
21794                 a->dw_attr_val.v.val_die_ref.external = 0;
21795               }
21796             else
21797               {
21798                 remove_AT (die, a->dw_attr);
21799                 ix--;
21800               }
21801           }
21802         break;
21803       default:
21804         break;
21805       }
21806
21807   FOR_EACH_CHILD (die, c, resolve_addr (c));
21808 }
21809 \f
21810 /* Helper routines for optimize_location_lists.
21811    This pass tries to share identical local lists in .debug_loc
21812    section.  */
21813
21814 /* Iteratively hash operands of LOC opcode.  */
21815
21816 static inline hashval_t
21817 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21818 {
21819   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21820   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21821
21822   switch (loc->dw_loc_opc)
21823     {
21824     case DW_OP_const4u:
21825     case DW_OP_const8u:
21826       if (loc->dtprel)
21827         goto hash_addr;
21828       /* FALLTHRU */
21829     case DW_OP_const1u:
21830     case DW_OP_const1s:
21831     case DW_OP_const2u:
21832     case DW_OP_const2s:
21833     case DW_OP_const4s:
21834     case DW_OP_const8s:
21835     case DW_OP_constu:
21836     case DW_OP_consts:
21837     case DW_OP_pick:
21838     case DW_OP_plus_uconst:
21839     case DW_OP_breg0:
21840     case DW_OP_breg1:
21841     case DW_OP_breg2:
21842     case DW_OP_breg3:
21843     case DW_OP_breg4:
21844     case DW_OP_breg5:
21845     case DW_OP_breg6:
21846     case DW_OP_breg7:
21847     case DW_OP_breg8:
21848     case DW_OP_breg9:
21849     case DW_OP_breg10:
21850     case DW_OP_breg11:
21851     case DW_OP_breg12:
21852     case DW_OP_breg13:
21853     case DW_OP_breg14:
21854     case DW_OP_breg15:
21855     case DW_OP_breg16:
21856     case DW_OP_breg17:
21857     case DW_OP_breg18:
21858     case DW_OP_breg19:
21859     case DW_OP_breg20:
21860     case DW_OP_breg21:
21861     case DW_OP_breg22:
21862     case DW_OP_breg23:
21863     case DW_OP_breg24:
21864     case DW_OP_breg25:
21865     case DW_OP_breg26:
21866     case DW_OP_breg27:
21867     case DW_OP_breg28:
21868     case DW_OP_breg29:
21869     case DW_OP_breg30:
21870     case DW_OP_breg31:
21871     case DW_OP_regx:
21872     case DW_OP_fbreg:
21873     case DW_OP_piece:
21874     case DW_OP_deref_size:
21875     case DW_OP_xderef_size:
21876       hash = iterative_hash_object (val1->v.val_int, hash);
21877       break;
21878     case DW_OP_skip:
21879     case DW_OP_bra:
21880       {
21881         int offset;
21882
21883         gcc_assert (val1->val_class == dw_val_class_loc);
21884         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21885         hash = iterative_hash_object (offset, hash);
21886       }
21887       break;
21888     case DW_OP_implicit_value:
21889       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21890       switch (val2->val_class)
21891         {
21892         case dw_val_class_const:
21893           hash = iterative_hash_object (val2->v.val_int, hash);
21894           break;
21895         case dw_val_class_vec:
21896           {
21897             unsigned int elt_size = val2->v.val_vec.elt_size;
21898             unsigned int len = val2->v.val_vec.length;
21899
21900             hash = iterative_hash_object (elt_size, hash);
21901             hash = iterative_hash_object (len, hash);
21902             hash = iterative_hash (val2->v.val_vec.array,
21903                                    len * elt_size, hash);
21904           }
21905           break;
21906         case dw_val_class_const_double:
21907           hash = iterative_hash_object (val2->v.val_double.low, hash);
21908           hash = iterative_hash_object (val2->v.val_double.high, hash);
21909           break;
21910         case dw_val_class_addr:
21911           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21912           break;
21913         default:
21914           gcc_unreachable ();
21915         }
21916       break;
21917     case DW_OP_bregx:
21918     case DW_OP_bit_piece:
21919       hash = iterative_hash_object (val1->v.val_int, hash);
21920       hash = iterative_hash_object (val2->v.val_int, hash);
21921       break;
21922     case DW_OP_addr:
21923     hash_addr:
21924       if (loc->dtprel)
21925         {
21926           unsigned char dtprel = 0xd1;
21927           hash = iterative_hash_object (dtprel, hash);
21928         }
21929       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21930       break;
21931     case DW_OP_GNU_implicit_pointer:
21932       hash = iterative_hash_object (val2->v.val_int, hash);
21933       break;
21934     case DW_OP_GNU_entry_value:
21935       hash = hash_loc_operands (val1->v.val_loc, hash);
21936       break;
21937     case DW_OP_GNU_regval_type:
21938     case DW_OP_GNU_deref_type:
21939       {
21940         unsigned int byte_size
21941           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21942         unsigned int encoding
21943           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21944         hash = iterative_hash_object (val1->v.val_int, hash);
21945         hash = iterative_hash_object (byte_size, hash);
21946         hash = iterative_hash_object (encoding, hash);
21947       }
21948       break;
21949     case DW_OP_GNU_convert:
21950     case DW_OP_GNU_reinterpret:
21951       if (val1->val_class == dw_val_class_unsigned_const)
21952         {
21953           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21954           break;
21955         }
21956       /* FALLTHRU */
21957     case DW_OP_GNU_const_type:
21958       {
21959         unsigned int byte_size
21960           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21961         unsigned int encoding
21962           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21963         hash = iterative_hash_object (byte_size, hash);
21964         hash = iterative_hash_object (encoding, hash);
21965         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21966           break;
21967         hash = iterative_hash_object (val2->val_class, hash);
21968         switch (val2->val_class)
21969           {
21970           case dw_val_class_const:
21971             hash = iterative_hash_object (val2->v.val_int, hash);
21972             break;
21973           case dw_val_class_vec:
21974             {
21975               unsigned int elt_size = val2->v.val_vec.elt_size;
21976               unsigned int len = val2->v.val_vec.length;
21977
21978               hash = iterative_hash_object (elt_size, hash);
21979               hash = iterative_hash_object (len, hash);
21980               hash = iterative_hash (val2->v.val_vec.array,
21981                                      len * elt_size, hash);
21982             }
21983             break;
21984           case dw_val_class_const_double:
21985             hash = iterative_hash_object (val2->v.val_double.low, hash);
21986             hash = iterative_hash_object (val2->v.val_double.high, hash);
21987             break;
21988           default:
21989             gcc_unreachable ();
21990           }
21991       }
21992       break;
21993
21994     default:
21995       /* Other codes have no operands.  */
21996       break;
21997     }
21998   return hash;
21999 }
22000
22001 /* Iteratively hash the whole DWARF location expression LOC.  */
22002
22003 static inline hashval_t
22004 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22005 {
22006   dw_loc_descr_ref l;
22007   bool sizes_computed = false;
22008   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22009   size_of_locs (loc);
22010
22011   for (l = loc; l != NULL; l = l->dw_loc_next)
22012     {
22013       enum dwarf_location_atom opc = l->dw_loc_opc;
22014       hash = iterative_hash_object (opc, hash);
22015       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22016         {
22017           size_of_locs (loc);
22018           sizes_computed = true;
22019         }
22020       hash = hash_loc_operands (l, hash);
22021     }
22022   return hash;
22023 }
22024
22025 /* Compute hash of the whole location list LIST_HEAD.  */
22026
22027 static inline void
22028 hash_loc_list (dw_loc_list_ref list_head)
22029 {
22030   dw_loc_list_ref curr = list_head;
22031   hashval_t hash = 0;
22032
22033   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22034     {
22035       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22036       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22037       if (curr->section)
22038         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22039                                hash);
22040       hash = hash_locs (curr->expr, hash);
22041     }
22042   list_head->hash = hash;
22043 }
22044
22045 /* Return true if X and Y opcodes have the same operands.  */
22046
22047 static inline bool
22048 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22049 {
22050   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22051   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22052   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22053   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22054
22055   switch (x->dw_loc_opc)
22056     {
22057     case DW_OP_const4u:
22058     case DW_OP_const8u:
22059       if (x->dtprel)
22060         goto hash_addr;
22061       /* FALLTHRU */
22062     case DW_OP_const1u:
22063     case DW_OP_const1s:
22064     case DW_OP_const2u:
22065     case DW_OP_const2s:
22066     case DW_OP_const4s:
22067     case DW_OP_const8s:
22068     case DW_OP_constu:
22069     case DW_OP_consts:
22070     case DW_OP_pick:
22071     case DW_OP_plus_uconst:
22072     case DW_OP_breg0:
22073     case DW_OP_breg1:
22074     case DW_OP_breg2:
22075     case DW_OP_breg3:
22076     case DW_OP_breg4:
22077     case DW_OP_breg5:
22078     case DW_OP_breg6:
22079     case DW_OP_breg7:
22080     case DW_OP_breg8:
22081     case DW_OP_breg9:
22082     case DW_OP_breg10:
22083     case DW_OP_breg11:
22084     case DW_OP_breg12:
22085     case DW_OP_breg13:
22086     case DW_OP_breg14:
22087     case DW_OP_breg15:
22088     case DW_OP_breg16:
22089     case DW_OP_breg17:
22090     case DW_OP_breg18:
22091     case DW_OP_breg19:
22092     case DW_OP_breg20:
22093     case DW_OP_breg21:
22094     case DW_OP_breg22:
22095     case DW_OP_breg23:
22096     case DW_OP_breg24:
22097     case DW_OP_breg25:
22098     case DW_OP_breg26:
22099     case DW_OP_breg27:
22100     case DW_OP_breg28:
22101     case DW_OP_breg29:
22102     case DW_OP_breg30:
22103     case DW_OP_breg31:
22104     case DW_OP_regx:
22105     case DW_OP_fbreg:
22106     case DW_OP_piece:
22107     case DW_OP_deref_size:
22108     case DW_OP_xderef_size:
22109       return valx1->v.val_int == valy1->v.val_int;
22110     case DW_OP_skip:
22111     case DW_OP_bra:
22112       gcc_assert (valx1->val_class == dw_val_class_loc
22113                   && valy1->val_class == dw_val_class_loc
22114                   && x->dw_loc_addr == y->dw_loc_addr);
22115       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22116     case DW_OP_implicit_value:
22117       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22118           || valx2->val_class != valy2->val_class)
22119         return false;
22120       switch (valx2->val_class)
22121         {
22122         case dw_val_class_const:
22123           return valx2->v.val_int == valy2->v.val_int;
22124         case dw_val_class_vec:
22125           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22126                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22127                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22128                             valx2->v.val_vec.elt_size
22129                             * valx2->v.val_vec.length) == 0;
22130         case dw_val_class_const_double:
22131           return valx2->v.val_double.low == valy2->v.val_double.low
22132                  && valx2->v.val_double.high == valy2->v.val_double.high;
22133         case dw_val_class_addr:
22134           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22135         default:
22136           gcc_unreachable ();
22137         }
22138     case DW_OP_bregx:
22139     case DW_OP_bit_piece:
22140       return valx1->v.val_int == valy1->v.val_int
22141              && valx2->v.val_int == valy2->v.val_int;
22142     case DW_OP_addr:
22143     hash_addr:
22144       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22145     case DW_OP_GNU_implicit_pointer:
22146       return valx1->val_class == dw_val_class_die_ref
22147              && valx1->val_class == valy1->val_class
22148              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22149              && valx2->v.val_int == valy2->v.val_int;
22150     case DW_OP_GNU_entry_value:
22151       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22152     case DW_OP_GNU_const_type:
22153       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22154           || valx2->val_class != valy2->val_class)
22155         return false;
22156       switch (valx2->val_class)
22157         {
22158         case dw_val_class_const:
22159           return valx2->v.val_int == valy2->v.val_int;
22160         case dw_val_class_vec:
22161           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22162                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22163                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22164                             valx2->v.val_vec.elt_size
22165                             * valx2->v.val_vec.length) == 0;
22166         case dw_val_class_const_double:
22167           return valx2->v.val_double.low == valy2->v.val_double.low
22168                  && valx2->v.val_double.high == valy2->v.val_double.high;
22169         default:
22170           gcc_unreachable ();
22171         }
22172     case DW_OP_GNU_regval_type:
22173     case DW_OP_GNU_deref_type:
22174       return valx1->v.val_int == valy1->v.val_int
22175              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22176     case DW_OP_GNU_convert:
22177     case DW_OP_GNU_reinterpret:
22178       if (valx1->val_class != valy1->val_class)
22179         return false;
22180       if (valx1->val_class == dw_val_class_unsigned_const)
22181         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22182       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22183     case DW_OP_GNU_parameter_ref:
22184       return valx1->val_class == dw_val_class_die_ref
22185              && valx1->val_class == valy1->val_class
22186              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22187     default:
22188       /* Other codes have no operands.  */
22189       return true;
22190     }
22191 }
22192
22193 /* Return true if DWARF location expressions X and Y are the same.  */
22194
22195 static inline bool
22196 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22197 {
22198   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22199     if (x->dw_loc_opc != y->dw_loc_opc
22200         || x->dtprel != y->dtprel
22201         || !compare_loc_operands (x, y))
22202       break;
22203   return x == NULL && y == NULL;
22204 }
22205
22206 /* Return precomputed hash of location list X.  */
22207
22208 static hashval_t
22209 loc_list_hash (const void *x)
22210 {
22211   return ((const struct dw_loc_list_struct *) x)->hash;
22212 }
22213
22214 /* Return 1 if location lists X and Y are the same.  */
22215
22216 static int
22217 loc_list_eq (const void *x, const void *y)
22218 {
22219   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22220   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22221   if (a == b)
22222     return 1;
22223   if (a->hash != b->hash)
22224     return 0;
22225   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22226     if (strcmp (a->begin, b->begin) != 0
22227         || strcmp (a->end, b->end) != 0
22228         || (a->section == NULL) != (b->section == NULL)
22229         || (a->section && strcmp (a->section, b->section) != 0)
22230         || !compare_locs (a->expr, b->expr))
22231       break;
22232   return a == NULL && b == NULL;
22233 }
22234
22235 /* Recursively optimize location lists referenced from DIE
22236    children and share them whenever possible.  */
22237
22238 static void
22239 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22240 {
22241   dw_die_ref c;
22242   dw_attr_ref a;
22243   unsigned ix;
22244   void **slot;
22245
22246   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22247     if (AT_class (a) == dw_val_class_loc_list)
22248       {
22249         dw_loc_list_ref list = AT_loc_list (a);
22250         /* TODO: perform some optimizations here, before hashing
22251            it and storing into the hash table.  */
22252         hash_loc_list (list);
22253         slot = htab_find_slot_with_hash (htab, list, list->hash,
22254                                          INSERT);
22255         if (*slot == NULL)
22256           *slot = (void *) list;
22257         else
22258           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22259       }
22260
22261   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22262 }
22263
22264 /* Optimize location lists referenced from DIE
22265    children and share them whenever possible.  */
22266
22267 static void
22268 optimize_location_lists (dw_die_ref die)
22269 {
22270   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22271   optimize_location_lists_1 (die, htab);
22272   htab_delete (htab);
22273 }
22274 \f
22275 /* Output stuff that dwarf requires at the end of every file,
22276    and generate the DWARF-2 debugging info.  */
22277
22278 static void
22279 dwarf2out_finish (const char *filename)
22280 {
22281   limbo_die_node *node, *next_node;
22282   comdat_type_node *ctnode;
22283   htab_t comdat_type_table;
22284   unsigned int i;
22285
22286   gen_scheduled_generic_parms_dies ();
22287   gen_remaining_tmpl_value_param_die_attribute ();
22288
22289   /* Add the name for the main input file now.  We delayed this from
22290      dwarf2out_init to avoid complications with PCH.  */
22291   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22292   if (!IS_ABSOLUTE_PATH (filename))
22293     add_comp_dir_attribute (comp_unit_die ());
22294   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22295     {
22296       bool p = false;
22297       htab_traverse (file_table, file_table_relative_p, &p);
22298       if (p)
22299         add_comp_dir_attribute (comp_unit_die ());
22300     }
22301
22302   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22303     {
22304       add_location_or_const_value_attribute (
22305         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22306         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22307         false,
22308         DW_AT_location);
22309     }
22310
22311   /* Traverse the limbo die list, and add parent/child links.  The only
22312      dies without parents that should be here are concrete instances of
22313      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22314      For concrete instances, we can get the parent die from the abstract
22315      instance.  */
22316   for (node = limbo_die_list; node; node = next_node)
22317     {
22318       dw_die_ref die = node->die;
22319       next_node = node->next;
22320
22321       if (die->die_parent == NULL)
22322         {
22323           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22324
22325           if (origin && origin->die_parent)
22326             add_child_die (origin->die_parent, die);
22327           else if (is_cu_die (die))
22328             ;
22329           else if (seen_error ())
22330             /* It's OK to be confused by errors in the input.  */
22331             add_child_die (comp_unit_die (), die);
22332           else
22333             {
22334               /* In certain situations, the lexical block containing a
22335                  nested function can be optimized away, which results
22336                  in the nested function die being orphaned.  Likewise
22337                  with the return type of that nested function.  Force
22338                  this to be a child of the containing function.
22339
22340                  It may happen that even the containing function got fully
22341                  inlined and optimized out.  In that case we are lost and
22342                  assign the empty child.  This should not be big issue as
22343                  the function is likely unreachable too.  */
22344               tree context = NULL_TREE;
22345
22346               gcc_assert (node->created_for);
22347
22348               if (DECL_P (node->created_for))
22349                 context = DECL_CONTEXT (node->created_for);
22350               else if (TYPE_P (node->created_for))
22351                 context = TYPE_CONTEXT (node->created_for);
22352
22353               gcc_assert (context
22354                           && (TREE_CODE (context) == FUNCTION_DECL
22355                               || TREE_CODE (context) == NAMESPACE_DECL));
22356
22357               origin = lookup_decl_die (context);
22358               if (origin)
22359                 add_child_die (origin, die);
22360               else
22361                 add_child_die (comp_unit_die (), die);
22362             }
22363         }
22364     }
22365
22366   limbo_die_list = NULL;
22367
22368 #if ENABLE_ASSERT_CHECKING
22369   {
22370     dw_die_ref die = comp_unit_die (), c;
22371     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22372   }
22373 #endif
22374   resolve_addr (comp_unit_die ());
22375   move_marked_base_types ();
22376
22377   for (node = deferred_asm_name; node; node = node->next)
22378     {
22379       tree decl = node->created_for;
22380       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22381         {
22382           add_linkage_attr (node->die, decl);
22383           move_linkage_attr (node->die);
22384         }
22385     }
22386
22387   deferred_asm_name = NULL;
22388
22389   /* Walk through the list of incomplete types again, trying once more to
22390      emit full debugging info for them.  */
22391   retry_incomplete_types ();
22392
22393   if (flag_eliminate_unused_debug_types)
22394     prune_unused_types ();
22395
22396   /* Generate separate CUs for each of the include files we've seen.
22397      They will go into limbo_die_list.  */
22398   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22399     break_out_includes (comp_unit_die ());
22400
22401   /* Generate separate COMDAT sections for type DIEs. */
22402   if (use_debug_types)
22403     {
22404       break_out_comdat_types (comp_unit_die ());
22405
22406       /* Each new type_unit DIE was added to the limbo die list when created.
22407          Since these have all been added to comdat_type_list, clear the
22408          limbo die list.  */
22409       limbo_die_list = NULL;
22410
22411       /* For each new comdat type unit, copy declarations for incomplete
22412          types to make the new unit self-contained (i.e., no direct
22413          references to the main compile unit).  */
22414       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22415         copy_decls_for_unworthy_types (ctnode->root_die);
22416       copy_decls_for_unworthy_types (comp_unit_die ());
22417
22418       /* In the process of copying declarations from one unit to another,
22419          we may have left some declarations behind that are no longer
22420          referenced.  Prune them.  */
22421       prune_unused_types ();
22422     }
22423
22424   /* Traverse the DIE's and add add sibling attributes to those DIE's
22425      that have children.  */
22426   add_sibling_attributes (comp_unit_die ());
22427   for (node = limbo_die_list; node; node = node->next)
22428     add_sibling_attributes (node->die);
22429   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22430     add_sibling_attributes (ctnode->root_die);
22431
22432   /* Output a terminator label for the .text section.  */
22433   switch_to_section (text_section);
22434   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22435   if (cold_text_section)
22436     {
22437       switch_to_section (cold_text_section);
22438       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22439     }
22440
22441   /* We can only use the low/high_pc attributes if all of the code was
22442      in .text.  */
22443   if (!have_multiple_function_sections 
22444       || (dwarf_version < 3 && dwarf_strict))
22445     {
22446       /* Don't add if the CU has no associated code.  */
22447       if (text_section_used)
22448         {
22449           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22450           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22451         }
22452     }
22453   else
22454     {
22455       unsigned fde_idx = 0;
22456       bool range_list_added = false;
22457
22458       if (text_section_used)
22459         add_ranges_by_labels (comp_unit_die (), text_section_label,
22460                               text_end_label, &range_list_added);
22461       if (cold_text_section_used)
22462         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22463                               cold_end_label, &range_list_added);
22464
22465       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22466         {
22467           dw_fde_ref fde = &fde_table[fde_idx];
22468
22469           if (!fde->in_std_section)
22470             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22471                                   fde->dw_fde_end, &range_list_added);
22472           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22473             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22474                                   fde->dw_fde_second_end, &range_list_added);
22475         }
22476
22477       if (range_list_added)
22478         {
22479           /* We need to give .debug_loc and .debug_ranges an appropriate
22480              "base address".  Use zero so that these addresses become
22481              absolute.  Historically, we've emitted the unexpected
22482              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22483              Emit both to give time for other tools to adapt.  */
22484           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22485           if (! dwarf_strict && dwarf_version < 4)
22486             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22487
22488           add_ranges (NULL);
22489         }
22490     }
22491
22492   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22493     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22494                     debug_line_section_label);
22495
22496   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22497     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
22498
22499   if (have_location_lists)
22500     optimize_location_lists (comp_unit_die ());
22501
22502   /* Output all of the compilation units.  We put the main one last so that
22503      the offsets are available to output_pubnames.  */
22504   for (node = limbo_die_list; node; node = node->next)
22505     output_comp_unit (node->die, 0);
22506
22507   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22508   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22509     {
22510       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22511
22512       /* Don't output duplicate types.  */
22513       if (*slot != HTAB_EMPTY_ENTRY)
22514         continue;
22515
22516       /* Add a pointer to the line table for the main compilation unit
22517          so that the debugger can make sense of DW_AT_decl_file
22518          attributes.  */
22519       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22520         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22521                         debug_line_section_label);
22522
22523       output_comdat_type_unit (ctnode);
22524       *slot = ctnode;
22525     }
22526   htab_delete (comdat_type_table);
22527
22528   /* Output the main compilation unit if non-empty or if .debug_macinfo
22529      will be emitted.  */
22530   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22531
22532   /* Output the abbreviation table.  */
22533   if (abbrev_die_table_in_use != 1)
22534     {
22535       switch_to_section (debug_abbrev_section);
22536       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22537       output_abbrev_section ();
22538     }
22539
22540   /* Output location list section if necessary.  */
22541   if (have_location_lists)
22542     {
22543       /* Output the location lists info.  */
22544       switch_to_section (debug_loc_section);
22545       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22546                                    DEBUG_LOC_SECTION_LABEL, 0);
22547       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22548       output_location_lists (comp_unit_die ());
22549     }
22550
22551   /* Output public names table if necessary.  */
22552   if (!VEC_empty (pubname_entry, pubname_table))
22553     {
22554       gcc_assert (info_section_emitted);
22555       switch_to_section (debug_pubnames_section);
22556       output_pubnames (pubname_table);
22557     }
22558
22559   /* Output public types table if necessary.  */
22560   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22561      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22562      simply won't look for the section.  */
22563   if (!VEC_empty (pubname_entry, pubtype_table))
22564     {
22565       bool empty = false;
22566       
22567       if (flag_eliminate_unused_debug_types)
22568         {
22569           /* The pubtypes table might be emptied by pruning unused items.  */
22570           unsigned i;
22571           pubname_ref p;
22572           empty = true;
22573           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22574             if (p->die->die_offset != 0)
22575               {
22576                 empty = false;
22577                 break;
22578               }
22579         }
22580       if (!empty)
22581         {
22582           gcc_assert (info_section_emitted);
22583           switch_to_section (debug_pubtypes_section);
22584           output_pubnames (pubtype_table);
22585         }
22586     }
22587
22588   /* Output the address range information if a CU (.debug_info section)
22589      was emitted.  We output an empty table even if we had no functions
22590      to put in it.  This because the consumer has no way to tell the
22591      difference between an empty table that we omitted and failure to
22592      generate a table that would have contained data.  */
22593   if (info_section_emitted)
22594     {
22595       unsigned long aranges_length = size_of_aranges ();
22596
22597       switch_to_section (debug_aranges_section);
22598       output_aranges (aranges_length);
22599     }
22600
22601   /* Output ranges section if necessary.  */
22602   if (ranges_table_in_use)
22603     {
22604       switch_to_section (debug_ranges_section);
22605       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22606       output_ranges ();
22607     }
22608
22609   /* Output the source line correspondence table.  We must do this
22610      even if there is no line information.  Otherwise, on an empty
22611      translation unit, we will generate a present, but empty,
22612      .debug_info section.  IRIX 6.5 `nm' will then complain when
22613      examining the file.  This is done late so that any filenames
22614      used by the debug_info section are marked as 'used'.  */
22615   switch_to_section (debug_line_section);
22616   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22617   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22618     output_line_info ();
22619
22620   /* Have to end the macro section.  */
22621   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22622     {
22623       switch_to_section (debug_macinfo_section);
22624       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22625       if (!VEC_empty (macinfo_entry, macinfo_table))
22626         output_macinfo ();
22627       dw2_asm_output_data (1, 0, "End compilation unit");
22628     }
22629
22630   /* If we emitted any DW_FORM_strp form attribute, output the string
22631      table too.  */
22632   if (debug_str_hash)
22633     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22634 }
22635
22636 #include "gt-dwarf2out.h"