OSDN Git Service

0f6918d0e9f542e39c88c2f80e1818ef009242c0
[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 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 /* Output CFIs from VEC, up to index UPTO, to bring current FDE to the
946    same state as after executing CFIs in CFI chain.  DO_CFI_ASM is
947    true if .cfi_* directives shall be emitted, false otherwise.  If it
948    is false, FDE and FOR_EH are the other arguments to pass to
949    output_cfi.  */
950
951 static void
952 output_cfis (cfi_vec vec, int upto, bool do_cfi_asm,
953              dw_fde_ref fde, bool for_eh)
954 {
955   int ix;
956   struct dw_cfi_struct cfi_buf;
957   dw_cfi_ref cfi2;
958   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
959   VEC(dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
960   unsigned int len, idx;
961
962   for (ix = 0; ix < upto + 1; ix++)
963     {
964       dw_cfi_ref cfi = ix < upto ? VEC_index (dw_cfi_ref, vec, ix) : NULL;
965       switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
966         {
967         case DW_CFA_advance_loc:
968         case DW_CFA_advance_loc1:
969         case DW_CFA_advance_loc2:
970         case DW_CFA_advance_loc4:
971         case DW_CFA_MIPS_advance_loc8:
972         case DW_CFA_set_loc:
973           /* All advances should be ignored.  */
974           break;
975         case DW_CFA_remember_state:
976           {
977             dw_cfi_ref args_size = cfi_args_size;
978
979             /* Skip everything between .cfi_remember_state and
980                .cfi_restore_state.  */
981             ix++;
982             if (ix == upto)
983               goto flush_all;
984
985             for (; ix < upto; ix++)
986               {
987                 cfi2 = VEC_index (dw_cfi_ref, vec, ix);
988                 if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
989                   break;
990                 else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
991                   args_size = cfi2;
992                 else
993                   gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
994               }
995
996             cfi_args_size = args_size;
997             break;
998           }
999         case DW_CFA_GNU_args_size:
1000           cfi_args_size = cfi;
1001           break;
1002         case DW_CFA_GNU_window_save:
1003           goto flush_all;
1004         case DW_CFA_offset:
1005         case DW_CFA_offset_extended:
1006         case DW_CFA_offset_extended_sf:
1007         case DW_CFA_restore:
1008         case DW_CFA_restore_extended:
1009         case DW_CFA_undefined:
1010         case DW_CFA_same_value:
1011         case DW_CFA_register:
1012         case DW_CFA_val_offset:
1013         case DW_CFA_val_offset_sf:
1014         case DW_CFA_expression:
1015         case DW_CFA_val_expression:
1016         case DW_CFA_GNU_negative_offset_extended:
1017           if (VEC_length (dw_cfi_ref, regs)
1018               <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
1019             VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
1020                                    cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
1021           VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num,
1022                        cfi);
1023           break;
1024         case DW_CFA_def_cfa:
1025         case DW_CFA_def_cfa_sf:
1026         case DW_CFA_def_cfa_expression:
1027           cfi_cfa = cfi;
1028           cfi_cfa_offset = cfi;
1029           break;
1030         case DW_CFA_def_cfa_register:
1031           cfi_cfa = cfi;
1032           break;
1033         case DW_CFA_def_cfa_offset:
1034         case DW_CFA_def_cfa_offset_sf:
1035           cfi_cfa_offset = cfi;
1036           break;
1037         case DW_CFA_nop:
1038           gcc_assert (cfi == NULL);
1039         flush_all:
1040           len = VEC_length (dw_cfi_ref, regs);
1041           for (idx = 0; idx < len; idx++)
1042             {
1043               cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
1044               if (cfi2 != NULL
1045                   && cfi2->dw_cfi_opc != DW_CFA_restore
1046                   && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
1047                 {
1048                   if (do_cfi_asm)
1049                     output_cfi_directive (cfi2);
1050                   else
1051                     output_cfi (cfi2, fde, for_eh);
1052                 }
1053             }
1054           if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
1055             {
1056               gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
1057               cfi_buf = *cfi_cfa;
1058               switch (cfi_cfa_offset->dw_cfi_opc)
1059                 {
1060                 case DW_CFA_def_cfa_offset:
1061                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
1062                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
1063                   break;
1064                 case DW_CFA_def_cfa_offset_sf:
1065                   cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
1066                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
1067                   break;
1068                 case DW_CFA_def_cfa:
1069                 case DW_CFA_def_cfa_sf:
1070                   cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
1071                   cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
1072                   break;
1073                 default:
1074                   gcc_unreachable ();
1075                 }
1076               cfi_cfa = &cfi_buf;
1077             }
1078           else if (cfi_cfa_offset)
1079             cfi_cfa = cfi_cfa_offset;
1080           if (cfi_cfa)
1081             {
1082               if (do_cfi_asm)
1083                 output_cfi_directive (cfi_cfa);
1084               else
1085                 output_cfi (cfi_cfa, fde, for_eh);
1086             }
1087           cfi_cfa = NULL;
1088           cfi_cfa_offset = NULL;
1089           if (cfi_args_size
1090               && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
1091             {
1092               if (do_cfi_asm)
1093                 output_cfi_directive (cfi_args_size);
1094               else
1095                 output_cfi (cfi_args_size, fde, for_eh);
1096             }
1097           cfi_args_size = NULL;
1098           if (cfi == NULL)
1099             {
1100               VEC_free (dw_cfi_ref, heap, regs);
1101               return;
1102             }
1103           else if (do_cfi_asm)
1104             output_cfi_directive (cfi);
1105           else
1106             output_cfi (cfi, fde, for_eh);
1107           break;
1108         default:
1109           gcc_unreachable ();
1110         }
1111     }
1112 }
1113
1114 /* Like output_cfis, but emit all CFIs in the vector.  */
1115 static void
1116 output_all_cfis (cfi_vec vec, bool do_cfi_asm,
1117                  dw_fde_ref fde, bool for_eh)
1118 {
1119   output_cfis (vec, VEC_length (dw_cfi_ref, vec), do_cfi_asm, fde, for_eh);
1120 }
1121
1122 /* Output one FDE.  */
1123
1124 static void
1125 output_fde (dw_fde_ref fde, bool for_eh, bool second,
1126             char *section_start_label, int fde_encoding, char *augmentation,
1127             bool any_lsda_needed, int lsda_encoding)
1128 {
1129   int ix;
1130   const char *begin, *end;
1131   static unsigned int j;
1132   char l1[20], l2[20];
1133   dw_cfi_ref cfi;
1134
1135   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
1136                                      /* empty */ 0);
1137   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
1138                                   for_eh + j);
1139   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
1140   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
1141   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
1142     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
1143                          " indicating 64-bit DWARF extension");
1144   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1145                         "FDE Length");
1146   ASM_OUTPUT_LABEL (asm_out_file, l1);
1147
1148   if (for_eh)
1149     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
1150   else
1151     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
1152                            debug_frame_section, "FDE CIE offset");
1153
1154   begin = second ? fde->dw_fde_second_begin : fde->dw_fde_begin;
1155   end = second ? fde->dw_fde_second_end : fde->dw_fde_end;
1156
1157   if (for_eh)
1158     {
1159       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
1160       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
1161       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
1162                                        "FDE initial location");
1163       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
1164                             end, begin, "FDE address range");
1165     }
1166   else
1167     {
1168       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
1169       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
1170     }
1171
1172   if (augmentation[0])
1173     {
1174       if (any_lsda_needed)
1175         {
1176           int size = size_of_encoded_value (lsda_encoding);
1177
1178           if (lsda_encoding == DW_EH_PE_aligned)
1179             {
1180               int offset = (  4         /* Length */
1181                             + 4         /* CIE offset */
1182                             + 2 * size_of_encoded_value (fde_encoding)
1183                             + 1         /* Augmentation size */ );
1184               int pad = -offset & (PTR_SIZE - 1);
1185
1186               size += pad;
1187               gcc_assert (size_of_uleb128 (size) == 1);
1188             }
1189
1190           dw2_asm_output_data_uleb128 (size, "Augmentation size");
1191
1192           if (fde->uses_eh_lsda)
1193             {
1194               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
1195                                            fde->funcdef_number);
1196               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
1197                                                gen_rtx_SYMBOL_REF (Pmode, l1),
1198                                                false,
1199                                                "Language Specific Data Area");
1200             }
1201           else
1202             {
1203               if (lsda_encoding == DW_EH_PE_aligned)
1204                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
1205               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
1206                                    "Language Specific Data Area (none)");
1207             }
1208         }
1209       else
1210         dw2_asm_output_data_uleb128 (0, "Augmentation size");
1211     }
1212
1213   /* Loop through the Call Frame Instructions associated with
1214      this FDE.  */
1215   fde->dw_fde_current_label = begin;
1216   if (fde->dw_fde_second_begin == NULL)
1217     FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1218       output_cfi (cfi, fde, for_eh);
1219   else if (!second)
1220     {
1221       if (fde->dw_fde_switch_cfi_index > 0)
1222         FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
1223           {
1224             if (ix == fde->dw_fde_switch_cfi_index)
1225               break;
1226             output_cfi (cfi, fde, for_eh);
1227           }
1228     }
1229   else
1230     {
1231       int i, from = 0;
1232       int until = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
1233
1234       if (fde->dw_fde_switch_cfi_index > 0)
1235         {
1236           from = fde->dw_fde_switch_cfi_index;
1237           output_cfis (fde->dw_fde_cfi, from, false, fde, for_eh);
1238         }
1239       for (i = from; i < until; i++)
1240         output_cfi (VEC_index (dw_cfi_ref, fde->dw_fde_cfi, i),
1241                     fde, for_eh);
1242     }
1243
1244   /* If we are to emit a ref/link from function bodies to their frame tables,
1245      do it now.  This is typically performed to make sure that tables
1246      associated with functions are dragged with them and not discarded in
1247      garbage collecting links. We need to do this on a per function basis to
1248      cope with -ffunction-sections.  */
1249
1250 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
1251   /* Switch to the function section, emit the ref to the tables, and
1252      switch *back* into the table section.  */
1253   switch_to_section (function_section (fde->decl));
1254   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
1255   switch_to_frame_table_section (for_eh, true);
1256 #endif
1257
1258   /* Pad the FDE out to an address sized boundary.  */
1259   ASM_OUTPUT_ALIGN (asm_out_file,
1260                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
1261   ASM_OUTPUT_LABEL (asm_out_file, l2);
1262
1263   j += 2;
1264 }
1265
1266 /* Return true if frame description entry FDE is needed for EH.  */
1267
1268 static bool
1269 fde_needed_for_eh_p (dw_fde_ref fde)
1270 {
1271   if (flag_asynchronous_unwind_tables)
1272     return true;
1273
1274   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
1275     return true;
1276
1277   if (fde->uses_eh_lsda)
1278     return true;
1279
1280   /* If exceptions are enabled, we have collected nothrow info.  */
1281   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
1282     return false;
1283
1284   return true;
1285 }
1286
1287 /* Output the call frame information used to record information
1288    that relates to calculating the frame pointer, and records the
1289    location of saved registers.  */
1290
1291 static void
1292 output_call_frame_info (int for_eh)
1293 {
1294   unsigned int i;
1295   dw_fde_ref fde;
1296   dw_cfi_ref cfi;
1297   char l1[20], l2[20], section_start_label[20];
1298   bool any_lsda_needed = false;
1299   char augmentation[6];
1300   int augmentation_size;
1301   int fde_encoding = DW_EH_PE_absptr;
1302   int per_encoding = DW_EH_PE_absptr;
1303   int lsda_encoding = DW_EH_PE_absptr;
1304   int return_reg;
1305   rtx personality = NULL;
1306   int dw_cie_version;
1307
1308   /* Don't emit a CIE if there won't be any FDEs.  */
1309   if (fde_table_in_use == 0)
1310     return;
1311
1312   /* Nothing to do if the assembler's doing it all.  */
1313   if (dwarf2out_do_cfi_asm ())
1314     return;
1315
1316   /* If we don't have any functions we'll want to unwind out of, don't emit
1317      any EH unwind information.  If we make FDEs linkonce, we may have to
1318      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
1319      want to avoid having an FDE kept around when the function it refers to
1320      is discarded.  Example where this matters: a primary function template
1321      in C++ requires EH information, an explicit specialization doesn't.  */
1322   if (for_eh)
1323     {
1324       bool any_eh_needed = false;
1325
1326       for (i = 0; i < fde_table_in_use; i++)
1327         if (fde_table[i].uses_eh_lsda)
1328           any_eh_needed = any_lsda_needed = true;
1329         else if (fde_needed_for_eh_p (&fde_table[i]))
1330           any_eh_needed = true;
1331         else if (TARGET_USES_WEAK_UNWIND_INFO)
1332           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
1333                                              1, 1);
1334
1335       if (!any_eh_needed)
1336         return;
1337     }
1338
1339   /* We're going to be generating comments, so turn on app.  */
1340   if (flag_debug_asm)
1341     app_enable ();
1342
1343   /* Switch to the proper frame section, first time.  */
1344   switch_to_frame_table_section (for_eh, false);
1345
1346   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
1347   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
1348
1349   /* Output the CIE.  */
1350   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
1351   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
1352   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
1353     dw2_asm_output_data (4, 0xffffffff,
1354       "Initial length escape value indicating 64-bit DWARF extension");
1355   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
1356                         "Length of Common Information Entry");
1357   ASM_OUTPUT_LABEL (asm_out_file, l1);
1358
1359   /* Now that the CIE pointer is PC-relative for EH,
1360      use 0 to identify the CIE.  */
1361   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
1362                        (for_eh ? 0 : DWARF_CIE_ID),
1363                        "CIE Identifier Tag");
1364
1365   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
1366      use CIE version 1, unless that would produce incorrect results
1367      due to overflowing the return register column.  */
1368   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
1369   dw_cie_version = 1;
1370   if (return_reg >= 256 || dwarf_version > 2)
1371     dw_cie_version = 3;
1372   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
1373
1374   augmentation[0] = 0;
1375   augmentation_size = 0;
1376
1377   personality = current_unit_personality;
1378   if (for_eh)
1379     {
1380       char *p;
1381
1382       /* Augmentation:
1383          z      Indicates that a uleb128 is present to size the
1384                 augmentation section.
1385          L      Indicates the encoding (and thus presence) of
1386                 an LSDA pointer in the FDE augmentation.
1387          R      Indicates a non-default pointer encoding for
1388                 FDE code pointers.
1389          P      Indicates the presence of an encoding + language
1390                 personality routine in the CIE augmentation.  */
1391
1392       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
1393       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
1394       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1395
1396       p = augmentation + 1;
1397       if (personality)
1398         {
1399           *p++ = 'P';
1400           augmentation_size += 1 + size_of_encoded_value (per_encoding);
1401           assemble_external_libcall (personality);
1402         }
1403       if (any_lsda_needed)
1404         {
1405           *p++ = 'L';
1406           augmentation_size += 1;
1407         }
1408       if (fde_encoding != DW_EH_PE_absptr)
1409         {
1410           *p++ = 'R';
1411           augmentation_size += 1;
1412         }
1413       if (p > augmentation + 1)
1414         {
1415           augmentation[0] = 'z';
1416           *p = '\0';
1417         }
1418
1419       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
1420       if (personality && per_encoding == DW_EH_PE_aligned)
1421         {
1422           int offset = (  4             /* Length */
1423                         + 4             /* CIE Id */
1424                         + 1             /* CIE version */
1425                         + strlen (augmentation) + 1     /* Augmentation */
1426                         + size_of_uleb128 (1)           /* Code alignment */
1427                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
1428                         + 1             /* RA column */
1429                         + 1             /* Augmentation size */
1430                         + 1             /* Personality encoding */ );
1431           int pad = -offset & (PTR_SIZE - 1);
1432
1433           augmentation_size += pad;
1434
1435           /* Augmentations should be small, so there's scarce need to
1436              iterate for a solution.  Die if we exceed one uleb128 byte.  */
1437           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
1438         }
1439     }
1440
1441   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
1442   if (dw_cie_version >= 4)
1443     {
1444       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
1445       dw2_asm_output_data (1, 0, "CIE Segment Size");
1446     }
1447   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
1448   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
1449                                "CIE Data Alignment Factor");
1450
1451   if (dw_cie_version == 1)
1452     dw2_asm_output_data (1, return_reg, "CIE RA Column");
1453   else
1454     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
1455
1456   if (augmentation[0])
1457     {
1458       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
1459       if (personality)
1460         {
1461           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
1462                                eh_data_format_name (per_encoding));
1463           dw2_asm_output_encoded_addr_rtx (per_encoding,
1464                                            personality,
1465                                            true, NULL);
1466         }
1467
1468       if (any_lsda_needed)
1469         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
1470                              eh_data_format_name (lsda_encoding));
1471
1472       if (fde_encoding != DW_EH_PE_absptr)
1473         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
1474                              eh_data_format_name (fde_encoding));
1475     }
1476
1477   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, i, cfi)
1478     output_cfi (cfi, NULL, for_eh);
1479
1480   /* Pad the CIE out to an address sized boundary.  */
1481   ASM_OUTPUT_ALIGN (asm_out_file,
1482                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
1483   ASM_OUTPUT_LABEL (asm_out_file, l2);
1484
1485   /* Loop through all of the FDE's.  */
1486   for (i = 0; i < fde_table_in_use; i++)
1487     {
1488       unsigned int k;
1489       fde = &fde_table[i];
1490
1491       /* Don't emit EH unwind info for leaf functions that don't need it.  */
1492       if (for_eh && !fde_needed_for_eh_p (fde))
1493         continue;
1494
1495       for (k = 0; k < (fde->dw_fde_second_begin ? 2 : 1); k++)
1496         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
1497                     augmentation, any_lsda_needed, lsda_encoding);
1498     }
1499
1500   if (for_eh && targetm.terminate_dw2_eh_frame_info)
1501     dw2_asm_output_data (4, 0, "End of Table");
1502 #ifdef MIPS_DEBUGGING_INFO
1503   /* Work around Irix 6 assembler bug whereby labels at the end of a section
1504      get a value of 0.  Putting .align 0 after the label fixes it.  */
1505   ASM_OUTPUT_ALIGN (asm_out_file, 0);
1506 #endif
1507
1508   /* Turn off app to make assembly quicker.  */
1509   if (flag_debug_asm)
1510     app_disable ();
1511 }
1512
1513 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
1514
1515 static void
1516 dwarf2out_do_cfi_startproc (bool second)
1517 {
1518   int enc;
1519   rtx ref;
1520   rtx personality = get_personality_function (current_function_decl);
1521
1522   fprintf (asm_out_file, "\t.cfi_startproc\n");
1523
1524   if (personality)
1525     {
1526       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
1527       ref = personality;
1528
1529       /* ??? The GAS support isn't entirely consistent.  We have to
1530          handle indirect support ourselves, but PC-relative is done
1531          in the assembler.  Further, the assembler can't handle any
1532          of the weirder relocation types.  */
1533       if (enc & DW_EH_PE_indirect)
1534         ref = dw2_force_const_mem (ref, true);
1535
1536       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
1537       output_addr_const (asm_out_file, ref);
1538       fputc ('\n', asm_out_file);
1539     }
1540
1541   if (crtl->uses_eh_lsda)
1542     {
1543       char lab[20];
1544
1545       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
1546       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
1547                                    current_function_funcdef_no);
1548       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
1549       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
1550
1551       if (enc & DW_EH_PE_indirect)
1552         ref = dw2_force_const_mem (ref, true);
1553
1554       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
1555       output_addr_const (asm_out_file, ref);
1556       fputc ('\n', asm_out_file);
1557     }
1558 }
1559
1560 /* Output a marker (i.e. a label) for the beginning of a function, before
1561    the prologue.  */
1562
1563 void
1564 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
1565                           const char *file ATTRIBUTE_UNUSED)
1566 {
1567   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1568   char * dup_label;
1569   dw_fde_ref fde;
1570   section *fnsec;
1571   bool do_frame;
1572
1573   current_function_func_begin_label = NULL;
1574
1575   do_frame = dwarf2out_do_frame ();
1576
1577   /* ??? current_function_func_begin_label is also used by except.c for
1578      call-site information.  We must emit this label if it might be used.  */
1579   if (!do_frame
1580       && (!flag_exceptions
1581           || targetm_common.except_unwind_info (&global_options) != UI_TARGET))
1582     return;
1583
1584   fnsec = function_section (current_function_decl);
1585   switch_to_section (fnsec);
1586   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
1587                                current_function_funcdef_no);
1588   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
1589                           current_function_funcdef_no);
1590   dup_label = xstrdup (label);
1591   current_function_func_begin_label = dup_label;
1592
1593   /* We can elide the fde allocation if we're not emitting debug info.  */
1594   if (!do_frame)
1595     return;
1596
1597   /* Expand the fde table if necessary.  */
1598   if (fde_table_in_use == fde_table_allocated)
1599     {
1600       fde_table_allocated += FDE_TABLE_INCREMENT;
1601       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
1602       memset (fde_table + fde_table_in_use, 0,
1603               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
1604     }
1605
1606   /* Record the FDE associated with this function.  */
1607   current_funcdef_fde = fde_table_in_use;
1608
1609   /* Add the new FDE at the end of the fde_table.  */
1610   fde = &fde_table[fde_table_in_use++];
1611   fde->decl = current_function_decl;
1612   fde->dw_fde_begin = dup_label;
1613   fde->dw_fde_end = NULL;
1614   fde->dw_fde_current_label = dup_label;
1615   fde->dw_fde_second_begin = NULL;
1616   fde->dw_fde_second_end = NULL;
1617   fde->dw_fde_vms_end_prologue = NULL;
1618   fde->dw_fde_vms_begin_epilogue = NULL;
1619   fde->dw_fde_cfi = VEC_alloc (dw_cfi_ref, gc, 20);
1620   fde->dw_fde_switch_cfi_index = 0;
1621   fde->funcdef_number = current_function_funcdef_no;
1622   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
1623   fde->uses_eh_lsda = crtl->uses_eh_lsda;
1624   fde->nothrow = crtl->nothrow;
1625   fde->drap_reg = INVALID_REGNUM;
1626   fde->vdrap_reg = INVALID_REGNUM;
1627   fde->in_std_section = (fnsec == text_section
1628                          || (cold_text_section && fnsec == cold_text_section));
1629   fde->second_in_std_section = 0;
1630
1631   dwarf2cfi_function_init ();
1632
1633   /* We only want to output line number information for the genuine dwarf2
1634      prologue case, not the eh frame case.  */
1635 #ifdef DWARF2_DEBUGGING_INFO
1636   if (file)
1637     dwarf2out_source_line (line, file, 0, true);
1638 #endif
1639
1640   if (dwarf2out_do_cfi_asm ())
1641     dwarf2out_do_cfi_startproc (false);
1642   else
1643     {
1644       rtx personality = get_personality_function (current_function_decl);
1645       if (!current_unit_personality)
1646         current_unit_personality = personality;
1647
1648       /* We cannot keep a current personality per function as without CFI
1649          asm, at the point where we emit the CFI data, there is no current
1650          function anymore.  */
1651       if (personality && current_unit_personality != personality)
1652         sorry ("multiple EH personalities are supported only with assemblers "
1653                "supporting .cfi_personality directive");
1654     }
1655 }
1656
1657 /* Output a marker (i.e. a label) for the end of the generated code
1658    for a function prologue.  This gets called *after* the prologue code has
1659    been generated.  */
1660
1661 void
1662 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
1663                         const char *file ATTRIBUTE_UNUSED)
1664 {
1665   dw_fde_ref fde;
1666   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1667
1668   /* Output a label to mark the endpoint of the code generated for this
1669      function.  */
1670   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
1671                                current_function_funcdef_no);
1672   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
1673                           current_function_funcdef_no);
1674   fde = &fde_table[fde_table_in_use - 1];
1675   fde->dw_fde_vms_end_prologue = xstrdup (label);
1676 }
1677
1678 /* Output a marker (i.e. a label) for the beginning of the generated code
1679    for a function epilogue.  This gets called *before* the prologue code has
1680    been generated.  */
1681
1682 void
1683 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1684                           const char *file ATTRIBUTE_UNUSED)
1685 {
1686   dw_fde_ref fde;
1687   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1688
1689   fde = &fde_table[fde_table_in_use - 1];
1690   if (fde->dw_fde_vms_begin_epilogue)
1691     return;
1692
1693   /* Output a label to mark the endpoint of the code generated for this
1694      function.  */
1695   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
1696                                current_function_funcdef_no);
1697   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
1698                           current_function_funcdef_no);
1699   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
1700 }
1701
1702 /* Output a marker (i.e. a label) for the absolute end of the generated code
1703    for a function definition.  This gets called *after* the epilogue code has
1704    been generated.  */
1705
1706 void
1707 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
1708                         const char *file ATTRIBUTE_UNUSED)
1709 {
1710   dw_fde_ref fde;
1711   char label[MAX_ARTIFICIAL_LABEL_BYTES];
1712
1713   last_var_location_insn = NULL_RTX;
1714
1715   if (dwarf2out_do_cfi_asm ())
1716     fprintf (asm_out_file, "\t.cfi_endproc\n");
1717
1718   /* Output a label to mark the endpoint of the code generated for this
1719      function.  */
1720   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
1721                                current_function_funcdef_no);
1722   ASM_OUTPUT_LABEL (asm_out_file, label);
1723   fde = current_fde ();
1724   gcc_assert (fde != NULL);
1725   if (fde->dw_fde_second_begin == NULL)
1726     fde->dw_fde_end = xstrdup (label);
1727 }
1728
1729 void
1730 dwarf2out_frame_init (void)
1731 {
1732   /* Allocate the initial hunk of the fde_table.  */
1733   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
1734   fde_table_allocated = FDE_TABLE_INCREMENT;
1735   fde_table_in_use = 0;
1736
1737   dwarf2cfi_frame_init ();
1738 }
1739
1740 void
1741 dwarf2out_frame_finish (void)
1742 {
1743   /* Output call frame information.  */
1744   if (targetm.debug_unwind_info () == UI_DWARF2)
1745     output_call_frame_info (0);
1746
1747   /* Output another copy for the unwinder.  */
1748   if ((flag_unwind_tables || flag_exceptions)
1749       && targetm_common.except_unwind_info (&global_options) == UI_DWARF2)
1750     output_call_frame_info (1);
1751 }
1752
1753 /* Note that the current function section is being used for code.  */
1754
1755 static void
1756 dwarf2out_note_section_used (void)
1757 {
1758   section *sec = current_function_section ();
1759   if (sec == text_section)
1760     text_section_used = true;
1761   else if (sec == cold_text_section)
1762     cold_text_section_used = true;
1763 }
1764
1765 static void var_location_switch_text_section (void);
1766 static void set_cur_line_info_table (section *);
1767
1768 void
1769 dwarf2out_switch_text_section (void)
1770 {
1771   section *sect;
1772   dw_fde_ref fde = current_fde ();
1773
1774   gcc_assert (cfun && fde && fde->dw_fde_second_begin == NULL);
1775
1776   if (!in_cold_section_p)
1777     {
1778       fde->dw_fde_end = crtl->subsections.cold_section_end_label;
1779       fde->dw_fde_second_begin = crtl->subsections.hot_section_label;
1780       fde->dw_fde_second_end = crtl->subsections.hot_section_end_label;
1781     }
1782   else
1783     {
1784       fde->dw_fde_end = crtl->subsections.hot_section_end_label;
1785       fde->dw_fde_second_begin = crtl->subsections.cold_section_label;
1786       fde->dw_fde_second_end = crtl->subsections.cold_section_end_label;
1787     }
1788   have_multiple_function_sections = true;
1789
1790   /* Reset the current label on switching text sections, so that we
1791      don't attempt to advance_loc4 between labels in different sections.  */
1792   fde->dw_fde_current_label = NULL;
1793
1794   /* There is no need to mark used sections when not debugging.  */
1795   if (cold_text_section != NULL)
1796     dwarf2out_note_section_used ();
1797
1798   if (dwarf2out_do_cfi_asm ())
1799     fprintf (asm_out_file, "\t.cfi_endproc\n");
1800
1801   /* Now do the real section switch.  */
1802   sect = current_function_section ();
1803   switch_to_section (sect);
1804
1805   fde->second_in_std_section
1806     = (sect == text_section
1807        || (cold_text_section && sect == cold_text_section));
1808
1809   if (dwarf2out_do_cfi_asm ())
1810     {
1811       dwarf2out_do_cfi_startproc (true);
1812       /* As this is a different FDE, insert all current CFI instructions
1813          again.  */
1814       output_all_cfis (fde->dw_fde_cfi, true, fde, true);
1815     }
1816   fde->dw_fde_switch_cfi_index = VEC_length (dw_cfi_ref, fde->dw_fde_cfi);
1817   var_location_switch_text_section ();
1818
1819   set_cur_line_info_table (sect);
1820 }
1821 \f
1822 /* And now, the subset of the debugging information support code necessary
1823    for emitting location expressions.  */
1824
1825 /* Data about a single source file.  */
1826 struct GTY(()) dwarf_file_data {
1827   const char * filename;
1828   int emitted_number;
1829 };
1830
1831 typedef struct GTY(()) deferred_locations_struct
1832 {
1833   tree variable;
1834   dw_die_ref die;
1835 } deferred_locations;
1836
1837 DEF_VEC_O(deferred_locations);
1838 DEF_VEC_ALLOC_O(deferred_locations,gc);
1839
1840 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
1841
1842 DEF_VEC_P(dw_die_ref);
1843 DEF_VEC_ALLOC_P(dw_die_ref,heap);
1844
1845 /* Location lists are ranges + location descriptions for that range,
1846    so you can track variables that are in different places over
1847    their entire life.  */
1848 typedef struct GTY(()) dw_loc_list_struct {
1849   dw_loc_list_ref dw_loc_next;
1850   const char *begin; /* Label for begin address of range */
1851   const char *end;  /* Label for end address of range */
1852   char *ll_symbol; /* Label for beginning of location list.
1853                       Only on head of list */
1854   const char *section; /* Section this loclist is relative to */
1855   dw_loc_descr_ref expr;
1856   hashval_t hash;
1857   /* True if all addresses in this and subsequent lists are known to be
1858      resolved.  */
1859   bool resolved_addr;
1860   /* True if this list has been replaced by dw_loc_next.  */
1861   bool replaced;
1862   bool emitted;
1863   /* True if the range should be emitted even if begin and end
1864      are the same.  */
1865   bool force;
1866 } dw_loc_list_node;
1867
1868 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
1869
1870 /* Convert a DWARF stack opcode into its string name.  */
1871
1872 static const char *
1873 dwarf_stack_op_name (unsigned int op)
1874 {
1875   switch (op)
1876     {
1877     case DW_OP_addr:
1878       return "DW_OP_addr";
1879     case DW_OP_deref:
1880       return "DW_OP_deref";
1881     case DW_OP_const1u:
1882       return "DW_OP_const1u";
1883     case DW_OP_const1s:
1884       return "DW_OP_const1s";
1885     case DW_OP_const2u:
1886       return "DW_OP_const2u";
1887     case DW_OP_const2s:
1888       return "DW_OP_const2s";
1889     case DW_OP_const4u:
1890       return "DW_OP_const4u";
1891     case DW_OP_const4s:
1892       return "DW_OP_const4s";
1893     case DW_OP_const8u:
1894       return "DW_OP_const8u";
1895     case DW_OP_const8s:
1896       return "DW_OP_const8s";
1897     case DW_OP_constu:
1898       return "DW_OP_constu";
1899     case DW_OP_consts:
1900       return "DW_OP_consts";
1901     case DW_OP_dup:
1902       return "DW_OP_dup";
1903     case DW_OP_drop:
1904       return "DW_OP_drop";
1905     case DW_OP_over:
1906       return "DW_OP_over";
1907     case DW_OP_pick:
1908       return "DW_OP_pick";
1909     case DW_OP_swap:
1910       return "DW_OP_swap";
1911     case DW_OP_rot:
1912       return "DW_OP_rot";
1913     case DW_OP_xderef:
1914       return "DW_OP_xderef";
1915     case DW_OP_abs:
1916       return "DW_OP_abs";
1917     case DW_OP_and:
1918       return "DW_OP_and";
1919     case DW_OP_div:
1920       return "DW_OP_div";
1921     case DW_OP_minus:
1922       return "DW_OP_minus";
1923     case DW_OP_mod:
1924       return "DW_OP_mod";
1925     case DW_OP_mul:
1926       return "DW_OP_mul";
1927     case DW_OP_neg:
1928       return "DW_OP_neg";
1929     case DW_OP_not:
1930       return "DW_OP_not";
1931     case DW_OP_or:
1932       return "DW_OP_or";
1933     case DW_OP_plus:
1934       return "DW_OP_plus";
1935     case DW_OP_plus_uconst:
1936       return "DW_OP_plus_uconst";
1937     case DW_OP_shl:
1938       return "DW_OP_shl";
1939     case DW_OP_shr:
1940       return "DW_OP_shr";
1941     case DW_OP_shra:
1942       return "DW_OP_shra";
1943     case DW_OP_xor:
1944       return "DW_OP_xor";
1945     case DW_OP_bra:
1946       return "DW_OP_bra";
1947     case DW_OP_eq:
1948       return "DW_OP_eq";
1949     case DW_OP_ge:
1950       return "DW_OP_ge";
1951     case DW_OP_gt:
1952       return "DW_OP_gt";
1953     case DW_OP_le:
1954       return "DW_OP_le";
1955     case DW_OP_lt:
1956       return "DW_OP_lt";
1957     case DW_OP_ne:
1958       return "DW_OP_ne";
1959     case DW_OP_skip:
1960       return "DW_OP_skip";
1961     case DW_OP_lit0:
1962       return "DW_OP_lit0";
1963     case DW_OP_lit1:
1964       return "DW_OP_lit1";
1965     case DW_OP_lit2:
1966       return "DW_OP_lit2";
1967     case DW_OP_lit3:
1968       return "DW_OP_lit3";
1969     case DW_OP_lit4:
1970       return "DW_OP_lit4";
1971     case DW_OP_lit5:
1972       return "DW_OP_lit5";
1973     case DW_OP_lit6:
1974       return "DW_OP_lit6";
1975     case DW_OP_lit7:
1976       return "DW_OP_lit7";
1977     case DW_OP_lit8:
1978       return "DW_OP_lit8";
1979     case DW_OP_lit9:
1980       return "DW_OP_lit9";
1981     case DW_OP_lit10:
1982       return "DW_OP_lit10";
1983     case DW_OP_lit11:
1984       return "DW_OP_lit11";
1985     case DW_OP_lit12:
1986       return "DW_OP_lit12";
1987     case DW_OP_lit13:
1988       return "DW_OP_lit13";
1989     case DW_OP_lit14:
1990       return "DW_OP_lit14";
1991     case DW_OP_lit15:
1992       return "DW_OP_lit15";
1993     case DW_OP_lit16:
1994       return "DW_OP_lit16";
1995     case DW_OP_lit17:
1996       return "DW_OP_lit17";
1997     case DW_OP_lit18:
1998       return "DW_OP_lit18";
1999     case DW_OP_lit19:
2000       return "DW_OP_lit19";
2001     case DW_OP_lit20:
2002       return "DW_OP_lit20";
2003     case DW_OP_lit21:
2004       return "DW_OP_lit21";
2005     case DW_OP_lit22:
2006       return "DW_OP_lit22";
2007     case DW_OP_lit23:
2008       return "DW_OP_lit23";
2009     case DW_OP_lit24:
2010       return "DW_OP_lit24";
2011     case DW_OP_lit25:
2012       return "DW_OP_lit25";
2013     case DW_OP_lit26:
2014       return "DW_OP_lit26";
2015     case DW_OP_lit27:
2016       return "DW_OP_lit27";
2017     case DW_OP_lit28:
2018       return "DW_OP_lit28";
2019     case DW_OP_lit29:
2020       return "DW_OP_lit29";
2021     case DW_OP_lit30:
2022       return "DW_OP_lit30";
2023     case DW_OP_lit31:
2024       return "DW_OP_lit31";
2025     case DW_OP_reg0:
2026       return "DW_OP_reg0";
2027     case DW_OP_reg1:
2028       return "DW_OP_reg1";
2029     case DW_OP_reg2:
2030       return "DW_OP_reg2";
2031     case DW_OP_reg3:
2032       return "DW_OP_reg3";
2033     case DW_OP_reg4:
2034       return "DW_OP_reg4";
2035     case DW_OP_reg5:
2036       return "DW_OP_reg5";
2037     case DW_OP_reg6:
2038       return "DW_OP_reg6";
2039     case DW_OP_reg7:
2040       return "DW_OP_reg7";
2041     case DW_OP_reg8:
2042       return "DW_OP_reg8";
2043     case DW_OP_reg9:
2044       return "DW_OP_reg9";
2045     case DW_OP_reg10:
2046       return "DW_OP_reg10";
2047     case DW_OP_reg11:
2048       return "DW_OP_reg11";
2049     case DW_OP_reg12:
2050       return "DW_OP_reg12";
2051     case DW_OP_reg13:
2052       return "DW_OP_reg13";
2053     case DW_OP_reg14:
2054       return "DW_OP_reg14";
2055     case DW_OP_reg15:
2056       return "DW_OP_reg15";
2057     case DW_OP_reg16:
2058       return "DW_OP_reg16";
2059     case DW_OP_reg17:
2060       return "DW_OP_reg17";
2061     case DW_OP_reg18:
2062       return "DW_OP_reg18";
2063     case DW_OP_reg19:
2064       return "DW_OP_reg19";
2065     case DW_OP_reg20:
2066       return "DW_OP_reg20";
2067     case DW_OP_reg21:
2068       return "DW_OP_reg21";
2069     case DW_OP_reg22:
2070       return "DW_OP_reg22";
2071     case DW_OP_reg23:
2072       return "DW_OP_reg23";
2073     case DW_OP_reg24:
2074       return "DW_OP_reg24";
2075     case DW_OP_reg25:
2076       return "DW_OP_reg25";
2077     case DW_OP_reg26:
2078       return "DW_OP_reg26";
2079     case DW_OP_reg27:
2080       return "DW_OP_reg27";
2081     case DW_OP_reg28:
2082       return "DW_OP_reg28";
2083     case DW_OP_reg29:
2084       return "DW_OP_reg29";
2085     case DW_OP_reg30:
2086       return "DW_OP_reg30";
2087     case DW_OP_reg31:
2088       return "DW_OP_reg31";
2089     case DW_OP_breg0:
2090       return "DW_OP_breg0";
2091     case DW_OP_breg1:
2092       return "DW_OP_breg1";
2093     case DW_OP_breg2:
2094       return "DW_OP_breg2";
2095     case DW_OP_breg3:
2096       return "DW_OP_breg3";
2097     case DW_OP_breg4:
2098       return "DW_OP_breg4";
2099     case DW_OP_breg5:
2100       return "DW_OP_breg5";
2101     case DW_OP_breg6:
2102       return "DW_OP_breg6";
2103     case DW_OP_breg7:
2104       return "DW_OP_breg7";
2105     case DW_OP_breg8:
2106       return "DW_OP_breg8";
2107     case DW_OP_breg9:
2108       return "DW_OP_breg9";
2109     case DW_OP_breg10:
2110       return "DW_OP_breg10";
2111     case DW_OP_breg11:
2112       return "DW_OP_breg11";
2113     case DW_OP_breg12:
2114       return "DW_OP_breg12";
2115     case DW_OP_breg13:
2116       return "DW_OP_breg13";
2117     case DW_OP_breg14:
2118       return "DW_OP_breg14";
2119     case DW_OP_breg15:
2120       return "DW_OP_breg15";
2121     case DW_OP_breg16:
2122       return "DW_OP_breg16";
2123     case DW_OP_breg17:
2124       return "DW_OP_breg17";
2125     case DW_OP_breg18:
2126       return "DW_OP_breg18";
2127     case DW_OP_breg19:
2128       return "DW_OP_breg19";
2129     case DW_OP_breg20:
2130       return "DW_OP_breg20";
2131     case DW_OP_breg21:
2132       return "DW_OP_breg21";
2133     case DW_OP_breg22:
2134       return "DW_OP_breg22";
2135     case DW_OP_breg23:
2136       return "DW_OP_breg23";
2137     case DW_OP_breg24:
2138       return "DW_OP_breg24";
2139     case DW_OP_breg25:
2140       return "DW_OP_breg25";
2141     case DW_OP_breg26:
2142       return "DW_OP_breg26";
2143     case DW_OP_breg27:
2144       return "DW_OP_breg27";
2145     case DW_OP_breg28:
2146       return "DW_OP_breg28";
2147     case DW_OP_breg29:
2148       return "DW_OP_breg29";
2149     case DW_OP_breg30:
2150       return "DW_OP_breg30";
2151     case DW_OP_breg31:
2152       return "DW_OP_breg31";
2153     case DW_OP_regx:
2154       return "DW_OP_regx";
2155     case DW_OP_fbreg:
2156       return "DW_OP_fbreg";
2157     case DW_OP_bregx:
2158       return "DW_OP_bregx";
2159     case DW_OP_piece:
2160       return "DW_OP_piece";
2161     case DW_OP_deref_size:
2162       return "DW_OP_deref_size";
2163     case DW_OP_xderef_size:
2164       return "DW_OP_xderef_size";
2165     case DW_OP_nop:
2166       return "DW_OP_nop";
2167
2168     case DW_OP_push_object_address:
2169       return "DW_OP_push_object_address";
2170     case DW_OP_call2:
2171       return "DW_OP_call2";
2172     case DW_OP_call4:
2173       return "DW_OP_call4";
2174     case DW_OP_call_ref:
2175       return "DW_OP_call_ref";
2176     case DW_OP_implicit_value:
2177       return "DW_OP_implicit_value";
2178     case DW_OP_stack_value:
2179       return "DW_OP_stack_value";
2180     case DW_OP_form_tls_address:
2181       return "DW_OP_form_tls_address";
2182     case DW_OP_call_frame_cfa:
2183       return "DW_OP_call_frame_cfa";
2184     case DW_OP_bit_piece:
2185       return "DW_OP_bit_piece";
2186
2187     case DW_OP_GNU_push_tls_address:
2188       return "DW_OP_GNU_push_tls_address";
2189     case DW_OP_GNU_uninit:
2190       return "DW_OP_GNU_uninit";
2191     case DW_OP_GNU_encoded_addr:
2192       return "DW_OP_GNU_encoded_addr";
2193     case DW_OP_GNU_implicit_pointer:
2194       return "DW_OP_GNU_implicit_pointer";
2195     case DW_OP_GNU_entry_value:
2196       return "DW_OP_GNU_entry_value";
2197     case DW_OP_GNU_const_type:
2198       return "DW_OP_GNU_const_type";
2199     case DW_OP_GNU_regval_type:
2200       return "DW_OP_GNU_regval_type";
2201     case DW_OP_GNU_deref_type:
2202       return "DW_OP_GNU_deref_type";
2203     case DW_OP_GNU_convert:
2204       return "DW_OP_GNU_convert";
2205     case DW_OP_GNU_reinterpret:
2206       return "DW_OP_GNU_reinterpret";
2207     case DW_OP_GNU_parameter_ref:
2208       return "DW_OP_GNU_parameter_ref";
2209
2210     default:
2211       return "OP_<unknown>";
2212     }
2213 }
2214
2215 /* Return a pointer to a newly allocated location description.  Location
2216    descriptions are simple expression terms that can be strung
2217    together to form more complicated location (address) descriptions.  */
2218
2219 static inline dw_loc_descr_ref
2220 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
2221                unsigned HOST_WIDE_INT oprnd2)
2222 {
2223   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
2224
2225   descr->dw_loc_opc = op;
2226   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
2227   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
2228   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
2229   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
2230
2231   return descr;
2232 }
2233
2234 /* Return a pointer to a newly allocated location description for
2235    REG and OFFSET.  */
2236
2237 static inline dw_loc_descr_ref
2238 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
2239 {
2240   if (reg <= 31)
2241     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
2242                           offset, 0);
2243   else
2244     return new_loc_descr (DW_OP_bregx, reg, offset);
2245 }
2246
2247 /* Add a location description term to a location description expression.  */
2248
2249 static inline void
2250 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
2251 {
2252   dw_loc_descr_ref *d;
2253
2254   /* Find the end of the chain.  */
2255   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
2256     ;
2257
2258   *d = descr;
2259 }
2260
2261 /* Add a constant OFFSET to a location expression.  */
2262
2263 static void
2264 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
2265 {
2266   dw_loc_descr_ref loc;
2267   HOST_WIDE_INT *p;
2268
2269   gcc_assert (*list_head != NULL);
2270
2271   if (!offset)
2272     return;
2273
2274   /* Find the end of the chain.  */
2275   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
2276     ;
2277
2278   p = NULL;
2279   if (loc->dw_loc_opc == DW_OP_fbreg
2280       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
2281     p = &loc->dw_loc_oprnd1.v.val_int;
2282   else if (loc->dw_loc_opc == DW_OP_bregx)
2283     p = &loc->dw_loc_oprnd2.v.val_int;
2284
2285   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
2286      offset.  Don't optimize if an signed integer overflow would happen.  */
2287   if (p != NULL
2288       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
2289           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
2290     *p += offset;
2291
2292   else if (offset > 0)
2293     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
2294
2295   else
2296     {
2297       loc->dw_loc_next = int_loc_descriptor (-offset);
2298       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
2299     }
2300 }
2301
2302 /* Add a constant OFFSET to a location list.  */
2303
2304 static void
2305 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
2306 {
2307   dw_loc_list_ref d;
2308   for (d = list_head; d != NULL; d = d->dw_loc_next)
2309     loc_descr_plus_const (&d->expr, offset);
2310 }
2311
2312 #define DWARF_REF_SIZE  \
2313   (dwarf_version == 2 ? DWARF2_ADDR_SIZE : DWARF_OFFSET_SIZE)
2314
2315 static unsigned long size_of_locs (dw_loc_descr_ref);
2316 static unsigned long int get_base_type_offset (dw_die_ref);
2317
2318 /* Return the size of a location descriptor.  */
2319
2320 static unsigned long
2321 size_of_loc_descr (dw_loc_descr_ref loc)
2322 {
2323   unsigned long size = 1;
2324
2325   switch (loc->dw_loc_opc)
2326     {
2327     case DW_OP_addr:
2328       size += DWARF2_ADDR_SIZE;
2329       break;
2330     case DW_OP_const1u:
2331     case DW_OP_const1s:
2332       size += 1;
2333       break;
2334     case DW_OP_const2u:
2335     case DW_OP_const2s:
2336       size += 2;
2337       break;
2338     case DW_OP_const4u:
2339     case DW_OP_const4s:
2340       size += 4;
2341       break;
2342     case DW_OP_const8u:
2343     case DW_OP_const8s:
2344       size += 8;
2345       break;
2346     case DW_OP_constu:
2347       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2348       break;
2349     case DW_OP_consts:
2350       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2351       break;
2352     case DW_OP_pick:
2353       size += 1;
2354       break;
2355     case DW_OP_plus_uconst:
2356       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2357       break;
2358     case DW_OP_skip:
2359     case DW_OP_bra:
2360       size += 2;
2361       break;
2362     case DW_OP_breg0:
2363     case DW_OP_breg1:
2364     case DW_OP_breg2:
2365     case DW_OP_breg3:
2366     case DW_OP_breg4:
2367     case DW_OP_breg5:
2368     case DW_OP_breg6:
2369     case DW_OP_breg7:
2370     case DW_OP_breg8:
2371     case DW_OP_breg9:
2372     case DW_OP_breg10:
2373     case DW_OP_breg11:
2374     case DW_OP_breg12:
2375     case DW_OP_breg13:
2376     case DW_OP_breg14:
2377     case DW_OP_breg15:
2378     case DW_OP_breg16:
2379     case DW_OP_breg17:
2380     case DW_OP_breg18:
2381     case DW_OP_breg19:
2382     case DW_OP_breg20:
2383     case DW_OP_breg21:
2384     case DW_OP_breg22:
2385     case DW_OP_breg23:
2386     case DW_OP_breg24:
2387     case DW_OP_breg25:
2388     case DW_OP_breg26:
2389     case DW_OP_breg27:
2390     case DW_OP_breg28:
2391     case DW_OP_breg29:
2392     case DW_OP_breg30:
2393     case DW_OP_breg31:
2394       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2395       break;
2396     case DW_OP_regx:
2397       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2398       break;
2399     case DW_OP_fbreg:
2400       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
2401       break;
2402     case DW_OP_bregx:
2403       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2404       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2405       break;
2406     case DW_OP_piece:
2407       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2408       break;
2409     case DW_OP_bit_piece:
2410       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2411       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
2412       break;
2413     case DW_OP_deref_size:
2414     case DW_OP_xderef_size:
2415       size += 1;
2416       break;
2417     case DW_OP_call2:
2418       size += 2;
2419       break;
2420     case DW_OP_call4:
2421       size += 4;
2422       break;
2423     case DW_OP_call_ref:
2424       size += DWARF_REF_SIZE;
2425       break;
2426     case DW_OP_implicit_value:
2427       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2428               + loc->dw_loc_oprnd1.v.val_unsigned;
2429       break;
2430     case DW_OP_GNU_implicit_pointer:
2431       size += DWARF_REF_SIZE + size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
2432       break;
2433     case DW_OP_GNU_entry_value:
2434       {
2435         unsigned long op_size = size_of_locs (loc->dw_loc_oprnd1.v.val_loc);
2436         size += size_of_uleb128 (op_size) + op_size;
2437         break;
2438       }
2439     case DW_OP_GNU_const_type:
2440       {
2441         unsigned long o
2442           = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2443         size += size_of_uleb128 (o) + 1;
2444         switch (loc->dw_loc_oprnd2.val_class)
2445           {
2446           case dw_val_class_vec:
2447             size += loc->dw_loc_oprnd2.v.val_vec.length
2448                     * loc->dw_loc_oprnd2.v.val_vec.elt_size;
2449             break;
2450           case dw_val_class_const:
2451             size += HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2452             break;
2453           case dw_val_class_const_double:
2454             size += 2 * HOST_BITS_PER_WIDE_INT / BITS_PER_UNIT;
2455             break;
2456           default:
2457             gcc_unreachable ();
2458           }
2459         break;
2460       }
2461     case DW_OP_GNU_regval_type:
2462       {
2463         unsigned long o
2464           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2465         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
2466                 + size_of_uleb128 (o);
2467       }
2468       break;
2469     case DW_OP_GNU_deref_type:
2470       {
2471         unsigned long o
2472           = get_base_type_offset (loc->dw_loc_oprnd2.v.val_die_ref.die);
2473         size += 1 + size_of_uleb128 (o);
2474       }
2475       break;
2476     case DW_OP_GNU_convert:
2477     case DW_OP_GNU_reinterpret:
2478       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2479         size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
2480       else
2481         {
2482           unsigned long o
2483             = get_base_type_offset (loc->dw_loc_oprnd1.v.val_die_ref.die);
2484           size += size_of_uleb128 (o);
2485         }
2486       break;
2487     case DW_OP_GNU_parameter_ref:
2488       size += 4;
2489       break;
2490     default:
2491       break;
2492     }
2493
2494   return size;
2495 }
2496
2497 /* Return the size of a series of location descriptors.  */
2498
2499 static unsigned long
2500 size_of_locs (dw_loc_descr_ref loc)
2501 {
2502   dw_loc_descr_ref l;
2503   unsigned long size;
2504
2505   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
2506      field, to avoid writing to a PCH file.  */
2507   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2508     {
2509       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
2510         break;
2511       size += size_of_loc_descr (l);
2512     }
2513   if (! l)
2514     return size;
2515
2516   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
2517     {
2518       l->dw_loc_addr = size;
2519       size += size_of_loc_descr (l);
2520     }
2521
2522   return size;
2523 }
2524
2525 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
2526 static void get_ref_die_offset_label (char *, dw_die_ref);
2527 static unsigned long int get_ref_die_offset (dw_die_ref);
2528 static void output_loc_sequence (dw_loc_descr_ref, int);
2529
2530 /* Output location description stack opcode's operands (if any).
2531    The for_eh_or_skip parameter controls whether register numbers are
2532    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2533    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2534    info).  This should be suppressed for the cases that have not been converted
2535    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2536
2537 static void
2538 output_loc_operands (dw_loc_descr_ref loc, int for_eh_or_skip)
2539 {
2540   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2541   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2542
2543   switch (loc->dw_loc_opc)
2544     {
2545 #ifdef DWARF2_DEBUGGING_INFO
2546     case DW_OP_const2u:
2547     case DW_OP_const2s:
2548       dw2_asm_output_data (2, val1->v.val_int, NULL);
2549       break;
2550     case DW_OP_const4u:
2551       if (loc->dtprel)
2552         {
2553           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2554           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
2555                                                val1->v.val_addr);
2556           fputc ('\n', asm_out_file);
2557           break;
2558         }
2559       /* FALLTHRU */
2560     case DW_OP_const4s:
2561       dw2_asm_output_data (4, val1->v.val_int, NULL);
2562       break;
2563     case DW_OP_const8u:
2564       if (loc->dtprel)
2565         {
2566           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
2567           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
2568                                                val1->v.val_addr);
2569           fputc ('\n', asm_out_file);
2570           break;
2571         }
2572       /* FALLTHRU */
2573     case DW_OP_const8s:
2574       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2575       dw2_asm_output_data (8, val1->v.val_int, NULL);
2576       break;
2577     case DW_OP_skip:
2578     case DW_OP_bra:
2579       {
2580         int offset;
2581
2582         gcc_assert (val1->val_class == dw_val_class_loc);
2583         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2584
2585         dw2_asm_output_data (2, offset, NULL);
2586       }
2587       break;
2588     case DW_OP_implicit_value:
2589       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2590       switch (val2->val_class)
2591         {
2592         case dw_val_class_const:
2593           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
2594           break;
2595         case dw_val_class_vec:
2596           {
2597             unsigned int elt_size = val2->v.val_vec.elt_size;
2598             unsigned int len = val2->v.val_vec.length;
2599             unsigned int i;
2600             unsigned char *p;
2601
2602             if (elt_size > sizeof (HOST_WIDE_INT))
2603               {
2604                 elt_size /= 2;
2605                 len *= 2;
2606               }
2607             for (i = 0, p = val2->v.val_vec.array;
2608                  i < len;
2609                  i++, p += elt_size)
2610               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2611                                    "fp or vector constant word %u", i);
2612           }
2613           break;
2614         case dw_val_class_const_double:
2615           {
2616             unsigned HOST_WIDE_INT first, second;
2617
2618             if (WORDS_BIG_ENDIAN)
2619               {
2620                 first = val2->v.val_double.high;
2621                 second = val2->v.val_double.low;
2622               }
2623             else
2624               {
2625                 first = val2->v.val_double.low;
2626                 second = val2->v.val_double.high;
2627               }
2628             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2629                                  first, NULL);
2630             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
2631                                  second, NULL);
2632           }
2633           break;
2634         case dw_val_class_addr:
2635           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
2636           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
2637           break;
2638         default:
2639           gcc_unreachable ();
2640         }
2641       break;
2642 #else
2643     case DW_OP_const2u:
2644     case DW_OP_const2s:
2645     case DW_OP_const4u:
2646     case DW_OP_const4s:
2647     case DW_OP_const8u:
2648     case DW_OP_const8s:
2649     case DW_OP_skip:
2650     case DW_OP_bra:
2651     case DW_OP_implicit_value:
2652       /* We currently don't make any attempt to make sure these are
2653          aligned properly like we do for the main unwind info, so
2654          don't support emitting things larger than a byte if we're
2655          only doing unwinding.  */
2656       gcc_unreachable ();
2657 #endif
2658     case DW_OP_const1u:
2659     case DW_OP_const1s:
2660       dw2_asm_output_data (1, val1->v.val_int, NULL);
2661       break;
2662     case DW_OP_constu:
2663       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2664       break;
2665     case DW_OP_consts:
2666       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2667       break;
2668     case DW_OP_pick:
2669       dw2_asm_output_data (1, val1->v.val_int, NULL);
2670       break;
2671     case DW_OP_plus_uconst:
2672       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2673       break;
2674     case DW_OP_breg0:
2675     case DW_OP_breg1:
2676     case DW_OP_breg2:
2677     case DW_OP_breg3:
2678     case DW_OP_breg4:
2679     case DW_OP_breg5:
2680     case DW_OP_breg6:
2681     case DW_OP_breg7:
2682     case DW_OP_breg8:
2683     case DW_OP_breg9:
2684     case DW_OP_breg10:
2685     case DW_OP_breg11:
2686     case DW_OP_breg12:
2687     case DW_OP_breg13:
2688     case DW_OP_breg14:
2689     case DW_OP_breg15:
2690     case DW_OP_breg16:
2691     case DW_OP_breg17:
2692     case DW_OP_breg18:
2693     case DW_OP_breg19:
2694     case DW_OP_breg20:
2695     case DW_OP_breg21:
2696     case DW_OP_breg22:
2697     case DW_OP_breg23:
2698     case DW_OP_breg24:
2699     case DW_OP_breg25:
2700     case DW_OP_breg26:
2701     case DW_OP_breg27:
2702     case DW_OP_breg28:
2703     case DW_OP_breg29:
2704     case DW_OP_breg30:
2705     case DW_OP_breg31:
2706       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2707       break;
2708     case DW_OP_regx:
2709       {
2710         unsigned r = val1->v.val_unsigned;
2711         if (for_eh_or_skip >= 0)
2712           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2713         gcc_assert (size_of_uleb128 (r) 
2714                     == size_of_uleb128 (val1->v.val_unsigned));
2715         dw2_asm_output_data_uleb128 (r, NULL);  
2716       }
2717       break;
2718     case DW_OP_fbreg:
2719       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
2720       break;
2721     case DW_OP_bregx:
2722       {
2723         unsigned r = val1->v.val_unsigned;
2724         if (for_eh_or_skip >= 0)
2725           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2726         gcc_assert (size_of_uleb128 (r) 
2727                     == size_of_uleb128 (val1->v.val_unsigned));
2728         dw2_asm_output_data_uleb128 (r, NULL);  
2729         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2730       }
2731       break;
2732     case DW_OP_piece:
2733       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2734       break;
2735     case DW_OP_bit_piece:
2736       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2737       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
2738       break;
2739     case DW_OP_deref_size:
2740     case DW_OP_xderef_size:
2741       dw2_asm_output_data (1, val1->v.val_int, NULL);
2742       break;
2743
2744     case DW_OP_addr:
2745       if (loc->dtprel)
2746         {
2747           if (targetm.asm_out.output_dwarf_dtprel)
2748             {
2749               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
2750                                                    DWARF2_ADDR_SIZE,
2751                                                    val1->v.val_addr);
2752               fputc ('\n', asm_out_file);
2753             }
2754           else
2755             gcc_unreachable ();
2756         }
2757       else
2758         {
2759 #ifdef DWARF2_DEBUGGING_INFO
2760           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
2761 #else
2762           gcc_unreachable ();
2763 #endif
2764         }
2765       break;
2766
2767     case DW_OP_GNU_implicit_pointer:
2768       {
2769         char label[MAX_ARTIFICIAL_LABEL_BYTES
2770                    + HOST_BITS_PER_WIDE_INT / 2 + 2];
2771         gcc_assert (val1->val_class == dw_val_class_die_ref);
2772         get_ref_die_offset_label (label, val1->v.val_die_ref.die);
2773         dw2_asm_output_offset (DWARF_REF_SIZE, label, debug_info_section, NULL);
2774         dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
2775       }
2776       break;
2777
2778     case DW_OP_GNU_entry_value:
2779       dw2_asm_output_data_uleb128 (size_of_locs (val1->v.val_loc), NULL);
2780       output_loc_sequence (val1->v.val_loc, for_eh_or_skip);
2781       break;
2782
2783     case DW_OP_GNU_const_type:
2784       {
2785         unsigned long o = get_base_type_offset (val1->v.val_die_ref.die), l;
2786         gcc_assert (o);
2787         dw2_asm_output_data_uleb128 (o, NULL);
2788         switch (val2->val_class)
2789           {
2790           case dw_val_class_const:
2791             l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2792             dw2_asm_output_data (1, l, NULL);
2793             dw2_asm_output_data (l, val2->v.val_int, NULL);
2794             break;
2795           case dw_val_class_vec:
2796             {
2797               unsigned int elt_size = val2->v.val_vec.elt_size;
2798               unsigned int len = val2->v.val_vec.length;
2799               unsigned int i;
2800               unsigned char *p;
2801
2802               l = len * elt_size;
2803               dw2_asm_output_data (1, l, NULL);
2804               if (elt_size > sizeof (HOST_WIDE_INT))
2805                 {
2806                   elt_size /= 2;
2807                   len *= 2;
2808                 }
2809               for (i = 0, p = val2->v.val_vec.array;
2810                    i < len;
2811                    i++, p += elt_size)
2812                 dw2_asm_output_data (elt_size, extract_int (p, elt_size),
2813                                      "fp or vector constant word %u", i);
2814             }
2815             break;
2816           case dw_val_class_const_double:
2817             {
2818               unsigned HOST_WIDE_INT first, second;
2819               l = HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
2820
2821               dw2_asm_output_data (1, 2 * l, NULL);
2822               if (WORDS_BIG_ENDIAN)
2823                 {
2824                   first = val2->v.val_double.high;
2825                   second = val2->v.val_double.low;
2826                 }
2827               else
2828                 {
2829                   first = val2->v.val_double.low;
2830                   second = val2->v.val_double.high;
2831                 }
2832               dw2_asm_output_data (l, first, NULL);
2833               dw2_asm_output_data (l, second, NULL);
2834             }
2835             break;
2836           default:
2837             gcc_unreachable ();
2838           }
2839       }
2840       break;
2841     case DW_OP_GNU_regval_type:
2842       {
2843         unsigned r = val1->v.val_unsigned;
2844         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2845         gcc_assert (o);
2846         if (for_eh_or_skip >= 0)
2847           {
2848             r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2849             gcc_assert (size_of_uleb128 (r)
2850                         == size_of_uleb128 (val1->v.val_unsigned));
2851           }
2852         dw2_asm_output_data_uleb128 (r, NULL);
2853         dw2_asm_output_data_uleb128 (o, NULL);
2854       }
2855       break;
2856     case DW_OP_GNU_deref_type:
2857       {
2858         unsigned long o = get_base_type_offset (val2->v.val_die_ref.die);
2859         gcc_assert (o);
2860         dw2_asm_output_data (1, val1->v.val_int, NULL);
2861         dw2_asm_output_data_uleb128 (o, NULL);
2862       }
2863       break;
2864     case DW_OP_GNU_convert:
2865     case DW_OP_GNU_reinterpret:
2866       if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
2867         dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
2868       else
2869         {
2870           unsigned long o = get_base_type_offset (val1->v.val_die_ref.die);
2871           gcc_assert (o);
2872           dw2_asm_output_data_uleb128 (o, NULL);
2873         }
2874       break;
2875
2876     case DW_OP_GNU_parameter_ref:
2877       {
2878         unsigned long o;
2879         gcc_assert (val1->val_class == dw_val_class_die_ref);
2880         o = get_ref_die_offset (val1->v.val_die_ref.die);
2881         dw2_asm_output_data (4, o, NULL);
2882       }
2883       break;
2884
2885     default:
2886       /* Other codes have no operands.  */
2887       break;
2888     }
2889 }
2890
2891 /* Output a sequence of location operations.  
2892    The for_eh_or_skip parameter controls whether register numbers are
2893    converted using DWARF2_FRAME_REG_OUT, which is needed in the case that
2894    hard reg numbers have been processed via DWARF_FRAME_REGNUM (i.e. for unwind
2895    info).  This should be suppressed for the cases that have not been converted
2896    (i.e. symbolic debug info), by setting the parameter < 0.  See PR47324.  */
2897
2898 static void
2899 output_loc_sequence (dw_loc_descr_ref loc, int for_eh_or_skip)
2900 {
2901   for (; loc != NULL; loc = loc->dw_loc_next)
2902     {
2903       enum dwarf_location_atom opc = loc->dw_loc_opc;
2904       /* Output the opcode.  */
2905       if (for_eh_or_skip >= 0 
2906           && opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
2907         {
2908           unsigned r = (opc - DW_OP_breg0);
2909           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2910           gcc_assert (r <= 31);
2911           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
2912         }
2913       else if (for_eh_or_skip >= 0 
2914                && opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
2915         {
2916           unsigned r = (opc - DW_OP_reg0);
2917           r = DWARF2_FRAME_REG_OUT (r, for_eh_or_skip);
2918           gcc_assert (r <= 31);
2919           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
2920         }
2921
2922       dw2_asm_output_data (1, opc,
2923                              "%s", dwarf_stack_op_name (opc));
2924
2925       /* Output the operand(s) (if any).  */
2926       output_loc_operands (loc, for_eh_or_skip);
2927     }
2928 }
2929
2930 /* Output location description stack opcode's operands (if any).
2931    The output is single bytes on a line, suitable for .cfi_escape.  */
2932
2933 static void
2934 output_loc_operands_raw (dw_loc_descr_ref loc)
2935 {
2936   dw_val_ref val1 = &loc->dw_loc_oprnd1;
2937   dw_val_ref val2 = &loc->dw_loc_oprnd2;
2938
2939   switch (loc->dw_loc_opc)
2940     {
2941     case DW_OP_addr:
2942     case DW_OP_implicit_value:
2943       /* We cannot output addresses in .cfi_escape, only bytes.  */
2944       gcc_unreachable ();
2945
2946     case DW_OP_const1u:
2947     case DW_OP_const1s:
2948     case DW_OP_pick:
2949     case DW_OP_deref_size:
2950     case DW_OP_xderef_size:
2951       fputc (',', asm_out_file);
2952       dw2_asm_output_data_raw (1, val1->v.val_int);
2953       break;
2954
2955     case DW_OP_const2u:
2956     case DW_OP_const2s:
2957       fputc (',', asm_out_file);
2958       dw2_asm_output_data_raw (2, val1->v.val_int);
2959       break;
2960
2961     case DW_OP_const4u:
2962     case DW_OP_const4s:
2963       fputc (',', asm_out_file);
2964       dw2_asm_output_data_raw (4, val1->v.val_int);
2965       break;
2966
2967     case DW_OP_const8u:
2968     case DW_OP_const8s:
2969       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
2970       fputc (',', asm_out_file);
2971       dw2_asm_output_data_raw (8, val1->v.val_int);
2972       break;
2973
2974     case DW_OP_skip:
2975     case DW_OP_bra:
2976       {
2977         int offset;
2978
2979         gcc_assert (val1->val_class == dw_val_class_loc);
2980         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
2981
2982         fputc (',', asm_out_file);
2983         dw2_asm_output_data_raw (2, offset);
2984       }
2985       break;
2986
2987     case DW_OP_regx:
2988       {
2989         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
2990         gcc_assert (size_of_uleb128 (r) 
2991                     == size_of_uleb128 (val1->v.val_unsigned));
2992         fputc (',', asm_out_file);
2993         dw2_asm_output_data_uleb128_raw (r);
2994       }
2995       break;
2996       
2997     case DW_OP_constu:
2998     case DW_OP_plus_uconst:
2999     case DW_OP_piece:
3000       fputc (',', asm_out_file);
3001       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
3002       break;
3003
3004     case DW_OP_bit_piece:
3005       fputc (',', asm_out_file);
3006       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
3007       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
3008       break;
3009
3010     case DW_OP_consts:
3011     case DW_OP_breg0:
3012     case DW_OP_breg1:
3013     case DW_OP_breg2:
3014     case DW_OP_breg3:
3015     case DW_OP_breg4:
3016     case DW_OP_breg5:
3017     case DW_OP_breg6:
3018     case DW_OP_breg7:
3019     case DW_OP_breg8:
3020     case DW_OP_breg9:
3021     case DW_OP_breg10:
3022     case DW_OP_breg11:
3023     case DW_OP_breg12:
3024     case DW_OP_breg13:
3025     case DW_OP_breg14:
3026     case DW_OP_breg15:
3027     case DW_OP_breg16:
3028     case DW_OP_breg17:
3029     case DW_OP_breg18:
3030     case DW_OP_breg19:
3031     case DW_OP_breg20:
3032     case DW_OP_breg21:
3033     case DW_OP_breg22:
3034     case DW_OP_breg23:
3035     case DW_OP_breg24:
3036     case DW_OP_breg25:
3037     case DW_OP_breg26:
3038     case DW_OP_breg27:
3039     case DW_OP_breg28:
3040     case DW_OP_breg29:
3041     case DW_OP_breg30:
3042     case DW_OP_breg31:
3043     case DW_OP_fbreg:
3044       fputc (',', asm_out_file);
3045       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
3046       break;
3047
3048     case DW_OP_bregx:
3049       {
3050         unsigned r = DWARF2_FRAME_REG_OUT (val1->v.val_unsigned, 1);
3051         gcc_assert (size_of_uleb128 (r) 
3052                     == size_of_uleb128 (val1->v.val_unsigned));
3053         fputc (',', asm_out_file);
3054         dw2_asm_output_data_uleb128_raw (r);
3055         fputc (',', asm_out_file);
3056         dw2_asm_output_data_sleb128_raw (val2->v.val_int);
3057       }
3058       break;
3059
3060     case DW_OP_GNU_implicit_pointer:
3061     case DW_OP_GNU_entry_value:
3062     case DW_OP_GNU_const_type:
3063     case DW_OP_GNU_regval_type:
3064     case DW_OP_GNU_deref_type:
3065     case DW_OP_GNU_convert:
3066     case DW_OP_GNU_reinterpret:
3067     case DW_OP_GNU_parameter_ref:
3068       gcc_unreachable ();
3069       break;
3070
3071     default:
3072       /* Other codes have no operands.  */
3073       break;
3074     }
3075 }
3076
3077 static void
3078 output_loc_sequence_raw (dw_loc_descr_ref loc)
3079 {
3080   while (1)
3081     {
3082       enum dwarf_location_atom opc = loc->dw_loc_opc;
3083       /* Output the opcode.  */
3084       if (opc >= DW_OP_breg0 && opc <= DW_OP_breg31)
3085         {
3086           unsigned r = (opc - DW_OP_breg0);
3087           r = DWARF2_FRAME_REG_OUT (r, 1);
3088           gcc_assert (r <= 31);
3089           opc = (enum dwarf_location_atom) (DW_OP_breg0 + r);
3090         }
3091       else if (opc >= DW_OP_reg0 && opc <= DW_OP_reg31)
3092         {
3093           unsigned r = (opc - DW_OP_reg0);
3094           r = DWARF2_FRAME_REG_OUT (r, 1);
3095           gcc_assert (r <= 31);
3096           opc = (enum dwarf_location_atom) (DW_OP_reg0 + r);
3097         }
3098       /* Output the opcode.  */
3099       fprintf (asm_out_file, "%#x", opc);
3100       output_loc_operands_raw (loc);
3101
3102       if (!loc->dw_loc_next)
3103         break;
3104       loc = loc->dw_loc_next;
3105
3106       fputc (',', asm_out_file);
3107     }
3108 }
3109
3110 /* This routine will generate the correct assembly data for a location
3111    description based on a cfi entry with a complex address.  */
3112
3113 static void
3114 output_cfa_loc (dw_cfi_ref cfi, int for_eh)
3115 {
3116   dw_loc_descr_ref loc;
3117   unsigned long size;
3118
3119   if (cfi->dw_cfi_opc == DW_CFA_expression)
3120     {
3121       unsigned r = 
3122         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3123       dw2_asm_output_data (1, r, NULL);
3124       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
3125     }
3126   else
3127     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3128
3129   /* Output the size of the block.  */
3130   size = size_of_locs (loc);
3131   dw2_asm_output_data_uleb128 (size, NULL);
3132
3133   /* Now output the operations themselves.  */
3134   output_loc_sequence (loc, for_eh);
3135 }
3136
3137 /* Similar, but used for .cfi_escape.  */
3138
3139 static void
3140 output_cfa_loc_raw (dw_cfi_ref cfi)
3141 {
3142   dw_loc_descr_ref loc;
3143   unsigned long size;
3144
3145   if (cfi->dw_cfi_opc == DW_CFA_expression)
3146     {
3147       unsigned r = 
3148         DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3149       fprintf (asm_out_file, "%#x,", r);
3150       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
3151     }
3152   else
3153     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3154
3155   /* Output the size of the block.  */
3156   size = size_of_locs (loc);
3157   dw2_asm_output_data_uleb128_raw (size);
3158   fputc (',', asm_out_file);
3159
3160   /* Now output the operations themselves.  */
3161   output_loc_sequence_raw (loc);
3162 }
3163
3164 /* This function builds a dwarf location descriptor sequence from a
3165    dw_cfa_location, adding the given OFFSET to the result of the
3166    expression.  */
3167
3168 struct dw_loc_descr_struct *
3169 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3170 {
3171   struct dw_loc_descr_struct *head, *tmp;
3172
3173   offset += cfa->offset;
3174
3175   if (cfa->indirect)
3176     {
3177       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
3178       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3179       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3180       add_loc_descr (&head, tmp);
3181       if (offset != 0)
3182         {
3183           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3184           add_loc_descr (&head, tmp);
3185         }
3186     }
3187   else
3188     head = new_reg_loc_descr (cfa->reg, offset);
3189
3190   return head;
3191 }
3192
3193 /* This function builds a dwarf location descriptor sequence for
3194    the address at OFFSET from the CFA when stack is aligned to
3195    ALIGNMENT byte.  */
3196
3197 struct dw_loc_descr_struct *
3198 build_cfa_aligned_loc (dw_cfa_location *cfa,
3199                        HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
3200 {
3201   struct dw_loc_descr_struct *head;
3202   unsigned int dwarf_fp
3203     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
3204
3205   /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
3206   if (cfa->reg == HARD_FRAME_POINTER_REGNUM && cfa->indirect == 0)
3207     {
3208       head = new_reg_loc_descr (dwarf_fp, 0);
3209       add_loc_descr (&head, int_loc_descriptor (alignment));
3210       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
3211       loc_descr_plus_const (&head, offset);
3212     }
3213   else
3214     head = new_reg_loc_descr (dwarf_fp, offset);
3215   return head;
3216 }
3217 \f
3218 /* And now, the support for symbolic debugging information.  */
3219
3220 /* .debug_str support.  */
3221 static int output_indirect_string (void **, void *);
3222
3223 static void dwarf2out_init (const char *);
3224 static void dwarf2out_finish (const char *);
3225 static void dwarf2out_assembly_start (void);
3226 static void dwarf2out_define (unsigned int, const char *);
3227 static void dwarf2out_undef (unsigned int, const char *);
3228 static void dwarf2out_start_source_file (unsigned, const char *);
3229 static void dwarf2out_end_source_file (unsigned);
3230 static void dwarf2out_function_decl (tree);
3231 static void dwarf2out_begin_block (unsigned, unsigned);
3232 static void dwarf2out_end_block (unsigned, unsigned);
3233 static bool dwarf2out_ignore_block (const_tree);
3234 static void dwarf2out_global_decl (tree);
3235 static void dwarf2out_type_decl (tree, int);
3236 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
3237 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
3238                                                  dw_die_ref);
3239 static void dwarf2out_abstract_function (tree);
3240 static void dwarf2out_var_location (rtx);
3241 static void dwarf2out_begin_function (tree);
3242 static void dwarf2out_set_name (tree, tree);
3243
3244 /* The debug hooks structure.  */
3245
3246 const struct gcc_debug_hooks dwarf2_debug_hooks =
3247 {
3248   dwarf2out_init,
3249   dwarf2out_finish,
3250   dwarf2out_assembly_start,
3251   dwarf2out_define,
3252   dwarf2out_undef,
3253   dwarf2out_start_source_file,
3254   dwarf2out_end_source_file,
3255   dwarf2out_begin_block,
3256   dwarf2out_end_block,
3257   dwarf2out_ignore_block,
3258   dwarf2out_source_line,
3259   dwarf2out_begin_prologue,
3260 #if VMS_DEBUGGING_INFO
3261   dwarf2out_vms_end_prologue,
3262   dwarf2out_vms_begin_epilogue,
3263 #else
3264   debug_nothing_int_charstar,
3265   debug_nothing_int_charstar,
3266 #endif
3267   dwarf2out_end_epilogue,
3268   dwarf2out_begin_function,
3269   debug_nothing_int,            /* end_function */
3270   dwarf2out_function_decl,      /* function_decl */
3271   dwarf2out_global_decl,
3272   dwarf2out_type_decl,          /* type_decl */
3273   dwarf2out_imported_module_or_decl,
3274   debug_nothing_tree,           /* deferred_inline_function */
3275   /* The DWARF 2 backend tries to reduce debugging bloat by not
3276      emitting the abstract description of inline functions until
3277      something tries to reference them.  */
3278   dwarf2out_abstract_function,  /* outlining_inline_function */
3279   debug_nothing_rtx,            /* label */
3280   debug_nothing_int,            /* handle_pch */
3281   dwarf2out_var_location,
3282   dwarf2out_switch_text_section,
3283   dwarf2out_set_name,
3284   1,                            /* start_end_main_source_file */
3285   TYPE_SYMTAB_IS_DIE            /* tree_type_symtab_field */
3286 };
3287 \f
3288 /* NOTE: In the comments in this file, many references are made to
3289    "Debugging Information Entries".  This term is abbreviated as `DIE'
3290    throughout the remainder of this file.  */
3291
3292 /* An internal representation of the DWARF output is built, and then
3293    walked to generate the DWARF debugging info.  The walk of the internal
3294    representation is done after the entire program has been compiled.
3295    The types below are used to describe the internal representation.  */
3296
3297 /* Whether to put type DIEs into their own section .debug_types instead
3298    of making them part of the .debug_info section.  Only supported for
3299    Dwarf V4 or higher and the user didn't disable them through
3300    -fno-debug-types-section.  It is more efficient to put them in a
3301    separate comdat sections since the linker will then be able to
3302    remove duplicates.  But not all tools support .debug_types sections
3303    yet.  */
3304
3305 #define use_debug_types (dwarf_version >= 4 && flag_debug_types_section)
3306
3307 /* Various DIE's use offsets relative to the beginning of the
3308    .debug_info section to refer to each other.  */
3309
3310 typedef long int dw_offset;
3311
3312 /* Define typedefs here to avoid circular dependencies.  */
3313
3314 typedef struct dw_attr_struct *dw_attr_ref;
3315 typedef struct dw_line_info_struct *dw_line_info_ref;
3316 typedef struct pubname_struct *pubname_ref;
3317 typedef struct dw_ranges_struct *dw_ranges_ref;
3318 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3319 typedef struct comdat_type_struct *comdat_type_node_ref;
3320
3321 /* The entries in the line_info table more-or-less mirror the opcodes
3322    that are used in the real dwarf line table.  Arrays of these entries
3323    are collected per section when DWARF2_ASM_LINE_DEBUG_INFO is not
3324    supported.  */
3325
3326 enum dw_line_info_opcode {
3327   /* Emit DW_LNE_set_address; the operand is the label index.  */
3328   LI_set_address,
3329
3330   /* Emit a row to the matrix with the given line.  This may be done
3331      via any combination of DW_LNS_copy, DW_LNS_advance_line, and
3332      special opcodes.  */
3333   LI_set_line,
3334
3335   /* Emit a DW_LNS_set_file.  */
3336   LI_set_file,
3337
3338   /* Emit a DW_LNS_set_column.  */
3339   LI_set_column,
3340
3341   /* Emit a DW_LNS_negate_stmt; the operand is ignored.  */
3342   LI_negate_stmt,
3343
3344   /* Emit a DW_LNS_set_prologue_end/epilogue_begin; the operand is ignored.  */
3345   LI_set_prologue_end,
3346   LI_set_epilogue_begin,
3347
3348   /* Emit a DW_LNE_set_discriminator.  */
3349   LI_set_discriminator
3350 };
3351
3352 typedef struct GTY(()) dw_line_info_struct {
3353   enum dw_line_info_opcode opcode;
3354   unsigned int val;
3355 } dw_line_info_entry;
3356
3357 DEF_VEC_O(dw_line_info_entry);
3358 DEF_VEC_ALLOC_O(dw_line_info_entry, gc);
3359
3360 typedef struct GTY(()) dw_line_info_table_struct {
3361   /* The label that marks the end of this section.  */
3362   const char *end_label;
3363
3364   /* The values for the last row of the matrix, as collected in the table.
3365      These are used to minimize the changes to the next row.  */
3366   unsigned int file_num;
3367   unsigned int line_num;
3368   unsigned int column_num;
3369   int discrim_num;
3370   bool is_stmt;
3371   bool in_use;
3372
3373   VEC(dw_line_info_entry, gc) *entries;
3374 } dw_line_info_table;
3375
3376 typedef dw_line_info_table *dw_line_info_table_p;
3377
3378 DEF_VEC_P(dw_line_info_table_p);
3379 DEF_VEC_ALLOC_P(dw_line_info_table_p, gc);
3380
3381 /* Each DIE attribute has a field specifying the attribute kind,
3382    a link to the next attribute in the chain, and an attribute value.
3383    Attributes are typically linked below the DIE they modify.  */
3384
3385 typedef struct GTY(()) dw_attr_struct {
3386   enum dwarf_attribute dw_attr;
3387   dw_val_node dw_attr_val;
3388 }
3389 dw_attr_node;
3390
3391 DEF_VEC_O(dw_attr_node);
3392 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3393
3394 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3395    The children of each node form a circular list linked by
3396    die_sib.  die_child points to the node *before* the "first" child node.  */
3397
3398 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
3399   union die_symbol_or_type_node
3400     {
3401       char * GTY ((tag ("0"))) die_symbol;
3402       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
3403     }
3404   GTY ((desc ("use_debug_types"))) die_id;
3405   VEC(dw_attr_node,gc) * die_attr;
3406   dw_die_ref die_parent;
3407   dw_die_ref die_child;
3408   dw_die_ref die_sib;
3409   dw_die_ref die_definition; /* ref from a specification to its definition */
3410   dw_offset die_offset;
3411   unsigned long die_abbrev;
3412   int die_mark;
3413   /* Die is used and must not be pruned as unused.  */
3414   int die_perennial_p;
3415   unsigned int decl_id;
3416   enum dwarf_tag die_tag;
3417 }
3418 die_node;
3419
3420 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3421 #define FOR_EACH_CHILD(die, c, expr) do {       \
3422   c = die->die_child;                           \
3423   if (c) do {                                   \
3424     c = c->die_sib;                             \
3425     expr;                                       \
3426   } while (c != die->die_child);                \
3427 } while (0)
3428
3429 /* The pubname structure */
3430
3431 typedef struct GTY(()) pubname_struct {
3432   dw_die_ref die;
3433   const char *name;
3434 }
3435 pubname_entry;
3436
3437 DEF_VEC_O(pubname_entry);
3438 DEF_VEC_ALLOC_O(pubname_entry, gc);
3439
3440 struct GTY(()) dw_ranges_struct {
3441   /* If this is positive, it's a block number, otherwise it's a
3442      bitwise-negated index into dw_ranges_by_label.  */
3443   int num;
3444 };
3445
3446 /* A structure to hold a macinfo entry.  */
3447
3448 typedef struct GTY(()) macinfo_struct {
3449   unsigned HOST_WIDE_INT code;
3450   unsigned HOST_WIDE_INT lineno;
3451   const char *info;
3452 }
3453 macinfo_entry;
3454
3455 DEF_VEC_O(macinfo_entry);
3456 DEF_VEC_ALLOC_O(macinfo_entry, gc);
3457
3458 struct GTY(()) dw_ranges_by_label_struct {
3459   const char *begin;
3460   const char *end;
3461 };
3462
3463 /* The comdat type node structure.  */
3464 typedef struct GTY(()) comdat_type_struct
3465 {
3466   dw_die_ref root_die;
3467   dw_die_ref type_die;
3468   char signature[DWARF_TYPE_SIGNATURE_SIZE];
3469   struct comdat_type_struct *next;
3470 }
3471 comdat_type_node;
3472
3473 /* The limbo die list structure.  */
3474 typedef struct GTY(()) limbo_die_struct {
3475   dw_die_ref die;
3476   tree created_for;
3477   struct limbo_die_struct *next;
3478 }
3479 limbo_die_node;
3480
3481 typedef struct skeleton_chain_struct
3482 {
3483   dw_die_ref old_die;
3484   dw_die_ref new_die;
3485   struct skeleton_chain_struct *parent;
3486 }
3487 skeleton_chain_node;
3488
3489 /* How to start an assembler comment.  */
3490 #ifndef ASM_COMMENT_START
3491 #define ASM_COMMENT_START ";#"
3492 #endif
3493
3494 /* Define a macro which returns nonzero for a TYPE_DECL which was
3495    implicitly generated for a type.
3496
3497    Note that, unlike the C front-end (which generates a NULL named
3498    TYPE_DECL node for each complete tagged type, each array type,
3499    and each function type node created) the C++ front-end generates
3500    a _named_ TYPE_DECL node for each tagged type node created.
3501    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3502    generate a DW_TAG_typedef DIE for them.  Likewise with the Ada
3503    front-end, but for each type, tagged or not.  */
3504
3505 #define TYPE_DECL_IS_STUB(decl)                         \
3506   (DECL_NAME (decl) == NULL_TREE                        \
3507    || (DECL_ARTIFICIAL (decl)                           \
3508        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3509            /* This is necessary for stub decls that     \
3510               appear in nested inline functions.  */    \
3511            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3512                && (decl_ultimate_origin (decl)          \
3513                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3514
3515 /* Information concerning the compilation unit's programming
3516    language, and compiler version.  */
3517
3518 /* Fixed size portion of the DWARF compilation unit header.  */
3519 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3520   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3521
3522 /* Fixed size portion of the DWARF comdat type unit header.  */
3523 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
3524   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
3525    + DWARF_OFFSET_SIZE)
3526
3527 /* Fixed size portion of public names info.  */
3528 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3529
3530 /* Fixed size portion of the address range info.  */
3531 #define DWARF_ARANGES_HEADER_SIZE                                       \
3532   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3533                 DWARF2_ADDR_SIZE * 2)                                   \
3534    - DWARF_INITIAL_LENGTH_SIZE)
3535
3536 /* Size of padding portion in the address range info.  It must be
3537    aligned to twice the pointer size.  */
3538 #define DWARF_ARANGES_PAD_SIZE \
3539   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3540                 DWARF2_ADDR_SIZE * 2)                              \
3541    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3542
3543 /* Use assembler line directives if available.  */
3544 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3545 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3546 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3547 #else
3548 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3549 #endif
3550 #endif
3551
3552 /* Minimum line offset in a special line info. opcode.
3553    This value was chosen to give a reasonable range of values.  */
3554 #define DWARF_LINE_BASE  -10
3555
3556 /* First special line opcode - leave room for the standard opcodes.  */
3557 #define DWARF_LINE_OPCODE_BASE  ((int)DW_LNS_set_isa + 1)
3558
3559 /* Range of line offsets in a special line info. opcode.  */
3560 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3561
3562 /* Flag that indicates the initial value of the is_stmt_start flag.
3563    In the present implementation, we do not mark any lines as
3564    the beginning of a source statement, because that information
3565    is not made available by the GCC front-end.  */
3566 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3567
3568 /* Maximum number of operations per instruction bundle.  */
3569 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
3570 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
3571 #endif
3572
3573 /* This location is used by calc_die_sizes() to keep track
3574    the offset of each DIE within the .debug_info section.  */
3575 static unsigned long next_die_offset;
3576
3577 /* Record the root of the DIE's built for the current compilation unit.  */
3578 static GTY(()) dw_die_ref single_comp_unit_die;
3579
3580 /* A list of type DIEs that have been separated into comdat sections.  */
3581 static GTY(()) comdat_type_node *comdat_type_list;
3582
3583 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3584 static GTY(()) limbo_die_node *limbo_die_list;
3585
3586 /* A list of DIEs for which we may have to generate
3587    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
3588 static GTY(()) limbo_die_node *deferred_asm_name;
3589
3590 /* Filenames referenced by this compilation unit.  */
3591 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3592
3593 /* A hash table of references to DIE's that describe declarations.
3594    The key is a DECL_UID() which is a unique number identifying each decl.  */
3595 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3596
3597 /* A hash table of references to DIE's that describe COMMON blocks.
3598    The key is DECL_UID() ^ die_parent.  */
3599 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
3600
3601 typedef struct GTY(()) die_arg_entry_struct {
3602     dw_die_ref die;
3603     tree arg;
3604 } die_arg_entry;
3605
3606 DEF_VEC_O(die_arg_entry);
3607 DEF_VEC_ALLOC_O(die_arg_entry,gc);
3608
3609 /* Node of the variable location list.  */
3610 struct GTY ((chain_next ("%h.next"))) var_loc_node {
3611   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
3612      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
3613      in mode of the EXPR_LIST node and first EXPR_LIST operand
3614      is either NOTE_INSN_VAR_LOCATION for a piece with a known
3615      location or NULL for padding.  For larger bitsizes,
3616      mode is 0 and first operand is a CONCAT with bitsize
3617      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
3618      NULL as second operand.  */
3619   rtx GTY (()) loc;
3620   const char * GTY (()) label;
3621   struct var_loc_node * GTY (()) next;
3622 };
3623
3624 /* Variable location list.  */
3625 struct GTY (()) var_loc_list_def {
3626   struct var_loc_node * GTY (()) first;
3627
3628   /* Pointer to the last but one or last element of the
3629      chained list.  If the list is empty, both first and
3630      last are NULL, if the list contains just one node
3631      or the last node certainly is not redundant, it points
3632      to the last node, otherwise points to the last but one.
3633      Do not mark it for GC because it is marked through the chain.  */
3634   struct var_loc_node * GTY ((skip ("%h"))) last;
3635
3636   /* Pointer to the last element before section switch,
3637      if NULL, either sections weren't switched or first
3638      is after section switch.  */
3639   struct var_loc_node * GTY ((skip ("%h"))) last_before_switch;
3640
3641   /* DECL_UID of the variable decl.  */
3642   unsigned int decl_id;
3643 };
3644 typedef struct var_loc_list_def var_loc_list;
3645
3646 /* Call argument location list.  */
3647 struct GTY ((chain_next ("%h.next"))) call_arg_loc_node {
3648   rtx GTY (()) call_arg_loc_note;
3649   const char * GTY (()) label;
3650   tree GTY (()) block;
3651   bool tail_call_p;
3652   rtx GTY (()) symbol_ref;
3653   struct call_arg_loc_node * GTY (()) next;
3654 };
3655
3656
3657 /* Table of decl location linked lists.  */
3658 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
3659
3660 /* Head and tail of call_arg_loc chain.  */
3661 static GTY (()) struct call_arg_loc_node *call_arg_locations;
3662 static struct call_arg_loc_node *call_arg_loc_last;
3663
3664 /* Number of call sites in the current function.  */
3665 static int call_site_count = -1;
3666 /* Number of tail call sites in the current function.  */
3667 static int tail_call_site_count = -1;
3668
3669 /* Vector mapping block numbers to DW_TAG_{lexical_block,inlined_subroutine}
3670    DIEs.  */
3671 static VEC (dw_die_ref, heap) *block_map;
3672
3673 /* A cached location list.  */
3674 struct GTY (()) cached_dw_loc_list_def {
3675   /* The DECL_UID of the decl that this entry describes.  */
3676   unsigned int decl_id;
3677
3678   /* The cached location list.  */
3679   dw_loc_list_ref loc_list;
3680 };
3681 typedef struct cached_dw_loc_list_def cached_dw_loc_list;
3682
3683 /* Table of cached location lists.  */
3684 static GTY ((param_is (cached_dw_loc_list))) htab_t cached_dw_loc_list_table;
3685
3686 /* A pointer to the base of a list of references to DIE's that
3687    are uniquely identified by their tag, presence/absence of
3688    children DIE's, and list of attribute/value pairs.  */
3689 static GTY((length ("abbrev_die_table_allocated")))
3690   dw_die_ref *abbrev_die_table;
3691
3692 /* Number of elements currently allocated for abbrev_die_table.  */
3693 static GTY(()) unsigned abbrev_die_table_allocated;
3694
3695 /* Number of elements in type_die_table currently in use.  */
3696 static GTY(()) unsigned abbrev_die_table_in_use;
3697
3698 /* Size (in elements) of increments by which we may expand the
3699    abbrev_die_table.  */
3700 #define ABBREV_DIE_TABLE_INCREMENT 256
3701
3702 /* A global counter for generating labels for line number data.  */
3703 static unsigned int line_info_label_num;
3704
3705 /* The current table to which we should emit line number information
3706    for the current function.  This will be set up at the beginning of
3707    assembly for the function.  */
3708 static dw_line_info_table *cur_line_info_table;
3709
3710 /* The two default tables of line number info.  */
3711 static GTY(()) dw_line_info_table *text_section_line_info;
3712 static GTY(()) dw_line_info_table *cold_text_section_line_info;
3713
3714 /* The set of all non-default tables of line number info.  */
3715 static GTY(()) VEC (dw_line_info_table_p, gc) *separate_line_info;
3716
3717 /* A flag to tell pubnames/types export if there is an info section to
3718    refer to.  */
3719 static bool info_section_emitted;
3720
3721 /* A pointer to the base of a table that contains a list of publicly
3722    accessible names.  */
3723 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
3724
3725 /* A pointer to the base of a table that contains a list of publicly
3726    accessible types.  */
3727 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
3728
3729 /* A pointer to the base of a table that contains a list of macro
3730    defines/undefines (and file start/end markers).  */
3731 static GTY (()) VEC (macinfo_entry, gc) * macinfo_table;
3732
3733 /* Array of dies for which we should generate .debug_ranges info.  */
3734 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
3735
3736 /* Number of elements currently allocated for ranges_table.  */
3737 static GTY(()) unsigned ranges_table_allocated;
3738
3739 /* Number of elements in ranges_table currently in use.  */
3740 static GTY(()) unsigned ranges_table_in_use;
3741
3742 /* Array of pairs of labels referenced in ranges_table.  */
3743 static GTY ((length ("ranges_by_label_allocated")))
3744      dw_ranges_by_label_ref ranges_by_label;
3745
3746 /* Number of elements currently allocated for ranges_by_label.  */
3747 static GTY(()) unsigned ranges_by_label_allocated;
3748
3749 /* Number of elements in ranges_by_label currently in use.  */
3750 static GTY(()) unsigned ranges_by_label_in_use;
3751
3752 /* Size (in elements) of increments by which we may expand the
3753    ranges_table.  */
3754 #define RANGES_TABLE_INCREMENT 64
3755
3756 /* Whether we have location lists that need outputting */
3757 static GTY(()) bool have_location_lists;
3758
3759 /* Unique label counter.  */
3760 static GTY(()) unsigned int loclabel_num;
3761
3762 /* Unique label counter for point-of-call tables.  */
3763 static GTY(()) unsigned int poc_label_num;
3764
3765 /* Record whether the function being analyzed contains inlined functions.  */
3766 static int current_function_has_inlines;
3767
3768 /* The last file entry emitted by maybe_emit_file().  */
3769 static GTY(()) struct dwarf_file_data * last_emitted_file;
3770
3771 /* Number of internal labels generated by gen_internal_sym().  */
3772 static GTY(()) int label_num;
3773
3774 /* Cached result of previous call to lookup_filename.  */
3775 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
3776
3777 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
3778
3779 /* Instances of generic types for which we need to generate debug
3780    info that describe their generic parameters and arguments. That
3781    generation needs to happen once all types are properly laid out so
3782    we do it at the end of compilation.  */
3783 static GTY(()) VEC(tree,gc) *generic_type_instances;
3784
3785 /* Offset from the "steady-state frame pointer" to the frame base,
3786    within the current function.  */
3787 static HOST_WIDE_INT frame_pointer_fb_offset;
3788 static bool frame_pointer_fb_offset_valid;
3789
3790 static VEC (dw_die_ref, heap) *base_types;
3791
3792 /* Forward declarations for functions defined in this file.  */
3793
3794 static int is_pseudo_reg (const_rtx);
3795 static tree type_main_variant (tree);
3796 static int is_tagged_type (const_tree);
3797 static const char *dwarf_tag_name (unsigned);
3798 static const char *dwarf_attr_name (unsigned);
3799 static const char *dwarf_form_name (unsigned);
3800 static tree decl_ultimate_origin (const_tree);
3801 static tree decl_class_context (tree);
3802 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
3803 static inline enum dw_val_class AT_class (dw_attr_ref);
3804 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
3805 static inline unsigned AT_flag (dw_attr_ref);
3806 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
3807 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
3808 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
3809 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
3810 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
3811                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
3812 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
3813                                unsigned int, unsigned char *);
3814 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
3815 static hashval_t debug_str_do_hash (const void *);
3816 static int debug_str_eq (const void *, const void *);
3817 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
3818 static inline const char *AT_string (dw_attr_ref);
3819 static enum dwarf_form AT_string_form (dw_attr_ref);
3820 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
3821 static void add_AT_specification (dw_die_ref, dw_die_ref);
3822 static inline dw_die_ref AT_ref (dw_attr_ref);
3823 static inline int AT_ref_external (dw_attr_ref);
3824 static inline void set_AT_ref_external (dw_attr_ref, int);
3825 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
3826 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
3827 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
3828 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
3829                              dw_loc_list_ref);
3830 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
3831 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
3832 static inline rtx AT_addr (dw_attr_ref);
3833 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
3834 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
3835 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
3836 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
3837                            unsigned HOST_WIDE_INT);
3838 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
3839                                unsigned long);
3840 static inline const char *AT_lbl (dw_attr_ref);
3841 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
3842 static const char *get_AT_low_pc (dw_die_ref);
3843 static const char *get_AT_hi_pc (dw_die_ref);
3844 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
3845 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
3846 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
3847 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
3848 static bool is_cxx (void);
3849 static bool is_fortran (void);
3850 static bool is_ada (void);
3851 static void remove_AT (dw_die_ref, enum dwarf_attribute);
3852 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
3853 static void add_child_die (dw_die_ref, dw_die_ref);
3854 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
3855 static dw_die_ref lookup_type_die (tree);
3856 static dw_die_ref strip_naming_typedef (tree, dw_die_ref);
3857 static dw_die_ref lookup_type_die_strip_naming_typedef (tree);
3858 static void equate_type_number_to_die (tree, dw_die_ref);
3859 static hashval_t decl_die_table_hash (const void *);
3860 static int decl_die_table_eq (const void *, const void *);
3861 static dw_die_ref lookup_decl_die (tree);
3862 static hashval_t common_block_die_table_hash (const void *);
3863 static int common_block_die_table_eq (const void *, const void *);
3864 static hashval_t decl_loc_table_hash (const void *);
3865 static int decl_loc_table_eq (const void *, const void *);
3866 static var_loc_list *lookup_decl_loc (const_tree);
3867 static void equate_decl_number_to_die (tree, dw_die_ref);
3868 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
3869 static void print_spaces (FILE *);
3870 static void print_die (dw_die_ref, FILE *);
3871 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
3872 static dw_die_ref pop_compile_unit (dw_die_ref);
3873 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
3874 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
3875 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
3876 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
3877 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
3878 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
3879 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
3880                                    struct md5_ctx *, int *);
3881 struct checksum_attributes;
3882 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
3883 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
3884 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
3885 static void generate_type_signature (dw_die_ref, comdat_type_node *);
3886 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
3887 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
3888 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
3889 static int same_die_p (dw_die_ref, dw_die_ref, int *);
3890 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
3891 static void compute_section_prefix (dw_die_ref);
3892 static int is_type_die (dw_die_ref);
3893 static int is_comdat_die (dw_die_ref);
3894 static int is_symbol_die (dw_die_ref);
3895 static void assign_symbol_names (dw_die_ref);
3896 static void break_out_includes (dw_die_ref);
3897 static int is_declaration_die (dw_die_ref);
3898 static int should_move_die_to_comdat (dw_die_ref);
3899 static dw_die_ref clone_as_declaration (dw_die_ref);
3900 static dw_die_ref clone_die (dw_die_ref);
3901 static dw_die_ref clone_tree (dw_die_ref);
3902 static void copy_declaration_context (dw_die_ref, dw_die_ref);
3903 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
3904 static void generate_skeleton_bottom_up (skeleton_chain_node *);
3905 static dw_die_ref generate_skeleton (dw_die_ref);
3906 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
3907                                                          dw_die_ref);
3908 static void break_out_comdat_types (dw_die_ref);
3909 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
3910 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
3911 static void copy_decls_for_unworthy_types (dw_die_ref);
3912
3913 static hashval_t htab_cu_hash (const void *);
3914 static int htab_cu_eq (const void *, const void *);
3915 static void htab_cu_del (void *);
3916 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
3917 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
3918 static void add_sibling_attributes (dw_die_ref);
3919 static void build_abbrev_table (dw_die_ref);
3920 static void output_location_lists (dw_die_ref);
3921 static int constant_size (unsigned HOST_WIDE_INT);
3922 static unsigned long size_of_die (dw_die_ref);
3923 static void calc_die_sizes (dw_die_ref);
3924 static void calc_base_type_die_sizes (void);
3925 static void mark_dies (dw_die_ref);
3926 static void unmark_dies (dw_die_ref);
3927 static void unmark_all_dies (dw_die_ref);
3928 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
3929 static unsigned long size_of_aranges (void);
3930 static enum dwarf_form value_format (dw_attr_ref);
3931 static void output_value_format (dw_attr_ref);
3932 static void output_abbrev_section (void);
3933 static void output_die_symbol (dw_die_ref);
3934 static void output_die (dw_die_ref);
3935 static void output_compilation_unit_header (void);
3936 static void output_comp_unit (dw_die_ref, int);
3937 static void output_comdat_type_unit (comdat_type_node *);
3938 static const char *dwarf2_name (tree, int);
3939 static void add_pubname (tree, dw_die_ref);
3940 static void add_pubname_string (const char *, dw_die_ref);
3941 static void add_pubtype (tree, dw_die_ref);
3942 static void output_pubnames (VEC (pubname_entry,gc) *);
3943 static void output_aranges (unsigned long);
3944 static unsigned int add_ranges_num (int);
3945 static unsigned int add_ranges (const_tree);
3946 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
3947                                   bool *);
3948 static void output_ranges (void);
3949 static dw_line_info_table *new_line_info_table (void);
3950 static void output_line_info (void);
3951 static void output_file_names (void);
3952 static dw_die_ref base_type_die (tree);
3953 static int is_base_type (tree);
3954 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
3955 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
3956 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
3957 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
3958 static int type_is_enum (const_tree);
3959 static unsigned int dbx_reg_number (const_rtx);
3960 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
3961 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
3962 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
3963                                                 enum var_init_status);
3964 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
3965                                                      enum var_init_status);
3966 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
3967                                          enum var_init_status);
3968 static int is_based_loc (const_rtx);
3969 static int resolve_one_addr (rtx *, void *);
3970 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
3971                                                enum var_init_status);
3972 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
3973                                         enum var_init_status);
3974 static dw_loc_list_ref loc_list_from_tree (tree, int);
3975 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
3976 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
3977 static tree field_type (const_tree);
3978 static unsigned int simple_type_align_in_bits (const_tree);
3979 static unsigned int simple_decl_align_in_bits (const_tree);
3980 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
3981 static HOST_WIDE_INT field_byte_offset (const_tree);
3982 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
3983                                          dw_loc_list_ref);
3984 static void add_data_member_location_attribute (dw_die_ref, tree);
3985 static bool add_const_value_attribute (dw_die_ref, rtx);
3986 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
3987 static void insert_double (double_int, unsigned char *);
3988 static void insert_float (const_rtx, unsigned char *);
3989 static rtx rtl_for_decl_location (tree);
3990 static bool add_location_or_const_value_attribute (dw_die_ref, tree, bool,
3991                                                    enum dwarf_attribute);
3992 static bool tree_add_const_value_attribute (dw_die_ref, tree);
3993 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
3994 static void add_name_attribute (dw_die_ref, const char *);
3995 static void add_gnat_descriptive_type_attribute (dw_die_ref, tree, dw_die_ref);
3996 static void add_comp_dir_attribute (dw_die_ref);
3997 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
3998 static void add_subscript_info (dw_die_ref, tree, bool);
3999 static void add_byte_size_attribute (dw_die_ref, tree);
4000 static void add_bit_offset_attribute (dw_die_ref, tree);
4001 static void add_bit_size_attribute (dw_die_ref, tree);
4002 static void add_prototyped_attribute (dw_die_ref, tree);
4003 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
4004 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4005 static void add_src_coords_attributes (dw_die_ref, tree);
4006 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4007 static void push_decl_scope (tree);
4008 static void pop_decl_scope (void);
4009 static dw_die_ref scope_die_for (tree, dw_die_ref);
4010 static inline int local_scope_p (dw_die_ref);
4011 static inline int class_scope_p (dw_die_ref);
4012 static inline int class_or_namespace_scope_p (dw_die_ref);
4013 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4014 static void add_calling_convention_attribute (dw_die_ref, tree);
4015 static const char *type_tag (const_tree);
4016 static tree member_declared_type (const_tree);
4017 #if 0
4018 static const char *decl_start_label (tree);
4019 #endif
4020 static void gen_array_type_die (tree, dw_die_ref);
4021 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4022 #if 0
4023 static void gen_entry_point_die (tree, dw_die_ref);
4024 #endif
4025 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4026 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
4027 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
4028 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4029 static void gen_formal_types_die (tree, dw_die_ref);
4030 static void gen_subprogram_die (tree, dw_die_ref);
4031 static void gen_variable_die (tree, tree, dw_die_ref);
4032 static void gen_const_die (tree, dw_die_ref);
4033 static void gen_label_die (tree, dw_die_ref);
4034 static void gen_lexical_block_die (tree, dw_die_ref, int);
4035 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4036 static void gen_field_die (tree, dw_die_ref);
4037 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4038 static dw_die_ref gen_compile_unit_die (const char *);
4039 static void gen_inheritance_die (tree, tree, dw_die_ref);
4040 static void gen_member_die (tree, dw_die_ref);
4041 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4042                                                 enum debug_info_usage);
4043 static void gen_subroutine_type_die (tree, dw_die_ref);
4044 static void gen_typedef_die (tree, dw_die_ref);
4045 static void gen_type_die (tree, dw_die_ref);
4046 static void gen_block_die (tree, dw_die_ref, int);
4047 static void decls_for_scope (tree, dw_die_ref, int);
4048 static inline int is_redundant_typedef (const_tree);
4049 static bool is_naming_typedef_decl (const_tree);
4050 static inline dw_die_ref get_context_die (tree);
4051 static void gen_namespace_die (tree, dw_die_ref);
4052 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
4053 static dw_die_ref force_decl_die (tree);
4054 static dw_die_ref force_type_die (tree);
4055 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4056 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
4057 static struct dwarf_file_data * lookup_filename (const char *);
4058 static void retry_incomplete_types (void);
4059 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4060 static void gen_generic_params_dies (tree);
4061 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
4062 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
4063 static void splice_child_die (dw_die_ref, dw_die_ref);
4064 static int file_info_cmp (const void *, const void *);
4065 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4066                                      const char *, const char *);
4067 static void output_loc_list (dw_loc_list_ref);
4068 static char *gen_internal_sym (const char *);
4069
4070 static void prune_unmark_dies (dw_die_ref);
4071 static void prune_unused_types_mark_generic_parms_dies (dw_die_ref);
4072 static void prune_unused_types_mark (dw_die_ref, int);
4073 static void prune_unused_types_walk (dw_die_ref);
4074 static void prune_unused_types_walk_attribs (dw_die_ref);
4075 static void prune_unused_types_prune (dw_die_ref);
4076 static void prune_unused_types (void);
4077 static int maybe_emit_file (struct dwarf_file_data *fd);
4078 static inline const char *AT_vms_delta1 (dw_attr_ref);
4079 static inline const char *AT_vms_delta2 (dw_attr_ref);
4080 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
4081                                      const char *, const char *);
4082 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
4083 static void gen_remaining_tmpl_value_param_die_attribute (void);
4084 static bool generic_type_p (tree);
4085 static void schedule_generic_params_dies_gen (tree t);
4086 static void gen_scheduled_generic_parms_dies (void);
4087
4088 /* Section names used to hold DWARF debugging information.  */
4089 #ifndef DEBUG_INFO_SECTION
4090 #define DEBUG_INFO_SECTION      ".debug_info"
4091 #endif
4092 #ifndef DEBUG_ABBREV_SECTION
4093 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4094 #endif
4095 #ifndef DEBUG_ARANGES_SECTION
4096 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4097 #endif
4098 #ifndef DEBUG_MACINFO_SECTION
4099 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4100 #endif
4101 #ifndef DEBUG_LINE_SECTION
4102 #define DEBUG_LINE_SECTION      ".debug_line"
4103 #endif
4104 #ifndef DEBUG_LOC_SECTION
4105 #define DEBUG_LOC_SECTION       ".debug_loc"
4106 #endif
4107 #ifndef DEBUG_PUBNAMES_SECTION
4108 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4109 #endif
4110 #ifndef DEBUG_PUBTYPES_SECTION
4111 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
4112 #endif
4113 #ifndef DEBUG_STR_SECTION
4114 #define DEBUG_STR_SECTION       ".debug_str"
4115 #endif
4116 #ifndef DEBUG_RANGES_SECTION
4117 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4118 #endif
4119
4120 /* Standard ELF section names for compiled code and data.  */
4121 #ifndef TEXT_SECTION_NAME
4122 #define TEXT_SECTION_NAME       ".text"
4123 #endif
4124
4125 /* Section flags for .debug_str section.  */
4126 #define DEBUG_STR_SECTION_FLAGS \
4127   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4128    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4129    : SECTION_DEBUG)
4130
4131 /* Labels we insert at beginning sections we can reference instead of
4132    the section names themselves.  */
4133
4134 #ifndef TEXT_SECTION_LABEL
4135 #define TEXT_SECTION_LABEL              "Ltext"
4136 #endif
4137 #ifndef COLD_TEXT_SECTION_LABEL
4138 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4139 #endif
4140 #ifndef DEBUG_LINE_SECTION_LABEL
4141 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4142 #endif
4143 #ifndef DEBUG_INFO_SECTION_LABEL
4144 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4145 #endif
4146 #ifndef DEBUG_ABBREV_SECTION_LABEL
4147 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4148 #endif
4149 #ifndef DEBUG_LOC_SECTION_LABEL
4150 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4151 #endif
4152 #ifndef DEBUG_RANGES_SECTION_LABEL
4153 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4154 #endif
4155 #ifndef DEBUG_MACINFO_SECTION_LABEL
4156 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4157 #endif
4158
4159
4160 /* Definitions of defaults for formats and names of various special
4161    (artificial) labels which may be generated within this file (when the -g
4162    options is used and DWARF2_DEBUGGING_INFO is in effect.
4163    If necessary, these may be overridden from within the tm.h file, but
4164    typically, overriding these defaults is unnecessary.  */
4165
4166 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4167 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4168 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4169 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4170 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4171 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4172 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4173 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4174 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4175 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4176
4177 #ifndef TEXT_END_LABEL
4178 #define TEXT_END_LABEL          "Letext"
4179 #endif
4180 #ifndef COLD_END_LABEL
4181 #define COLD_END_LABEL          "Letext_cold"
4182 #endif
4183 #ifndef BLOCK_BEGIN_LABEL
4184 #define BLOCK_BEGIN_LABEL       "LBB"
4185 #endif
4186 #ifndef BLOCK_END_LABEL
4187 #define BLOCK_END_LABEL         "LBE"
4188 #endif
4189 #ifndef LINE_CODE_LABEL
4190 #define LINE_CODE_LABEL         "LM"
4191 #endif
4192
4193 \f
4194 /* Return the root of the DIE's built for the current compilation unit.  */
4195 static dw_die_ref
4196 comp_unit_die (void)
4197 {
4198   if (!single_comp_unit_die)
4199     single_comp_unit_die = gen_compile_unit_die (NULL);
4200   return single_comp_unit_die;
4201 }
4202
4203 /* We allow a language front-end to designate a function that is to be
4204    called to "demangle" any name before it is put into a DIE.  */
4205
4206 static const char *(*demangle_name_func) (const char *);
4207
4208 void
4209 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4210 {
4211   demangle_name_func = func;
4212 }
4213
4214 /* Test if rtl node points to a pseudo register.  */
4215
4216 static inline int
4217 is_pseudo_reg (const_rtx rtl)
4218 {
4219   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4220           || (GET_CODE (rtl) == SUBREG
4221               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4222 }
4223
4224 /* Return a reference to a type, with its const and volatile qualifiers
4225    removed.  */
4226
4227 static inline tree
4228 type_main_variant (tree type)
4229 {
4230   type = TYPE_MAIN_VARIANT (type);
4231
4232   /* ??? There really should be only one main variant among any group of
4233      variants of a given type (and all of the MAIN_VARIANT values for all
4234      members of the group should point to that one type) but sometimes the C
4235      front-end messes this up for array types, so we work around that bug
4236      here.  */
4237   if (TREE_CODE (type) == ARRAY_TYPE)
4238     while (type != TYPE_MAIN_VARIANT (type))
4239       type = TYPE_MAIN_VARIANT (type);
4240
4241   return type;
4242 }
4243
4244 /* Return nonzero if the given type node represents a tagged type.  */
4245
4246 static inline int
4247 is_tagged_type (const_tree type)
4248 {
4249   enum tree_code code = TREE_CODE (type);
4250
4251   return (code == RECORD_TYPE || code == UNION_TYPE
4252           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4253 }
4254
4255 /* Set label to debug_info_section_label + die_offset of a DIE reference.  */
4256
4257 static void
4258 get_ref_die_offset_label (char *label, dw_die_ref ref)
4259 {
4260   sprintf (label, "%s+%ld", debug_info_section_label, ref->die_offset);
4261 }
4262
4263 /* Return die_offset of a DIE reference to a base type.  */
4264
4265 static unsigned long int
4266 get_base_type_offset (dw_die_ref ref)
4267 {
4268   if (ref->die_offset)
4269     return ref->die_offset;
4270   if (comp_unit_die ()->die_abbrev)
4271     {
4272       calc_base_type_die_sizes ();
4273       gcc_assert (ref->die_offset);
4274     }
4275   return ref->die_offset;
4276 }
4277
4278 /* Return die_offset of a DIE reference other than base type.  */
4279
4280 static unsigned long int
4281 get_ref_die_offset (dw_die_ref ref)
4282 {
4283   gcc_assert (ref->die_offset);
4284   return ref->die_offset;
4285 }
4286
4287 /* Convert a DIE tag into its string name.  */
4288
4289 static const char *
4290 dwarf_tag_name (unsigned int tag)
4291 {
4292   switch (tag)
4293     {
4294     case DW_TAG_padding:
4295       return "DW_TAG_padding";
4296     case DW_TAG_array_type:
4297       return "DW_TAG_array_type";
4298     case DW_TAG_class_type:
4299       return "DW_TAG_class_type";
4300     case DW_TAG_entry_point:
4301       return "DW_TAG_entry_point";
4302     case DW_TAG_enumeration_type:
4303       return "DW_TAG_enumeration_type";
4304     case DW_TAG_formal_parameter:
4305       return "DW_TAG_formal_parameter";
4306     case DW_TAG_imported_declaration:
4307       return "DW_TAG_imported_declaration";
4308     case DW_TAG_label:
4309       return "DW_TAG_label";
4310     case DW_TAG_lexical_block:
4311       return "DW_TAG_lexical_block";
4312     case DW_TAG_member:
4313       return "DW_TAG_member";
4314     case DW_TAG_pointer_type:
4315       return "DW_TAG_pointer_type";
4316     case DW_TAG_reference_type:
4317       return "DW_TAG_reference_type";
4318     case DW_TAG_compile_unit:
4319       return "DW_TAG_compile_unit";
4320     case DW_TAG_string_type:
4321       return "DW_TAG_string_type";
4322     case DW_TAG_structure_type:
4323       return "DW_TAG_structure_type";
4324     case DW_TAG_subroutine_type:
4325       return "DW_TAG_subroutine_type";
4326     case DW_TAG_typedef:
4327       return "DW_TAG_typedef";
4328     case DW_TAG_union_type:
4329       return "DW_TAG_union_type";
4330     case DW_TAG_unspecified_parameters:
4331       return "DW_TAG_unspecified_parameters";
4332     case DW_TAG_variant:
4333       return "DW_TAG_variant";
4334     case DW_TAG_common_block:
4335       return "DW_TAG_common_block";
4336     case DW_TAG_common_inclusion:
4337       return "DW_TAG_common_inclusion";
4338     case DW_TAG_inheritance:
4339       return "DW_TAG_inheritance";
4340     case DW_TAG_inlined_subroutine:
4341       return "DW_TAG_inlined_subroutine";
4342     case DW_TAG_module:
4343       return "DW_TAG_module";
4344     case DW_TAG_ptr_to_member_type:
4345       return "DW_TAG_ptr_to_member_type";
4346     case DW_TAG_set_type:
4347       return "DW_TAG_set_type";
4348     case DW_TAG_subrange_type:
4349       return "DW_TAG_subrange_type";
4350     case DW_TAG_with_stmt:
4351       return "DW_TAG_with_stmt";
4352     case DW_TAG_access_declaration:
4353       return "DW_TAG_access_declaration";
4354     case DW_TAG_base_type:
4355       return "DW_TAG_base_type";
4356     case DW_TAG_catch_block:
4357       return "DW_TAG_catch_block";
4358     case DW_TAG_const_type:
4359       return "DW_TAG_const_type";
4360     case DW_TAG_constant:
4361       return "DW_TAG_constant";
4362     case DW_TAG_enumerator:
4363       return "DW_TAG_enumerator";
4364     case DW_TAG_file_type:
4365       return "DW_TAG_file_type";
4366     case DW_TAG_friend:
4367       return "DW_TAG_friend";
4368     case DW_TAG_namelist:
4369       return "DW_TAG_namelist";
4370     case DW_TAG_namelist_item:
4371       return "DW_TAG_namelist_item";
4372     case DW_TAG_packed_type:
4373       return "DW_TAG_packed_type";
4374     case DW_TAG_subprogram:
4375       return "DW_TAG_subprogram";
4376     case DW_TAG_template_type_param:
4377       return "DW_TAG_template_type_param";
4378     case DW_TAG_template_value_param:
4379       return "DW_TAG_template_value_param";
4380     case DW_TAG_thrown_type:
4381       return "DW_TAG_thrown_type";
4382     case DW_TAG_try_block:
4383       return "DW_TAG_try_block";
4384     case DW_TAG_variant_part:
4385       return "DW_TAG_variant_part";
4386     case DW_TAG_variable:
4387       return "DW_TAG_variable";
4388     case DW_TAG_volatile_type:
4389       return "DW_TAG_volatile_type";
4390     case DW_TAG_dwarf_procedure:
4391       return "DW_TAG_dwarf_procedure";
4392     case DW_TAG_restrict_type:
4393       return "DW_TAG_restrict_type";
4394     case DW_TAG_interface_type:
4395       return "DW_TAG_interface_type";
4396     case DW_TAG_namespace:
4397       return "DW_TAG_namespace";
4398     case DW_TAG_imported_module:
4399       return "DW_TAG_imported_module";
4400     case DW_TAG_unspecified_type:
4401       return "DW_TAG_unspecified_type";
4402     case DW_TAG_partial_unit:
4403       return "DW_TAG_partial_unit";
4404     case DW_TAG_imported_unit:
4405       return "DW_TAG_imported_unit";
4406     case DW_TAG_condition:
4407       return "DW_TAG_condition";
4408     case DW_TAG_shared_type:
4409       return "DW_TAG_shared_type";
4410     case DW_TAG_type_unit:
4411       return "DW_TAG_type_unit";
4412     case DW_TAG_rvalue_reference_type:
4413       return "DW_TAG_rvalue_reference_type";
4414     case DW_TAG_template_alias:
4415       return "DW_TAG_template_alias";
4416     case DW_TAG_GNU_template_parameter_pack:
4417       return "DW_TAG_GNU_template_parameter_pack";
4418     case DW_TAG_GNU_formal_parameter_pack:
4419       return "DW_TAG_GNU_formal_parameter_pack";
4420     case DW_TAG_MIPS_loop:
4421       return "DW_TAG_MIPS_loop";
4422     case DW_TAG_format_label:
4423       return "DW_TAG_format_label";
4424     case DW_TAG_function_template:
4425       return "DW_TAG_function_template";
4426     case DW_TAG_class_template:
4427       return "DW_TAG_class_template";
4428     case DW_TAG_GNU_BINCL:
4429       return "DW_TAG_GNU_BINCL";
4430     case DW_TAG_GNU_EINCL:
4431       return "DW_TAG_GNU_EINCL";
4432     case DW_TAG_GNU_template_template_param:
4433       return "DW_TAG_GNU_template_template_param";
4434     case DW_TAG_GNU_call_site:
4435       return "DW_TAG_GNU_call_site";
4436     case DW_TAG_GNU_call_site_parameter:
4437       return "DW_TAG_GNU_call_site_parameter";
4438     default:
4439       return "DW_TAG_<unknown>";
4440     }
4441 }
4442
4443 /* Convert a DWARF attribute code into its string name.  */
4444
4445 static const char *
4446 dwarf_attr_name (unsigned int attr)
4447 {
4448   switch (attr)
4449     {
4450     case DW_AT_sibling:
4451       return "DW_AT_sibling";
4452     case DW_AT_location:
4453       return "DW_AT_location";
4454     case DW_AT_name:
4455       return "DW_AT_name";
4456     case DW_AT_ordering:
4457       return "DW_AT_ordering";
4458     case DW_AT_subscr_data:
4459       return "DW_AT_subscr_data";
4460     case DW_AT_byte_size:
4461       return "DW_AT_byte_size";
4462     case DW_AT_bit_offset:
4463       return "DW_AT_bit_offset";
4464     case DW_AT_bit_size:
4465       return "DW_AT_bit_size";
4466     case DW_AT_element_list:
4467       return "DW_AT_element_list";
4468     case DW_AT_stmt_list:
4469       return "DW_AT_stmt_list";
4470     case DW_AT_low_pc:
4471       return "DW_AT_low_pc";
4472     case DW_AT_high_pc:
4473       return "DW_AT_high_pc";
4474     case DW_AT_language:
4475       return "DW_AT_language";
4476     case DW_AT_member:
4477       return "DW_AT_member";
4478     case DW_AT_discr:
4479       return "DW_AT_discr";
4480     case DW_AT_discr_value:
4481       return "DW_AT_discr_value";
4482     case DW_AT_visibility:
4483       return "DW_AT_visibility";
4484     case DW_AT_import:
4485       return "DW_AT_import";
4486     case DW_AT_string_length:
4487       return "DW_AT_string_length";
4488     case DW_AT_common_reference:
4489       return "DW_AT_common_reference";
4490     case DW_AT_comp_dir:
4491       return "DW_AT_comp_dir";
4492     case DW_AT_const_value:
4493       return "DW_AT_const_value";
4494     case DW_AT_containing_type:
4495       return "DW_AT_containing_type";
4496     case DW_AT_default_value:
4497       return "DW_AT_default_value";
4498     case DW_AT_inline:
4499       return "DW_AT_inline";
4500     case DW_AT_is_optional:
4501       return "DW_AT_is_optional";
4502     case DW_AT_lower_bound:
4503       return "DW_AT_lower_bound";
4504     case DW_AT_producer:
4505       return "DW_AT_producer";
4506     case DW_AT_prototyped:
4507       return "DW_AT_prototyped";
4508     case DW_AT_return_addr:
4509       return "DW_AT_return_addr";
4510     case DW_AT_start_scope:
4511       return "DW_AT_start_scope";
4512     case DW_AT_bit_stride:
4513       return "DW_AT_bit_stride";
4514     case DW_AT_upper_bound:
4515       return "DW_AT_upper_bound";
4516     case DW_AT_abstract_origin:
4517       return "DW_AT_abstract_origin";
4518     case DW_AT_accessibility:
4519       return "DW_AT_accessibility";
4520     case DW_AT_address_class:
4521       return "DW_AT_address_class";
4522     case DW_AT_artificial:
4523       return "DW_AT_artificial";
4524     case DW_AT_base_types:
4525       return "DW_AT_base_types";
4526     case DW_AT_calling_convention:
4527       return "DW_AT_calling_convention";
4528     case DW_AT_count:
4529       return "DW_AT_count";
4530     case DW_AT_data_member_location:
4531       return "DW_AT_data_member_location";
4532     case DW_AT_decl_column:
4533       return "DW_AT_decl_column";
4534     case DW_AT_decl_file:
4535       return "DW_AT_decl_file";
4536     case DW_AT_decl_line:
4537       return "DW_AT_decl_line";
4538     case DW_AT_declaration:
4539       return "DW_AT_declaration";
4540     case DW_AT_discr_list:
4541       return "DW_AT_discr_list";
4542     case DW_AT_encoding:
4543       return "DW_AT_encoding";
4544     case DW_AT_external:
4545       return "DW_AT_external";
4546     case DW_AT_explicit:
4547       return "DW_AT_explicit";
4548     case DW_AT_frame_base:
4549       return "DW_AT_frame_base";
4550     case DW_AT_friend:
4551       return "DW_AT_friend";
4552     case DW_AT_identifier_case:
4553       return "DW_AT_identifier_case";
4554     case DW_AT_macro_info:
4555       return "DW_AT_macro_info";
4556     case DW_AT_namelist_items:
4557       return "DW_AT_namelist_items";
4558     case DW_AT_priority:
4559       return "DW_AT_priority";
4560     case DW_AT_segment:
4561       return "DW_AT_segment";
4562     case DW_AT_specification:
4563       return "DW_AT_specification";
4564     case DW_AT_static_link:
4565       return "DW_AT_static_link";
4566     case DW_AT_type:
4567       return "DW_AT_type";
4568     case DW_AT_use_location:
4569       return "DW_AT_use_location";
4570     case DW_AT_variable_parameter:
4571       return "DW_AT_variable_parameter";
4572     case DW_AT_virtuality:
4573       return "DW_AT_virtuality";
4574     case DW_AT_vtable_elem_location:
4575       return "DW_AT_vtable_elem_location";
4576
4577     case DW_AT_allocated:
4578       return "DW_AT_allocated";
4579     case DW_AT_associated:
4580       return "DW_AT_associated";
4581     case DW_AT_data_location:
4582       return "DW_AT_data_location";
4583     case DW_AT_byte_stride:
4584       return "DW_AT_byte_stride";
4585     case DW_AT_entry_pc:
4586       return "DW_AT_entry_pc";
4587     case DW_AT_use_UTF8:
4588       return "DW_AT_use_UTF8";
4589     case DW_AT_extension:
4590       return "DW_AT_extension";
4591     case DW_AT_ranges:
4592       return "DW_AT_ranges";
4593     case DW_AT_trampoline:
4594       return "DW_AT_trampoline";
4595     case DW_AT_call_column:
4596       return "DW_AT_call_column";
4597     case DW_AT_call_file:
4598       return "DW_AT_call_file";
4599     case DW_AT_call_line:
4600       return "DW_AT_call_line";
4601     case DW_AT_object_pointer:
4602       return "DW_AT_object_pointer";
4603
4604     case DW_AT_signature:
4605       return "DW_AT_signature";
4606     case DW_AT_main_subprogram:
4607       return "DW_AT_main_subprogram";
4608     case DW_AT_data_bit_offset:
4609       return "DW_AT_data_bit_offset";
4610     case DW_AT_const_expr:
4611       return "DW_AT_const_expr";
4612     case DW_AT_enum_class:
4613       return "DW_AT_enum_class";
4614     case DW_AT_linkage_name:
4615       return "DW_AT_linkage_name";
4616
4617     case DW_AT_MIPS_fde:
4618       return "DW_AT_MIPS_fde";
4619     case DW_AT_MIPS_loop_begin:
4620       return "DW_AT_MIPS_loop_begin";
4621     case DW_AT_MIPS_tail_loop_begin:
4622       return "DW_AT_MIPS_tail_loop_begin";
4623     case DW_AT_MIPS_epilog_begin:
4624       return "DW_AT_MIPS_epilog_begin";
4625 #if VMS_DEBUGGING_INFO
4626     case DW_AT_HP_prologue:
4627       return "DW_AT_HP_prologue";
4628 #else
4629     case DW_AT_MIPS_loop_unroll_factor:
4630       return "DW_AT_MIPS_loop_unroll_factor";
4631 #endif
4632     case DW_AT_MIPS_software_pipeline_depth:
4633       return "DW_AT_MIPS_software_pipeline_depth";
4634     case DW_AT_MIPS_linkage_name:
4635       return "DW_AT_MIPS_linkage_name";
4636 #if VMS_DEBUGGING_INFO
4637     case DW_AT_HP_epilogue:
4638       return "DW_AT_HP_epilogue";
4639 #else
4640     case DW_AT_MIPS_stride:
4641       return "DW_AT_MIPS_stride";
4642 #endif
4643     case DW_AT_MIPS_abstract_name:
4644       return "DW_AT_MIPS_abstract_name";
4645     case DW_AT_MIPS_clone_origin:
4646       return "DW_AT_MIPS_clone_origin";
4647     case DW_AT_MIPS_has_inlines:
4648       return "DW_AT_MIPS_has_inlines";
4649
4650     case DW_AT_sf_names:
4651       return "DW_AT_sf_names";
4652     case DW_AT_src_info:
4653       return "DW_AT_src_info";
4654     case DW_AT_mac_info:
4655       return "DW_AT_mac_info";
4656     case DW_AT_src_coords:
4657       return "DW_AT_src_coords";
4658     case DW_AT_body_begin:
4659       return "DW_AT_body_begin";
4660     case DW_AT_body_end:
4661       return "DW_AT_body_end";
4662
4663     case DW_AT_GNU_vector:
4664       return "DW_AT_GNU_vector";
4665     case DW_AT_GNU_guarded_by:
4666       return "DW_AT_GNU_guarded_by";
4667     case DW_AT_GNU_pt_guarded_by:
4668       return "DW_AT_GNU_pt_guarded_by";
4669     case DW_AT_GNU_guarded:
4670       return "DW_AT_GNU_guarded";
4671     case DW_AT_GNU_pt_guarded:
4672       return "DW_AT_GNU_pt_guarded";
4673     case DW_AT_GNU_locks_excluded:
4674       return "DW_AT_GNU_locks_excluded";
4675     case DW_AT_GNU_exclusive_locks_required:
4676       return "DW_AT_GNU_exclusive_locks_required";
4677     case DW_AT_GNU_shared_locks_required:
4678       return "DW_AT_GNU_shared_locks_required";
4679     case DW_AT_GNU_odr_signature:
4680       return "DW_AT_GNU_odr_signature";
4681     case DW_AT_GNU_template_name:
4682       return "DW_AT_GNU_template_name";
4683     case DW_AT_GNU_call_site_value:
4684       return "DW_AT_GNU_call_site_value";
4685     case DW_AT_GNU_call_site_data_value:
4686       return "DW_AT_GNU_call_site_data_value";
4687     case DW_AT_GNU_call_site_target:
4688       return "DW_AT_GNU_call_site_target";
4689     case DW_AT_GNU_call_site_target_clobbered:
4690       return "DW_AT_GNU_call_site_target_clobbered";
4691     case DW_AT_GNU_tail_call:
4692       return "DW_AT_GNU_tail_call";
4693     case DW_AT_GNU_all_tail_call_sites:
4694       return "DW_AT_GNU_all_tail_call_sites";
4695     case DW_AT_GNU_all_call_sites:
4696       return "DW_AT_GNU_all_call_sites";
4697     case DW_AT_GNU_all_source_call_sites:
4698       return "DW_AT_GNU_all_source_call_sites";
4699
4700     case DW_AT_GNAT_descriptive_type:
4701       return "DW_AT_GNAT_descriptive_type";
4702
4703     case DW_AT_VMS_rtnbeg_pd_address:
4704       return "DW_AT_VMS_rtnbeg_pd_address";
4705
4706     default:
4707       return "DW_AT_<unknown>";
4708     }
4709 }
4710
4711 /* Convert a DWARF value form code into its string name.  */
4712
4713 static const char *
4714 dwarf_form_name (unsigned int form)
4715 {
4716   switch (form)
4717     {
4718     case DW_FORM_addr:
4719       return "DW_FORM_addr";
4720     case DW_FORM_block2:
4721       return "DW_FORM_block2";
4722     case DW_FORM_block4:
4723       return "DW_FORM_block4";
4724     case DW_FORM_data2:
4725       return "DW_FORM_data2";
4726     case DW_FORM_data4:
4727       return "DW_FORM_data4";
4728     case DW_FORM_data8:
4729       return "DW_FORM_data8";
4730     case DW_FORM_string:
4731       return "DW_FORM_string";
4732     case DW_FORM_block:
4733       return "DW_FORM_block";
4734     case DW_FORM_block1:
4735       return "DW_FORM_block1";
4736     case DW_FORM_data1:
4737       return "DW_FORM_data1";
4738     case DW_FORM_flag:
4739       return "DW_FORM_flag";
4740     case DW_FORM_sdata:
4741       return "DW_FORM_sdata";
4742     case DW_FORM_strp:
4743       return "DW_FORM_strp";
4744     case DW_FORM_udata:
4745       return "DW_FORM_udata";
4746     case DW_FORM_ref_addr:
4747       return "DW_FORM_ref_addr";
4748     case DW_FORM_ref1:
4749       return "DW_FORM_ref1";
4750     case DW_FORM_ref2:
4751       return "DW_FORM_ref2";
4752     case DW_FORM_ref4:
4753       return "DW_FORM_ref4";
4754     case DW_FORM_ref8:
4755       return "DW_FORM_ref8";
4756     case DW_FORM_ref_udata:
4757       return "DW_FORM_ref_udata";
4758     case DW_FORM_indirect:
4759       return "DW_FORM_indirect";
4760     case DW_FORM_sec_offset:
4761       return "DW_FORM_sec_offset";
4762     case DW_FORM_exprloc:
4763       return "DW_FORM_exprloc";
4764     case DW_FORM_flag_present:
4765       return "DW_FORM_flag_present";
4766     case DW_FORM_ref_sig8:
4767       return "DW_FORM_ref_sig8";
4768     default:
4769       return "DW_FORM_<unknown>";
4770     }
4771 }
4772 \f
4773 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4774    instance of an inlined instance of a decl which is local to an inline
4775    function, so we have to trace all of the way back through the origin chain
4776    to find out what sort of node actually served as the original seed for the
4777    given block.  */
4778
4779 static tree
4780 decl_ultimate_origin (const_tree decl)
4781 {
4782   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4783     return NULL_TREE;
4784
4785   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4786      nodes in the function to point to themselves; ignore that if
4787      we're trying to output the abstract instance of this function.  */
4788   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4789     return NULL_TREE;
4790
4791   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4792      most distant ancestor, this should never happen.  */
4793   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4794
4795   return DECL_ABSTRACT_ORIGIN (decl);
4796 }
4797
4798 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
4799    of a virtual function may refer to a base class, so we check the 'this'
4800    parameter.  */
4801
4802 static tree
4803 decl_class_context (tree decl)
4804 {
4805   tree context = NULL_TREE;
4806
4807   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
4808     context = DECL_CONTEXT (decl);
4809   else
4810     context = TYPE_MAIN_VARIANT
4811       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4812
4813   if (context && !TYPE_P (context))
4814     context = NULL_TREE;
4815
4816   return context;
4817 }
4818 \f
4819 /* Add an attribute/value pair to a DIE.  */
4820
4821 static inline void
4822 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
4823 {
4824   /* Maybe this should be an assert?  */
4825   if (die == NULL)
4826     return;
4827
4828   if (die->die_attr == NULL)
4829     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
4830   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
4831 }
4832
4833 static inline enum dw_val_class
4834 AT_class (dw_attr_ref a)
4835 {
4836   return a->dw_attr_val.val_class;
4837 }
4838
4839 /* Add a flag value attribute to a DIE.  */
4840
4841 static inline void
4842 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
4843 {
4844   dw_attr_node attr;
4845
4846   attr.dw_attr = attr_kind;
4847   attr.dw_attr_val.val_class = dw_val_class_flag;
4848   attr.dw_attr_val.v.val_flag = flag;
4849   add_dwarf_attr (die, &attr);
4850 }
4851
4852 static inline unsigned
4853 AT_flag (dw_attr_ref a)
4854 {
4855   gcc_assert (a && AT_class (a) == dw_val_class_flag);
4856   return a->dw_attr_val.v.val_flag;
4857 }
4858
4859 /* Add a signed integer attribute value to a DIE.  */
4860
4861 static inline void
4862 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
4863 {
4864   dw_attr_node attr;
4865
4866   attr.dw_attr = attr_kind;
4867   attr.dw_attr_val.val_class = dw_val_class_const;
4868   attr.dw_attr_val.v.val_int = int_val;
4869   add_dwarf_attr (die, &attr);
4870 }
4871
4872 static inline HOST_WIDE_INT
4873 AT_int (dw_attr_ref a)
4874 {
4875   gcc_assert (a && AT_class (a) == dw_val_class_const);
4876   return a->dw_attr_val.v.val_int;
4877 }
4878
4879 /* Add an unsigned integer attribute value to a DIE.  */
4880
4881 static inline void
4882 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
4883                  unsigned HOST_WIDE_INT unsigned_val)
4884 {
4885   dw_attr_node attr;
4886
4887   attr.dw_attr = attr_kind;
4888   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
4889   attr.dw_attr_val.v.val_unsigned = unsigned_val;
4890   add_dwarf_attr (die, &attr);
4891 }
4892
4893 static inline unsigned HOST_WIDE_INT
4894 AT_unsigned (dw_attr_ref a)
4895 {
4896   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
4897   return a->dw_attr_val.v.val_unsigned;
4898 }
4899
4900 /* Add an unsigned double integer attribute value to a DIE.  */
4901
4902 static inline void
4903 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
4904                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
4905 {
4906   dw_attr_node attr;
4907
4908   attr.dw_attr = attr_kind;
4909   attr.dw_attr_val.val_class = dw_val_class_const_double;
4910   attr.dw_attr_val.v.val_double.high = high;
4911   attr.dw_attr_val.v.val_double.low = low;
4912   add_dwarf_attr (die, &attr);
4913 }
4914
4915 /* Add a floating point attribute value to a DIE and return it.  */
4916
4917 static inline void
4918 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
4919             unsigned int length, unsigned int elt_size, unsigned char *array)
4920 {
4921   dw_attr_node attr;
4922
4923   attr.dw_attr = attr_kind;
4924   attr.dw_attr_val.val_class = dw_val_class_vec;
4925   attr.dw_attr_val.v.val_vec.length = length;
4926   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
4927   attr.dw_attr_val.v.val_vec.array = array;
4928   add_dwarf_attr (die, &attr);
4929 }
4930
4931 /* Add an 8-byte data attribute value to a DIE.  */
4932
4933 static inline void
4934 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
4935               unsigned char data8[8])
4936 {
4937   dw_attr_node attr;
4938
4939   attr.dw_attr = attr_kind;
4940   attr.dw_attr_val.val_class = dw_val_class_data8;
4941   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
4942   add_dwarf_attr (die, &attr);
4943 }
4944
4945 /* Hash and equality functions for debug_str_hash.  */
4946
4947 static hashval_t
4948 debug_str_do_hash (const void *x)
4949 {
4950   return htab_hash_string (((const struct indirect_string_node *)x)->str);
4951 }
4952
4953 static int
4954 debug_str_eq (const void *x1, const void *x2)
4955 {
4956   return strcmp ((((const struct indirect_string_node *)x1)->str),
4957                  (const char *)x2) == 0;
4958 }
4959
4960 /* Add STR to the indirect string hash table.  */
4961
4962 static struct indirect_string_node *
4963 find_AT_string (const char *str)
4964 {
4965   struct indirect_string_node *node;
4966   void **slot;
4967
4968   if (! debug_str_hash)
4969     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
4970                                       debug_str_eq, NULL);
4971
4972   slot = htab_find_slot_with_hash (debug_str_hash, str,
4973                                    htab_hash_string (str), INSERT);
4974   if (*slot == NULL)
4975     {
4976       node = ggc_alloc_cleared_indirect_string_node ();
4977       node->str = ggc_strdup (str);
4978       *slot = node;
4979     }
4980   else
4981     node = (struct indirect_string_node *) *slot;
4982
4983   node->refcount++;
4984   return node;
4985 }
4986
4987 /* Add a string attribute value to a DIE.  */
4988
4989 static inline void
4990 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
4991 {
4992   dw_attr_node attr;
4993   struct indirect_string_node *node;
4994
4995   node = find_AT_string (str);
4996
4997   attr.dw_attr = attr_kind;
4998   attr.dw_attr_val.val_class = dw_val_class_str;
4999   attr.dw_attr_val.v.val_str = node;
5000   add_dwarf_attr (die, &attr);
5001 }
5002
5003 static inline const char *
5004 AT_string (dw_attr_ref a)
5005 {
5006   gcc_assert (a && AT_class (a) == dw_val_class_str);
5007   return a->dw_attr_val.v.val_str->str;
5008 }
5009
5010 /* Find out whether a string should be output inline in DIE
5011    or out-of-line in .debug_str section.  */
5012
5013 static enum dwarf_form
5014 AT_string_form (dw_attr_ref a)
5015 {
5016   struct indirect_string_node *node;
5017   unsigned int len;
5018   char label[32];
5019
5020   gcc_assert (a && AT_class (a) == dw_val_class_str);
5021
5022   node = a->dw_attr_val.v.val_str;
5023   if (node->form)
5024     return node->form;
5025
5026   len = strlen (node->str) + 1;
5027
5028   /* If the string is shorter or equal to the size of the reference, it is
5029      always better to put it inline.  */
5030   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5031     return node->form = DW_FORM_string;
5032
5033   /* If we cannot expect the linker to merge strings in .debug_str
5034      section, only put it into .debug_str if it is worth even in this
5035      single module.  */
5036   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
5037       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
5038       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
5039     return node->form = DW_FORM_string;
5040
5041   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5042   ++dw2_string_counter;
5043   node->label = xstrdup (label);
5044
5045   return node->form = DW_FORM_strp;
5046 }
5047
5048 /* Add a DIE reference attribute value to a DIE.  */
5049
5050 static inline void
5051 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5052 {
5053   dw_attr_node attr;
5054
5055 #ifdef ENABLE_CHECKING
5056   gcc_assert (targ_die != NULL);
5057 #else
5058   /* With LTO we can end up trying to reference something we didn't create
5059      a DIE for.  Avoid crashing later on a NULL referenced DIE.  */
5060   if (targ_die == NULL)
5061     return;
5062 #endif
5063
5064   attr.dw_attr = attr_kind;
5065   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5066   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5067   attr.dw_attr_val.v.val_die_ref.external = 0;
5068   add_dwarf_attr (die, &attr);
5069 }
5070
5071 /* Add an AT_specification attribute to a DIE, and also make the back
5072    pointer from the specification to the definition.  */
5073
5074 static inline void
5075 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5076 {
5077   add_AT_die_ref (die, DW_AT_specification, targ_die);
5078   gcc_assert (!targ_die->die_definition);
5079   targ_die->die_definition = die;
5080 }
5081
5082 static inline dw_die_ref
5083 AT_ref (dw_attr_ref a)
5084 {
5085   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5086   return a->dw_attr_val.v.val_die_ref.die;
5087 }
5088
5089 static inline int
5090 AT_ref_external (dw_attr_ref a)
5091 {
5092   if (a && AT_class (a) == dw_val_class_die_ref)
5093     return a->dw_attr_val.v.val_die_ref.external;
5094
5095   return 0;
5096 }
5097
5098 static inline void
5099 set_AT_ref_external (dw_attr_ref a, int i)
5100 {
5101   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5102   a->dw_attr_val.v.val_die_ref.external = i;
5103 }
5104
5105 /* Add an FDE reference attribute value to a DIE.  */
5106
5107 static inline void
5108 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5109 {
5110   dw_attr_node attr;
5111
5112   attr.dw_attr = attr_kind;
5113   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5114   attr.dw_attr_val.v.val_fde_index = targ_fde;
5115   add_dwarf_attr (die, &attr);
5116 }
5117
5118 /* Add a location description attribute value to a DIE.  */
5119
5120 static inline void
5121 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5122 {
5123   dw_attr_node attr;
5124
5125   attr.dw_attr = attr_kind;
5126   attr.dw_attr_val.val_class = dw_val_class_loc;
5127   attr.dw_attr_val.v.val_loc = loc;
5128   add_dwarf_attr (die, &attr);
5129 }
5130
5131 static inline dw_loc_descr_ref
5132 AT_loc (dw_attr_ref a)
5133 {
5134   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5135   return a->dw_attr_val.v.val_loc;
5136 }
5137
5138 static inline void
5139 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5140 {
5141   dw_attr_node attr;
5142
5143   attr.dw_attr = attr_kind;
5144   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5145   attr.dw_attr_val.v.val_loc_list = loc_list;
5146   add_dwarf_attr (die, &attr);
5147   have_location_lists = true;
5148 }
5149
5150 static inline dw_loc_list_ref
5151 AT_loc_list (dw_attr_ref a)
5152 {
5153   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5154   return a->dw_attr_val.v.val_loc_list;
5155 }
5156
5157 static inline dw_loc_list_ref *
5158 AT_loc_list_ptr (dw_attr_ref a)
5159 {
5160   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5161   return &a->dw_attr_val.v.val_loc_list;
5162 }
5163
5164 /* Add an address constant attribute value to a DIE.  */
5165
5166 static inline void
5167 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5168 {
5169   dw_attr_node attr;
5170
5171   attr.dw_attr = attr_kind;
5172   attr.dw_attr_val.val_class = dw_val_class_addr;
5173   attr.dw_attr_val.v.val_addr = addr;
5174   add_dwarf_attr (die, &attr);
5175 }
5176
5177 /* Get the RTX from to an address DIE attribute.  */
5178
5179 static inline rtx
5180 AT_addr (dw_attr_ref a)
5181 {
5182   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5183   return a->dw_attr_val.v.val_addr;
5184 }
5185
5186 /* Add a file attribute value to a DIE.  */
5187
5188 static inline void
5189 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5190              struct dwarf_file_data *fd)
5191 {
5192   dw_attr_node attr;
5193
5194   attr.dw_attr = attr_kind;
5195   attr.dw_attr_val.val_class = dw_val_class_file;
5196   attr.dw_attr_val.v.val_file = fd;
5197   add_dwarf_attr (die, &attr);
5198 }
5199
5200 /* Get the dwarf_file_data from a file DIE attribute.  */
5201
5202 static inline struct dwarf_file_data *
5203 AT_file (dw_attr_ref a)
5204 {
5205   gcc_assert (a && AT_class (a) == dw_val_class_file);
5206   return a->dw_attr_val.v.val_file;
5207 }
5208
5209 /* Add a vms delta attribute value to a DIE.  */
5210
5211 static inline void
5212 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
5213                   const char *lbl1, const char *lbl2)
5214 {
5215   dw_attr_node attr;
5216
5217   attr.dw_attr = attr_kind;
5218   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
5219   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
5220   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
5221   add_dwarf_attr (die, &attr);
5222 }
5223
5224 /* Add a label identifier attribute value to a DIE.  */
5225
5226 static inline void
5227 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5228 {
5229   dw_attr_node attr;
5230
5231   attr.dw_attr = attr_kind;
5232   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5233   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5234   add_dwarf_attr (die, &attr);
5235 }
5236
5237 /* Add a section offset attribute value to a DIE, an offset into the
5238    debug_line section.  */
5239
5240 static inline void
5241 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5242                 const char *label)
5243 {
5244   dw_attr_node attr;
5245
5246   attr.dw_attr = attr_kind;
5247   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5248   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5249   add_dwarf_attr (die, &attr);
5250 }
5251
5252 /* Add a section offset attribute value to a DIE, an offset into the
5253    debug_macinfo section.  */
5254
5255 static inline void
5256 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5257                const char *label)
5258 {
5259   dw_attr_node attr;
5260
5261   attr.dw_attr = attr_kind;
5262   attr.dw_attr_val.val_class = dw_val_class_macptr;
5263   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5264   add_dwarf_attr (die, &attr);
5265 }
5266
5267 /* Add an offset attribute value to a DIE.  */
5268
5269 static inline void
5270 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5271                unsigned HOST_WIDE_INT offset)
5272 {
5273   dw_attr_node attr;
5274
5275   attr.dw_attr = attr_kind;
5276   attr.dw_attr_val.val_class = dw_val_class_offset;
5277   attr.dw_attr_val.v.val_offset = offset;
5278   add_dwarf_attr (die, &attr);
5279 }
5280
5281 /* Add an range_list attribute value to a DIE.  */
5282
5283 static void
5284 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5285                    long unsigned int offset)
5286 {
5287   dw_attr_node attr;
5288
5289   attr.dw_attr = attr_kind;
5290   attr.dw_attr_val.val_class = dw_val_class_range_list;
5291   attr.dw_attr_val.v.val_offset = offset;
5292   add_dwarf_attr (die, &attr);
5293 }
5294
5295 /* Return the start label of a delta attribute.  */
5296
5297 static inline const char *
5298 AT_vms_delta1 (dw_attr_ref a)
5299 {
5300   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5301   return a->dw_attr_val.v.val_vms_delta.lbl1;
5302 }
5303
5304 /* Return the end label of a delta attribute.  */
5305
5306 static inline const char *
5307 AT_vms_delta2 (dw_attr_ref a)
5308 {
5309   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
5310   return a->dw_attr_val.v.val_vms_delta.lbl2;
5311 }
5312
5313 static inline const char *
5314 AT_lbl (dw_attr_ref a)
5315 {
5316   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5317                     || AT_class (a) == dw_val_class_lineptr
5318                     || AT_class (a) == dw_val_class_macptr));
5319   return a->dw_attr_val.v.val_lbl_id;
5320 }
5321
5322 /* Get the attribute of type attr_kind.  */
5323
5324 static dw_attr_ref
5325 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5326 {
5327   dw_attr_ref a;
5328   unsigned ix;
5329   dw_die_ref spec = NULL;
5330
5331   if (! die)
5332     return NULL;
5333
5334   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5335     if (a->dw_attr == attr_kind)
5336       return a;
5337     else if (a->dw_attr == DW_AT_specification
5338              || a->dw_attr == DW_AT_abstract_origin)
5339       spec = AT_ref (a);
5340
5341   if (spec)
5342     return get_AT (spec, attr_kind);
5343
5344   return NULL;
5345 }
5346
5347 /* Return the "low pc" attribute value, typically associated with a subprogram
5348    DIE.  Return null if the "low pc" attribute is either not present, or if it
5349    cannot be represented as an assembler label identifier.  */
5350
5351 static inline const char *
5352 get_AT_low_pc (dw_die_ref die)
5353 {
5354   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5355
5356   return a ? AT_lbl (a) : NULL;
5357 }
5358
5359 /* Return the "high pc" attribute value, typically associated with a subprogram
5360    DIE.  Return null if the "high pc" attribute is either not present, or if it
5361    cannot be represented as an assembler label identifier.  */
5362
5363 static inline const char *
5364 get_AT_hi_pc (dw_die_ref die)
5365 {
5366   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5367
5368   return a ? AT_lbl (a) : NULL;
5369 }
5370
5371 /* Return the value of the string attribute designated by ATTR_KIND, or
5372    NULL if it is not present.  */
5373
5374 static inline const char *
5375 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5376 {
5377   dw_attr_ref a = get_AT (die, attr_kind);
5378
5379   return a ? AT_string (a) : NULL;
5380 }
5381
5382 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5383    if it is not present.  */
5384
5385 static inline int
5386 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5387 {
5388   dw_attr_ref a = get_AT (die, attr_kind);
5389
5390   return a ? AT_flag (a) : 0;
5391 }
5392
5393 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5394    if it is not present.  */
5395
5396 static inline unsigned
5397 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5398 {
5399   dw_attr_ref a = get_AT (die, attr_kind);
5400
5401   return a ? AT_unsigned (a) : 0;
5402 }
5403
5404 static inline dw_die_ref
5405 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5406 {
5407   dw_attr_ref a = get_AT (die, attr_kind);
5408
5409   return a ? AT_ref (a) : NULL;
5410 }
5411
5412 static inline struct dwarf_file_data *
5413 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5414 {
5415   dw_attr_ref a = get_AT (die, attr_kind);
5416
5417   return a ? AT_file (a) : NULL;
5418 }
5419
5420 /* Return TRUE if the language is C++.  */
5421
5422 static inline bool
5423 is_cxx (void)
5424 {
5425   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5426
5427   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5428 }
5429
5430 /* Return TRUE if the language is Fortran.  */
5431
5432 static inline bool
5433 is_fortran (void)
5434 {
5435   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5436
5437   return (lang == DW_LANG_Fortran77
5438           || lang == DW_LANG_Fortran90
5439           || lang == DW_LANG_Fortran95);
5440 }
5441
5442 /* Return TRUE if the language is Ada.  */
5443
5444 static inline bool
5445 is_ada (void)
5446 {
5447   unsigned int lang = get_AT_unsigned (comp_unit_die (), DW_AT_language);
5448
5449   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5450 }
5451
5452 /* Remove the specified attribute if present.  */
5453
5454 static void
5455 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5456 {
5457   dw_attr_ref a;
5458   unsigned ix;
5459
5460   if (! die)
5461     return;
5462
5463   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
5464     if (a->dw_attr == attr_kind)
5465       {
5466         if (AT_class (a) == dw_val_class_str)
5467           if (a->dw_attr_val.v.val_str->refcount)
5468             a->dw_attr_val.v.val_str->refcount--;
5469
5470         /* VEC_ordered_remove should help reduce the number of abbrevs
5471            that are needed.  */
5472         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5473         return;
5474       }
5475 }
5476
5477 /* Remove CHILD from its parent.  PREV must have the property that
5478    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5479
5480 static void
5481 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5482 {
5483   gcc_assert (child->die_parent == prev->die_parent);
5484   gcc_assert (prev->die_sib == child);
5485   if (prev == child)
5486     {
5487       gcc_assert (child->die_parent->die_child == child);
5488       prev = NULL;
5489     }
5490   else
5491     prev->die_sib = child->die_sib;
5492   if (child->die_parent->die_child == child)
5493     child->die_parent->die_child = prev;
5494 }
5495
5496 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
5497    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
5498
5499 static void
5500 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
5501 {
5502   dw_die_ref parent = old_child->die_parent;
5503
5504   gcc_assert (parent == prev->die_parent);
5505   gcc_assert (prev->die_sib == old_child);
5506
5507   new_child->die_parent = parent;
5508   if (prev == old_child)
5509     {
5510       gcc_assert (parent->die_child == old_child);
5511       new_child->die_sib = new_child;
5512     }
5513   else
5514     {
5515       prev->die_sib = new_child;
5516       new_child->die_sib = old_child->die_sib;
5517     }
5518   if (old_child->die_parent->die_child == old_child)
5519     old_child->die_parent->die_child = new_child;
5520 }
5521
5522 /* Move all children from OLD_PARENT to NEW_PARENT.  */
5523
5524 static void
5525 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
5526 {
5527   dw_die_ref c;
5528   new_parent->die_child = old_parent->die_child;
5529   old_parent->die_child = NULL;
5530   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
5531 }
5532
5533 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5534    matches TAG.  */
5535
5536 static void
5537 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5538 {
5539   dw_die_ref c;
5540
5541   c = die->die_child;
5542   if (c) do {
5543     dw_die_ref prev = c;
5544     c = c->die_sib;
5545     while (c->die_tag == tag)
5546       {
5547         remove_child_with_prev (c, prev);
5548         /* Might have removed every child.  */
5549         if (c == c->die_sib)
5550           return;
5551         c = c->die_sib;
5552       }
5553   } while (c != die->die_child);
5554 }
5555
5556 /* Add a CHILD_DIE as the last child of DIE.  */
5557
5558 static void
5559 add_child_die (dw_die_ref die, dw_die_ref child_die)
5560 {
5561   /* FIXME this should probably be an assert.  */
5562   if (! die || ! child_die)
5563     return;
5564   gcc_assert (die != child_die);
5565
5566   child_die->die_parent = die;
5567   if (die->die_child)
5568     {
5569       child_die->die_sib = die->die_child->die_sib;
5570       die->die_child->die_sib = child_die;
5571     }
5572   else
5573     child_die->die_sib = child_die;
5574   die->die_child = child_die;
5575 }
5576
5577 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5578    is the specification, to the end of PARENT's list of children.
5579    This is done by removing and re-adding it.  */
5580
5581 static void
5582 splice_child_die (dw_die_ref parent, dw_die_ref child)
5583 {
5584   dw_die_ref p;
5585
5586   /* We want the declaration DIE from inside the class, not the
5587      specification DIE at toplevel.  */
5588   if (child->die_parent != parent)
5589     {
5590       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5591
5592       if (tmp)
5593         child = tmp;
5594     }
5595
5596   gcc_assert (child->die_parent == parent
5597               || (child->die_parent
5598                   == get_AT_ref (parent, DW_AT_specification)));
5599
5600   for (p = child->die_parent->die_child; ; p = p->die_sib)
5601     if (p->die_sib == child)
5602       {
5603         remove_child_with_prev (child, p);
5604         break;
5605       }
5606
5607   add_child_die (parent, child);
5608 }
5609
5610 /* Return a pointer to a newly created DIE node.  */
5611
5612 static inline dw_die_ref
5613 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5614 {
5615   dw_die_ref die = ggc_alloc_cleared_die_node ();
5616
5617   die->die_tag = tag_value;
5618
5619   if (parent_die != NULL)
5620     add_child_die (parent_die, die);
5621   else
5622     {
5623       limbo_die_node *limbo_node;
5624
5625       limbo_node = ggc_alloc_cleared_limbo_die_node ();
5626       limbo_node->die = die;
5627       limbo_node->created_for = t;
5628       limbo_node->next = limbo_die_list;
5629       limbo_die_list = limbo_node;
5630     }
5631
5632   return die;
5633 }
5634
5635 /* Return the DIE associated with the given type specifier.  */
5636
5637 static inline dw_die_ref
5638 lookup_type_die (tree type)
5639 {
5640   return TYPE_SYMTAB_DIE (type);
5641 }
5642
5643 /* Given a TYPE_DIE representing the type TYPE, if TYPE is an
5644    anonymous type named by the typedef TYPE_DIE, return the DIE of the
5645    anonymous type instead the one of the naming typedef.  */
5646
5647 static inline dw_die_ref
5648 strip_naming_typedef (tree type, dw_die_ref type_die)
5649 {
5650   if (type
5651       && TREE_CODE (type) == RECORD_TYPE
5652       && type_die
5653       && type_die->die_tag == DW_TAG_typedef
5654       && is_naming_typedef_decl (TYPE_NAME (type)))
5655     type_die = get_AT_ref (type_die, DW_AT_type);
5656   return type_die;
5657 }
5658
5659 /* Like lookup_type_die, but if type is an anonymous type named by a
5660    typedef[1], return the DIE of the anonymous type instead the one of
5661    the naming typedef.  This is because in gen_typedef_die, we did
5662    equate the anonymous struct named by the typedef with the DIE of
5663    the naming typedef. So by default, lookup_type_die on an anonymous
5664    struct yields the DIE of the naming typedef.
5665
5666    [1]: Read the comment of is_naming_typedef_decl to learn about what
5667    a naming typedef is.  */
5668
5669 static inline dw_die_ref
5670 lookup_type_die_strip_naming_typedef (tree type)
5671 {
5672   dw_die_ref die = lookup_type_die (type);
5673   return strip_naming_typedef (type, die);
5674 }
5675
5676 /* Equate a DIE to a given type specifier.  */
5677
5678 static inline void
5679 equate_type_number_to_die (tree type, dw_die_ref type_die)
5680 {
5681   TYPE_SYMTAB_DIE (type) = type_die;
5682 }
5683
5684 /* Returns a hash value for X (which really is a die_struct).  */
5685
5686 static hashval_t
5687 decl_die_table_hash (const void *x)
5688 {
5689   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5690 }
5691
5692 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5693
5694 static int
5695 decl_die_table_eq (const void *x, const void *y)
5696 {
5697   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5698 }
5699
5700 /* Return the DIE associated with a given declaration.  */
5701
5702 static inline dw_die_ref
5703 lookup_decl_die (tree decl)
5704 {
5705   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5706 }
5707
5708 /* Returns a hash value for X (which really is a var_loc_list).  */
5709
5710 static hashval_t
5711 decl_loc_table_hash (const void *x)
5712 {
5713   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5714 }
5715
5716 /* Return nonzero if decl_id of var_loc_list X is the same as
5717    UID of decl *Y.  */
5718
5719 static int
5720 decl_loc_table_eq (const void *x, const void *y)
5721 {
5722   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5723 }
5724
5725 /* Return the var_loc list associated with a given declaration.  */
5726
5727 static inline var_loc_list *
5728 lookup_decl_loc (const_tree decl)
5729 {
5730   if (!decl_loc_table)
5731     return NULL;
5732   return (var_loc_list *)
5733     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5734 }
5735
5736 /* Returns a hash value for X (which really is a cached_dw_loc_list_list).  */
5737
5738 static hashval_t
5739 cached_dw_loc_list_table_hash (const void *x)
5740 {
5741   return (hashval_t) ((const cached_dw_loc_list *) x)->decl_id;
5742 }
5743
5744 /* Return nonzero if decl_id of cached_dw_loc_list X is the same as
5745    UID of decl *Y.  */
5746
5747 static int
5748 cached_dw_loc_list_table_eq (const void *x, const void *y)
5749 {
5750   return (((const cached_dw_loc_list *) x)->decl_id
5751           == DECL_UID ((const_tree) y));
5752 }
5753
5754 /* Equate a DIE to a particular declaration.  */
5755
5756 static void
5757 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5758 {
5759   unsigned int decl_id = DECL_UID (decl);
5760   void **slot;
5761
5762   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5763   *slot = decl_die;
5764   decl_die->decl_id = decl_id;
5765 }
5766
5767 /* Return how many bits covers PIECE EXPR_LIST.  */
5768
5769 static int
5770 decl_piece_bitsize (rtx piece)
5771 {
5772   int ret = (int) GET_MODE (piece);
5773   if (ret)
5774     return ret;
5775   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
5776               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
5777   return INTVAL (XEXP (XEXP (piece, 0), 0));
5778 }
5779
5780 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
5781
5782 static rtx *
5783 decl_piece_varloc_ptr (rtx piece)
5784 {
5785   if ((int) GET_MODE (piece))
5786     return &XEXP (piece, 0);
5787   else
5788     return &XEXP (XEXP (piece, 0), 1);
5789 }
5790
5791 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
5792    Next is the chain of following piece nodes.  */
5793
5794 static rtx
5795 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
5796 {
5797   if (bitsize <= (int) MAX_MACHINE_MODE)
5798     return alloc_EXPR_LIST (bitsize, loc_note, next);
5799   else
5800     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
5801                                                GEN_INT (bitsize),
5802                                                loc_note), next);
5803 }
5804
5805 /* Return rtx that should be stored into loc field for
5806    LOC_NOTE and BITPOS/BITSIZE.  */
5807
5808 static rtx
5809 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
5810                       HOST_WIDE_INT bitsize)
5811 {
5812   if (bitsize != -1)
5813     {
5814       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
5815       if (bitpos != 0)
5816         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
5817     }
5818   return loc_note;
5819 }
5820
5821 /* This function either modifies location piece list *DEST in
5822    place (if SRC and INNER is NULL), or copies location piece list
5823    *SRC to *DEST while modifying it.  Location BITPOS is modified
5824    to contain LOC_NOTE, any pieces overlapping it are removed resp.
5825    not copied and if needed some padding around it is added.
5826    When modifying in place, DEST should point to EXPR_LIST where
5827    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
5828    to the start of the whole list and INNER points to the EXPR_LIST
5829    where earlier pieces cover PIECE_BITPOS bits.  */
5830
5831 static void
5832 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
5833                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
5834                    HOST_WIDE_INT bitsize, rtx loc_note)
5835 {
5836   int diff;
5837   bool copy = inner != NULL;
5838
5839   if (copy)
5840     {
5841       /* First copy all nodes preceeding the current bitpos.  */
5842       while (src != inner)
5843         {
5844           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5845                                    decl_piece_bitsize (*src), NULL_RTX);
5846           dest = &XEXP (*dest, 1);
5847           src = &XEXP (*src, 1);
5848         }
5849     }
5850   /* Add padding if needed.  */
5851   if (bitpos != piece_bitpos)
5852     {
5853       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
5854                                copy ? NULL_RTX : *dest);
5855       dest = &XEXP (*dest, 1);
5856     }
5857   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
5858     {
5859       gcc_assert (!copy);
5860       /* A piece with correct bitpos and bitsize already exist,
5861          just update the location for it and return.  */
5862       *decl_piece_varloc_ptr (*dest) = loc_note;
5863       return;
5864     }
5865   /* Add the piece that changed.  */
5866   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
5867   dest = &XEXP (*dest, 1);
5868   /* Skip over pieces that overlap it.  */
5869   diff = bitpos - piece_bitpos + bitsize;
5870   if (!copy)
5871     src = dest;
5872   while (diff > 0 && *src)
5873     {
5874       rtx piece = *src;
5875       diff -= decl_piece_bitsize (piece);
5876       if (copy)
5877         src = &XEXP (piece, 1);
5878       else
5879         {
5880           *src = XEXP (piece, 1);
5881           free_EXPR_LIST_node (piece);
5882         }
5883     }
5884   /* Add padding if needed.  */
5885   if (diff < 0 && *src)
5886     {
5887       if (!copy)
5888         dest = src;
5889       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
5890       dest = &XEXP (*dest, 1);
5891     }
5892   if (!copy)
5893     return;
5894   /* Finally copy all nodes following it.  */
5895   while (*src)
5896     {
5897       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
5898                                decl_piece_bitsize (*src), NULL_RTX);
5899       dest = &XEXP (*dest, 1);
5900       src = &XEXP (*src, 1);
5901     }
5902 }
5903
5904 /* Add a variable location node to the linked list for DECL.  */
5905
5906 static struct var_loc_node *
5907 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
5908 {
5909   unsigned int decl_id;
5910   var_loc_list *temp;
5911   void **slot;
5912   struct var_loc_node *loc = NULL;
5913   HOST_WIDE_INT bitsize = -1, bitpos = -1;
5914
5915   if (DECL_DEBUG_EXPR_IS_FROM (decl))
5916     {
5917       tree realdecl = DECL_DEBUG_EXPR (decl);
5918       if (realdecl && handled_component_p (realdecl))
5919         {
5920           HOST_WIDE_INT maxsize;
5921           tree innerdecl;
5922           innerdecl
5923             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
5924           if (!DECL_P (innerdecl)
5925               || DECL_IGNORED_P (innerdecl)
5926               || TREE_STATIC (innerdecl)
5927               || bitsize <= 0
5928               || bitpos + bitsize > 256
5929               || bitsize != maxsize)
5930             return NULL;
5931           decl = innerdecl;
5932         }
5933     }
5934
5935   decl_id = DECL_UID (decl);
5936   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5937   if (*slot == NULL)
5938     {
5939       temp = ggc_alloc_cleared_var_loc_list ();
5940       temp->decl_id = decl_id;
5941       *slot = temp;
5942     }
5943   else
5944     temp = (var_loc_list *) *slot;
5945
5946   /* For PARM_DECLs try to keep around the original incoming value,
5947      even if that means we'll emit a zero-range .debug_loc entry.  */
5948   if (temp->last
5949       && temp->first == temp->last
5950       && TREE_CODE (decl) == PARM_DECL
5951       && GET_CODE (temp->first->loc) == NOTE
5952       && NOTE_VAR_LOCATION_DECL (temp->first->loc) == decl
5953       && DECL_INCOMING_RTL (decl)
5954       && NOTE_VAR_LOCATION_LOC (temp->first->loc)
5955       && GET_CODE (NOTE_VAR_LOCATION_LOC (temp->first->loc))
5956          == GET_CODE (DECL_INCOMING_RTL (decl))
5957       && prev_real_insn (temp->first->loc) == NULL_RTX
5958       && (bitsize != -1
5959           || !rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->first->loc),
5960                            NOTE_VAR_LOCATION_LOC (loc_note))
5961           || (NOTE_VAR_LOCATION_STATUS (temp->first->loc)
5962               != NOTE_VAR_LOCATION_STATUS (loc_note))))
5963     {
5964       loc = ggc_alloc_cleared_var_loc_node ();
5965       temp->first->next = loc;
5966       temp->last = loc;
5967       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
5968     }
5969   else if (temp->last)
5970     {
5971       struct var_loc_node *last = temp->last, *unused = NULL;
5972       rtx *piece_loc = NULL, last_loc_note;
5973       int piece_bitpos = 0;
5974       if (last->next)
5975         {
5976           last = last->next;
5977           gcc_assert (last->next == NULL);
5978         }
5979       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
5980         {
5981           piece_loc = &last->loc;
5982           do
5983             {
5984               int cur_bitsize = decl_piece_bitsize (*piece_loc);
5985               if (piece_bitpos + cur_bitsize > bitpos)
5986                 break;
5987               piece_bitpos += cur_bitsize;
5988               piece_loc = &XEXP (*piece_loc, 1);
5989             }
5990           while (*piece_loc);
5991         }
5992       /* TEMP->LAST here is either pointer to the last but one or
5993          last element in the chained list, LAST is pointer to the
5994          last element.  */
5995       if (label && strcmp (last->label, label) == 0)
5996         {
5997           /* For SRA optimized variables if there weren't any real
5998              insns since last note, just modify the last node.  */
5999           if (piece_loc != NULL)
6000             {
6001               adjust_piece_list (piece_loc, NULL, NULL,
6002                                  bitpos, piece_bitpos, bitsize, loc_note);
6003               return NULL;
6004             }
6005           /* If the last note doesn't cover any instructions, remove it.  */
6006           if (temp->last != last)
6007             {
6008               temp->last->next = NULL;
6009               unused = last;
6010               last = temp->last;
6011               gcc_assert (strcmp (last->label, label) != 0);
6012             }
6013           else
6014             {
6015               gcc_assert (temp->first == temp->last
6016                           || (temp->first->next == temp->last
6017                               && TREE_CODE (decl) == PARM_DECL));
6018               memset (temp->last, '\0', sizeof (*temp->last));
6019               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
6020               return temp->last;
6021             }
6022         }
6023       if (bitsize == -1 && NOTE_P (last->loc))
6024         last_loc_note = last->loc;
6025       else if (piece_loc != NULL
6026                && *piece_loc != NULL_RTX
6027                && piece_bitpos == bitpos
6028                && decl_piece_bitsize (*piece_loc) == bitsize)
6029         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
6030       else
6031         last_loc_note = NULL_RTX;
6032       /* If the current location is the same as the end of the list,
6033          and either both or neither of the locations is uninitialized,
6034          we have nothing to do.  */
6035       if (last_loc_note == NULL_RTX
6036           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
6037                             NOTE_VAR_LOCATION_LOC (loc_note)))
6038           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6039                != NOTE_VAR_LOCATION_STATUS (loc_note))
6040               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
6041                    == VAR_INIT_STATUS_UNINITIALIZED)
6042                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
6043                       == VAR_INIT_STATUS_UNINITIALIZED))))
6044         {
6045           /* Add LOC to the end of list and update LAST.  If the last
6046              element of the list has been removed above, reuse its
6047              memory for the new node, otherwise allocate a new one.  */
6048           if (unused)
6049             {
6050               loc = unused;
6051               memset (loc, '\0', sizeof (*loc));
6052             }
6053           else
6054             loc = ggc_alloc_cleared_var_loc_node ();
6055           if (bitsize == -1 || piece_loc == NULL)
6056             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6057           else
6058             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
6059                                bitpos, piece_bitpos, bitsize, loc_note);
6060           last->next = loc;
6061           /* Ensure TEMP->LAST will point either to the new last but one
6062              element of the chain, or to the last element in it.  */
6063           if (last != temp->last)
6064             temp->last = last;
6065         }
6066       else if (unused)
6067         ggc_free (unused);
6068     }
6069   else
6070     {
6071       loc = ggc_alloc_cleared_var_loc_node ();
6072       temp->first = loc;
6073       temp->last = loc;
6074       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
6075     }
6076   return loc;
6077 }
6078 \f
6079 /* Keep track of the number of spaces used to indent the
6080    output of the debugging routines that print the structure of
6081    the DIE internal representation.  */
6082 static int print_indent;
6083
6084 /* Indent the line the number of spaces given by print_indent.  */
6085
6086 static inline void
6087 print_spaces (FILE *outfile)
6088 {
6089   fprintf (outfile, "%*s", print_indent, "");
6090 }
6091
6092 /* Print a type signature in hex.  */
6093
6094 static inline void
6095 print_signature (FILE *outfile, char *sig)
6096 {
6097   int i;
6098
6099   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
6100     fprintf (outfile, "%02x", sig[i] & 0xff);
6101 }
6102
6103 /* Print the information associated with a given DIE, and its children.
6104    This routine is a debugging aid only.  */
6105
6106 static void
6107 print_die (dw_die_ref die, FILE *outfile)
6108 {
6109   dw_attr_ref a;
6110   dw_die_ref c;
6111   unsigned ix;
6112
6113   print_spaces (outfile);
6114   fprintf (outfile, "DIE %4ld: %s (%p)\n",
6115            die->die_offset, dwarf_tag_name (die->die_tag),
6116            (void*) die);
6117   print_spaces (outfile);
6118   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
6119   fprintf (outfile, " offset: %ld", die->die_offset);
6120   fprintf (outfile, " mark: %d\n", die->die_mark);
6121
6122   if (use_debug_types && die->die_id.die_type_node)
6123     {
6124       print_spaces (outfile);
6125       fprintf (outfile, "  signature: ");
6126       print_signature (outfile, die->die_id.die_type_node->signature);
6127       fprintf (outfile, "\n");
6128     }
6129
6130   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6131     {
6132       print_spaces (outfile);
6133       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
6134
6135       switch (AT_class (a))
6136         {
6137         case dw_val_class_addr:
6138           fprintf (outfile, "address");
6139           break;
6140         case dw_val_class_offset:
6141           fprintf (outfile, "offset");
6142           break;
6143         case dw_val_class_loc:
6144           fprintf (outfile, "location descriptor");
6145           break;
6146         case dw_val_class_loc_list:
6147           fprintf (outfile, "location list -> label:%s",
6148                    AT_loc_list (a)->ll_symbol);
6149           break;
6150         case dw_val_class_range_list:
6151           fprintf (outfile, "range list");
6152           break;
6153         case dw_val_class_const:
6154           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
6155           break;
6156         case dw_val_class_unsigned_const:
6157           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
6158           break;
6159         case dw_val_class_const_double:
6160           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
6161                             HOST_WIDE_INT_PRINT_UNSIGNED")",
6162                    a->dw_attr_val.v.val_double.high,
6163                    a->dw_attr_val.v.val_double.low);
6164           break;
6165         case dw_val_class_vec:
6166           fprintf (outfile, "floating-point or vector constant");
6167           break;
6168         case dw_val_class_flag:
6169           fprintf (outfile, "%u", AT_flag (a));
6170           break;
6171         case dw_val_class_die_ref:
6172           if (AT_ref (a) != NULL)
6173             {
6174               if (use_debug_types && AT_ref (a)->die_id.die_type_node)
6175                 {
6176                   fprintf (outfile, "die -> signature: ");
6177                   print_signature (outfile,
6178                                    AT_ref (a)->die_id.die_type_node->signature);
6179                 }
6180               else if (! use_debug_types && AT_ref (a)->die_id.die_symbol)
6181                 fprintf (outfile, "die -> label: %s",
6182                          AT_ref (a)->die_id.die_symbol);
6183               else
6184                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
6185               fprintf (outfile, " (%p)", (void *) AT_ref (a));
6186             }
6187           else
6188             fprintf (outfile, "die -> <null>");
6189           break;
6190         case dw_val_class_vms_delta:
6191           fprintf (outfile, "delta: @slotcount(%s-%s)",
6192                    AT_vms_delta2 (a), AT_vms_delta1 (a));
6193           break;
6194         case dw_val_class_lbl_id:
6195         case dw_val_class_lineptr:
6196         case dw_val_class_macptr:
6197           fprintf (outfile, "label: %s", AT_lbl (a));
6198           break;
6199         case dw_val_class_str:
6200           if (AT_string (a) != NULL)
6201             fprintf (outfile, "\"%s\"", AT_string (a));
6202           else
6203             fprintf (outfile, "<null>");
6204           break;
6205         case dw_val_class_file:
6206           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
6207                    AT_file (a)->emitted_number);
6208           break;
6209         case dw_val_class_data8:
6210           {
6211             int i;
6212
6213             for (i = 0; i < 8; i++)
6214               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
6215             break;
6216           }
6217         default:
6218           break;
6219         }
6220
6221       fprintf (outfile, "\n");
6222     }
6223
6224   if (die->die_child != NULL)
6225     {
6226       print_indent += 4;
6227       FOR_EACH_CHILD (die, c, print_die (c, outfile));
6228       print_indent -= 4;
6229     }
6230   if (print_indent == 0)
6231     fprintf (outfile, "\n");
6232 }
6233
6234 /* Print the information collected for a given DIE.  */
6235
6236 DEBUG_FUNCTION void
6237 debug_dwarf_die (dw_die_ref die)
6238 {
6239   print_die (die, stderr);
6240 }
6241
6242 /* Print all DWARF information collected for the compilation unit.
6243    This routine is a debugging aid only.  */
6244
6245 DEBUG_FUNCTION void
6246 debug_dwarf (void)
6247 {
6248   print_indent = 0;
6249   print_die (comp_unit_die (), stderr);
6250 }
6251 \f
6252 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6253    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6254    DIE that marks the start of the DIEs for this include file.  */
6255
6256 static dw_die_ref
6257 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6258 {
6259   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6260   dw_die_ref new_unit = gen_compile_unit_die (filename);
6261
6262   new_unit->die_sib = old_unit;
6263   return new_unit;
6264 }
6265
6266 /* Close an include-file CU and reopen the enclosing one.  */
6267
6268 static dw_die_ref
6269 pop_compile_unit (dw_die_ref old_unit)
6270 {
6271   dw_die_ref new_unit = old_unit->die_sib;
6272
6273   old_unit->die_sib = NULL;
6274   return new_unit;
6275 }
6276
6277 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6278 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6279
6280 /* Calculate the checksum of a location expression.  */
6281
6282 static inline void
6283 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6284 {
6285   int tem;
6286
6287   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
6288   CHECKSUM (tem);
6289   CHECKSUM (loc->dw_loc_oprnd1);
6290   CHECKSUM (loc->dw_loc_oprnd2);
6291 }
6292
6293 /* Calculate the checksum of an attribute.  */
6294
6295 static void
6296 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6297 {
6298   dw_loc_descr_ref loc;
6299   rtx r;
6300
6301   CHECKSUM (at->dw_attr);
6302
6303   /* We don't care that this was compiled with a different compiler
6304      snapshot; if the output is the same, that's what matters.  */
6305   if (at->dw_attr == DW_AT_producer)
6306     return;
6307
6308   switch (AT_class (at))
6309     {
6310     case dw_val_class_const:
6311       CHECKSUM (at->dw_attr_val.v.val_int);
6312       break;
6313     case dw_val_class_unsigned_const:
6314       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6315       break;
6316     case dw_val_class_const_double:
6317       CHECKSUM (at->dw_attr_val.v.val_double);
6318       break;
6319     case dw_val_class_vec:
6320       CHECKSUM (at->dw_attr_val.v.val_vec);
6321       break;
6322     case dw_val_class_flag:
6323       CHECKSUM (at->dw_attr_val.v.val_flag);
6324       break;
6325     case dw_val_class_str:
6326       CHECKSUM_STRING (AT_string (at));
6327       break;
6328
6329     case dw_val_class_addr:
6330       r = AT_addr (at);
6331       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6332       CHECKSUM_STRING (XSTR (r, 0));
6333       break;
6334
6335     case dw_val_class_offset:
6336       CHECKSUM (at->dw_attr_val.v.val_offset);
6337       break;
6338
6339     case dw_val_class_loc:
6340       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6341         loc_checksum (loc, ctx);
6342       break;
6343
6344     case dw_val_class_die_ref:
6345       die_checksum (AT_ref (at), ctx, mark);
6346       break;
6347
6348     case dw_val_class_fde_ref:
6349     case dw_val_class_vms_delta:
6350     case dw_val_class_lbl_id:
6351     case dw_val_class_lineptr:
6352     case dw_val_class_macptr:
6353       break;
6354
6355     case dw_val_class_file:
6356       CHECKSUM_STRING (AT_file (at)->filename);
6357       break;
6358
6359     case dw_val_class_data8:
6360       CHECKSUM (at->dw_attr_val.v.val_data8);
6361       break;
6362
6363     default:
6364       break;
6365     }
6366 }
6367
6368 /* Calculate the checksum of a DIE.  */
6369
6370 static void
6371 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6372 {
6373   dw_die_ref c;
6374   dw_attr_ref a;
6375   unsigned ix;
6376
6377   /* To avoid infinite recursion.  */
6378   if (die->die_mark)
6379     {
6380       CHECKSUM (die->die_mark);
6381       return;
6382     }
6383   die->die_mark = ++(*mark);
6384
6385   CHECKSUM (die->die_tag);
6386
6387   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6388     attr_checksum (a, ctx, mark);
6389
6390   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6391 }
6392
6393 #undef CHECKSUM
6394 #undef CHECKSUM_STRING
6395
6396 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
6397 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6398 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
6399 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
6400 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
6401 #define CHECKSUM_ATTR(FOO) \
6402   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
6403
6404 /* Calculate the checksum of a number in signed LEB128 format.  */
6405
6406 static void
6407 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
6408 {
6409   unsigned char byte;
6410   bool more;
6411
6412   while (1)
6413     {
6414       byte = (value & 0x7f);
6415       value >>= 7;
6416       more = !((value == 0 && (byte & 0x40) == 0)
6417                 || (value == -1 && (byte & 0x40) != 0));
6418       if (more)
6419         byte |= 0x80;
6420       CHECKSUM (byte);
6421       if (!more)
6422         break;
6423     }
6424 }
6425
6426 /* Calculate the checksum of a number in unsigned LEB128 format.  */
6427
6428 static void
6429 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
6430 {
6431   while (1)
6432     {
6433       unsigned char byte = (value & 0x7f);
6434       value >>= 7;
6435       if (value != 0)
6436         /* More bytes to follow.  */
6437         byte |= 0x80;
6438       CHECKSUM (byte);
6439       if (value == 0)
6440         break;
6441     }
6442 }
6443
6444 /* Checksum the context of the DIE.  This adds the names of any
6445    surrounding namespaces or structures to the checksum.  */
6446
6447 static void
6448 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
6449 {
6450   const char *name;
6451   dw_die_ref spec;
6452   int tag = die->die_tag;
6453
6454   if (tag != DW_TAG_namespace
6455       && tag != DW_TAG_structure_type
6456       && tag != DW_TAG_class_type)
6457     return;
6458
6459   name = get_AT_string (die, DW_AT_name);
6460
6461   spec = get_AT_ref (die, DW_AT_specification);
6462   if (spec != NULL)
6463     die = spec;
6464
6465   if (die->die_parent != NULL)
6466     checksum_die_context (die->die_parent, ctx);
6467
6468   CHECKSUM_ULEB128 ('C');
6469   CHECKSUM_ULEB128 (tag);
6470   if (name != NULL)
6471     CHECKSUM_STRING (name);
6472 }
6473
6474 /* Calculate the checksum of a location expression.  */
6475
6476 static inline void
6477 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6478 {
6479   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
6480      were emitted as a DW_FORM_sdata instead of a location expression.  */
6481   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
6482     {
6483       CHECKSUM_ULEB128 (DW_FORM_sdata);
6484       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
6485       return;
6486     }
6487
6488   /* Otherwise, just checksum the raw location expression.  */
6489   while (loc != NULL)
6490     {
6491       CHECKSUM_ULEB128 (loc->dw_loc_opc);
6492       CHECKSUM (loc->dw_loc_oprnd1);
6493       CHECKSUM (loc->dw_loc_oprnd2);
6494       loc = loc->dw_loc_next;
6495     }
6496 }
6497
6498 /* Calculate the checksum of an attribute.  */
6499
6500 static void
6501 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
6502                        struct md5_ctx *ctx, int *mark)
6503 {
6504   dw_loc_descr_ref loc;
6505   rtx r;
6506
6507   if (AT_class (at) == dw_val_class_die_ref)
6508     {
6509       dw_die_ref target_die = AT_ref (at);
6510
6511       /* For pointer and reference types, we checksum only the (qualified)
6512          name of the target type (if there is a name).  For friend entries,
6513          we checksum only the (qualified) name of the target type or function.
6514          This allows the checksum to remain the same whether the target type
6515          is complete or not.  */
6516       if ((at->dw_attr == DW_AT_type
6517            && (tag == DW_TAG_pointer_type
6518                || tag == DW_TAG_reference_type
6519                || tag == DW_TAG_rvalue_reference_type
6520                || tag == DW_TAG_ptr_to_member_type))
6521           || (at->dw_attr == DW_AT_friend
6522               && tag == DW_TAG_friend))
6523         {
6524           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
6525
6526           if (name_attr != NULL)
6527             {
6528               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6529
6530               if (decl == NULL)
6531                 decl = target_die;
6532               CHECKSUM_ULEB128 ('N');
6533               CHECKSUM_ULEB128 (at->dw_attr);
6534               if (decl->die_parent != NULL)
6535                 checksum_die_context (decl->die_parent, ctx);
6536               CHECKSUM_ULEB128 ('E');
6537               CHECKSUM_STRING (AT_string (name_attr));
6538               return;
6539             }
6540         }
6541
6542       /* For all other references to another DIE, we check to see if the
6543          target DIE has already been visited.  If it has, we emit a
6544          backward reference; if not, we descend recursively.  */
6545       if (target_die->die_mark > 0)
6546         {
6547           CHECKSUM_ULEB128 ('R');
6548           CHECKSUM_ULEB128 (at->dw_attr);
6549           CHECKSUM_ULEB128 (target_die->die_mark);
6550         }
6551       else
6552         {
6553           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
6554
6555           if (decl == NULL)
6556             decl = target_die;
6557           target_die->die_mark = ++(*mark);
6558           CHECKSUM_ULEB128 ('T');
6559           CHECKSUM_ULEB128 (at->dw_attr);
6560           if (decl->die_parent != NULL)
6561             checksum_die_context (decl->die_parent, ctx);
6562           die_checksum_ordered (target_die, ctx, mark);
6563         }
6564       return;
6565     }
6566
6567   CHECKSUM_ULEB128 ('A');
6568   CHECKSUM_ULEB128 (at->dw_attr);
6569
6570   switch (AT_class (at))
6571     {
6572     case dw_val_class_const:
6573       CHECKSUM_ULEB128 (DW_FORM_sdata);
6574       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
6575       break;
6576
6577     case dw_val_class_unsigned_const:
6578       CHECKSUM_ULEB128 (DW_FORM_sdata);
6579       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
6580       break;
6581
6582     case dw_val_class_const_double:
6583       CHECKSUM_ULEB128 (DW_FORM_block);
6584       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
6585       CHECKSUM (at->dw_attr_val.v.val_double);
6586       break;
6587
6588     case dw_val_class_vec:
6589       CHECKSUM_ULEB128 (DW_FORM_block);
6590       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
6591       CHECKSUM (at->dw_attr_val.v.val_vec);
6592       break;
6593
6594     case dw_val_class_flag:
6595       CHECKSUM_ULEB128 (DW_FORM_flag);
6596       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
6597       break;
6598
6599     case dw_val_class_str:
6600       CHECKSUM_ULEB128 (DW_FORM_string);
6601       CHECKSUM_STRING (AT_string (at));
6602       break;
6603
6604     case dw_val_class_addr:
6605       r = AT_addr (at);
6606       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6607       CHECKSUM_ULEB128 (DW_FORM_string);
6608       CHECKSUM_STRING (XSTR (r, 0));
6609       break;
6610
6611     case dw_val_class_offset:
6612       CHECKSUM_ULEB128 (DW_FORM_sdata);
6613       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
6614       break;
6615
6616     case dw_val_class_loc:
6617       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6618         loc_checksum_ordered (loc, ctx);
6619       break;
6620
6621     case dw_val_class_fde_ref:
6622     case dw_val_class_lbl_id:
6623     case dw_val_class_lineptr:
6624     case dw_val_class_macptr:
6625       break;
6626
6627     case dw_val_class_file:
6628       CHECKSUM_ULEB128 (DW_FORM_string);
6629       CHECKSUM_STRING (AT_file (at)->filename);
6630       break;
6631
6632     case dw_val_class_data8:
6633       CHECKSUM (at->dw_attr_val.v.val_data8);
6634       break;
6635
6636     default:
6637       break;
6638     }
6639 }
6640
6641 struct checksum_attributes
6642 {
6643   dw_attr_ref at_name;
6644   dw_attr_ref at_type;
6645   dw_attr_ref at_friend;
6646   dw_attr_ref at_accessibility;
6647   dw_attr_ref at_address_class;
6648   dw_attr_ref at_allocated;
6649   dw_attr_ref at_artificial;
6650   dw_attr_ref at_associated;
6651   dw_attr_ref at_binary_scale;
6652   dw_attr_ref at_bit_offset;
6653   dw_attr_ref at_bit_size;
6654   dw_attr_ref at_bit_stride;
6655   dw_attr_ref at_byte_size;
6656   dw_attr_ref at_byte_stride;
6657   dw_attr_ref at_const_value;
6658   dw_attr_ref at_containing_type;
6659   dw_attr_ref at_count;
6660   dw_attr_ref at_data_location;
6661   dw_attr_ref at_data_member_location;
6662   dw_attr_ref at_decimal_scale;
6663   dw_attr_ref at_decimal_sign;
6664   dw_attr_ref at_default_value;
6665   dw_attr_ref at_digit_count;
6666   dw_attr_ref at_discr;
6667   dw_attr_ref at_discr_list;
6668   dw_attr_ref at_discr_value;
6669   dw_attr_ref at_encoding;
6670   dw_attr_ref at_endianity;
6671   dw_attr_ref at_explicit;
6672   dw_attr_ref at_is_optional;
6673   dw_attr_ref at_location;
6674   dw_attr_ref at_lower_bound;
6675   dw_attr_ref at_mutable;
6676   dw_attr_ref at_ordering;
6677   dw_attr_ref at_picture_string;
6678   dw_attr_ref at_prototyped;
6679   dw_attr_ref at_small;
6680   dw_attr_ref at_segment;
6681   dw_attr_ref at_string_length;
6682   dw_attr_ref at_threads_scaled;
6683   dw_attr_ref at_upper_bound;
6684   dw_attr_ref at_use_location;
6685   dw_attr_ref at_use_UTF8;
6686   dw_attr_ref at_variable_parameter;
6687   dw_attr_ref at_virtuality;
6688   dw_attr_ref at_visibility;
6689   dw_attr_ref at_vtable_elem_location;
6690 };
6691
6692 /* Collect the attributes that we will want to use for the checksum.  */
6693
6694 static void
6695 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
6696 {
6697   dw_attr_ref a;
6698   unsigned ix;
6699
6700   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
6701     {
6702       switch (a->dw_attr)
6703         {
6704         case DW_AT_name:
6705           attrs->at_name = a;
6706           break;
6707         case DW_AT_type:
6708           attrs->at_type = a;
6709           break;
6710         case DW_AT_friend:
6711           attrs->at_friend = a;
6712           break;
6713         case DW_AT_accessibility:
6714           attrs->at_accessibility = a;
6715           break;
6716         case DW_AT_address_class:
6717           attrs->at_address_class = a;
6718           break;
6719         case DW_AT_allocated:
6720           attrs->at_allocated = a;
6721           break;
6722         case DW_AT_artificial:
6723           attrs->at_artificial = a;
6724           break;
6725         case DW_AT_associated:
6726           attrs->at_associated = a;
6727           break;
6728         case DW_AT_binary_scale:
6729           attrs->at_binary_scale = a;
6730           break;
6731         case DW_AT_bit_offset:
6732           attrs->at_bit_offset = a;
6733           break;
6734         case DW_AT_bit_size:
6735           attrs->at_bit_size = a;
6736           break;
6737         case DW_AT_bit_stride:
6738           attrs->at_bit_stride = a;
6739           break;
6740         case DW_AT_byte_size:
6741           attrs->at_byte_size = a;
6742           break;
6743         case DW_AT_byte_stride:
6744           attrs->at_byte_stride = a;
6745           break;
6746         case DW_AT_const_value:
6747           attrs->at_const_value = a;
6748           break;
6749         case DW_AT_containing_type:
6750           attrs->at_containing_type = a;
6751           break;
6752         case DW_AT_count:
6753           attrs->at_count = a;
6754           break;
6755         case DW_AT_data_location:
6756           attrs->at_data_location = a;
6757           break;
6758         case DW_AT_data_member_location:
6759           attrs->at_data_member_location = a;
6760           break;
6761         case DW_AT_decimal_scale:
6762           attrs->at_decimal_scale = a;
6763           break;
6764         case DW_AT_decimal_sign:
6765           attrs->at_decimal_sign = a;
6766           break;
6767         case DW_AT_default_value:
6768           attrs->at_default_value = a;
6769           break;
6770         case DW_AT_digit_count:
6771           attrs->at_digit_count = a;
6772           break;
6773         case DW_AT_discr:
6774           attrs->at_discr = a;
6775           break;
6776         case DW_AT_discr_list:
6777           attrs->at_discr_list = a;
6778           break;
6779         case DW_AT_discr_value:
6780           attrs->at_discr_value = a;
6781           break;
6782         case DW_AT_encoding:
6783           attrs->at_encoding = a;
6784           break;
6785         case DW_AT_endianity:
6786           attrs->at_endianity = a;
6787           break;
6788         case DW_AT_explicit:
6789           attrs->at_explicit = a;
6790           break;
6791         case DW_AT_is_optional:
6792           attrs->at_is_optional = a;
6793           break;
6794         case DW_AT_location:
6795           attrs->at_location = a;
6796           break;
6797         case DW_AT_lower_bound:
6798           attrs->at_lower_bound = a;
6799           break;
6800         case DW_AT_mutable:
6801           attrs->at_mutable = a;
6802           break;
6803         case DW_AT_ordering:
6804           attrs->at_ordering = a;
6805           break;
6806         case DW_AT_picture_string:
6807           attrs->at_picture_string = a;
6808           break;
6809         case DW_AT_prototyped:
6810           attrs->at_prototyped = a;
6811           break;
6812         case DW_AT_small:
6813           attrs->at_small = a;
6814           break;
6815         case DW_AT_segment:
6816           attrs->at_segment = a;
6817           break;
6818         case DW_AT_string_length:
6819           attrs->at_string_length = a;
6820           break;
6821         case DW_AT_threads_scaled:
6822           attrs->at_threads_scaled = a;
6823           break;
6824         case DW_AT_upper_bound:
6825           attrs->at_upper_bound = a;
6826           break;
6827         case DW_AT_use_location:
6828           attrs->at_use_location = a;
6829           break;
6830         case DW_AT_use_UTF8:
6831           attrs->at_use_UTF8 = a;
6832           break;
6833         case DW_AT_variable_parameter:
6834           attrs->at_variable_parameter = a;
6835           break;
6836         case DW_AT_virtuality:
6837           attrs->at_virtuality = a;
6838           break;
6839         case DW_AT_visibility:
6840           attrs->at_visibility = a;
6841           break;
6842         case DW_AT_vtable_elem_location:
6843           attrs->at_vtable_elem_location = a;
6844           break;
6845         default:
6846           break;
6847         }
6848     }
6849 }
6850
6851 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
6852
6853 static void
6854 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6855 {
6856   dw_die_ref c;
6857   dw_die_ref decl;
6858   struct checksum_attributes attrs;
6859
6860   CHECKSUM_ULEB128 ('D');
6861   CHECKSUM_ULEB128 (die->die_tag);
6862
6863   memset (&attrs, 0, sizeof (attrs));
6864
6865   decl = get_AT_ref (die, DW_AT_specification);
6866   if (decl != NULL)
6867     collect_checksum_attributes (&attrs, decl);
6868   collect_checksum_attributes (&attrs, die);
6869
6870   CHECKSUM_ATTR (attrs.at_name);
6871   CHECKSUM_ATTR (attrs.at_accessibility);
6872   CHECKSUM_ATTR (attrs.at_address_class);
6873   CHECKSUM_ATTR (attrs.at_allocated);
6874   CHECKSUM_ATTR (attrs.at_artificial);
6875   CHECKSUM_ATTR (attrs.at_associated);
6876   CHECKSUM_ATTR (attrs.at_binary_scale);
6877   CHECKSUM_ATTR (attrs.at_bit_offset);
6878   CHECKSUM_ATTR (attrs.at_bit_size);
6879   CHECKSUM_ATTR (attrs.at_bit_stride);
6880   CHECKSUM_ATTR (attrs.at_byte_size);
6881   CHECKSUM_ATTR (attrs.at_byte_stride);
6882   CHECKSUM_ATTR (attrs.at_const_value);
6883   CHECKSUM_ATTR (attrs.at_containing_type);
6884   CHECKSUM_ATTR (attrs.at_count);
6885   CHECKSUM_ATTR (attrs.at_data_location);
6886   CHECKSUM_ATTR (attrs.at_data_member_location);
6887   CHECKSUM_ATTR (attrs.at_decimal_scale);
6888   CHECKSUM_ATTR (attrs.at_decimal_sign);
6889   CHECKSUM_ATTR (attrs.at_default_value);
6890   CHECKSUM_ATTR (attrs.at_digit_count);
6891   CHECKSUM_ATTR (attrs.at_discr);
6892   CHECKSUM_ATTR (attrs.at_discr_list);
6893   CHECKSUM_ATTR (attrs.at_discr_value);
6894   CHECKSUM_ATTR (attrs.at_encoding);
6895   CHECKSUM_ATTR (attrs.at_endianity);
6896   CHECKSUM_ATTR (attrs.at_explicit);
6897   CHECKSUM_ATTR (attrs.at_is_optional);
6898   CHECKSUM_ATTR (attrs.at_location);
6899   CHECKSUM_ATTR (attrs.at_lower_bound);
6900   CHECKSUM_ATTR (attrs.at_mutable);
6901   CHECKSUM_ATTR (attrs.at_ordering);
6902   CHECKSUM_ATTR (attrs.at_picture_string);
6903   CHECKSUM_ATTR (attrs.at_prototyped);
6904   CHECKSUM_ATTR (attrs.at_small);
6905   CHECKSUM_ATTR (attrs.at_segment);
6906   CHECKSUM_ATTR (attrs.at_string_length);
6907   CHECKSUM_ATTR (attrs.at_threads_scaled);
6908   CHECKSUM_ATTR (attrs.at_upper_bound);
6909   CHECKSUM_ATTR (attrs.at_use_location);
6910   CHECKSUM_ATTR (attrs.at_use_UTF8);
6911   CHECKSUM_ATTR (attrs.at_variable_parameter);
6912   CHECKSUM_ATTR (attrs.at_virtuality);
6913   CHECKSUM_ATTR (attrs.at_visibility);
6914   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
6915   CHECKSUM_ATTR (attrs.at_type);
6916   CHECKSUM_ATTR (attrs.at_friend);
6917
6918   /* Checksum the child DIEs, except for nested types and member functions.  */
6919   c = die->die_child;
6920   if (c) do {
6921     dw_attr_ref name_attr;
6922
6923     c = c->die_sib;
6924     name_attr = get_AT (c, DW_AT_name);
6925     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
6926         && name_attr != NULL)
6927       {
6928         CHECKSUM_ULEB128 ('S');
6929         CHECKSUM_ULEB128 (c->die_tag);
6930         CHECKSUM_STRING (AT_string (name_attr));
6931       }
6932     else
6933       {
6934         /* Mark this DIE so it gets processed when unmarking.  */
6935         if (c->die_mark == 0)
6936           c->die_mark = -1;
6937         die_checksum_ordered (c, ctx, mark);
6938       }
6939   } while (c != die->die_child);
6940
6941   CHECKSUM_ULEB128 (0);
6942 }
6943
6944 #undef CHECKSUM
6945 #undef CHECKSUM_STRING
6946 #undef CHECKSUM_ATTR
6947 #undef CHECKSUM_LEB128
6948 #undef CHECKSUM_ULEB128
6949
6950 /* Generate the type signature for DIE.  This is computed by generating an
6951    MD5 checksum over the DIE's tag, its relevant attributes, and its
6952    children.  Attributes that are references to other DIEs are processed
6953    by recursion, using the MARK field to prevent infinite recursion.
6954    If the DIE is nested inside a namespace or another type, we also
6955    need to include that context in the signature.  The lower 64 bits
6956    of the resulting MD5 checksum comprise the signature.  */
6957
6958 static void
6959 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
6960 {
6961   int mark;
6962   const char *name;
6963   unsigned char checksum[16];
6964   struct md5_ctx ctx;
6965   dw_die_ref decl;
6966
6967   name = get_AT_string (die, DW_AT_name);
6968   decl = get_AT_ref (die, DW_AT_specification);
6969
6970   /* First, compute a signature for just the type name (and its surrounding
6971      context, if any.  This is stored in the type unit DIE for link-time
6972      ODR (one-definition rule) checking.  */
6973
6974   if (is_cxx() && name != NULL)
6975     {
6976       md5_init_ctx (&ctx);
6977
6978       /* Checksum the names of surrounding namespaces and structures.  */
6979       if (decl != NULL && decl->die_parent != NULL)
6980         checksum_die_context (decl->die_parent, &ctx);
6981
6982       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
6983       md5_process_bytes (name, strlen (name) + 1, &ctx);
6984       md5_finish_ctx (&ctx, checksum);
6985
6986       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
6987     }
6988
6989   /* Next, compute the complete type signature.  */
6990
6991   md5_init_ctx (&ctx);
6992   mark = 1;
6993   die->die_mark = mark;
6994
6995   /* Checksum the names of surrounding namespaces and structures.  */
6996   if (decl != NULL && decl->die_parent != NULL)
6997     checksum_die_context (decl->die_parent, &ctx);
6998
6999   /* Checksum the DIE and its children.  */
7000   die_checksum_ordered (die, &ctx, &mark);
7001   unmark_all_dies (die);
7002   md5_finish_ctx (&ctx, checksum);
7003
7004   /* Store the signature in the type node and link the type DIE and the
7005      type node together.  */
7006   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
7007           DWARF_TYPE_SIGNATURE_SIZE);
7008   die->die_id.die_type_node = type_node;
7009   type_node->type_die = die;
7010
7011   /* If the DIE is a specification, link its declaration to the type node
7012      as well.  */
7013   if (decl != NULL)
7014     decl->die_id.die_type_node = type_node;
7015 }
7016
7017 /* Do the location expressions look same?  */
7018 static inline int
7019 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7020 {
7021   return loc1->dw_loc_opc == loc2->dw_loc_opc
7022          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7023          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7024 }
7025
7026 /* Do the values look the same?  */
7027 static int
7028 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7029 {
7030   dw_loc_descr_ref loc1, loc2;
7031   rtx r1, r2;
7032
7033   if (v1->val_class != v2->val_class)
7034     return 0;
7035
7036   switch (v1->val_class)
7037     {
7038     case dw_val_class_const:
7039       return v1->v.val_int == v2->v.val_int;
7040     case dw_val_class_unsigned_const:
7041       return v1->v.val_unsigned == v2->v.val_unsigned;
7042     case dw_val_class_const_double:
7043       return v1->v.val_double.high == v2->v.val_double.high
7044              && v1->v.val_double.low == v2->v.val_double.low;
7045     case dw_val_class_vec:
7046       if (v1->v.val_vec.length != v2->v.val_vec.length
7047           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7048         return 0;
7049       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7050                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7051         return 0;
7052       return 1;
7053     case dw_val_class_flag:
7054       return v1->v.val_flag == v2->v.val_flag;
7055     case dw_val_class_str:
7056       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7057
7058     case dw_val_class_addr:
7059       r1 = v1->v.val_addr;
7060       r2 = v2->v.val_addr;
7061       if (GET_CODE (r1) != GET_CODE (r2))
7062         return 0;
7063       return !rtx_equal_p (r1, r2);
7064
7065     case dw_val_class_offset:
7066       return v1->v.val_offset == v2->v.val_offset;
7067
7068     case dw_val_class_loc:
7069       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7070            loc1 && loc2;
7071            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7072         if (!same_loc_p (loc1, loc2, mark))
7073           return 0;
7074       return !loc1 && !loc2;
7075
7076     case dw_val_class_die_ref:
7077       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7078
7079     case dw_val_class_fde_ref:
7080     case dw_val_class_vms_delta:
7081     case dw_val_class_lbl_id:
7082     case dw_val_class_lineptr:
7083     case dw_val_class_macptr:
7084       return 1;
7085
7086     case dw_val_class_file:
7087       return v1->v.val_file == v2->v.val_file;
7088
7089     case dw_val_class_data8:
7090       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
7091
7092     default:
7093       return 1;
7094     }
7095 }
7096
7097 /* Do the attributes look the same?  */
7098
7099 static int
7100 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7101 {
7102   if (at1->dw_attr != at2->dw_attr)
7103     return 0;
7104
7105   /* We don't care that this was compiled with a different compiler
7106      snapshot; if the output is the same, that's what matters. */
7107   if (at1->dw_attr == DW_AT_producer)
7108     return 1;
7109
7110   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7111 }
7112
7113 /* Do the dies look the same?  */
7114
7115 static int
7116 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7117 {
7118   dw_die_ref c1, c2;
7119   dw_attr_ref a1;
7120   unsigned ix;
7121
7122   /* To avoid infinite recursion.  */
7123   if (die1->die_mark)
7124     return die1->die_mark == die2->die_mark;
7125   die1->die_mark = die2->die_mark = ++(*mark);
7126
7127   if (die1->die_tag != die2->die_tag)
7128     return 0;
7129
7130   if (VEC_length (dw_attr_node, die1->die_attr)
7131       != VEC_length (dw_attr_node, die2->die_attr))
7132     return 0;
7133
7134   FOR_EACH_VEC_ELT (dw_attr_node, die1->die_attr, ix, a1)
7135     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7136       return 0;
7137
7138   c1 = die1->die_child;
7139   c2 = die2->die_child;
7140   if (! c1)
7141     {
7142       if (c2)
7143         return 0;
7144     }
7145   else
7146     for (;;)
7147       {
7148         if (!same_die_p (c1, c2, mark))
7149           return 0;
7150         c1 = c1->die_sib;
7151         c2 = c2->die_sib;
7152         if (c1 == die1->die_child)
7153           {
7154             if (c2 == die2->die_child)
7155               break;
7156             else
7157               return 0;
7158           }
7159     }
7160
7161   return 1;
7162 }
7163
7164 /* Do the dies look the same?  Wrapper around same_die_p.  */
7165
7166 static int
7167 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7168 {
7169   int mark = 0;
7170   int ret = same_die_p (die1, die2, &mark);
7171
7172   unmark_all_dies (die1);
7173   unmark_all_dies (die2);
7174
7175   return ret;
7176 }
7177
7178 /* The prefix to attach to symbols on DIEs in the current comdat debug
7179    info section.  */
7180 static char *comdat_symbol_id;
7181
7182 /* The index of the current symbol within the current comdat CU.  */
7183 static unsigned int comdat_symbol_number;
7184
7185 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7186    children, and set comdat_symbol_id accordingly.  */
7187
7188 static void
7189 compute_section_prefix (dw_die_ref unit_die)
7190 {
7191   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7192   const char *base = die_name ? lbasename (die_name) : "anonymous";
7193   char *name = XALLOCAVEC (char, strlen (base) + 64);
7194   char *p;
7195   int i, mark;
7196   unsigned char checksum[16];
7197   struct md5_ctx ctx;
7198
7199   /* Compute the checksum of the DIE, then append part of it as hex digits to
7200      the name filename of the unit.  */
7201
7202   md5_init_ctx (&ctx);
7203   mark = 0;
7204   die_checksum (unit_die, &ctx, &mark);
7205   unmark_all_dies (unit_die);
7206   md5_finish_ctx (&ctx, checksum);
7207
7208   sprintf (name, "%s.", base);
7209   clean_symbol_name (name);
7210
7211   p = name + strlen (name);
7212   for (i = 0; i < 4; i++)
7213     {
7214       sprintf (p, "%.2x", checksum[i]);
7215       p += 2;
7216     }
7217
7218   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
7219   comdat_symbol_number = 0;
7220 }
7221
7222 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
7223
7224 static int
7225 is_type_die (dw_die_ref die)
7226 {
7227   switch (die->die_tag)
7228     {
7229     case DW_TAG_array_type:
7230     case DW_TAG_class_type:
7231     case DW_TAG_interface_type:
7232     case DW_TAG_enumeration_type:
7233     case DW_TAG_pointer_type:
7234     case DW_TAG_reference_type:
7235     case DW_TAG_rvalue_reference_type:
7236     case DW_TAG_string_type:
7237     case DW_TAG_structure_type:
7238     case DW_TAG_subroutine_type:
7239     case DW_TAG_union_type:
7240     case DW_TAG_ptr_to_member_type:
7241     case DW_TAG_set_type:
7242     case DW_TAG_subrange_type:
7243     case DW_TAG_base_type:
7244     case DW_TAG_const_type:
7245     case DW_TAG_file_type:
7246     case DW_TAG_packed_type:
7247     case DW_TAG_volatile_type:
7248     case DW_TAG_typedef:
7249       return 1;
7250     default:
7251       return 0;
7252     }
7253 }
7254
7255 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
7256    Basically, we want to choose the bits that are likely to be shared between
7257    compilations (types) and leave out the bits that are specific to individual
7258    compilations (functions).  */
7259
7260 static int
7261 is_comdat_die (dw_die_ref c)
7262 {
7263   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
7264      we do for stabs.  The advantage is a greater likelihood of sharing between
7265      objects that don't include headers in the same order (and therefore would
7266      put the base types in a different comdat).  jason 8/28/00 */
7267
7268   if (c->die_tag == DW_TAG_base_type)
7269     return 0;
7270
7271   if (c->die_tag == DW_TAG_pointer_type
7272       || c->die_tag == DW_TAG_reference_type
7273       || c->die_tag == DW_TAG_rvalue_reference_type
7274       || c->die_tag == DW_TAG_const_type
7275       || c->die_tag == DW_TAG_volatile_type)
7276     {
7277       dw_die_ref t = get_AT_ref (c, DW_AT_type);
7278
7279       return t ? is_comdat_die (t) : 0;
7280     }
7281
7282   return is_type_die (c);
7283 }
7284
7285 /* Returns 1 iff C is the sort of DIE that might be referred to from another
7286    compilation unit.  */
7287
7288 static int
7289 is_symbol_die (dw_die_ref c)
7290 {
7291   return (is_type_die (c)
7292           || is_declaration_die (c)
7293           || c->die_tag == DW_TAG_namespace
7294           || c->die_tag == DW_TAG_module);
7295 }
7296
7297 /* Returns true iff C is a compile-unit DIE.  */
7298
7299 static inline bool
7300 is_cu_die (dw_die_ref c)
7301 {
7302   return c && c->die_tag == DW_TAG_compile_unit;
7303 }
7304
7305 static char *
7306 gen_internal_sym (const char *prefix)
7307 {
7308   char buf[256];
7309
7310   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
7311   return xstrdup (buf);
7312 }
7313
7314 /* Assign symbols to all worthy DIEs under DIE.  */
7315
7316 static void
7317 assign_symbol_names (dw_die_ref die)
7318 {
7319   dw_die_ref c;
7320
7321   if (is_symbol_die (die))
7322     {
7323       if (comdat_symbol_id)
7324         {
7325           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
7326
7327           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
7328                    comdat_symbol_id, comdat_symbol_number++);
7329           die->die_id.die_symbol = xstrdup (p);
7330         }
7331       else
7332         die->die_id.die_symbol = gen_internal_sym ("LDIE");
7333     }
7334
7335   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
7336 }
7337
7338 struct cu_hash_table_entry
7339 {
7340   dw_die_ref cu;
7341   unsigned min_comdat_num, max_comdat_num;
7342   struct cu_hash_table_entry *next;
7343 };
7344
7345 /* Routines to manipulate hash table of CUs.  */
7346 static hashval_t
7347 htab_cu_hash (const void *of)
7348 {
7349   const struct cu_hash_table_entry *const entry =
7350     (const struct cu_hash_table_entry *) of;
7351
7352   return htab_hash_string (entry->cu->die_id.die_symbol);
7353 }
7354
7355 static int
7356 htab_cu_eq (const void *of1, const void *of2)
7357 {
7358   const struct cu_hash_table_entry *const entry1 =
7359     (const struct cu_hash_table_entry *) of1;
7360   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7361
7362   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
7363 }
7364
7365 static void
7366 htab_cu_del (void *what)
7367 {
7368   struct cu_hash_table_entry *next,
7369     *entry = (struct cu_hash_table_entry *) what;
7370
7371   while (entry)
7372     {
7373       next = entry->next;
7374       free (entry);
7375       entry = next;
7376     }
7377 }
7378
7379 /* Check whether we have already seen this CU and set up SYM_NUM
7380    accordingly.  */
7381 static int
7382 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
7383 {
7384   struct cu_hash_table_entry dummy;
7385   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
7386
7387   dummy.max_comdat_num = 0;
7388
7389   slot = (struct cu_hash_table_entry **)
7390     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
7391         INSERT);
7392   entry = *slot;
7393
7394   for (; entry; last = entry, entry = entry->next)
7395     {
7396       if (same_die_p_wrap (cu, entry->cu))
7397         break;
7398     }
7399
7400   if (entry)
7401     {
7402       *sym_num = entry->min_comdat_num;
7403       return 1;
7404     }
7405
7406   entry = XCNEW (struct cu_hash_table_entry);
7407   entry->cu = cu;
7408   entry->min_comdat_num = *sym_num = last->max_comdat_num;
7409   entry->next = *slot;
7410   *slot = entry;
7411
7412   return 0;
7413 }
7414
7415 /* Record SYM_NUM to record of CU in HTABLE.  */
7416 static void
7417 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
7418 {
7419   struct cu_hash_table_entry **slot, *entry;
7420
7421   slot = (struct cu_hash_table_entry **)
7422     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
7423         NO_INSERT);
7424   entry = *slot;
7425
7426   entry->max_comdat_num = sym_num;
7427 }
7428
7429 /* Traverse the DIE (which is always comp_unit_die), and set up
7430    additional compilation units for each of the include files we see
7431    bracketed by BINCL/EINCL.  */
7432
7433 static void
7434 break_out_includes (dw_die_ref die)
7435 {
7436   dw_die_ref c;
7437   dw_die_ref unit = NULL;
7438   limbo_die_node *node, **pnode;
7439   htab_t cu_hash_table;
7440
7441   c = die->die_child;
7442   if (c) do {
7443     dw_die_ref prev = c;
7444     c = c->die_sib;
7445     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
7446            || (unit && is_comdat_die (c)))
7447       {
7448         dw_die_ref next = c->die_sib;
7449
7450         /* This DIE is for a secondary CU; remove it from the main one.  */
7451         remove_child_with_prev (c, prev);
7452
7453         if (c->die_tag == DW_TAG_GNU_BINCL)
7454           unit = push_new_compile_unit (unit, c);
7455         else if (c->die_tag == DW_TAG_GNU_EINCL)
7456           unit = pop_compile_unit (unit);
7457         else
7458           add_child_die (unit, c);
7459         c = next;
7460         if (c == die->die_child)
7461           break;
7462       }
7463   } while (c != die->die_child);
7464
7465 #if 0
7466   /* We can only use this in debugging, since the frontend doesn't check
7467      to make sure that we leave every include file we enter.  */
7468   gcc_assert (!unit);
7469 #endif
7470
7471   assign_symbol_names (die);
7472   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
7473   for (node = limbo_die_list, pnode = &limbo_die_list;
7474        node;
7475        node = node->next)
7476     {
7477       int is_dupl;
7478
7479       compute_section_prefix (node->die);
7480       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
7481                         &comdat_symbol_number);
7482       assign_symbol_names (node->die);
7483       if (is_dupl)
7484         *pnode = node->next;
7485       else
7486         {
7487           pnode = &node->next;
7488           record_comdat_symbol_number (node->die, cu_hash_table,
7489                 comdat_symbol_number);
7490         }
7491     }
7492   htab_delete (cu_hash_table);
7493 }
7494
7495 /* Return non-zero if this DIE is a declaration.  */
7496
7497 static int
7498 is_declaration_die (dw_die_ref die)
7499 {
7500   dw_attr_ref a;
7501   unsigned ix;
7502
7503   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7504     if (a->dw_attr == DW_AT_declaration)
7505       return 1;
7506
7507   return 0;
7508 }
7509
7510 /* Return non-zero if this DIE is nested inside a subprogram.  */
7511
7512 static int
7513 is_nested_in_subprogram (dw_die_ref die)
7514 {
7515   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
7516
7517   if (decl == NULL)
7518     decl = die;
7519   return local_scope_p (decl);
7520 }
7521
7522 /* Return non-zero if this DIE contains a defining declaration of a
7523    subprogram.  */
7524
7525 static int
7526 contains_subprogram_definition (dw_die_ref die)
7527 {
7528   dw_die_ref c;
7529
7530   if (die->die_tag == DW_TAG_subprogram && ! is_declaration_die (die))
7531     return 1;
7532   FOR_EACH_CHILD (die, c, if (contains_subprogram_definition(c)) return 1);
7533   return 0;
7534 }
7535
7536 /* Return non-zero if this is a type DIE that should be moved to a
7537    COMDAT .debug_types section.  */
7538
7539 static int
7540 should_move_die_to_comdat (dw_die_ref die)
7541 {
7542   switch (die->die_tag)
7543     {
7544     case DW_TAG_class_type:
7545     case DW_TAG_structure_type:
7546     case DW_TAG_enumeration_type:
7547     case DW_TAG_union_type:
7548       /* Don't move declarations, inlined instances, or types nested in a
7549          subprogram.  */
7550       if (is_declaration_die (die)
7551           || get_AT (die, DW_AT_abstract_origin)
7552           || is_nested_in_subprogram (die))
7553         return 0;
7554       /* A type definition should never contain a subprogram definition.  */
7555       gcc_assert (!contains_subprogram_definition (die));
7556       return 1;
7557     case DW_TAG_array_type:
7558     case DW_TAG_interface_type:
7559     case DW_TAG_pointer_type:
7560     case DW_TAG_reference_type:
7561     case DW_TAG_rvalue_reference_type:
7562     case DW_TAG_string_type:
7563     case DW_TAG_subroutine_type:
7564     case DW_TAG_ptr_to_member_type:
7565     case DW_TAG_set_type:
7566     case DW_TAG_subrange_type:
7567     case DW_TAG_base_type:
7568     case DW_TAG_const_type:
7569     case DW_TAG_file_type:
7570     case DW_TAG_packed_type:
7571     case DW_TAG_volatile_type:
7572     case DW_TAG_typedef:
7573     default:
7574       return 0;
7575     }
7576 }
7577
7578 /* Make a clone of DIE.  */
7579
7580 static dw_die_ref
7581 clone_die (dw_die_ref die)
7582 {
7583   dw_die_ref clone;
7584   dw_attr_ref a;
7585   unsigned ix;
7586
7587   clone = ggc_alloc_cleared_die_node ();
7588   clone->die_tag = die->die_tag;
7589
7590   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7591     add_dwarf_attr (clone, a);
7592
7593   return clone;
7594 }
7595
7596 /* Make a clone of the tree rooted at DIE.  */
7597
7598 static dw_die_ref
7599 clone_tree (dw_die_ref die)
7600 {
7601   dw_die_ref c;
7602   dw_die_ref clone = clone_die (die);
7603
7604   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
7605
7606   return clone;
7607 }
7608
7609 /* Make a clone of DIE as a declaration.  */
7610
7611 static dw_die_ref
7612 clone_as_declaration (dw_die_ref die)
7613 {
7614   dw_die_ref clone;
7615   dw_die_ref decl;
7616   dw_attr_ref a;
7617   unsigned ix;
7618
7619   /* If the DIE is already a declaration, just clone it.  */
7620   if (is_declaration_die (die))
7621     return clone_die (die);
7622
7623   /* If the DIE is a specification, just clone its declaration DIE.  */
7624   decl = get_AT_ref (die, DW_AT_specification);
7625   if (decl != NULL)
7626     return clone_die (decl);
7627
7628   clone = ggc_alloc_cleared_die_node ();
7629   clone->die_tag = die->die_tag;
7630
7631   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7632     {
7633       /* We don't want to copy over all attributes.
7634          For example we don't want DW_AT_byte_size because otherwise we will no
7635          longer have a declaration and GDB will treat it as a definition.  */
7636
7637       switch (a->dw_attr)
7638         {
7639         case DW_AT_artificial:
7640         case DW_AT_containing_type:
7641         case DW_AT_external:
7642         case DW_AT_name:
7643         case DW_AT_type:
7644         case DW_AT_virtuality:
7645         case DW_AT_linkage_name:
7646         case DW_AT_MIPS_linkage_name:
7647           add_dwarf_attr (clone, a);
7648           break;
7649         case DW_AT_byte_size:
7650         default:
7651           break;
7652         }
7653     }
7654
7655   if (die->die_id.die_type_node)
7656     add_AT_die_ref (clone, DW_AT_signature, die);
7657
7658   add_AT_flag (clone, DW_AT_declaration, 1);
7659   return clone;
7660 }
7661
7662 /* Copy the declaration context to the new compile unit DIE.  This includes
7663    any surrounding namespace or type declarations.  If the DIE has an
7664    AT_specification attribute, it also includes attributes and children
7665    attached to the specification.  */
7666
7667 static void
7668 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
7669 {
7670   dw_die_ref decl;
7671   dw_die_ref new_decl;
7672
7673   decl = get_AT_ref (die, DW_AT_specification);
7674   if (decl == NULL)
7675     decl = die;
7676   else
7677     {
7678       unsigned ix;
7679       dw_die_ref c;
7680       dw_attr_ref a;
7681
7682       /* Copy the type node pointer from the new DIE to the original
7683          declaration DIE so we can forward references later.  */
7684       decl->die_id.die_type_node = die->die_id.die_type_node;
7685
7686       remove_AT (die, DW_AT_specification);
7687
7688       FOR_EACH_VEC_ELT (dw_attr_node, decl->die_attr, ix, a)
7689         {
7690           if (a->dw_attr != DW_AT_name
7691               && a->dw_attr != DW_AT_declaration
7692               && a->dw_attr != DW_AT_external)
7693             add_dwarf_attr (die, a);
7694         }
7695
7696       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
7697     }
7698
7699   if (decl->die_parent != NULL
7700       && decl->die_parent->die_tag != DW_TAG_compile_unit
7701       && decl->die_parent->die_tag != DW_TAG_type_unit)
7702     {
7703       new_decl = copy_ancestor_tree (unit, decl, NULL);
7704       if (new_decl != NULL)
7705         {
7706           remove_AT (new_decl, DW_AT_signature);
7707           add_AT_specification (die, new_decl);
7708         }
7709     }
7710 }
7711
7712 /* Generate the skeleton ancestor tree for the given NODE, then clone
7713    the DIE and add the clone into the tree.  */
7714
7715 static void
7716 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
7717 {
7718   if (node->new_die != NULL)
7719     return;
7720
7721   node->new_die = clone_as_declaration (node->old_die);
7722
7723   if (node->parent != NULL)
7724     {
7725       generate_skeleton_ancestor_tree (node->parent);
7726       add_child_die (node->parent->new_die, node->new_die);
7727     }
7728 }
7729
7730 /* Generate a skeleton tree of DIEs containing any declarations that are
7731    found in the original tree.  We traverse the tree looking for declaration
7732    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
7733
7734 static void
7735 generate_skeleton_bottom_up (skeleton_chain_node *parent)
7736 {
7737   skeleton_chain_node node;
7738   dw_die_ref c;
7739   dw_die_ref first;
7740   dw_die_ref prev = NULL;
7741   dw_die_ref next = NULL;
7742
7743   node.parent = parent;
7744
7745   first = c = parent->old_die->die_child;
7746   if (c)
7747     next = c->die_sib;
7748   if (c) do {
7749     if (prev == NULL || prev->die_sib == c)
7750       prev = c;
7751     c = next;
7752     next = (c == first ? NULL : c->die_sib);
7753     node.old_die = c;
7754     node.new_die = NULL;
7755     if (is_declaration_die (c))
7756       {
7757         /* Clone the existing DIE, move the original to the skeleton
7758            tree (which is in the main CU), and put the clone, with
7759            all the original's children, where the original came from.  */
7760         dw_die_ref clone = clone_die (c);
7761         move_all_children (c, clone);
7762
7763         replace_child (c, clone, prev);
7764         generate_skeleton_ancestor_tree (parent);
7765         add_child_die (parent->new_die, c);
7766         node.new_die = c;
7767         c = clone;
7768       }
7769     generate_skeleton_bottom_up (&node);
7770   } while (next != NULL);
7771 }
7772
7773 /* Wrapper function for generate_skeleton_bottom_up.  */
7774
7775 static dw_die_ref
7776 generate_skeleton (dw_die_ref die)
7777 {
7778   skeleton_chain_node node;
7779
7780   node.old_die = die;
7781   node.new_die = NULL;
7782   node.parent = NULL;
7783
7784   /* If this type definition is nested inside another type,
7785      always leave at least a declaration in its place.  */
7786   if (die->die_parent != NULL && is_type_die (die->die_parent))
7787     node.new_die = clone_as_declaration (die);
7788
7789   generate_skeleton_bottom_up (&node);
7790   return node.new_die;
7791 }
7792
7793 /* Remove the DIE from its parent, possibly replacing it with a cloned
7794    declaration.  The original DIE will be moved to a new compile unit
7795    so that existing references to it follow it to the new location.  If
7796    any of the original DIE's descendants is a declaration, we need to
7797    replace the original DIE with a skeleton tree and move the
7798    declarations back into the skeleton tree.  */
7799
7800 static dw_die_ref
7801 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
7802 {
7803   dw_die_ref skeleton;
7804
7805   skeleton = generate_skeleton (child);
7806   if (skeleton == NULL)
7807     remove_child_with_prev (child, prev);
7808   else
7809     {
7810       skeleton->die_id.die_type_node = child->die_id.die_type_node;
7811       replace_child (child, skeleton, prev);
7812     }
7813
7814   return skeleton;
7815 }
7816
7817 /* Traverse the DIE and set up additional .debug_types sections for each
7818    type worthy of being placed in a COMDAT section.  */
7819
7820 static void
7821 break_out_comdat_types (dw_die_ref die)
7822 {
7823   dw_die_ref c;
7824   dw_die_ref first;
7825   dw_die_ref prev = NULL;
7826   dw_die_ref next = NULL;
7827   dw_die_ref unit = NULL;
7828
7829   first = c = die->die_child;
7830   if (c)
7831     next = c->die_sib;
7832   if (c) do {
7833     if (prev == NULL || prev->die_sib == c)
7834       prev = c;
7835     c = next;
7836     next = (c == first ? NULL : c->die_sib);
7837     if (should_move_die_to_comdat (c))
7838       {
7839         dw_die_ref replacement;
7840         comdat_type_node_ref type_node;
7841
7842         /* Create a new type unit DIE as the root for the new tree, and
7843            add it to the list of comdat types.  */
7844         unit = new_die (DW_TAG_type_unit, NULL, NULL);
7845         add_AT_unsigned (unit, DW_AT_language,
7846                          get_AT_unsigned (comp_unit_die (), DW_AT_language));
7847         type_node = ggc_alloc_cleared_comdat_type_node ();
7848         type_node->root_die = unit;
7849         type_node->next = comdat_type_list;
7850         comdat_type_list = type_node;
7851
7852         /* Generate the type signature.  */
7853         generate_type_signature (c, type_node);
7854
7855         /* Copy the declaration context, attributes, and children of the
7856            declaration into the new compile unit DIE.  */
7857         copy_declaration_context (unit, c);
7858
7859         /* Remove this DIE from the main CU.  */
7860         replacement = remove_child_or_replace_with_skeleton (c, prev);
7861
7862         /* Break out nested types into their own type units.  */
7863         break_out_comdat_types (c);
7864
7865         /* Add the DIE to the new compunit.  */
7866         add_child_die (unit, c);
7867
7868         if (replacement != NULL)
7869           c = replacement;
7870       }
7871     else if (c->die_tag == DW_TAG_namespace
7872              || c->die_tag == DW_TAG_class_type
7873              || c->die_tag == DW_TAG_structure_type
7874              || c->die_tag == DW_TAG_union_type)
7875       {
7876         /* Look for nested types that can be broken out.  */
7877         break_out_comdat_types (c);
7878       }
7879   } while (next != NULL);
7880 }
7881
7882 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
7883
7884 struct decl_table_entry
7885 {
7886   dw_die_ref orig;
7887   dw_die_ref copy;
7888 };
7889
7890 /* Routines to manipulate hash table of copied declarations.  */
7891
7892 static hashval_t
7893 htab_decl_hash (const void *of)
7894 {
7895   const struct decl_table_entry *const entry =
7896     (const struct decl_table_entry *) of;
7897
7898   return htab_hash_pointer (entry->orig);
7899 }
7900
7901 static int
7902 htab_decl_eq (const void *of1, const void *of2)
7903 {
7904   const struct decl_table_entry *const entry1 =
7905     (const struct decl_table_entry *) of1;
7906   const struct die_struct *const entry2 = (const struct die_struct *) of2;
7907
7908   return entry1->orig == entry2;
7909 }
7910
7911 static void
7912 htab_decl_del (void *what)
7913 {
7914   struct decl_table_entry *entry = (struct decl_table_entry *) what;
7915
7916   free (entry);
7917 }
7918
7919 /* Copy DIE and its ancestors, up to, but not including, the compile unit
7920    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
7921    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
7922    to check if the ancestor has already been copied into UNIT.  */
7923
7924 static dw_die_ref
7925 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7926 {
7927   dw_die_ref parent = die->die_parent;
7928   dw_die_ref new_parent = unit;
7929   dw_die_ref copy;
7930   void **slot = NULL;
7931   struct decl_table_entry *entry = NULL;
7932
7933   if (decl_table)
7934     {
7935       /* Check if the entry has already been copied to UNIT.  */
7936       slot = htab_find_slot_with_hash (decl_table, die,
7937                                        htab_hash_pointer (die), INSERT);
7938       if (*slot != HTAB_EMPTY_ENTRY)
7939         {
7940           entry = (struct decl_table_entry *) *slot;
7941           return entry->copy;
7942         }
7943
7944       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
7945       entry = XCNEW (struct decl_table_entry);
7946       entry->orig = die;
7947       entry->copy = NULL;
7948       *slot = entry;
7949     }
7950
7951   if (parent != NULL)
7952     {
7953       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
7954       if (spec != NULL)
7955         parent = spec;
7956       if (parent->die_tag != DW_TAG_compile_unit
7957           && parent->die_tag != DW_TAG_type_unit)
7958         new_parent = copy_ancestor_tree (unit, parent, decl_table);
7959     }
7960
7961   copy = clone_as_declaration (die);
7962   add_child_die (new_parent, copy);
7963
7964   if (decl_table != NULL)
7965     {
7966       /* Record the pointer to the copy.  */
7967       entry->copy = copy;
7968     }
7969
7970   return copy;
7971 }
7972
7973 /* Walk the DIE and its children, looking for references to incomplete
7974    or trivial types that are unmarked (i.e., that are not in the current
7975    type_unit).  */
7976
7977 static void
7978 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
7979 {
7980   dw_die_ref c;
7981   dw_attr_ref a;
7982   unsigned ix;
7983
7984   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
7985     {
7986       if (AT_class (a) == dw_val_class_die_ref)
7987         {
7988           dw_die_ref targ = AT_ref (a);
7989           comdat_type_node_ref type_node = targ->die_id.die_type_node;
7990           void **slot;
7991           struct decl_table_entry *entry;
7992
7993           if (targ->die_mark != 0 || type_node != NULL)
7994             continue;
7995
7996           slot = htab_find_slot_with_hash (decl_table, targ,
7997                                            htab_hash_pointer (targ), INSERT);
7998
7999           if (*slot != HTAB_EMPTY_ENTRY)
8000             {
8001               /* TARG has already been copied, so we just need to
8002                  modify the reference to point to the copy.  */
8003               entry = (struct decl_table_entry *) *slot;
8004               a->dw_attr_val.v.val_die_ref.die = entry->copy;
8005             }
8006           else
8007             {
8008               dw_die_ref parent = unit;
8009               dw_die_ref copy = clone_tree (targ);
8010
8011               /* Make sure the cloned tree is marked as part of the
8012                  type unit.  */
8013               mark_dies (copy);
8014
8015               /* Record in DECL_TABLE that TARG has been copied.
8016                  Need to do this now, before the recursive call,
8017                  because DECL_TABLE may be expanded and SLOT
8018                  would no longer be a valid pointer.  */
8019               entry = XCNEW (struct decl_table_entry);
8020               entry->orig = targ;
8021               entry->copy = copy;
8022               *slot = entry;
8023
8024               /* If TARG has surrounding context, copy its ancestor tree
8025                  into the new type unit.  */
8026               if (targ->die_parent != NULL
8027                   && targ->die_parent->die_tag != DW_TAG_compile_unit
8028                   && targ->die_parent->die_tag != DW_TAG_type_unit)
8029                 parent = copy_ancestor_tree (unit, targ->die_parent,
8030                                              decl_table);
8031
8032               add_child_die (parent, copy);
8033               a->dw_attr_val.v.val_die_ref.die = copy;
8034
8035               /* Make sure the newly-copied DIE is walked.  If it was
8036                  installed in a previously-added context, it won't
8037                  get visited otherwise.  */
8038               if (parent != unit)
8039                 {
8040                   /* Find the highest point of the newly-added tree,
8041                      mark each node along the way, and walk from there.  */
8042                   parent->die_mark = 1;
8043                   while (parent->die_parent
8044                          && parent->die_parent->die_mark == 0)
8045                     {
8046                       parent = parent->die_parent;
8047                       parent->die_mark = 1;
8048                     }
8049                   copy_decls_walk (unit, parent, decl_table);
8050                 }
8051             }
8052         }
8053     }
8054
8055   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
8056 }
8057
8058 /* Copy declarations for "unworthy" types into the new comdat section.
8059    Incomplete types, modified types, and certain other types aren't broken
8060    out into comdat sections of their own, so they don't have a signature,
8061    and we need to copy the declaration into the same section so that we
8062    don't have an external reference.  */
8063
8064 static void
8065 copy_decls_for_unworthy_types (dw_die_ref unit)
8066 {
8067   htab_t decl_table;
8068
8069   mark_dies (unit);
8070   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
8071   copy_decls_walk (unit, unit, decl_table);
8072   htab_delete (decl_table);
8073   unmark_dies (unit);
8074 }
8075
8076 /* Traverse the DIE and add a sibling attribute if it may have the
8077    effect of speeding up access to siblings.  To save some space,
8078    avoid generating sibling attributes for DIE's without children.  */
8079
8080 static void
8081 add_sibling_attributes (dw_die_ref die)
8082 {
8083   dw_die_ref c;
8084
8085   if (! die->die_child)
8086     return;
8087
8088   if (die->die_parent && die != die->die_parent->die_child)
8089     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8090
8091   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8092 }
8093
8094 /* Output all location lists for the DIE and its children.  */
8095
8096 static void
8097 output_location_lists (dw_die_ref die)
8098 {
8099   dw_die_ref c;
8100   dw_attr_ref a;
8101   unsigned ix;
8102
8103   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8104     if (AT_class (a) == dw_val_class_loc_list)
8105       output_loc_list (AT_loc_list (a));
8106
8107   FOR_EACH_CHILD (die, c, output_location_lists (c));
8108 }
8109
8110 /* The format of each DIE (and its attribute value pairs) is encoded in an
8111    abbreviation table.  This routine builds the abbreviation table and assigns
8112    a unique abbreviation id for each abbreviation entry.  The children of each
8113    die are visited recursively.  */
8114
8115 static void
8116 build_abbrev_table (dw_die_ref die)
8117 {
8118   unsigned long abbrev_id;
8119   unsigned int n_alloc;
8120   dw_die_ref c;
8121   dw_attr_ref a;
8122   unsigned ix;
8123
8124   /* Scan the DIE references, and mark as external any that refer to
8125      DIEs from other CUs (i.e. those which are not marked).  */
8126   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8127     if (AT_class (a) == dw_val_class_die_ref
8128         && AT_ref (a)->die_mark == 0)
8129       {
8130         gcc_assert (use_debug_types || AT_ref (a)->die_id.die_symbol);
8131         set_AT_ref_external (a, 1);
8132       }
8133
8134   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8135     {
8136       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8137       dw_attr_ref die_a, abbrev_a;
8138       unsigned ix;
8139       bool ok = true;
8140
8141       if (abbrev->die_tag != die->die_tag)
8142         continue;
8143       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8144         continue;
8145
8146       if (VEC_length (dw_attr_node, abbrev->die_attr)
8147           != VEC_length (dw_attr_node, die->die_attr))
8148         continue;
8149
8150       FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, die_a)
8151         {
8152           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8153           if ((abbrev_a->dw_attr != die_a->dw_attr)
8154               || (value_format (abbrev_a) != value_format (die_a)))
8155             {
8156               ok = false;
8157               break;
8158             }
8159         }
8160       if (ok)
8161         break;
8162     }
8163
8164   if (abbrev_id >= abbrev_die_table_in_use)
8165     {
8166       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8167         {
8168           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8169           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8170                                             n_alloc);
8171
8172           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8173                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8174           abbrev_die_table_allocated = n_alloc;
8175         }
8176
8177       ++abbrev_die_table_in_use;
8178       abbrev_die_table[abbrev_id] = die;
8179     }
8180
8181   die->die_abbrev = abbrev_id;
8182   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8183 }
8184 \f
8185 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8186
8187 static int
8188 constant_size (unsigned HOST_WIDE_INT value)
8189 {
8190   int log;
8191
8192   if (value == 0)
8193     log = 0;
8194   else
8195     log = floor_log2 (value);
8196
8197   log = log / 8;
8198   log = 1 << (floor_log2 (log) + 1);
8199
8200   return log;
8201 }
8202
8203 /* Return the size of a DIE as it is represented in the
8204    .debug_info section.  */
8205
8206 static unsigned long
8207 size_of_die (dw_die_ref die)
8208 {
8209   unsigned long size = 0;
8210   dw_attr_ref a;
8211   unsigned ix;
8212
8213   size += size_of_uleb128 (die->die_abbrev);
8214   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8215     {
8216       switch (AT_class (a))
8217         {
8218         case dw_val_class_addr:
8219           size += DWARF2_ADDR_SIZE;
8220           break;
8221         case dw_val_class_offset:
8222           size += DWARF_OFFSET_SIZE;
8223           break;
8224         case dw_val_class_loc:
8225           {
8226             unsigned long lsize = size_of_locs (AT_loc (a));
8227
8228             /* Block length.  */
8229             if (dwarf_version >= 4)
8230               size += size_of_uleb128 (lsize);
8231             else
8232               size += constant_size (lsize);
8233             size += lsize;
8234           }
8235           break;
8236         case dw_val_class_loc_list:
8237           size += DWARF_OFFSET_SIZE;
8238           break;
8239         case dw_val_class_range_list:
8240           size += DWARF_OFFSET_SIZE;
8241           break;
8242         case dw_val_class_const:
8243           size += size_of_sleb128 (AT_int (a));
8244           break;
8245         case dw_val_class_unsigned_const:
8246           size += constant_size (AT_unsigned (a));
8247           break;
8248         case dw_val_class_const_double:
8249           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8250           if (HOST_BITS_PER_WIDE_INT >= 64)
8251             size++; /* block */
8252           break;
8253         case dw_val_class_vec:
8254           size += constant_size (a->dw_attr_val.v.val_vec.length
8255                                  * a->dw_attr_val.v.val_vec.elt_size)
8256                   + a->dw_attr_val.v.val_vec.length
8257                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8258           break;
8259         case dw_val_class_flag:
8260           if (dwarf_version >= 4)
8261             /* Currently all add_AT_flag calls pass in 1 as last argument,
8262                so DW_FORM_flag_present can be used.  If that ever changes,
8263                we'll need to use DW_FORM_flag and have some optimization
8264                in build_abbrev_table that will change those to
8265                DW_FORM_flag_present if it is set to 1 in all DIEs using
8266                the same abbrev entry.  */
8267             gcc_assert (a->dw_attr_val.v.val_flag == 1);
8268           else
8269             size += 1;
8270           break;
8271         case dw_val_class_die_ref:
8272           if (AT_ref_external (a))
8273             {
8274               /* In DWARF4, we use DW_FORM_ref_sig8; for earlier versions
8275                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
8276                  is sized by target address length, whereas in DWARF3
8277                  it's always sized as an offset.  */
8278               if (use_debug_types)
8279                 size += DWARF_TYPE_SIGNATURE_SIZE;
8280               else if (dwarf_version == 2)
8281                 size += DWARF2_ADDR_SIZE;
8282               else
8283                 size += DWARF_OFFSET_SIZE;
8284             }
8285           else
8286             size += DWARF_OFFSET_SIZE;
8287           break;
8288         case dw_val_class_fde_ref:
8289           size += DWARF_OFFSET_SIZE;
8290           break;
8291         case dw_val_class_lbl_id:
8292           size += DWARF2_ADDR_SIZE;
8293           break;
8294         case dw_val_class_lineptr:
8295         case dw_val_class_macptr:
8296           size += DWARF_OFFSET_SIZE;
8297           break;
8298         case dw_val_class_str:
8299           if (AT_string_form (a) == DW_FORM_strp)
8300             size += DWARF_OFFSET_SIZE;
8301           else
8302             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8303           break;
8304         case dw_val_class_file:
8305           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8306           break;
8307         case dw_val_class_data8:
8308           size += 8;
8309           break;
8310         case dw_val_class_vms_delta:
8311           size += DWARF_OFFSET_SIZE;
8312           break;
8313         default:
8314           gcc_unreachable ();
8315         }
8316     }
8317
8318   return size;
8319 }
8320
8321 /* Size the debugging information associated with a given DIE.  Visits the
8322    DIE's children recursively.  Updates the global variable next_die_offset, on
8323    each time through.  Uses the current value of next_die_offset to update the
8324    die_offset field in each DIE.  */
8325
8326 static void
8327 calc_die_sizes (dw_die_ref die)
8328 {
8329   dw_die_ref c;
8330
8331   gcc_assert (die->die_offset == 0
8332               || (unsigned long int) die->die_offset == next_die_offset);
8333   die->die_offset = next_die_offset;
8334   next_die_offset += size_of_die (die);
8335
8336   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8337
8338   if (die->die_child != NULL)
8339     /* Count the null byte used to terminate sibling lists.  */
8340     next_die_offset += 1;
8341 }
8342
8343 /* Size just the base type children at the start of the CU.
8344    This is needed because build_abbrev needs to size locs
8345    and sizing of type based stack ops needs to know die_offset
8346    values for the base types.  */
8347
8348 static void
8349 calc_base_type_die_sizes (void)
8350 {
8351   unsigned long die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
8352   unsigned int i;
8353   dw_die_ref base_type;
8354 #if ENABLE_ASSERT_CHECKING
8355   dw_die_ref prev = comp_unit_die ()->die_child;
8356 #endif
8357
8358   die_offset += size_of_die (comp_unit_die ());
8359   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
8360     {
8361 #if ENABLE_ASSERT_CHECKING
8362       gcc_assert (base_type->die_offset == 0
8363                   && prev->die_sib == base_type
8364                   && base_type->die_child == NULL
8365                   && base_type->die_abbrev);
8366       prev = base_type;
8367 #endif
8368       base_type->die_offset = die_offset;
8369       die_offset += size_of_die (base_type);
8370     }
8371 }
8372
8373 /* Set the marks for a die and its children.  We do this so
8374    that we know whether or not a reference needs to use FORM_ref_addr; only
8375    DIEs in the same CU will be marked.  We used to clear out the offset
8376    and use that as the flag, but ran into ordering problems.  */
8377
8378 static void
8379 mark_dies (dw_die_ref die)
8380 {
8381   dw_die_ref c;
8382
8383   gcc_assert (!die->die_mark);
8384
8385   die->die_mark = 1;
8386   FOR_EACH_CHILD (die, c, mark_dies (c));
8387 }
8388
8389 /* Clear the marks for a die and its children.  */
8390
8391 static void
8392 unmark_dies (dw_die_ref die)
8393 {
8394   dw_die_ref c;
8395
8396   if (! use_debug_types)
8397     gcc_assert (die->die_mark);
8398
8399   die->die_mark = 0;
8400   FOR_EACH_CHILD (die, c, unmark_dies (c));
8401 }
8402
8403 /* Clear the marks for a die, its children and referred dies.  */
8404
8405 static void
8406 unmark_all_dies (dw_die_ref die)
8407 {
8408   dw_die_ref c;
8409   dw_attr_ref a;
8410   unsigned ix;
8411
8412   if (!die->die_mark)
8413     return;
8414   die->die_mark = 0;
8415
8416   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8417
8418   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8419     if (AT_class (a) == dw_val_class_die_ref)
8420       unmark_all_dies (AT_ref (a));
8421 }
8422
8423 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8424    generated for the compilation unit.  */
8425
8426 static unsigned long
8427 size_of_pubnames (VEC (pubname_entry, gc) * names)
8428 {
8429   unsigned long size;
8430   unsigned i;
8431   pubname_ref p;
8432
8433   size = DWARF_PUBNAMES_HEADER_SIZE;
8434   FOR_EACH_VEC_ELT (pubname_entry, names, i, p)
8435     if (names != pubtype_table
8436         || p->die->die_offset != 0
8437         || !flag_eliminate_unused_debug_types)
8438       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8439
8440   size += DWARF_OFFSET_SIZE;
8441   return size;
8442 }
8443
8444 /* Return the size of the information in the .debug_aranges section.  */
8445
8446 static unsigned long
8447 size_of_aranges (void)
8448 {
8449   unsigned long size;
8450
8451   size = DWARF_ARANGES_HEADER_SIZE;
8452
8453   /* Count the address/length pair for this compilation unit.  */
8454   if (text_section_used)
8455     size += 2 * DWARF2_ADDR_SIZE;
8456   if (cold_text_section_used)
8457     size += 2 * DWARF2_ADDR_SIZE;
8458   if (have_multiple_function_sections)
8459     {
8460       unsigned fde_idx = 0;
8461
8462       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
8463         {
8464           dw_fde_ref fde = &fde_table[fde_idx];
8465
8466           if (!fde->in_std_section)
8467             size += 2 * DWARF2_ADDR_SIZE;
8468           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
8469             size += 2 * DWARF2_ADDR_SIZE;
8470         }
8471     }
8472
8473   /* Count the two zero words used to terminated the address range table.  */
8474   size += 2 * DWARF2_ADDR_SIZE;
8475   return size;
8476 }
8477 \f
8478 /* Select the encoding of an attribute value.  */
8479
8480 static enum dwarf_form
8481 value_format (dw_attr_ref a)
8482 {
8483   switch (a->dw_attr_val.val_class)
8484     {
8485     case dw_val_class_addr:
8486       /* Only very few attributes allow DW_FORM_addr.  */
8487       switch (a->dw_attr)
8488         {
8489         case DW_AT_low_pc:
8490         case DW_AT_high_pc:
8491         case DW_AT_entry_pc:
8492         case DW_AT_trampoline:
8493           return DW_FORM_addr;
8494         default:
8495           break;
8496         }
8497       switch (DWARF2_ADDR_SIZE)
8498         {
8499         case 1:
8500           return DW_FORM_data1;
8501         case 2:
8502           return DW_FORM_data2;
8503         case 4:
8504           return DW_FORM_data4;
8505         case 8:
8506           return DW_FORM_data8;
8507         default:
8508           gcc_unreachable ();
8509         }
8510     case dw_val_class_range_list:
8511     case dw_val_class_loc_list:
8512       if (dwarf_version >= 4)
8513         return DW_FORM_sec_offset;
8514       /* FALLTHRU */
8515     case dw_val_class_vms_delta:
8516     case dw_val_class_offset:
8517       switch (DWARF_OFFSET_SIZE)
8518         {
8519         case 4:
8520           return DW_FORM_data4;
8521         case 8:
8522           return DW_FORM_data8;
8523         default:
8524           gcc_unreachable ();
8525         }
8526     case dw_val_class_loc:
8527       if (dwarf_version >= 4)
8528         return DW_FORM_exprloc;
8529       switch (constant_size (size_of_locs (AT_loc (a))))
8530         {
8531         case 1:
8532           return DW_FORM_block1;
8533         case 2:
8534           return DW_FORM_block2;
8535         default:
8536           gcc_unreachable ();
8537         }
8538     case dw_val_class_const:
8539       return DW_FORM_sdata;
8540     case dw_val_class_unsigned_const:
8541       switch (constant_size (AT_unsigned (a)))
8542         {
8543         case 1:
8544           return DW_FORM_data1;
8545         case 2:
8546           return DW_FORM_data2;
8547         case 4:
8548           return DW_FORM_data4;
8549         case 8:
8550           return DW_FORM_data8;
8551         default:
8552           gcc_unreachable ();
8553         }
8554     case dw_val_class_const_double:
8555       switch (HOST_BITS_PER_WIDE_INT)
8556         {
8557         case 8:
8558           return DW_FORM_data2;
8559         case 16:
8560           return DW_FORM_data4;
8561         case 32:
8562           return DW_FORM_data8;
8563         case 64:
8564         default:
8565           return DW_FORM_block1;
8566         }
8567     case dw_val_class_vec:
8568       switch (constant_size (a->dw_attr_val.v.val_vec.length
8569                              * a->dw_attr_val.v.val_vec.elt_size))
8570         {
8571         case 1:
8572           return DW_FORM_block1;
8573         case 2:
8574           return DW_FORM_block2;
8575         case 4:
8576           return DW_FORM_block4;
8577         default:
8578           gcc_unreachable ();
8579         }
8580     case dw_val_class_flag:
8581       if (dwarf_version >= 4)
8582         {
8583           /* Currently all add_AT_flag calls pass in 1 as last argument,
8584              so DW_FORM_flag_present can be used.  If that ever changes,
8585              we'll need to use DW_FORM_flag and have some optimization
8586              in build_abbrev_table that will change those to
8587              DW_FORM_flag_present if it is set to 1 in all DIEs using
8588              the same abbrev entry.  */
8589           gcc_assert (a->dw_attr_val.v.val_flag == 1);
8590           return DW_FORM_flag_present;
8591         }
8592       return DW_FORM_flag;
8593     case dw_val_class_die_ref:
8594       if (AT_ref_external (a))
8595         return use_debug_types ? DW_FORM_ref_sig8 : DW_FORM_ref_addr;
8596       else
8597         return DW_FORM_ref;
8598     case dw_val_class_fde_ref:
8599       return DW_FORM_data;
8600     case dw_val_class_lbl_id:
8601       return DW_FORM_addr;
8602     case dw_val_class_lineptr:
8603     case dw_val_class_macptr:
8604       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
8605     case dw_val_class_str:
8606       return AT_string_form (a);
8607     case dw_val_class_file:
8608       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8609         {
8610         case 1:
8611           return DW_FORM_data1;
8612         case 2:
8613           return DW_FORM_data2;
8614         case 4:
8615           return DW_FORM_data4;
8616         default:
8617           gcc_unreachable ();
8618         }
8619
8620     case dw_val_class_data8:
8621       return DW_FORM_data8;
8622
8623     default:
8624       gcc_unreachable ();
8625     }
8626 }
8627
8628 /* Output the encoding of an attribute value.  */
8629
8630 static void
8631 output_value_format (dw_attr_ref a)
8632 {
8633   enum dwarf_form form = value_format (a);
8634
8635   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8636 }
8637
8638 /* Output the .debug_abbrev section which defines the DIE abbreviation
8639    table.  */
8640
8641 static void
8642 output_abbrev_section (void)
8643 {
8644   unsigned long abbrev_id;
8645
8646   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8647     {
8648       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8649       unsigned ix;
8650       dw_attr_ref a_attr;
8651
8652       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8653       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8654                                    dwarf_tag_name (abbrev->die_tag));
8655
8656       if (abbrev->die_child != NULL)
8657         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8658       else
8659         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8660
8661       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8662            ix++)
8663         {
8664           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8665                                        dwarf_attr_name (a_attr->dw_attr));
8666           output_value_format (a_attr);
8667         }
8668
8669       dw2_asm_output_data (1, 0, NULL);
8670       dw2_asm_output_data (1, 0, NULL);
8671     }
8672
8673   /* Terminate the table.  */
8674   dw2_asm_output_data (1, 0, NULL);
8675 }
8676
8677 /* Output a symbol we can use to refer to this DIE from another CU.  */
8678
8679 static inline void
8680 output_die_symbol (dw_die_ref die)
8681 {
8682   char *sym = die->die_id.die_symbol;
8683
8684   if (sym == 0)
8685     return;
8686
8687   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8688     /* We make these global, not weak; if the target doesn't support
8689        .linkonce, it doesn't support combining the sections, so debugging
8690        will break.  */
8691     targetm.asm_out.globalize_label (asm_out_file, sym);
8692
8693   ASM_OUTPUT_LABEL (asm_out_file, sym);
8694 }
8695
8696 /* Return a new location list, given the begin and end range, and the
8697    expression.  */
8698
8699 static inline dw_loc_list_ref
8700 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8701               const char *section)
8702 {
8703   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
8704
8705   retlist->begin = begin;
8706   retlist->end = end;
8707   retlist->expr = expr;
8708   retlist->section = section;
8709
8710   return retlist;
8711 }
8712
8713 /* Generate a new internal symbol for this location list node, if it
8714    hasn't got one yet.  */
8715
8716 static inline void
8717 gen_llsym (dw_loc_list_ref list)
8718 {
8719   gcc_assert (!list->ll_symbol);
8720   list->ll_symbol = gen_internal_sym ("LLST");
8721 }
8722
8723 /* Output the location list given to us.  */
8724
8725 static void
8726 output_loc_list (dw_loc_list_ref list_head)
8727 {
8728   dw_loc_list_ref curr = list_head;
8729
8730   if (list_head->emitted)
8731     return;
8732   list_head->emitted = true;
8733
8734   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8735
8736   /* Walk the location list, and output each range + expression.  */
8737   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8738     {
8739       unsigned long size;
8740       /* Don't output an entry that starts and ends at the same address.  */
8741       if (strcmp (curr->begin, curr->end) == 0 && !curr->force)
8742         continue;
8743       if (!have_multiple_function_sections)
8744         {
8745           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8746                                 "Location list begin address (%s)",
8747                                 list_head->ll_symbol);
8748           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8749                                 "Location list end address (%s)",
8750                                 list_head->ll_symbol);
8751         }
8752       else
8753         {
8754           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8755                                "Location list begin address (%s)",
8756                                list_head->ll_symbol);
8757           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8758                                "Location list end address (%s)",
8759                                list_head->ll_symbol);
8760         }
8761       size = size_of_locs (curr->expr);
8762
8763       /* Output the block length for this list of location operations.  */
8764       gcc_assert (size <= 0xffff);
8765       dw2_asm_output_data (2, size, "%s", "Location expression size");
8766
8767       output_loc_sequence (curr->expr, -1);
8768     }
8769
8770   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8771                        "Location list terminator begin (%s)",
8772                        list_head->ll_symbol);
8773   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8774                        "Location list terminator end (%s)",
8775                        list_head->ll_symbol);
8776 }
8777
8778 /* Output a type signature.  */
8779
8780 static inline void
8781 output_signature (const char *sig, const char *name)
8782 {
8783   int i;
8784
8785   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8786     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
8787 }
8788
8789 /* Output the DIE and its attributes.  Called recursively to generate
8790    the definitions of each child DIE.  */
8791
8792 static void
8793 output_die (dw_die_ref die)
8794 {
8795   dw_attr_ref a;
8796   dw_die_ref c;
8797   unsigned long size;
8798   unsigned ix;
8799
8800   /* If someone in another CU might refer to us, set up a symbol for
8801      them to point to.  */
8802   if (! use_debug_types && die->die_id.die_symbol)
8803     output_die_symbol (die);
8804
8805   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
8806                                (unsigned long)die->die_offset,
8807                                dwarf_tag_name (die->die_tag));
8808
8809   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
8810     {
8811       const char *name = dwarf_attr_name (a->dw_attr);
8812
8813       switch (AT_class (a))
8814         {
8815         case dw_val_class_addr:
8816           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8817           break;
8818
8819         case dw_val_class_offset:
8820           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8821                                "%s", name);
8822           break;
8823
8824         case dw_val_class_range_list:
8825           {
8826             char *p = strchr (ranges_section_label, '\0');
8827
8828             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8829                      a->dw_attr_val.v.val_offset);
8830             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8831                                    debug_ranges_section, "%s", name);
8832             *p = '\0';
8833           }
8834           break;
8835
8836         case dw_val_class_loc:
8837           size = size_of_locs (AT_loc (a));
8838
8839           /* Output the block length for this list of location operations.  */
8840           if (dwarf_version >= 4)
8841             dw2_asm_output_data_uleb128 (size, "%s", name);
8842           else
8843             dw2_asm_output_data (constant_size (size), size, "%s", name);
8844
8845           output_loc_sequence (AT_loc (a), -1);
8846           break;
8847
8848         case dw_val_class_const:
8849           /* ??? It would be slightly more efficient to use a scheme like is
8850              used for unsigned constants below, but gdb 4.x does not sign
8851              extend.  Gdb 5.x does sign extend.  */
8852           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8853           break;
8854
8855         case dw_val_class_unsigned_const:
8856           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8857                                AT_unsigned (a), "%s", name);
8858           break;
8859
8860         case dw_val_class_const_double:
8861           {
8862             unsigned HOST_WIDE_INT first, second;
8863
8864             if (HOST_BITS_PER_WIDE_INT >= 64)
8865               dw2_asm_output_data (1,
8866                                    2 * HOST_BITS_PER_WIDE_INT
8867                                    / HOST_BITS_PER_CHAR,
8868                                    NULL);
8869
8870             if (WORDS_BIG_ENDIAN)
8871               {
8872                 first = a->dw_attr_val.v.val_double.high;
8873                 second = a->dw_attr_val.v.val_double.low;
8874               }
8875             else
8876               {
8877                 first = a->dw_attr_val.v.val_double.low;
8878                 second = a->dw_attr_val.v.val_double.high;
8879               }
8880
8881             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8882                                  first, name);
8883             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8884                                  second, NULL);
8885           }
8886           break;
8887
8888         case dw_val_class_vec:
8889           {
8890             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8891             unsigned int len = a->dw_attr_val.v.val_vec.length;
8892             unsigned int i;
8893             unsigned char *p;
8894
8895             dw2_asm_output_data (constant_size (len * elt_size),
8896                                  len * elt_size, "%s", name);
8897             if (elt_size > sizeof (HOST_WIDE_INT))
8898               {
8899                 elt_size /= 2;
8900                 len *= 2;
8901               }
8902             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8903                  i < len;
8904                  i++, p += elt_size)
8905               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8906                                    "fp or vector constant word %u", i);
8907             break;
8908           }
8909
8910         case dw_val_class_flag:
8911           if (dwarf_version >= 4)
8912             {
8913               /* Currently all add_AT_flag calls pass in 1 as last argument,
8914                  so DW_FORM_flag_present can be used.  If that ever changes,
8915                  we'll need to use DW_FORM_flag and have some optimization
8916                  in build_abbrev_table that will change those to
8917                  DW_FORM_flag_present if it is set to 1 in all DIEs using
8918                  the same abbrev entry.  */
8919               gcc_assert (AT_flag (a) == 1);
8920               if (flag_debug_asm)
8921                 fprintf (asm_out_file, "\t\t\t%s %s\n",
8922                          ASM_COMMENT_START, name);
8923               break;
8924             }
8925           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8926           break;
8927
8928         case dw_val_class_loc_list:
8929           {
8930             char *sym = AT_loc_list (a)->ll_symbol;
8931
8932             gcc_assert (sym);
8933             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8934                                    "%s", name);
8935           }
8936           break;
8937
8938         case dw_val_class_die_ref:
8939           if (AT_ref_external (a))
8940             {
8941               if (use_debug_types)
8942                 {
8943                   comdat_type_node_ref type_node =
8944                     AT_ref (a)->die_id.die_type_node;
8945
8946                   gcc_assert (type_node);
8947                   output_signature (type_node->signature, name);
8948                 }
8949               else
8950                 {
8951                   char *sym = AT_ref (a)->die_id.die_symbol;
8952                   int size;
8953
8954                   gcc_assert (sym);
8955                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
8956                      length, whereas in DWARF3 it's always sized as an
8957                      offset.  */
8958                   if (dwarf_version == 2)
8959                     size = DWARF2_ADDR_SIZE;
8960                   else
8961                     size = DWARF_OFFSET_SIZE;
8962                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
8963                                          name);
8964                 }
8965             }
8966           else
8967             {
8968               gcc_assert (AT_ref (a)->die_offset);
8969               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
8970                                    "%s", name);
8971             }
8972           break;
8973
8974         case dw_val_class_fde_ref:
8975           {
8976             char l1[20];
8977
8978             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
8979                                          a->dw_attr_val.v.val_fde_index * 2);
8980             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
8981                                    "%s", name);
8982           }
8983           break;
8984
8985         case dw_val_class_vms_delta:
8986           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
8987                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
8988                                     "%s", name);
8989           break;
8990
8991         case dw_val_class_lbl_id:
8992           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
8993           break;
8994
8995         case dw_val_class_lineptr:
8996           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
8997                                  debug_line_section, "%s", name);
8998           break;
8999
9000         case dw_val_class_macptr:
9001           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9002                                  debug_macinfo_section, "%s", name);
9003           break;
9004
9005         case dw_val_class_str:
9006           if (AT_string_form (a) == DW_FORM_strp)
9007             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9008                                    a->dw_attr_val.v.val_str->label,
9009                                    debug_str_section,
9010                                    "%s: \"%s\"", name, AT_string (a));
9011           else
9012             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9013           break;
9014
9015         case dw_val_class_file:
9016           {
9017             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9018
9019             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9020                                  a->dw_attr_val.v.val_file->filename);
9021             break;
9022           }
9023
9024         case dw_val_class_data8:
9025           {
9026             int i;
9027
9028             for (i = 0; i < 8; i++)
9029               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
9030                                    i == 0 ? "%s" : NULL, name);
9031             break;
9032           }
9033
9034         default:
9035           gcc_unreachable ();
9036         }
9037     }
9038
9039   FOR_EACH_CHILD (die, c, output_die (c));
9040
9041   /* Add null byte to terminate sibling list.  */
9042   if (die->die_child != NULL)
9043     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
9044                          (unsigned long) die->die_offset);
9045 }
9046
9047 /* Output the compilation unit that appears at the beginning of the
9048    .debug_info section, and precedes the DIE descriptions.  */
9049
9050 static void
9051 output_compilation_unit_header (void)
9052 {
9053   int ver = dwarf_version;
9054
9055   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9056     dw2_asm_output_data (4, 0xffffffff,
9057       "Initial length escape value indicating 64-bit DWARF extension");
9058   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9059                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9060                        "Length of Compilation Unit Info");
9061   dw2_asm_output_data (2, ver, "DWARF version number");
9062   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9063                          debug_abbrev_section,
9064                          "Offset Into Abbrev. Section");
9065   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9066 }
9067
9068 /* Output the compilation unit DIE and its children.  */
9069
9070 static void
9071 output_comp_unit (dw_die_ref die, int output_if_empty)
9072 {
9073   const char *secname;
9074   char *oldsym, *tmp;
9075
9076   /* Unless we are outputting main CU, we may throw away empty ones.  */
9077   if (!output_if_empty && die->die_child == NULL)
9078     return;
9079
9080   /* Even if there are no children of this DIE, we must output the information
9081      about the compilation unit.  Otherwise, on an empty translation unit, we
9082      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9083      will then complain when examining the file.  First mark all the DIEs in
9084      this CU so we know which get local refs.  */
9085   mark_dies (die);
9086
9087   build_abbrev_table (die);
9088
9089   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9090   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9091   calc_die_sizes (die);
9092
9093   oldsym = die->die_id.die_symbol;
9094   if (oldsym)
9095     {
9096       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9097
9098       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9099       secname = tmp;
9100       die->die_id.die_symbol = NULL;
9101       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9102     }
9103   else
9104     {
9105       switch_to_section (debug_info_section);
9106       ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
9107       info_section_emitted = true;
9108     }
9109
9110   /* Output debugging information.  */
9111   output_compilation_unit_header ();
9112   output_die (die);
9113
9114   /* Leave the marks on the main CU, so we can check them in
9115      output_pubnames.  */
9116   if (oldsym)
9117     {
9118       unmark_dies (die);
9119       die->die_id.die_symbol = oldsym;
9120     }
9121 }
9122
9123 /* Output a comdat type unit DIE and its children.  */
9124
9125 static void
9126 output_comdat_type_unit (comdat_type_node *node)
9127 {
9128   const char *secname;
9129   char *tmp;
9130   int i;
9131 #if defined (OBJECT_FORMAT_ELF)
9132   tree comdat_key;
9133 #endif
9134
9135   /* First mark all the DIEs in this CU so we know which get local refs.  */
9136   mark_dies (node->root_die);
9137
9138   build_abbrev_table (node->root_die);
9139
9140   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9141   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
9142   calc_die_sizes (node->root_die);
9143
9144 #if defined (OBJECT_FORMAT_ELF)
9145   secname = ".debug_types";
9146   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9147   sprintf (tmp, "wt.");
9148   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9149     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
9150   comdat_key = get_identifier (tmp);
9151   targetm.asm_out.named_section (secname,
9152                                  SECTION_DEBUG | SECTION_LINKONCE,
9153                                  comdat_key);
9154 #else
9155   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
9156   sprintf (tmp, ".gnu.linkonce.wt.");
9157   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
9158     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
9159   secname = tmp;
9160   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9161 #endif
9162
9163   /* Output debugging information.  */
9164   output_compilation_unit_header ();
9165   output_signature (node->signature, "Type Signature");
9166   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
9167                        "Offset to Type DIE");
9168   output_die (node->root_die);
9169
9170   unmark_dies (node->root_die);
9171 }
9172
9173 /* Return the DWARF2/3 pubname associated with a decl.  */
9174
9175 static const char *
9176 dwarf2_name (tree decl, int scope)
9177 {
9178   if (DECL_NAMELESS (decl))
9179     return NULL;
9180   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9181 }
9182
9183 /* Add a new entry to .debug_pubnames if appropriate.  */
9184
9185 static void
9186 add_pubname_string (const char *str, dw_die_ref die)
9187 {
9188   if (targetm.want_debug_pub_sections)
9189     {
9190       pubname_entry e;
9191
9192       e.die = die;
9193       e.name = xstrdup (str);
9194       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9195     }
9196 }
9197
9198 static void
9199 add_pubname (tree decl, dw_die_ref die)
9200 {
9201   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
9202     {
9203       const char *name = dwarf2_name (decl, 1);
9204       if (name)
9205         add_pubname_string (name, die);
9206     }
9207 }
9208
9209 /* Add a new entry to .debug_pubtypes if appropriate.  */
9210
9211 static void
9212 add_pubtype (tree decl, dw_die_ref die)
9213 {
9214   pubname_entry e;
9215
9216   if (!targetm.want_debug_pub_sections)
9217     return;
9218
9219   e.name = NULL;
9220   if ((TREE_PUBLIC (decl)
9221        || is_cu_die (die->die_parent))
9222       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9223     {
9224       e.die = die;
9225       if (TYPE_P (decl))
9226         {
9227           if (TYPE_NAME (decl))
9228             {
9229               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9230                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9231               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9232                        && DECL_NAME (TYPE_NAME (decl)))
9233                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9234               else
9235                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9236             }
9237         }
9238       else
9239         {
9240           e.name = dwarf2_name (decl, 1);
9241           if (e.name)
9242             e.name = xstrdup (e.name);
9243         }
9244
9245       /* If we don't have a name for the type, there's no point in adding
9246          it to the table.  */
9247       if (e.name && e.name[0] != '\0')
9248         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9249     }
9250 }
9251
9252 /* Output the public names table used to speed up access to externally
9253    visible names; or the public types table used to find type definitions.  */
9254
9255 static void
9256 output_pubnames (VEC (pubname_entry, gc) * names)
9257 {
9258   unsigned i;
9259   unsigned long pubnames_length = size_of_pubnames (names);
9260   pubname_ref pub;
9261
9262   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9263     dw2_asm_output_data (4, 0xffffffff,
9264       "Initial length escape value indicating 64-bit DWARF extension");
9265   if (names == pubname_table)
9266     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9267                          "Length of Public Names Info");
9268   else
9269     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9270                          "Length of Public Type Names Info");
9271   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9272   dw2_asm_output_data (2, 2, "DWARF Version");
9273   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9274                          debug_info_section,
9275                          "Offset of Compilation Unit Info");
9276   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9277                        "Compilation Unit Length");
9278
9279   FOR_EACH_VEC_ELT (pubname_entry, names, i, pub)
9280     {
9281       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9282       if (names == pubname_table)
9283         gcc_assert (pub->die->die_mark);
9284
9285       if (names != pubtype_table
9286           || pub->die->die_offset != 0
9287           || !flag_eliminate_unused_debug_types)
9288         {
9289           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9290                                "DIE offset");
9291
9292           dw2_asm_output_nstring (pub->name, -1, "external name");
9293         }
9294     }
9295
9296   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9297 }
9298
9299 /* Output the information that goes into the .debug_aranges table.
9300    Namely, define the beginning and ending address range of the
9301    text section generated for this compilation unit.  */
9302
9303 static void
9304 output_aranges (unsigned long aranges_length)
9305 {
9306   unsigned i;
9307
9308   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9309     dw2_asm_output_data (4, 0xffffffff,
9310       "Initial length escape value indicating 64-bit DWARF extension");
9311   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9312                        "Length of Address Ranges Info");
9313   /* Version number for aranges is still 2, even in DWARF3.  */
9314   dw2_asm_output_data (2, 2, "DWARF Version");
9315   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9316                          debug_info_section,
9317                          "Offset of Compilation Unit Info");
9318   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9319   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9320
9321   /* We need to align to twice the pointer size here.  */
9322   if (DWARF_ARANGES_PAD_SIZE)
9323     {
9324       /* Pad using a 2 byte words so that padding is correct for any
9325          pointer size.  */
9326       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9327                            2 * DWARF2_ADDR_SIZE);
9328       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9329         dw2_asm_output_data (2, 0, NULL);
9330     }
9331
9332   /* It is necessary not to output these entries if the sections were
9333      not used; if the sections were not used, the length will be 0 and
9334      the address may end up as 0 if the section is discarded by ld
9335      --gc-sections, leaving an invalid (0, 0) entry that can be
9336      confused with the terminator.  */
9337   if (text_section_used)
9338     {
9339       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9340       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9341                             text_section_label, "Length");
9342     }
9343   if (cold_text_section_used)
9344     {
9345       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9346                            "Address");
9347       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9348                             cold_text_section_label, "Length");
9349     }
9350
9351   if (have_multiple_function_sections)
9352     {
9353       unsigned fde_idx = 0;
9354
9355       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
9356         {
9357           dw_fde_ref fde = &fde_table[fde_idx];
9358
9359           if (!fde->in_std_section)
9360             {
9361               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
9362                                    "Address");
9363               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_end,
9364                                     fde->dw_fde_begin, "Length");
9365             }
9366           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
9367             {
9368               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_second_begin,
9369                                    "Address");
9370               dw2_asm_output_delta (DWARF2_ADDR_SIZE, fde->dw_fde_second_end,
9371                                     fde->dw_fde_second_begin, "Length");
9372             }
9373         }
9374     }
9375
9376   /* Output the terminator words.  */
9377   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9378   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9379 }
9380
9381 /* Add a new entry to .debug_ranges.  Return the offset at which it
9382    was placed.  */
9383
9384 static unsigned int
9385 add_ranges_num (int num)
9386 {
9387   unsigned int in_use = ranges_table_in_use;
9388
9389   if (in_use == ranges_table_allocated)
9390     {
9391       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9392       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9393                                     ranges_table_allocated);
9394       memset (ranges_table + ranges_table_in_use, 0,
9395               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9396     }
9397
9398   ranges_table[in_use].num = num;
9399   ranges_table_in_use = in_use + 1;
9400
9401   return in_use * 2 * DWARF2_ADDR_SIZE;
9402 }
9403
9404 /* Add a new entry to .debug_ranges corresponding to a block, or a
9405    range terminator if BLOCK is NULL.  */
9406
9407 static unsigned int
9408 add_ranges (const_tree block)
9409 {
9410   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9411 }
9412
9413 /* Add a new entry to .debug_ranges corresponding to a pair of
9414    labels.  */
9415
9416 static void
9417 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
9418                       bool *added)
9419 {
9420   unsigned int in_use = ranges_by_label_in_use;
9421   unsigned int offset;
9422
9423   if (in_use == ranges_by_label_allocated)
9424     {
9425       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9426       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9427                                        ranges_by_label,
9428                                        ranges_by_label_allocated);
9429       memset (ranges_by_label + ranges_by_label_in_use, 0,
9430               RANGES_TABLE_INCREMENT
9431               * sizeof (struct dw_ranges_by_label_struct));
9432     }
9433
9434   ranges_by_label[in_use].begin = begin;
9435   ranges_by_label[in_use].end = end;
9436   ranges_by_label_in_use = in_use + 1;
9437
9438   offset = add_ranges_num (-(int)in_use - 1);
9439   if (!*added)
9440     {
9441       add_AT_range_list (die, DW_AT_ranges, offset);
9442       *added = true;
9443     }
9444 }
9445
9446 static void
9447 output_ranges (void)
9448 {
9449   unsigned i;
9450   static const char *const start_fmt = "Offset %#x";
9451   const char *fmt = start_fmt;
9452
9453   for (i = 0; i < ranges_table_in_use; i++)
9454     {
9455       int block_num = ranges_table[i].num;
9456
9457       if (block_num > 0)
9458         {
9459           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9460           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9461
9462           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9463           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9464
9465           /* If all code is in the text section, then the compilation
9466              unit base address defaults to DW_AT_low_pc, which is the
9467              base of the text section.  */
9468           if (!have_multiple_function_sections)
9469             {
9470               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9471                                     text_section_label,
9472                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9473               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9474                                     text_section_label, NULL);
9475             }
9476
9477           /* Otherwise, the compilation unit base address is zero,
9478              which allows us to use absolute addresses, and not worry
9479              about whether the target supports cross-section
9480              arithmetic.  */
9481           else
9482             {
9483               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9484                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9485               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9486             }
9487
9488           fmt = NULL;
9489         }
9490
9491       /* Negative block_num stands for an index into ranges_by_label.  */
9492       else if (block_num < 0)
9493         {
9494           int lab_idx = - block_num - 1;
9495
9496           if (!have_multiple_function_sections)
9497             {
9498               gcc_unreachable ();
9499 #if 0
9500               /* If we ever use add_ranges_by_labels () for a single
9501                  function section, all we have to do is to take out
9502                  the #if 0 above.  */
9503               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9504                                     ranges_by_label[lab_idx].begin,
9505                                     text_section_label,
9506                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9507               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9508                                     ranges_by_label[lab_idx].end,
9509                                     text_section_label, NULL);
9510 #endif
9511             }
9512           else
9513             {
9514               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9515                                    ranges_by_label[lab_idx].begin,
9516                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9517               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9518                                    ranges_by_label[lab_idx].end,
9519                                    NULL);
9520             }
9521         }
9522       else
9523         {
9524           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9525           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9526           fmt = start_fmt;
9527         }
9528     }
9529 }
9530
9531 /* Data structure containing information about input files.  */
9532 struct file_info
9533 {
9534   const char *path;     /* Complete file name.  */
9535   const char *fname;    /* File name part.  */
9536   int length;           /* Length of entire string.  */
9537   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9538   int dir_idx;          /* Index in directory table.  */
9539 };
9540
9541 /* Data structure containing information about directories with source
9542    files.  */
9543 struct dir_info
9544 {
9545   const char *path;     /* Path including directory name.  */
9546   int length;           /* Path length.  */
9547   int prefix;           /* Index of directory entry which is a prefix.  */
9548   int count;            /* Number of files in this directory.  */
9549   int dir_idx;          /* Index of directory used as base.  */
9550 };
9551
9552 /* Callback function for file_info comparison.  We sort by looking at
9553    the directories in the path.  */
9554
9555 static int
9556 file_info_cmp (const void *p1, const void *p2)
9557 {
9558   const struct file_info *const s1 = (const struct file_info *) p1;
9559   const struct file_info *const s2 = (const struct file_info *) p2;
9560   const unsigned char *cp1;
9561   const unsigned char *cp2;
9562
9563   /* Take care of file names without directories.  We need to make sure that
9564      we return consistent values to qsort since some will get confused if
9565      we return the same value when identical operands are passed in opposite
9566      orders.  So if neither has a directory, return 0 and otherwise return
9567      1 or -1 depending on which one has the directory.  */
9568   if ((s1->path == s1->fname || s2->path == s2->fname))
9569     return (s2->path == s2->fname) - (s1->path == s1->fname);
9570
9571   cp1 = (const unsigned char *) s1->path;
9572   cp2 = (const unsigned char *) s2->path;
9573
9574   while (1)
9575     {
9576       ++cp1;
9577       ++cp2;
9578       /* Reached the end of the first path?  If so, handle like above.  */
9579       if ((cp1 == (const unsigned char *) s1->fname)
9580           || (cp2 == (const unsigned char *) s2->fname))
9581         return ((cp2 == (const unsigned char *) s2->fname)
9582                 - (cp1 == (const unsigned char *) s1->fname));
9583
9584       /* Character of current path component the same?  */
9585       else if (*cp1 != *cp2)
9586         return *cp1 - *cp2;
9587     }
9588 }
9589
9590 struct file_name_acquire_data
9591 {
9592   struct file_info *files;
9593   int used_files;
9594   int max_files;
9595 };
9596
9597 /* Traversal function for the hash table.  */
9598
9599 static int
9600 file_name_acquire (void ** slot, void *data)
9601 {
9602   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9603   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9604   struct file_info *fi;
9605   const char *f;
9606
9607   gcc_assert (fnad->max_files >= d->emitted_number);
9608
9609   if (! d->emitted_number)
9610     return 1;
9611
9612   gcc_assert (fnad->max_files != fnad->used_files);
9613
9614   fi = fnad->files + fnad->used_files++;
9615
9616   /* Skip all leading "./".  */
9617   f = d->filename;
9618   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9619     f += 2;
9620
9621   /* Create a new array entry.  */
9622   fi->path = f;
9623   fi->length = strlen (f);
9624   fi->file_idx = d;
9625
9626   /* Search for the file name part.  */
9627   f = strrchr (f, DIR_SEPARATOR);
9628 #if defined (DIR_SEPARATOR_2)
9629   {
9630     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9631
9632     if (g != NULL)
9633       {
9634         if (f == NULL || f < g)
9635           f = g;
9636       }
9637   }
9638 #endif
9639
9640   fi->fname = f == NULL ? fi->path : f + 1;
9641   return 1;
9642 }
9643
9644 /* Output the directory table and the file name table.  We try to minimize
9645    the total amount of memory needed.  A heuristic is used to avoid large
9646    slowdowns with many input files.  */
9647
9648 static void
9649 output_file_names (void)
9650 {
9651   struct file_name_acquire_data fnad;
9652   int numfiles;
9653   struct file_info *files;
9654   struct dir_info *dirs;
9655   int *saved;
9656   int *savehere;
9657   int *backmap;
9658   int ndirs;
9659   int idx_offset;
9660   int i;
9661
9662   if (!last_emitted_file)
9663     {
9664       dw2_asm_output_data (1, 0, "End directory table");
9665       dw2_asm_output_data (1, 0, "End file name table");
9666       return;
9667     }
9668
9669   numfiles = last_emitted_file->emitted_number;
9670
9671   /* Allocate the various arrays we need.  */
9672   files = XALLOCAVEC (struct file_info, numfiles);
9673   dirs = XALLOCAVEC (struct dir_info, numfiles);
9674
9675   fnad.files = files;
9676   fnad.used_files = 0;
9677   fnad.max_files = numfiles;
9678   htab_traverse (file_table, file_name_acquire, &fnad);
9679   gcc_assert (fnad.used_files == fnad.max_files);
9680
9681   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9682
9683   /* Find all the different directories used.  */
9684   dirs[0].path = files[0].path;
9685   dirs[0].length = files[0].fname - files[0].path;
9686   dirs[0].prefix = -1;
9687   dirs[0].count = 1;
9688   dirs[0].dir_idx = 0;
9689   files[0].dir_idx = 0;
9690   ndirs = 1;
9691
9692   for (i = 1; i < numfiles; i++)
9693     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9694         && memcmp (dirs[ndirs - 1].path, files[i].path,
9695                    dirs[ndirs - 1].length) == 0)
9696       {
9697         /* Same directory as last entry.  */
9698         files[i].dir_idx = ndirs - 1;
9699         ++dirs[ndirs - 1].count;
9700       }
9701     else
9702       {
9703         int j;
9704
9705         /* This is a new directory.  */
9706         dirs[ndirs].path = files[i].path;
9707         dirs[ndirs].length = files[i].fname - files[i].path;
9708         dirs[ndirs].count = 1;
9709         dirs[ndirs].dir_idx = ndirs;
9710         files[i].dir_idx = ndirs;
9711
9712         /* Search for a prefix.  */
9713         dirs[ndirs].prefix = -1;
9714         for (j = 0; j < ndirs; j++)
9715           if (dirs[j].length < dirs[ndirs].length
9716               && dirs[j].length > 1
9717               && (dirs[ndirs].prefix == -1
9718                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9719               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9720             dirs[ndirs].prefix = j;
9721
9722         ++ndirs;
9723       }
9724
9725   /* Now to the actual work.  We have to find a subset of the directories which
9726      allow expressing the file name using references to the directory table
9727      with the least amount of characters.  We do not do an exhaustive search
9728      where we would have to check out every combination of every single
9729      possible prefix.  Instead we use a heuristic which provides nearly optimal
9730      results in most cases and never is much off.  */
9731   saved = XALLOCAVEC (int, ndirs);
9732   savehere = XALLOCAVEC (int, ndirs);
9733
9734   memset (saved, '\0', ndirs * sizeof (saved[0]));
9735   for (i = 0; i < ndirs; i++)
9736     {
9737       int j;
9738       int total;
9739
9740       /* We can always save some space for the current directory.  But this
9741          does not mean it will be enough to justify adding the directory.  */
9742       savehere[i] = dirs[i].length;
9743       total = (savehere[i] - saved[i]) * dirs[i].count;
9744
9745       for (j = i + 1; j < ndirs; j++)
9746         {
9747           savehere[j] = 0;
9748           if (saved[j] < dirs[i].length)
9749             {
9750               /* Determine whether the dirs[i] path is a prefix of the
9751                  dirs[j] path.  */
9752               int k;
9753
9754               k = dirs[j].prefix;
9755               while (k != -1 && k != (int) i)
9756                 k = dirs[k].prefix;
9757
9758               if (k == (int) i)
9759                 {
9760                   /* Yes it is.  We can possibly save some memory by
9761                      writing the filenames in dirs[j] relative to
9762                      dirs[i].  */
9763                   savehere[j] = dirs[i].length;
9764                   total += (savehere[j] - saved[j]) * dirs[j].count;
9765                 }
9766             }
9767         }
9768
9769       /* Check whether we can save enough to justify adding the dirs[i]
9770          directory.  */
9771       if (total > dirs[i].length + 1)
9772         {
9773           /* It's worthwhile adding.  */
9774           for (j = i; j < ndirs; j++)
9775             if (savehere[j] > 0)
9776               {
9777                 /* Remember how much we saved for this directory so far.  */
9778                 saved[j] = savehere[j];
9779
9780                 /* Remember the prefix directory.  */
9781                 dirs[j].dir_idx = i;
9782               }
9783         }
9784     }
9785
9786   /* Emit the directory name table.  */
9787   idx_offset = dirs[0].length > 0 ? 1 : 0;
9788   for (i = 1 - idx_offset; i < ndirs; i++)
9789     dw2_asm_output_nstring (dirs[i].path,
9790                             dirs[i].length
9791                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9792                             "Directory Entry: %#x", i + idx_offset);
9793
9794   dw2_asm_output_data (1, 0, "End directory table");
9795
9796   /* We have to emit them in the order of emitted_number since that's
9797      used in the debug info generation.  To do this efficiently we
9798      generate a back-mapping of the indices first.  */
9799   backmap = XALLOCAVEC (int, numfiles);
9800   for (i = 0; i < numfiles; i++)
9801     backmap[files[i].file_idx->emitted_number - 1] = i;
9802
9803   /* Now write all the file names.  */
9804   for (i = 0; i < numfiles; i++)
9805     {
9806       int file_idx = backmap[i];
9807       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9808
9809 #ifdef VMS_DEBUGGING_INFO
9810 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9811
9812       /* Setting these fields can lead to debugger miscomparisons,
9813          but VMS Debug requires them to be set correctly.  */
9814
9815       int ver;
9816       long long cdt;
9817       long siz;
9818       int maxfilelen = strlen (files[file_idx].path)
9819                                + dirs[dir_idx].length
9820                                + MAX_VMS_VERSION_LEN + 1;
9821       char *filebuf = XALLOCAVEC (char, maxfilelen);
9822
9823       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9824       snprintf (filebuf, maxfilelen, "%s;%d",
9825                 files[file_idx].path + dirs[dir_idx].length, ver);
9826
9827       dw2_asm_output_nstring
9828         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
9829
9830       /* Include directory index.  */
9831       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9832
9833       /* Modification time.  */
9834       dw2_asm_output_data_uleb128
9835         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9836           ? cdt : 0,
9837          NULL);
9838
9839       /* File length in bytes.  */
9840       dw2_asm_output_data_uleb128
9841         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9842           ? siz : 0,
9843          NULL);
9844 #else
9845       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9846                               "File Entry: %#x", (unsigned) i + 1);
9847
9848       /* Include directory index.  */
9849       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9850
9851       /* Modification time.  */
9852       dw2_asm_output_data_uleb128 (0, NULL);
9853
9854       /* File length in bytes.  */
9855       dw2_asm_output_data_uleb128 (0, NULL);
9856 #endif /* VMS_DEBUGGING_INFO */
9857     }
9858
9859   dw2_asm_output_data (1, 0, "End file name table");
9860 }
9861
9862
9863 /* Output one line number table into the .debug_line section.  */
9864
9865 static void
9866 output_one_line_info_table (dw_line_info_table *table)
9867 {
9868   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9869   unsigned int current_line = 1;
9870   bool current_is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
9871   dw_line_info_entry *ent;
9872   size_t i;
9873
9874   FOR_EACH_VEC_ELT (dw_line_info_entry, table->entries, i, ent)
9875     {
9876       switch (ent->opcode)
9877         {
9878         case LI_set_address:
9879           /* ??? Unfortunately, we have little choice here currently, and
9880              must always use the most general form.  GCC does not know the
9881              address delta itself, so we can't use DW_LNS_advance_pc.  Many
9882              ports do have length attributes which will give an upper bound
9883              on the address range.  We could perhaps use length attributes
9884              to determine when it is safe to use DW_LNS_fixed_advance_pc.  */
9885           ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, ent->val);
9886
9887           /* This can handle any delta.  This takes
9888              4+DWARF2_ADDR_SIZE bytes.  */
9889           dw2_asm_output_data (1, 0, "set address %s", line_label);
9890           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9891           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9892           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9893           break;
9894
9895         case LI_set_line:
9896           if (ent->val == current_line)
9897             {
9898               /* We still need to start a new row, so output a copy insn.  */
9899               dw2_asm_output_data (1, DW_LNS_copy,
9900                                    "copy line %u", current_line);
9901             }
9902           else
9903             {
9904               int line_offset = ent->val - current_line;
9905               int line_delta = line_offset - DWARF_LINE_BASE;
9906
9907               current_line = ent->val;
9908               if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
9909                 {
9910                   /* This can handle deltas from -10 to 234, using the current
9911                      definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.
9912                      This takes 1 byte.  */
9913                   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
9914                                        "line %u", current_line);
9915                 }
9916               else
9917                 {
9918                   /* This can handle any delta.  This takes at least 4 bytes,
9919                      depending on the value being encoded.  */
9920                   dw2_asm_output_data (1, DW_LNS_advance_line,
9921                                        "advance to line %u", current_line);
9922                   dw2_asm_output_data_sleb128 (line_offset, NULL);
9923                   dw2_asm_output_data (1, DW_LNS_copy, NULL);
9924                 }
9925             }
9926           break;
9927
9928         case LI_set_file:
9929           dw2_asm_output_data (1, DW_LNS_set_file, "set file %u", ent->val);
9930           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9931           break;
9932
9933         case LI_set_column:
9934           dw2_asm_output_data (1, DW_LNS_set_column, "column %u", ent->val);
9935           dw2_asm_output_data_uleb128 (ent->val, "%u", ent->val);
9936           break;
9937
9938         case LI_negate_stmt:
9939           current_is_stmt = !current_is_stmt;
9940           dw2_asm_output_data (1, DW_LNS_negate_stmt,
9941                                "is_stmt %d", current_is_stmt);
9942           break;
9943
9944         case LI_set_prologue_end:
9945           dw2_asm_output_data (1, DW_LNS_set_prologue_end,
9946                                "set prologue end");
9947           break;
9948           
9949         case LI_set_epilogue_begin:
9950           dw2_asm_output_data (1, DW_LNS_set_epilogue_begin,
9951                                "set epilogue begin");
9952           break;
9953
9954         case LI_set_discriminator:
9955           dw2_asm_output_data (1, 0, "discriminator %u", ent->val);
9956           dw2_asm_output_data_uleb128 (1 + size_of_uleb128 (ent->val), NULL);
9957           dw2_asm_output_data (1, DW_LNE_set_discriminator, NULL);
9958           dw2_asm_output_data_uleb128 (ent->val, NULL);
9959           break;
9960         }
9961     }
9962
9963   /* Emit debug info for the address of the end of the table.  */
9964   dw2_asm_output_data (1, 0, "set address %s", table->end_label);
9965   dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9966   dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9967   dw2_asm_output_addr (DWARF2_ADDR_SIZE, table->end_label, NULL);
9968
9969   dw2_asm_output_data (1, 0, "end sequence");
9970   dw2_asm_output_data_uleb128 (1, NULL);
9971   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
9972 }
9973
9974 /* Output the source line number correspondence information.  This
9975    information goes into the .debug_line section.  */
9976
9977 static void
9978 output_line_info (void)
9979 {
9980   char l1[20], l2[20], p1[20], p2[20];
9981   int ver = dwarf_version;
9982   bool saw_one = false;
9983   int opc;
9984
9985   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9986   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9987   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9988   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9989
9990   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9991     dw2_asm_output_data (4, 0xffffffff,
9992       "Initial length escape value indicating 64-bit DWARF extension");
9993   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9994                         "Length of Source Line Info");
9995   ASM_OUTPUT_LABEL (asm_out_file, l1);
9996
9997   dw2_asm_output_data (2, ver, "DWARF Version");
9998   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9999   ASM_OUTPUT_LABEL (asm_out_file, p1);
10000
10001   /* Define the architecture-dependent minimum instruction length (in bytes).
10002      In this implementation of DWARF, this field is used for information
10003      purposes only.  Since GCC generates assembly language, we have no
10004      a priori knowledge of how many instruction bytes are generated for each
10005      source line, and therefore can use only the DW_LNE_set_address and
10006      DW_LNS_fixed_advance_pc line information commands.  Accordingly, we fix
10007      this as '1', which is "correct enough" for all architectures,
10008      and don't let the target override.  */
10009   dw2_asm_output_data (1, 1, "Minimum Instruction Length");
10010
10011   if (ver >= 4)
10012     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
10013                          "Maximum Operations Per Instruction");
10014   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
10015                        "Default is_stmt_start flag");
10016   dw2_asm_output_data (1, DWARF_LINE_BASE,
10017                        "Line Base Value (Special Opcodes)");
10018   dw2_asm_output_data (1, DWARF_LINE_RANGE,
10019                        "Line Range Value (Special Opcodes)");
10020   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
10021                        "Special Opcode Base");
10022
10023   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
10024     {
10025       int n_op_args;
10026       switch (opc)
10027         {
10028         case DW_LNS_advance_pc:
10029         case DW_LNS_advance_line:
10030         case DW_LNS_set_file:
10031         case DW_LNS_set_column:
10032         case DW_LNS_fixed_advance_pc:
10033         case DW_LNS_set_isa:
10034           n_op_args = 1;
10035           break;
10036         default:
10037           n_op_args = 0;
10038           break;
10039         }
10040
10041       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
10042                            opc, n_op_args);
10043     }
10044
10045   /* Write out the information about the files we use.  */
10046   output_file_names ();
10047   ASM_OUTPUT_LABEL (asm_out_file, p2);
10048
10049   if (separate_line_info)
10050     {
10051       dw_line_info_table *table;
10052       size_t i;
10053
10054       FOR_EACH_VEC_ELT (dw_line_info_table_p, separate_line_info, i, table)
10055         if (table->in_use)
10056           {
10057             output_one_line_info_table (table);
10058             saw_one = true;
10059           }
10060     }
10061   if (cold_text_section_line_info && cold_text_section_line_info->in_use)
10062     {
10063       output_one_line_info_table (cold_text_section_line_info);
10064       saw_one = true;
10065     }
10066
10067   /* ??? Some Darwin linkers crash on a .debug_line section with no
10068      sequences.  Further, merely a DW_LNE_end_sequence entry is not
10069      sufficient -- the address column must also be initialized.
10070      Make sure to output at least one set_address/end_sequence pair,
10071      choosing .text since that section is always present.  */
10072   if (text_section_line_info->in_use || !saw_one)
10073     output_one_line_info_table (text_section_line_info);
10074
10075   /* Output the marker for the end of the line number info.  */
10076   ASM_OUTPUT_LABEL (asm_out_file, l2);
10077 }
10078 \f
10079 /* Given a pointer to a tree node for some base type, return a pointer to
10080    a DIE that describes the given type.
10081
10082    This routine must only be called for GCC type nodes that correspond to
10083    Dwarf base (fundamental) types.  */
10084
10085 static dw_die_ref
10086 base_type_die (tree type)
10087 {
10088   dw_die_ref base_type_result;
10089   enum dwarf_type encoding;
10090
10091   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10092     return 0;
10093
10094   /* If this is a subtype that should not be emitted as a subrange type,
10095      use the base type.  See subrange_type_for_debug_p.  */
10096   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10097     type = TREE_TYPE (type);
10098
10099   switch (TREE_CODE (type))
10100     {
10101     case INTEGER_TYPE:
10102       if ((dwarf_version >= 4 || !dwarf_strict)
10103           && TYPE_NAME (type)
10104           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
10105           && DECL_IS_BUILTIN (TYPE_NAME (type))
10106           && DECL_NAME (TYPE_NAME (type)))
10107         {
10108           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
10109           if (strcmp (name, "char16_t") == 0
10110               || strcmp (name, "char32_t") == 0)
10111             {
10112               encoding = DW_ATE_UTF;
10113               break;
10114             }
10115         }
10116       if (TYPE_STRING_FLAG (type))
10117         {
10118           if (TYPE_UNSIGNED (type))
10119             encoding = DW_ATE_unsigned_char;
10120           else
10121             encoding = DW_ATE_signed_char;
10122         }
10123       else if (TYPE_UNSIGNED (type))
10124         encoding = DW_ATE_unsigned;
10125       else
10126         encoding = DW_ATE_signed;
10127       break;
10128
10129     case REAL_TYPE:
10130       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10131         {
10132           if (dwarf_version >= 3 || !dwarf_strict)
10133             encoding = DW_ATE_decimal_float;
10134           else
10135             encoding = DW_ATE_lo_user;
10136         }
10137       else
10138         encoding = DW_ATE_float;
10139       break;
10140
10141     case FIXED_POINT_TYPE:
10142       if (!(dwarf_version >= 3 || !dwarf_strict))
10143         encoding = DW_ATE_lo_user;
10144       else if (TYPE_UNSIGNED (type))
10145         encoding = DW_ATE_unsigned_fixed;
10146       else
10147         encoding = DW_ATE_signed_fixed;
10148       break;
10149
10150       /* Dwarf2 doesn't know anything about complex ints, so use
10151          a user defined type for it.  */
10152     case COMPLEX_TYPE:
10153       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10154         encoding = DW_ATE_complex_float;
10155       else
10156         encoding = DW_ATE_lo_user;
10157       break;
10158
10159     case BOOLEAN_TYPE:
10160       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10161       encoding = DW_ATE_boolean;
10162       break;
10163
10164     default:
10165       /* No other TREE_CODEs are Dwarf fundamental types.  */
10166       gcc_unreachable ();
10167     }
10168
10169   base_type_result = new_die (DW_TAG_base_type, comp_unit_die (), type);
10170
10171   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10172                    int_size_in_bytes (type));
10173   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10174
10175   return base_type_result;
10176 }
10177
10178 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10179    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10180
10181 static inline int
10182 is_base_type (tree type)
10183 {
10184   switch (TREE_CODE (type))
10185     {
10186     case ERROR_MARK:
10187     case VOID_TYPE:
10188     case INTEGER_TYPE:
10189     case REAL_TYPE:
10190     case FIXED_POINT_TYPE:
10191     case COMPLEX_TYPE:
10192     case BOOLEAN_TYPE:
10193       return 1;
10194
10195     case ARRAY_TYPE:
10196     case RECORD_TYPE:
10197     case UNION_TYPE:
10198     case QUAL_UNION_TYPE:
10199     case ENUMERAL_TYPE:
10200     case FUNCTION_TYPE:
10201     case METHOD_TYPE:
10202     case POINTER_TYPE:
10203     case REFERENCE_TYPE:
10204     case NULLPTR_TYPE:
10205     case OFFSET_TYPE:
10206     case LANG_TYPE:
10207     case VECTOR_TYPE:
10208       return 0;
10209
10210     default:
10211       gcc_unreachable ();
10212     }
10213
10214   return 0;
10215 }
10216
10217 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10218    node, return the size in bits for the type if it is a constant, or else
10219    return the alignment for the type if the type's size is not constant, or
10220    else return BITS_PER_WORD if the type actually turns out to be an
10221    ERROR_MARK node.  */
10222
10223 static inline unsigned HOST_WIDE_INT
10224 simple_type_size_in_bits (const_tree type)
10225 {
10226   if (TREE_CODE (type) == ERROR_MARK)
10227     return BITS_PER_WORD;
10228   else if (TYPE_SIZE (type) == NULL_TREE)
10229     return 0;
10230   else if (host_integerp (TYPE_SIZE (type), 1))
10231     return tree_low_cst (TYPE_SIZE (type), 1);
10232   else
10233     return TYPE_ALIGN (type);
10234 }
10235
10236 /* Similarly, but return a double_int instead of UHWI.  */
10237
10238 static inline double_int
10239 double_int_type_size_in_bits (const_tree type)
10240 {
10241   if (TREE_CODE (type) == ERROR_MARK)
10242     return uhwi_to_double_int (BITS_PER_WORD);
10243   else if (TYPE_SIZE (type) == NULL_TREE)
10244     return double_int_zero;
10245   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
10246     return tree_to_double_int (TYPE_SIZE (type));
10247   else
10248     return uhwi_to_double_int (TYPE_ALIGN (type));
10249 }
10250
10251 /*  Given a pointer to a tree node for a subrange type, return a pointer
10252     to a DIE that describes the given type.  */
10253
10254 static dw_die_ref
10255 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10256 {
10257   dw_die_ref subrange_die;
10258   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10259
10260   if (context_die == NULL)
10261     context_die = comp_unit_die ();
10262
10263   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10264
10265   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10266     {
10267       /* The size of the subrange type and its base type do not match,
10268          so we need to generate a size attribute for the subrange type.  */
10269       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10270     }
10271
10272   if (low)
10273     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10274   if (high)
10275     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10276
10277   return subrange_die;
10278 }
10279
10280 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10281    entry that chains various modifiers in front of the given type.  */
10282
10283 static dw_die_ref
10284 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10285                    dw_die_ref context_die)
10286 {
10287   enum tree_code code = TREE_CODE (type);
10288   dw_die_ref mod_type_die;
10289   dw_die_ref sub_die = NULL;
10290   tree item_type = NULL;
10291   tree qualified_type;
10292   tree name, low, high;
10293
10294   if (code == ERROR_MARK)
10295     return NULL;
10296
10297   /* See if we already have the appropriately qualified variant of
10298      this type.  */
10299   qualified_type
10300     = get_qualified_type (type,
10301                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10302                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10303
10304   if (qualified_type == sizetype
10305       && TYPE_NAME (qualified_type)
10306       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
10307     {
10308       tree t = TREE_TYPE (TYPE_NAME (qualified_type));
10309
10310       gcc_checking_assert (TREE_CODE (t) == INTEGER_TYPE
10311                            && TYPE_PRECISION (t)
10312                            == TYPE_PRECISION (qualified_type)
10313                            && TYPE_UNSIGNED (t)
10314                            == TYPE_UNSIGNED (qualified_type));
10315       qualified_type = t;
10316     }
10317
10318   /* If we do, then we can just use its DIE, if it exists.  */
10319   if (qualified_type)
10320     {
10321       mod_type_die = lookup_type_die (qualified_type);
10322       if (mod_type_die)
10323         return mod_type_die;
10324     }
10325
10326   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10327
10328   /* Handle C typedef types.  */
10329   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
10330       && !DECL_ARTIFICIAL (name))
10331     {
10332       tree dtype = TREE_TYPE (name);
10333
10334       if (qualified_type == dtype)
10335         {
10336           /* For a named type, use the typedef.  */
10337           gen_type_die (qualified_type, context_die);
10338           return lookup_type_die (qualified_type);
10339         }
10340       else if (is_const_type < TYPE_READONLY (dtype)
10341                || is_volatile_type < TYPE_VOLATILE (dtype)
10342                || (is_const_type <= TYPE_READONLY (dtype)
10343                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10344                    && DECL_ORIGINAL_TYPE (name) != type))
10345         /* cv-unqualified version of named type.  Just use the unnamed
10346            type to which it refers.  */
10347         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10348                                   is_const_type, is_volatile_type,
10349                                   context_die);
10350       /* Else cv-qualified version of named type; fall through.  */
10351     }
10352
10353   if (is_const_type
10354       /* If both is_const_type and is_volatile_type, prefer the path
10355          which leads to a qualified type.  */
10356       && (!is_volatile_type
10357           || get_qualified_type (type, TYPE_QUAL_CONST) == NULL_TREE
10358           || get_qualified_type (type, TYPE_QUAL_VOLATILE) != NULL_TREE))
10359     {
10360       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die (), type);
10361       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10362     }
10363   else if (is_volatile_type)
10364     {
10365       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die (), type);
10366       sub_die = modified_type_die (type, is_const_type, 0, context_die);
10367     }
10368   else if (code == POINTER_TYPE)
10369     {
10370       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die (), type);
10371       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10372                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10373       item_type = TREE_TYPE (type);
10374       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10375         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10376                          TYPE_ADDR_SPACE (item_type));
10377     }
10378   else if (code == REFERENCE_TYPE)
10379     {
10380       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
10381         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die (),
10382                                 type);
10383       else
10384         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die (), type);
10385       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10386                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10387       item_type = TREE_TYPE (type);
10388       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
10389         add_AT_unsigned (mod_type_die, DW_AT_address_class,
10390                          TYPE_ADDR_SPACE (item_type));
10391     }
10392   else if (code == INTEGER_TYPE
10393            && TREE_TYPE (type) != NULL_TREE
10394            && subrange_type_for_debug_p (type, &low, &high))
10395     {
10396       mod_type_die = subrange_type_die (type, low, high, context_die);
10397       item_type = TREE_TYPE (type);
10398     }
10399   else if (is_base_type (type))
10400     mod_type_die = base_type_die (type);
10401   else
10402     {
10403       gen_type_die (type, context_die);
10404
10405       /* We have to get the type_main_variant here (and pass that to the
10406          `lookup_type_die' routine) because the ..._TYPE node we have
10407          might simply be a *copy* of some original type node (where the
10408          copy was created to help us keep track of typedef names) and
10409          that copy might have a different TYPE_UID from the original
10410          ..._TYPE node.  */
10411       if (TREE_CODE (type) != VECTOR_TYPE)
10412         return lookup_type_die (type_main_variant (type));
10413       else
10414         /* Vectors have the debugging information in the type,
10415            not the main variant.  */
10416         return lookup_type_die (type);
10417     }
10418
10419   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10420      don't output a DW_TAG_typedef, since there isn't one in the
10421      user's program; just attach a DW_AT_name to the type.
10422      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
10423      if the base type already has the same name.  */
10424   if (name
10425       && ((TREE_CODE (name) != TYPE_DECL
10426            && (qualified_type == TYPE_MAIN_VARIANT (type)
10427                || (!is_const_type && !is_volatile_type)))
10428           || (TREE_CODE (name) == TYPE_DECL
10429               && TREE_TYPE (name) == qualified_type
10430               && DECL_NAME (name))))
10431     {
10432       if (TREE_CODE (name) == TYPE_DECL)
10433         /* Could just call add_name_and_src_coords_attributes here,
10434            but since this is a builtin type it doesn't have any
10435            useful source coordinates anyway.  */
10436         name = DECL_NAME (name);
10437       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10438       add_gnat_descriptive_type_attribute (mod_type_die, type, context_die);
10439       if (TYPE_ARTIFICIAL (type))
10440         add_AT_flag (mod_type_die, DW_AT_artificial, 1);
10441     }
10442   /* This probably indicates a bug.  */
10443   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
10444     add_name_attribute (mod_type_die, "__unknown__");
10445
10446   if (qualified_type)
10447     equate_type_number_to_die (qualified_type, mod_type_die);
10448
10449   if (item_type)
10450     /* We must do this after the equate_type_number_to_die call, in case
10451        this is a recursive type.  This ensures that the modified_type_die
10452        recursion will terminate even if the type is recursive.  Recursive
10453        types are possible in Ada.  */
10454     sub_die = modified_type_die (item_type,
10455                                  TYPE_READONLY (item_type),
10456                                  TYPE_VOLATILE (item_type),
10457                                  context_die);
10458
10459   if (sub_die != NULL)
10460     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10461
10462   return mod_type_die;
10463 }
10464
10465 /* Generate DIEs for the generic parameters of T.
10466    T must be either a generic type or a generic function.
10467    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10468
10469 static void
10470 gen_generic_params_dies (tree t)
10471 {
10472   tree parms, args;
10473   int parms_num, i;
10474   dw_die_ref die = NULL;
10475
10476   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10477     return;
10478
10479   if (TYPE_P (t))
10480     die = lookup_type_die (t);
10481   else if (DECL_P (t))
10482     die = lookup_decl_die (t);
10483
10484   gcc_assert (die);
10485
10486   parms = lang_hooks.get_innermost_generic_parms (t);
10487   if (!parms)
10488     /* T has no generic parameter. It means T is neither a generic type
10489        or function. End of story.  */
10490     return;
10491
10492   parms_num = TREE_VEC_LENGTH (parms);
10493   args = lang_hooks.get_innermost_generic_args (t);
10494   for (i = 0; i < parms_num; i++)
10495     {
10496       tree parm, arg, arg_pack_elems;
10497
10498       parm = TREE_VEC_ELT (parms, i);
10499       arg = TREE_VEC_ELT (args, i);
10500       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10501       gcc_assert (parm && TREE_VALUE (parm) && arg);
10502
10503       if (parm && TREE_VALUE (parm) && arg)
10504         {
10505           /* If PARM represents a template parameter pack,
10506              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10507              by DW_TAG_template_*_parameter DIEs for the argument
10508              pack elements of ARG. Note that ARG would then be
10509              an argument pack.  */
10510           if (arg_pack_elems)
10511             template_parameter_pack_die (TREE_VALUE (parm),
10512                                          arg_pack_elems,
10513                                          die);
10514           else
10515             generic_parameter_die (TREE_VALUE (parm), arg,
10516                                    true /* Emit DW_AT_name */, die);
10517         }
10518     }
10519 }
10520
10521 /* Create and return a DIE for PARM which should be
10522    the representation of a generic type parameter.
10523    For instance, in the C++ front end, PARM would be a template parameter.
10524    ARG is the argument to PARM.
10525    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10526    name of the PARM.
10527    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10528    as a child node.  */
10529
10530 static dw_die_ref
10531 generic_parameter_die (tree parm, tree arg,
10532                        bool emit_name_p,
10533                        dw_die_ref parent_die)
10534 {
10535   dw_die_ref tmpl_die = NULL;
10536   const char *name = NULL;
10537
10538   if (!parm || !DECL_NAME (parm) || !arg)
10539     return NULL;
10540
10541   /* We support non-type generic parameters and arguments,
10542      type generic parameters and arguments, as well as
10543      generic generic parameters (a.k.a. template template parameters in C++)
10544      and arguments.  */
10545   if (TREE_CODE (parm) == PARM_DECL)
10546     /* PARM is a nontype generic parameter  */
10547     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10548   else if (TREE_CODE (parm) == TYPE_DECL)
10549     /* PARM is a type generic parameter.  */
10550     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10551   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10552     /* PARM is a generic generic parameter.
10553        Its DIE is a GNU extension. It shall have a
10554        DW_AT_name attribute to represent the name of the template template
10555        parameter, and a DW_AT_GNU_template_name attribute to represent the
10556        name of the template template argument.  */
10557     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10558                         parent_die, parm);
10559   else
10560     gcc_unreachable ();
10561
10562   if (tmpl_die)
10563     {
10564       tree tmpl_type;
10565
10566       /* If PARM is a generic parameter pack, it means we are
10567          emitting debug info for a template argument pack element.
10568          In other terms, ARG is a template argument pack element.
10569          In that case, we don't emit any DW_AT_name attribute for
10570          the die.  */
10571       if (emit_name_p)
10572         {
10573           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10574           gcc_assert (name);
10575           add_AT_string (tmpl_die, DW_AT_name, name);
10576         }
10577
10578       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10579         {
10580           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10581              TMPL_DIE should have a child DW_AT_type attribute that is set
10582              to the type of the argument to PARM, which is ARG.
10583              If PARM is a type generic parameter, TMPL_DIE should have a
10584              child DW_AT_type that is set to ARG.  */
10585           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10586           add_type_attribute (tmpl_die, tmpl_type, 0,
10587                               TREE_THIS_VOLATILE (tmpl_type),
10588                               parent_die);
10589         }
10590       else
10591         {
10592           /* So TMPL_DIE is a DIE representing a
10593              a generic generic template parameter, a.k.a template template
10594              parameter in C++ and arg is a template.  */
10595
10596           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10597              to the name of the argument.  */
10598           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10599           if (name)
10600             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10601         }
10602
10603       if (TREE_CODE (parm) == PARM_DECL)
10604         /* So PARM is a non-type generic parameter.
10605            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10606            attribute of TMPL_DIE which value represents the value
10607            of ARG.
10608            We must be careful here:
10609            The value of ARG might reference some function decls.
10610            We might currently be emitting debug info for a generic
10611            type and types are emitted before function decls, we don't
10612            know if the function decls referenced by ARG will actually be
10613            emitted after cgraph computations.
10614            So must defer the generation of the DW_AT_const_value to
10615            after cgraph is ready.  */
10616         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10617     }
10618
10619   return tmpl_die;
10620 }
10621
10622 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10623    PARM_PACK must be a template parameter pack. The returned DIE
10624    will be child DIE of PARENT_DIE.  */
10625
10626 static dw_die_ref
10627 template_parameter_pack_die (tree parm_pack,
10628                              tree parm_pack_args,
10629                              dw_die_ref parent_die)
10630 {
10631   dw_die_ref die;
10632   int j;
10633
10634   gcc_assert (parent_die && parm_pack);
10635
10636   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10637   add_name_and_src_coords_attributes (die, parm_pack);
10638   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10639     generic_parameter_die (parm_pack,
10640                            TREE_VEC_ELT (parm_pack_args, j),
10641                            false /* Don't emit DW_AT_name */,
10642                            die);
10643   return die;
10644 }
10645
10646 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10647    an enumerated type.  */
10648
10649 static inline int
10650 type_is_enum (const_tree type)
10651 {
10652   return TREE_CODE (type) == ENUMERAL_TYPE;
10653 }
10654
10655 /* Return the DBX register number described by a given RTL node.  */
10656
10657 static unsigned int
10658 dbx_reg_number (const_rtx rtl)
10659 {
10660   unsigned regno = REGNO (rtl);
10661
10662   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10663
10664 #ifdef LEAF_REG_REMAP
10665   if (current_function_uses_only_leaf_regs)
10666     {
10667       int leaf_reg = LEAF_REG_REMAP (regno);
10668       if (leaf_reg != -1)
10669         regno = (unsigned) leaf_reg;
10670     }
10671 #endif
10672
10673   return DBX_REGISTER_NUMBER (regno);
10674 }
10675
10676 /* Optionally add a DW_OP_piece term to a location description expression.
10677    DW_OP_piece is only added if the location description expression already
10678    doesn't end with DW_OP_piece.  */
10679
10680 static void
10681 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10682 {
10683   dw_loc_descr_ref loc;
10684
10685   if (*list_head != NULL)
10686     {
10687       /* Find the end of the chain.  */
10688       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10689         ;
10690
10691       if (loc->dw_loc_opc != DW_OP_piece)
10692         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10693     }
10694 }
10695
10696 /* Return a location descriptor that designates a machine register or
10697    zero if there is none.  */
10698
10699 static dw_loc_descr_ref
10700 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10701 {
10702   rtx regs;
10703
10704   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10705     return 0;
10706
10707   /* We only use "frame base" when we're sure we're talking about the
10708      post-prologue local stack frame.  We do this by *not* running
10709      register elimination until this point, and recognizing the special
10710      argument pointer and soft frame pointer rtx's.
10711      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
10712   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
10713       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
10714     {
10715       dw_loc_descr_ref result = NULL;
10716
10717       if (dwarf_version >= 4 || !dwarf_strict)
10718         {
10719           result = mem_loc_descriptor (rtl, GET_MODE (rtl), VOIDmode,
10720                                        initialized);
10721           if (result)
10722             add_loc_descr (&result,
10723                            new_loc_descr (DW_OP_stack_value, 0, 0));
10724         }
10725       return result;
10726     }
10727
10728   regs = targetm.dwarf_register_span (rtl);
10729
10730   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10731     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10732   else
10733     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10734 }
10735
10736 /* Return a location descriptor that designates a machine register for
10737    a given hard register number.  */
10738
10739 static dw_loc_descr_ref
10740 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10741 {
10742   dw_loc_descr_ref reg_loc_descr;
10743
10744   if (regno <= 31)
10745     reg_loc_descr
10746       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10747   else
10748     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10749
10750   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10751     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10752
10753   return reg_loc_descr;
10754 }
10755
10756 /* Given an RTL of a register, return a location descriptor that
10757    designates a value that spans more than one register.  */
10758
10759 static dw_loc_descr_ref
10760 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10761                              enum var_init_status initialized)
10762 {
10763   int nregs, size, i;
10764   unsigned reg;
10765   dw_loc_descr_ref loc_result = NULL;
10766
10767   reg = REGNO (rtl);
10768 #ifdef LEAF_REG_REMAP
10769   if (current_function_uses_only_leaf_regs)
10770     {
10771       int leaf_reg = LEAF_REG_REMAP (reg);
10772       if (leaf_reg != -1)
10773         reg = (unsigned) leaf_reg;
10774     }
10775 #endif
10776   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10777   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10778
10779   /* Simple, contiguous registers.  */
10780   if (regs == NULL_RTX)
10781     {
10782       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10783
10784       loc_result = NULL;
10785       while (nregs--)
10786         {
10787           dw_loc_descr_ref t;
10788
10789           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10790                                       VAR_INIT_STATUS_INITIALIZED);
10791           add_loc_descr (&loc_result, t);
10792           add_loc_descr_op_piece (&loc_result, size);
10793           ++reg;
10794         }
10795       return loc_result;
10796     }
10797
10798   /* Now onto stupid register sets in non contiguous locations.  */
10799
10800   gcc_assert (GET_CODE (regs) == PARALLEL);
10801
10802   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10803   loc_result = NULL;
10804
10805   for (i = 0; i < XVECLEN (regs, 0); ++i)
10806     {
10807       dw_loc_descr_ref t;
10808
10809       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10810                                   VAR_INIT_STATUS_INITIALIZED);
10811       add_loc_descr (&loc_result, t);
10812       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10813       add_loc_descr_op_piece (&loc_result, size);
10814     }
10815
10816   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10817     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10818   return loc_result;
10819 }
10820
10821 /* Return a location descriptor that designates a constant.  */
10822
10823 static dw_loc_descr_ref
10824 int_loc_descriptor (HOST_WIDE_INT i)
10825 {
10826   enum dwarf_location_atom op;
10827
10828   /* Pick the smallest representation of a constant, rather than just
10829      defaulting to the LEB encoding.  */
10830   if (i >= 0)
10831     {
10832       if (i <= 31)
10833         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10834       else if (i <= 0xff)
10835         op = DW_OP_const1u;
10836       else if (i <= 0xffff)
10837         op = DW_OP_const2u;
10838       else if (HOST_BITS_PER_WIDE_INT == 32
10839                || i <= 0xffffffff)
10840         op = DW_OP_const4u;
10841       else
10842         op = DW_OP_constu;
10843     }
10844   else
10845     {
10846       if (i >= -0x80)
10847         op = DW_OP_const1s;
10848       else if (i >= -0x8000)
10849         op = DW_OP_const2s;
10850       else if (HOST_BITS_PER_WIDE_INT == 32
10851                || i >= -0x80000000)
10852         op = DW_OP_const4s;
10853       else
10854         op = DW_OP_consts;
10855     }
10856
10857   return new_loc_descr (op, i, 0);
10858 }
10859
10860 /* Return loc description representing "address" of integer value.
10861    This can appear only as toplevel expression.  */
10862
10863 static dw_loc_descr_ref
10864 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10865 {
10866   int litsize;
10867   dw_loc_descr_ref loc_result = NULL;
10868
10869   if (!(dwarf_version >= 4 || !dwarf_strict))
10870     return NULL;
10871
10872   if (i >= 0)
10873     {
10874       if (i <= 31)
10875         litsize = 1;
10876       else if (i <= 0xff)
10877         litsize = 2;
10878       else if (i <= 0xffff)
10879         litsize = 3;
10880       else if (HOST_BITS_PER_WIDE_INT == 32
10881                || i <= 0xffffffff)
10882         litsize = 5;
10883       else
10884         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10885     }
10886   else
10887     {
10888       if (i >= -0x80)
10889         litsize = 2;
10890       else if (i >= -0x8000)
10891         litsize = 3;
10892       else if (HOST_BITS_PER_WIDE_INT == 32
10893                || i >= -0x80000000)
10894         litsize = 5;
10895       else
10896         litsize = 1 + size_of_sleb128 (i);
10897     }
10898   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10899      is more compact.  For DW_OP_stack_value we need:
10900      litsize + 1 (DW_OP_stack_value)
10901      and for DW_OP_implicit_value:
10902      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10903   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10904     {
10905       loc_result = int_loc_descriptor (i);
10906       add_loc_descr (&loc_result,
10907                      new_loc_descr (DW_OP_stack_value, 0, 0));
10908       return loc_result;
10909     }
10910
10911   loc_result = new_loc_descr (DW_OP_implicit_value,
10912                               size, 0);
10913   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10914   loc_result->dw_loc_oprnd2.v.val_int = i;
10915   return loc_result;
10916 }
10917
10918 /* Return a location descriptor that designates a base+offset location.  */
10919
10920 static dw_loc_descr_ref
10921 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10922                  enum var_init_status initialized)
10923 {
10924   unsigned int regno;
10925   dw_loc_descr_ref result;
10926   dw_fde_ref fde = current_fde ();
10927
10928   /* We only use "frame base" when we're sure we're talking about the
10929      post-prologue local stack frame.  We do this by *not* running
10930      register elimination until this point, and recognizing the special
10931      argument pointer and soft frame pointer rtx's.  */
10932   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10933     {
10934       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10935
10936       if (elim != reg)
10937         {
10938           if (GET_CODE (elim) == PLUS)
10939             {
10940               offset += INTVAL (XEXP (elim, 1));
10941               elim = XEXP (elim, 0);
10942             }
10943           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10944                        && (elim == hard_frame_pointer_rtx
10945                            || elim == stack_pointer_rtx))
10946                       || elim == (frame_pointer_needed
10947                                   ? hard_frame_pointer_rtx
10948                                   : stack_pointer_rtx));
10949
10950           /* If drap register is used to align stack, use frame
10951              pointer + offset to access stack variables.  If stack
10952              is aligned without drap, use stack pointer + offset to
10953              access stack variables.  */
10954           if (crtl->stack_realign_tried
10955               && reg == frame_pointer_rtx)
10956             {
10957               int base_reg
10958                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10959                                       ? HARD_FRAME_POINTER_REGNUM
10960                                       : REGNO (elim));
10961               return new_reg_loc_descr (base_reg, offset);
10962             }
10963
10964           gcc_assert (frame_pointer_fb_offset_valid);
10965           offset += frame_pointer_fb_offset;
10966           return new_loc_descr (DW_OP_fbreg, offset, 0);
10967         }
10968     }
10969   else if (!optimize
10970            && fde
10971            && (fde->drap_reg == REGNO (reg)
10972                || fde->vdrap_reg == REGNO (reg)))
10973     {
10974       /* Use cfa+offset to represent the location of arguments passed
10975          on the stack when drap is used to align stack.
10976          Only do this when not optimizing, for optimized code var-tracking
10977          is supposed to track where the arguments live and the register
10978          used as vdrap or drap in some spot might be used for something
10979          else in other part of the routine.  */
10980       return new_loc_descr (DW_OP_fbreg, offset, 0);
10981     }
10982
10983   regno = dbx_reg_number (reg);
10984   if (regno <= 31)
10985     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10986                             offset, 0);
10987   else
10988     result = new_loc_descr (DW_OP_bregx, regno, offset);
10989
10990   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10991     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10992
10993   return result;
10994 }
10995
10996 /* Return true if this RTL expression describes a base+offset calculation.  */
10997
10998 static inline int
10999 is_based_loc (const_rtx rtl)
11000 {
11001   return (GET_CODE (rtl) == PLUS
11002           && ((REG_P (XEXP (rtl, 0))
11003                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11004                && CONST_INT_P (XEXP (rtl, 1)))));
11005 }
11006
11007 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11008    failed.  */
11009
11010 static dw_loc_descr_ref
11011 tls_mem_loc_descriptor (rtx mem)
11012 {
11013   tree base;
11014   dw_loc_descr_ref loc_result;
11015
11016   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
11017     return NULL;
11018
11019   base = get_base_address (MEM_EXPR (mem));
11020   if (base == NULL
11021       || TREE_CODE (base) != VAR_DECL
11022       || !DECL_THREAD_LOCAL_P (base))
11023     return NULL;
11024
11025   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11026   if (loc_result == NULL)
11027     return NULL;
11028
11029   if (INTVAL (MEM_OFFSET (mem)))
11030     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
11031
11032   return loc_result;
11033 }
11034
11035 /* Output debug info about reason why we failed to expand expression as dwarf
11036    expression.  */
11037
11038 static void
11039 expansion_failed (tree expr, rtx rtl, char const *reason)
11040 {
11041   if (dump_file && (dump_flags & TDF_DETAILS))
11042     {
11043       fprintf (dump_file, "Failed to expand as dwarf: ");
11044       if (expr)
11045         print_generic_expr (dump_file, expr, dump_flags);
11046       if (rtl)
11047         {
11048           fprintf (dump_file, "\n");
11049           print_rtl (dump_file, rtl);
11050         }
11051       fprintf (dump_file, "\nReason: %s\n", reason);
11052     }
11053 }
11054
11055 /* Helper function for const_ok_for_output, called either directly
11056    or via for_each_rtx.  */
11057
11058 static int
11059 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11060 {
11061   rtx rtl = *rtlp;
11062
11063   if (GET_CODE (rtl) == UNSPEC)
11064     {
11065       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
11066          we can't express it in the debug info.  */
11067 #ifdef ENABLE_CHECKING
11068       /* Don't complain about TLS UNSPECs, those are just too hard to
11069          delegitimize.  */
11070       if (XVECLEN (rtl, 0) != 1
11071           || GET_CODE (XVECEXP (rtl, 0, 0)) != SYMBOL_REF
11072           || SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0)) == NULL
11073           || TREE_CODE (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))) != VAR_DECL
11074           || !DECL_THREAD_LOCAL_P (SYMBOL_REF_DECL (XVECEXP (rtl, 0, 0))))
11075         inform (current_function_decl
11076                 ? DECL_SOURCE_LOCATION (current_function_decl)
11077                 : UNKNOWN_LOCATION,
11078 #if NUM_UNSPEC_VALUES > 0
11079                 "non-delegitimized UNSPEC %s (%d) found in variable location",
11080                 ((XINT (rtl, 1) >= 0 && XINT (rtl, 1) < NUM_UNSPEC_VALUES)
11081                  ? unspec_strings[XINT (rtl, 1)] : "unknown"),
11082                 XINT (rtl, 1));
11083 #else
11084                 "non-delegitimized UNSPEC %d found in variable location",
11085                 XINT (rtl, 1));
11086 #endif
11087 #endif
11088       expansion_failed (NULL_TREE, rtl,
11089                         "UNSPEC hasn't been delegitimized.\n");
11090       return 1;
11091     }
11092
11093   if (GET_CODE (rtl) != SYMBOL_REF)
11094     return 0;
11095
11096   if (CONSTANT_POOL_ADDRESS_P (rtl))
11097     {
11098       bool marked;
11099       get_pool_constant_mark (rtl, &marked);
11100       /* If all references to this pool constant were optimized away,
11101          it was not output and thus we can't represent it.  */
11102       if (!marked)
11103         {
11104           expansion_failed (NULL_TREE, rtl,
11105                             "Constant was removed from constant pool.\n");
11106           return 1;
11107         }
11108     }
11109
11110   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11111     return 1;
11112
11113   /* Avoid references to external symbols in debug info, on several targets
11114      the linker might even refuse to link when linking a shared library,
11115      and in many other cases the relocations for .debug_info/.debug_loc are
11116      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11117      to be defined within the same shared library or executable are fine.  */
11118   if (SYMBOL_REF_EXTERNAL_P (rtl))
11119     {
11120       tree decl = SYMBOL_REF_DECL (rtl);
11121
11122       if (decl == NULL || !targetm.binds_local_p (decl))
11123         {
11124           expansion_failed (NULL_TREE, rtl,
11125                             "Symbol not defined in current TU.\n");
11126           return 1;
11127         }
11128     }
11129
11130   return 0;
11131 }
11132
11133 /* Return true if constant RTL can be emitted in DW_OP_addr or
11134    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11135    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11136
11137 static bool
11138 const_ok_for_output (rtx rtl)
11139 {
11140   if (GET_CODE (rtl) == SYMBOL_REF)
11141     return const_ok_for_output_1 (&rtl, NULL) == 0;
11142
11143   if (GET_CODE (rtl) == CONST)
11144     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11145
11146   return true;
11147 }
11148
11149 /* Return a reference to DW_TAG_base_type corresponding to MODE and UNSIGNEDP
11150    if possible, NULL otherwise.  */
11151
11152 static dw_die_ref
11153 base_type_for_mode (enum machine_mode mode, bool unsignedp)
11154 {
11155   dw_die_ref type_die;
11156   tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
11157
11158   if (type == NULL)
11159     return NULL;
11160   switch (TREE_CODE (type))
11161     {
11162     case INTEGER_TYPE:
11163     case REAL_TYPE:
11164       break;
11165     default:
11166       return NULL;
11167     }
11168   type_die = lookup_type_die (type);
11169   if (!type_die)
11170     type_die = modified_type_die (type, false, false, comp_unit_die ());
11171   if (type_die == NULL || type_die->die_tag != DW_TAG_base_type)
11172     return NULL;
11173   return type_die;
11174 }
11175
11176 /* For OP descriptor assumed to be in unsigned MODE, convert it to a unsigned
11177    type matching MODE, or, if MODE is narrower than or as wide as
11178    DWARF2_ADDR_SIZE, untyped.  Return NULL if the conversion is not
11179    possible.  */
11180
11181 static dw_loc_descr_ref
11182 convert_descriptor_to_mode (enum machine_mode mode, dw_loc_descr_ref op)
11183 {
11184   enum machine_mode outer_mode = mode;
11185   dw_die_ref type_die;
11186   dw_loc_descr_ref cvt;
11187
11188   if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
11189     {
11190       add_loc_descr (&op, new_loc_descr (DW_OP_GNU_convert, 0, 0));
11191       return op;
11192     }
11193   type_die = base_type_for_mode (outer_mode, 1);
11194   if (type_die == NULL)
11195     return NULL;
11196   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11197   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11198   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11199   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11200   add_loc_descr (&op, cvt);
11201   return op;
11202 }
11203
11204 /* Return location descriptor for comparison OP with operands OP0 and OP1.  */
11205
11206 static dw_loc_descr_ref
11207 compare_loc_descriptor (enum dwarf_location_atom op, dw_loc_descr_ref op0,
11208                         dw_loc_descr_ref op1)
11209 {
11210   dw_loc_descr_ref ret = op0;
11211   add_loc_descr (&ret, op1);
11212   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11213   if (STORE_FLAG_VALUE != 1)
11214     {
11215       add_loc_descr (&ret, int_loc_descriptor (STORE_FLAG_VALUE));
11216       add_loc_descr (&ret, new_loc_descr (DW_OP_mul, 0, 0));
11217     }
11218   return ret;
11219 }
11220
11221 /* Return location descriptor for signed comparison OP RTL.  */
11222
11223 static dw_loc_descr_ref
11224 scompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11225                          enum machine_mode mem_mode)
11226 {
11227   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11228   dw_loc_descr_ref op0, op1;
11229   int shift;
11230
11231   if (op_mode == VOIDmode)
11232     op_mode = GET_MODE (XEXP (rtl, 1));
11233   if (op_mode == VOIDmode)
11234     return NULL;
11235
11236   if (dwarf_strict
11237       && (GET_MODE_CLASS (op_mode) != MODE_INT
11238           || GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE))
11239     return NULL;
11240
11241   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11242                             VAR_INIT_STATUS_INITIALIZED);
11243   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11244                             VAR_INIT_STATUS_INITIALIZED);
11245
11246   if (op0 == NULL || op1 == NULL)
11247     return NULL;
11248
11249   if (GET_MODE_CLASS (op_mode) != MODE_INT
11250       || GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11251     return compare_loc_descriptor (op, op0, op1);
11252
11253   if (GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11254     {
11255       dw_die_ref type_die = base_type_for_mode (op_mode, 0);
11256       dw_loc_descr_ref cvt;
11257
11258       if (type_die == NULL)
11259         return NULL;
11260       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11261       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11262       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11263       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11264       add_loc_descr (&op0, cvt);
11265       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11266       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11267       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11268       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11269       add_loc_descr (&op1, cvt);
11270       return compare_loc_descriptor (op, op0, op1);
11271     }
11272
11273   shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode)) * BITS_PER_UNIT;
11274   /* For eq/ne, if the operands are known to be zero-extended,
11275      there is no need to do the fancy shifting up.  */
11276   if (op == DW_OP_eq || op == DW_OP_ne)
11277     {
11278       dw_loc_descr_ref last0, last1;
11279       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11280         ;
11281       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11282         ;
11283       /* deref_size zero extends, and for constants we can check
11284          whether they are zero extended or not.  */
11285       if (((last0->dw_loc_opc == DW_OP_deref_size
11286             && last0->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11287            || (CONST_INT_P (XEXP (rtl, 0))
11288                && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
11289                   == (INTVAL (XEXP (rtl, 0)) & GET_MODE_MASK (op_mode))))
11290           && ((last1->dw_loc_opc == DW_OP_deref_size
11291                && last1->dw_loc_oprnd1.v.val_int <= GET_MODE_SIZE (op_mode))
11292               || (CONST_INT_P (XEXP (rtl, 1))
11293                   && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 1))
11294                      == (INTVAL (XEXP (rtl, 1)) & GET_MODE_MASK (op_mode)))))
11295         return compare_loc_descriptor (op, op0, op1);
11296     }
11297   add_loc_descr (&op0, int_loc_descriptor (shift));
11298   add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11299   if (CONST_INT_P (XEXP (rtl, 1)))
11300     op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11301   else
11302     {
11303       add_loc_descr (&op1, int_loc_descriptor (shift));
11304       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11305     }
11306   return compare_loc_descriptor (op, op0, op1);
11307 }
11308
11309 /* Return location descriptor for unsigned comparison OP RTL.  */
11310
11311 static dw_loc_descr_ref
11312 ucompare_loc_descriptor (enum dwarf_location_atom op, rtx rtl,
11313                          enum machine_mode mem_mode)
11314 {
11315   enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
11316   dw_loc_descr_ref op0, op1;
11317
11318   if (op_mode == VOIDmode)
11319     op_mode = GET_MODE (XEXP (rtl, 1));
11320   if (op_mode == VOIDmode)
11321     return NULL;
11322   if (GET_MODE_CLASS (op_mode) != MODE_INT)
11323     return NULL;
11324
11325   if (dwarf_strict && GET_MODE_SIZE (op_mode) > DWARF2_ADDR_SIZE)
11326     return NULL;
11327
11328   op0 = mem_loc_descriptor (XEXP (rtl, 0), op_mode, mem_mode,
11329                             VAR_INIT_STATUS_INITIALIZED);
11330   op1 = mem_loc_descriptor (XEXP (rtl, 1), op_mode, mem_mode,
11331                             VAR_INIT_STATUS_INITIALIZED);
11332
11333   if (op0 == NULL || op1 == NULL)
11334     return NULL;
11335
11336   if (GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
11337     {
11338       HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
11339       dw_loc_descr_ref last0, last1;
11340       for (last0 = op0; last0->dw_loc_next != NULL; last0 = last0->dw_loc_next)
11341         ;
11342       for (last1 = op1; last1->dw_loc_next != NULL; last1 = last1->dw_loc_next)
11343         ;
11344       if (CONST_INT_P (XEXP (rtl, 0)))
11345         op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
11346       /* deref_size zero extends, so no need to mask it again.  */
11347       else if (last0->dw_loc_opc != DW_OP_deref_size
11348                || last0->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11349         {
11350           add_loc_descr (&op0, int_loc_descriptor (mask));
11351           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11352         }
11353       if (CONST_INT_P (XEXP (rtl, 1)))
11354         op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11355       /* deref_size zero extends, so no need to mask it again.  */
11356       else if (last1->dw_loc_opc != DW_OP_deref_size
11357                || last1->dw_loc_oprnd1.v.val_int > GET_MODE_SIZE (op_mode))
11358         {
11359           add_loc_descr (&op1, int_loc_descriptor (mask));
11360           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11361         }
11362     }
11363   else if (GET_MODE_SIZE (op_mode) == DWARF2_ADDR_SIZE)
11364     {
11365       HOST_WIDE_INT bias = 1;
11366       bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11367       add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11368       if (CONST_INT_P (XEXP (rtl, 1)))
11369         op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11370                                   + INTVAL (XEXP (rtl, 1)));
11371       else
11372         add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
11373                                             bias, 0));
11374     }
11375   return compare_loc_descriptor (op, op0, op1);
11376 }
11377
11378 /* Return location descriptor for {U,S}{MIN,MAX}.  */
11379
11380 static dw_loc_descr_ref
11381 minmax_loc_descriptor (rtx rtl, enum machine_mode mode,
11382                        enum machine_mode mem_mode)
11383 {
11384   enum dwarf_location_atom op;
11385   dw_loc_descr_ref op0, op1, ret;
11386   dw_loc_descr_ref bra_node, drop_node;
11387
11388   if (dwarf_strict
11389       && (GET_MODE_CLASS (mode) != MODE_INT
11390           || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE))
11391     return NULL;
11392
11393   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11394                             VAR_INIT_STATUS_INITIALIZED);
11395   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11396                             VAR_INIT_STATUS_INITIALIZED);
11397
11398   if (op0 == NULL || op1 == NULL)
11399     return NULL;
11400
11401   add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11402   add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11403   add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11404   if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11405     {
11406       if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11407         {
11408           HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11409           add_loc_descr (&op0, int_loc_descriptor (mask));
11410           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11411           add_loc_descr (&op1, int_loc_descriptor (mask));
11412           add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11413         }
11414       else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
11415         {
11416           HOST_WIDE_INT bias = 1;
11417           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11418           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11419           add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11420         }
11421     }
11422   else if (GET_MODE_CLASS (mode) == MODE_INT
11423            && GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11424     {
11425       int shift = (DWARF2_ADDR_SIZE - GET_MODE_SIZE (mode)) * BITS_PER_UNIT;
11426       add_loc_descr (&op0, int_loc_descriptor (shift));
11427       add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11428       add_loc_descr (&op1, int_loc_descriptor (shift));
11429       add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11430     }
11431   else if (GET_MODE_CLASS (mode) == MODE_INT
11432            && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11433     {
11434       dw_die_ref type_die = base_type_for_mode (mode, 0);
11435       dw_loc_descr_ref cvt;
11436       if (type_die == NULL)
11437         return NULL;
11438       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11439       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11440       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11441       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11442       add_loc_descr (&op0, cvt);
11443       cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11444       cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11445       cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11446       cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11447       add_loc_descr (&op1, cvt);
11448     }
11449
11450   if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11451     op = DW_OP_lt;
11452   else
11453     op = DW_OP_gt;
11454   ret = op0;
11455   add_loc_descr (&ret, op1);
11456   add_loc_descr (&ret, new_loc_descr (op, 0, 0));
11457   bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11458   add_loc_descr (&ret, bra_node);
11459   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11460   drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11461   add_loc_descr (&ret, drop_node);
11462   bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11463   bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11464   if ((GET_CODE (rtl) == SMIN || GET_CODE (rtl) == SMAX)
11465       && GET_MODE_CLASS (mode) == MODE_INT
11466       && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
11467     ret = convert_descriptor_to_mode (mode, ret);
11468   return ret;
11469 }
11470
11471 /* Helper function for mem_loc_descriptor.  Perform OP binary op,
11472    but after converting arguments to type_die, afterwards
11473    convert back to unsigned.  */
11474
11475 static dw_loc_descr_ref
11476 typed_binop (enum dwarf_location_atom op, rtx rtl, dw_die_ref type_die,
11477              enum machine_mode mode, enum machine_mode mem_mode)
11478 {
11479   dw_loc_descr_ref cvt, op0, op1;
11480
11481   if (type_die == NULL)
11482     return NULL;
11483   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11484                             VAR_INIT_STATUS_INITIALIZED);
11485   op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
11486                             VAR_INIT_STATUS_INITIALIZED);
11487   if (op0 == NULL || op1 == NULL)
11488     return NULL;
11489   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11490   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11491   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11492   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11493   add_loc_descr (&op0, cvt);
11494   cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11495   cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11496   cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
11497   cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
11498   add_loc_descr (&op1, cvt);
11499   add_loc_descr (&op0, op1);
11500   add_loc_descr (&op0, new_loc_descr (op, 0, 0));
11501   return convert_descriptor_to_mode (mode, op0);
11502 }
11503
11504 /* CLZ (where constV is CLZ_DEFINED_VALUE_AT_ZERO computed value,
11505    const0 is DW_OP_lit0 or corresponding typed constant,
11506    const1 is DW_OP_lit1 or corresponding typed constant
11507    and constMSB is constant with just the MSB bit set
11508    for the mode):
11509        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11510    L1: const0 DW_OP_swap
11511    L2: DW_OP_dup constMSB DW_OP_and DW_OP_bra <L3> const1 DW_OP_shl
11512        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11513    L3: DW_OP_drop
11514    L4: DW_OP_nop
11515
11516    CTZ is similar:
11517        DW_OP_dup DW_OP_bra <L1> DW_OP_drop constV DW_OP_skip <L4>
11518    L1: const0 DW_OP_swap
11519    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11520        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11521    L3: DW_OP_drop
11522    L4: DW_OP_nop
11523
11524    FFS is similar:
11525        DW_OP_dup DW_OP_bra <L1> DW_OP_drop const0 DW_OP_skip <L4>
11526    L1: const1 DW_OP_swap
11527    L2: DW_OP_dup const1 DW_OP_and DW_OP_bra <L3> const1 DW_OP_shr
11528        DW_OP_swap DW_OP_plus_uconst <1> DW_OP_swap DW_OP_skip <L2>
11529    L3: DW_OP_drop
11530    L4: DW_OP_nop  */
11531
11532 static dw_loc_descr_ref
11533 clz_loc_descriptor (rtx rtl, enum machine_mode mode,
11534                     enum machine_mode mem_mode)
11535 {
11536   dw_loc_descr_ref op0, ret, tmp;
11537   HOST_WIDE_INT valv;
11538   dw_loc_descr_ref l1jump, l1label;
11539   dw_loc_descr_ref l2jump, l2label;
11540   dw_loc_descr_ref l3jump, l3label;
11541   dw_loc_descr_ref l4jump, l4label;
11542   rtx msb;
11543
11544   if (GET_MODE_CLASS (mode) != MODE_INT
11545       || GET_MODE (XEXP (rtl, 0)) != mode
11546       || (GET_CODE (rtl) == CLZ
11547           && GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT))
11548     return NULL;
11549
11550   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11551                             VAR_INIT_STATUS_INITIALIZED);
11552   if (op0 == NULL)
11553     return NULL;
11554   ret = op0;
11555   if (GET_CODE (rtl) == CLZ)
11556     {
11557       if (!CLZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11558         valv = GET_MODE_BITSIZE (mode);
11559     }
11560   else if (GET_CODE (rtl) == FFS)
11561     valv = 0;
11562   else if (!CTZ_DEFINED_VALUE_AT_ZERO (mode, valv))
11563     valv = GET_MODE_BITSIZE (mode);
11564   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11565   l1jump = new_loc_descr (DW_OP_bra, 0, 0);
11566   add_loc_descr (&ret, l1jump);
11567   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11568   tmp = mem_loc_descriptor (GEN_INT (valv), mode, mem_mode,
11569                             VAR_INIT_STATUS_INITIALIZED);
11570   if (tmp == NULL)
11571     return NULL;
11572   add_loc_descr (&ret, tmp);
11573   l4jump = new_loc_descr (DW_OP_skip, 0, 0);
11574   add_loc_descr (&ret, l4jump);
11575   l1label = mem_loc_descriptor (GET_CODE (rtl) == FFS
11576                                 ? const1_rtx : const0_rtx,
11577                                 mode, mem_mode,
11578                                 VAR_INIT_STATUS_INITIALIZED);
11579   if (l1label == NULL)
11580     return NULL;
11581   add_loc_descr (&ret, l1label);
11582   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11583   l2label = new_loc_descr (DW_OP_dup, 0, 0);
11584   add_loc_descr (&ret, l2label);
11585   if (GET_CODE (rtl) != CLZ)
11586     msb = const1_rtx;
11587   else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11588     msb = GEN_INT ((unsigned HOST_WIDE_INT) 1
11589                    << (GET_MODE_BITSIZE (mode) - 1));
11590   else
11591     msb = immed_double_const (0, (unsigned HOST_WIDE_INT) 1
11592                                   << (GET_MODE_BITSIZE (mode)
11593                                       - HOST_BITS_PER_WIDE_INT - 1), mode);
11594   if (GET_CODE (msb) == CONST_INT && INTVAL (msb) < 0)
11595     tmp = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11596                          ? DW_OP_const4u : HOST_BITS_PER_WIDE_INT == 64
11597                          ? DW_OP_const8u : DW_OP_constu, INTVAL (msb), 0);
11598   else
11599     tmp = mem_loc_descriptor (msb, mode, mem_mode,
11600                               VAR_INIT_STATUS_INITIALIZED);
11601   if (tmp == NULL)
11602     return NULL;
11603   add_loc_descr (&ret, tmp);
11604   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11605   l3jump = new_loc_descr (DW_OP_bra, 0, 0);
11606   add_loc_descr (&ret, l3jump);
11607   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11608                             VAR_INIT_STATUS_INITIALIZED);
11609   if (tmp == NULL)
11610     return NULL;
11611   add_loc_descr (&ret, tmp);
11612   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == CLZ
11613                                       ? DW_OP_shl : DW_OP_shr, 0, 0));
11614   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11615   add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, 1, 0));
11616   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11617   l2jump = new_loc_descr (DW_OP_skip, 0, 0);
11618   add_loc_descr (&ret, l2jump);
11619   l3label = new_loc_descr (DW_OP_drop, 0, 0);
11620   add_loc_descr (&ret, l3label);
11621   l4label = new_loc_descr (DW_OP_nop, 0, 0);
11622   add_loc_descr (&ret, l4label);
11623   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11624   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11625   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11626   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11627   l3jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11628   l3jump->dw_loc_oprnd1.v.val_loc = l3label;
11629   l4jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11630   l4jump->dw_loc_oprnd1.v.val_loc = l4label;
11631   return ret;
11632 }
11633
11634 /* POPCOUNT (const0 is DW_OP_lit0 or corresponding typed constant,
11635    const1 is DW_OP_lit1 or corresponding typed constant):
11636        const0 DW_OP_swap
11637    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11638        DW_OP_plus DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11639    L2: DW_OP_drop
11640
11641    PARITY is similar:
11642    L1: DW_OP_dup DW_OP_bra <L2> DW_OP_dup DW_OP_rot const1 DW_OP_and
11643        DW_OP_xor DW_OP_swap const1 DW_OP_shr DW_OP_skip <L1>
11644    L2: DW_OP_drop  */
11645
11646 static dw_loc_descr_ref
11647 popcount_loc_descriptor (rtx rtl, enum machine_mode mode,
11648                          enum machine_mode mem_mode)
11649 {
11650   dw_loc_descr_ref op0, ret, tmp;
11651   dw_loc_descr_ref l1jump, l1label;
11652   dw_loc_descr_ref l2jump, l2label;
11653
11654   if (GET_MODE_CLASS (mode) != MODE_INT
11655       || GET_MODE (XEXP (rtl, 0)) != mode)
11656     return NULL;
11657
11658   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11659                             VAR_INIT_STATUS_INITIALIZED);
11660   if (op0 == NULL)
11661     return NULL;
11662   ret = op0;
11663   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11664                             VAR_INIT_STATUS_INITIALIZED);
11665   if (tmp == NULL)
11666     return NULL;
11667   add_loc_descr (&ret, tmp);
11668   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11669   l1label = new_loc_descr (DW_OP_dup, 0, 0);
11670   add_loc_descr (&ret, l1label);
11671   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11672   add_loc_descr (&ret, l2jump);
11673   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11674   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11675   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11676                             VAR_INIT_STATUS_INITIALIZED);
11677   if (tmp == NULL)
11678     return NULL;
11679   add_loc_descr (&ret, tmp);
11680   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11681   add_loc_descr (&ret, new_loc_descr (GET_CODE (rtl) == POPCOUNT
11682                                       ? DW_OP_plus : DW_OP_xor, 0, 0));
11683   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11684   tmp = mem_loc_descriptor (const1_rtx, mode, mem_mode,
11685                             VAR_INIT_STATUS_INITIALIZED);
11686   add_loc_descr (&ret, tmp);
11687   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11688   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11689   add_loc_descr (&ret, l1jump);
11690   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11691   add_loc_descr (&ret, l2label);
11692   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11693   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11694   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11695   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11696   return ret;
11697 }
11698
11699 /* BSWAP (constS is initial shift count, either 56 or 24):
11700        constS const0
11701    L1: DW_OP_pick <2> constS DW_OP_pick <3> DW_OP_minus DW_OP_shr
11702        const255 DW_OP_and DW_OP_pick <2> DW_OP_shl DW_OP_or
11703        DW_OP_swap DW_OP_dup const0 DW_OP_eq DW_OP_bra <L2> const8
11704        DW_OP_minus DW_OP_swap DW_OP_skip <L1>
11705    L2: DW_OP_drop DW_OP_swap DW_OP_drop  */
11706
11707 static dw_loc_descr_ref
11708 bswap_loc_descriptor (rtx rtl, enum machine_mode mode,
11709                       enum machine_mode mem_mode)
11710 {
11711   dw_loc_descr_ref op0, ret, tmp;
11712   dw_loc_descr_ref l1jump, l1label;
11713   dw_loc_descr_ref l2jump, l2label;
11714
11715   if (GET_MODE_CLASS (mode) != MODE_INT
11716       || BITS_PER_UNIT != 8
11717       || (GET_MODE_BITSIZE (mode) != 32
11718           &&  GET_MODE_BITSIZE (mode) != 64))
11719     return NULL;
11720
11721   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11722                             VAR_INIT_STATUS_INITIALIZED);
11723   if (op0 == NULL)
11724     return NULL;
11725
11726   ret = op0;
11727   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11728                             mode, mem_mode,
11729                             VAR_INIT_STATUS_INITIALIZED);
11730   if (tmp == NULL)
11731     return NULL;
11732   add_loc_descr (&ret, tmp);
11733   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11734                             VAR_INIT_STATUS_INITIALIZED);
11735   if (tmp == NULL)
11736     return NULL;
11737   add_loc_descr (&ret, tmp);
11738   l1label = new_loc_descr (DW_OP_pick, 2, 0);
11739   add_loc_descr (&ret, l1label);
11740   tmp = mem_loc_descriptor (GEN_INT (GET_MODE_BITSIZE (mode) - 8),
11741                             mode, mem_mode,
11742                             VAR_INIT_STATUS_INITIALIZED);
11743   add_loc_descr (&ret, tmp);
11744   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 3, 0));
11745   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11746   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11747   tmp = mem_loc_descriptor (GEN_INT (255), mode, mem_mode,
11748                             VAR_INIT_STATUS_INITIALIZED);
11749   if (tmp == NULL)
11750     return NULL;
11751   add_loc_descr (&ret, tmp);
11752   add_loc_descr (&ret, new_loc_descr (DW_OP_and, 0, 0));
11753   add_loc_descr (&ret, new_loc_descr (DW_OP_pick, 2, 0));
11754   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11755   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11756   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11757   add_loc_descr (&ret, new_loc_descr (DW_OP_dup, 0, 0));
11758   tmp = mem_loc_descriptor (const0_rtx, mode, mem_mode,
11759                             VAR_INIT_STATUS_INITIALIZED);
11760   add_loc_descr (&ret, tmp);
11761   add_loc_descr (&ret, new_loc_descr (DW_OP_eq, 0, 0));
11762   l2jump = new_loc_descr (DW_OP_bra, 0, 0);
11763   add_loc_descr (&ret, l2jump);
11764   tmp = mem_loc_descriptor (GEN_INT (8), mode, mem_mode,
11765                             VAR_INIT_STATUS_INITIALIZED);
11766   add_loc_descr (&ret, tmp);
11767   add_loc_descr (&ret, new_loc_descr (DW_OP_minus, 0, 0));
11768   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11769   l1jump = new_loc_descr (DW_OP_skip, 0, 0);
11770   add_loc_descr (&ret, l1jump);
11771   l2label = new_loc_descr (DW_OP_drop, 0, 0);
11772   add_loc_descr (&ret, l2label);
11773   add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11774   add_loc_descr (&ret, new_loc_descr (DW_OP_drop, 0, 0));
11775   l1jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11776   l1jump->dw_loc_oprnd1.v.val_loc = l1label;
11777   l2jump->dw_loc_oprnd1.val_class = dw_val_class_loc;
11778   l2jump->dw_loc_oprnd1.v.val_loc = l2label;
11779   return ret;
11780 }
11781
11782 /* ROTATE (constMASK is mode mask, BITSIZE is bitsize of mode):
11783    DW_OP_over DW_OP_over DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11784    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_neg
11785    DW_OP_plus_uconst <BITSIZE> DW_OP_shr DW_OP_or
11786
11787    ROTATERT is similar:
11788    DW_OP_over DW_OP_over DW_OP_neg DW_OP_plus_uconst <BITSIZE>
11789    DW_OP_shl [ constMASK DW_OP_and ] DW_OP_rot
11790    [ DW_OP_swap constMASK DW_OP_and DW_OP_swap ] DW_OP_shr DW_OP_or  */
11791
11792 static dw_loc_descr_ref
11793 rotate_loc_descriptor (rtx rtl, enum machine_mode mode,
11794                        enum machine_mode mem_mode)
11795 {
11796   rtx rtlop1 = XEXP (rtl, 1);
11797   dw_loc_descr_ref op0, op1, ret, mask[2] = { NULL, NULL };
11798   int i;
11799
11800   if (GET_MODE_CLASS (mode) != MODE_INT)
11801     return NULL;
11802
11803   if (GET_MODE (rtlop1) != VOIDmode
11804       && GET_MODE_BITSIZE (GET_MODE (rtlop1)) < GET_MODE_BITSIZE (mode))
11805     rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
11806   op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
11807                             VAR_INIT_STATUS_INITIALIZED);
11808   op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
11809                             VAR_INIT_STATUS_INITIALIZED);
11810   if (op0 == NULL || op1 == NULL)
11811     return NULL;
11812   if (GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE)
11813     for (i = 0; i < 2; i++)
11814       {
11815         if (GET_MODE_BITSIZE (mode) < HOST_BITS_PER_WIDE_INT)
11816           mask[i] = mem_loc_descriptor (GEN_INT (GET_MODE_MASK (mode)),
11817                                         mode, mem_mode,
11818                                         VAR_INIT_STATUS_INITIALIZED);
11819         else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
11820           mask[i] = new_loc_descr (HOST_BITS_PER_WIDE_INT == 32
11821                                    ? DW_OP_const4u
11822                                    : HOST_BITS_PER_WIDE_INT == 64
11823                                    ? DW_OP_const8u : DW_OP_constu,
11824                                    GET_MODE_MASK (mode), 0);
11825         else
11826           mask[i] = NULL;
11827         if (mask[i] == NULL)
11828           return NULL;
11829         add_loc_descr (&mask[i], new_loc_descr (DW_OP_and, 0, 0));
11830       }
11831   ret = op0;
11832   add_loc_descr (&ret, op1);
11833   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11834   add_loc_descr (&ret, new_loc_descr (DW_OP_over, 0, 0));
11835   if (GET_CODE (rtl) == ROTATERT)
11836     {
11837       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11838       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11839                                           GET_MODE_BITSIZE (mode), 0));
11840     }
11841   add_loc_descr (&ret, new_loc_descr (DW_OP_shl, 0, 0));
11842   if (mask[0] != NULL)
11843     add_loc_descr (&ret, mask[0]);
11844   add_loc_descr (&ret, new_loc_descr (DW_OP_rot, 0, 0));
11845   if (mask[1] != NULL)
11846     {
11847       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11848       add_loc_descr (&ret, mask[1]);
11849       add_loc_descr (&ret, new_loc_descr (DW_OP_swap, 0, 0));
11850     }
11851   if (GET_CODE (rtl) == ROTATE)
11852     {
11853       add_loc_descr (&ret, new_loc_descr (DW_OP_neg, 0, 0));
11854       add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst,
11855                                           GET_MODE_BITSIZE (mode), 0));
11856     }
11857   add_loc_descr (&ret, new_loc_descr (DW_OP_shr, 0, 0));
11858   add_loc_descr (&ret, new_loc_descr (DW_OP_or, 0, 0));
11859   return ret;
11860 }
11861
11862 /* Helper function for mem_loc_descriptor.  Return DW_OP_GNU_parameter_ref
11863    for DEBUG_PARAMETER_REF RTL.  */
11864
11865 static dw_loc_descr_ref
11866 parameter_ref_descriptor (rtx rtl)
11867 {
11868   dw_loc_descr_ref ret;
11869   dw_die_ref ref;
11870
11871   if (dwarf_strict)
11872     return NULL;
11873   gcc_assert (TREE_CODE (DEBUG_PARAMETER_REF_DECL (rtl)) == PARM_DECL);
11874   ref = lookup_decl_die (DEBUG_PARAMETER_REF_DECL (rtl));
11875   ret = new_loc_descr (DW_OP_GNU_parameter_ref, 0, 0);
11876   if (ref)
11877     {
11878       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
11879       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
11880       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
11881     }
11882   else
11883     {
11884       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
11885       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_PARAMETER_REF_DECL (rtl);
11886     }
11887   return ret;
11888 }
11889
11890 /* Helper function to get mode of MEM's address.  */
11891
11892 enum machine_mode
11893 get_address_mode (rtx mem)
11894 {
11895   enum machine_mode mode = GET_MODE (XEXP (mem, 0));
11896   if (mode != VOIDmode)
11897     return mode;
11898   return targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
11899 }
11900
11901 /* The following routine converts the RTL for a variable or parameter
11902    (resident in memory) into an equivalent Dwarf representation of a
11903    mechanism for getting the address of that same variable onto the top of a
11904    hypothetical "address evaluation" stack.
11905
11906    When creating memory location descriptors, we are effectively transforming
11907    the RTL for a memory-resident object into its Dwarf postfix expression
11908    equivalent.  This routine recursively descends an RTL tree, turning
11909    it into Dwarf postfix code as it goes.
11910
11911    MODE is the mode that should be assumed for the rtl if it is VOIDmode.
11912
11913    MEM_MODE is the mode of the memory reference, needed to handle some
11914    autoincrement addressing modes.
11915
11916    Return 0 if we can't represent the location.  */
11917
11918 dw_loc_descr_ref
11919 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11920                     enum machine_mode mem_mode,
11921                     enum var_init_status initialized)
11922 {
11923   dw_loc_descr_ref mem_loc_result = NULL;
11924   enum dwarf_location_atom op;
11925   dw_loc_descr_ref op0, op1;
11926
11927   if (mode == VOIDmode)
11928     mode = GET_MODE (rtl);
11929
11930   /* Note that for a dynamically sized array, the location we will generate a
11931      description of here will be the lowest numbered location which is
11932      actually within the array.  That's *not* necessarily the same as the
11933      zeroth element of the array.  */
11934
11935   rtl = targetm.delegitimize_address (rtl);
11936
11937   if (mode != GET_MODE (rtl) && GET_MODE (rtl) != VOIDmode)
11938     return NULL;
11939
11940   switch (GET_CODE (rtl))
11941     {
11942     case POST_INC:
11943     case POST_DEC:
11944     case POST_MODIFY:
11945       return mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode, initialized);
11946
11947     case SUBREG:
11948       /* The case of a subreg may arise when we have a local (register)
11949          variable or a formal (register) parameter which doesn't quite fill
11950          up an entire register.  For now, just assume that it is
11951          legitimate to make the Dwarf info refer to the whole register which
11952          contains the given subreg.  */
11953       if (!subreg_lowpart_p (rtl))
11954         break;
11955       if (GET_MODE_CLASS (mode) == MODE_INT
11956           && GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) == MODE_INT
11957           && (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
11958 #ifdef POINTERS_EXTEND_UNSIGNED
11959               || (mode == Pmode && mem_mode != VOIDmode)
11960 #endif
11961              )
11962           && GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))) <= DWARF2_ADDR_SIZE)
11963         {
11964           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11965                                                GET_MODE (SUBREG_REG (rtl)),
11966                                                mem_mode, initialized);
11967           break;
11968         }
11969       if (dwarf_strict)
11970         break;
11971       if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11972         break;
11973       if (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl)))
11974           && (GET_MODE_CLASS (mode) != MODE_INT
11975               || GET_MODE_CLASS (GET_MODE (SUBREG_REG (rtl))) != MODE_INT))
11976         break;
11977       else
11978         {
11979           dw_die_ref type_die;
11980           dw_loc_descr_ref cvt;
11981
11982           mem_loc_result = mem_loc_descriptor (SUBREG_REG (rtl),
11983                                                GET_MODE (SUBREG_REG (rtl)),
11984                                                mem_mode, initialized);
11985           if (mem_loc_result == NULL)
11986             break;
11987           type_die = base_type_for_mode (mode,
11988                                          GET_MODE_CLASS (mode) == MODE_INT);
11989           if (type_die == NULL)
11990             {
11991               mem_loc_result = NULL;
11992               break;
11993             }
11994           if (GET_MODE_SIZE (mode)
11995               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (rtl))))
11996             cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
11997           else
11998             cvt = new_loc_descr (DW_OP_GNU_reinterpret, 0, 0);
11999           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12000           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12001           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12002           add_loc_descr (&mem_loc_result, cvt);
12003         }
12004       break;
12005
12006     case REG:
12007       if (GET_MODE_CLASS (mode) != MODE_INT
12008           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12009 #ifdef POINTERS_EXTEND_UNSIGNED
12010               && (mode != Pmode || mem_mode == VOIDmode)
12011 #endif
12012               ))
12013         {
12014           dw_die_ref type_die;
12015
12016           if (dwarf_strict)
12017             break;
12018           if (REGNO (rtl) > FIRST_PSEUDO_REGISTER)
12019             break;
12020           type_die = base_type_for_mode (mode,
12021                                          GET_MODE_CLASS (mode) == MODE_INT);
12022           if (type_die == NULL)
12023             break;
12024           mem_loc_result = new_loc_descr (DW_OP_GNU_regval_type,
12025                                           dbx_reg_number (rtl), 0);
12026           mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12027           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12028           mem_loc_result->dw_loc_oprnd2.v.val_die_ref.external = 0;
12029           break;
12030         }
12031       /* Whenever a register number forms a part of the description of the
12032          method for calculating the (dynamic) address of a memory resident
12033          object, DWARF rules require the register number be referred to as
12034          a "base register".  This distinction is not based in any way upon
12035          what category of register the hardware believes the given register
12036          belongs to.  This is strictly DWARF terminology we're dealing with
12037          here. Note that in cases where the location of a memory-resident
12038          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
12039          OP_CONST (0)) the actual DWARF location descriptor that we generate
12040          may just be OP_BASEREG (basereg).  This may look deceptively like
12041          the object in question was allocated to a register (rather than in
12042          memory) so DWARF consumers need to be aware of the subtle
12043          distinction between OP_REG and OP_BASEREG.  */
12044       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
12045         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
12046       else if (stack_realign_drap
12047                && crtl->drap_reg
12048                && crtl->args.internal_arg_pointer == rtl
12049                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
12050         {
12051           /* If RTL is internal_arg_pointer, which has been optimized
12052              out, use DRAP instead.  */
12053           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
12054                                             VAR_INIT_STATUS_INITIALIZED);
12055         }
12056       break;
12057
12058     case SIGN_EXTEND:
12059     case ZERO_EXTEND:
12060       if (GET_MODE_CLASS (mode) != MODE_INT)
12061         break;
12062       op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12063                                 mem_mode, VAR_INIT_STATUS_INITIALIZED);
12064       if (op0 == 0)
12065         break;
12066       else if (GET_CODE (rtl) == ZERO_EXTEND
12067                && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12068                && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12069                   < HOST_BITS_PER_WIDE_INT
12070                /* If DW_OP_const{1,2,4}u won't be used, it is shorter
12071                   to expand zero extend as two shifts instead of
12072                   masking.  */
12073                && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= 4)
12074         {
12075           enum machine_mode imode = GET_MODE (XEXP (rtl, 0));
12076           mem_loc_result = op0;
12077           add_loc_descr (&mem_loc_result,
12078                          int_loc_descriptor (GET_MODE_MASK (imode)));
12079           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_and, 0, 0));
12080         }
12081       else if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12082         {
12083           int shift = DWARF2_ADDR_SIZE
12084                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
12085           shift *= BITS_PER_UNIT;
12086           if (GET_CODE (rtl) == SIGN_EXTEND)
12087             op = DW_OP_shra;
12088           else
12089             op = DW_OP_shr;
12090           mem_loc_result = op0;
12091           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12092           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12093           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
12094           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12095         }
12096       else if (!dwarf_strict)
12097         {
12098           dw_die_ref type_die1, type_die2;
12099           dw_loc_descr_ref cvt;
12100
12101           type_die1 = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12102                                           GET_CODE (rtl) == ZERO_EXTEND);
12103           if (type_die1 == NULL)
12104             break;
12105           type_die2 = base_type_for_mode (mode, 1);
12106           if (type_die2 == NULL)
12107             break;
12108           mem_loc_result = op0;
12109           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12110           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12111           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die1;
12112           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12113           add_loc_descr (&mem_loc_result, cvt);
12114           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12115           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12116           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die2;
12117           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12118           add_loc_descr (&mem_loc_result, cvt);
12119         }
12120       break;
12121
12122     case MEM:
12123       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0),
12124                                            get_address_mode (rtl), mode,
12125                                            VAR_INIT_STATUS_INITIALIZED);
12126       if (mem_loc_result == NULL)
12127         mem_loc_result = tls_mem_loc_descriptor (rtl);
12128       if (mem_loc_result != 0)
12129         {
12130           if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12131               || GET_MODE_CLASS (mode) != MODE_INT)
12132             {
12133               dw_die_ref type_die;
12134               dw_loc_descr_ref deref;
12135
12136               if (dwarf_strict)
12137                 return NULL;
12138               type_die
12139                 = base_type_for_mode (mode, GET_MODE_CLASS (mode) == MODE_INT);
12140               if (type_die == NULL)
12141                 return NULL;
12142               deref = new_loc_descr (DW_OP_GNU_deref_type,
12143                                      GET_MODE_SIZE (mode), 0);
12144               deref->dw_loc_oprnd2.val_class = dw_val_class_die_ref;
12145               deref->dw_loc_oprnd2.v.val_die_ref.die = type_die;
12146               deref->dw_loc_oprnd2.v.val_die_ref.external = 0;
12147               add_loc_descr (&mem_loc_result, deref);
12148             }
12149           else if (GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE)
12150             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
12151           else
12152             add_loc_descr (&mem_loc_result,
12153                            new_loc_descr (DW_OP_deref_size,
12154                                           GET_MODE_SIZE (mode), 0));
12155         }
12156       else
12157         {
12158           rtx new_rtl = avoid_constant_pool_reference (rtl);
12159           if (new_rtl != rtl)
12160             return mem_loc_descriptor (new_rtl, mode, mem_mode, initialized);
12161         }
12162       break;
12163
12164     case LO_SUM:
12165       return mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode, initialized);
12166
12167     case LABEL_REF:
12168       /* Some ports can transform a symbol ref into a label ref, because
12169          the symbol ref is too far away and has to be dumped into a constant
12170          pool.  */
12171     case CONST:
12172     case SYMBOL_REF:
12173       if (GET_MODE_CLASS (mode) != MODE_INT
12174           || (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE
12175 #ifdef POINTERS_EXTEND_UNSIGNED
12176               && (mode != Pmode || mem_mode == VOIDmode)
12177 #endif
12178               ))
12179         break;
12180       if (GET_CODE (rtl) == SYMBOL_REF
12181           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12182         {
12183           dw_loc_descr_ref temp;
12184
12185           /* If this is not defined, we have no way to emit the data.  */
12186           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
12187             break;
12188
12189           /* We used to emit DW_OP_addr here, but that's wrong, since
12190              DW_OP_addr should be relocated by the debug info consumer,
12191              while DW_OP_GNU_push_tls_address operand should not.  */
12192           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
12193                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
12194           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
12195           temp->dw_loc_oprnd1.v.val_addr = rtl;
12196           temp->dtprel = true;
12197
12198           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
12199           add_loc_descr (&mem_loc_result, temp);
12200
12201           break;
12202         }
12203
12204       if (!const_ok_for_output (rtl))
12205         break;
12206
12207     symref:
12208       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
12209       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
12210       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
12211       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12212       break;
12213
12214     case CONCAT:
12215     case CONCATN:
12216     case VAR_LOCATION:
12217     case DEBUG_IMPLICIT_PTR:
12218       expansion_failed (NULL_TREE, rtl,
12219                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
12220       return 0;
12221
12222     case ENTRY_VALUE:
12223       if (dwarf_strict)
12224         return NULL;
12225       if (REG_P (ENTRY_VALUE_EXP (rtl)))
12226         {
12227           if (GET_MODE_CLASS (mode) != MODE_INT
12228               || GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12229             op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12230                                       VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12231           else
12232             op0
12233               = one_reg_loc_descriptor (dbx_reg_number (ENTRY_VALUE_EXP (rtl)),
12234                                         VAR_INIT_STATUS_INITIALIZED);
12235         }
12236       else if (MEM_P (ENTRY_VALUE_EXP (rtl))
12237                && REG_P (XEXP (ENTRY_VALUE_EXP (rtl), 0)))
12238         {
12239           op0 = mem_loc_descriptor (ENTRY_VALUE_EXP (rtl), mode,
12240                                     VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12241           if (op0 && op0->dw_loc_opc == DW_OP_fbreg)
12242             return NULL;
12243         }
12244       else
12245         gcc_unreachable ();
12246       if (op0 == NULL)
12247         return NULL;
12248       mem_loc_result = new_loc_descr (DW_OP_GNU_entry_value, 0, 0);
12249       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_loc;
12250       mem_loc_result->dw_loc_oprnd1.v.val_loc = op0;
12251       break;
12252
12253     case DEBUG_PARAMETER_REF:
12254       mem_loc_result = parameter_ref_descriptor (rtl);
12255       break;
12256
12257     case PRE_MODIFY:
12258       /* Extract the PLUS expression nested inside and fall into
12259          PLUS code below.  */
12260       rtl = XEXP (rtl, 1);
12261       goto plus;
12262
12263     case PRE_INC:
12264     case PRE_DEC:
12265       /* Turn these into a PLUS expression and fall into the PLUS code
12266          below.  */
12267       rtl = gen_rtx_PLUS (mode, XEXP (rtl, 0),
12268                           GEN_INT (GET_CODE (rtl) == PRE_INC
12269                                    ? GET_MODE_UNIT_SIZE (mem_mode)
12270                                    : -GET_MODE_UNIT_SIZE (mem_mode)));
12271
12272       /* ... fall through ...  */
12273
12274     case PLUS:
12275     plus:
12276       if (is_based_loc (rtl)
12277           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12278           && GET_MODE_CLASS (mode) == MODE_INT)
12279         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
12280                                           INTVAL (XEXP (rtl, 1)),
12281                                           VAR_INIT_STATUS_INITIALIZED);
12282       else
12283         {
12284           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12285                                                VAR_INIT_STATUS_INITIALIZED);
12286           if (mem_loc_result == 0)
12287             break;
12288
12289           if (CONST_INT_P (XEXP (rtl, 1))
12290               && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE)
12291             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
12292           else
12293             {
12294               op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12295                                         VAR_INIT_STATUS_INITIALIZED);
12296               if (op1 == 0)
12297                 break;
12298               add_loc_descr (&mem_loc_result, op1);
12299               add_loc_descr (&mem_loc_result,
12300                              new_loc_descr (DW_OP_plus, 0, 0));
12301             }
12302         }
12303       break;
12304
12305     /* If a pseudo-reg is optimized away, it is possible for it to
12306        be replaced with a MEM containing a multiply or shift.  */
12307     case MINUS:
12308       op = DW_OP_minus;
12309       goto do_binop;
12310
12311     case MULT:
12312       op = DW_OP_mul;
12313       goto do_binop;
12314
12315     case DIV:
12316       if (!dwarf_strict
12317           && GET_MODE_CLASS (mode) == MODE_INT
12318           && GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE)
12319         {
12320           mem_loc_result = typed_binop (DW_OP_div, rtl,
12321                                         base_type_for_mode (mode, 0),
12322                                         mode, mem_mode);
12323           break;
12324         }
12325       op = DW_OP_div;
12326       goto do_binop;
12327
12328     case UMOD:
12329       op = DW_OP_mod;
12330       goto do_binop;
12331
12332     case ASHIFT:
12333       op = DW_OP_shl;
12334       goto do_shift;
12335
12336     case ASHIFTRT:
12337       op = DW_OP_shra;
12338       goto do_shift;
12339
12340     case LSHIFTRT:
12341       op = DW_OP_shr;
12342       goto do_shift;
12343
12344     do_shift:
12345       if (GET_MODE_CLASS (mode) != MODE_INT)
12346         break;
12347       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12348                                 VAR_INIT_STATUS_INITIALIZED);
12349       {
12350         rtx rtlop1 = XEXP (rtl, 1);
12351         if (GET_MODE (rtlop1) != VOIDmode
12352             && GET_MODE_BITSIZE (GET_MODE (rtlop1))
12353                < GET_MODE_BITSIZE (mode))
12354           rtlop1 = gen_rtx_ZERO_EXTEND (mode, rtlop1);
12355         op1 = mem_loc_descriptor (rtlop1, mode, mem_mode,
12356                                   VAR_INIT_STATUS_INITIALIZED);
12357       }
12358
12359       if (op0 == 0 || op1 == 0)
12360         break;
12361
12362       mem_loc_result = op0;
12363       add_loc_descr (&mem_loc_result, op1);
12364       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12365       break;
12366
12367     case AND:
12368       op = DW_OP_and;
12369       goto do_binop;
12370
12371     case IOR:
12372       op = DW_OP_or;
12373       goto do_binop;
12374
12375     case XOR:
12376       op = DW_OP_xor;
12377       goto do_binop;
12378
12379     do_binop:
12380       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12381                                 VAR_INIT_STATUS_INITIALIZED);
12382       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12383                                 VAR_INIT_STATUS_INITIALIZED);
12384
12385       if (op0 == 0 || op1 == 0)
12386         break;
12387
12388       mem_loc_result = op0;
12389       add_loc_descr (&mem_loc_result, op1);
12390       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12391       break;
12392
12393     case MOD:
12394       if (GET_MODE_SIZE (mode) > DWARF2_ADDR_SIZE && !dwarf_strict)
12395         {
12396           mem_loc_result = typed_binop (DW_OP_mod, rtl,
12397                                         base_type_for_mode (mode, 0),
12398                                         mode, mem_mode);
12399           break;
12400         }
12401
12402       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12403                                 VAR_INIT_STATUS_INITIALIZED);
12404       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12405                                 VAR_INIT_STATUS_INITIALIZED);
12406
12407       if (op0 == 0 || op1 == 0)
12408         break;
12409
12410       mem_loc_result = op0;
12411       add_loc_descr (&mem_loc_result, op1);
12412       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12413       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
12414       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
12415       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
12416       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
12417       break;
12418
12419     case UDIV:
12420       if (!dwarf_strict && GET_MODE_CLASS (mode) == MODE_INT)
12421         {
12422           if (GET_MODE_CLASS (mode) > DWARF2_ADDR_SIZE)
12423             {
12424               op = DW_OP_div;
12425               goto do_binop;
12426             }
12427           mem_loc_result = typed_binop (DW_OP_div, rtl,
12428                                         base_type_for_mode (mode, 1),
12429                                         mode, mem_mode);
12430         }
12431       break;
12432
12433     case NOT:
12434       op = DW_OP_not;
12435       goto do_unop;
12436
12437     case ABS:
12438       op = DW_OP_abs;
12439       goto do_unop;
12440
12441     case NEG:
12442       op = DW_OP_neg;
12443       goto do_unop;
12444
12445     do_unop:
12446       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode, mem_mode,
12447                                 VAR_INIT_STATUS_INITIALIZED);
12448
12449       if (op0 == 0)
12450         break;
12451
12452       mem_loc_result = op0;
12453       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12454       break;
12455
12456     case CONST_INT:
12457       if (GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12458 #ifdef POINTERS_EXTEND_UNSIGNED
12459           || (mode == Pmode
12460               && mem_mode != VOIDmode
12461               && trunc_int_for_mode (INTVAL (rtl), ptr_mode) == INTVAL (rtl))
12462 #endif
12463           )
12464         {
12465           mem_loc_result = int_loc_descriptor (INTVAL (rtl));
12466           break;
12467         }
12468       if (!dwarf_strict
12469           && (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT
12470               || GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT))
12471         {
12472           dw_die_ref type_die = base_type_for_mode (mode, 1);
12473           if (type_die == NULL)
12474             return NULL;
12475           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0,
12476                                           INTVAL (rtl));
12477           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12478           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12479           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12480           if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
12481             mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
12482           else
12483             {
12484               mem_loc_result->dw_loc_oprnd2.val_class
12485                 = dw_val_class_const_double;
12486               mem_loc_result->dw_loc_oprnd2.v.val_double
12487                 = shwi_to_double_int (INTVAL (rtl));
12488             }
12489         }
12490       break;
12491
12492     case CONST_DOUBLE:
12493       if (!dwarf_strict)
12494         {
12495           dw_die_ref type_die;
12496
12497           /* Note that a CONST_DOUBLE rtx could represent either an integer
12498              or a floating-point constant.  A CONST_DOUBLE is used whenever
12499              the constant requires more than one word in order to be
12500              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12501           if (mode == VOIDmode
12502               || (GET_MODE (rtl) == VOIDmode
12503                   && GET_MODE_BITSIZE (mode) != 2 * HOST_BITS_PER_WIDE_INT))
12504             break;
12505           type_die = base_type_for_mode (mode,
12506                                          GET_MODE_CLASS (mode) == MODE_INT);
12507           if (type_die == NULL)
12508             return NULL;
12509           mem_loc_result = new_loc_descr (DW_OP_GNU_const_type, 0, 0);
12510           mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12511           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12512           mem_loc_result->dw_loc_oprnd1.v.val_die_ref.external = 0;
12513           if (SCALAR_FLOAT_MODE_P (mode))
12514             {
12515               unsigned int length = GET_MODE_SIZE (mode);
12516               unsigned char *array
12517                   = (unsigned char*) ggc_alloc_atomic (length);
12518
12519               insert_float (rtl, array);
12520               mem_loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12521               mem_loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
12522               mem_loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
12523               mem_loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12524             }
12525           else
12526             {
12527               mem_loc_result->dw_loc_oprnd2.val_class
12528                 = dw_val_class_const_double;
12529               mem_loc_result->dw_loc_oprnd2.v.val_double
12530                 = rtx_to_double_int (rtl);
12531             }
12532         }
12533       break;
12534
12535     case EQ:
12536       mem_loc_result = scompare_loc_descriptor (DW_OP_eq, rtl, mem_mode);
12537       break;
12538
12539     case GE:
12540       mem_loc_result = scompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12541       break;
12542
12543     case GT:
12544       mem_loc_result = scompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12545       break;
12546
12547     case LE:
12548       mem_loc_result = scompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12549       break;
12550
12551     case LT:
12552       mem_loc_result = scompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12553       break;
12554
12555     case NE:
12556       mem_loc_result = scompare_loc_descriptor (DW_OP_ne, rtl, mem_mode);
12557       break;
12558
12559     case GEU:
12560       mem_loc_result = ucompare_loc_descriptor (DW_OP_ge, rtl, mem_mode);
12561       break;
12562
12563     case GTU:
12564       mem_loc_result = ucompare_loc_descriptor (DW_OP_gt, rtl, mem_mode);
12565       break;
12566
12567     case LEU:
12568       mem_loc_result = ucompare_loc_descriptor (DW_OP_le, rtl, mem_mode);
12569       break;
12570
12571     case LTU:
12572       mem_loc_result = ucompare_loc_descriptor (DW_OP_lt, rtl, mem_mode);
12573       break;
12574
12575     case UMIN:
12576     case UMAX:
12577       if (GET_MODE_CLASS (mode) != MODE_INT)
12578         break;
12579       /* FALLTHRU */
12580     case SMIN:
12581     case SMAX:
12582       mem_loc_result = minmax_loc_descriptor (rtl, mode, mem_mode);
12583       break;
12584
12585     case ZERO_EXTRACT:
12586     case SIGN_EXTRACT:
12587       if (CONST_INT_P (XEXP (rtl, 1))
12588           && CONST_INT_P (XEXP (rtl, 2))
12589           && ((unsigned) INTVAL (XEXP (rtl, 1))
12590               + (unsigned) INTVAL (XEXP (rtl, 2))
12591               <= GET_MODE_BITSIZE (mode))
12592           && GET_MODE_CLASS (mode) == MODE_INT
12593           && GET_MODE_SIZE (mode) <= DWARF2_ADDR_SIZE
12594           && GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
12595         {
12596           int shift, size;
12597           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12598                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12599           if (op0 == 0)
12600             break;
12601           if (GET_CODE (rtl) == SIGN_EXTRACT)
12602             op = DW_OP_shra;
12603           else
12604             op = DW_OP_shr;
12605           mem_loc_result = op0;
12606           size = INTVAL (XEXP (rtl, 1));
12607           shift = INTVAL (XEXP (rtl, 2));
12608           if (BITS_BIG_ENDIAN)
12609             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
12610                     - shift - size;
12611           if (shift + size != (int) DWARF2_ADDR_SIZE)
12612             {
12613               add_loc_descr (&mem_loc_result,
12614                              int_loc_descriptor (DWARF2_ADDR_SIZE
12615                                                  - shift - size));
12616               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
12617             }
12618           if (size != (int) DWARF2_ADDR_SIZE)
12619             {
12620               add_loc_descr (&mem_loc_result,
12621                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
12622               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
12623             }
12624         }
12625       break;
12626
12627     case IF_THEN_ELSE:
12628       {
12629         dw_loc_descr_ref op2, bra_node, drop_node;
12630         op0 = mem_loc_descriptor (XEXP (rtl, 0),
12631                                   GET_MODE (XEXP (rtl, 0)) == VOIDmode
12632                                   ? word_mode : GET_MODE (XEXP (rtl, 0)),
12633                                   mem_mode, VAR_INIT_STATUS_INITIALIZED);
12634         op1 = mem_loc_descriptor (XEXP (rtl, 1), mode, mem_mode,
12635                                   VAR_INIT_STATUS_INITIALIZED);
12636         op2 = mem_loc_descriptor (XEXP (rtl, 2), mode, mem_mode,
12637                                   VAR_INIT_STATUS_INITIALIZED);
12638         if (op0 == NULL || op1 == NULL || op2 == NULL)
12639           break;
12640
12641         mem_loc_result = op1;
12642         add_loc_descr (&mem_loc_result, op2);
12643         add_loc_descr (&mem_loc_result, op0);
12644         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12645         add_loc_descr (&mem_loc_result, bra_node);
12646         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
12647         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
12648         add_loc_descr (&mem_loc_result, drop_node);
12649         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12650         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
12651       }
12652       break;
12653
12654     case FLOAT_EXTEND:
12655     case FLOAT_TRUNCATE:
12656     case FLOAT:
12657     case UNSIGNED_FLOAT:
12658     case FIX:
12659     case UNSIGNED_FIX:
12660       if (!dwarf_strict)
12661         {
12662           dw_die_ref type_die;
12663           dw_loc_descr_ref cvt;
12664
12665           op0 = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (XEXP (rtl, 0)),
12666                                     mem_mode, VAR_INIT_STATUS_INITIALIZED);
12667           if (op0 == NULL)
12668             break;
12669           if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) == MODE_INT
12670               && (GET_CODE (rtl) == FLOAT
12671                   || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)))
12672                      <= DWARF2_ADDR_SIZE))
12673             {
12674               type_die = base_type_for_mode (GET_MODE (XEXP (rtl, 0)),
12675                                              GET_CODE (rtl) == UNSIGNED_FLOAT);
12676               if (type_die == NULL)
12677                 break;
12678               cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12679               cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12680               cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12681               cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12682               add_loc_descr (&op0, cvt);
12683             }
12684           type_die = base_type_for_mode (mode, GET_CODE (rtl) == UNSIGNED_FIX);
12685           if (type_die == NULL)
12686             break;
12687           cvt = new_loc_descr (DW_OP_GNU_convert, 0, 0);
12688           cvt->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12689           cvt->dw_loc_oprnd1.v.val_die_ref.die = type_die;
12690           cvt->dw_loc_oprnd1.v.val_die_ref.external = 0;
12691           add_loc_descr (&op0, cvt);
12692           if (GET_MODE_CLASS (mode) == MODE_INT
12693               && (GET_CODE (rtl) == FIX
12694                   || GET_MODE_SIZE (mode) < DWARF2_ADDR_SIZE))
12695             {
12696               op0 = convert_descriptor_to_mode (mode, op0);
12697               if (op0 == NULL)
12698                 break;
12699             }
12700           mem_loc_result = op0;
12701         }
12702       break;
12703
12704     case CLZ:
12705     case CTZ:
12706     case FFS:
12707       mem_loc_result = clz_loc_descriptor (rtl, mode, mem_mode);
12708       break;
12709
12710     case POPCOUNT:
12711     case PARITY:
12712       mem_loc_result = popcount_loc_descriptor (rtl, mode, mem_mode);
12713       break;
12714
12715     case BSWAP:
12716       mem_loc_result = bswap_loc_descriptor (rtl, mode, mem_mode);
12717       break;
12718
12719     case ROTATE:
12720     case ROTATERT:
12721       mem_loc_result = rotate_loc_descriptor (rtl, mode, mem_mode);
12722       break;
12723
12724     case COMPARE:
12725     case TRUNCATE:
12726       /* In theory, we could implement the above.  */
12727       /* DWARF cannot represent the unsigned compare operations
12728          natively.  */
12729     case SS_MULT:
12730     case US_MULT:
12731     case SS_DIV:
12732     case US_DIV:
12733     case SS_PLUS:
12734     case US_PLUS:
12735     case SS_MINUS:
12736     case US_MINUS:
12737     case SS_NEG:
12738     case US_NEG:
12739     case SS_ABS:
12740     case SS_ASHIFT:
12741     case US_ASHIFT:
12742     case SS_TRUNCATE:
12743     case US_TRUNCATE:
12744     case UNORDERED:
12745     case ORDERED:
12746     case UNEQ:
12747     case UNGE:
12748     case UNGT:
12749     case UNLE:
12750     case UNLT:
12751     case LTGT:
12752     case FRACT_CONVERT:
12753     case UNSIGNED_FRACT_CONVERT:
12754     case SAT_FRACT:
12755     case UNSIGNED_SAT_FRACT:
12756     case SQRT:
12757     case ASM_OPERANDS:
12758     case VEC_MERGE:
12759     case VEC_SELECT:
12760     case VEC_CONCAT:
12761     case VEC_DUPLICATE:
12762     case UNSPEC:
12763     case HIGH:
12764     case FMA:
12765     case STRICT_LOW_PART:
12766     case CONST_VECTOR:
12767     case CONST_FIXED:
12768     case CLRSB:
12769       /* If delegitimize_address couldn't do anything with the UNSPEC, we
12770          can't express it in the debug info.  This can happen e.g. with some
12771          TLS UNSPECs.  */
12772       break;
12773
12774     case CONST_STRING:
12775       resolve_one_addr (&rtl, NULL);
12776       goto symref;
12777
12778     default:
12779 #ifdef ENABLE_CHECKING
12780       print_rtl (stderr, rtl);
12781       gcc_unreachable ();
12782 #else
12783       break;
12784 #endif
12785     }
12786
12787   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12788     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12789
12790   return mem_loc_result;
12791 }
12792
12793 /* Return a descriptor that describes the concatenation of two locations.
12794    This is typically a complex variable.  */
12795
12796 static dw_loc_descr_ref
12797 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
12798 {
12799   dw_loc_descr_ref cc_loc_result = NULL;
12800   dw_loc_descr_ref x0_ref
12801     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12802   dw_loc_descr_ref x1_ref
12803     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12804
12805   if (x0_ref == 0 || x1_ref == 0)
12806     return 0;
12807
12808   cc_loc_result = x0_ref;
12809   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
12810
12811   add_loc_descr (&cc_loc_result, x1_ref);
12812   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
12813
12814   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
12815     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12816
12817   return cc_loc_result;
12818 }
12819
12820 /* Return a descriptor that describes the concatenation of N
12821    locations.  */
12822
12823 static dw_loc_descr_ref
12824 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
12825 {
12826   unsigned int i;
12827   dw_loc_descr_ref cc_loc_result = NULL;
12828   unsigned int n = XVECLEN (concatn, 0);
12829
12830   for (i = 0; i < n; ++i)
12831     {
12832       dw_loc_descr_ref ref;
12833       rtx x = XVECEXP (concatn, 0, i);
12834
12835       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
12836       if (ref == NULL)
12837         return NULL;
12838
12839       add_loc_descr (&cc_loc_result, ref);
12840       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
12841     }
12842
12843   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
12844     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
12845
12846   return cc_loc_result;
12847 }
12848
12849 /* Helper function for loc_descriptor.  Return DW_OP_GNU_implicit_pointer
12850    for DEBUG_IMPLICIT_PTR RTL.  */
12851
12852 static dw_loc_descr_ref
12853 implicit_ptr_descriptor (rtx rtl, HOST_WIDE_INT offset)
12854 {
12855   dw_loc_descr_ref ret;
12856   dw_die_ref ref;
12857
12858   if (dwarf_strict)
12859     return NULL;
12860   gcc_assert (TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == VAR_DECL
12861               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == PARM_DECL
12862               || TREE_CODE (DEBUG_IMPLICIT_PTR_DECL (rtl)) == RESULT_DECL);
12863   ref = lookup_decl_die (DEBUG_IMPLICIT_PTR_DECL (rtl));
12864   ret = new_loc_descr (DW_OP_GNU_implicit_pointer, 0, offset);
12865   ret->dw_loc_oprnd2.val_class = dw_val_class_const;
12866   if (ref)
12867     {
12868       ret->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
12869       ret->dw_loc_oprnd1.v.val_die_ref.die = ref;
12870       ret->dw_loc_oprnd1.v.val_die_ref.external = 0;
12871     }
12872   else
12873     {
12874       ret->dw_loc_oprnd1.val_class = dw_val_class_decl_ref;
12875       ret->dw_loc_oprnd1.v.val_decl_ref = DEBUG_IMPLICIT_PTR_DECL (rtl);
12876     }
12877   return ret;
12878 }
12879
12880 /* Output a proper Dwarf location descriptor for a variable or parameter
12881    which is either allocated in a register or in a memory location.  For a
12882    register, we just generate an OP_REG and the register number.  For a
12883    memory location we provide a Dwarf postfix expression describing how to
12884    generate the (dynamic) address of the object onto the address stack.
12885
12886    MODE is mode of the decl if this loc_descriptor is going to be used in
12887    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
12888    allowed, VOIDmode otherwise.
12889
12890    If we don't know how to describe it, return 0.  */
12891
12892 static dw_loc_descr_ref
12893 loc_descriptor (rtx rtl, enum machine_mode mode,
12894                 enum var_init_status initialized)
12895 {
12896   dw_loc_descr_ref loc_result = NULL;
12897
12898   switch (GET_CODE (rtl))
12899     {
12900     case SUBREG:
12901       /* The case of a subreg may arise when we have a local (register)
12902          variable or a formal (register) parameter which doesn't quite fill
12903          up an entire register.  For now, just assume that it is
12904          legitimate to make the Dwarf info refer to the whole register which
12905          contains the given subreg.  */
12906       if (REG_P (SUBREG_REG (rtl)) && subreg_lowpart_p (rtl))
12907         loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
12908       else
12909         goto do_default;
12910       break;
12911
12912     case REG:
12913       loc_result = reg_loc_descriptor (rtl, initialized);
12914       break;
12915
12916     case MEM:
12917       loc_result = mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
12918                                        GET_MODE (rtl), initialized);
12919       if (loc_result == NULL)
12920         loc_result = tls_mem_loc_descriptor (rtl);
12921       if (loc_result == NULL)
12922         {
12923           rtx new_rtl = avoid_constant_pool_reference (rtl);
12924           if (new_rtl != rtl)
12925             loc_result = loc_descriptor (new_rtl, mode, initialized);
12926         }
12927       break;
12928
12929     case CONCAT:
12930       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
12931                                           initialized);
12932       break;
12933
12934     case CONCATN:
12935       loc_result = concatn_loc_descriptor (rtl, initialized);
12936       break;
12937
12938     case VAR_LOCATION:
12939       /* Single part.  */
12940       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
12941         {
12942           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
12943           if (GET_CODE (loc) == EXPR_LIST)
12944             loc = XEXP (loc, 0);
12945           loc_result = loc_descriptor (loc, mode, initialized);
12946           break;
12947         }
12948
12949       rtl = XEXP (rtl, 1);
12950       /* FALLTHRU */
12951
12952     case PARALLEL:
12953       {
12954         rtvec par_elems = XVEC (rtl, 0);
12955         int num_elem = GET_NUM_ELEM (par_elems);
12956         enum machine_mode mode;
12957         int i;
12958
12959         /* Create the first one, so we have something to add to.  */
12960         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
12961                                      VOIDmode, initialized);
12962         if (loc_result == NULL)
12963           return NULL;
12964         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
12965         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12966         for (i = 1; i < num_elem; i++)
12967           {
12968             dw_loc_descr_ref temp;
12969
12970             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
12971                                    VOIDmode, initialized);
12972             if (temp == NULL)
12973               return NULL;
12974             add_loc_descr (&loc_result, temp);
12975             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
12976             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
12977           }
12978       }
12979       break;
12980
12981     case CONST_INT:
12982       if (mode != VOIDmode && mode != BLKmode)
12983         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
12984                                                     INTVAL (rtl));
12985       break;
12986
12987     case CONST_DOUBLE:
12988       if (mode == VOIDmode)
12989         mode = GET_MODE (rtl);
12990
12991       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
12992         {
12993           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
12994
12995           /* Note that a CONST_DOUBLE rtx could represent either an integer
12996              or a floating-point constant.  A CONST_DOUBLE is used whenever
12997              the constant requires more than one word in order to be
12998              adequately represented.  We output CONST_DOUBLEs as blocks.  */
12999           loc_result = new_loc_descr (DW_OP_implicit_value,
13000                                       GET_MODE_SIZE (mode), 0);
13001           if (SCALAR_FLOAT_MODE_P (mode))
13002             {
13003               unsigned int length = GET_MODE_SIZE (mode);
13004               unsigned char *array
13005                   = (unsigned char*) ggc_alloc_atomic (length);
13006
13007               insert_float (rtl, array);
13008               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13009               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
13010               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
13011               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13012             }
13013           else
13014             {
13015               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
13016               loc_result->dw_loc_oprnd2.v.val_double
13017                 = rtx_to_double_int (rtl);
13018             }
13019         }
13020       break;
13021
13022     case CONST_VECTOR:
13023       if (mode == VOIDmode)
13024         mode = GET_MODE (rtl);
13025
13026       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
13027         {
13028           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
13029           unsigned int length = CONST_VECTOR_NUNITS (rtl);
13030           unsigned char *array = (unsigned char *)
13031             ggc_alloc_atomic (length * elt_size);
13032           unsigned int i;
13033           unsigned char *p;
13034
13035           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
13036           switch (GET_MODE_CLASS (mode))
13037             {
13038             case MODE_VECTOR_INT:
13039               for (i = 0, p = array; i < length; i++, p += elt_size)
13040                 {
13041                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13042                   double_int val = rtx_to_double_int (elt);
13043
13044                   if (elt_size <= sizeof (HOST_WIDE_INT))
13045                     insert_int (double_int_to_shwi (val), elt_size, p);
13046                   else
13047                     {
13048                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13049                       insert_double (val, p);
13050                     }
13051                 }
13052               break;
13053
13054             case MODE_VECTOR_FLOAT:
13055               for (i = 0, p = array; i < length; i++, p += elt_size)
13056                 {
13057                   rtx elt = CONST_VECTOR_ELT (rtl, i);
13058                   insert_float (elt, p);
13059                 }
13060               break;
13061
13062             default:
13063               gcc_unreachable ();
13064             }
13065
13066           loc_result = new_loc_descr (DW_OP_implicit_value,
13067                                       length * elt_size, 0);
13068           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
13069           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
13070           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
13071           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
13072         }
13073       break;
13074
13075     case CONST:
13076       if (mode == VOIDmode
13077           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
13078           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
13079           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
13080         {
13081           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
13082           break;
13083         }
13084       /* FALLTHROUGH */
13085     case SYMBOL_REF:
13086       if (!const_ok_for_output (rtl))
13087         break;
13088     case LABEL_REF:
13089       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
13090           && (dwarf_version >= 4 || !dwarf_strict))
13091         {
13092           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13093           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13094           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13095           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
13096           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13097         }
13098       break;
13099
13100     case DEBUG_IMPLICIT_PTR:
13101       loc_result = implicit_ptr_descriptor (rtl, 0);
13102       break;
13103
13104     case PLUS:
13105       if (GET_CODE (XEXP (rtl, 0)) == DEBUG_IMPLICIT_PTR
13106           && CONST_INT_P (XEXP (rtl, 1)))
13107         {
13108           loc_result
13109             = implicit_ptr_descriptor (XEXP (rtl, 0), INTVAL (XEXP (rtl, 1)));
13110           break;
13111         }
13112       /* FALLTHRU */
13113     do_default:
13114     default:
13115       if ((GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
13116            && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
13117            && dwarf_version >= 4)
13118           || (!dwarf_strict && mode != VOIDmode && mode != BLKmode))
13119         {
13120           /* Value expression.  */
13121           loc_result = mem_loc_descriptor (rtl, mode, VOIDmode, initialized);
13122           if (loc_result)
13123             add_loc_descr (&loc_result,
13124                            new_loc_descr (DW_OP_stack_value, 0, 0));
13125         }
13126       break;
13127     }
13128
13129   return loc_result;
13130 }
13131
13132 /* We need to figure out what section we should use as the base for the
13133    address ranges where a given location is valid.
13134    1. If this particular DECL has a section associated with it, use that.
13135    2. If this function has a section associated with it, use that.
13136    3. Otherwise, use the text section.
13137    XXX: If you split a variable across multiple sections, we won't notice.  */
13138
13139 static const char *
13140 secname_for_decl (const_tree decl)
13141 {
13142   const char *secname;
13143
13144   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13145     {
13146       tree sectree = DECL_SECTION_NAME (decl);
13147       secname = TREE_STRING_POINTER (sectree);
13148     }
13149   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13150     {
13151       tree sectree = DECL_SECTION_NAME (current_function_decl);
13152       secname = TREE_STRING_POINTER (sectree);
13153     }
13154   else if (cfun && in_cold_section_p)
13155     secname = crtl->subsections.cold_section_label;
13156   else
13157     secname = text_section_label;
13158
13159   return secname;
13160 }
13161
13162 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
13163
13164 static bool
13165 decl_by_reference_p (tree decl)
13166 {
13167   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
13168            || TREE_CODE (decl) == VAR_DECL)
13169           && DECL_BY_REFERENCE (decl));
13170 }
13171
13172 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13173    for VARLOC.  */
13174
13175 static dw_loc_descr_ref
13176 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
13177                enum var_init_status initialized)
13178 {
13179   int have_address = 0;
13180   dw_loc_descr_ref descr;
13181   enum machine_mode mode;
13182
13183   if (want_address != 2)
13184     {
13185       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
13186       /* Single part.  */
13187       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13188         {
13189           varloc = PAT_VAR_LOCATION_LOC (varloc);
13190           if (GET_CODE (varloc) == EXPR_LIST)
13191             varloc = XEXP (varloc, 0);
13192           mode = GET_MODE (varloc);
13193           if (MEM_P (varloc))
13194             {
13195               rtx addr = XEXP (varloc, 0);
13196               descr = mem_loc_descriptor (addr, get_address_mode (varloc),
13197                                           mode, initialized);
13198               if (descr)
13199                 have_address = 1;
13200               else
13201                 {
13202                   rtx x = avoid_constant_pool_reference (varloc);
13203                   if (x != varloc)
13204                     descr = mem_loc_descriptor (x, mode, VOIDmode,
13205                                                 initialized);
13206                 }
13207             }
13208           else
13209             descr = mem_loc_descriptor (varloc, mode, VOIDmode, initialized);
13210         }
13211       else
13212         return 0;
13213     }
13214   else
13215     {
13216       if (GET_CODE (varloc) == VAR_LOCATION)
13217         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
13218       else
13219         mode = DECL_MODE (loc);
13220       descr = loc_descriptor (varloc, mode, initialized);
13221       have_address = 1;
13222     }
13223
13224   if (!descr)
13225     return 0;
13226
13227   if (want_address == 2 && !have_address
13228       && (dwarf_version >= 4 || !dwarf_strict))
13229     {
13230       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13231         {
13232           expansion_failed (loc, NULL_RTX,
13233                             "DWARF address size mismatch");
13234           return 0;
13235         }
13236       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
13237       have_address = 1;
13238     }
13239   /* Show if we can't fill the request for an address.  */
13240   if (want_address && !have_address)
13241     {
13242       expansion_failed (loc, NULL_RTX,
13243                         "Want address and only have value");
13244       return 0;
13245     }
13246
13247   /* If we've got an address and don't want one, dereference.  */
13248   if (!want_address && have_address)
13249     {
13250       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13251       enum dwarf_location_atom op;
13252
13253       if (size > DWARF2_ADDR_SIZE || size == -1)
13254         {
13255           expansion_failed (loc, NULL_RTX,
13256                             "DWARF address size mismatch");
13257           return 0;
13258         }
13259       else if (size == DWARF2_ADDR_SIZE)
13260         op = DW_OP_deref;
13261       else
13262         op = DW_OP_deref_size;
13263
13264       add_loc_descr (&descr, new_loc_descr (op, size, 0));
13265     }
13266
13267   return descr;
13268 }
13269
13270 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
13271    if it is not possible.  */
13272
13273 static dw_loc_descr_ref
13274 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
13275 {
13276   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
13277     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
13278   else if (dwarf_version >= 3 || !dwarf_strict)
13279     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
13280   else
13281     return NULL;
13282 }
13283
13284 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
13285    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
13286
13287 static dw_loc_descr_ref
13288 dw_sra_loc_expr (tree decl, rtx loc)
13289 {
13290   rtx p;
13291   unsigned int padsize = 0;
13292   dw_loc_descr_ref descr, *descr_tail;
13293   unsigned HOST_WIDE_INT decl_size;
13294   rtx varloc;
13295   enum var_init_status initialized;
13296
13297   if (DECL_SIZE (decl) == NULL
13298       || !host_integerp (DECL_SIZE (decl), 1))
13299     return NULL;
13300
13301   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
13302   descr = NULL;
13303   descr_tail = &descr;
13304
13305   for (p = loc; p; p = XEXP (p, 1))
13306     {
13307       unsigned int bitsize = decl_piece_bitsize (p);
13308       rtx loc_note = *decl_piece_varloc_ptr (p);
13309       dw_loc_descr_ref cur_descr;
13310       dw_loc_descr_ref *tail, last = NULL;
13311       unsigned int opsize = 0;
13312
13313       if (loc_note == NULL_RTX
13314           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
13315         {
13316           padsize += bitsize;
13317           continue;
13318         }
13319       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
13320       varloc = NOTE_VAR_LOCATION (loc_note);
13321       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
13322       if (cur_descr == NULL)
13323         {
13324           padsize += bitsize;
13325           continue;
13326         }
13327
13328       /* Check that cur_descr either doesn't use
13329          DW_OP_*piece operations, or their sum is equal
13330          to bitsize.  Otherwise we can't embed it.  */
13331       for (tail = &cur_descr; *tail != NULL;
13332            tail = &(*tail)->dw_loc_next)
13333         if ((*tail)->dw_loc_opc == DW_OP_piece)
13334           {
13335             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
13336                       * BITS_PER_UNIT;
13337             last = *tail;
13338           }
13339         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
13340           {
13341             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
13342             last = *tail;
13343           }
13344
13345       if (last != NULL && opsize != bitsize)
13346         {
13347           padsize += bitsize;
13348           continue;
13349         }
13350
13351       /* If there is a hole, add DW_OP_*piece after empty DWARF
13352          expression, which means that those bits are optimized out.  */
13353       if (padsize)
13354         {
13355           if (padsize > decl_size)
13356             return NULL;
13357           decl_size -= padsize;
13358           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
13359           if (*descr_tail == NULL)
13360             return NULL;
13361           descr_tail = &(*descr_tail)->dw_loc_next;
13362           padsize = 0;
13363         }
13364       *descr_tail = cur_descr;
13365       descr_tail = tail;
13366       if (bitsize > decl_size)
13367         return NULL;
13368       decl_size -= bitsize;
13369       if (last == NULL)
13370         {
13371           HOST_WIDE_INT offset = 0;
13372           if (GET_CODE (varloc) == VAR_LOCATION
13373               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
13374             {
13375               varloc = PAT_VAR_LOCATION_LOC (varloc);
13376               if (GET_CODE (varloc) == EXPR_LIST)
13377                 varloc = XEXP (varloc, 0);
13378             }
13379           do 
13380             {
13381               if (GET_CODE (varloc) == CONST
13382                   || GET_CODE (varloc) == SIGN_EXTEND
13383                   || GET_CODE (varloc) == ZERO_EXTEND)
13384                 varloc = XEXP (varloc, 0);
13385               else if (GET_CODE (varloc) == SUBREG)
13386                 varloc = SUBREG_REG (varloc);
13387               else
13388                 break;
13389             }
13390           while (1);
13391           /* DW_OP_bit_size offset should be zero for register
13392              or implicit location descriptions and empty location
13393              descriptions, but for memory addresses needs big endian
13394              adjustment.  */
13395           if (MEM_P (varloc))
13396             {
13397               unsigned HOST_WIDE_INT memsize
13398                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
13399               if (memsize != bitsize)
13400                 {
13401                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
13402                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
13403                     return NULL;
13404                   if (memsize < bitsize)
13405                     return NULL;
13406                   if (BITS_BIG_ENDIAN)
13407                     offset = memsize - bitsize;
13408                 }
13409             }
13410
13411           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
13412           if (*descr_tail == NULL)
13413             return NULL;
13414           descr_tail = &(*descr_tail)->dw_loc_next;
13415         }
13416     }
13417
13418   /* If there were any non-empty expressions, add padding till the end of
13419      the decl.  */
13420   if (descr != NULL && decl_size != 0)
13421     {
13422       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
13423       if (*descr_tail == NULL)
13424         return NULL;
13425     }
13426   return descr;
13427 }
13428
13429 /* Return the dwarf representation of the location list LOC_LIST of
13430    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
13431    function.  */
13432
13433 static dw_loc_list_ref
13434 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
13435 {
13436   const char *endname, *secname;
13437   rtx varloc;
13438   enum var_init_status initialized;
13439   struct var_loc_node *node;
13440   dw_loc_descr_ref descr;
13441   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13442   dw_loc_list_ref list = NULL;
13443   dw_loc_list_ref *listp = &list;
13444
13445   /* Now that we know what section we are using for a base,
13446      actually construct the list of locations.
13447      The first location information is what is passed to the
13448      function that creates the location list, and the remaining
13449      locations just get added on to that list.
13450      Note that we only know the start address for a location
13451      (IE location changes), so to build the range, we use
13452      the range [current location start, next location start].
13453      This means we have to special case the last node, and generate
13454      a range of [last location start, end of function label].  */
13455
13456   secname = secname_for_decl (decl);
13457
13458   for (node = loc_list->first; node; node = node->next)
13459     if (GET_CODE (node->loc) == EXPR_LIST
13460         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
13461       {
13462         if (GET_CODE (node->loc) == EXPR_LIST)
13463           {
13464             /* This requires DW_OP_{,bit_}piece, which is not usable
13465                inside DWARF expressions.  */
13466             if (want_address != 2)
13467               continue;
13468             descr = dw_sra_loc_expr (decl, node->loc);
13469             if (descr == NULL)
13470               continue;
13471           }
13472         else
13473           {
13474             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13475             varloc = NOTE_VAR_LOCATION (node->loc);
13476             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
13477           }
13478         if (descr)
13479           {
13480             bool range_across_switch = false;
13481             /* If section switch happens in between node->label
13482                and node->next->label (or end of function) and
13483                we can't emit it as a single entry list,
13484                emit two ranges, first one ending at the end
13485                of first partition and second one starting at the
13486                beginning of second partition.  */
13487             if (node == loc_list->last_before_switch
13488                 && (node != loc_list->first || loc_list->first->next)
13489                 && current_function_decl)
13490               {
13491                 endname = current_fde ()->dw_fde_end;
13492                 range_across_switch = true;
13493               }
13494             /* The variable has a location between NODE->LABEL and
13495                NODE->NEXT->LABEL.  */
13496             else if (node->next)
13497               endname = node->next->label;
13498             /* If the variable has a location at the last label
13499                it keeps its location until the end of function.  */
13500             else if (!current_function_decl)
13501               endname = text_end_label;
13502             else
13503               {
13504                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13505                                              current_function_funcdef_no);
13506                 endname = ggc_strdup (label_id);
13507               }
13508
13509             *listp = new_loc_list (descr, node->label, endname, secname);
13510             if (TREE_CODE (decl) == PARM_DECL
13511                 && node == loc_list->first
13512                 && GET_CODE (node->loc) == NOTE
13513                 && strcmp (node->label, endname) == 0)
13514               (*listp)->force = true;
13515             listp = &(*listp)->dw_loc_next;
13516
13517             if (range_across_switch)
13518               {
13519                 if (GET_CODE (node->loc) == EXPR_LIST)
13520                   descr = dw_sra_loc_expr (decl, node->loc);
13521                 else
13522                   {
13523                     initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
13524                     varloc = NOTE_VAR_LOCATION (node->loc);
13525                     descr = dw_loc_list_1 (decl, varloc, want_address,
13526                                            initialized);
13527                   }
13528                 gcc_assert (descr);
13529                 /* The variable has a location between NODE->LABEL and
13530                    NODE->NEXT->LABEL.  */
13531                 if (node->next)
13532                   endname = node->next->label;
13533                 else
13534                   endname = current_fde ()->dw_fde_second_end;
13535                 *listp = new_loc_list (descr,
13536                                        current_fde ()->dw_fde_second_begin,
13537                                        endname, secname);
13538                 listp = &(*listp)->dw_loc_next;
13539               }
13540           }
13541       }
13542
13543   /* Try to avoid the overhead of a location list emitting a location
13544      expression instead, but only if we didn't have more than one
13545      location entry in the first place.  If some entries were not
13546      representable, we don't want to pretend a single entry that was
13547      applies to the entire scope in which the variable is
13548      available.  */
13549   if (list && loc_list->first->next)
13550     gen_llsym (list);
13551
13552   return list;
13553 }
13554
13555 /* Return if the loc_list has only single element and thus can be represented
13556    as location description.   */
13557
13558 static bool
13559 single_element_loc_list_p (dw_loc_list_ref list)
13560 {
13561   gcc_assert (!list->dw_loc_next || list->ll_symbol);
13562   return !list->ll_symbol;
13563 }
13564
13565 /* To each location in list LIST add loc descr REF.  */
13566
13567 static void
13568 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
13569 {
13570   dw_loc_descr_ref copy;
13571   add_loc_descr (&list->expr, ref);
13572   list = list->dw_loc_next;
13573   while (list)
13574     {
13575       copy = ggc_alloc_dw_loc_descr_node ();
13576       memcpy (copy, ref, sizeof (dw_loc_descr_node));
13577       add_loc_descr (&list->expr, copy);
13578       while (copy->dw_loc_next)
13579         {
13580           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
13581           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
13582           copy->dw_loc_next = new_copy;
13583           copy = new_copy;
13584         }
13585       list = list->dw_loc_next;
13586     }
13587 }
13588
13589 /* Given two lists RET and LIST
13590    produce location list that is result of adding expression in LIST
13591    to expression in RET on each possition in program.
13592    Might be destructive on both RET and LIST.
13593
13594    TODO: We handle only simple cases of RET or LIST having at most one
13595    element. General case would inolve sorting the lists in program order
13596    and merging them that will need some additional work.
13597    Adding that will improve quality of debug info especially for SRA-ed
13598    structures.  */
13599
13600 static void
13601 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
13602 {
13603   if (!list)
13604     return;
13605   if (!*ret)
13606     {
13607       *ret = list;
13608       return;
13609     }
13610   if (!list->dw_loc_next)
13611     {
13612       add_loc_descr_to_each (*ret, list->expr);
13613       return;
13614     }
13615   if (!(*ret)->dw_loc_next)
13616     {
13617       add_loc_descr_to_each (list, (*ret)->expr);
13618       *ret = list;
13619       return;
13620     }
13621   expansion_failed (NULL_TREE, NULL_RTX,
13622                     "Don't know how to merge two non-trivial"
13623                     " location lists.\n");
13624   *ret = NULL;
13625   return;
13626 }
13627
13628 /* LOC is constant expression.  Try a luck, look it up in constant
13629    pool and return its loc_descr of its address.  */
13630
13631 static dw_loc_descr_ref
13632 cst_pool_loc_descr (tree loc)
13633 {
13634   /* Get an RTL for this, if something has been emitted.  */
13635   rtx rtl = lookup_constant_def (loc);
13636
13637   if (!rtl || !MEM_P (rtl))
13638     {
13639       gcc_assert (!rtl);
13640       return 0;
13641     }
13642   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
13643
13644   /* TODO: We might get more coverage if we was actually delaying expansion
13645      of all expressions till end of compilation when constant pools are fully
13646      populated.  */
13647   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
13648     {
13649       expansion_failed (loc, NULL_RTX,
13650                         "CST value in contant pool but not marked.");
13651       return 0;
13652     }
13653   return mem_loc_descriptor (XEXP (rtl, 0), get_address_mode (rtl),
13654                              GET_MODE (rtl), VAR_INIT_STATUS_INITIALIZED);
13655 }
13656
13657 /* Return dw_loc_list representing address of addr_expr LOC
13658    by looking for innder INDIRECT_REF expression and turing it
13659    into simple arithmetics.  */
13660
13661 static dw_loc_list_ref
13662 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
13663 {
13664   tree obj, offset;
13665   HOST_WIDE_INT bitsize, bitpos, bytepos;
13666   enum machine_mode mode;
13667   int volatilep;
13668   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13669   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13670
13671   obj = get_inner_reference (TREE_OPERAND (loc, 0),
13672                              &bitsize, &bitpos, &offset, &mode,
13673                              &unsignedp, &volatilep, false);
13674   STRIP_NOPS (obj);
13675   if (bitpos % BITS_PER_UNIT)
13676     {
13677       expansion_failed (loc, NULL_RTX, "bitfield access");
13678       return 0;
13679     }
13680   if (!INDIRECT_REF_P (obj))
13681     {
13682       expansion_failed (obj,
13683                         NULL_RTX, "no indirect ref in inner refrence");
13684       return 0;
13685     }
13686   if (!offset && !bitpos)
13687     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
13688   else if (toplev
13689            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
13690            && (dwarf_version >= 4 || !dwarf_strict))
13691     {
13692       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
13693       if (!list_ret)
13694         return 0;
13695       if (offset)
13696         {
13697           /* Variable offset.  */
13698           list_ret1 = loc_list_from_tree (offset, 0);
13699           if (list_ret1 == 0)
13700             return 0;
13701           add_loc_list (&list_ret, list_ret1);
13702           if (!list_ret)
13703             return 0;
13704           add_loc_descr_to_each (list_ret,
13705                                  new_loc_descr (DW_OP_plus, 0, 0));
13706         }
13707       bytepos = bitpos / BITS_PER_UNIT;
13708       if (bytepos > 0)
13709         add_loc_descr_to_each (list_ret,
13710                                new_loc_descr (DW_OP_plus_uconst,
13711                                               bytepos, 0));
13712       else if (bytepos < 0)
13713         loc_list_plus_const (list_ret, bytepos);
13714       add_loc_descr_to_each (list_ret,
13715                              new_loc_descr (DW_OP_stack_value, 0, 0));
13716     }
13717   return list_ret;
13718 }
13719
13720
13721 /* Generate Dwarf location list representing LOC.
13722    If WANT_ADDRESS is false, expression computing LOC will be computed
13723    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
13724    if WANT_ADDRESS is 2, expression computing address useable in location
13725      will be returned (i.e. DW_OP_reg can be used
13726      to refer to register values).  */
13727
13728 static dw_loc_list_ref
13729 loc_list_from_tree (tree loc, int want_address)
13730 {
13731   dw_loc_descr_ref ret = NULL, ret1 = NULL;
13732   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
13733   int have_address = 0;
13734   enum dwarf_location_atom op;
13735
13736   /* ??? Most of the time we do not take proper care for sign/zero
13737      extending the values properly.  Hopefully this won't be a real
13738      problem...  */
13739
13740   switch (TREE_CODE (loc))
13741     {
13742     case ERROR_MARK:
13743       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
13744       return 0;
13745
13746     case PLACEHOLDER_EXPR:
13747       /* This case involves extracting fields from an object to determine the
13748          position of other fields.  We don't try to encode this here.  The
13749          only user of this is Ada, which encodes the needed information using
13750          the names of types.  */
13751       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
13752       return 0;
13753
13754     case CALL_EXPR:
13755       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
13756       /* There are no opcodes for these operations.  */
13757       return 0;
13758
13759     case PREINCREMENT_EXPR:
13760     case PREDECREMENT_EXPR:
13761     case POSTINCREMENT_EXPR:
13762     case POSTDECREMENT_EXPR:
13763       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
13764       /* There are no opcodes for these operations.  */
13765       return 0;
13766
13767     case ADDR_EXPR:
13768       /* If we already want an address, see if there is INDIRECT_REF inside
13769          e.g. for &this->field.  */
13770       if (want_address)
13771         {
13772           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
13773                        (loc, want_address == 2);
13774           if (list_ret)
13775             have_address = 1;
13776           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
13777                    && (ret = cst_pool_loc_descr (loc)))
13778             have_address = 1;
13779         }
13780         /* Otherwise, process the argument and look for the address.  */
13781       if (!list_ret && !ret)
13782         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
13783       else
13784         {
13785           if (want_address)
13786             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
13787           return NULL;
13788         }
13789       break;
13790
13791     case VAR_DECL:
13792       if (DECL_THREAD_LOCAL_P (loc))
13793         {
13794           rtx rtl;
13795           enum dwarf_location_atom first_op;
13796           enum dwarf_location_atom second_op;
13797           bool dtprel = false;
13798
13799           if (targetm.have_tls)
13800             {
13801               /* If this is not defined, we have no way to emit the
13802                  data.  */
13803               if (!targetm.asm_out.output_dwarf_dtprel)
13804                 return 0;
13805
13806                /* The way DW_OP_GNU_push_tls_address is specified, we
13807                   can only look up addresses of objects in the current
13808                   module.  We used DW_OP_addr as first op, but that's
13809                   wrong, because DW_OP_addr is relocated by the debug
13810                   info consumer, while DW_OP_GNU_push_tls_address
13811                   operand shouldn't be.  */
13812               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
13813                 return 0;
13814               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
13815               dtprel = true;
13816               second_op = DW_OP_GNU_push_tls_address;
13817             }
13818           else
13819             {
13820               if (!targetm.emutls.debug_form_tls_address
13821                   || !(dwarf_version >= 3 || !dwarf_strict))
13822                 return 0;
13823               /* We stuffed the control variable into the DECL_VALUE_EXPR
13824                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
13825                  no longer appear in gimple code.  We used the control
13826                  variable in specific so that we could pick it up here.  */
13827               loc = DECL_VALUE_EXPR (loc);
13828               first_op = DW_OP_addr;
13829               second_op = DW_OP_form_tls_address;
13830             }
13831
13832           rtl = rtl_for_decl_location (loc);
13833           if (rtl == NULL_RTX)
13834             return 0;
13835
13836           if (!MEM_P (rtl))
13837             return 0;
13838           rtl = XEXP (rtl, 0);
13839           if (! CONSTANT_P (rtl))
13840             return 0;
13841
13842           ret = new_loc_descr (first_op, 0, 0);
13843           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13844           ret->dw_loc_oprnd1.v.val_addr = rtl;
13845           ret->dtprel = dtprel;
13846
13847           ret1 = new_loc_descr (second_op, 0, 0);
13848           add_loc_descr (&ret, ret1);
13849
13850           have_address = 1;
13851           break;
13852         }
13853       /* FALLTHRU */
13854
13855     case PARM_DECL:
13856     case RESULT_DECL:
13857       if (DECL_HAS_VALUE_EXPR_P (loc))
13858         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
13859                                    want_address);
13860       /* FALLTHRU */
13861
13862     case FUNCTION_DECL:
13863       {
13864         rtx rtl;
13865         var_loc_list *loc_list = lookup_decl_loc (loc);
13866
13867         if (loc_list && loc_list->first)
13868           {
13869             list_ret = dw_loc_list (loc_list, loc, want_address);
13870             have_address = want_address != 0;
13871             break;
13872           }
13873         rtl = rtl_for_decl_location (loc);
13874         if (rtl == NULL_RTX)
13875           {
13876             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
13877             return 0;
13878           }
13879         else if (CONST_INT_P (rtl))
13880           {
13881             HOST_WIDE_INT val = INTVAL (rtl);
13882             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
13883               val &= GET_MODE_MASK (DECL_MODE (loc));
13884             ret = int_loc_descriptor (val);
13885           }
13886         else if (GET_CODE (rtl) == CONST_STRING)
13887           {
13888             expansion_failed (loc, NULL_RTX, "CONST_STRING");
13889             return 0;
13890           }
13891         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
13892           {
13893             ret = new_loc_descr (DW_OP_addr, 0, 0);
13894             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
13895             ret->dw_loc_oprnd1.v.val_addr = rtl;
13896           }
13897         else
13898           {
13899             enum machine_mode mode, mem_mode;
13900
13901             /* Certain constructs can only be represented at top-level.  */
13902             if (want_address == 2)
13903               {
13904                 ret = loc_descriptor (rtl, VOIDmode,
13905                                       VAR_INIT_STATUS_INITIALIZED);
13906                 have_address = 1;
13907               }
13908             else
13909               {
13910                 mode = GET_MODE (rtl);
13911                 mem_mode = VOIDmode;
13912                 if (MEM_P (rtl))
13913                   {
13914                     mem_mode = mode;
13915                     mode = get_address_mode (rtl);
13916                     rtl = XEXP (rtl, 0);
13917                     have_address = 1;
13918                   }
13919                 ret = mem_loc_descriptor (rtl, mode, mem_mode,
13920                                           VAR_INIT_STATUS_INITIALIZED);
13921               }
13922             if (!ret)
13923               expansion_failed (loc, rtl,
13924                                 "failed to produce loc descriptor for rtl");
13925           }
13926       }
13927       break;
13928
13929     case MEM_REF:
13930       /* ??? FIXME.  */
13931       if (!integer_zerop (TREE_OPERAND (loc, 1)))
13932         return 0;
13933       /* Fallthru.  */
13934     case INDIRECT_REF:
13935       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
13936       have_address = 1;
13937       break;
13938
13939     case COMPOUND_EXPR:
13940       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
13941
13942     CASE_CONVERT:
13943     case VIEW_CONVERT_EXPR:
13944     case SAVE_EXPR:
13945     case MODIFY_EXPR:
13946       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
13947
13948     case COMPONENT_REF:
13949     case BIT_FIELD_REF:
13950     case ARRAY_REF:
13951     case ARRAY_RANGE_REF:
13952     case REALPART_EXPR:
13953     case IMAGPART_EXPR:
13954       {
13955         tree obj, offset;
13956         HOST_WIDE_INT bitsize, bitpos, bytepos;
13957         enum machine_mode mode;
13958         int volatilep;
13959         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
13960
13961         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
13962                                    &unsignedp, &volatilep, false);
13963
13964         gcc_assert (obj != loc);
13965
13966         list_ret = loc_list_from_tree (obj,
13967                                        want_address == 2
13968                                        && !bitpos && !offset ? 2 : 1);
13969         /* TODO: We can extract value of the small expression via shifting even
13970            for nonzero bitpos.  */
13971         if (list_ret == 0)
13972           return 0;
13973         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
13974           {
13975             expansion_failed (loc, NULL_RTX,
13976                               "bitfield access");
13977             return 0;
13978           }
13979
13980         if (offset != NULL_TREE)
13981           {
13982             /* Variable offset.  */
13983             list_ret1 = loc_list_from_tree (offset, 0);
13984             if (list_ret1 == 0)
13985               return 0;
13986             add_loc_list (&list_ret, list_ret1);
13987             if (!list_ret)
13988               return 0;
13989             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
13990           }
13991
13992         bytepos = bitpos / BITS_PER_UNIT;
13993         if (bytepos > 0)
13994           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
13995         else if (bytepos < 0)
13996           loc_list_plus_const (list_ret, bytepos);
13997
13998         have_address = 1;
13999         break;
14000       }
14001
14002     case INTEGER_CST:
14003       if ((want_address || !host_integerp (loc, 0))
14004           && (ret = cst_pool_loc_descr (loc)))
14005         have_address = 1;
14006       else if (want_address == 2
14007                && host_integerp (loc, 0)
14008                && (ret = address_of_int_loc_descriptor
14009                            (int_size_in_bytes (TREE_TYPE (loc)),
14010                             tree_low_cst (loc, 0))))
14011         have_address = 1;
14012       else if (host_integerp (loc, 0))
14013         ret = int_loc_descriptor (tree_low_cst (loc, 0));
14014       else
14015         {
14016           expansion_failed (loc, NULL_RTX,
14017                             "Integer operand is not host integer");
14018           return 0;
14019         }
14020       break;
14021
14022     case CONSTRUCTOR:
14023     case REAL_CST:
14024     case STRING_CST:
14025     case COMPLEX_CST:
14026       if ((ret = cst_pool_loc_descr (loc)))
14027         have_address = 1;
14028       else
14029       /* We can construct small constants here using int_loc_descriptor.  */
14030         expansion_failed (loc, NULL_RTX,
14031                           "constructor or constant not in constant pool");
14032       break;
14033
14034     case TRUTH_AND_EXPR:
14035     case TRUTH_ANDIF_EXPR:
14036     case BIT_AND_EXPR:
14037       op = DW_OP_and;
14038       goto do_binop;
14039
14040     case TRUTH_XOR_EXPR:
14041     case BIT_XOR_EXPR:
14042       op = DW_OP_xor;
14043       goto do_binop;
14044
14045     case TRUTH_OR_EXPR:
14046     case TRUTH_ORIF_EXPR:
14047     case BIT_IOR_EXPR:
14048       op = DW_OP_or;
14049       goto do_binop;
14050
14051     case FLOOR_DIV_EXPR:
14052     case CEIL_DIV_EXPR:
14053     case ROUND_DIV_EXPR:
14054     case TRUNC_DIV_EXPR:
14055       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14056         return 0;
14057       op = DW_OP_div;
14058       goto do_binop;
14059
14060     case MINUS_EXPR:
14061       op = DW_OP_minus;
14062       goto do_binop;
14063
14064     case FLOOR_MOD_EXPR:
14065     case CEIL_MOD_EXPR:
14066     case ROUND_MOD_EXPR:
14067     case TRUNC_MOD_EXPR:
14068       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
14069         {
14070           op = DW_OP_mod;
14071           goto do_binop;
14072         }
14073       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14074       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14075       if (list_ret == 0 || list_ret1 == 0)
14076         return 0;
14077
14078       add_loc_list (&list_ret, list_ret1);
14079       if (list_ret == 0)
14080         return 0;
14081       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14082       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
14083       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
14084       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
14085       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
14086       break;
14087
14088     case MULT_EXPR:
14089       op = DW_OP_mul;
14090       goto do_binop;
14091
14092     case LSHIFT_EXPR:
14093       op = DW_OP_shl;
14094       goto do_binop;
14095
14096     case RSHIFT_EXPR:
14097       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
14098       goto do_binop;
14099
14100     case POINTER_PLUS_EXPR:
14101     case PLUS_EXPR:
14102       if (host_integerp (TREE_OPERAND (loc, 1), 0))
14103         {
14104           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14105           if (list_ret == 0)
14106             return 0;
14107
14108           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
14109           break;
14110         }
14111
14112       op = DW_OP_plus;
14113       goto do_binop;
14114
14115     case LE_EXPR:
14116       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14117         return 0;
14118
14119       op = DW_OP_le;
14120       goto do_binop;
14121
14122     case GE_EXPR:
14123       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14124         return 0;
14125
14126       op = DW_OP_ge;
14127       goto do_binop;
14128
14129     case LT_EXPR:
14130       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14131         return 0;
14132
14133       op = DW_OP_lt;
14134       goto do_binop;
14135
14136     case GT_EXPR:
14137       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
14138         return 0;
14139
14140       op = DW_OP_gt;
14141       goto do_binop;
14142
14143     case EQ_EXPR:
14144       op = DW_OP_eq;
14145       goto do_binop;
14146
14147     case NE_EXPR:
14148       op = DW_OP_ne;
14149       goto do_binop;
14150
14151     do_binop:
14152       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14153       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
14154       if (list_ret == 0 || list_ret1 == 0)
14155         return 0;
14156
14157       add_loc_list (&list_ret, list_ret1);
14158       if (list_ret == 0)
14159         return 0;
14160       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14161       break;
14162
14163     case TRUTH_NOT_EXPR:
14164     case BIT_NOT_EXPR:
14165       op = DW_OP_not;
14166       goto do_unop;
14167
14168     case ABS_EXPR:
14169       op = DW_OP_abs;
14170       goto do_unop;
14171
14172     case NEGATE_EXPR:
14173       op = DW_OP_neg;
14174       goto do_unop;
14175
14176     do_unop:
14177       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14178       if (list_ret == 0)
14179         return 0;
14180
14181       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
14182       break;
14183
14184     case MIN_EXPR:
14185     case MAX_EXPR:
14186       {
14187         const enum tree_code code =
14188           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
14189
14190         loc = build3 (COND_EXPR, TREE_TYPE (loc),
14191                       build2 (code, integer_type_node,
14192                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
14193                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
14194       }
14195
14196       /* ... fall through ...  */
14197
14198     case COND_EXPR:
14199       {
14200         dw_loc_descr_ref lhs
14201           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
14202         dw_loc_list_ref rhs
14203           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
14204         dw_loc_descr_ref bra_node, jump_node, tmp;
14205
14206         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
14207         if (list_ret == 0 || lhs == 0 || rhs == 0)
14208           return 0;
14209
14210         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14211         add_loc_descr_to_each (list_ret, bra_node);
14212
14213         add_loc_list (&list_ret, rhs);
14214         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
14215         add_loc_descr_to_each (list_ret, jump_node);
14216
14217         add_loc_descr_to_each (list_ret, lhs);
14218         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14219         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
14220
14221         /* ??? Need a node to point the skip at.  Use a nop.  */
14222         tmp = new_loc_descr (DW_OP_nop, 0, 0);
14223         add_loc_descr_to_each (list_ret, tmp);
14224         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14225         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
14226       }
14227       break;
14228
14229     case FIX_TRUNC_EXPR:
14230       return 0;
14231
14232     default:
14233       /* Leave front-end specific codes as simply unknown.  This comes
14234          up, for instance, with the C STMT_EXPR.  */
14235       if ((unsigned int) TREE_CODE (loc)
14236           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
14237         {
14238           expansion_failed (loc, NULL_RTX,
14239                             "language specific tree node");
14240           return 0;
14241         }
14242
14243 #ifdef ENABLE_CHECKING
14244       /* Otherwise this is a generic code; we should just lists all of
14245          these explicitly.  We forgot one.  */
14246       gcc_unreachable ();
14247 #else
14248       /* In a release build, we want to degrade gracefully: better to
14249          generate incomplete debugging information than to crash.  */
14250       return NULL;
14251 #endif
14252     }
14253
14254   if (!ret && !list_ret)
14255     return 0;
14256
14257   if (want_address == 2 && !have_address
14258       && (dwarf_version >= 4 || !dwarf_strict))
14259     {
14260       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14261         {
14262           expansion_failed (loc, NULL_RTX,
14263                             "DWARF address size mismatch");
14264           return 0;
14265         }
14266       if (ret)
14267         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
14268       else
14269         add_loc_descr_to_each (list_ret,
14270                                new_loc_descr (DW_OP_stack_value, 0, 0));
14271       have_address = 1;
14272     }
14273   /* Show if we can't fill the request for an address.  */
14274   if (want_address && !have_address)
14275     {
14276       expansion_failed (loc, NULL_RTX,
14277                         "Want address and only have value");
14278       return 0;
14279     }
14280
14281   gcc_assert (!ret || !list_ret);
14282
14283   /* If we've got an address and don't want one, dereference.  */
14284   if (!want_address && have_address)
14285     {
14286       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14287
14288       if (size > DWARF2_ADDR_SIZE || size == -1)
14289         {
14290           expansion_failed (loc, NULL_RTX,
14291                             "DWARF address size mismatch");
14292           return 0;
14293         }
14294       else if (size == DWARF2_ADDR_SIZE)
14295         op = DW_OP_deref;
14296       else
14297         op = DW_OP_deref_size;
14298
14299       if (ret)
14300         add_loc_descr (&ret, new_loc_descr (op, size, 0));
14301       else
14302         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
14303     }
14304   if (ret)
14305     list_ret = new_loc_list (ret, NULL, NULL, NULL);
14306
14307   return list_ret;
14308 }
14309
14310 /* Same as above but return only single location expression.  */
14311 static dw_loc_descr_ref
14312 loc_descriptor_from_tree (tree loc, int want_address)
14313 {
14314   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
14315   if (!ret)
14316     return NULL;
14317   if (ret->dw_loc_next)
14318     {
14319       expansion_failed (loc, NULL_RTX,
14320                         "Location list where only loc descriptor needed");
14321       return NULL;
14322     }
14323   return ret->expr;
14324 }
14325
14326 /* Given a value, round it up to the lowest multiple of `boundary'
14327    which is not less than the value itself.  */
14328
14329 static inline HOST_WIDE_INT
14330 ceiling (HOST_WIDE_INT value, unsigned int boundary)
14331 {
14332   return (((value + boundary - 1) / boundary) * boundary);
14333 }
14334
14335 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
14336    pointer to the declared type for the relevant field variable, or return
14337    `integer_type_node' if the given node turns out to be an
14338    ERROR_MARK node.  */
14339
14340 static inline tree
14341 field_type (const_tree decl)
14342 {
14343   tree type;
14344
14345   if (TREE_CODE (decl) == ERROR_MARK)
14346     return integer_type_node;
14347
14348   type = DECL_BIT_FIELD_TYPE (decl);
14349   if (type == NULL_TREE)
14350     type = TREE_TYPE (decl);
14351
14352   return type;
14353 }
14354
14355 /* Given a pointer to a tree node, return the alignment in bits for
14356    it, or else return BITS_PER_WORD if the node actually turns out to
14357    be an ERROR_MARK node.  */
14358
14359 static inline unsigned
14360 simple_type_align_in_bits (const_tree type)
14361 {
14362   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
14363 }
14364
14365 static inline unsigned
14366 simple_decl_align_in_bits (const_tree decl)
14367 {
14368   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
14369 }
14370
14371 /* Return the result of rounding T up to ALIGN.  */
14372
14373 static inline double_int
14374 round_up_to_align (double_int t, unsigned int align)
14375 {
14376   double_int alignd = uhwi_to_double_int (align);
14377   t = double_int_add (t, alignd);
14378   t = double_int_add (t, double_int_minus_one);
14379   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
14380   t = double_int_mul (t, alignd);
14381   return t;
14382 }
14383
14384 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
14385    lowest addressed byte of the "containing object" for the given FIELD_DECL,
14386    or return 0 if we are unable to determine what that offset is, either
14387    because the argument turns out to be a pointer to an ERROR_MARK node, or
14388    because the offset is actually variable.  (We can't handle the latter case
14389    just yet).  */
14390
14391 static HOST_WIDE_INT
14392 field_byte_offset (const_tree decl)
14393 {
14394   double_int object_offset_in_bits;
14395   double_int object_offset_in_bytes;
14396   double_int bitpos_int;
14397
14398   if (TREE_CODE (decl) == ERROR_MARK)
14399     return 0;
14400
14401   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
14402
14403   /* We cannot yet cope with fields whose positions are variable, so
14404      for now, when we see such things, we simply return 0.  Someday, we may
14405      be able to handle such cases, but it will be damn difficult.  */
14406   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
14407     return 0;
14408
14409   bitpos_int = tree_to_double_int (bit_position (decl));
14410
14411 #ifdef PCC_BITFIELD_TYPE_MATTERS
14412   if (PCC_BITFIELD_TYPE_MATTERS)
14413     {
14414       tree type;
14415       tree field_size_tree;
14416       double_int deepest_bitpos;
14417       double_int field_size_in_bits;
14418       unsigned int type_align_in_bits;
14419       unsigned int decl_align_in_bits;
14420       double_int type_size_in_bits;
14421
14422       type = field_type (decl);
14423       type_size_in_bits = double_int_type_size_in_bits (type);
14424       type_align_in_bits = simple_type_align_in_bits (type);
14425
14426       field_size_tree = DECL_SIZE (decl);
14427
14428       /* The size could be unspecified if there was an error, or for
14429          a flexible array member.  */
14430       if (!field_size_tree)
14431         field_size_tree = bitsize_zero_node;
14432
14433       /* If the size of the field is not constant, use the type size.  */
14434       if (TREE_CODE (field_size_tree) == INTEGER_CST)
14435         field_size_in_bits = tree_to_double_int (field_size_tree);
14436       else
14437         field_size_in_bits = type_size_in_bits;
14438
14439       decl_align_in_bits = simple_decl_align_in_bits (decl);
14440
14441       /* The GCC front-end doesn't make any attempt to keep track of the
14442          starting bit offset (relative to the start of the containing
14443          structure type) of the hypothetical "containing object" for a
14444          bit-field.  Thus, when computing the byte offset value for the
14445          start of the "containing object" of a bit-field, we must deduce
14446          this information on our own. This can be rather tricky to do in
14447          some cases.  For example, handling the following structure type
14448          definition when compiling for an i386/i486 target (which only
14449          aligns long long's to 32-bit boundaries) can be very tricky:
14450
14451          struct S { int field1; long long field2:31; };
14452
14453          Fortunately, there is a simple rule-of-thumb which can be used
14454          in such cases.  When compiling for an i386/i486, GCC will
14455          allocate 8 bytes for the structure shown above.  It decides to
14456          do this based upon one simple rule for bit-field allocation.
14457          GCC allocates each "containing object" for each bit-field at
14458          the first (i.e. lowest addressed) legitimate alignment boundary
14459          (based upon the required minimum alignment for the declared
14460          type of the field) which it can possibly use, subject to the
14461          condition that there is still enough available space remaining
14462          in the containing object (when allocated at the selected point)
14463          to fully accommodate all of the bits of the bit-field itself.
14464
14465          This simple rule makes it obvious why GCC allocates 8 bytes for
14466          each object of the structure type shown above.  When looking
14467          for a place to allocate the "containing object" for `field2',
14468          the compiler simply tries to allocate a 64-bit "containing
14469          object" at each successive 32-bit boundary (starting at zero)
14470          until it finds a place to allocate that 64- bit field such that
14471          at least 31 contiguous (and previously unallocated) bits remain
14472          within that selected 64 bit field.  (As it turns out, for the
14473          example above, the compiler finds it is OK to allocate the
14474          "containing object" 64-bit field at bit-offset zero within the
14475          structure type.)
14476
14477          Here we attempt to work backwards from the limited set of facts
14478          we're given, and we try to deduce from those facts, where GCC
14479          must have believed that the containing object started (within
14480          the structure type). The value we deduce is then used (by the
14481          callers of this routine) to generate DW_AT_location and
14482          DW_AT_bit_offset attributes for fields (both bit-fields and, in
14483          the case of DW_AT_location, regular fields as well).  */
14484
14485       /* Figure out the bit-distance from the start of the structure to
14486          the "deepest" bit of the bit-field.  */
14487       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
14488
14489       /* This is the tricky part.  Use some fancy footwork to deduce
14490          where the lowest addressed bit of the containing object must
14491          be.  */
14492       object_offset_in_bits
14493         = double_int_sub (deepest_bitpos, type_size_in_bits);
14494
14495       /* Round up to type_align by default.  This works best for
14496          bitfields.  */
14497       object_offset_in_bits
14498         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
14499
14500       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
14501         {
14502           object_offset_in_bits
14503             = double_int_sub (deepest_bitpos, type_size_in_bits);
14504
14505           /* Round up to decl_align instead.  */
14506           object_offset_in_bits
14507             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
14508         }
14509     }
14510   else
14511 #endif /* PCC_BITFIELD_TYPE_MATTERS */
14512     object_offset_in_bits = bitpos_int;
14513
14514   object_offset_in_bytes
14515     = double_int_div (object_offset_in_bits,
14516                       uhwi_to_double_int (BITS_PER_UNIT), true,
14517                       TRUNC_DIV_EXPR);
14518   return double_int_to_shwi (object_offset_in_bytes);
14519 }
14520 \f
14521 /* The following routines define various Dwarf attributes and any data
14522    associated with them.  */
14523
14524 /* Add a location description attribute value to a DIE.
14525
14526    This emits location attributes suitable for whole variables and
14527    whole parameters.  Note that the location attributes for struct fields are
14528    generated by the routine `data_member_location_attribute' below.  */
14529
14530 static inline void
14531 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
14532                              dw_loc_list_ref descr)
14533 {
14534   if (descr == 0)
14535     return;
14536   if (single_element_loc_list_p (descr))
14537     add_AT_loc (die, attr_kind, descr->expr);
14538   else
14539     add_AT_loc_list (die, attr_kind, descr);
14540 }
14541
14542 /* Add DW_AT_accessibility attribute to DIE if needed.  */
14543
14544 static void
14545 add_accessibility_attribute (dw_die_ref die, tree decl)
14546 {
14547   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
14548      children, otherwise the default is DW_ACCESS_public.  In DWARF2
14549      the default has always been DW_ACCESS_public.  */
14550   if (TREE_PROTECTED (decl))
14551     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
14552   else if (TREE_PRIVATE (decl))
14553     {
14554       if (dwarf_version == 2
14555           || die->die_parent == NULL
14556           || die->die_parent->die_tag != DW_TAG_class_type)
14557         add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
14558     }
14559   else if (dwarf_version > 2
14560            && die->die_parent
14561            && die->die_parent->die_tag == DW_TAG_class_type)
14562     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
14563 }
14564
14565 /* Attach the specialized form of location attribute used for data members of
14566    struct and union types.  In the special case of a FIELD_DECL node which
14567    represents a bit-field, the "offset" part of this special location
14568    descriptor must indicate the distance in bytes from the lowest-addressed
14569    byte of the containing struct or union type to the lowest-addressed byte of
14570    the "containing object" for the bit-field.  (See the `field_byte_offset'
14571    function above).
14572
14573    For any given bit-field, the "containing object" is a hypothetical object
14574    (of some integral or enum type) within which the given bit-field lives.  The
14575    type of this hypothetical "containing object" is always the same as the
14576    declared type of the individual bit-field itself (for GCC anyway... the
14577    DWARF spec doesn't actually mandate this).  Note that it is the size (in
14578    bytes) of the hypothetical "containing object" which will be given in the
14579    DW_AT_byte_size attribute for this bit-field.  (See the
14580    `byte_size_attribute' function below.)  It is also used when calculating the
14581    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
14582    function below.)  */
14583
14584 static void
14585 add_data_member_location_attribute (dw_die_ref die, tree decl)
14586 {
14587   HOST_WIDE_INT offset;
14588   dw_loc_descr_ref loc_descr = 0;
14589
14590   if (TREE_CODE (decl) == TREE_BINFO)
14591     {
14592       /* We're working on the TAG_inheritance for a base class.  */
14593       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
14594         {
14595           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
14596              aren't at a fixed offset from all (sub)objects of the same
14597              type.  We need to extract the appropriate offset from our
14598              vtable.  The following dwarf expression means
14599
14600                BaseAddr = ObAddr + *((*ObAddr) - Offset)
14601
14602              This is specific to the V3 ABI, of course.  */
14603
14604           dw_loc_descr_ref tmp;
14605
14606           /* Make a copy of the object address.  */
14607           tmp = new_loc_descr (DW_OP_dup, 0, 0);
14608           add_loc_descr (&loc_descr, tmp);
14609
14610           /* Extract the vtable address.  */
14611           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14612           add_loc_descr (&loc_descr, tmp);
14613
14614           /* Calculate the address of the offset.  */
14615           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
14616           gcc_assert (offset < 0);
14617
14618           tmp = int_loc_descriptor (-offset);
14619           add_loc_descr (&loc_descr, tmp);
14620           tmp = new_loc_descr (DW_OP_minus, 0, 0);
14621           add_loc_descr (&loc_descr, tmp);
14622
14623           /* Extract the offset.  */
14624           tmp = new_loc_descr (DW_OP_deref, 0, 0);
14625           add_loc_descr (&loc_descr, tmp);
14626
14627           /* Add it to the object address.  */
14628           tmp = new_loc_descr (DW_OP_plus, 0, 0);
14629           add_loc_descr (&loc_descr, tmp);
14630         }
14631       else
14632         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
14633     }
14634   else
14635     offset = field_byte_offset (decl);
14636
14637   if (! loc_descr)
14638     {
14639       if (dwarf_version > 2)
14640         {
14641           /* Don't need to output a location expression, just the constant. */
14642           if (offset < 0)
14643             add_AT_int (die, DW_AT_data_member_location, offset);
14644           else
14645             add_AT_unsigned (die, DW_AT_data_member_location, offset);
14646           return;
14647         }
14648       else
14649         {
14650           enum dwarf_location_atom op;
14651
14652           /* The DWARF2 standard says that we should assume that the structure
14653              address is already on the stack, so we can specify a structure
14654              field address by using DW_OP_plus_uconst.  */
14655
14656 #ifdef MIPS_DEBUGGING_INFO
14657           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
14658              operator correctly.  It works only if we leave the offset on the
14659              stack.  */
14660           op = DW_OP_constu;
14661 #else
14662           op = DW_OP_plus_uconst;
14663 #endif
14664
14665           loc_descr = new_loc_descr (op, offset, 0);
14666         }
14667     }
14668
14669   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
14670 }
14671
14672 /* Writes integer values to dw_vec_const array.  */
14673
14674 static void
14675 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
14676 {
14677   while (size != 0)
14678     {
14679       *dest++ = val & 0xff;
14680       val >>= 8;
14681       --size;
14682     }
14683 }
14684
14685 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
14686
14687 static HOST_WIDE_INT
14688 extract_int (const unsigned char *src, unsigned int size)
14689 {
14690   HOST_WIDE_INT val = 0;
14691
14692   src += size;
14693   while (size != 0)
14694     {
14695       val <<= 8;
14696       val |= *--src & 0xff;
14697       --size;
14698     }
14699   return val;
14700 }
14701
14702 /* Writes double_int values to dw_vec_const array.  */
14703
14704 static void
14705 insert_double (double_int val, unsigned char *dest)
14706 {
14707   unsigned char *p0 = dest;
14708   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
14709
14710   if (WORDS_BIG_ENDIAN)
14711     {
14712       p0 = p1;
14713       p1 = dest;
14714     }
14715
14716   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
14717   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
14718 }
14719
14720 /* Writes floating point values to dw_vec_const array.  */
14721
14722 static void
14723 insert_float (const_rtx rtl, unsigned char *array)
14724 {
14725   REAL_VALUE_TYPE rv;
14726   long val[4];
14727   int i;
14728
14729   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
14730   real_to_target (val, &rv, GET_MODE (rtl));
14731
14732   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
14733   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
14734     {
14735       insert_int (val[i], 4, array);
14736       array += 4;
14737     }
14738 }
14739
14740 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
14741    does not have a "location" either in memory or in a register.  These
14742    things can arise in GNU C when a constant is passed as an actual parameter
14743    to an inlined function.  They can also arise in C++ where declared
14744    constants do not necessarily get memory "homes".  */
14745
14746 static bool
14747 add_const_value_attribute (dw_die_ref die, rtx rtl)
14748 {
14749   switch (GET_CODE (rtl))
14750     {
14751     case CONST_INT:
14752       {
14753         HOST_WIDE_INT val = INTVAL (rtl);
14754
14755         if (val < 0)
14756           add_AT_int (die, DW_AT_const_value, val);
14757         else
14758           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
14759       }
14760       return true;
14761
14762     case CONST_DOUBLE:
14763       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
14764          floating-point constant.  A CONST_DOUBLE is used whenever the
14765          constant requires more than one word in order to be adequately
14766          represented.  */
14767       {
14768         enum machine_mode mode = GET_MODE (rtl);
14769
14770         if (SCALAR_FLOAT_MODE_P (mode))
14771           {
14772             unsigned int length = GET_MODE_SIZE (mode);
14773             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
14774
14775             insert_float (rtl, array);
14776             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
14777           }
14778         else
14779           add_AT_double (die, DW_AT_const_value,
14780                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
14781       }
14782       return true;
14783
14784     case CONST_VECTOR:
14785       {
14786         enum machine_mode mode = GET_MODE (rtl);
14787         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
14788         unsigned int length = CONST_VECTOR_NUNITS (rtl);
14789         unsigned char *array = (unsigned char *) ggc_alloc_atomic
14790           (length * elt_size);
14791         unsigned int i;
14792         unsigned char *p;
14793
14794         switch (GET_MODE_CLASS (mode))
14795           {
14796           case MODE_VECTOR_INT:
14797             for (i = 0, p = array; i < length; i++, p += elt_size)
14798               {
14799                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14800                 double_int val = rtx_to_double_int (elt);
14801
14802                 if (elt_size <= sizeof (HOST_WIDE_INT))
14803                   insert_int (double_int_to_shwi (val), elt_size, p);
14804                 else
14805                   {
14806                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14807                     insert_double (val, p);
14808                   }
14809               }
14810             break;
14811
14812           case MODE_VECTOR_FLOAT:
14813             for (i = 0, p = array; i < length; i++, p += elt_size)
14814               {
14815                 rtx elt = CONST_VECTOR_ELT (rtl, i);
14816                 insert_float (elt, p);
14817               }
14818             break;
14819
14820           default:
14821             gcc_unreachable ();
14822           }
14823
14824         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
14825       }
14826       return true;
14827
14828     case CONST_STRING:
14829       if (dwarf_version >= 4 || !dwarf_strict)
14830         {
14831           dw_loc_descr_ref loc_result;
14832           resolve_one_addr (&rtl, NULL);
14833         rtl_addr:
14834           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14835           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14836           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14837           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14838           add_AT_loc (die, DW_AT_location, loc_result);
14839           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14840           return true;
14841         }
14842       return false;
14843
14844     case CONST:
14845       if (CONSTANT_P (XEXP (rtl, 0)))
14846         return add_const_value_attribute (die, XEXP (rtl, 0));
14847       /* FALLTHROUGH */
14848     case SYMBOL_REF:
14849       if (!const_ok_for_output (rtl))
14850         return false;
14851     case LABEL_REF:
14852       if (dwarf_version >= 4 || !dwarf_strict)
14853         goto rtl_addr;
14854       return false;
14855
14856     case PLUS:
14857       /* In cases where an inlined instance of an inline function is passed
14858          the address of an `auto' variable (which is local to the caller) we
14859          can get a situation where the DECL_RTL of the artificial local
14860          variable (for the inlining) which acts as a stand-in for the
14861          corresponding formal parameter (of the inline function) will look
14862          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
14863          exactly a compile-time constant expression, but it isn't the address
14864          of the (artificial) local variable either.  Rather, it represents the
14865          *value* which the artificial local variable always has during its
14866          lifetime.  We currently have no way to represent such quasi-constant
14867          values in Dwarf, so for now we just punt and generate nothing.  */
14868       return false;
14869
14870     case HIGH:
14871     case CONST_FIXED:
14872       return false;
14873
14874     case MEM:
14875       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
14876           && MEM_READONLY_P (rtl)
14877           && GET_MODE (rtl) == BLKmode)
14878         {
14879           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
14880           return true;
14881         }
14882       return false;
14883
14884     default:
14885       /* No other kinds of rtx should be possible here.  */
14886       gcc_unreachable ();
14887     }
14888   return false;
14889 }
14890
14891 /* Determine whether the evaluation of EXPR references any variables
14892    or functions which aren't otherwise used (and therefore may not be
14893    output).  */
14894 static tree
14895 reference_to_unused (tree * tp, int * walk_subtrees,
14896                      void * data ATTRIBUTE_UNUSED)
14897 {
14898   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
14899     *walk_subtrees = 0;
14900
14901   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
14902       && ! TREE_ASM_WRITTEN (*tp))
14903     return *tp;
14904   /* ???  The C++ FE emits debug information for using decls, so
14905      putting gcc_unreachable here falls over.  See PR31899.  For now
14906      be conservative.  */
14907   else if (!cgraph_global_info_ready
14908            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
14909     return *tp;
14910   else if (TREE_CODE (*tp) == VAR_DECL)
14911     {
14912       struct varpool_node *node = varpool_get_node (*tp);
14913       if (!node || !node->needed)
14914         return *tp;
14915     }
14916   else if (TREE_CODE (*tp) == FUNCTION_DECL
14917            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
14918     {
14919       /* The call graph machinery must have finished analyzing,
14920          optimizing and gimplifying the CU by now.
14921          So if *TP has no call graph node associated
14922          to it, it means *TP will not be emitted.  */
14923       if (!cgraph_get_node (*tp))
14924         return *tp;
14925     }
14926   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
14927     return *tp;
14928
14929   return NULL_TREE;
14930 }
14931
14932 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
14933    for use in a later add_const_value_attribute call.  */
14934
14935 static rtx
14936 rtl_for_decl_init (tree init, tree type)
14937 {
14938   rtx rtl = NULL_RTX;
14939
14940   STRIP_NOPS (init);
14941
14942   /* If a variable is initialized with a string constant without embedded
14943      zeros, build CONST_STRING.  */
14944   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
14945     {
14946       tree enttype = TREE_TYPE (type);
14947       tree domain = TYPE_DOMAIN (type);
14948       enum machine_mode mode = TYPE_MODE (enttype);
14949
14950       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
14951           && domain
14952           && integer_zerop (TYPE_MIN_VALUE (domain))
14953           && compare_tree_int (TYPE_MAX_VALUE (domain),
14954                                TREE_STRING_LENGTH (init) - 1) == 0
14955           && ((size_t) TREE_STRING_LENGTH (init)
14956               == strlen (TREE_STRING_POINTER (init)) + 1))
14957         {
14958           rtl = gen_rtx_CONST_STRING (VOIDmode,
14959                                       ggc_strdup (TREE_STRING_POINTER (init)));
14960           rtl = gen_rtx_MEM (BLKmode, rtl);
14961           MEM_READONLY_P (rtl) = 1;
14962         }
14963     }
14964   /* Other aggregates, and complex values, could be represented using
14965      CONCAT: FIXME!  */
14966   else if (AGGREGATE_TYPE_P (type)
14967            || (TREE_CODE (init) == VIEW_CONVERT_EXPR
14968                && AGGREGATE_TYPE_P (TREE_TYPE (TREE_OPERAND (init, 0))))
14969            || TREE_CODE (type) == COMPLEX_TYPE)
14970     ;
14971   /* Vectors only work if their mode is supported by the target.
14972      FIXME: generic vectors ought to work too.  */
14973   else if (TREE_CODE (type) == VECTOR_TYPE
14974            && !VECTOR_MODE_P (TYPE_MODE (type)))
14975     ;
14976   /* If the initializer is something that we know will expand into an
14977      immediate RTL constant, expand it now.  We must be careful not to
14978      reference variables which won't be output.  */
14979   else if (initializer_constant_valid_p (init, type)
14980            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
14981     {
14982       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
14983          possible.  */
14984       if (TREE_CODE (type) == VECTOR_TYPE)
14985         switch (TREE_CODE (init))
14986           {
14987           case VECTOR_CST:
14988             break;
14989           case CONSTRUCTOR:
14990             if (TREE_CONSTANT (init))
14991               {
14992                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
14993                 bool constant_p = true;
14994                 tree value;
14995                 unsigned HOST_WIDE_INT ix;
14996
14997                 /* Even when ctor is constant, it might contain non-*_CST
14998                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
14999                    belong into VECTOR_CST nodes.  */
15000                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
15001                   if (!CONSTANT_CLASS_P (value))
15002                     {
15003                       constant_p = false;
15004                       break;
15005                     }
15006
15007                 if (constant_p)
15008                   {
15009                     init = build_vector_from_ctor (type, elts);
15010                     break;
15011                   }
15012               }
15013             /* FALLTHRU */
15014
15015           default:
15016             return NULL;
15017           }
15018
15019       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
15020
15021       /* If expand_expr returns a MEM, it wasn't immediate.  */
15022       gcc_assert (!rtl || !MEM_P (rtl));
15023     }
15024
15025   return rtl;
15026 }
15027
15028 /* Generate RTL for the variable DECL to represent its location.  */
15029
15030 static rtx
15031 rtl_for_decl_location (tree decl)
15032 {
15033   rtx rtl;
15034
15035   /* Here we have to decide where we are going to say the parameter "lives"
15036      (as far as the debugger is concerned).  We only have a couple of
15037      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
15038
15039      DECL_RTL normally indicates where the parameter lives during most of the
15040      activation of the function.  If optimization is enabled however, this
15041      could be either NULL or else a pseudo-reg.  Both of those cases indicate
15042      that the parameter doesn't really live anywhere (as far as the code
15043      generation parts of GCC are concerned) during most of the function's
15044      activation.  That will happen (for example) if the parameter is never
15045      referenced within the function.
15046
15047      We could just generate a location descriptor here for all non-NULL
15048      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
15049      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
15050      where DECL_RTL is NULL or is a pseudo-reg.
15051
15052      Note however that we can only get away with using DECL_INCOMING_RTL as
15053      a backup substitute for DECL_RTL in certain limited cases.  In cases
15054      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
15055      we can be sure that the parameter was passed using the same type as it is
15056      declared to have within the function, and that its DECL_INCOMING_RTL
15057      points us to a place where a value of that type is passed.
15058
15059      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
15060      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
15061      because in these cases DECL_INCOMING_RTL points us to a value of some
15062      type which is *different* from the type of the parameter itself.  Thus,
15063      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
15064      such cases, the debugger would end up (for example) trying to fetch a
15065      `float' from a place which actually contains the first part of a
15066      `double'.  That would lead to really incorrect and confusing
15067      output at debug-time.
15068
15069      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
15070      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
15071      are a couple of exceptions however.  On little-endian machines we can
15072      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
15073      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
15074      an integral type that is smaller than TREE_TYPE (decl). These cases arise
15075      when (on a little-endian machine) a non-prototyped function has a
15076      parameter declared to be of type `short' or `char'.  In such cases,
15077      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
15078      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
15079      passed `int' value.  If the debugger then uses that address to fetch
15080      a `short' or a `char' (on a little-endian machine) the result will be
15081      the correct data, so we allow for such exceptional cases below.
15082
15083      Note that our goal here is to describe the place where the given formal
15084      parameter lives during most of the function's activation (i.e. between the
15085      end of the prologue and the start of the epilogue).  We'll do that as best
15086      as we can. Note however that if the given formal parameter is modified
15087      sometime during the execution of the function, then a stack backtrace (at
15088      debug-time) will show the function as having been called with the *new*
15089      value rather than the value which was originally passed in.  This happens
15090      rarely enough that it is not a major problem, but it *is* a problem, and
15091      I'd like to fix it.
15092
15093      A future version of dwarf2out.c may generate two additional attributes for
15094      any given DW_TAG_formal_parameter DIE which will describe the "passed
15095      type" and the "passed location" for the given formal parameter in addition
15096      to the attributes we now generate to indicate the "declared type" and the
15097      "active location" for each parameter.  This additional set of attributes
15098      could be used by debuggers for stack backtraces. Separately, note that
15099      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
15100      This happens (for example) for inlined-instances of inline function formal
15101      parameters which are never referenced.  This really shouldn't be
15102      happening.  All PARM_DECL nodes should get valid non-NULL
15103      DECL_INCOMING_RTL values.  FIXME.  */
15104
15105   /* Use DECL_RTL as the "location" unless we find something better.  */
15106   rtl = DECL_RTL_IF_SET (decl);
15107
15108   /* When generating abstract instances, ignore everything except
15109      constants, symbols living in memory, and symbols living in
15110      fixed registers.  */
15111   if (! reload_completed)
15112     {
15113       if (rtl
15114           && (CONSTANT_P (rtl)
15115               || (MEM_P (rtl)
15116                   && CONSTANT_P (XEXP (rtl, 0)))
15117               || (REG_P (rtl)
15118                   && TREE_CODE (decl) == VAR_DECL
15119                   && TREE_STATIC (decl))))
15120         {
15121           rtl = targetm.delegitimize_address (rtl);
15122           return rtl;
15123         }
15124       rtl = NULL_RTX;
15125     }
15126   else if (TREE_CODE (decl) == PARM_DECL)
15127     {
15128       if (rtl == NULL_RTX
15129           || is_pseudo_reg (rtl)
15130           || (MEM_P (rtl)
15131               && is_pseudo_reg (XEXP (rtl, 0))
15132               && DECL_INCOMING_RTL (decl)
15133               && MEM_P (DECL_INCOMING_RTL (decl))
15134               && GET_MODE (rtl) == GET_MODE (DECL_INCOMING_RTL (decl))))
15135         {
15136           tree declared_type = TREE_TYPE (decl);
15137           tree passed_type = DECL_ARG_TYPE (decl);
15138           enum machine_mode dmode = TYPE_MODE (declared_type);
15139           enum machine_mode pmode = TYPE_MODE (passed_type);
15140
15141           /* This decl represents a formal parameter which was optimized out.
15142              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
15143              all cases where (rtl == NULL_RTX) just below.  */
15144           if (dmode == pmode)
15145             rtl = DECL_INCOMING_RTL (decl);
15146           else if ((rtl == NULL_RTX || is_pseudo_reg (rtl))
15147                    && SCALAR_INT_MODE_P (dmode)
15148                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
15149                    && DECL_INCOMING_RTL (decl))
15150             {
15151               rtx inc = DECL_INCOMING_RTL (decl);
15152               if (REG_P (inc))
15153                 rtl = inc;
15154               else if (MEM_P (inc))
15155                 {
15156                   if (BYTES_BIG_ENDIAN)
15157                     rtl = adjust_address_nv (inc, dmode,
15158                                              GET_MODE_SIZE (pmode)
15159                                              - GET_MODE_SIZE (dmode));
15160                   else
15161                     rtl = inc;
15162                 }
15163             }
15164         }
15165
15166       /* If the parm was passed in registers, but lives on the stack, then
15167          make a big endian correction if the mode of the type of the
15168          parameter is not the same as the mode of the rtl.  */
15169       /* ??? This is the same series of checks that are made in dbxout.c before
15170          we reach the big endian correction code there.  It isn't clear if all
15171          of these checks are necessary here, but keeping them all is the safe
15172          thing to do.  */
15173       else if (MEM_P (rtl)
15174                && XEXP (rtl, 0) != const0_rtx
15175                && ! CONSTANT_P (XEXP (rtl, 0))
15176                /* Not passed in memory.  */
15177                && !MEM_P (DECL_INCOMING_RTL (decl))
15178                /* Not passed by invisible reference.  */
15179                && (!REG_P (XEXP (rtl, 0))
15180                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
15181                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
15182 #if !HARD_FRAME_POINTER_IS_ARG_POINTER
15183                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
15184 #endif
15185                      )
15186                /* Big endian correction check.  */
15187                && BYTES_BIG_ENDIAN
15188                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
15189                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
15190                    < UNITS_PER_WORD))
15191         {
15192           int offset = (UNITS_PER_WORD
15193                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
15194
15195           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15196                              plus_constant (XEXP (rtl, 0), offset));
15197         }
15198     }
15199   else if (TREE_CODE (decl) == VAR_DECL
15200            && rtl
15201            && MEM_P (rtl)
15202            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
15203            && BYTES_BIG_ENDIAN)
15204     {
15205       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
15206       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
15207
15208       /* If a variable is declared "register" yet is smaller than
15209          a register, then if we store the variable to memory, it
15210          looks like we're storing a register-sized value, when in
15211          fact we are not.  We need to adjust the offset of the
15212          storage location to reflect the actual value's bytes,
15213          else gdb will not be able to display it.  */
15214       if (rsize > dsize)
15215         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
15216                            plus_constant (XEXP (rtl, 0), rsize-dsize));
15217     }
15218
15219   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
15220      and will have been substituted directly into all expressions that use it.
15221      C does not have such a concept, but C++ and other languages do.  */
15222   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
15223     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
15224
15225   if (rtl)
15226     rtl = targetm.delegitimize_address (rtl);
15227
15228   /* If we don't look past the constant pool, we risk emitting a
15229      reference to a constant pool entry that isn't referenced from
15230      code, and thus is not emitted.  */
15231   if (rtl)
15232     rtl = avoid_constant_pool_reference (rtl);
15233
15234   /* Try harder to get a rtl.  If this symbol ends up not being emitted
15235      in the current CU, resolve_addr will remove the expression referencing
15236      it.  */
15237   if (rtl == NULL_RTX
15238       && TREE_CODE (decl) == VAR_DECL
15239       && !DECL_EXTERNAL (decl)
15240       && TREE_STATIC (decl)
15241       && DECL_NAME (decl)
15242       && !DECL_HARD_REGISTER (decl)
15243       && DECL_MODE (decl) != VOIDmode)
15244     {
15245       rtl = make_decl_rtl_for_debug (decl);
15246       if (!MEM_P (rtl)
15247           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
15248           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
15249         rtl = NULL_RTX;
15250     }
15251
15252   return rtl;
15253 }
15254
15255 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
15256    returned.  If so, the decl for the COMMON block is returned, and the
15257    value is the offset into the common block for the symbol.  */
15258
15259 static tree
15260 fortran_common (tree decl, HOST_WIDE_INT *value)
15261 {
15262   tree val_expr, cvar;
15263   enum machine_mode mode;
15264   HOST_WIDE_INT bitsize, bitpos;
15265   tree offset;
15266   int volatilep = 0, unsignedp = 0;
15267
15268   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
15269      it does not have a value (the offset into the common area), or if it
15270      is thread local (as opposed to global) then it isn't common, and shouldn't
15271      be handled as such.  */
15272   if (TREE_CODE (decl) != VAR_DECL
15273       || !TREE_STATIC (decl)
15274       || !DECL_HAS_VALUE_EXPR_P (decl)
15275       || !is_fortran ())
15276     return NULL_TREE;
15277
15278   val_expr = DECL_VALUE_EXPR (decl);
15279   if (TREE_CODE (val_expr) != COMPONENT_REF)
15280     return NULL_TREE;
15281
15282   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
15283                               &mode, &unsignedp, &volatilep, true);
15284
15285   if (cvar == NULL_TREE
15286       || TREE_CODE (cvar) != VAR_DECL
15287       || DECL_ARTIFICIAL (cvar)
15288       || !TREE_PUBLIC (cvar))
15289     return NULL_TREE;
15290
15291   *value = 0;
15292   if (offset != NULL)
15293     {
15294       if (!host_integerp (offset, 0))
15295         return NULL_TREE;
15296       *value = tree_low_cst (offset, 0);
15297     }
15298   if (bitpos != 0)
15299     *value += bitpos / BITS_PER_UNIT;
15300
15301   return cvar;
15302 }
15303
15304 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
15305    data attribute for a variable or a parameter.  We generate the
15306    DW_AT_const_value attribute only in those cases where the given variable
15307    or parameter does not have a true "location" either in memory or in a
15308    register.  This can happen (for example) when a constant is passed as an
15309    actual argument in a call to an inline function.  (It's possible that
15310    these things can crop up in other ways also.)  Note that one type of
15311    constant value which can be passed into an inlined function is a constant
15312    pointer.  This can happen for example if an actual argument in an inlined
15313    function call evaluates to a compile-time constant address.
15314
15315    CACHE_P is true if it is worth caching the location list for DECL,
15316    so that future calls can reuse it rather than regenerate it from scratch.
15317    This is true for BLOCK_NONLOCALIZED_VARS in inlined subroutines,
15318    since we will need to refer to them each time the function is inlined.  */
15319
15320 static bool
15321 add_location_or_const_value_attribute (dw_die_ref die, tree decl, bool cache_p,
15322                                        enum dwarf_attribute attr)
15323 {
15324   rtx rtl;
15325   dw_loc_list_ref list;
15326   var_loc_list *loc_list;
15327   cached_dw_loc_list *cache;
15328   void **slot;
15329
15330   if (TREE_CODE (decl) == ERROR_MARK)
15331     return false;
15332
15333   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
15334               || TREE_CODE (decl) == RESULT_DECL);
15335
15336   /* Try to get some constant RTL for this decl, and use that as the value of
15337      the location.  */
15338
15339   rtl = rtl_for_decl_location (decl);
15340   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15341       && add_const_value_attribute (die, rtl))
15342     return true;
15343
15344   /* See if we have single element location list that is equivalent to
15345      a constant value.  That way we are better to use add_const_value_attribute
15346      rather than expanding constant value equivalent.  */
15347   loc_list = lookup_decl_loc (decl);
15348   if (loc_list
15349       && loc_list->first
15350       && loc_list->first->next == NULL
15351       && NOTE_P (loc_list->first->loc)
15352       && NOTE_VAR_LOCATION (loc_list->first->loc)
15353       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
15354     {
15355       struct var_loc_node *node;
15356
15357       node = loc_list->first;
15358       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
15359       if (GET_CODE (rtl) == EXPR_LIST)
15360         rtl = XEXP (rtl, 0);
15361       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
15362           && add_const_value_attribute (die, rtl))
15363          return true;
15364     }
15365   /* If this decl is from BLOCK_NONLOCALIZED_VARS, we might need its
15366      list several times.  See if we've already cached the contents.  */
15367   list = NULL;
15368   if (loc_list == NULL || cached_dw_loc_list_table == NULL)
15369     cache_p = false;
15370   if (cache_p)
15371     {
15372       cache = (cached_dw_loc_list *)
15373         htab_find_with_hash (cached_dw_loc_list_table, decl, DECL_UID (decl));
15374       if (cache)
15375         list = cache->loc_list;
15376     }
15377   if (list == NULL)
15378     {
15379       list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
15380       /* It is usually worth caching this result if the decl is from
15381          BLOCK_NONLOCALIZED_VARS and if the list has at least two elements.  */
15382       if (cache_p && list && list->dw_loc_next)
15383         {
15384           slot = htab_find_slot_with_hash (cached_dw_loc_list_table, decl,
15385                                            DECL_UID (decl), INSERT);
15386           cache = ggc_alloc_cleared_cached_dw_loc_list ();
15387           cache->decl_id = DECL_UID (decl);
15388           cache->loc_list = list;
15389           *slot = cache;
15390         }
15391     }
15392   if (list)
15393     {
15394       add_AT_location_description (die, attr, list);
15395       return true;
15396     }
15397   /* None of that worked, so it must not really have a location;
15398      try adding a constant value attribute from the DECL_INITIAL.  */
15399   return tree_add_const_value_attribute_for_decl (die, decl);
15400 }
15401
15402 /* Add VARIABLE and DIE into deferred locations list.  */
15403
15404 static void
15405 defer_location (tree variable, dw_die_ref die)
15406 {
15407   deferred_locations entry;
15408   entry.variable = variable;
15409   entry.die = die;
15410   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
15411 }
15412
15413 /* Helper function for tree_add_const_value_attribute.  Natively encode
15414    initializer INIT into an array.  Return true if successful.  */
15415
15416 static bool
15417 native_encode_initializer (tree init, unsigned char *array, int size)
15418 {
15419   tree type;
15420
15421   if (init == NULL_TREE)
15422     return false;
15423
15424   STRIP_NOPS (init);
15425   switch (TREE_CODE (init))
15426     {
15427     case STRING_CST:
15428       type = TREE_TYPE (init);
15429       if (TREE_CODE (type) == ARRAY_TYPE)
15430         {
15431           tree enttype = TREE_TYPE (type);
15432           enum machine_mode mode = TYPE_MODE (enttype);
15433
15434           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
15435             return false;
15436           if (int_size_in_bytes (type) != size)
15437             return false;
15438           if (size > TREE_STRING_LENGTH (init))
15439             {
15440               memcpy (array, TREE_STRING_POINTER (init),
15441                       TREE_STRING_LENGTH (init));
15442               memset (array + TREE_STRING_LENGTH (init),
15443                       '\0', size - TREE_STRING_LENGTH (init));
15444             }
15445           else
15446             memcpy (array, TREE_STRING_POINTER (init), size);
15447           return true;
15448         }
15449       return false;
15450     case CONSTRUCTOR:
15451       type = TREE_TYPE (init);
15452       if (int_size_in_bytes (type) != size)
15453         return false;
15454       if (TREE_CODE (type) == ARRAY_TYPE)
15455         {
15456           HOST_WIDE_INT min_index;
15457           unsigned HOST_WIDE_INT cnt;
15458           int curpos = 0, fieldsize;
15459           constructor_elt *ce;
15460
15461           if (TYPE_DOMAIN (type) == NULL_TREE
15462               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
15463             return false;
15464
15465           fieldsize = int_size_in_bytes (TREE_TYPE (type));
15466           if (fieldsize <= 0)
15467             return false;
15468
15469           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
15470           memset (array, '\0', size);
15471           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15472             {
15473               tree val = ce->value;
15474               tree index = ce->index;
15475               int pos = curpos;
15476               if (index && TREE_CODE (index) == RANGE_EXPR)
15477                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
15478                       * fieldsize;
15479               else if (index)
15480                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
15481
15482               if (val)
15483                 {
15484                   STRIP_NOPS (val);
15485                   if (!native_encode_initializer (val, array + pos, fieldsize))
15486                     return false;
15487                 }
15488               curpos = pos + fieldsize;
15489               if (index && TREE_CODE (index) == RANGE_EXPR)
15490                 {
15491                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
15492                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
15493                   while (count-- > 0)
15494                     {
15495                       if (val)
15496                         memcpy (array + curpos, array + pos, fieldsize);
15497                       curpos += fieldsize;
15498                     }
15499                 }
15500               gcc_assert (curpos <= size);
15501             }
15502           return true;
15503         }
15504       else if (TREE_CODE (type) == RECORD_TYPE
15505                || TREE_CODE (type) == UNION_TYPE)
15506         {
15507           tree field = NULL_TREE;
15508           unsigned HOST_WIDE_INT cnt;
15509           constructor_elt *ce;
15510
15511           if (int_size_in_bytes (type) != size)
15512             return false;
15513
15514           if (TREE_CODE (type) == RECORD_TYPE)
15515             field = TYPE_FIELDS (type);
15516
15517           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce)
15518             {
15519               tree val = ce->value;
15520               int pos, fieldsize;
15521
15522               if (ce->index != 0)
15523                 field = ce->index;
15524
15525               if (val)
15526                 STRIP_NOPS (val);
15527
15528               if (field == NULL_TREE || DECL_BIT_FIELD (field))
15529                 return false;
15530
15531               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
15532                   && TYPE_DOMAIN (TREE_TYPE (field))
15533                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
15534                 return false;
15535               else if (DECL_SIZE_UNIT (field) == NULL_TREE
15536                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
15537                 return false;
15538               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
15539               pos = int_byte_position (field);
15540               gcc_assert (pos + fieldsize <= size);
15541               if (val
15542                   && !native_encode_initializer (val, array + pos, fieldsize))
15543                 return false;
15544             }
15545           return true;
15546         }
15547       return false;
15548     case VIEW_CONVERT_EXPR:
15549     case NON_LVALUE_EXPR:
15550       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
15551     default:
15552       return native_encode_expr (init, array, size) == size;
15553     }
15554 }
15555
15556 /* Attach a DW_AT_const_value attribute to DIE. The value of the
15557    attribute is the const value T.  */
15558
15559 static bool
15560 tree_add_const_value_attribute (dw_die_ref die, tree t)
15561 {
15562   tree init;
15563   tree type = TREE_TYPE (t);
15564   rtx rtl;
15565
15566   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
15567     return false;
15568
15569   init = t;
15570   gcc_assert (!DECL_P (init));
15571
15572   rtl = rtl_for_decl_init (init, type);
15573   if (rtl)
15574     return add_const_value_attribute (die, rtl);
15575   /* If the host and target are sane, try harder.  */
15576   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
15577            && initializer_constant_valid_p (init, type))
15578     {
15579       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
15580       if (size > 0 && (int) size == size)
15581         {
15582           unsigned char *array = (unsigned char *)
15583             ggc_alloc_cleared_atomic (size);
15584
15585           if (native_encode_initializer (init, array, size))
15586             {
15587               add_AT_vec (die, DW_AT_const_value, size, 1, array);
15588               return true;
15589             }
15590         }
15591     }
15592   return false;
15593 }
15594
15595 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
15596    attribute is the const value of T, where T is an integral constant
15597    variable with static storage duration
15598    (so it can't be a PARM_DECL or a RESULT_DECL).  */
15599
15600 static bool
15601 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
15602 {
15603
15604   if (!decl
15605       || (TREE_CODE (decl) != VAR_DECL
15606           && TREE_CODE (decl) != CONST_DECL)
15607       || (TREE_CODE (decl) == VAR_DECL
15608           && !TREE_STATIC (decl)))
15609     return false;
15610
15611     if (TREE_READONLY (decl)
15612         && ! TREE_THIS_VOLATILE (decl)
15613         && DECL_INITIAL (decl))
15614       /* OK */;
15615     else
15616       return false;
15617
15618   /* Don't add DW_AT_const_value if abstract origin already has one.  */
15619   if (get_AT (var_die, DW_AT_const_value))
15620     return false;
15621
15622   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
15623 }
15624
15625 /* Convert the CFI instructions for the current function into a
15626    location list.  This is used for DW_AT_frame_base when we targeting
15627    a dwarf2 consumer that does not support the dwarf3
15628    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
15629    expressions.  */
15630
15631 static dw_loc_list_ref
15632 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
15633 {
15634   int ix;
15635   dw_fde_ref fde;
15636   dw_loc_list_ref list, *list_tail;
15637   dw_cfi_ref cfi;
15638   dw_cfa_location last_cfa, next_cfa;
15639   const char *start_label, *last_label, *section;
15640   dw_cfa_location remember;
15641
15642   fde = current_fde ();
15643   gcc_assert (fde != NULL);
15644
15645   section = secname_for_decl (current_function_decl);
15646   list_tail = &list;
15647   list = NULL;
15648
15649   memset (&next_cfa, 0, sizeof (next_cfa));
15650   next_cfa.reg = INVALID_REGNUM;
15651   remember = next_cfa;
15652
15653   start_label = fde->dw_fde_begin;
15654
15655   /* ??? Bald assumption that the CIE opcode list does not contain
15656      advance opcodes.  */
15657   FOR_EACH_VEC_ELT (dw_cfi_ref, cie_cfi_vec, ix, cfi)
15658     lookup_cfa_1 (cfi, &next_cfa, &remember);
15659
15660   last_cfa = next_cfa;
15661   last_label = start_label;
15662
15663   if (fde->dw_fde_second_begin && fde->dw_fde_switch_cfi_index == 0)
15664     {
15665       /* If the first partition contained no CFI adjustments, the
15666          CIE opcodes apply to the whole first partition.  */
15667       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15668                                  fde->dw_fde_begin, fde->dw_fde_end, section);
15669       list_tail =&(*list_tail)->dw_loc_next;
15670       start_label = last_label = fde->dw_fde_second_begin;
15671     }
15672
15673   FOR_EACH_VEC_ELT (dw_cfi_ref, fde->dw_fde_cfi, ix, cfi)
15674     {
15675       switch (cfi->dw_cfi_opc)
15676         {
15677         case DW_CFA_set_loc:
15678         case DW_CFA_advance_loc1:
15679         case DW_CFA_advance_loc2:
15680         case DW_CFA_advance_loc4:
15681           if (!cfa_equal_p (&last_cfa, &next_cfa))
15682             {
15683               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15684                                          start_label, last_label, section);
15685
15686               list_tail = &(*list_tail)->dw_loc_next;
15687               last_cfa = next_cfa;
15688               start_label = last_label;
15689             }
15690           last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
15691           break;
15692
15693         case DW_CFA_advance_loc:
15694           /* The encoding is complex enough that we should never emit this.  */
15695           gcc_unreachable ();
15696
15697         default:
15698           lookup_cfa_1 (cfi, &next_cfa, &remember);
15699           break;
15700         }
15701       if (ix + 1 == fde->dw_fde_switch_cfi_index)
15702         {
15703           if (!cfa_equal_p (&last_cfa, &next_cfa))
15704             {
15705               *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15706                                          start_label, last_label, section);
15707
15708               list_tail = &(*list_tail)->dw_loc_next;
15709               last_cfa = next_cfa;
15710               start_label = last_label;
15711             }
15712           *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15713                                      start_label, fde->dw_fde_end, section);
15714           list_tail = &(*list_tail)->dw_loc_next;
15715           start_label = last_label = fde->dw_fde_second_begin;
15716         }
15717     }
15718
15719   if (!cfa_equal_p (&last_cfa, &next_cfa))
15720     {
15721       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
15722                                  start_label, last_label, section);
15723       list_tail = &(*list_tail)->dw_loc_next;
15724       start_label = last_label;
15725     }
15726
15727   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
15728                              start_label,
15729                              fde->dw_fde_second_begin
15730                              ? fde->dw_fde_second_end : fde->dw_fde_end,
15731                              section);
15732
15733   if (list && list->dw_loc_next)
15734     gen_llsym (list);
15735
15736   return list;
15737 }
15738
15739 /* Compute a displacement from the "steady-state frame pointer" to the
15740    frame base (often the same as the CFA), and store it in
15741    frame_pointer_fb_offset.  OFFSET is added to the displacement
15742    before the latter is negated.  */
15743
15744 static void
15745 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
15746 {
15747   rtx reg, elim;
15748
15749 #ifdef FRAME_POINTER_CFA_OFFSET
15750   reg = frame_pointer_rtx;
15751   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
15752 #else
15753   reg = arg_pointer_rtx;
15754   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
15755 #endif
15756
15757   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
15758   if (GET_CODE (elim) == PLUS)
15759     {
15760       offset += INTVAL (XEXP (elim, 1));
15761       elim = XEXP (elim, 0);
15762     }
15763
15764   frame_pointer_fb_offset = -offset;
15765
15766   /* ??? AVR doesn't set up valid eliminations when there is no stack frame
15767      in which to eliminate.  This is because it's stack pointer isn't 
15768      directly accessible as a register within the ISA.  To work around
15769      this, assume that while we cannot provide a proper value for
15770      frame_pointer_fb_offset, we won't need one either.  */
15771   frame_pointer_fb_offset_valid
15772     = ((SUPPORTS_STACK_ALIGNMENT
15773         && (elim == hard_frame_pointer_rtx
15774             || elim == stack_pointer_rtx))
15775        || elim == (frame_pointer_needed
15776                    ? hard_frame_pointer_rtx
15777                    : stack_pointer_rtx));
15778 }
15779
15780 /* Generate a DW_AT_name attribute given some string value to be included as
15781    the value of the attribute.  */
15782
15783 static void
15784 add_name_attribute (dw_die_ref die, const char *name_string)
15785 {
15786   if (name_string != NULL && *name_string != 0)
15787     {
15788       if (demangle_name_func)
15789         name_string = (*demangle_name_func) (name_string);
15790
15791       add_AT_string (die, DW_AT_name, name_string);
15792     }
15793 }
15794
15795 /* Retrieve the descriptive type of TYPE, if any, make sure it has a
15796    DIE and attach a DW_AT_GNAT_descriptive_type attribute to the DIE
15797    of TYPE accordingly.
15798
15799    ??? This is a temporary measure until after we're able to generate
15800    regular DWARF for the complex Ada type system.  */
15801
15802 static void 
15803 add_gnat_descriptive_type_attribute (dw_die_ref die, tree type,
15804                                      dw_die_ref context_die)
15805 {
15806   tree dtype;
15807   dw_die_ref dtype_die;
15808
15809   if (!lang_hooks.types.descriptive_type)
15810     return;
15811
15812   dtype = lang_hooks.types.descriptive_type (type);
15813   if (!dtype)
15814     return;
15815
15816   dtype_die = lookup_type_die (dtype);
15817   if (!dtype_die)
15818     {
15819       gen_type_die (dtype, context_die);
15820       dtype_die = lookup_type_die (dtype);
15821       gcc_assert (dtype_die);
15822     }
15823
15824   add_AT_die_ref (die, DW_AT_GNAT_descriptive_type, dtype_die);
15825 }
15826
15827 /* Generate a DW_AT_comp_dir attribute for DIE.  */
15828
15829 static void
15830 add_comp_dir_attribute (dw_die_ref die)
15831 {
15832   const char *wd = get_src_pwd ();
15833   char *wd1;
15834
15835   if (wd == NULL)
15836     return;
15837
15838   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
15839     {
15840       int wdlen;
15841
15842       wdlen = strlen (wd);
15843       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
15844       strcpy (wd1, wd);
15845       wd1 [wdlen] = DIR_SEPARATOR;
15846       wd1 [wdlen + 1] = 0;
15847       wd = wd1;
15848     }
15849
15850     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
15851 }
15852
15853 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
15854    default.  */
15855
15856 static int
15857 lower_bound_default (void)
15858 {
15859   switch (get_AT_unsigned (comp_unit_die (), DW_AT_language))
15860     {
15861     case DW_LANG_C:
15862     case DW_LANG_C89:
15863     case DW_LANG_C99:
15864     case DW_LANG_C_plus_plus:
15865     case DW_LANG_ObjC:
15866     case DW_LANG_ObjC_plus_plus:
15867     case DW_LANG_Java:
15868       return 0;
15869     case DW_LANG_Fortran77:
15870     case DW_LANG_Fortran90:
15871     case DW_LANG_Fortran95:
15872       return 1;
15873     case DW_LANG_UPC:
15874     case DW_LANG_D:
15875     case DW_LANG_Python:
15876       return dwarf_version >= 4 ? 0 : -1;
15877     case DW_LANG_Ada95:
15878     case DW_LANG_Ada83:
15879     case DW_LANG_Cobol74:
15880     case DW_LANG_Cobol85:
15881     case DW_LANG_Pascal83:
15882     case DW_LANG_Modula2:
15883     case DW_LANG_PLI:
15884       return dwarf_version >= 4 ? 1 : -1;
15885     default:
15886       return -1;
15887     }
15888 }
15889
15890 /* Given a tree node describing an array bound (either lower or upper) output
15891    a representation for that bound.  */
15892
15893 static void
15894 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
15895 {
15896   switch (TREE_CODE (bound))
15897     {
15898     case ERROR_MARK:
15899       return;
15900
15901     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
15902     case INTEGER_CST:
15903       {
15904         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
15905         int dflt;
15906
15907         /* Use the default if possible.  */
15908         if (bound_attr == DW_AT_lower_bound
15909             && host_integerp (bound, 0)
15910             && (dflt = lower_bound_default ()) != -1
15911             && tree_low_cst (bound, 0) == dflt)
15912           ;
15913
15914         /* Otherwise represent the bound as an unsigned value with the
15915            precision of its type.  The precision and signedness of the
15916            type will be necessary to re-interpret it unambiguously.  */
15917         else if (prec < HOST_BITS_PER_WIDE_INT)
15918           {
15919             unsigned HOST_WIDE_INT mask
15920               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
15921             add_AT_unsigned (subrange_die, bound_attr,
15922                              TREE_INT_CST_LOW (bound) & mask);
15923           }
15924         else if (prec == HOST_BITS_PER_WIDE_INT
15925                  || TREE_INT_CST_HIGH (bound) == 0)
15926           add_AT_unsigned (subrange_die, bound_attr,
15927                            TREE_INT_CST_LOW (bound));
15928         else
15929           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
15930                          TREE_INT_CST_LOW (bound));
15931       }
15932       break;
15933
15934     CASE_CONVERT:
15935     case VIEW_CONVERT_EXPR:
15936       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
15937       break;
15938
15939     case SAVE_EXPR:
15940       break;
15941
15942     case VAR_DECL:
15943     case PARM_DECL:
15944     case RESULT_DECL:
15945       {
15946         dw_die_ref decl_die = lookup_decl_die (bound);
15947
15948         /* ??? Can this happen, or should the variable have been bound
15949            first?  Probably it can, since I imagine that we try to create
15950            the types of parameters in the order in which they exist in
15951            the list, and won't have created a forward reference to a
15952            later parameter.  */
15953         if (decl_die != NULL)
15954           {
15955             add_AT_die_ref (subrange_die, bound_attr, decl_die);
15956             break;
15957           }
15958       }
15959       /* FALLTHRU */
15960
15961     default:
15962       {
15963         /* Otherwise try to create a stack operation procedure to
15964            evaluate the value of the array bound.  */
15965
15966         dw_die_ref ctx, decl_die;
15967         dw_loc_list_ref list;
15968
15969         list = loc_list_from_tree (bound, 2);
15970         if (list == NULL || single_element_loc_list_p (list))
15971           {
15972             /* If DW_AT_*bound is not a reference nor constant, it is
15973                a DWARF expression rather than location description.
15974                For that loc_list_from_tree (bound, 0) is needed.
15975                If that fails to give a single element list,
15976                fall back to outputting this as a reference anyway.  */
15977             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
15978             if (list2 && single_element_loc_list_p (list2))
15979               {
15980                 add_AT_loc (subrange_die, bound_attr, list2->expr);
15981                 break;
15982               }
15983           }
15984         if (list == NULL)
15985           break;
15986
15987         if (current_function_decl == 0)
15988           ctx = comp_unit_die ();
15989         else
15990           ctx = lookup_decl_die (current_function_decl);
15991
15992         decl_die = new_die (DW_TAG_variable, ctx, bound);
15993         add_AT_flag (decl_die, DW_AT_artificial, 1);
15994         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
15995         add_AT_location_description (decl_die, DW_AT_location, list);
15996         add_AT_die_ref (subrange_die, bound_attr, decl_die);
15997         break;
15998       }
15999     }
16000 }
16001
16002 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
16003    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
16004    Note that the block of subscript information for an array type also
16005    includes information about the element type of the given array type.  */
16006
16007 static void
16008 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
16009 {
16010   unsigned dimension_number;
16011   tree lower, upper;
16012   dw_die_ref subrange_die;
16013
16014   for (dimension_number = 0;
16015        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
16016        type = TREE_TYPE (type), dimension_number++)
16017     {
16018       tree domain = TYPE_DOMAIN (type);
16019
16020       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
16021         break;
16022
16023       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
16024          and (in GNU C only) variable bounds.  Handle all three forms
16025          here.  */
16026       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
16027       if (domain)
16028         {
16029           /* We have an array type with specified bounds.  */
16030           lower = TYPE_MIN_VALUE (domain);
16031           upper = TYPE_MAX_VALUE (domain);
16032
16033           /* Define the index type.  */
16034           if (TREE_TYPE (domain))
16035             {
16036               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
16037                  TREE_TYPE field.  We can't emit debug info for this
16038                  because it is an unnamed integral type.  */
16039               if (TREE_CODE (domain) == INTEGER_TYPE
16040                   && TYPE_NAME (domain) == NULL_TREE
16041                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
16042                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
16043                 ;
16044               else
16045                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
16046                                     type_die);
16047             }
16048
16049           /* ??? If upper is NULL, the array has unspecified length,
16050              but it does have a lower bound.  This happens with Fortran
16051                dimension arr(N:*)
16052              Since the debugger is definitely going to need to know N
16053              to produce useful results, go ahead and output the lower
16054              bound solo, and hope the debugger can cope.  */
16055
16056           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
16057           if (upper)
16058             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
16059         }
16060
16061       /* Otherwise we have an array type with an unspecified length.  The
16062          DWARF-2 spec does not say how to handle this; let's just leave out the
16063          bounds.  */
16064     }
16065 }
16066
16067 static void
16068 add_byte_size_attribute (dw_die_ref die, tree tree_node)
16069 {
16070   unsigned size;
16071
16072   switch (TREE_CODE (tree_node))
16073     {
16074     case ERROR_MARK:
16075       size = 0;
16076       break;
16077     case ENUMERAL_TYPE:
16078     case RECORD_TYPE:
16079     case UNION_TYPE:
16080     case QUAL_UNION_TYPE:
16081       size = int_size_in_bytes (tree_node);
16082       break;
16083     case FIELD_DECL:
16084       /* For a data member of a struct or union, the DW_AT_byte_size is
16085          generally given as the number of bytes normally allocated for an
16086          object of the *declared* type of the member itself.  This is true
16087          even for bit-fields.  */
16088       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
16089       break;
16090     default:
16091       gcc_unreachable ();
16092     }
16093
16094   /* Note that `size' might be -1 when we get to this point.  If it is, that
16095      indicates that the byte size of the entity in question is variable.  We
16096      have no good way of expressing this fact in Dwarf at the present time,
16097      so just let the -1 pass on through.  */
16098   add_AT_unsigned (die, DW_AT_byte_size, size);
16099 }
16100
16101 /* For a FIELD_DECL node which represents a bit-field, output an attribute
16102    which specifies the distance in bits from the highest order bit of the
16103    "containing object" for the bit-field to the highest order bit of the
16104    bit-field itself.
16105
16106    For any given bit-field, the "containing object" is a hypothetical object
16107    (of some integral or enum type) within which the given bit-field lives.  The
16108    type of this hypothetical "containing object" is always the same as the
16109    declared type of the individual bit-field itself.  The determination of the
16110    exact location of the "containing object" for a bit-field is rather
16111    complicated.  It's handled by the `field_byte_offset' function (above).
16112
16113    Note that it is the size (in bytes) of the hypothetical "containing object"
16114    which will be given in the DW_AT_byte_size attribute for this bit-field.
16115    (See `byte_size_attribute' above).  */
16116
16117 static inline void
16118 add_bit_offset_attribute (dw_die_ref die, tree decl)
16119 {
16120   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
16121   tree type = DECL_BIT_FIELD_TYPE (decl);
16122   HOST_WIDE_INT bitpos_int;
16123   HOST_WIDE_INT highest_order_object_bit_offset;
16124   HOST_WIDE_INT highest_order_field_bit_offset;
16125   HOST_WIDE_INT bit_offset;
16126
16127   /* Must be a field and a bit field.  */
16128   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
16129
16130   /* We can't yet handle bit-fields whose offsets are variable, so if we
16131      encounter such things, just return without generating any attribute
16132      whatsoever.  Likewise for variable or too large size.  */
16133   if (! host_integerp (bit_position (decl), 0)
16134       || ! host_integerp (DECL_SIZE (decl), 1))
16135     return;
16136
16137   bitpos_int = int_bit_position (decl);
16138
16139   /* Note that the bit offset is always the distance (in bits) from the
16140      highest-order bit of the "containing object" to the highest-order bit of
16141      the bit-field itself.  Since the "high-order end" of any object or field
16142      is different on big-endian and little-endian machines, the computation
16143      below must take account of these differences.  */
16144   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
16145   highest_order_field_bit_offset = bitpos_int;
16146
16147   if (! BYTES_BIG_ENDIAN)
16148     {
16149       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
16150       highest_order_object_bit_offset += simple_type_size_in_bits (type);
16151     }
16152
16153   bit_offset
16154     = (! BYTES_BIG_ENDIAN
16155        ? highest_order_object_bit_offset - highest_order_field_bit_offset
16156        : highest_order_field_bit_offset - highest_order_object_bit_offset);
16157
16158   if (bit_offset < 0)
16159     add_AT_int (die, DW_AT_bit_offset, bit_offset);
16160   else
16161     add_AT_unsigned (die, DW_AT_bit_offset, (unsigned HOST_WIDE_INT) bit_offset);
16162 }
16163
16164 /* For a FIELD_DECL node which represents a bit field, output an attribute
16165    which specifies the length in bits of the given field.  */
16166
16167 static inline void
16168 add_bit_size_attribute (dw_die_ref die, tree decl)
16169 {
16170   /* Must be a field and a bit field.  */
16171   gcc_assert (TREE_CODE (decl) == FIELD_DECL
16172               && DECL_BIT_FIELD_TYPE (decl));
16173
16174   if (host_integerp (DECL_SIZE (decl), 1))
16175     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
16176 }
16177
16178 /* If the compiled language is ANSI C, then add a 'prototyped'
16179    attribute, if arg types are given for the parameters of a function.  */
16180
16181 static inline void
16182 add_prototyped_attribute (dw_die_ref die, tree func_type)
16183 {
16184   if (get_AT_unsigned (comp_unit_die (), DW_AT_language) == DW_LANG_C89
16185       && prototype_p (func_type))
16186     add_AT_flag (die, DW_AT_prototyped, 1);
16187 }
16188
16189 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
16190    by looking in either the type declaration or object declaration
16191    equate table.  */
16192
16193 static inline dw_die_ref
16194 add_abstract_origin_attribute (dw_die_ref die, tree origin)
16195 {
16196   dw_die_ref origin_die = NULL;
16197
16198   if (TREE_CODE (origin) != FUNCTION_DECL)
16199     {
16200       /* We may have gotten separated from the block for the inlined
16201          function, if we're in an exception handler or some such; make
16202          sure that the abstract function has been written out.
16203
16204          Doing this for nested functions is wrong, however; functions are
16205          distinct units, and our context might not even be inline.  */
16206       tree fn = origin;
16207
16208       if (TYPE_P (fn))
16209         fn = TYPE_STUB_DECL (fn);
16210
16211       fn = decl_function_context (fn);
16212       if (fn)
16213         dwarf2out_abstract_function (fn);
16214     }
16215
16216   if (DECL_P (origin))
16217     origin_die = lookup_decl_die (origin);
16218   else if (TYPE_P (origin))
16219     origin_die = lookup_type_die (origin);
16220
16221   /* XXX: Functions that are never lowered don't always have correct block
16222      trees (in the case of java, they simply have no block tree, in some other
16223      languages).  For these functions, there is nothing we can really do to
16224      output correct debug info for inlined functions in all cases.  Rather
16225      than die, we'll just produce deficient debug info now, in that we will
16226      have variables without a proper abstract origin.  In the future, when all
16227      functions are lowered, we should re-add a gcc_assert (origin_die)
16228      here.  */
16229
16230   if (origin_die)
16231     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
16232   return origin_die;
16233 }
16234
16235 /* We do not currently support the pure_virtual attribute.  */
16236
16237 static inline void
16238 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
16239 {
16240   if (DECL_VINDEX (func_decl))
16241     {
16242       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16243
16244       if (host_integerp (DECL_VINDEX (func_decl), 0))
16245         add_AT_loc (die, DW_AT_vtable_elem_location,
16246                     new_loc_descr (DW_OP_constu,
16247                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
16248                                    0));
16249
16250       /* GNU extension: Record what type this method came from originally.  */
16251       if (debug_info_level > DINFO_LEVEL_TERSE
16252           && DECL_CONTEXT (func_decl))
16253         add_AT_die_ref (die, DW_AT_containing_type,
16254                         lookup_type_die (DECL_CONTEXT (func_decl)));
16255     }
16256 }
16257 \f
16258 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
16259    given decl.  This used to be a vendor extension until after DWARF 4
16260    standardized it.  */
16261
16262 static void
16263 add_linkage_attr (dw_die_ref die, tree decl)
16264 {
16265   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
16266
16267   /* Mimic what assemble_name_raw does with a leading '*'.  */
16268   if (name[0] == '*')
16269     name = &name[1];
16270
16271   if (dwarf_version >= 4)
16272     add_AT_string (die, DW_AT_linkage_name, name);
16273   else
16274     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
16275 }
16276
16277 /* Add source coordinate attributes for the given decl.  */
16278
16279 static void
16280 add_src_coords_attributes (dw_die_ref die, tree decl)
16281 {
16282   expanded_location s;
16283
16284   if (DECL_SOURCE_LOCATION (decl) == UNKNOWN_LOCATION)
16285     return;
16286   s = expand_location (DECL_SOURCE_LOCATION (decl));
16287   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
16288   add_AT_unsigned (die, DW_AT_decl_line, s.line);
16289 }
16290
16291 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
16292
16293 static void
16294 add_linkage_name (dw_die_ref die, tree decl)
16295 {
16296   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
16297        && TREE_PUBLIC (decl)
16298        && !DECL_ABSTRACT (decl)
16299        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
16300        && die->die_tag != DW_TAG_member)
16301     {
16302       /* Defer until we have an assembler name set.  */
16303       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
16304         {
16305           limbo_die_node *asm_name;
16306
16307           asm_name = ggc_alloc_cleared_limbo_die_node ();
16308           asm_name->die = die;
16309           asm_name->created_for = decl;
16310           asm_name->next = deferred_asm_name;
16311           deferred_asm_name = asm_name;
16312         }
16313       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
16314         add_linkage_attr (die, decl);
16315     }
16316 }
16317
16318 /* Add a DW_AT_name attribute and source coordinate attribute for the
16319    given decl, but only if it actually has a name.  */
16320
16321 static void
16322 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
16323 {
16324   tree decl_name;
16325
16326   decl_name = DECL_NAME (decl);
16327   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
16328     {
16329       const char *name = dwarf2_name (decl, 0);
16330       if (name)
16331         add_name_attribute (die, name);
16332       if (! DECL_ARTIFICIAL (decl))
16333         add_src_coords_attributes (die, decl);
16334
16335       add_linkage_name (die, decl);
16336     }
16337
16338 #ifdef VMS_DEBUGGING_INFO
16339   /* Get the function's name, as described by its RTL.  This may be different
16340      from the DECL_NAME name used in the source file.  */
16341   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
16342     {
16343       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
16344                    XEXP (DECL_RTL (decl), 0));
16345       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
16346     }
16347 #endif /* VMS_DEBUGGING_INFO */
16348 }
16349
16350 #ifdef VMS_DEBUGGING_INFO
16351 /* Output the debug main pointer die for VMS */
16352
16353 void
16354 dwarf2out_vms_debug_main_pointer (void)
16355 {
16356   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16357   dw_die_ref die;
16358
16359   /* Allocate the VMS debug main subprogram die.  */
16360   die = ggc_alloc_cleared_die_node ();
16361   die->die_tag = DW_TAG_subprogram;
16362   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
16363   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
16364                                current_function_funcdef_no);
16365   add_AT_lbl_id (die, DW_AT_entry_pc, label);
16366
16367   /* Make it the first child of comp_unit_die ().  */
16368   die->die_parent = comp_unit_die ();
16369   if (comp_unit_die ()->die_child)
16370     {
16371       die->die_sib = comp_unit_die ()->die_child->die_sib;
16372       comp_unit_die ()->die_child->die_sib = die;
16373     }
16374   else
16375     {
16376       die->die_sib = die;
16377       comp_unit_die ()->die_child = die;
16378     }
16379 }
16380 #endif /* VMS_DEBUGGING_INFO */
16381
16382 /* Push a new declaration scope.  */
16383
16384 static void
16385 push_decl_scope (tree scope)
16386 {
16387   VEC_safe_push (tree, gc, decl_scope_table, scope);
16388 }
16389
16390 /* Pop a declaration scope.  */
16391
16392 static inline void
16393 pop_decl_scope (void)
16394 {
16395   VEC_pop (tree, decl_scope_table);
16396 }
16397
16398 /* Return the DIE for the scope that immediately contains this type.
16399    Non-named types get global scope.  Named types nested in other
16400    types get their containing scope if it's open, or global scope
16401    otherwise.  All other types (i.e. function-local named types) get
16402    the current active scope.  */
16403
16404 static dw_die_ref
16405 scope_die_for (tree t, dw_die_ref context_die)
16406 {
16407   dw_die_ref scope_die = NULL;
16408   tree containing_scope;
16409   int i;
16410
16411   /* Non-types always go in the current scope.  */
16412   gcc_assert (TYPE_P (t));
16413
16414   containing_scope = TYPE_CONTEXT (t);
16415
16416   /* Use the containing namespace if it was passed in (for a declaration).  */
16417   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
16418     {
16419       if (context_die == lookup_decl_die (containing_scope))
16420         /* OK */;
16421       else
16422         containing_scope = NULL_TREE;
16423     }
16424
16425   /* Ignore function type "scopes" from the C frontend.  They mean that
16426      a tagged type is local to a parmlist of a function declarator, but
16427      that isn't useful to DWARF.  */
16428   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
16429     containing_scope = NULL_TREE;
16430
16431   if (SCOPE_FILE_SCOPE_P (containing_scope))
16432     scope_die = comp_unit_die ();
16433   else if (TYPE_P (containing_scope))
16434     {
16435       /* For types, we can just look up the appropriate DIE.  But
16436          first we check to see if we're in the middle of emitting it
16437          so we know where the new DIE should go.  */
16438       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
16439         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
16440           break;
16441
16442       if (i < 0)
16443         {
16444           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
16445                       || TREE_ASM_WRITTEN (containing_scope));
16446           /*We are not in the middle of emitting the type
16447             CONTAINING_SCOPE. Let's see if it's emitted already.  */
16448           scope_die = lookup_type_die (containing_scope);
16449
16450           /* If none of the current dies are suitable, we get file scope.  */
16451           if (scope_die == NULL)
16452             scope_die = comp_unit_die ();
16453         }
16454       else
16455         scope_die = lookup_type_die_strip_naming_typedef (containing_scope);
16456     }
16457   else
16458     scope_die = context_die;
16459
16460   return scope_die;
16461 }
16462
16463 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
16464
16465 static inline int
16466 local_scope_p (dw_die_ref context_die)
16467 {
16468   for (; context_die; context_die = context_die->die_parent)
16469     if (context_die->die_tag == DW_TAG_inlined_subroutine
16470         || context_die->die_tag == DW_TAG_subprogram)
16471       return 1;
16472
16473   return 0;
16474 }
16475
16476 /* Returns nonzero if CONTEXT_DIE is a class.  */
16477
16478 static inline int
16479 class_scope_p (dw_die_ref context_die)
16480 {
16481   return (context_die
16482           && (context_die->die_tag == DW_TAG_structure_type
16483               || context_die->die_tag == DW_TAG_class_type
16484               || context_die->die_tag == DW_TAG_interface_type
16485               || context_die->die_tag == DW_TAG_union_type));
16486 }
16487
16488 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
16489    whether or not to treat a DIE in this context as a declaration.  */
16490
16491 static inline int
16492 class_or_namespace_scope_p (dw_die_ref context_die)
16493 {
16494   return (class_scope_p (context_die)
16495           || (context_die && context_die->die_tag == DW_TAG_namespace));
16496 }
16497
16498 /* Many forms of DIEs require a "type description" attribute.  This
16499    routine locates the proper "type descriptor" die for the type given
16500    by 'type', and adds a DW_AT_type attribute below the given die.  */
16501
16502 static void
16503 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
16504                     int decl_volatile, dw_die_ref context_die)
16505 {
16506   enum tree_code code  = TREE_CODE (type);
16507   dw_die_ref type_die  = NULL;
16508
16509   /* ??? If this type is an unnamed subrange type of an integral, floating-point
16510      or fixed-point type, use the inner type.  This is because we have no
16511      support for unnamed types in base_type_die.  This can happen if this is
16512      an Ada subrange type.  Correct solution is emit a subrange type die.  */
16513   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
16514       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
16515     type = TREE_TYPE (type), code = TREE_CODE (type);
16516
16517   if (code == ERROR_MARK
16518       /* Handle a special case.  For functions whose return type is void, we
16519          generate *no* type attribute.  (Note that no object may have type
16520          `void', so this only applies to function return types).  */
16521       || code == VOID_TYPE)
16522     return;
16523
16524   type_die = modified_type_die (type,
16525                                 decl_const || TYPE_READONLY (type),
16526                                 decl_volatile || TYPE_VOLATILE (type),
16527                                 context_die);
16528
16529   if (type_die != NULL)
16530     add_AT_die_ref (object_die, DW_AT_type, type_die);
16531 }
16532
16533 /* Given an object die, add the calling convention attribute for the
16534    function call type.  */
16535 static void
16536 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
16537 {
16538   enum dwarf_calling_convention value = DW_CC_normal;
16539
16540   value = ((enum dwarf_calling_convention)
16541            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
16542
16543   if (is_fortran ()
16544       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
16545     {
16546       /* DWARF 2 doesn't provide a way to identify a program's source-level
16547         entry point.  DW_AT_calling_convention attributes are only meant
16548         to describe functions' calling conventions.  However, lacking a
16549         better way to signal the Fortran main program, we used this for 
16550         a long time, following existing custom.  Now, DWARF 4 has 
16551         DW_AT_main_subprogram, which we add below, but some tools still
16552         rely on the old way, which we thus keep.  */
16553       value = DW_CC_program;
16554
16555       if (dwarf_version >= 4 || !dwarf_strict)
16556         add_AT_flag (subr_die, DW_AT_main_subprogram, 1);
16557     }
16558
16559   /* Only add the attribute if the backend requests it, and
16560      is not DW_CC_normal.  */
16561   if (value && (value != DW_CC_normal))
16562     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
16563 }
16564
16565 /* Given a tree pointer to a struct, class, union, or enum type node, return
16566    a pointer to the (string) tag name for the given type, or zero if the type
16567    was declared without a tag.  */
16568
16569 static const char *
16570 type_tag (const_tree type)
16571 {
16572   const char *name = 0;
16573
16574   if (TYPE_NAME (type) != 0)
16575     {
16576       tree t = 0;
16577
16578       /* Find the IDENTIFIER_NODE for the type name.  */
16579       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
16580           && !TYPE_NAMELESS (type))
16581         t = TYPE_NAME (type);
16582
16583       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
16584          a TYPE_DECL node, regardless of whether or not a `typedef' was
16585          involved.  */
16586       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16587                && ! DECL_IGNORED_P (TYPE_NAME (type)))
16588         {
16589           /* We want to be extra verbose.  Don't call dwarf_name if
16590              DECL_NAME isn't set.  The default hook for decl_printable_name
16591              doesn't like that, and in this context it's correct to return
16592              0, instead of "<anonymous>" or the like.  */
16593           if (DECL_NAME (TYPE_NAME (type))
16594               && !DECL_NAMELESS (TYPE_NAME (type)))
16595             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
16596         }
16597
16598       /* Now get the name as a string, or invent one.  */
16599       if (!name && t != 0)
16600         name = IDENTIFIER_POINTER (t);
16601     }
16602
16603   return (name == 0 || *name == '\0') ? 0 : name;
16604 }
16605
16606 /* Return the type associated with a data member, make a special check
16607    for bit field types.  */
16608
16609 static inline tree
16610 member_declared_type (const_tree member)
16611 {
16612   return (DECL_BIT_FIELD_TYPE (member)
16613           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
16614 }
16615
16616 /* Get the decl's label, as described by its RTL. This may be different
16617    from the DECL_NAME name used in the source file.  */
16618
16619 #if 0
16620 static const char *
16621 decl_start_label (tree decl)
16622 {
16623   rtx x;
16624   const char *fnname;
16625
16626   x = DECL_RTL (decl);
16627   gcc_assert (MEM_P (x));
16628
16629   x = XEXP (x, 0);
16630   gcc_assert (GET_CODE (x) == SYMBOL_REF);
16631
16632   fnname = XSTR (x, 0);
16633   return fnname;
16634 }
16635 #endif
16636 \f
16637 /* These routines generate the internal representation of the DIE's for
16638    the compilation unit.  Debugging information is collected by walking
16639    the declaration trees passed in from dwarf2out_decl().  */
16640
16641 static void
16642 gen_array_type_die (tree type, dw_die_ref context_die)
16643 {
16644   dw_die_ref scope_die = scope_die_for (type, context_die);
16645   dw_die_ref array_die;
16646
16647   /* GNU compilers represent multidimensional array types as sequences of one
16648      dimensional array types whose element types are themselves array types.
16649      We sometimes squish that down to a single array_type DIE with multiple
16650      subscripts in the Dwarf debugging info.  The draft Dwarf specification
16651      say that we are allowed to do this kind of compression in C, because
16652      there is no difference between an array of arrays and a multidimensional
16653      array.  We don't do this for Ada to remain as close as possible to the
16654      actual representation, which is especially important against the language
16655      flexibilty wrt arrays of variable size.  */
16656
16657   bool collapse_nested_arrays = !is_ada ();
16658   tree element_type;
16659
16660   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
16661      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
16662   if (TYPE_STRING_FLAG (type)
16663       && TREE_CODE (type) == ARRAY_TYPE
16664       && is_fortran ()
16665       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
16666     {
16667       HOST_WIDE_INT size;
16668
16669       array_die = new_die (DW_TAG_string_type, scope_die, type);
16670       add_name_attribute (array_die, type_tag (type));
16671       equate_type_number_to_die (type, array_die);
16672       size = int_size_in_bytes (type);
16673       if (size >= 0)
16674         add_AT_unsigned (array_die, DW_AT_byte_size, size);
16675       else if (TYPE_DOMAIN (type) != NULL_TREE
16676                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
16677                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
16678         {
16679           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
16680           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
16681
16682           size = int_size_in_bytes (TREE_TYPE (szdecl));
16683           if (loc && size > 0)
16684             {
16685               add_AT_location_description (array_die, DW_AT_string_length, loc);
16686               if (size != DWARF2_ADDR_SIZE)
16687                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
16688             }
16689         }
16690       return;
16691     }
16692
16693   /* ??? The SGI dwarf reader fails for array of array of enum types
16694      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
16695      array type comes before the outer array type.  We thus call gen_type_die
16696      before we new_die and must prevent nested array types collapsing for this
16697      target.  */
16698
16699 #ifdef MIPS_DEBUGGING_INFO
16700   gen_type_die (TREE_TYPE (type), context_die);
16701   collapse_nested_arrays = false;
16702 #endif
16703
16704   array_die = new_die (DW_TAG_array_type, scope_die, type);
16705   add_name_attribute (array_die, type_tag (type));
16706   add_gnat_descriptive_type_attribute (array_die, type, context_die);
16707   if (TYPE_ARTIFICIAL (type))
16708     add_AT_flag (array_die, DW_AT_artificial, 1);
16709   equate_type_number_to_die (type, array_die);
16710
16711   if (TREE_CODE (type) == VECTOR_TYPE)
16712     add_AT_flag (array_die, DW_AT_GNU_vector, 1);
16713
16714   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16715   if (is_fortran ()
16716       && TREE_CODE (type) == ARRAY_TYPE
16717       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
16718       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
16719     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16720
16721 #if 0
16722   /* We default the array ordering.  SDB will probably do
16723      the right things even if DW_AT_ordering is not present.  It's not even
16724      an issue until we start to get into multidimensional arrays anyway.  If
16725      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
16726      then we'll have to put the DW_AT_ordering attribute back in.  (But if
16727      and when we find out that we need to put these in, we will only do so
16728      for multidimensional arrays.  */
16729   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
16730 #endif
16731
16732 #ifdef MIPS_DEBUGGING_INFO
16733   /* The SGI compilers handle arrays of unknown bound by setting
16734      AT_declaration and not emitting any subrange DIEs.  */
16735   if (TREE_CODE (type) == ARRAY_TYPE
16736       && ! TYPE_DOMAIN (type))
16737     add_AT_flag (array_die, DW_AT_declaration, 1);
16738   else
16739 #endif
16740   if (TREE_CODE (type) == VECTOR_TYPE)
16741     {
16742       /* For VECTOR_TYPEs we use an array die with appropriate bounds.  */
16743       dw_die_ref subrange_die = new_die (DW_TAG_subrange_type, array_die, NULL);
16744       add_bound_info (subrange_die, DW_AT_lower_bound, size_zero_node);
16745       add_bound_info (subrange_die, DW_AT_upper_bound,
16746                       size_int (TYPE_VECTOR_SUBPARTS (type) - 1));
16747     }
16748   else
16749     add_subscript_info (array_die, type, collapse_nested_arrays);
16750
16751   /* Add representation of the type of the elements of this array type and
16752      emit the corresponding DIE if we haven't done it already.  */
16753   element_type = TREE_TYPE (type);
16754   if (collapse_nested_arrays)
16755     while (TREE_CODE (element_type) == ARRAY_TYPE)
16756       {
16757         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
16758           break;
16759         element_type = TREE_TYPE (element_type);
16760       }
16761
16762 #ifndef MIPS_DEBUGGING_INFO
16763   gen_type_die (element_type, context_die);
16764 #endif
16765
16766   add_type_attribute (array_die, element_type, 0, 0, context_die);
16767
16768   if (get_AT (array_die, DW_AT_name))
16769     add_pubtype (type, array_die);
16770 }
16771
16772 static dw_loc_descr_ref
16773 descr_info_loc (tree val, tree base_decl)
16774 {
16775   HOST_WIDE_INT size;
16776   dw_loc_descr_ref loc, loc2;
16777   enum dwarf_location_atom op;
16778
16779   if (val == base_decl)
16780     return new_loc_descr (DW_OP_push_object_address, 0, 0);
16781
16782   switch (TREE_CODE (val))
16783     {
16784     CASE_CONVERT:
16785       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16786     case VAR_DECL:
16787       return loc_descriptor_from_tree (val, 0);
16788     case INTEGER_CST:
16789       if (host_integerp (val, 0))
16790         return int_loc_descriptor (tree_low_cst (val, 0));
16791       break;
16792     case INDIRECT_REF:
16793       size = int_size_in_bytes (TREE_TYPE (val));
16794       if (size < 0)
16795         break;
16796       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16797       if (!loc)
16798         break;
16799       if (size == DWARF2_ADDR_SIZE)
16800         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
16801       else
16802         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
16803       return loc;
16804     case POINTER_PLUS_EXPR:
16805     case PLUS_EXPR:
16806       if (host_integerp (TREE_OPERAND (val, 1), 1)
16807           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
16808              < 16384)
16809         {
16810           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16811           if (!loc)
16812             break;
16813           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
16814         }
16815       else
16816         {
16817           op = DW_OP_plus;
16818         do_binop:
16819           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
16820           if (!loc)
16821             break;
16822           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
16823           if (!loc2)
16824             break;
16825           add_loc_descr (&loc, loc2);
16826           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
16827         }
16828       return loc;
16829     case MINUS_EXPR:
16830       op = DW_OP_minus;
16831       goto do_binop;
16832     case MULT_EXPR:
16833       op = DW_OP_mul;
16834       goto do_binop;
16835     case EQ_EXPR:
16836       op = DW_OP_eq;
16837       goto do_binop;
16838     case NE_EXPR:
16839       op = DW_OP_ne;
16840       goto do_binop;
16841     default:
16842       break;
16843     }
16844   return NULL;
16845 }
16846
16847 static void
16848 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
16849                       tree val, tree base_decl)
16850 {
16851   dw_loc_descr_ref loc;
16852
16853   if (host_integerp (val, 0))
16854     {
16855       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
16856       return;
16857     }
16858
16859   loc = descr_info_loc (val, base_decl);
16860   if (!loc)
16861     return;
16862
16863   add_AT_loc (die, attr, loc);
16864 }
16865
16866 /* This routine generates DIE for array with hidden descriptor, details
16867    are filled into *info by a langhook.  */
16868
16869 static void
16870 gen_descr_array_type_die (tree type, struct array_descr_info *info,
16871                           dw_die_ref context_die)
16872 {
16873   dw_die_ref scope_die = scope_die_for (type, context_die);
16874   dw_die_ref array_die;
16875   int dim;
16876
16877   array_die = new_die (DW_TAG_array_type, scope_die, type);
16878   add_name_attribute (array_die, type_tag (type));
16879   equate_type_number_to_die (type, array_die);
16880
16881   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
16882   if (is_fortran ()
16883       && info->ndimensions >= 2)
16884     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
16885
16886   if (info->data_location)
16887     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
16888                           info->base_decl);
16889   if (info->associated)
16890     add_descr_info_field (array_die, DW_AT_associated, info->associated,
16891                           info->base_decl);
16892   if (info->allocated)
16893     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
16894                           info->base_decl);
16895
16896   for (dim = 0; dim < info->ndimensions; dim++)
16897     {
16898       dw_die_ref subrange_die
16899         = new_die (DW_TAG_subrange_type, array_die, NULL);
16900
16901       if (info->dimen[dim].lower_bound)
16902         {
16903           /* If it is the default value, omit it.  */
16904           int dflt;
16905
16906           if (host_integerp (info->dimen[dim].lower_bound, 0)
16907               && (dflt = lower_bound_default ()) != -1
16908               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
16909             ;
16910           else
16911             add_descr_info_field (subrange_die, DW_AT_lower_bound,
16912                                   info->dimen[dim].lower_bound,
16913                                   info->base_decl);
16914         }
16915       if (info->dimen[dim].upper_bound)
16916         add_descr_info_field (subrange_die, DW_AT_upper_bound,
16917                               info->dimen[dim].upper_bound,
16918                               info->base_decl);
16919       if (info->dimen[dim].stride)
16920         add_descr_info_field (subrange_die, DW_AT_byte_stride,
16921                               info->dimen[dim].stride,
16922                               info->base_decl);
16923     }
16924
16925   gen_type_die (info->element_type, context_die);
16926   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
16927
16928   if (get_AT (array_die, DW_AT_name))
16929     add_pubtype (type, array_die);
16930 }
16931
16932 #if 0
16933 static void
16934 gen_entry_point_die (tree decl, dw_die_ref context_die)
16935 {
16936   tree origin = decl_ultimate_origin (decl);
16937   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
16938
16939   if (origin != NULL)
16940     add_abstract_origin_attribute (decl_die, origin);
16941   else
16942     {
16943       add_name_and_src_coords_attributes (decl_die, decl);
16944       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
16945                           0, 0, context_die);
16946     }
16947
16948   if (DECL_ABSTRACT (decl))
16949     equate_decl_number_to_die (decl, decl_die);
16950   else
16951     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
16952 }
16953 #endif
16954
16955 /* Walk through the list of incomplete types again, trying once more to
16956    emit full debugging info for them.  */
16957
16958 static void
16959 retry_incomplete_types (void)
16960 {
16961   int i;
16962
16963   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
16964     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
16965                                   DINFO_USAGE_DIR_USE))
16966       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die ());
16967 }
16968
16969 /* Determine what tag to use for a record type.  */
16970
16971 static enum dwarf_tag
16972 record_type_tag (tree type)
16973 {
16974   if (! lang_hooks.types.classify_record)
16975     return DW_TAG_structure_type;
16976
16977   switch (lang_hooks.types.classify_record (type))
16978     {
16979     case RECORD_IS_STRUCT:
16980       return DW_TAG_structure_type;
16981
16982     case RECORD_IS_CLASS:
16983       return DW_TAG_class_type;
16984
16985     case RECORD_IS_INTERFACE:
16986       if (dwarf_version >= 3 || !dwarf_strict)
16987         return DW_TAG_interface_type;
16988       return DW_TAG_structure_type;
16989
16990     default:
16991       gcc_unreachable ();
16992     }
16993 }
16994
16995 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
16996    include all of the information about the enumeration values also. Each
16997    enumerated type name/value is listed as a child of the enumerated type
16998    DIE.  */
16999
17000 static dw_die_ref
17001 gen_enumeration_type_die (tree type, dw_die_ref context_die)
17002 {
17003   dw_die_ref type_die = lookup_type_die (type);
17004
17005   if (type_die == NULL)
17006     {
17007       type_die = new_die (DW_TAG_enumeration_type,
17008                           scope_die_for (type, context_die), type);
17009       equate_type_number_to_die (type, type_die);
17010       add_name_attribute (type_die, type_tag (type));
17011       add_gnat_descriptive_type_attribute (type_die, type, context_die);
17012       if (TYPE_ARTIFICIAL (type))
17013         add_AT_flag (type_die, DW_AT_artificial, 1);
17014       if (dwarf_version >= 4 || !dwarf_strict)
17015         {
17016           if (ENUM_IS_SCOPED (type))
17017             add_AT_flag (type_die, DW_AT_enum_class, 1);
17018           if (ENUM_IS_OPAQUE (type))
17019             add_AT_flag (type_die, DW_AT_declaration, 1);
17020         }
17021     }
17022   else if (! TYPE_SIZE (type))
17023     return type_die;
17024   else
17025     remove_AT (type_die, DW_AT_declaration);
17026
17027   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
17028      given enum type is incomplete, do not generate the DW_AT_byte_size
17029      attribute or the DW_AT_element_list attribute.  */
17030   if (TYPE_SIZE (type))
17031     {
17032       tree link;
17033
17034       TREE_ASM_WRITTEN (type) = 1;
17035       add_byte_size_attribute (type_die, type);
17036       if (TYPE_STUB_DECL (type) != NULL_TREE)
17037         {
17038           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
17039           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
17040         }
17041
17042       /* If the first reference to this type was as the return type of an
17043          inline function, then it may not have a parent.  Fix this now.  */
17044       if (type_die->die_parent == NULL)
17045         add_child_die (scope_die_for (type, context_die), type_die);
17046
17047       for (link = TYPE_VALUES (type);
17048            link != NULL; link = TREE_CHAIN (link))
17049         {
17050           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
17051           tree value = TREE_VALUE (link);
17052
17053           add_name_attribute (enum_die,
17054                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
17055
17056           if (TREE_CODE (value) == CONST_DECL)
17057             value = DECL_INITIAL (value);
17058
17059           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
17060             /* DWARF2 does not provide a way of indicating whether or
17061                not enumeration constants are signed or unsigned.  GDB
17062                always assumes the values are signed, so we output all
17063                values as if they were signed.  That means that
17064                enumeration constants with very large unsigned values
17065                will appear to have negative values in the debugger.  */
17066             add_AT_int (enum_die, DW_AT_const_value,
17067                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
17068         }
17069     }
17070   else
17071     add_AT_flag (type_die, DW_AT_declaration, 1);
17072
17073   if (get_AT (type_die, DW_AT_name))
17074     add_pubtype (type, type_die);
17075
17076   return type_die;
17077 }
17078
17079 /* Generate a DIE to represent either a real live formal parameter decl or to
17080    represent just the type of some formal parameter position in some function
17081    type.
17082
17083    Note that this routine is a bit unusual because its argument may be a
17084    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
17085    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
17086    node.  If it's the former then this function is being called to output a
17087    DIE to represent a formal parameter object (or some inlining thereof).  If
17088    it's the latter, then this function is only being called to output a
17089    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
17090    argument type of some subprogram type.
17091    If EMIT_NAME_P is true, name and source coordinate attributes
17092    are emitted.  */
17093
17094 static dw_die_ref
17095 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
17096                           dw_die_ref context_die)
17097 {
17098   tree node_or_origin = node ? node : origin;
17099   tree ultimate_origin;
17100   dw_die_ref parm_die
17101     = new_die (DW_TAG_formal_parameter, context_die, node);
17102
17103   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
17104     {
17105     case tcc_declaration:
17106       ultimate_origin = decl_ultimate_origin (node_or_origin);
17107       if (node || ultimate_origin)
17108         origin = ultimate_origin;
17109       if (origin != NULL)
17110         add_abstract_origin_attribute (parm_die, origin);
17111       else if (emit_name_p)
17112         add_name_and_src_coords_attributes (parm_die, node);
17113       if (origin == NULL
17114           || (! DECL_ABSTRACT (node_or_origin)
17115               && variably_modified_type_p (TREE_TYPE (node_or_origin),
17116                                            decl_function_context
17117                                                             (node_or_origin))))
17118         {
17119           tree type = TREE_TYPE (node_or_origin);
17120           if (decl_by_reference_p (node_or_origin))
17121             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
17122                                 context_die);
17123           else
17124             add_type_attribute (parm_die, type,
17125                                 TREE_READONLY (node_or_origin),
17126                                 TREE_THIS_VOLATILE (node_or_origin),
17127                                 context_die);
17128         }
17129       if (origin == NULL && DECL_ARTIFICIAL (node))
17130         add_AT_flag (parm_die, DW_AT_artificial, 1);
17131
17132       if (node && node != origin)
17133         equate_decl_number_to_die (node, parm_die);
17134       if (! DECL_ABSTRACT (node_or_origin))
17135         add_location_or_const_value_attribute (parm_die, node_or_origin,
17136                                                node == NULL, DW_AT_location);
17137
17138       break;
17139
17140     case tcc_type:
17141       /* We were called with some kind of a ..._TYPE node.  */
17142       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
17143       break;
17144
17145     default:
17146       gcc_unreachable ();
17147     }
17148
17149   return parm_die;
17150 }
17151
17152 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
17153    children DW_TAG_formal_parameter DIEs representing the arguments of the
17154    parameter pack.
17155
17156    PARM_PACK must be a function parameter pack.
17157    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
17158    must point to the subsequent arguments of the function PACK_ARG belongs to.
17159    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
17160    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
17161    following the last one for which a DIE was generated.  */
17162
17163 static dw_die_ref
17164 gen_formal_parameter_pack_die  (tree parm_pack,
17165                                 tree pack_arg,
17166                                 dw_die_ref subr_die,
17167                                 tree *next_arg)
17168 {
17169   tree arg;
17170   dw_die_ref parm_pack_die;
17171
17172   gcc_assert (parm_pack
17173               && lang_hooks.function_parameter_pack_p (parm_pack)
17174               && subr_die);
17175
17176   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
17177   add_src_coords_attributes (parm_pack_die, parm_pack);
17178
17179   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
17180     {
17181       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
17182                                                                  parm_pack))
17183         break;
17184       gen_formal_parameter_die (arg, NULL,
17185                                 false /* Don't emit name attribute.  */,
17186                                 parm_pack_die);
17187     }
17188   if (next_arg)
17189     *next_arg = arg;
17190   return parm_pack_die;
17191 }
17192
17193 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
17194    at the end of an (ANSI prototyped) formal parameters list.  */
17195
17196 static void
17197 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
17198 {
17199   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
17200 }
17201
17202 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
17203    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
17204    parameters as specified in some function type specification (except for
17205    those which appear as part of a function *definition*).  */
17206
17207 static void
17208 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
17209 {
17210   tree link;
17211   tree formal_type = NULL;
17212   tree first_parm_type;
17213   tree arg;
17214
17215   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
17216     {
17217       arg = DECL_ARGUMENTS (function_or_method_type);
17218       function_or_method_type = TREE_TYPE (function_or_method_type);
17219     }
17220   else
17221     arg = NULL_TREE;
17222
17223   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
17224
17225   /* Make our first pass over the list of formal parameter types and output a
17226      DW_TAG_formal_parameter DIE for each one.  */
17227   for (link = first_parm_type; link; )
17228     {
17229       dw_die_ref parm_die;
17230
17231       formal_type = TREE_VALUE (link);
17232       if (formal_type == void_type_node)
17233         break;
17234
17235       /* Output a (nameless) DIE to represent the formal parameter itself.  */
17236       parm_die = gen_formal_parameter_die (formal_type, NULL,
17237                                            true /* Emit name attribute.  */,
17238                                            context_die);
17239       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
17240           && link == first_parm_type)
17241         {
17242           add_AT_flag (parm_die, DW_AT_artificial, 1);
17243           if (dwarf_version >= 3 || !dwarf_strict)
17244             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
17245         }
17246       else if (arg && DECL_ARTIFICIAL (arg))
17247         add_AT_flag (parm_die, DW_AT_artificial, 1);
17248
17249       link = TREE_CHAIN (link);
17250       if (arg)
17251         arg = DECL_CHAIN (arg);
17252     }
17253
17254   /* If this function type has an ellipsis, add a
17255      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
17256   if (formal_type != void_type_node)
17257     gen_unspecified_parameters_die (function_or_method_type, context_die);
17258
17259   /* Make our second (and final) pass over the list of formal parameter types
17260      and output DIEs to represent those types (as necessary).  */
17261   for (link = TYPE_ARG_TYPES (function_or_method_type);
17262        link && TREE_VALUE (link);
17263        link = TREE_CHAIN (link))
17264     gen_type_die (TREE_VALUE (link), context_die);
17265 }
17266
17267 /* We want to generate the DIE for TYPE so that we can generate the
17268    die for MEMBER, which has been defined; we will need to refer back
17269    to the member declaration nested within TYPE.  If we're trying to
17270    generate minimal debug info for TYPE, processing TYPE won't do the
17271    trick; we need to attach the member declaration by hand.  */
17272
17273 static void
17274 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
17275 {
17276   gen_type_die (type, context_die);
17277
17278   /* If we're trying to avoid duplicate debug info, we may not have
17279      emitted the member decl for this function.  Emit it now.  */
17280   if (TYPE_STUB_DECL (type)
17281       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
17282       && ! lookup_decl_die (member))
17283     {
17284       dw_die_ref type_die;
17285       gcc_assert (!decl_ultimate_origin (member));
17286
17287       push_decl_scope (type);
17288       type_die = lookup_type_die_strip_naming_typedef (type);
17289       if (TREE_CODE (member) == FUNCTION_DECL)
17290         gen_subprogram_die (member, type_die);
17291       else if (TREE_CODE (member) == FIELD_DECL)
17292         {
17293           /* Ignore the nameless fields that are used to skip bits but handle
17294              C++ anonymous unions and structs.  */
17295           if (DECL_NAME (member) != NULL_TREE
17296               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
17297               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
17298             {
17299               gen_type_die (member_declared_type (member), type_die);
17300               gen_field_die (member, type_die);
17301             }
17302         }
17303       else
17304         gen_variable_die (member, NULL_TREE, type_die);
17305
17306       pop_decl_scope ();
17307     }
17308 }
17309
17310 /* Generate the DWARF2 info for the "abstract" instance of a function which we
17311    may later generate inlined and/or out-of-line instances of.  */
17312
17313 static void
17314 dwarf2out_abstract_function (tree decl)
17315 {
17316   dw_die_ref old_die;
17317   tree save_fn;
17318   tree context;
17319   int was_abstract;
17320   htab_t old_decl_loc_table;
17321   htab_t old_cached_dw_loc_list_table;
17322   int old_call_site_count, old_tail_call_site_count;
17323   struct call_arg_loc_node *old_call_arg_locations;
17324
17325   /* Make sure we have the actual abstract inline, not a clone.  */
17326   decl = DECL_ORIGIN (decl);
17327
17328   old_die = lookup_decl_die (decl);
17329   if (old_die && get_AT (old_die, DW_AT_inline))
17330     /* We've already generated the abstract instance.  */
17331     return;
17332
17333   /* We can be called while recursively when seeing block defining inlined subroutine
17334      DIE.  Be sure to not clobber the outer location table nor use it or we would
17335      get locations in abstract instantces.  */
17336   old_decl_loc_table = decl_loc_table;
17337   decl_loc_table = NULL;
17338   old_cached_dw_loc_list_table = cached_dw_loc_list_table;
17339   cached_dw_loc_list_table = NULL;
17340   old_call_arg_locations = call_arg_locations;
17341   call_arg_locations = NULL;
17342   old_call_site_count = call_site_count;
17343   call_site_count = -1;
17344   old_tail_call_site_count = tail_call_site_count;
17345   tail_call_site_count = -1;
17346
17347   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
17348      we don't get confused by DECL_ABSTRACT.  */
17349   if (debug_info_level > DINFO_LEVEL_TERSE)
17350     {
17351       context = decl_class_context (decl);
17352       if (context)
17353         gen_type_die_for_member
17354           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die ());
17355     }
17356
17357   /* Pretend we've just finished compiling this function.  */
17358   save_fn = current_function_decl;
17359   current_function_decl = decl;
17360   push_cfun (DECL_STRUCT_FUNCTION (decl));
17361
17362   was_abstract = DECL_ABSTRACT (decl);
17363   set_decl_abstract_flags (decl, 1);
17364   dwarf2out_decl (decl);
17365   if (! was_abstract)
17366     set_decl_abstract_flags (decl, 0);
17367
17368   current_function_decl = save_fn;
17369   decl_loc_table = old_decl_loc_table;
17370   cached_dw_loc_list_table = old_cached_dw_loc_list_table;
17371   call_arg_locations = old_call_arg_locations;
17372   call_site_count = old_call_site_count;
17373   tail_call_site_count = old_tail_call_site_count;
17374   pop_cfun ();
17375 }
17376
17377 /* Helper function of premark_used_types() which gets called through
17378    htab_traverse.
17379
17380    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17381    marked as unused by prune_unused_types.  */
17382
17383 static int
17384 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
17385 {
17386   tree type;
17387   dw_die_ref die;
17388
17389   type = (tree) *slot;
17390   die = lookup_type_die (type);
17391   if (die != NULL)
17392     die->die_perennial_p = 1;
17393   return 1;
17394 }
17395
17396 /* Helper function of premark_types_used_by_global_vars which gets called
17397    through htab_traverse.
17398
17399    Marks the DIE of a given type in *SLOT as perennial, so it never gets
17400    marked as unused by prune_unused_types. The DIE of the type is marked
17401    only if the global variable using the type will actually be emitted.  */
17402
17403 static int
17404 premark_types_used_by_global_vars_helper (void **slot,
17405                                           void *data ATTRIBUTE_UNUSED)
17406 {
17407   struct types_used_by_vars_entry *entry;
17408   dw_die_ref die;
17409
17410   entry = (struct types_used_by_vars_entry *) *slot;
17411   gcc_assert (entry->type != NULL
17412               && entry->var_decl != NULL);
17413   die = lookup_type_die (entry->type);
17414   if (die)
17415     {
17416       /* Ask cgraph if the global variable really is to be emitted.
17417          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
17418       struct varpool_node *node = varpool_get_node (entry->var_decl);
17419       if (node && node->needed)
17420         {
17421           die->die_perennial_p = 1;
17422           /* Keep the parent DIEs as well.  */
17423           while ((die = die->die_parent) && die->die_perennial_p == 0)
17424             die->die_perennial_p = 1;
17425         }
17426     }
17427   return 1;
17428 }
17429
17430 /* Mark all members of used_types_hash as perennial.  */
17431
17432 static void
17433 premark_used_types (void)
17434 {
17435   if (cfun && cfun->used_types_hash)
17436     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
17437 }
17438
17439 /* Mark all members of types_used_by_vars_entry as perennial.  */
17440
17441 static void
17442 premark_types_used_by_global_vars (void)
17443 {
17444   if (types_used_by_vars_hash)
17445     htab_traverse (types_used_by_vars_hash,
17446                    premark_types_used_by_global_vars_helper, NULL);
17447 }
17448
17449 /* Generate a DW_TAG_GNU_call_site DIE in function DECL under SUBR_DIE
17450    for CA_LOC call arg loc node.  */
17451
17452 static dw_die_ref
17453 gen_call_site_die (tree decl, dw_die_ref subr_die,
17454                    struct call_arg_loc_node *ca_loc)
17455 {
17456   dw_die_ref stmt_die = NULL, die;
17457   tree block = ca_loc->block;
17458
17459   while (block
17460          && block != DECL_INITIAL (decl)
17461          && TREE_CODE (block) == BLOCK)
17462     {
17463       if (VEC_length (dw_die_ref, block_map) > BLOCK_NUMBER (block))
17464         stmt_die = VEC_index (dw_die_ref, block_map, BLOCK_NUMBER (block));
17465       if (stmt_die)
17466         break;
17467       block = BLOCK_SUPERCONTEXT (block);
17468     }
17469   if (stmt_die == NULL)
17470     stmt_die = subr_die;
17471   die = new_die (DW_TAG_GNU_call_site, stmt_die, NULL_TREE);
17472   add_AT_lbl_id (die, DW_AT_low_pc, ca_loc->label);
17473   if (ca_loc->tail_call_p)
17474     add_AT_flag (die, DW_AT_GNU_tail_call, 1);
17475   if (ca_loc->symbol_ref)
17476     {
17477       dw_die_ref tdie = lookup_decl_die (SYMBOL_REF_DECL (ca_loc->symbol_ref));
17478       if (tdie)
17479         add_AT_die_ref (die, DW_AT_abstract_origin, tdie);
17480       else
17481         add_AT_addr (die, DW_AT_abstract_origin, ca_loc->symbol_ref);
17482     }
17483   return die;
17484 }
17485
17486 /* Generate a DIE to represent a declared function (either file-scope or
17487    block-local).  */
17488
17489 static void
17490 gen_subprogram_die (tree decl, dw_die_ref context_die)
17491 {
17492   tree origin = decl_ultimate_origin (decl);
17493   dw_die_ref subr_die;
17494   tree outer_scope;
17495   dw_die_ref old_die = lookup_decl_die (decl);
17496   int declaration = (current_function_decl != decl
17497                      || class_or_namespace_scope_p (context_die));
17498
17499   premark_used_types ();
17500
17501   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
17502      started to generate the abstract instance of an inline, decided to output
17503      its containing class, and proceeded to emit the declaration of the inline
17504      from the member list for the class.  If so, DECLARATION takes priority;
17505      we'll get back to the abstract instance when done with the class.  */
17506
17507   /* The class-scope declaration DIE must be the primary DIE.  */
17508   if (origin && declaration && class_or_namespace_scope_p (context_die))
17509     {
17510       origin = NULL;
17511       gcc_assert (!old_die);
17512     }
17513
17514   /* Now that the C++ front end lazily declares artificial member fns, we
17515      might need to retrofit the declaration into its class.  */
17516   if (!declaration && !origin && !old_die
17517       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
17518       && !class_or_namespace_scope_p (context_die)
17519       && debug_info_level > DINFO_LEVEL_TERSE)
17520     old_die = force_decl_die (decl);
17521
17522   if (origin != NULL)
17523     {
17524       gcc_assert (!declaration || local_scope_p (context_die));
17525
17526       /* Fixup die_parent for the abstract instance of a nested
17527          inline function.  */
17528       if (old_die && old_die->die_parent == NULL)
17529         add_child_die (context_die, old_die);
17530
17531       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17532       add_abstract_origin_attribute (subr_die, origin);
17533       /*  This is where the actual code for a cloned function is.
17534           Let's emit linkage name attribute for it.  This helps
17535           debuggers to e.g, set breakpoints into
17536           constructors/destructors when the user asks "break
17537           K::K".  */
17538       add_linkage_name (subr_die, decl);
17539     }
17540   else if (old_die)
17541     {
17542       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17543       struct dwarf_file_data * file_index = lookup_filename (s.file);
17544
17545       if (!get_AT_flag (old_die, DW_AT_declaration)
17546           /* We can have a normal definition following an inline one in the
17547              case of redefinition of GNU C extern inlines.
17548              It seems reasonable to use AT_specification in this case.  */
17549           && !get_AT (old_die, DW_AT_inline))
17550         {
17551           /* Detect and ignore this case, where we are trying to output
17552              something we have already output.  */
17553           return;
17554         }
17555
17556       /* If the definition comes from the same place as the declaration,
17557          maybe use the old DIE.  We always want the DIE for this function
17558          that has the *_pc attributes to be under comp_unit_die so the
17559          debugger can find it.  We also need to do this for abstract
17560          instances of inlines, since the spec requires the out-of-line copy
17561          to have the same parent.  For local class methods, this doesn't
17562          apply; we just use the old DIE.  */
17563       if ((is_cu_die (old_die->die_parent) || context_die == NULL)
17564           && (DECL_ARTIFICIAL (decl)
17565               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
17566                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
17567                       == (unsigned) s.line))))
17568         {
17569           subr_die = old_die;
17570
17571           /* Clear out the declaration attribute and the formal parameters.
17572              Do not remove all children, because it is possible that this
17573              declaration die was forced using force_decl_die(). In such
17574              cases die that forced declaration die (e.g. TAG_imported_module)
17575              is one of the children that we do not want to remove.  */
17576           remove_AT (subr_die, DW_AT_declaration);
17577           remove_AT (subr_die, DW_AT_object_pointer);
17578           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
17579         }
17580       else
17581         {
17582           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17583           add_AT_specification (subr_die, old_die);
17584           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
17585             add_AT_file (subr_die, DW_AT_decl_file, file_index);
17586           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
17587             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
17588         }
17589     }
17590   else
17591     {
17592       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
17593
17594       if (TREE_PUBLIC (decl))
17595         add_AT_flag (subr_die, DW_AT_external, 1);
17596
17597       add_name_and_src_coords_attributes (subr_die, decl);
17598       if (debug_info_level > DINFO_LEVEL_TERSE)
17599         {
17600           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
17601           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
17602                               0, 0, context_die);
17603         }
17604
17605       add_pure_or_virtual_attribute (subr_die, decl);
17606       if (DECL_ARTIFICIAL (decl))
17607         add_AT_flag (subr_die, DW_AT_artificial, 1);
17608
17609       add_accessibility_attribute (subr_die, decl);
17610     }
17611
17612   if (declaration)
17613     {
17614       if (!old_die || !get_AT (old_die, DW_AT_inline))
17615         {
17616           add_AT_flag (subr_die, DW_AT_declaration, 1);
17617
17618           /* If this is an explicit function declaration then generate
17619              a DW_AT_explicit attribute.  */
17620           if (lang_hooks.decls.function_decl_explicit_p (decl)
17621               && (dwarf_version >= 3 || !dwarf_strict))
17622             add_AT_flag (subr_die, DW_AT_explicit, 1);
17623
17624           /* The first time we see a member function, it is in the context of
17625              the class to which it belongs.  We make sure of this by emitting
17626              the class first.  The next time is the definition, which is
17627              handled above.  The two may come from the same source text.
17628
17629              Note that force_decl_die() forces function declaration die. It is
17630              later reused to represent definition.  */
17631           equate_decl_number_to_die (decl, subr_die);
17632         }
17633     }
17634   else if (DECL_ABSTRACT (decl))
17635     {
17636       if (DECL_DECLARED_INLINE_P (decl))
17637         {
17638           if (cgraph_function_possibly_inlined_p (decl))
17639             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
17640           else
17641             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
17642         }
17643       else
17644         {
17645           if (cgraph_function_possibly_inlined_p (decl))
17646             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
17647           else
17648             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
17649         }
17650
17651       if (DECL_DECLARED_INLINE_P (decl)
17652           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
17653         add_AT_flag (subr_die, DW_AT_artificial, 1);
17654
17655       equate_decl_number_to_die (decl, subr_die);
17656     }
17657   else if (!DECL_EXTERNAL (decl))
17658     {
17659       HOST_WIDE_INT cfa_fb_offset;
17660
17661       if (!old_die || !get_AT (old_die, DW_AT_inline))
17662         equate_decl_number_to_die (decl, subr_die);
17663
17664       if (!flag_reorder_blocks_and_partition)
17665         {
17666           dw_fde_ref fde = &fde_table[current_funcdef_fde];
17667           if (fde->dw_fde_begin)
17668             {
17669               /* We have already generated the labels.  */
17670               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17671               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17672             }
17673           else
17674             {
17675               /* Create start/end labels and add the range.  */
17676               char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
17677               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
17678                                            current_function_funcdef_no);
17679               add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
17680               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
17681                                            current_function_funcdef_no);
17682               add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
17683             }
17684
17685 #if VMS_DEBUGGING_INFO
17686       /* HP OpenVMS Industry Standard 64: DWARF Extensions
17687          Section 2.3 Prologue and Epilogue Attributes:
17688          When a breakpoint is set on entry to a function, it is generally
17689          desirable for execution to be suspended, not on the very first
17690          instruction of the function, but rather at a point after the
17691          function's frame has been set up, after any language defined local
17692          declaration processing has been completed, and before execution of
17693          the first statement of the function begins. Debuggers generally
17694          cannot properly determine where this point is.  Similarly for a
17695          breakpoint set on exit from a function. The prologue and epilogue
17696          attributes allow a compiler to communicate the location(s) to use.  */
17697
17698       {
17699         if (fde->dw_fde_vms_end_prologue)
17700           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
17701             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
17702
17703         if (fde->dw_fde_vms_begin_epilogue)
17704           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
17705             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
17706       }
17707 #endif
17708
17709           add_pubname (decl, subr_die);
17710         }
17711       else
17712         {  /* Generate pubnames entries for the split function code
17713               ranges.  */
17714           dw_fde_ref fde = &fde_table[current_funcdef_fde];
17715
17716           if (fde->dw_fde_second_begin)
17717             {
17718               if (dwarf_version >= 3 || !dwarf_strict)
17719                 {
17720                   /* We should use ranges for non-contiguous code section 
17721                      addresses.  Use the actual code range for the initial
17722                      section, since the HOT/COLD labels might precede an 
17723                      alignment offset.  */
17724                   bool range_list_added = false;
17725                   add_ranges_by_labels (subr_die, fde->dw_fde_begin,
17726                                         fde->dw_fde_end, &range_list_added);
17727                   add_ranges_by_labels (subr_die, fde->dw_fde_second_begin,
17728                                         fde->dw_fde_second_end,
17729                                         &range_list_added);
17730                   add_pubname (decl, subr_die);
17731                   if (range_list_added)
17732                     add_ranges (NULL);
17733                 }
17734               else
17735                 {
17736                   /* There is no real support in DW2 for this .. so we make
17737                      a work-around.  First, emit the pub name for the segment
17738                      containing the function label.  Then make and emit a
17739                      simplified subprogram DIE for the second segment with the
17740                      name pre-fixed by __hot/cold_sect_of_.  We use the same
17741                      linkage name for the second die so that gdb will find both
17742                      sections when given "b foo".  */
17743                   const char *name = NULL;
17744                   tree decl_name = DECL_NAME (decl);
17745                   dw_die_ref seg_die;
17746
17747                   /* Do the 'primary' section.   */
17748                   add_AT_lbl_id (subr_die, DW_AT_low_pc,
17749                                  fde->dw_fde_begin);
17750                   add_AT_lbl_id (subr_die, DW_AT_high_pc,
17751                                  fde->dw_fde_end);
17752                   /* Add it.   */
17753                   add_pubname (decl, subr_die);
17754
17755                   /* Build a minimal DIE for the secondary section.  */
17756                   seg_die = new_die (DW_TAG_subprogram,
17757                                      subr_die->die_parent, decl);
17758
17759                   if (TREE_PUBLIC (decl))
17760                     add_AT_flag (seg_die, DW_AT_external, 1);
17761
17762                   if (decl_name != NULL 
17763                       && IDENTIFIER_POINTER (decl_name) != NULL)
17764                     {
17765                       name = dwarf2_name (decl, 1);
17766                       if (! DECL_ARTIFICIAL (decl))
17767                         add_src_coords_attributes (seg_die, decl);
17768
17769                       add_linkage_name (seg_die, decl);
17770                     }
17771                   gcc_assert (name != NULL);
17772                   add_pure_or_virtual_attribute (seg_die, decl);
17773                   if (DECL_ARTIFICIAL (decl))
17774                     add_AT_flag (seg_die, DW_AT_artificial, 1);
17775
17776                   name = concat ("__second_sect_of_", name, NULL); 
17777                   add_AT_lbl_id (seg_die, DW_AT_low_pc,
17778                                  fde->dw_fde_second_begin);
17779                   add_AT_lbl_id (seg_die, DW_AT_high_pc,
17780                                  fde->dw_fde_second_end);
17781                   add_name_attribute (seg_die, name);
17782                   add_pubname_string (name, seg_die);
17783                 }
17784             }
17785           else
17786             {
17787               add_AT_lbl_id (subr_die, DW_AT_low_pc, fde->dw_fde_begin);
17788               add_AT_lbl_id (subr_die, DW_AT_high_pc, fde->dw_fde_end);
17789               add_pubname (decl, subr_die);
17790             }
17791         }
17792
17793 #ifdef MIPS_DEBUGGING_INFO
17794       /* Add a reference to the FDE for this routine.  */
17795       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
17796 #endif
17797
17798       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
17799
17800       /* We define the "frame base" as the function's CFA.  This is more
17801          convenient for several reasons: (1) It's stable across the prologue
17802          and epilogue, which makes it better than just a frame pointer,
17803          (2) With dwarf3, there exists a one-byte encoding that allows us
17804          to reference the .debug_frame data by proxy, but failing that,
17805          (3) We can at least reuse the code inspection and interpretation
17806          code that determines the CFA position at various points in the
17807          function.  */
17808       if (dwarf_version >= 3)
17809         {
17810           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
17811           add_AT_loc (subr_die, DW_AT_frame_base, op);
17812         }
17813       else
17814         {
17815           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
17816           if (list->dw_loc_next)
17817             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
17818           else
17819             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
17820         }
17821
17822       /* Compute a displacement from the "steady-state frame pointer" to
17823          the CFA.  The former is what all stack slots and argument slots
17824          will reference in the rtl; the later is what we've told the
17825          debugger about.  We'll need to adjust all frame_base references
17826          by this displacement.  */
17827       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
17828
17829       if (cfun->static_chain_decl)
17830         add_AT_location_description (subr_die, DW_AT_static_link,
17831                  loc_list_from_tree (cfun->static_chain_decl, 2));
17832     }
17833
17834   /* Generate child dies for template paramaters.  */
17835   if (debug_info_level > DINFO_LEVEL_TERSE)
17836     gen_generic_params_dies (decl);
17837
17838   /* Now output descriptions of the arguments for this function. This gets
17839      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
17840      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
17841      `...' at the end of the formal parameter list.  In order to find out if
17842      there was a trailing ellipsis or not, we must instead look at the type
17843      associated with the FUNCTION_DECL.  This will be a node of type
17844      FUNCTION_TYPE. If the chain of type nodes hanging off of this
17845      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
17846      an ellipsis at the end.  */
17847
17848   /* In the case where we are describing a mere function declaration, all we
17849      need to do here (and all we *can* do here) is to describe the *types* of
17850      its formal parameters.  */
17851   if (debug_info_level <= DINFO_LEVEL_TERSE)
17852     ;
17853   else if (declaration)
17854     gen_formal_types_die (decl, subr_die);
17855   else
17856     {
17857       /* Generate DIEs to represent all known formal parameters.  */
17858       tree parm = DECL_ARGUMENTS (decl);
17859       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
17860       tree generic_decl_parm = generic_decl
17861                                 ? DECL_ARGUMENTS (generic_decl)
17862                                 : NULL;
17863
17864       /* Now we want to walk the list of parameters of the function and
17865          emit their relevant DIEs.
17866
17867          We consider the case of DECL being an instance of a generic function
17868          as well as it being a normal function.
17869
17870          If DECL is an instance of a generic function we walk the
17871          parameters of the generic function declaration _and_ the parameters of
17872          DECL itself. This is useful because we want to emit specific DIEs for
17873          function parameter packs and those are declared as part of the
17874          generic function declaration. In that particular case,
17875          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
17876          That DIE has children DIEs representing the set of arguments
17877          of the pack. Note that the set of pack arguments can be empty.
17878          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
17879          children DIE.
17880
17881          Otherwise, we just consider the parameters of DECL.  */
17882       while (generic_decl_parm || parm)
17883         {
17884           if (generic_decl_parm
17885               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
17886             gen_formal_parameter_pack_die (generic_decl_parm,
17887                                            parm, subr_die,
17888                                            &parm);
17889           else if (parm)
17890             {
17891               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
17892
17893               if (parm == DECL_ARGUMENTS (decl)
17894                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
17895                   && parm_die
17896                   && (dwarf_version >= 3 || !dwarf_strict))
17897                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
17898
17899               parm = DECL_CHAIN (parm);
17900             }
17901
17902           if (generic_decl_parm)
17903             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
17904         }
17905
17906       /* Decide whether we need an unspecified_parameters DIE at the end.
17907          There are 2 more cases to do this for: 1) the ansi ... declaration -
17908          this is detectable when the end of the arg list is not a
17909          void_type_node 2) an unprototyped function declaration (not a
17910          definition).  This just means that we have no info about the
17911          parameters at all.  */
17912       if (prototype_p (TREE_TYPE (decl)))
17913         {
17914           /* This is the prototyped case, check for....  */
17915           if (stdarg_p (TREE_TYPE (decl)))
17916             gen_unspecified_parameters_die (decl, subr_die);
17917         }
17918       else if (DECL_INITIAL (decl) == NULL_TREE)
17919         gen_unspecified_parameters_die (decl, subr_die);
17920     }
17921
17922   /* Output Dwarf info for all of the stuff within the body of the function
17923      (if it has one - it may be just a declaration).  */
17924   outer_scope = DECL_INITIAL (decl);
17925
17926   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
17927      a function.  This BLOCK actually represents the outermost binding contour
17928      for the function, i.e. the contour in which the function's formal
17929      parameters and labels get declared. Curiously, it appears that the front
17930      end doesn't actually put the PARM_DECL nodes for the current function onto
17931      the BLOCK_VARS list for this outer scope, but are strung off of the
17932      DECL_ARGUMENTS list for the function instead.
17933
17934      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
17935      the LABEL_DECL nodes for the function however, and we output DWARF info
17936      for those in decls_for_scope.  Just within the `outer_scope' there will be
17937      a BLOCK node representing the function's outermost pair of curly braces,
17938      and any blocks used for the base and member initializers of a C++
17939      constructor function.  */
17940   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
17941     {
17942       int call_site_note_count = 0;
17943       int tail_call_site_note_count = 0;
17944
17945       /* Emit a DW_TAG_variable DIE for a named return value.  */
17946       if (DECL_NAME (DECL_RESULT (decl)))
17947         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
17948
17949       current_function_has_inlines = 0;
17950       decls_for_scope (outer_scope, subr_die, 0);
17951
17952       if (call_arg_locations && !dwarf_strict)
17953         {
17954           struct call_arg_loc_node *ca_loc;
17955           for (ca_loc = call_arg_locations; ca_loc; ca_loc = ca_loc->next)
17956             {
17957               dw_die_ref die = NULL;
17958               rtx tloc = NULL_RTX, tlocc = NULL_RTX;
17959               rtx arg, next_arg;
17960
17961               for (arg = NOTE_VAR_LOCATION (ca_loc->call_arg_loc_note);
17962                    arg; arg = next_arg)
17963                 {
17964                   dw_loc_descr_ref reg, val;
17965                   enum machine_mode mode = GET_MODE (XEXP (XEXP (arg, 0), 1));
17966                   dw_die_ref cdie, tdie = NULL;
17967
17968                   next_arg = XEXP (arg, 1);
17969                   if (REG_P (XEXP (XEXP (arg, 0), 0))
17970                       && next_arg
17971                       && MEM_P (XEXP (XEXP (next_arg, 0), 0))
17972                       && REG_P (XEXP (XEXP (XEXP (next_arg, 0), 0), 0))
17973                       && REGNO (XEXP (XEXP (arg, 0), 0))
17974                          == REGNO (XEXP (XEXP (XEXP (next_arg, 0), 0), 0)))
17975                     next_arg = XEXP (next_arg, 1);
17976                   if (mode == VOIDmode)
17977                     {
17978                       mode = GET_MODE (XEXP (XEXP (arg, 0), 0));
17979                       if (mode == VOIDmode)
17980                         mode = GET_MODE (XEXP (arg, 0));
17981                     }
17982                   if (mode == VOIDmode || mode == BLKmode)
17983                     continue;
17984                   if (XEXP (XEXP (arg, 0), 0) == pc_rtx)
17985                     {
17986                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17987                       tloc = XEXP (XEXP (arg, 0), 1);
17988                       continue;
17989                     }
17990                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0)) == CLOBBER
17991                            && XEXP (XEXP (XEXP (arg, 0), 0), 0) == pc_rtx)
17992                     {
17993                       gcc_assert (ca_loc->symbol_ref == NULL_RTX);
17994                       tlocc = XEXP (XEXP (arg, 0), 1);
17995                       continue;
17996                     }
17997                   reg = NULL;
17998                   if (REG_P (XEXP (XEXP (arg, 0), 0)))
17999                     reg = reg_loc_descriptor (XEXP (XEXP (arg, 0), 0),
18000                                               VAR_INIT_STATUS_INITIALIZED);
18001                   else if (MEM_P (XEXP (XEXP (arg, 0), 0)))
18002                     {
18003                       rtx mem = XEXP (XEXP (arg, 0), 0);
18004                       reg = mem_loc_descriptor (XEXP (mem, 0),
18005                                                 get_address_mode (mem),
18006                                                 GET_MODE (mem),
18007                                                 VAR_INIT_STATUS_INITIALIZED);
18008                     }
18009                   else if (GET_CODE (XEXP (XEXP (arg, 0), 0))
18010                            == DEBUG_PARAMETER_REF)
18011                     {
18012                       tree tdecl
18013                         = DEBUG_PARAMETER_REF_DECL (XEXP (XEXP (arg, 0), 0));
18014                       tdie = lookup_decl_die (tdecl);
18015                       if (tdie == NULL)
18016                         continue;
18017                     }
18018                   else
18019                     continue;
18020                   if (reg == NULL
18021                       && GET_CODE (XEXP (XEXP (arg, 0), 0))
18022                          != DEBUG_PARAMETER_REF)
18023                     continue;
18024                   val = mem_loc_descriptor (XEXP (XEXP (arg, 0), 1), mode,
18025                                             VOIDmode,
18026                                             VAR_INIT_STATUS_INITIALIZED);
18027                   if (val == NULL)
18028                     continue;
18029                   if (die == NULL)
18030                     die = gen_call_site_die (decl, subr_die, ca_loc);
18031                   cdie = new_die (DW_TAG_GNU_call_site_parameter, die,
18032                                   NULL_TREE);
18033                   if (reg != NULL)
18034                     add_AT_loc (cdie, DW_AT_location, reg);
18035                   else if (tdie != NULL)
18036                     add_AT_die_ref (cdie, DW_AT_abstract_origin, tdie);
18037                   add_AT_loc (cdie, DW_AT_GNU_call_site_value, val);
18038                   if (next_arg != XEXP (arg, 1))
18039                     {
18040                       mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 1));
18041                       if (mode == VOIDmode)
18042                         mode = GET_MODE (XEXP (XEXP (XEXP (arg, 1), 0), 0));
18043                       val = mem_loc_descriptor (XEXP (XEXP (XEXP (arg, 1),
18044                                                             0), 1),
18045                                                 mode, VOIDmode,
18046                                                 VAR_INIT_STATUS_INITIALIZED);
18047                       if (val != NULL)
18048                         add_AT_loc (cdie, DW_AT_GNU_call_site_data_value, val);
18049                     }
18050                 }
18051               if (die == NULL
18052                   && (ca_loc->symbol_ref || tloc))
18053                 die = gen_call_site_die (decl, subr_die, ca_loc);
18054               if (die != NULL && (tloc != NULL_RTX || tlocc != NULL_RTX))
18055                 {
18056                   dw_loc_descr_ref tval = NULL;
18057
18058                   if (tloc != NULL_RTX)
18059                     tval = mem_loc_descriptor (tloc,
18060                                                GET_MODE (tloc) == VOIDmode
18061                                                ? Pmode : GET_MODE (tloc),
18062                                                VOIDmode,
18063                                                VAR_INIT_STATUS_INITIALIZED);
18064                   if (tval)
18065                     add_AT_loc (die, DW_AT_GNU_call_site_target, tval);
18066                   else if (tlocc != NULL_RTX)
18067                     {
18068                       tval = mem_loc_descriptor (tlocc,
18069                                                  GET_MODE (tlocc) == VOIDmode
18070                                                  ? Pmode : GET_MODE (tlocc),
18071                                                  VOIDmode,
18072                                                  VAR_INIT_STATUS_INITIALIZED);
18073                       if (tval)
18074                         add_AT_loc (die, DW_AT_GNU_call_site_target_clobbered,
18075                                     tval);
18076                     }
18077                 }
18078               if (die != NULL)
18079                 {
18080                   call_site_note_count++;
18081                   if (ca_loc->tail_call_p)
18082                     tail_call_site_note_count++;
18083                 }
18084             }
18085         }
18086       call_arg_locations = NULL;
18087       call_arg_loc_last = NULL;
18088       if (tail_call_site_count >= 0
18089           && tail_call_site_count == tail_call_site_note_count
18090           && !dwarf_strict)
18091         {
18092           if (call_site_count >= 0
18093               && call_site_count == call_site_note_count)
18094             add_AT_flag (subr_die, DW_AT_GNU_all_call_sites, 1);
18095           else
18096             add_AT_flag (subr_die, DW_AT_GNU_all_tail_call_sites, 1);
18097         }
18098       call_site_count = -1;
18099       tail_call_site_count = -1;
18100     }
18101   /* Add the calling convention attribute if requested.  */
18102   add_calling_convention_attribute (subr_die, decl);
18103
18104 }
18105
18106 /* Returns a hash value for X (which really is a die_struct).  */
18107
18108 static hashval_t
18109 common_block_die_table_hash (const void *x)
18110 {
18111   const_dw_die_ref d = (const_dw_die_ref) x;
18112   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18113 }
18114
18115 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18116    as decl_id and die_parent of die_struct Y.  */
18117
18118 static int
18119 common_block_die_table_eq (const void *x, const void *y)
18120 {
18121   const_dw_die_ref d = (const_dw_die_ref) x;
18122   const_dw_die_ref e = (const_dw_die_ref) y;
18123   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18124 }
18125
18126 /* Generate a DIE to represent a declared data object.
18127    Either DECL or ORIGIN must be non-null.  */
18128
18129 static void
18130 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18131 {
18132   HOST_WIDE_INT off;
18133   tree com_decl;
18134   tree decl_or_origin = decl ? decl : origin;
18135   tree ultimate_origin;
18136   dw_die_ref var_die;
18137   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
18138   dw_die_ref origin_die;
18139   bool declaration = (DECL_EXTERNAL (decl_or_origin)
18140                       || class_or_namespace_scope_p (context_die));
18141   bool specialization_p = false;
18142
18143   ultimate_origin = decl_ultimate_origin (decl_or_origin);
18144   if (decl || ultimate_origin)
18145     origin = ultimate_origin;
18146   com_decl = fortran_common (decl_or_origin, &off);
18147
18148   /* Symbol in common gets emitted as a child of the common block, in the form
18149      of a data member.  */
18150   if (com_decl)
18151     {
18152       dw_die_ref com_die;
18153       dw_loc_list_ref loc;
18154       die_node com_die_arg;
18155
18156       var_die = lookup_decl_die (decl_or_origin);
18157       if (var_die)
18158         {
18159           if (get_AT (var_die, DW_AT_location) == NULL)
18160             {
18161               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
18162               if (loc)
18163                 {
18164                   if (off)
18165                     {
18166                       /* Optimize the common case.  */
18167                       if (single_element_loc_list_p (loc)
18168                           && loc->expr->dw_loc_opc == DW_OP_addr
18169                           && loc->expr->dw_loc_next == NULL
18170                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
18171                              == SYMBOL_REF)
18172                         loc->expr->dw_loc_oprnd1.v.val_addr
18173                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18174                         else
18175                           loc_list_plus_const (loc, off);
18176                     }
18177                   add_AT_location_description (var_die, DW_AT_location, loc);
18178                   remove_AT (var_die, DW_AT_declaration);
18179                 }
18180             }
18181           return;
18182         }
18183
18184       if (common_block_die_table == NULL)
18185         common_block_die_table
18186           = htab_create_ggc (10, common_block_die_table_hash,
18187                              common_block_die_table_eq, NULL);
18188
18189       com_die_arg.decl_id = DECL_UID (com_decl);
18190       com_die_arg.die_parent = context_die;
18191       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
18192       loc = loc_list_from_tree (com_decl, 2);
18193       if (com_die == NULL)
18194         {
18195           const char *cnam
18196             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
18197           void **slot;
18198
18199           com_die = new_die (DW_TAG_common_block, context_die, decl);
18200           add_name_and_src_coords_attributes (com_die, com_decl);
18201           if (loc)
18202             {
18203               add_AT_location_description (com_die, DW_AT_location, loc);
18204               /* Avoid sharing the same loc descriptor between
18205                  DW_TAG_common_block and DW_TAG_variable.  */
18206               loc = loc_list_from_tree (com_decl, 2);
18207             }
18208           else if (DECL_EXTERNAL (decl))
18209             add_AT_flag (com_die, DW_AT_declaration, 1);
18210           add_pubname_string (cnam, com_die); /* ??? needed? */
18211           com_die->decl_id = DECL_UID (com_decl);
18212           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
18213           *slot = (void *) com_die;
18214         }
18215       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
18216         {
18217           add_AT_location_description (com_die, DW_AT_location, loc);
18218           loc = loc_list_from_tree (com_decl, 2);
18219           remove_AT (com_die, DW_AT_declaration);
18220         }
18221       var_die = new_die (DW_TAG_variable, com_die, decl);
18222       add_name_and_src_coords_attributes (var_die, decl);
18223       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
18224                           TREE_THIS_VOLATILE (decl), context_die);
18225       add_AT_flag (var_die, DW_AT_external, 1);
18226       if (loc)
18227         {
18228           if (off)
18229             {
18230               /* Optimize the common case.  */
18231               if (single_element_loc_list_p (loc)
18232                   && loc->expr->dw_loc_opc == DW_OP_addr
18233                   && loc->expr->dw_loc_next == NULL
18234                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
18235                 loc->expr->dw_loc_oprnd1.v.val_addr
18236                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
18237               else
18238                 loc_list_plus_const (loc, off);
18239             }
18240           add_AT_location_description (var_die, DW_AT_location, loc);
18241         }
18242       else if (DECL_EXTERNAL (decl))
18243         add_AT_flag (var_die, DW_AT_declaration, 1);
18244       equate_decl_number_to_die (decl, var_die);
18245       return;
18246     }
18247
18248   /* If the compiler emitted a definition for the DECL declaration
18249      and if we already emitted a DIE for it, don't emit a second
18250      DIE for it again. Allow re-declarations of DECLs that are
18251      inside functions, though.  */
18252   if (old_die && declaration && !local_scope_p (context_die))
18253     return;
18254
18255   /* For static data members, the declaration in the class is supposed
18256      to have DW_TAG_member tag; the specification should still be
18257      DW_TAG_variable referencing the DW_TAG_member DIE.  */
18258   if (declaration && class_scope_p (context_die))
18259     var_die = new_die (DW_TAG_member, context_die, decl);
18260   else
18261     var_die = new_die (DW_TAG_variable, context_die, decl);
18262
18263   origin_die = NULL;
18264   if (origin != NULL)
18265     origin_die = add_abstract_origin_attribute (var_die, origin);
18266
18267   /* Loop unrolling can create multiple blocks that refer to the same
18268      static variable, so we must test for the DW_AT_declaration flag.
18269
18270      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
18271      copy decls and set the DECL_ABSTRACT flag on them instead of
18272      sharing them.
18273
18274      ??? Duplicated blocks have been rewritten to use .debug_ranges.
18275
18276      ??? The declare_in_namespace support causes us to get two DIEs for one
18277      variable, both of which are declarations.  We want to avoid considering
18278      one to be a specification, so we must test that this DIE is not a
18279      declaration.  */
18280   else if (old_die && TREE_STATIC (decl) && ! declaration
18281            && get_AT_flag (old_die, DW_AT_declaration) == 1)
18282     {
18283       /* This is a definition of a C++ class level static.  */
18284       add_AT_specification (var_die, old_die);
18285       specialization_p = true;
18286       if (DECL_NAME (decl))
18287         {
18288           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18289           struct dwarf_file_data * file_index = lookup_filename (s.file);
18290
18291           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18292             add_AT_file (var_die, DW_AT_decl_file, file_index);
18293
18294           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18295             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
18296
18297           if (old_die->die_tag == DW_TAG_member)
18298             add_linkage_name (var_die, decl);
18299         }
18300     }
18301   else
18302     add_name_and_src_coords_attributes (var_die, decl);
18303
18304   if ((origin == NULL && !specialization_p)
18305       || (origin != NULL
18306           && !DECL_ABSTRACT (decl_or_origin)
18307           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
18308                                        decl_function_context
18309                                                         (decl_or_origin))))
18310     {
18311       tree type = TREE_TYPE (decl_or_origin);
18312
18313       if (decl_by_reference_p (decl_or_origin))
18314         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
18315       else
18316         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
18317                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
18318     }
18319
18320   if (origin == NULL && !specialization_p)
18321     {
18322       if (TREE_PUBLIC (decl))
18323         add_AT_flag (var_die, DW_AT_external, 1);
18324
18325       if (DECL_ARTIFICIAL (decl))
18326         add_AT_flag (var_die, DW_AT_artificial, 1);
18327
18328       add_accessibility_attribute (var_die, decl);
18329     }
18330
18331   if (declaration)
18332     add_AT_flag (var_die, DW_AT_declaration, 1);
18333
18334   if (decl && (DECL_ABSTRACT (decl) || declaration || old_die == NULL))
18335     equate_decl_number_to_die (decl, var_die);
18336
18337   if (! declaration
18338       && (! DECL_ABSTRACT (decl_or_origin)
18339           /* Local static vars are shared between all clones/inlines,
18340              so emit DW_AT_location on the abstract DIE if DECL_RTL is
18341              already set.  */
18342           || (TREE_CODE (decl_or_origin) == VAR_DECL
18343               && TREE_STATIC (decl_or_origin)
18344               && DECL_RTL_SET_P (decl_or_origin)))
18345       /* When abstract origin already has DW_AT_location attribute, no need
18346          to add it again.  */
18347       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
18348     {
18349       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
18350           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
18351         defer_location (decl_or_origin, var_die);
18352       else
18353         add_location_or_const_value_attribute (var_die, decl_or_origin,
18354                                                decl == NULL, DW_AT_location);
18355       add_pubname (decl_or_origin, var_die);
18356     }
18357   else
18358     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
18359 }
18360
18361 /* Generate a DIE to represent a named constant.  */
18362
18363 static void
18364 gen_const_die (tree decl, dw_die_ref context_die)
18365 {
18366   dw_die_ref const_die;
18367   tree type = TREE_TYPE (decl);
18368
18369   const_die = new_die (DW_TAG_constant, context_die, decl);
18370   add_name_and_src_coords_attributes (const_die, decl);
18371   add_type_attribute (const_die, type, 1, 0, context_die);
18372   if (TREE_PUBLIC (decl))
18373     add_AT_flag (const_die, DW_AT_external, 1);
18374   if (DECL_ARTIFICIAL (decl))
18375     add_AT_flag (const_die, DW_AT_artificial, 1);
18376   tree_add_const_value_attribute_for_decl (const_die, decl);
18377 }
18378
18379 /* Generate a DIE to represent a label identifier.  */
18380
18381 static void
18382 gen_label_die (tree decl, dw_die_ref context_die)
18383 {
18384   tree origin = decl_ultimate_origin (decl);
18385   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
18386   rtx insn;
18387   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18388
18389   if (origin != NULL)
18390     add_abstract_origin_attribute (lbl_die, origin);
18391   else
18392     add_name_and_src_coords_attributes (lbl_die, decl);
18393
18394   if (DECL_ABSTRACT (decl))
18395     equate_decl_number_to_die (decl, lbl_die);
18396   else
18397     {
18398       insn = DECL_RTL_IF_SET (decl);
18399
18400       /* Deleted labels are programmer specified labels which have been
18401          eliminated because of various optimizations.  We still emit them
18402          here so that it is possible to put breakpoints on them.  */
18403       if (insn
18404           && (LABEL_P (insn)
18405               || ((NOTE_P (insn)
18406                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
18407         {
18408           /* When optimization is enabled (via -O) some parts of the compiler
18409              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
18410              represent source-level labels which were explicitly declared by
18411              the user.  This really shouldn't be happening though, so catch
18412              it if it ever does happen.  */
18413           gcc_assert (!INSN_DELETED_P (insn));
18414
18415           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
18416           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
18417         }
18418     }
18419 }
18420
18421 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
18422    attributes to the DIE for a block STMT, to describe where the inlined
18423    function was called from.  This is similar to add_src_coords_attributes.  */
18424
18425 static inline void
18426 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
18427 {
18428   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
18429
18430   if (dwarf_version >= 3 || !dwarf_strict)
18431     {
18432       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
18433       add_AT_unsigned (die, DW_AT_call_line, s.line);
18434     }
18435 }
18436
18437
18438 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
18439    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
18440
18441 static inline void
18442 add_high_low_attributes (tree stmt, dw_die_ref die)
18443 {
18444   char label[MAX_ARTIFICIAL_LABEL_BYTES];
18445
18446   if (BLOCK_FRAGMENT_CHAIN (stmt)
18447       && (dwarf_version >= 3 || !dwarf_strict))
18448     {
18449       tree chain;
18450
18451       if (inlined_function_outer_scope_p (stmt))
18452         {
18453           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18454                                        BLOCK_NUMBER (stmt));
18455           add_AT_lbl_id (die, DW_AT_entry_pc, label);
18456         }
18457
18458       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
18459
18460       chain = BLOCK_FRAGMENT_CHAIN (stmt);
18461       do
18462         {
18463           add_ranges (chain);
18464           chain = BLOCK_FRAGMENT_CHAIN (chain);
18465         }
18466       while (chain);
18467       add_ranges (NULL);
18468     }
18469   else
18470     {
18471       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
18472                                    BLOCK_NUMBER (stmt));
18473       add_AT_lbl_id (die, DW_AT_low_pc, label);
18474       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
18475                                    BLOCK_NUMBER (stmt));
18476       add_AT_lbl_id (die, DW_AT_high_pc, label);
18477     }
18478 }
18479
18480 /* Generate a DIE for a lexical block.  */
18481
18482 static void
18483 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
18484 {
18485   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
18486
18487   if (call_arg_locations)
18488     {
18489       if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18490         VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18491                                BLOCK_NUMBER (stmt) + 1);
18492       VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), stmt_die);
18493     }
18494
18495   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
18496     add_high_low_attributes (stmt, stmt_die);
18497
18498   decls_for_scope (stmt, stmt_die, depth);
18499 }
18500
18501 /* Generate a DIE for an inlined subprogram.  */
18502
18503 static void
18504 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
18505 {
18506   tree decl;
18507
18508   /* The instance of function that is effectively being inlined shall not
18509      be abstract.  */
18510   gcc_assert (! BLOCK_ABSTRACT (stmt));
18511
18512   decl = block_ultimate_origin (stmt);
18513
18514   /* Emit info for the abstract instance first, if we haven't yet.  We
18515      must emit this even if the block is abstract, otherwise when we
18516      emit the block below (or elsewhere), we may end up trying to emit
18517      a die whose origin die hasn't been emitted, and crashing.  */
18518   dwarf2out_abstract_function (decl);
18519
18520   if (! BLOCK_ABSTRACT (stmt))
18521     {
18522       dw_die_ref subr_die
18523         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
18524
18525       if (call_arg_locations)
18526         {
18527           if (VEC_length (dw_die_ref, block_map) <= BLOCK_NUMBER (stmt))
18528             VEC_safe_grow_cleared (dw_die_ref, heap, block_map,
18529                                    BLOCK_NUMBER (stmt) + 1);
18530           VEC_replace (dw_die_ref, block_map, BLOCK_NUMBER (stmt), subr_die);
18531         }
18532       add_abstract_origin_attribute (subr_die, decl);
18533       if (TREE_ASM_WRITTEN (stmt))
18534         add_high_low_attributes (stmt, subr_die);
18535       add_call_src_coords_attributes (stmt, subr_die);
18536
18537       decls_for_scope (stmt, subr_die, depth);
18538       current_function_has_inlines = 1;
18539     }
18540 }
18541
18542 /* Generate a DIE for a field in a record, or structure.  */
18543
18544 static void
18545 gen_field_die (tree decl, dw_die_ref context_die)
18546 {
18547   dw_die_ref decl_die;
18548
18549   if (TREE_TYPE (decl) == error_mark_node)
18550     return;
18551
18552   decl_die = new_die (DW_TAG_member, context_die, decl);
18553   add_name_and_src_coords_attributes (decl_die, decl);
18554   add_type_attribute (decl_die, member_declared_type (decl),
18555                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
18556                       context_die);
18557
18558   if (DECL_BIT_FIELD_TYPE (decl))
18559     {
18560       add_byte_size_attribute (decl_die, decl);
18561       add_bit_size_attribute (decl_die, decl);
18562       add_bit_offset_attribute (decl_die, decl);
18563     }
18564
18565   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
18566     add_data_member_location_attribute (decl_die, decl);
18567
18568   if (DECL_ARTIFICIAL (decl))
18569     add_AT_flag (decl_die, DW_AT_artificial, 1);
18570
18571   add_accessibility_attribute (decl_die, decl);
18572
18573   /* Equate decl number to die, so that we can look up this decl later on.  */
18574   equate_decl_number_to_die (decl, decl_die);
18575 }
18576
18577 #if 0
18578 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18579    Use modified_type_die instead.
18580    We keep this code here just in case these types of DIEs may be needed to
18581    represent certain things in other languages (e.g. Pascal) someday.  */
18582
18583 static void
18584 gen_pointer_type_die (tree type, dw_die_ref context_die)
18585 {
18586   dw_die_ref ptr_die
18587     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
18588
18589   equate_type_number_to_die (type, ptr_die);
18590   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18591   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18592 }
18593
18594 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
18595    Use modified_type_die instead.
18596    We keep this code here just in case these types of DIEs may be needed to
18597    represent certain things in other languages (e.g. Pascal) someday.  */
18598
18599 static void
18600 gen_reference_type_die (tree type, dw_die_ref context_die)
18601 {
18602   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
18603
18604   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
18605     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
18606   else
18607     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
18608
18609   equate_type_number_to_die (type, ref_die);
18610   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
18611   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
18612 }
18613 #endif
18614
18615 /* Generate a DIE for a pointer to a member type.  */
18616
18617 static void
18618 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
18619 {
18620   dw_die_ref ptr_die
18621     = new_die (DW_TAG_ptr_to_member_type,
18622                scope_die_for (type, context_die), type);
18623
18624   equate_type_number_to_die (type, ptr_die);
18625   add_AT_die_ref (ptr_die, DW_AT_containing_type,
18626                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
18627   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
18628 }
18629
18630 /* Generate the DIE for the compilation unit.  */
18631
18632 static dw_die_ref
18633 gen_compile_unit_die (const char *filename)
18634 {
18635   dw_die_ref die;
18636   char producer[250];
18637   const char *language_string = lang_hooks.name;
18638   int language;
18639
18640   die = new_die (DW_TAG_compile_unit, NULL, NULL);
18641
18642   if (filename)
18643     {
18644       add_name_attribute (die, filename);
18645       /* Don't add cwd for <built-in>.  */
18646       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
18647         add_comp_dir_attribute (die);
18648     }
18649
18650   sprintf (producer, "%s %s", language_string, version_string);
18651
18652 #ifdef MIPS_DEBUGGING_INFO
18653   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
18654      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
18655      not appear in the producer string, the debugger reaches the conclusion
18656      that the object file is stripped and has no debugging information.
18657      To get the MIPS/SGI debugger to believe that there is debugging
18658      information in the object file, we add a -g to the producer string.  */
18659   if (debug_info_level > DINFO_LEVEL_TERSE)
18660     strcat (producer, " -g");
18661 #endif
18662
18663   add_AT_string (die, DW_AT_producer, producer);
18664
18665   /* If our producer is LTO try to figure out a common language to use
18666      from the global list of translation units.  */
18667   if (strcmp (language_string, "GNU GIMPLE") == 0)
18668     {
18669       unsigned i;
18670       tree t;
18671       const char *common_lang = NULL;
18672
18673       FOR_EACH_VEC_ELT (tree, all_translation_units, i, t)
18674         {
18675           if (!TRANSLATION_UNIT_LANGUAGE (t))
18676             continue;
18677           if (!common_lang)
18678             common_lang = TRANSLATION_UNIT_LANGUAGE (t);
18679           else if (strcmp (common_lang, TRANSLATION_UNIT_LANGUAGE (t)) == 0)
18680             ;
18681           else if (strncmp (common_lang, "GNU C", 5) == 0
18682                    && strncmp (TRANSLATION_UNIT_LANGUAGE (t), "GNU C", 5) == 0)
18683             /* Mixing C and C++ is ok, use C++ in that case.  */
18684             common_lang = "GNU C++";
18685           else
18686             {
18687               /* Fall back to C.  */
18688               common_lang = NULL;
18689               break;
18690             }
18691         }
18692
18693       if (common_lang)
18694         language_string = common_lang;
18695     }
18696
18697   language = DW_LANG_C89;
18698   if (strcmp (language_string, "GNU C++") == 0)
18699     language = DW_LANG_C_plus_plus;
18700   else if (strcmp (language_string, "GNU F77") == 0)
18701     language = DW_LANG_Fortran77;
18702   else if (strcmp (language_string, "GNU Pascal") == 0)
18703     language = DW_LANG_Pascal83;
18704   else if (dwarf_version >= 3 || !dwarf_strict)
18705     {
18706       if (strcmp (language_string, "GNU Ada") == 0)
18707         language = DW_LANG_Ada95;
18708       else if (strcmp (language_string, "GNU Fortran") == 0)
18709         language = DW_LANG_Fortran95;
18710       else if (strcmp (language_string, "GNU Java") == 0)
18711         language = DW_LANG_Java;
18712       else if (strcmp (language_string, "GNU Objective-C") == 0)
18713         language = DW_LANG_ObjC;
18714       else if (strcmp (language_string, "GNU Objective-C++") == 0)
18715         language = DW_LANG_ObjC_plus_plus;
18716     }
18717
18718   add_AT_unsigned (die, DW_AT_language, language);
18719
18720   switch (language)
18721     {
18722     case DW_LANG_Fortran77:
18723     case DW_LANG_Fortran90:
18724     case DW_LANG_Fortran95:
18725       /* Fortran has case insensitive identifiers and the front-end
18726          lowercases everything.  */
18727       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
18728       break;
18729     default:
18730       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
18731       break;
18732     }
18733   return die;
18734 }
18735
18736 /* Generate the DIE for a base class.  */
18737
18738 static void
18739 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
18740 {
18741   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
18742
18743   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
18744   add_data_member_location_attribute (die, binfo);
18745
18746   if (BINFO_VIRTUAL_P (binfo))
18747     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
18748
18749   /* In DWARF3+ the default is DW_ACCESS_private only in DW_TAG_class_type
18750      children, otherwise the default is DW_ACCESS_public.  In DWARF2
18751      the default has always been DW_ACCESS_private.  */
18752   if (access == access_public_node)
18753     {
18754       if (dwarf_version == 2
18755           || context_die->die_tag == DW_TAG_class_type)
18756       add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
18757     }
18758   else if (access == access_protected_node)
18759     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
18760   else if (dwarf_version > 2
18761            && context_die->die_tag != DW_TAG_class_type)
18762     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
18763 }
18764
18765 /* Generate a DIE for a class member.  */
18766
18767 static void
18768 gen_member_die (tree type, dw_die_ref context_die)
18769 {
18770   tree member;
18771   tree binfo = TYPE_BINFO (type);
18772   dw_die_ref child;
18773
18774   /* If this is not an incomplete type, output descriptions of each of its
18775      members. Note that as we output the DIEs necessary to represent the
18776      members of this record or union type, we will also be trying to output
18777      DIEs to represent the *types* of those members. However the `type'
18778      function (above) will specifically avoid generating type DIEs for member
18779      types *within* the list of member DIEs for this (containing) type except
18780      for those types (of members) which are explicitly marked as also being
18781      members of this (containing) type themselves.  The g++ front- end can
18782      force any given type to be treated as a member of some other (containing)
18783      type by setting the TYPE_CONTEXT of the given (member) type to point to
18784      the TREE node representing the appropriate (containing) type.  */
18785
18786   /* First output info about the base classes.  */
18787   if (binfo)
18788     {
18789       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
18790       int i;
18791       tree base;
18792
18793       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
18794         gen_inheritance_die (base,
18795                              (accesses ? VEC_index (tree, accesses, i)
18796                               : access_public_node), context_die);
18797     }
18798
18799   /* Now output info about the data members and type members.  */
18800   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
18801     {
18802       /* If we thought we were generating minimal debug info for TYPE
18803          and then changed our minds, some of the member declarations
18804          may have already been defined.  Don't define them again, but
18805          do put them in the right order.  */
18806
18807       child = lookup_decl_die (member);
18808       if (child)
18809         splice_child_die (context_die, child);
18810       else
18811         gen_decl_die (member, NULL, context_die);
18812     }
18813
18814   /* Now output info about the function members (if any).  */
18815   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
18816     {
18817       /* Don't include clones in the member list.  */
18818       if (DECL_ABSTRACT_ORIGIN (member))
18819         continue;
18820
18821       child = lookup_decl_die (member);
18822       if (child)
18823         splice_child_die (context_die, child);
18824       else
18825         gen_decl_die (member, NULL, context_die);
18826     }
18827 }
18828
18829 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
18830    is set, we pretend that the type was never defined, so we only get the
18831    member DIEs needed by later specification DIEs.  */
18832
18833 static void
18834 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
18835                                 enum debug_info_usage usage)
18836 {
18837   dw_die_ref type_die = lookup_type_die (type);
18838   dw_die_ref scope_die = 0;
18839   int nested = 0;
18840   int complete = (TYPE_SIZE (type)
18841                   && (! TYPE_STUB_DECL (type)
18842                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
18843   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
18844   complete = complete && should_emit_struct_debug (type, usage);
18845
18846   if (type_die && ! complete)
18847     return;
18848
18849   if (TYPE_CONTEXT (type) != NULL_TREE
18850       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
18851           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
18852     nested = 1;
18853
18854   scope_die = scope_die_for (type, context_die);
18855
18856   if (! type_die || (nested && is_cu_die (scope_die)))
18857     /* First occurrence of type or toplevel definition of nested class.  */
18858     {
18859       dw_die_ref old_die = type_die;
18860
18861       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
18862                           ? record_type_tag (type) : DW_TAG_union_type,
18863                           scope_die, type);
18864       equate_type_number_to_die (type, type_die);
18865       if (old_die)
18866         add_AT_specification (type_die, old_die);
18867       else
18868         {
18869           add_name_attribute (type_die, type_tag (type));
18870           add_gnat_descriptive_type_attribute (type_die, type, context_die);
18871           if (TYPE_ARTIFICIAL (type))
18872             add_AT_flag (type_die, DW_AT_artificial, 1);
18873         }
18874     }
18875   else
18876     remove_AT (type_die, DW_AT_declaration);
18877
18878   /* Generate child dies for template paramaters.  */
18879   if (debug_info_level > DINFO_LEVEL_TERSE
18880       && COMPLETE_TYPE_P (type))
18881     schedule_generic_params_dies_gen (type);
18882
18883   /* If this type has been completed, then give it a byte_size attribute and
18884      then give a list of members.  */
18885   if (complete && !ns_decl)
18886     {
18887       /* Prevent infinite recursion in cases where the type of some member of
18888          this type is expressed in terms of this type itself.  */
18889       TREE_ASM_WRITTEN (type) = 1;
18890       add_byte_size_attribute (type_die, type);
18891       if (TYPE_STUB_DECL (type) != NULL_TREE)
18892         {
18893           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18894           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18895         }
18896
18897       /* If the first reference to this type was as the return type of an
18898          inline function, then it may not have a parent.  Fix this now.  */
18899       if (type_die->die_parent == NULL)
18900         add_child_die (scope_die, type_die);
18901
18902       push_decl_scope (type);
18903       gen_member_die (type, type_die);
18904       pop_decl_scope ();
18905
18906       /* GNU extension: Record what type our vtable lives in.  */
18907       if (TYPE_VFIELD (type))
18908         {
18909           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
18910
18911           gen_type_die (vtype, context_die);
18912           add_AT_die_ref (type_die, DW_AT_containing_type,
18913                           lookup_type_die (vtype));
18914         }
18915     }
18916   else
18917     {
18918       add_AT_flag (type_die, DW_AT_declaration, 1);
18919
18920       /* We don't need to do this for function-local types.  */
18921       if (TYPE_STUB_DECL (type)
18922           && ! decl_function_context (TYPE_STUB_DECL (type)))
18923         VEC_safe_push (tree, gc, incomplete_types, type);
18924     }
18925
18926   if (get_AT (type_die, DW_AT_name))
18927     add_pubtype (type, type_die);
18928 }
18929
18930 /* Generate a DIE for a subroutine _type_.  */
18931
18932 static void
18933 gen_subroutine_type_die (tree type, dw_die_ref context_die)
18934 {
18935   tree return_type = TREE_TYPE (type);
18936   dw_die_ref subr_die
18937     = new_die (DW_TAG_subroutine_type,
18938                scope_die_for (type, context_die), type);
18939
18940   equate_type_number_to_die (type, subr_die);
18941   add_prototyped_attribute (subr_die, type);
18942   add_type_attribute (subr_die, return_type, 0, 0, context_die);
18943   gen_formal_types_die (type, subr_die);
18944
18945   if (get_AT (subr_die, DW_AT_name))
18946     add_pubtype (type, subr_die);
18947 }
18948
18949 /* Generate a DIE for a type definition.  */
18950
18951 static void
18952 gen_typedef_die (tree decl, dw_die_ref context_die)
18953 {
18954   dw_die_ref type_die;
18955   tree origin;
18956
18957   if (TREE_ASM_WRITTEN (decl))
18958     return;
18959
18960   TREE_ASM_WRITTEN (decl) = 1;
18961   type_die = new_die (DW_TAG_typedef, context_die, decl);
18962   origin = decl_ultimate_origin (decl);
18963   if (origin != NULL)
18964     add_abstract_origin_attribute (type_die, origin);
18965   else
18966     {
18967       tree type;
18968
18969       add_name_and_src_coords_attributes (type_die, decl);
18970       if (DECL_ORIGINAL_TYPE (decl))
18971         {
18972           type = DECL_ORIGINAL_TYPE (decl);
18973
18974           gcc_assert (type != TREE_TYPE (decl));
18975           equate_type_number_to_die (TREE_TYPE (decl), type_die);
18976         }
18977       else
18978         {
18979           type = TREE_TYPE (decl);
18980
18981           if (is_naming_typedef_decl (TYPE_NAME (type)))
18982             {
18983               /* Here, we are in the case of decl being a typedef naming
18984                  an anonymous type, e.g:
18985                      typedef struct {...} foo;
18986                  In that case TREE_TYPE (decl) is not a typedef variant
18987                  type and TYPE_NAME of the anonymous type is set to the
18988                  TYPE_DECL of the typedef. This construct is emitted by
18989                  the C++ FE.
18990
18991                  TYPE is the anonymous struct named by the typedef
18992                  DECL. As we need the DW_AT_type attribute of the
18993                  DW_TAG_typedef to point to the DIE of TYPE, let's
18994                  generate that DIE right away. add_type_attribute
18995                  called below will then pick (via lookup_type_die) that
18996                  anonymous struct DIE.  */
18997               if (!TREE_ASM_WRITTEN (type))
18998                 gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
18999
19000               /* This is a GNU Extension.  We are adding a
19001                  DW_AT_linkage_name attribute to the DIE of the
19002                  anonymous struct TYPE.  The value of that attribute
19003                  is the name of the typedef decl naming the anonymous
19004                  struct.  This greatly eases the work of consumers of
19005                  this debug info.  */
19006               add_linkage_attr (lookup_type_die (type), decl);
19007             }
19008         }
19009
19010       add_type_attribute (type_die, type, TREE_READONLY (decl),
19011                           TREE_THIS_VOLATILE (decl), context_die);
19012
19013       if (is_naming_typedef_decl (decl))
19014         /* We want that all subsequent calls to lookup_type_die with
19015            TYPE in argument yield the DW_TAG_typedef we have just
19016            created.  */
19017         equate_type_number_to_die (type, type_die);
19018
19019       add_accessibility_attribute (type_die, decl);
19020     }
19021
19022   if (DECL_ABSTRACT (decl))
19023     equate_decl_number_to_die (decl, type_die);
19024
19025   if (get_AT (type_die, DW_AT_name))
19026     add_pubtype (decl, type_die);
19027 }
19028
19029 /* Generate a DIE for a struct, class, enum or union type.  */
19030
19031 static void
19032 gen_tagged_type_die (tree type,
19033                      dw_die_ref context_die,
19034                      enum debug_info_usage usage)
19035 {
19036   int need_pop;
19037
19038   if (type == NULL_TREE
19039       || !is_tagged_type (type))
19040     return;
19041
19042   /* If this is a nested type whose containing class hasn't been written
19043      out yet, writing it out will cover this one, too.  This does not apply
19044      to instantiations of member class templates; they need to be added to
19045      the containing class as they are generated.  FIXME: This hurts the
19046      idea of combining type decls from multiple TUs, since we can't predict
19047      what set of template instantiations we'll get.  */
19048   if (TYPE_CONTEXT (type)
19049       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19050       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19051     {
19052       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19053
19054       if (TREE_ASM_WRITTEN (type))
19055         return;
19056
19057       /* If that failed, attach ourselves to the stub.  */
19058       push_decl_scope (TYPE_CONTEXT (type));
19059       context_die = lookup_type_die (TYPE_CONTEXT (type));
19060       need_pop = 1;
19061     }
19062   else if (TYPE_CONTEXT (type) != NULL_TREE
19063            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19064     {
19065       /* If this type is local to a function that hasn't been written
19066          out yet, use a NULL context for now; it will be fixed up in
19067          decls_for_scope.  */
19068       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19069       /* A declaration DIE doesn't count; nested types need to go in the
19070          specification.  */
19071       if (context_die && is_declaration_die (context_die))
19072         context_die = NULL;
19073       need_pop = 0;
19074     }
19075   else
19076     {
19077       context_die = declare_in_namespace (type, context_die);
19078       need_pop = 0;
19079     }
19080
19081   if (TREE_CODE (type) == ENUMERAL_TYPE)
19082     {
19083       /* This might have been written out by the call to
19084          declare_in_namespace.  */
19085       if (!TREE_ASM_WRITTEN (type))
19086         gen_enumeration_type_die (type, context_die);
19087     }
19088   else
19089     gen_struct_or_union_type_die (type, context_die, usage);
19090
19091   if (need_pop)
19092     pop_decl_scope ();
19093
19094   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19095      it up if it is ever completed.  gen_*_type_die will set it for us
19096      when appropriate.  */
19097 }
19098
19099 /* Generate a type description DIE.  */
19100
19101 static void
19102 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19103                          enum debug_info_usage usage)
19104 {
19105   struct array_descr_info info;
19106
19107   if (type == NULL_TREE || type == error_mark_node)
19108     return;
19109
19110   if (TYPE_NAME (type) != NULL_TREE
19111       && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
19112       && is_redundant_typedef (TYPE_NAME (type))
19113       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
19114     /* The DECL of this type is a typedef we don't want to emit debug
19115        info for but we want debug info for its underlying typedef.
19116        This can happen for e.g, the injected-class-name of a C++
19117        type.  */
19118     type = DECL_ORIGINAL_TYPE (TYPE_NAME (type));
19119
19120   /* If TYPE is a typedef type variant, let's generate debug info
19121      for the parent typedef which TYPE is a type of.  */
19122   if (typedef_variant_p (type))
19123     {
19124       if (TREE_ASM_WRITTEN (type))
19125         return;
19126
19127       /* Prevent broken recursion; we can't hand off to the same type.  */
19128       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19129
19130       /* Use the DIE of the containing namespace as the parent DIE of
19131          the type description DIE we want to generate.  */
19132       if (DECL_CONTEXT (TYPE_NAME (type))
19133           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19134         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19135
19136       TREE_ASM_WRITTEN (type) = 1;
19137
19138       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19139       return;
19140     }
19141
19142   /* If type is an anonymous tagged type named by a typedef, let's
19143      generate debug info for the typedef.  */
19144   if (is_naming_typedef_decl (TYPE_NAME (type)))
19145     {
19146       /* Use the DIE of the containing namespace as the parent DIE of
19147          the type description DIE we want to generate.  */
19148       if (DECL_CONTEXT (TYPE_NAME (type))
19149           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19150         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19151       
19152       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19153       return;
19154     }
19155
19156   /* If this is an array type with hidden descriptor, handle it first.  */
19157   if (!TREE_ASM_WRITTEN (type)
19158       && lang_hooks.types.get_array_descr_info
19159       && lang_hooks.types.get_array_descr_info (type, &info)
19160       && (dwarf_version >= 3 || !dwarf_strict))
19161     {
19162       gen_descr_array_type_die (type, &info, context_die);
19163       TREE_ASM_WRITTEN (type) = 1;
19164       return;
19165     }
19166
19167   /* We are going to output a DIE to represent the unqualified version
19168      of this type (i.e. without any const or volatile qualifiers) so
19169      get the main variant (i.e. the unqualified version) of this type
19170      now.  (Vectors are special because the debugging info is in the
19171      cloned type itself).  */
19172   if (TREE_CODE (type) != VECTOR_TYPE)
19173     type = type_main_variant (type);
19174
19175   if (TREE_ASM_WRITTEN (type))
19176     return;
19177
19178   switch (TREE_CODE (type))
19179     {
19180     case ERROR_MARK:
19181       break;
19182
19183     case POINTER_TYPE:
19184     case REFERENCE_TYPE:
19185       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19186          ensures that the gen_type_die recursion will terminate even if the
19187          type is recursive.  Recursive types are possible in Ada.  */
19188       /* ??? We could perhaps do this for all types before the switch
19189          statement.  */
19190       TREE_ASM_WRITTEN (type) = 1;
19191
19192       /* For these types, all that is required is that we output a DIE (or a
19193          set of DIEs) to represent the "basis" type.  */
19194       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19195                                 DINFO_USAGE_IND_USE);
19196       break;
19197
19198     case OFFSET_TYPE:
19199       /* This code is used for C++ pointer-to-data-member types.
19200          Output a description of the relevant class type.  */
19201       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19202                                         DINFO_USAGE_IND_USE);
19203
19204       /* Output a description of the type of the object pointed to.  */
19205       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19206                                         DINFO_USAGE_IND_USE);
19207
19208       /* Now output a DIE to represent this pointer-to-data-member type
19209          itself.  */
19210       gen_ptr_to_mbr_type_die (type, context_die);
19211       break;
19212
19213     case FUNCTION_TYPE:
19214       /* Force out return type (in case it wasn't forced out already).  */
19215       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19216                                         DINFO_USAGE_DIR_USE);
19217       gen_subroutine_type_die (type, context_die);
19218       break;
19219
19220     case METHOD_TYPE:
19221       /* Force out return type (in case it wasn't forced out already).  */
19222       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19223                                         DINFO_USAGE_DIR_USE);
19224       gen_subroutine_type_die (type, context_die);
19225       break;
19226
19227     case ARRAY_TYPE:
19228       gen_array_type_die (type, context_die);
19229       break;
19230
19231     case VECTOR_TYPE:
19232       gen_array_type_die (type, context_die);
19233       break;
19234
19235     case ENUMERAL_TYPE:
19236     case RECORD_TYPE:
19237     case UNION_TYPE:
19238     case QUAL_UNION_TYPE:
19239       gen_tagged_type_die (type, context_die, usage);
19240       return;
19241
19242     case VOID_TYPE:
19243     case INTEGER_TYPE:
19244     case REAL_TYPE:
19245     case FIXED_POINT_TYPE:
19246     case COMPLEX_TYPE:
19247     case BOOLEAN_TYPE:
19248       /* No DIEs needed for fundamental types.  */
19249       break;
19250
19251     case NULLPTR_TYPE:
19252     case LANG_TYPE:
19253       /* Just use DW_TAG_unspecified_type.  */
19254       {
19255         dw_die_ref type_die = lookup_type_die (type);
19256         if (type_die == NULL)
19257           {
19258             tree name = TYPE_NAME (type);
19259             if (TREE_CODE (name) == TYPE_DECL)
19260               name = DECL_NAME (name);
19261             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die (), type);
19262             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
19263             equate_type_number_to_die (type, type_die);
19264           }
19265       }
19266       break;
19267
19268     default:
19269       gcc_unreachable ();
19270     }
19271
19272   TREE_ASM_WRITTEN (type) = 1;
19273 }
19274
19275 static void
19276 gen_type_die (tree type, dw_die_ref context_die)
19277 {
19278   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
19279 }
19280
19281 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
19282    things which are local to the given block.  */
19283
19284 static void
19285 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
19286 {
19287   int must_output_die = 0;
19288   bool inlined_func;
19289
19290   /* Ignore blocks that are NULL.  */
19291   if (stmt == NULL_TREE)
19292     return;
19293
19294   inlined_func = inlined_function_outer_scope_p (stmt);
19295
19296   /* If the block is one fragment of a non-contiguous block, do not
19297      process the variables, since they will have been done by the
19298      origin block.  Do process subblocks.  */
19299   if (BLOCK_FRAGMENT_ORIGIN (stmt))
19300     {
19301       tree sub;
19302
19303       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
19304         gen_block_die (sub, context_die, depth + 1);
19305
19306       return;
19307     }
19308
19309   /* Determine if we need to output any Dwarf DIEs at all to represent this
19310      block.  */
19311   if (inlined_func)
19312     /* The outer scopes for inlinings *must* always be represented.  We
19313        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
19314     must_output_die = 1;
19315   else
19316     {
19317       /* Determine if this block directly contains any "significant"
19318          local declarations which we will need to output DIEs for.  */
19319       if (debug_info_level > DINFO_LEVEL_TERSE)
19320         /* We are not in terse mode so *any* local declaration counts
19321            as being a "significant" one.  */
19322         must_output_die = ((BLOCK_VARS (stmt) != NULL
19323                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
19324                            && (TREE_USED (stmt)
19325                                || TREE_ASM_WRITTEN (stmt)
19326                                || BLOCK_ABSTRACT (stmt)));
19327       else if ((TREE_USED (stmt)
19328                 || TREE_ASM_WRITTEN (stmt)
19329                 || BLOCK_ABSTRACT (stmt))
19330                && !dwarf2out_ignore_block (stmt))
19331         must_output_die = 1;
19332     }
19333
19334   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
19335      DIE for any block which contains no significant local declarations at
19336      all.  Rather, in such cases we just call `decls_for_scope' so that any
19337      needed Dwarf info for any sub-blocks will get properly generated. Note
19338      that in terse mode, our definition of what constitutes a "significant"
19339      local declaration gets restricted to include only inlined function
19340      instances and local (nested) function definitions.  */
19341   if (must_output_die)
19342     {
19343       if (inlined_func)
19344         {
19345           /* If STMT block is abstract, that means we have been called
19346              indirectly from dwarf2out_abstract_function.
19347              That function rightfully marks the descendent blocks (of
19348              the abstract function it is dealing with) as being abstract,
19349              precisely to prevent us from emitting any
19350              DW_TAG_inlined_subroutine DIE as a descendent
19351              of an abstract function instance. So in that case, we should
19352              not call gen_inlined_subroutine_die.
19353
19354              Later though, when cgraph asks dwarf2out to emit info
19355              for the concrete instance of the function decl into which
19356              the concrete instance of STMT got inlined, the later will lead
19357              to the generation of a DW_TAG_inlined_subroutine DIE.  */
19358           if (! BLOCK_ABSTRACT (stmt))
19359             gen_inlined_subroutine_die (stmt, context_die, depth);
19360         }
19361       else
19362         gen_lexical_block_die (stmt, context_die, depth);
19363     }
19364   else
19365     decls_for_scope (stmt, context_die, depth);
19366 }
19367
19368 /* Process variable DECL (or variable with origin ORIGIN) within
19369    block STMT and add it to CONTEXT_DIE.  */
19370 static void
19371 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
19372 {
19373   dw_die_ref die;
19374   tree decl_or_origin = decl ? decl : origin;
19375
19376   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
19377     die = lookup_decl_die (decl_or_origin);
19378   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
19379            && TYPE_DECL_IS_STUB (decl_or_origin))
19380     die = lookup_type_die (TREE_TYPE (decl_or_origin));
19381   else
19382     die = NULL;
19383
19384   if (die != NULL && die->die_parent == NULL)
19385     add_child_die (context_die, die);
19386   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
19387     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
19388                                          stmt, context_die);
19389   else
19390     gen_decl_die (decl, origin, context_die);
19391 }
19392
19393 /* Generate all of the decls declared within a given scope and (recursively)
19394    all of its sub-blocks.  */
19395
19396 static void
19397 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
19398 {
19399   tree decl;
19400   unsigned int i;
19401   tree subblocks;
19402
19403   /* Ignore NULL blocks.  */
19404   if (stmt == NULL_TREE)
19405     return;
19406
19407   /* Output the DIEs to represent all of the data objects and typedefs
19408      declared directly within this block but not within any nested
19409      sub-blocks.  Also, nested function and tag DIEs have been
19410      generated with a parent of NULL; fix that up now.  */
19411   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
19412     process_scope_var (stmt, decl, NULL_TREE, context_die);
19413   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
19414     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
19415                        context_die);
19416
19417   /* If we're at -g1, we're not interested in subblocks.  */
19418   if (debug_info_level <= DINFO_LEVEL_TERSE)
19419     return;
19420
19421   /* Output the DIEs to represent all sub-blocks (and the items declared
19422      therein) of this block.  */
19423   for (subblocks = BLOCK_SUBBLOCKS (stmt);
19424        subblocks != NULL;
19425        subblocks = BLOCK_CHAIN (subblocks))
19426     gen_block_die (subblocks, context_die, depth + 1);
19427 }
19428
19429 /* Is this a typedef we can avoid emitting?  */
19430
19431 static inline int
19432 is_redundant_typedef (const_tree decl)
19433 {
19434   if (TYPE_DECL_IS_STUB (decl))
19435     return 1;
19436
19437   if (DECL_ARTIFICIAL (decl)
19438       && DECL_CONTEXT (decl)
19439       && is_tagged_type (DECL_CONTEXT (decl))
19440       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
19441       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
19442     /* Also ignore the artificial member typedef for the class name.  */
19443     return 1;
19444
19445   return 0;
19446 }
19447
19448 /* Return TRUE if TYPE is a typedef that names a type for linkage
19449    purposes. This kind of typedefs is produced by the C++ FE for
19450    constructs like:
19451
19452    typedef struct {...} foo;
19453
19454    In that case, there is no typedef variant type produced for foo.
19455    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
19456    struct type.  */
19457
19458 static bool
19459 is_naming_typedef_decl (const_tree decl)
19460 {
19461   if (decl == NULL_TREE
19462       || TREE_CODE (decl) != TYPE_DECL
19463       || !is_tagged_type (TREE_TYPE (decl))
19464       || DECL_IS_BUILTIN (decl)
19465       || is_redundant_typedef (decl)
19466       /* It looks like Ada produces TYPE_DECLs that are very similar
19467          to C++ naming typedefs but that have different
19468          semantics. Let's be specific to c++ for now.  */
19469       || !is_cxx ())
19470     return FALSE;
19471
19472   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
19473           && TYPE_NAME (TREE_TYPE (decl)) == decl
19474           && (TYPE_STUB_DECL (TREE_TYPE (decl))
19475               != TYPE_NAME (TREE_TYPE (decl))));
19476 }
19477
19478 /* Returns the DIE for a context.  */
19479
19480 static inline dw_die_ref
19481 get_context_die (tree context)
19482 {
19483   if (context)
19484     {
19485       /* Find die that represents this context.  */
19486       if (TYPE_P (context))
19487         {
19488           context = TYPE_MAIN_VARIANT (context);
19489           return strip_naming_typedef (context, force_type_die (context));
19490         }
19491       else
19492         return force_decl_die (context);
19493     }
19494   return comp_unit_die ();
19495 }
19496
19497 /* Returns the DIE for decl.  A DIE will always be returned.  */
19498
19499 static dw_die_ref
19500 force_decl_die (tree decl)
19501 {
19502   dw_die_ref decl_die;
19503   unsigned saved_external_flag;
19504   tree save_fn = NULL_TREE;
19505   decl_die = lookup_decl_die (decl);
19506   if (!decl_die)
19507     {
19508       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
19509
19510       decl_die = lookup_decl_die (decl);
19511       if (decl_die)
19512         return decl_die;
19513
19514       switch (TREE_CODE (decl))
19515         {
19516         case FUNCTION_DECL:
19517           /* Clear current_function_decl, so that gen_subprogram_die thinks
19518              that this is a declaration. At this point, we just want to force
19519              declaration die.  */
19520           save_fn = current_function_decl;
19521           current_function_decl = NULL_TREE;
19522           gen_subprogram_die (decl, context_die);
19523           current_function_decl = save_fn;
19524           break;
19525
19526         case VAR_DECL:
19527           /* Set external flag to force declaration die. Restore it after
19528            gen_decl_die() call.  */
19529           saved_external_flag = DECL_EXTERNAL (decl);
19530           DECL_EXTERNAL (decl) = 1;
19531           gen_decl_die (decl, NULL, context_die);
19532           DECL_EXTERNAL (decl) = saved_external_flag;
19533           break;
19534
19535         case NAMESPACE_DECL:
19536           if (dwarf_version >= 3 || !dwarf_strict)
19537             dwarf2out_decl (decl);
19538           else
19539             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
19540             decl_die = comp_unit_die ();
19541           break;
19542
19543         case TRANSLATION_UNIT_DECL:
19544           decl_die = comp_unit_die ();
19545           break;
19546
19547         default:
19548           gcc_unreachable ();
19549         }
19550
19551       /* We should be able to find the DIE now.  */
19552       if (!decl_die)
19553         decl_die = lookup_decl_die (decl);
19554       gcc_assert (decl_die);
19555     }
19556
19557   return decl_die;
19558 }
19559
19560 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
19561    always returned.  */
19562
19563 static dw_die_ref
19564 force_type_die (tree type)
19565 {
19566   dw_die_ref type_die;
19567
19568   type_die = lookup_type_die (type);
19569   if (!type_die)
19570     {
19571       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
19572
19573       type_die = modified_type_die (type, TYPE_READONLY (type),
19574                                     TYPE_VOLATILE (type), context_die);
19575       gcc_assert (type_die);
19576     }
19577   return type_die;
19578 }
19579
19580 /* Force out any required namespaces to be able to output DECL,
19581    and return the new context_die for it, if it's changed.  */
19582
19583 static dw_die_ref
19584 setup_namespace_context (tree thing, dw_die_ref context_die)
19585 {
19586   tree context = (DECL_P (thing)
19587                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
19588   if (context && TREE_CODE (context) == NAMESPACE_DECL)
19589     /* Force out the namespace.  */
19590     context_die = force_decl_die (context);
19591
19592   return context_die;
19593 }
19594
19595 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
19596    type) within its namespace, if appropriate.
19597
19598    For compatibility with older debuggers, namespace DIEs only contain
19599    declarations; all definitions are emitted at CU scope.  */
19600
19601 static dw_die_ref
19602 declare_in_namespace (tree thing, dw_die_ref context_die)
19603 {
19604   dw_die_ref ns_context;
19605
19606   if (debug_info_level <= DINFO_LEVEL_TERSE)
19607     return context_die;
19608
19609   /* If this decl is from an inlined function, then don't try to emit it in its
19610      namespace, as we will get confused.  It would have already been emitted
19611      when the abstract instance of the inline function was emitted anyways.  */
19612   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
19613     return context_die;
19614
19615   ns_context = setup_namespace_context (thing, context_die);
19616
19617   if (ns_context != context_die)
19618     {
19619       if (is_fortran ())
19620         return ns_context;
19621       if (DECL_P (thing))
19622         gen_decl_die (thing, NULL, ns_context);
19623       else
19624         gen_type_die (thing, ns_context);
19625     }
19626   return context_die;
19627 }
19628
19629 /* Generate a DIE for a namespace or namespace alias.  */
19630
19631 static void
19632 gen_namespace_die (tree decl, dw_die_ref context_die)
19633 {
19634   dw_die_ref namespace_die;
19635
19636   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
19637      they are an alias of.  */
19638   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
19639     {
19640       /* Output a real namespace or module.  */
19641       context_die = setup_namespace_context (decl, comp_unit_die ());
19642       namespace_die = new_die (is_fortran ()
19643                                ? DW_TAG_module : DW_TAG_namespace,
19644                                context_die, decl);
19645       /* For Fortran modules defined in different CU don't add src coords.  */
19646       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
19647         {
19648           const char *name = dwarf2_name (decl, 0);
19649           if (name)
19650             add_name_attribute (namespace_die, name);
19651         }
19652       else
19653         add_name_and_src_coords_attributes (namespace_die, decl);
19654       if (DECL_EXTERNAL (decl))
19655         add_AT_flag (namespace_die, DW_AT_declaration, 1);
19656       equate_decl_number_to_die (decl, namespace_die);
19657     }
19658   else
19659     {
19660       /* Output a namespace alias.  */
19661
19662       /* Force out the namespace we are an alias of, if necessary.  */
19663       dw_die_ref origin_die
19664         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
19665
19666       if (DECL_FILE_SCOPE_P (decl)
19667           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
19668         context_die = setup_namespace_context (decl, comp_unit_die ());
19669       /* Now create the namespace alias DIE.  */
19670       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
19671       add_name_and_src_coords_attributes (namespace_die, decl);
19672       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
19673       equate_decl_number_to_die (decl, namespace_die);
19674     }
19675 }
19676
19677 /* Generate Dwarf debug information for a decl described by DECL.
19678    The return value is currently only meaningful for PARM_DECLs,
19679    for all other decls it returns NULL.  */
19680
19681 static dw_die_ref
19682 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
19683 {
19684   tree decl_or_origin = decl ? decl : origin;
19685   tree class_origin = NULL, ultimate_origin;
19686
19687   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
19688     return NULL;
19689
19690   switch (TREE_CODE (decl_or_origin))
19691     {
19692     case ERROR_MARK:
19693       break;
19694
19695     case CONST_DECL:
19696       if (!is_fortran () && !is_ada ())
19697         {
19698           /* The individual enumerators of an enum type get output when we output
19699              the Dwarf representation of the relevant enum type itself.  */
19700           break;
19701         }
19702
19703       /* Emit its type.  */
19704       gen_type_die (TREE_TYPE (decl), context_die);
19705
19706       /* And its containing namespace.  */
19707       context_die = declare_in_namespace (decl, context_die);
19708
19709       gen_const_die (decl, context_die);
19710       break;
19711
19712     case FUNCTION_DECL:
19713       /* Don't output any DIEs to represent mere function declarations,
19714          unless they are class members or explicit block externs.  */
19715       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
19716           && DECL_FILE_SCOPE_P (decl_or_origin)
19717           && (current_function_decl == NULL_TREE
19718               || DECL_ARTIFICIAL (decl_or_origin)))
19719         break;
19720
19721 #if 0
19722       /* FIXME */
19723       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
19724          on local redeclarations of global functions.  That seems broken.  */
19725       if (current_function_decl != decl)
19726         /* This is only a declaration.  */;
19727 #endif
19728
19729       /* If we're emitting a clone, emit info for the abstract instance.  */
19730       if (origin || DECL_ORIGIN (decl) != decl)
19731         dwarf2out_abstract_function (origin
19732                                      ? DECL_ORIGIN (origin)
19733                                      : DECL_ABSTRACT_ORIGIN (decl));
19734
19735       /* If we're emitting an out-of-line copy of an inline function,
19736          emit info for the abstract instance and set up to refer to it.  */
19737       else if (cgraph_function_possibly_inlined_p (decl)
19738                && ! DECL_ABSTRACT (decl)
19739                && ! class_or_namespace_scope_p (context_die)
19740                /* dwarf2out_abstract_function won't emit a die if this is just
19741                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
19742                   that case, because that works only if we have a die.  */
19743                && DECL_INITIAL (decl) != NULL_TREE)
19744         {
19745           dwarf2out_abstract_function (decl);
19746           set_decl_origin_self (decl);
19747         }
19748
19749       /* Otherwise we're emitting the primary DIE for this decl.  */
19750       else if (debug_info_level > DINFO_LEVEL_TERSE)
19751         {
19752           /* Before we describe the FUNCTION_DECL itself, make sure that we
19753              have its containing type.  */
19754           if (!origin)
19755             origin = decl_class_context (decl);
19756           if (origin != NULL_TREE)
19757             gen_type_die (origin, context_die);
19758
19759           /* And its return type.  */
19760           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
19761
19762           /* And its virtual context.  */
19763           if (DECL_VINDEX (decl) != NULL_TREE)
19764             gen_type_die (DECL_CONTEXT (decl), context_die);
19765
19766           /* Make sure we have a member DIE for decl.  */
19767           if (origin != NULL_TREE)
19768             gen_type_die_for_member (origin, decl, context_die);
19769
19770           /* And its containing namespace.  */
19771           context_die = declare_in_namespace (decl, context_die);
19772         }
19773
19774       /* Now output a DIE to represent the function itself.  */
19775       if (decl)
19776         gen_subprogram_die (decl, context_die);
19777       break;
19778
19779     case TYPE_DECL:
19780       /* If we are in terse mode, don't generate any DIEs to represent any
19781          actual typedefs.  */
19782       if (debug_info_level <= DINFO_LEVEL_TERSE)
19783         break;
19784
19785       /* In the special case of a TYPE_DECL node representing the declaration
19786          of some type tag, if the given TYPE_DECL is marked as having been
19787          instantiated from some other (original) TYPE_DECL node (e.g. one which
19788          was generated within the original definition of an inline function) we
19789          used to generate a special (abbreviated) DW_TAG_structure_type,
19790          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
19791          should be actually referencing those DIEs, as variable DIEs with that
19792          type would be emitted already in the abstract origin, so it was always
19793          removed during unused type prunning.  Don't add anything in this
19794          case.  */
19795       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
19796         break;
19797
19798       if (is_redundant_typedef (decl))
19799         gen_type_die (TREE_TYPE (decl), context_die);
19800       else
19801         /* Output a DIE to represent the typedef itself.  */
19802         gen_typedef_die (decl, context_die);
19803       break;
19804
19805     case LABEL_DECL:
19806       if (debug_info_level >= DINFO_LEVEL_NORMAL)
19807         gen_label_die (decl, context_die);
19808       break;
19809
19810     case VAR_DECL:
19811     case RESULT_DECL:
19812       /* If we are in terse mode, don't generate any DIEs to represent any
19813          variable declarations or definitions.  */
19814       if (debug_info_level <= DINFO_LEVEL_TERSE)
19815         break;
19816
19817       /* Output any DIEs that are needed to specify the type of this data
19818          object.  */
19819       if (decl_by_reference_p (decl_or_origin))
19820         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19821       else
19822         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19823
19824       /* And its containing type.  */
19825       class_origin = decl_class_context (decl_or_origin);
19826       if (class_origin != NULL_TREE)
19827         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
19828
19829       /* And its containing namespace.  */
19830       context_die = declare_in_namespace (decl_or_origin, context_die);
19831
19832       /* Now output the DIE to represent the data object itself.  This gets
19833          complicated because of the possibility that the VAR_DECL really
19834          represents an inlined instance of a formal parameter for an inline
19835          function.  */
19836       ultimate_origin = decl_ultimate_origin (decl_or_origin);
19837       if (ultimate_origin != NULL_TREE
19838           && TREE_CODE (ultimate_origin) == PARM_DECL)
19839         gen_formal_parameter_die (decl, origin,
19840                                   true /* Emit name attribute.  */,
19841                                   context_die);
19842       else
19843         gen_variable_die (decl, origin, context_die);
19844       break;
19845
19846     case FIELD_DECL:
19847       /* Ignore the nameless fields that are used to skip bits but handle C++
19848          anonymous unions and structs.  */
19849       if (DECL_NAME (decl) != NULL_TREE
19850           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
19851           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
19852         {
19853           gen_type_die (member_declared_type (decl), context_die);
19854           gen_field_die (decl, context_die);
19855         }
19856       break;
19857
19858     case PARM_DECL:
19859       if (DECL_BY_REFERENCE (decl_or_origin))
19860         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
19861       else
19862         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
19863       return gen_formal_parameter_die (decl, origin,
19864                                        true /* Emit name attribute.  */,
19865                                        context_die);
19866
19867     case NAMESPACE_DECL:
19868     case IMPORTED_DECL:
19869       if (dwarf_version >= 3 || !dwarf_strict)
19870         gen_namespace_die (decl, context_die);
19871       break;
19872
19873     default:
19874       /* Probably some frontend-internal decl.  Assume we don't care.  */
19875       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
19876       break;
19877     }
19878
19879   return NULL;
19880 }
19881 \f
19882 /* Output debug information for global decl DECL.  Called from toplev.c after
19883    compilation proper has finished.  */
19884
19885 static void
19886 dwarf2out_global_decl (tree decl)
19887 {
19888   /* Output DWARF2 information for file-scope tentative data object
19889      declarations, file-scope (extern) function declarations (which
19890      had no corresponding body) and file-scope tagged type declarations
19891      and definitions which have not yet been forced out.  */
19892   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
19893     dwarf2out_decl (decl);
19894 }
19895
19896 /* Output debug information for type decl DECL.  Called from toplev.c
19897    and from language front ends (to record built-in types).  */
19898 static void
19899 dwarf2out_type_decl (tree decl, int local)
19900 {
19901   if (!local)
19902     dwarf2out_decl (decl);
19903 }
19904
19905 /* Output debug information for imported module or decl DECL.
19906    NAME is non-NULL name in the lexical block if the decl has been renamed.
19907    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
19908    that DECL belongs to.
19909    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
19910 static void
19911 dwarf2out_imported_module_or_decl_1 (tree decl,
19912                                      tree name,
19913                                      tree lexical_block,
19914                                      dw_die_ref lexical_block_die)
19915 {
19916   expanded_location xloc;
19917   dw_die_ref imported_die = NULL;
19918   dw_die_ref at_import_die;
19919
19920   if (TREE_CODE (decl) == IMPORTED_DECL)
19921     {
19922       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
19923       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
19924       gcc_assert (decl);
19925     }
19926   else
19927     xloc = expand_location (input_location);
19928
19929   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
19930     {
19931       at_import_die = force_type_die (TREE_TYPE (decl));
19932       /* For namespace N { typedef void T; } using N::T; base_type_die
19933          returns NULL, but DW_TAG_imported_declaration requires
19934          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
19935       if (!at_import_die)
19936         {
19937           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
19938           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
19939           at_import_die = lookup_type_die (TREE_TYPE (decl));
19940           gcc_assert (at_import_die);
19941         }
19942     }
19943   else
19944     {
19945       at_import_die = lookup_decl_die (decl);
19946       if (!at_import_die)
19947         {
19948           /* If we're trying to avoid duplicate debug info, we may not have
19949              emitted the member decl for this field.  Emit it now.  */
19950           if (TREE_CODE (decl) == FIELD_DECL)
19951             {
19952               tree type = DECL_CONTEXT (decl);
19953
19954               if (TYPE_CONTEXT (type)
19955                   && TYPE_P (TYPE_CONTEXT (type))
19956                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
19957                                                 DINFO_USAGE_DIR_USE))
19958                 return;
19959               gen_type_die_for_member (type, decl,
19960                                        get_context_die (TYPE_CONTEXT (type)));
19961             }
19962           at_import_die = force_decl_die (decl);
19963         }
19964     }
19965
19966   if (TREE_CODE (decl) == NAMESPACE_DECL)
19967     {
19968       if (dwarf_version >= 3 || !dwarf_strict)
19969         imported_die = new_die (DW_TAG_imported_module,
19970                                 lexical_block_die,
19971                                 lexical_block);
19972       else
19973         return;
19974     }
19975   else
19976     imported_die = new_die (DW_TAG_imported_declaration,
19977                             lexical_block_die,
19978                             lexical_block);
19979
19980   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
19981   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
19982   if (name)
19983     add_AT_string (imported_die, DW_AT_name,
19984                    IDENTIFIER_POINTER (name));
19985   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
19986 }
19987
19988 /* Output debug information for imported module or decl DECL.
19989    NAME is non-NULL name in context if the decl has been renamed.
19990    CHILD is true if decl is one of the renamed decls as part of
19991    importing whole module.  */
19992
19993 static void
19994 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
19995                                    bool child)
19996 {
19997   /* dw_die_ref at_import_die;  */
19998   dw_die_ref scope_die;
19999
20000   if (debug_info_level <= DINFO_LEVEL_TERSE)
20001     return;
20002
20003   gcc_assert (decl);
20004
20005   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20006      We need decl DIE for reference and scope die. First, get DIE for the decl
20007      itself.  */
20008
20009   /* Get the scope die for decl context. Use comp_unit_die for global module
20010      or decl. If die is not found for non globals, force new die.  */
20011   if (context
20012       && TYPE_P (context)
20013       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20014     return;
20015
20016   if (!(dwarf_version >= 3 || !dwarf_strict))
20017     return;
20018
20019   scope_die = get_context_die (context);
20020
20021   if (child)
20022     {
20023       gcc_assert (scope_die->die_child);
20024       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20025       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20026       scope_die = scope_die->die_child;
20027     }
20028
20029   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20030   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20031
20032 }
20033
20034 /* Write the debugging output for DECL.  */
20035
20036 void
20037 dwarf2out_decl (tree decl)
20038 {
20039   dw_die_ref context_die = comp_unit_die ();
20040
20041   switch (TREE_CODE (decl))
20042     {
20043     case ERROR_MARK:
20044       return;
20045
20046     case FUNCTION_DECL:
20047       /* What we would really like to do here is to filter out all mere
20048          file-scope declarations of file-scope functions which are never
20049          referenced later within this translation unit (and keep all of ones
20050          that *are* referenced later on) but we aren't clairvoyant, so we have
20051          no idea which functions will be referenced in the future (i.e. later
20052          on within the current translation unit). So here we just ignore all
20053          file-scope function declarations which are not also definitions.  If
20054          and when the debugger needs to know something about these functions,
20055          it will have to hunt around and find the DWARF information associated
20056          with the definition of the function.
20057
20058          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20059          nodes represent definitions and which ones represent mere
20060          declarations.  We have to check DECL_INITIAL instead. That's because
20061          the C front-end supports some weird semantics for "extern inline"
20062          function definitions.  These can get inlined within the current
20063          translation unit (and thus, we need to generate Dwarf info for their
20064          abstract instances so that the Dwarf info for the concrete inlined
20065          instances can have something to refer to) but the compiler never
20066          generates any out-of-lines instances of such things (despite the fact
20067          that they *are* definitions).
20068
20069          The important point is that the C front-end marks these "extern
20070          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20071          them anyway. Note that the C++ front-end also plays some similar games
20072          for inline function definitions appearing within include files which
20073          also contain `#pragma interface' pragmas.  */
20074       if (DECL_INITIAL (decl) == NULL_TREE)
20075         return;
20076
20077       /* If we're a nested function, initially use a parent of NULL; if we're
20078          a plain function, this will be fixed up in decls_for_scope.  If
20079          we're a method, it will be ignored, since we already have a DIE.  */
20080       if (decl_function_context (decl)
20081           /* But if we're in terse mode, we don't care about scope.  */
20082           && debug_info_level > DINFO_LEVEL_TERSE)
20083         context_die = NULL;
20084       break;
20085
20086     case VAR_DECL:
20087       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20088          declaration and if the declaration was never even referenced from
20089          within this entire compilation unit.  We suppress these DIEs in
20090          order to save space in the .debug section (by eliminating entries
20091          which are probably useless).  Note that we must not suppress
20092          block-local extern declarations (whether used or not) because that
20093          would screw-up the debugger's name lookup mechanism and cause it to
20094          miss things which really ought to be in scope at a given point.  */
20095       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20096         return;
20097
20098       /* For local statics lookup proper context die.  */
20099       if (TREE_STATIC (decl) && decl_function_context (decl))
20100         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20101
20102       /* If we are in terse mode, don't generate any DIEs to represent any
20103          variable declarations or definitions.  */
20104       if (debug_info_level <= DINFO_LEVEL_TERSE)
20105         return;
20106       break;
20107
20108     case CONST_DECL:
20109       if (debug_info_level <= DINFO_LEVEL_TERSE)
20110         return;
20111       if (!is_fortran () && !is_ada ())
20112         return;
20113       if (TREE_STATIC (decl) && decl_function_context (decl))
20114         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20115       break;
20116
20117     case NAMESPACE_DECL:
20118     case IMPORTED_DECL:
20119       if (debug_info_level <= DINFO_LEVEL_TERSE)
20120         return;
20121       if (lookup_decl_die (decl) != NULL)
20122         return;
20123       break;
20124
20125     case TYPE_DECL:
20126       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20127       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20128         return;
20129
20130       /* Don't bother trying to generate any DIEs to represent any of the
20131          normal built-in types for the language we are compiling.  */
20132       if (DECL_IS_BUILTIN (decl))
20133         return;
20134
20135       /* If we are in terse mode, don't generate any DIEs for types.  */
20136       if (debug_info_level <= DINFO_LEVEL_TERSE)
20137         return;
20138
20139       /* If we're a function-scope tag, initially use a parent of NULL;
20140          this will be fixed up in decls_for_scope.  */
20141       if (decl_function_context (decl))
20142         context_die = NULL;
20143
20144       break;
20145
20146     default:
20147       return;
20148     }
20149
20150   gen_decl_die (decl, NULL, context_die);
20151 }
20152
20153 /* Write the debugging output for DECL.  */
20154
20155 static void
20156 dwarf2out_function_decl (tree decl)
20157 {
20158   dwarf2out_decl (decl);
20159   call_arg_locations = NULL;
20160   call_arg_loc_last = NULL;
20161   call_site_count = -1;
20162   tail_call_site_count = -1;
20163   VEC_free (dw_die_ref, heap, block_map);
20164   htab_empty (decl_loc_table);
20165   htab_empty (cached_dw_loc_list_table);
20166 }
20167
20168 /* Output a marker (i.e. a label) for the beginning of the generated code for
20169    a lexical block.  */
20170
20171 static void
20172 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20173                        unsigned int blocknum)
20174 {
20175   switch_to_section (current_function_section ());
20176   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20177 }
20178
20179 /* Output a marker (i.e. a label) for the end of the generated code for a
20180    lexical block.  */
20181
20182 static void
20183 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20184 {
20185   switch_to_section (current_function_section ());
20186   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20187 }
20188
20189 /* Returns nonzero if it is appropriate not to emit any debugging
20190    information for BLOCK, because it doesn't contain any instructions.
20191
20192    Don't allow this for blocks with nested functions or local classes
20193    as we would end up with orphans, and in the presence of scheduling
20194    we may end up calling them anyway.  */
20195
20196 static bool
20197 dwarf2out_ignore_block (const_tree block)
20198 {
20199   tree decl;
20200   unsigned int i;
20201
20202   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20203     if (TREE_CODE (decl) == FUNCTION_DECL
20204         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20205       return 0;
20206   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20207     {
20208       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20209       if (TREE_CODE (decl) == FUNCTION_DECL
20210           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20211       return 0;
20212     }
20213
20214   return 1;
20215 }
20216
20217 /* Hash table routines for file_hash.  */
20218
20219 static int
20220 file_table_eq (const void *p1_p, const void *p2_p)
20221 {
20222   const struct dwarf_file_data *const p1 =
20223     (const struct dwarf_file_data *) p1_p;
20224   const char *const p2 = (const char *) p2_p;
20225   return filename_cmp (p1->filename, p2) == 0;
20226 }
20227
20228 static hashval_t
20229 file_table_hash (const void *p_p)
20230 {
20231   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
20232   return htab_hash_string (p->filename);
20233 }
20234
20235 /* Lookup FILE_NAME (in the list of filenames that we know about here in
20236    dwarf2out.c) and return its "index".  The index of each (known) filename is
20237    just a unique number which is associated with only that one filename.  We
20238    need such numbers for the sake of generating labels (in the .debug_sfnames
20239    section) and references to those files numbers (in the .debug_srcinfo
20240    and.debug_macinfo sections).  If the filename given as an argument is not
20241    found in our current list, add it to the list and assign it the next
20242    available unique index number.  In order to speed up searches, we remember
20243    the index of the filename was looked up last.  This handles the majority of
20244    all searches.  */
20245
20246 static struct dwarf_file_data *
20247 lookup_filename (const char *file_name)
20248 {
20249   void ** slot;
20250   struct dwarf_file_data * created;
20251
20252   /* Check to see if the file name that was searched on the previous
20253      call matches this file name.  If so, return the index.  */
20254   if (file_table_last_lookup
20255       && (file_name == file_table_last_lookup->filename
20256           || filename_cmp (file_table_last_lookup->filename, file_name) == 0))
20257     return file_table_last_lookup;
20258
20259   /* Didn't match the previous lookup, search the table.  */
20260   slot = htab_find_slot_with_hash (file_table, file_name,
20261                                    htab_hash_string (file_name), INSERT);
20262   if (*slot)
20263     return (struct dwarf_file_data *) *slot;
20264
20265   created = ggc_alloc_dwarf_file_data ();
20266   created->filename = file_name;
20267   created->emitted_number = 0;
20268   *slot = created;
20269   return created;
20270 }
20271
20272 /* If the assembler will construct the file table, then translate the compiler
20273    internal file table number into the assembler file table number, and emit
20274    a .file directive if we haven't already emitted one yet.  The file table
20275    numbers are different because we prune debug info for unused variables and
20276    types, which may include filenames.  */
20277
20278 static int
20279 maybe_emit_file (struct dwarf_file_data * fd)
20280 {
20281   if (! fd->emitted_number)
20282     {
20283       if (last_emitted_file)
20284         fd->emitted_number = last_emitted_file->emitted_number + 1;
20285       else
20286         fd->emitted_number = 1;
20287       last_emitted_file = fd;
20288
20289       if (DWARF2_ASM_LINE_DEBUG_INFO)
20290         {
20291           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
20292           output_quoted_string (asm_out_file,
20293                                 remap_debug_filename (fd->filename));
20294           fputc ('\n', asm_out_file);
20295         }
20296     }
20297
20298   return fd->emitted_number;
20299 }
20300
20301 /* Schedule generation of a DW_AT_const_value attribute to DIE.
20302    That generation should happen after function debug info has been
20303    generated. The value of the attribute is the constant value of ARG.  */
20304
20305 static void
20306 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
20307 {
20308   die_arg_entry entry;
20309
20310   if (!die || !arg)
20311     return;
20312
20313   if (!tmpl_value_parm_die_table)
20314     tmpl_value_parm_die_table
20315       = VEC_alloc (die_arg_entry, gc, 32);
20316
20317   entry.die = die;
20318   entry.arg = arg;
20319   VEC_safe_push (die_arg_entry, gc,
20320                  tmpl_value_parm_die_table,
20321                  &entry);
20322 }
20323
20324 /* Return TRUE if T is an instance of generic type, FALSE
20325    otherwise.  */
20326
20327 static bool
20328 generic_type_p (tree t)
20329 {
20330   if (t == NULL_TREE || !TYPE_P (t))
20331     return false;
20332   return lang_hooks.get_innermost_generic_parms (t) != NULL_TREE;
20333 }
20334
20335 /* Schedule the generation of the generic parameter dies for the
20336   instance of generic type T. The proper generation itself is later
20337   done by gen_scheduled_generic_parms_dies. */
20338
20339 static void
20340 schedule_generic_params_dies_gen (tree t)
20341 {
20342   if (!generic_type_p (t))
20343     return;
20344
20345   if (generic_type_instances == NULL)
20346     generic_type_instances = VEC_alloc (tree, gc, 256);
20347
20348   VEC_safe_push (tree, gc, generic_type_instances, t);
20349 }
20350
20351 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
20352    by append_entry_to_tmpl_value_parm_die_table. This function must
20353    be called after function DIEs have been generated.  */
20354
20355 static void
20356 gen_remaining_tmpl_value_param_die_attribute (void)
20357 {
20358   if (tmpl_value_parm_die_table)
20359     {
20360       unsigned i;
20361       die_arg_entry *e;
20362
20363       FOR_EACH_VEC_ELT (die_arg_entry, tmpl_value_parm_die_table, i, e)
20364         tree_add_const_value_attribute (e->die, e->arg);
20365     }
20366 }
20367
20368 /* Generate generic parameters DIEs for instances of generic types
20369    that have been previously scheduled by
20370    schedule_generic_params_dies_gen. This function must be called
20371    after all the types of the CU have been laid out.  */
20372
20373 static void
20374 gen_scheduled_generic_parms_dies (void)
20375 {
20376   unsigned i;
20377   tree t;
20378
20379   if (generic_type_instances == NULL)
20380     return;
20381   
20382   FOR_EACH_VEC_ELT (tree, generic_type_instances, i, t)
20383     gen_generic_params_dies (t);
20384 }
20385
20386
20387 /* Replace DW_AT_name for the decl with name.  */
20388
20389 static void
20390 dwarf2out_set_name (tree decl, tree name)
20391 {
20392   dw_die_ref die;
20393   dw_attr_ref attr;
20394   const char *dname;
20395
20396   die = TYPE_SYMTAB_DIE (decl);
20397   if (!die)
20398     return;
20399
20400   dname = dwarf2_name (name, 0);
20401   if (!dname)
20402     return;
20403
20404   attr = get_AT (die, DW_AT_name);
20405   if (attr)
20406     {
20407       struct indirect_string_node *node;
20408
20409       node = find_AT_string (dname);
20410       /* replace the string.  */
20411       attr->dw_attr_val.v.val_str = node;
20412     }
20413
20414   else
20415     add_name_attribute (die, dname);
20416 }
20417
20418 /* Called by the final INSN scan whenever we see a var location.  We
20419    use it to drop labels in the right places, and throw the location in
20420    our lookup table.  */
20421
20422 static void
20423 dwarf2out_var_location (rtx loc_note)
20424 {
20425   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
20426   struct var_loc_node *newloc;
20427   rtx next_real;
20428   static const char *last_label;
20429   static const char *last_postcall_label;
20430   static bool last_in_cold_section_p;
20431   tree decl;
20432   bool var_loc_p;
20433
20434   if (!NOTE_P (loc_note))
20435     {
20436       if (CALL_P (loc_note))
20437         {
20438           call_site_count++;
20439           if (SIBLING_CALL_P (loc_note))
20440             tail_call_site_count++;
20441         }
20442       return;
20443     }
20444
20445   var_loc_p = NOTE_KIND (loc_note) == NOTE_INSN_VAR_LOCATION;
20446   if (var_loc_p && !DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
20447     return;
20448
20449   next_real = next_real_insn (loc_note);
20450
20451   /* If there are no instructions which would be affected by this note,
20452      don't do anything.  */
20453   if (var_loc_p
20454       && next_real == NULL_RTX
20455       && !NOTE_DURING_CALL_P (loc_note))
20456     return;
20457
20458   if (next_real == NULL_RTX)
20459     next_real = get_last_insn ();
20460
20461   /* If there were any real insns between note we processed last time
20462      and this note (or if it is the first note), clear
20463      last_{,postcall_}label so that they are not reused this time.  */
20464   if (last_var_location_insn == NULL_RTX
20465       || last_var_location_insn != next_real
20466       || last_in_cold_section_p != in_cold_section_p)
20467     {
20468       last_label = NULL;
20469       last_postcall_label = NULL;
20470     }
20471
20472   if (var_loc_p)
20473     {
20474       decl = NOTE_VAR_LOCATION_DECL (loc_note);
20475       newloc = add_var_loc_to_decl (decl, loc_note,
20476                                     NOTE_DURING_CALL_P (loc_note)
20477                                     ? last_postcall_label : last_label);
20478       if (newloc == NULL)
20479         return;
20480     }
20481   else
20482     {
20483       decl = NULL_TREE;
20484       newloc = NULL;
20485     }
20486
20487   /* If there were no real insns between note we processed last time
20488      and this note, use the label we emitted last time.  Otherwise
20489      create a new label and emit it.  */
20490   if (last_label == NULL)
20491     {
20492       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
20493       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
20494       loclabel_num++;
20495       last_label = ggc_strdup (loclabel);
20496     }
20497
20498   if (!var_loc_p)
20499     {
20500       struct call_arg_loc_node *ca_loc
20501         = ggc_alloc_cleared_call_arg_loc_node ();
20502       rtx prev = prev_real_insn (loc_note), x;
20503       ca_loc->call_arg_loc_note = loc_note;
20504       ca_loc->next = NULL;
20505       ca_loc->label = last_label;
20506       gcc_assert (prev
20507                   && (CALL_P (prev)
20508                       || (NONJUMP_INSN_P (prev)
20509                           && GET_CODE (PATTERN (prev)) == SEQUENCE
20510                           && CALL_P (XVECEXP (PATTERN (prev), 0, 0)))));
20511       if (!CALL_P (prev))
20512         prev = XVECEXP (PATTERN (prev), 0, 0);
20513       ca_loc->tail_call_p = SIBLING_CALL_P (prev);
20514       x = PATTERN (prev);
20515       if (GET_CODE (x) == PARALLEL)
20516         x = XVECEXP (x, 0, 0);
20517       if (GET_CODE (x) == SET)
20518         x = SET_SRC (x);
20519       if (GET_CODE (x) == CALL && MEM_P (XEXP (x, 0)))
20520         {
20521           x = XEXP (XEXP (x, 0), 0);
20522           if (GET_CODE (x) == SYMBOL_REF
20523               && SYMBOL_REF_DECL (x)
20524               && TREE_CODE (SYMBOL_REF_DECL (x)) == FUNCTION_DECL)
20525             ca_loc->symbol_ref = x;
20526         }
20527       ca_loc->block = insn_scope (prev);
20528       if (call_arg_locations)
20529         call_arg_loc_last->next = ca_loc;
20530       else
20531         call_arg_locations = ca_loc;
20532       call_arg_loc_last = ca_loc;
20533     }
20534   else if (!NOTE_DURING_CALL_P (loc_note))
20535     newloc->label = last_label;
20536   else
20537     {
20538       if (!last_postcall_label)
20539         {
20540           sprintf (loclabel, "%s-1", last_label);
20541           last_postcall_label = ggc_strdup (loclabel);
20542         }
20543       newloc->label = last_postcall_label;
20544     }
20545
20546   last_var_location_insn = next_real;
20547   last_in_cold_section_p = in_cold_section_p;
20548 }
20549
20550 /* Note in one location list that text section has changed.  */
20551
20552 static int
20553 var_location_switch_text_section_1 (void **slot, void *data ATTRIBUTE_UNUSED)
20554 {
20555   var_loc_list *list = (var_loc_list *) *slot;
20556   if (list->first)
20557     list->last_before_switch
20558       = list->last->next ? list->last->next : list->last;
20559   return 1;
20560 }
20561
20562 /* Note in all location lists that text section has changed.  */
20563
20564 static void
20565 var_location_switch_text_section (void)
20566 {
20567   if (decl_loc_table == NULL)
20568     return;
20569
20570   htab_traverse (decl_loc_table, var_location_switch_text_section_1, NULL);
20571 }
20572
20573 /* Create a new line number table.  */
20574
20575 static dw_line_info_table *
20576 new_line_info_table (void)
20577 {
20578   dw_line_info_table *table;
20579
20580   table = ggc_alloc_cleared_dw_line_info_table_struct ();
20581   table->file_num = 1;
20582   table->line_num = 1;
20583   table->is_stmt = DWARF_LINE_DEFAULT_IS_STMT_START;
20584
20585   return table;
20586 }
20587
20588 /* Lookup the "current" table into which we emit line info, so
20589    that we don't have to do it for every source line.  */
20590
20591 static void
20592 set_cur_line_info_table (section *sec)
20593 {
20594   dw_line_info_table *table;
20595
20596   if (sec == text_section)
20597     table = text_section_line_info;
20598   else if (sec == cold_text_section)
20599     {
20600       table = cold_text_section_line_info;
20601       if (!table)
20602         {
20603           cold_text_section_line_info = table = new_line_info_table ();
20604           table->end_label = cold_end_label;
20605         }
20606     }
20607   else
20608     {
20609       const char *end_label;
20610
20611       if (flag_reorder_blocks_and_partition)
20612         {
20613           if (in_cold_section_p)
20614             end_label = crtl->subsections.cold_section_end_label;
20615           else
20616             end_label = crtl->subsections.hot_section_end_label;
20617         }
20618       else
20619         {
20620           char label[MAX_ARTIFICIAL_LABEL_BYTES];
20621           ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
20622                                        current_function_funcdef_no);
20623           end_label = ggc_strdup (label);
20624         }
20625
20626       table = new_line_info_table ();
20627       table->end_label = end_label;
20628
20629       VEC_safe_push (dw_line_info_table_p, gc, separate_line_info, table);
20630     }
20631
20632   cur_line_info_table = table;
20633 }
20634
20635
20636 /* We need to reset the locations at the beginning of each
20637    function. We can't do this in the end_function hook, because the
20638    declarations that use the locations won't have been output when
20639    that hook is called.  Also compute have_multiple_function_sections here.  */
20640
20641 static void
20642 dwarf2out_begin_function (tree fun)
20643 {
20644   section *sec = function_section (fun);
20645
20646   if (sec != text_section)
20647     have_multiple_function_sections = true;
20648
20649   if (flag_reorder_blocks_and_partition && !cold_text_section)
20650     {
20651       gcc_assert (current_function_decl == fun);
20652       cold_text_section = unlikely_text_section ();
20653       switch_to_section (cold_text_section);
20654       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
20655       switch_to_section (sec);
20656     }
20657
20658   dwarf2out_note_section_used ();
20659   call_site_count = 0;
20660   tail_call_site_count = 0;
20661
20662   set_cur_line_info_table (sec);
20663 }
20664
20665 /* Add OPCODE+VAL as an entry at the end of the opcode array in TABLE.  */
20666
20667 static void
20668 push_dw_line_info_entry (dw_line_info_table *table,
20669                          enum dw_line_info_opcode opcode, unsigned int val)
20670 {
20671   dw_line_info_entry e;
20672   e.opcode = opcode;
20673   e.val = val;
20674   VEC_safe_push (dw_line_info_entry, gc, table->entries, &e);
20675 }
20676
20677 /* Output a label to mark the beginning of a source code line entry
20678    and record information relating to this source line, in
20679    'line_info_table' for later output of the .debug_line section.  */
20680 /* ??? The discriminator parameter ought to be unsigned.  */
20681
20682 static void
20683 dwarf2out_source_line (unsigned int line, const char *filename,
20684                        int discriminator, bool is_stmt)
20685 {
20686   unsigned int file_num;
20687   dw_line_info_table *table;
20688
20689   if (debug_info_level < DINFO_LEVEL_NORMAL || line == 0)
20690     return;
20691
20692   /* The discriminator column was added in dwarf4.  Simplify the below
20693      by simply removing it if we're not supposed to output it.  */
20694   if (dwarf_version < 4 && dwarf_strict)
20695     discriminator = 0;
20696
20697   table = cur_line_info_table;
20698   file_num = maybe_emit_file (lookup_filename (filename));
20699
20700   /* ??? TODO: Elide duplicate line number entries.  Traditionally,
20701      the debugger has used the second (possibly duplicate) line number
20702      at the beginning of the function to mark the end of the prologue.
20703      We could eliminate any other duplicates within the function.  For
20704      Dwarf3, we ought to include the DW_LNS_set_prologue_end mark in
20705      that second line number entry.  */
20706   /* Recall that this end-of-prologue indication is *not* the same thing
20707      as the end_prologue debug hook.  The NOTE_INSN_PROLOGUE_END note,
20708      to which the hook corresponds, follows the last insn that was 
20709      emitted by gen_prologue.  What we need is to preceed the first insn
20710      that had been emitted after NOTE_INSN_FUNCTION_BEG, i.e. the first
20711      insn that corresponds to something the user wrote.  These may be
20712      very different locations once scheduling is enabled.  */
20713
20714   if (0 && file_num == table->file_num
20715       && line == table->line_num
20716       && discriminator == table->discrim_num
20717       && is_stmt == table->is_stmt)
20718     return;
20719
20720   switch_to_section (current_function_section ());
20721
20722   /* If requested, emit something human-readable.  */
20723   if (flag_debug_asm)
20724     fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START, filename, line);
20725
20726   if (DWARF2_ASM_LINE_DEBUG_INFO)
20727     {
20728       /* Emit the .loc directive understood by GNU as.  */
20729       fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
20730       if (is_stmt != table->is_stmt)
20731         fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
20732       if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
20733         fprintf (asm_out_file, " discriminator %d", discriminator);
20734       fputc ('\n', asm_out_file);
20735     }
20736   else
20737     {
20738       unsigned int label_num = ++line_info_label_num;
20739
20740       targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL, label_num);
20741
20742       push_dw_line_info_entry (table, LI_set_address, label_num);
20743       if (file_num != table->file_num)
20744         push_dw_line_info_entry (table, LI_set_file, file_num);
20745       if (discriminator != table->discrim_num)
20746         push_dw_line_info_entry (table, LI_set_discriminator, discriminator);
20747       if (is_stmt != table->is_stmt)
20748         push_dw_line_info_entry (table, LI_negate_stmt, 0);
20749       push_dw_line_info_entry (table, LI_set_line, line);
20750     }
20751
20752   table->file_num = file_num;
20753   table->line_num = line;
20754   table->discrim_num = discriminator;
20755   table->is_stmt = is_stmt;
20756   table->in_use = true;
20757 }
20758
20759 /* Record the beginning of a new source file.  */
20760
20761 static void
20762 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
20763 {
20764   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20765     {
20766       /* Record the beginning of the file for break_out_includes.  */
20767       dw_die_ref bincl_die;
20768
20769       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die (), NULL);
20770       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
20771     }
20772
20773   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20774     {
20775       macinfo_entry e;
20776       e.code = DW_MACINFO_start_file;
20777       e.lineno = lineno;
20778       e.info = xstrdup (filename);
20779       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20780     }
20781 }
20782
20783 /* Record the end of a source file.  */
20784
20785 static void
20786 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
20787 {
20788   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
20789     /* Record the end of the file for break_out_includes.  */
20790     new_die (DW_TAG_GNU_EINCL, comp_unit_die (), NULL);
20791
20792   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20793     {
20794       macinfo_entry e;
20795       e.code = DW_MACINFO_end_file;
20796       e.lineno = lineno;
20797       e.info = NULL;
20798       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20799     }
20800 }
20801
20802 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
20803    the tail part of the directive line, i.e. the part which is past the
20804    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20805
20806 static void
20807 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
20808                   const char *buffer ATTRIBUTE_UNUSED)
20809 {
20810   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20811     {
20812       macinfo_entry e;
20813       e.code = DW_MACINFO_define;
20814       e.lineno = lineno;
20815       e.info = xstrdup (buffer);;
20816       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20817     }
20818 }
20819
20820 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
20821    the tail part of the directive line, i.e. the part which is past the
20822    initial whitespace, #, whitespace, directive-name, whitespace part.  */
20823
20824 static void
20825 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
20826                  const char *buffer ATTRIBUTE_UNUSED)
20827 {
20828   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20829     {
20830       macinfo_entry e;
20831       e.code = DW_MACINFO_undef;
20832       e.lineno = lineno;
20833       e.info = xstrdup (buffer);;
20834       VEC_safe_push (macinfo_entry, gc, macinfo_table, &e);
20835     }
20836 }
20837
20838 static void
20839 output_macinfo (void)
20840 {
20841   unsigned i;
20842   unsigned long length = VEC_length (macinfo_entry, macinfo_table);
20843   macinfo_entry *ref;
20844
20845   if (! length)
20846     return;
20847
20848   for (i = 0; VEC_iterate (macinfo_entry, macinfo_table, i, ref); i++)
20849     {
20850       switch (ref->code)
20851         {
20852           case DW_MACINFO_start_file:
20853             {
20854               int file_num = maybe_emit_file (lookup_filename (ref->info));
20855               dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
20856               dw2_asm_output_data_uleb128 
20857                         (ref->lineno, "Included from line number %lu", 
20858                                                 (unsigned long)ref->lineno);
20859               dw2_asm_output_data_uleb128 (file_num, "file %s", ref->info);
20860             }
20861             break;
20862           case DW_MACINFO_end_file:
20863             dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
20864             break;
20865           case DW_MACINFO_define:
20866             dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
20867             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu", 
20868                                                 (unsigned long)ref->lineno);
20869             dw2_asm_output_nstring (ref->info, -1, "The macro");
20870             break;
20871           case DW_MACINFO_undef:
20872             dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
20873             dw2_asm_output_data_uleb128 (ref->lineno, "At line number %lu",
20874                                                 (unsigned long)ref->lineno);
20875             dw2_asm_output_nstring (ref->info, -1, "The macro");
20876             break;
20877           default:
20878            fprintf (asm_out_file, "%s unrecognized macinfo code %lu\n",
20879              ASM_COMMENT_START, (unsigned long)ref->code);
20880           break;
20881         }
20882     }
20883 }
20884
20885 /* Set up for Dwarf output at the start of compilation.  */
20886
20887 static void
20888 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
20889 {
20890   /* Allocate the file_table.  */
20891   file_table = htab_create_ggc (50, file_table_hash,
20892                                 file_table_eq, NULL);
20893
20894   /* Allocate the decl_die_table.  */
20895   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
20896                                     decl_die_table_eq, NULL);
20897
20898   /* Allocate the decl_loc_table.  */
20899   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
20900                                     decl_loc_table_eq, NULL);
20901
20902   /* Allocate the cached_dw_loc_list_table.  */
20903   cached_dw_loc_list_table
20904     = htab_create_ggc (10, cached_dw_loc_list_table_hash,
20905                        cached_dw_loc_list_table_eq, NULL);
20906
20907   /* Allocate the initial hunk of the decl_scope_table.  */
20908   decl_scope_table = VEC_alloc (tree, gc, 256);
20909
20910   /* Allocate the initial hunk of the abbrev_die_table.  */
20911   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
20912     (ABBREV_DIE_TABLE_INCREMENT);
20913   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
20914   /* Zero-th entry is allocated, but unused.  */
20915   abbrev_die_table_in_use = 1;
20916
20917   /* Allocate the pubtypes and pubnames vectors.  */
20918   pubname_table = VEC_alloc (pubname_entry, gc, 32);
20919   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
20920
20921   incomplete_types = VEC_alloc (tree, gc, 64);
20922
20923   used_rtx_array = VEC_alloc (rtx, gc, 32);
20924
20925   debug_info_section = get_section (DEBUG_INFO_SECTION,
20926                                     SECTION_DEBUG, NULL);
20927   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
20928                                       SECTION_DEBUG, NULL);
20929   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
20930                                        SECTION_DEBUG, NULL);
20931   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
20932                                        SECTION_DEBUG, NULL);
20933   debug_line_section = get_section (DEBUG_LINE_SECTION,
20934                                     SECTION_DEBUG, NULL);
20935   debug_loc_section = get_section (DEBUG_LOC_SECTION,
20936                                    SECTION_DEBUG, NULL);
20937   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
20938                                         SECTION_DEBUG, NULL);
20939   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
20940                                         SECTION_DEBUG, NULL);
20941   debug_str_section = get_section (DEBUG_STR_SECTION,
20942                                    DEBUG_STR_SECTION_FLAGS, NULL);
20943   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
20944                                       SECTION_DEBUG, NULL);
20945   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
20946                                      SECTION_DEBUG, NULL);
20947
20948   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
20949   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
20950                                DEBUG_ABBREV_SECTION_LABEL, 0);
20951   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
20952   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
20953                                COLD_TEXT_SECTION_LABEL, 0);
20954   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
20955
20956   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
20957                                DEBUG_INFO_SECTION_LABEL, 0);
20958   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
20959                                DEBUG_LINE_SECTION_LABEL, 0);
20960   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
20961                                DEBUG_RANGES_SECTION_LABEL, 0);
20962   ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
20963                                DEBUG_MACINFO_SECTION_LABEL, 0);
20964
20965   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
20966     macinfo_table = VEC_alloc (macinfo_entry, gc, 64);
20967
20968   switch_to_section (text_section);
20969   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
20970
20971   /* Make sure the line number table for .text always exists.  */
20972   text_section_line_info = new_line_info_table ();
20973   text_section_line_info->end_label = text_end_label;
20974 }
20975
20976 /* Called before cgraph_optimize starts outputtting functions, variables
20977    and toplevel asms into assembly.  */
20978
20979 static void
20980 dwarf2out_assembly_start (void)
20981 {
20982   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE
20983       && dwarf2out_do_cfi_asm ()
20984       && (!(flag_unwind_tables || flag_exceptions)
20985           || targetm_common.except_unwind_info (&global_options) != UI_DWARF2))
20986     fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
20987 }
20988
20989 /* A helper function for dwarf2out_finish called through
20990    htab_traverse.  Emit one queued .debug_str string.  */
20991
20992 static int
20993 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
20994 {
20995   struct indirect_string_node *node = (struct indirect_string_node *) *h;
20996
20997   if (node->form == DW_FORM_strp)
20998     {
20999       switch_to_section (debug_str_section);
21000       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21001       assemble_string (node->str, strlen (node->str) + 1);
21002     }
21003
21004   return 1;
21005 }
21006
21007 #if ENABLE_ASSERT_CHECKING
21008 /* Verify that all marks are clear.  */
21009
21010 static void
21011 verify_marks_clear (dw_die_ref die)
21012 {
21013   dw_die_ref c;
21014
21015   gcc_assert (! die->die_mark);
21016   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21017 }
21018 #endif /* ENABLE_ASSERT_CHECKING */
21019
21020 /* Clear the marks for a die and its children.
21021    Be cool if the mark isn't set.  */
21022
21023 static void
21024 prune_unmark_dies (dw_die_ref die)
21025 {
21026   dw_die_ref c;
21027
21028   if (die->die_mark)
21029     die->die_mark = 0;
21030   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21031 }
21032
21033 /* Given DIE that we're marking as used, find any other dies
21034    it references as attributes and mark them as used.  */
21035
21036 static void
21037 prune_unused_types_walk_attribs (dw_die_ref die)
21038 {
21039   dw_attr_ref a;
21040   unsigned ix;
21041
21042   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21043     {
21044       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21045         {
21046           /* A reference to another DIE.
21047              Make sure that it will get emitted.
21048              If it was broken out into a comdat group, don't follow it.  */
21049           if (! use_debug_types
21050               || a->dw_attr == DW_AT_specification
21051               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21052             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21053         }
21054       /* Set the string's refcount to 0 so that prune_unused_types_mark
21055          accounts properly for it.  */
21056       if (AT_class (a) == dw_val_class_str)
21057         a->dw_attr_val.v.val_str->refcount = 0;
21058     }
21059 }
21060
21061 /* Mark the generic parameters and arguments children DIEs of DIE.  */
21062
21063 static void
21064 prune_unused_types_mark_generic_parms_dies (dw_die_ref die)
21065 {
21066   dw_die_ref c;
21067
21068   if (die == NULL || die->die_child == NULL)
21069     return;
21070   c = die->die_child;
21071   do
21072     {
21073       switch (c->die_tag)
21074         {
21075         case DW_TAG_template_type_param:
21076         case DW_TAG_template_value_param:
21077         case DW_TAG_GNU_template_template_param:
21078         case DW_TAG_GNU_template_parameter_pack:
21079           prune_unused_types_mark (c, 1);
21080           break;
21081         default:
21082           break;
21083         }
21084       c = c->die_sib;
21085     } while (c && c != die->die_child);
21086 }
21087
21088 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21089    to DIE's children.  */
21090
21091 static void
21092 prune_unused_types_mark (dw_die_ref die, int dokids)
21093 {
21094   dw_die_ref c;
21095
21096   if (die->die_mark == 0)
21097     {
21098       /* We haven't done this node yet.  Mark it as used.  */
21099       die->die_mark = 1;
21100       /* If this is the DIE of a generic type instantiation,
21101          mark the children DIEs that describe its generic parms and
21102          args.  */
21103       prune_unused_types_mark_generic_parms_dies (die);
21104
21105       /* We also have to mark its parents as used.
21106          (But we don't want to mark our parents' kids due to this.)  */
21107       if (die->die_parent)
21108         prune_unused_types_mark (die->die_parent, 0);
21109
21110       /* Mark any referenced nodes.  */
21111       prune_unused_types_walk_attribs (die);
21112
21113       /* If this node is a specification,
21114          also mark the definition, if it exists.  */
21115       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21116         prune_unused_types_mark (die->die_definition, 1);
21117     }
21118
21119   if (dokids && die->die_mark != 2)
21120     {
21121       /* We need to walk the children, but haven't done so yet.
21122          Remember that we've walked the kids.  */
21123       die->die_mark = 2;
21124
21125       /* If this is an array type, we need to make sure our
21126          kids get marked, even if they're types.  If we're
21127          breaking out types into comdat sections, do this
21128          for all type definitions.  */
21129       if (die->die_tag == DW_TAG_array_type
21130           || (use_debug_types
21131               && is_type_die (die) && ! is_declaration_die (die)))
21132         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21133       else
21134         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21135     }
21136 }
21137
21138 /* For local classes, look if any static member functions were emitted
21139    and if so, mark them.  */
21140
21141 static void
21142 prune_unused_types_walk_local_classes (dw_die_ref die)
21143 {
21144   dw_die_ref c;
21145
21146   if (die->die_mark == 2)
21147     return;
21148
21149   switch (die->die_tag)
21150     {
21151     case DW_TAG_structure_type:
21152     case DW_TAG_union_type:
21153     case DW_TAG_class_type:
21154       break;
21155
21156     case DW_TAG_subprogram:
21157       if (!get_AT_flag (die, DW_AT_declaration)
21158           || die->die_definition != NULL)
21159         prune_unused_types_mark (die, 1);
21160       return;
21161
21162     default:
21163       return;
21164     }
21165
21166   /* Mark children.  */
21167   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21168 }
21169
21170 /* Walk the tree DIE and mark types that we actually use.  */
21171
21172 static void
21173 prune_unused_types_walk (dw_die_ref die)
21174 {
21175   dw_die_ref c;
21176
21177   /* Don't do anything if this node is already marked and
21178      children have been marked as well.  */
21179   if (die->die_mark == 2)
21180     return;
21181
21182   switch (die->die_tag)
21183     {
21184     case DW_TAG_structure_type:
21185     case DW_TAG_union_type:
21186     case DW_TAG_class_type:
21187       if (die->die_perennial_p)
21188         break;
21189
21190       for (c = die->die_parent; c; c = c->die_parent)
21191         if (c->die_tag == DW_TAG_subprogram)
21192           break;
21193
21194       /* Finding used static member functions inside of classes
21195          is needed just for local classes, because for other classes
21196          static member function DIEs with DW_AT_specification
21197          are emitted outside of the DW_TAG_*_type.  If we ever change
21198          it, we'd need to call this even for non-local classes.  */
21199       if (c)
21200         prune_unused_types_walk_local_classes (die);
21201
21202       /* It's a type node --- don't mark it.  */
21203       return;
21204
21205     case DW_TAG_const_type:
21206     case DW_TAG_packed_type:
21207     case DW_TAG_pointer_type:
21208     case DW_TAG_reference_type:
21209     case DW_TAG_rvalue_reference_type:
21210     case DW_TAG_volatile_type:
21211     case DW_TAG_typedef:
21212     case DW_TAG_array_type:
21213     case DW_TAG_interface_type:
21214     case DW_TAG_friend:
21215     case DW_TAG_variant_part:
21216     case DW_TAG_enumeration_type:
21217     case DW_TAG_subroutine_type:
21218     case DW_TAG_string_type:
21219     case DW_TAG_set_type:
21220     case DW_TAG_subrange_type:
21221     case DW_TAG_ptr_to_member_type:
21222     case DW_TAG_file_type:
21223       if (die->die_perennial_p)
21224         break;
21225
21226       /* It's a type node --- don't mark it.  */
21227       return;
21228
21229     default:
21230       /* Mark everything else.  */
21231       break;
21232   }
21233
21234   if (die->die_mark == 0)
21235     {
21236       die->die_mark = 1;
21237
21238       /* Now, mark any dies referenced from here.  */
21239       prune_unused_types_walk_attribs (die);
21240     }
21241
21242   die->die_mark = 2;
21243
21244   /* Mark children.  */
21245   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21246 }
21247
21248 /* Increment the string counts on strings referred to from DIE's
21249    attributes.  */
21250
21251 static void
21252 prune_unused_types_update_strings (dw_die_ref die)
21253 {
21254   dw_attr_ref a;
21255   unsigned ix;
21256
21257   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21258     if (AT_class (a) == dw_val_class_str)
21259       {
21260         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21261         s->refcount++;
21262         /* Avoid unnecessarily putting strings that are used less than
21263            twice in the hash table.  */
21264         if (s->refcount
21265             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21266           {
21267             void ** slot;
21268             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21269                                              htab_hash_string (s->str),
21270                                              INSERT);
21271             gcc_assert (*slot == NULL);
21272             *slot = s;
21273           }
21274       }
21275 }
21276
21277 /* Remove from the tree DIE any dies that aren't marked.  */
21278
21279 static void
21280 prune_unused_types_prune (dw_die_ref die)
21281 {
21282   dw_die_ref c;
21283
21284   gcc_assert (die->die_mark);
21285   prune_unused_types_update_strings (die);
21286
21287   if (! die->die_child)
21288     return;
21289
21290   c = die->die_child;
21291   do {
21292     dw_die_ref prev = c;
21293     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21294       if (c == die->die_child)
21295         {
21296           /* No marked children between 'prev' and the end of the list.  */
21297           if (prev == c)
21298             /* No marked children at all.  */
21299             die->die_child = NULL;
21300           else
21301             {
21302               prev->die_sib = c->die_sib;
21303               die->die_child = prev;
21304             }
21305           return;
21306         }
21307
21308     if (c != prev->die_sib)
21309       prev->die_sib = c;
21310     prune_unused_types_prune (c);
21311   } while (c != die->die_child);
21312 }
21313
21314 /* Remove dies representing declarations that we never use.  */
21315
21316 static void
21317 prune_unused_types (void)
21318 {
21319   unsigned int i;
21320   limbo_die_node *node;
21321   comdat_type_node *ctnode;
21322   pubname_ref pub;
21323   dw_die_ref base_type;
21324
21325 #if ENABLE_ASSERT_CHECKING
21326   /* All the marks should already be clear.  */
21327   verify_marks_clear (comp_unit_die ());
21328   for (node = limbo_die_list; node; node = node->next)
21329     verify_marks_clear (node->die);
21330   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21331     verify_marks_clear (ctnode->root_die);
21332 #endif /* ENABLE_ASSERT_CHECKING */
21333
21334   /* Mark types that are used in global variables.  */
21335   premark_types_used_by_global_vars ();
21336
21337   /* Set the mark on nodes that are actually used.  */
21338   prune_unused_types_walk (comp_unit_die ());
21339   for (node = limbo_die_list; node; node = node->next)
21340     prune_unused_types_walk (node->die);
21341   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21342     {
21343       prune_unused_types_walk (ctnode->root_die);
21344       prune_unused_types_mark (ctnode->type_die, 1);
21345     }
21346
21347   /* Also set the mark on nodes referenced from the
21348      pubname_table.  */
21349   FOR_EACH_VEC_ELT (pubname_entry, pubname_table, i, pub)
21350     prune_unused_types_mark (pub->die, 1);
21351   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21352     prune_unused_types_mark (base_type, 1);
21353
21354   if (debug_str_hash)
21355     htab_empty (debug_str_hash);
21356   prune_unused_types_prune (comp_unit_die ());
21357   for (node = limbo_die_list; node; node = node->next)
21358     prune_unused_types_prune (node->die);
21359   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21360     prune_unused_types_prune (ctnode->root_die);
21361
21362   /* Leave the marks clear.  */
21363   prune_unmark_dies (comp_unit_die ());
21364   for (node = limbo_die_list; node; node = node->next)
21365     prune_unmark_dies (node->die);
21366   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21367     prune_unmark_dies (ctnode->root_die);
21368 }
21369
21370 /* Set the parameter to true if there are any relative pathnames in
21371    the file table.  */
21372 static int
21373 file_table_relative_p (void ** slot, void *param)
21374 {
21375   bool *p = (bool *) param;
21376   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
21377   if (!IS_ABSOLUTE_PATH (d->filename))
21378     {
21379       *p = true;
21380       return 0;
21381     }
21382   return 1;
21383 }
21384
21385 /* Routines to manipulate hash table of comdat type units.  */
21386
21387 static hashval_t
21388 htab_ct_hash (const void *of)
21389 {
21390   hashval_t h;
21391   const comdat_type_node *const type_node = (const comdat_type_node *) of;
21392
21393   memcpy (&h, type_node->signature, sizeof (h));
21394   return h;
21395 }
21396
21397 static int
21398 htab_ct_eq (const void *of1, const void *of2)
21399 {
21400   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
21401   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
21402
21403   return (! memcmp (type_node_1->signature, type_node_2->signature,
21404                     DWARF_TYPE_SIGNATURE_SIZE));
21405 }
21406
21407 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
21408    to the location it would have been added, should we know its
21409    DECL_ASSEMBLER_NAME when we added other attributes.  This will
21410    probably improve compactness of debug info, removing equivalent
21411    abbrevs, and hide any differences caused by deferring the
21412    computation of the assembler name, triggered by e.g. PCH.  */
21413
21414 static inline void
21415 move_linkage_attr (dw_die_ref die)
21416 {
21417   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
21418   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
21419
21420   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
21421               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
21422
21423   while (--ix > 0)
21424     {
21425       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
21426
21427       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
21428         break;
21429     }
21430
21431   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
21432     {
21433       VEC_pop (dw_attr_node, die->die_attr);
21434       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
21435     }
21436 }
21437
21438 /* Helper function for resolve_addr, mark DW_TAG_base_type nodes
21439    referenced from typed stack ops and count how often they are used.  */
21440
21441 static void
21442 mark_base_types (dw_loc_descr_ref loc)
21443 {
21444   dw_die_ref base_type = NULL;
21445
21446   for (; loc; loc = loc->dw_loc_next)
21447     {
21448       switch (loc->dw_loc_opc)
21449         {
21450         case DW_OP_GNU_regval_type:
21451         case DW_OP_GNU_deref_type:
21452           base_type = loc->dw_loc_oprnd2.v.val_die_ref.die;
21453           break;
21454         case DW_OP_GNU_convert:
21455         case DW_OP_GNU_reinterpret:
21456           if (loc->dw_loc_oprnd1.val_class == dw_val_class_unsigned_const)
21457             continue;
21458           /* FALLTHRU */
21459         case DW_OP_GNU_const_type:
21460           base_type = loc->dw_loc_oprnd1.v.val_die_ref.die;
21461           break;
21462         case DW_OP_GNU_entry_value:
21463           mark_base_types (loc->dw_loc_oprnd1.v.val_loc);
21464           continue;
21465         default:
21466           continue;
21467         }
21468       gcc_assert (base_type->die_parent == comp_unit_die ());
21469       if (base_type->die_mark)
21470         base_type->die_mark++;
21471       else
21472         {
21473           VEC_safe_push (dw_die_ref, heap, base_types, base_type);
21474           base_type->die_mark = 1;
21475         }
21476     }
21477 }
21478
21479 /* Comparison function for sorting marked base types.  */
21480
21481 static int
21482 base_type_cmp (const void *x, const void *y)
21483 {
21484   dw_die_ref dx = *(const dw_die_ref *) x;
21485   dw_die_ref dy = *(const dw_die_ref *) y;
21486   unsigned int byte_size1, byte_size2;
21487   unsigned int encoding1, encoding2;
21488   if (dx->die_mark > dy->die_mark)
21489     return -1;
21490   if (dx->die_mark < dy->die_mark)
21491     return 1;
21492   byte_size1 = get_AT_unsigned (dx, DW_AT_byte_size);
21493   byte_size2 = get_AT_unsigned (dy, DW_AT_byte_size);
21494   if (byte_size1 < byte_size2)
21495     return 1;
21496   if (byte_size1 > byte_size2)
21497     return -1;
21498   encoding1 = get_AT_unsigned (dx, DW_AT_encoding);
21499   encoding2 = get_AT_unsigned (dy, DW_AT_encoding);
21500   if (encoding1 < encoding2)
21501     return 1;
21502   if (encoding1 > encoding2)
21503     return -1;
21504   return 0;
21505 }
21506
21507 /* Move base types marked by mark_base_types as early as possible
21508    in the CU, sorted by decreasing usage count both to make the
21509    uleb128 references as small as possible and to make sure they
21510    will have die_offset already computed by calc_die_sizes when
21511    sizes of typed stack loc ops is computed.  */
21512
21513 static void
21514 move_marked_base_types (void)
21515 {
21516   unsigned int i;
21517   dw_die_ref base_type, die, c;
21518
21519   if (VEC_empty (dw_die_ref, base_types))
21520     return;
21521
21522   /* Sort by decreasing usage count, they will be added again in that
21523      order later on.  */
21524   VEC_qsort (dw_die_ref, base_types, base_type_cmp);
21525   die = comp_unit_die ();
21526   c = die->die_child;
21527   do
21528     {
21529       dw_die_ref prev = c;
21530       c = c->die_sib;
21531       while (c->die_mark)
21532         {
21533           remove_child_with_prev (c, prev);
21534           /* As base types got marked, there must be at least
21535              one node other than DW_TAG_base_type.  */
21536           gcc_assert (c != c->die_sib);
21537           c = c->die_sib;
21538         }
21539     }
21540   while (c != die->die_child);
21541   gcc_assert (die->die_child);
21542   c = die->die_child;
21543   for (i = 0; VEC_iterate (dw_die_ref, base_types, i, base_type); i++)
21544     {
21545       base_type->die_mark = 0;
21546       base_type->die_sib = c->die_sib;
21547       c->die_sib = base_type;
21548       c = base_type;
21549     }
21550 }
21551
21552 /* Helper function for resolve_addr, attempt to resolve
21553    one CONST_STRING, return non-zero if not successful.  Similarly verify that
21554    SYMBOL_REFs refer to variables emitted in the current CU.  */
21555
21556 static int
21557 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
21558 {
21559   rtx rtl = *addr;
21560
21561   if (GET_CODE (rtl) == CONST_STRING)
21562     {
21563       size_t len = strlen (XSTR (rtl, 0)) + 1;
21564       tree t = build_string (len, XSTR (rtl, 0));
21565       tree tlen = size_int (len - 1);
21566       TREE_TYPE (t)
21567         = build_array_type (char_type_node, build_index_type (tlen));
21568       rtl = lookup_constant_def (t);
21569       if (!rtl || !MEM_P (rtl))
21570         return 1;
21571       rtl = XEXP (rtl, 0);
21572       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
21573       *addr = rtl;
21574       return 0;
21575     }
21576
21577   if (GET_CODE (rtl) == SYMBOL_REF
21578       && SYMBOL_REF_DECL (rtl))
21579     {
21580       if (TREE_CONSTANT_POOL_ADDRESS_P (rtl))
21581         {
21582           if (!TREE_ASM_WRITTEN (DECL_INITIAL (SYMBOL_REF_DECL (rtl))))
21583             return 1;
21584         }
21585       else if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
21586         return 1;
21587     }
21588
21589   if (GET_CODE (rtl) == CONST
21590       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
21591     return 1;
21592
21593   return 0;
21594 }
21595
21596 /* Helper function for resolve_addr, handle one location
21597    expression, return false if at least one CONST_STRING or SYMBOL_REF in
21598    the location list couldn't be resolved.  */
21599
21600 static bool
21601 resolve_addr_in_expr (dw_loc_descr_ref loc)
21602 {
21603   dw_loc_descr_ref keep = NULL;
21604   for (; loc; loc = loc->dw_loc_next)
21605     switch (loc->dw_loc_opc)
21606       {
21607       case DW_OP_addr:
21608         if (resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21609           return false;
21610         break;
21611       case DW_OP_const4u:
21612       case DW_OP_const8u:
21613         if (loc->dtprel
21614             && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
21615           return false;
21616         break;
21617       case DW_OP_implicit_value:
21618         if (loc->dw_loc_oprnd2.val_class == dw_val_class_addr
21619             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL))
21620           return false;
21621         break;
21622       case DW_OP_GNU_implicit_pointer:
21623       case DW_OP_GNU_parameter_ref:
21624         if (loc->dw_loc_oprnd1.val_class == dw_val_class_decl_ref)
21625           {
21626             dw_die_ref ref
21627               = lookup_decl_die (loc->dw_loc_oprnd1.v.val_decl_ref);
21628             if (ref == NULL)
21629               return false;
21630             loc->dw_loc_oprnd1.val_class = dw_val_class_die_ref;
21631             loc->dw_loc_oprnd1.v.val_die_ref.die = ref;
21632             loc->dw_loc_oprnd1.v.val_die_ref.external = 0;
21633           }
21634         break;
21635       case DW_OP_GNU_const_type:
21636       case DW_OP_GNU_regval_type:
21637       case DW_OP_GNU_deref_type:
21638       case DW_OP_GNU_convert:
21639       case DW_OP_GNU_reinterpret:
21640         while (loc->dw_loc_next
21641                && loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_convert)
21642           {
21643             dw_die_ref base1, base2;
21644             unsigned enc1, enc2, size1, size2;
21645             if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21646                 || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21647               base1 = loc->dw_loc_oprnd2.v.val_die_ref.die;
21648             else if (loc->dw_loc_oprnd1.val_class
21649                      == dw_val_class_unsigned_const)
21650               break;
21651             else
21652               base1 = loc->dw_loc_oprnd1.v.val_die_ref.die;
21653             if (loc->dw_loc_next->dw_loc_oprnd1.val_class
21654                 == dw_val_class_unsigned_const)
21655               break;
21656             base2 = loc->dw_loc_next->dw_loc_oprnd1.v.val_die_ref.die;
21657             gcc_assert (base1->die_tag == DW_TAG_base_type
21658                         && base2->die_tag == DW_TAG_base_type);
21659             enc1 = get_AT_unsigned (base1, DW_AT_encoding);
21660             enc2 = get_AT_unsigned (base2, DW_AT_encoding);
21661             size1 = get_AT_unsigned (base1, DW_AT_byte_size);
21662             size2 = get_AT_unsigned (base2, DW_AT_byte_size);
21663             if (size1 == size2
21664                 && (((enc1 == DW_ATE_unsigned || enc1 == DW_ATE_signed)
21665                      && (enc2 == DW_ATE_unsigned || enc2 == DW_ATE_signed)
21666                      && loc != keep)
21667                     || enc1 == enc2))
21668               {
21669                 /* Optimize away next DW_OP_GNU_convert after
21670                    adjusting LOC's base type die reference.  */
21671                 if (loc->dw_loc_opc == DW_OP_GNU_regval_type
21672                     || loc->dw_loc_opc == DW_OP_GNU_deref_type)
21673                   loc->dw_loc_oprnd2.v.val_die_ref.die = base2;
21674                 else
21675                   loc->dw_loc_oprnd1.v.val_die_ref.die = base2;
21676                 loc->dw_loc_next = loc->dw_loc_next->dw_loc_next;
21677                 continue;
21678               }
21679             /* Don't change integer DW_OP_GNU_convert after e.g. floating
21680                point typed stack entry.  */
21681             else if (enc1 != DW_ATE_unsigned && enc1 != DW_ATE_signed)
21682               keep = loc->dw_loc_next;
21683             break;
21684           }
21685         break;
21686       default:
21687         break;
21688       }
21689   return true;
21690 }
21691
21692 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
21693    an address in .rodata section if the string literal is emitted there,
21694    or remove the containing location list or replace DW_AT_const_value
21695    with DW_AT_location and empty location expression, if it isn't found
21696    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
21697    to something that has been emitted in the current CU.  */
21698
21699 static void
21700 resolve_addr (dw_die_ref die)
21701 {
21702   dw_die_ref c;
21703   dw_attr_ref a;
21704   dw_loc_list_ref *curr, *start, loc;
21705   unsigned ix;
21706
21707   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
21708     switch (AT_class (a))
21709       {
21710       case dw_val_class_loc_list:
21711         start = curr = AT_loc_list_ptr (a);
21712         loc = *curr;
21713         gcc_assert (loc);
21714         /* The same list can be referenced more than once.  See if we have
21715            already recorded the result from a previous pass.  */
21716         if (loc->replaced)
21717           *curr = loc->dw_loc_next;
21718         else if (!loc->resolved_addr)
21719           {
21720             /* As things stand, we do not expect or allow one die to
21721                reference a suffix of another die's location list chain.
21722                References must be identical or completely separate.
21723                There is therefore no need to cache the result of this
21724                pass on any list other than the first; doing so
21725                would lead to unnecessary writes.  */
21726             while (*curr)
21727               {
21728                 gcc_assert (!(*curr)->replaced && !(*curr)->resolved_addr);
21729                 if (!resolve_addr_in_expr ((*curr)->expr))
21730                   {
21731                     dw_loc_list_ref next = (*curr)->dw_loc_next;
21732                     if (next && (*curr)->ll_symbol)
21733                       {
21734                         gcc_assert (!next->ll_symbol);
21735                         next->ll_symbol = (*curr)->ll_symbol;
21736                       }
21737                     *curr = next;
21738                   }
21739                 else
21740                   {
21741                     mark_base_types ((*curr)->expr);
21742                     curr = &(*curr)->dw_loc_next;
21743                   }
21744               }
21745             if (loc == *start)
21746               loc->resolved_addr = 1;
21747             else
21748               {
21749                 loc->replaced = 1;
21750                 loc->dw_loc_next = *start;
21751               }
21752           }
21753         if (!*start)
21754           {
21755             remove_AT (die, a->dw_attr);
21756             ix--;
21757           }
21758         break;
21759       case dw_val_class_loc:
21760         if (!resolve_addr_in_expr (AT_loc (a)))
21761           {
21762             remove_AT (die, a->dw_attr);
21763             ix--;
21764           }
21765         else
21766           mark_base_types (AT_loc (a));
21767         break;
21768       case dw_val_class_addr:
21769         if (a->dw_attr == DW_AT_const_value
21770             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
21771           {
21772             remove_AT (die, a->dw_attr);
21773             ix--;
21774           }
21775         if (die->die_tag == DW_TAG_GNU_call_site
21776             && a->dw_attr == DW_AT_abstract_origin)
21777           {
21778             tree tdecl = SYMBOL_REF_DECL (a->dw_attr_val.v.val_addr);
21779             dw_die_ref tdie = lookup_decl_die (tdecl);
21780             if (tdie == NULL
21781                 && DECL_EXTERNAL (tdecl)
21782                 && DECL_ABSTRACT_ORIGIN (tdecl) == NULL_TREE)
21783               {
21784                 force_decl_die (tdecl);
21785                 tdie = lookup_decl_die (tdecl);
21786               }
21787             if (tdie)
21788               {
21789                 a->dw_attr_val.val_class = dw_val_class_die_ref;
21790                 a->dw_attr_val.v.val_die_ref.die = tdie;
21791                 a->dw_attr_val.v.val_die_ref.external = 0;
21792               }
21793             else
21794               {
21795                 remove_AT (die, a->dw_attr);
21796                 ix--;
21797               }
21798           }
21799         break;
21800       default:
21801         break;
21802       }
21803
21804   FOR_EACH_CHILD (die, c, resolve_addr (c));
21805 }
21806 \f
21807 /* Helper routines for optimize_location_lists.
21808    This pass tries to share identical local lists in .debug_loc
21809    section.  */
21810
21811 /* Iteratively hash operands of LOC opcode.  */
21812
21813 static inline hashval_t
21814 hash_loc_operands (dw_loc_descr_ref loc, hashval_t hash)
21815 {
21816   dw_val_ref val1 = &loc->dw_loc_oprnd1;
21817   dw_val_ref val2 = &loc->dw_loc_oprnd2;
21818
21819   switch (loc->dw_loc_opc)
21820     {
21821     case DW_OP_const4u:
21822     case DW_OP_const8u:
21823       if (loc->dtprel)
21824         goto hash_addr;
21825       /* FALLTHRU */
21826     case DW_OP_const1u:
21827     case DW_OP_const1s:
21828     case DW_OP_const2u:
21829     case DW_OP_const2s:
21830     case DW_OP_const4s:
21831     case DW_OP_const8s:
21832     case DW_OP_constu:
21833     case DW_OP_consts:
21834     case DW_OP_pick:
21835     case DW_OP_plus_uconst:
21836     case DW_OP_breg0:
21837     case DW_OP_breg1:
21838     case DW_OP_breg2:
21839     case DW_OP_breg3:
21840     case DW_OP_breg4:
21841     case DW_OP_breg5:
21842     case DW_OP_breg6:
21843     case DW_OP_breg7:
21844     case DW_OP_breg8:
21845     case DW_OP_breg9:
21846     case DW_OP_breg10:
21847     case DW_OP_breg11:
21848     case DW_OP_breg12:
21849     case DW_OP_breg13:
21850     case DW_OP_breg14:
21851     case DW_OP_breg15:
21852     case DW_OP_breg16:
21853     case DW_OP_breg17:
21854     case DW_OP_breg18:
21855     case DW_OP_breg19:
21856     case DW_OP_breg20:
21857     case DW_OP_breg21:
21858     case DW_OP_breg22:
21859     case DW_OP_breg23:
21860     case DW_OP_breg24:
21861     case DW_OP_breg25:
21862     case DW_OP_breg26:
21863     case DW_OP_breg27:
21864     case DW_OP_breg28:
21865     case DW_OP_breg29:
21866     case DW_OP_breg30:
21867     case DW_OP_breg31:
21868     case DW_OP_regx:
21869     case DW_OP_fbreg:
21870     case DW_OP_piece:
21871     case DW_OP_deref_size:
21872     case DW_OP_xderef_size:
21873       hash = iterative_hash_object (val1->v.val_int, hash);
21874       break;
21875     case DW_OP_skip:
21876     case DW_OP_bra:
21877       {
21878         int offset;
21879
21880         gcc_assert (val1->val_class == dw_val_class_loc);
21881         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
21882         hash = iterative_hash_object (offset, hash);
21883       }
21884       break;
21885     case DW_OP_implicit_value:
21886       hash = iterative_hash_object (val1->v.val_unsigned, hash);
21887       switch (val2->val_class)
21888         {
21889         case dw_val_class_const:
21890           hash = iterative_hash_object (val2->v.val_int, hash);
21891           break;
21892         case dw_val_class_vec:
21893           {
21894             unsigned int elt_size = val2->v.val_vec.elt_size;
21895             unsigned int len = val2->v.val_vec.length;
21896
21897             hash = iterative_hash_object (elt_size, hash);
21898             hash = iterative_hash_object (len, hash);
21899             hash = iterative_hash (val2->v.val_vec.array,
21900                                    len * elt_size, hash);
21901           }
21902           break;
21903         case dw_val_class_const_double:
21904           hash = iterative_hash_object (val2->v.val_double.low, hash);
21905           hash = iterative_hash_object (val2->v.val_double.high, hash);
21906           break;
21907         case dw_val_class_addr:
21908           hash = iterative_hash_rtx (val2->v.val_addr, hash);
21909           break;
21910         default:
21911           gcc_unreachable ();
21912         }
21913       break;
21914     case DW_OP_bregx:
21915     case DW_OP_bit_piece:
21916       hash = iterative_hash_object (val1->v.val_int, hash);
21917       hash = iterative_hash_object (val2->v.val_int, hash);
21918       break;
21919     case DW_OP_addr:
21920     hash_addr:
21921       if (loc->dtprel)
21922         {
21923           unsigned char dtprel = 0xd1;
21924           hash = iterative_hash_object (dtprel, hash);
21925         }
21926       hash = iterative_hash_rtx (val1->v.val_addr, hash);
21927       break;
21928     case DW_OP_GNU_implicit_pointer:
21929       hash = iterative_hash_object (val2->v.val_int, hash);
21930       break;
21931     case DW_OP_GNU_entry_value:
21932       hash = hash_loc_operands (val1->v.val_loc, hash);
21933       break;
21934     case DW_OP_GNU_regval_type:
21935     case DW_OP_GNU_deref_type:
21936       {
21937         unsigned int byte_size
21938           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_byte_size);
21939         unsigned int encoding
21940           = get_AT_unsigned (val2->v.val_die_ref.die, DW_AT_encoding);
21941         hash = iterative_hash_object (val1->v.val_int, hash);
21942         hash = iterative_hash_object (byte_size, hash);
21943         hash = iterative_hash_object (encoding, hash);
21944       }
21945       break;
21946     case DW_OP_GNU_convert:
21947     case DW_OP_GNU_reinterpret:
21948       if (val1->val_class == dw_val_class_unsigned_const)
21949         {
21950           hash = iterative_hash_object (val1->v.val_unsigned, hash);
21951           break;
21952         }
21953       /* FALLTHRU */
21954     case DW_OP_GNU_const_type:
21955       {
21956         unsigned int byte_size
21957           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_byte_size);
21958         unsigned int encoding
21959           = get_AT_unsigned (val1->v.val_die_ref.die, DW_AT_encoding);
21960         hash = iterative_hash_object (byte_size, hash);
21961         hash = iterative_hash_object (encoding, hash);
21962         if (loc->dw_loc_opc != DW_OP_GNU_const_type)
21963           break;
21964         hash = iterative_hash_object (val2->val_class, hash);
21965         switch (val2->val_class)
21966           {
21967           case dw_val_class_const:
21968             hash = iterative_hash_object (val2->v.val_int, hash);
21969             break;
21970           case dw_val_class_vec:
21971             {
21972               unsigned int elt_size = val2->v.val_vec.elt_size;
21973               unsigned int len = val2->v.val_vec.length;
21974
21975               hash = iterative_hash_object (elt_size, hash);
21976               hash = iterative_hash_object (len, hash);
21977               hash = iterative_hash (val2->v.val_vec.array,
21978                                      len * elt_size, hash);
21979             }
21980             break;
21981           case dw_val_class_const_double:
21982             hash = iterative_hash_object (val2->v.val_double.low, hash);
21983             hash = iterative_hash_object (val2->v.val_double.high, hash);
21984             break;
21985           default:
21986             gcc_unreachable ();
21987           }
21988       }
21989       break;
21990
21991     default:
21992       /* Other codes have no operands.  */
21993       break;
21994     }
21995   return hash;
21996 }
21997
21998 /* Iteratively hash the whole DWARF location expression LOC.  */
21999
22000 static inline hashval_t
22001 hash_locs (dw_loc_descr_ref loc, hashval_t hash)
22002 {
22003   dw_loc_descr_ref l;
22004   bool sizes_computed = false;
22005   /* Compute sizes, so that DW_OP_skip/DW_OP_bra can be checksummed.  */
22006   size_of_locs (loc);
22007
22008   for (l = loc; l != NULL; l = l->dw_loc_next)
22009     {
22010       enum dwarf_location_atom opc = l->dw_loc_opc;
22011       hash = iterative_hash_object (opc, hash);
22012       if ((opc == DW_OP_skip || opc == DW_OP_bra) && !sizes_computed)
22013         {
22014           size_of_locs (loc);
22015           sizes_computed = true;
22016         }
22017       hash = hash_loc_operands (l, hash);
22018     }
22019   return hash;
22020 }
22021
22022 /* Compute hash of the whole location list LIST_HEAD.  */
22023
22024 static inline void
22025 hash_loc_list (dw_loc_list_ref list_head)
22026 {
22027   dw_loc_list_ref curr = list_head;
22028   hashval_t hash = 0;
22029
22030   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
22031     {
22032       hash = iterative_hash (curr->begin, strlen (curr->begin) + 1, hash);
22033       hash = iterative_hash (curr->end, strlen (curr->end) + 1, hash);
22034       if (curr->section)
22035         hash = iterative_hash (curr->section, strlen (curr->section) + 1,
22036                                hash);
22037       hash = hash_locs (curr->expr, hash);
22038     }
22039   list_head->hash = hash;
22040 }
22041
22042 /* Return true if X and Y opcodes have the same operands.  */
22043
22044 static inline bool
22045 compare_loc_operands (dw_loc_descr_ref x, dw_loc_descr_ref y)
22046 {
22047   dw_val_ref valx1 = &x->dw_loc_oprnd1;
22048   dw_val_ref valx2 = &x->dw_loc_oprnd2;
22049   dw_val_ref valy1 = &y->dw_loc_oprnd1;
22050   dw_val_ref valy2 = &y->dw_loc_oprnd2;
22051
22052   switch (x->dw_loc_opc)
22053     {
22054     case DW_OP_const4u:
22055     case DW_OP_const8u:
22056       if (x->dtprel)
22057         goto hash_addr;
22058       /* FALLTHRU */
22059     case DW_OP_const1u:
22060     case DW_OP_const1s:
22061     case DW_OP_const2u:
22062     case DW_OP_const2s:
22063     case DW_OP_const4s:
22064     case DW_OP_const8s:
22065     case DW_OP_constu:
22066     case DW_OP_consts:
22067     case DW_OP_pick:
22068     case DW_OP_plus_uconst:
22069     case DW_OP_breg0:
22070     case DW_OP_breg1:
22071     case DW_OP_breg2:
22072     case DW_OP_breg3:
22073     case DW_OP_breg4:
22074     case DW_OP_breg5:
22075     case DW_OP_breg6:
22076     case DW_OP_breg7:
22077     case DW_OP_breg8:
22078     case DW_OP_breg9:
22079     case DW_OP_breg10:
22080     case DW_OP_breg11:
22081     case DW_OP_breg12:
22082     case DW_OP_breg13:
22083     case DW_OP_breg14:
22084     case DW_OP_breg15:
22085     case DW_OP_breg16:
22086     case DW_OP_breg17:
22087     case DW_OP_breg18:
22088     case DW_OP_breg19:
22089     case DW_OP_breg20:
22090     case DW_OP_breg21:
22091     case DW_OP_breg22:
22092     case DW_OP_breg23:
22093     case DW_OP_breg24:
22094     case DW_OP_breg25:
22095     case DW_OP_breg26:
22096     case DW_OP_breg27:
22097     case DW_OP_breg28:
22098     case DW_OP_breg29:
22099     case DW_OP_breg30:
22100     case DW_OP_breg31:
22101     case DW_OP_regx:
22102     case DW_OP_fbreg:
22103     case DW_OP_piece:
22104     case DW_OP_deref_size:
22105     case DW_OP_xderef_size:
22106       return valx1->v.val_int == valy1->v.val_int;
22107     case DW_OP_skip:
22108     case DW_OP_bra:
22109       gcc_assert (valx1->val_class == dw_val_class_loc
22110                   && valy1->val_class == dw_val_class_loc
22111                   && x->dw_loc_addr == y->dw_loc_addr);
22112       return valx1->v.val_loc->dw_loc_addr == valy1->v.val_loc->dw_loc_addr;
22113     case DW_OP_implicit_value:
22114       if (valx1->v.val_unsigned != valy1->v.val_unsigned
22115           || valx2->val_class != valy2->val_class)
22116         return false;
22117       switch (valx2->val_class)
22118         {
22119         case dw_val_class_const:
22120           return valx2->v.val_int == valy2->v.val_int;
22121         case dw_val_class_vec:
22122           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22123                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22124                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22125                             valx2->v.val_vec.elt_size
22126                             * valx2->v.val_vec.length) == 0;
22127         case dw_val_class_const_double:
22128           return valx2->v.val_double.low == valy2->v.val_double.low
22129                  && valx2->v.val_double.high == valy2->v.val_double.high;
22130         case dw_val_class_addr:
22131           return rtx_equal_p (valx2->v.val_addr, valy2->v.val_addr);
22132         default:
22133           gcc_unreachable ();
22134         }
22135     case DW_OP_bregx:
22136     case DW_OP_bit_piece:
22137       return valx1->v.val_int == valy1->v.val_int
22138              && valx2->v.val_int == valy2->v.val_int;
22139     case DW_OP_addr:
22140     hash_addr:
22141       return rtx_equal_p (valx1->v.val_addr, valy1->v.val_addr);
22142     case DW_OP_GNU_implicit_pointer:
22143       return valx1->val_class == dw_val_class_die_ref
22144              && valx1->val_class == valy1->val_class
22145              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die
22146              && valx2->v.val_int == valy2->v.val_int;
22147     case DW_OP_GNU_entry_value:
22148       return compare_loc_operands (valx1->v.val_loc, valy1->v.val_loc);
22149     case DW_OP_GNU_const_type:
22150       if (valx1->v.val_die_ref.die != valy1->v.val_die_ref.die
22151           || valx2->val_class != valy2->val_class)
22152         return false;
22153       switch (valx2->val_class)
22154         {
22155         case dw_val_class_const:
22156           return valx2->v.val_int == valy2->v.val_int;
22157         case dw_val_class_vec:
22158           return valx2->v.val_vec.elt_size == valy2->v.val_vec.elt_size
22159                  && valx2->v.val_vec.length == valy2->v.val_vec.length
22160                  && memcmp (valx2->v.val_vec.array, valy2->v.val_vec.array,
22161                             valx2->v.val_vec.elt_size
22162                             * valx2->v.val_vec.length) == 0;
22163         case dw_val_class_const_double:
22164           return valx2->v.val_double.low == valy2->v.val_double.low
22165                  && valx2->v.val_double.high == valy2->v.val_double.high;
22166         default:
22167           gcc_unreachable ();
22168         }
22169     case DW_OP_GNU_regval_type:
22170     case DW_OP_GNU_deref_type:
22171       return valx1->v.val_int == valy1->v.val_int
22172              && valx2->v.val_die_ref.die == valy2->v.val_die_ref.die;
22173     case DW_OP_GNU_convert:
22174     case DW_OP_GNU_reinterpret:
22175       if (valx1->val_class != valy1->val_class)
22176         return false;
22177       if (valx1->val_class == dw_val_class_unsigned_const)
22178         return valx1->v.val_unsigned == valy1->v.val_unsigned;
22179       return valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22180     case DW_OP_GNU_parameter_ref:
22181       return valx1->val_class == dw_val_class_die_ref
22182              && valx1->val_class == valy1->val_class
22183              && valx1->v.val_die_ref.die == valy1->v.val_die_ref.die;
22184     default:
22185       /* Other codes have no operands.  */
22186       return true;
22187     }
22188 }
22189
22190 /* Return true if DWARF location expressions X and Y are the same.  */
22191
22192 static inline bool
22193 compare_locs (dw_loc_descr_ref x, dw_loc_descr_ref y)
22194 {
22195   for (; x != NULL && y != NULL; x = x->dw_loc_next, y = y->dw_loc_next)
22196     if (x->dw_loc_opc != y->dw_loc_opc
22197         || x->dtprel != y->dtprel
22198         || !compare_loc_operands (x, y))
22199       break;
22200   return x == NULL && y == NULL;
22201 }
22202
22203 /* Return precomputed hash of location list X.  */
22204
22205 static hashval_t
22206 loc_list_hash (const void *x)
22207 {
22208   return ((const struct dw_loc_list_struct *) x)->hash;
22209 }
22210
22211 /* Return 1 if location lists X and Y are the same.  */
22212
22213 static int
22214 loc_list_eq (const void *x, const void *y)
22215 {
22216   const struct dw_loc_list_struct *a = (const struct dw_loc_list_struct *) x;
22217   const struct dw_loc_list_struct *b = (const struct dw_loc_list_struct *) y;
22218   if (a == b)
22219     return 1;
22220   if (a->hash != b->hash)
22221     return 0;
22222   for (; a != NULL && b != NULL; a = a->dw_loc_next, b = b->dw_loc_next)
22223     if (strcmp (a->begin, b->begin) != 0
22224         || strcmp (a->end, b->end) != 0
22225         || (a->section == NULL) != (b->section == NULL)
22226         || (a->section && strcmp (a->section, b->section) != 0)
22227         || !compare_locs (a->expr, b->expr))
22228       break;
22229   return a == NULL && b == NULL;
22230 }
22231
22232 /* Recursively optimize location lists referenced from DIE
22233    children and share them whenever possible.  */
22234
22235 static void
22236 optimize_location_lists_1 (dw_die_ref die, htab_t htab)
22237 {
22238   dw_die_ref c;
22239   dw_attr_ref a;
22240   unsigned ix;
22241   void **slot;
22242
22243   FOR_EACH_VEC_ELT (dw_attr_node, die->die_attr, ix, a)
22244     if (AT_class (a) == dw_val_class_loc_list)
22245       {
22246         dw_loc_list_ref list = AT_loc_list (a);
22247         /* TODO: perform some optimizations here, before hashing
22248            it and storing into the hash table.  */
22249         hash_loc_list (list);
22250         slot = htab_find_slot_with_hash (htab, list, list->hash,
22251                                          INSERT);
22252         if (*slot == NULL)
22253           *slot = (void *) list;
22254         else
22255           a->dw_attr_val.v.val_loc_list = (dw_loc_list_ref) *slot;
22256       }
22257
22258   FOR_EACH_CHILD (die, c, optimize_location_lists_1 (c, htab));
22259 }
22260
22261 /* Optimize location lists referenced from DIE
22262    children and share them whenever possible.  */
22263
22264 static void
22265 optimize_location_lists (dw_die_ref die)
22266 {
22267   htab_t htab = htab_create (500, loc_list_hash, loc_list_eq, NULL);
22268   optimize_location_lists_1 (die, htab);
22269   htab_delete (htab);
22270 }
22271 \f
22272 /* Output stuff that dwarf requires at the end of every file,
22273    and generate the DWARF-2 debugging info.  */
22274
22275 static void
22276 dwarf2out_finish (const char *filename)
22277 {
22278   limbo_die_node *node, *next_node;
22279   comdat_type_node *ctnode;
22280   htab_t comdat_type_table;
22281   unsigned int i;
22282
22283   gen_scheduled_generic_parms_dies ();
22284   gen_remaining_tmpl_value_param_die_attribute ();
22285
22286   /* Add the name for the main input file now.  We delayed this from
22287      dwarf2out_init to avoid complications with PCH.  */
22288   add_name_attribute (comp_unit_die (), remap_debug_filename (filename));
22289   if (!IS_ABSOLUTE_PATH (filename))
22290     add_comp_dir_attribute (comp_unit_die ());
22291   else if (get_AT (comp_unit_die (), DW_AT_comp_dir) == NULL)
22292     {
22293       bool p = false;
22294       htab_traverse (file_table, file_table_relative_p, &p);
22295       if (p)
22296         add_comp_dir_attribute (comp_unit_die ());
22297     }
22298
22299   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22300     {
22301       add_location_or_const_value_attribute (
22302         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22303         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22304         false,
22305         DW_AT_location);
22306     }
22307
22308   /* Traverse the limbo die list, and add parent/child links.  The only
22309      dies without parents that should be here are concrete instances of
22310      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22311      For concrete instances, we can get the parent die from the abstract
22312      instance.  */
22313   for (node = limbo_die_list; node; node = next_node)
22314     {
22315       dw_die_ref die = node->die;
22316       next_node = node->next;
22317
22318       if (die->die_parent == NULL)
22319         {
22320           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22321
22322           if (origin && origin->die_parent)
22323             add_child_die (origin->die_parent, die);
22324           else if (is_cu_die (die))
22325             ;
22326           else if (seen_error ())
22327             /* It's OK to be confused by errors in the input.  */
22328             add_child_die (comp_unit_die (), die);
22329           else
22330             {
22331               /* In certain situations, the lexical block containing a
22332                  nested function can be optimized away, which results
22333                  in the nested function die being orphaned.  Likewise
22334                  with the return type of that nested function.  Force
22335                  this to be a child of the containing function.
22336
22337                  It may happen that even the containing function got fully
22338                  inlined and optimized out.  In that case we are lost and
22339                  assign the empty child.  This should not be big issue as
22340                  the function is likely unreachable too.  */
22341               tree context = NULL_TREE;
22342
22343               gcc_assert (node->created_for);
22344
22345               if (DECL_P (node->created_for))
22346                 context = DECL_CONTEXT (node->created_for);
22347               else if (TYPE_P (node->created_for))
22348                 context = TYPE_CONTEXT (node->created_for);
22349
22350               gcc_assert (context
22351                           && (TREE_CODE (context) == FUNCTION_DECL
22352                               || TREE_CODE (context) == NAMESPACE_DECL));
22353
22354               origin = lookup_decl_die (context);
22355               if (origin)
22356                 add_child_die (origin, die);
22357               else
22358                 add_child_die (comp_unit_die (), die);
22359             }
22360         }
22361     }
22362
22363   limbo_die_list = NULL;
22364
22365 #if ENABLE_ASSERT_CHECKING
22366   {
22367     dw_die_ref die = comp_unit_die (), c;
22368     FOR_EACH_CHILD (die, c, gcc_assert (! c->die_mark));
22369   }
22370 #endif
22371   resolve_addr (comp_unit_die ());
22372   move_marked_base_types ();
22373
22374   for (node = deferred_asm_name; node; node = node->next)
22375     {
22376       tree decl = node->created_for;
22377       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22378         {
22379           add_linkage_attr (node->die, decl);
22380           move_linkage_attr (node->die);
22381         }
22382     }
22383
22384   deferred_asm_name = NULL;
22385
22386   /* Walk through the list of incomplete types again, trying once more to
22387      emit full debugging info for them.  */
22388   retry_incomplete_types ();
22389
22390   if (flag_eliminate_unused_debug_types)
22391     prune_unused_types ();
22392
22393   /* Generate separate CUs for each of the include files we've seen.
22394      They will go into limbo_die_list.  */
22395   if (flag_eliminate_dwarf2_dups && ! use_debug_types)
22396     break_out_includes (comp_unit_die ());
22397
22398   /* Generate separate COMDAT sections for type DIEs. */
22399   if (use_debug_types)
22400     {
22401       break_out_comdat_types (comp_unit_die ());
22402
22403       /* Each new type_unit DIE was added to the limbo die list when created.
22404          Since these have all been added to comdat_type_list, clear the
22405          limbo die list.  */
22406       limbo_die_list = NULL;
22407
22408       /* For each new comdat type unit, copy declarations for incomplete
22409          types to make the new unit self-contained (i.e., no direct
22410          references to the main compile unit).  */
22411       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22412         copy_decls_for_unworthy_types (ctnode->root_die);
22413       copy_decls_for_unworthy_types (comp_unit_die ());
22414
22415       /* In the process of copying declarations from one unit to another,
22416          we may have left some declarations behind that are no longer
22417          referenced.  Prune them.  */
22418       prune_unused_types ();
22419     }
22420
22421   /* Traverse the DIE's and add add sibling attributes to those DIE's
22422      that have children.  */
22423   add_sibling_attributes (comp_unit_die ());
22424   for (node = limbo_die_list; node; node = node->next)
22425     add_sibling_attributes (node->die);
22426   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22427     add_sibling_attributes (ctnode->root_die);
22428
22429   /* Output a terminator label for the .text section.  */
22430   switch_to_section (text_section);
22431   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22432   if (cold_text_section)
22433     {
22434       switch_to_section (cold_text_section);
22435       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22436     }
22437
22438   /* We can only use the low/high_pc attributes if all of the code was
22439      in .text.  */
22440   if (!have_multiple_function_sections 
22441       || (dwarf_version < 3 && dwarf_strict))
22442     {
22443       /* Don't add if the CU has no associated code.  */
22444       if (text_section_used)
22445         {
22446           add_AT_lbl_id (comp_unit_die (), DW_AT_low_pc, text_section_label);
22447           add_AT_lbl_id (comp_unit_die (), DW_AT_high_pc, text_end_label);
22448         }
22449     }
22450   else
22451     {
22452       unsigned fde_idx = 0;
22453       bool range_list_added = false;
22454
22455       if (text_section_used)
22456         add_ranges_by_labels (comp_unit_die (), text_section_label,
22457                               text_end_label, &range_list_added);
22458       if (cold_text_section_used)
22459         add_ranges_by_labels (comp_unit_die (), cold_text_section_label,
22460                               cold_end_label, &range_list_added);
22461
22462       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22463         {
22464           dw_fde_ref fde = &fde_table[fde_idx];
22465
22466           if (!fde->in_std_section)
22467             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_begin,
22468                                   fde->dw_fde_end, &range_list_added);
22469           if (fde->dw_fde_second_begin && !fde->second_in_std_section)
22470             add_ranges_by_labels (comp_unit_die (), fde->dw_fde_second_begin,
22471                                   fde->dw_fde_second_end, &range_list_added);
22472         }
22473
22474       if (range_list_added)
22475         {
22476           /* We need to give .debug_loc and .debug_ranges an appropriate
22477              "base address".  Use zero so that these addresses become
22478              absolute.  Historically, we've emitted the unexpected
22479              DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22480              Emit both to give time for other tools to adapt.  */
22481           add_AT_addr (comp_unit_die (), DW_AT_low_pc, const0_rtx);
22482           if (! dwarf_strict && dwarf_version < 4)
22483             add_AT_addr (comp_unit_die (), DW_AT_entry_pc, const0_rtx);
22484
22485           add_ranges (NULL);
22486         }
22487     }
22488
22489   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22490     add_AT_lineptr (comp_unit_die (), DW_AT_stmt_list,
22491                     debug_line_section_label);
22492
22493   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22494     add_AT_macptr (comp_unit_die (), DW_AT_macro_info, macinfo_section_label);
22495
22496   if (have_location_lists)
22497     optimize_location_lists (comp_unit_die ());
22498
22499   /* Output all of the compilation units.  We put the main one last so that
22500      the offsets are available to output_pubnames.  */
22501   for (node = limbo_die_list; node; node = node->next)
22502     output_comp_unit (node->die, 0);
22503
22504   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22505   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22506     {
22507       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22508
22509       /* Don't output duplicate types.  */
22510       if (*slot != HTAB_EMPTY_ENTRY)
22511         continue;
22512
22513       /* Add a pointer to the line table for the main compilation unit
22514          so that the debugger can make sense of DW_AT_decl_file
22515          attributes.  */
22516       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22517         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22518                         debug_line_section_label);
22519
22520       output_comdat_type_unit (ctnode);
22521       *slot = ctnode;
22522     }
22523   htab_delete (comdat_type_table);
22524
22525   /* Output the main compilation unit if non-empty or if .debug_macinfo
22526      will be emitted.  */
22527   output_comp_unit (comp_unit_die (), debug_info_level >= DINFO_LEVEL_VERBOSE);
22528
22529   /* Output the abbreviation table.  */
22530   if (abbrev_die_table_in_use != 1)
22531     {
22532       switch_to_section (debug_abbrev_section);
22533       ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
22534       output_abbrev_section ();
22535     }
22536
22537   /* Output location list section if necessary.  */
22538   if (have_location_lists)
22539     {
22540       /* Output the location lists info.  */
22541       switch_to_section (debug_loc_section);
22542       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22543                                    DEBUG_LOC_SECTION_LABEL, 0);
22544       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22545       output_location_lists (comp_unit_die ());
22546     }
22547
22548   /* Output public names table if necessary.  */
22549   if (!VEC_empty (pubname_entry, pubname_table))
22550     {
22551       gcc_assert (info_section_emitted);
22552       switch_to_section (debug_pubnames_section);
22553       output_pubnames (pubname_table);
22554     }
22555
22556   /* Output public types table if necessary.  */
22557   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22558      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22559      simply won't look for the section.  */
22560   if (!VEC_empty (pubname_entry, pubtype_table))
22561     {
22562       bool empty = false;
22563       
22564       if (flag_eliminate_unused_debug_types)
22565         {
22566           /* The pubtypes table might be emptied by pruning unused items.  */
22567           unsigned i;
22568           pubname_ref p;
22569           empty = true;
22570           FOR_EACH_VEC_ELT (pubname_entry, pubtype_table, i, p)
22571             if (p->die->die_offset != 0)
22572               {
22573                 empty = false;
22574                 break;
22575               }
22576         }
22577       if (!empty)
22578         {
22579           gcc_assert (info_section_emitted);
22580           switch_to_section (debug_pubtypes_section);
22581           output_pubnames (pubtype_table);
22582         }
22583     }
22584
22585   /* Output the address range information if a CU (.debug_info section)
22586      was emitted.  We output an empty table even if we had no functions
22587      to put in it.  This because the consumer has no way to tell the
22588      difference between an empty table that we omitted and failure to
22589      generate a table that would have contained data.  */
22590   if (info_section_emitted)
22591     {
22592       unsigned long aranges_length = size_of_aranges ();
22593
22594       switch_to_section (debug_aranges_section);
22595       output_aranges (aranges_length);
22596     }
22597
22598   /* Output ranges section if necessary.  */
22599   if (ranges_table_in_use)
22600     {
22601       switch_to_section (debug_ranges_section);
22602       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22603       output_ranges ();
22604     }
22605
22606   /* Output the source line correspondence table.  We must do this
22607      even if there is no line information.  Otherwise, on an empty
22608      translation unit, we will generate a present, but empty,
22609      .debug_info section.  IRIX 6.5 `nm' will then complain when
22610      examining the file.  This is done late so that any filenames
22611      used by the debug_info section are marked as 'used'.  */
22612   switch_to_section (debug_line_section);
22613   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
22614   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22615     output_line_info ();
22616
22617   /* Have to end the macro section.  */
22618   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22619     {
22620       switch_to_section (debug_macinfo_section);
22621       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
22622       if (!VEC_empty (macinfo_entry, macinfo_table))
22623         output_macinfo ();
22624       dw2_asm_output_data (1, 0, "End compilation unit");
22625     }
22626
22627   /* If we emitted any DW_FORM_strp form attribute, output the string
22628      table too.  */
22629   if (debug_str_hash)
22630     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22631 }
22632
22633 #include "gt-dwarf2out.h"