OSDN Git Service

PR debug/45110
[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
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 "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92 #include "gimple.h"
93 #include "tree-pass.h"
94 #include "tree-flow.h"
95
96 #ifdef DWARF2_DEBUGGING_INFO
97 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
98
99 static rtx last_var_location_insn;
100 #endif
101
102 #ifdef VMS_DEBUGGING_INFO
103 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
104
105 /* Define this macro to be a nonzero value if the directory specifications
106     which are output in the debug info should end with a separator.  */
107 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
108 /* Define this macro to evaluate to a nonzero value if GCC should refrain
109    from generating indirect strings in DWARF2 debug information, for instance
110    if your target is stuck with an old version of GDB that is unable to
111    process them properly or uses VMS Debug.  */
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
113 #else
114 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
115 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
116 #endif
117
118 #ifndef DWARF2_FRAME_INFO
119 # ifdef DWARF2_DEBUGGING_INFO
120 #  define DWARF2_FRAME_INFO \
121   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
122 # else
123 #  define DWARF2_FRAME_INFO 0
124 # endif
125 #endif
126
127 /* Map register numbers held in the call frame info that gcc has
128    collected using DWARF_FRAME_REGNUM to those that should be output in
129    .debug_frame and .eh_frame.  */
130 #ifndef DWARF2_FRAME_REG_OUT
131 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
132 #endif
133
134 /* Save the result of dwarf2out_do_frame across PCH.  */
135 static GTY(()) bool saved_do_cfi_asm = 0;
136
137 /* Decide whether we want to emit frame unwind information for the current
138    translation unit.  */
139
140 int
141 dwarf2out_do_frame (void)
142 {
143   /* We want to emit correct CFA location expressions or lists, so we
144      have to return true if we're going to output debug info, even if
145      we're not going to output frame or unwind info.  */
146   return (write_symbols == DWARF2_DEBUG
147           || write_symbols == VMS_AND_DWARF2_DEBUG
148           || DWARF2_FRAME_INFO || saved_do_cfi_asm
149 #ifdef DWARF2_UNWIND_INFO
150           || (DWARF2_UNWIND_INFO
151               && (flag_unwind_tables
152                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
153 #endif
154           );
155 }
156
157 /* Decide whether to emit frame unwind via assembler directives.  */
158
159 int
160 dwarf2out_do_cfi_asm (void)
161 {
162   int enc;
163
164 #ifdef MIPS_DEBUGGING_INFO
165   return false;
166 #endif
167   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
168     return false;
169   if (saved_do_cfi_asm)
170     return true;
171   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
172     return false;
173
174   /* Make sure the personality encoding is one the assembler can support.
175      In particular, aligned addresses can't be handled.  */
176   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
177   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
178     return false;
179   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
180   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
181     return false;
182
183   if (!HAVE_GAS_CFI_SECTIONS_DIRECTIVE)
184     {
185 #ifdef TARGET_UNWIND_INFO
186       return false;
187 #else
188       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
189         return false;
190 #endif
191     }
192
193   saved_do_cfi_asm = true;
194   return true;
195 }
196
197 /* The size of the target's pointer type.  */
198 #ifndef PTR_SIZE
199 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
200 #endif
201
202 /* Array of RTXes referenced by the debugging information, which therefore
203    must be kept around forever.  */
204 static GTY(()) VEC(rtx,gc) *used_rtx_array;
205
206 /* A pointer to the base of a list of incomplete types which might be
207    completed at some later time.  incomplete_types_list needs to be a
208    VEC(tree,gc) because we want to tell the garbage collector about
209    it.  */
210 static GTY(()) VEC(tree,gc) *incomplete_types;
211
212 /* A pointer to the base of a table of references to declaration
213    scopes.  This table is a display which tracks the nesting
214    of declaration scopes at the current scope and containing
215    scopes.  This table is used to find the proper place to
216    define type declaration DIE's.  */
217 static GTY(()) VEC(tree,gc) *decl_scope_table;
218
219 /* Pointers to various DWARF2 sections.  */
220 static GTY(()) section *debug_info_section;
221 static GTY(()) section *debug_abbrev_section;
222 static GTY(()) section *debug_aranges_section;
223 static GTY(()) section *debug_macinfo_section;
224 static GTY(()) section *debug_line_section;
225 static GTY(()) section *debug_loc_section;
226 static GTY(()) section *debug_pubnames_section;
227 static GTY(()) section *debug_pubtypes_section;
228 static GTY(()) section *debug_dcall_section;
229 static GTY(()) section *debug_vcall_section;
230 static GTY(()) section *debug_str_section;
231 static GTY(()) section *debug_ranges_section;
232 static GTY(()) section *debug_frame_section;
233
234 /* Personality decl of current unit.  Used only when assembler does not support
235    personality CFI.  */
236 static GTY(()) rtx current_unit_personality;
237
238 /* How to start an assembler comment.  */
239 #ifndef ASM_COMMENT_START
240 #define ASM_COMMENT_START ";#"
241 #endif
242
243 typedef struct dw_cfi_struct *dw_cfi_ref;
244 typedef struct dw_fde_struct *dw_fde_ref;
245 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
246
247 /* Call frames are described using a sequence of Call Frame
248    Information instructions.  The register number, offset
249    and address fields are provided as possible operands;
250    their use is selected by the opcode field.  */
251
252 enum dw_cfi_oprnd_type {
253   dw_cfi_oprnd_unused,
254   dw_cfi_oprnd_reg_num,
255   dw_cfi_oprnd_offset,
256   dw_cfi_oprnd_addr,
257   dw_cfi_oprnd_loc
258 };
259
260 typedef union GTY(()) dw_cfi_oprnd_struct {
261   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
262   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
263   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
264   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
265 }
266 dw_cfi_oprnd;
267
268 typedef struct GTY(()) dw_cfi_struct {
269   dw_cfi_ref dw_cfi_next;
270   enum dwarf_call_frame_info dw_cfi_opc;
271   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
272     dw_cfi_oprnd1;
273   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
274     dw_cfi_oprnd2;
275 }
276 dw_cfi_node;
277
278 /* This is how we define the location of the CFA. We use to handle it
279    as REG + OFFSET all the time,  but now it can be more complex.
280    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
281    Instead of passing around REG and OFFSET, we pass a copy
282    of this structure.  */
283 typedef struct GTY(()) cfa_loc {
284   HOST_WIDE_INT offset;
285   HOST_WIDE_INT base_offset;
286   unsigned int reg;
287   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
288   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
289 } dw_cfa_location;
290
291 /* All call frame descriptions (FDE's) in the GCC generated DWARF
292    refer to a single Common Information Entry (CIE), defined at
293    the beginning of the .debug_frame section.  This use of a single
294    CIE obviates the need to keep track of multiple CIE's
295    in the DWARF generation routines below.  */
296
297 typedef struct GTY(()) dw_fde_struct {
298   tree decl;
299   const char *dw_fde_begin;
300   const char *dw_fde_current_label;
301   const char *dw_fde_end;
302   const char *dw_fde_vms_end_prologue;
303   const char *dw_fde_vms_begin_epilogue;
304   const char *dw_fde_hot_section_label;
305   const char *dw_fde_hot_section_end_label;
306   const char *dw_fde_unlikely_section_label;
307   const char *dw_fde_unlikely_section_end_label;
308   dw_cfi_ref dw_fde_cfi;
309   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
310   HOST_WIDE_INT stack_realignment;
311   unsigned funcdef_number;
312   /* Dynamic realign argument pointer register.  */
313   unsigned int drap_reg;
314   /* Virtual dynamic realign argument pointer register.  */
315   unsigned int vdrap_reg;
316   /* These 3 flags are copied from rtl_data in function.h.  */
317   unsigned all_throwers_are_sibcalls : 1;
318   unsigned uses_eh_lsda : 1;
319   unsigned nothrow : 1;
320   /* Whether we did stack realign in this call frame.  */
321   unsigned stack_realign : 1;
322   /* Whether dynamic realign argument pointer register has been saved.  */
323   unsigned drap_reg_saved: 1;
324   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
325   unsigned in_std_section : 1;
326   /* True iff dw_fde_unlikely_section_label is in text_section or
327      cold_text_section.  */
328   unsigned cold_in_std_section : 1;
329   /* True iff switched sections.  */
330   unsigned dw_fde_switched_sections : 1;
331   /* True iff switching from cold to hot section.  */
332   unsigned dw_fde_switched_cold_to_hot : 1;
333 }
334 dw_fde_node;
335
336 /* Maximum size (in bytes) of an artificially generated label.  */
337 #define MAX_ARTIFICIAL_LABEL_BYTES      30
338
339 /* The size of addresses as they appear in the Dwarf 2 data.
340    Some architectures use word addresses to refer to code locations,
341    but Dwarf 2 info always uses byte addresses.  On such machines,
342    Dwarf 2 addresses need to be larger than the architecture's
343    pointers.  */
344 #ifndef DWARF2_ADDR_SIZE
345 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
346 #endif
347
348 /* The size in bytes of a DWARF field indicating an offset or length
349    relative to a debug info section, specified to be 4 bytes in the
350    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
351    as PTR_SIZE.  */
352
353 #ifndef DWARF_OFFSET_SIZE
354 #define DWARF_OFFSET_SIZE 4
355 #endif
356
357 /* The size in bytes of a DWARF 4 type signature.  */
358
359 #ifndef DWARF_TYPE_SIGNATURE_SIZE
360 #define DWARF_TYPE_SIGNATURE_SIZE 8
361 #endif
362
363 /* According to the (draft) DWARF 3 specification, the initial length
364    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
365    bytes are 0xffffffff, followed by the length stored in the next 8
366    bytes.
367
368    However, the SGI/MIPS ABI uses an initial length which is equal to
369    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
370
371 #ifndef DWARF_INITIAL_LENGTH_SIZE
372 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
373 #endif
374
375 /* Round SIZE up to the nearest BOUNDARY.  */
376 #define DWARF_ROUND(SIZE,BOUNDARY) \
377   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
378
379 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
380 #ifndef DWARF_CIE_DATA_ALIGNMENT
381 #ifdef STACK_GROWS_DOWNWARD
382 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
383 #else
384 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
385 #endif
386 #endif
387
388 /* CIE identifier.  */
389 #if HOST_BITS_PER_WIDE_INT >= 64
390 #define DWARF_CIE_ID \
391   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
392 #else
393 #define DWARF_CIE_ID DW_CIE_ID
394 #endif
395
396 /* A pointer to the base of a table that contains frame description
397    information for each routine.  */
398 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
399
400 /* Number of elements currently allocated for fde_table.  */
401 static GTY(()) unsigned fde_table_allocated;
402
403 /* Number of elements in fde_table currently in use.  */
404 static GTY(()) unsigned fde_table_in_use;
405
406 /* Size (in elements) of increments by which we may expand the
407    fde_table.  */
408 #define FDE_TABLE_INCREMENT 256
409
410 /* Get the current fde_table entry we should use.  */
411
412 static inline dw_fde_ref
413 current_fde (void)
414 {
415   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
416 }
417
418 /* A list of call frame insns for the CIE.  */
419 static GTY(()) dw_cfi_ref cie_cfi_head;
420
421 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
422 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
423    attribute that accelerates the lookup of the FDE associated
424    with the subprogram.  This variable holds the table index of the FDE
425    associated with the current function (body) definition.  */
426 static unsigned current_funcdef_fde;
427 #endif
428
429 struct GTY(()) indirect_string_node {
430   const char *str;
431   unsigned int refcount;
432   enum dwarf_form form;
433   char *label;
434 };
435
436 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
437
438 /* True if the compilation unit has location entries that reference
439    debug strings.  */
440 static GTY(()) bool debug_str_hash_forced = false;
441
442 static GTY(()) int dw2_string_counter;
443 static GTY(()) unsigned long dwarf2out_cfi_label_num;
444
445 /* True if the compilation unit places functions in more than one section.  */
446 static GTY(()) bool have_multiple_function_sections = false;
447
448 /* Whether the default text and cold text sections have been used at all.  */
449
450 static GTY(()) bool text_section_used = false;
451 static GTY(()) bool cold_text_section_used = false;
452
453 /* The default cold text section.  */
454 static GTY(()) section *cold_text_section;
455
456 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
457
458 /* Forward declarations for functions defined in this file.  */
459
460 static char *stripattributes (const char *);
461 static const char *dwarf_cfi_name (unsigned);
462 static dw_cfi_ref new_cfi (void);
463 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
464 static void add_fde_cfi (const char *, dw_cfi_ref);
465 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
466 static void lookup_cfa (dw_cfa_location *);
467 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
468 #ifdef DWARF2_UNWIND_INFO
469 static void initial_return_save (rtx);
470 #endif
471 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
472                                           HOST_WIDE_INT);
473 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
474 static void output_cfi_directive (dw_cfi_ref);
475 static void output_call_frame_info (int);
476 static void dwarf2out_note_section_used (void);
477 static void flush_queued_reg_saves (void);
478 static bool clobbers_queued_reg_save (const_rtx);
479 static void dwarf2out_frame_debug_expr (rtx, const char *);
480
481 /* Support for complex CFA locations.  */
482 static void output_cfa_loc (dw_cfi_ref);
483 static void output_cfa_loc_raw (dw_cfi_ref);
484 static void get_cfa_from_loc_descr (dw_cfa_location *,
485                                     struct dw_loc_descr_struct *);
486 static struct dw_loc_descr_struct *build_cfa_loc
487   (dw_cfa_location *, HOST_WIDE_INT);
488 static struct dw_loc_descr_struct *build_cfa_aligned_loc
489   (HOST_WIDE_INT, HOST_WIDE_INT);
490 static void def_cfa_1 (const char *, dw_cfa_location *);
491
492 /* How to start an assembler comment.  */
493 #ifndef ASM_COMMENT_START
494 #define ASM_COMMENT_START ";#"
495 #endif
496
497 /* Data and reference forms for relocatable data.  */
498 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
499 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
500
501 #ifndef DEBUG_FRAME_SECTION
502 #define DEBUG_FRAME_SECTION     ".debug_frame"
503 #endif
504
505 #ifndef FUNC_BEGIN_LABEL
506 #define FUNC_BEGIN_LABEL        "LFB"
507 #endif
508
509 #ifndef FUNC_END_LABEL
510 #define FUNC_END_LABEL          "LFE"
511 #endif
512
513 #ifndef PROLOGUE_END_LABEL
514 #define PROLOGUE_END_LABEL      "LPE"
515 #endif
516
517 #ifndef EPILOGUE_BEGIN_LABEL
518 #define EPILOGUE_BEGIN_LABEL    "LEB"
519 #endif
520
521 #ifndef FRAME_BEGIN_LABEL
522 #define FRAME_BEGIN_LABEL       "Lframe"
523 #endif
524 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
525 #define CIE_END_LABEL           "LECIE"
526 #define FDE_LABEL               "LSFDE"
527 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
528 #define FDE_END_LABEL           "LEFDE"
529 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
530 #define LINE_NUMBER_END_LABEL   "LELT"
531 #define LN_PROLOG_AS_LABEL      "LASLTP"
532 #define LN_PROLOG_END_LABEL     "LELTP"
533 #define DIE_LABEL_PREFIX        "DW"
534
535 /* The DWARF 2 CFA column which tracks the return address.  Normally this
536    is the column for PC, or the first column after all of the hard
537    registers.  */
538 #ifndef DWARF_FRAME_RETURN_COLUMN
539 #ifdef PC_REGNUM
540 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
541 #else
542 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
543 #endif
544 #endif
545
546 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
547    default, we just provide columns for all registers.  */
548 #ifndef DWARF_FRAME_REGNUM
549 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
550 #endif
551 \f
552 /* Hook used by __throw.  */
553
554 rtx
555 expand_builtin_dwarf_sp_column (void)
556 {
557   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
558   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
559 }
560
561 /* Return a pointer to a copy of the section string name S with all
562    attributes stripped off, and an asterisk prepended (for assemble_name).  */
563
564 static inline char *
565 stripattributes (const char *s)
566 {
567   char *stripped = XNEWVEC (char, strlen (s) + 2);
568   char *p = stripped;
569
570   *p++ = '*';
571
572   while (*s && *s != ',')
573     *p++ = *s++;
574
575   *p = '\0';
576   return stripped;
577 }
578
579 /* MEM is a memory reference for the register size table, each element of
580    which has mode MODE.  Initialize column C as a return address column.  */
581
582 static void
583 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
584 {
585   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
586   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
587   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
588 }
589
590 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
591
592 static inline HOST_WIDE_INT
593 div_data_align (HOST_WIDE_INT off)
594 {
595   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
596   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
597   return r;
598 }
599
600 /* Return true if we need a signed version of a given opcode
601    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
602
603 static inline bool
604 need_data_align_sf_opcode (HOST_WIDE_INT off)
605 {
606   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
607 }
608
609 /* Generate code to initialize the register size table.  */
610
611 void
612 expand_builtin_init_dwarf_reg_sizes (tree address)
613 {
614   unsigned int i;
615   enum machine_mode mode = TYPE_MODE (char_type_node);
616   rtx addr = expand_normal (address);
617   rtx mem = gen_rtx_MEM (BLKmode, addr);
618   bool wrote_return_column = false;
619
620   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
621     {
622       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
623
624       if (rnum < DWARF_FRAME_REGISTERS)
625         {
626           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
627           enum machine_mode save_mode = reg_raw_mode[i];
628           HOST_WIDE_INT size;
629
630           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
631             save_mode = choose_hard_reg_mode (i, 1, true);
632           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
633             {
634               if (save_mode == VOIDmode)
635                 continue;
636               wrote_return_column = true;
637             }
638           size = GET_MODE_SIZE (save_mode);
639           if (offset < 0)
640             continue;
641
642           emit_move_insn (adjust_address (mem, mode, offset),
643                           gen_int_mode (size, mode));
644         }
645     }
646
647   if (!wrote_return_column)
648     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
649
650 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
651   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
652 #endif
653
654   targetm.init_dwarf_reg_sizes_extra (address);
655 }
656
657 /* Convert a DWARF call frame info. operation to its string name */
658
659 static const char *
660 dwarf_cfi_name (unsigned int cfi_opc)
661 {
662   switch (cfi_opc)
663     {
664     case DW_CFA_advance_loc:
665       return "DW_CFA_advance_loc";
666     case DW_CFA_offset:
667       return "DW_CFA_offset";
668     case DW_CFA_restore:
669       return "DW_CFA_restore";
670     case DW_CFA_nop:
671       return "DW_CFA_nop";
672     case DW_CFA_set_loc:
673       return "DW_CFA_set_loc";
674     case DW_CFA_advance_loc1:
675       return "DW_CFA_advance_loc1";
676     case DW_CFA_advance_loc2:
677       return "DW_CFA_advance_loc2";
678     case DW_CFA_advance_loc4:
679       return "DW_CFA_advance_loc4";
680     case DW_CFA_offset_extended:
681       return "DW_CFA_offset_extended";
682     case DW_CFA_restore_extended:
683       return "DW_CFA_restore_extended";
684     case DW_CFA_undefined:
685       return "DW_CFA_undefined";
686     case DW_CFA_same_value:
687       return "DW_CFA_same_value";
688     case DW_CFA_register:
689       return "DW_CFA_register";
690     case DW_CFA_remember_state:
691       return "DW_CFA_remember_state";
692     case DW_CFA_restore_state:
693       return "DW_CFA_restore_state";
694     case DW_CFA_def_cfa:
695       return "DW_CFA_def_cfa";
696     case DW_CFA_def_cfa_register:
697       return "DW_CFA_def_cfa_register";
698     case DW_CFA_def_cfa_offset:
699       return "DW_CFA_def_cfa_offset";
700
701     /* DWARF 3 */
702     case DW_CFA_def_cfa_expression:
703       return "DW_CFA_def_cfa_expression";
704     case DW_CFA_expression:
705       return "DW_CFA_expression";
706     case DW_CFA_offset_extended_sf:
707       return "DW_CFA_offset_extended_sf";
708     case DW_CFA_def_cfa_sf:
709       return "DW_CFA_def_cfa_sf";
710     case DW_CFA_def_cfa_offset_sf:
711       return "DW_CFA_def_cfa_offset_sf";
712
713     /* SGI/MIPS specific */
714     case DW_CFA_MIPS_advance_loc8:
715       return "DW_CFA_MIPS_advance_loc8";
716
717     /* GNU extensions */
718     case DW_CFA_GNU_window_save:
719       return "DW_CFA_GNU_window_save";
720     case DW_CFA_GNU_args_size:
721       return "DW_CFA_GNU_args_size";
722     case DW_CFA_GNU_negative_offset_extended:
723       return "DW_CFA_GNU_negative_offset_extended";
724
725     default:
726       return "DW_CFA_<unknown>";
727     }
728 }
729
730 /* Return a pointer to a newly allocated Call Frame Instruction.  */
731
732 static inline dw_cfi_ref
733 new_cfi (void)
734 {
735   dw_cfi_ref cfi = ggc_alloc_dw_cfi_node ();
736
737   cfi->dw_cfi_next = NULL;
738   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
739   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
740
741   return cfi;
742 }
743
744 /* Add a Call Frame Instruction to list of instructions.  */
745
746 static inline void
747 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
748 {
749   dw_cfi_ref *p;
750   dw_fde_ref fde = current_fde ();
751
752   /* When DRAP is used, CFA is defined with an expression.  Redefine
753      CFA may lead to a different CFA value.   */
754   /* ??? Of course, this heuristic fails when we're annotating epilogues,
755      because of course we'll always want to redefine the CFA back to the
756      stack pointer on the way out.  Where should we move this check?  */
757   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
758     switch (cfi->dw_cfi_opc)
759       {
760         case DW_CFA_def_cfa_register:
761         case DW_CFA_def_cfa_offset:
762         case DW_CFA_def_cfa_offset_sf:
763         case DW_CFA_def_cfa:
764         case DW_CFA_def_cfa_sf:
765           gcc_unreachable ();
766
767         default:
768           break;
769       }
770
771   /* Find the end of the chain.  */
772   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
773     ;
774
775   *p = cfi;
776 }
777
778 /* Generate a new label for the CFI info to refer to.  FORCE is true
779    if a label needs to be output even when using .cfi_* directives.  */
780
781 char *
782 dwarf2out_cfi_label (bool force)
783 {
784   static char label[20];
785
786   if (!force && dwarf2out_do_cfi_asm ())
787     {
788       /* In this case, we will be emitting the asm directive instead of
789          the label, so just return a placeholder to keep the rest of the
790          interfaces happy.  */
791       strcpy (label, "<do not output>");
792     }
793   else
794     {
795       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
796       ASM_OUTPUT_LABEL (asm_out_file, label);
797     }
798
799   return label;
800 }
801
802 /* True if remember_state should be emitted before following CFI directive.  */
803 static bool emit_cfa_remember;
804
805 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
806    or to the CIE if LABEL is NULL.  */
807
808 static void
809 add_fde_cfi (const char *label, dw_cfi_ref cfi)
810 {
811   dw_cfi_ref *list_head;
812
813   if (emit_cfa_remember)
814     {
815       dw_cfi_ref cfi_remember;
816
817       /* Emit the state save.  */
818       emit_cfa_remember = false;
819       cfi_remember = new_cfi ();
820       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
821       add_fde_cfi (label, cfi_remember);
822     }
823
824   list_head = &cie_cfi_head;
825
826   if (dwarf2out_do_cfi_asm ())
827     {
828       if (label)
829         {
830           dw_fde_ref fde = current_fde ();
831
832           gcc_assert (fde != NULL);
833
834           /* We still have to add the cfi to the list so that lookup_cfa
835              works later on.  When -g2 and above we even need to force
836              emitting of CFI labels and add to list a DW_CFA_set_loc for
837              convert_cfa_to_fb_loc_list purposes.  If we're generating
838              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
839              convert_cfa_to_fb_loc_list.  */
840           if (dwarf_version == 2
841               && debug_info_level > DINFO_LEVEL_TERSE
842               && (write_symbols == DWARF2_DEBUG
843                   || write_symbols == VMS_AND_DWARF2_DEBUG))
844             {
845               switch (cfi->dw_cfi_opc)
846                 {
847                 case DW_CFA_def_cfa_offset:
848                 case DW_CFA_def_cfa_offset_sf:
849                 case DW_CFA_def_cfa_register:
850                 case DW_CFA_def_cfa:
851                 case DW_CFA_def_cfa_sf:
852                 case DW_CFA_def_cfa_expression:
853                 case DW_CFA_restore_state:
854                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
855                     label = dwarf2out_cfi_label (true);
856
857                   if (fde->dw_fde_current_label == NULL
858                       || strcmp (label, fde->dw_fde_current_label) != 0)
859                     {
860                       dw_cfi_ref xcfi;
861
862                       label = xstrdup (label);
863
864                       /* Set the location counter to the new label.  */
865                       xcfi = new_cfi ();
866                       /* It doesn't metter whether DW_CFA_set_loc
867                          or DW_CFA_advance_loc4 is added here, those aren't
868                          emitted into assembly, only looked up by
869                          convert_cfa_to_fb_loc_list.  */
870                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
871                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
872                       add_cfi (&fde->dw_fde_cfi, xcfi);
873                       fde->dw_fde_current_label = label;
874                     }
875                   break;
876                 default:
877                   break;
878                 }
879             }
880
881           output_cfi_directive (cfi);
882
883           list_head = &fde->dw_fde_cfi;
884         }
885       /* ??? If this is a CFI for the CIE, we don't emit.  This
886          assumes that the standard CIE contents that the assembler
887          uses matches the standard CIE contents that the compiler
888          uses.  This is probably a bad assumption.  I'm not quite
889          sure how to address this for now.  */
890     }
891   else if (label)
892     {
893       dw_fde_ref fde = current_fde ();
894
895       gcc_assert (fde != NULL);
896
897       if (*label == 0)
898         label = dwarf2out_cfi_label (false);
899
900       if (fde->dw_fde_current_label == NULL
901           || strcmp (label, fde->dw_fde_current_label) != 0)
902         {
903           dw_cfi_ref xcfi;
904
905           label = xstrdup (label);
906
907           /* Set the location counter to the new label.  */
908           xcfi = new_cfi ();
909           /* If we have a current label, advance from there, otherwise
910              set the location directly using set_loc.  */
911           xcfi->dw_cfi_opc = fde->dw_fde_current_label
912                              ? DW_CFA_advance_loc4
913                              : DW_CFA_set_loc;
914           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
915           add_cfi (&fde->dw_fde_cfi, xcfi);
916
917           fde->dw_fde_current_label = label;
918         }
919
920       list_head = &fde->dw_fde_cfi;
921     }
922
923   add_cfi (list_head, cfi);
924 }
925
926 /* Subroutine of lookup_cfa.  */
927
928 static void
929 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
930 {
931   switch (cfi->dw_cfi_opc)
932     {
933     case DW_CFA_def_cfa_offset:
934     case DW_CFA_def_cfa_offset_sf:
935       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
936       break;
937     case DW_CFA_def_cfa_register:
938       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
939       break;
940     case DW_CFA_def_cfa:
941     case DW_CFA_def_cfa_sf:
942       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
943       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
944       break;
945     case DW_CFA_def_cfa_expression:
946       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
947       break;
948
949     case DW_CFA_remember_state:
950       gcc_assert (!remember->in_use);
951       *remember = *loc;
952       remember->in_use = 1;
953       break;
954     case DW_CFA_restore_state:
955       gcc_assert (remember->in_use);
956       *loc = *remember;
957       remember->in_use = 0;
958       break;
959
960     default:
961       break;
962     }
963 }
964
965 /* Find the previous value for the CFA.  */
966
967 static void
968 lookup_cfa (dw_cfa_location *loc)
969 {
970   dw_cfi_ref cfi;
971   dw_fde_ref fde;
972   dw_cfa_location remember;
973
974   memset (loc, 0, sizeof (*loc));
975   loc->reg = INVALID_REGNUM;
976   remember = *loc;
977
978   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
979     lookup_cfa_1 (cfi, loc, &remember);
980
981   fde = current_fde ();
982   if (fde)
983     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
984       lookup_cfa_1 (cfi, loc, &remember);
985 }
986
987 /* The current rule for calculating the DWARF2 canonical frame address.  */
988 static dw_cfa_location cfa;
989
990 /* The register used for saving registers to the stack, and its offset
991    from the CFA.  */
992 static dw_cfa_location cfa_store;
993
994 /* The current save location around an epilogue.  */
995 static dw_cfa_location cfa_remember;
996
997 /* The running total of the size of arguments pushed onto the stack.  */
998 static HOST_WIDE_INT args_size;
999
1000 /* The last args_size we actually output.  */
1001 static HOST_WIDE_INT old_args_size;
1002
1003 /* Entry point to update the canonical frame address (CFA).
1004    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
1005    calculated from REG+OFFSET.  */
1006
1007 void
1008 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1009 {
1010   dw_cfa_location loc;
1011   loc.indirect = 0;
1012   loc.base_offset = 0;
1013   loc.reg = reg;
1014   loc.offset = offset;
1015   def_cfa_1 (label, &loc);
1016 }
1017
1018 /* Determine if two dw_cfa_location structures define the same data.  */
1019
1020 static bool
1021 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
1022 {
1023   return (loc1->reg == loc2->reg
1024           && loc1->offset == loc2->offset
1025           && loc1->indirect == loc2->indirect
1026           && (loc1->indirect == 0
1027               || loc1->base_offset == loc2->base_offset));
1028 }
1029
1030 /* This routine does the actual work.  The CFA is now calculated from
1031    the dw_cfa_location structure.  */
1032
1033 static void
1034 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1035 {
1036   dw_cfi_ref cfi;
1037   dw_cfa_location old_cfa, loc;
1038
1039   cfa = *loc_p;
1040   loc = *loc_p;
1041
1042   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1043     cfa_store.offset = loc.offset;
1044
1045   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1046   lookup_cfa (&old_cfa);
1047
1048   /* If nothing changed, no need to issue any call frame instructions.  */
1049   if (cfa_equal_p (&loc, &old_cfa))
1050     return;
1051
1052   cfi = new_cfi ();
1053
1054   if (loc.reg == old_cfa.reg && !loc.indirect && !old_cfa.indirect)
1055     {
1056       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1057          the CFA register did not change but the offset did.  The data
1058          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1059          in the assembler via the .cfi_def_cfa_offset directive.  */
1060       if (loc.offset < 0)
1061         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1062       else
1063         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1064       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1065     }
1066
1067 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1068   else if (loc.offset == old_cfa.offset
1069            && old_cfa.reg != INVALID_REGNUM
1070            && !loc.indirect
1071            && !old_cfa.indirect)
1072     {
1073       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1074          indicating the CFA register has changed to <register> but the
1075          offset has not changed.  */
1076       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1077       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1078     }
1079 #endif
1080
1081   else if (loc.indirect == 0)
1082     {
1083       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1084          indicating the CFA register has changed to <register> with
1085          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1086          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1087          directive.  */
1088       if (loc.offset < 0)
1089         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1090       else
1091         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1092       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1093       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1094     }
1095   else
1096     {
1097       /* Construct a DW_CFA_def_cfa_expression instruction to
1098          calculate the CFA using a full location expression since no
1099          register-offset pair is available.  */
1100       struct dw_loc_descr_struct *loc_list;
1101
1102       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1103       loc_list = build_cfa_loc (&loc, 0);
1104       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1105     }
1106
1107   add_fde_cfi (label, cfi);
1108 }
1109
1110 /* Add the CFI for saving a register.  REG is the CFA column number.
1111    LABEL is passed to add_fde_cfi.
1112    If SREG is -1, the register is saved at OFFSET from the CFA;
1113    otherwise it is saved in SREG.  */
1114
1115 static void
1116 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1117 {
1118   dw_cfi_ref cfi = new_cfi ();
1119   dw_fde_ref fde = current_fde ();
1120
1121   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1122
1123   /* When stack is aligned, store REG using DW_CFA_expression with
1124      FP.  */
1125   if (fde
1126       && fde->stack_realign
1127       && sreg == INVALID_REGNUM)
1128     {
1129       cfi->dw_cfi_opc = DW_CFA_expression;
1130       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1131       cfi->dw_cfi_oprnd2.dw_cfi_loc
1132         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1133     }
1134   else if (sreg == INVALID_REGNUM)
1135     {
1136       if (need_data_align_sf_opcode (offset))
1137         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1138       else if (reg & ~0x3f)
1139         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1140       else
1141         cfi->dw_cfi_opc = DW_CFA_offset;
1142       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1143     }
1144   else if (sreg == reg)
1145     cfi->dw_cfi_opc = DW_CFA_same_value;
1146   else
1147     {
1148       cfi->dw_cfi_opc = DW_CFA_register;
1149       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1150     }
1151
1152   add_fde_cfi (label, cfi);
1153 }
1154
1155 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1156    This CFI tells the unwinder that it needs to restore the window registers
1157    from the previous frame's window save area.
1158
1159    ??? Perhaps we should note in the CIE where windows are saved (instead of
1160    assuming 0(cfa)) and what registers are in the window.  */
1161
1162 void
1163 dwarf2out_window_save (const char *label)
1164 {
1165   dw_cfi_ref cfi = new_cfi ();
1166
1167   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1168   add_fde_cfi (label, cfi);
1169 }
1170
1171 /* Entry point for saving a register to the stack.  REG is the GCC register
1172    number.  LABEL and OFFSET are passed to reg_save.  */
1173
1174 void
1175 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1176 {
1177   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1178 }
1179
1180 /* Entry point for saving the return address in the stack.
1181    LABEL and OFFSET are passed to reg_save.  */
1182
1183 void
1184 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1185 {
1186   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1187 }
1188
1189 /* Entry point for saving the return address in a register.
1190    LABEL and SREG are passed to reg_save.  */
1191
1192 void
1193 dwarf2out_return_reg (const char *label, unsigned int sreg)
1194 {
1195   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1196 }
1197
1198 #ifdef DWARF2_UNWIND_INFO
1199 /* Record the initial position of the return address.  RTL is
1200    INCOMING_RETURN_ADDR_RTX.  */
1201
1202 static void
1203 initial_return_save (rtx rtl)
1204 {
1205   unsigned int reg = INVALID_REGNUM;
1206   HOST_WIDE_INT offset = 0;
1207
1208   switch (GET_CODE (rtl))
1209     {
1210     case REG:
1211       /* RA is in a register.  */
1212       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1213       break;
1214
1215     case MEM:
1216       /* RA is on the stack.  */
1217       rtl = XEXP (rtl, 0);
1218       switch (GET_CODE (rtl))
1219         {
1220         case REG:
1221           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1222           offset = 0;
1223           break;
1224
1225         case PLUS:
1226           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1227           offset = INTVAL (XEXP (rtl, 1));
1228           break;
1229
1230         case MINUS:
1231           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1232           offset = -INTVAL (XEXP (rtl, 1));
1233           break;
1234
1235         default:
1236           gcc_unreachable ();
1237         }
1238
1239       break;
1240
1241     case PLUS:
1242       /* The return address is at some offset from any value we can
1243          actually load.  For instance, on the SPARC it is in %i7+8. Just
1244          ignore the offset for now; it doesn't matter for unwinding frames.  */
1245       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1246       initial_return_save (XEXP (rtl, 0));
1247       return;
1248
1249     default:
1250       gcc_unreachable ();
1251     }
1252
1253   if (reg != DWARF_FRAME_RETURN_COLUMN)
1254     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1255 }
1256 #endif
1257
1258 /* Given a SET, calculate the amount of stack adjustment it
1259    contains.  */
1260
1261 static HOST_WIDE_INT
1262 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1263                      HOST_WIDE_INT cur_offset)
1264 {
1265   const_rtx src = SET_SRC (pattern);
1266   const_rtx dest = SET_DEST (pattern);
1267   HOST_WIDE_INT offset = 0;
1268   enum rtx_code code;
1269
1270   if (dest == stack_pointer_rtx)
1271     {
1272       code = GET_CODE (src);
1273
1274       /* Assume (set (reg sp) (reg whatever)) sets args_size
1275          level to 0.  */
1276       if (code == REG && src != stack_pointer_rtx)
1277         {
1278           offset = -cur_args_size;
1279 #ifndef STACK_GROWS_DOWNWARD
1280           offset = -offset;
1281 #endif
1282           return offset - cur_offset;
1283         }
1284
1285       if (! (code == PLUS || code == MINUS)
1286           || XEXP (src, 0) != stack_pointer_rtx
1287           || !CONST_INT_P (XEXP (src, 1)))
1288         return 0;
1289
1290       /* (set (reg sp) (plus (reg sp) (const_int))) */
1291       offset = INTVAL (XEXP (src, 1));
1292       if (code == PLUS)
1293         offset = -offset;
1294       return offset;
1295     }
1296
1297   if (MEM_P (src) && !MEM_P (dest))
1298     dest = src;
1299   if (MEM_P (dest))
1300     {
1301       /* (set (mem (pre_dec (reg sp))) (foo)) */
1302       src = XEXP (dest, 0);
1303       code = GET_CODE (src);
1304
1305       switch (code)
1306         {
1307         case PRE_MODIFY:
1308         case POST_MODIFY:
1309           if (XEXP (src, 0) == stack_pointer_rtx)
1310             {
1311               rtx val = XEXP (XEXP (src, 1), 1);
1312               /* We handle only adjustments by constant amount.  */
1313               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1314                           && CONST_INT_P (val));
1315               offset = -INTVAL (val);
1316               break;
1317             }
1318           return 0;
1319
1320         case PRE_DEC:
1321         case POST_DEC:
1322           if (XEXP (src, 0) == stack_pointer_rtx)
1323             {
1324               offset = GET_MODE_SIZE (GET_MODE (dest));
1325               break;
1326             }
1327           return 0;
1328
1329         case PRE_INC:
1330         case POST_INC:
1331           if (XEXP (src, 0) == stack_pointer_rtx)
1332             {
1333               offset = -GET_MODE_SIZE (GET_MODE (dest));
1334               break;
1335             }
1336           return 0;
1337
1338         default:
1339           return 0;
1340         }
1341     }
1342   else
1343     return 0;
1344
1345   return offset;
1346 }
1347
1348 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1349    indexed by INSN_UID.  */
1350
1351 static HOST_WIDE_INT *barrier_args_size;
1352
1353 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1354
1355 static HOST_WIDE_INT
1356 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1357                              VEC (rtx, heap) **next)
1358 {
1359   HOST_WIDE_INT offset = 0;
1360   int i;
1361
1362   if (! RTX_FRAME_RELATED_P (insn))
1363     {
1364       if (prologue_epilogue_contains (insn))
1365         /* Nothing */;
1366       else if (GET_CODE (PATTERN (insn)) == SET)
1367         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1368       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1369                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1370         {
1371           /* There may be stack adjustments inside compound insns.  Search
1372              for them.  */
1373           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1374             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1375               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1376                                              cur_args_size, offset);
1377         }
1378     }
1379   else
1380     {
1381       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1382
1383       if (expr)
1384         {
1385           expr = XEXP (expr, 0);
1386           if (GET_CODE (expr) == PARALLEL
1387               || GET_CODE (expr) == SEQUENCE)
1388             for (i = 1; i < XVECLEN (expr, 0); i++)
1389               {
1390                 rtx elem = XVECEXP (expr, 0, i);
1391
1392                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1393                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1394               }
1395         }
1396     }
1397
1398 #ifndef STACK_GROWS_DOWNWARD
1399   offset = -offset;
1400 #endif
1401
1402   cur_args_size += offset;
1403   if (cur_args_size < 0)
1404     cur_args_size = 0;
1405
1406   if (JUMP_P (insn))
1407     {
1408       rtx dest = JUMP_LABEL (insn);
1409
1410       if (dest)
1411         {
1412           if (barrier_args_size [INSN_UID (dest)] < 0)
1413             {
1414               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1415               VEC_safe_push (rtx, heap, *next, dest);
1416             }
1417         }
1418     }
1419
1420   return cur_args_size;
1421 }
1422
1423 /* Walk the whole function and compute args_size on BARRIERs.  */
1424
1425 static void
1426 compute_barrier_args_size (void)
1427 {
1428   int max_uid = get_max_uid (), i;
1429   rtx insn;
1430   VEC (rtx, heap) *worklist, *next, *tmp;
1431
1432   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1433   for (i = 0; i < max_uid; i++)
1434     barrier_args_size[i] = -1;
1435
1436   worklist = VEC_alloc (rtx, heap, 20);
1437   next = VEC_alloc (rtx, heap, 20);
1438   insn = get_insns ();
1439   barrier_args_size[INSN_UID (insn)] = 0;
1440   VEC_quick_push (rtx, worklist, insn);
1441   for (;;)
1442     {
1443       while (!VEC_empty (rtx, worklist))
1444         {
1445           rtx prev, body, first_insn;
1446           HOST_WIDE_INT cur_args_size;
1447
1448           first_insn = insn = VEC_pop (rtx, worklist);
1449           cur_args_size = barrier_args_size[INSN_UID (insn)];
1450           prev = prev_nonnote_insn (insn);
1451           if (prev && BARRIER_P (prev))
1452             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1453
1454           for (; insn; insn = NEXT_INSN (insn))
1455             {
1456               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1457                 continue;
1458               if (BARRIER_P (insn))
1459                 break;
1460
1461               if (LABEL_P (insn))
1462                 {
1463                   if (insn == first_insn)
1464                     continue;
1465                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1466                     {
1467                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1468                       continue;
1469                     }
1470                   else
1471                     {
1472                       /* The insns starting with this label have been
1473                          already scanned or are in the worklist.  */
1474                       break;
1475                     }
1476                 }
1477
1478               body = PATTERN (insn);
1479               if (GET_CODE (body) == SEQUENCE)
1480                 {
1481                   HOST_WIDE_INT dest_args_size = cur_args_size;
1482                   for (i = 1; i < XVECLEN (body, 0); i++)
1483                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1484                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1485                       dest_args_size
1486                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1487                                                        dest_args_size, &next);
1488                     else
1489                       cur_args_size
1490                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1491                                                        cur_args_size, &next);
1492
1493                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1494                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1495                                                  dest_args_size, &next);
1496                   else
1497                     cur_args_size
1498                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1499                                                      cur_args_size, &next);
1500                 }
1501               else
1502                 cur_args_size
1503                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1504             }
1505         }
1506
1507       if (VEC_empty (rtx, next))
1508         break;
1509
1510       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1511       tmp = next;
1512       next = worklist;
1513       worklist = tmp;
1514       VEC_truncate (rtx, next, 0);
1515     }
1516
1517   VEC_free (rtx, heap, worklist);
1518   VEC_free (rtx, heap, next);
1519 }
1520
1521 /* Add a CFI to update the running total of the size of arguments
1522    pushed onto the stack.  */
1523
1524 static void
1525 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1526 {
1527   dw_cfi_ref cfi;
1528
1529   if (size == old_args_size)
1530     return;
1531
1532   old_args_size = size;
1533
1534   cfi = new_cfi ();
1535   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1536   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1537   add_fde_cfi (label, cfi);
1538 }
1539
1540 /* Record a stack adjustment of OFFSET bytes.  */
1541
1542 static void
1543 dwarf2out_stack_adjust (HOST_WIDE_INT offset, const char *label)
1544 {
1545   if (cfa.reg == STACK_POINTER_REGNUM)
1546     cfa.offset += offset;
1547
1548   if (cfa_store.reg == STACK_POINTER_REGNUM)
1549     cfa_store.offset += offset;
1550
1551   if (ACCUMULATE_OUTGOING_ARGS)
1552     return;
1553
1554 #ifndef STACK_GROWS_DOWNWARD
1555   offset = -offset;
1556 #endif
1557
1558   args_size += offset;
1559   if (args_size < 0)
1560     args_size = 0;
1561
1562   def_cfa_1 (label, &cfa);
1563   if (flag_asynchronous_unwind_tables)
1564     dwarf2out_args_size (label, args_size);
1565 }
1566
1567 /* Check INSN to see if it looks like a push or a stack adjustment, and
1568    make a note of it if it does.  EH uses this information to find out
1569    how much extra space it needs to pop off the stack.  */
1570
1571 static void
1572 dwarf2out_notice_stack_adjust (rtx insn, bool after_p)
1573 {
1574   HOST_WIDE_INT offset;
1575   const char *label;
1576   int i;
1577
1578   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1579      with this function.  Proper support would require all frame-related
1580      insns to be marked, and to be able to handle saving state around
1581      epilogues textually in the middle of the function.  */
1582   if (prologue_epilogue_contains (insn))
1583     return;
1584
1585   /* If INSN is an instruction from target of an annulled branch, the
1586      effects are for the target only and so current argument size
1587      shouldn't change at all.  */
1588   if (final_sequence
1589       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1590       && INSN_FROM_TARGET_P (insn))
1591     return;
1592
1593   /* If only calls can throw, and we have a frame pointer,
1594      save up adjustments until we see the CALL_INSN.  */
1595   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1596     {
1597       if (CALL_P (insn) && !after_p)
1598         {
1599           /* Extract the size of the args from the CALL rtx itself.  */
1600           insn = PATTERN (insn);
1601           if (GET_CODE (insn) == PARALLEL)
1602             insn = XVECEXP (insn, 0, 0);
1603           if (GET_CODE (insn) == SET)
1604             insn = SET_SRC (insn);
1605           gcc_assert (GET_CODE (insn) == CALL);
1606           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1607         }
1608       return;
1609     }
1610
1611   if (CALL_P (insn) && !after_p)
1612     {
1613       if (!flag_asynchronous_unwind_tables)
1614         dwarf2out_args_size ("", args_size);
1615       return;
1616     }
1617   else if (BARRIER_P (insn))
1618     {
1619       /* Don't call compute_barrier_args_size () if the only
1620          BARRIER is at the end of function.  */
1621       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1622         compute_barrier_args_size ();
1623       if (barrier_args_size == NULL)
1624         offset = 0;
1625       else
1626         {
1627           offset = barrier_args_size[INSN_UID (insn)];
1628           if (offset < 0)
1629             offset = 0;
1630         }
1631
1632       offset -= args_size;
1633 #ifndef STACK_GROWS_DOWNWARD
1634       offset = -offset;
1635 #endif
1636     }
1637   else if (GET_CODE (PATTERN (insn)) == SET)
1638     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1639   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1640            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1641     {
1642       /* There may be stack adjustments inside compound insns.  Search
1643          for them.  */
1644       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1645         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1646           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1647                                          args_size, offset);
1648     }
1649   else
1650     return;
1651
1652   if (offset == 0)
1653     return;
1654
1655   label = dwarf2out_cfi_label (false);
1656   dwarf2out_stack_adjust (offset, label);
1657 }
1658
1659 #endif
1660
1661 /* We delay emitting a register save until either (a) we reach the end
1662    of the prologue or (b) the register is clobbered.  This clusters
1663    register saves so that there are fewer pc advances.  */
1664
1665 struct GTY(()) queued_reg_save {
1666   struct queued_reg_save *next;
1667   rtx reg;
1668   HOST_WIDE_INT cfa_offset;
1669   rtx saved_reg;
1670 };
1671
1672 static GTY(()) struct queued_reg_save *queued_reg_saves;
1673
1674 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1675 struct GTY(()) reg_saved_in_data {
1676   rtx orig_reg;
1677   rtx saved_in_reg;
1678 };
1679
1680 /* A list of registers saved in other registers.
1681    The list intentionally has a small maximum capacity of 4; if your
1682    port needs more than that, you might consider implementing a
1683    more efficient data structure.  */
1684 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1685 static GTY(()) size_t num_regs_saved_in_regs;
1686
1687 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1688 static const char *last_reg_save_label;
1689
1690 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1691    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1692
1693 static void
1694 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1695 {
1696   struct queued_reg_save *q;
1697
1698   /* Duplicates waste space, but it's also necessary to remove them
1699      for correctness, since the queue gets output in reverse
1700      order.  */
1701   for (q = queued_reg_saves; q != NULL; q = q->next)
1702     if (REGNO (q->reg) == REGNO (reg))
1703       break;
1704
1705   if (q == NULL)
1706     {
1707       q = ggc_alloc_queued_reg_save ();
1708       q->next = queued_reg_saves;
1709       queued_reg_saves = q;
1710     }
1711
1712   q->reg = reg;
1713   q->cfa_offset = offset;
1714   q->saved_reg = sreg;
1715
1716   last_reg_save_label = label;
1717 }
1718
1719 /* Output all the entries in QUEUED_REG_SAVES.  */
1720
1721 static void
1722 flush_queued_reg_saves (void)
1723 {
1724   struct queued_reg_save *q;
1725
1726   for (q = queued_reg_saves; q; q = q->next)
1727     {
1728       size_t i;
1729       unsigned int reg, sreg;
1730
1731       for (i = 0; i < num_regs_saved_in_regs; i++)
1732         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1733           break;
1734       if (q->saved_reg && i == num_regs_saved_in_regs)
1735         {
1736           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1737           num_regs_saved_in_regs++;
1738         }
1739       if (i != num_regs_saved_in_regs)
1740         {
1741           regs_saved_in_regs[i].orig_reg = q->reg;
1742           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1743         }
1744
1745       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1746       if (q->saved_reg)
1747         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1748       else
1749         sreg = INVALID_REGNUM;
1750       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1751     }
1752
1753   queued_reg_saves = NULL;
1754   last_reg_save_label = NULL;
1755 }
1756
1757 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1758    location for?  Or, does it clobber a register which we've previously
1759    said that some other register is saved in, and for which we now
1760    have a new location for?  */
1761
1762 static bool
1763 clobbers_queued_reg_save (const_rtx insn)
1764 {
1765   struct queued_reg_save *q;
1766
1767   for (q = queued_reg_saves; q; q = q->next)
1768     {
1769       size_t i;
1770       if (modified_in_p (q->reg, insn))
1771         return true;
1772       for (i = 0; i < num_regs_saved_in_regs; i++)
1773         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1774             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1775           return true;
1776     }
1777
1778   return false;
1779 }
1780
1781 /* Entry point for saving the first register into the second.  */
1782
1783 void
1784 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1785 {
1786   size_t i;
1787   unsigned int regno, sregno;
1788
1789   for (i = 0; i < num_regs_saved_in_regs; i++)
1790     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1791       break;
1792   if (i == num_regs_saved_in_regs)
1793     {
1794       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1795       num_regs_saved_in_regs++;
1796     }
1797   regs_saved_in_regs[i].orig_reg = reg;
1798   regs_saved_in_regs[i].saved_in_reg = sreg;
1799
1800   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1801   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1802   reg_save (label, regno, sregno, 0);
1803 }
1804
1805 /* What register, if any, is currently saved in REG?  */
1806
1807 static rtx
1808 reg_saved_in (rtx reg)
1809 {
1810   unsigned int regn = REGNO (reg);
1811   size_t i;
1812   struct queued_reg_save *q;
1813
1814   for (q = queued_reg_saves; q; q = q->next)
1815     if (q->saved_reg && regn == REGNO (q->saved_reg))
1816       return q->reg;
1817
1818   for (i = 0; i < num_regs_saved_in_regs; i++)
1819     if (regs_saved_in_regs[i].saved_in_reg
1820         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1821       return regs_saved_in_regs[i].orig_reg;
1822
1823   return NULL_RTX;
1824 }
1825
1826
1827 /* A temporary register holding an integral value used in adjusting SP
1828    or setting up the store_reg.  The "offset" field holds the integer
1829    value, not an offset.  */
1830 static dw_cfa_location cfa_temp;
1831
1832 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1833
1834 static void
1835 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1836 {
1837   memset (&cfa, 0, sizeof (cfa));
1838
1839   switch (GET_CODE (pat))
1840     {
1841     case PLUS:
1842       cfa.reg = REGNO (XEXP (pat, 0));
1843       cfa.offset = INTVAL (XEXP (pat, 1));
1844       break;
1845
1846     case REG:
1847       cfa.reg = REGNO (pat);
1848       break;
1849
1850     default:
1851       /* Recurse and define an expression.  */
1852       gcc_unreachable ();
1853     }
1854
1855   def_cfa_1 (label, &cfa);
1856 }
1857
1858 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1859
1860 static void
1861 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1862 {
1863   rtx src, dest;
1864
1865   gcc_assert (GET_CODE (pat) == SET);
1866   dest = XEXP (pat, 0);
1867   src = XEXP (pat, 1);
1868
1869   switch (GET_CODE (src))
1870     {
1871     case PLUS:
1872       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1873       cfa.offset -= INTVAL (XEXP (src, 1));
1874       break;
1875
1876     case REG:
1877         break;
1878
1879     default:
1880         gcc_unreachable ();
1881     }
1882
1883   cfa.reg = REGNO (dest);
1884   gcc_assert (cfa.indirect == 0);
1885
1886   def_cfa_1 (label, &cfa);
1887 }
1888
1889 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1890
1891 static void
1892 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1893 {
1894   HOST_WIDE_INT offset;
1895   rtx src, addr, span;
1896
1897   src = XEXP (set, 1);
1898   addr = XEXP (set, 0);
1899   gcc_assert (MEM_P (addr));
1900   addr = XEXP (addr, 0);
1901
1902   /* As documented, only consider extremely simple addresses.  */
1903   switch (GET_CODE (addr))
1904     {
1905     case REG:
1906       gcc_assert (REGNO (addr) == cfa.reg);
1907       offset = -cfa.offset;
1908       break;
1909     case PLUS:
1910       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1911       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1912       break;
1913     default:
1914       gcc_unreachable ();
1915     }
1916
1917   span = targetm.dwarf_register_span (src);
1918
1919   /* ??? We'd like to use queue_reg_save, but we need to come up with
1920      a different flushing heuristic for epilogues.  */
1921   if (!span)
1922     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1923   else
1924     {
1925       /* We have a PARALLEL describing where the contents of SRC live.
1926          Queue register saves for each piece of the PARALLEL.  */
1927       int par_index;
1928       int limit;
1929       HOST_WIDE_INT span_offset = offset;
1930
1931       gcc_assert (GET_CODE (span) == PARALLEL);
1932
1933       limit = XVECLEN (span, 0);
1934       for (par_index = 0; par_index < limit; par_index++)
1935         {
1936           rtx elem = XVECEXP (span, 0, par_index);
1937
1938           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1939                     INVALID_REGNUM, span_offset);
1940           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1941         }
1942     }
1943 }
1944
1945 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1946
1947 static void
1948 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1949 {
1950   rtx src, dest;
1951   unsigned sregno, dregno;
1952
1953   src = XEXP (set, 1);
1954   dest = XEXP (set, 0);
1955
1956   if (src == pc_rtx)
1957     sregno = DWARF_FRAME_RETURN_COLUMN;
1958   else
1959     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1960
1961   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1962
1963   /* ??? We'd like to use queue_reg_save, but we need to come up with
1964      a different flushing heuristic for epilogues.  */
1965   reg_save (label, sregno, dregno, 0);
1966 }
1967
1968 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1969
1970 static void
1971 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1972 {
1973   dw_cfi_ref cfi = new_cfi ();
1974   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1975
1976   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1977   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1978
1979   add_fde_cfi (label, cfi);
1980 }
1981
1982 /* Record call frame debugging information for an expression EXPR,
1983    which either sets SP or FP (adjusting how we calculate the frame
1984    address) or saves a register to the stack or another register.
1985    LABEL indicates the address of EXPR.
1986
1987    This function encodes a state machine mapping rtxes to actions on
1988    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1989    users need not read the source code.
1990
1991   The High-Level Picture
1992
1993   Changes in the register we use to calculate the CFA: Currently we
1994   assume that if you copy the CFA register into another register, we
1995   should take the other one as the new CFA register; this seems to
1996   work pretty well.  If it's wrong for some target, it's simple
1997   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1998
1999   Changes in the register we use for saving registers to the stack:
2000   This is usually SP, but not always.  Again, we deduce that if you
2001   copy SP into another register (and SP is not the CFA register),
2002   then the new register is the one we will be using for register
2003   saves.  This also seems to work.
2004
2005   Register saves: There's not much guesswork about this one; if
2006   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
2007   register save, and the register used to calculate the destination
2008   had better be the one we think we're using for this purpose.
2009   It's also assumed that a copy from a call-saved register to another
2010   register is saving that register if RTX_FRAME_RELATED_P is set on
2011   that instruction.  If the copy is from a call-saved register to
2012   the *same* register, that means that the register is now the same
2013   value as in the caller.
2014
2015   Except: If the register being saved is the CFA register, and the
2016   offset is nonzero, we are saving the CFA, so we assume we have to
2017   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
2018   the intent is to save the value of SP from the previous frame.
2019
2020   In addition, if a register has previously been saved to a different
2021   register,
2022
2023   Invariants / Summaries of Rules
2024
2025   cfa          current rule for calculating the CFA.  It usually
2026                consists of a register and an offset.
2027   cfa_store    register used by prologue code to save things to the stack
2028                cfa_store.offset is the offset from the value of
2029                cfa_store.reg to the actual CFA
2030   cfa_temp     register holding an integral value.  cfa_temp.offset
2031                stores the value, which will be used to adjust the
2032                stack pointer.  cfa_temp is also used like cfa_store,
2033                to track stores to the stack via fp or a temp reg.
2034
2035   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2036                with cfa.reg as the first operand changes the cfa.reg and its
2037                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2038                cfa_temp.offset.
2039
2040   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2041                expression yielding a constant.  This sets cfa_temp.reg
2042                and cfa_temp.offset.
2043
2044   Rule 5:      Create a new register cfa_store used to save items to the
2045                stack.
2046
2047   Rules 10-14: Save a register to the stack.  Define offset as the
2048                difference of the original location and cfa_store's
2049                location (or cfa_temp's location if cfa_temp is used).
2050
2051   Rules 16-20: If AND operation happens on sp in prologue, we assume
2052                stack is realigned.  We will use a group of DW_OP_XXX
2053                expressions to represent the location of the stored
2054                register instead of CFA+offset.
2055
2056   The Rules
2057
2058   "{a,b}" indicates a choice of a xor b.
2059   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2060
2061   Rule 1:
2062   (set <reg1> <reg2>:cfa.reg)
2063   effects: cfa.reg = <reg1>
2064            cfa.offset unchanged
2065            cfa_temp.reg = <reg1>
2066            cfa_temp.offset = cfa.offset
2067
2068   Rule 2:
2069   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2070                               {<const_int>,<reg>:cfa_temp.reg}))
2071   effects: cfa.reg = sp if fp used
2072            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2073            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2074              if cfa_store.reg==sp
2075
2076   Rule 3:
2077   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2078   effects: cfa.reg = fp
2079            cfa_offset += +/- <const_int>
2080
2081   Rule 4:
2082   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2083   constraints: <reg1> != fp
2084                <reg1> != sp
2085   effects: cfa.reg = <reg1>
2086            cfa_temp.reg = <reg1>
2087            cfa_temp.offset = cfa.offset
2088
2089   Rule 5:
2090   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2091   constraints: <reg1> != fp
2092                <reg1> != sp
2093   effects: cfa_store.reg = <reg1>
2094            cfa_store.offset = cfa.offset - cfa_temp.offset
2095
2096   Rule 6:
2097   (set <reg> <const_int>)
2098   effects: cfa_temp.reg = <reg>
2099            cfa_temp.offset = <const_int>
2100
2101   Rule 7:
2102   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2103   effects: cfa_temp.reg = <reg1>
2104            cfa_temp.offset |= <const_int>
2105
2106   Rule 8:
2107   (set <reg> (high <exp>))
2108   effects: none
2109
2110   Rule 9:
2111   (set <reg> (lo_sum <exp> <const_int>))
2112   effects: cfa_temp.reg = <reg>
2113            cfa_temp.offset = <const_int>
2114
2115   Rule 10:
2116   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2117   effects: cfa_store.offset -= <const_int>
2118            cfa.offset = cfa_store.offset if cfa.reg == sp
2119            cfa.reg = sp
2120            cfa.base_offset = -cfa_store.offset
2121
2122   Rule 11:
2123   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2124   effects: cfa_store.offset += -/+ mode_size(mem)
2125            cfa.offset = cfa_store.offset if cfa.reg == sp
2126            cfa.reg = sp
2127            cfa.base_offset = -cfa_store.offset
2128
2129   Rule 12:
2130   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2131
2132        <reg2>)
2133   effects: cfa.reg = <reg1>
2134            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2135
2136   Rule 13:
2137   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2138   effects: cfa.reg = <reg1>
2139            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2140
2141   Rule 14:
2142   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2143   effects: cfa.reg = <reg1>
2144            cfa.base_offset = -cfa_temp.offset
2145            cfa_temp.offset -= mode_size(mem)
2146
2147   Rule 15:
2148   (set <reg> {unspec, unspec_volatile})
2149   effects: target-dependent
2150
2151   Rule 16:
2152   (set sp (and: sp <const_int>))
2153   constraints: cfa_store.reg == sp
2154   effects: current_fde.stack_realign = 1
2155            cfa_store.offset = 0
2156            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2157
2158   Rule 17:
2159   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2160   effects: cfa_store.offset += -/+ mode_size(mem)
2161
2162   Rule 18:
2163   (set (mem ({pre_inc, pre_dec} sp)) fp)
2164   constraints: fde->stack_realign == 1
2165   effects: cfa_store.offset = 0
2166            cfa.reg != HARD_FRAME_POINTER_REGNUM
2167
2168   Rule 19:
2169   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2170   constraints: fde->stack_realign == 1
2171                && cfa.offset == 0
2172                && cfa.indirect == 0
2173                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2174   effects: Use DW_CFA_def_cfa_expression to define cfa
2175            cfa.reg == fde->drap_reg  */
2176
2177 static void
2178 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2179 {
2180   rtx src, dest, span;
2181   HOST_WIDE_INT offset;
2182   dw_fde_ref fde;
2183
2184   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2185      the PARALLEL independently. The first element is always processed if
2186      it is a SET. This is for backward compatibility.   Other elements
2187      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2188      flag is set in them.  */
2189   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2190     {
2191       int par_index;
2192       int limit = XVECLEN (expr, 0);
2193       rtx elem;
2194
2195       /* PARALLELs have strict read-modify-write semantics, so we
2196          ought to evaluate every rvalue before changing any lvalue.
2197          It's cumbersome to do that in general, but there's an
2198          easy approximation that is enough for all current users:
2199          handle register saves before register assignments.  */
2200       if (GET_CODE (expr) == PARALLEL)
2201         for (par_index = 0; par_index < limit; par_index++)
2202           {
2203             elem = XVECEXP (expr, 0, par_index);
2204             if (GET_CODE (elem) == SET
2205                 && MEM_P (SET_DEST (elem))
2206                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2207               dwarf2out_frame_debug_expr (elem, label);
2208           }
2209
2210       for (par_index = 0; par_index < limit; par_index++)
2211         {
2212           elem = XVECEXP (expr, 0, par_index);
2213           if (GET_CODE (elem) == SET
2214               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2215               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2216             dwarf2out_frame_debug_expr (elem, label);
2217           else if (GET_CODE (elem) == SET
2218                    && par_index != 0
2219                    && !RTX_FRAME_RELATED_P (elem))
2220             {
2221               /* Stack adjustment combining might combine some post-prologue
2222                  stack adjustment into a prologue stack adjustment.  */
2223               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2224
2225               if (offset != 0)
2226                 dwarf2out_stack_adjust (offset, label);
2227             }
2228         }
2229       return;
2230     }
2231
2232   gcc_assert (GET_CODE (expr) == SET);
2233
2234   src = SET_SRC (expr);
2235   dest = SET_DEST (expr);
2236
2237   if (REG_P (src))
2238     {
2239       rtx rsi = reg_saved_in (src);
2240       if (rsi)
2241         src = rsi;
2242     }
2243
2244   fde = current_fde ();
2245
2246   switch (GET_CODE (dest))
2247     {
2248     case REG:
2249       switch (GET_CODE (src))
2250         {
2251           /* Setting FP from SP.  */
2252         case REG:
2253           if (cfa.reg == (unsigned) REGNO (src))
2254             {
2255               /* Rule 1 */
2256               /* Update the CFA rule wrt SP or FP.  Make sure src is
2257                  relative to the current CFA register.
2258
2259                  We used to require that dest be either SP or FP, but the
2260                  ARM copies SP to a temporary register, and from there to
2261                  FP.  So we just rely on the backends to only set
2262                  RTX_FRAME_RELATED_P on appropriate insns.  */
2263               cfa.reg = REGNO (dest);
2264               cfa_temp.reg = cfa.reg;
2265               cfa_temp.offset = cfa.offset;
2266             }
2267           else
2268             {
2269               /* Saving a register in a register.  */
2270               gcc_assert (!fixed_regs [REGNO (dest)]
2271                           /* For the SPARC and its register window.  */
2272                           || (DWARF_FRAME_REGNUM (REGNO (src))
2273                               == DWARF_FRAME_RETURN_COLUMN));
2274
2275               /* After stack is aligned, we can only save SP in FP
2276                  if drap register is used.  In this case, we have
2277                  to restore stack pointer with the CFA value and we
2278                  don't generate this DWARF information.  */
2279               if (fde
2280                   && fde->stack_realign
2281                   && REGNO (src) == STACK_POINTER_REGNUM)
2282                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2283                             && fde->drap_reg != INVALID_REGNUM
2284                             && cfa.reg != REGNO (src));
2285               else
2286                 queue_reg_save (label, src, dest, 0);
2287             }
2288           break;
2289
2290         case PLUS:
2291         case MINUS:
2292         case LO_SUM:
2293           if (dest == stack_pointer_rtx)
2294             {
2295               /* Rule 2 */
2296               /* Adjusting SP.  */
2297               switch (GET_CODE (XEXP (src, 1)))
2298                 {
2299                 case CONST_INT:
2300                   offset = INTVAL (XEXP (src, 1));
2301                   break;
2302                 case REG:
2303                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2304                               == cfa_temp.reg);
2305                   offset = cfa_temp.offset;
2306                   break;
2307                 default:
2308                   gcc_unreachable ();
2309                 }
2310
2311               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2312                 {
2313                   /* Restoring SP from FP in the epilogue.  */
2314                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2315                   cfa.reg = STACK_POINTER_REGNUM;
2316                 }
2317               else if (GET_CODE (src) == LO_SUM)
2318                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2319                 ;
2320               else
2321                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2322
2323               if (GET_CODE (src) != MINUS)
2324                 offset = -offset;
2325               if (cfa.reg == STACK_POINTER_REGNUM)
2326                 cfa.offset += offset;
2327               if (cfa_store.reg == STACK_POINTER_REGNUM)
2328                 cfa_store.offset += offset;
2329             }
2330           else if (dest == hard_frame_pointer_rtx)
2331             {
2332               /* Rule 3 */
2333               /* Either setting the FP from an offset of the SP,
2334                  or adjusting the FP */
2335               gcc_assert (frame_pointer_needed);
2336
2337               gcc_assert (REG_P (XEXP (src, 0))
2338                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2339                           && CONST_INT_P (XEXP (src, 1)));
2340               offset = INTVAL (XEXP (src, 1));
2341               if (GET_CODE (src) != MINUS)
2342                 offset = -offset;
2343               cfa.offset += offset;
2344               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2345             }
2346           else
2347             {
2348               gcc_assert (GET_CODE (src) != MINUS);
2349
2350               /* Rule 4 */
2351               if (REG_P (XEXP (src, 0))
2352                   && REGNO (XEXP (src, 0)) == cfa.reg
2353                   && CONST_INT_P (XEXP (src, 1)))
2354                 {
2355                   /* Setting a temporary CFA register that will be copied
2356                      into the FP later on.  */
2357                   offset = - INTVAL (XEXP (src, 1));
2358                   cfa.offset += offset;
2359                   cfa.reg = REGNO (dest);
2360                   /* Or used to save regs to the stack.  */
2361                   cfa_temp.reg = cfa.reg;
2362                   cfa_temp.offset = cfa.offset;
2363                 }
2364
2365               /* Rule 5 */
2366               else if (REG_P (XEXP (src, 0))
2367                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2368                        && XEXP (src, 1) == stack_pointer_rtx)
2369                 {
2370                   /* Setting a scratch register that we will use instead
2371                      of SP for saving registers to the stack.  */
2372                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2373                   cfa_store.reg = REGNO (dest);
2374                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2375                 }
2376
2377               /* Rule 9 */
2378               else if (GET_CODE (src) == LO_SUM
2379                        && CONST_INT_P (XEXP (src, 1)))
2380                 {
2381                   cfa_temp.reg = REGNO (dest);
2382                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2383                 }
2384               else
2385                 gcc_unreachable ();
2386             }
2387           break;
2388
2389           /* Rule 6 */
2390         case CONST_INT:
2391           cfa_temp.reg = REGNO (dest);
2392           cfa_temp.offset = INTVAL (src);
2393           break;
2394
2395           /* Rule 7 */
2396         case IOR:
2397           gcc_assert (REG_P (XEXP (src, 0))
2398                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2399                       && CONST_INT_P (XEXP (src, 1)));
2400
2401           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2402             cfa_temp.reg = REGNO (dest);
2403           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2404           break;
2405
2406           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2407              which will fill in all of the bits.  */
2408           /* Rule 8 */
2409         case HIGH:
2410           break;
2411
2412           /* Rule 15 */
2413         case UNSPEC:
2414         case UNSPEC_VOLATILE:
2415           gcc_assert (targetm.dwarf_handle_frame_unspec);
2416           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2417           return;
2418
2419           /* Rule 16 */
2420         case AND:
2421           /* If this AND operation happens on stack pointer in prologue,
2422              we assume the stack is realigned and we extract the
2423              alignment.  */
2424           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2425             {
2426               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2427               fde->stack_realign = 1;
2428               fde->stack_realignment = INTVAL (XEXP (src, 1));
2429               cfa_store.offset = 0;
2430
2431               if (cfa.reg != STACK_POINTER_REGNUM
2432                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2433                 fde->drap_reg = cfa.reg;
2434             }
2435           return;
2436
2437         default:
2438           gcc_unreachable ();
2439         }
2440
2441       def_cfa_1 (label, &cfa);
2442       break;
2443
2444     case MEM:
2445
2446       /* Saving a register to the stack.  Make sure dest is relative to the
2447          CFA register.  */
2448       switch (GET_CODE (XEXP (dest, 0)))
2449         {
2450           /* Rule 10 */
2451           /* With a push.  */
2452         case PRE_MODIFY:
2453           /* We can't handle variable size modifications.  */
2454           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2455                       == CONST_INT);
2456           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2457
2458           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2459                       && cfa_store.reg == STACK_POINTER_REGNUM);
2460
2461           cfa_store.offset += offset;
2462           if (cfa.reg == STACK_POINTER_REGNUM)
2463             cfa.offset = cfa_store.offset;
2464
2465           offset = -cfa_store.offset;
2466           break;
2467
2468           /* Rule 11 */
2469         case PRE_INC:
2470         case PRE_DEC:
2471           offset = GET_MODE_SIZE (GET_MODE (dest));
2472           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2473             offset = -offset;
2474
2475           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2476                        == STACK_POINTER_REGNUM)
2477                       && cfa_store.reg == STACK_POINTER_REGNUM);
2478
2479           cfa_store.offset += offset;
2480
2481           /* Rule 18: If stack is aligned, we will use FP as a
2482              reference to represent the address of the stored
2483              regiser.  */
2484           if (fde
2485               && fde->stack_realign
2486               && src == hard_frame_pointer_rtx)
2487             {
2488               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2489               cfa_store.offset = 0;
2490             }
2491
2492           if (cfa.reg == STACK_POINTER_REGNUM)
2493             cfa.offset = cfa_store.offset;
2494
2495           offset = -cfa_store.offset;
2496           break;
2497
2498           /* Rule 12 */
2499           /* With an offset.  */
2500         case PLUS:
2501         case MINUS:
2502         case LO_SUM:
2503           {
2504             int regno;
2505
2506             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2507                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2508             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2509             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2510               offset = -offset;
2511
2512             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2513
2514             if (cfa_store.reg == (unsigned) regno)
2515               offset -= cfa_store.offset;
2516             else
2517               {
2518                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2519                 offset -= cfa_temp.offset;
2520               }
2521           }
2522           break;
2523
2524           /* Rule 13 */
2525           /* Without an offset.  */
2526         case REG:
2527           {
2528             int regno = REGNO (XEXP (dest, 0));
2529
2530             if (cfa_store.reg == (unsigned) regno)
2531               offset = -cfa_store.offset;
2532             else
2533               {
2534                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2535                 offset = -cfa_temp.offset;
2536               }
2537           }
2538           break;
2539
2540           /* Rule 14 */
2541         case POST_INC:
2542           gcc_assert (cfa_temp.reg
2543                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2544           offset = -cfa_temp.offset;
2545           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2546           break;
2547
2548         default:
2549           gcc_unreachable ();
2550         }
2551
2552         /* Rule 17 */
2553         /* If the source operand of this MEM operation is not a
2554            register, basically the source is return address.  Here
2555            we only care how much stack grew and we don't save it.  */
2556       if (!REG_P (src))
2557         break;
2558
2559       if (REGNO (src) != STACK_POINTER_REGNUM
2560           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2561           && (unsigned) REGNO (src) == cfa.reg)
2562         {
2563           /* We're storing the current CFA reg into the stack.  */
2564
2565           if (cfa.offset == 0)
2566             {
2567               /* Rule 19 */
2568               /* If stack is aligned, putting CFA reg into stack means
2569                  we can no longer use reg + offset to represent CFA.
2570                  Here we use DW_CFA_def_cfa_expression instead.  The
2571                  result of this expression equals to the original CFA
2572                  value.  */
2573               if (fde
2574                   && fde->stack_realign
2575                   && cfa.indirect == 0
2576                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2577                 {
2578                   dw_cfa_location cfa_exp;
2579
2580                   gcc_assert (fde->drap_reg == cfa.reg);
2581
2582                   cfa_exp.indirect = 1;
2583                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2584                   cfa_exp.base_offset = offset;
2585                   cfa_exp.offset = 0;
2586
2587                   fde->drap_reg_saved = 1;
2588
2589                   def_cfa_1 (label, &cfa_exp);
2590                   break;
2591                 }
2592
2593               /* If the source register is exactly the CFA, assume
2594                  we're saving SP like any other register; this happens
2595                  on the ARM.  */
2596               def_cfa_1 (label, &cfa);
2597               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2598               break;
2599             }
2600           else
2601             {
2602               /* Otherwise, we'll need to look in the stack to
2603                  calculate the CFA.  */
2604               rtx x = XEXP (dest, 0);
2605
2606               if (!REG_P (x))
2607                 x = XEXP (x, 0);
2608               gcc_assert (REG_P (x));
2609
2610               cfa.reg = REGNO (x);
2611               cfa.base_offset = offset;
2612               cfa.indirect = 1;
2613               def_cfa_1 (label, &cfa);
2614               break;
2615             }
2616         }
2617
2618       def_cfa_1 (label, &cfa);
2619       {
2620         span = targetm.dwarf_register_span (src);
2621
2622         if (!span)
2623           queue_reg_save (label, src, NULL_RTX, offset);
2624         else
2625           {
2626             /* We have a PARALLEL describing where the contents of SRC
2627                live.  Queue register saves for each piece of the
2628                PARALLEL.  */
2629             int par_index;
2630             int limit;
2631             HOST_WIDE_INT span_offset = offset;
2632
2633             gcc_assert (GET_CODE (span) == PARALLEL);
2634
2635             limit = XVECLEN (span, 0);
2636             for (par_index = 0; par_index < limit; par_index++)
2637               {
2638                 rtx elem = XVECEXP (span, 0, par_index);
2639
2640                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2641                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2642               }
2643           }
2644       }
2645       break;
2646
2647     default:
2648       gcc_unreachable ();
2649     }
2650 }
2651
2652 /* Record call frame debugging information for INSN, which either
2653    sets SP or FP (adjusting how we calculate the frame address) or saves a
2654    register to the stack.  If INSN is NULL_RTX, initialize our state.
2655
2656    If AFTER_P is false, we're being called before the insn is emitted,
2657    otherwise after.  Call instructions get invoked twice.  */
2658
2659 void
2660 dwarf2out_frame_debug (rtx insn, bool after_p)
2661 {
2662   const char *label;
2663   rtx note, n;
2664   bool handled_one = false;
2665
2666   if (insn == NULL_RTX)
2667     {
2668       size_t i;
2669
2670       /* Flush any queued register saves.  */
2671       flush_queued_reg_saves ();
2672
2673       /* Set up state for generating call frame debug info.  */
2674       lookup_cfa (&cfa);
2675       gcc_assert (cfa.reg
2676                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2677
2678       cfa.reg = STACK_POINTER_REGNUM;
2679       cfa_store = cfa;
2680       cfa_temp.reg = -1;
2681       cfa_temp.offset = 0;
2682
2683       for (i = 0; i < num_regs_saved_in_regs; i++)
2684         {
2685           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2686           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2687         }
2688       num_regs_saved_in_regs = 0;
2689
2690       if (barrier_args_size)
2691         {
2692           XDELETEVEC (barrier_args_size);
2693           barrier_args_size = NULL;
2694         }
2695       return;
2696     }
2697
2698   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2699     flush_queued_reg_saves ();
2700
2701   if (!RTX_FRAME_RELATED_P (insn))
2702     {
2703       /* ??? This should be done unconditionally since stack adjustments
2704          matter if the stack pointer is not the CFA register anymore but
2705          is still used to save registers.  */
2706       if (!ACCUMULATE_OUTGOING_ARGS)
2707         dwarf2out_notice_stack_adjust (insn, after_p);
2708       return;
2709     }
2710
2711   label = dwarf2out_cfi_label (false);
2712
2713   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2714     switch (REG_NOTE_KIND (note))
2715       {
2716       case REG_FRAME_RELATED_EXPR:
2717         insn = XEXP (note, 0);
2718         goto found;
2719
2720       case REG_CFA_DEF_CFA:
2721         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2722         handled_one = true;
2723         break;
2724
2725       case REG_CFA_ADJUST_CFA:
2726         n = XEXP (note, 0);
2727         if (n == NULL)
2728           {
2729             n = PATTERN (insn);
2730             if (GET_CODE (n) == PARALLEL)
2731               n = XVECEXP (n, 0, 0);
2732           }
2733         dwarf2out_frame_debug_adjust_cfa (n, label);
2734         handled_one = true;
2735         break;
2736
2737       case REG_CFA_OFFSET:
2738         n = XEXP (note, 0);
2739         if (n == NULL)
2740           n = single_set (insn);
2741         dwarf2out_frame_debug_cfa_offset (n, label);
2742         handled_one = true;
2743         break;
2744
2745       case REG_CFA_REGISTER:
2746         n = XEXP (note, 0);
2747         if (n == NULL)
2748           {
2749             n = PATTERN (insn);
2750             if (GET_CODE (n) == PARALLEL)
2751               n = XVECEXP (n, 0, 0);
2752           }
2753         dwarf2out_frame_debug_cfa_register (n, label);
2754         handled_one = true;
2755         break;
2756
2757       case REG_CFA_RESTORE:
2758         n = XEXP (note, 0);
2759         if (n == NULL)
2760           {
2761             n = PATTERN (insn);
2762             if (GET_CODE (n) == PARALLEL)
2763               n = XVECEXP (n, 0, 0);
2764             n = XEXP (n, 0);
2765           }
2766         dwarf2out_frame_debug_cfa_restore (n, label);
2767         handled_one = true;
2768         break;
2769
2770       case REG_CFA_SET_VDRAP:
2771         n = XEXP (note, 0);
2772         if (REG_P (n))
2773           {
2774             dw_fde_ref fde = current_fde ();
2775             if (fde)
2776               {
2777                 gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2778                 if (REG_P (n))
2779                   fde->vdrap_reg = REGNO (n);
2780               }
2781           }
2782         handled_one = true;
2783         break;
2784
2785       default:
2786         break;
2787       }
2788   if (handled_one)
2789     return;
2790
2791   insn = PATTERN (insn);
2792  found:
2793   dwarf2out_frame_debug_expr (insn, label);
2794
2795   /* Check again.  A parallel can save and update the same register.
2796      We could probably check just once, here, but this is safer than
2797      removing the check above.  */
2798   if (clobbers_queued_reg_save (insn))
2799     flush_queued_reg_saves ();
2800 }
2801
2802 /* Determine if we need to save and restore CFI information around this
2803    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2804    we do need to save/restore, then emit the save now, and insert a
2805    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2806
2807 void
2808 dwarf2out_cfi_begin_epilogue (rtx insn)
2809 {
2810   bool saw_frp = false;
2811   rtx i;
2812
2813   /* Scan forward to the return insn, noticing if there are possible
2814      frame related insns.  */
2815   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2816     {
2817       if (!INSN_P (i))
2818         continue;
2819
2820       /* Look for both regular and sibcalls to end the block.  */
2821       if (returnjump_p (i))
2822         break;
2823       if (CALL_P (i) && SIBLING_CALL_P (i))
2824         break;
2825
2826       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2827         {
2828           int idx;
2829           rtx seq = PATTERN (i);
2830
2831           if (returnjump_p (XVECEXP (seq, 0, 0)))
2832             break;
2833           if (CALL_P (XVECEXP (seq, 0, 0))
2834               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2835             break;
2836
2837           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2838             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2839               saw_frp = true;
2840         }
2841
2842       if (RTX_FRAME_RELATED_P (i))
2843         saw_frp = true;
2844     }
2845
2846   /* If the port doesn't emit epilogue unwind info, we don't need a
2847      save/restore pair.  */
2848   if (!saw_frp)
2849     return;
2850
2851   /* Otherwise, search forward to see if the return insn was the last
2852      basic block of the function.  If so, we don't need save/restore.  */
2853   gcc_assert (i != NULL);
2854   i = next_real_insn (i);
2855   if (i == NULL)
2856     return;
2857
2858   /* Insert the restore before that next real insn in the stream, and before
2859      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2860      properly nested.  This should be after any label or alignment.  This
2861      will be pushed into the CFI stream by the function below.  */
2862   while (1)
2863     {
2864       rtx p = PREV_INSN (i);
2865       if (!NOTE_P (p))
2866         break;
2867       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2868         break;
2869       i = p;
2870     }
2871   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2872
2873   emit_cfa_remember = true;
2874
2875   /* And emulate the state save.  */
2876   gcc_assert (!cfa_remember.in_use);
2877   cfa_remember = cfa;
2878   cfa_remember.in_use = 1;
2879 }
2880
2881 /* A "subroutine" of dwarf2out_cfi_begin_epilogue.  Emit the restore
2882    required.  */
2883
2884 void
2885 dwarf2out_frame_debug_restore_state (void)
2886 {
2887   dw_cfi_ref cfi = new_cfi ();
2888   const char *label = dwarf2out_cfi_label (false);
2889
2890   cfi->dw_cfi_opc = DW_CFA_restore_state;
2891   add_fde_cfi (label, cfi);
2892
2893   gcc_assert (cfa_remember.in_use);
2894   cfa = cfa_remember;
2895   cfa_remember.in_use = 0;
2896 }
2897
2898 #endif
2899
2900 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2901 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2902  (enum dwarf_call_frame_info cfi);
2903
2904 static enum dw_cfi_oprnd_type
2905 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2906 {
2907   switch (cfi)
2908     {
2909     case DW_CFA_nop:
2910     case DW_CFA_GNU_window_save:
2911     case DW_CFA_remember_state:
2912     case DW_CFA_restore_state:
2913       return dw_cfi_oprnd_unused;
2914
2915     case DW_CFA_set_loc:
2916     case DW_CFA_advance_loc1:
2917     case DW_CFA_advance_loc2:
2918     case DW_CFA_advance_loc4:
2919     case DW_CFA_MIPS_advance_loc8:
2920       return dw_cfi_oprnd_addr;
2921
2922     case DW_CFA_offset:
2923     case DW_CFA_offset_extended:
2924     case DW_CFA_def_cfa:
2925     case DW_CFA_offset_extended_sf:
2926     case DW_CFA_def_cfa_sf:
2927     case DW_CFA_restore:
2928     case DW_CFA_restore_extended:
2929     case DW_CFA_undefined:
2930     case DW_CFA_same_value:
2931     case DW_CFA_def_cfa_register:
2932     case DW_CFA_register:
2933     case DW_CFA_expression:
2934       return dw_cfi_oprnd_reg_num;
2935
2936     case DW_CFA_def_cfa_offset:
2937     case DW_CFA_GNU_args_size:
2938     case DW_CFA_def_cfa_offset_sf:
2939       return dw_cfi_oprnd_offset;
2940
2941     case DW_CFA_def_cfa_expression:
2942       return dw_cfi_oprnd_loc;
2943
2944     default:
2945       gcc_unreachable ();
2946     }
2947 }
2948
2949 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2950 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2951  (enum dwarf_call_frame_info cfi);
2952
2953 static enum dw_cfi_oprnd_type
2954 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2955 {
2956   switch (cfi)
2957     {
2958     case DW_CFA_def_cfa:
2959     case DW_CFA_def_cfa_sf:
2960     case DW_CFA_offset:
2961     case DW_CFA_offset_extended_sf:
2962     case DW_CFA_offset_extended:
2963       return dw_cfi_oprnd_offset;
2964
2965     case DW_CFA_register:
2966       return dw_cfi_oprnd_reg_num;
2967
2968     case DW_CFA_expression:
2969       return dw_cfi_oprnd_loc;
2970
2971     default:
2972       return dw_cfi_oprnd_unused;
2973     }
2974 }
2975
2976 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2977
2978 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2979    switch to the data section instead, and write out a synthetic start label
2980    for collect2 the first time around.  */
2981
2982 static void
2983 switch_to_eh_frame_section (bool back)
2984 {
2985   tree label;
2986
2987 #ifdef EH_FRAME_SECTION_NAME
2988   if (eh_frame_section == 0)
2989     {
2990       int flags;
2991
2992       if (EH_TABLES_CAN_BE_READ_ONLY)
2993         {
2994           int fde_encoding;
2995           int per_encoding;
2996           int lsda_encoding;
2997
2998           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2999                                                        /*global=*/0);
3000           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
3001                                                        /*global=*/1);
3002           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
3003                                                         /*global=*/0);
3004           flags = ((! flag_pic
3005                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
3006                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
3007                         && (per_encoding & 0x70) != DW_EH_PE_absptr
3008                         && (per_encoding & 0x70) != DW_EH_PE_aligned
3009                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
3010                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
3011                    ? 0 : SECTION_WRITE);
3012         }
3013       else
3014         flags = SECTION_WRITE;
3015       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
3016     }
3017 #endif
3018
3019   if (eh_frame_section)
3020     switch_to_section (eh_frame_section);
3021   else
3022     {
3023       /* We have no special eh_frame section.  Put the information in
3024          the data section and emit special labels to guide collect2.  */
3025       switch_to_section (data_section);
3026
3027       if (!back)
3028         {
3029           label = get_file_function_name ("F");
3030           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3031           targetm.asm_out.globalize_label (asm_out_file,
3032                                            IDENTIFIER_POINTER (label));
3033           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3034         }
3035     }
3036 }
3037
3038 /* Switch [BACK] to the eh or debug frame table section, depending on
3039    FOR_EH.  */
3040
3041 static void
3042 switch_to_frame_table_section (int for_eh, bool back)
3043 {
3044   if (for_eh)
3045     switch_to_eh_frame_section (back);
3046   else
3047     {
3048       if (!debug_frame_section)
3049         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3050                                            SECTION_DEBUG, NULL);
3051       switch_to_section (debug_frame_section);
3052     }
3053 }
3054
3055 /* Output a Call Frame Information opcode and its operand(s).  */
3056
3057 static void
3058 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3059 {
3060   unsigned long r;
3061   HOST_WIDE_INT off;
3062
3063   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3064     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3065                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3066                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3067                          ((unsigned HOST_WIDE_INT)
3068                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3069   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3070     {
3071       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3072       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3073                            "DW_CFA_offset, column %#lx", r);
3074       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3075       dw2_asm_output_data_uleb128 (off, NULL);
3076     }
3077   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3078     {
3079       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3080       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3081                            "DW_CFA_restore, column %#lx", r);
3082     }
3083   else
3084     {
3085       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3086                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3087
3088       switch (cfi->dw_cfi_opc)
3089         {
3090         case DW_CFA_set_loc:
3091           if (for_eh)
3092             dw2_asm_output_encoded_addr_rtx (
3093                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3094                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3095                 false, NULL);
3096           else
3097             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3098                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3099           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3100           break;
3101
3102         case DW_CFA_advance_loc1:
3103           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3104                                 fde->dw_fde_current_label, NULL);
3105           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3106           break;
3107
3108         case DW_CFA_advance_loc2:
3109           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3110                                 fde->dw_fde_current_label, NULL);
3111           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3112           break;
3113
3114         case DW_CFA_advance_loc4:
3115           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3116                                 fde->dw_fde_current_label, NULL);
3117           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3118           break;
3119
3120         case DW_CFA_MIPS_advance_loc8:
3121           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3122                                 fde->dw_fde_current_label, NULL);
3123           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3124           break;
3125
3126         case DW_CFA_offset_extended:
3127           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3128           dw2_asm_output_data_uleb128 (r, NULL);
3129           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3130           dw2_asm_output_data_uleb128 (off, NULL);
3131           break;
3132
3133         case DW_CFA_def_cfa:
3134           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3135           dw2_asm_output_data_uleb128 (r, NULL);
3136           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3137           break;
3138
3139         case DW_CFA_offset_extended_sf:
3140           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3141           dw2_asm_output_data_uleb128 (r, NULL);
3142           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3143           dw2_asm_output_data_sleb128 (off, NULL);
3144           break;
3145
3146         case DW_CFA_def_cfa_sf:
3147           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3148           dw2_asm_output_data_uleb128 (r, NULL);
3149           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3150           dw2_asm_output_data_sleb128 (off, NULL);
3151           break;
3152
3153         case DW_CFA_restore_extended:
3154         case DW_CFA_undefined:
3155         case DW_CFA_same_value:
3156         case DW_CFA_def_cfa_register:
3157           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3158           dw2_asm_output_data_uleb128 (r, NULL);
3159           break;
3160
3161         case DW_CFA_register:
3162           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3163           dw2_asm_output_data_uleb128 (r, NULL);
3164           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3165           dw2_asm_output_data_uleb128 (r, NULL);
3166           break;
3167
3168         case DW_CFA_def_cfa_offset:
3169         case DW_CFA_GNU_args_size:
3170           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3171           break;
3172
3173         case DW_CFA_def_cfa_offset_sf:
3174           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3175           dw2_asm_output_data_sleb128 (off, NULL);
3176           break;
3177
3178         case DW_CFA_GNU_window_save:
3179           break;
3180
3181         case DW_CFA_def_cfa_expression:
3182         case DW_CFA_expression:
3183           output_cfa_loc (cfi);
3184           break;
3185
3186         case DW_CFA_GNU_negative_offset_extended:
3187           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3188           gcc_unreachable ();
3189
3190         default:
3191           break;
3192         }
3193     }
3194 }
3195
3196 /* Similar, but do it via assembler directives instead.  */
3197
3198 static void
3199 output_cfi_directive (dw_cfi_ref cfi)
3200 {
3201   unsigned long r, r2;
3202
3203   switch (cfi->dw_cfi_opc)
3204     {
3205     case DW_CFA_advance_loc:
3206     case DW_CFA_advance_loc1:
3207     case DW_CFA_advance_loc2:
3208     case DW_CFA_advance_loc4:
3209     case DW_CFA_MIPS_advance_loc8:
3210     case DW_CFA_set_loc:
3211       /* Should only be created by add_fde_cfi in a code path not
3212          followed when emitting via directives.  The assembler is
3213          going to take care of this for us.  */
3214       gcc_unreachable ();
3215
3216     case DW_CFA_offset:
3217     case DW_CFA_offset_extended:
3218     case DW_CFA_offset_extended_sf:
3219       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3220       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3221                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3222       break;
3223
3224     case DW_CFA_restore:
3225     case DW_CFA_restore_extended:
3226       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3227       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3228       break;
3229
3230     case DW_CFA_undefined:
3231       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3232       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3233       break;
3234
3235     case DW_CFA_same_value:
3236       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3237       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3238       break;
3239
3240     case DW_CFA_def_cfa:
3241     case DW_CFA_def_cfa_sf:
3242       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3243       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3244                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3245       break;
3246
3247     case DW_CFA_def_cfa_register:
3248       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3249       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3250       break;
3251
3252     case DW_CFA_register:
3253       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3254       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3255       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3256       break;
3257
3258     case DW_CFA_def_cfa_offset:
3259     case DW_CFA_def_cfa_offset_sf:
3260       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3261                HOST_WIDE_INT_PRINT_DEC"\n",
3262                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3263       break;
3264
3265     case DW_CFA_remember_state:
3266       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3267       break;
3268     case DW_CFA_restore_state:
3269       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3270       break;
3271
3272     case DW_CFA_GNU_args_size:
3273       fprintf (asm_out_file, "\t.cfi_escape %#x,", DW_CFA_GNU_args_size);
3274       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3275       if (flag_debug_asm)
3276         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3277                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3278       fputc ('\n', asm_out_file);
3279       break;
3280
3281     case DW_CFA_GNU_window_save:
3282       fprintf (asm_out_file, "\t.cfi_window_save\n");
3283       break;
3284
3285     case DW_CFA_def_cfa_expression:
3286     case DW_CFA_expression:
3287       fprintf (asm_out_file, "\t.cfi_escape %#x,", cfi->dw_cfi_opc);
3288       output_cfa_loc_raw (cfi);
3289       fputc ('\n', asm_out_file);
3290       break;
3291
3292     default:
3293       gcc_unreachable ();
3294     }
3295 }
3296
3297 DEF_VEC_P (dw_cfi_ref);
3298 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3299
3300 /* Output CFIs to bring current FDE to the same state as after executing
3301    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3302    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3303    other arguments to pass to output_cfi.  */
3304
3305 static void
3306 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3307 {
3308   struct dw_cfi_struct cfi_buf;
3309   dw_cfi_ref cfi2;
3310   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3311   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3312   unsigned int len, idx;
3313
3314   for (;; cfi = cfi->dw_cfi_next)
3315     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3316       {
3317       case DW_CFA_advance_loc:
3318       case DW_CFA_advance_loc1:
3319       case DW_CFA_advance_loc2:
3320       case DW_CFA_advance_loc4:
3321       case DW_CFA_MIPS_advance_loc8:
3322       case DW_CFA_set_loc:
3323         /* All advances should be ignored.  */
3324         break;
3325       case DW_CFA_remember_state:
3326         {
3327           dw_cfi_ref args_size = cfi_args_size;
3328
3329           /* Skip everything between .cfi_remember_state and
3330              .cfi_restore_state.  */
3331           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3332             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3333               break;
3334             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3335               args_size = cfi2;
3336             else
3337               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3338
3339           if (cfi2 == NULL)
3340             goto flush_all;
3341           else
3342             {
3343               cfi = cfi2;
3344               cfi_args_size = args_size;
3345             }
3346           break;
3347         }
3348       case DW_CFA_GNU_args_size:
3349         cfi_args_size = cfi;
3350         break;
3351       case DW_CFA_GNU_window_save:
3352         goto flush_all;
3353       case DW_CFA_offset:
3354       case DW_CFA_offset_extended:
3355       case DW_CFA_offset_extended_sf:
3356       case DW_CFA_restore:
3357       case DW_CFA_restore_extended:
3358       case DW_CFA_undefined:
3359       case DW_CFA_same_value:
3360       case DW_CFA_register:
3361       case DW_CFA_val_offset:
3362       case DW_CFA_val_offset_sf:
3363       case DW_CFA_expression:
3364       case DW_CFA_val_expression:
3365       case DW_CFA_GNU_negative_offset_extended:
3366         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3367           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3368                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3369         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3370         break;
3371       case DW_CFA_def_cfa:
3372       case DW_CFA_def_cfa_sf:
3373       case DW_CFA_def_cfa_expression:
3374         cfi_cfa = cfi;
3375         cfi_cfa_offset = cfi;
3376         break;
3377       case DW_CFA_def_cfa_register:
3378         cfi_cfa = cfi;
3379         break;
3380       case DW_CFA_def_cfa_offset:
3381       case DW_CFA_def_cfa_offset_sf:
3382         cfi_cfa_offset = cfi;
3383         break;
3384       case DW_CFA_nop:
3385         gcc_assert (cfi == NULL);
3386       flush_all:
3387         len = VEC_length (dw_cfi_ref, regs);
3388         for (idx = 0; idx < len; idx++)
3389           {
3390             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3391             if (cfi2 != NULL
3392                 && cfi2->dw_cfi_opc != DW_CFA_restore
3393                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3394               {
3395                 if (do_cfi_asm)
3396                   output_cfi_directive (cfi2);
3397                 else
3398                   output_cfi (cfi2, fde, for_eh);
3399               }
3400           }
3401         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3402           {
3403             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3404             cfi_buf = *cfi_cfa;
3405             switch (cfi_cfa_offset->dw_cfi_opc)
3406               {
3407               case DW_CFA_def_cfa_offset:
3408                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3409                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3410                 break;
3411               case DW_CFA_def_cfa_offset_sf:
3412                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3413                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3414                 break;
3415               case DW_CFA_def_cfa:
3416               case DW_CFA_def_cfa_sf:
3417                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3418                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3419                 break;
3420               default:
3421                 gcc_unreachable ();
3422               }
3423             cfi_cfa = &cfi_buf;
3424           }
3425         else if (cfi_cfa_offset)
3426           cfi_cfa = cfi_cfa_offset;
3427         if (cfi_cfa)
3428           {
3429             if (do_cfi_asm)
3430               output_cfi_directive (cfi_cfa);
3431             else
3432               output_cfi (cfi_cfa, fde, for_eh);
3433           }
3434         cfi_cfa = NULL;
3435         cfi_cfa_offset = NULL;
3436         if (cfi_args_size
3437             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3438           {
3439             if (do_cfi_asm)
3440               output_cfi_directive (cfi_args_size);
3441             else
3442               output_cfi (cfi_args_size, fde, for_eh);
3443           }
3444         cfi_args_size = NULL;
3445         if (cfi == NULL)
3446           {
3447             VEC_free (dw_cfi_ref, heap, regs);
3448             return;
3449           }
3450         else if (do_cfi_asm)
3451           output_cfi_directive (cfi);
3452         else
3453           output_cfi (cfi, fde, for_eh);
3454         break;
3455       default:
3456         gcc_unreachable ();
3457     }
3458 }
3459
3460 /* Output one FDE.  */
3461
3462 static void
3463 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3464             char *section_start_label, int fde_encoding, char *augmentation,
3465             bool any_lsda_needed, int lsda_encoding)
3466 {
3467   const char *begin, *end;
3468   static unsigned int j;
3469   char l1[20], l2[20];
3470   dw_cfi_ref cfi;
3471
3472   targetm.asm_out.emit_unwind_label (asm_out_file, fde->decl, for_eh,
3473                                      /* empty */ 0);
3474   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3475                                   for_eh + j);
3476   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3477   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3478   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3479     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3480                          " indicating 64-bit DWARF extension");
3481   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3482                         "FDE Length");
3483   ASM_OUTPUT_LABEL (asm_out_file, l1);
3484
3485   if (for_eh)
3486     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3487   else
3488     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3489                            debug_frame_section, "FDE CIE offset");
3490
3491   if (!fde->dw_fde_switched_sections)
3492     {
3493       begin = fde->dw_fde_begin;
3494       end = fde->dw_fde_end;
3495     }
3496   else
3497     {
3498       /* For the first section, prefer dw_fde_begin over
3499          dw_fde_{hot,cold}_section_label, as the latter
3500          might be separated from the real start of the
3501          function by alignment padding.  */
3502       if (!second)
3503         begin = fde->dw_fde_begin;
3504       else if (fde->dw_fde_switched_cold_to_hot)
3505         begin = fde->dw_fde_hot_section_label;
3506       else
3507         begin = fde->dw_fde_unlikely_section_label;
3508       if (second ^ fde->dw_fde_switched_cold_to_hot)
3509         end = fde->dw_fde_unlikely_section_end_label;
3510       else
3511         end = fde->dw_fde_hot_section_end_label;
3512     }
3513
3514   if (for_eh)
3515     {
3516       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3517       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3518       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3519                                        "FDE initial location");
3520       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3521                             end, begin, "FDE address range");
3522     }
3523   else
3524     {
3525       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3526       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3527     }
3528
3529   if (augmentation[0])
3530     {
3531       if (any_lsda_needed)
3532         {
3533           int size = size_of_encoded_value (lsda_encoding);
3534
3535           if (lsda_encoding == DW_EH_PE_aligned)
3536             {
3537               int offset = (  4         /* Length */
3538                             + 4         /* CIE offset */
3539                             + 2 * size_of_encoded_value (fde_encoding)
3540                             + 1         /* Augmentation size */ );
3541               int pad = -offset & (PTR_SIZE - 1);
3542
3543               size += pad;
3544               gcc_assert (size_of_uleb128 (size) == 1);
3545             }
3546
3547           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3548
3549           if (fde->uses_eh_lsda)
3550             {
3551               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3552                                            fde->funcdef_number);
3553               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3554                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3555                                                false,
3556                                                "Language Specific Data Area");
3557             }
3558           else
3559             {
3560               if (lsda_encoding == DW_EH_PE_aligned)
3561                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3562               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3563                                    "Language Specific Data Area (none)");
3564             }
3565         }
3566       else
3567         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3568     }
3569
3570   /* Loop through the Call Frame Instructions associated with
3571      this FDE.  */
3572   fde->dw_fde_current_label = begin;
3573   if (!fde->dw_fde_switched_sections)
3574     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3575       output_cfi (cfi, fde, for_eh);
3576   else if (!second)
3577     {
3578       if (fde->dw_fde_switch_cfi)
3579         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3580           {
3581             output_cfi (cfi, fde, for_eh);
3582             if (cfi == fde->dw_fde_switch_cfi)
3583               break;
3584           }
3585     }
3586   else
3587     {
3588       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3589
3590       if (fde->dw_fde_switch_cfi)
3591         {
3592           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3593           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3594           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3595           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3596         }
3597       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3598         output_cfi (cfi, fde, for_eh);
3599     }
3600
3601   /* If we are to emit a ref/link from function bodies to their frame tables,
3602      do it now.  This is typically performed to make sure that tables
3603      associated with functions are dragged with them and not discarded in
3604      garbage collecting links. We need to do this on a per function basis to
3605      cope with -ffunction-sections.  */
3606
3607 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3608   /* Switch to the function section, emit the ref to the tables, and
3609      switch *back* into the table section.  */
3610   switch_to_section (function_section (fde->decl));
3611   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3612   switch_to_frame_table_section (for_eh, true);
3613 #endif
3614
3615   /* Pad the FDE out to an address sized boundary.  */
3616   ASM_OUTPUT_ALIGN (asm_out_file,
3617                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3618   ASM_OUTPUT_LABEL (asm_out_file, l2);
3619
3620   j += 2;
3621 }
3622
3623 /* Return true if frame description entry FDE is needed for EH.  */
3624
3625 static bool
3626 fde_needed_for_eh_p (dw_fde_ref fde)
3627 {
3628   if (flag_asynchronous_unwind_tables)
3629     return true;
3630
3631   if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde->decl))
3632     return true;
3633
3634   if (fde->uses_eh_lsda)
3635     return true;
3636
3637   /* If exceptions are enabled, we have collected nothrow info.  */
3638   if (flag_exceptions && (fde->all_throwers_are_sibcalls || fde->nothrow))
3639     return false;
3640
3641   return true;
3642 }
3643
3644 /* Output the call frame information used to record information
3645    that relates to calculating the frame pointer, and records the
3646    location of saved registers.  */
3647
3648 static void
3649 output_call_frame_info (int for_eh)
3650 {
3651   unsigned int i;
3652   dw_fde_ref fde;
3653   dw_cfi_ref cfi;
3654   char l1[20], l2[20], section_start_label[20];
3655   bool any_lsda_needed = false;
3656   char augmentation[6];
3657   int augmentation_size;
3658   int fde_encoding = DW_EH_PE_absptr;
3659   int per_encoding = DW_EH_PE_absptr;
3660   int lsda_encoding = DW_EH_PE_absptr;
3661   int return_reg;
3662   rtx personality = NULL;
3663   int dw_cie_version;
3664
3665   /* Don't emit a CIE if there won't be any FDEs.  */
3666   if (fde_table_in_use == 0)
3667     return;
3668
3669   /* Nothing to do if the assembler's doing it all.  */
3670   if (dwarf2out_do_cfi_asm ())
3671     return;
3672
3673   /* If we don't have any functions we'll want to unwind out of, don't emit
3674      any EH unwind information.  If we make FDEs linkonce, we may have to
3675      emit an empty label for an FDE that wouldn't otherwise be emitted.  We
3676      want to avoid having an FDE kept around when the function it refers to
3677      is discarded.  Example where this matters: a primary function template
3678      in C++ requires EH information, an explicit specialization doesn't.  */
3679   if (for_eh)
3680     {
3681       bool any_eh_needed = false;
3682
3683       for (i = 0; i < fde_table_in_use; i++)
3684         if (fde_table[i].uses_eh_lsda)
3685           any_eh_needed = any_lsda_needed = true;
3686         else if (fde_needed_for_eh_p (&fde_table[i]))
3687           any_eh_needed = true;
3688         else if (TARGET_USES_WEAK_UNWIND_INFO)
3689           targetm.asm_out.emit_unwind_label (asm_out_file, fde_table[i].decl,
3690                                              1, 1);
3691
3692       if (!any_eh_needed)
3693         return;
3694     }
3695
3696   /* We're going to be generating comments, so turn on app.  */
3697   if (flag_debug_asm)
3698     app_enable ();
3699
3700   /* Switch to the proper frame section, first time.  */
3701   switch_to_frame_table_section (for_eh, false);
3702
3703   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3704   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3705
3706   /* Output the CIE.  */
3707   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3708   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3709   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3710     dw2_asm_output_data (4, 0xffffffff,
3711       "Initial length escape value indicating 64-bit DWARF extension");
3712   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3713                         "Length of Common Information Entry");
3714   ASM_OUTPUT_LABEL (asm_out_file, l1);
3715
3716   /* Now that the CIE pointer is PC-relative for EH,
3717      use 0 to identify the CIE.  */
3718   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3719                        (for_eh ? 0 : DWARF_CIE_ID),
3720                        "CIE Identifier Tag");
3721
3722   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3723      use CIE version 1, unless that would produce incorrect results
3724      due to overflowing the return register column.  */
3725   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3726   dw_cie_version = 1;
3727   if (return_reg >= 256 || dwarf_version > 2)
3728     dw_cie_version = 3;
3729   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3730
3731   augmentation[0] = 0;
3732   augmentation_size = 0;
3733
3734   personality = current_unit_personality;
3735   if (for_eh)
3736     {
3737       char *p;
3738
3739       /* Augmentation:
3740          z      Indicates that a uleb128 is present to size the
3741                 augmentation section.
3742          L      Indicates the encoding (and thus presence) of
3743                 an LSDA pointer in the FDE augmentation.
3744          R      Indicates a non-default pointer encoding for
3745                 FDE code pointers.
3746          P      Indicates the presence of an encoding + language
3747                 personality routine in the CIE augmentation.  */
3748
3749       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3750       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3751       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3752
3753       p = augmentation + 1;
3754       if (personality)
3755         {
3756           *p++ = 'P';
3757           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3758           assemble_external_libcall (personality);
3759         }
3760       if (any_lsda_needed)
3761         {
3762           *p++ = 'L';
3763           augmentation_size += 1;
3764         }
3765       if (fde_encoding != DW_EH_PE_absptr)
3766         {
3767           *p++ = 'R';
3768           augmentation_size += 1;
3769         }
3770       if (p > augmentation + 1)
3771         {
3772           augmentation[0] = 'z';
3773           *p = '\0';
3774         }
3775
3776       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3777       if (personality && per_encoding == DW_EH_PE_aligned)
3778         {
3779           int offset = (  4             /* Length */
3780                         + 4             /* CIE Id */
3781                         + 1             /* CIE version */
3782                         + strlen (augmentation) + 1     /* Augmentation */
3783                         + size_of_uleb128 (1)           /* Code alignment */
3784                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3785                         + 1             /* RA column */
3786                         + 1             /* Augmentation size */
3787                         + 1             /* Personality encoding */ );
3788           int pad = -offset & (PTR_SIZE - 1);
3789
3790           augmentation_size += pad;
3791
3792           /* Augmentations should be small, so there's scarce need to
3793              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3794           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3795         }
3796     }
3797
3798   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3799   if (dw_cie_version >= 4)
3800     {
3801       dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "CIE Address Size");
3802       dw2_asm_output_data (1, 0, "CIE Segment Size");
3803     }
3804   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3805   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3806                                "CIE Data Alignment Factor");
3807
3808   if (dw_cie_version == 1)
3809     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3810   else
3811     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3812
3813   if (augmentation[0])
3814     {
3815       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3816       if (personality)
3817         {
3818           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3819                                eh_data_format_name (per_encoding));
3820           dw2_asm_output_encoded_addr_rtx (per_encoding,
3821                                            personality,
3822                                            true, NULL);
3823         }
3824
3825       if (any_lsda_needed)
3826         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3827                              eh_data_format_name (lsda_encoding));
3828
3829       if (fde_encoding != DW_EH_PE_absptr)
3830         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3831                              eh_data_format_name (fde_encoding));
3832     }
3833
3834   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3835     output_cfi (cfi, NULL, for_eh);
3836
3837   /* Pad the CIE out to an address sized boundary.  */
3838   ASM_OUTPUT_ALIGN (asm_out_file,
3839                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3840   ASM_OUTPUT_LABEL (asm_out_file, l2);
3841
3842   /* Loop through all of the FDE's.  */
3843   for (i = 0; i < fde_table_in_use; i++)
3844     {
3845       unsigned int k;
3846       fde = &fde_table[i];
3847
3848       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3849       if (for_eh && !fde_needed_for_eh_p (fde))
3850         continue;
3851
3852       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3853         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3854                     augmentation, any_lsda_needed, lsda_encoding);
3855     }
3856
3857   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3858     dw2_asm_output_data (4, 0, "End of Table");
3859 #ifdef MIPS_DEBUGGING_INFO
3860   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3861      get a value of 0.  Putting .align 0 after the label fixes it.  */
3862   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3863 #endif
3864
3865   /* Turn off app to make assembly quicker.  */
3866   if (flag_debug_asm)
3867     app_disable ();
3868 }
3869
3870 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3871
3872 static void
3873 dwarf2out_do_cfi_startproc (bool second)
3874 {
3875   int enc;
3876   rtx ref;
3877   rtx personality = get_personality_function (current_function_decl);
3878
3879   fprintf (asm_out_file, "\t.cfi_startproc\n");
3880
3881   if (personality)
3882     {
3883       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3884       ref = personality;
3885
3886       /* ??? The GAS support isn't entirely consistent.  We have to
3887          handle indirect support ourselves, but PC-relative is done
3888          in the assembler.  Further, the assembler can't handle any
3889          of the weirder relocation types.  */
3890       if (enc & DW_EH_PE_indirect)
3891         ref = dw2_force_const_mem (ref, true);
3892
3893       fprintf (asm_out_file, "\t.cfi_personality %#x,", enc);
3894       output_addr_const (asm_out_file, ref);
3895       fputc ('\n', asm_out_file);
3896     }
3897
3898   if (crtl->uses_eh_lsda)
3899     {
3900       char lab[20];
3901
3902       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3903       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3904                                    current_function_funcdef_no);
3905       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3906       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3907
3908       if (enc & DW_EH_PE_indirect)
3909         ref = dw2_force_const_mem (ref, true);
3910
3911       fprintf (asm_out_file, "\t.cfi_lsda %#x,", enc);
3912       output_addr_const (asm_out_file, ref);
3913       fputc ('\n', asm_out_file);
3914     }
3915 }
3916
3917 /* Output a marker (i.e. a label) for the beginning of a function, before
3918    the prologue.  */
3919
3920 void
3921 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3922                           const char *file ATTRIBUTE_UNUSED)
3923 {
3924   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3925   char * dup_label;
3926   dw_fde_ref fde;
3927   section *fnsec;
3928
3929   current_function_func_begin_label = NULL;
3930
3931 #ifdef TARGET_UNWIND_INFO
3932   /* ??? current_function_func_begin_label is also used by except.c
3933      for call-site information.  We must emit this label if it might
3934      be used.  */
3935   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3936       && ! dwarf2out_do_frame ())
3937     return;
3938 #else
3939   if (! dwarf2out_do_frame ())
3940     return;
3941 #endif
3942
3943   fnsec = function_section (current_function_decl);
3944   switch_to_section (fnsec);
3945   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3946                                current_function_funcdef_no);
3947   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3948                           current_function_funcdef_no);
3949   dup_label = xstrdup (label);
3950   current_function_func_begin_label = dup_label;
3951
3952 #ifdef TARGET_UNWIND_INFO
3953   /* We can elide the fde allocation if we're not emitting debug info.  */
3954   if (! dwarf2out_do_frame ())
3955     return;
3956 #endif
3957
3958   /* Expand the fde table if necessary.  */
3959   if (fde_table_in_use == fde_table_allocated)
3960     {
3961       fde_table_allocated += FDE_TABLE_INCREMENT;
3962       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3963       memset (fde_table + fde_table_in_use, 0,
3964               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3965     }
3966
3967   /* Record the FDE associated with this function.  */
3968   current_funcdef_fde = fde_table_in_use;
3969
3970   /* Add the new FDE at the end of the fde_table.  */
3971   fde = &fde_table[fde_table_in_use++];
3972   fde->decl = current_function_decl;
3973   fde->dw_fde_begin = dup_label;
3974   fde->dw_fde_current_label = dup_label;
3975   fde->dw_fde_hot_section_label = NULL;
3976   fde->dw_fde_hot_section_end_label = NULL;
3977   fde->dw_fde_unlikely_section_label = NULL;
3978   fde->dw_fde_unlikely_section_end_label = NULL;
3979   fde->dw_fde_switched_sections = 0;
3980   fde->dw_fde_switched_cold_to_hot = 0;
3981   fde->dw_fde_end = NULL;
3982   fde->dw_fde_vms_end_prologue = NULL;
3983   fde->dw_fde_vms_begin_epilogue = NULL;
3984   fde->dw_fde_cfi = NULL;
3985   fde->dw_fde_switch_cfi = NULL;
3986   fde->funcdef_number = current_function_funcdef_no;
3987   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3988   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3989   fde->nothrow = crtl->nothrow;
3990   fde->drap_reg = INVALID_REGNUM;
3991   fde->vdrap_reg = INVALID_REGNUM;
3992   if (flag_reorder_blocks_and_partition)
3993     {
3994       section *unlikelysec;
3995       if (first_function_block_is_cold)
3996         fde->in_std_section = 1;
3997       else
3998         fde->in_std_section
3999           = (fnsec == text_section
4000              || (cold_text_section && fnsec == cold_text_section));
4001       unlikelysec = unlikely_text_section ();
4002       fde->cold_in_std_section
4003         = (unlikelysec == text_section
4004            || (cold_text_section && unlikelysec == cold_text_section));
4005     }
4006   else
4007     {
4008       fde->in_std_section
4009         = (fnsec == text_section
4010            || (cold_text_section && fnsec == cold_text_section));
4011       fde->cold_in_std_section = 0;
4012     }
4013
4014   args_size = old_args_size = 0;
4015
4016   /* We only want to output line number information for the genuine dwarf2
4017      prologue case, not the eh frame case.  */
4018 #ifdef DWARF2_DEBUGGING_INFO
4019   if (file)
4020     dwarf2out_source_line (line, file, 0, true);
4021 #endif
4022
4023   if (dwarf2out_do_cfi_asm ())
4024     dwarf2out_do_cfi_startproc (false);
4025   else
4026     {
4027       rtx personality = get_personality_function (current_function_decl);
4028       if (!current_unit_personality)
4029         current_unit_personality = personality;
4030
4031       /* We cannot keep a current personality per function as without CFI
4032          asm, at the point where we emit the CFI data, there is no current
4033          function anymore.  */
4034       if (personality && current_unit_personality != personality)
4035         sorry ("multiple EH personalities are supported only with assemblers "
4036                "supporting .cfi_personality directive");
4037     }
4038 }
4039
4040 /* Output a marker (i.e. a label) for the end of the generated code
4041    for a function prologue.  This gets called *after* the prologue code has
4042    been generated.  */
4043
4044 void
4045 dwarf2out_vms_end_prologue (unsigned int line ATTRIBUTE_UNUSED,
4046                         const char *file ATTRIBUTE_UNUSED)
4047 {
4048   dw_fde_ref fde;
4049   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4050
4051   /* Output a label to mark the endpoint of the code generated for this
4052      function.  */
4053   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
4054                                current_function_funcdef_no);
4055   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, PROLOGUE_END_LABEL,
4056                           current_function_funcdef_no);
4057   fde = &fde_table[fde_table_in_use - 1];
4058   fde->dw_fde_vms_end_prologue = xstrdup (label);
4059 }
4060
4061 /* Output a marker (i.e. a label) for the beginning of the generated code
4062    for a function epilogue.  This gets called *before* the prologue code has
4063    been generated.  */
4064
4065 void
4066 dwarf2out_vms_begin_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4067                           const char *file ATTRIBUTE_UNUSED)
4068 {
4069   dw_fde_ref fde;
4070   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4071
4072   fde = &fde_table[fde_table_in_use - 1];
4073   if (fde->dw_fde_vms_begin_epilogue)
4074     return;
4075
4076   /* Output a label to mark the endpoint of the code generated for this
4077      function.  */
4078   ASM_GENERATE_INTERNAL_LABEL (label, EPILOGUE_BEGIN_LABEL,
4079                                current_function_funcdef_no);
4080   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, EPILOGUE_BEGIN_LABEL,
4081                           current_function_funcdef_no);
4082   fde->dw_fde_vms_begin_epilogue = xstrdup (label);
4083 }
4084
4085 /* Output a marker (i.e. a label) for the absolute end of the generated code
4086    for a function definition.  This gets called *after* the epilogue code has
4087    been generated.  */
4088
4089 void
4090 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4091                         const char *file ATTRIBUTE_UNUSED)
4092 {
4093   dw_fde_ref fde;
4094   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4095
4096 #ifdef DWARF2_DEBUGGING_INFO
4097   last_var_location_insn = NULL_RTX;
4098 #endif
4099
4100   if (dwarf2out_do_cfi_asm ())
4101     fprintf (asm_out_file, "\t.cfi_endproc\n");
4102
4103   /* Output a label to mark the endpoint of the code generated for this
4104      function.  */
4105   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4106                                current_function_funcdef_no);
4107   ASM_OUTPUT_LABEL (asm_out_file, label);
4108   fde = current_fde ();
4109   gcc_assert (fde != NULL);
4110   fde->dw_fde_end = xstrdup (label);
4111 }
4112
4113 void
4114 dwarf2out_frame_init (void)
4115 {
4116   /* Allocate the initial hunk of the fde_table.  */
4117   fde_table = ggc_alloc_cleared_vec_dw_fde_node (FDE_TABLE_INCREMENT);
4118   fde_table_allocated = FDE_TABLE_INCREMENT;
4119   fde_table_in_use = 0;
4120
4121   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4122      sake of lookup_cfa.  */
4123
4124   /* On entry, the Canonical Frame Address is at SP.  */
4125   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4126
4127 #ifdef DWARF2_UNWIND_INFO
4128   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4129     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4130 #endif
4131 }
4132
4133 void
4134 dwarf2out_frame_finish (void)
4135 {
4136   /* Output call frame information.  */
4137   if (DWARF2_FRAME_INFO)
4138     output_call_frame_info (0);
4139
4140 #ifndef TARGET_UNWIND_INFO
4141   /* Output another copy for the unwinder.  */
4142   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4143     output_call_frame_info (1);
4144 #endif
4145 }
4146
4147 /* Note that the current function section is being used for code.  */
4148
4149 static void
4150 dwarf2out_note_section_used (void)
4151 {
4152   section *sec = current_function_section ();
4153   if (sec == text_section)
4154     text_section_used = true;
4155   else if (sec == cold_text_section)
4156     cold_text_section_used = true;
4157 }
4158
4159 void
4160 dwarf2out_switch_text_section (void)
4161 {
4162   dw_fde_ref fde = current_fde ();
4163
4164   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4165
4166   fde->dw_fde_switched_sections = 1;
4167   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4168
4169   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4170   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4171   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4172   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4173   have_multiple_function_sections = true;
4174
4175   /* Reset the current label on switching text sections, so that we
4176      don't attempt to advance_loc4 between labels in different sections.  */
4177   fde->dw_fde_current_label = NULL;
4178
4179   /* There is no need to mark used sections when not debugging.  */
4180   if (cold_text_section != NULL)
4181     dwarf2out_note_section_used ();
4182
4183   if (dwarf2out_do_cfi_asm ())
4184     fprintf (asm_out_file, "\t.cfi_endproc\n");
4185
4186   /* Now do the real section switch.  */
4187   switch_to_section (current_function_section ());
4188
4189   if (dwarf2out_do_cfi_asm ())
4190     {
4191       dwarf2out_do_cfi_startproc (true);
4192       /* As this is a different FDE, insert all current CFI instructions
4193          again.  */
4194       output_cfis (fde->dw_fde_cfi, true, fde, true);
4195     }
4196   else
4197     {
4198       dw_cfi_ref cfi = fde->dw_fde_cfi;
4199
4200       cfi = fde->dw_fde_cfi;
4201       if (cfi)
4202         while (cfi->dw_cfi_next != NULL)
4203           cfi = cfi->dw_cfi_next;
4204       fde->dw_fde_switch_cfi = cfi;
4205     }
4206 }
4207 #endif
4208 \f
4209 /* And now, the subset of the debugging information support code necessary
4210    for emitting location expressions.  */
4211
4212 /* Data about a single source file.  */
4213 struct GTY(()) dwarf_file_data {
4214   const char * filename;
4215   int emitted_number;
4216 };
4217
4218 typedef struct dw_val_struct *dw_val_ref;
4219 typedef struct die_struct *dw_die_ref;
4220 typedef const struct die_struct *const_dw_die_ref;
4221 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4222 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4223
4224 typedef struct GTY(()) deferred_locations_struct
4225 {
4226   tree variable;
4227   dw_die_ref die;
4228 } deferred_locations;
4229
4230 DEF_VEC_O(deferred_locations);
4231 DEF_VEC_ALLOC_O(deferred_locations,gc);
4232
4233 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4234
4235 DEF_VEC_P(dw_die_ref);
4236 DEF_VEC_ALLOC_P(dw_die_ref,heap);
4237
4238 /* Each DIE may have a series of attribute/value pairs.  Values
4239    can take on several forms.  The forms that are used in this
4240    implementation are listed below.  */
4241
4242 enum dw_val_class
4243 {
4244   dw_val_class_addr,
4245   dw_val_class_offset,
4246   dw_val_class_loc,
4247   dw_val_class_loc_list,
4248   dw_val_class_range_list,
4249   dw_val_class_const,
4250   dw_val_class_unsigned_const,
4251   dw_val_class_const_double,
4252   dw_val_class_vec,
4253   dw_val_class_flag,
4254   dw_val_class_die_ref,
4255   dw_val_class_fde_ref,
4256   dw_val_class_lbl_id,
4257   dw_val_class_lineptr,
4258   dw_val_class_str,
4259   dw_val_class_macptr,
4260   dw_val_class_file,
4261   dw_val_class_data8,
4262   dw_val_class_vms_delta
4263 };
4264
4265 /* Describe a floating point constant value, or a vector constant value.  */
4266
4267 typedef struct GTY(()) dw_vec_struct {
4268   unsigned char * GTY((length ("%h.length"))) array;
4269   unsigned length;
4270   unsigned elt_size;
4271 }
4272 dw_vec_const;
4273
4274 /* The dw_val_node describes an attribute's value, as it is
4275    represented internally.  */
4276
4277 typedef struct GTY(()) dw_val_struct {
4278   enum dw_val_class val_class;
4279   union dw_val_struct_union
4280     {
4281       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4282       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4283       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4284       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4285       HOST_WIDE_INT GTY ((default)) val_int;
4286       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4287       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4288       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4289       struct dw_val_die_union
4290         {
4291           dw_die_ref die;
4292           int external;
4293         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4294       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4295       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4296       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4297       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4298       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4299       unsigned char GTY ((tag ("dw_val_class_data8"))) val_data8[8];
4300       struct dw_val_vms_delta_union
4301         {
4302           char * lbl1;
4303           char * lbl2;
4304         } GTY ((tag ("dw_val_class_vms_delta"))) val_vms_delta;
4305     }
4306   GTY ((desc ("%1.val_class"))) v;
4307 }
4308 dw_val_node;
4309
4310 /* Locations in memory are described using a sequence of stack machine
4311    operations.  */
4312
4313 typedef struct GTY(()) dw_loc_descr_struct {
4314   dw_loc_descr_ref dw_loc_next;
4315   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4316   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4317      from DW_OP_addr with a dtp-relative symbol relocation.  */
4318   unsigned int dtprel : 1;
4319   int dw_loc_addr;
4320   dw_val_node dw_loc_oprnd1;
4321   dw_val_node dw_loc_oprnd2;
4322 }
4323 dw_loc_descr_node;
4324
4325 /* Location lists are ranges + location descriptions for that range,
4326    so you can track variables that are in different places over
4327    their entire life.  */
4328 typedef struct GTY(()) dw_loc_list_struct {
4329   dw_loc_list_ref dw_loc_next;
4330   const char *begin; /* Label for begin address of range */
4331   const char *end;  /* Label for end address of range */
4332   char *ll_symbol; /* Label for beginning of location list.
4333                       Only on head of list */
4334   const char *section; /* Section this loclist is relative to */
4335   dw_loc_descr_ref expr;
4336 } dw_loc_list_node;
4337
4338 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4339
4340 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4341
4342 /* Convert a DWARF stack opcode into its string name.  */
4343
4344 static const char *
4345 dwarf_stack_op_name (unsigned int op)
4346 {
4347   switch (op)
4348     {
4349     case DW_OP_addr:
4350       return "DW_OP_addr";
4351     case DW_OP_deref:
4352       return "DW_OP_deref";
4353     case DW_OP_const1u:
4354       return "DW_OP_const1u";
4355     case DW_OP_const1s:
4356       return "DW_OP_const1s";
4357     case DW_OP_const2u:
4358       return "DW_OP_const2u";
4359     case DW_OP_const2s:
4360       return "DW_OP_const2s";
4361     case DW_OP_const4u:
4362       return "DW_OP_const4u";
4363     case DW_OP_const4s:
4364       return "DW_OP_const4s";
4365     case DW_OP_const8u:
4366       return "DW_OP_const8u";
4367     case DW_OP_const8s:
4368       return "DW_OP_const8s";
4369     case DW_OP_constu:
4370       return "DW_OP_constu";
4371     case DW_OP_consts:
4372       return "DW_OP_consts";
4373     case DW_OP_dup:
4374       return "DW_OP_dup";
4375     case DW_OP_drop:
4376       return "DW_OP_drop";
4377     case DW_OP_over:
4378       return "DW_OP_over";
4379     case DW_OP_pick:
4380       return "DW_OP_pick";
4381     case DW_OP_swap:
4382       return "DW_OP_swap";
4383     case DW_OP_rot:
4384       return "DW_OP_rot";
4385     case DW_OP_xderef:
4386       return "DW_OP_xderef";
4387     case DW_OP_abs:
4388       return "DW_OP_abs";
4389     case DW_OP_and:
4390       return "DW_OP_and";
4391     case DW_OP_div:
4392       return "DW_OP_div";
4393     case DW_OP_minus:
4394       return "DW_OP_minus";
4395     case DW_OP_mod:
4396       return "DW_OP_mod";
4397     case DW_OP_mul:
4398       return "DW_OP_mul";
4399     case DW_OP_neg:
4400       return "DW_OP_neg";
4401     case DW_OP_not:
4402       return "DW_OP_not";
4403     case DW_OP_or:
4404       return "DW_OP_or";
4405     case DW_OP_plus:
4406       return "DW_OP_plus";
4407     case DW_OP_plus_uconst:
4408       return "DW_OP_plus_uconst";
4409     case DW_OP_shl:
4410       return "DW_OP_shl";
4411     case DW_OP_shr:
4412       return "DW_OP_shr";
4413     case DW_OP_shra:
4414       return "DW_OP_shra";
4415     case DW_OP_xor:
4416       return "DW_OP_xor";
4417     case DW_OP_bra:
4418       return "DW_OP_bra";
4419     case DW_OP_eq:
4420       return "DW_OP_eq";
4421     case DW_OP_ge:
4422       return "DW_OP_ge";
4423     case DW_OP_gt:
4424       return "DW_OP_gt";
4425     case DW_OP_le:
4426       return "DW_OP_le";
4427     case DW_OP_lt:
4428       return "DW_OP_lt";
4429     case DW_OP_ne:
4430       return "DW_OP_ne";
4431     case DW_OP_skip:
4432       return "DW_OP_skip";
4433     case DW_OP_lit0:
4434       return "DW_OP_lit0";
4435     case DW_OP_lit1:
4436       return "DW_OP_lit1";
4437     case DW_OP_lit2:
4438       return "DW_OP_lit2";
4439     case DW_OP_lit3:
4440       return "DW_OP_lit3";
4441     case DW_OP_lit4:
4442       return "DW_OP_lit4";
4443     case DW_OP_lit5:
4444       return "DW_OP_lit5";
4445     case DW_OP_lit6:
4446       return "DW_OP_lit6";
4447     case DW_OP_lit7:
4448       return "DW_OP_lit7";
4449     case DW_OP_lit8:
4450       return "DW_OP_lit8";
4451     case DW_OP_lit9:
4452       return "DW_OP_lit9";
4453     case DW_OP_lit10:
4454       return "DW_OP_lit10";
4455     case DW_OP_lit11:
4456       return "DW_OP_lit11";
4457     case DW_OP_lit12:
4458       return "DW_OP_lit12";
4459     case DW_OP_lit13:
4460       return "DW_OP_lit13";
4461     case DW_OP_lit14:
4462       return "DW_OP_lit14";
4463     case DW_OP_lit15:
4464       return "DW_OP_lit15";
4465     case DW_OP_lit16:
4466       return "DW_OP_lit16";
4467     case DW_OP_lit17:
4468       return "DW_OP_lit17";
4469     case DW_OP_lit18:
4470       return "DW_OP_lit18";
4471     case DW_OP_lit19:
4472       return "DW_OP_lit19";
4473     case DW_OP_lit20:
4474       return "DW_OP_lit20";
4475     case DW_OP_lit21:
4476       return "DW_OP_lit21";
4477     case DW_OP_lit22:
4478       return "DW_OP_lit22";
4479     case DW_OP_lit23:
4480       return "DW_OP_lit23";
4481     case DW_OP_lit24:
4482       return "DW_OP_lit24";
4483     case DW_OP_lit25:
4484       return "DW_OP_lit25";
4485     case DW_OP_lit26:
4486       return "DW_OP_lit26";
4487     case DW_OP_lit27:
4488       return "DW_OP_lit27";
4489     case DW_OP_lit28:
4490       return "DW_OP_lit28";
4491     case DW_OP_lit29:
4492       return "DW_OP_lit29";
4493     case DW_OP_lit30:
4494       return "DW_OP_lit30";
4495     case DW_OP_lit31:
4496       return "DW_OP_lit31";
4497     case DW_OP_reg0:
4498       return "DW_OP_reg0";
4499     case DW_OP_reg1:
4500       return "DW_OP_reg1";
4501     case DW_OP_reg2:
4502       return "DW_OP_reg2";
4503     case DW_OP_reg3:
4504       return "DW_OP_reg3";
4505     case DW_OP_reg4:
4506       return "DW_OP_reg4";
4507     case DW_OP_reg5:
4508       return "DW_OP_reg5";
4509     case DW_OP_reg6:
4510       return "DW_OP_reg6";
4511     case DW_OP_reg7:
4512       return "DW_OP_reg7";
4513     case DW_OP_reg8:
4514       return "DW_OP_reg8";
4515     case DW_OP_reg9:
4516       return "DW_OP_reg9";
4517     case DW_OP_reg10:
4518       return "DW_OP_reg10";
4519     case DW_OP_reg11:
4520       return "DW_OP_reg11";
4521     case DW_OP_reg12:
4522       return "DW_OP_reg12";
4523     case DW_OP_reg13:
4524       return "DW_OP_reg13";
4525     case DW_OP_reg14:
4526       return "DW_OP_reg14";
4527     case DW_OP_reg15:
4528       return "DW_OP_reg15";
4529     case DW_OP_reg16:
4530       return "DW_OP_reg16";
4531     case DW_OP_reg17:
4532       return "DW_OP_reg17";
4533     case DW_OP_reg18:
4534       return "DW_OP_reg18";
4535     case DW_OP_reg19:
4536       return "DW_OP_reg19";
4537     case DW_OP_reg20:
4538       return "DW_OP_reg20";
4539     case DW_OP_reg21:
4540       return "DW_OP_reg21";
4541     case DW_OP_reg22:
4542       return "DW_OP_reg22";
4543     case DW_OP_reg23:
4544       return "DW_OP_reg23";
4545     case DW_OP_reg24:
4546       return "DW_OP_reg24";
4547     case DW_OP_reg25:
4548       return "DW_OP_reg25";
4549     case DW_OP_reg26:
4550       return "DW_OP_reg26";
4551     case DW_OP_reg27:
4552       return "DW_OP_reg27";
4553     case DW_OP_reg28:
4554       return "DW_OP_reg28";
4555     case DW_OP_reg29:
4556       return "DW_OP_reg29";
4557     case DW_OP_reg30:
4558       return "DW_OP_reg30";
4559     case DW_OP_reg31:
4560       return "DW_OP_reg31";
4561     case DW_OP_breg0:
4562       return "DW_OP_breg0";
4563     case DW_OP_breg1:
4564       return "DW_OP_breg1";
4565     case DW_OP_breg2:
4566       return "DW_OP_breg2";
4567     case DW_OP_breg3:
4568       return "DW_OP_breg3";
4569     case DW_OP_breg4:
4570       return "DW_OP_breg4";
4571     case DW_OP_breg5:
4572       return "DW_OP_breg5";
4573     case DW_OP_breg6:
4574       return "DW_OP_breg6";
4575     case DW_OP_breg7:
4576       return "DW_OP_breg7";
4577     case DW_OP_breg8:
4578       return "DW_OP_breg8";
4579     case DW_OP_breg9:
4580       return "DW_OP_breg9";
4581     case DW_OP_breg10:
4582       return "DW_OP_breg10";
4583     case DW_OP_breg11:
4584       return "DW_OP_breg11";
4585     case DW_OP_breg12:
4586       return "DW_OP_breg12";
4587     case DW_OP_breg13:
4588       return "DW_OP_breg13";
4589     case DW_OP_breg14:
4590       return "DW_OP_breg14";
4591     case DW_OP_breg15:
4592       return "DW_OP_breg15";
4593     case DW_OP_breg16:
4594       return "DW_OP_breg16";
4595     case DW_OP_breg17:
4596       return "DW_OP_breg17";
4597     case DW_OP_breg18:
4598       return "DW_OP_breg18";
4599     case DW_OP_breg19:
4600       return "DW_OP_breg19";
4601     case DW_OP_breg20:
4602       return "DW_OP_breg20";
4603     case DW_OP_breg21:
4604       return "DW_OP_breg21";
4605     case DW_OP_breg22:
4606       return "DW_OP_breg22";
4607     case DW_OP_breg23:
4608       return "DW_OP_breg23";
4609     case DW_OP_breg24:
4610       return "DW_OP_breg24";
4611     case DW_OP_breg25:
4612       return "DW_OP_breg25";
4613     case DW_OP_breg26:
4614       return "DW_OP_breg26";
4615     case DW_OP_breg27:
4616       return "DW_OP_breg27";
4617     case DW_OP_breg28:
4618       return "DW_OP_breg28";
4619     case DW_OP_breg29:
4620       return "DW_OP_breg29";
4621     case DW_OP_breg30:
4622       return "DW_OP_breg30";
4623     case DW_OP_breg31:
4624       return "DW_OP_breg31";
4625     case DW_OP_regx:
4626       return "DW_OP_regx";
4627     case DW_OP_fbreg:
4628       return "DW_OP_fbreg";
4629     case DW_OP_bregx:
4630       return "DW_OP_bregx";
4631     case DW_OP_piece:
4632       return "DW_OP_piece";
4633     case DW_OP_deref_size:
4634       return "DW_OP_deref_size";
4635     case DW_OP_xderef_size:
4636       return "DW_OP_xderef_size";
4637     case DW_OP_nop:
4638       return "DW_OP_nop";
4639
4640     case DW_OP_push_object_address:
4641       return "DW_OP_push_object_address";
4642     case DW_OP_call2:
4643       return "DW_OP_call2";
4644     case DW_OP_call4:
4645       return "DW_OP_call4";
4646     case DW_OP_call_ref:
4647       return "DW_OP_call_ref";
4648     case DW_OP_implicit_value:
4649       return "DW_OP_implicit_value";
4650     case DW_OP_stack_value:
4651       return "DW_OP_stack_value";
4652     case DW_OP_form_tls_address:
4653       return "DW_OP_form_tls_address";
4654     case DW_OP_call_frame_cfa:
4655       return "DW_OP_call_frame_cfa";
4656     case DW_OP_bit_piece:
4657       return "DW_OP_bit_piece";
4658
4659     case DW_OP_GNU_push_tls_address:
4660       return "DW_OP_GNU_push_tls_address";
4661     case DW_OP_GNU_uninit:
4662       return "DW_OP_GNU_uninit";
4663     case DW_OP_GNU_encoded_addr:
4664       return "DW_OP_GNU_encoded_addr";
4665
4666     default:
4667       return "OP_<unknown>";
4668     }
4669 }
4670
4671 /* Return a pointer to a newly allocated location description.  Location
4672    descriptions are simple expression terms that can be strung
4673    together to form more complicated location (address) descriptions.  */
4674
4675 static inline dw_loc_descr_ref
4676 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4677                unsigned HOST_WIDE_INT oprnd2)
4678 {
4679   dw_loc_descr_ref descr = ggc_alloc_cleared_dw_loc_descr_node ();
4680
4681   descr->dw_loc_opc = op;
4682   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4683   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4684   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4685   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4686
4687   return descr;
4688 }
4689
4690 /* Return a pointer to a newly allocated location description for
4691    REG and OFFSET.  */
4692
4693 static inline dw_loc_descr_ref
4694 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4695 {
4696   if (reg <= 31)
4697     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4698                           offset, 0);
4699   else
4700     return new_loc_descr (DW_OP_bregx, reg, offset);
4701 }
4702
4703 /* Add a location description term to a location description expression.  */
4704
4705 static inline void
4706 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4707 {
4708   dw_loc_descr_ref *d;
4709
4710   /* Find the end of the chain.  */
4711   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4712     ;
4713
4714   *d = descr;
4715 }
4716
4717 /* Add a constant OFFSET to a location expression.  */
4718
4719 static void
4720 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4721 {
4722   dw_loc_descr_ref loc;
4723   HOST_WIDE_INT *p;
4724
4725   gcc_assert (*list_head != NULL);
4726
4727   if (!offset)
4728     return;
4729
4730   /* Find the end of the chain.  */
4731   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4732     ;
4733
4734   p = NULL;
4735   if (loc->dw_loc_opc == DW_OP_fbreg
4736       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4737     p = &loc->dw_loc_oprnd1.v.val_int;
4738   else if (loc->dw_loc_opc == DW_OP_bregx)
4739     p = &loc->dw_loc_oprnd2.v.val_int;
4740
4741   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4742      offset.  Don't optimize if an signed integer overflow would happen.  */
4743   if (p != NULL
4744       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4745           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4746     *p += offset;
4747
4748   else if (offset > 0)
4749     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4750
4751   else
4752     {
4753       loc->dw_loc_next = int_loc_descriptor (-offset);
4754       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_minus, 0, 0));
4755     }
4756 }
4757
4758 #ifdef DWARF2_DEBUGGING_INFO
4759 /* Add a constant OFFSET to a location list.  */
4760
4761 static void
4762 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4763 {
4764   dw_loc_list_ref d;
4765   for (d = list_head; d != NULL; d = d->dw_loc_next)
4766     loc_descr_plus_const (&d->expr, offset);
4767 }
4768 #endif
4769
4770 /* Return the size of a location descriptor.  */
4771
4772 static unsigned long
4773 size_of_loc_descr (dw_loc_descr_ref loc)
4774 {
4775   unsigned long size = 1;
4776
4777   switch (loc->dw_loc_opc)
4778     {
4779     case DW_OP_addr:
4780       size += DWARF2_ADDR_SIZE;
4781       break;
4782     case DW_OP_const1u:
4783     case DW_OP_const1s:
4784       size += 1;
4785       break;
4786     case DW_OP_const2u:
4787     case DW_OP_const2s:
4788       size += 2;
4789       break;
4790     case DW_OP_const4u:
4791     case DW_OP_const4s:
4792       size += 4;
4793       break;
4794     case DW_OP_const8u:
4795     case DW_OP_const8s:
4796       size += 8;
4797       break;
4798     case DW_OP_constu:
4799       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4800       break;
4801     case DW_OP_consts:
4802       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4803       break;
4804     case DW_OP_pick:
4805       size += 1;
4806       break;
4807     case DW_OP_plus_uconst:
4808       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4809       break;
4810     case DW_OP_skip:
4811     case DW_OP_bra:
4812       size += 2;
4813       break;
4814     case DW_OP_breg0:
4815     case DW_OP_breg1:
4816     case DW_OP_breg2:
4817     case DW_OP_breg3:
4818     case DW_OP_breg4:
4819     case DW_OP_breg5:
4820     case DW_OP_breg6:
4821     case DW_OP_breg7:
4822     case DW_OP_breg8:
4823     case DW_OP_breg9:
4824     case DW_OP_breg10:
4825     case DW_OP_breg11:
4826     case DW_OP_breg12:
4827     case DW_OP_breg13:
4828     case DW_OP_breg14:
4829     case DW_OP_breg15:
4830     case DW_OP_breg16:
4831     case DW_OP_breg17:
4832     case DW_OP_breg18:
4833     case DW_OP_breg19:
4834     case DW_OP_breg20:
4835     case DW_OP_breg21:
4836     case DW_OP_breg22:
4837     case DW_OP_breg23:
4838     case DW_OP_breg24:
4839     case DW_OP_breg25:
4840     case DW_OP_breg26:
4841     case DW_OP_breg27:
4842     case DW_OP_breg28:
4843     case DW_OP_breg29:
4844     case DW_OP_breg30:
4845     case DW_OP_breg31:
4846       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4847       break;
4848     case DW_OP_regx:
4849       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4850       break;
4851     case DW_OP_fbreg:
4852       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4853       break;
4854     case DW_OP_bregx:
4855       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4856       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4857       break;
4858     case DW_OP_piece:
4859       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4860       break;
4861     case DW_OP_bit_piece:
4862       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4863       size += size_of_uleb128 (loc->dw_loc_oprnd2.v.val_unsigned);
4864       break;
4865     case DW_OP_deref_size:
4866     case DW_OP_xderef_size:
4867       size += 1;
4868       break;
4869     case DW_OP_call2:
4870       size += 2;
4871       break;
4872     case DW_OP_call4:
4873       size += 4;
4874       break;
4875     case DW_OP_call_ref:
4876       size += DWARF2_ADDR_SIZE;
4877       break;
4878     case DW_OP_implicit_value:
4879       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4880               + loc->dw_loc_oprnd1.v.val_unsigned;
4881       break;
4882     default:
4883       break;
4884     }
4885
4886   return size;
4887 }
4888
4889 /* Return the size of a series of location descriptors.  */
4890
4891 static unsigned long
4892 size_of_locs (dw_loc_descr_ref loc)
4893 {
4894   dw_loc_descr_ref l;
4895   unsigned long size;
4896
4897   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4898      field, to avoid writing to a PCH file.  */
4899   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4900     {
4901       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4902         break;
4903       size += size_of_loc_descr (l);
4904     }
4905   if (! l)
4906     return size;
4907
4908   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4909     {
4910       l->dw_loc_addr = size;
4911       size += size_of_loc_descr (l);
4912     }
4913
4914   return size;
4915 }
4916
4917 #ifdef DWARF2_DEBUGGING_INFO
4918 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4919 #endif
4920
4921 /* Output location description stack opcode's operands (if any).  */
4922
4923 static void
4924 output_loc_operands (dw_loc_descr_ref loc)
4925 {
4926   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4927   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4928
4929   switch (loc->dw_loc_opc)
4930     {
4931 #ifdef DWARF2_DEBUGGING_INFO
4932     case DW_OP_const2u:
4933     case DW_OP_const2s:
4934       dw2_asm_output_data (2, val1->v.val_int, NULL);
4935       break;
4936     case DW_OP_const4u:
4937       if (loc->dtprel)
4938         {
4939           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4940           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 4,
4941                                                val1->v.val_addr);
4942           fputc ('\n', asm_out_file);
4943           break;
4944         }
4945       /* FALLTHRU */
4946     case DW_OP_const4s:
4947       dw2_asm_output_data (4, val1->v.val_int, NULL);
4948       break;
4949     case DW_OP_const8u:
4950       if (loc->dtprel)
4951         {
4952           gcc_assert (targetm.asm_out.output_dwarf_dtprel);
4953           targetm.asm_out.output_dwarf_dtprel (asm_out_file, 8,
4954                                                val1->v.val_addr);
4955           fputc ('\n', asm_out_file);
4956           break;
4957         }
4958       /* FALLTHRU */
4959     case DW_OP_const8s:
4960       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4961       dw2_asm_output_data (8, val1->v.val_int, NULL);
4962       break;
4963     case DW_OP_skip:
4964     case DW_OP_bra:
4965       {
4966         int offset;
4967
4968         gcc_assert (val1->val_class == dw_val_class_loc);
4969         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4970
4971         dw2_asm_output_data (2, offset, NULL);
4972       }
4973       break;
4974     case DW_OP_implicit_value:
4975       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4976       switch (val2->val_class)
4977         {
4978         case dw_val_class_const:
4979           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4980           break;
4981         case dw_val_class_vec:
4982           {
4983             unsigned int elt_size = val2->v.val_vec.elt_size;
4984             unsigned int len = val2->v.val_vec.length;
4985             unsigned int i;
4986             unsigned char *p;
4987
4988             if (elt_size > sizeof (HOST_WIDE_INT))
4989               {
4990                 elt_size /= 2;
4991                 len *= 2;
4992               }
4993             for (i = 0, p = val2->v.val_vec.array;
4994                  i < len;
4995                  i++, p += elt_size)
4996               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4997                                    "fp or vector constant word %u", i);
4998           }
4999           break;
5000         case dw_val_class_const_double:
5001           {
5002             unsigned HOST_WIDE_INT first, second;
5003
5004             if (WORDS_BIG_ENDIAN)
5005               {
5006                 first = val2->v.val_double.high;
5007                 second = val2->v.val_double.low;
5008               }
5009             else
5010               {
5011                 first = val2->v.val_double.low;
5012                 second = val2->v.val_double.high;
5013               }
5014             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5015                                  first, NULL);
5016             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
5017                                  second, NULL);
5018           }
5019           break;
5020         case dw_val_class_addr:
5021           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
5022           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
5023           break;
5024         default:
5025           gcc_unreachable ();
5026         }
5027       break;
5028 #else
5029     case DW_OP_const2u:
5030     case DW_OP_const2s:
5031     case DW_OP_const4u:
5032     case DW_OP_const4s:
5033     case DW_OP_const8u:
5034     case DW_OP_const8s:
5035     case DW_OP_skip:
5036     case DW_OP_bra:
5037     case DW_OP_implicit_value:
5038       /* We currently don't make any attempt to make sure these are
5039          aligned properly like we do for the main unwind info, so
5040          don't support emitting things larger than a byte if we're
5041          only doing unwinding.  */
5042       gcc_unreachable ();
5043 #endif
5044     case DW_OP_const1u:
5045     case DW_OP_const1s:
5046       dw2_asm_output_data (1, val1->v.val_int, NULL);
5047       break;
5048     case DW_OP_constu:
5049       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5050       break;
5051     case DW_OP_consts:
5052       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5053       break;
5054     case DW_OP_pick:
5055       dw2_asm_output_data (1, val1->v.val_int, NULL);
5056       break;
5057     case DW_OP_plus_uconst:
5058       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5059       break;
5060     case DW_OP_breg0:
5061     case DW_OP_breg1:
5062     case DW_OP_breg2:
5063     case DW_OP_breg3:
5064     case DW_OP_breg4:
5065     case DW_OP_breg5:
5066     case DW_OP_breg6:
5067     case DW_OP_breg7:
5068     case DW_OP_breg8:
5069     case DW_OP_breg9:
5070     case DW_OP_breg10:
5071     case DW_OP_breg11:
5072     case DW_OP_breg12:
5073     case DW_OP_breg13:
5074     case DW_OP_breg14:
5075     case DW_OP_breg15:
5076     case DW_OP_breg16:
5077     case DW_OP_breg17:
5078     case DW_OP_breg18:
5079     case DW_OP_breg19:
5080     case DW_OP_breg20:
5081     case DW_OP_breg21:
5082     case DW_OP_breg22:
5083     case DW_OP_breg23:
5084     case DW_OP_breg24:
5085     case DW_OP_breg25:
5086     case DW_OP_breg26:
5087     case DW_OP_breg27:
5088     case DW_OP_breg28:
5089     case DW_OP_breg29:
5090     case DW_OP_breg30:
5091     case DW_OP_breg31:
5092       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5093       break;
5094     case DW_OP_regx:
5095       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5096       break;
5097     case DW_OP_fbreg:
5098       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
5099       break;
5100     case DW_OP_bregx:
5101       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5102       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
5103       break;
5104     case DW_OP_piece:
5105       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5106       break;
5107     case DW_OP_bit_piece:
5108       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
5109       dw2_asm_output_data_uleb128 (val2->v.val_unsigned, NULL);
5110       break;
5111     case DW_OP_deref_size:
5112     case DW_OP_xderef_size:
5113       dw2_asm_output_data (1, val1->v.val_int, NULL);
5114       break;
5115
5116     case DW_OP_addr:
5117       if (loc->dtprel)
5118         {
5119           if (targetm.asm_out.output_dwarf_dtprel)
5120             {
5121               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
5122                                                    DWARF2_ADDR_SIZE,
5123                                                    val1->v.val_addr);
5124               fputc ('\n', asm_out_file);
5125             }
5126           else
5127             gcc_unreachable ();
5128         }
5129       else
5130         {
5131 #ifdef DWARF2_DEBUGGING_INFO
5132           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5133 #else
5134           gcc_unreachable ();
5135 #endif
5136         }
5137       break;
5138
5139     default:
5140       /* Other codes have no operands.  */
5141       break;
5142     }
5143 }
5144
5145 /* Output a sequence of location operations.  */
5146
5147 static void
5148 output_loc_sequence (dw_loc_descr_ref loc)
5149 {
5150   for (; loc != NULL; loc = loc->dw_loc_next)
5151     {
5152       /* Output the opcode.  */
5153       dw2_asm_output_data (1, loc->dw_loc_opc,
5154                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5155
5156       /* Output the operand(s) (if any).  */
5157       output_loc_operands (loc);
5158     }
5159 }
5160
5161 /* Output location description stack opcode's operands (if any).
5162    The output is single bytes on a line, suitable for .cfi_escape.  */
5163
5164 static void
5165 output_loc_operands_raw (dw_loc_descr_ref loc)
5166 {
5167   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5168   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5169
5170   switch (loc->dw_loc_opc)
5171     {
5172     case DW_OP_addr:
5173     case DW_OP_implicit_value:
5174       /* We cannot output addresses in .cfi_escape, only bytes.  */
5175       gcc_unreachable ();
5176
5177     case DW_OP_const1u:
5178     case DW_OP_const1s:
5179     case DW_OP_pick:
5180     case DW_OP_deref_size:
5181     case DW_OP_xderef_size:
5182       fputc (',', asm_out_file);
5183       dw2_asm_output_data_raw (1, val1->v.val_int);
5184       break;
5185
5186     case DW_OP_const2u:
5187     case DW_OP_const2s:
5188       fputc (',', asm_out_file);
5189       dw2_asm_output_data_raw (2, val1->v.val_int);
5190       break;
5191
5192     case DW_OP_const4u:
5193     case DW_OP_const4s:
5194       fputc (',', asm_out_file);
5195       dw2_asm_output_data_raw (4, val1->v.val_int);
5196       break;
5197
5198     case DW_OP_const8u:
5199     case DW_OP_const8s:
5200       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5201       fputc (',', asm_out_file);
5202       dw2_asm_output_data_raw (8, val1->v.val_int);
5203       break;
5204
5205     case DW_OP_skip:
5206     case DW_OP_bra:
5207       {
5208         int offset;
5209
5210         gcc_assert (val1->val_class == dw_val_class_loc);
5211         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5212
5213         fputc (',', asm_out_file);
5214         dw2_asm_output_data_raw (2, offset);
5215       }
5216       break;
5217
5218     case DW_OP_constu:
5219     case DW_OP_plus_uconst:
5220     case DW_OP_regx:
5221     case DW_OP_piece:
5222       fputc (',', asm_out_file);
5223       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5224       break;
5225
5226     case DW_OP_bit_piece:
5227       fputc (',', asm_out_file);
5228       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5229       dw2_asm_output_data_uleb128_raw (val2->v.val_unsigned);
5230       break;
5231
5232     case DW_OP_consts:
5233     case DW_OP_breg0:
5234     case DW_OP_breg1:
5235     case DW_OP_breg2:
5236     case DW_OP_breg3:
5237     case DW_OP_breg4:
5238     case DW_OP_breg5:
5239     case DW_OP_breg6:
5240     case DW_OP_breg7:
5241     case DW_OP_breg8:
5242     case DW_OP_breg9:
5243     case DW_OP_breg10:
5244     case DW_OP_breg11:
5245     case DW_OP_breg12:
5246     case DW_OP_breg13:
5247     case DW_OP_breg14:
5248     case DW_OP_breg15:
5249     case DW_OP_breg16:
5250     case DW_OP_breg17:
5251     case DW_OP_breg18:
5252     case DW_OP_breg19:
5253     case DW_OP_breg20:
5254     case DW_OP_breg21:
5255     case DW_OP_breg22:
5256     case DW_OP_breg23:
5257     case DW_OP_breg24:
5258     case DW_OP_breg25:
5259     case DW_OP_breg26:
5260     case DW_OP_breg27:
5261     case DW_OP_breg28:
5262     case DW_OP_breg29:
5263     case DW_OP_breg30:
5264     case DW_OP_breg31:
5265     case DW_OP_fbreg:
5266       fputc (',', asm_out_file);
5267       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5268       break;
5269
5270     case DW_OP_bregx:
5271       fputc (',', asm_out_file);
5272       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5273       fputc (',', asm_out_file);
5274       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5275       break;
5276
5277     default:
5278       /* Other codes have no operands.  */
5279       break;
5280     }
5281 }
5282
5283 static void
5284 output_loc_sequence_raw (dw_loc_descr_ref loc)
5285 {
5286   while (1)
5287     {
5288       /* Output the opcode.  */
5289       fprintf (asm_out_file, "%#x", loc->dw_loc_opc);
5290       output_loc_operands_raw (loc);
5291
5292       if (!loc->dw_loc_next)
5293         break;
5294       loc = loc->dw_loc_next;
5295
5296       fputc (',', asm_out_file);
5297     }
5298 }
5299
5300 /* This routine will generate the correct assembly data for a location
5301    description based on a cfi entry with a complex address.  */
5302
5303 static void
5304 output_cfa_loc (dw_cfi_ref cfi)
5305 {
5306   dw_loc_descr_ref loc;
5307   unsigned long size;
5308
5309   if (cfi->dw_cfi_opc == DW_CFA_expression)
5310     {
5311       dw2_asm_output_data (1, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, NULL);
5312       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5313     }
5314   else
5315     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5316
5317   /* Output the size of the block.  */
5318   size = size_of_locs (loc);
5319   dw2_asm_output_data_uleb128 (size, NULL);
5320
5321   /* Now output the operations themselves.  */
5322   output_loc_sequence (loc);
5323 }
5324
5325 /* Similar, but used for .cfi_escape.  */
5326
5327 static void
5328 output_cfa_loc_raw (dw_cfi_ref cfi)
5329 {
5330   dw_loc_descr_ref loc;
5331   unsigned long size;
5332
5333   if (cfi->dw_cfi_opc == DW_CFA_expression)
5334     {
5335       fprintf (asm_out_file, "%#x,", cfi->dw_cfi_oprnd1.dw_cfi_reg_num);
5336       loc = cfi->dw_cfi_oprnd2.dw_cfi_loc;
5337     }
5338   else
5339     loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5340
5341   /* Output the size of the block.  */
5342   size = size_of_locs (loc);
5343   dw2_asm_output_data_uleb128_raw (size);
5344   fputc (',', asm_out_file);
5345
5346   /* Now output the operations themselves.  */
5347   output_loc_sequence_raw (loc);
5348 }
5349
5350 /* This function builds a dwarf location descriptor sequence from a
5351    dw_cfa_location, adding the given OFFSET to the result of the
5352    expression.  */
5353
5354 static struct dw_loc_descr_struct *
5355 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5356 {
5357   struct dw_loc_descr_struct *head, *tmp;
5358
5359   offset += cfa->offset;
5360
5361   if (cfa->indirect)
5362     {
5363       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5364       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5365       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5366       add_loc_descr (&head, tmp);
5367       if (offset != 0)
5368         {
5369           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5370           add_loc_descr (&head, tmp);
5371         }
5372     }
5373   else
5374     head = new_reg_loc_descr (cfa->reg, offset);
5375
5376   return head;
5377 }
5378
5379 /* This function builds a dwarf location descriptor sequence for
5380    the address at OFFSET from the CFA when stack is aligned to
5381    ALIGNMENT byte.  */
5382
5383 static struct dw_loc_descr_struct *
5384 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5385 {
5386   struct dw_loc_descr_struct *head;
5387   unsigned int dwarf_fp
5388     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5389
5390  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5391   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5392     {
5393       head = new_reg_loc_descr (dwarf_fp, 0);
5394       add_loc_descr (&head, int_loc_descriptor (alignment));
5395       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5396       loc_descr_plus_const (&head, offset);
5397     }
5398   else
5399     head = new_reg_loc_descr (dwarf_fp, offset);
5400   return head;
5401 }
5402
5403 /* This function fills in aa dw_cfa_location structure from a dwarf location
5404    descriptor sequence.  */
5405
5406 static void
5407 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5408 {
5409   struct dw_loc_descr_struct *ptr;
5410   cfa->offset = 0;
5411   cfa->base_offset = 0;
5412   cfa->indirect = 0;
5413   cfa->reg = -1;
5414
5415   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5416     {
5417       enum dwarf_location_atom op = ptr->dw_loc_opc;
5418
5419       switch (op)
5420         {
5421         case DW_OP_reg0:
5422         case DW_OP_reg1:
5423         case DW_OP_reg2:
5424         case DW_OP_reg3:
5425         case DW_OP_reg4:
5426         case DW_OP_reg5:
5427         case DW_OP_reg6:
5428         case DW_OP_reg7:
5429         case DW_OP_reg8:
5430         case DW_OP_reg9:
5431         case DW_OP_reg10:
5432         case DW_OP_reg11:
5433         case DW_OP_reg12:
5434         case DW_OP_reg13:
5435         case DW_OP_reg14:
5436         case DW_OP_reg15:
5437         case DW_OP_reg16:
5438         case DW_OP_reg17:
5439         case DW_OP_reg18:
5440         case DW_OP_reg19:
5441         case DW_OP_reg20:
5442         case DW_OP_reg21:
5443         case DW_OP_reg22:
5444         case DW_OP_reg23:
5445         case DW_OP_reg24:
5446         case DW_OP_reg25:
5447         case DW_OP_reg26:
5448         case DW_OP_reg27:
5449         case DW_OP_reg28:
5450         case DW_OP_reg29:
5451         case DW_OP_reg30:
5452         case DW_OP_reg31:
5453           cfa->reg = op - DW_OP_reg0;
5454           break;
5455         case DW_OP_regx:
5456           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5457           break;
5458         case DW_OP_breg0:
5459         case DW_OP_breg1:
5460         case DW_OP_breg2:
5461         case DW_OP_breg3:
5462         case DW_OP_breg4:
5463         case DW_OP_breg5:
5464         case DW_OP_breg6:
5465         case DW_OP_breg7:
5466         case DW_OP_breg8:
5467         case DW_OP_breg9:
5468         case DW_OP_breg10:
5469         case DW_OP_breg11:
5470         case DW_OP_breg12:
5471         case DW_OP_breg13:
5472         case DW_OP_breg14:
5473         case DW_OP_breg15:
5474         case DW_OP_breg16:
5475         case DW_OP_breg17:
5476         case DW_OP_breg18:
5477         case DW_OP_breg19:
5478         case DW_OP_breg20:
5479         case DW_OP_breg21:
5480         case DW_OP_breg22:
5481         case DW_OP_breg23:
5482         case DW_OP_breg24:
5483         case DW_OP_breg25:
5484         case DW_OP_breg26:
5485         case DW_OP_breg27:
5486         case DW_OP_breg28:
5487         case DW_OP_breg29:
5488         case DW_OP_breg30:
5489         case DW_OP_breg31:
5490           cfa->reg = op - DW_OP_breg0;
5491           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5492           break;
5493         case DW_OP_bregx:
5494           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5495           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5496           break;
5497         case DW_OP_deref:
5498           cfa->indirect = 1;
5499           break;
5500         case DW_OP_plus_uconst:
5501           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5502           break;
5503         default:
5504           internal_error ("DW_LOC_OP %s not implemented",
5505                           dwarf_stack_op_name (ptr->dw_loc_opc));
5506         }
5507     }
5508 }
5509 #endif /* .debug_frame support */
5510 \f
5511 /* And now, the support for symbolic debugging information.  */
5512 #ifdef DWARF2_DEBUGGING_INFO
5513
5514 /* .debug_str support.  */
5515 static int output_indirect_string (void **, void *);
5516
5517 static void dwarf2out_init (const char *);
5518 static void dwarf2out_finish (const char *);
5519 static void dwarf2out_assembly_start (void);
5520 static void dwarf2out_define (unsigned int, const char *);
5521 static void dwarf2out_undef (unsigned int, const char *);
5522 static void dwarf2out_start_source_file (unsigned, const char *);
5523 static void dwarf2out_end_source_file (unsigned);
5524 static void dwarf2out_function_decl (tree);
5525 static void dwarf2out_begin_block (unsigned, unsigned);
5526 static void dwarf2out_end_block (unsigned, unsigned);
5527 static bool dwarf2out_ignore_block (const_tree);
5528 static void dwarf2out_global_decl (tree);
5529 static void dwarf2out_type_decl (tree, int);
5530 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5531 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5532                                                  dw_die_ref);
5533 static void dwarf2out_abstract_function (tree);
5534 static void dwarf2out_var_location (rtx);
5535 static void dwarf2out_direct_call (tree);
5536 static void dwarf2out_virtual_call_token (tree, int);
5537 static void dwarf2out_copy_call_info (rtx, rtx);
5538 static void dwarf2out_virtual_call (int);
5539 static void dwarf2out_begin_function (tree);
5540 static void dwarf2out_set_name (tree, tree);
5541
5542 /* The debug hooks structure.  */
5543
5544 const struct gcc_debug_hooks dwarf2_debug_hooks =
5545 {
5546   dwarf2out_init,
5547   dwarf2out_finish,
5548   dwarf2out_assembly_start,
5549   dwarf2out_define,
5550   dwarf2out_undef,
5551   dwarf2out_start_source_file,
5552   dwarf2out_end_source_file,
5553   dwarf2out_begin_block,
5554   dwarf2out_end_block,
5555   dwarf2out_ignore_block,
5556   dwarf2out_source_line,
5557   dwarf2out_begin_prologue,
5558 #if VMS_DEBUGGING_INFO
5559   dwarf2out_vms_end_prologue,
5560   dwarf2out_vms_begin_epilogue,
5561 #else
5562   debug_nothing_int_charstar,
5563   debug_nothing_int_charstar,
5564 #endif
5565   dwarf2out_end_epilogue,
5566   dwarf2out_begin_function,
5567   debug_nothing_int,            /* end_function */
5568   dwarf2out_function_decl,      /* function_decl */
5569   dwarf2out_global_decl,
5570   dwarf2out_type_decl,          /* type_decl */
5571   dwarf2out_imported_module_or_decl,
5572   debug_nothing_tree,           /* deferred_inline_function */
5573   /* The DWARF 2 backend tries to reduce debugging bloat by not
5574      emitting the abstract description of inline functions until
5575      something tries to reference them.  */
5576   dwarf2out_abstract_function,  /* outlining_inline_function */
5577   debug_nothing_rtx,            /* label */
5578   debug_nothing_int,            /* handle_pch */
5579   dwarf2out_var_location,
5580   dwarf2out_switch_text_section,
5581   dwarf2out_direct_call,
5582   dwarf2out_virtual_call_token,
5583   dwarf2out_copy_call_info,
5584   dwarf2out_virtual_call,
5585   dwarf2out_set_name,
5586   1                             /* start_end_main_source_file */
5587 };
5588 #endif
5589 \f
5590 /* NOTE: In the comments in this file, many references are made to
5591    "Debugging Information Entries".  This term is abbreviated as `DIE'
5592    throughout the remainder of this file.  */
5593
5594 /* An internal representation of the DWARF output is built, and then
5595    walked to generate the DWARF debugging info.  The walk of the internal
5596    representation is done after the entire program has been compiled.
5597    The types below are used to describe the internal representation.  */
5598
5599 /* Various DIE's use offsets relative to the beginning of the
5600    .debug_info section to refer to each other.  */
5601
5602 typedef long int dw_offset;
5603
5604 /* Define typedefs here to avoid circular dependencies.  */
5605
5606 typedef struct dw_attr_struct *dw_attr_ref;
5607 typedef struct dw_line_info_struct *dw_line_info_ref;
5608 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5609 typedef struct pubname_struct *pubname_ref;
5610 typedef struct dw_ranges_struct *dw_ranges_ref;
5611 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5612 typedef struct comdat_type_struct *comdat_type_node_ref;
5613
5614 /* Each entry in the line_info_table maintains the file and
5615    line number associated with the label generated for that
5616    entry.  The label gives the PC value associated with
5617    the line number entry.  */
5618
5619 typedef struct GTY(()) dw_line_info_struct {
5620   unsigned long dw_file_num;
5621   unsigned long dw_line_num;
5622 }
5623 dw_line_info_entry;
5624
5625 /* Line information for functions in separate sections; each one gets its
5626    own sequence.  */
5627 typedef struct GTY(()) dw_separate_line_info_struct {
5628   unsigned long dw_file_num;
5629   unsigned long dw_line_num;
5630   unsigned long function;
5631 }
5632 dw_separate_line_info_entry;
5633
5634 /* Each DIE attribute has a field specifying the attribute kind,
5635    a link to the next attribute in the chain, and an attribute value.
5636    Attributes are typically linked below the DIE they modify.  */
5637
5638 typedef struct GTY(()) dw_attr_struct {
5639   enum dwarf_attribute dw_attr;
5640   dw_val_node dw_attr_val;
5641 }
5642 dw_attr_node;
5643
5644 DEF_VEC_O(dw_attr_node);
5645 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5646
5647 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5648    The children of each node form a circular list linked by
5649    die_sib.  die_child points to the node *before* the "first" child node.  */
5650
5651 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5652   enum dwarf_tag die_tag;
5653   union die_symbol_or_type_node
5654     {
5655       char * GTY ((tag ("0"))) die_symbol;
5656       comdat_type_node_ref GTY ((tag ("1"))) die_type_node;
5657     }
5658   GTY ((desc ("dwarf_version >= 4"))) die_id;
5659   VEC(dw_attr_node,gc) * die_attr;
5660   dw_die_ref die_parent;
5661   dw_die_ref die_child;
5662   dw_die_ref die_sib;
5663   dw_die_ref die_definition; /* ref from a specification to its definition */
5664   dw_offset die_offset;
5665   unsigned long die_abbrev;
5666   int die_mark;
5667   /* Die is used and must not be pruned as unused.  */
5668   int die_perennial_p;
5669   unsigned int decl_id;
5670 }
5671 die_node;
5672
5673 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5674 #define FOR_EACH_CHILD(die, c, expr) do {       \
5675   c = die->die_child;                           \
5676   if (c) do {                                   \
5677     c = c->die_sib;                             \
5678     expr;                                       \
5679   } while (c != die->die_child);                \
5680 } while (0)
5681
5682 /* The pubname structure */
5683
5684 typedef struct GTY(()) pubname_struct {
5685   dw_die_ref die;
5686   const char *name;
5687 }
5688 pubname_entry;
5689
5690 DEF_VEC_O(pubname_entry);
5691 DEF_VEC_ALLOC_O(pubname_entry, gc);
5692
5693 struct GTY(()) dw_ranges_struct {
5694   /* If this is positive, it's a block number, otherwise it's a
5695      bitwise-negated index into dw_ranges_by_label.  */
5696   int num;
5697 };
5698
5699 struct GTY(()) dw_ranges_by_label_struct {
5700   const char *begin;
5701   const char *end;
5702 };
5703
5704 /* The comdat type node structure.  */
5705 typedef struct GTY(()) comdat_type_struct
5706 {
5707   dw_die_ref root_die;
5708   dw_die_ref type_die;
5709   char signature[DWARF_TYPE_SIGNATURE_SIZE];
5710   struct comdat_type_struct *next;
5711 }
5712 comdat_type_node;
5713
5714 /* The limbo die list structure.  */
5715 typedef struct GTY(()) limbo_die_struct {
5716   dw_die_ref die;
5717   tree created_for;
5718   struct limbo_die_struct *next;
5719 }
5720 limbo_die_node;
5721
5722 typedef struct GTY(()) skeleton_chain_struct
5723 {
5724   dw_die_ref old_die;
5725   dw_die_ref new_die;
5726   struct skeleton_chain_struct *parent;
5727 }
5728 skeleton_chain_node;
5729
5730 /* How to start an assembler comment.  */
5731 #ifndef ASM_COMMENT_START
5732 #define ASM_COMMENT_START ";#"
5733 #endif
5734
5735 /* Define a macro which returns nonzero for a TYPE_DECL which was
5736    implicitly generated for a tagged type.
5737
5738    Note that unlike the gcc front end (which generates a NULL named
5739    TYPE_DECL node for each complete tagged type, each array type, and
5740    each function type node created) the g++ front end generates a
5741    _named_ TYPE_DECL node for each tagged type node created.
5742    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5743    generate a DW_TAG_typedef DIE for them.  */
5744
5745 #define TYPE_DECL_IS_STUB(decl)                         \
5746   (DECL_NAME (decl) == NULL_TREE                        \
5747    || (DECL_ARTIFICIAL (decl)                           \
5748        && is_tagged_type (TREE_TYPE (decl))             \
5749        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5750            /* This is necessary for stub decls that     \
5751               appear in nested inline functions.  */    \
5752            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5753                && (decl_ultimate_origin (decl)          \
5754                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5755
5756 /* Information concerning the compilation unit's programming
5757    language, and compiler version.  */
5758
5759 /* Fixed size portion of the DWARF compilation unit header.  */
5760 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5761   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5762
5763 /* Fixed size portion of the DWARF comdat type unit header.  */
5764 #define DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE \
5765   (DWARF_COMPILE_UNIT_HEADER_SIZE + DWARF_TYPE_SIGNATURE_SIZE \
5766    + DWARF_OFFSET_SIZE)
5767
5768 /* Fixed size portion of public names info.  */
5769 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5770
5771 /* Fixed size portion of the address range info.  */
5772 #define DWARF_ARANGES_HEADER_SIZE                                       \
5773   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5774                 DWARF2_ADDR_SIZE * 2)                                   \
5775    - DWARF_INITIAL_LENGTH_SIZE)
5776
5777 /* Size of padding portion in the address range info.  It must be
5778    aligned to twice the pointer size.  */
5779 #define DWARF_ARANGES_PAD_SIZE \
5780   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5781                 DWARF2_ADDR_SIZE * 2)                              \
5782    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5783
5784 /* Use assembler line directives if available.  */
5785 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5786 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5787 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5788 #else
5789 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5790 #endif
5791 #endif
5792
5793 /* Minimum line offset in a special line info. opcode.
5794    This value was chosen to give a reasonable range of values.  */
5795 #define DWARF_LINE_BASE  -10
5796
5797 /* First special line opcode - leave room for the standard opcodes.  */
5798 #define DWARF_LINE_OPCODE_BASE  10
5799
5800 /* Range of line offsets in a special line info. opcode.  */
5801 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5802
5803 /* Flag that indicates the initial value of the is_stmt_start flag.
5804    In the present implementation, we do not mark any lines as
5805    the beginning of a source statement, because that information
5806    is not made available by the GCC front-end.  */
5807 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5808
5809 /* Maximum number of operations per instruction bundle.  */
5810 #ifndef DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN
5811 #define DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN 1
5812 #endif
5813
5814 #ifdef DWARF2_DEBUGGING_INFO
5815 /* This location is used by calc_die_sizes() to keep track
5816    the offset of each DIE within the .debug_info section.  */
5817 static unsigned long next_die_offset;
5818 #endif
5819
5820 /* Record the root of the DIE's built for the current compilation unit.  */
5821 static GTY(()) dw_die_ref comp_unit_die;
5822
5823 /* A list of type DIEs that have been separated into comdat sections.  */
5824 static GTY(()) comdat_type_node *comdat_type_list;
5825
5826 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5827 static GTY(()) limbo_die_node *limbo_die_list;
5828
5829 /* A list of DIEs for which we may have to generate
5830    DW_AT_{,MIPS_}linkage_name once their DECL_ASSEMBLER_NAMEs are set.  */
5831 static GTY(()) limbo_die_node *deferred_asm_name;
5832
5833 /* Filenames referenced by this compilation unit.  */
5834 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5835
5836 /* A hash table of references to DIE's that describe declarations.
5837    The key is a DECL_UID() which is a unique number identifying each decl.  */
5838 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5839
5840 /* A hash table of references to DIE's that describe COMMON blocks.
5841    The key is DECL_UID() ^ die_parent.  */
5842 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5843
5844 typedef struct GTY(()) die_arg_entry_struct {
5845     dw_die_ref die;
5846     tree arg;
5847 } die_arg_entry;
5848
5849 DEF_VEC_O(die_arg_entry);
5850 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5851
5852 /* Node of the variable location list.  */
5853 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5854   /* Either NOTE_INSN_VAR_LOCATION, or, for SRA optimized variables,
5855      EXPR_LIST chain.  For small bitsizes, bitsize is encoded
5856      in mode of the EXPR_LIST node and first EXPR_LIST operand
5857      is either NOTE_INSN_VAR_LOCATION for a piece with a known
5858      location or NULL for padding.  For larger bitsizes,
5859      mode is 0 and first operand is a CONCAT with bitsize
5860      as first CONCAT operand and NOTE_INSN_VAR_LOCATION resp.
5861      NULL as second operand.  */
5862   rtx GTY (()) loc;
5863   const char * GTY (()) label;
5864   struct var_loc_node * GTY (()) next;
5865 };
5866
5867 /* Variable location list.  */
5868 struct GTY (()) var_loc_list_def {
5869   struct var_loc_node * GTY (()) first;
5870
5871   /* Pointer to the last but one or last element of the
5872      chained list.  If the list is empty, both first and
5873      last are NULL, if the list contains just one node
5874      or the last node certainly is not redundant, it points
5875      to the last node, otherwise points to the last but one.
5876      Do not mark it for GC because it is marked through the chain.  */
5877   struct var_loc_node * GTY ((skip ("%h"))) last;
5878
5879   /* DECL_UID of the variable decl.  */
5880   unsigned int decl_id;
5881 };
5882 typedef struct var_loc_list_def var_loc_list;
5883
5884
5885 /* Table of decl location linked lists.  */
5886 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5887
5888 /* A pointer to the base of a list of references to DIE's that
5889    are uniquely identified by their tag, presence/absence of
5890    children DIE's, and list of attribute/value pairs.  */
5891 static GTY((length ("abbrev_die_table_allocated")))
5892   dw_die_ref *abbrev_die_table;
5893
5894 /* Number of elements currently allocated for abbrev_die_table.  */
5895 static GTY(()) unsigned abbrev_die_table_allocated;
5896
5897 /* Number of elements in type_die_table currently in use.  */
5898 static GTY(()) unsigned abbrev_die_table_in_use;
5899
5900 /* Size (in elements) of increments by which we may expand the
5901    abbrev_die_table.  */
5902 #define ABBREV_DIE_TABLE_INCREMENT 256
5903
5904 /* A pointer to the base of a table that contains line information
5905    for each source code line in .text in the compilation unit.  */
5906 static GTY((length ("line_info_table_allocated")))
5907      dw_line_info_ref line_info_table;
5908
5909 /* Number of elements currently allocated for line_info_table.  */
5910 static GTY(()) unsigned line_info_table_allocated;
5911
5912 /* Number of elements in line_info_table currently in use.  */
5913 static GTY(()) unsigned line_info_table_in_use;
5914
5915 /* A pointer to the base of a table that contains line information
5916    for each source code line outside of .text in the compilation unit.  */
5917 static GTY ((length ("separate_line_info_table_allocated")))
5918      dw_separate_line_info_ref separate_line_info_table;
5919
5920 /* Number of elements currently allocated for separate_line_info_table.  */
5921 static GTY(()) unsigned separate_line_info_table_allocated;
5922
5923 /* Number of elements in separate_line_info_table currently in use.  */
5924 static GTY(()) unsigned separate_line_info_table_in_use;
5925
5926 /* Size (in elements) of increments by which we may expand the
5927    line_info_table.  */
5928 #define LINE_INFO_TABLE_INCREMENT 1024
5929
5930 /* A pointer to the base of a table that contains a list of publicly
5931    accessible names.  */
5932 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5933
5934 /* A pointer to the base of a table that contains a list of publicly
5935    accessible types.  */
5936 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5937
5938 /* Array of dies for which we should generate .debug_arange info.  */
5939 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5940
5941 /* Number of elements currently allocated for arange_table.  */
5942 static GTY(()) unsigned arange_table_allocated;
5943
5944 /* Number of elements in arange_table currently in use.  */
5945 static GTY(()) unsigned arange_table_in_use;
5946
5947 /* Size (in elements) of increments by which we may expand the
5948    arange_table.  */
5949 #define ARANGE_TABLE_INCREMENT 64
5950
5951 /* Array of dies for which we should generate .debug_ranges info.  */
5952 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5953
5954 /* Number of elements currently allocated for ranges_table.  */
5955 static GTY(()) unsigned ranges_table_allocated;
5956
5957 /* Number of elements in ranges_table currently in use.  */
5958 static GTY(()) unsigned ranges_table_in_use;
5959
5960 /* Array of pairs of labels referenced in ranges_table.  */
5961 static GTY ((length ("ranges_by_label_allocated")))
5962      dw_ranges_by_label_ref ranges_by_label;
5963
5964 /* Number of elements currently allocated for ranges_by_label.  */
5965 static GTY(()) unsigned ranges_by_label_allocated;
5966
5967 /* Number of elements in ranges_by_label currently in use.  */
5968 static GTY(()) unsigned ranges_by_label_in_use;
5969
5970 /* Size (in elements) of increments by which we may expand the
5971    ranges_table.  */
5972 #define RANGES_TABLE_INCREMENT 64
5973
5974 /* Whether we have location lists that need outputting */
5975 static GTY(()) bool have_location_lists;
5976
5977 /* Unique label counter.  */
5978 static GTY(()) unsigned int loclabel_num;
5979
5980 /* Unique label counter for point-of-call tables.  */
5981 static GTY(()) unsigned int poc_label_num;
5982
5983 /* The direct call table structure.  */
5984
5985 typedef struct GTY(()) dcall_struct {
5986   unsigned int poc_label_num;
5987   tree poc_decl;
5988   dw_die_ref targ_die;
5989 }
5990 dcall_entry;
5991
5992 DEF_VEC_O(dcall_entry);
5993 DEF_VEC_ALLOC_O(dcall_entry, gc);
5994
5995 /* The virtual call table structure.  */
5996
5997 typedef struct GTY(()) vcall_struct {
5998   unsigned int poc_label_num;
5999   unsigned int vtable_slot;
6000 }
6001 vcall_entry;
6002
6003 DEF_VEC_O(vcall_entry);
6004 DEF_VEC_ALLOC_O(vcall_entry, gc);
6005
6006 /* Pointers to the direct and virtual call tables.  */
6007 static GTY (()) VEC (dcall_entry, gc) * dcall_table = NULL;
6008 static GTY (()) VEC (vcall_entry, gc) * vcall_table = NULL;
6009
6010 /* A hash table to map INSN_UIDs to vtable slot indexes.  */
6011
6012 struct GTY (()) vcall_insn {
6013   int insn_uid;
6014   unsigned int vtable_slot;
6015 };
6016
6017 static GTY ((param_is (struct vcall_insn))) htab_t vcall_insn_table;
6018
6019 #ifdef DWARF2_DEBUGGING_INFO
6020 /* Record whether the function being analyzed contains inlined functions.  */
6021 static int current_function_has_inlines;
6022 #endif
6023 #if 0 && defined (MIPS_DEBUGGING_INFO)
6024 static int comp_unit_has_inlines;
6025 #endif
6026
6027 /* The last file entry emitted by maybe_emit_file().  */
6028 static GTY(()) struct dwarf_file_data * last_emitted_file;
6029
6030 /* Number of internal labels generated by gen_internal_sym().  */
6031 static GTY(()) int label_num;
6032
6033 /* Cached result of previous call to lookup_filename.  */
6034 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
6035
6036 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
6037
6038 #ifdef DWARF2_DEBUGGING_INFO
6039
6040 /* Offset from the "steady-state frame pointer" to the frame base,
6041    within the current function.  */
6042 static HOST_WIDE_INT frame_pointer_fb_offset;
6043
6044 /* Forward declarations for functions defined in this file.  */
6045
6046 static int is_pseudo_reg (const_rtx);
6047 static tree type_main_variant (tree);
6048 static int is_tagged_type (const_tree);
6049 static const char *dwarf_tag_name (unsigned);
6050 static const char *dwarf_attr_name (unsigned);
6051 static const char *dwarf_form_name (unsigned);
6052 static tree decl_ultimate_origin (const_tree);
6053 static tree decl_class_context (tree);
6054 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
6055 static inline enum dw_val_class AT_class (dw_attr_ref);
6056 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
6057 static inline unsigned AT_flag (dw_attr_ref);
6058 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
6059 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
6060 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
6061 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
6062 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
6063                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
6064 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
6065                                unsigned int, unsigned char *);
6066 static void add_AT_data8 (dw_die_ref, enum dwarf_attribute, unsigned char *);
6067 static hashval_t debug_str_do_hash (const void *);
6068 static int debug_str_eq (const void *, const void *);
6069 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
6070 static inline const char *AT_string (dw_attr_ref);
6071 static enum dwarf_form AT_string_form (dw_attr_ref);
6072 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
6073 static void add_AT_specification (dw_die_ref, dw_die_ref);
6074 static inline dw_die_ref AT_ref (dw_attr_ref);
6075 static inline int AT_ref_external (dw_attr_ref);
6076 static inline void set_AT_ref_external (dw_attr_ref, int);
6077 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
6078 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
6079 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
6080 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
6081                              dw_loc_list_ref);
6082 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
6083 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
6084 static inline rtx AT_addr (dw_attr_ref);
6085 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
6086 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
6087 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
6088 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
6089                            unsigned HOST_WIDE_INT);
6090 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
6091                                unsigned long);
6092 static inline const char *AT_lbl (dw_attr_ref);
6093 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
6094 static const char *get_AT_low_pc (dw_die_ref);
6095 static const char *get_AT_hi_pc (dw_die_ref);
6096 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
6097 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
6098 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
6099 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
6100 static bool is_cxx (void);
6101 static bool is_fortran (void);
6102 static bool is_ada (void);
6103 static void remove_AT (dw_die_ref, enum dwarf_attribute);
6104 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
6105 static void add_child_die (dw_die_ref, dw_die_ref);
6106 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
6107 static dw_die_ref lookup_type_die (tree);
6108 static void equate_type_number_to_die (tree, dw_die_ref);
6109 static hashval_t decl_die_table_hash (const void *);
6110 static int decl_die_table_eq (const void *, const void *);
6111 static dw_die_ref lookup_decl_die (tree);
6112 static hashval_t common_block_die_table_hash (const void *);
6113 static int common_block_die_table_eq (const void *, const void *);
6114 static hashval_t decl_loc_table_hash (const void *);
6115 static int decl_loc_table_eq (const void *, const void *);
6116 static var_loc_list *lookup_decl_loc (const_tree);
6117 static void equate_decl_number_to_die (tree, dw_die_ref);
6118 static struct var_loc_node *add_var_loc_to_decl (tree, rtx, const char *);
6119 static void print_spaces (FILE *);
6120 static void print_die (dw_die_ref, FILE *);
6121 static void print_dwarf_line_table (FILE *);
6122 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
6123 static dw_die_ref pop_compile_unit (dw_die_ref);
6124 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
6125 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
6126 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
6127 static void checksum_sleb128 (HOST_WIDE_INT, struct md5_ctx *);
6128 static void checksum_uleb128 (unsigned HOST_WIDE_INT, struct md5_ctx *);
6129 static void loc_checksum_ordered (dw_loc_descr_ref, struct md5_ctx *);
6130 static void attr_checksum_ordered (enum dwarf_tag, dw_attr_ref,
6131                                    struct md5_ctx *, int *);
6132 struct checksum_attributes;
6133 static void collect_checksum_attributes (struct checksum_attributes *, dw_die_ref);
6134 static void die_checksum_ordered (dw_die_ref, struct md5_ctx *, int *);
6135 static void checksum_die_context (dw_die_ref, struct md5_ctx *);
6136 static void generate_type_signature (dw_die_ref, comdat_type_node *);
6137 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
6138 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
6139 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
6140 static int same_die_p (dw_die_ref, dw_die_ref, int *);
6141 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
6142 static void compute_section_prefix (dw_die_ref);
6143 static int is_type_die (dw_die_ref);
6144 static int is_comdat_die (dw_die_ref);
6145 static int is_symbol_die (dw_die_ref);
6146 static void assign_symbol_names (dw_die_ref);
6147 static void break_out_includes (dw_die_ref);
6148 static int is_declaration_die (dw_die_ref);
6149 static int should_move_die_to_comdat (dw_die_ref);
6150 static dw_die_ref clone_as_declaration (dw_die_ref);
6151 static dw_die_ref clone_die (dw_die_ref);
6152 static dw_die_ref clone_tree (dw_die_ref);
6153 static void copy_declaration_context (dw_die_ref, dw_die_ref);
6154 static void generate_skeleton_ancestor_tree (skeleton_chain_node *);
6155 static void generate_skeleton_bottom_up (skeleton_chain_node *);
6156 static dw_die_ref generate_skeleton (dw_die_ref);
6157 static dw_die_ref remove_child_or_replace_with_skeleton (dw_die_ref,
6158                                                          dw_die_ref);
6159 static void break_out_comdat_types (dw_die_ref);
6160 static dw_die_ref copy_ancestor_tree (dw_die_ref, dw_die_ref, htab_t);
6161 static void copy_decls_walk (dw_die_ref, dw_die_ref, htab_t);
6162 static void copy_decls_for_unworthy_types (dw_die_ref);
6163
6164 static hashval_t htab_cu_hash (const void *);
6165 static int htab_cu_eq (const void *, const void *);
6166 static void htab_cu_del (void *);
6167 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
6168 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
6169 static void add_sibling_attributes (dw_die_ref);
6170 static void build_abbrev_table (dw_die_ref);
6171 static void output_location_lists (dw_die_ref);
6172 static int constant_size (unsigned HOST_WIDE_INT);
6173 static unsigned long size_of_die (dw_die_ref);
6174 static void calc_die_sizes (dw_die_ref);
6175 static void mark_dies (dw_die_ref);
6176 static void unmark_dies (dw_die_ref);
6177 static void unmark_all_dies (dw_die_ref);
6178 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
6179 static unsigned long size_of_aranges (void);
6180 static enum dwarf_form value_format (dw_attr_ref);
6181 static void output_value_format (dw_attr_ref);
6182 static void output_abbrev_section (void);
6183 static void output_die_symbol (dw_die_ref);
6184 static void output_die (dw_die_ref);
6185 static void output_compilation_unit_header (void);
6186 static void output_comp_unit (dw_die_ref, int);
6187 static void output_comdat_type_unit (comdat_type_node *);
6188 static const char *dwarf2_name (tree, int);
6189 static void add_pubname (tree, dw_die_ref);
6190 static void add_pubname_string (const char *, dw_die_ref);
6191 static void add_pubtype (tree, dw_die_ref);
6192 static void output_pubnames (VEC (pubname_entry,gc) *);
6193 static void add_arange (tree, dw_die_ref);
6194 static void output_aranges (void);
6195 static unsigned int add_ranges_num (int);
6196 static unsigned int add_ranges (const_tree);
6197 static void add_ranges_by_labels (dw_die_ref, const char *, const char *,
6198                                   bool *);
6199 static void output_ranges (void);
6200 static void output_line_info (void);
6201 static void output_file_names (void);
6202 static dw_die_ref base_type_die (tree);
6203 static int is_base_type (tree);
6204 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
6205 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
6206 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
6207 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
6208 static int type_is_enum (const_tree);
6209 static unsigned int dbx_reg_number (const_rtx);
6210 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
6211 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
6212 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
6213                                                 enum var_init_status);
6214 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
6215                                                      enum var_init_status);
6216 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
6217                                          enum var_init_status);
6218 static int is_based_loc (const_rtx);
6219 static int resolve_one_addr (rtx *, void *);
6220 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
6221                                             enum var_init_status);
6222 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
6223                                                enum var_init_status);
6224 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
6225                                         enum var_init_status);
6226 static dw_loc_list_ref loc_list_from_tree (tree, int);
6227 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
6228 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
6229 static tree field_type (const_tree);
6230 static unsigned int simple_type_align_in_bits (const_tree);
6231 static unsigned int simple_decl_align_in_bits (const_tree);
6232 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
6233 static HOST_WIDE_INT field_byte_offset (const_tree);
6234 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
6235                                          dw_loc_list_ref);
6236 static void add_data_member_location_attribute (dw_die_ref, tree);
6237 static bool add_const_value_attribute (dw_die_ref, rtx);
6238 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
6239 static void insert_double (double_int, unsigned char *);
6240 static void insert_float (const_rtx, unsigned char *);
6241 static rtx rtl_for_decl_location (tree);
6242 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
6243                                                    enum dwarf_attribute);
6244 static bool tree_add_const_value_attribute (dw_die_ref, tree);
6245 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
6246 static void add_name_attribute (dw_die_ref, const char *);
6247 static void add_comp_dir_attribute (dw_die_ref);
6248 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
6249 static void add_subscript_info (dw_die_ref, tree, bool);
6250 static void add_byte_size_attribute (dw_die_ref, tree);
6251 static void add_bit_offset_attribute (dw_die_ref, tree);
6252 static void add_bit_size_attribute (dw_die_ref, tree);
6253 static void add_prototyped_attribute (dw_die_ref, tree);
6254 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
6255 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
6256 static void add_src_coords_attributes (dw_die_ref, tree);
6257 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
6258 static void push_decl_scope (tree);
6259 static void pop_decl_scope (void);
6260 static dw_die_ref scope_die_for (tree, dw_die_ref);
6261 static inline int local_scope_p (dw_die_ref);
6262 static inline int class_scope_p (dw_die_ref);
6263 static inline int class_or_namespace_scope_p (dw_die_ref);
6264 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
6265 static void add_calling_convention_attribute (dw_die_ref, tree);
6266 static const char *type_tag (const_tree);
6267 static tree member_declared_type (const_tree);
6268 #if 0
6269 static const char *decl_start_label (tree);
6270 #endif
6271 static void gen_array_type_die (tree, dw_die_ref);
6272 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6273 #if 0
6274 static void gen_entry_point_die (tree, dw_die_ref);
6275 #endif
6276 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6277 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6278 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6279 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6280 static void gen_formal_types_die (tree, dw_die_ref);
6281 static void gen_subprogram_die (tree, dw_die_ref);
6282 static void gen_variable_die (tree, tree, dw_die_ref);
6283 static void gen_const_die (tree, dw_die_ref);
6284 static void gen_label_die (tree, dw_die_ref);
6285 static void gen_lexical_block_die (tree, dw_die_ref, int);
6286 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6287 static void gen_field_die (tree, dw_die_ref);
6288 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6289 static dw_die_ref gen_compile_unit_die (const char *);
6290 static void gen_inheritance_die (tree, tree, dw_die_ref);
6291 static void gen_member_die (tree, dw_die_ref);
6292 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6293                                                 enum debug_info_usage);
6294 static void gen_subroutine_type_die (tree, dw_die_ref);
6295 static void gen_typedef_die (tree, dw_die_ref);
6296 static void gen_type_die (tree, dw_die_ref);
6297 static void gen_block_die (tree, dw_die_ref, int);
6298 static void decls_for_scope (tree, dw_die_ref, int);
6299 static int is_redundant_typedef (const_tree);
6300 static bool is_naming_typedef_decl (const_tree);
6301 static inline dw_die_ref get_context_die (tree);
6302 static void gen_namespace_die (tree, dw_die_ref);
6303 static dw_die_ref gen_decl_die (tree, tree, dw_die_ref);
6304 static dw_die_ref force_decl_die (tree);
6305 static dw_die_ref force_type_die (tree);
6306 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6307 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6308 static struct dwarf_file_data * lookup_filename (const char *);
6309 static void retry_incomplete_types (void);
6310 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6311 static void gen_generic_params_dies (tree);
6312 static void gen_tagged_type_die (tree, dw_die_ref, enum debug_info_usage);
6313 static void gen_type_die_with_usage (tree, dw_die_ref, enum debug_info_usage);
6314 static void splice_child_die (dw_die_ref, dw_die_ref);
6315 static int file_info_cmp (const void *, const void *);
6316 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6317                                      const char *, const char *);
6318 static void output_loc_list (dw_loc_list_ref);
6319 static char *gen_internal_sym (const char *);
6320
6321 static void prune_unmark_dies (dw_die_ref);
6322 static void prune_unused_types_mark (dw_die_ref, int);
6323 static void prune_unused_types_walk (dw_die_ref);
6324 static void prune_unused_types_walk_attribs (dw_die_ref);
6325 static void prune_unused_types_prune (dw_die_ref);
6326 static void prune_unused_types (void);
6327 static int maybe_emit_file (struct dwarf_file_data *fd);
6328 static inline const char *AT_vms_delta1 (dw_attr_ref);
6329 static inline const char *AT_vms_delta2 (dw_attr_ref);
6330 static inline void add_AT_vms_delta (dw_die_ref, enum dwarf_attribute,
6331                                      const char *, const char *);
6332 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6333 static void gen_remaining_tmpl_value_param_die_attribute (void);
6334
6335 /* Section names used to hold DWARF debugging information.  */
6336 #ifndef DEBUG_INFO_SECTION
6337 #define DEBUG_INFO_SECTION      ".debug_info"
6338 #endif
6339 #ifndef DEBUG_ABBREV_SECTION
6340 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6341 #endif
6342 #ifndef DEBUG_ARANGES_SECTION
6343 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6344 #endif
6345 #ifndef DEBUG_MACINFO_SECTION
6346 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6347 #endif
6348 #ifndef DEBUG_LINE_SECTION
6349 #define DEBUG_LINE_SECTION      ".debug_line"
6350 #endif
6351 #ifndef DEBUG_LOC_SECTION
6352 #define DEBUG_LOC_SECTION       ".debug_loc"
6353 #endif
6354 #ifndef DEBUG_PUBNAMES_SECTION
6355 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6356 #endif
6357 #ifndef DEBUG_PUBTYPES_SECTION
6358 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6359 #endif
6360 #ifndef DEBUG_DCALL_SECTION
6361 #define DEBUG_DCALL_SECTION     ".debug_dcall"
6362 #endif
6363 #ifndef DEBUG_VCALL_SECTION
6364 #define DEBUG_VCALL_SECTION     ".debug_vcall"
6365 #endif
6366 #ifndef DEBUG_STR_SECTION
6367 #define DEBUG_STR_SECTION       ".debug_str"
6368 #endif
6369 #ifndef DEBUG_RANGES_SECTION
6370 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6371 #endif
6372
6373 /* Standard ELF section names for compiled code and data.  */
6374 #ifndef TEXT_SECTION_NAME
6375 #define TEXT_SECTION_NAME       ".text"
6376 #endif
6377
6378 /* Section flags for .debug_str section.  */
6379 #define DEBUG_STR_SECTION_FLAGS \
6380   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6381    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6382    : SECTION_DEBUG)
6383
6384 /* Labels we insert at beginning sections we can reference instead of
6385    the section names themselves.  */
6386
6387 #ifndef TEXT_SECTION_LABEL
6388 #define TEXT_SECTION_LABEL              "Ltext"
6389 #endif
6390 #ifndef COLD_TEXT_SECTION_LABEL
6391 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6392 #endif
6393 #ifndef DEBUG_LINE_SECTION_LABEL
6394 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6395 #endif
6396 #ifndef DEBUG_INFO_SECTION_LABEL
6397 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6398 #endif
6399 #ifndef DEBUG_ABBREV_SECTION_LABEL
6400 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6401 #endif
6402 #ifndef DEBUG_LOC_SECTION_LABEL
6403 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6404 #endif
6405 #ifndef DEBUG_RANGES_SECTION_LABEL
6406 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6407 #endif
6408 #ifndef DEBUG_MACINFO_SECTION_LABEL
6409 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6410 #endif
6411
6412
6413 /* Definitions of defaults for formats and names of various special
6414    (artificial) labels which may be generated within this file (when the -g
6415    options is used and DWARF2_DEBUGGING_INFO is in effect.
6416    If necessary, these may be overridden from within the tm.h file, but
6417    typically, overriding these defaults is unnecessary.  */
6418
6419 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6420 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6421 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6422 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6423 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6424 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6425 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6426 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6427 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6428 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6429
6430 #ifndef TEXT_END_LABEL
6431 #define TEXT_END_LABEL          "Letext"
6432 #endif
6433 #ifndef COLD_END_LABEL
6434 #define COLD_END_LABEL          "Letext_cold"
6435 #endif
6436 #ifndef BLOCK_BEGIN_LABEL
6437 #define BLOCK_BEGIN_LABEL       "LBB"
6438 #endif
6439 #ifndef BLOCK_END_LABEL
6440 #define BLOCK_END_LABEL         "LBE"
6441 #endif
6442 #ifndef LINE_CODE_LABEL
6443 #define LINE_CODE_LABEL         "LM"
6444 #endif
6445 #ifndef SEPARATE_LINE_CODE_LABEL
6446 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6447 #endif
6448
6449 \f
6450 /* We allow a language front-end to designate a function that is to be
6451    called to "demangle" any name before it is put into a DIE.  */
6452
6453 static const char *(*demangle_name_func) (const char *);
6454
6455 void
6456 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6457 {
6458   demangle_name_func = func;
6459 }
6460
6461 /* Test if rtl node points to a pseudo register.  */
6462
6463 static inline int
6464 is_pseudo_reg (const_rtx rtl)
6465 {
6466   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6467           || (GET_CODE (rtl) == SUBREG
6468               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6469 }
6470
6471 /* Return a reference to a type, with its const and volatile qualifiers
6472    removed.  */
6473
6474 static inline tree
6475 type_main_variant (tree type)
6476 {
6477   type = TYPE_MAIN_VARIANT (type);
6478
6479   /* ??? There really should be only one main variant among any group of
6480      variants of a given type (and all of the MAIN_VARIANT values for all
6481      members of the group should point to that one type) but sometimes the C
6482      front-end messes this up for array types, so we work around that bug
6483      here.  */
6484   if (TREE_CODE (type) == ARRAY_TYPE)
6485     while (type != TYPE_MAIN_VARIANT (type))
6486       type = TYPE_MAIN_VARIANT (type);
6487
6488   return type;
6489 }
6490
6491 /* Return nonzero if the given type node represents a tagged type.  */
6492
6493 static inline int
6494 is_tagged_type (const_tree type)
6495 {
6496   enum tree_code code = TREE_CODE (type);
6497
6498   return (code == RECORD_TYPE || code == UNION_TYPE
6499           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6500 }
6501
6502 /* Convert a DIE tag into its string name.  */
6503
6504 static const char *
6505 dwarf_tag_name (unsigned int tag)
6506 {
6507   switch (tag)
6508     {
6509     case DW_TAG_padding:
6510       return "DW_TAG_padding";
6511     case DW_TAG_array_type:
6512       return "DW_TAG_array_type";
6513     case DW_TAG_class_type:
6514       return "DW_TAG_class_type";
6515     case DW_TAG_entry_point:
6516       return "DW_TAG_entry_point";
6517     case DW_TAG_enumeration_type:
6518       return "DW_TAG_enumeration_type";
6519     case DW_TAG_formal_parameter:
6520       return "DW_TAG_formal_parameter";
6521     case DW_TAG_imported_declaration:
6522       return "DW_TAG_imported_declaration";
6523     case DW_TAG_label:
6524       return "DW_TAG_label";
6525     case DW_TAG_lexical_block:
6526       return "DW_TAG_lexical_block";
6527     case DW_TAG_member:
6528       return "DW_TAG_member";
6529     case DW_TAG_pointer_type:
6530       return "DW_TAG_pointer_type";
6531     case DW_TAG_reference_type:
6532       return "DW_TAG_reference_type";
6533     case DW_TAG_compile_unit:
6534       return "DW_TAG_compile_unit";
6535     case DW_TAG_string_type:
6536       return "DW_TAG_string_type";
6537     case DW_TAG_structure_type:
6538       return "DW_TAG_structure_type";
6539     case DW_TAG_subroutine_type:
6540       return "DW_TAG_subroutine_type";
6541     case DW_TAG_typedef:
6542       return "DW_TAG_typedef";
6543     case DW_TAG_union_type:
6544       return "DW_TAG_union_type";
6545     case DW_TAG_unspecified_parameters:
6546       return "DW_TAG_unspecified_parameters";
6547     case DW_TAG_variant:
6548       return "DW_TAG_variant";
6549     case DW_TAG_common_block:
6550       return "DW_TAG_common_block";
6551     case DW_TAG_common_inclusion:
6552       return "DW_TAG_common_inclusion";
6553     case DW_TAG_inheritance:
6554       return "DW_TAG_inheritance";
6555     case DW_TAG_inlined_subroutine:
6556       return "DW_TAG_inlined_subroutine";
6557     case DW_TAG_module:
6558       return "DW_TAG_module";
6559     case DW_TAG_ptr_to_member_type:
6560       return "DW_TAG_ptr_to_member_type";
6561     case DW_TAG_set_type:
6562       return "DW_TAG_set_type";
6563     case DW_TAG_subrange_type:
6564       return "DW_TAG_subrange_type";
6565     case DW_TAG_with_stmt:
6566       return "DW_TAG_with_stmt";
6567     case DW_TAG_access_declaration:
6568       return "DW_TAG_access_declaration";
6569     case DW_TAG_base_type:
6570       return "DW_TAG_base_type";
6571     case DW_TAG_catch_block:
6572       return "DW_TAG_catch_block";
6573     case DW_TAG_const_type:
6574       return "DW_TAG_const_type";
6575     case DW_TAG_constant:
6576       return "DW_TAG_constant";
6577     case DW_TAG_enumerator:
6578       return "DW_TAG_enumerator";
6579     case DW_TAG_file_type:
6580       return "DW_TAG_file_type";
6581     case DW_TAG_friend:
6582       return "DW_TAG_friend";
6583     case DW_TAG_namelist:
6584       return "DW_TAG_namelist";
6585     case DW_TAG_namelist_item:
6586       return "DW_TAG_namelist_item";
6587     case DW_TAG_packed_type:
6588       return "DW_TAG_packed_type";
6589     case DW_TAG_subprogram:
6590       return "DW_TAG_subprogram";
6591     case DW_TAG_template_type_param:
6592       return "DW_TAG_template_type_param";
6593     case DW_TAG_template_value_param:
6594       return "DW_TAG_template_value_param";
6595     case DW_TAG_thrown_type:
6596       return "DW_TAG_thrown_type";
6597     case DW_TAG_try_block:
6598       return "DW_TAG_try_block";
6599     case DW_TAG_variant_part:
6600       return "DW_TAG_variant_part";
6601     case DW_TAG_variable:
6602       return "DW_TAG_variable";
6603     case DW_TAG_volatile_type:
6604       return "DW_TAG_volatile_type";
6605     case DW_TAG_dwarf_procedure:
6606       return "DW_TAG_dwarf_procedure";
6607     case DW_TAG_restrict_type:
6608       return "DW_TAG_restrict_type";
6609     case DW_TAG_interface_type:
6610       return "DW_TAG_interface_type";
6611     case DW_TAG_namespace:
6612       return "DW_TAG_namespace";
6613     case DW_TAG_imported_module:
6614       return "DW_TAG_imported_module";
6615     case DW_TAG_unspecified_type:
6616       return "DW_TAG_unspecified_type";
6617     case DW_TAG_partial_unit:
6618       return "DW_TAG_partial_unit";
6619     case DW_TAG_imported_unit:
6620       return "DW_TAG_imported_unit";
6621     case DW_TAG_condition:
6622       return "DW_TAG_condition";
6623     case DW_TAG_shared_type:
6624       return "DW_TAG_shared_type";
6625     case DW_TAG_type_unit:
6626       return "DW_TAG_type_unit";
6627     case DW_TAG_rvalue_reference_type:
6628       return "DW_TAG_rvalue_reference_type";
6629     case DW_TAG_template_alias:
6630       return "DW_TAG_template_alias";
6631     case DW_TAG_GNU_template_parameter_pack:
6632       return "DW_TAG_GNU_template_parameter_pack";
6633     case DW_TAG_GNU_formal_parameter_pack:
6634       return "DW_TAG_GNU_formal_parameter_pack";
6635     case DW_TAG_MIPS_loop:
6636       return "DW_TAG_MIPS_loop";
6637     case DW_TAG_format_label:
6638       return "DW_TAG_format_label";
6639     case DW_TAG_function_template:
6640       return "DW_TAG_function_template";
6641     case DW_TAG_class_template:
6642       return "DW_TAG_class_template";
6643     case DW_TAG_GNU_BINCL:
6644       return "DW_TAG_GNU_BINCL";
6645     case DW_TAG_GNU_EINCL:
6646       return "DW_TAG_GNU_EINCL";
6647     case DW_TAG_GNU_template_template_param:
6648       return "DW_TAG_GNU_template_template_param";
6649     default:
6650       return "DW_TAG_<unknown>";
6651     }
6652 }
6653
6654 /* Convert a DWARF attribute code into its string name.  */
6655
6656 static const char *
6657 dwarf_attr_name (unsigned int attr)
6658 {
6659   switch (attr)
6660     {
6661     case DW_AT_sibling:
6662       return "DW_AT_sibling";
6663     case DW_AT_location:
6664       return "DW_AT_location";
6665     case DW_AT_name:
6666       return "DW_AT_name";
6667     case DW_AT_ordering:
6668       return "DW_AT_ordering";
6669     case DW_AT_subscr_data:
6670       return "DW_AT_subscr_data";
6671     case DW_AT_byte_size:
6672       return "DW_AT_byte_size";
6673     case DW_AT_bit_offset:
6674       return "DW_AT_bit_offset";
6675     case DW_AT_bit_size:
6676       return "DW_AT_bit_size";
6677     case DW_AT_element_list:
6678       return "DW_AT_element_list";
6679     case DW_AT_stmt_list:
6680       return "DW_AT_stmt_list";
6681     case DW_AT_low_pc:
6682       return "DW_AT_low_pc";
6683     case DW_AT_high_pc:
6684       return "DW_AT_high_pc";
6685     case DW_AT_language:
6686       return "DW_AT_language";
6687     case DW_AT_member:
6688       return "DW_AT_member";
6689     case DW_AT_discr:
6690       return "DW_AT_discr";
6691     case DW_AT_discr_value:
6692       return "DW_AT_discr_value";
6693     case DW_AT_visibility:
6694       return "DW_AT_visibility";
6695     case DW_AT_import:
6696       return "DW_AT_import";
6697     case DW_AT_string_length:
6698       return "DW_AT_string_length";
6699     case DW_AT_common_reference:
6700       return "DW_AT_common_reference";
6701     case DW_AT_comp_dir:
6702       return "DW_AT_comp_dir";
6703     case DW_AT_const_value:
6704       return "DW_AT_const_value";
6705     case DW_AT_containing_type:
6706       return "DW_AT_containing_type";
6707     case DW_AT_default_value:
6708       return "DW_AT_default_value";
6709     case DW_AT_inline:
6710       return "DW_AT_inline";
6711     case DW_AT_is_optional:
6712       return "DW_AT_is_optional";
6713     case DW_AT_lower_bound:
6714       return "DW_AT_lower_bound";
6715     case DW_AT_producer:
6716       return "DW_AT_producer";
6717     case DW_AT_prototyped:
6718       return "DW_AT_prototyped";
6719     case DW_AT_return_addr:
6720       return "DW_AT_return_addr";
6721     case DW_AT_start_scope:
6722       return "DW_AT_start_scope";
6723     case DW_AT_bit_stride:
6724       return "DW_AT_bit_stride";
6725     case DW_AT_upper_bound:
6726       return "DW_AT_upper_bound";
6727     case DW_AT_abstract_origin:
6728       return "DW_AT_abstract_origin";
6729     case DW_AT_accessibility:
6730       return "DW_AT_accessibility";
6731     case DW_AT_address_class:
6732       return "DW_AT_address_class";
6733     case DW_AT_artificial:
6734       return "DW_AT_artificial";
6735     case DW_AT_base_types:
6736       return "DW_AT_base_types";
6737     case DW_AT_calling_convention:
6738       return "DW_AT_calling_convention";
6739     case DW_AT_count:
6740       return "DW_AT_count";
6741     case DW_AT_data_member_location:
6742       return "DW_AT_data_member_location";
6743     case DW_AT_decl_column:
6744       return "DW_AT_decl_column";
6745     case DW_AT_decl_file:
6746       return "DW_AT_decl_file";
6747     case DW_AT_decl_line:
6748       return "DW_AT_decl_line";
6749     case DW_AT_declaration:
6750       return "DW_AT_declaration";
6751     case DW_AT_discr_list:
6752       return "DW_AT_discr_list";
6753     case DW_AT_encoding:
6754       return "DW_AT_encoding";
6755     case DW_AT_external:
6756       return "DW_AT_external";
6757     case DW_AT_explicit:
6758       return "DW_AT_explicit";
6759     case DW_AT_frame_base:
6760       return "DW_AT_frame_base";
6761     case DW_AT_friend:
6762       return "DW_AT_friend";
6763     case DW_AT_identifier_case:
6764       return "DW_AT_identifier_case";
6765     case DW_AT_macro_info:
6766       return "DW_AT_macro_info";
6767     case DW_AT_namelist_items:
6768       return "DW_AT_namelist_items";
6769     case DW_AT_priority:
6770       return "DW_AT_priority";
6771     case DW_AT_segment:
6772       return "DW_AT_segment";
6773     case DW_AT_specification:
6774       return "DW_AT_specification";
6775     case DW_AT_static_link:
6776       return "DW_AT_static_link";
6777     case DW_AT_type:
6778       return "DW_AT_type";
6779     case DW_AT_use_location:
6780       return "DW_AT_use_location";
6781     case DW_AT_variable_parameter:
6782       return "DW_AT_variable_parameter";
6783     case DW_AT_virtuality:
6784       return "DW_AT_virtuality";
6785     case DW_AT_vtable_elem_location:
6786       return "DW_AT_vtable_elem_location";
6787
6788     case DW_AT_allocated:
6789       return "DW_AT_allocated";
6790     case DW_AT_associated:
6791       return "DW_AT_associated";
6792     case DW_AT_data_location:
6793       return "DW_AT_data_location";
6794     case DW_AT_byte_stride:
6795       return "DW_AT_byte_stride";
6796     case DW_AT_entry_pc:
6797       return "DW_AT_entry_pc";
6798     case DW_AT_use_UTF8:
6799       return "DW_AT_use_UTF8";
6800     case DW_AT_extension:
6801       return "DW_AT_extension";
6802     case DW_AT_ranges:
6803       return "DW_AT_ranges";
6804     case DW_AT_trampoline:
6805       return "DW_AT_trampoline";
6806     case DW_AT_call_column:
6807       return "DW_AT_call_column";
6808     case DW_AT_call_file:
6809       return "DW_AT_call_file";
6810     case DW_AT_call_line:
6811       return "DW_AT_call_line";
6812     case DW_AT_object_pointer:
6813       return "DW_AT_object_pointer";
6814
6815     case DW_AT_signature:
6816       return "DW_AT_signature";
6817     case DW_AT_main_subprogram:
6818       return "DW_AT_main_subprogram";
6819     case DW_AT_data_bit_offset:
6820       return "DW_AT_data_bit_offset";
6821     case DW_AT_const_expr:
6822       return "DW_AT_const_expr";
6823     case DW_AT_enum_class:
6824       return "DW_AT_enum_class";
6825     case DW_AT_linkage_name:
6826       return "DW_AT_linkage_name";
6827
6828     case DW_AT_MIPS_fde:
6829       return "DW_AT_MIPS_fde";
6830     case DW_AT_MIPS_loop_begin:
6831       return "DW_AT_MIPS_loop_begin";
6832     case DW_AT_MIPS_tail_loop_begin:
6833       return "DW_AT_MIPS_tail_loop_begin";
6834     case DW_AT_MIPS_epilog_begin:
6835       return "DW_AT_MIPS_epilog_begin";
6836 #if VMS_DEBUGGING_INFO
6837     case DW_AT_HP_prologue:
6838       return "DW_AT_HP_prologue";
6839 #else
6840     case DW_AT_MIPS_loop_unroll_factor:
6841       return "DW_AT_MIPS_loop_unroll_factor";
6842 #endif
6843     case DW_AT_MIPS_software_pipeline_depth:
6844       return "DW_AT_MIPS_software_pipeline_depth";
6845     case DW_AT_MIPS_linkage_name:
6846       return "DW_AT_MIPS_linkage_name";
6847 #if VMS_DEBUGGING_INFO
6848     case DW_AT_HP_epilogue:
6849       return "DW_AT_HP_epilogue";
6850 #else
6851     case DW_AT_MIPS_stride:
6852       return "DW_AT_MIPS_stride";
6853 #endif
6854     case DW_AT_MIPS_abstract_name:
6855       return "DW_AT_MIPS_abstract_name";
6856     case DW_AT_MIPS_clone_origin:
6857       return "DW_AT_MIPS_clone_origin";
6858     case DW_AT_MIPS_has_inlines:
6859       return "DW_AT_MIPS_has_inlines";
6860
6861     case DW_AT_sf_names:
6862       return "DW_AT_sf_names";
6863     case DW_AT_src_info:
6864       return "DW_AT_src_info";
6865     case DW_AT_mac_info:
6866       return "DW_AT_mac_info";
6867     case DW_AT_src_coords:
6868       return "DW_AT_src_coords";
6869     case DW_AT_body_begin:
6870       return "DW_AT_body_begin";
6871     case DW_AT_body_end:
6872       return "DW_AT_body_end";
6873     case DW_AT_GNU_vector:
6874       return "DW_AT_GNU_vector";
6875     case DW_AT_GNU_guarded_by:
6876       return "DW_AT_GNU_guarded_by";
6877     case DW_AT_GNU_pt_guarded_by:
6878       return "DW_AT_GNU_pt_guarded_by";
6879     case DW_AT_GNU_guarded:
6880       return "DW_AT_GNU_guarded";
6881     case DW_AT_GNU_pt_guarded:
6882       return "DW_AT_GNU_pt_guarded";
6883     case DW_AT_GNU_locks_excluded:
6884       return "DW_AT_GNU_locks_excluded";
6885     case DW_AT_GNU_exclusive_locks_required:
6886       return "DW_AT_GNU_exclusive_locks_required";
6887     case DW_AT_GNU_shared_locks_required:
6888       return "DW_AT_GNU_shared_locks_required";
6889     case DW_AT_GNU_odr_signature:
6890       return "DW_AT_GNU_odr_signature";
6891     case DW_AT_GNU_template_name:
6892       return "DW_AT_GNU_template_name";
6893
6894     case DW_AT_VMS_rtnbeg_pd_address:
6895       return "DW_AT_VMS_rtnbeg_pd_address";
6896
6897     default:
6898       return "DW_AT_<unknown>";
6899     }
6900 }
6901
6902 /* Convert a DWARF value form code into its string name.  */
6903
6904 static const char *
6905 dwarf_form_name (unsigned int form)
6906 {
6907   switch (form)
6908     {
6909     case DW_FORM_addr:
6910       return "DW_FORM_addr";
6911     case DW_FORM_block2:
6912       return "DW_FORM_block2";
6913     case DW_FORM_block4:
6914       return "DW_FORM_block4";
6915     case DW_FORM_data2:
6916       return "DW_FORM_data2";
6917     case DW_FORM_data4:
6918       return "DW_FORM_data4";
6919     case DW_FORM_data8:
6920       return "DW_FORM_data8";
6921     case DW_FORM_string:
6922       return "DW_FORM_string";
6923     case DW_FORM_block:
6924       return "DW_FORM_block";
6925     case DW_FORM_block1:
6926       return "DW_FORM_block1";
6927     case DW_FORM_data1:
6928       return "DW_FORM_data1";
6929     case DW_FORM_flag:
6930       return "DW_FORM_flag";
6931     case DW_FORM_sdata:
6932       return "DW_FORM_sdata";
6933     case DW_FORM_strp:
6934       return "DW_FORM_strp";
6935     case DW_FORM_udata:
6936       return "DW_FORM_udata";
6937     case DW_FORM_ref_addr:
6938       return "DW_FORM_ref_addr";
6939     case DW_FORM_ref1:
6940       return "DW_FORM_ref1";
6941     case DW_FORM_ref2:
6942       return "DW_FORM_ref2";
6943     case DW_FORM_ref4:
6944       return "DW_FORM_ref4";
6945     case DW_FORM_ref8:
6946       return "DW_FORM_ref8";
6947     case DW_FORM_ref_udata:
6948       return "DW_FORM_ref_udata";
6949     case DW_FORM_indirect:
6950       return "DW_FORM_indirect";
6951     case DW_FORM_sec_offset:
6952       return "DW_FORM_sec_offset";
6953     case DW_FORM_exprloc:
6954       return "DW_FORM_exprloc";
6955     case DW_FORM_flag_present:
6956       return "DW_FORM_flag_present";
6957     case DW_FORM_ref_sig8:
6958       return "DW_FORM_ref_sig8";
6959     default:
6960       return "DW_FORM_<unknown>";
6961     }
6962 }
6963 \f
6964 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6965    instance of an inlined instance of a decl which is local to an inline
6966    function, so we have to trace all of the way back through the origin chain
6967    to find out what sort of node actually served as the original seed for the
6968    given block.  */
6969
6970 static tree
6971 decl_ultimate_origin (const_tree decl)
6972 {
6973   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6974     return NULL_TREE;
6975
6976   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6977      nodes in the function to point to themselves; ignore that if
6978      we're trying to output the abstract instance of this function.  */
6979   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6980     return NULL_TREE;
6981
6982   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6983      most distant ancestor, this should never happen.  */
6984   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6985
6986   return DECL_ABSTRACT_ORIGIN (decl);
6987 }
6988
6989 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6990    of a virtual function may refer to a base class, so we check the 'this'
6991    parameter.  */
6992
6993 static tree
6994 decl_class_context (tree decl)
6995 {
6996   tree context = NULL_TREE;
6997
6998   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6999     context = DECL_CONTEXT (decl);
7000   else
7001     context = TYPE_MAIN_VARIANT
7002       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
7003
7004   if (context && !TYPE_P (context))
7005     context = NULL_TREE;
7006
7007   return context;
7008 }
7009 \f
7010 /* Add an attribute/value pair to a DIE.  */
7011
7012 static inline void
7013 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
7014 {
7015   /* Maybe this should be an assert?  */
7016   if (die == NULL)
7017     return;
7018
7019   if (die->die_attr == NULL)
7020     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
7021   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
7022 }
7023
7024 static inline enum dw_val_class
7025 AT_class (dw_attr_ref a)
7026 {
7027   return a->dw_attr_val.val_class;
7028 }
7029
7030 /* Add a flag value attribute to a DIE.  */
7031
7032 static inline void
7033 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
7034 {
7035   dw_attr_node attr;
7036
7037   attr.dw_attr = attr_kind;
7038   attr.dw_attr_val.val_class = dw_val_class_flag;
7039   attr.dw_attr_val.v.val_flag = flag;
7040   add_dwarf_attr (die, &attr);
7041 }
7042
7043 static inline unsigned
7044 AT_flag (dw_attr_ref a)
7045 {
7046   gcc_assert (a && AT_class (a) == dw_val_class_flag);
7047   return a->dw_attr_val.v.val_flag;
7048 }
7049
7050 /* Add a signed integer attribute value to a DIE.  */
7051
7052 static inline void
7053 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
7054 {
7055   dw_attr_node attr;
7056
7057   attr.dw_attr = attr_kind;
7058   attr.dw_attr_val.val_class = dw_val_class_const;
7059   attr.dw_attr_val.v.val_int = int_val;
7060   add_dwarf_attr (die, &attr);
7061 }
7062
7063 static inline HOST_WIDE_INT
7064 AT_int (dw_attr_ref a)
7065 {
7066   gcc_assert (a && AT_class (a) == dw_val_class_const);
7067   return a->dw_attr_val.v.val_int;
7068 }
7069
7070 /* Add an unsigned integer attribute value to a DIE.  */
7071
7072 static inline void
7073 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
7074                  unsigned HOST_WIDE_INT unsigned_val)
7075 {
7076   dw_attr_node attr;
7077
7078   attr.dw_attr = attr_kind;
7079   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
7080   attr.dw_attr_val.v.val_unsigned = unsigned_val;
7081   add_dwarf_attr (die, &attr);
7082 }
7083
7084 static inline unsigned HOST_WIDE_INT
7085 AT_unsigned (dw_attr_ref a)
7086 {
7087   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
7088   return a->dw_attr_val.v.val_unsigned;
7089 }
7090
7091 /* Add an unsigned double integer attribute value to a DIE.  */
7092
7093 static inline void
7094 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
7095                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
7096 {
7097   dw_attr_node attr;
7098
7099   attr.dw_attr = attr_kind;
7100   attr.dw_attr_val.val_class = dw_val_class_const_double;
7101   attr.dw_attr_val.v.val_double.high = high;
7102   attr.dw_attr_val.v.val_double.low = low;
7103   add_dwarf_attr (die, &attr);
7104 }
7105
7106 /* Add a floating point attribute value to a DIE and return it.  */
7107
7108 static inline void
7109 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
7110             unsigned int length, unsigned int elt_size, unsigned char *array)
7111 {
7112   dw_attr_node attr;
7113
7114   attr.dw_attr = attr_kind;
7115   attr.dw_attr_val.val_class = dw_val_class_vec;
7116   attr.dw_attr_val.v.val_vec.length = length;
7117   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
7118   attr.dw_attr_val.v.val_vec.array = array;
7119   add_dwarf_attr (die, &attr);
7120 }
7121
7122 /* Add an 8-byte data attribute value to a DIE.  */
7123
7124 static inline void
7125 add_AT_data8 (dw_die_ref die, enum dwarf_attribute attr_kind,
7126               unsigned char data8[8])
7127 {
7128   dw_attr_node attr;
7129
7130   attr.dw_attr = attr_kind;
7131   attr.dw_attr_val.val_class = dw_val_class_data8;
7132   memcpy (attr.dw_attr_val.v.val_data8, data8, 8);
7133   add_dwarf_attr (die, &attr);
7134 }
7135
7136 /* Hash and equality functions for debug_str_hash.  */
7137
7138 static hashval_t
7139 debug_str_do_hash (const void *x)
7140 {
7141   return htab_hash_string (((const struct indirect_string_node *)x)->str);
7142 }
7143
7144 static int
7145 debug_str_eq (const void *x1, const void *x2)
7146 {
7147   return strcmp ((((const struct indirect_string_node *)x1)->str),
7148                  (const char *)x2) == 0;
7149 }
7150
7151 /* Add STR to the indirect string hash table.  */
7152
7153 static struct indirect_string_node *
7154 find_AT_string (const char *str)
7155 {
7156   struct indirect_string_node *node;
7157   void **slot;
7158
7159   if (! debug_str_hash)
7160     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
7161                                       debug_str_eq, NULL);
7162
7163   slot = htab_find_slot_with_hash (debug_str_hash, str,
7164                                    htab_hash_string (str), INSERT);
7165   if (*slot == NULL)
7166     {
7167       node = ggc_alloc_cleared_indirect_string_node ();
7168       node->str = ggc_strdup (str);
7169       *slot = node;
7170     }
7171   else
7172     node = (struct indirect_string_node *) *slot;
7173
7174   node->refcount++;
7175   return node;
7176 }
7177
7178 /* Add a string attribute value to a DIE.  */
7179
7180 static inline void
7181 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
7182 {
7183   dw_attr_node attr;
7184   struct indirect_string_node *node;
7185
7186   node = find_AT_string (str);
7187
7188   attr.dw_attr = attr_kind;
7189   attr.dw_attr_val.val_class = dw_val_class_str;
7190   attr.dw_attr_val.v.val_str = node;
7191   add_dwarf_attr (die, &attr);
7192 }
7193
7194 /* Create a label for an indirect string node, ensuring it is going to
7195    be output, unless its reference count goes down to zero.  */
7196
7197 static inline void
7198 gen_label_for_indirect_string (struct indirect_string_node *node)
7199 {
7200   char label[32];
7201
7202   if (node->label)
7203     return;
7204
7205   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
7206   ++dw2_string_counter;
7207   node->label = xstrdup (label);
7208 }
7209
7210 /* Create a SYMBOL_REF rtx whose value is the initial address of a
7211    debug string STR.  */
7212
7213 static inline rtx
7214 get_debug_string_label (const char *str)
7215 {
7216   struct indirect_string_node *node = find_AT_string (str);
7217
7218   debug_str_hash_forced = true;
7219
7220   gen_label_for_indirect_string (node);
7221
7222   return gen_rtx_SYMBOL_REF (Pmode, node->label);
7223 }
7224
7225 static inline const char *
7226 AT_string (dw_attr_ref a)
7227 {
7228   gcc_assert (a && AT_class (a) == dw_val_class_str);
7229   return a->dw_attr_val.v.val_str->str;
7230 }
7231
7232 /* Find out whether a string should be output inline in DIE
7233    or out-of-line in .debug_str section.  */
7234
7235 static enum dwarf_form
7236 AT_string_form (dw_attr_ref a)
7237 {
7238   struct indirect_string_node *node;
7239   unsigned int len;
7240
7241   gcc_assert (a && AT_class (a) == dw_val_class_str);
7242
7243   node = a->dw_attr_val.v.val_str;
7244   if (node->form)
7245     return node->form;
7246
7247   len = strlen (node->str) + 1;
7248
7249   /* If the string is shorter or equal to the size of the reference, it is
7250      always better to put it inline.  */
7251   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
7252     return node->form = DW_FORM_string;
7253
7254   /* If we cannot expect the linker to merge strings in .debug_str
7255      section, only put it into .debug_str if it is worth even in this
7256      single module.  */
7257   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
7258       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
7259       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
7260     return node->form = DW_FORM_string;
7261
7262   gen_label_for_indirect_string (node);
7263
7264   return node->form = DW_FORM_strp;
7265 }
7266
7267 /* Add a DIE reference attribute value to a DIE.  */
7268
7269 static inline void
7270 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
7271 {
7272   dw_attr_node attr;
7273
7274   attr.dw_attr = attr_kind;
7275   attr.dw_attr_val.val_class = dw_val_class_die_ref;
7276   attr.dw_attr_val.v.val_die_ref.die = targ_die;
7277   attr.dw_attr_val.v.val_die_ref.external = 0;
7278   add_dwarf_attr (die, &attr);
7279 }
7280
7281 /* Add an AT_specification attribute to a DIE, and also make the back
7282    pointer from the specification to the definition.  */
7283
7284 static inline void
7285 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
7286 {
7287   add_AT_die_ref (die, DW_AT_specification, targ_die);
7288   gcc_assert (!targ_die->die_definition);
7289   targ_die->die_definition = die;
7290 }
7291
7292 static inline dw_die_ref
7293 AT_ref (dw_attr_ref a)
7294 {
7295   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7296   return a->dw_attr_val.v.val_die_ref.die;
7297 }
7298
7299 static inline int
7300 AT_ref_external (dw_attr_ref a)
7301 {
7302   if (a && AT_class (a) == dw_val_class_die_ref)
7303     return a->dw_attr_val.v.val_die_ref.external;
7304
7305   return 0;
7306 }
7307
7308 static inline void
7309 set_AT_ref_external (dw_attr_ref a, int i)
7310 {
7311   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
7312   a->dw_attr_val.v.val_die_ref.external = i;
7313 }
7314
7315 /* Add an FDE reference attribute value to a DIE.  */
7316
7317 static inline void
7318 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
7319 {
7320   dw_attr_node attr;
7321
7322   attr.dw_attr = attr_kind;
7323   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
7324   attr.dw_attr_val.v.val_fde_index = targ_fde;
7325   add_dwarf_attr (die, &attr);
7326 }
7327
7328 /* Add a location description attribute value to a DIE.  */
7329
7330 static inline void
7331 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
7332 {
7333   dw_attr_node attr;
7334
7335   attr.dw_attr = attr_kind;
7336   attr.dw_attr_val.val_class = dw_val_class_loc;
7337   attr.dw_attr_val.v.val_loc = loc;
7338   add_dwarf_attr (die, &attr);
7339 }
7340
7341 static inline dw_loc_descr_ref
7342 AT_loc (dw_attr_ref a)
7343 {
7344   gcc_assert (a && AT_class (a) == dw_val_class_loc);
7345   return a->dw_attr_val.v.val_loc;
7346 }
7347
7348 static inline void
7349 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7350 {
7351   dw_attr_node attr;
7352
7353   attr.dw_attr = attr_kind;
7354   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7355   attr.dw_attr_val.v.val_loc_list = loc_list;
7356   add_dwarf_attr (die, &attr);
7357   have_location_lists = true;
7358 }
7359
7360 static inline dw_loc_list_ref
7361 AT_loc_list (dw_attr_ref a)
7362 {
7363   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7364   return a->dw_attr_val.v.val_loc_list;
7365 }
7366
7367 static inline dw_loc_list_ref *
7368 AT_loc_list_ptr (dw_attr_ref a)
7369 {
7370   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7371   return &a->dw_attr_val.v.val_loc_list;
7372 }
7373
7374 /* Add an address constant attribute value to a DIE.  */
7375
7376 static inline void
7377 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7378 {
7379   dw_attr_node attr;
7380
7381   attr.dw_attr = attr_kind;
7382   attr.dw_attr_val.val_class = dw_val_class_addr;
7383   attr.dw_attr_val.v.val_addr = addr;
7384   add_dwarf_attr (die, &attr);
7385 }
7386
7387 /* Get the RTX from to an address DIE attribute.  */
7388
7389 static inline rtx
7390 AT_addr (dw_attr_ref a)
7391 {
7392   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7393   return a->dw_attr_val.v.val_addr;
7394 }
7395
7396 /* Add a file attribute value to a DIE.  */
7397
7398 static inline void
7399 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7400              struct dwarf_file_data *fd)
7401 {
7402   dw_attr_node attr;
7403
7404   attr.dw_attr = attr_kind;
7405   attr.dw_attr_val.val_class = dw_val_class_file;
7406   attr.dw_attr_val.v.val_file = fd;
7407   add_dwarf_attr (die, &attr);
7408 }
7409
7410 /* Get the dwarf_file_data from a file DIE attribute.  */
7411
7412 static inline struct dwarf_file_data *
7413 AT_file (dw_attr_ref a)
7414 {
7415   gcc_assert (a && AT_class (a) == dw_val_class_file);
7416   return a->dw_attr_val.v.val_file;
7417 }
7418
7419 /* Add a vms delta attribute value to a DIE.  */
7420
7421 static inline void
7422 add_AT_vms_delta (dw_die_ref die, enum dwarf_attribute attr_kind,
7423                   const char *lbl1, const char *lbl2)
7424 {
7425   dw_attr_node attr;
7426
7427   attr.dw_attr = attr_kind;
7428   attr.dw_attr_val.val_class = dw_val_class_vms_delta;
7429   attr.dw_attr_val.v.val_vms_delta.lbl1 = xstrdup (lbl1);
7430   attr.dw_attr_val.v.val_vms_delta.lbl2 = xstrdup (lbl2);
7431   add_dwarf_attr (die, &attr);
7432 }
7433
7434 /* Add a label identifier attribute value to a DIE.  */
7435
7436 static inline void
7437 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7438 {
7439   dw_attr_node attr;
7440
7441   attr.dw_attr = attr_kind;
7442   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7443   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7444   add_dwarf_attr (die, &attr);
7445 }
7446
7447 /* Add a section offset attribute value to a DIE, an offset into the
7448    debug_line section.  */
7449
7450 static inline void
7451 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7452                 const char *label)
7453 {
7454   dw_attr_node attr;
7455
7456   attr.dw_attr = attr_kind;
7457   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7458   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7459   add_dwarf_attr (die, &attr);
7460 }
7461
7462 /* Add a section offset attribute value to a DIE, an offset into the
7463    debug_macinfo section.  */
7464
7465 static inline void
7466 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7467                const char *label)
7468 {
7469   dw_attr_node attr;
7470
7471   attr.dw_attr = attr_kind;
7472   attr.dw_attr_val.val_class = dw_val_class_macptr;
7473   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7474   add_dwarf_attr (die, &attr);
7475 }
7476
7477 /* Add an offset attribute value to a DIE.  */
7478
7479 static inline void
7480 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7481                unsigned HOST_WIDE_INT offset)
7482 {
7483   dw_attr_node attr;
7484
7485   attr.dw_attr = attr_kind;
7486   attr.dw_attr_val.val_class = dw_val_class_offset;
7487   attr.dw_attr_val.v.val_offset = offset;
7488   add_dwarf_attr (die, &attr);
7489 }
7490
7491 /* Add an range_list attribute value to a DIE.  */
7492
7493 static void
7494 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7495                    long unsigned int offset)
7496 {
7497   dw_attr_node attr;
7498
7499   attr.dw_attr = attr_kind;
7500   attr.dw_attr_val.val_class = dw_val_class_range_list;
7501   attr.dw_attr_val.v.val_offset = offset;
7502   add_dwarf_attr (die, &attr);
7503 }
7504
7505 /* Return the start label of a delta attribute.  */
7506
7507 static inline const char *
7508 AT_vms_delta1 (dw_attr_ref a)
7509 {
7510   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7511   return a->dw_attr_val.v.val_vms_delta.lbl1;
7512 }
7513
7514 /* Return the end label of a delta attribute.  */
7515
7516 static inline const char *
7517 AT_vms_delta2 (dw_attr_ref a)
7518 {
7519   gcc_assert (a && (AT_class (a) == dw_val_class_vms_delta));
7520   return a->dw_attr_val.v.val_vms_delta.lbl2;
7521 }
7522
7523 static inline const char *
7524 AT_lbl (dw_attr_ref a)
7525 {
7526   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7527                     || AT_class (a) == dw_val_class_lineptr
7528                     || AT_class (a) == dw_val_class_macptr));
7529   return a->dw_attr_val.v.val_lbl_id;
7530 }
7531
7532 /* Get the attribute of type attr_kind.  */
7533
7534 static dw_attr_ref
7535 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7536 {
7537   dw_attr_ref a;
7538   unsigned ix;
7539   dw_die_ref spec = NULL;
7540
7541   if (! die)
7542     return NULL;
7543
7544   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7545     if (a->dw_attr == attr_kind)
7546       return a;
7547     else if (a->dw_attr == DW_AT_specification
7548              || a->dw_attr == DW_AT_abstract_origin)
7549       spec = AT_ref (a);
7550
7551   if (spec)
7552     return get_AT (spec, attr_kind);
7553
7554   return NULL;
7555 }
7556
7557 /* Return the "low pc" attribute value, typically associated with a subprogram
7558    DIE.  Return null if the "low pc" attribute is either not present, or if it
7559    cannot be represented as an assembler label identifier.  */
7560
7561 static inline const char *
7562 get_AT_low_pc (dw_die_ref die)
7563 {
7564   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7565
7566   return a ? AT_lbl (a) : NULL;
7567 }
7568
7569 /* Return the "high pc" attribute value, typically associated with a subprogram
7570    DIE.  Return null if the "high pc" attribute is either not present, or if it
7571    cannot be represented as an assembler label identifier.  */
7572
7573 static inline const char *
7574 get_AT_hi_pc (dw_die_ref die)
7575 {
7576   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7577
7578   return a ? AT_lbl (a) : NULL;
7579 }
7580
7581 /* Return the value of the string attribute designated by ATTR_KIND, or
7582    NULL if it is not present.  */
7583
7584 static inline const char *
7585 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7586 {
7587   dw_attr_ref a = get_AT (die, attr_kind);
7588
7589   return a ? AT_string (a) : NULL;
7590 }
7591
7592 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7593    if it is not present.  */
7594
7595 static inline int
7596 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7597 {
7598   dw_attr_ref a = get_AT (die, attr_kind);
7599
7600   return a ? AT_flag (a) : 0;
7601 }
7602
7603 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7604    if it is not present.  */
7605
7606 static inline unsigned
7607 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7608 {
7609   dw_attr_ref a = get_AT (die, attr_kind);
7610
7611   return a ? AT_unsigned (a) : 0;
7612 }
7613
7614 static inline dw_die_ref
7615 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7616 {
7617   dw_attr_ref a = get_AT (die, attr_kind);
7618
7619   return a ? AT_ref (a) : NULL;
7620 }
7621
7622 static inline struct dwarf_file_data *
7623 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7624 {
7625   dw_attr_ref a = get_AT (die, attr_kind);
7626
7627   return a ? AT_file (a) : NULL;
7628 }
7629
7630 /* Return TRUE if the language is C++.  */
7631
7632 static inline bool
7633 is_cxx (void)
7634 {
7635   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7636
7637   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7638 }
7639
7640 /* Return TRUE if the language is Fortran.  */
7641
7642 static inline bool
7643 is_fortran (void)
7644 {
7645   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7646
7647   return (lang == DW_LANG_Fortran77
7648           || lang == DW_LANG_Fortran90
7649           || lang == DW_LANG_Fortran95);
7650 }
7651
7652 /* Return TRUE if the language is Ada.  */
7653
7654 static inline bool
7655 is_ada (void)
7656 {
7657   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7658
7659   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7660 }
7661
7662 /* Remove the specified attribute if present.  */
7663
7664 static void
7665 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7666 {
7667   dw_attr_ref a;
7668   unsigned ix;
7669
7670   if (! die)
7671     return;
7672
7673   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7674     if (a->dw_attr == attr_kind)
7675       {
7676         if (AT_class (a) == dw_val_class_str)
7677           if (a->dw_attr_val.v.val_str->refcount)
7678             a->dw_attr_val.v.val_str->refcount--;
7679
7680         /* VEC_ordered_remove should help reduce the number of abbrevs
7681            that are needed.  */
7682         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7683         return;
7684       }
7685 }
7686
7687 /* Remove CHILD from its parent.  PREV must have the property that
7688    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7689
7690 static void
7691 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7692 {
7693   gcc_assert (child->die_parent == prev->die_parent);
7694   gcc_assert (prev->die_sib == child);
7695   if (prev == child)
7696     {
7697       gcc_assert (child->die_parent->die_child == child);
7698       prev = NULL;
7699     }
7700   else
7701     prev->die_sib = child->die_sib;
7702   if (child->die_parent->die_child == child)
7703     child->die_parent->die_child = prev;
7704 }
7705
7706 /* Replace OLD_CHILD with NEW_CHILD.  PREV must have the property that
7707    PREV->DIE_SIB == OLD_CHILD.  Does not alter OLD_CHILD.  */
7708
7709 static void
7710 replace_child (dw_die_ref old_child, dw_die_ref new_child, dw_die_ref prev)
7711 {
7712   dw_die_ref parent = old_child->die_parent;
7713
7714   gcc_assert (parent == prev->die_parent);
7715   gcc_assert (prev->die_sib == old_child);
7716
7717   new_child->die_parent = parent;
7718   if (prev == old_child)
7719     {
7720       gcc_assert (parent->die_child == old_child);
7721       new_child->die_sib = new_child;
7722     }
7723   else
7724     {
7725       prev->die_sib = new_child;
7726       new_child->die_sib = old_child->die_sib;
7727     }
7728   if (old_child->die_parent->die_child == old_child)
7729     old_child->die_parent->die_child = new_child;
7730 }
7731
7732 /* Move all children from OLD_PARENT to NEW_PARENT.  */
7733
7734 static void
7735 move_all_children (dw_die_ref old_parent, dw_die_ref new_parent)
7736 {
7737   dw_die_ref c;
7738   new_parent->die_child = old_parent->die_child;
7739   old_parent->die_child = NULL;
7740   FOR_EACH_CHILD (new_parent, c, c->die_parent = new_parent);
7741 }
7742
7743 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7744    matches TAG.  */
7745
7746 static void
7747 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7748 {
7749   dw_die_ref c;
7750
7751   c = die->die_child;
7752   if (c) do {
7753     dw_die_ref prev = c;
7754     c = c->die_sib;
7755     while (c->die_tag == tag)
7756       {
7757         remove_child_with_prev (c, prev);
7758         /* Might have removed every child.  */
7759         if (c == c->die_sib)
7760           return;
7761         c = c->die_sib;
7762       }
7763   } while (c != die->die_child);
7764 }
7765
7766 /* Add a CHILD_DIE as the last child of DIE.  */
7767
7768 static void
7769 add_child_die (dw_die_ref die, dw_die_ref child_die)
7770 {
7771   /* FIXME this should probably be an assert.  */
7772   if (! die || ! child_die)
7773     return;
7774   gcc_assert (die != child_die);
7775
7776   child_die->die_parent = die;
7777   if (die->die_child)
7778     {
7779       child_die->die_sib = die->die_child->die_sib;
7780       die->die_child->die_sib = child_die;
7781     }
7782   else
7783     child_die->die_sib = child_die;
7784   die->die_child = child_die;
7785 }
7786
7787 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7788    is the specification, to the end of PARENT's list of children.
7789    This is done by removing and re-adding it.  */
7790
7791 static void
7792 splice_child_die (dw_die_ref parent, dw_die_ref child)
7793 {
7794   dw_die_ref p;
7795
7796   /* We want the declaration DIE from inside the class, not the
7797      specification DIE at toplevel.  */
7798   if (child->die_parent != parent)
7799     {
7800       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7801
7802       if (tmp)
7803         child = tmp;
7804     }
7805
7806   gcc_assert (child->die_parent == parent
7807               || (child->die_parent
7808                   == get_AT_ref (parent, DW_AT_specification)));
7809
7810   for (p = child->die_parent->die_child; ; p = p->die_sib)
7811     if (p->die_sib == child)
7812       {
7813         remove_child_with_prev (child, p);
7814         break;
7815       }
7816
7817   add_child_die (parent, child);
7818 }
7819
7820 /* Return a pointer to a newly created DIE node.  */
7821
7822 static inline dw_die_ref
7823 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7824 {
7825   dw_die_ref die = ggc_alloc_cleared_die_node ();
7826
7827   die->die_tag = tag_value;
7828
7829   if (parent_die != NULL)
7830     add_child_die (parent_die, die);
7831   else
7832     {
7833       limbo_die_node *limbo_node;
7834
7835       limbo_node = ggc_alloc_cleared_limbo_die_node ();
7836       limbo_node->die = die;
7837       limbo_node->created_for = t;
7838       limbo_node->next = limbo_die_list;
7839       limbo_die_list = limbo_node;
7840     }
7841
7842   return die;
7843 }
7844
7845 /* Return the DIE associated with the given type specifier.  */
7846
7847 static inline dw_die_ref
7848 lookup_type_die (tree type)
7849 {
7850   return TYPE_SYMTAB_DIE (type);
7851 }
7852
7853 /* Equate a DIE to a given type specifier.  */
7854
7855 static inline void
7856 equate_type_number_to_die (tree type, dw_die_ref type_die)
7857 {
7858   TYPE_SYMTAB_DIE (type) = type_die;
7859 }
7860
7861 /* Returns a hash value for X (which really is a die_struct).  */
7862
7863 static hashval_t
7864 decl_die_table_hash (const void *x)
7865 {
7866   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7867 }
7868
7869 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7870
7871 static int
7872 decl_die_table_eq (const void *x, const void *y)
7873 {
7874   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7875 }
7876
7877 /* Return the DIE associated with a given declaration.  */
7878
7879 static inline dw_die_ref
7880 lookup_decl_die (tree decl)
7881 {
7882   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7883 }
7884
7885 /* Returns a hash value for X (which really is a var_loc_list).  */
7886
7887 static hashval_t
7888 decl_loc_table_hash (const void *x)
7889 {
7890   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7891 }
7892
7893 /* Return nonzero if decl_id of var_loc_list X is the same as
7894    UID of decl *Y.  */
7895
7896 static int
7897 decl_loc_table_eq (const void *x, const void *y)
7898 {
7899   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7900 }
7901
7902 /* Return the var_loc list associated with a given declaration.  */
7903
7904 static inline var_loc_list *
7905 lookup_decl_loc (const_tree decl)
7906 {
7907   if (!decl_loc_table)
7908     return NULL;
7909   return (var_loc_list *)
7910     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7911 }
7912
7913 /* Equate a DIE to a particular declaration.  */
7914
7915 static void
7916 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7917 {
7918   unsigned int decl_id = DECL_UID (decl);
7919   void **slot;
7920
7921   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7922   *slot = decl_die;
7923   decl_die->decl_id = decl_id;
7924 }
7925
7926 /* Return how many bits covers PIECE EXPR_LIST.  */
7927
7928 static int
7929 decl_piece_bitsize (rtx piece)
7930 {
7931   int ret = (int) GET_MODE (piece);
7932   if (ret)
7933     return ret;
7934   gcc_assert (GET_CODE (XEXP (piece, 0)) == CONCAT
7935               && CONST_INT_P (XEXP (XEXP (piece, 0), 0)));
7936   return INTVAL (XEXP (XEXP (piece, 0), 0));
7937 }
7938
7939 /* Return pointer to the location of location note in PIECE EXPR_LIST.  */
7940
7941 static rtx *
7942 decl_piece_varloc_ptr (rtx piece)
7943 {
7944   if ((int) GET_MODE (piece))
7945     return &XEXP (piece, 0);
7946   else
7947     return &XEXP (XEXP (piece, 0), 1);
7948 }
7949
7950 /* Create an EXPR_LIST for location note LOC_NOTE covering BITSIZE bits.
7951    Next is the chain of following piece nodes.  */
7952
7953 static rtx
7954 decl_piece_node (rtx loc_note, HOST_WIDE_INT bitsize, rtx next)
7955 {
7956   if (bitsize <= (int) MAX_MACHINE_MODE)
7957     return alloc_EXPR_LIST (bitsize, loc_note, next);
7958   else
7959     return alloc_EXPR_LIST (0, gen_rtx_CONCAT (VOIDmode,
7960                                                GEN_INT (bitsize),
7961                                                loc_note), next);
7962 }
7963
7964 /* Return rtx that should be stored into loc field for
7965    LOC_NOTE and BITPOS/BITSIZE.  */
7966
7967 static rtx
7968 construct_piece_list (rtx loc_note, HOST_WIDE_INT bitpos,
7969                       HOST_WIDE_INT bitsize)
7970 {
7971   if (bitsize != -1)
7972     {
7973       loc_note = decl_piece_node (loc_note, bitsize, NULL_RTX);
7974       if (bitpos != 0)
7975         loc_note = decl_piece_node (NULL_RTX, bitpos, loc_note);
7976     }
7977   return loc_note;
7978 }
7979
7980 /* This function either modifies location piece list *DEST in
7981    place (if SRC and INNER is NULL), or copies location piece list
7982    *SRC to *DEST while modifying it.  Location BITPOS is modified
7983    to contain LOC_NOTE, any pieces overlapping it are removed resp.
7984    not copied and if needed some padding around it is added.
7985    When modifying in place, DEST should point to EXPR_LIST where
7986    earlier pieces cover PIECE_BITPOS bits, when copying SRC points
7987    to the start of the whole list and INNER points to the EXPR_LIST
7988    where earlier pieces cover PIECE_BITPOS bits.  */
7989
7990 static void
7991 adjust_piece_list (rtx *dest, rtx *src, rtx *inner,
7992                    HOST_WIDE_INT bitpos, HOST_WIDE_INT piece_bitpos,
7993                    HOST_WIDE_INT bitsize, rtx loc_note)
7994 {
7995   int diff;
7996   bool copy = inner != NULL;
7997
7998   if (copy)
7999     {
8000       /* First copy all nodes preceeding the current bitpos.  */
8001       while (src != inner)
8002         {
8003           *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8004                                    decl_piece_bitsize (*src), NULL_RTX);
8005           dest = &XEXP (*dest, 1);
8006           src = &XEXP (*src, 1);
8007         }
8008     }
8009   /* Add padding if needed.  */
8010   if (bitpos != piece_bitpos)
8011     {
8012       *dest = decl_piece_node (NULL_RTX, bitpos - piece_bitpos,
8013                                copy ? NULL_RTX : *dest);
8014       dest = &XEXP (*dest, 1);
8015     }
8016   else if (*dest && decl_piece_bitsize (*dest) == bitsize)
8017     {
8018       gcc_assert (!copy);
8019       /* A piece with correct bitpos and bitsize already exist,
8020          just update the location for it and return.  */
8021       *decl_piece_varloc_ptr (*dest) = loc_note;
8022       return;
8023     }
8024   /* Add the piece that changed.  */
8025   *dest = decl_piece_node (loc_note, bitsize, copy ? NULL_RTX : *dest);
8026   dest = &XEXP (*dest, 1);
8027   /* Skip over pieces that overlap it.  */
8028   diff = bitpos - piece_bitpos + bitsize;
8029   if (!copy)
8030     src = dest;
8031   while (diff > 0 && *src)
8032     {
8033       rtx piece = *src;
8034       diff -= decl_piece_bitsize (piece);
8035       if (copy)
8036         src = &XEXP (piece, 1);
8037       else
8038         {
8039           *src = XEXP (piece, 1);
8040           free_EXPR_LIST_node (piece);
8041         }
8042     }
8043   /* Add padding if needed.  */
8044   if (diff < 0 && *src)
8045     {
8046       if (!copy)
8047         dest = src;
8048       *dest = decl_piece_node (NULL_RTX, -diff, copy ? NULL_RTX : *dest);
8049       dest = &XEXP (*dest, 1);
8050     }
8051   if (!copy)
8052     return;
8053   /* Finally copy all nodes following it.  */
8054   while (*src)
8055     {
8056       *dest = decl_piece_node (*decl_piece_varloc_ptr (*src),
8057                                decl_piece_bitsize (*src), NULL_RTX);
8058       dest = &XEXP (*dest, 1);
8059       src = &XEXP (*src, 1);
8060     }
8061 }
8062
8063 /* Add a variable location node to the linked list for DECL.  */
8064
8065 static struct var_loc_node *
8066 add_var_loc_to_decl (tree decl, rtx loc_note, const char *label)
8067 {
8068   unsigned int decl_id;
8069   var_loc_list *temp;
8070   void **slot;
8071   struct var_loc_node *loc = NULL;
8072   HOST_WIDE_INT bitsize = -1, bitpos = -1;
8073
8074   if (DECL_DEBUG_EXPR_IS_FROM (decl))
8075     {
8076       tree realdecl = DECL_DEBUG_EXPR (decl);
8077       if (realdecl && handled_component_p (realdecl))
8078         {
8079           HOST_WIDE_INT maxsize;
8080           tree innerdecl;
8081           innerdecl
8082             = get_ref_base_and_extent (realdecl, &bitpos, &bitsize, &maxsize);
8083           if (!DECL_P (innerdecl)
8084               || DECL_IGNORED_P (innerdecl)
8085               || TREE_STATIC (innerdecl)
8086               || bitsize <= 0
8087               || bitpos + bitsize > 256
8088               || bitsize != maxsize)
8089             return NULL;
8090           decl = innerdecl;
8091         }
8092     }
8093
8094   decl_id = DECL_UID (decl);
8095   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
8096   if (*slot == NULL)
8097     {
8098       temp = ggc_alloc_cleared_var_loc_list ();
8099       temp->decl_id = decl_id;
8100       *slot = temp;
8101     }
8102   else
8103     temp = (var_loc_list *) *slot;
8104
8105   if (temp->last)
8106     {
8107       struct var_loc_node *last = temp->last, *unused = NULL;
8108       rtx *piece_loc = NULL, last_loc_note;
8109       int piece_bitpos = 0;
8110       if (last->next)
8111         {
8112           last = last->next;
8113           gcc_assert (last->next == NULL);
8114         }
8115       if (bitsize != -1 && GET_CODE (last->loc) == EXPR_LIST)
8116         {
8117           piece_loc = &last->loc;
8118           do
8119             {
8120               int cur_bitsize = decl_piece_bitsize (*piece_loc);
8121               if (piece_bitpos + cur_bitsize > bitpos)
8122                 break;
8123               piece_bitpos += cur_bitsize;
8124               piece_loc = &XEXP (*piece_loc, 1);
8125             }
8126           while (*piece_loc);
8127         }
8128       /* TEMP->LAST here is either pointer to the last but one or
8129          last element in the chained list, LAST is pointer to the
8130          last element.  */
8131       if (label && strcmp (last->label, label) == 0)
8132         {
8133           /* For SRA optimized variables if there weren't any real
8134              insns since last note, just modify the last node.  */
8135           if (piece_loc != NULL)
8136             {
8137               adjust_piece_list (piece_loc, NULL, NULL,
8138                                  bitpos, piece_bitpos, bitsize, loc_note);
8139               return NULL;
8140             }
8141           /* If the last note doesn't cover any instructions, remove it.  */
8142           if (temp->last != last)
8143             {
8144               temp->last->next = NULL;
8145               unused = last;
8146               last = temp->last;
8147               gcc_assert (strcmp (last->label, label) != 0);
8148             }
8149           else
8150             {
8151               gcc_assert (temp->first == temp->last);
8152               memset (temp->last, '\0', sizeof (*temp->last));
8153               temp->last->loc = construct_piece_list (loc_note, bitpos, bitsize);
8154               return temp->last;
8155             }
8156         }
8157       if (bitsize == -1 && NOTE_P (last->loc))
8158         last_loc_note = last->loc;
8159       else if (piece_loc != NULL
8160                && *piece_loc != NULL_RTX
8161                && piece_bitpos == bitpos
8162                && decl_piece_bitsize (*piece_loc) == bitsize)
8163         last_loc_note = *decl_piece_varloc_ptr (*piece_loc);
8164       else
8165         last_loc_note = NULL_RTX;
8166       /* If the current location is the same as the end of the list,
8167          and either both or neither of the locations is uninitialized,
8168          we have nothing to do.  */
8169       if (last_loc_note == NULL_RTX
8170           || (!rtx_equal_p (NOTE_VAR_LOCATION_LOC (last_loc_note),
8171                             NOTE_VAR_LOCATION_LOC (loc_note)))
8172           || ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8173                != NOTE_VAR_LOCATION_STATUS (loc_note))
8174               && ((NOTE_VAR_LOCATION_STATUS (last_loc_note)
8175                    == VAR_INIT_STATUS_UNINITIALIZED)
8176                   || (NOTE_VAR_LOCATION_STATUS (loc_note)
8177                       == VAR_INIT_STATUS_UNINITIALIZED))))
8178         {
8179           /* Add LOC to the end of list and update LAST.  If the last
8180              element of the list has been removed above, reuse its
8181              memory for the new node, otherwise allocate a new one.  */
8182           if (unused)
8183             {
8184               loc = unused;
8185               memset (loc, '\0', sizeof (*loc));
8186             }
8187           else
8188             loc = ggc_alloc_cleared_var_loc_node ();
8189           if (bitsize == -1 || piece_loc == NULL)
8190             loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8191           else
8192             adjust_piece_list (&loc->loc, &last->loc, piece_loc,
8193                                bitpos, piece_bitpos, bitsize, loc_note);
8194           last->next = loc;
8195           /* Ensure TEMP->LAST will point either to the new last but one
8196              element of the chain, or to the last element in it.  */
8197           if (last != temp->last)
8198             temp->last = last;
8199         }
8200       else if (unused)
8201         ggc_free (unused);
8202     }
8203   else
8204     {
8205       loc = ggc_alloc_cleared_var_loc_node ();
8206       temp->first = loc;
8207       temp->last = loc;
8208       loc->loc = construct_piece_list (loc_note, bitpos, bitsize);
8209     }
8210   return loc;
8211 }
8212 \f
8213 /* Keep track of the number of spaces used to indent the
8214    output of the debugging routines that print the structure of
8215    the DIE internal representation.  */
8216 static int print_indent;
8217
8218 /* Indent the line the number of spaces given by print_indent.  */
8219
8220 static inline void
8221 print_spaces (FILE *outfile)
8222 {
8223   fprintf (outfile, "%*s", print_indent, "");
8224 }
8225
8226 /* Print a type signature in hex.  */
8227
8228 static inline void
8229 print_signature (FILE *outfile, char *sig)
8230 {
8231   int i;
8232
8233   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
8234     fprintf (outfile, "%02x", sig[i] & 0xff);
8235 }
8236
8237 /* Print the information associated with a given DIE, and its children.
8238    This routine is a debugging aid only.  */
8239
8240 static void
8241 print_die (dw_die_ref die, FILE *outfile)
8242 {
8243   dw_attr_ref a;
8244   dw_die_ref c;
8245   unsigned ix;
8246
8247   print_spaces (outfile);
8248   fprintf (outfile, "DIE %4ld: %s\n",
8249            die->die_offset, dwarf_tag_name (die->die_tag));
8250   print_spaces (outfile);
8251   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
8252   fprintf (outfile, " offset: %ld\n", die->die_offset);
8253   if (dwarf_version >= 4 && die->die_id.die_type_node)
8254     {
8255       print_spaces (outfile);
8256       fprintf (outfile, "  signature: ");
8257       print_signature (outfile, die->die_id.die_type_node->signature);
8258       fprintf (outfile, "\n");
8259     }
8260
8261   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8262     {
8263       print_spaces (outfile);
8264       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
8265
8266       switch (AT_class (a))
8267         {
8268         case dw_val_class_addr:
8269           fprintf (outfile, "address");
8270           break;
8271         case dw_val_class_offset:
8272           fprintf (outfile, "offset");
8273           break;
8274         case dw_val_class_loc:
8275           fprintf (outfile, "location descriptor");
8276           break;
8277         case dw_val_class_loc_list:
8278           fprintf (outfile, "location list -> label:%s",
8279                    AT_loc_list (a)->ll_symbol);
8280           break;
8281         case dw_val_class_range_list:
8282           fprintf (outfile, "range list");
8283           break;
8284         case dw_val_class_const:
8285           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
8286           break;
8287         case dw_val_class_unsigned_const:
8288           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
8289           break;
8290         case dw_val_class_const_double:
8291           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
8292                             HOST_WIDE_INT_PRINT_UNSIGNED")",
8293                    a->dw_attr_val.v.val_double.high,
8294                    a->dw_attr_val.v.val_double.low);
8295           break;
8296         case dw_val_class_vec:
8297           fprintf (outfile, "floating-point or vector constant");
8298           break;
8299         case dw_val_class_flag:
8300           fprintf (outfile, "%u", AT_flag (a));
8301           break;
8302         case dw_val_class_die_ref:
8303           if (AT_ref (a) != NULL)
8304             {
8305               if (dwarf_version >= 4 && AT_ref (a)->die_id.die_type_node)
8306                 {
8307                   fprintf (outfile, "die -> signature: ");
8308                   print_signature (outfile,
8309                                    AT_ref (a)->die_id.die_type_node->signature);
8310                 }
8311               else if (dwarf_version < 4 && AT_ref (a)->die_id.die_symbol)
8312                 fprintf (outfile, "die -> label: %s",
8313                          AT_ref (a)->die_id.die_symbol);
8314               else
8315                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
8316             }
8317           else
8318             fprintf (outfile, "die -> <null>");
8319           break;
8320         case dw_val_class_vms_delta:
8321           fprintf (outfile, "delta: @slotcount(%s-%s)",
8322                    AT_vms_delta2 (a), AT_vms_delta1 (a));
8323           break;
8324         case dw_val_class_lbl_id:
8325         case dw_val_class_lineptr:
8326         case dw_val_class_macptr:
8327           fprintf (outfile, "label: %s", AT_lbl (a));
8328           break;
8329         case dw_val_class_str:
8330           if (AT_string (a) != NULL)
8331             fprintf (outfile, "\"%s\"", AT_string (a));
8332           else
8333             fprintf (outfile, "<null>");
8334           break;
8335         case dw_val_class_file:
8336           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
8337                    AT_file (a)->emitted_number);
8338           break;
8339         case dw_val_class_data8:
8340           {
8341             int i;
8342
8343             for (i = 0; i < 8; i++)
8344               fprintf (outfile, "%02x", a->dw_attr_val.v.val_data8[i]);
8345             break;
8346           }
8347         default:
8348           break;
8349         }
8350
8351       fprintf (outfile, "\n");
8352     }
8353
8354   if (die->die_child != NULL)
8355     {
8356       print_indent += 4;
8357       FOR_EACH_CHILD (die, c, print_die (c, outfile));
8358       print_indent -= 4;
8359     }
8360   if (print_indent == 0)
8361     fprintf (outfile, "\n");
8362 }
8363
8364 /* Print the contents of the source code line number correspondence table.
8365    This routine is a debugging aid only.  */
8366
8367 static void
8368 print_dwarf_line_table (FILE *outfile)
8369 {
8370   unsigned i;
8371   dw_line_info_ref line_info;
8372
8373   fprintf (outfile, "\n\nDWARF source line information\n");
8374   for (i = 1; i < line_info_table_in_use; i++)
8375     {
8376       line_info = &line_info_table[i];
8377       fprintf (outfile, "%5d: %4ld %6ld\n", i,
8378                line_info->dw_file_num,
8379                line_info->dw_line_num);
8380     }
8381
8382   fprintf (outfile, "\n\n");
8383 }
8384
8385 /* Print the information collected for a given DIE.  */
8386
8387 DEBUG_FUNCTION void
8388 debug_dwarf_die (dw_die_ref die)
8389 {
8390   print_die (die, stderr);
8391 }
8392
8393 /* Print all DWARF information collected for the compilation unit.
8394    This routine is a debugging aid only.  */
8395
8396 DEBUG_FUNCTION void
8397 debug_dwarf (void)
8398 {
8399   print_indent = 0;
8400   print_die (comp_unit_die, stderr);
8401   if (! DWARF2_ASM_LINE_DEBUG_INFO)
8402     print_dwarf_line_table (stderr);
8403 }
8404 \f
8405 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
8406    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
8407    DIE that marks the start of the DIEs for this include file.  */
8408
8409 static dw_die_ref
8410 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
8411 {
8412   const char *filename = get_AT_string (bincl_die, DW_AT_name);
8413   dw_die_ref new_unit = gen_compile_unit_die (filename);
8414
8415   new_unit->die_sib = old_unit;
8416   return new_unit;
8417 }
8418
8419 /* Close an include-file CU and reopen the enclosing one.  */
8420
8421 static dw_die_ref
8422 pop_compile_unit (dw_die_ref old_unit)
8423 {
8424   dw_die_ref new_unit = old_unit->die_sib;
8425
8426   old_unit->die_sib = NULL;
8427   return new_unit;
8428 }
8429
8430 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8431 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
8432
8433 /* Calculate the checksum of a location expression.  */
8434
8435 static inline void
8436 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8437 {
8438   int tem;
8439
8440   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
8441   CHECKSUM (tem);
8442   CHECKSUM (loc->dw_loc_oprnd1);
8443   CHECKSUM (loc->dw_loc_oprnd2);
8444 }
8445
8446 /* Calculate the checksum of an attribute.  */
8447
8448 static void
8449 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
8450 {
8451   dw_loc_descr_ref loc;
8452   rtx r;
8453
8454   CHECKSUM (at->dw_attr);
8455
8456   /* We don't care that this was compiled with a different compiler
8457      snapshot; if the output is the same, that's what matters.  */
8458   if (at->dw_attr == DW_AT_producer)
8459     return;
8460
8461   switch (AT_class (at))
8462     {
8463     case dw_val_class_const:
8464       CHECKSUM (at->dw_attr_val.v.val_int);
8465       break;
8466     case dw_val_class_unsigned_const:
8467       CHECKSUM (at->dw_attr_val.v.val_unsigned);
8468       break;
8469     case dw_val_class_const_double:
8470       CHECKSUM (at->dw_attr_val.v.val_double);
8471       break;
8472     case dw_val_class_vec:
8473       CHECKSUM (at->dw_attr_val.v.val_vec);
8474       break;
8475     case dw_val_class_flag:
8476       CHECKSUM (at->dw_attr_val.v.val_flag);
8477       break;
8478     case dw_val_class_str:
8479       CHECKSUM_STRING (AT_string (at));
8480       break;
8481
8482     case dw_val_class_addr:
8483       r = AT_addr (at);
8484       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8485       CHECKSUM_STRING (XSTR (r, 0));
8486       break;
8487
8488     case dw_val_class_offset:
8489       CHECKSUM (at->dw_attr_val.v.val_offset);
8490       break;
8491
8492     case dw_val_class_loc:
8493       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8494         loc_checksum (loc, ctx);
8495       break;
8496
8497     case dw_val_class_die_ref:
8498       die_checksum (AT_ref (at), ctx, mark);
8499       break;
8500
8501     case dw_val_class_fde_ref:
8502     case dw_val_class_vms_delta:
8503     case dw_val_class_lbl_id:
8504     case dw_val_class_lineptr:
8505     case dw_val_class_macptr:
8506       break;
8507
8508     case dw_val_class_file:
8509       CHECKSUM_STRING (AT_file (at)->filename);
8510       break;
8511
8512     case dw_val_class_data8:
8513       CHECKSUM (at->dw_attr_val.v.val_data8);
8514       break;
8515
8516     default:
8517       break;
8518     }
8519 }
8520
8521 /* Calculate the checksum of a DIE.  */
8522
8523 static void
8524 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
8525 {
8526   dw_die_ref c;
8527   dw_attr_ref a;
8528   unsigned ix;
8529
8530   /* To avoid infinite recursion.  */
8531   if (die->die_mark)
8532     {
8533       CHECKSUM (die->die_mark);
8534       return;
8535     }
8536   die->die_mark = ++(*mark);
8537
8538   CHECKSUM (die->die_tag);
8539
8540   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8541     attr_checksum (a, ctx, mark);
8542
8543   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
8544 }
8545
8546 #undef CHECKSUM
8547 #undef CHECKSUM_STRING
8548
8549 /* For DWARF-4 types, include the trailing NULL when checksumming strings.  */
8550 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
8551 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO) + 1, ctx)
8552 #define CHECKSUM_SLEB128(FOO) checksum_sleb128 ((FOO), ctx)
8553 #define CHECKSUM_ULEB128(FOO) checksum_uleb128 ((FOO), ctx)
8554 #define CHECKSUM_ATTR(FOO) \
8555   if (FOO) attr_checksum_ordered (die->die_tag, (FOO), ctx, mark)
8556
8557 /* Calculate the checksum of a number in signed LEB128 format.  */
8558
8559 static void
8560 checksum_sleb128 (HOST_WIDE_INT value, struct md5_ctx *ctx)
8561 {
8562   unsigned char byte;
8563   bool more;
8564
8565   while (1)
8566     {
8567       byte = (value & 0x7f);
8568       value >>= 7;
8569       more = !((value == 0 && (byte & 0x40) == 0)
8570                 || (value == -1 && (byte & 0x40) != 0));
8571       if (more)
8572         byte |= 0x80;
8573       CHECKSUM (byte);
8574       if (!more)
8575         break;
8576     }
8577 }
8578
8579 /* Calculate the checksum of a number in unsigned LEB128 format.  */
8580
8581 static void
8582 checksum_uleb128 (unsigned HOST_WIDE_INT value, struct md5_ctx *ctx)
8583 {
8584   while (1)
8585     {
8586       unsigned char byte = (value & 0x7f);
8587       value >>= 7;
8588       if (value != 0)
8589         /* More bytes to follow.  */
8590         byte |= 0x80;
8591       CHECKSUM (byte);
8592       if (value == 0)
8593         break;
8594     }
8595 }
8596
8597 /* Checksum the context of the DIE.  This adds the names of any
8598    surrounding namespaces or structures to the checksum.  */
8599
8600 static void
8601 checksum_die_context (dw_die_ref die, struct md5_ctx *ctx)
8602 {
8603   const char *name;
8604   dw_die_ref spec;
8605   int tag = die->die_tag;
8606
8607   if (tag != DW_TAG_namespace
8608       && tag != DW_TAG_structure_type
8609       && tag != DW_TAG_class_type)
8610     return;
8611
8612   name = get_AT_string (die, DW_AT_name);
8613
8614   spec = get_AT_ref (die, DW_AT_specification);
8615   if (spec != NULL)
8616     die = spec;
8617
8618   if (die->die_parent != NULL)
8619     checksum_die_context (die->die_parent, ctx);
8620
8621   CHECKSUM_ULEB128 ('C');
8622   CHECKSUM_ULEB128 (tag);
8623   if (name != NULL)
8624     CHECKSUM_STRING (name);
8625 }
8626
8627 /* Calculate the checksum of a location expression.  */
8628
8629 static inline void
8630 loc_checksum_ordered (dw_loc_descr_ref loc, struct md5_ctx *ctx)
8631 {
8632   /* Special case for lone DW_OP_plus_uconst: checksum as if the location
8633      were emitted as a DW_FORM_sdata instead of a location expression.  */
8634   if (loc->dw_loc_opc == DW_OP_plus_uconst && loc->dw_loc_next == NULL)
8635     {
8636       CHECKSUM_ULEB128 (DW_FORM_sdata);
8637       CHECKSUM_SLEB128 ((HOST_WIDE_INT) loc->dw_loc_oprnd1.v.val_unsigned);
8638       return;
8639     }
8640
8641   /* Otherwise, just checksum the raw location expression.  */
8642   while (loc != NULL)
8643     {
8644       CHECKSUM_ULEB128 (loc->dw_loc_opc);
8645       CHECKSUM (loc->dw_loc_oprnd1);
8646       CHECKSUM (loc->dw_loc_oprnd2);
8647       loc = loc->dw_loc_next;
8648     }
8649 }
8650
8651 /* Calculate the checksum of an attribute.  */
8652
8653 static void
8654 attr_checksum_ordered (enum dwarf_tag tag, dw_attr_ref at,
8655                        struct md5_ctx *ctx, int *mark)
8656 {
8657   dw_loc_descr_ref loc;
8658   rtx r;
8659
8660   if (AT_class (at) == dw_val_class_die_ref)
8661     {
8662       dw_die_ref target_die = AT_ref (at);
8663
8664       /* For pointer and reference types, we checksum only the (qualified)
8665          name of the target type (if there is a name).  For friend entries,
8666          we checksum only the (qualified) name of the target type or function.
8667          This allows the checksum to remain the same whether the target type
8668          is complete or not.  */
8669       if ((at->dw_attr == DW_AT_type
8670            && (tag == DW_TAG_pointer_type
8671                || tag == DW_TAG_reference_type
8672                || tag == DW_TAG_rvalue_reference_type
8673                || tag == DW_TAG_ptr_to_member_type))
8674           || (at->dw_attr == DW_AT_friend
8675               && tag == DW_TAG_friend))
8676         {
8677           dw_attr_ref name_attr = get_AT (target_die, DW_AT_name);
8678
8679           if (name_attr != NULL)
8680             {
8681               dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8682
8683               if (decl == NULL)
8684                 decl = target_die;
8685               CHECKSUM_ULEB128 ('N');
8686               CHECKSUM_ULEB128 (at->dw_attr);
8687               if (decl->die_parent != NULL)
8688                 checksum_die_context (decl->die_parent, ctx);
8689               CHECKSUM_ULEB128 ('E');
8690               CHECKSUM_STRING (AT_string (name_attr));
8691               return;
8692             }
8693         }
8694
8695       /* For all other references to another DIE, we check to see if the
8696          target DIE has already been visited.  If it has, we emit a
8697          backward reference; if not, we descend recursively.  */
8698       if (target_die->die_mark > 0)
8699         {
8700           CHECKSUM_ULEB128 ('R');
8701           CHECKSUM_ULEB128 (at->dw_attr);
8702           CHECKSUM_ULEB128 (target_die->die_mark);
8703         }
8704       else
8705         {
8706           dw_die_ref decl = get_AT_ref (target_die, DW_AT_specification);
8707
8708           if (decl == NULL)
8709             decl = target_die;
8710           target_die->die_mark = ++(*mark);
8711           CHECKSUM_ULEB128 ('T');
8712           CHECKSUM_ULEB128 (at->dw_attr);
8713           if (decl->die_parent != NULL)
8714             checksum_die_context (decl->die_parent, ctx);
8715           die_checksum_ordered (target_die, ctx, mark);
8716         }
8717       return;
8718     }
8719
8720   CHECKSUM_ULEB128 ('A');
8721   CHECKSUM_ULEB128 (at->dw_attr);
8722
8723   switch (AT_class (at))
8724     {
8725     case dw_val_class_const:
8726       CHECKSUM_ULEB128 (DW_FORM_sdata);
8727       CHECKSUM_SLEB128 (at->dw_attr_val.v.val_int);
8728       break;
8729
8730     case dw_val_class_unsigned_const:
8731       CHECKSUM_ULEB128 (DW_FORM_sdata);
8732       CHECKSUM_SLEB128 ((int) at->dw_attr_val.v.val_unsigned);
8733       break;
8734
8735     case dw_val_class_const_double:
8736       CHECKSUM_ULEB128 (DW_FORM_block);
8737       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_double));
8738       CHECKSUM (at->dw_attr_val.v.val_double);
8739       break;
8740
8741     case dw_val_class_vec:
8742       CHECKSUM_ULEB128 (DW_FORM_block);
8743       CHECKSUM_ULEB128 (sizeof (at->dw_attr_val.v.val_vec));
8744       CHECKSUM (at->dw_attr_val.v.val_vec);
8745       break;
8746
8747     case dw_val_class_flag:
8748       CHECKSUM_ULEB128 (DW_FORM_flag);
8749       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_flag ? 1 : 0);
8750       break;
8751
8752     case dw_val_class_str:
8753       CHECKSUM_ULEB128 (DW_FORM_string);
8754       CHECKSUM_STRING (AT_string (at));
8755       break;
8756
8757     case dw_val_class_addr:
8758       r = AT_addr (at);
8759       gcc_assert (GET_CODE (r) == SYMBOL_REF);
8760       CHECKSUM_ULEB128 (DW_FORM_string);
8761       CHECKSUM_STRING (XSTR (r, 0));
8762       break;
8763
8764     case dw_val_class_offset:
8765       CHECKSUM_ULEB128 (DW_FORM_sdata);
8766       CHECKSUM_ULEB128 (at->dw_attr_val.v.val_offset);
8767       break;
8768
8769     case dw_val_class_loc:
8770       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
8771         loc_checksum_ordered (loc, ctx);
8772       break;
8773
8774     case dw_val_class_fde_ref:
8775     case dw_val_class_lbl_id:
8776     case dw_val_class_lineptr:
8777     case dw_val_class_macptr:
8778       break;
8779
8780     case dw_val_class_file:
8781       CHECKSUM_ULEB128 (DW_FORM_string);
8782       CHECKSUM_STRING (AT_file (at)->filename);
8783       break;
8784
8785     case dw_val_class_data8:
8786       CHECKSUM (at->dw_attr_val.v.val_data8);
8787       break;
8788
8789     default:
8790       break;
8791     }
8792 }
8793
8794 struct checksum_attributes
8795 {
8796   dw_attr_ref at_name;
8797   dw_attr_ref at_type;
8798   dw_attr_ref at_friend;
8799   dw_attr_ref at_accessibility;
8800   dw_attr_ref at_address_class;
8801   dw_attr_ref at_allocated;
8802   dw_attr_ref at_artificial;
8803   dw_attr_ref at_associated;
8804   dw_attr_ref at_binary_scale;
8805   dw_attr_ref at_bit_offset;
8806   dw_attr_ref at_bit_size;
8807   dw_attr_ref at_bit_stride;
8808   dw_attr_ref at_byte_size;
8809   dw_attr_ref at_byte_stride;
8810   dw_attr_ref at_const_value;
8811   dw_attr_ref at_containing_type;
8812   dw_attr_ref at_count;
8813   dw_attr_ref at_data_location;
8814   dw_attr_ref at_data_member_location;
8815   dw_attr_ref at_decimal_scale;
8816   dw_attr_ref at_decimal_sign;
8817   dw_attr_ref at_default_value;
8818   dw_attr_ref at_digit_count;
8819   dw_attr_ref at_discr;
8820   dw_attr_ref at_discr_list;
8821   dw_attr_ref at_discr_value;
8822   dw_attr_ref at_encoding;
8823   dw_attr_ref at_endianity;
8824   dw_attr_ref at_explicit;
8825   dw_attr_ref at_is_optional;
8826   dw_attr_ref at_location;
8827   dw_attr_ref at_lower_bound;
8828   dw_attr_ref at_mutable;
8829   dw_attr_ref at_ordering;
8830   dw_attr_ref at_picture_string;
8831   dw_attr_ref at_prototyped;
8832   dw_attr_ref at_small;
8833   dw_attr_ref at_segment;
8834   dw_attr_ref at_string_length;
8835   dw_attr_ref at_threads_scaled;
8836   dw_attr_ref at_upper_bound;
8837   dw_attr_ref at_use_location;
8838   dw_attr_ref at_use_UTF8;
8839   dw_attr_ref at_variable_parameter;
8840   dw_attr_ref at_virtuality;
8841   dw_attr_ref at_visibility;
8842   dw_attr_ref at_vtable_elem_location;
8843 };
8844
8845 /* Collect the attributes that we will want to use for the checksum.  */
8846
8847 static void
8848 collect_checksum_attributes (struct checksum_attributes *attrs, dw_die_ref die)
8849 {
8850   dw_attr_ref a;
8851   unsigned ix;
8852
8853   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8854     {
8855       switch (a->dw_attr)
8856         {
8857         case DW_AT_name:
8858           attrs->at_name = a;
8859           break;
8860         case DW_AT_type:
8861           attrs->at_type = a;
8862           break;
8863         case DW_AT_friend:
8864           attrs->at_friend = a;
8865           break;
8866         case DW_AT_accessibility:
8867           attrs->at_accessibility = a;
8868           break;
8869         case DW_AT_address_class:
8870           attrs->at_address_class = a;
8871           break;
8872         case DW_AT_allocated:
8873           attrs->at_allocated = a;
8874           break;
8875         case DW_AT_artificial:
8876           attrs->at_artificial = a;
8877           break;
8878         case DW_AT_associated:
8879           attrs->at_associated = a;
8880           break;
8881         case DW_AT_binary_scale:
8882           attrs->at_binary_scale = a;
8883           break;
8884         case DW_AT_bit_offset:
8885           attrs->at_bit_offset = a;
8886           break;
8887         case DW_AT_bit_size:
8888           attrs->at_bit_size = a;
8889           break;
8890         case DW_AT_bit_stride:
8891           attrs->at_bit_stride = a;
8892           break;
8893         case DW_AT_byte_size:
8894           attrs->at_byte_size = a;
8895           break;
8896         case DW_AT_byte_stride:
8897           attrs->at_byte_stride = a;
8898           break;
8899         case DW_AT_const_value:
8900           attrs->at_const_value = a;
8901           break;
8902         case DW_AT_containing_type:
8903           attrs->at_containing_type = a;
8904           break;
8905         case DW_AT_count:
8906           attrs->at_count = a;
8907           break;
8908         case DW_AT_data_location:
8909           attrs->at_data_location = a;
8910           break;
8911         case DW_AT_data_member_location:
8912           attrs->at_data_member_location = a;
8913           break;
8914         case DW_AT_decimal_scale:
8915           attrs->at_decimal_scale = a;
8916           break;
8917         case DW_AT_decimal_sign:
8918           attrs->at_decimal_sign = a;
8919           break;
8920         case DW_AT_default_value:
8921           attrs->at_default_value = a;
8922           break;
8923         case DW_AT_digit_count:
8924           attrs->at_digit_count = a;
8925           break;
8926         case DW_AT_discr:
8927           attrs->at_discr = a;
8928           break;
8929         case DW_AT_discr_list:
8930           attrs->at_discr_list = a;
8931           break;
8932         case DW_AT_discr_value:
8933           attrs->at_discr_value = a;
8934           break;
8935         case DW_AT_encoding:
8936           attrs->at_encoding = a;
8937           break;
8938         case DW_AT_endianity:
8939           attrs->at_endianity = a;
8940           break;
8941         case DW_AT_explicit:
8942           attrs->at_explicit = a;
8943           break;
8944         case DW_AT_is_optional:
8945           attrs->at_is_optional = a;
8946           break;
8947         case DW_AT_location:
8948           attrs->at_location = a;
8949           break;
8950         case DW_AT_lower_bound:
8951           attrs->at_lower_bound = a;
8952           break;
8953         case DW_AT_mutable:
8954           attrs->at_mutable = a;
8955           break;
8956         case DW_AT_ordering:
8957           attrs->at_ordering = a;
8958           break;
8959         case DW_AT_picture_string:
8960           attrs->at_picture_string = a;
8961           break;
8962         case DW_AT_prototyped:
8963           attrs->at_prototyped = a;
8964           break;
8965         case DW_AT_small:
8966           attrs->at_small = a;
8967           break;
8968         case DW_AT_segment:
8969           attrs->at_segment = a;
8970           break;
8971         case DW_AT_string_length:
8972           attrs->at_string_length = a;
8973           break;
8974         case DW_AT_threads_scaled:
8975           attrs->at_threads_scaled = a;
8976           break;
8977         case DW_AT_upper_bound:
8978           attrs->at_upper_bound = a;
8979           break;
8980         case DW_AT_use_location:
8981           attrs->at_use_location = a;
8982           break;
8983         case DW_AT_use_UTF8:
8984           attrs->at_use_UTF8 = a;
8985           break;
8986         case DW_AT_variable_parameter:
8987           attrs->at_variable_parameter = a;
8988           break;
8989         case DW_AT_virtuality:
8990           attrs->at_virtuality = a;
8991           break;
8992         case DW_AT_visibility:
8993           attrs->at_visibility = a;
8994           break;
8995         case DW_AT_vtable_elem_location:
8996           attrs->at_vtable_elem_location = a;
8997           break;
8998         default:
8999           break;
9000         }
9001     }
9002 }
9003
9004 /* Calculate the checksum of a DIE, using an ordered subset of attributes.  */
9005
9006 static void
9007 die_checksum_ordered (dw_die_ref die, struct md5_ctx *ctx, int *mark)
9008 {
9009   dw_die_ref c;
9010   dw_die_ref decl;
9011   struct checksum_attributes attrs;
9012
9013   CHECKSUM_ULEB128 ('D');
9014   CHECKSUM_ULEB128 (die->die_tag);
9015
9016   memset (&attrs, 0, sizeof (attrs));
9017
9018   decl = get_AT_ref (die, DW_AT_specification);
9019   if (decl != NULL)
9020     collect_checksum_attributes (&attrs, decl);
9021   collect_checksum_attributes (&attrs, die);
9022
9023   CHECKSUM_ATTR (attrs.at_name);
9024   CHECKSUM_ATTR (attrs.at_accessibility);
9025   CHECKSUM_ATTR (attrs.at_address_class);
9026   CHECKSUM_ATTR (attrs.at_allocated);
9027   CHECKSUM_ATTR (attrs.at_artificial);
9028   CHECKSUM_ATTR (attrs.at_associated);
9029   CHECKSUM_ATTR (attrs.at_binary_scale);
9030   CHECKSUM_ATTR (attrs.at_bit_offset);
9031   CHECKSUM_ATTR (attrs.at_bit_size);
9032   CHECKSUM_ATTR (attrs.at_bit_stride);
9033   CHECKSUM_ATTR (attrs.at_byte_size);
9034   CHECKSUM_ATTR (attrs.at_byte_stride);
9035   CHECKSUM_ATTR (attrs.at_const_value);
9036   CHECKSUM_ATTR (attrs.at_containing_type);
9037   CHECKSUM_ATTR (attrs.at_count);
9038   CHECKSUM_ATTR (attrs.at_data_location);
9039   CHECKSUM_ATTR (attrs.at_data_member_location);
9040   CHECKSUM_ATTR (attrs.at_decimal_scale);
9041   CHECKSUM_ATTR (attrs.at_decimal_sign);
9042   CHECKSUM_ATTR (attrs.at_default_value);
9043   CHECKSUM_ATTR (attrs.at_digit_count);
9044   CHECKSUM_ATTR (attrs.at_discr);
9045   CHECKSUM_ATTR (attrs.at_discr_list);
9046   CHECKSUM_ATTR (attrs.at_discr_value);
9047   CHECKSUM_ATTR (attrs.at_encoding);
9048   CHECKSUM_ATTR (attrs.at_endianity);
9049   CHECKSUM_ATTR (attrs.at_explicit);
9050   CHECKSUM_ATTR (attrs.at_is_optional);
9051   CHECKSUM_ATTR (attrs.at_location);
9052   CHECKSUM_ATTR (attrs.at_lower_bound);
9053   CHECKSUM_ATTR (attrs.at_mutable);
9054   CHECKSUM_ATTR (attrs.at_ordering);
9055   CHECKSUM_ATTR (attrs.at_picture_string);
9056   CHECKSUM_ATTR (attrs.at_prototyped);
9057   CHECKSUM_ATTR (attrs.at_small);
9058   CHECKSUM_ATTR (attrs.at_segment);
9059   CHECKSUM_ATTR (attrs.at_string_length);
9060   CHECKSUM_ATTR (attrs.at_threads_scaled);
9061   CHECKSUM_ATTR (attrs.at_upper_bound);
9062   CHECKSUM_ATTR (attrs.at_use_location);
9063   CHECKSUM_ATTR (attrs.at_use_UTF8);
9064   CHECKSUM_ATTR (attrs.at_variable_parameter);
9065   CHECKSUM_ATTR (attrs.at_virtuality);
9066   CHECKSUM_ATTR (attrs.at_visibility);
9067   CHECKSUM_ATTR (attrs.at_vtable_elem_location);
9068   CHECKSUM_ATTR (attrs.at_type);
9069   CHECKSUM_ATTR (attrs.at_friend);
9070
9071   /* Checksum the child DIEs, except for nested types and member functions.  */
9072   c = die->die_child;
9073   if (c) do {
9074     dw_attr_ref name_attr;
9075
9076     c = c->die_sib;
9077     name_attr = get_AT (c, DW_AT_name);
9078     if ((is_type_die (c) || c->die_tag == DW_TAG_subprogram)
9079         && name_attr != NULL)
9080       {
9081         CHECKSUM_ULEB128 ('S');
9082         CHECKSUM_ULEB128 (c->die_tag);
9083         CHECKSUM_STRING (AT_string (name_attr));
9084       }
9085     else
9086       {
9087         /* Mark this DIE so it gets processed when unmarking.  */
9088         if (c->die_mark == 0)
9089           c->die_mark = -1;
9090         die_checksum_ordered (c, ctx, mark);
9091       }
9092   } while (c != die->die_child);
9093
9094   CHECKSUM_ULEB128 (0);
9095 }
9096
9097 #undef CHECKSUM
9098 #undef CHECKSUM_STRING
9099 #undef CHECKSUM_ATTR
9100 #undef CHECKSUM_LEB128
9101 #undef CHECKSUM_ULEB128
9102
9103 /* Generate the type signature for DIE.  This is computed by generating an
9104    MD5 checksum over the DIE's tag, its relevant attributes, and its
9105    children.  Attributes that are references to other DIEs are processed
9106    by recursion, using the MARK field to prevent infinite recursion.
9107    If the DIE is nested inside a namespace or another type, we also
9108    need to include that context in the signature.  The lower 64 bits
9109    of the resulting MD5 checksum comprise the signature.  */
9110
9111 static void
9112 generate_type_signature (dw_die_ref die, comdat_type_node *type_node)
9113 {
9114   int mark;
9115   const char *name;
9116   unsigned char checksum[16];
9117   struct md5_ctx ctx;
9118   dw_die_ref decl;
9119
9120   name = get_AT_string (die, DW_AT_name);
9121   decl = get_AT_ref (die, DW_AT_specification);
9122
9123   /* First, compute a signature for just the type name (and its surrounding
9124      context, if any.  This is stored in the type unit DIE for link-time
9125      ODR (one-definition rule) checking.  */
9126
9127   if (is_cxx() && name != NULL)
9128     {
9129       md5_init_ctx (&ctx);
9130
9131       /* Checksum the names of surrounding namespaces and structures.  */
9132       if (decl != NULL && decl->die_parent != NULL)
9133         checksum_die_context (decl->die_parent, &ctx);
9134
9135       md5_process_bytes (&die->die_tag, sizeof (die->die_tag), &ctx);
9136       md5_process_bytes (name, strlen (name) + 1, &ctx);
9137       md5_finish_ctx (&ctx, checksum);
9138
9139       add_AT_data8 (type_node->root_die, DW_AT_GNU_odr_signature, &checksum[8]);
9140     }
9141
9142   /* Next, compute the complete type signature.  */
9143
9144   md5_init_ctx (&ctx);
9145   mark = 1;
9146   die->die_mark = mark;
9147
9148   /* Checksum the names of surrounding namespaces and structures.  */
9149   if (decl != NULL && decl->die_parent != NULL)
9150     checksum_die_context (decl->die_parent, &ctx);
9151
9152   /* Checksum the DIE and its children.  */
9153   die_checksum_ordered (die, &ctx, &mark);
9154   unmark_all_dies (die);
9155   md5_finish_ctx (&ctx, checksum);
9156
9157   /* Store the signature in the type node and link the type DIE and the
9158      type node together.  */
9159   memcpy (type_node->signature, &checksum[16 - DWARF_TYPE_SIGNATURE_SIZE],
9160           DWARF_TYPE_SIGNATURE_SIZE);
9161   die->die_id.die_type_node = type_node;
9162   type_node->type_die = die;
9163
9164   /* If the DIE is a specification, link its declaration to the type node
9165      as well.  */
9166   if (decl != NULL)
9167     decl->die_id.die_type_node = type_node;
9168 }
9169
9170 /* Do the location expressions look same?  */
9171 static inline int
9172 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
9173 {
9174   return loc1->dw_loc_opc == loc2->dw_loc_opc
9175          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
9176          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
9177 }
9178
9179 /* Do the values look the same?  */
9180 static int
9181 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
9182 {
9183   dw_loc_descr_ref loc1, loc2;
9184   rtx r1, r2;
9185
9186   if (v1->val_class != v2->val_class)
9187     return 0;
9188
9189   switch (v1->val_class)
9190     {
9191     case dw_val_class_const:
9192       return v1->v.val_int == v2->v.val_int;
9193     case dw_val_class_unsigned_const:
9194       return v1->v.val_unsigned == v2->v.val_unsigned;
9195     case dw_val_class_const_double:
9196       return v1->v.val_double.high == v2->v.val_double.high
9197              && v1->v.val_double.low == v2->v.val_double.low;
9198     case dw_val_class_vec:
9199       if (v1->v.val_vec.length != v2->v.val_vec.length
9200           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
9201         return 0;
9202       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
9203                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
9204         return 0;
9205       return 1;
9206     case dw_val_class_flag:
9207       return v1->v.val_flag == v2->v.val_flag;
9208     case dw_val_class_str:
9209       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
9210
9211     case dw_val_class_addr:
9212       r1 = v1->v.val_addr;
9213       r2 = v2->v.val_addr;
9214       if (GET_CODE (r1) != GET_CODE (r2))
9215         return 0;
9216       return !rtx_equal_p (r1, r2);
9217
9218     case dw_val_class_offset:
9219       return v1->v.val_offset == v2->v.val_offset;
9220
9221     case dw_val_class_loc:
9222       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
9223            loc1 && loc2;
9224            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
9225         if (!same_loc_p (loc1, loc2, mark))
9226           return 0;
9227       return !loc1 && !loc2;
9228
9229     case dw_val_class_die_ref:
9230       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
9231
9232     case dw_val_class_fde_ref:
9233     case dw_val_class_vms_delta:
9234     case dw_val_class_lbl_id:
9235     case dw_val_class_lineptr:
9236     case dw_val_class_macptr:
9237       return 1;
9238
9239     case dw_val_class_file:
9240       return v1->v.val_file == v2->v.val_file;
9241
9242     case dw_val_class_data8:
9243       return !memcmp (v1->v.val_data8, v2->v.val_data8, 8);
9244
9245     default:
9246       return 1;
9247     }
9248 }
9249
9250 /* Do the attributes look the same?  */
9251
9252 static int
9253 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
9254 {
9255   if (at1->dw_attr != at2->dw_attr)
9256     return 0;
9257
9258   /* We don't care that this was compiled with a different compiler
9259      snapshot; if the output is the same, that's what matters. */
9260   if (at1->dw_attr == DW_AT_producer)
9261     return 1;
9262
9263   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
9264 }
9265
9266 /* Do the dies look the same?  */
9267
9268 static int
9269 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
9270 {
9271   dw_die_ref c1, c2;
9272   dw_attr_ref a1;
9273   unsigned ix;
9274
9275   /* To avoid infinite recursion.  */
9276   if (die1->die_mark)
9277     return die1->die_mark == die2->die_mark;
9278   die1->die_mark = die2->die_mark = ++(*mark);
9279
9280   if (die1->die_tag != die2->die_tag)
9281     return 0;
9282
9283   if (VEC_length (dw_attr_node, die1->die_attr)
9284       != VEC_length (dw_attr_node, die2->die_attr))
9285     return 0;
9286
9287   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
9288     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
9289       return 0;
9290
9291   c1 = die1->die_child;
9292   c2 = die2->die_child;
9293   if (! c1)
9294     {
9295       if (c2)
9296         return 0;
9297     }
9298   else
9299     for (;;)
9300       {
9301         if (!same_die_p (c1, c2, mark))
9302           return 0;
9303         c1 = c1->die_sib;
9304         c2 = c2->die_sib;
9305         if (c1 == die1->die_child)
9306           {
9307             if (c2 == die2->die_child)
9308               break;
9309             else
9310               return 0;
9311           }
9312     }
9313
9314   return 1;
9315 }
9316
9317 /* Do the dies look the same?  Wrapper around same_die_p.  */
9318
9319 static int
9320 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
9321 {
9322   int mark = 0;
9323   int ret = same_die_p (die1, die2, &mark);
9324
9325   unmark_all_dies (die1);
9326   unmark_all_dies (die2);
9327
9328   return ret;
9329 }
9330
9331 /* The prefix to attach to symbols on DIEs in the current comdat debug
9332    info section.  */
9333 static char *comdat_symbol_id;
9334
9335 /* The index of the current symbol within the current comdat CU.  */
9336 static unsigned int comdat_symbol_number;
9337
9338 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
9339    children, and set comdat_symbol_id accordingly.  */
9340
9341 static void
9342 compute_section_prefix (dw_die_ref unit_die)
9343 {
9344   const char *die_name = get_AT_string (unit_die, DW_AT_name);
9345   const char *base = die_name ? lbasename (die_name) : "anonymous";
9346   char *name = XALLOCAVEC (char, strlen (base) + 64);
9347   char *p;
9348   int i, mark;
9349   unsigned char checksum[16];
9350   struct md5_ctx ctx;
9351
9352   /* Compute the checksum of the DIE, then append part of it as hex digits to
9353      the name filename of the unit.  */
9354
9355   md5_init_ctx (&ctx);
9356   mark = 0;
9357   die_checksum (unit_die, &ctx, &mark);
9358   unmark_all_dies (unit_die);
9359   md5_finish_ctx (&ctx, checksum);
9360
9361   sprintf (name, "%s.", base);
9362   clean_symbol_name (name);
9363
9364   p = name + strlen (name);
9365   for (i = 0; i < 4; i++)
9366     {
9367       sprintf (p, "%.2x", checksum[i]);
9368       p += 2;
9369     }
9370
9371   comdat_symbol_id = unit_die->die_id.die_symbol = xstrdup (name);
9372   comdat_symbol_number = 0;
9373 }
9374
9375 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
9376
9377 static int
9378 is_type_die (dw_die_ref die)
9379 {
9380   switch (die->die_tag)
9381     {
9382     case DW_TAG_array_type:
9383     case DW_TAG_class_type:
9384     case DW_TAG_interface_type:
9385     case DW_TAG_enumeration_type:
9386     case DW_TAG_pointer_type:
9387     case DW_TAG_reference_type:
9388     case DW_TAG_rvalue_reference_type:
9389     case DW_TAG_string_type:
9390     case DW_TAG_structure_type:
9391     case DW_TAG_subroutine_type:
9392     case DW_TAG_union_type:
9393     case DW_TAG_ptr_to_member_type:
9394     case DW_TAG_set_type:
9395     case DW_TAG_subrange_type:
9396     case DW_TAG_base_type:
9397     case DW_TAG_const_type:
9398     case DW_TAG_file_type:
9399     case DW_TAG_packed_type:
9400     case DW_TAG_volatile_type:
9401     case DW_TAG_typedef:
9402       return 1;
9403     default:
9404       return 0;
9405     }
9406 }
9407
9408 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
9409    Basically, we want to choose the bits that are likely to be shared between
9410    compilations (types) and leave out the bits that are specific to individual
9411    compilations (functions).  */
9412
9413 static int
9414 is_comdat_die (dw_die_ref c)
9415 {
9416   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
9417      we do for stabs.  The advantage is a greater likelihood of sharing between
9418      objects that don't include headers in the same order (and therefore would
9419      put the base types in a different comdat).  jason 8/28/00 */
9420
9421   if (c->die_tag == DW_TAG_base_type)
9422     return 0;
9423
9424   if (c->die_tag == DW_TAG_pointer_type
9425       || c->die_tag == DW_TAG_reference_type
9426       || c->die_tag == DW_TAG_rvalue_reference_type
9427       || c->die_tag == DW_TAG_const_type
9428       || c->die_tag == DW_TAG_volatile_type)
9429     {
9430       dw_die_ref t = get_AT_ref (c, DW_AT_type);
9431
9432       return t ? is_comdat_die (t) : 0;
9433     }
9434
9435   return is_type_die (c);
9436 }
9437
9438 /* Returns 1 iff C is the sort of DIE that might be referred to from another
9439    compilation unit.  */
9440
9441 static int
9442 is_symbol_die (dw_die_ref c)
9443 {
9444   return (is_type_die (c)
9445           || is_declaration_die (c)
9446           || c->die_tag == DW_TAG_namespace
9447           || c->die_tag == DW_TAG_module);
9448 }
9449
9450 static char *
9451 gen_internal_sym (const char *prefix)
9452 {
9453   char buf[256];
9454
9455   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
9456   return xstrdup (buf);
9457 }
9458
9459 /* Assign symbols to all worthy DIEs under DIE.  */
9460
9461 static void
9462 assign_symbol_names (dw_die_ref die)
9463 {
9464   dw_die_ref c;
9465
9466   if (is_symbol_die (die))
9467     {
9468       if (comdat_symbol_id)
9469         {
9470           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
9471
9472           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
9473                    comdat_symbol_id, comdat_symbol_number++);
9474           die->die_id.die_symbol = xstrdup (p);
9475         }
9476       else
9477         die->die_id.die_symbol = gen_internal_sym ("LDIE");
9478     }
9479
9480   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
9481 }
9482
9483 struct cu_hash_table_entry
9484 {
9485   dw_die_ref cu;
9486   unsigned min_comdat_num, max_comdat_num;
9487   struct cu_hash_table_entry *next;
9488 };
9489
9490 /* Routines to manipulate hash table of CUs.  */
9491 static hashval_t
9492 htab_cu_hash (const void *of)
9493 {
9494   const struct cu_hash_table_entry *const entry =
9495     (const struct cu_hash_table_entry *) of;
9496
9497   return htab_hash_string (entry->cu->die_id.die_symbol);
9498 }
9499
9500 static int
9501 htab_cu_eq (const void *of1, const void *of2)
9502 {
9503   const struct cu_hash_table_entry *const entry1 =
9504     (const struct cu_hash_table_entry *) of1;
9505   const struct die_struct *const entry2 = (const struct die_struct *) of2;
9506
9507   return !strcmp (entry1->cu->die_id.die_symbol, entry2->die_id.die_symbol);
9508 }
9509
9510 static void
9511 htab_cu_del (void *what)
9512 {
9513   struct cu_hash_table_entry *next,
9514     *entry = (struct cu_hash_table_entry *) what;
9515
9516   while (entry)
9517     {
9518       next = entry->next;
9519       free (entry);
9520       entry = next;
9521     }
9522 }
9523
9524 /* Check whether we have already seen this CU and set up SYM_NUM
9525    accordingly.  */
9526 static int
9527 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
9528 {
9529   struct cu_hash_table_entry dummy;
9530   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
9531
9532   dummy.max_comdat_num = 0;
9533
9534   slot = (struct cu_hash_table_entry **)
9535     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9536         INSERT);
9537   entry = *slot;
9538
9539   for (; entry; last = entry, entry = entry->next)
9540     {
9541       if (same_die_p_wrap (cu, entry->cu))
9542         break;
9543     }
9544
9545   if (entry)
9546     {
9547       *sym_num = entry->min_comdat_num;
9548       return 1;
9549     }
9550
9551   entry = XCNEW (struct cu_hash_table_entry);
9552   entry->cu = cu;
9553   entry->min_comdat_num = *sym_num = last->max_comdat_num;
9554   entry->next = *slot;
9555   *slot = entry;
9556
9557   return 0;
9558 }
9559
9560 /* Record SYM_NUM to record of CU in HTABLE.  */
9561 static void
9562 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
9563 {
9564   struct cu_hash_table_entry **slot, *entry;
9565
9566   slot = (struct cu_hash_table_entry **)
9567     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_id.die_symbol),
9568         NO_INSERT);
9569   entry = *slot;
9570
9571   entry->max_comdat_num = sym_num;
9572 }
9573
9574 /* Traverse the DIE (which is always comp_unit_die), and set up
9575    additional compilation units for each of the include files we see
9576    bracketed by BINCL/EINCL.  */
9577
9578 static void
9579 break_out_includes (dw_die_ref die)
9580 {
9581   dw_die_ref c;
9582   dw_die_ref unit = NULL;
9583   limbo_die_node *node, **pnode;
9584   htab_t cu_hash_table;
9585
9586   c = die->die_child;
9587   if (c) do {
9588     dw_die_ref prev = c;
9589     c = c->die_sib;
9590     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
9591            || (unit && is_comdat_die (c)))
9592       {
9593         dw_die_ref next = c->die_sib;
9594
9595         /* This DIE is for a secondary CU; remove it from the main one.  */
9596         remove_child_with_prev (c, prev);
9597
9598         if (c->die_tag == DW_TAG_GNU_BINCL)
9599           unit = push_new_compile_unit (unit, c);
9600         else if (c->die_tag == DW_TAG_GNU_EINCL)
9601           unit = pop_compile_unit (unit);
9602         else
9603           add_child_die (unit, c);
9604         c = next;
9605         if (c == die->die_child)
9606           break;
9607       }
9608   } while (c != die->die_child);
9609
9610 #if 0
9611   /* We can only use this in debugging, since the frontend doesn't check
9612      to make sure that we leave every include file we enter.  */
9613   gcc_assert (!unit);
9614 #endif
9615
9616   assign_symbol_names (die);
9617   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
9618   for (node = limbo_die_list, pnode = &limbo_die_list;
9619        node;
9620        node = node->next)
9621     {
9622       int is_dupl;
9623
9624       compute_section_prefix (node->die);
9625       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
9626                         &comdat_symbol_number);
9627       assign_symbol_names (node->die);
9628       if (is_dupl)
9629         *pnode = node->next;
9630       else
9631         {
9632           pnode = &node->next;
9633           record_comdat_symbol_number (node->die, cu_hash_table,
9634                 comdat_symbol_number);
9635         }
9636     }
9637   htab_delete (cu_hash_table);
9638 }
9639
9640 /* Return non-zero if this DIE is a declaration.  */
9641
9642 static int
9643 is_declaration_die (dw_die_ref die)
9644 {
9645   dw_attr_ref a;
9646   unsigned ix;
9647
9648   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9649     if (a->dw_attr == DW_AT_declaration)
9650       return 1;
9651
9652   return 0;
9653 }
9654
9655 /* Return non-zero if this DIE is nested inside a subprogram.  */
9656
9657 static int
9658 is_nested_in_subprogram (dw_die_ref die)
9659 {
9660   dw_die_ref decl = get_AT_ref (die, DW_AT_specification);
9661
9662   if (decl == NULL)
9663     decl = die;
9664   return local_scope_p (decl);
9665 }
9666
9667 /* Return non-zero if this is a type DIE that should be moved to a
9668    COMDAT .debug_types section.  */
9669
9670 static int
9671 should_move_die_to_comdat (dw_die_ref die)
9672 {
9673   switch (die->die_tag)
9674     {
9675     case DW_TAG_class_type:
9676     case DW_TAG_structure_type:
9677     case DW_TAG_enumeration_type:
9678     case DW_TAG_union_type:
9679       /* Don't move declarations, inlined instances, or types nested in a
9680          subprogram.  */
9681       if (is_declaration_die (die)
9682           || get_AT (die, DW_AT_abstract_origin)
9683           || is_nested_in_subprogram (die))
9684         return 0;
9685       return 1;
9686     case DW_TAG_array_type:
9687     case DW_TAG_interface_type:
9688     case DW_TAG_pointer_type:
9689     case DW_TAG_reference_type:
9690     case DW_TAG_rvalue_reference_type:
9691     case DW_TAG_string_type:
9692     case DW_TAG_subroutine_type:
9693     case DW_TAG_ptr_to_member_type:
9694     case DW_TAG_set_type:
9695     case DW_TAG_subrange_type:
9696     case DW_TAG_base_type:
9697     case DW_TAG_const_type:
9698     case DW_TAG_file_type:
9699     case DW_TAG_packed_type:
9700     case DW_TAG_volatile_type:
9701     case DW_TAG_typedef:
9702     default:
9703       return 0;
9704     }
9705 }
9706
9707 /* Make a clone of DIE.  */
9708
9709 static dw_die_ref
9710 clone_die (dw_die_ref die)
9711 {
9712   dw_die_ref clone;
9713   dw_attr_ref a;
9714   unsigned ix;
9715
9716   clone = ggc_alloc_cleared_die_node ();
9717   clone->die_tag = die->die_tag;
9718
9719   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9720     add_dwarf_attr (clone, a);
9721
9722   return clone;
9723 }
9724
9725 /* Make a clone of the tree rooted at DIE.  */
9726
9727 static dw_die_ref
9728 clone_tree (dw_die_ref die)
9729 {
9730   dw_die_ref c;
9731   dw_die_ref clone = clone_die (die);
9732
9733   FOR_EACH_CHILD (die, c, add_child_die (clone, clone_tree(c)));
9734
9735   return clone;
9736 }
9737
9738 /* Make a clone of DIE as a declaration.  */
9739
9740 static dw_die_ref
9741 clone_as_declaration (dw_die_ref die)
9742 {
9743   dw_die_ref clone;
9744   dw_die_ref decl;
9745   dw_attr_ref a;
9746   unsigned ix;
9747
9748   /* If the DIE is already a declaration, just clone it.  */
9749   if (is_declaration_die (die))
9750     return clone_die (die);
9751
9752   /* If the DIE is a specification, just clone its declaration DIE.  */
9753   decl = get_AT_ref (die, DW_AT_specification);
9754   if (decl != NULL)
9755     return clone_die (decl);
9756
9757   clone = ggc_alloc_cleared_die_node ();
9758   clone->die_tag = die->die_tag;
9759
9760   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
9761     {
9762       /* We don't want to copy over all attributes.
9763          For example we don't want DW_AT_byte_size because otherwise we will no
9764          longer have a declaration and GDB will treat it as a definition.  */
9765
9766       switch (a->dw_attr)
9767         {
9768         case DW_AT_artificial:
9769         case DW_AT_containing_type:
9770         case DW_AT_external:
9771         case DW_AT_name:
9772         case DW_AT_type:
9773         case DW_AT_virtuality:
9774         case DW_AT_linkage_name:
9775         case DW_AT_MIPS_linkage_name:
9776           add_dwarf_attr (clone, a);
9777           break;
9778         case DW_AT_byte_size:
9779         default:
9780           break;
9781         }
9782     }
9783
9784   if (die->die_id.die_type_node)
9785     add_AT_die_ref (clone, DW_AT_signature, die);
9786
9787   add_AT_flag (clone, DW_AT_declaration, 1);
9788   return clone;
9789 }
9790
9791 /* Copy the declaration context to the new compile unit DIE.  This includes
9792    any surrounding namespace or type declarations.  If the DIE has an
9793    AT_specification attribute, it also includes attributes and children
9794    attached to the specification.  */
9795
9796 static void
9797 copy_declaration_context (dw_die_ref unit, dw_die_ref die)
9798 {
9799   dw_die_ref decl;
9800   dw_die_ref new_decl;
9801
9802   decl = get_AT_ref (die, DW_AT_specification);
9803   if (decl == NULL)
9804     decl = die;
9805   else
9806     {
9807       unsigned ix;
9808       dw_die_ref c;
9809       dw_attr_ref a;
9810
9811       /* Copy the type node pointer from the new DIE to the original
9812          declaration DIE so we can forward references later.  */
9813       decl->die_id.die_type_node = die->die_id.die_type_node;
9814
9815       remove_AT (die, DW_AT_specification);
9816
9817       for (ix = 0; VEC_iterate (dw_attr_node, decl->die_attr, ix, a); ix++)
9818         {
9819           if (a->dw_attr != DW_AT_name
9820               && a->dw_attr != DW_AT_declaration
9821               && a->dw_attr != DW_AT_external)
9822             add_dwarf_attr (die, a);
9823         }
9824
9825       FOR_EACH_CHILD (decl, c, add_child_die (die, clone_tree(c)));
9826     }
9827
9828   if (decl->die_parent != NULL
9829       && decl->die_parent->die_tag != DW_TAG_compile_unit
9830       && decl->die_parent->die_tag != DW_TAG_type_unit)
9831     {
9832       new_decl = copy_ancestor_tree (unit, decl, NULL);
9833       if (new_decl != NULL)
9834         {
9835           remove_AT (new_decl, DW_AT_signature);
9836           add_AT_specification (die, new_decl);
9837         }
9838     }
9839 }
9840
9841 /* Generate the skeleton ancestor tree for the given NODE, then clone
9842    the DIE and add the clone into the tree.  */
9843
9844 static void
9845 generate_skeleton_ancestor_tree (skeleton_chain_node *node)
9846 {
9847   if (node->new_die != NULL)
9848     return;
9849
9850   node->new_die = clone_as_declaration (node->old_die);
9851
9852   if (node->parent != NULL)
9853     {
9854       generate_skeleton_ancestor_tree (node->parent);
9855       add_child_die (node->parent->new_die, node->new_die);
9856     }
9857 }
9858
9859 /* Generate a skeleton tree of DIEs containing any declarations that are
9860    found in the original tree.  We traverse the tree looking for declaration
9861    DIEs, and construct the skeleton from the bottom up whenever we find one.  */
9862
9863 static void
9864 generate_skeleton_bottom_up (skeleton_chain_node *parent)
9865 {
9866   skeleton_chain_node node;
9867   dw_die_ref c;
9868   dw_die_ref first;
9869   dw_die_ref prev = NULL;
9870   dw_die_ref next = NULL;
9871
9872   node.parent = parent;
9873
9874   first = c = parent->old_die->die_child;
9875   if (c)
9876     next = c->die_sib;
9877   if (c) do {
9878     if (prev == NULL || prev->die_sib == c)
9879       prev = c;
9880     c = next;
9881     next = (c == first ? NULL : c->die_sib);
9882     node.old_die = c;
9883     node.new_die = NULL;
9884     if (is_declaration_die (c))
9885       {
9886         /* Clone the existing DIE, move the original to the skeleton
9887            tree (which is in the main CU), and put the clone, with
9888            all the original's children, where the original came from.  */
9889         dw_die_ref clone = clone_die (c);
9890         move_all_children (c, clone);
9891
9892         replace_child (c, clone, prev);
9893         generate_skeleton_ancestor_tree (parent);
9894         add_child_die (parent->new_die, c);
9895         node.new_die = c;
9896         c = clone;
9897       }
9898     generate_skeleton_bottom_up (&node);
9899   } while (next != NULL);
9900 }
9901
9902 /* Wrapper function for generate_skeleton_bottom_up.  */
9903
9904 static dw_die_ref
9905 generate_skeleton (dw_die_ref die)
9906 {
9907   skeleton_chain_node node;
9908
9909   node.old_die = die;
9910   node.new_die = NULL;
9911   node.parent = NULL;
9912
9913   /* If this type definition is nested inside another type,
9914      always leave at least a declaration in its place.  */
9915   if (die->die_parent != NULL && is_type_die (die->die_parent))
9916     node.new_die = clone_as_declaration (die);
9917
9918   generate_skeleton_bottom_up (&node);
9919   return node.new_die;
9920 }
9921
9922 /* Remove the DIE from its parent, possibly replacing it with a cloned
9923    declaration.  The original DIE will be moved to a new compile unit
9924    so that existing references to it follow it to the new location.  If
9925    any of the original DIE's descendants is a declaration, we need to
9926    replace the original DIE with a skeleton tree and move the
9927    declarations back into the skeleton tree.  */
9928
9929 static dw_die_ref
9930 remove_child_or_replace_with_skeleton (dw_die_ref child, dw_die_ref prev)
9931 {
9932   dw_die_ref skeleton;
9933
9934   skeleton = generate_skeleton (child);
9935   if (skeleton == NULL)
9936     remove_child_with_prev (child, prev);
9937   else
9938     {
9939       skeleton->die_id.die_type_node = child->die_id.die_type_node;
9940       replace_child (child, skeleton, prev);
9941     }
9942
9943   return skeleton;
9944 }
9945
9946 /* Traverse the DIE and set up additional .debug_types sections for each
9947    type worthy of being placed in a COMDAT section.  */
9948
9949 static void
9950 break_out_comdat_types (dw_die_ref die)
9951 {
9952   dw_die_ref c;
9953   dw_die_ref first;
9954   dw_die_ref prev = NULL;
9955   dw_die_ref next = NULL;
9956   dw_die_ref unit = NULL;
9957
9958   first = c = die->die_child;
9959   if (c)
9960     next = c->die_sib;
9961   if (c) do {
9962     if (prev == NULL || prev->die_sib == c)
9963       prev = c;
9964     c = next;
9965     next = (c == first ? NULL : c->die_sib);
9966     if (should_move_die_to_comdat (c))
9967       {
9968         dw_die_ref replacement;
9969         comdat_type_node_ref type_node;
9970
9971         /* Create a new type unit DIE as the root for the new tree, and
9972            add it to the list of comdat types.  */
9973         unit = new_die (DW_TAG_type_unit, NULL, NULL);
9974         add_AT_unsigned (unit, DW_AT_language,
9975                          get_AT_unsigned (comp_unit_die, DW_AT_language));
9976         type_node = ggc_alloc_cleared_comdat_type_node ();
9977         type_node->root_die = unit;
9978         type_node->next = comdat_type_list;
9979         comdat_type_list = type_node;
9980
9981         /* Generate the type signature.  */
9982         generate_type_signature (c, type_node);
9983
9984         /* Copy the declaration context, attributes, and children of the
9985            declaration into the new compile unit DIE.  */
9986         copy_declaration_context (unit, c);
9987
9988         /* Remove this DIE from the main CU.  */
9989         replacement = remove_child_or_replace_with_skeleton (c, prev);
9990
9991         /* Break out nested types into their own type units.  */
9992         break_out_comdat_types (c);
9993
9994         /* Add the DIE to the new compunit.  */
9995         add_child_die (unit, c);
9996
9997         if (replacement != NULL)
9998           c = replacement;
9999       }
10000     else if (c->die_tag == DW_TAG_namespace
10001              || c->die_tag == DW_TAG_class_type
10002              || c->die_tag == DW_TAG_structure_type
10003              || c->die_tag == DW_TAG_union_type)
10004       {
10005         /* Look for nested types that can be broken out.  */
10006         break_out_comdat_types (c);
10007       }
10008   } while (next != NULL);
10009 }
10010
10011 /* Structure to map a DIE in one CU to its copy in a comdat type unit.  */
10012
10013 struct decl_table_entry
10014 {
10015   dw_die_ref orig;
10016   dw_die_ref copy;
10017 };
10018
10019 /* Routines to manipulate hash table of copied declarations.  */
10020
10021 static hashval_t
10022 htab_decl_hash (const void *of)
10023 {
10024   const struct decl_table_entry *const entry =
10025     (const struct decl_table_entry *) of;
10026
10027   return htab_hash_pointer (entry->orig);
10028 }
10029
10030 static int
10031 htab_decl_eq (const void *of1, const void *of2)
10032 {
10033   const struct decl_table_entry *const entry1 =
10034     (const struct decl_table_entry *) of1;
10035   const struct die_struct *const entry2 = (const struct die_struct *) of2;
10036
10037   return entry1->orig == entry2;
10038 }
10039
10040 static void
10041 htab_decl_del (void *what)
10042 {
10043   struct decl_table_entry *entry = (struct decl_table_entry *) what;
10044
10045   free (entry);
10046 }
10047
10048 /* Copy DIE and its ancestors, up to, but not including, the compile unit
10049    or type unit entry, to a new tree.  Adds the new tree to UNIT and returns
10050    a pointer to the copy of DIE.  If DECL_TABLE is provided, it is used
10051    to check if the ancestor has already been copied into UNIT.  */
10052
10053 static dw_die_ref
10054 copy_ancestor_tree (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10055 {
10056   dw_die_ref parent = die->die_parent;
10057   dw_die_ref new_parent = unit;
10058   dw_die_ref copy;
10059   void **slot = NULL;
10060   struct decl_table_entry *entry = NULL;
10061
10062   if (decl_table)
10063     {
10064       /* Check if the entry has already been copied to UNIT.  */
10065       slot = htab_find_slot_with_hash (decl_table, die,
10066                                        htab_hash_pointer (die), INSERT);
10067       if (*slot != HTAB_EMPTY_ENTRY)
10068         {
10069           entry = (struct decl_table_entry *) *slot;
10070           return entry->copy;
10071         }
10072
10073       /* Record in DECL_TABLE that DIE has been copied to UNIT.  */
10074       entry = XCNEW (struct decl_table_entry);
10075       entry->orig = die;
10076       entry->copy = NULL;
10077       *slot = entry;
10078     }
10079
10080   if (parent != NULL)
10081     {
10082       dw_die_ref spec = get_AT_ref (parent, DW_AT_specification);
10083       if (spec != NULL)
10084         parent = spec;
10085       if (parent->die_tag != DW_TAG_compile_unit
10086           && parent->die_tag != DW_TAG_type_unit)
10087         new_parent = copy_ancestor_tree (unit, parent, decl_table);
10088     }
10089
10090   copy = clone_as_declaration (die);
10091   add_child_die (new_parent, copy);
10092
10093   if (decl_table != NULL)
10094     {
10095       /* Record the pointer to the copy.  */
10096       entry->copy = copy;
10097     }
10098
10099   return copy;
10100 }
10101
10102 /* Walk the DIE and its children, looking for references to incomplete
10103    or trivial types that are unmarked (i.e., that are not in the current
10104    type_unit).  */
10105
10106 static void
10107 copy_decls_walk (dw_die_ref unit, dw_die_ref die, htab_t decl_table)
10108 {
10109   dw_die_ref c;
10110   dw_attr_ref a;
10111   unsigned ix;
10112
10113   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10114     {
10115       if (AT_class (a) == dw_val_class_die_ref)
10116         {
10117           dw_die_ref targ = AT_ref (a);
10118           comdat_type_node_ref type_node = targ->die_id.die_type_node;
10119           void **slot;
10120           struct decl_table_entry *entry;
10121
10122           if (targ->die_mark != 0 || type_node != NULL)
10123             continue;
10124
10125           slot = htab_find_slot_with_hash (decl_table, targ,
10126                                            htab_hash_pointer (targ), INSERT);
10127
10128           if (*slot != HTAB_EMPTY_ENTRY)
10129             {
10130               /* TARG has already been copied, so we just need to
10131                  modify the reference to point to the copy.  */
10132               entry = (struct decl_table_entry *) *slot;
10133               a->dw_attr_val.v.val_die_ref.die = entry->copy;
10134             }
10135           else
10136             {
10137               dw_die_ref parent = unit;
10138               dw_die_ref copy = clone_tree (targ);
10139
10140               /* Make sure the cloned tree is marked as part of the
10141                  type unit.  */
10142               mark_dies (copy);
10143
10144               /* Record in DECL_TABLE that TARG has been copied.
10145                  Need to do this now, before the recursive call,
10146                  because DECL_TABLE may be expanded and SLOT
10147                  would no longer be a valid pointer.  */
10148               entry = XCNEW (struct decl_table_entry);
10149               entry->orig = targ;
10150               entry->copy = copy;
10151               *slot = entry;
10152
10153               /* If TARG has surrounding context, copy its ancestor tree
10154                  into the new type unit.  */
10155               if (targ->die_parent != NULL
10156                   && targ->die_parent->die_tag != DW_TAG_compile_unit
10157                   && targ->die_parent->die_tag != DW_TAG_type_unit)
10158                 parent = copy_ancestor_tree (unit, targ->die_parent,
10159                                              decl_table);
10160
10161               add_child_die (parent, copy);
10162               a->dw_attr_val.v.val_die_ref.die = copy;
10163
10164               /* Make sure the newly-copied DIE is walked.  If it was
10165                  installed in a previously-added context, it won't
10166                  get visited otherwise.  */
10167               if (parent != unit)
10168                 {
10169                   /* Find the highest point of the newly-added tree,
10170                      mark each node along the way, and walk from there.  */
10171                   parent->die_mark = 1;
10172                   while (parent->die_parent
10173                          && parent->die_parent->die_mark == 0)
10174                     {
10175                       parent = parent->die_parent;
10176                       parent->die_mark = 1;
10177                     }
10178                   copy_decls_walk (unit, parent, decl_table);
10179                 }
10180             }
10181         }
10182     }
10183
10184   FOR_EACH_CHILD (die, c, copy_decls_walk (unit, c, decl_table));
10185 }
10186
10187 /* Copy declarations for "unworthy" types into the new comdat section.
10188    Incomplete types, modified types, and certain other types aren't broken
10189    out into comdat sections of their own, so they don't have a signature,
10190    and we need to copy the declaration into the same section so that we
10191    don't have an external reference.  */
10192
10193 static void
10194 copy_decls_for_unworthy_types (dw_die_ref unit)
10195 {
10196   htab_t decl_table;
10197
10198   mark_dies (unit);
10199   decl_table = htab_create (10, htab_decl_hash, htab_decl_eq, htab_decl_del);
10200   copy_decls_walk (unit, unit, decl_table);
10201   htab_delete (decl_table);
10202   unmark_dies (unit);
10203 }
10204
10205 /* Traverse the DIE and add a sibling attribute if it may have the
10206    effect of speeding up access to siblings.  To save some space,
10207    avoid generating sibling attributes for DIE's without children.  */
10208
10209 static void
10210 add_sibling_attributes (dw_die_ref die)
10211 {
10212   dw_die_ref c;
10213
10214   if (! die->die_child)
10215     return;
10216
10217   if (die->die_parent && die != die->die_parent->die_child)
10218     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
10219
10220   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
10221 }
10222
10223 /* Output all location lists for the DIE and its children.  */
10224
10225 static void
10226 output_location_lists (dw_die_ref die)
10227 {
10228   dw_die_ref c;
10229   dw_attr_ref a;
10230   unsigned ix;
10231
10232   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10233     if (AT_class (a) == dw_val_class_loc_list)
10234       output_loc_list (AT_loc_list (a));
10235
10236   FOR_EACH_CHILD (die, c, output_location_lists (c));
10237 }
10238
10239 /* The format of each DIE (and its attribute value pairs) is encoded in an
10240    abbreviation table.  This routine builds the abbreviation table and assigns
10241    a unique abbreviation id for each abbreviation entry.  The children of each
10242    die are visited recursively.  */
10243
10244 static void
10245 build_abbrev_table (dw_die_ref die)
10246 {
10247   unsigned long abbrev_id;
10248   unsigned int n_alloc;
10249   dw_die_ref c;
10250   dw_attr_ref a;
10251   unsigned ix;
10252
10253   /* Scan the DIE references, and mark as external any that refer to
10254      DIEs from other CUs (i.e. those which are not marked).  */
10255   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10256     if (AT_class (a) == dw_val_class_die_ref
10257         && AT_ref (a)->die_mark == 0)
10258       {
10259         gcc_assert (dwarf_version >= 4 || AT_ref (a)->die_id.die_symbol);
10260         set_AT_ref_external (a, 1);
10261       }
10262
10263   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10264     {
10265       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10266       dw_attr_ref die_a, abbrev_a;
10267       unsigned ix;
10268       bool ok = true;
10269
10270       if (abbrev->die_tag != die->die_tag)
10271         continue;
10272       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
10273         continue;
10274
10275       if (VEC_length (dw_attr_node, abbrev->die_attr)
10276           != VEC_length (dw_attr_node, die->die_attr))
10277         continue;
10278
10279       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
10280         {
10281           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
10282           if ((abbrev_a->dw_attr != die_a->dw_attr)
10283               || (value_format (abbrev_a) != value_format (die_a)))
10284             {
10285               ok = false;
10286               break;
10287             }
10288         }
10289       if (ok)
10290         break;
10291     }
10292
10293   if (abbrev_id >= abbrev_die_table_in_use)
10294     {
10295       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
10296         {
10297           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
10298           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
10299                                             n_alloc);
10300
10301           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
10302                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
10303           abbrev_die_table_allocated = n_alloc;
10304         }
10305
10306       ++abbrev_die_table_in_use;
10307       abbrev_die_table[abbrev_id] = die;
10308     }
10309
10310   die->die_abbrev = abbrev_id;
10311   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
10312 }
10313 \f
10314 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
10315
10316 static int
10317 constant_size (unsigned HOST_WIDE_INT value)
10318 {
10319   int log;
10320
10321   if (value == 0)
10322     log = 0;
10323   else
10324     log = floor_log2 (value);
10325
10326   log = log / 8;
10327   log = 1 << (floor_log2 (log) + 1);
10328
10329   return log;
10330 }
10331
10332 /* Return the size of a DIE as it is represented in the
10333    .debug_info section.  */
10334
10335 static unsigned long
10336 size_of_die (dw_die_ref die)
10337 {
10338   unsigned long size = 0;
10339   dw_attr_ref a;
10340   unsigned ix;
10341
10342   size += size_of_uleb128 (die->die_abbrev);
10343   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10344     {
10345       switch (AT_class (a))
10346         {
10347         case dw_val_class_addr:
10348           size += DWARF2_ADDR_SIZE;
10349           break;
10350         case dw_val_class_offset:
10351           size += DWARF_OFFSET_SIZE;
10352           break;
10353         case dw_val_class_loc:
10354           {
10355             unsigned long lsize = size_of_locs (AT_loc (a));
10356
10357             /* Block length.  */
10358             if (dwarf_version >= 4)
10359               size += size_of_uleb128 (lsize);
10360             else
10361               size += constant_size (lsize);
10362             size += lsize;
10363           }
10364           break;
10365         case dw_val_class_loc_list:
10366           size += DWARF_OFFSET_SIZE;
10367           break;
10368         case dw_val_class_range_list:
10369           size += DWARF_OFFSET_SIZE;
10370           break;
10371         case dw_val_class_const:
10372           size += size_of_sleb128 (AT_int (a));
10373           break;
10374         case dw_val_class_unsigned_const:
10375           size += constant_size (AT_unsigned (a));
10376           break;
10377         case dw_val_class_const_double:
10378           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
10379           if (HOST_BITS_PER_WIDE_INT >= 64)
10380             size++; /* block */
10381           break;
10382         case dw_val_class_vec:
10383           size += constant_size (a->dw_attr_val.v.val_vec.length
10384                                  * a->dw_attr_val.v.val_vec.elt_size)
10385                   + a->dw_attr_val.v.val_vec.length
10386                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
10387           break;
10388         case dw_val_class_flag:
10389           if (dwarf_version >= 4)
10390             /* Currently all add_AT_flag calls pass in 1 as last argument,
10391                so DW_FORM_flag_present can be used.  If that ever changes,
10392                we'll need to use DW_FORM_flag and have some optimization
10393                in build_abbrev_table that will change those to
10394                DW_FORM_flag_present if it is set to 1 in all DIEs using
10395                the same abbrev entry.  */
10396             gcc_assert (a->dw_attr_val.v.val_flag == 1);
10397           else
10398             size += 1;
10399           break;
10400         case dw_val_class_die_ref:
10401           if (AT_ref_external (a))
10402             {
10403               /* In DWARF4, we use DW_FORM_sig8; for earlier versions
10404                  we use DW_FORM_ref_addr.  In DWARF2, DW_FORM_ref_addr
10405                  is sized by target address length, whereas in DWARF3
10406                  it's always sized as an offset.  */
10407               if (dwarf_version >= 4)
10408                 size += DWARF_TYPE_SIGNATURE_SIZE;
10409               else if (dwarf_version == 2)
10410                 size += DWARF2_ADDR_SIZE;
10411               else
10412                 size += DWARF_OFFSET_SIZE;
10413             }
10414           else
10415             size += DWARF_OFFSET_SIZE;
10416           break;
10417         case dw_val_class_fde_ref:
10418           size += DWARF_OFFSET_SIZE;
10419           break;
10420         case dw_val_class_lbl_id:
10421           size += DWARF2_ADDR_SIZE;
10422           break;
10423         case dw_val_class_lineptr:
10424         case dw_val_class_macptr:
10425           size += DWARF_OFFSET_SIZE;
10426           break;
10427         case dw_val_class_str:
10428           if (AT_string_form (a) == DW_FORM_strp)
10429             size += DWARF_OFFSET_SIZE;
10430           else
10431             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
10432           break;
10433         case dw_val_class_file:
10434           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
10435           break;
10436         case dw_val_class_data8:
10437           size += 8;
10438           break;
10439         case dw_val_class_vms_delta:
10440           size += DWARF_OFFSET_SIZE;
10441           break;
10442         default:
10443           gcc_unreachable ();
10444         }
10445     }
10446
10447   return size;
10448 }
10449
10450 /* Size the debugging information associated with a given DIE.  Visits the
10451    DIE's children recursively.  Updates the global variable next_die_offset, on
10452    each time through.  Uses the current value of next_die_offset to update the
10453    die_offset field in each DIE.  */
10454
10455 static void
10456 calc_die_sizes (dw_die_ref die)
10457 {
10458   dw_die_ref c;
10459
10460   die->die_offset = next_die_offset;
10461   next_die_offset += size_of_die (die);
10462
10463   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
10464
10465   if (die->die_child != NULL)
10466     /* Count the null byte used to terminate sibling lists.  */
10467     next_die_offset += 1;
10468 }
10469
10470 /* Set the marks for a die and its children.  We do this so
10471    that we know whether or not a reference needs to use FORM_ref_addr; only
10472    DIEs in the same CU will be marked.  We used to clear out the offset
10473    and use that as the flag, but ran into ordering problems.  */
10474
10475 static void
10476 mark_dies (dw_die_ref die)
10477 {
10478   dw_die_ref c;
10479
10480   gcc_assert (!die->die_mark);
10481
10482   die->die_mark = 1;
10483   FOR_EACH_CHILD (die, c, mark_dies (c));
10484 }
10485
10486 /* Clear the marks for a die and its children.  */
10487
10488 static void
10489 unmark_dies (dw_die_ref die)
10490 {
10491   dw_die_ref c;
10492
10493   if (dwarf_version < 4)
10494     gcc_assert (die->die_mark);
10495
10496   die->die_mark = 0;
10497   FOR_EACH_CHILD (die, c, unmark_dies (c));
10498 }
10499
10500 /* Clear the marks for a die, its children and referred dies.  */
10501
10502 static void
10503 unmark_all_dies (dw_die_ref die)
10504 {
10505   dw_die_ref c;
10506   dw_attr_ref a;
10507   unsigned ix;
10508
10509   if (!die->die_mark)
10510     return;
10511   die->die_mark = 0;
10512
10513   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
10514
10515   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10516     if (AT_class (a) == dw_val_class_die_ref)
10517       unmark_all_dies (AT_ref (a));
10518 }
10519
10520 /* Return the size of the .debug_pubnames or .debug_pubtypes table
10521    generated for the compilation unit.  */
10522
10523 static unsigned long
10524 size_of_pubnames (VEC (pubname_entry, gc) * names)
10525 {
10526   unsigned long size;
10527   unsigned i;
10528   pubname_ref p;
10529
10530   size = DWARF_PUBNAMES_HEADER_SIZE;
10531   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
10532     if (names != pubtype_table
10533         || p->die->die_offset != 0
10534         || !flag_eliminate_unused_debug_types)
10535       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
10536
10537   size += DWARF_OFFSET_SIZE;
10538   return size;
10539 }
10540
10541 /* Return the size of the information in the .debug_aranges section.  */
10542
10543 static unsigned long
10544 size_of_aranges (void)
10545 {
10546   unsigned long size;
10547
10548   size = DWARF_ARANGES_HEADER_SIZE;
10549
10550   /* Count the address/length pair for this compilation unit.  */
10551   if (text_section_used)
10552     size += 2 * DWARF2_ADDR_SIZE;
10553   if (cold_text_section_used)
10554     size += 2 * DWARF2_ADDR_SIZE;
10555   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
10556
10557   /* Count the two zero words used to terminated the address range table.  */
10558   size += 2 * DWARF2_ADDR_SIZE;
10559   return size;
10560 }
10561 \f
10562 /* Select the encoding of an attribute value.  */
10563
10564 static enum dwarf_form
10565 value_format (dw_attr_ref a)
10566 {
10567   switch (a->dw_attr_val.val_class)
10568     {
10569     case dw_val_class_addr:
10570       /* Only very few attributes allow DW_FORM_addr.  */
10571       switch (a->dw_attr)
10572         {
10573         case DW_AT_low_pc:
10574         case DW_AT_high_pc:
10575         case DW_AT_entry_pc:
10576         case DW_AT_trampoline:
10577           return DW_FORM_addr;
10578         default:
10579           break;
10580         }
10581       switch (DWARF2_ADDR_SIZE)
10582         {
10583         case 1:
10584           return DW_FORM_data1;
10585         case 2:
10586           return DW_FORM_data2;
10587         case 4:
10588           return DW_FORM_data4;
10589         case 8:
10590           return DW_FORM_data8;
10591         default:
10592           gcc_unreachable ();
10593         }
10594     case dw_val_class_range_list:
10595     case dw_val_class_loc_list:
10596       if (dwarf_version >= 4)
10597         return DW_FORM_sec_offset;
10598       /* FALLTHRU */
10599     case dw_val_class_vms_delta:
10600     case dw_val_class_offset:
10601       switch (DWARF_OFFSET_SIZE)
10602         {
10603         case 4:
10604           return DW_FORM_data4;
10605         case 8:
10606           return DW_FORM_data8;
10607         default:
10608           gcc_unreachable ();
10609         }
10610     case dw_val_class_loc:
10611       if (dwarf_version >= 4)
10612         return DW_FORM_exprloc;
10613       switch (constant_size (size_of_locs (AT_loc (a))))
10614         {
10615         case 1:
10616           return DW_FORM_block1;
10617         case 2:
10618           return DW_FORM_block2;
10619         default:
10620           gcc_unreachable ();
10621         }
10622     case dw_val_class_const:
10623       return DW_FORM_sdata;
10624     case dw_val_class_unsigned_const:
10625       switch (constant_size (AT_unsigned (a)))
10626         {
10627         case 1:
10628           return DW_FORM_data1;
10629         case 2:
10630           return DW_FORM_data2;
10631         case 4:
10632           return DW_FORM_data4;
10633         case 8:
10634           return DW_FORM_data8;
10635         default:
10636           gcc_unreachable ();
10637         }
10638     case dw_val_class_const_double:
10639       switch (HOST_BITS_PER_WIDE_INT)
10640         {
10641         case 8:
10642           return DW_FORM_data2;
10643         case 16:
10644           return DW_FORM_data4;
10645         case 32:
10646           return DW_FORM_data8;
10647         case 64:
10648         default:
10649           return DW_FORM_block1;
10650         }
10651     case dw_val_class_vec:
10652       switch (constant_size (a->dw_attr_val.v.val_vec.length
10653                              * a->dw_attr_val.v.val_vec.elt_size))
10654         {
10655         case 1:
10656           return DW_FORM_block1;
10657         case 2:
10658           return DW_FORM_block2;
10659         case 4:
10660           return DW_FORM_block4;
10661         default:
10662           gcc_unreachable ();
10663         }
10664     case dw_val_class_flag:
10665       if (dwarf_version >= 4)
10666         {
10667           /* Currently all add_AT_flag calls pass in 1 as last argument,
10668              so DW_FORM_flag_present can be used.  If that ever changes,
10669              we'll need to use DW_FORM_flag and have some optimization
10670              in build_abbrev_table that will change those to
10671              DW_FORM_flag_present if it is set to 1 in all DIEs using
10672              the same abbrev entry.  */
10673           gcc_assert (a->dw_attr_val.v.val_flag == 1);
10674           return DW_FORM_flag_present;
10675         }
10676       return DW_FORM_flag;
10677     case dw_val_class_die_ref:
10678       if (AT_ref_external (a))
10679         return dwarf_version >= 4 ? DW_FORM_sig8 : DW_FORM_ref_addr;
10680       else
10681         return DW_FORM_ref;
10682     case dw_val_class_fde_ref:
10683       return DW_FORM_data;
10684     case dw_val_class_lbl_id:
10685       return DW_FORM_addr;
10686     case dw_val_class_lineptr:
10687     case dw_val_class_macptr:
10688       return dwarf_version >= 4 ? DW_FORM_sec_offset : DW_FORM_data;
10689     case dw_val_class_str:
10690       return AT_string_form (a);
10691     case dw_val_class_file:
10692       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
10693         {
10694         case 1:
10695           return DW_FORM_data1;
10696         case 2:
10697           return DW_FORM_data2;
10698         case 4:
10699           return DW_FORM_data4;
10700         default:
10701           gcc_unreachable ();
10702         }
10703
10704     case dw_val_class_data8:
10705       return DW_FORM_data8;
10706
10707     default:
10708       gcc_unreachable ();
10709     }
10710 }
10711
10712 /* Output the encoding of an attribute value.  */
10713
10714 static void
10715 output_value_format (dw_attr_ref a)
10716 {
10717   enum dwarf_form form = value_format (a);
10718
10719   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
10720 }
10721
10722 /* Output the .debug_abbrev section which defines the DIE abbreviation
10723    table.  */
10724
10725 static void
10726 output_abbrev_section (void)
10727 {
10728   unsigned long abbrev_id;
10729
10730   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
10731     {
10732       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
10733       unsigned ix;
10734       dw_attr_ref a_attr;
10735
10736       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
10737       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
10738                                    dwarf_tag_name (abbrev->die_tag));
10739
10740       if (abbrev->die_child != NULL)
10741         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
10742       else
10743         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
10744
10745       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
10746            ix++)
10747         {
10748           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
10749                                        dwarf_attr_name (a_attr->dw_attr));
10750           output_value_format (a_attr);
10751         }
10752
10753       dw2_asm_output_data (1, 0, NULL);
10754       dw2_asm_output_data (1, 0, NULL);
10755     }
10756
10757   /* Terminate the table.  */
10758   dw2_asm_output_data (1, 0, NULL);
10759 }
10760
10761 /* Output a symbol we can use to refer to this DIE from another CU.  */
10762
10763 static inline void
10764 output_die_symbol (dw_die_ref die)
10765 {
10766   char *sym = die->die_id.die_symbol;
10767
10768   if (sym == 0)
10769     return;
10770
10771   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
10772     /* We make these global, not weak; if the target doesn't support
10773        .linkonce, it doesn't support combining the sections, so debugging
10774        will break.  */
10775     targetm.asm_out.globalize_label (asm_out_file, sym);
10776
10777   ASM_OUTPUT_LABEL (asm_out_file, sym);
10778 }
10779
10780 /* Return a new location list, given the begin and end range, and the
10781    expression.  */
10782
10783 static inline dw_loc_list_ref
10784 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
10785               const char *section)
10786 {
10787   dw_loc_list_ref retlist = ggc_alloc_cleared_dw_loc_list_node ();
10788
10789   retlist->begin = begin;
10790   retlist->end = end;
10791   retlist->expr = expr;
10792   retlist->section = section;
10793
10794   return retlist;
10795 }
10796
10797 /* Generate a new internal symbol for this location list node, if it
10798    hasn't got one yet.  */
10799
10800 static inline void
10801 gen_llsym (dw_loc_list_ref list)
10802 {
10803   gcc_assert (!list->ll_symbol);
10804   list->ll_symbol = gen_internal_sym ("LLST");
10805 }
10806
10807 /* Output the location list given to us.  */
10808
10809 static void
10810 output_loc_list (dw_loc_list_ref list_head)
10811 {
10812   dw_loc_list_ref curr = list_head;
10813
10814   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
10815
10816   /* Walk the location list, and output each range + expression.  */
10817   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
10818     {
10819       unsigned long size;
10820       /* Don't output an entry that starts and ends at the same address.  */
10821       if (strcmp (curr->begin, curr->end) == 0)
10822         continue;
10823       if (!have_multiple_function_sections)
10824         {
10825           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
10826                                 "Location list begin address (%s)",
10827                                 list_head->ll_symbol);
10828           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
10829                                 "Location list end address (%s)",
10830                                 list_head->ll_symbol);
10831         }
10832       else
10833         {
10834           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
10835                                "Location list begin address (%s)",
10836                                list_head->ll_symbol);
10837           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
10838                                "Location list end address (%s)",
10839                                list_head->ll_symbol);
10840         }
10841       size = size_of_locs (curr->expr);
10842
10843       /* Output the block length for this list of location operations.  */
10844       gcc_assert (size <= 0xffff);
10845       dw2_asm_output_data (2, size, "%s", "Location expression size");
10846
10847       output_loc_sequence (curr->expr);
10848     }
10849
10850   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10851                        "Location list terminator begin (%s)",
10852                        list_head->ll_symbol);
10853   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
10854                        "Location list terminator end (%s)",
10855                        list_head->ll_symbol);
10856 }
10857
10858 /* Output a type signature.  */
10859
10860 static inline void
10861 output_signature (const char *sig, const char *name)
10862 {
10863   int i;
10864
10865   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
10866     dw2_asm_output_data (1, sig[i], i == 0 ? "%s" : NULL, name);
10867 }
10868
10869 /* Output the DIE and its attributes.  Called recursively to generate
10870    the definitions of each child DIE.  */
10871
10872 static void
10873 output_die (dw_die_ref die)
10874 {
10875   dw_attr_ref a;
10876   dw_die_ref c;
10877   unsigned long size;
10878   unsigned ix;
10879
10880   /* If someone in another CU might refer to us, set up a symbol for
10881      them to point to.  */
10882   if (dwarf_version < 4 && die->die_id.die_symbol)
10883     output_die_symbol (die);
10884
10885   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (%#lx) %s)",
10886                                (unsigned long)die->die_offset,
10887                                dwarf_tag_name (die->die_tag));
10888
10889   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
10890     {
10891       const char *name = dwarf_attr_name (a->dw_attr);
10892
10893       switch (AT_class (a))
10894         {
10895         case dw_val_class_addr:
10896           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
10897           break;
10898
10899         case dw_val_class_offset:
10900           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
10901                                "%s", name);
10902           break;
10903
10904         case dw_val_class_range_list:
10905           {
10906             char *p = strchr (ranges_section_label, '\0');
10907
10908             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
10909                      a->dw_attr_val.v.val_offset);
10910             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
10911                                    debug_ranges_section, "%s", name);
10912             *p = '\0';
10913           }
10914           break;
10915
10916         case dw_val_class_loc:
10917           size = size_of_locs (AT_loc (a));
10918
10919           /* Output the block length for this list of location operations.  */
10920           if (dwarf_version >= 4)
10921             dw2_asm_output_data_uleb128 (size, "%s", name);
10922           else
10923             dw2_asm_output_data (constant_size (size), size, "%s", name);
10924
10925           output_loc_sequence (AT_loc (a));
10926           break;
10927
10928         case dw_val_class_const:
10929           /* ??? It would be slightly more efficient to use a scheme like is
10930              used for unsigned constants below, but gdb 4.x does not sign
10931              extend.  Gdb 5.x does sign extend.  */
10932           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
10933           break;
10934
10935         case dw_val_class_unsigned_const:
10936           dw2_asm_output_data (constant_size (AT_unsigned (a)),
10937                                AT_unsigned (a), "%s", name);
10938           break;
10939
10940         case dw_val_class_const_double:
10941           {
10942             unsigned HOST_WIDE_INT first, second;
10943
10944             if (HOST_BITS_PER_WIDE_INT >= 64)
10945               dw2_asm_output_data (1,
10946                                    2 * HOST_BITS_PER_WIDE_INT
10947                                    / HOST_BITS_PER_CHAR,
10948                                    NULL);
10949
10950             if (WORDS_BIG_ENDIAN)
10951               {
10952                 first = a->dw_attr_val.v.val_double.high;
10953                 second = a->dw_attr_val.v.val_double.low;
10954               }
10955             else
10956               {
10957                 first = a->dw_attr_val.v.val_double.low;
10958                 second = a->dw_attr_val.v.val_double.high;
10959               }
10960
10961             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10962                                  first, name);
10963             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
10964                                  second, NULL);
10965           }
10966           break;
10967
10968         case dw_val_class_vec:
10969           {
10970             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
10971             unsigned int len = a->dw_attr_val.v.val_vec.length;
10972             unsigned int i;
10973             unsigned char *p;
10974
10975             dw2_asm_output_data (constant_size (len * elt_size),
10976                                  len * elt_size, "%s", name);
10977             if (elt_size > sizeof (HOST_WIDE_INT))
10978               {
10979                 elt_size /= 2;
10980                 len *= 2;
10981               }
10982             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
10983                  i < len;
10984                  i++, p += elt_size)
10985               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
10986                                    "fp or vector constant word %u", i);
10987             break;
10988           }
10989
10990         case dw_val_class_flag:
10991           if (dwarf_version >= 4)
10992             {
10993               /* Currently all add_AT_flag calls pass in 1 as last argument,
10994                  so DW_FORM_flag_present can be used.  If that ever changes,
10995                  we'll need to use DW_FORM_flag and have some optimization
10996                  in build_abbrev_table that will change those to
10997                  DW_FORM_flag_present if it is set to 1 in all DIEs using
10998                  the same abbrev entry.  */
10999               gcc_assert (AT_flag (a) == 1);
11000               if (flag_debug_asm)
11001                 fprintf (asm_out_file, "\t\t\t%s %s\n",
11002                          ASM_COMMENT_START, name);
11003               break;
11004             }
11005           dw2_asm_output_data (1, AT_flag (a), "%s", name);
11006           break;
11007
11008         case dw_val_class_loc_list:
11009           {
11010             char *sym = AT_loc_list (a)->ll_symbol;
11011
11012             gcc_assert (sym);
11013             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
11014                                    "%s", name);
11015           }
11016           break;
11017
11018         case dw_val_class_die_ref:
11019           if (AT_ref_external (a))
11020             {
11021               if (dwarf_version >= 4)
11022                 {
11023                   comdat_type_node_ref type_node =
11024                     AT_ref (a)->die_id.die_type_node;
11025
11026                   gcc_assert (type_node);
11027                   output_signature (type_node->signature, name);
11028                 }
11029               else
11030                 {
11031                   char *sym = AT_ref (a)->die_id.die_symbol;
11032                   int size;
11033
11034                   gcc_assert (sym);
11035                   /* In DWARF2, DW_FORM_ref_addr is sized by target address
11036                      length, whereas in DWARF3 it's always sized as an
11037                      offset.  */
11038                   if (dwarf_version == 2)
11039                     size = DWARF2_ADDR_SIZE;
11040                   else
11041                     size = DWARF_OFFSET_SIZE;
11042                   dw2_asm_output_offset (size, sym, debug_info_section, "%s",
11043                                          name);
11044                 }
11045             }
11046           else
11047             {
11048               gcc_assert (AT_ref (a)->die_offset);
11049               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
11050                                    "%s", name);
11051             }
11052           break;
11053
11054         case dw_val_class_fde_ref:
11055           {
11056             char l1[20];
11057
11058             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
11059                                          a->dw_attr_val.v.val_fde_index * 2);
11060             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
11061                                    "%s", name);
11062           }
11063           break;
11064
11065         case dw_val_class_vms_delta:
11066           dw2_asm_output_vms_delta (DWARF_OFFSET_SIZE,
11067                                     AT_vms_delta2 (a), AT_vms_delta1 (a),
11068                                     "%s", name);
11069           break;
11070
11071         case dw_val_class_lbl_id:
11072           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
11073           break;
11074
11075         case dw_val_class_lineptr:
11076           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11077                                  debug_line_section, "%s", name);
11078           break;
11079
11080         case dw_val_class_macptr:
11081           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
11082                                  debug_macinfo_section, "%s", name);
11083           break;
11084
11085         case dw_val_class_str:
11086           if (AT_string_form (a) == DW_FORM_strp)
11087             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
11088                                    a->dw_attr_val.v.val_str->label,
11089                                    debug_str_section,
11090                                    "%s: \"%s\"", name, AT_string (a));
11091           else
11092             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
11093           break;
11094
11095         case dw_val_class_file:
11096           {
11097             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
11098
11099             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
11100                                  a->dw_attr_val.v.val_file->filename);
11101             break;
11102           }
11103
11104         case dw_val_class_data8:
11105           {
11106             int i;
11107
11108             for (i = 0; i < 8; i++)
11109               dw2_asm_output_data (1, a->dw_attr_val.v.val_data8[i],
11110                                    i == 0 ? "%s" : NULL, name);
11111             break;
11112           }
11113
11114         default:
11115           gcc_unreachable ();
11116         }
11117     }
11118
11119   FOR_EACH_CHILD (die, c, output_die (c));
11120
11121   /* Add null byte to terminate sibling list.  */
11122   if (die->die_child != NULL)
11123     dw2_asm_output_data (1, 0, "end of children of DIE %#lx",
11124                          (unsigned long) die->die_offset);
11125 }
11126
11127 /* Output the compilation unit that appears at the beginning of the
11128    .debug_info section, and precedes the DIE descriptions.  */
11129
11130 static void
11131 output_compilation_unit_header (void)
11132 {
11133   int ver = dwarf_version;
11134
11135   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11136     dw2_asm_output_data (4, 0xffffffff,
11137       "Initial length escape value indicating 64-bit DWARF extension");
11138   dw2_asm_output_data (DWARF_OFFSET_SIZE,
11139                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
11140                        "Length of Compilation Unit Info");
11141   dw2_asm_output_data (2, ver, "DWARF version number");
11142   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
11143                          debug_abbrev_section,
11144                          "Offset Into Abbrev. Section");
11145   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
11146 }
11147
11148 /* Output the compilation unit DIE and its children.  */
11149
11150 static void
11151 output_comp_unit (dw_die_ref die, int output_if_empty)
11152 {
11153   const char *secname;
11154   char *oldsym, *tmp;
11155
11156   /* Unless we are outputting main CU, we may throw away empty ones.  */
11157   if (!output_if_empty && die->die_child == NULL)
11158     return;
11159
11160   /* Even if there are no children of this DIE, we must output the information
11161      about the compilation unit.  Otherwise, on an empty translation unit, we
11162      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
11163      will then complain when examining the file.  First mark all the DIEs in
11164      this CU so we know which get local refs.  */
11165   mark_dies (die);
11166
11167   build_abbrev_table (die);
11168
11169   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11170   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
11171   calc_die_sizes (die);
11172
11173   oldsym = die->die_id.die_symbol;
11174   if (oldsym)
11175     {
11176       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
11177
11178       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
11179       secname = tmp;
11180       die->die_id.die_symbol = NULL;
11181       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11182     }
11183   else
11184     switch_to_section (debug_info_section);
11185
11186   /* Output debugging information.  */
11187   output_compilation_unit_header ();
11188   output_die (die);
11189
11190   /* Leave the marks on the main CU, so we can check them in
11191      output_pubnames.  */
11192   if (oldsym)
11193     {
11194       unmark_dies (die);
11195       die->die_id.die_symbol = oldsym;
11196     }
11197 }
11198
11199 /* Output a comdat type unit DIE and its children.  */
11200
11201 static void
11202 output_comdat_type_unit (comdat_type_node *node)
11203 {
11204   const char *secname;
11205   char *tmp;
11206   int i;
11207 #if defined (OBJECT_FORMAT_ELF)
11208   tree comdat_key;
11209 #endif
11210
11211   /* First mark all the DIEs in this CU so we know which get local refs.  */
11212   mark_dies (node->root_die);
11213
11214   build_abbrev_table (node->root_die);
11215
11216   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
11217   next_die_offset = DWARF_COMDAT_TYPE_UNIT_HEADER_SIZE;
11218   calc_die_sizes (node->root_die);
11219
11220 #if defined (OBJECT_FORMAT_ELF)
11221   secname = ".debug_types";
11222   tmp = XALLOCAVEC (char, 4 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11223   sprintf (tmp, "wt.");
11224   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11225     sprintf (tmp + 3 + i * 2, "%02x", node->signature[i] & 0xff);
11226   comdat_key = get_identifier (tmp);
11227   targetm.asm_out.named_section (secname,
11228                                  SECTION_DEBUG | SECTION_LINKONCE,
11229                                  comdat_key);
11230 #else
11231   tmp = XALLOCAVEC (char, 18 + DWARF_TYPE_SIGNATURE_SIZE * 2);
11232   sprintf (tmp, ".gnu.linkonce.wt.");
11233   for (i = 0; i < DWARF_TYPE_SIGNATURE_SIZE; i++)
11234     sprintf (tmp + 17 + i * 2, "%02x", node->signature[i] & 0xff);
11235   secname = tmp;
11236   switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
11237 #endif
11238
11239   /* Output debugging information.  */
11240   output_compilation_unit_header ();
11241   output_signature (node->signature, "Type Signature");
11242   dw2_asm_output_data (DWARF_OFFSET_SIZE, node->type_die->die_offset,
11243                        "Offset to Type DIE");
11244   output_die (node->root_die);
11245
11246   unmark_dies (node->root_die);
11247 }
11248
11249 /* Return the DWARF2/3 pubname associated with a decl.  */
11250
11251 static const char *
11252 dwarf2_name (tree decl, int scope)
11253 {
11254   if (DECL_NAMELESS (decl))
11255     return NULL;
11256   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
11257 }
11258
11259 /* Add a new entry to .debug_pubnames if appropriate.  */
11260
11261 static void
11262 add_pubname_string (const char *str, dw_die_ref die)
11263 {
11264   if (targetm.want_debug_pub_sections)
11265     {
11266       pubname_entry e;
11267
11268       e.die = die;
11269       e.name = xstrdup (str);
11270       VEC_safe_push (pubname_entry, gc, pubname_table, &e);
11271     }
11272 }
11273
11274 static void
11275 add_pubname (tree decl, dw_die_ref die)
11276 {
11277   if (targetm.want_debug_pub_sections && TREE_PUBLIC (decl))
11278     {
11279       const char *name = dwarf2_name (decl, 1);
11280       if (name)
11281         add_pubname_string (name, die);
11282     }
11283 }
11284
11285 /* Add a new entry to .debug_pubtypes if appropriate.  */
11286
11287 static void
11288 add_pubtype (tree decl, dw_die_ref die)
11289 {
11290   pubname_entry e;
11291
11292   if (!targetm.want_debug_pub_sections)
11293     return;
11294
11295   e.name = NULL;
11296   if ((TREE_PUBLIC (decl)
11297        || die->die_parent == comp_unit_die)
11298       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
11299     {
11300       e.die = die;
11301       if (TYPE_P (decl))
11302         {
11303           if (TYPE_NAME (decl))
11304             {
11305               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
11306                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
11307               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
11308                        && DECL_NAME (TYPE_NAME (decl)))
11309                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
11310               else
11311                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
11312             }
11313         }
11314       else
11315         {
11316           e.name = dwarf2_name (decl, 1);
11317           if (e.name)
11318             e.name = xstrdup (e.name);
11319         }
11320
11321       /* If we don't have a name for the type, there's no point in adding
11322          it to the table.  */
11323       if (e.name && e.name[0] != '\0')
11324         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
11325     }
11326 }
11327
11328 /* Output the public names table used to speed up access to externally
11329    visible names; or the public types table used to find type definitions.  */
11330
11331 static void
11332 output_pubnames (VEC (pubname_entry, gc) * names)
11333 {
11334   unsigned i;
11335   unsigned long pubnames_length = size_of_pubnames (names);
11336   pubname_ref pub;
11337
11338   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11339     dw2_asm_output_data (4, 0xffffffff,
11340       "Initial length escape value indicating 64-bit DWARF extension");
11341   if (names == pubname_table)
11342     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11343                          "Length of Public Names Info");
11344   else
11345     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
11346                          "Length of Public Type Names Info");
11347   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
11348   dw2_asm_output_data (2, 2, "DWARF Version");
11349   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11350                          debug_info_section,
11351                          "Offset of Compilation Unit Info");
11352   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
11353                        "Compilation Unit Length");
11354
11355   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
11356     {
11357       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
11358       if (names == pubname_table)
11359         gcc_assert (pub->die->die_mark);
11360
11361       if (names != pubtype_table
11362           || pub->die->die_offset != 0
11363           || !flag_eliminate_unused_debug_types)
11364         {
11365           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
11366                                "DIE offset");
11367
11368           dw2_asm_output_nstring (pub->name, -1, "external name");
11369         }
11370     }
11371
11372   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
11373 }
11374
11375 /* Add a new entry to .debug_aranges if appropriate.  */
11376
11377 static void
11378 add_arange (tree decl, dw_die_ref die)
11379 {
11380   if (! DECL_SECTION_NAME (decl))
11381     return;
11382
11383   if (arange_table_in_use == arange_table_allocated)
11384     {
11385       arange_table_allocated += ARANGE_TABLE_INCREMENT;
11386       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
11387                                     arange_table_allocated);
11388       memset (arange_table + arange_table_in_use, 0,
11389               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
11390     }
11391
11392   arange_table[arange_table_in_use++] = die;
11393 }
11394
11395 /* Output the information that goes into the .debug_aranges table.
11396    Namely, define the beginning and ending address range of the
11397    text section generated for this compilation unit.  */
11398
11399 static void
11400 output_aranges (void)
11401 {
11402   unsigned i;
11403   unsigned long aranges_length = size_of_aranges ();
11404
11405   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11406     dw2_asm_output_data (4, 0xffffffff,
11407       "Initial length escape value indicating 64-bit DWARF extension");
11408   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
11409                        "Length of Address Ranges Info");
11410   /* Version number for aranges is still 2, even in DWARF3.  */
11411   dw2_asm_output_data (2, 2, "DWARF Version");
11412   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
11413                          debug_info_section,
11414                          "Offset of Compilation Unit Info");
11415   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
11416   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
11417
11418   /* We need to align to twice the pointer size here.  */
11419   if (DWARF_ARANGES_PAD_SIZE)
11420     {
11421       /* Pad using a 2 byte words so that padding is correct for any
11422          pointer size.  */
11423       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
11424                            2 * DWARF2_ADDR_SIZE);
11425       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
11426         dw2_asm_output_data (2, 0, NULL);
11427     }
11428
11429   /* It is necessary not to output these entries if the sections were
11430      not used; if the sections were not used, the length will be 0 and
11431      the address may end up as 0 if the section is discarded by ld
11432      --gc-sections, leaving an invalid (0, 0) entry that can be
11433      confused with the terminator.  */
11434   if (text_section_used)
11435     {
11436       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
11437       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
11438                             text_section_label, "Length");
11439     }
11440   if (cold_text_section_used)
11441     {
11442       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
11443                            "Address");
11444       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
11445                             cold_text_section_label, "Length");
11446     }
11447
11448   for (i = 0; i < arange_table_in_use; i++)
11449     {
11450       dw_die_ref die = arange_table[i];
11451
11452       /* We shouldn't see aranges for DIEs outside of the main CU.  */
11453       gcc_assert (die->die_mark);
11454
11455       if (die->die_tag == DW_TAG_subprogram)
11456         {
11457           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
11458                                "Address");
11459           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
11460                                 get_AT_low_pc (die), "Length");
11461         }
11462       else
11463         {
11464           /* A static variable; extract the symbol from DW_AT_location.
11465              Note that this code isn't currently hit, as we only emit
11466              aranges for functions (jason 9/23/99).  */
11467           dw_attr_ref a = get_AT (die, DW_AT_location);
11468           dw_loc_descr_ref loc;
11469
11470           gcc_assert (a && AT_class (a) == dw_val_class_loc);
11471
11472           loc = AT_loc (a);
11473           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
11474
11475           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
11476                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
11477           dw2_asm_output_data (DWARF2_ADDR_SIZE,
11478                                get_AT_unsigned (die, DW_AT_byte_size),
11479                                "Length");
11480         }
11481     }
11482
11483   /* Output the terminator words.  */
11484   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11485   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11486 }
11487
11488 /* Add a new entry to .debug_ranges.  Return the offset at which it
11489    was placed.  */
11490
11491 static unsigned int
11492 add_ranges_num (int num)
11493 {
11494   unsigned int in_use = ranges_table_in_use;
11495
11496   if (in_use == ranges_table_allocated)
11497     {
11498       ranges_table_allocated += RANGES_TABLE_INCREMENT;
11499       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
11500                                     ranges_table_allocated);
11501       memset (ranges_table + ranges_table_in_use, 0,
11502               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
11503     }
11504
11505   ranges_table[in_use].num = num;
11506   ranges_table_in_use = in_use + 1;
11507
11508   return in_use * 2 * DWARF2_ADDR_SIZE;
11509 }
11510
11511 /* Add a new entry to .debug_ranges corresponding to a block, or a
11512    range terminator if BLOCK is NULL.  */
11513
11514 static unsigned int
11515 add_ranges (const_tree block)
11516 {
11517   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
11518 }
11519
11520 /* Add a new entry to .debug_ranges corresponding to a pair of
11521    labels.  */
11522
11523 static void
11524 add_ranges_by_labels (dw_die_ref die, const char *begin, const char *end,
11525                       bool *added)
11526 {
11527   unsigned int in_use = ranges_by_label_in_use;
11528   unsigned int offset;
11529
11530   if (in_use == ranges_by_label_allocated)
11531     {
11532       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
11533       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
11534                                        ranges_by_label,
11535                                        ranges_by_label_allocated);
11536       memset (ranges_by_label + ranges_by_label_in_use, 0,
11537               RANGES_TABLE_INCREMENT
11538               * sizeof (struct dw_ranges_by_label_struct));
11539     }
11540
11541   ranges_by_label[in_use].begin = begin;
11542   ranges_by_label[in_use].end = end;
11543   ranges_by_label_in_use = in_use + 1;
11544
11545   offset = add_ranges_num (-(int)in_use - 1);
11546   if (!*added)
11547     {
11548       add_AT_range_list (die, DW_AT_ranges, offset);
11549       *added = true;
11550     }
11551 }
11552
11553 static void
11554 output_ranges (void)
11555 {
11556   unsigned i;
11557   static const char *const start_fmt = "Offset %#x";
11558   const char *fmt = start_fmt;
11559
11560   for (i = 0; i < ranges_table_in_use; i++)
11561     {
11562       int block_num = ranges_table[i].num;
11563
11564       if (block_num > 0)
11565         {
11566           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
11567           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
11568
11569           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
11570           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
11571
11572           /* If all code is in the text section, then the compilation
11573              unit base address defaults to DW_AT_low_pc, which is the
11574              base of the text section.  */
11575           if (!have_multiple_function_sections)
11576             {
11577               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
11578                                     text_section_label,
11579                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11580               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
11581                                     text_section_label, NULL);
11582             }
11583
11584           /* Otherwise, the compilation unit base address is zero,
11585              which allows us to use absolute addresses, and not worry
11586              about whether the target supports cross-section
11587              arithmetic.  */
11588           else
11589             {
11590               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
11591                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11592               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
11593             }
11594
11595           fmt = NULL;
11596         }
11597
11598       /* Negative block_num stands for an index into ranges_by_label.  */
11599       else if (block_num < 0)
11600         {
11601           int lab_idx = - block_num - 1;
11602
11603           if (!have_multiple_function_sections)
11604             {
11605               gcc_unreachable ();
11606 #if 0
11607               /* If we ever use add_ranges_by_labels () for a single
11608                  function section, all we have to do is to take out
11609                  the #if 0 above.  */
11610               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11611                                     ranges_by_label[lab_idx].begin,
11612                                     text_section_label,
11613                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
11614               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
11615                                     ranges_by_label[lab_idx].end,
11616                                     text_section_label, NULL);
11617 #endif
11618             }
11619           else
11620             {
11621               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11622                                    ranges_by_label[lab_idx].begin,
11623                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
11624               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
11625                                    ranges_by_label[lab_idx].end,
11626                                    NULL);
11627             }
11628         }
11629       else
11630         {
11631           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11632           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
11633           fmt = start_fmt;
11634         }
11635     }
11636 }
11637
11638 /* Data structure containing information about input files.  */
11639 struct file_info
11640 {
11641   const char *path;     /* Complete file name.  */
11642   const char *fname;    /* File name part.  */
11643   int length;           /* Length of entire string.  */
11644   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
11645   int dir_idx;          /* Index in directory table.  */
11646 };
11647
11648 /* Data structure containing information about directories with source
11649    files.  */
11650 struct dir_info
11651 {
11652   const char *path;     /* Path including directory name.  */
11653   int length;           /* Path length.  */
11654   int prefix;           /* Index of directory entry which is a prefix.  */
11655   int count;            /* Number of files in this directory.  */
11656   int dir_idx;          /* Index of directory used as base.  */
11657 };
11658
11659 /* Callback function for file_info comparison.  We sort by looking at
11660    the directories in the path.  */
11661
11662 static int
11663 file_info_cmp (const void *p1, const void *p2)
11664 {
11665   const struct file_info *const s1 = (const struct file_info *) p1;
11666   const struct file_info *const s2 = (const struct file_info *) p2;
11667   const unsigned char *cp1;
11668   const unsigned char *cp2;
11669
11670   /* Take care of file names without directories.  We need to make sure that
11671      we return consistent values to qsort since some will get confused if
11672      we return the same value when identical operands are passed in opposite
11673      orders.  So if neither has a directory, return 0 and otherwise return
11674      1 or -1 depending on which one has the directory.  */
11675   if ((s1->path == s1->fname || s2->path == s2->fname))
11676     return (s2->path == s2->fname) - (s1->path == s1->fname);
11677
11678   cp1 = (const unsigned char *) s1->path;
11679   cp2 = (const unsigned char *) s2->path;
11680
11681   while (1)
11682     {
11683       ++cp1;
11684       ++cp2;
11685       /* Reached the end of the first path?  If so, handle like above.  */
11686       if ((cp1 == (const unsigned char *) s1->fname)
11687           || (cp2 == (const unsigned char *) s2->fname))
11688         return ((cp2 == (const unsigned char *) s2->fname)
11689                 - (cp1 == (const unsigned char *) s1->fname));
11690
11691       /* Character of current path component the same?  */
11692       else if (*cp1 != *cp2)
11693         return *cp1 - *cp2;
11694     }
11695 }
11696
11697 struct file_name_acquire_data
11698 {
11699   struct file_info *files;
11700   int used_files;
11701   int max_files;
11702 };
11703
11704 /* Traversal function for the hash table.  */
11705
11706 static int
11707 file_name_acquire (void ** slot, void *data)
11708 {
11709   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
11710   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
11711   struct file_info *fi;
11712   const char *f;
11713
11714   gcc_assert (fnad->max_files >= d->emitted_number);
11715
11716   if (! d->emitted_number)
11717     return 1;
11718
11719   gcc_assert (fnad->max_files != fnad->used_files);
11720
11721   fi = fnad->files + fnad->used_files++;
11722
11723   /* Skip all leading "./".  */
11724   f = d->filename;
11725   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
11726     f += 2;
11727
11728   /* Create a new array entry.  */
11729   fi->path = f;
11730   fi->length = strlen (f);
11731   fi->file_idx = d;
11732
11733   /* Search for the file name part.  */
11734   f = strrchr (f, DIR_SEPARATOR);
11735 #if defined (DIR_SEPARATOR_2)
11736   {
11737     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
11738
11739     if (g != NULL)
11740       {
11741         if (f == NULL || f < g)
11742           f = g;
11743       }
11744   }
11745 #endif
11746
11747   fi->fname = f == NULL ? fi->path : f + 1;
11748   return 1;
11749 }
11750
11751 /* Output the directory table and the file name table.  We try to minimize
11752    the total amount of memory needed.  A heuristic is used to avoid large
11753    slowdowns with many input files.  */
11754
11755 static void
11756 output_file_names (void)
11757 {
11758   struct file_name_acquire_data fnad;
11759   int numfiles;
11760   struct file_info *files;
11761   struct dir_info *dirs;
11762   int *saved;
11763   int *savehere;
11764   int *backmap;
11765   int ndirs;
11766   int idx_offset;
11767   int i;
11768
11769   if (!last_emitted_file)
11770     {
11771       dw2_asm_output_data (1, 0, "End directory table");
11772       dw2_asm_output_data (1, 0, "End file name table");
11773       return;
11774     }
11775
11776   numfiles = last_emitted_file->emitted_number;
11777
11778   /* Allocate the various arrays we need.  */
11779   files = XALLOCAVEC (struct file_info, numfiles);
11780   dirs = XALLOCAVEC (struct dir_info, numfiles);
11781
11782   fnad.files = files;
11783   fnad.used_files = 0;
11784   fnad.max_files = numfiles;
11785   htab_traverse (file_table, file_name_acquire, &fnad);
11786   gcc_assert (fnad.used_files == fnad.max_files);
11787
11788   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
11789
11790   /* Find all the different directories used.  */
11791   dirs[0].path = files[0].path;
11792   dirs[0].length = files[0].fname - files[0].path;
11793   dirs[0].prefix = -1;
11794   dirs[0].count = 1;
11795   dirs[0].dir_idx = 0;
11796   files[0].dir_idx = 0;
11797   ndirs = 1;
11798
11799   for (i = 1; i < numfiles; i++)
11800     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
11801         && memcmp (dirs[ndirs - 1].path, files[i].path,
11802                    dirs[ndirs - 1].length) == 0)
11803       {
11804         /* Same directory as last entry.  */
11805         files[i].dir_idx = ndirs - 1;
11806         ++dirs[ndirs - 1].count;
11807       }
11808     else
11809       {
11810         int j;
11811
11812         /* This is a new directory.  */
11813         dirs[ndirs].path = files[i].path;
11814         dirs[ndirs].length = files[i].fname - files[i].path;
11815         dirs[ndirs].count = 1;
11816         dirs[ndirs].dir_idx = ndirs;
11817         files[i].dir_idx = ndirs;
11818
11819         /* Search for a prefix.  */
11820         dirs[ndirs].prefix = -1;
11821         for (j = 0; j < ndirs; j++)
11822           if (dirs[j].length < dirs[ndirs].length
11823               && dirs[j].length > 1
11824               && (dirs[ndirs].prefix == -1
11825                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
11826               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
11827             dirs[ndirs].prefix = j;
11828
11829         ++ndirs;
11830       }
11831
11832   /* Now to the actual work.  We have to find a subset of the directories which
11833      allow expressing the file name using references to the directory table
11834      with the least amount of characters.  We do not do an exhaustive search
11835      where we would have to check out every combination of every single
11836      possible prefix.  Instead we use a heuristic which provides nearly optimal
11837      results in most cases and never is much off.  */
11838   saved = XALLOCAVEC (int, ndirs);
11839   savehere = XALLOCAVEC (int, ndirs);
11840
11841   memset (saved, '\0', ndirs * sizeof (saved[0]));
11842   for (i = 0; i < ndirs; i++)
11843     {
11844       int j;
11845       int total;
11846
11847       /* We can always save some space for the current directory.  But this
11848          does not mean it will be enough to justify adding the directory.  */
11849       savehere[i] = dirs[i].length;
11850       total = (savehere[i] - saved[i]) * dirs[i].count;
11851
11852       for (j = i + 1; j < ndirs; j++)
11853         {
11854           savehere[j] = 0;
11855           if (saved[j] < dirs[i].length)
11856             {
11857               /* Determine whether the dirs[i] path is a prefix of the
11858                  dirs[j] path.  */
11859               int k;
11860
11861               k = dirs[j].prefix;
11862               while (k != -1 && k != (int) i)
11863                 k = dirs[k].prefix;
11864
11865               if (k == (int) i)
11866                 {
11867                   /* Yes it is.  We can possibly save some memory by
11868                      writing the filenames in dirs[j] relative to
11869                      dirs[i].  */
11870                   savehere[j] = dirs[i].length;
11871                   total += (savehere[j] - saved[j]) * dirs[j].count;
11872                 }
11873             }
11874         }
11875
11876       /* Check whether we can save enough to justify adding the dirs[i]
11877          directory.  */
11878       if (total > dirs[i].length + 1)
11879         {
11880           /* It's worthwhile adding.  */
11881           for (j = i; j < ndirs; j++)
11882             if (savehere[j] > 0)
11883               {
11884                 /* Remember how much we saved for this directory so far.  */
11885                 saved[j] = savehere[j];
11886
11887                 /* Remember the prefix directory.  */
11888                 dirs[j].dir_idx = i;
11889               }
11890         }
11891     }
11892
11893   /* Emit the directory name table.  */
11894   idx_offset = dirs[0].length > 0 ? 1 : 0;
11895   for (i = 1 - idx_offset; i < ndirs; i++)
11896     dw2_asm_output_nstring (dirs[i].path,
11897                             dirs[i].length
11898                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
11899                             "Directory Entry: %#x", i + idx_offset);
11900
11901   dw2_asm_output_data (1, 0, "End directory table");
11902
11903   /* We have to emit them in the order of emitted_number since that's
11904      used in the debug info generation.  To do this efficiently we
11905      generate a back-mapping of the indices first.  */
11906   backmap = XALLOCAVEC (int, numfiles);
11907   for (i = 0; i < numfiles; i++)
11908     backmap[files[i].file_idx->emitted_number - 1] = i;
11909
11910   /* Now write all the file names.  */
11911   for (i = 0; i < numfiles; i++)
11912     {
11913       int file_idx = backmap[i];
11914       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
11915
11916 #ifdef VMS_DEBUGGING_INFO
11917 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
11918
11919       /* Setting these fields can lead to debugger miscomparisons,
11920          but VMS Debug requires them to be set correctly.  */
11921
11922       int ver;
11923       long long cdt;
11924       long siz;
11925       int maxfilelen = strlen (files[file_idx].path)
11926                                + dirs[dir_idx].length
11927                                + MAX_VMS_VERSION_LEN + 1;
11928       char *filebuf = XALLOCAVEC (char, maxfilelen);
11929
11930       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
11931       snprintf (filebuf, maxfilelen, "%s;%d",
11932                 files[file_idx].path + dirs[dir_idx].length, ver);
11933
11934       dw2_asm_output_nstring
11935         (filebuf, -1, "File Entry: %#x", (unsigned) i + 1);
11936
11937       /* Include directory index.  */
11938       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11939
11940       /* Modification time.  */
11941       dw2_asm_output_data_uleb128
11942         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
11943           ? cdt : 0,
11944          NULL);
11945
11946       /* File length in bytes.  */
11947       dw2_asm_output_data_uleb128
11948         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
11949           ? siz : 0,
11950          NULL);
11951 #else
11952       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
11953                               "File Entry: %#x", (unsigned) i + 1);
11954
11955       /* Include directory index.  */
11956       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
11957
11958       /* Modification time.  */
11959       dw2_asm_output_data_uleb128 (0, NULL);
11960
11961       /* File length in bytes.  */
11962       dw2_asm_output_data_uleb128 (0, NULL);
11963 #endif
11964     }
11965
11966   dw2_asm_output_data (1, 0, "End file name table");
11967 }
11968
11969
11970 /* Output the source line number correspondence information.  This
11971    information goes into the .debug_line section.  */
11972
11973 static void
11974 output_line_info (void)
11975 {
11976   char l1[20], l2[20], p1[20], p2[20];
11977   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11978   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
11979   unsigned opc;
11980   unsigned n_op_args;
11981   unsigned long lt_index;
11982   unsigned long current_line;
11983   long line_offset;
11984   long line_delta;
11985   unsigned long current_file;
11986   unsigned long function;
11987   int ver = dwarf_version;
11988
11989   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
11990   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
11991   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
11992   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
11993
11994   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
11995     dw2_asm_output_data (4, 0xffffffff,
11996       "Initial length escape value indicating 64-bit DWARF extension");
11997   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
11998                         "Length of Source Line Info");
11999   ASM_OUTPUT_LABEL (asm_out_file, l1);
12000
12001   dw2_asm_output_data (2, ver, "DWARF Version");
12002   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
12003   ASM_OUTPUT_LABEL (asm_out_file, p1);
12004
12005   /* Define the architecture-dependent minimum instruction length (in
12006    bytes).  In this implementation of DWARF, this field is used for
12007    information purposes only.  Since GCC generates assembly language,
12008    we have no a priori knowledge of how many instruction bytes are
12009    generated for each source line, and therefore can use only the
12010    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
12011    commands.  Accordingly, we fix this as `1', which is "correct
12012    enough" for all architectures, and don't let the target override.  */
12013   dw2_asm_output_data (1, 1,
12014                        "Minimum Instruction Length");
12015
12016   if (ver >= 4)
12017     dw2_asm_output_data (1, DWARF_LINE_DEFAULT_MAX_OPS_PER_INSN,
12018                          "Maximum Operations Per Instruction");
12019   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
12020                        "Default is_stmt_start flag");
12021   dw2_asm_output_data (1, DWARF_LINE_BASE,
12022                        "Line Base Value (Special Opcodes)");
12023   dw2_asm_output_data (1, DWARF_LINE_RANGE,
12024                        "Line Range Value (Special Opcodes)");
12025   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
12026                        "Special Opcode Base");
12027
12028   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
12029     {
12030       switch (opc)
12031         {
12032         case DW_LNS_advance_pc:
12033         case DW_LNS_advance_line:
12034         case DW_LNS_set_file:
12035         case DW_LNS_set_column:
12036         case DW_LNS_fixed_advance_pc:
12037           n_op_args = 1;
12038           break;
12039         default:
12040           n_op_args = 0;
12041           break;
12042         }
12043
12044       dw2_asm_output_data (1, n_op_args, "opcode: %#x has %d args",
12045                            opc, n_op_args);
12046     }
12047
12048   /* Write out the information about the files we use.  */
12049   output_file_names ();
12050   ASM_OUTPUT_LABEL (asm_out_file, p2);
12051
12052   /* We used to set the address register to the first location in the text
12053      section here, but that didn't accomplish anything since we already
12054      have a line note for the opening brace of the first function.  */
12055
12056   /* Generate the line number to PC correspondence table, encoded as
12057      a series of state machine operations.  */
12058   current_file = 1;
12059   current_line = 1;
12060
12061   if (cfun && in_cold_section_p)
12062     strcpy (prev_line_label, crtl->subsections.cold_section_label);
12063   else
12064     strcpy (prev_line_label, text_section_label);
12065   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
12066     {
12067       dw_line_info_ref line_info = &line_info_table[lt_index];
12068
12069 #if 0
12070       /* Disable this optimization for now; GDB wants to see two line notes
12071          at the beginning of a function so it can find the end of the
12072          prologue.  */
12073
12074       /* Don't emit anything for redundant notes.  Just updating the
12075          address doesn't accomplish anything, because we already assume
12076          that anything after the last address is this line.  */
12077       if (line_info->dw_line_num == current_line
12078           && line_info->dw_file_num == current_file)
12079         continue;
12080 #endif
12081
12082       /* Emit debug info for the address of the current line.
12083
12084          Unfortunately, we have little choice here currently, and must always
12085          use the most general form.  GCC does not know the address delta
12086          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
12087          attributes which will give an upper bound on the address range.  We
12088          could perhaps use length attributes to determine when it is safe to
12089          use DW_LNS_fixed_advance_pc.  */
12090
12091       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
12092       if (0)
12093         {
12094           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
12095           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12096                                "DW_LNS_fixed_advance_pc");
12097           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12098         }
12099       else
12100         {
12101           /* This can handle any delta.  This takes
12102              4+DWARF2_ADDR_SIZE bytes.  */
12103           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12104           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12105           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12106           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12107         }
12108
12109       strcpy (prev_line_label, line_label);
12110
12111       /* Emit debug info for the source file of the current line, if
12112          different from the previous line.  */
12113       if (line_info->dw_file_num != current_file)
12114         {
12115           current_file = line_info->dw_file_num;
12116           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12117           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12118         }
12119
12120       /* Emit debug info for the current line number, choosing the encoding
12121          that uses the least amount of space.  */
12122       if (line_info->dw_line_num != current_line)
12123         {
12124           line_offset = line_info->dw_line_num - current_line;
12125           line_delta = line_offset - DWARF_LINE_BASE;
12126           current_line = line_info->dw_line_num;
12127           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12128             /* This can handle deltas from -10 to 234, using the current
12129                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
12130                takes 1 byte.  */
12131             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12132                                  "line %lu", current_line);
12133           else
12134             {
12135               /* This can handle any delta.  This takes at least 4 bytes,
12136                  depending on the value being encoded.  */
12137               dw2_asm_output_data (1, DW_LNS_advance_line,
12138                                    "advance to line %lu", current_line);
12139               dw2_asm_output_data_sleb128 (line_offset, NULL);
12140               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12141             }
12142         }
12143       else
12144         /* We still need to start a new row, so output a copy insn.  */
12145         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12146     }
12147
12148   /* Emit debug info for the address of the end of the function.  */
12149   if (0)
12150     {
12151       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12152                            "DW_LNS_fixed_advance_pc");
12153       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
12154     }
12155   else
12156     {
12157       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12158       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12159       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12160       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
12161     }
12162
12163   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12164   dw2_asm_output_data_uleb128 (1, NULL);
12165   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12166
12167   function = 0;
12168   current_file = 1;
12169   current_line = 1;
12170   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
12171     {
12172       dw_separate_line_info_ref line_info
12173         = &separate_line_info_table[lt_index];
12174
12175 #if 0
12176       /* Don't emit anything for redundant notes.  */
12177       if (line_info->dw_line_num == current_line
12178           && line_info->dw_file_num == current_file
12179           && line_info->function == function)
12180         goto cont;
12181 #endif
12182
12183       /* Emit debug info for the address of the current line.  If this is
12184          a new function, or the first line of a function, then we need
12185          to handle it differently.  */
12186       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
12187                                    lt_index);
12188       if (function != line_info->function)
12189         {
12190           function = line_info->function;
12191
12192           /* Set the address register to the first line in the function.  */
12193           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12194           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12195           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12196           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12197         }
12198       else
12199         {
12200           /* ??? See the DW_LNS_advance_pc comment above.  */
12201           if (0)
12202             {
12203               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12204                                    "DW_LNS_fixed_advance_pc");
12205               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12206             }
12207           else
12208             {
12209               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12210               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12211               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12212               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12213             }
12214         }
12215
12216       strcpy (prev_line_label, line_label);
12217
12218       /* Emit debug info for the source file of the current line, if
12219          different from the previous line.  */
12220       if (line_info->dw_file_num != current_file)
12221         {
12222           current_file = line_info->dw_file_num;
12223           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
12224           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
12225         }
12226
12227       /* Emit debug info for the current line number, choosing the encoding
12228          that uses the least amount of space.  */
12229       if (line_info->dw_line_num != current_line)
12230         {
12231           line_offset = line_info->dw_line_num - current_line;
12232           line_delta = line_offset - DWARF_LINE_BASE;
12233           current_line = line_info->dw_line_num;
12234           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
12235             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
12236                                  "line %lu", current_line);
12237           else
12238             {
12239               dw2_asm_output_data (1, DW_LNS_advance_line,
12240                                    "advance to line %lu", current_line);
12241               dw2_asm_output_data_sleb128 (line_offset, NULL);
12242               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12243             }
12244         }
12245       else
12246         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
12247
12248 #if 0
12249     cont:
12250 #endif
12251
12252       lt_index++;
12253
12254       /* If we're done with a function, end its sequence.  */
12255       if (lt_index == separate_line_info_table_in_use
12256           || separate_line_info_table[lt_index].function != function)
12257         {
12258           current_file = 1;
12259           current_line = 1;
12260
12261           /* Emit debug info for the address of the end of the function.  */
12262           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
12263           if (0)
12264             {
12265               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
12266                                    "DW_LNS_fixed_advance_pc");
12267               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
12268             }
12269           else
12270             {
12271               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
12272               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
12273               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
12274               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
12275             }
12276
12277           /* Output the marker for the end of this sequence.  */
12278           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
12279           dw2_asm_output_data_uleb128 (1, NULL);
12280           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
12281         }
12282     }
12283
12284   /* Output the marker for the end of the line number info.  */
12285   ASM_OUTPUT_LABEL (asm_out_file, l2);
12286 }
12287
12288 /* Return the size of the .debug_dcall table for the compilation unit.  */
12289
12290 static unsigned long
12291 size_of_dcall_table (void)
12292 {
12293   unsigned long size;
12294   unsigned int i;
12295   dcall_entry *p;
12296   tree last_poc_decl = NULL;
12297
12298   /* Header:  version + debug info section pointer + pointer size.  */
12299   size = 2 + DWARF_OFFSET_SIZE + 1;
12300
12301   /* Each entry:  code label + DIE offset.  */
12302   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12303     {
12304       gcc_assert (p->targ_die != NULL);
12305       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12306       if (p->poc_decl != last_poc_decl)
12307         {
12308           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12309           gcc_assert (poc_die);
12310           last_poc_decl = p->poc_decl;
12311           if (poc_die)
12312             size += (DWARF_OFFSET_SIZE
12313                      + size_of_uleb128 (poc_die->die_offset));
12314         }
12315       size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->targ_die->die_offset);
12316     }
12317
12318   return size;
12319 }
12320
12321 /* Output the direct call table used to disambiguate PC values when
12322    identical function have been merged.  */
12323
12324 static void
12325 output_dcall_table (void)
12326 {
12327   unsigned i;
12328   unsigned long dcall_length = size_of_dcall_table ();
12329   dcall_entry *p;
12330   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12331   tree last_poc_decl = NULL;
12332
12333   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12334     dw2_asm_output_data (4, 0xffffffff,
12335       "Initial length escape value indicating 64-bit DWARF extension");
12336   dw2_asm_output_data (DWARF_OFFSET_SIZE, dcall_length,
12337                        "Length of Direct Call Table");
12338   dw2_asm_output_data (2, 4, "Version number");
12339   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
12340                          debug_info_section,
12341                          "Offset of Compilation Unit Info");
12342   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12343
12344   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, p); i++)
12345     {
12346       /* Insert a "from" entry when the point-of-call DIE offset changes.  */
12347       if (p->poc_decl != last_poc_decl)
12348         {
12349           dw_die_ref poc_die = lookup_decl_die (p->poc_decl);
12350           last_poc_decl = p->poc_decl;
12351           if (poc_die)
12352             {
12353               dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, "New caller");
12354               dw2_asm_output_data_uleb128 (poc_die->die_offset,
12355                                            "Caller DIE offset");
12356             }
12357         }
12358       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12359       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12360       dw2_asm_output_data_uleb128 (p->targ_die->die_offset,
12361                                    "Callee DIE offset");
12362     }
12363 }
12364 \f
12365 /* Return the size of the .debug_vcall table for the compilation unit.  */
12366
12367 static unsigned long
12368 size_of_vcall_table (void)
12369 {
12370   unsigned long size;
12371   unsigned int i;
12372   vcall_entry *p;
12373
12374   /* Header:  version + pointer size.  */
12375   size = 2 + 1;
12376
12377   /* Each entry:  code label + vtable slot index.  */
12378   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12379     size += DWARF_OFFSET_SIZE + size_of_uleb128 (p->vtable_slot);
12380
12381   return size;
12382 }
12383
12384 /* Output the virtual call table used to disambiguate PC values when
12385    identical function have been merged.  */
12386
12387 static void
12388 output_vcall_table (void)
12389 {
12390   unsigned i;
12391   unsigned long vcall_length = size_of_vcall_table ();
12392   vcall_entry *p;
12393   char poc_label[MAX_ARTIFICIAL_LABEL_BYTES];
12394
12395   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
12396     dw2_asm_output_data (4, 0xffffffff,
12397       "Initial length escape value indicating 64-bit DWARF extension");
12398   dw2_asm_output_data (DWARF_OFFSET_SIZE, vcall_length,
12399                        "Length of Virtual Call Table");
12400   dw2_asm_output_data (2, 4, "Version number");
12401   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
12402
12403   for (i = 0; VEC_iterate (vcall_entry, vcall_table, i, p); i++)
12404     {
12405       ASM_GENERATE_INTERNAL_LABEL (poc_label, "LPOC", p->poc_label_num);
12406       dw2_asm_output_addr (DWARF_OFFSET_SIZE, poc_label, "Point of call");
12407       dw2_asm_output_data_uleb128 (p->vtable_slot, "Vtable slot");
12408     }
12409 }
12410 \f
12411 /* Given a pointer to a tree node for some base type, return a pointer to
12412    a DIE that describes the given type.
12413
12414    This routine must only be called for GCC type nodes that correspond to
12415    Dwarf base (fundamental) types.  */
12416
12417 static dw_die_ref
12418 base_type_die (tree type)
12419 {
12420   dw_die_ref base_type_result;
12421   enum dwarf_type encoding;
12422
12423   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
12424     return 0;
12425
12426   /* If this is a subtype that should not be emitted as a subrange type,
12427      use the base type.  See subrange_type_for_debug_p.  */
12428   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
12429     type = TREE_TYPE (type);
12430
12431   switch (TREE_CODE (type))
12432     {
12433     case INTEGER_TYPE:
12434       if ((dwarf_version >= 4 || !dwarf_strict)
12435           && TYPE_NAME (type)
12436           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12437           && DECL_IS_BUILTIN (TYPE_NAME (type))
12438           && DECL_NAME (TYPE_NAME (type)))
12439         {
12440           const char *name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type)));
12441           if (strcmp (name, "char16_t") == 0
12442               || strcmp (name, "char32_t") == 0)
12443             {
12444               encoding = DW_ATE_UTF;
12445               break;
12446             }
12447         }
12448       if (TYPE_STRING_FLAG (type))
12449         {
12450           if (TYPE_UNSIGNED (type))
12451             encoding = DW_ATE_unsigned_char;
12452           else
12453             encoding = DW_ATE_signed_char;
12454         }
12455       else if (TYPE_UNSIGNED (type))
12456         encoding = DW_ATE_unsigned;
12457       else
12458         encoding = DW_ATE_signed;
12459       break;
12460
12461     case REAL_TYPE:
12462       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
12463         {
12464           if (dwarf_version >= 3 || !dwarf_strict)
12465             encoding = DW_ATE_decimal_float;
12466           else
12467             encoding = DW_ATE_lo_user;
12468         }
12469       else
12470         encoding = DW_ATE_float;
12471       break;
12472
12473     case FIXED_POINT_TYPE:
12474       if (!(dwarf_version >= 3 || !dwarf_strict))
12475         encoding = DW_ATE_lo_user;
12476       else if (TYPE_UNSIGNED (type))
12477         encoding = DW_ATE_unsigned_fixed;
12478       else
12479         encoding = DW_ATE_signed_fixed;
12480       break;
12481
12482       /* Dwarf2 doesn't know anything about complex ints, so use
12483          a user defined type for it.  */
12484     case COMPLEX_TYPE:
12485       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
12486         encoding = DW_ATE_complex_float;
12487       else
12488         encoding = DW_ATE_lo_user;
12489       break;
12490
12491     case BOOLEAN_TYPE:
12492       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
12493       encoding = DW_ATE_boolean;
12494       break;
12495
12496     default:
12497       /* No other TREE_CODEs are Dwarf fundamental types.  */
12498       gcc_unreachable ();
12499     }
12500
12501   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
12502
12503   add_AT_unsigned (base_type_result, DW_AT_byte_size,
12504                    int_size_in_bytes (type));
12505   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
12506
12507   return base_type_result;
12508 }
12509
12510 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
12511    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
12512
12513 static inline int
12514 is_base_type (tree type)
12515 {
12516   switch (TREE_CODE (type))
12517     {
12518     case ERROR_MARK:
12519     case VOID_TYPE:
12520     case INTEGER_TYPE:
12521     case REAL_TYPE:
12522     case FIXED_POINT_TYPE:
12523     case COMPLEX_TYPE:
12524     case BOOLEAN_TYPE:
12525       return 1;
12526
12527     case ARRAY_TYPE:
12528     case RECORD_TYPE:
12529     case UNION_TYPE:
12530     case QUAL_UNION_TYPE:
12531     case ENUMERAL_TYPE:
12532     case FUNCTION_TYPE:
12533     case METHOD_TYPE:
12534     case POINTER_TYPE:
12535     case REFERENCE_TYPE:
12536     case OFFSET_TYPE:
12537     case LANG_TYPE:
12538     case VECTOR_TYPE:
12539       return 0;
12540
12541     default:
12542       gcc_unreachable ();
12543     }
12544
12545   return 0;
12546 }
12547
12548 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
12549    node, return the size in bits for the type if it is a constant, or else
12550    return the alignment for the type if the type's size is not constant, or
12551    else return BITS_PER_WORD if the type actually turns out to be an
12552    ERROR_MARK node.  */
12553
12554 static inline unsigned HOST_WIDE_INT
12555 simple_type_size_in_bits (const_tree type)
12556 {
12557   if (TREE_CODE (type) == ERROR_MARK)
12558     return BITS_PER_WORD;
12559   else if (TYPE_SIZE (type) == NULL_TREE)
12560     return 0;
12561   else if (host_integerp (TYPE_SIZE (type), 1))
12562     return tree_low_cst (TYPE_SIZE (type), 1);
12563   else
12564     return TYPE_ALIGN (type);
12565 }
12566
12567 /* Similarly, but return a double_int instead of UHWI.  */
12568
12569 static inline double_int
12570 double_int_type_size_in_bits (const_tree type)
12571 {
12572   if (TREE_CODE (type) == ERROR_MARK)
12573     return uhwi_to_double_int (BITS_PER_WORD);
12574   else if (TYPE_SIZE (type) == NULL_TREE)
12575     return double_int_zero;
12576   else if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
12577     return tree_to_double_int (TYPE_SIZE (type));
12578   else
12579     return uhwi_to_double_int (TYPE_ALIGN (type));
12580 }
12581
12582 /*  Given a pointer to a tree node for a subrange type, return a pointer
12583     to a DIE that describes the given type.  */
12584
12585 static dw_die_ref
12586 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
12587 {
12588   dw_die_ref subrange_die;
12589   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
12590
12591   if (context_die == NULL)
12592     context_die = comp_unit_die;
12593
12594   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
12595
12596   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
12597     {
12598       /* The size of the subrange type and its base type do not match,
12599          so we need to generate a size attribute for the subrange type.  */
12600       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
12601     }
12602
12603   if (low)
12604     add_bound_info (subrange_die, DW_AT_lower_bound, low);
12605   if (high)
12606     add_bound_info (subrange_die, DW_AT_upper_bound, high);
12607
12608   return subrange_die;
12609 }
12610
12611 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
12612    entry that chains various modifiers in front of the given type.  */
12613
12614 static dw_die_ref
12615 modified_type_die (tree type, int is_const_type, int is_volatile_type,
12616                    dw_die_ref context_die)
12617 {
12618   enum tree_code code = TREE_CODE (type);
12619   dw_die_ref mod_type_die;
12620   dw_die_ref sub_die = NULL;
12621   tree item_type = NULL;
12622   tree qualified_type;
12623   tree name, low, high;
12624
12625   if (code == ERROR_MARK)
12626     return NULL;
12627
12628   /* See if we already have the appropriately qualified variant of
12629      this type.  */
12630   qualified_type
12631     = get_qualified_type (type,
12632                           ((is_const_type ? TYPE_QUAL_CONST : 0)
12633                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
12634
12635   if (qualified_type == sizetype
12636       && TYPE_NAME (qualified_type)
12637       && TREE_CODE (TYPE_NAME (qualified_type)) == TYPE_DECL)
12638     {
12639 #ifdef ENABLE_CHECKING
12640       gcc_assert (TREE_CODE (TREE_TYPE (TYPE_NAME (qualified_type)))
12641                   == INTEGER_TYPE
12642                   && TYPE_PRECISION (TREE_TYPE (TYPE_NAME (qualified_type)))
12643                      == TYPE_PRECISION (qualified_type)
12644                   && TYPE_UNSIGNED (TREE_TYPE (TYPE_NAME (qualified_type)))
12645                      == TYPE_UNSIGNED (qualified_type));
12646 #endif
12647       qualified_type = TREE_TYPE (TYPE_NAME (qualified_type));
12648     }
12649
12650   /* If we do, then we can just use its DIE, if it exists.  */
12651   if (qualified_type)
12652     {
12653       mod_type_die = lookup_type_die (qualified_type);
12654       if (mod_type_die)
12655         return mod_type_die;
12656     }
12657
12658   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
12659
12660   /* Handle C typedef types.  */
12661   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name)
12662       && !DECL_ARTIFICIAL (name))
12663     {
12664       tree dtype = TREE_TYPE (name);
12665
12666       if (qualified_type == dtype)
12667         {
12668           /* For a named type, use the typedef.  */
12669           gen_type_die (qualified_type, context_die);
12670           return lookup_type_die (qualified_type);
12671         }
12672       else if (is_const_type < TYPE_READONLY (dtype)
12673                || is_volatile_type < TYPE_VOLATILE (dtype)
12674                || (is_const_type <= TYPE_READONLY (dtype)
12675                    && is_volatile_type <= TYPE_VOLATILE (dtype)
12676                    && DECL_ORIGINAL_TYPE (name) != type))
12677         /* cv-unqualified version of named type.  Just use the unnamed
12678            type to which it refers.  */
12679         return modified_type_die (DECL_ORIGINAL_TYPE (name),
12680                                   is_const_type, is_volatile_type,
12681                                   context_die);
12682       /* Else cv-qualified version of named type; fall through.  */
12683     }
12684
12685   if (is_const_type)
12686     {
12687       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
12688       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
12689     }
12690   else if (is_volatile_type)
12691     {
12692       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
12693       sub_die = modified_type_die (type, 0, 0, context_die);
12694     }
12695   else if (code == POINTER_TYPE)
12696     {
12697       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
12698       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12699                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12700       item_type = TREE_TYPE (type);
12701       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12702         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12703                          TYPE_ADDR_SPACE (item_type));
12704     }
12705   else if (code == REFERENCE_TYPE)
12706     {
12707       if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
12708         mod_type_die = new_die (DW_TAG_rvalue_reference_type, comp_unit_die,
12709                                 type);
12710       else
12711         mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
12712       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
12713                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
12714       item_type = TREE_TYPE (type);
12715       if (!ADDR_SPACE_GENERIC_P (TYPE_ADDR_SPACE (item_type)))
12716         add_AT_unsigned (mod_type_die, DW_AT_address_class,
12717                          TYPE_ADDR_SPACE (item_type));
12718     }
12719   else if (code == INTEGER_TYPE
12720            && TREE_TYPE (type) != NULL_TREE
12721            && subrange_type_for_debug_p (type, &low, &high))
12722     {
12723       mod_type_die = subrange_type_die (type, low, high, context_die);
12724       item_type = TREE_TYPE (type);
12725     }
12726   else if (is_base_type (type))
12727     mod_type_die = base_type_die (type);
12728   else
12729     {
12730       gen_type_die (type, context_die);
12731
12732       /* We have to get the type_main_variant here (and pass that to the
12733          `lookup_type_die' routine) because the ..._TYPE node we have
12734          might simply be a *copy* of some original type node (where the
12735          copy was created to help us keep track of typedef names) and
12736          that copy might have a different TYPE_UID from the original
12737          ..._TYPE node.  */
12738       if (TREE_CODE (type) != VECTOR_TYPE)
12739         return lookup_type_die (type_main_variant (type));
12740       else
12741         /* Vectors have the debugging information in the type,
12742            not the main variant.  */
12743         return lookup_type_die (type);
12744     }
12745
12746   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
12747      don't output a DW_TAG_typedef, since there isn't one in the
12748      user's program; just attach a DW_AT_name to the type.
12749      Don't attach a DW_AT_name to DW_TAG_const_type or DW_TAG_volatile_type
12750      if the base type already has the same name.  */
12751   if (name
12752       && ((TREE_CODE (name) != TYPE_DECL
12753            && (qualified_type == TYPE_MAIN_VARIANT (type)
12754                || (!is_const_type && !is_volatile_type)))
12755           || (TREE_CODE (name) == TYPE_DECL
12756               && TREE_TYPE (name) == qualified_type
12757               && DECL_NAME (name))))
12758     {
12759       if (TREE_CODE (name) == TYPE_DECL)
12760         /* Could just call add_name_and_src_coords_attributes here,
12761            but since this is a builtin type it doesn't have any
12762            useful source coordinates anyway.  */
12763         name = DECL_NAME (name);
12764       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
12765     }
12766   /* This probably indicates a bug.  */
12767   else if (mod_type_die && mod_type_die->die_tag == DW_TAG_base_type)
12768     add_name_attribute (mod_type_die, "__unknown__");
12769
12770   if (qualified_type)
12771     equate_type_number_to_die (qualified_type, mod_type_die);
12772
12773   if (item_type)
12774     /* We must do this after the equate_type_number_to_die call, in case
12775        this is a recursive type.  This ensures that the modified_type_die
12776        recursion will terminate even if the type is recursive.  Recursive
12777        types are possible in Ada.  */
12778     sub_die = modified_type_die (item_type,
12779                                  TYPE_READONLY (item_type),
12780                                  TYPE_VOLATILE (item_type),
12781                                  context_die);
12782
12783   if (sub_die != NULL)
12784     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
12785
12786   return mod_type_die;
12787 }
12788
12789 /* Generate DIEs for the generic parameters of T.
12790    T must be either a generic type or a generic function.
12791    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
12792
12793 static void
12794 gen_generic_params_dies (tree t)
12795 {
12796   tree parms, args;
12797   int parms_num, i;
12798   dw_die_ref die = NULL;
12799
12800   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
12801     return;
12802
12803   if (TYPE_P (t))
12804     die = lookup_type_die (t);
12805   else if (DECL_P (t))
12806     die = lookup_decl_die (t);
12807
12808   gcc_assert (die);
12809
12810   parms = lang_hooks.get_innermost_generic_parms (t);
12811   if (!parms)
12812     /* T has no generic parameter. It means T is neither a generic type
12813        or function. End of story.  */
12814     return;
12815
12816   parms_num = TREE_VEC_LENGTH (parms);
12817   args = lang_hooks.get_innermost_generic_args (t);
12818   for (i = 0; i < parms_num; i++)
12819     {
12820       tree parm, arg, arg_pack_elems;
12821
12822       parm = TREE_VEC_ELT (parms, i);
12823       arg = TREE_VEC_ELT (args, i);
12824       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
12825       gcc_assert (parm && TREE_VALUE (parm) && arg);
12826
12827       if (parm && TREE_VALUE (parm) && arg)
12828         {
12829           /* If PARM represents a template parameter pack,
12830              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
12831              by DW_TAG_template_*_parameter DIEs for the argument
12832              pack elements of ARG. Note that ARG would then be
12833              an argument pack.  */
12834           if (arg_pack_elems)
12835             template_parameter_pack_die (TREE_VALUE (parm),
12836                                          arg_pack_elems,
12837                                          die);
12838           else
12839             generic_parameter_die (TREE_VALUE (parm), arg,
12840                                    true /* Emit DW_AT_name */, die);
12841         }
12842     }
12843 }
12844
12845 /* Create and return a DIE for PARM which should be
12846    the representation of a generic type parameter.
12847    For instance, in the C++ front end, PARM would be a template parameter.
12848    ARG is the argument to PARM.
12849    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
12850    name of the PARM.
12851    PARENT_DIE is the parent DIE which the new created DIE should be added to,
12852    as a child node.  */
12853
12854 static dw_die_ref
12855 generic_parameter_die (tree parm, tree arg,
12856                        bool emit_name_p,
12857                        dw_die_ref parent_die)
12858 {
12859   dw_die_ref tmpl_die = NULL;
12860   const char *name = NULL;
12861
12862   if (!parm || !DECL_NAME (parm) || !arg)
12863     return NULL;
12864
12865   /* We support non-type generic parameters and arguments,
12866      type generic parameters and arguments, as well as
12867      generic generic parameters (a.k.a. template template parameters in C++)
12868      and arguments.  */
12869   if (TREE_CODE (parm) == PARM_DECL)
12870     /* PARM is a nontype generic parameter  */
12871     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
12872   else if (TREE_CODE (parm) == TYPE_DECL)
12873     /* PARM is a type generic parameter.  */
12874     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
12875   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12876     /* PARM is a generic generic parameter.
12877        Its DIE is a GNU extension. It shall have a
12878        DW_AT_name attribute to represent the name of the template template
12879        parameter, and a DW_AT_GNU_template_name attribute to represent the
12880        name of the template template argument.  */
12881     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
12882                         parent_die, parm);
12883   else
12884     gcc_unreachable ();
12885
12886   if (tmpl_die)
12887     {
12888       tree tmpl_type;
12889
12890       /* If PARM is a generic parameter pack, it means we are
12891          emitting debug info for a template argument pack element.
12892          In other terms, ARG is a template argument pack element.
12893          In that case, we don't emit any DW_AT_name attribute for
12894          the die.  */
12895       if (emit_name_p)
12896         {
12897           name = IDENTIFIER_POINTER (DECL_NAME (parm));
12898           gcc_assert (name);
12899           add_AT_string (tmpl_die, DW_AT_name, name);
12900         }
12901
12902       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
12903         {
12904           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
12905              TMPL_DIE should have a child DW_AT_type attribute that is set
12906              to the type of the argument to PARM, which is ARG.
12907              If PARM is a type generic parameter, TMPL_DIE should have a
12908              child DW_AT_type that is set to ARG.  */
12909           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
12910           add_type_attribute (tmpl_die, tmpl_type, 0,
12911                               TREE_THIS_VOLATILE (tmpl_type),
12912                               parent_die);
12913         }
12914       else
12915         {
12916           /* So TMPL_DIE is a DIE representing a
12917              a generic generic template parameter, a.k.a template template
12918              parameter in C++ and arg is a template.  */
12919
12920           /* The DW_AT_GNU_template_name attribute of the DIE must be set
12921              to the name of the argument.  */
12922           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
12923           if (name)
12924             add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
12925         }
12926
12927       if (TREE_CODE (parm) == PARM_DECL)
12928         /* So PARM is a non-type generic parameter.
12929            DWARF3 5.6.8 says we must set a DW_AT_const_value child
12930            attribute of TMPL_DIE which value represents the value
12931            of ARG.
12932            We must be careful here:
12933            The value of ARG might reference some function decls.
12934            We might currently be emitting debug info for a generic
12935            type and types are emitted before function decls, we don't
12936            know if the function decls referenced by ARG will actually be
12937            emitted after cgraph computations.
12938            So must defer the generation of the DW_AT_const_value to
12939            after cgraph is ready.  */
12940         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
12941     }
12942
12943   return tmpl_die;
12944 }
12945
12946 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
12947    PARM_PACK must be a template parameter pack. The returned DIE
12948    will be child DIE of PARENT_DIE.  */
12949
12950 static dw_die_ref
12951 template_parameter_pack_die (tree parm_pack,
12952                              tree parm_pack_args,
12953                              dw_die_ref parent_die)
12954 {
12955   dw_die_ref die;
12956   int j;
12957
12958   gcc_assert (parent_die && parm_pack);
12959
12960   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
12961   add_name_and_src_coords_attributes (die, parm_pack);
12962   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
12963     generic_parameter_die (parm_pack,
12964                            TREE_VEC_ELT (parm_pack_args, j),
12965                            false /* Don't emit DW_AT_name */,
12966                            die);
12967   return die;
12968 }
12969
12970 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
12971    an enumerated type.  */
12972
12973 static inline int
12974 type_is_enum (const_tree type)
12975 {
12976   return TREE_CODE (type) == ENUMERAL_TYPE;
12977 }
12978
12979 /* Return the DBX register number described by a given RTL node.  */
12980
12981 static unsigned int
12982 dbx_reg_number (const_rtx rtl)
12983 {
12984   unsigned regno = REGNO (rtl);
12985
12986   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
12987
12988 #ifdef LEAF_REG_REMAP
12989   if (current_function_uses_only_leaf_regs)
12990     {
12991       int leaf_reg = LEAF_REG_REMAP (regno);
12992       if (leaf_reg != -1)
12993         regno = (unsigned) leaf_reg;
12994     }
12995 #endif
12996
12997   return DBX_REGISTER_NUMBER (regno);
12998 }
12999
13000 /* Optionally add a DW_OP_piece term to a location description expression.
13001    DW_OP_piece is only added if the location description expression already
13002    doesn't end with DW_OP_piece.  */
13003
13004 static void
13005 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
13006 {
13007   dw_loc_descr_ref loc;
13008
13009   if (*list_head != NULL)
13010     {
13011       /* Find the end of the chain.  */
13012       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
13013         ;
13014
13015       if (loc->dw_loc_opc != DW_OP_piece)
13016         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
13017     }
13018 }
13019
13020 /* Return a location descriptor that designates a machine register or
13021    zero if there is none.  */
13022
13023 static dw_loc_descr_ref
13024 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
13025 {
13026   rtx regs;
13027
13028   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
13029     return 0;
13030
13031   /* We only use "frame base" when we're sure we're talking about the
13032      post-prologue local stack frame.  We do this by *not* running
13033      register elimination until this point, and recognizing the special
13034      argument pointer and soft frame pointer rtx's.
13035      Use DW_OP_fbreg offset DW_OP_stack_value in this case.  */
13036   if ((rtl == arg_pointer_rtx || rtl == frame_pointer_rtx)
13037       && eliminate_regs (rtl, VOIDmode, NULL_RTX) != rtl)
13038     {
13039       dw_loc_descr_ref result = NULL;
13040
13041       if (dwarf_version >= 4 || !dwarf_strict)
13042         {
13043           result = mem_loc_descriptor (rtl, VOIDmode, initialized);
13044           if (result)
13045             add_loc_descr (&result,
13046                            new_loc_descr (DW_OP_stack_value, 0, 0));
13047         }
13048       return result;
13049     }
13050
13051   regs = targetm.dwarf_register_span (rtl);
13052
13053   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
13054     return multiple_reg_loc_descriptor (rtl, regs, initialized);
13055   else
13056     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
13057 }
13058
13059 /* Return a location descriptor that designates a machine register for
13060    a given hard register number.  */
13061
13062 static dw_loc_descr_ref
13063 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
13064 {
13065   dw_loc_descr_ref reg_loc_descr;
13066
13067   if (regno <= 31)
13068     reg_loc_descr
13069       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
13070   else
13071     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
13072
13073   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13074     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13075
13076   return reg_loc_descr;
13077 }
13078
13079 /* Given an RTL of a register, return a location descriptor that
13080    designates a value that spans more than one register.  */
13081
13082 static dw_loc_descr_ref
13083 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
13084                              enum var_init_status initialized)
13085 {
13086   int nregs, size, i;
13087   unsigned reg;
13088   dw_loc_descr_ref loc_result = NULL;
13089
13090   reg = REGNO (rtl);
13091 #ifdef LEAF_REG_REMAP
13092   if (current_function_uses_only_leaf_regs)
13093     {
13094       int leaf_reg = LEAF_REG_REMAP (reg);
13095       if (leaf_reg != -1)
13096         reg = (unsigned) leaf_reg;
13097     }
13098 #endif
13099   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
13100   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
13101
13102   /* Simple, contiguous registers.  */
13103   if (regs == NULL_RTX)
13104     {
13105       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
13106
13107       loc_result = NULL;
13108       while (nregs--)
13109         {
13110           dw_loc_descr_ref t;
13111
13112           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
13113                                       VAR_INIT_STATUS_INITIALIZED);
13114           add_loc_descr (&loc_result, t);
13115           add_loc_descr_op_piece (&loc_result, size);
13116           ++reg;
13117         }
13118       return loc_result;
13119     }
13120
13121   /* Now onto stupid register sets in non contiguous locations.  */
13122
13123   gcc_assert (GET_CODE (regs) == PARALLEL);
13124
13125   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13126   loc_result = NULL;
13127
13128   for (i = 0; i < XVECLEN (regs, 0); ++i)
13129     {
13130       dw_loc_descr_ref t;
13131
13132       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
13133                                   VAR_INIT_STATUS_INITIALIZED);
13134       add_loc_descr (&loc_result, t);
13135       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
13136       add_loc_descr_op_piece (&loc_result, size);
13137     }
13138
13139   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
13140     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13141   return loc_result;
13142 }
13143
13144 #endif /* DWARF2_DEBUGGING_INFO */
13145
13146 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
13147
13148 /* Return a location descriptor that designates a constant.  */
13149
13150 static dw_loc_descr_ref
13151 int_loc_descriptor (HOST_WIDE_INT i)
13152 {
13153   enum dwarf_location_atom op;
13154
13155   /* Pick the smallest representation of a constant, rather than just
13156      defaulting to the LEB encoding.  */
13157   if (i >= 0)
13158     {
13159       if (i <= 31)
13160         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
13161       else if (i <= 0xff)
13162         op = DW_OP_const1u;
13163       else if (i <= 0xffff)
13164         op = DW_OP_const2u;
13165       else if (HOST_BITS_PER_WIDE_INT == 32
13166                || i <= 0xffffffff)
13167         op = DW_OP_const4u;
13168       else
13169         op = DW_OP_constu;
13170     }
13171   else
13172     {
13173       if (i >= -0x80)
13174         op = DW_OP_const1s;
13175       else if (i >= -0x8000)
13176         op = DW_OP_const2s;
13177       else if (HOST_BITS_PER_WIDE_INT == 32
13178                || i >= -0x80000000)
13179         op = DW_OP_const4s;
13180       else
13181         op = DW_OP_consts;
13182     }
13183
13184   return new_loc_descr (op, i, 0);
13185 }
13186 #endif
13187
13188 #ifdef DWARF2_DEBUGGING_INFO
13189 /* Return loc description representing "address" of integer value.
13190    This can appear only as toplevel expression.  */
13191
13192 static dw_loc_descr_ref
13193 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
13194 {
13195   int litsize;
13196   dw_loc_descr_ref loc_result = NULL;
13197
13198   if (!(dwarf_version >= 4 || !dwarf_strict))
13199     return NULL;
13200
13201   if (i >= 0)
13202     {
13203       if (i <= 31)
13204         litsize = 1;
13205       else if (i <= 0xff)
13206         litsize = 2;
13207       else if (i <= 0xffff)
13208         litsize = 3;
13209       else if (HOST_BITS_PER_WIDE_INT == 32
13210                || i <= 0xffffffff)
13211         litsize = 5;
13212       else
13213         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
13214     }
13215   else
13216     {
13217       if (i >= -0x80)
13218         litsize = 2;
13219       else if (i >= -0x8000)
13220         litsize = 3;
13221       else if (HOST_BITS_PER_WIDE_INT == 32
13222                || i >= -0x80000000)
13223         litsize = 5;
13224       else
13225         litsize = 1 + size_of_sleb128 (i);
13226     }
13227   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
13228      is more compact.  For DW_OP_stack_value we need:
13229      litsize + 1 (DW_OP_stack_value)
13230      and for DW_OP_implicit_value:
13231      1 (DW_OP_implicit_value) + 1 (length) + size.  */
13232   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
13233     {
13234       loc_result = int_loc_descriptor (i);
13235       add_loc_descr (&loc_result,
13236                      new_loc_descr (DW_OP_stack_value, 0, 0));
13237       return loc_result;
13238     }
13239
13240   loc_result = new_loc_descr (DW_OP_implicit_value,
13241                               size, 0);
13242   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
13243   loc_result->dw_loc_oprnd2.v.val_int = i;
13244   return loc_result;
13245 }
13246
13247 /* Return a location descriptor that designates a base+offset location.  */
13248
13249 static dw_loc_descr_ref
13250 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
13251                  enum var_init_status initialized)
13252 {
13253   unsigned int regno;
13254   dw_loc_descr_ref result;
13255   dw_fde_ref fde = current_fde ();
13256
13257   /* We only use "frame base" when we're sure we're talking about the
13258      post-prologue local stack frame.  We do this by *not* running
13259      register elimination until this point, and recognizing the special
13260      argument pointer and soft frame pointer rtx's.  */
13261   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
13262     {
13263       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13264
13265       if (elim != reg)
13266         {
13267           if (GET_CODE (elim) == PLUS)
13268             {
13269               offset += INTVAL (XEXP (elim, 1));
13270               elim = XEXP (elim, 0);
13271             }
13272           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13273                        && (elim == hard_frame_pointer_rtx
13274                            || elim == stack_pointer_rtx))
13275                       || elim == (frame_pointer_needed
13276                                   ? hard_frame_pointer_rtx
13277                                   : stack_pointer_rtx));
13278
13279           /* If drap register is used to align stack, use frame
13280              pointer + offset to access stack variables.  If stack
13281              is aligned without drap, use stack pointer + offset to
13282              access stack variables.  */
13283           if (crtl->stack_realign_tried
13284               && reg == frame_pointer_rtx)
13285             {
13286               int base_reg
13287                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
13288                                       ? HARD_FRAME_POINTER_REGNUM
13289                                       : STACK_POINTER_REGNUM);
13290               return new_reg_loc_descr (base_reg, offset);
13291             }
13292
13293           offset += frame_pointer_fb_offset;
13294           return new_loc_descr (DW_OP_fbreg, offset, 0);
13295         }
13296     }
13297   else if (!optimize
13298            && fde
13299            && (fde->drap_reg == REGNO (reg)
13300                || fde->vdrap_reg == REGNO (reg)))
13301     {
13302       /* Use cfa+offset to represent the location of arguments passed
13303          on the stack when drap is used to align stack.
13304          Only do this when not optimizing, for optimized code var-tracking
13305          is supposed to track where the arguments live and the register
13306          used as vdrap or drap in some spot might be used for something
13307          else in other part of the routine.  */
13308       return new_loc_descr (DW_OP_fbreg, offset, 0);
13309     }
13310
13311   regno = dbx_reg_number (reg);
13312   if (regno <= 31)
13313     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
13314                             offset, 0);
13315   else
13316     result = new_loc_descr (DW_OP_bregx, regno, offset);
13317
13318   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
13319     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
13320
13321   return result;
13322 }
13323
13324 /* Return true if this RTL expression describes a base+offset calculation.  */
13325
13326 static inline int
13327 is_based_loc (const_rtx rtl)
13328 {
13329   return (GET_CODE (rtl) == PLUS
13330           && ((REG_P (XEXP (rtl, 0))
13331                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
13332                && CONST_INT_P (XEXP (rtl, 1)))));
13333 }
13334
13335 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
13336    failed.  */
13337
13338 static dw_loc_descr_ref
13339 tls_mem_loc_descriptor (rtx mem)
13340 {
13341   tree base;
13342   dw_loc_descr_ref loc_result;
13343
13344   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
13345     return NULL;
13346
13347   base = get_base_address (MEM_EXPR (mem));
13348   if (base == NULL
13349       || TREE_CODE (base) != VAR_DECL
13350       || !DECL_THREAD_LOCAL_P (base))
13351     return NULL;
13352
13353   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
13354   if (loc_result == NULL)
13355     return NULL;
13356
13357   if (INTVAL (MEM_OFFSET (mem)))
13358     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
13359
13360   return loc_result;
13361 }
13362
13363 /* Output debug info about reason why we failed to expand expression as dwarf
13364    expression.  */
13365
13366 static void
13367 expansion_failed (tree expr, rtx rtl, char const *reason)
13368 {
13369   if (dump_file && (dump_flags & TDF_DETAILS))
13370     {
13371       fprintf (dump_file, "Failed to expand as dwarf: ");
13372       if (expr)
13373         print_generic_expr (dump_file, expr, dump_flags);
13374       if (rtl)
13375         {
13376           fprintf (dump_file, "\n");
13377           print_rtl (dump_file, rtl);
13378         }
13379       fprintf (dump_file, "\nReason: %s\n", reason);
13380     }
13381 }
13382
13383 /* Helper function for const_ok_for_output, called either directly
13384    or via for_each_rtx.  */
13385
13386 static int
13387 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
13388 {
13389   rtx rtl = *rtlp;
13390
13391   if (GET_CODE (rtl) == UNSPEC)
13392     {
13393       /* If delegitimize_address couldn't do anything with the UNSPEC, assume
13394          we can't express it in the debug info.  */
13395 #ifdef ENABLE_CHECKING
13396       inform (current_function_decl
13397               ? DECL_SOURCE_LOCATION (current_function_decl)
13398               : UNKNOWN_LOCATION,
13399               "non-delegitimized UNSPEC %d found in variable location",
13400               XINT (rtl, 1));
13401 #endif
13402       expansion_failed (NULL_TREE, rtl,
13403                         "UNSPEC hasn't been delegitimized.\n");
13404       return 1;
13405     }
13406
13407   if (GET_CODE (rtl) != SYMBOL_REF)
13408     return 0;
13409
13410   if (CONSTANT_POOL_ADDRESS_P (rtl))
13411     {
13412       bool marked;
13413       get_pool_constant_mark (rtl, &marked);
13414       /* If all references to this pool constant were optimized away,
13415          it was not output and thus we can't represent it.  */
13416       if (!marked)
13417         {
13418           expansion_failed (NULL_TREE, rtl,
13419                             "Constant was removed from constant pool.\n");
13420           return 1;
13421         }
13422     }
13423
13424   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13425     return 1;
13426
13427   /* Avoid references to external symbols in debug info, on several targets
13428      the linker might even refuse to link when linking a shared library,
13429      and in many other cases the relocations for .debug_info/.debug_loc are
13430      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
13431      to be defined within the same shared library or executable are fine.  */
13432   if (SYMBOL_REF_EXTERNAL_P (rtl))
13433     {
13434       tree decl = SYMBOL_REF_DECL (rtl);
13435
13436       if (decl == NULL || !targetm.binds_local_p (decl))
13437         {
13438           expansion_failed (NULL_TREE, rtl,
13439                             "Symbol not defined in current TU.\n");
13440           return 1;
13441         }
13442     }
13443
13444   return 0;
13445 }
13446
13447 /* Return true if constant RTL can be emitted in DW_OP_addr or
13448    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
13449    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
13450
13451 static bool
13452 const_ok_for_output (rtx rtl)
13453 {
13454   if (GET_CODE (rtl) == SYMBOL_REF)
13455     return const_ok_for_output_1 (&rtl, NULL) == 0;
13456
13457   if (GET_CODE (rtl) == CONST)
13458     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
13459
13460   return true;
13461 }
13462
13463 /* The following routine converts the RTL for a variable or parameter
13464    (resident in memory) into an equivalent Dwarf representation of a
13465    mechanism for getting the address of that same variable onto the top of a
13466    hypothetical "address evaluation" stack.
13467
13468    When creating memory location descriptors, we are effectively transforming
13469    the RTL for a memory-resident object into its Dwarf postfix expression
13470    equivalent.  This routine recursively descends an RTL tree, turning
13471    it into Dwarf postfix code as it goes.
13472
13473    MODE is the mode of the memory reference, needed to handle some
13474    autoincrement addressing modes.
13475
13476    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
13477    location list for RTL.
13478
13479    Return 0 if we can't represent the location.  */
13480
13481 static dw_loc_descr_ref
13482 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
13483                     enum var_init_status initialized)
13484 {
13485   dw_loc_descr_ref mem_loc_result = NULL;
13486   enum dwarf_location_atom op;
13487   dw_loc_descr_ref op0, op1;
13488
13489   /* Note that for a dynamically sized array, the location we will generate a
13490      description of here will be the lowest numbered location which is
13491      actually within the array.  That's *not* necessarily the same as the
13492      zeroth element of the array.  */
13493
13494   rtl = targetm.delegitimize_address (rtl);
13495
13496   switch (GET_CODE (rtl))
13497     {
13498     case POST_INC:
13499     case POST_DEC:
13500     case POST_MODIFY:
13501       return mem_loc_descriptor (XEXP (rtl, 0), mode, initialized);
13502
13503     case SUBREG:
13504       /* The case of a subreg may arise when we have a local (register)
13505          variable or a formal (register) parameter which doesn't quite fill
13506          up an entire register.  For now, just assume that it is
13507          legitimate to make the Dwarf info refer to the whole register which
13508          contains the given subreg.  */
13509       if (!subreg_lowpart_p (rtl))
13510         break;
13511       rtl = SUBREG_REG (rtl);
13512       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13513         break;
13514       if (GET_MODE_CLASS (GET_MODE (rtl)) != MODE_INT)
13515         break;
13516       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
13517       break;
13518
13519     case REG:
13520       /* Whenever a register number forms a part of the description of the
13521          method for calculating the (dynamic) address of a memory resident
13522          object, DWARF rules require the register number be referred to as
13523          a "base register".  This distinction is not based in any way upon
13524          what category of register the hardware believes the given register
13525          belongs to.  This is strictly DWARF terminology we're dealing with
13526          here. Note that in cases where the location of a memory-resident
13527          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
13528          OP_CONST (0)) the actual DWARF location descriptor that we generate
13529          may just be OP_BASEREG (basereg).  This may look deceptively like
13530          the object in question was allocated to a register (rather than in
13531          memory) so DWARF consumers need to be aware of the subtle
13532          distinction between OP_REG and OP_BASEREG.  */
13533       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
13534         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
13535       else if (stack_realign_drap
13536                && crtl->drap_reg
13537                && crtl->args.internal_arg_pointer == rtl
13538                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
13539         {
13540           /* If RTL is internal_arg_pointer, which has been optimized
13541              out, use DRAP instead.  */
13542           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
13543                                             VAR_INIT_STATUS_INITIALIZED);
13544         }
13545       break;
13546
13547     case SIGN_EXTEND:
13548     case ZERO_EXTEND:
13549       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13550                                 VAR_INIT_STATUS_INITIALIZED);
13551       if (op0 == 0)
13552         break;
13553       else
13554         {
13555           int shift = DWARF2_ADDR_SIZE
13556                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
13557           shift *= BITS_PER_UNIT;
13558           if (GET_CODE (rtl) == SIGN_EXTEND)
13559             op = DW_OP_shra;
13560           else
13561             op = DW_OP_shr;
13562           mem_loc_result = op0;
13563           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13564           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
13565           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
13566           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13567         }
13568       break;
13569
13570     case MEM:
13571       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
13572                                            VAR_INIT_STATUS_INITIALIZED);
13573       if (mem_loc_result == NULL)
13574         mem_loc_result = tls_mem_loc_descriptor (rtl);
13575       if (mem_loc_result != 0)
13576         {
13577           if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
13578             {
13579               expansion_failed (NULL_TREE, rtl, "DWARF address size mismatch");
13580               return 0;
13581             }
13582           else if (GET_MODE_SIZE (GET_MODE (rtl)) == DWARF2_ADDR_SIZE)
13583             add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
13584           else
13585             add_loc_descr (&mem_loc_result,
13586                            new_loc_descr (DW_OP_deref_size,
13587                                           GET_MODE_SIZE (GET_MODE (rtl)), 0));
13588         }
13589       else
13590         {
13591           rtx new_rtl = avoid_constant_pool_reference (rtl);
13592           if (new_rtl != rtl)
13593             return mem_loc_descriptor (new_rtl, mode, initialized);
13594         }
13595       break;
13596
13597     case LO_SUM:
13598          rtl = XEXP (rtl, 1);
13599
13600       /* ... fall through ...  */
13601
13602     case LABEL_REF:
13603       /* Some ports can transform a symbol ref into a label ref, because
13604          the symbol ref is too far away and has to be dumped into a constant
13605          pool.  */
13606     case CONST:
13607     case SYMBOL_REF:
13608       if (GET_CODE (rtl) == SYMBOL_REF
13609           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
13610         {
13611           dw_loc_descr_ref temp;
13612
13613           /* If this is not defined, we have no way to emit the data.  */
13614           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
13615             break;
13616
13617           /* We used to emit DW_OP_addr here, but that's wrong, since
13618              DW_OP_addr should be relocated by the debug info consumer,
13619              while DW_OP_GNU_push_tls_address operand should not.  */
13620           temp = new_loc_descr (DWARF2_ADDR_SIZE == 4
13621                                 ? DW_OP_const4u : DW_OP_const8u, 0, 0);
13622           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
13623           temp->dw_loc_oprnd1.v.val_addr = rtl;
13624           temp->dtprel = true;
13625
13626           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
13627           add_loc_descr (&mem_loc_result, temp);
13628
13629           break;
13630         }
13631
13632       if (!const_ok_for_output (rtl))
13633         break;
13634
13635     symref:
13636       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
13637       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
13638       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
13639       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13640       break;
13641
13642     case CONCAT:
13643     case CONCATN:
13644     case VAR_LOCATION:
13645       expansion_failed (NULL_TREE, rtl,
13646                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
13647       return 0;
13648
13649     case PRE_MODIFY:
13650       /* Extract the PLUS expression nested inside and fall into
13651          PLUS code below.  */
13652       rtl = XEXP (rtl, 1);
13653       goto plus;
13654
13655     case PRE_INC:
13656     case PRE_DEC:
13657       /* Turn these into a PLUS expression and fall into the PLUS code
13658          below.  */
13659       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
13660                           GEN_INT (GET_CODE (rtl) == PRE_INC
13661                                    ? GET_MODE_UNIT_SIZE (mode)
13662                                    : -GET_MODE_UNIT_SIZE (mode)));
13663
13664       /* ... fall through ...  */
13665
13666     case PLUS:
13667     plus:
13668       if (is_based_loc (rtl))
13669         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
13670                                           INTVAL (XEXP (rtl, 1)),
13671                                           VAR_INIT_STATUS_INITIALIZED);
13672       else
13673         {
13674           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
13675                                                VAR_INIT_STATUS_INITIALIZED);
13676           if (mem_loc_result == 0)
13677             break;
13678
13679           if (CONST_INT_P (XEXP (rtl, 1)))
13680             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
13681           else
13682             {
13683               dw_loc_descr_ref mem_loc_result2
13684                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13685                                       VAR_INIT_STATUS_INITIALIZED);
13686               if (mem_loc_result2 == 0)
13687                 break;
13688               add_loc_descr (&mem_loc_result, mem_loc_result2);
13689               add_loc_descr (&mem_loc_result,
13690                              new_loc_descr (DW_OP_plus, 0, 0));
13691             }
13692         }
13693       break;
13694
13695     /* If a pseudo-reg is optimized away, it is possible for it to
13696        be replaced with a MEM containing a multiply or shift.  */
13697     case MINUS:
13698       op = DW_OP_minus;
13699       goto do_binop;
13700
13701     case MULT:
13702       op = DW_OP_mul;
13703       goto do_binop;
13704
13705     case DIV:
13706       op = DW_OP_div;
13707       goto do_binop;
13708
13709     case UMOD:
13710       op = DW_OP_mod;
13711       goto do_binop;
13712
13713     case ASHIFT:
13714       op = DW_OP_shl;
13715       goto do_binop;
13716
13717     case ASHIFTRT:
13718       op = DW_OP_shra;
13719       goto do_binop;
13720
13721     case LSHIFTRT:
13722       op = DW_OP_shr;
13723       goto do_binop;
13724
13725     case AND:
13726       op = DW_OP_and;
13727       goto do_binop;
13728
13729     case IOR:
13730       op = DW_OP_or;
13731       goto do_binop;
13732
13733     case XOR:
13734       op = DW_OP_xor;
13735       goto do_binop;
13736
13737     do_binop:
13738       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13739                                 VAR_INIT_STATUS_INITIALIZED);
13740       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13741                                 VAR_INIT_STATUS_INITIALIZED);
13742
13743       if (op0 == 0 || op1 == 0)
13744         break;
13745
13746       mem_loc_result = op0;
13747       add_loc_descr (&mem_loc_result, op1);
13748       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13749       break;
13750
13751     case MOD:
13752       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13753                                 VAR_INIT_STATUS_INITIALIZED);
13754       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13755                                 VAR_INIT_STATUS_INITIALIZED);
13756
13757       if (op0 == 0 || op1 == 0)
13758         break;
13759
13760       mem_loc_result = op0;
13761       add_loc_descr (&mem_loc_result, op1);
13762       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13763       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_over, 0, 0));
13764       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_div, 0, 0));
13765       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13766       add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_minus, 0, 0));
13767       break;
13768
13769     case NOT:
13770       op = DW_OP_not;
13771       goto do_unop;
13772
13773     case ABS:
13774       op = DW_OP_abs;
13775       goto do_unop;
13776
13777     case NEG:
13778       op = DW_OP_neg;
13779       goto do_unop;
13780
13781     do_unop:
13782       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13783                                 VAR_INIT_STATUS_INITIALIZED);
13784
13785       if (op0 == 0)
13786         break;
13787
13788       mem_loc_result = op0;
13789       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13790       break;
13791
13792     case CONST_INT:
13793       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
13794       break;
13795
13796     case EQ:
13797       op = DW_OP_eq;
13798       goto do_scompare;
13799
13800     case GE:
13801       op = DW_OP_ge;
13802       goto do_scompare;
13803
13804     case GT:
13805       op = DW_OP_gt;
13806       goto do_scompare;
13807
13808     case LE:
13809       op = DW_OP_le;
13810       goto do_scompare;
13811
13812     case LT:
13813       op = DW_OP_lt;
13814       goto do_scompare;
13815
13816     case NE:
13817       op = DW_OP_ne;
13818       goto do_scompare;
13819
13820     do_scompare:
13821       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13822           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13823         break;
13824       else
13825         {
13826           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13827
13828           if (op_mode == VOIDmode)
13829             op_mode = GET_MODE (XEXP (rtl, 1));
13830           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13831             break;
13832
13833           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13834                                     VAR_INIT_STATUS_INITIALIZED);
13835           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13836                                     VAR_INIT_STATUS_INITIALIZED);
13837
13838           if (op0 == 0 || op1 == 0)
13839             break;
13840
13841           if (op_mode != VOIDmode
13842               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13843             {
13844               int shift = DWARF2_ADDR_SIZE - GET_MODE_SIZE (op_mode);
13845               shift *= BITS_PER_UNIT;
13846               /* For eq/ne, if the operands are known to be zero-extended,
13847                  there is no need to do the fancy shifting up.  */
13848               if (op == DW_OP_eq || op == DW_OP_ne)
13849                 {
13850                   dw_loc_descr_ref last0, last1;
13851                   for (last0 = op0;
13852                        last0->dw_loc_next != NULL;
13853                        last0 = last0->dw_loc_next)
13854                     ;
13855                   for (last1 = op1;
13856                        last1->dw_loc_next != NULL;
13857                        last1 = last1->dw_loc_next)
13858                     ;
13859                   /* deref_size zero extends, and for constants we can check
13860                      whether they are zero extended or not.  */
13861                   if (((last0->dw_loc_opc == DW_OP_deref_size
13862                         && last0->dw_loc_oprnd1.v.val_int
13863                            <= GET_MODE_SIZE (op_mode))
13864                        || (CONST_INT_P (XEXP (rtl, 0))
13865                             && (unsigned HOST_WIDE_INT) INTVAL (XEXP (rtl, 0))
13866                                == (INTVAL (XEXP (rtl, 0))
13867                                    & GET_MODE_MASK (op_mode))))
13868                       && ((last1->dw_loc_opc == DW_OP_deref_size
13869                            && last1->dw_loc_oprnd1.v.val_int
13870                               <= GET_MODE_SIZE (op_mode))
13871                           || (CONST_INT_P (XEXP (rtl, 1))
13872                               && (unsigned HOST_WIDE_INT)
13873                                  INTVAL (XEXP (rtl, 1))
13874                                  == (INTVAL (XEXP (rtl, 1))
13875                                      & GET_MODE_MASK (op_mode)))))
13876                     goto do_compare;
13877                 }
13878               add_loc_descr (&op0, int_loc_descriptor (shift));
13879               add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
13880               if (CONST_INT_P (XEXP (rtl, 1)))
13881                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
13882               else
13883                 {
13884                   add_loc_descr (&op1, int_loc_descriptor (shift));
13885                   add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
13886                 }
13887             }
13888         }
13889
13890     do_compare:
13891       mem_loc_result = op0;
13892       add_loc_descr (&mem_loc_result, op1);
13893       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
13894       if (STORE_FLAG_VALUE != 1)
13895         {
13896           add_loc_descr (&mem_loc_result,
13897                          int_loc_descriptor (STORE_FLAG_VALUE));
13898           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
13899         }
13900       break;
13901
13902     case GEU:
13903       op = DW_OP_ge;
13904       goto do_ucompare;
13905
13906     case GTU:
13907       op = DW_OP_gt;
13908       goto do_ucompare;
13909
13910     case LEU:
13911       op = DW_OP_le;
13912       goto do_ucompare;
13913
13914     case LTU:
13915       op = DW_OP_lt;
13916       goto do_ucompare;
13917
13918     do_ucompare:
13919       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13920           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 1))) > DWARF2_ADDR_SIZE)
13921         break;
13922       else
13923         {
13924           enum machine_mode op_mode = GET_MODE (XEXP (rtl, 0));
13925
13926           if (op_mode == VOIDmode)
13927             op_mode = GET_MODE (XEXP (rtl, 1));
13928           if (op_mode != VOIDmode && GET_MODE_CLASS (op_mode) != MODE_INT)
13929             break;
13930
13931           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13932                                     VAR_INIT_STATUS_INITIALIZED);
13933           op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
13934                                     VAR_INIT_STATUS_INITIALIZED);
13935
13936           if (op0 == 0 || op1 == 0)
13937             break;
13938
13939           if (op_mode != VOIDmode
13940               && GET_MODE_SIZE (op_mode) < DWARF2_ADDR_SIZE)
13941             {
13942               HOST_WIDE_INT mask = GET_MODE_MASK (op_mode);
13943               dw_loc_descr_ref last0, last1;
13944               for (last0 = op0;
13945                    last0->dw_loc_next != NULL;
13946                    last0 = last0->dw_loc_next)
13947                 ;
13948               for (last1 = op1;
13949                    last1->dw_loc_next != NULL;
13950                    last1 = last1->dw_loc_next)
13951                 ;
13952               if (CONST_INT_P (XEXP (rtl, 0)))
13953                 op0 = int_loc_descriptor (INTVAL (XEXP (rtl, 0)) & mask);
13954               /* deref_size zero extends, so no need to mask it again.  */
13955               else if (last0->dw_loc_opc != DW_OP_deref_size
13956                        || last0->dw_loc_oprnd1.v.val_int
13957                           > GET_MODE_SIZE (op_mode))
13958                 {
13959                   add_loc_descr (&op0, int_loc_descriptor (mask));
13960                   add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
13961                 }
13962               if (CONST_INT_P (XEXP (rtl, 1)))
13963                 op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
13964               /* deref_size zero extends, so no need to mask it again.  */
13965               else if (last1->dw_loc_opc != DW_OP_deref_size
13966                        || last1->dw_loc_oprnd1.v.val_int
13967                           > GET_MODE_SIZE (op_mode))
13968                 {
13969                   add_loc_descr (&op1, int_loc_descriptor (mask));
13970                   add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
13971                 }
13972             }
13973           else
13974             {
13975               HOST_WIDE_INT bias = 1;
13976               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
13977               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
13978               if (CONST_INT_P (XEXP (rtl, 1)))
13979                 op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
13980                                           + INTVAL (XEXP (rtl, 1)));
13981               else
13982                 add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst,
13983                                                     bias, 0));
13984             }
13985         }
13986       goto do_compare;
13987
13988     case SMIN:
13989     case SMAX:
13990     case UMIN:
13991     case UMAX:
13992       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
13993           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
13994           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
13995         break;
13996
13997       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
13998                                 VAR_INIT_STATUS_INITIALIZED);
13999       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
14000                                 VAR_INIT_STATUS_INITIALIZED);
14001
14002       if (op0 == 0 || op1 == 0)
14003         break;
14004
14005       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
14006       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
14007       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
14008       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
14009         {
14010           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14011             {
14012               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
14013               add_loc_descr (&op0, int_loc_descriptor (mask));
14014               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
14015               add_loc_descr (&op1, int_loc_descriptor (mask));
14016               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
14017             }
14018           else
14019             {
14020               HOST_WIDE_INT bias = 1;
14021               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
14022               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14023               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
14024             }
14025         }
14026       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
14027         {
14028           int shift = DWARF2_ADDR_SIZE
14029                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
14030           shift *= BITS_PER_UNIT;
14031           add_loc_descr (&op0, int_loc_descriptor (shift));
14032           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
14033           add_loc_descr (&op1, int_loc_descriptor (shift));
14034           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
14035         }
14036
14037       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
14038         op = DW_OP_lt;
14039       else
14040         op = DW_OP_gt;
14041       mem_loc_result = op0;
14042       add_loc_descr (&mem_loc_result, op1);
14043       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14044       {
14045         dw_loc_descr_ref bra_node, drop_node;
14046
14047         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
14048         add_loc_descr (&mem_loc_result, bra_node);
14049         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
14050         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
14051         add_loc_descr (&mem_loc_result, drop_node);
14052         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
14053         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
14054       }
14055       break;
14056
14057     case ZERO_EXTRACT:
14058     case SIGN_EXTRACT:
14059       if (CONST_INT_P (XEXP (rtl, 1))
14060           && CONST_INT_P (XEXP (rtl, 2))
14061           && ((unsigned) INTVAL (XEXP (rtl, 1))
14062               + (unsigned) INTVAL (XEXP (rtl, 2))
14063               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
14064           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14065           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
14066         {
14067           int shift, size;
14068           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
14069                                     VAR_INIT_STATUS_INITIALIZED);
14070           if (op0 == 0)
14071             break;
14072           if (GET_CODE (rtl) == SIGN_EXTRACT)
14073             op = DW_OP_shra;
14074           else
14075             op = DW_OP_shr;
14076           mem_loc_result = op0;
14077           size = INTVAL (XEXP (rtl, 1));
14078           shift = INTVAL (XEXP (rtl, 2));
14079           if (BITS_BIG_ENDIAN)
14080             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
14081                     - shift - size;
14082           if (shift + size != (int) DWARF2_ADDR_SIZE)
14083             {
14084               add_loc_descr (&mem_loc_result,
14085                              int_loc_descriptor (DWARF2_ADDR_SIZE
14086                                                  - shift - size));
14087               add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
14088             }
14089           if (size != (int) DWARF2_ADDR_SIZE)
14090             {
14091               add_loc_descr (&mem_loc_result,
14092                              int_loc_descriptor (DWARF2_ADDR_SIZE - size));
14093               add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
14094             }
14095         }
14096       break;
14097
14098     case COMPARE:
14099     case IF_THEN_ELSE:
14100     case ROTATE:
14101     case ROTATERT:
14102     case TRUNCATE:
14103       /* In theory, we could implement the above.  */
14104       /* DWARF cannot represent the unsigned compare operations
14105          natively.  */
14106     case SS_MULT:
14107     case US_MULT:
14108     case SS_DIV:
14109     case US_DIV:
14110     case SS_PLUS:
14111     case US_PLUS:
14112     case SS_MINUS:
14113     case US_MINUS:
14114     case SS_NEG:
14115     case US_NEG:
14116     case SS_ABS:
14117     case SS_ASHIFT:
14118     case US_ASHIFT:
14119     case SS_TRUNCATE:
14120     case US_TRUNCATE:
14121     case UDIV:
14122     case UNORDERED:
14123     case ORDERED:
14124     case UNEQ:
14125     case UNGE:
14126     case UNGT:
14127     case UNLE:
14128     case UNLT:
14129     case LTGT:
14130     case FLOAT_EXTEND:
14131     case FLOAT_TRUNCATE:
14132     case FLOAT:
14133     case UNSIGNED_FLOAT:
14134     case FIX:
14135     case UNSIGNED_FIX:
14136     case FRACT_CONVERT:
14137     case UNSIGNED_FRACT_CONVERT:
14138     case SAT_FRACT:
14139     case UNSIGNED_SAT_FRACT:
14140     case SQRT:
14141     case BSWAP:
14142     case FFS:
14143     case CLZ:
14144     case CTZ:
14145     case POPCOUNT:
14146     case PARITY:
14147     case ASM_OPERANDS:
14148     case VEC_MERGE:
14149     case VEC_SELECT:
14150     case VEC_CONCAT:
14151     case VEC_DUPLICATE:
14152     case UNSPEC:
14153     case HIGH:
14154       /* If delegitimize_address couldn't do anything with the UNSPEC, we
14155          can't express it in the debug info.  This can happen e.g. with some
14156          TLS UNSPECs.  */
14157       break;
14158
14159     case CONST_STRING:
14160       resolve_one_addr (&rtl, NULL);
14161       goto symref;
14162
14163     default:
14164 #ifdef ENABLE_CHECKING
14165       print_rtl (stderr, rtl);
14166       gcc_unreachable ();
14167 #else
14168       break;
14169 #endif
14170     }
14171
14172   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14173     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14174
14175   return mem_loc_result;
14176 }
14177
14178 /* Return a descriptor that describes the concatenation of two locations.
14179    This is typically a complex variable.  */
14180
14181 static dw_loc_descr_ref
14182 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
14183 {
14184   dw_loc_descr_ref cc_loc_result = NULL;
14185   dw_loc_descr_ref x0_ref
14186     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14187   dw_loc_descr_ref x1_ref
14188     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14189
14190   if (x0_ref == 0 || x1_ref == 0)
14191     return 0;
14192
14193   cc_loc_result = x0_ref;
14194   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
14195
14196   add_loc_descr (&cc_loc_result, x1_ref);
14197   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
14198
14199   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
14200     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14201
14202   return cc_loc_result;
14203 }
14204
14205 /* Return a descriptor that describes the concatenation of N
14206    locations.  */
14207
14208 static dw_loc_descr_ref
14209 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
14210 {
14211   unsigned int i;
14212   dw_loc_descr_ref cc_loc_result = NULL;
14213   unsigned int n = XVECLEN (concatn, 0);
14214
14215   for (i = 0; i < n; ++i)
14216     {
14217       dw_loc_descr_ref ref;
14218       rtx x = XVECEXP (concatn, 0, i);
14219
14220       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
14221       if (ref == NULL)
14222         return NULL;
14223
14224       add_loc_descr (&cc_loc_result, ref);
14225       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
14226     }
14227
14228   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
14229     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
14230
14231   return cc_loc_result;
14232 }
14233
14234 /* Output a proper Dwarf location descriptor for a variable or parameter
14235    which is either allocated in a register or in a memory location.  For a
14236    register, we just generate an OP_REG and the register number.  For a
14237    memory location we provide a Dwarf postfix expression describing how to
14238    generate the (dynamic) address of the object onto the address stack.
14239
14240    MODE is mode of the decl if this loc_descriptor is going to be used in
14241    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
14242    allowed, VOIDmode otherwise.
14243
14244    If we don't know how to describe it, return 0.  */
14245
14246 static dw_loc_descr_ref
14247 loc_descriptor (rtx rtl, enum machine_mode mode,
14248                 enum var_init_status initialized)
14249 {
14250   dw_loc_descr_ref loc_result = NULL;
14251
14252   switch (GET_CODE (rtl))
14253     {
14254     case SUBREG:
14255       /* The case of a subreg may arise when we have a local (register)
14256          variable or a formal (register) parameter which doesn't quite fill
14257          up an entire register.  For now, just assume that it is
14258          legitimate to make the Dwarf info refer to the whole register which
14259          contains the given subreg.  */
14260       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
14261       break;
14262
14263     case REG:
14264       loc_result = reg_loc_descriptor (rtl, initialized);
14265       break;
14266
14267     case MEM:
14268       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
14269                                        initialized);
14270       if (loc_result == NULL)
14271         loc_result = tls_mem_loc_descriptor (rtl);
14272       if (loc_result == NULL)
14273         {
14274           rtx new_rtl = avoid_constant_pool_reference (rtl);
14275           if (new_rtl != rtl)
14276             loc_result = loc_descriptor (new_rtl, mode, initialized);
14277         }
14278       break;
14279
14280     case CONCAT:
14281       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
14282                                           initialized);
14283       break;
14284
14285     case CONCATN:
14286       loc_result = concatn_loc_descriptor (rtl, initialized);
14287       break;
14288
14289     case VAR_LOCATION:
14290       /* Single part.  */
14291       if (GET_CODE (PAT_VAR_LOCATION_LOC (rtl)) != PARALLEL)
14292         {
14293           rtx loc = PAT_VAR_LOCATION_LOC (rtl);
14294           if (GET_CODE (loc) == EXPR_LIST)
14295             loc = XEXP (loc, 0);
14296           loc_result = loc_descriptor (loc, mode, initialized);
14297           break;
14298         }
14299
14300       rtl = XEXP (rtl, 1);
14301       /* FALLTHRU */
14302
14303     case PARALLEL:
14304       {
14305         rtvec par_elems = XVEC (rtl, 0);
14306         int num_elem = GET_NUM_ELEM (par_elems);
14307         enum machine_mode mode;
14308         int i;
14309
14310         /* Create the first one, so we have something to add to.  */
14311         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
14312                                      VOIDmode, initialized);
14313         if (loc_result == NULL)
14314           return NULL;
14315         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
14316         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14317         for (i = 1; i < num_elem; i++)
14318           {
14319             dw_loc_descr_ref temp;
14320
14321             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
14322                                    VOIDmode, initialized);
14323             if (temp == NULL)
14324               return NULL;
14325             add_loc_descr (&loc_result, temp);
14326             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
14327             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
14328           }
14329       }
14330       break;
14331
14332     case CONST_INT:
14333       if (mode != VOIDmode && mode != BLKmode)
14334         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
14335                                                     INTVAL (rtl));
14336       break;
14337
14338     case CONST_DOUBLE:
14339       if (mode == VOIDmode)
14340         mode = GET_MODE (rtl);
14341
14342       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14343         {
14344           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14345
14346           /* Note that a CONST_DOUBLE rtx could represent either an integer
14347              or a floating-point constant.  A CONST_DOUBLE is used whenever
14348              the constant requires more than one word in order to be
14349              adequately represented.  We output CONST_DOUBLEs as blocks.  */
14350           loc_result = new_loc_descr (DW_OP_implicit_value,
14351                                       GET_MODE_SIZE (mode), 0);
14352           if (SCALAR_FLOAT_MODE_P (mode))
14353             {
14354               unsigned int length = GET_MODE_SIZE (mode);
14355               unsigned char *array
14356                   = (unsigned char*) ggc_alloc_atomic (length);
14357
14358               insert_float (rtl, array);
14359               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14360               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
14361               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
14362               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14363             }
14364           else
14365             {
14366               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
14367               loc_result->dw_loc_oprnd2.v.val_double
14368                 = rtx_to_double_int (rtl);
14369             }
14370         }
14371       break;
14372
14373     case CONST_VECTOR:
14374       if (mode == VOIDmode)
14375         mode = GET_MODE (rtl);
14376
14377       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
14378         {
14379           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
14380           unsigned int length = CONST_VECTOR_NUNITS (rtl);
14381           unsigned char *array = (unsigned char *)
14382             ggc_alloc_atomic (length * elt_size);
14383           unsigned int i;
14384           unsigned char *p;
14385
14386           gcc_assert (mode == GET_MODE (rtl) || VOIDmode == GET_MODE (rtl));
14387           switch (GET_MODE_CLASS (mode))
14388             {
14389             case MODE_VECTOR_INT:
14390               for (i = 0, p = array; i < length; i++, p += elt_size)
14391                 {
14392                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14393                   double_int val = rtx_to_double_int (elt);
14394
14395                   if (elt_size <= sizeof (HOST_WIDE_INT))
14396                     insert_int (double_int_to_shwi (val), elt_size, p);
14397                   else
14398                     {
14399                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
14400                       insert_double (val, p);
14401                     }
14402                 }
14403               break;
14404
14405             case MODE_VECTOR_FLOAT:
14406               for (i = 0, p = array; i < length; i++, p += elt_size)
14407                 {
14408                   rtx elt = CONST_VECTOR_ELT (rtl, i);
14409                   insert_float (elt, p);
14410                 }
14411               break;
14412
14413             default:
14414               gcc_unreachable ();
14415             }
14416
14417           loc_result = new_loc_descr (DW_OP_implicit_value,
14418                                       length * elt_size, 0);
14419           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
14420           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
14421           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
14422           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
14423         }
14424       break;
14425
14426     case CONST:
14427       if (mode == VOIDmode
14428           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
14429           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
14430           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
14431         {
14432           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
14433           break;
14434         }
14435       /* FALLTHROUGH */
14436     case SYMBOL_REF:
14437       if (!const_ok_for_output (rtl))
14438         break;
14439     case LABEL_REF:
14440       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
14441           && (dwarf_version >= 4 || !dwarf_strict))
14442         {
14443           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
14444           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
14445           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
14446           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
14447           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
14448         }
14449       break;
14450
14451     default:
14452       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
14453           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
14454           && (dwarf_version >= 4 || !dwarf_strict))
14455         {
14456           /* Value expression.  */
14457           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
14458           if (loc_result)
14459             add_loc_descr (&loc_result,
14460                            new_loc_descr (DW_OP_stack_value, 0, 0));
14461         }
14462       break;
14463     }
14464
14465   return loc_result;
14466 }
14467
14468 /* We need to figure out what section we should use as the base for the
14469    address ranges where a given location is valid.
14470    1. If this particular DECL has a section associated with it, use that.
14471    2. If this function has a section associated with it, use that.
14472    3. Otherwise, use the text section.
14473    XXX: If you split a variable across multiple sections, we won't notice.  */
14474
14475 static const char *
14476 secname_for_decl (const_tree decl)
14477 {
14478   const char *secname;
14479
14480   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
14481     {
14482       tree sectree = DECL_SECTION_NAME (decl);
14483       secname = TREE_STRING_POINTER (sectree);
14484     }
14485   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
14486     {
14487       tree sectree = DECL_SECTION_NAME (current_function_decl);
14488       secname = TREE_STRING_POINTER (sectree);
14489     }
14490   else if (cfun && in_cold_section_p)
14491     secname = crtl->subsections.cold_section_label;
14492   else
14493     secname = text_section_label;
14494
14495   return secname;
14496 }
14497
14498 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
14499
14500 static bool
14501 decl_by_reference_p (tree decl)
14502 {
14503   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
14504            || TREE_CODE (decl) == VAR_DECL)
14505           && DECL_BY_REFERENCE (decl));
14506 }
14507
14508 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14509    for VARLOC.  */
14510
14511 static dw_loc_descr_ref
14512 dw_loc_list_1 (tree loc, rtx varloc, int want_address,
14513                enum var_init_status initialized)
14514 {
14515   int have_address = 0;
14516   dw_loc_descr_ref descr;
14517   enum machine_mode mode;
14518
14519   if (want_address != 2)
14520     {
14521       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
14522       /* Single part.  */
14523       if (GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14524         {
14525           varloc = PAT_VAR_LOCATION_LOC (varloc);
14526           if (GET_CODE (varloc) == EXPR_LIST)
14527             varloc = XEXP (varloc, 0);
14528           mode = GET_MODE (varloc);
14529           if (MEM_P (varloc))
14530             {
14531               rtx addr = XEXP (varloc, 0);
14532               descr = mem_loc_descriptor (addr, mode, initialized);
14533               if (descr)
14534                 have_address = 1;
14535               else
14536                 {
14537                   rtx x = avoid_constant_pool_reference (varloc);
14538                   if (x != varloc)
14539                     descr = mem_loc_descriptor (x, mode, initialized);
14540                 }
14541             }
14542           else
14543             descr = mem_loc_descriptor (varloc, mode, initialized);
14544         }
14545       else
14546         return 0;
14547     }
14548   else
14549     {
14550       if (GET_CODE (varloc) == VAR_LOCATION)
14551         mode = DECL_MODE (PAT_VAR_LOCATION_DECL (varloc));
14552       else
14553         mode = DECL_MODE (loc);
14554       descr = loc_descriptor (varloc, mode, initialized);
14555       have_address = 1;
14556     }
14557
14558   if (!descr)
14559     return 0;
14560
14561   if (want_address == 2 && !have_address
14562       && (dwarf_version >= 4 || !dwarf_strict))
14563     {
14564       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
14565         {
14566           expansion_failed (loc, NULL_RTX,
14567                             "DWARF address size mismatch");
14568           return 0;
14569         }
14570       add_loc_descr (&descr, new_loc_descr (DW_OP_stack_value, 0, 0));
14571       have_address = 1;
14572     }
14573   /* Show if we can't fill the request for an address.  */
14574   if (want_address && !have_address)
14575     {
14576       expansion_failed (loc, NULL_RTX,
14577                         "Want address and only have value");
14578       return 0;
14579     }
14580
14581   /* If we've got an address and don't want one, dereference.  */
14582   if (!want_address && have_address)
14583     {
14584       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
14585       enum dwarf_location_atom op;
14586
14587       if (size > DWARF2_ADDR_SIZE || size == -1)
14588         {
14589           expansion_failed (loc, NULL_RTX,
14590                             "DWARF address size mismatch");
14591           return 0;
14592         }
14593       else if (size == DWARF2_ADDR_SIZE)
14594         op = DW_OP_deref;
14595       else
14596         op = DW_OP_deref_size;
14597
14598       add_loc_descr (&descr, new_loc_descr (op, size, 0));
14599     }
14600
14601   return descr;
14602 }
14603
14604 /* Create a DW_OP_piece or DW_OP_bit_piece for bitsize, or return NULL
14605    if it is not possible.  */
14606
14607 static dw_loc_descr_ref
14608 new_loc_descr_op_bit_piece (HOST_WIDE_INT bitsize, HOST_WIDE_INT offset)
14609 {
14610   if ((bitsize % BITS_PER_UNIT) == 0 && offset == 0)
14611     return new_loc_descr (DW_OP_piece, bitsize / BITS_PER_UNIT, 0);
14612   else if (dwarf_version >= 3 || !dwarf_strict)
14613     return new_loc_descr (DW_OP_bit_piece, bitsize, offset);
14614   else
14615     return NULL;
14616 }
14617
14618 /* Helper function for dw_loc_list.  Compute proper Dwarf location descriptor
14619    for VAR_LOC_NOTE for variable DECL that has been optimized by SRA.  */
14620
14621 static dw_loc_descr_ref
14622 dw_sra_loc_expr (tree decl, rtx loc)
14623 {
14624   rtx p;
14625   unsigned int padsize = 0;
14626   dw_loc_descr_ref descr, *descr_tail;
14627   unsigned HOST_WIDE_INT decl_size;
14628   rtx varloc;
14629   enum var_init_status initialized;
14630
14631   if (DECL_SIZE (decl) == NULL
14632       || !host_integerp (DECL_SIZE (decl), 1))
14633     return NULL;
14634
14635   decl_size = tree_low_cst (DECL_SIZE (decl), 1);
14636   descr = NULL;
14637   descr_tail = &descr;
14638
14639   for (p = loc; p; p = XEXP (p, 1))
14640     {
14641       unsigned int bitsize = decl_piece_bitsize (p);
14642       rtx loc_note = *decl_piece_varloc_ptr (p);
14643       dw_loc_descr_ref cur_descr;
14644       dw_loc_descr_ref *tail, last = NULL;
14645       unsigned int opsize = 0;
14646
14647       if (loc_note == NULL_RTX
14648           || NOTE_VAR_LOCATION_LOC (loc_note) == NULL_RTX)
14649         {
14650           padsize += bitsize;
14651           continue;
14652         }
14653       initialized = NOTE_VAR_LOCATION_STATUS (loc_note);
14654       varloc = NOTE_VAR_LOCATION (loc_note);
14655       cur_descr = dw_loc_list_1 (decl, varloc, 2, initialized);
14656       if (cur_descr == NULL)
14657         {
14658           padsize += bitsize;
14659           continue;
14660         }
14661
14662       /* Check that cur_descr either doesn't use
14663          DW_OP_*piece operations, or their sum is equal
14664          to bitsize.  Otherwise we can't embed it.  */
14665       for (tail = &cur_descr; *tail != NULL;
14666            tail = &(*tail)->dw_loc_next)
14667         if ((*tail)->dw_loc_opc == DW_OP_piece)
14668           {
14669             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned
14670                       * BITS_PER_UNIT;
14671             last = *tail;
14672           }
14673         else if ((*tail)->dw_loc_opc == DW_OP_bit_piece)
14674           {
14675             opsize += (*tail)->dw_loc_oprnd1.v.val_unsigned;
14676             last = *tail;
14677           }
14678
14679       if (last != NULL && opsize != bitsize)
14680         {
14681           padsize += bitsize;
14682           continue;
14683         }
14684
14685       /* If there is a hole, add DW_OP_*piece after empty DWARF
14686          expression, which means that those bits are optimized out.  */
14687       if (padsize)
14688         {
14689           if (padsize > decl_size)
14690             return NULL;
14691           decl_size -= padsize;
14692           *descr_tail = new_loc_descr_op_bit_piece (padsize, 0);
14693           if (*descr_tail == NULL)
14694             return NULL;
14695           descr_tail = &(*descr_tail)->dw_loc_next;
14696           padsize = 0;
14697         }
14698       *descr_tail = cur_descr;
14699       descr_tail = tail;
14700       if (bitsize > decl_size)
14701         return NULL;
14702       decl_size -= bitsize;
14703       if (last == NULL)
14704         {
14705           HOST_WIDE_INT offset = 0;
14706           if (GET_CODE (varloc) == VAR_LOCATION
14707               && GET_CODE (PAT_VAR_LOCATION_LOC (varloc)) != PARALLEL)
14708             {
14709               varloc = PAT_VAR_LOCATION_LOC (varloc);
14710               if (GET_CODE (varloc) == EXPR_LIST)
14711                 varloc = XEXP (varloc, 0);
14712             }
14713           do 
14714             {
14715               if (GET_CODE (varloc) == CONST
14716                   || GET_CODE (varloc) == SIGN_EXTEND
14717                   || GET_CODE (varloc) == ZERO_EXTEND)
14718                 varloc = XEXP (varloc, 0);
14719               else if (GET_CODE (varloc) == SUBREG)
14720                 varloc = SUBREG_REG (varloc);
14721               else
14722                 break;
14723             }
14724           while (1);
14725           /* DW_OP_bit_size offset should be zero for register
14726              or implicit location descriptions and empty location
14727              descriptions, but for memory addresses needs big endian
14728              adjustment.  */
14729           if (MEM_P (varloc))
14730             {
14731               unsigned HOST_WIDE_INT memsize
14732                 = INTVAL (MEM_SIZE (varloc)) * BITS_PER_UNIT;
14733               if (memsize != bitsize)
14734                 {
14735                   if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN
14736                       && (memsize > BITS_PER_WORD || bitsize > BITS_PER_WORD))
14737                     return NULL;
14738                   if (memsize < bitsize)
14739                     return NULL;
14740                   if (BITS_BIG_ENDIAN)
14741                     offset = memsize - bitsize;
14742                 }
14743             }
14744
14745           *descr_tail = new_loc_descr_op_bit_piece (bitsize, offset);
14746           if (*descr_tail == NULL)
14747             return NULL;
14748           descr_tail = &(*descr_tail)->dw_loc_next;
14749         }
14750     }
14751
14752   /* If there were any non-empty expressions, add padding till the end of
14753      the decl.  */
14754   if (descr != NULL && decl_size != 0)
14755     {
14756       *descr_tail = new_loc_descr_op_bit_piece (decl_size, 0);
14757       if (*descr_tail == NULL)
14758         return NULL;
14759     }
14760   return descr;
14761 }
14762
14763 /* Return the dwarf representation of the location list LOC_LIST of
14764    DECL.  WANT_ADDRESS has the same meaning as in loc_list_from_tree
14765    function.  */
14766
14767 static dw_loc_list_ref
14768 dw_loc_list (var_loc_list *loc_list, tree decl, int want_address)
14769 {
14770   const char *endname, *secname;
14771   rtx varloc;
14772   enum var_init_status initialized;
14773   struct var_loc_node *node;
14774   dw_loc_descr_ref descr;
14775   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
14776   dw_loc_list_ref list = NULL;
14777   dw_loc_list_ref *listp = &list;
14778
14779   /* Now that we know what section we are using for a base,
14780      actually construct the list of locations.
14781      The first location information is what is passed to the
14782      function that creates the location list, and the remaining
14783      locations just get added on to that list.
14784      Note that we only know the start address for a location
14785      (IE location changes), so to build the range, we use
14786      the range [current location start, next location start].
14787      This means we have to special case the last node, and generate
14788      a range of [last location start, end of function label].  */
14789
14790   secname = secname_for_decl (decl);
14791
14792   for (node = loc_list->first; node; node = node->next)
14793     if (GET_CODE (node->loc) == EXPR_LIST
14794         || NOTE_VAR_LOCATION_LOC (node->loc) != NULL_RTX)
14795       {
14796         if (GET_CODE (node->loc) == EXPR_LIST)
14797           {
14798             /* This requires DW_OP_{,bit_}piece, which is not usable
14799                inside DWARF expressions.  */
14800             if (want_address != 2)
14801               continue;
14802             descr = dw_sra_loc_expr (decl, node->loc);
14803             if (descr == NULL)
14804               continue;
14805           }
14806         else
14807           {
14808             initialized = NOTE_VAR_LOCATION_STATUS (node->loc);
14809             varloc = NOTE_VAR_LOCATION (node->loc);
14810             descr = dw_loc_list_1 (decl, varloc, want_address, initialized);
14811           }
14812         if (descr)
14813           {
14814             /* The variable has a location between NODE->LABEL and
14815                NODE->NEXT->LABEL.  */
14816             if (node->next)
14817               endname = node->next->label;
14818             /* If the variable has a location at the last label
14819                it keeps its location until the end of function.  */
14820             else if (!current_function_decl)
14821               endname = text_end_label;
14822             else
14823               {
14824                 ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
14825                                              current_function_funcdef_no);
14826                 endname = ggc_strdup (label_id);
14827               }
14828
14829             *listp = new_loc_list (descr, node->label, endname, secname);
14830             listp = &(*listp)->dw_loc_next;
14831           }
14832       }
14833
14834   /* Try to avoid the overhead of a location list emitting a location
14835      expression instead, but only if we didn't have more than one
14836      location entry in the first place.  If some entries were not
14837      representable, we don't want to pretend a single entry that was
14838      applies to the entire scope in which the variable is
14839      available.  */
14840   if (list && loc_list->first->next)
14841     gen_llsym (list);
14842
14843   return list;
14844 }
14845
14846 /* Return if the loc_list has only single element and thus can be represented
14847    as location description.   */
14848
14849 static bool
14850 single_element_loc_list_p (dw_loc_list_ref list)
14851 {
14852   gcc_assert (!list->dw_loc_next || list->ll_symbol);
14853   return !list->ll_symbol;
14854 }
14855
14856 /* To each location in list LIST add loc descr REF.  */
14857
14858 static void
14859 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
14860 {
14861   dw_loc_descr_ref copy;
14862   add_loc_descr (&list->expr, ref);
14863   list = list->dw_loc_next;
14864   while (list)
14865     {
14866       copy = ggc_alloc_dw_loc_descr_node ();
14867       memcpy (copy, ref, sizeof (dw_loc_descr_node));
14868       add_loc_descr (&list->expr, copy);
14869       while (copy->dw_loc_next)
14870         {
14871           dw_loc_descr_ref new_copy = ggc_alloc_dw_loc_descr_node ();
14872           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
14873           copy->dw_loc_next = new_copy;
14874           copy = new_copy;
14875         }
14876       list = list->dw_loc_next;
14877     }
14878 }
14879
14880 /* Given two lists RET and LIST
14881    produce location list that is result of adding expression in LIST
14882    to expression in RET on each possition in program.
14883    Might be destructive on both RET and LIST.
14884
14885    TODO: We handle only simple cases of RET or LIST having at most one
14886    element. General case would inolve sorting the lists in program order
14887    and merging them that will need some additional work.
14888    Adding that will improve quality of debug info especially for SRA-ed
14889    structures.  */
14890
14891 static void
14892 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
14893 {
14894   if (!list)
14895     return;
14896   if (!*ret)
14897     {
14898       *ret = list;
14899       return;
14900     }
14901   if (!list->dw_loc_next)
14902     {
14903       add_loc_descr_to_each (*ret, list->expr);
14904       return;
14905     }
14906   if (!(*ret)->dw_loc_next)
14907     {
14908       add_loc_descr_to_each (list, (*ret)->expr);
14909       *ret = list;
14910       return;
14911     }
14912   expansion_failed (NULL_TREE, NULL_RTX,
14913                     "Don't know how to merge two non-trivial"
14914                     " location lists.\n");
14915   *ret = NULL;
14916   return;
14917 }
14918
14919 /* LOC is constant expression.  Try a luck, look it up in constant
14920    pool and return its loc_descr of its address.  */
14921
14922 static dw_loc_descr_ref
14923 cst_pool_loc_descr (tree loc)
14924 {
14925   /* Get an RTL for this, if something has been emitted.  */
14926   rtx rtl = lookup_constant_def (loc);
14927   enum machine_mode mode;
14928
14929   if (!rtl || !MEM_P (rtl))
14930     {
14931       gcc_assert (!rtl);
14932       return 0;
14933     }
14934   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
14935
14936   /* TODO: We might get more coverage if we was actually delaying expansion
14937      of all expressions till end of compilation when constant pools are fully
14938      populated.  */
14939   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
14940     {
14941       expansion_failed (loc, NULL_RTX,
14942                         "CST value in contant pool but not marked.");
14943       return 0;
14944     }
14945   mode = GET_MODE (rtl);
14946   rtl = XEXP (rtl, 0);
14947   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
14948 }
14949
14950 /* Return dw_loc_list representing address of addr_expr LOC
14951    by looking for innder INDIRECT_REF expression and turing it
14952    into simple arithmetics.  */
14953
14954 static dw_loc_list_ref
14955 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
14956 {
14957   tree obj, offset;
14958   HOST_WIDE_INT bitsize, bitpos, bytepos;
14959   enum machine_mode mode;
14960   int volatilep;
14961   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
14962   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
14963
14964   obj = get_inner_reference (TREE_OPERAND (loc, 0),
14965                              &bitsize, &bitpos, &offset, &mode,
14966                              &unsignedp, &volatilep, false);
14967   STRIP_NOPS (obj);
14968   if (bitpos % BITS_PER_UNIT)
14969     {
14970       expansion_failed (loc, NULL_RTX, "bitfield access");
14971       return 0;
14972     }
14973   if (!INDIRECT_REF_P (obj))
14974     {
14975       expansion_failed (obj,
14976                         NULL_RTX, "no indirect ref in inner refrence");
14977       return 0;
14978     }
14979   if (!offset && !bitpos)
14980     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
14981   else if (toplev
14982            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
14983            && (dwarf_version >= 4 || !dwarf_strict))
14984     {
14985       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
14986       if (!list_ret)
14987         return 0;
14988       if (offset)
14989         {
14990           /* Variable offset.  */
14991           list_ret1 = loc_list_from_tree (offset, 0);
14992           if (list_ret1 == 0)
14993             return 0;
14994           add_loc_list (&list_ret, list_ret1);
14995           if (!list_ret)
14996             return 0;
14997           add_loc_descr_to_each (list_ret,
14998                                  new_loc_descr (DW_OP_plus, 0, 0));
14999         }
15000       bytepos = bitpos / BITS_PER_UNIT;
15001       if (bytepos > 0)
15002         add_loc_descr_to_each (list_ret,
15003                                new_loc_descr (DW_OP_plus_uconst,
15004                                               bytepos, 0));
15005       else if (bytepos < 0)
15006         loc_list_plus_const (list_ret, bytepos);
15007       add_loc_descr_to_each (list_ret,
15008                              new_loc_descr (DW_OP_stack_value, 0, 0));
15009     }
15010   return list_ret;
15011 }
15012
15013
15014 /* Generate Dwarf location list representing LOC.
15015    If WANT_ADDRESS is false, expression computing LOC will be computed
15016    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
15017    if WANT_ADDRESS is 2, expression computing address useable in location
15018      will be returned (i.e. DW_OP_reg can be used
15019      to refer to register values).  */
15020
15021 static dw_loc_list_ref
15022 loc_list_from_tree (tree loc, int want_address)
15023 {
15024   dw_loc_descr_ref ret = NULL, ret1 = NULL;
15025   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
15026   int have_address = 0;
15027   enum dwarf_location_atom op;
15028
15029   /* ??? Most of the time we do not take proper care for sign/zero
15030      extending the values properly.  Hopefully this won't be a real
15031      problem...  */
15032
15033   switch (TREE_CODE (loc))
15034     {
15035     case ERROR_MARK:
15036       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
15037       return 0;
15038
15039     case PLACEHOLDER_EXPR:
15040       /* This case involves extracting fields from an object to determine the
15041          position of other fields.  We don't try to encode this here.  The
15042          only user of this is Ada, which encodes the needed information using
15043          the names of types.  */
15044       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
15045       return 0;
15046
15047     case CALL_EXPR:
15048       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
15049       /* There are no opcodes for these operations.  */
15050       return 0;
15051
15052     case PREINCREMENT_EXPR:
15053     case PREDECREMENT_EXPR:
15054     case POSTINCREMENT_EXPR:
15055     case POSTDECREMENT_EXPR:
15056       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
15057       /* There are no opcodes for these operations.  */
15058       return 0;
15059
15060     case ADDR_EXPR:
15061       /* If we already want an address, see if there is INDIRECT_REF inside
15062          e.g. for &this->field.  */
15063       if (want_address)
15064         {
15065           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
15066                        (loc, want_address == 2);
15067           if (list_ret)
15068             have_address = 1;
15069           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
15070                    && (ret = cst_pool_loc_descr (loc)))
15071             have_address = 1;
15072         }
15073         /* Otherwise, process the argument and look for the address.  */
15074       if (!list_ret && !ret)
15075         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
15076       else
15077         {
15078           if (want_address)
15079             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
15080           return NULL;
15081         }
15082       break;
15083
15084     case VAR_DECL:
15085       if (DECL_THREAD_LOCAL_P (loc))
15086         {
15087           rtx rtl;
15088           enum dwarf_location_atom first_op;
15089           enum dwarf_location_atom second_op;
15090           bool dtprel = false;
15091
15092           if (targetm.have_tls)
15093             {
15094               /* If this is not defined, we have no way to emit the
15095                  data.  */
15096               if (!targetm.asm_out.output_dwarf_dtprel)
15097                 return 0;
15098
15099                /* The way DW_OP_GNU_push_tls_address is specified, we
15100                   can only look up addresses of objects in the current
15101                   module.  We used DW_OP_addr as first op, but that's
15102                   wrong, because DW_OP_addr is relocated by the debug
15103                   info consumer, while DW_OP_GNU_push_tls_address
15104                   operand shouldn't be.  */
15105               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
15106                 return 0;
15107               first_op = DWARF2_ADDR_SIZE == 4 ? DW_OP_const4u : DW_OP_const8u;
15108               dtprel = true;
15109               second_op = DW_OP_GNU_push_tls_address;
15110             }
15111           else
15112             {
15113               if (!targetm.emutls.debug_form_tls_address
15114                   || !(dwarf_version >= 3 || !dwarf_strict))
15115                 return 0;
15116               /* We stuffed the control variable into the DECL_VALUE_EXPR
15117                  to signal (via DECL_HAS_VALUE_EXPR_P) that the decl should
15118                  no longer appear in gimple code.  We used the control
15119                  variable in specific so that we could pick it up here.  */
15120               loc = DECL_VALUE_EXPR (loc);
15121               first_op = DW_OP_addr;
15122               second_op = DW_OP_form_tls_address;
15123             }
15124
15125           rtl = rtl_for_decl_location (loc);
15126           if (rtl == NULL_RTX)
15127             return 0;
15128
15129           if (!MEM_P (rtl))
15130             return 0;
15131           rtl = XEXP (rtl, 0);
15132           if (! CONSTANT_P (rtl))
15133             return 0;
15134
15135           ret = new_loc_descr (first_op, 0, 0);
15136           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15137           ret->dw_loc_oprnd1.v.val_addr = rtl;
15138           ret->dtprel = dtprel;
15139
15140           ret1 = new_loc_descr (second_op, 0, 0);
15141           add_loc_descr (&ret, ret1);
15142
15143           have_address = 1;
15144           break;
15145         }
15146       /* FALLTHRU */
15147
15148     case PARM_DECL:
15149       if (DECL_HAS_VALUE_EXPR_P (loc))
15150         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
15151                                    want_address);
15152       /* FALLTHRU */
15153
15154     case RESULT_DECL:
15155     case FUNCTION_DECL:
15156       {
15157         rtx rtl;
15158         var_loc_list *loc_list = lookup_decl_loc (loc);
15159
15160         if (loc_list && loc_list->first)
15161           {
15162             list_ret = dw_loc_list (loc_list, loc, want_address);
15163             have_address = want_address != 0;
15164             break;
15165           }
15166         rtl = rtl_for_decl_location (loc);
15167         if (rtl == NULL_RTX)
15168           {
15169             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
15170             return 0;
15171           }
15172         else if (CONST_INT_P (rtl))
15173           {
15174             HOST_WIDE_INT val = INTVAL (rtl);
15175             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15176               val &= GET_MODE_MASK (DECL_MODE (loc));
15177             ret = int_loc_descriptor (val);
15178           }
15179         else if (GET_CODE (rtl) == CONST_STRING)
15180           {
15181             expansion_failed (loc, NULL_RTX, "CONST_STRING");
15182             return 0;
15183           }
15184         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
15185           {
15186             ret = new_loc_descr (DW_OP_addr, 0, 0);
15187             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
15188             ret->dw_loc_oprnd1.v.val_addr = rtl;
15189           }
15190         else
15191           {
15192             enum machine_mode mode;
15193
15194             /* Certain constructs can only be represented at top-level.  */
15195             if (want_address == 2)
15196               {
15197                 ret = loc_descriptor (rtl, VOIDmode,
15198                                       VAR_INIT_STATUS_INITIALIZED);
15199                 have_address = 1;
15200               }
15201             else
15202               {
15203                 mode = GET_MODE (rtl);
15204                 if (MEM_P (rtl))
15205                   {
15206                     rtl = XEXP (rtl, 0);
15207                     have_address = 1;
15208                   }
15209                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
15210               }
15211             if (!ret)
15212               expansion_failed (loc, rtl,
15213                                 "failed to produce loc descriptor for rtl");
15214           }
15215       }
15216       break;
15217
15218     case MEM_REF:
15219       /* ??? FIXME.  */
15220       if (!integer_zerop (TREE_OPERAND (loc, 1)))
15221         return 0;
15222       /* Fallthru.  */
15223     case INDIRECT_REF:
15224     case MISALIGNED_INDIRECT_REF:
15225       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15226       have_address = 1;
15227       break;
15228
15229     case COMPOUND_EXPR:
15230       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
15231
15232     CASE_CONVERT:
15233     case VIEW_CONVERT_EXPR:
15234     case SAVE_EXPR:
15235     case MODIFY_EXPR:
15236       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
15237
15238     case COMPONENT_REF:
15239     case BIT_FIELD_REF:
15240     case ARRAY_REF:
15241     case ARRAY_RANGE_REF:
15242     case REALPART_EXPR:
15243     case IMAGPART_EXPR:
15244       {
15245         tree obj, offset;
15246         HOST_WIDE_INT bitsize, bitpos, bytepos;
15247         enum machine_mode mode;
15248         int volatilep;
15249         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
15250
15251         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
15252                                    &unsignedp, &volatilep, false);
15253
15254         gcc_assert (obj != loc);
15255
15256         list_ret = loc_list_from_tree (obj,
15257                                        want_address == 2
15258                                        && !bitpos && !offset ? 2 : 1);
15259         /* TODO: We can extract value of the small expression via shifting even
15260            for nonzero bitpos.  */
15261         if (list_ret == 0)
15262           return 0;
15263         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
15264           {
15265             expansion_failed (loc, NULL_RTX,
15266                               "bitfield access");
15267             return 0;
15268           }
15269
15270         if (offset != NULL_TREE)
15271           {
15272             /* Variable offset.  */
15273             list_ret1 = loc_list_from_tree (offset, 0);
15274             if (list_ret1 == 0)
15275               return 0;
15276             add_loc_list (&list_ret, list_ret1);
15277             if (!list_ret)
15278               return 0;
15279             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
15280           }
15281
15282         bytepos = bitpos / BITS_PER_UNIT;
15283         if (bytepos > 0)
15284           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
15285         else if (bytepos < 0)
15286           loc_list_plus_const (list_ret, bytepos);
15287
15288         have_address = 1;
15289         break;
15290       }
15291
15292     case INTEGER_CST:
15293       if ((want_address || !host_integerp (loc, 0))
15294           && (ret = cst_pool_loc_descr (loc)))
15295         have_address = 1;
15296       else if (want_address == 2
15297                && host_integerp (loc, 0)
15298                && (ret = address_of_int_loc_descriptor
15299                            (int_size_in_bytes (TREE_TYPE (loc)),
15300                             tree_low_cst (loc, 0))))
15301         have_address = 1;
15302       else if (host_integerp (loc, 0))
15303         ret = int_loc_descriptor (tree_low_cst (loc, 0));
15304       else
15305         {
15306           expansion_failed (loc, NULL_RTX,
15307                             "Integer operand is not host integer");
15308           return 0;
15309         }
15310       break;
15311
15312     case CONSTRUCTOR:
15313     case REAL_CST:
15314     case STRING_CST:
15315     case COMPLEX_CST:
15316       if ((ret = cst_pool_loc_descr (loc)))
15317         have_address = 1;
15318       else
15319       /* We can construct small constants here using int_loc_descriptor.  */
15320         expansion_failed (loc, NULL_RTX,
15321                           "constructor or constant not in constant pool");
15322       break;
15323
15324     case TRUTH_AND_EXPR:
15325     case TRUTH_ANDIF_EXPR:
15326     case BIT_AND_EXPR:
15327       op = DW_OP_and;
15328       goto do_binop;
15329
15330     case TRUTH_XOR_EXPR:
15331     case BIT_XOR_EXPR:
15332       op = DW_OP_xor;
15333       goto do_binop;
15334
15335     case TRUTH_OR_EXPR:
15336     case TRUTH_ORIF_EXPR:
15337     case BIT_IOR_EXPR:
15338       op = DW_OP_or;
15339       goto do_binop;
15340
15341     case FLOOR_DIV_EXPR:
15342     case CEIL_DIV_EXPR:
15343     case ROUND_DIV_EXPR:
15344     case TRUNC_DIV_EXPR:
15345       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15346         return 0;
15347       op = DW_OP_div;
15348       goto do_binop;
15349
15350     case MINUS_EXPR:
15351       op = DW_OP_minus;
15352       goto do_binop;
15353
15354     case FLOOR_MOD_EXPR:
15355     case CEIL_MOD_EXPR:
15356     case ROUND_MOD_EXPR:
15357     case TRUNC_MOD_EXPR:
15358       if (TYPE_UNSIGNED (TREE_TYPE (loc)))
15359         {
15360           op = DW_OP_mod;
15361           goto do_binop;
15362         }
15363       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15364       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15365       if (list_ret == 0 || list_ret1 == 0)
15366         return 0;
15367
15368       add_loc_list (&list_ret, list_ret1);
15369       if (list_ret == 0)
15370         return 0;
15371       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15372       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_over, 0, 0));
15373       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_div, 0, 0));
15374       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_mul, 0, 0));
15375       add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_minus, 0, 0));
15376       break;
15377
15378     case MULT_EXPR:
15379       op = DW_OP_mul;
15380       goto do_binop;
15381
15382     case LSHIFT_EXPR:
15383       op = DW_OP_shl;
15384       goto do_binop;
15385
15386     case RSHIFT_EXPR:
15387       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
15388       goto do_binop;
15389
15390     case POINTER_PLUS_EXPR:
15391     case PLUS_EXPR:
15392       if (host_integerp (TREE_OPERAND (loc, 1), 0))
15393         {
15394           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15395           if (list_ret == 0)
15396             return 0;
15397
15398           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
15399           break;
15400         }
15401
15402       op = DW_OP_plus;
15403       goto do_binop;
15404
15405     case LE_EXPR:
15406       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15407         return 0;
15408
15409       op = DW_OP_le;
15410       goto do_binop;
15411
15412     case GE_EXPR:
15413       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15414         return 0;
15415
15416       op = DW_OP_ge;
15417       goto do_binop;
15418
15419     case LT_EXPR:
15420       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15421         return 0;
15422
15423       op = DW_OP_lt;
15424       goto do_binop;
15425
15426     case GT_EXPR:
15427       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
15428         return 0;
15429
15430       op = DW_OP_gt;
15431       goto do_binop;
15432
15433     case EQ_EXPR:
15434       op = DW_OP_eq;
15435       goto do_binop;
15436
15437     case NE_EXPR:
15438       op = DW_OP_ne;
15439       goto do_binop;
15440
15441     do_binop:
15442       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15443       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
15444       if (list_ret == 0 || list_ret1 == 0)
15445         return 0;
15446
15447       add_loc_list (&list_ret, list_ret1);
15448       if (list_ret == 0)
15449         return 0;
15450       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15451       break;
15452
15453     case TRUTH_NOT_EXPR:
15454     case BIT_NOT_EXPR:
15455       op = DW_OP_not;
15456       goto do_unop;
15457
15458     case ABS_EXPR:
15459       op = DW_OP_abs;
15460       goto do_unop;
15461
15462     case NEGATE_EXPR:
15463       op = DW_OP_neg;
15464       goto do_unop;
15465
15466     do_unop:
15467       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15468       if (list_ret == 0)
15469         return 0;
15470
15471       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
15472       break;
15473
15474     case MIN_EXPR:
15475     case MAX_EXPR:
15476       {
15477         const enum tree_code code =
15478           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
15479
15480         loc = build3 (COND_EXPR, TREE_TYPE (loc),
15481                       build2 (code, integer_type_node,
15482                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
15483                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
15484       }
15485
15486       /* ... fall through ...  */
15487
15488     case COND_EXPR:
15489       {
15490         dw_loc_descr_ref lhs
15491           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
15492         dw_loc_list_ref rhs
15493           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
15494         dw_loc_descr_ref bra_node, jump_node, tmp;
15495
15496         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
15497         if (list_ret == 0 || lhs == 0 || rhs == 0)
15498           return 0;
15499
15500         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
15501         add_loc_descr_to_each (list_ret, bra_node);
15502
15503         add_loc_list (&list_ret, rhs);
15504         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
15505         add_loc_descr_to_each (list_ret, jump_node);
15506
15507         add_loc_descr_to_each (list_ret, lhs);
15508         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15509         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
15510
15511         /* ??? Need a node to point the skip at.  Use a nop.  */
15512         tmp = new_loc_descr (DW_OP_nop, 0, 0);
15513         add_loc_descr_to_each (list_ret, tmp);
15514         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
15515         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
15516       }
15517       break;
15518
15519     case FIX_TRUNC_EXPR:
15520       return 0;
15521
15522     default:
15523       /* Leave front-end specific codes as simply unknown.  This comes
15524          up, for instance, with the C STMT_EXPR.  */
15525       if ((unsigned int) TREE_CODE (loc)
15526           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
15527         {
15528           expansion_failed (loc, NULL_RTX,
15529                             "language specific tree node");
15530           return 0;
15531         }
15532
15533 #ifdef ENABLE_CHECKING
15534       /* Otherwise this is a generic code; we should just lists all of
15535          these explicitly.  We forgot one.  */
15536       gcc_unreachable ();
15537 #else
15538       /* In a release build, we want to degrade gracefully: better to
15539          generate incomplete debugging information than to crash.  */
15540       return NULL;
15541 #endif
15542     }
15543
15544   if (!ret && !list_ret)
15545     return 0;
15546
15547   if (want_address == 2 && !have_address
15548       && (dwarf_version >= 4 || !dwarf_strict))
15549     {
15550       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
15551         {
15552           expansion_failed (loc, NULL_RTX,
15553                             "DWARF address size mismatch");
15554           return 0;
15555         }
15556       if (ret)
15557         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
15558       else
15559         add_loc_descr_to_each (list_ret,
15560                                new_loc_descr (DW_OP_stack_value, 0, 0));
15561       have_address = 1;
15562     }
15563   /* Show if we can't fill the request for an address.  */
15564   if (want_address && !have_address)
15565     {
15566       expansion_failed (loc, NULL_RTX,
15567                         "Want address and only have value");
15568       return 0;
15569     }
15570
15571   gcc_assert (!ret || !list_ret);
15572
15573   /* If we've got an address and don't want one, dereference.  */
15574   if (!want_address && have_address)
15575     {
15576       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
15577
15578       if (size > DWARF2_ADDR_SIZE || size == -1)
15579         {
15580           expansion_failed (loc, NULL_RTX,
15581                             "DWARF address size mismatch");
15582           return 0;
15583         }
15584       else if (size == DWARF2_ADDR_SIZE)
15585         op = DW_OP_deref;
15586       else
15587         op = DW_OP_deref_size;
15588
15589       if (ret)
15590         add_loc_descr (&ret, new_loc_descr (op, size, 0));
15591       else
15592         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
15593     }
15594   if (ret)
15595     list_ret = new_loc_list (ret, NULL, NULL, NULL);
15596
15597   return list_ret;
15598 }
15599
15600 /* Same as above but return only single location expression.  */
15601 static dw_loc_descr_ref
15602 loc_descriptor_from_tree (tree loc, int want_address)
15603 {
15604   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
15605   if (!ret)
15606     return NULL;
15607   if (ret->dw_loc_next)
15608     {
15609       expansion_failed (loc, NULL_RTX,
15610                         "Location list where only loc descriptor needed");
15611       return NULL;
15612     }
15613   return ret->expr;
15614 }
15615
15616 /* Given a value, round it up to the lowest multiple of `boundary'
15617    which is not less than the value itself.  */
15618
15619 static inline HOST_WIDE_INT
15620 ceiling (HOST_WIDE_INT value, unsigned int boundary)
15621 {
15622   return (((value + boundary - 1) / boundary) * boundary);
15623 }
15624
15625 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
15626    pointer to the declared type for the relevant field variable, or return
15627    `integer_type_node' if the given node turns out to be an
15628    ERROR_MARK node.  */
15629
15630 static inline tree
15631 field_type (const_tree decl)
15632 {
15633   tree type;
15634
15635   if (TREE_CODE (decl) == ERROR_MARK)
15636     return integer_type_node;
15637
15638   type = DECL_BIT_FIELD_TYPE (decl);
15639   if (type == NULL_TREE)
15640     type = TREE_TYPE (decl);
15641
15642   return type;
15643 }
15644
15645 /* Given a pointer to a tree node, return the alignment in bits for
15646    it, or else return BITS_PER_WORD if the node actually turns out to
15647    be an ERROR_MARK node.  */
15648
15649 static inline unsigned
15650 simple_type_align_in_bits (const_tree type)
15651 {
15652   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
15653 }
15654
15655 static inline unsigned
15656 simple_decl_align_in_bits (const_tree decl)
15657 {
15658   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
15659 }
15660
15661 /* Return the result of rounding T up to ALIGN.  */
15662
15663 static inline double_int
15664 round_up_to_align (double_int t, unsigned int align)
15665 {
15666   double_int alignd = uhwi_to_double_int (align);
15667   t = double_int_add (t, alignd);
15668   t = double_int_add (t, double_int_minus_one);
15669   t = double_int_div (t, alignd, true, TRUNC_DIV_EXPR);
15670   t = double_int_mul (t, alignd);
15671   return t;
15672 }
15673
15674 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
15675    lowest addressed byte of the "containing object" for the given FIELD_DECL,
15676    or return 0 if we are unable to determine what that offset is, either
15677    because the argument turns out to be a pointer to an ERROR_MARK node, or
15678    because the offset is actually variable.  (We can't handle the latter case
15679    just yet).  */
15680
15681 static HOST_WIDE_INT
15682 field_byte_offset (const_tree decl)
15683 {
15684   double_int object_offset_in_bits;
15685   double_int object_offset_in_bytes;
15686   double_int bitpos_int;
15687
15688   if (TREE_CODE (decl) == ERROR_MARK)
15689     return 0;
15690
15691   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
15692
15693   /* We cannot yet cope with fields whose positions are variable, so
15694      for now, when we see such things, we simply return 0.  Someday, we may
15695      be able to handle such cases, but it will be damn difficult.  */
15696   if (TREE_CODE (bit_position (decl)) != INTEGER_CST)
15697     return 0;
15698
15699   bitpos_int = tree_to_double_int (bit_position (decl));
15700
15701 #ifdef PCC_BITFIELD_TYPE_MATTERS
15702   if (PCC_BITFIELD_TYPE_MATTERS)
15703     {
15704       tree type;
15705       tree field_size_tree;
15706       double_int deepest_bitpos;
15707       double_int field_size_in_bits;
15708       unsigned int type_align_in_bits;
15709       unsigned int decl_align_in_bits;
15710       double_int type_size_in_bits;
15711
15712       type = field_type (decl);
15713       type_size_in_bits = double_int_type_size_in_bits (type);
15714       type_align_in_bits = simple_type_align_in_bits (type);
15715
15716       field_size_tree = DECL_SIZE (decl);
15717
15718       /* The size could be unspecified if there was an error, or for
15719          a flexible array member.  */
15720       if (!field_size_tree)
15721         field_size_tree = bitsize_zero_node;
15722
15723       /* If the size of the field is not constant, use the type size.  */
15724       if (TREE_CODE (field_size_tree) == INTEGER_CST)
15725         field_size_in_bits = tree_to_double_int (field_size_tree);
15726       else
15727         field_size_in_bits = type_size_in_bits;
15728
15729       decl_align_in_bits = simple_decl_align_in_bits (decl);
15730
15731       /* The GCC front-end doesn't make any attempt to keep track of the
15732          starting bit offset (relative to the start of the containing
15733          structure type) of the hypothetical "containing object" for a
15734          bit-field.  Thus, when computing the byte offset value for the
15735          start of the "containing object" of a bit-field, we must deduce
15736          this information on our own. This can be rather tricky to do in
15737          some cases.  For example, handling the following structure type
15738          definition when compiling for an i386/i486 target (which only
15739          aligns long long's to 32-bit boundaries) can be very tricky:
15740
15741          struct S { int field1; long long field2:31; };
15742
15743          Fortunately, there is a simple rule-of-thumb which can be used
15744          in such cases.  When compiling for an i386/i486, GCC will
15745          allocate 8 bytes for the structure shown above.  It decides to
15746          do this based upon one simple rule for bit-field allocation.
15747          GCC allocates each "containing object" for each bit-field at
15748          the first (i.e. lowest addressed) legitimate alignment boundary
15749          (based upon the required minimum alignment for the declared
15750          type of the field) which it can possibly use, subject to the
15751          condition that there is still enough available space remaining
15752          in the containing object (when allocated at the selected point)
15753          to fully accommodate all of the bits of the bit-field itself.
15754
15755          This simple rule makes it obvious why GCC allocates 8 bytes for
15756          each object of the structure type shown above.  When looking
15757          for a place to allocate the "containing object" for `field2',
15758          the compiler simply tries to allocate a 64-bit "containing
15759          object" at each successive 32-bit boundary (starting at zero)
15760          until it finds a place to allocate that 64- bit field such that
15761          at least 31 contiguous (and previously unallocated) bits remain
15762          within that selected 64 bit field.  (As it turns out, for the
15763          example above, the compiler finds it is OK to allocate the
15764          "containing object" 64-bit field at bit-offset zero within the
15765          structure type.)
15766
15767          Here we attempt to work backwards from the limited set of facts
15768          we're given, and we try to deduce from those facts, where GCC
15769          must have believed that the containing object started (within
15770          the structure type). The value we deduce is then used (by the
15771          callers of this routine) to generate DW_AT_location and
15772          DW_AT_bit_offset attributes for fields (both bit-fields and, in
15773          the case of DW_AT_location, regular fields as well).  */
15774
15775       /* Figure out the bit-distance from the start of the structure to
15776          the "deepest" bit of the bit-field.  */
15777       deepest_bitpos = double_int_add (bitpos_int, field_size_in_bits);
15778
15779       /* This is the tricky part.  Use some fancy footwork to deduce
15780          where the lowest addressed bit of the containing object must
15781          be.  */
15782       object_offset_in_bits
15783         = double_int_sub (deepest_bitpos, type_size_in_bits);
15784
15785       /* Round up to type_align by default.  This works best for
15786          bitfields.  */
15787       object_offset_in_bits
15788         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
15789
15790       if (double_int_ucmp (object_offset_in_bits, bitpos_int) > 0)
15791         {
15792           object_offset_in_bits
15793             = double_int_sub (deepest_bitpos, type_size_in_bits);
15794
15795           /* Round up to decl_align instead.  */
15796           object_offset_in_bits
15797             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
15798         }
15799     }
15800   else
15801 #endif
15802     object_offset_in_bits = bitpos_int;
15803
15804   object_offset_in_bytes
15805     = double_int_div (object_offset_in_bits,
15806                       uhwi_to_double_int (BITS_PER_UNIT), true,
15807                       TRUNC_DIV_EXPR);
15808   return double_int_to_shwi (object_offset_in_bytes);
15809 }
15810 \f
15811 /* The following routines define various Dwarf attributes and any data
15812    associated with them.  */
15813
15814 /* Add a location description attribute value to a DIE.
15815
15816    This emits location attributes suitable for whole variables and
15817    whole parameters.  Note that the location attributes for struct fields are
15818    generated by the routine `data_member_location_attribute' below.  */
15819
15820 static inline void
15821 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
15822                              dw_loc_list_ref descr)
15823 {
15824   if (descr == 0)
15825     return;
15826   if (single_element_loc_list_p (descr))
15827     add_AT_loc (die, attr_kind, descr->expr);
15828   else
15829     add_AT_loc_list (die, attr_kind, descr);
15830 }
15831
15832 /* Add DW_AT_accessibility attribute to DIE if needed.  */
15833
15834 static void
15835 add_accessibility_attribute (dw_die_ref die, tree decl)
15836 {
15837   if (TREE_PROTECTED (decl))
15838     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
15839   else if (TREE_PRIVATE (decl))
15840     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_private);
15841 }
15842
15843 /* Attach the specialized form of location attribute used for data members of
15844    struct and union types.  In the special case of a FIELD_DECL node which
15845    represents a bit-field, the "offset" part of this special location
15846    descriptor must indicate the distance in bytes from the lowest-addressed
15847    byte of the containing struct or union type to the lowest-addressed byte of
15848    the "containing object" for the bit-field.  (See the `field_byte_offset'
15849    function above).
15850
15851    For any given bit-field, the "containing object" is a hypothetical object
15852    (of some integral or enum type) within which the given bit-field lives.  The
15853    type of this hypothetical "containing object" is always the same as the
15854    declared type of the individual bit-field itself (for GCC anyway... the
15855    DWARF spec doesn't actually mandate this).  Note that it is the size (in
15856    bytes) of the hypothetical "containing object" which will be given in the
15857    DW_AT_byte_size attribute for this bit-field.  (See the
15858    `byte_size_attribute' function below.)  It is also used when calculating the
15859    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
15860    function below.)  */
15861
15862 static void
15863 add_data_member_location_attribute (dw_die_ref die, tree decl)
15864 {
15865   HOST_WIDE_INT offset;
15866   dw_loc_descr_ref loc_descr = 0;
15867
15868   if (TREE_CODE (decl) == TREE_BINFO)
15869     {
15870       /* We're working on the TAG_inheritance for a base class.  */
15871       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
15872         {
15873           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
15874              aren't at a fixed offset from all (sub)objects of the same
15875              type.  We need to extract the appropriate offset from our
15876              vtable.  The following dwarf expression means
15877
15878                BaseAddr = ObAddr + *((*ObAddr) - Offset)
15879
15880              This is specific to the V3 ABI, of course.  */
15881
15882           dw_loc_descr_ref tmp;
15883
15884           /* Make a copy of the object address.  */
15885           tmp = new_loc_descr (DW_OP_dup, 0, 0);
15886           add_loc_descr (&loc_descr, tmp);
15887
15888           /* Extract the vtable address.  */
15889           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15890           add_loc_descr (&loc_descr, tmp);
15891
15892           /* Calculate the address of the offset.  */
15893           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
15894           gcc_assert (offset < 0);
15895
15896           tmp = int_loc_descriptor (-offset);
15897           add_loc_descr (&loc_descr, tmp);
15898           tmp = new_loc_descr (DW_OP_minus, 0, 0);
15899           add_loc_descr (&loc_descr, tmp);
15900
15901           /* Extract the offset.  */
15902           tmp = new_loc_descr (DW_OP_deref, 0, 0);
15903           add_loc_descr (&loc_descr, tmp);
15904
15905           /* Add it to the object address.  */
15906           tmp = new_loc_descr (DW_OP_plus, 0, 0);
15907           add_loc_descr (&loc_descr, tmp);
15908         }
15909       else
15910         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
15911     }
15912   else
15913     offset = field_byte_offset (decl);
15914
15915   if (! loc_descr)
15916     {
15917       if (dwarf_version > 2)
15918         {
15919           /* Don't need to output a location expression, just the constant. */
15920           if (offset < 0)
15921             add_AT_int (die, DW_AT_data_member_location, offset);
15922           else
15923             add_AT_unsigned (die, DW_AT_data_member_location, offset);
15924           return;
15925         }
15926       else
15927         {
15928           enum dwarf_location_atom op;
15929
15930           /* The DWARF2 standard says that we should assume that the structure
15931              address is already on the stack, so we can specify a structure
15932              field address by using DW_OP_plus_uconst.  */
15933
15934 #ifdef MIPS_DEBUGGING_INFO
15935           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
15936              operator correctly.  It works only if we leave the offset on the
15937              stack.  */
15938           op = DW_OP_constu;
15939 #else
15940           op = DW_OP_plus_uconst;
15941 #endif
15942
15943           loc_descr = new_loc_descr (op, offset, 0);
15944         }
15945     }
15946
15947   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
15948 }
15949
15950 /* Writes integer values to dw_vec_const array.  */
15951
15952 static void
15953 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
15954 {
15955   while (size != 0)
15956     {
15957       *dest++ = val & 0xff;
15958       val >>= 8;
15959       --size;
15960     }
15961 }
15962
15963 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
15964
15965 static HOST_WIDE_INT
15966 extract_int (const unsigned char *src, unsigned int size)
15967 {
15968   HOST_WIDE_INT val = 0;
15969
15970   src += size;
15971   while (size != 0)
15972     {
15973       val <<= 8;
15974       val |= *--src & 0xff;
15975       --size;
15976     }
15977   return val;
15978 }
15979
15980 /* Writes double_int values to dw_vec_const array.  */
15981
15982 static void
15983 insert_double (double_int val, unsigned char *dest)
15984 {
15985   unsigned char *p0 = dest;
15986   unsigned char *p1 = dest + sizeof (HOST_WIDE_INT);
15987
15988   if (WORDS_BIG_ENDIAN)
15989     {
15990       p0 = p1;
15991       p1 = dest;
15992     }
15993
15994   insert_int ((HOST_WIDE_INT) val.low, sizeof (HOST_WIDE_INT), p0);
15995   insert_int ((HOST_WIDE_INT) val.high, sizeof (HOST_WIDE_INT), p1);
15996 }
15997
15998 /* Writes floating point values to dw_vec_const array.  */
15999
16000 static void
16001 insert_float (const_rtx rtl, unsigned char *array)
16002 {
16003   REAL_VALUE_TYPE rv;
16004   long val[4];
16005   int i;
16006
16007   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
16008   real_to_target (val, &rv, GET_MODE (rtl));
16009
16010   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
16011   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
16012     {
16013       insert_int (val[i], 4, array);
16014       array += 4;
16015     }
16016 }
16017
16018 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
16019    does not have a "location" either in memory or in a register.  These
16020    things can arise in GNU C when a constant is passed as an actual parameter
16021    to an inlined function.  They can also arise in C++ where declared
16022    constants do not necessarily get memory "homes".  */
16023
16024 static bool
16025 add_const_value_attribute (dw_die_ref die, rtx rtl)
16026 {
16027   switch (GET_CODE (rtl))
16028     {
16029     case CONST_INT:
16030       {
16031         HOST_WIDE_INT val = INTVAL (rtl);
16032
16033         if (val < 0)
16034           add_AT_int (die, DW_AT_const_value, val);
16035         else
16036           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
16037       }
16038       return true;
16039
16040     case CONST_DOUBLE:
16041       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
16042          floating-point constant.  A CONST_DOUBLE is used whenever the
16043          constant requires more than one word in order to be adequately
16044          represented.  */
16045       {
16046         enum machine_mode mode = GET_MODE (rtl);
16047
16048         if (SCALAR_FLOAT_MODE_P (mode))
16049           {
16050             unsigned int length = GET_MODE_SIZE (mode);
16051             unsigned char *array = (unsigned char *) ggc_alloc_atomic (length);
16052
16053             insert_float (rtl, array);
16054             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
16055           }
16056         else
16057           add_AT_double (die, DW_AT_const_value,
16058                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
16059       }
16060       return true;
16061
16062     case CONST_VECTOR:
16063       {
16064         enum machine_mode mode = GET_MODE (rtl);
16065         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
16066         unsigned int length = CONST_VECTOR_NUNITS (rtl);
16067         unsigned char *array = (unsigned char *) ggc_alloc_atomic
16068           (length * elt_size);
16069         unsigned int i;
16070         unsigned char *p;
16071
16072         switch (GET_MODE_CLASS (mode))
16073           {
16074           case MODE_VECTOR_INT:
16075             for (i = 0, p = array; i < length; i++, p += elt_size)
16076               {
16077                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16078                 double_int val = rtx_to_double_int (elt);
16079
16080                 if (elt_size <= sizeof (HOST_WIDE_INT))
16081                   insert_int (double_int_to_shwi (val), elt_size, p);
16082                 else
16083                   {
16084                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
16085                     insert_double (val, p);
16086                   }
16087               }
16088             break;
16089
16090           case MODE_VECTOR_FLOAT:
16091             for (i = 0, p = array; i < length; i++, p += elt_size)
16092               {
16093                 rtx elt = CONST_VECTOR_ELT (rtl, i);
16094                 insert_float (elt, p);
16095               }
16096             break;
16097
16098           default:
16099             gcc_unreachable ();
16100           }
16101
16102         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
16103       }
16104       return true;
16105
16106     case CONST_STRING:
16107       if (dwarf_version >= 4 || !dwarf_strict)
16108         {
16109           dw_loc_descr_ref loc_result;
16110           resolve_one_addr (&rtl, NULL);
16111         rtl_addr:
16112           loc_result = new_loc_descr (DW_OP_addr, 0, 0);
16113           loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
16114           loc_result->dw_loc_oprnd1.v.val_addr = rtl;
16115           add_loc_descr (&loc_result, new_loc_descr (DW_OP_stack_value, 0, 0));
16116           add_AT_loc (die, DW_AT_location, loc_result);
16117           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
16118           return true;
16119         }
16120       return false;
16121
16122     case CONST:
16123       if (CONSTANT_P (XEXP (rtl, 0)))
16124         return add_const_value_attribute (die, XEXP (rtl, 0));
16125       /* FALLTHROUGH */
16126     case SYMBOL_REF:
16127       if (!const_ok_for_output (rtl))
16128         return false;
16129     case LABEL_REF:
16130       if (dwarf_version >= 4 || !dwarf_strict)
16131         goto rtl_addr;
16132       return false;
16133
16134     case PLUS:
16135       /* In cases where an inlined instance of an inline function is passed
16136          the address of an `auto' variable (which is local to the caller) we
16137          can get a situation where the DECL_RTL of the artificial local
16138          variable (for the inlining) which acts as a stand-in for the
16139          corresponding formal parameter (of the inline function) will look
16140          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
16141          exactly a compile-time constant expression, but it isn't the address
16142          of the (artificial) local variable either.  Rather, it represents the
16143          *value* which the artificial local variable always has during its
16144          lifetime.  We currently have no way to represent such quasi-constant
16145          values in Dwarf, so for now we just punt and generate nothing.  */
16146       return false;
16147
16148     case HIGH:
16149     case CONST_FIXED:
16150       return false;
16151
16152     case MEM:
16153       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
16154           && MEM_READONLY_P (rtl)
16155           && GET_MODE (rtl) == BLKmode)
16156         {
16157           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
16158           return true;
16159         }
16160       return false;
16161
16162     default:
16163       /* No other kinds of rtx should be possible here.  */
16164       gcc_unreachable ();
16165     }
16166   return false;
16167 }
16168
16169 /* Determine whether the evaluation of EXPR references any variables
16170    or functions which aren't otherwise used (and therefore may not be
16171    output).  */
16172 static tree
16173 reference_to_unused (tree * tp, int * walk_subtrees,
16174                      void * data ATTRIBUTE_UNUSED)
16175 {
16176   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
16177     *walk_subtrees = 0;
16178
16179   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
16180       && ! TREE_ASM_WRITTEN (*tp))
16181     return *tp;
16182   /* ???  The C++ FE emits debug information for using decls, so
16183      putting gcc_unreachable here falls over.  See PR31899.  For now
16184      be conservative.  */
16185   else if (!cgraph_global_info_ready
16186            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
16187     return *tp;
16188   else if (TREE_CODE (*tp) == VAR_DECL)
16189     {
16190       struct varpool_node *node = varpool_get_node (*tp);
16191       if (!node || !node->needed)
16192         return *tp;
16193     }
16194   else if (TREE_CODE (*tp) == FUNCTION_DECL
16195            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
16196     {
16197       /* The call graph machinery must have finished analyzing,
16198          optimizing and gimplifying the CU by now.
16199          So if *TP has no call graph node associated
16200          to it, it means *TP will not be emitted.  */
16201       if (!cgraph_get_node (*tp))
16202         return *tp;
16203     }
16204   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
16205     return *tp;
16206
16207   return NULL_TREE;
16208 }
16209
16210 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
16211    for use in a later add_const_value_attribute call.  */
16212
16213 static rtx
16214 rtl_for_decl_init (tree init, tree type)
16215 {
16216   rtx rtl = NULL_RTX;
16217
16218   /* If a variable is initialized with a string constant without embedded
16219      zeros, build CONST_STRING.  */
16220   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
16221     {
16222       tree enttype = TREE_TYPE (type);
16223       tree domain = TYPE_DOMAIN (type);
16224       enum machine_mode mode = TYPE_MODE (enttype);
16225
16226       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
16227           && domain
16228           && integer_zerop (TYPE_MIN_VALUE (domain))
16229           && compare_tree_int (TYPE_MAX_VALUE (domain),
16230                                TREE_STRING_LENGTH (init) - 1) == 0
16231           && ((size_t) TREE_STRING_LENGTH (init)
16232               == strlen (TREE_STRING_POINTER (init)) + 1))
16233         {
16234           rtl = gen_rtx_CONST_STRING (VOIDmode,
16235                                       ggc_strdup (TREE_STRING_POINTER (init)));
16236           rtl = gen_rtx_MEM (BLKmode, rtl);
16237           MEM_READONLY_P (rtl) = 1;
16238         }
16239     }
16240   /* Other aggregates, and complex values, could be represented using
16241      CONCAT: FIXME!  */
16242   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
16243     ;
16244   /* Vectors only work if their mode is supported by the target.
16245      FIXME: generic vectors ought to work too.  */
16246   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
16247     ;
16248   /* If the initializer is something that we know will expand into an
16249      immediate RTL constant, expand it now.  We must be careful not to
16250      reference variables which won't be output.  */
16251   else if (initializer_constant_valid_p (init, type)
16252            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
16253     {
16254       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
16255          possible.  */
16256       if (TREE_CODE (type) == VECTOR_TYPE)
16257         switch (TREE_CODE (init))
16258           {
16259           case VECTOR_CST:
16260             break;
16261           case CONSTRUCTOR:
16262             if (TREE_CONSTANT (init))
16263               {
16264                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
16265                 bool constant_p = true;
16266                 tree value;
16267                 unsigned HOST_WIDE_INT ix;
16268
16269                 /* Even when ctor is constant, it might contain non-*_CST
16270                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
16271                    belong into VECTOR_CST nodes.  */
16272                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
16273                   if (!CONSTANT_CLASS_P (value))
16274                     {
16275                       constant_p = false;
16276                       break;
16277                     }
16278
16279                 if (constant_p)
16280                   {
16281                     init = build_vector_from_ctor (type, elts);
16282                     break;
16283                   }
16284               }
16285             /* FALLTHRU */
16286
16287           default:
16288             return NULL;
16289           }
16290
16291       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
16292
16293       /* If expand_expr returns a MEM, it wasn't immediate.  */
16294       gcc_assert (!rtl || !MEM_P (rtl));
16295     }
16296
16297   return rtl;
16298 }
16299
16300 /* Generate RTL for the variable DECL to represent its location.  */
16301
16302 static rtx
16303 rtl_for_decl_location (tree decl)
16304 {
16305   rtx rtl;
16306
16307   /* Here we have to decide where we are going to say the parameter "lives"
16308      (as far as the debugger is concerned).  We only have a couple of
16309      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
16310
16311      DECL_RTL normally indicates where the parameter lives during most of the
16312      activation of the function.  If optimization is enabled however, this
16313      could be either NULL or else a pseudo-reg.  Both of those cases indicate
16314      that the parameter doesn't really live anywhere (as far as the code
16315      generation parts of GCC are concerned) during most of the function's
16316      activation.  That will happen (for example) if the parameter is never
16317      referenced within the function.
16318
16319      We could just generate a location descriptor here for all non-NULL
16320      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
16321      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
16322      where DECL_RTL is NULL or is a pseudo-reg.
16323
16324      Note however that we can only get away with using DECL_INCOMING_RTL as
16325      a backup substitute for DECL_RTL in certain limited cases.  In cases
16326      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
16327      we can be sure that the parameter was passed using the same type as it is
16328      declared to have within the function, and that its DECL_INCOMING_RTL
16329      points us to a place where a value of that type is passed.
16330
16331      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
16332      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
16333      because in these cases DECL_INCOMING_RTL points us to a value of some
16334      type which is *different* from the type of the parameter itself.  Thus,
16335      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
16336      such cases, the debugger would end up (for example) trying to fetch a
16337      `float' from a place which actually contains the first part of a
16338      `double'.  That would lead to really incorrect and confusing
16339      output at debug-time.
16340
16341      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
16342      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
16343      are a couple of exceptions however.  On little-endian machines we can
16344      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
16345      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
16346      an integral type that is smaller than TREE_TYPE (decl). These cases arise
16347      when (on a little-endian machine) a non-prototyped function has a
16348      parameter declared to be of type `short' or `char'.  In such cases,
16349      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
16350      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
16351      passed `int' value.  If the debugger then uses that address to fetch
16352      a `short' or a `char' (on a little-endian machine) the result will be
16353      the correct data, so we allow for such exceptional cases below.
16354
16355      Note that our goal here is to describe the place where the given formal
16356      parameter lives during most of the function's activation (i.e. between the
16357      end of the prologue and the start of the epilogue).  We'll do that as best
16358      as we can. Note however that if the given formal parameter is modified
16359      sometime during the execution of the function, then a stack backtrace (at
16360      debug-time) will show the function as having been called with the *new*
16361      value rather than the value which was originally passed in.  This happens
16362      rarely enough that it is not a major problem, but it *is* a problem, and
16363      I'd like to fix it.
16364
16365      A future version of dwarf2out.c may generate two additional attributes for
16366      any given DW_TAG_formal_parameter DIE which will describe the "passed
16367      type" and the "passed location" for the given formal parameter in addition
16368      to the attributes we now generate to indicate the "declared type" and the
16369      "active location" for each parameter.  This additional set of attributes
16370      could be used by debuggers for stack backtraces. Separately, note that
16371      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
16372      This happens (for example) for inlined-instances of inline function formal
16373      parameters which are never referenced.  This really shouldn't be
16374      happening.  All PARM_DECL nodes should get valid non-NULL
16375      DECL_INCOMING_RTL values.  FIXME.  */
16376
16377   /* Use DECL_RTL as the "location" unless we find something better.  */
16378   rtl = DECL_RTL_IF_SET (decl);
16379
16380   /* When generating abstract instances, ignore everything except
16381      constants, symbols living in memory, and symbols living in
16382      fixed registers.  */
16383   if (! reload_completed)
16384     {
16385       if (rtl
16386           && (CONSTANT_P (rtl)
16387               || (MEM_P (rtl)
16388                   && CONSTANT_P (XEXP (rtl, 0)))
16389               || (REG_P (rtl)
16390                   && TREE_CODE (decl) == VAR_DECL
16391                   && TREE_STATIC (decl))))
16392         {
16393           rtl = targetm.delegitimize_address (rtl);
16394           return rtl;
16395         }
16396       rtl = NULL_RTX;
16397     }
16398   else if (TREE_CODE (decl) == PARM_DECL)
16399     {
16400       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
16401         {
16402           tree declared_type = TREE_TYPE (decl);
16403           tree passed_type = DECL_ARG_TYPE (decl);
16404           enum machine_mode dmode = TYPE_MODE (declared_type);
16405           enum machine_mode pmode = TYPE_MODE (passed_type);
16406
16407           /* This decl represents a formal parameter which was optimized out.
16408              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
16409              all cases where (rtl == NULL_RTX) just below.  */
16410           if (dmode == pmode)
16411             rtl = DECL_INCOMING_RTL (decl);
16412           else if (SCALAR_INT_MODE_P (dmode)
16413                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
16414                    && DECL_INCOMING_RTL (decl))
16415             {
16416               rtx inc = DECL_INCOMING_RTL (decl);
16417               if (REG_P (inc))
16418                 rtl = inc;
16419               else if (MEM_P (inc))
16420                 {
16421                   if (BYTES_BIG_ENDIAN)
16422                     rtl = adjust_address_nv (inc, dmode,
16423                                              GET_MODE_SIZE (pmode)
16424                                              - GET_MODE_SIZE (dmode));
16425                   else
16426                     rtl = inc;
16427                 }
16428             }
16429         }
16430
16431       /* If the parm was passed in registers, but lives on the stack, then
16432          make a big endian correction if the mode of the type of the
16433          parameter is not the same as the mode of the rtl.  */
16434       /* ??? This is the same series of checks that are made in dbxout.c before
16435          we reach the big endian correction code there.  It isn't clear if all
16436          of these checks are necessary here, but keeping them all is the safe
16437          thing to do.  */
16438       else if (MEM_P (rtl)
16439                && XEXP (rtl, 0) != const0_rtx
16440                && ! CONSTANT_P (XEXP (rtl, 0))
16441                /* Not passed in memory.  */
16442                && !MEM_P (DECL_INCOMING_RTL (decl))
16443                /* Not passed by invisible reference.  */
16444                && (!REG_P (XEXP (rtl, 0))
16445                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
16446                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
16447 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
16448                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
16449 #endif
16450                      )
16451                /* Big endian correction check.  */
16452                && BYTES_BIG_ENDIAN
16453                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
16454                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
16455                    < UNITS_PER_WORD))
16456         {
16457           int offset = (UNITS_PER_WORD
16458                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
16459
16460           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16461                              plus_constant (XEXP (rtl, 0), offset));
16462         }
16463     }
16464   else if (TREE_CODE (decl) == VAR_DECL
16465            && rtl
16466            && MEM_P (rtl)
16467            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
16468            && BYTES_BIG_ENDIAN)
16469     {
16470       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
16471       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
16472
16473       /* If a variable is declared "register" yet is smaller than
16474          a register, then if we store the variable to memory, it
16475          looks like we're storing a register-sized value, when in
16476          fact we are not.  We need to adjust the offset of the
16477          storage location to reflect the actual value's bytes,
16478          else gdb will not be able to display it.  */
16479       if (rsize > dsize)
16480         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
16481                            plus_constant (XEXP (rtl, 0), rsize-dsize));
16482     }
16483
16484   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
16485      and will have been substituted directly into all expressions that use it.
16486      C does not have such a concept, but C++ and other languages do.  */
16487   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
16488     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
16489
16490   if (rtl)
16491     rtl = targetm.delegitimize_address (rtl);
16492
16493   /* If we don't look past the constant pool, we risk emitting a
16494      reference to a constant pool entry that isn't referenced from
16495      code, and thus is not emitted.  */
16496   if (rtl)
16497     rtl = avoid_constant_pool_reference (rtl);
16498
16499   /* Try harder to get a rtl.  If this symbol ends up not being emitted
16500      in the current CU, resolve_addr will remove the expression referencing
16501      it.  */
16502   if (rtl == NULL_RTX
16503       && TREE_CODE (decl) == VAR_DECL
16504       && !DECL_EXTERNAL (decl)
16505       && TREE_STATIC (decl)
16506       && DECL_NAME (decl)
16507       && !DECL_HARD_REGISTER (decl)
16508       && DECL_MODE (decl) != VOIDmode)
16509     {
16510       rtl = make_decl_rtl_for_debug (decl);
16511       if (!MEM_P (rtl)
16512           || GET_CODE (XEXP (rtl, 0)) != SYMBOL_REF
16513           || SYMBOL_REF_DECL (XEXP (rtl, 0)) != decl)
16514         rtl = NULL_RTX;
16515     }
16516
16517   return rtl;
16518 }
16519
16520 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
16521    returned.  If so, the decl for the COMMON block is returned, and the
16522    value is the offset into the common block for the symbol.  */
16523
16524 static tree
16525 fortran_common (tree decl, HOST_WIDE_INT *value)
16526 {
16527   tree val_expr, cvar;
16528   enum machine_mode mode;
16529   HOST_WIDE_INT bitsize, bitpos;
16530   tree offset;
16531   int volatilep = 0, unsignedp = 0;
16532
16533   /* If the decl isn't a VAR_DECL, or if it isn't static, or if
16534      it does not have a value (the offset into the common area), or if it
16535      is thread local (as opposed to global) then it isn't common, and shouldn't
16536      be handled as such.  */
16537   if (TREE_CODE (decl) != VAR_DECL
16538       || !TREE_STATIC (decl)
16539       || !DECL_HAS_VALUE_EXPR_P (decl)
16540       || !is_fortran ())
16541     return NULL_TREE;
16542
16543   val_expr = DECL_VALUE_EXPR (decl);
16544   if (TREE_CODE (val_expr) != COMPONENT_REF)
16545     return NULL_TREE;
16546
16547   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
16548                               &mode, &unsignedp, &volatilep, true);
16549
16550   if (cvar == NULL_TREE
16551       || TREE_CODE (cvar) != VAR_DECL
16552       || DECL_ARTIFICIAL (cvar)
16553       || !TREE_PUBLIC (cvar))
16554     return NULL_TREE;
16555
16556   *value = 0;
16557   if (offset != NULL)
16558     {
16559       if (!host_integerp (offset, 0))
16560         return NULL_TREE;
16561       *value = tree_low_cst (offset, 0);
16562     }
16563   if (bitpos != 0)
16564     *value += bitpos / BITS_PER_UNIT;
16565
16566   return cvar;
16567 }
16568
16569 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
16570    data attribute for a variable or a parameter.  We generate the
16571    DW_AT_const_value attribute only in those cases where the given variable
16572    or parameter does not have a true "location" either in memory or in a
16573    register.  This can happen (for example) when a constant is passed as an
16574    actual argument in a call to an inline function.  (It's possible that
16575    these things can crop up in other ways also.)  Note that one type of
16576    constant value which can be passed into an inlined function is a constant
16577    pointer.  This can happen for example if an actual argument in an inlined
16578    function call evaluates to a compile-time constant address.  */
16579
16580 static bool
16581 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
16582                                        enum dwarf_attribute attr)
16583 {
16584   rtx rtl;
16585   dw_loc_list_ref list;
16586   var_loc_list *loc_list;
16587
16588   if (TREE_CODE (decl) == ERROR_MARK)
16589     return false;
16590
16591   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
16592               || TREE_CODE (decl) == RESULT_DECL);
16593
16594   /* Try to get some constant RTL for this decl, and use that as the value of
16595      the location.  */
16596
16597   rtl = rtl_for_decl_location (decl);
16598   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16599       && add_const_value_attribute (die, rtl))
16600     return true;
16601
16602   /* See if we have single element location list that is equivalent to
16603      a constant value.  That way we are better to use add_const_value_attribute
16604      rather than expanding constant value equivalent.  */
16605   loc_list = lookup_decl_loc (decl);
16606   if (loc_list
16607       && loc_list->first
16608       && loc_list->first->next == NULL
16609       && NOTE_P (loc_list->first->loc)
16610       && NOTE_VAR_LOCATION (loc_list->first->loc)
16611       && NOTE_VAR_LOCATION_LOC (loc_list->first->loc))
16612     {
16613       struct var_loc_node *node;
16614
16615       node = loc_list->first;
16616       rtl = NOTE_VAR_LOCATION_LOC (node->loc);
16617       if (GET_CODE (rtl) == EXPR_LIST)
16618         rtl = XEXP (rtl, 0);
16619       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
16620           && add_const_value_attribute (die, rtl))
16621          return true;
16622     }
16623   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
16624   if (list)
16625     {
16626       add_AT_location_description (die, attr, list);
16627       return true;
16628     }
16629   /* None of that worked, so it must not really have a location;
16630      try adding a constant value attribute from the DECL_INITIAL.  */
16631   return tree_add_const_value_attribute_for_decl (die, decl);
16632 }
16633
16634 /* Add VARIABLE and DIE into deferred locations list.  */
16635
16636 static void
16637 defer_location (tree variable, dw_die_ref die)
16638 {
16639   deferred_locations entry;
16640   entry.variable = variable;
16641   entry.die = die;
16642   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
16643 }
16644
16645 /* Helper function for tree_add_const_value_attribute.  Natively encode
16646    initializer INIT into an array.  Return true if successful.  */
16647
16648 static bool
16649 native_encode_initializer (tree init, unsigned char *array, int size)
16650 {
16651   tree type;
16652
16653   if (init == NULL_TREE)
16654     return false;
16655
16656   STRIP_NOPS (init);
16657   switch (TREE_CODE (init))
16658     {
16659     case STRING_CST:
16660       type = TREE_TYPE (init);
16661       if (TREE_CODE (type) == ARRAY_TYPE)
16662         {
16663           tree enttype = TREE_TYPE (type);
16664           enum machine_mode mode = TYPE_MODE (enttype);
16665
16666           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
16667             return false;
16668           if (int_size_in_bytes (type) != size)
16669             return false;
16670           if (size > TREE_STRING_LENGTH (init))
16671             {
16672               memcpy (array, TREE_STRING_POINTER (init),
16673                       TREE_STRING_LENGTH (init));
16674               memset (array + TREE_STRING_LENGTH (init),
16675                       '\0', size - TREE_STRING_LENGTH (init));
16676             }
16677           else
16678             memcpy (array, TREE_STRING_POINTER (init), size);
16679           return true;
16680         }
16681       return false;
16682     case CONSTRUCTOR:
16683       type = TREE_TYPE (init);
16684       if (int_size_in_bytes (type) != size)
16685         return false;
16686       if (TREE_CODE (type) == ARRAY_TYPE)
16687         {
16688           HOST_WIDE_INT min_index;
16689           unsigned HOST_WIDE_INT cnt;
16690           int curpos = 0, fieldsize;
16691           constructor_elt *ce;
16692
16693           if (TYPE_DOMAIN (type) == NULL_TREE
16694               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
16695             return false;
16696
16697           fieldsize = int_size_in_bytes (TREE_TYPE (type));
16698           if (fieldsize <= 0)
16699             return false;
16700
16701           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
16702           memset (array, '\0', size);
16703           for (cnt = 0;
16704                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16705                cnt++)
16706             {
16707               tree val = ce->value;
16708               tree index = ce->index;
16709               int pos = curpos;
16710               if (index && TREE_CODE (index) == RANGE_EXPR)
16711                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
16712                       * fieldsize;
16713               else if (index)
16714                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
16715
16716               if (val)
16717                 {
16718                   STRIP_NOPS (val);
16719                   if (!native_encode_initializer (val, array + pos, fieldsize))
16720                     return false;
16721                 }
16722               curpos = pos + fieldsize;
16723               if (index && TREE_CODE (index) == RANGE_EXPR)
16724                 {
16725                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
16726                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
16727                   while (count > 0)
16728                     {
16729                       if (val)
16730                         memcpy (array + curpos, array + pos, fieldsize);
16731                       curpos += fieldsize;
16732                     }
16733                 }
16734               gcc_assert (curpos <= size);
16735             }
16736           return true;
16737         }
16738       else if (TREE_CODE (type) == RECORD_TYPE
16739                || TREE_CODE (type) == UNION_TYPE)
16740         {
16741           tree field = NULL_TREE;
16742           unsigned HOST_WIDE_INT cnt;
16743           constructor_elt *ce;
16744
16745           if (int_size_in_bytes (type) != size)
16746             return false;
16747
16748           if (TREE_CODE (type) == RECORD_TYPE)
16749             field = TYPE_FIELDS (type);
16750
16751           for (cnt = 0;
16752                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
16753                cnt++, field = field ? DECL_CHAIN (field) : 0)
16754             {
16755               tree val = ce->value;
16756               int pos, fieldsize;
16757
16758               if (ce->index != 0)
16759                 field = ce->index;
16760
16761               if (val)
16762                 STRIP_NOPS (val);
16763
16764               if (field == NULL_TREE || DECL_BIT_FIELD (field))
16765                 return false;
16766
16767               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
16768                   && TYPE_DOMAIN (TREE_TYPE (field))
16769                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
16770                 return false;
16771               else if (DECL_SIZE_UNIT (field) == NULL_TREE
16772                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
16773                 return false;
16774               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
16775               pos = int_byte_position (field);
16776               gcc_assert (pos + fieldsize <= size);
16777               if (val
16778                   && !native_encode_initializer (val, array + pos, fieldsize))
16779                 return false;
16780             }
16781           return true;
16782         }
16783       return false;
16784     case VIEW_CONVERT_EXPR:
16785     case NON_LVALUE_EXPR:
16786       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
16787     default:
16788       return native_encode_expr (init, array, size) == size;
16789     }
16790 }
16791
16792 /* Attach a DW_AT_const_value attribute to DIE. The value of the
16793    attribute is the const value T.  */
16794
16795 static bool
16796 tree_add_const_value_attribute (dw_die_ref die, tree t)
16797 {
16798   tree init;
16799   tree type = TREE_TYPE (t);
16800   rtx rtl;
16801
16802   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
16803     return false;
16804
16805   init = t;
16806   gcc_assert (!DECL_P (init));
16807
16808   rtl = rtl_for_decl_init (init, type);
16809   if (rtl)
16810     return add_const_value_attribute (die, rtl);
16811   /* If the host and target are sane, try harder.  */
16812   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
16813            && initializer_constant_valid_p (init, type))
16814     {
16815       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
16816       if (size > 0 && (int) size == size)
16817         {
16818           unsigned char *array = (unsigned char *)
16819             ggc_alloc_cleared_atomic (size);
16820
16821           if (native_encode_initializer (init, array, size))
16822             {
16823               add_AT_vec (die, DW_AT_const_value, size, 1, array);
16824               return true;
16825             }
16826         }
16827     }
16828   return false;
16829 }
16830
16831 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
16832    attribute is the const value of T, where T is an integral constant
16833    variable with static storage duration
16834    (so it can't be a PARM_DECL or a RESULT_DECL).  */
16835
16836 static bool
16837 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
16838 {
16839
16840   if (!decl
16841       || (TREE_CODE (decl) != VAR_DECL
16842           && TREE_CODE (decl) != CONST_DECL))
16843     return false;
16844
16845     if (TREE_READONLY (decl)
16846         && ! TREE_THIS_VOLATILE (decl)
16847         && DECL_INITIAL (decl))
16848       /* OK */;
16849     else
16850       return false;
16851
16852   /* Don't add DW_AT_const_value if abstract origin already has one.  */
16853   if (get_AT (var_die, DW_AT_const_value))
16854     return false;
16855
16856   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
16857 }
16858
16859 /* Convert the CFI instructions for the current function into a
16860    location list.  This is used for DW_AT_frame_base when we targeting
16861    a dwarf2 consumer that does not support the dwarf3
16862    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
16863    expressions.  */
16864
16865 static dw_loc_list_ref
16866 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
16867 {
16868   dw_fde_ref fde;
16869   dw_loc_list_ref list, *list_tail;
16870   dw_cfi_ref cfi;
16871   dw_cfa_location last_cfa, next_cfa;
16872   const char *start_label, *last_label, *section;
16873   dw_cfa_location remember;
16874
16875   fde = current_fde ();
16876   gcc_assert (fde != NULL);
16877
16878   section = secname_for_decl (current_function_decl);
16879   list_tail = &list;
16880   list = NULL;
16881
16882   memset (&next_cfa, 0, sizeof (next_cfa));
16883   next_cfa.reg = INVALID_REGNUM;
16884   remember = next_cfa;
16885
16886   start_label = fde->dw_fde_begin;
16887
16888   /* ??? Bald assumption that the CIE opcode list does not contain
16889      advance opcodes.  */
16890   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
16891     lookup_cfa_1 (cfi, &next_cfa, &remember);
16892
16893   last_cfa = next_cfa;
16894   last_label = start_label;
16895
16896   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
16897     switch (cfi->dw_cfi_opc)
16898       {
16899       case DW_CFA_set_loc:
16900       case DW_CFA_advance_loc1:
16901       case DW_CFA_advance_loc2:
16902       case DW_CFA_advance_loc4:
16903         if (!cfa_equal_p (&last_cfa, &next_cfa))
16904           {
16905             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16906                                        start_label, last_label, section);
16907
16908             list_tail = &(*list_tail)->dw_loc_next;
16909             last_cfa = next_cfa;
16910             start_label = last_label;
16911           }
16912         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
16913         break;
16914
16915       case DW_CFA_advance_loc:
16916         /* The encoding is complex enough that we should never emit this.  */
16917         gcc_unreachable ();
16918
16919       default:
16920         lookup_cfa_1 (cfi, &next_cfa, &remember);
16921         break;
16922       }
16923
16924   if (!cfa_equal_p (&last_cfa, &next_cfa))
16925     {
16926       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
16927                                  start_label, last_label, section);
16928       list_tail = &(*list_tail)->dw_loc_next;
16929       start_label = last_label;
16930     }
16931
16932   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
16933                              start_label, fde->dw_fde_end, section);
16934
16935   if (list && list->dw_loc_next)
16936     gen_llsym (list);
16937
16938   return list;
16939 }
16940
16941 /* Compute a displacement from the "steady-state frame pointer" to the
16942    frame base (often the same as the CFA), and store it in
16943    frame_pointer_fb_offset.  OFFSET is added to the displacement
16944    before the latter is negated.  */
16945
16946 static void
16947 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
16948 {
16949   rtx reg, elim;
16950
16951 #ifdef FRAME_POINTER_CFA_OFFSET
16952   reg = frame_pointer_rtx;
16953   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
16954 #else
16955   reg = arg_pointer_rtx;
16956   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
16957 #endif
16958
16959   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
16960   if (GET_CODE (elim) == PLUS)
16961     {
16962       offset += INTVAL (XEXP (elim, 1));
16963       elim = XEXP (elim, 0);
16964     }
16965
16966   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
16967                && (elim == hard_frame_pointer_rtx
16968                    || elim == stack_pointer_rtx))
16969               || elim == (frame_pointer_needed
16970                           ? hard_frame_pointer_rtx
16971                           : stack_pointer_rtx));
16972
16973   frame_pointer_fb_offset = -offset;
16974 }
16975
16976 /* Generate a DW_AT_name attribute given some string value to be included as
16977    the value of the attribute.  */
16978
16979 static void
16980 add_name_attribute (dw_die_ref die, const char *name_string)
16981 {
16982   if (name_string != NULL && *name_string != 0)
16983     {
16984       if (demangle_name_func)
16985         name_string = (*demangle_name_func) (name_string);
16986
16987       add_AT_string (die, DW_AT_name, name_string);
16988     }
16989 }
16990
16991 /* Generate a DW_AT_comp_dir attribute for DIE.  */
16992
16993 static void
16994 add_comp_dir_attribute (dw_die_ref die)
16995 {
16996   const char *wd = get_src_pwd ();
16997   char *wd1;
16998
16999   if (wd == NULL)
17000     return;
17001
17002   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
17003     {
17004       int wdlen;
17005
17006       wdlen = strlen (wd);
17007       wd1 = (char *) ggc_alloc_atomic (wdlen + 2);
17008       strcpy (wd1, wd);
17009       wd1 [wdlen] = DIR_SEPARATOR;
17010       wd1 [wdlen + 1] = 0;
17011       wd = wd1;
17012     }
17013
17014     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
17015 }
17016
17017 /* Return the default for DW_AT_lower_bound, or -1 if there is not any
17018    default.  */
17019
17020 static int
17021 lower_bound_default (void)
17022 {
17023   switch (get_AT_unsigned (comp_unit_die, DW_AT_language))
17024     {
17025     case DW_LANG_C:
17026     case DW_LANG_C89:
17027     case DW_LANG_C99:
17028     case DW_LANG_C_plus_plus:
17029     case DW_LANG_ObjC:
17030     case DW_LANG_ObjC_plus_plus:
17031     case DW_LANG_Java:
17032       return 0;
17033     case DW_LANG_Fortran77:
17034     case DW_LANG_Fortran90:
17035     case DW_LANG_Fortran95:
17036       return 1;
17037     case DW_LANG_UPC:
17038     case DW_LANG_D:
17039     case DW_LANG_Python:
17040       return dwarf_version >= 4 ? 0 : -1;
17041     case DW_LANG_Ada95:
17042     case DW_LANG_Ada83:
17043     case DW_LANG_Cobol74:
17044     case DW_LANG_Cobol85:
17045     case DW_LANG_Pascal83:
17046     case DW_LANG_Modula2:
17047     case DW_LANG_PLI:
17048       return dwarf_version >= 4 ? 1 : -1;
17049     default:
17050       return -1;
17051     }
17052 }
17053
17054 /* Given a tree node describing an array bound (either lower or upper) output
17055    a representation for that bound.  */
17056
17057 static void
17058 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
17059 {
17060   switch (TREE_CODE (bound))
17061     {
17062     case ERROR_MARK:
17063       return;
17064
17065     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
17066     case INTEGER_CST:
17067       {
17068         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
17069         int dflt;
17070
17071         /* Use the default if possible.  */
17072         if (bound_attr == DW_AT_lower_bound
17073             && host_integerp (bound, 0)
17074             && (dflt = lower_bound_default ()) != -1
17075             && tree_low_cst (bound, 0) == dflt)
17076           ;
17077
17078         /* Otherwise represent the bound as an unsigned value with the
17079            precision of its type.  The precision and signedness of the
17080            type will be necessary to re-interpret it unambiguously.  */
17081         else if (prec < HOST_BITS_PER_WIDE_INT)
17082           {
17083             unsigned HOST_WIDE_INT mask
17084               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
17085             add_AT_unsigned (subrange_die, bound_attr,
17086                              TREE_INT_CST_LOW (bound) & mask);
17087           }
17088         else if (prec == HOST_BITS_PER_WIDE_INT
17089                  || TREE_INT_CST_HIGH (bound) == 0)
17090           add_AT_unsigned (subrange_die, bound_attr,
17091                            TREE_INT_CST_LOW (bound));
17092         else
17093           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
17094                          TREE_INT_CST_LOW (bound));
17095       }
17096       break;
17097
17098     CASE_CONVERT:
17099     case VIEW_CONVERT_EXPR:
17100       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
17101       break;
17102
17103     case SAVE_EXPR:
17104       break;
17105
17106     case VAR_DECL:
17107     case PARM_DECL:
17108     case RESULT_DECL:
17109       {
17110         dw_die_ref decl_die = lookup_decl_die (bound);
17111
17112         /* ??? Can this happen, or should the variable have been bound
17113            first?  Probably it can, since I imagine that we try to create
17114            the types of parameters in the order in which they exist in
17115            the list, and won't have created a forward reference to a
17116            later parameter.  */
17117         if (decl_die != NULL)
17118           {
17119             add_AT_die_ref (subrange_die, bound_attr, decl_die);
17120             break;
17121           }
17122       }
17123       /* FALLTHRU */
17124
17125     default:
17126       {
17127         /* Otherwise try to create a stack operation procedure to
17128            evaluate the value of the array bound.  */
17129
17130         dw_die_ref ctx, decl_die;
17131         dw_loc_list_ref list;
17132
17133         list = loc_list_from_tree (bound, 2);
17134         if (list == NULL || single_element_loc_list_p (list))
17135           {
17136             /* If DW_AT_*bound is not a reference nor constant, it is
17137                a DWARF expression rather than location description.
17138                For that loc_list_from_tree (bound, 0) is needed.
17139                If that fails to give a single element list,
17140                fall back to outputting this as a reference anyway.  */
17141             dw_loc_list_ref list2 = loc_list_from_tree (bound, 0);
17142             if (list2 && single_element_loc_list_p (list2))
17143               {
17144                 add_AT_loc (subrange_die, bound_attr, list2->expr);
17145                 break;
17146               }
17147           }
17148         if (list == NULL)
17149           break;
17150
17151         if (current_function_decl == 0)
17152           ctx = comp_unit_die;
17153         else
17154           ctx = lookup_decl_die (current_function_decl);
17155
17156         decl_die = new_die (DW_TAG_variable, ctx, bound);
17157         add_AT_flag (decl_die, DW_AT_artificial, 1);
17158         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
17159         add_AT_location_description (decl_die, DW_AT_location, list);
17160         add_AT_die_ref (subrange_die, bound_attr, decl_die);
17161         break;
17162       }
17163     }
17164 }
17165
17166 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
17167    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
17168    Note that the block of subscript information for an array type also
17169    includes information about the element type of the given array type.  */
17170
17171 static void
17172 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
17173 {
17174   unsigned dimension_number;
17175   tree lower, upper;
17176   dw_die_ref subrange_die;
17177
17178   for (dimension_number = 0;
17179        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
17180        type = TREE_TYPE (type), dimension_number++)
17181     {
17182       tree domain = TYPE_DOMAIN (type);
17183
17184       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
17185         break;
17186
17187       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
17188          and (in GNU C only) variable bounds.  Handle all three forms
17189          here.  */
17190       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
17191       if (domain)
17192         {
17193           /* We have an array type with specified bounds.  */
17194           lower = TYPE_MIN_VALUE (domain);
17195           upper = TYPE_MAX_VALUE (domain);
17196
17197           /* Define the index type.  */
17198           if (TREE_TYPE (domain))
17199             {
17200               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
17201                  TREE_TYPE field.  We can't emit debug info for this
17202                  because it is an unnamed integral type.  */
17203               if (TREE_CODE (domain) == INTEGER_TYPE
17204                   && TYPE_NAME (domain) == NULL_TREE
17205                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
17206                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
17207                 ;
17208               else
17209                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
17210                                     type_die);
17211             }
17212
17213           /* ??? If upper is NULL, the array has unspecified length,
17214              but it does have a lower bound.  This happens with Fortran
17215                dimension arr(N:*)
17216              Since the debugger is definitely going to need to know N
17217              to produce useful results, go ahead and output the lower
17218              bound solo, and hope the debugger can cope.  */
17219
17220           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
17221           if (upper)
17222             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
17223         }
17224
17225       /* Otherwise we have an array type with an unspecified length.  The
17226          DWARF-2 spec does not say how to handle this; let's just leave out the
17227          bounds.  */
17228     }
17229 }
17230
17231 static void
17232 add_byte_size_attribute (dw_die_ref die, tree tree_node)
17233 {
17234   unsigned size;
17235
17236   switch (TREE_CODE (tree_node))
17237     {
17238     case ERROR_MARK:
17239       size = 0;
17240       break;
17241     case ENUMERAL_TYPE:
17242     case RECORD_TYPE:
17243     case UNION_TYPE:
17244     case QUAL_UNION_TYPE:
17245       size = int_size_in_bytes (tree_node);
17246       break;
17247     case FIELD_DECL:
17248       /* For a data member of a struct or union, the DW_AT_byte_size is
17249          generally given as the number of bytes normally allocated for an
17250          object of the *declared* type of the member itself.  This is true
17251          even for bit-fields.  */
17252       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
17253       break;
17254     default:
17255       gcc_unreachable ();
17256     }
17257
17258   /* Note that `size' might be -1 when we get to this point.  If it is, that
17259      indicates that the byte size of the entity in question is variable.  We
17260      have no good way of expressing this fact in Dwarf at the present time,
17261      so just let the -1 pass on through.  */
17262   add_AT_unsigned (die, DW_AT_byte_size, size);
17263 }
17264
17265 /* For a FIELD_DECL node which represents a bit-field, output an attribute
17266    which specifies the distance in bits from the highest order bit of the
17267    "containing object" for the bit-field to the highest order bit of the
17268    bit-field itself.
17269
17270    For any given bit-field, the "containing object" is a hypothetical object
17271    (of some integral or enum type) within which the given bit-field lives.  The
17272    type of this hypothetical "containing object" is always the same as the
17273    declared type of the individual bit-field itself.  The determination of the
17274    exact location of the "containing object" for a bit-field is rather
17275    complicated.  It's handled by the `field_byte_offset' function (above).
17276
17277    Note that it is the size (in bytes) of the hypothetical "containing object"
17278    which will be given in the DW_AT_byte_size attribute for this bit-field.
17279    (See `byte_size_attribute' above).  */
17280
17281 static inline void
17282 add_bit_offset_attribute (dw_die_ref die, tree decl)
17283 {
17284   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
17285   tree type = DECL_BIT_FIELD_TYPE (decl);
17286   HOST_WIDE_INT bitpos_int;
17287   HOST_WIDE_INT highest_order_object_bit_offset;
17288   HOST_WIDE_INT highest_order_field_bit_offset;
17289   HOST_WIDE_INT unsigned bit_offset;
17290
17291   /* Must be a field and a bit field.  */
17292   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
17293
17294   /* We can't yet handle bit-fields whose offsets are variable, so if we
17295      encounter such things, just return without generating any attribute
17296      whatsoever.  Likewise for variable or too large size.  */
17297   if (! host_integerp (bit_position (decl), 0)
17298       || ! host_integerp (DECL_SIZE (decl), 1))
17299     return;
17300
17301   bitpos_int = int_bit_position (decl);
17302
17303   /* Note that the bit offset is always the distance (in bits) from the
17304      highest-order bit of the "containing object" to the highest-order bit of
17305      the bit-field itself.  Since the "high-order end" of any object or field
17306      is different on big-endian and little-endian machines, the computation
17307      below must take account of these differences.  */
17308   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
17309   highest_order_field_bit_offset = bitpos_int;
17310
17311   if (! BYTES_BIG_ENDIAN)
17312     {
17313       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
17314       highest_order_object_bit_offset += simple_type_size_in_bits (type);
17315     }
17316
17317   bit_offset
17318     = (! BYTES_BIG_ENDIAN
17319        ? highest_order_object_bit_offset - highest_order_field_bit_offset
17320        : highest_order_field_bit_offset - highest_order_object_bit_offset);
17321
17322   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
17323 }
17324
17325 /* For a FIELD_DECL node which represents a bit field, output an attribute
17326    which specifies the length in bits of the given field.  */
17327
17328 static inline void
17329 add_bit_size_attribute (dw_die_ref die, tree decl)
17330 {
17331   /* Must be a field and a bit field.  */
17332   gcc_assert (TREE_CODE (decl) == FIELD_DECL
17333               && DECL_BIT_FIELD_TYPE (decl));
17334
17335   if (host_integerp (DECL_SIZE (decl), 1))
17336     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
17337 }
17338
17339 /* If the compiled language is ANSI C, then add a 'prototyped'
17340    attribute, if arg types are given for the parameters of a function.  */
17341
17342 static inline void
17343 add_prototyped_attribute (dw_die_ref die, tree func_type)
17344 {
17345   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
17346       && TYPE_ARG_TYPES (func_type) != NULL)
17347     add_AT_flag (die, DW_AT_prototyped, 1);
17348 }
17349
17350 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
17351    by looking in either the type declaration or object declaration
17352    equate table.  */
17353
17354 static inline dw_die_ref
17355 add_abstract_origin_attribute (dw_die_ref die, tree origin)
17356 {
17357   dw_die_ref origin_die = NULL;
17358
17359   if (TREE_CODE (origin) != FUNCTION_DECL)
17360     {
17361       /* We may have gotten separated from the block for the inlined
17362          function, if we're in an exception handler or some such; make
17363          sure that the abstract function has been written out.
17364
17365          Doing this for nested functions is wrong, however; functions are
17366          distinct units, and our context might not even be inline.  */
17367       tree fn = origin;
17368
17369       if (TYPE_P (fn))
17370         fn = TYPE_STUB_DECL (fn);
17371
17372       fn = decl_function_context (fn);
17373       if (fn)
17374         dwarf2out_abstract_function (fn);
17375     }
17376
17377   if (DECL_P (origin))
17378     origin_die = lookup_decl_die (origin);
17379   else if (TYPE_P (origin))
17380     origin_die = lookup_type_die (origin);
17381
17382   /* XXX: Functions that are never lowered don't always have correct block
17383      trees (in the case of java, they simply have no block tree, in some other
17384      languages).  For these functions, there is nothing we can really do to
17385      output correct debug info for inlined functions in all cases.  Rather
17386      than die, we'll just produce deficient debug info now, in that we will
17387      have variables without a proper abstract origin.  In the future, when all
17388      functions are lowered, we should re-add a gcc_assert (origin_die)
17389      here.  */
17390
17391   if (origin_die)
17392     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
17393   return origin_die;
17394 }
17395
17396 /* We do not currently support the pure_virtual attribute.  */
17397
17398 static inline void
17399 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
17400 {
17401   if (DECL_VINDEX (func_decl))
17402     {
17403       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
17404
17405       if (host_integerp (DECL_VINDEX (func_decl), 0))
17406         add_AT_loc (die, DW_AT_vtable_elem_location,
17407                     new_loc_descr (DW_OP_constu,
17408                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
17409                                    0));
17410
17411       /* GNU extension: Record what type this method came from originally.  */
17412       if (debug_info_level > DINFO_LEVEL_TERSE
17413           && DECL_CONTEXT (func_decl))
17414         add_AT_die_ref (die, DW_AT_containing_type,
17415                         lookup_type_die (DECL_CONTEXT (func_decl)));
17416     }
17417 }
17418 \f
17419 /* Add a DW_AT_linkage_name or DW_AT_MIPS_linkage_name attribute for the
17420    given decl.  This used to be a vendor extension until after DWARF 4
17421    standardized it.  */
17422
17423 static void
17424 add_linkage_attr (dw_die_ref die, tree decl)
17425 {
17426   const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
17427
17428   /* Mimic what assemble_name_raw does with a leading '*'.  */
17429   if (name[0] == '*')
17430     name = &name[1];
17431
17432   if (dwarf_version >= 4)
17433     add_AT_string (die, DW_AT_linkage_name, name);
17434   else
17435     add_AT_string (die, DW_AT_MIPS_linkage_name, name);
17436 }
17437
17438 /* Add source coordinate attributes for the given decl.  */
17439
17440 static void
17441 add_src_coords_attributes (dw_die_ref die, tree decl)
17442 {
17443   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
17444
17445   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
17446   add_AT_unsigned (die, DW_AT_decl_line, s.line);
17447 }
17448
17449 /* Add DW_AT_{,MIPS_}linkage_name attribute for the given decl.  */
17450
17451 static void
17452 add_linkage_name (dw_die_ref die, tree decl)
17453 {
17454   if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
17455        && TREE_PUBLIC (decl)
17456        && !DECL_ABSTRACT (decl)
17457        && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
17458        && die->die_tag != DW_TAG_member)
17459     {
17460       /* Defer until we have an assembler name set.  */
17461       if (!DECL_ASSEMBLER_NAME_SET_P (decl))
17462         {
17463           limbo_die_node *asm_name;
17464
17465           asm_name = ggc_alloc_cleared_limbo_die_node ();
17466           asm_name->die = die;
17467           asm_name->created_for = decl;
17468           asm_name->next = deferred_asm_name;
17469           deferred_asm_name = asm_name;
17470         }
17471       else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
17472         add_linkage_attr (die, decl);
17473     }
17474 }
17475
17476 /* Add a DW_AT_name attribute and source coordinate attribute for the
17477    given decl, but only if it actually has a name.  */
17478
17479 static void
17480 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
17481 {
17482   tree decl_name;
17483
17484   decl_name = DECL_NAME (decl);
17485   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
17486     {
17487       const char *name = dwarf2_name (decl, 0);
17488       if (name)
17489         add_name_attribute (die, name);
17490       if (! DECL_ARTIFICIAL (decl))
17491         add_src_coords_attributes (die, decl);
17492
17493       add_linkage_name (die, decl);
17494     }
17495
17496 #ifdef VMS_DEBUGGING_INFO
17497   /* Get the function's name, as described by its RTL.  This may be different
17498      from the DECL_NAME name used in the source file.  */
17499   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
17500     {
17501       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
17502                    XEXP (DECL_RTL (decl), 0));
17503       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
17504     }
17505 #endif
17506 }
17507
17508 #ifdef VMS_DEBUGGING_INFO
17509
17510 /* Output the debug main pointer die for VMS */
17511
17512 void
17513 dwarf2out_vms_debug_main_pointer (void)
17514 {
17515   char label[MAX_ARTIFICIAL_LABEL_BYTES];
17516   dw_die_ref die;
17517
17518   /* Allocate the VMS debug main subprogram die.  */
17519   die = ggc_alloc_cleared_die_node ();
17520   die->die_tag = DW_TAG_subprogram;
17521   add_name_attribute (die, VMS_DEBUG_MAIN_POINTER);
17522   ASM_GENERATE_INTERNAL_LABEL (label, PROLOGUE_END_LABEL,
17523                                current_function_funcdef_no);
17524   add_AT_lbl_id (die, DW_AT_entry_pc, label);
17525
17526   /* Make it the first child of comp_unit_die.  */
17527   die->die_parent = comp_unit_die;
17528   if (comp_unit_die->die_child)
17529     {
17530       die->die_sib = comp_unit_die->die_child->die_sib;
17531       comp_unit_die->die_child->die_sib = die;
17532     }
17533   else
17534     {
17535       die->die_sib = die;
17536       comp_unit_die->die_child = die;
17537     }
17538 }
17539 #endif
17540
17541 /* Push a new declaration scope.  */
17542
17543 static void
17544 push_decl_scope (tree scope)
17545 {
17546   VEC_safe_push (tree, gc, decl_scope_table, scope);
17547 }
17548
17549 /* Pop a declaration scope.  */
17550
17551 static inline void
17552 pop_decl_scope (void)
17553 {
17554   VEC_pop (tree, decl_scope_table);
17555 }
17556
17557 /* Return the DIE for the scope that immediately contains this type.
17558    Non-named types get global scope.  Named types nested in other
17559    types get their containing scope if it's open, or global scope
17560    otherwise.  All other types (i.e. function-local named types) get
17561    the current active scope.  */
17562
17563 static dw_die_ref
17564 scope_die_for (tree t, dw_die_ref context_die)
17565 {
17566   dw_die_ref scope_die = NULL;
17567   tree containing_scope;
17568   int i;
17569
17570   /* Non-types always go in the current scope.  */
17571   gcc_assert (TYPE_P (t));
17572
17573   containing_scope = TYPE_CONTEXT (t);
17574
17575   /* Use the containing namespace if it was passed in (for a declaration).  */
17576   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
17577     {
17578       if (context_die == lookup_decl_die (containing_scope))
17579         /* OK */;
17580       else
17581         containing_scope = NULL_TREE;
17582     }
17583
17584   /* Ignore function type "scopes" from the C frontend.  They mean that
17585      a tagged type is local to a parmlist of a function declarator, but
17586      that isn't useful to DWARF.  */
17587   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
17588     containing_scope = NULL_TREE;
17589
17590   if (containing_scope == NULL_TREE)
17591     scope_die = comp_unit_die;
17592   else if (TYPE_P (containing_scope))
17593     {
17594       /* For types, we can just look up the appropriate DIE.  But
17595          first we check to see if we're in the middle of emitting it
17596          so we know where the new DIE should go.  */
17597       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
17598         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
17599           break;
17600
17601       if (i < 0)
17602         {
17603           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
17604                       || TREE_ASM_WRITTEN (containing_scope));
17605           /*We are not in the middle of emitting the type
17606             CONTAINING_SCOPE. Let's see if it's emitted already.  */
17607           scope_die = lookup_type_die (containing_scope);
17608
17609           /* If none of the current dies are suitable, we get file scope.  */
17610           if (scope_die == NULL)
17611             scope_die = comp_unit_die;
17612         }
17613       else
17614         scope_die = lookup_type_die (containing_scope);
17615     }
17616   else
17617     scope_die = context_die;
17618
17619   return scope_die;
17620 }
17621
17622 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
17623
17624 static inline int
17625 local_scope_p (dw_die_ref context_die)
17626 {
17627   for (; context_die; context_die = context_die->die_parent)
17628     if (context_die->die_tag == DW_TAG_inlined_subroutine
17629         || context_die->die_tag == DW_TAG_subprogram)
17630       return 1;
17631
17632   return 0;
17633 }
17634
17635 /* Returns nonzero if CONTEXT_DIE is a class.  */
17636
17637 static inline int
17638 class_scope_p (dw_die_ref context_die)
17639 {
17640   return (context_die
17641           && (context_die->die_tag == DW_TAG_structure_type
17642               || context_die->die_tag == DW_TAG_class_type
17643               || context_die->die_tag == DW_TAG_interface_type
17644               || context_die->die_tag == DW_TAG_union_type));
17645 }
17646
17647 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
17648    whether or not to treat a DIE in this context as a declaration.  */
17649
17650 static inline int
17651 class_or_namespace_scope_p (dw_die_ref context_die)
17652 {
17653   return (class_scope_p (context_die)
17654           || (context_die && context_die->die_tag == DW_TAG_namespace));
17655 }
17656
17657 /* Many forms of DIEs require a "type description" attribute.  This
17658    routine locates the proper "type descriptor" die for the type given
17659    by 'type', and adds a DW_AT_type attribute below the given die.  */
17660
17661 static void
17662 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
17663                     int decl_volatile, dw_die_ref context_die)
17664 {
17665   enum tree_code code  = TREE_CODE (type);
17666   dw_die_ref type_die  = NULL;
17667
17668   /* ??? If this type is an unnamed subrange type of an integral, floating-point
17669      or fixed-point type, use the inner type.  This is because we have no
17670      support for unnamed types in base_type_die.  This can happen if this is
17671      an Ada subrange type.  Correct solution is emit a subrange type die.  */
17672   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
17673       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
17674     type = TREE_TYPE (type), code = TREE_CODE (type);
17675
17676   if (code == ERROR_MARK
17677       /* Handle a special case.  For functions whose return type is void, we
17678          generate *no* type attribute.  (Note that no object may have type
17679          `void', so this only applies to function return types).  */
17680       || code == VOID_TYPE)
17681     return;
17682
17683   type_die = modified_type_die (type,
17684                                 decl_const || TYPE_READONLY (type),
17685                                 decl_volatile || TYPE_VOLATILE (type),
17686                                 context_die);
17687
17688   if (type_die != NULL)
17689     add_AT_die_ref (object_die, DW_AT_type, type_die);
17690 }
17691
17692 /* Given an object die, add the calling convention attribute for the
17693    function call type.  */
17694 static void
17695 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
17696 {
17697   enum dwarf_calling_convention value = DW_CC_normal;
17698
17699   value = ((enum dwarf_calling_convention)
17700            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
17701
17702   /* DWARF doesn't provide a way to identify a program's source-level
17703      entry point.  DW_AT_calling_convention attributes are only meant
17704      to describe functions' calling conventions.  However, lacking a
17705      better way to signal the Fortran main program, we use this for the
17706      time being, following existing custom.  */
17707   if (is_fortran ()
17708       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
17709     value = DW_CC_program;
17710
17711   /* Only add the attribute if the backend requests it, and
17712      is not DW_CC_normal.  */
17713   if (value && (value != DW_CC_normal))
17714     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
17715 }
17716
17717 /* Given a tree pointer to a struct, class, union, or enum type node, return
17718    a pointer to the (string) tag name for the given type, or zero if the type
17719    was declared without a tag.  */
17720
17721 static const char *
17722 type_tag (const_tree type)
17723 {
17724   const char *name = 0;
17725
17726   if (TYPE_NAME (type) != 0)
17727     {
17728       tree t = 0;
17729
17730       /* Find the IDENTIFIER_NODE for the type name.  */
17731       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE
17732           && !TYPE_NAMELESS (type))
17733         t = TYPE_NAME (type);
17734
17735       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
17736          a TYPE_DECL node, regardless of whether or not a `typedef' was
17737          involved.  */
17738       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17739                && ! DECL_IGNORED_P (TYPE_NAME (type)))
17740         {
17741           /* We want to be extra verbose.  Don't call dwarf_name if
17742              DECL_NAME isn't set.  The default hook for decl_printable_name
17743              doesn't like that, and in this context it's correct to return
17744              0, instead of "<anonymous>" or the like.  */
17745           if (DECL_NAME (TYPE_NAME (type))
17746               && !DECL_NAMELESS (TYPE_NAME (type)))
17747             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
17748         }
17749
17750       /* Now get the name as a string, or invent one.  */
17751       if (!name && t != 0)
17752         name = IDENTIFIER_POINTER (t);
17753     }
17754
17755   return (name == 0 || *name == '\0') ? 0 : name;
17756 }
17757
17758 /* Return the type associated with a data member, make a special check
17759    for bit field types.  */
17760
17761 static inline tree
17762 member_declared_type (const_tree member)
17763 {
17764   return (DECL_BIT_FIELD_TYPE (member)
17765           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
17766 }
17767
17768 /* Get the decl's label, as described by its RTL. This may be different
17769    from the DECL_NAME name used in the source file.  */
17770
17771 #if 0
17772 static const char *
17773 decl_start_label (tree decl)
17774 {
17775   rtx x;
17776   const char *fnname;
17777
17778   x = DECL_RTL (decl);
17779   gcc_assert (MEM_P (x));
17780
17781   x = XEXP (x, 0);
17782   gcc_assert (GET_CODE (x) == SYMBOL_REF);
17783
17784   fnname = XSTR (x, 0);
17785   return fnname;
17786 }
17787 #endif
17788 \f
17789 /* These routines generate the internal representation of the DIE's for
17790    the compilation unit.  Debugging information is collected by walking
17791    the declaration trees passed in from dwarf2out_decl().  */
17792
17793 static void
17794 gen_array_type_die (tree type, dw_die_ref context_die)
17795 {
17796   dw_die_ref scope_die = scope_die_for (type, context_die);
17797   dw_die_ref array_die;
17798
17799   /* GNU compilers represent multidimensional array types as sequences of one
17800      dimensional array types whose element types are themselves array types.
17801      We sometimes squish that down to a single array_type DIE with multiple
17802      subscripts in the Dwarf debugging info.  The draft Dwarf specification
17803      say that we are allowed to do this kind of compression in C, because
17804      there is no difference between an array of arrays and a multidimensional
17805      array.  We don't do this for Ada to remain as close as possible to the
17806      actual representation, which is especially important against the language
17807      flexibilty wrt arrays of variable size.  */
17808
17809   bool collapse_nested_arrays = !is_ada ();
17810   tree element_type;
17811
17812   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
17813      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
17814   if (TYPE_STRING_FLAG (type)
17815       && TREE_CODE (type) == ARRAY_TYPE
17816       && is_fortran ()
17817       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
17818     {
17819       HOST_WIDE_INT size;
17820
17821       array_die = new_die (DW_TAG_string_type, scope_die, type);
17822       add_name_attribute (array_die, type_tag (type));
17823       equate_type_number_to_die (type, array_die);
17824       size = int_size_in_bytes (type);
17825       if (size >= 0)
17826         add_AT_unsigned (array_die, DW_AT_byte_size, size);
17827       else if (TYPE_DOMAIN (type) != NULL_TREE
17828                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
17829                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
17830         {
17831           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
17832           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
17833
17834           size = int_size_in_bytes (TREE_TYPE (szdecl));
17835           if (loc && size > 0)
17836             {
17837               add_AT_location_description (array_die, DW_AT_string_length, loc);
17838               if (size != DWARF2_ADDR_SIZE)
17839                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
17840             }
17841         }
17842       return;
17843     }
17844
17845   /* ??? The SGI dwarf reader fails for array of array of enum types
17846      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
17847      array type comes before the outer array type.  We thus call gen_type_die
17848      before we new_die and must prevent nested array types collapsing for this
17849      target.  */
17850
17851 #ifdef MIPS_DEBUGGING_INFO
17852   gen_type_die (TREE_TYPE (type), context_die);
17853   collapse_nested_arrays = false;
17854 #endif
17855
17856   array_die = new_die (DW_TAG_array_type, scope_die, type);
17857   add_name_attribute (array_die, type_tag (type));
17858   equate_type_number_to_die (type, array_die);
17859
17860   if (TREE_CODE (type) == VECTOR_TYPE)
17861     {
17862       /* The frontend feeds us a representation for the vector as a struct
17863          containing an array.  Pull out the array type.  */
17864       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
17865       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
17866     }
17867
17868   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
17869   if (is_fortran ()
17870       && TREE_CODE (type) == ARRAY_TYPE
17871       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
17872       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
17873     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
17874
17875 #if 0
17876   /* We default the array ordering.  SDB will probably do
17877      the right things even if DW_AT_ordering is not present.  It's not even
17878      an issue until we start to get into multidimensional arrays anyway.  If
17879      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
17880      then we'll have to put the DW_AT_ordering attribute back in.  (But if
17881      and when we find out that we need to put these in, we will only do so
17882      for multidimensional arrays.  */
17883   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
17884 #endif
17885
17886 #ifdef MIPS_DEBUGGING_INFO
17887   /* The SGI compilers handle arrays of unknown bound by setting
17888      AT_declaration and not emitting any subrange DIEs.  */
17889   if (! TYPE_DOMAIN (type))
17890     add_AT_flag (array_die, DW_AT_declaration, 1);
17891   else
17892 #endif
17893     add_subscript_info (array_die, type, collapse_nested_arrays);
17894
17895   /* Add representation of the type of the elements of this array type and
17896      emit the corresponding DIE if we haven't done it already.  */
17897   element_type = TREE_TYPE (type);
17898   if (collapse_nested_arrays)
17899     while (TREE_CODE (element_type) == ARRAY_TYPE)
17900       {
17901         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
17902           break;
17903         element_type = TREE_TYPE (element_type);
17904       }
17905
17906 #ifndef MIPS_DEBUGGING_INFO
17907   gen_type_die (element_type, context_die);
17908 #endif
17909
17910   add_type_attribute (array_die, element_type, 0, 0, context_die);
17911
17912   if (get_AT (array_die, DW_AT_name))
17913     add_pubtype (type, array_die);
17914 }
17915
17916 static dw_loc_descr_ref
17917 descr_info_loc (tree val, tree base_decl)
17918 {
17919   HOST_WIDE_INT size;
17920   dw_loc_descr_ref loc, loc2;
17921   enum dwarf_location_atom op;
17922
17923   if (val == base_decl)
17924     return new_loc_descr (DW_OP_push_object_address, 0, 0);
17925
17926   switch (TREE_CODE (val))
17927     {
17928     CASE_CONVERT:
17929       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17930     case VAR_DECL:
17931       return loc_descriptor_from_tree (val, 0);
17932     case INTEGER_CST:
17933       if (host_integerp (val, 0))
17934         return int_loc_descriptor (tree_low_cst (val, 0));
17935       break;
17936     case INDIRECT_REF:
17937       size = int_size_in_bytes (TREE_TYPE (val));
17938       if (size < 0)
17939         break;
17940       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17941       if (!loc)
17942         break;
17943       if (size == DWARF2_ADDR_SIZE)
17944         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
17945       else
17946         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
17947       return loc;
17948     case POINTER_PLUS_EXPR:
17949     case PLUS_EXPR:
17950       if (host_integerp (TREE_OPERAND (val, 1), 1)
17951           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
17952              < 16384)
17953         {
17954           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17955           if (!loc)
17956             break;
17957           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
17958         }
17959       else
17960         {
17961           op = DW_OP_plus;
17962         do_binop:
17963           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
17964           if (!loc)
17965             break;
17966           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
17967           if (!loc2)
17968             break;
17969           add_loc_descr (&loc, loc2);
17970           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
17971         }
17972       return loc;
17973     case MINUS_EXPR:
17974       op = DW_OP_minus;
17975       goto do_binop;
17976     case MULT_EXPR:
17977       op = DW_OP_mul;
17978       goto do_binop;
17979     case EQ_EXPR:
17980       op = DW_OP_eq;
17981       goto do_binop;
17982     case NE_EXPR:
17983       op = DW_OP_ne;
17984       goto do_binop;
17985     default:
17986       break;
17987     }
17988   return NULL;
17989 }
17990
17991 static void
17992 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
17993                       tree val, tree base_decl)
17994 {
17995   dw_loc_descr_ref loc;
17996
17997   if (host_integerp (val, 0))
17998     {
17999       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
18000       return;
18001     }
18002
18003   loc = descr_info_loc (val, base_decl);
18004   if (!loc)
18005     return;
18006
18007   add_AT_loc (die, attr, loc);
18008 }
18009
18010 /* This routine generates DIE for array with hidden descriptor, details
18011    are filled into *info by a langhook.  */
18012
18013 static void
18014 gen_descr_array_type_die (tree type, struct array_descr_info *info,
18015                           dw_die_ref context_die)
18016 {
18017   dw_die_ref scope_die = scope_die_for (type, context_die);
18018   dw_die_ref array_die;
18019   int dim;
18020
18021   array_die = new_die (DW_TAG_array_type, scope_die, type);
18022   add_name_attribute (array_die, type_tag (type));
18023   equate_type_number_to_die (type, array_die);
18024
18025   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
18026   if (is_fortran ()
18027       && info->ndimensions >= 2)
18028     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
18029
18030   if (info->data_location)
18031     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
18032                           info->base_decl);
18033   if (info->associated)
18034     add_descr_info_field (array_die, DW_AT_associated, info->associated,
18035                           info->base_decl);
18036   if (info->allocated)
18037     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
18038                           info->base_decl);
18039
18040   for (dim = 0; dim < info->ndimensions; dim++)
18041     {
18042       dw_die_ref subrange_die
18043         = new_die (DW_TAG_subrange_type, array_die, NULL);
18044
18045       if (info->dimen[dim].lower_bound)
18046         {
18047           /* If it is the default value, omit it.  */
18048           int dflt;
18049
18050           if (host_integerp (info->dimen[dim].lower_bound, 0)
18051               && (dflt = lower_bound_default ()) != -1
18052               && tree_low_cst (info->dimen[dim].lower_bound, 0) == dflt)
18053             ;
18054           else
18055             add_descr_info_field (subrange_die, DW_AT_lower_bound,
18056                                   info->dimen[dim].lower_bound,
18057                                   info->base_decl);
18058         }
18059       if (info->dimen[dim].upper_bound)
18060         add_descr_info_field (subrange_die, DW_AT_upper_bound,
18061                               info->dimen[dim].upper_bound,
18062                               info->base_decl);
18063       if (info->dimen[dim].stride)
18064         add_descr_info_field (subrange_die, DW_AT_byte_stride,
18065                               info->dimen[dim].stride,
18066                               info->base_decl);
18067     }
18068
18069   gen_type_die (info->element_type, context_die);
18070   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
18071
18072   if (get_AT (array_die, DW_AT_name))
18073     add_pubtype (type, array_die);
18074 }
18075
18076 #if 0
18077 static void
18078 gen_entry_point_die (tree decl, dw_die_ref context_die)
18079 {
18080   tree origin = decl_ultimate_origin (decl);
18081   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
18082
18083   if (origin != NULL)
18084     add_abstract_origin_attribute (decl_die, origin);
18085   else
18086     {
18087       add_name_and_src_coords_attributes (decl_die, decl);
18088       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
18089                           0, 0, context_die);
18090     }
18091
18092   if (DECL_ABSTRACT (decl))
18093     equate_decl_number_to_die (decl, decl_die);
18094   else
18095     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
18096 }
18097 #endif
18098
18099 /* Walk through the list of incomplete types again, trying once more to
18100    emit full debugging info for them.  */
18101
18102 static void
18103 retry_incomplete_types (void)
18104 {
18105   int i;
18106
18107   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
18108     if (should_emit_struct_debug (VEC_index (tree, incomplete_types, i),
18109                                   DINFO_USAGE_DIR_USE))
18110       gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
18111 }
18112
18113 /* Determine what tag to use for a record type.  */
18114
18115 static enum dwarf_tag
18116 record_type_tag (tree type)
18117 {
18118   if (! lang_hooks.types.classify_record)
18119     return DW_TAG_structure_type;
18120
18121   switch (lang_hooks.types.classify_record (type))
18122     {
18123     case RECORD_IS_STRUCT:
18124       return DW_TAG_structure_type;
18125
18126     case RECORD_IS_CLASS:
18127       return DW_TAG_class_type;
18128
18129     case RECORD_IS_INTERFACE:
18130       if (dwarf_version >= 3 || !dwarf_strict)
18131         return DW_TAG_interface_type;
18132       return DW_TAG_structure_type;
18133
18134     default:
18135       gcc_unreachable ();
18136     }
18137 }
18138
18139 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
18140    include all of the information about the enumeration values also. Each
18141    enumerated type name/value is listed as a child of the enumerated type
18142    DIE.  */
18143
18144 static dw_die_ref
18145 gen_enumeration_type_die (tree type, dw_die_ref context_die)
18146 {
18147   dw_die_ref type_die = lookup_type_die (type);
18148
18149   if (type_die == NULL)
18150     {
18151       type_die = new_die (DW_TAG_enumeration_type,
18152                           scope_die_for (type, context_die), type);
18153       equate_type_number_to_die (type, type_die);
18154       add_name_attribute (type_die, type_tag (type));
18155       if ((dwarf_version >= 4 || !dwarf_strict)
18156           && ENUM_IS_SCOPED (type))
18157         add_AT_flag (type_die, DW_AT_enum_class, 1);
18158     }
18159   else if (! TYPE_SIZE (type))
18160     return type_die;
18161   else
18162     remove_AT (type_die, DW_AT_declaration);
18163
18164   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
18165      given enum type is incomplete, do not generate the DW_AT_byte_size
18166      attribute or the DW_AT_element_list attribute.  */
18167   if (TYPE_SIZE (type))
18168     {
18169       tree link;
18170
18171       TREE_ASM_WRITTEN (type) = 1;
18172       add_byte_size_attribute (type_die, type);
18173       if (TYPE_STUB_DECL (type) != NULL_TREE)
18174         {
18175           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
18176           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
18177         }
18178
18179       /* If the first reference to this type was as the return type of an
18180          inline function, then it may not have a parent.  Fix this now.  */
18181       if (type_die->die_parent == NULL)
18182         add_child_die (scope_die_for (type, context_die), type_die);
18183
18184       for (link = TYPE_VALUES (type);
18185            link != NULL; link = TREE_CHAIN (link))
18186         {
18187           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
18188           tree value = TREE_VALUE (link);
18189
18190           add_name_attribute (enum_die,
18191                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
18192
18193           if (TREE_CODE (value) == CONST_DECL)
18194             value = DECL_INITIAL (value);
18195
18196           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
18197             /* DWARF2 does not provide a way of indicating whether or
18198                not enumeration constants are signed or unsigned.  GDB
18199                always assumes the values are signed, so we output all
18200                values as if they were signed.  That means that
18201                enumeration constants with very large unsigned values
18202                will appear to have negative values in the debugger.  */
18203             add_AT_int (enum_die, DW_AT_const_value,
18204                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
18205         }
18206     }
18207   else
18208     add_AT_flag (type_die, DW_AT_declaration, 1);
18209
18210   if (get_AT (type_die, DW_AT_name))
18211     add_pubtype (type, type_die);
18212
18213   return type_die;
18214 }
18215
18216 /* Generate a DIE to represent either a real live formal parameter decl or to
18217    represent just the type of some formal parameter position in some function
18218    type.
18219
18220    Note that this routine is a bit unusual because its argument may be a
18221    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
18222    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
18223    node.  If it's the former then this function is being called to output a
18224    DIE to represent a formal parameter object (or some inlining thereof).  If
18225    it's the latter, then this function is only being called to output a
18226    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
18227    argument type of some subprogram type.
18228    If EMIT_NAME_P is true, name and source coordinate attributes
18229    are emitted.  */
18230
18231 static dw_die_ref
18232 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
18233                           dw_die_ref context_die)
18234 {
18235   tree node_or_origin = node ? node : origin;
18236   tree ultimate_origin;
18237   dw_die_ref parm_die
18238     = new_die (DW_TAG_formal_parameter, context_die, node);
18239
18240   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
18241     {
18242     case tcc_declaration:
18243       ultimate_origin = decl_ultimate_origin (node_or_origin);
18244       if (node || ultimate_origin)
18245         origin = ultimate_origin;
18246       if (origin != NULL)
18247         add_abstract_origin_attribute (parm_die, origin);
18248       else if (emit_name_p)
18249         add_name_and_src_coords_attributes (parm_die, node);
18250       if (origin == NULL
18251           || (! DECL_ABSTRACT (node_or_origin)
18252               && variably_modified_type_p (TREE_TYPE (node_or_origin),
18253                                            decl_function_context
18254                                                             (node_or_origin))))
18255         {
18256           tree type = TREE_TYPE (node_or_origin);
18257           if (decl_by_reference_p (node_or_origin))
18258             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
18259                                 context_die);
18260           else
18261             add_type_attribute (parm_die, type,
18262                                 TREE_READONLY (node_or_origin),
18263                                 TREE_THIS_VOLATILE (node_or_origin),
18264                                 context_die);
18265         }
18266       if (origin == NULL && DECL_ARTIFICIAL (node))
18267         add_AT_flag (parm_die, DW_AT_artificial, 1);
18268
18269       if (node && node != origin)
18270         equate_decl_number_to_die (node, parm_die);
18271       if (! DECL_ABSTRACT (node_or_origin))
18272         add_location_or_const_value_attribute (parm_die, node_or_origin,
18273                                                DW_AT_location);
18274
18275       break;
18276
18277     case tcc_type:
18278       /* We were called with some kind of a ..._TYPE node.  */
18279       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
18280       break;
18281
18282     default:
18283       gcc_unreachable ();
18284     }
18285
18286   return parm_die;
18287 }
18288
18289 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
18290    children DW_TAG_formal_parameter DIEs representing the arguments of the
18291    parameter pack.
18292
18293    PARM_PACK must be a function parameter pack.
18294    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
18295    must point to the subsequent arguments of the function PACK_ARG belongs to.
18296    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
18297    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
18298    following the last one for which a DIE was generated.  */
18299
18300 static dw_die_ref
18301 gen_formal_parameter_pack_die  (tree parm_pack,
18302                                 tree pack_arg,
18303                                 dw_die_ref subr_die,
18304                                 tree *next_arg)
18305 {
18306   tree arg;
18307   dw_die_ref parm_pack_die;
18308
18309   gcc_assert (parm_pack
18310               && lang_hooks.function_parameter_pack_p (parm_pack)
18311               && subr_die);
18312
18313   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
18314   add_src_coords_attributes (parm_pack_die, parm_pack);
18315
18316   for (arg = pack_arg; arg; arg = DECL_CHAIN (arg))
18317     {
18318       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
18319                                                                  parm_pack))
18320         break;
18321       gen_formal_parameter_die (arg, NULL,
18322                                 false /* Don't emit name attribute.  */,
18323                                 parm_pack_die);
18324     }
18325   if (next_arg)
18326     *next_arg = arg;
18327   return parm_pack_die;
18328 }
18329
18330 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
18331    at the end of an (ANSI prototyped) formal parameters list.  */
18332
18333 static void
18334 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
18335 {
18336   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
18337 }
18338
18339 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
18340    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
18341    parameters as specified in some function type specification (except for
18342    those which appear as part of a function *definition*).  */
18343
18344 static void
18345 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
18346 {
18347   tree link;
18348   tree formal_type = NULL;
18349   tree first_parm_type;
18350   tree arg;
18351
18352   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
18353     {
18354       arg = DECL_ARGUMENTS (function_or_method_type);
18355       function_or_method_type = TREE_TYPE (function_or_method_type);
18356     }
18357   else
18358     arg = NULL_TREE;
18359
18360   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
18361
18362   /* Make our first pass over the list of formal parameter types and output a
18363      DW_TAG_formal_parameter DIE for each one.  */
18364   for (link = first_parm_type; link; )
18365     {
18366       dw_die_ref parm_die;
18367
18368       formal_type = TREE_VALUE (link);
18369       if (formal_type == void_type_node)
18370         break;
18371
18372       /* Output a (nameless) DIE to represent the formal parameter itself.  */
18373       parm_die = gen_formal_parameter_die (formal_type, NULL,
18374                                            true /* Emit name attribute.  */,
18375                                            context_die);
18376       if (TREE_CODE (function_or_method_type) == METHOD_TYPE
18377           && link == first_parm_type)
18378         {
18379           add_AT_flag (parm_die, DW_AT_artificial, 1);
18380           if (dwarf_version >= 3 || !dwarf_strict)
18381             add_AT_die_ref (context_die, DW_AT_object_pointer, parm_die);
18382         }
18383       else if (arg && DECL_ARTIFICIAL (arg))
18384         add_AT_flag (parm_die, DW_AT_artificial, 1);
18385
18386       link = TREE_CHAIN (link);
18387       if (arg)
18388         arg = DECL_CHAIN (arg);
18389     }
18390
18391   /* If this function type has an ellipsis, add a
18392      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
18393   if (formal_type != void_type_node)
18394     gen_unspecified_parameters_die (function_or_method_type, context_die);
18395
18396   /* Make our second (and final) pass over the list of formal parameter types
18397      and output DIEs to represent those types (as necessary).  */
18398   for (link = TYPE_ARG_TYPES (function_or_method_type);
18399        link && TREE_VALUE (link);
18400        link = TREE_CHAIN (link))
18401     gen_type_die (TREE_VALUE (link), context_die);
18402 }
18403
18404 /* We want to generate the DIE for TYPE so that we can generate the
18405    die for MEMBER, which has been defined; we will need to refer back
18406    to the member declaration nested within TYPE.  If we're trying to
18407    generate minimal debug info for TYPE, processing TYPE won't do the
18408    trick; we need to attach the member declaration by hand.  */
18409
18410 static void
18411 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
18412 {
18413   gen_type_die (type, context_die);
18414
18415   /* If we're trying to avoid duplicate debug info, we may not have
18416      emitted the member decl for this function.  Emit it now.  */
18417   if (TYPE_STUB_DECL (type)
18418       && TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
18419       && ! lookup_decl_die (member))
18420     {
18421       dw_die_ref type_die;
18422       gcc_assert (!decl_ultimate_origin (member));
18423
18424       push_decl_scope (type);
18425       type_die = lookup_type_die (type);
18426       if (TREE_CODE (member) == FUNCTION_DECL)
18427         gen_subprogram_die (member, type_die);
18428       else if (TREE_CODE (member) == FIELD_DECL)
18429         {
18430           /* Ignore the nameless fields that are used to skip bits but handle
18431              C++ anonymous unions and structs.  */
18432           if (DECL_NAME (member) != NULL_TREE
18433               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
18434               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
18435             {
18436               gen_type_die (member_declared_type (member), type_die);
18437               gen_field_die (member, type_die);
18438             }
18439         }
18440       else
18441         gen_variable_die (member, NULL_TREE, type_die);
18442
18443       pop_decl_scope ();
18444     }
18445 }
18446
18447 /* Generate the DWARF2 info for the "abstract" instance of a function which we
18448    may later generate inlined and/or out-of-line instances of.  */
18449
18450 static void
18451 dwarf2out_abstract_function (tree decl)
18452 {
18453   dw_die_ref old_die;
18454   tree save_fn;
18455   tree context;
18456   int was_abstract;
18457   htab_t old_decl_loc_table;
18458
18459   /* Make sure we have the actual abstract inline, not a clone.  */
18460   decl = DECL_ORIGIN (decl);
18461
18462   old_die = lookup_decl_die (decl);
18463   if (old_die && get_AT (old_die, DW_AT_inline))
18464     /* We've already generated the abstract instance.  */
18465     return;
18466
18467   /* We can be called while recursively when seeing block defining inlined subroutine
18468      DIE.  Be sure to not clobber the outer location table nor use it or we would
18469      get locations in abstract instantces.  */
18470   old_decl_loc_table = decl_loc_table;
18471   decl_loc_table = NULL;
18472
18473   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
18474      we don't get confused by DECL_ABSTRACT.  */
18475   if (debug_info_level > DINFO_LEVEL_TERSE)
18476     {
18477       context = decl_class_context (decl);
18478       if (context)
18479         gen_type_die_for_member
18480           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
18481     }
18482
18483   /* Pretend we've just finished compiling this function.  */
18484   save_fn = current_function_decl;
18485   current_function_decl = decl;
18486   push_cfun (DECL_STRUCT_FUNCTION (decl));
18487
18488   was_abstract = DECL_ABSTRACT (decl);
18489   set_decl_abstract_flags (decl, 1);
18490   dwarf2out_decl (decl);
18491   if (! was_abstract)
18492     set_decl_abstract_flags (decl, 0);
18493
18494   current_function_decl = save_fn;
18495   decl_loc_table = old_decl_loc_table;
18496   pop_cfun ();
18497 }
18498
18499 /* Helper function of premark_used_types() which gets called through
18500    htab_traverse.
18501
18502    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18503    marked as unused by prune_unused_types.  */
18504
18505 static int
18506 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
18507 {
18508   tree type;
18509   dw_die_ref die;
18510
18511   type = (tree) *slot;
18512   die = lookup_type_die (type);
18513   if (die != NULL)
18514     die->die_perennial_p = 1;
18515   return 1;
18516 }
18517
18518 /* Helper function of premark_types_used_by_global_vars which gets called
18519    through htab_traverse.
18520
18521    Marks the DIE of a given type in *SLOT as perennial, so it never gets
18522    marked as unused by prune_unused_types. The DIE of the type is marked
18523    only if the global variable using the type will actually be emitted.  */
18524
18525 static int
18526 premark_types_used_by_global_vars_helper (void **slot,
18527                                           void *data ATTRIBUTE_UNUSED)
18528 {
18529   struct types_used_by_vars_entry *entry;
18530   dw_die_ref die;
18531
18532   entry = (struct types_used_by_vars_entry *) *slot;
18533   gcc_assert (entry->type != NULL
18534               && entry->var_decl != NULL);
18535   die = lookup_type_die (entry->type);
18536   if (die)
18537     {
18538       /* Ask cgraph if the global variable really is to be emitted.
18539          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
18540       struct varpool_node *node = varpool_get_node (entry->var_decl);
18541       if (node && node->needed)
18542         {
18543           die->die_perennial_p = 1;
18544           /* Keep the parent DIEs as well.  */
18545           while ((die = die->die_parent) && die->die_perennial_p == 0)
18546             die->die_perennial_p = 1;
18547         }
18548     }
18549   return 1;
18550 }
18551
18552 /* Mark all members of used_types_hash as perennial.  */
18553
18554 static void
18555 premark_used_types (void)
18556 {
18557   if (cfun && cfun->used_types_hash)
18558     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
18559 }
18560
18561 /* Mark all members of types_used_by_vars_entry as perennial.  */
18562
18563 static void
18564 premark_types_used_by_global_vars (void)
18565 {
18566   if (types_used_by_vars_hash)
18567     htab_traverse (types_used_by_vars_hash,
18568                    premark_types_used_by_global_vars_helper, NULL);
18569 }
18570
18571 /* Generate a DIE to represent a declared function (either file-scope or
18572    block-local).  */
18573
18574 static void
18575 gen_subprogram_die (tree decl, dw_die_ref context_die)
18576 {
18577   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
18578   tree origin = decl_ultimate_origin (decl);
18579   dw_die_ref subr_die;
18580   tree fn_arg_types;
18581   tree outer_scope;
18582   dw_die_ref old_die = lookup_decl_die (decl);
18583   int declaration = (current_function_decl != decl
18584                      || class_or_namespace_scope_p (context_die));
18585
18586   premark_used_types ();
18587
18588   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
18589      started to generate the abstract instance of an inline, decided to output
18590      its containing class, and proceeded to emit the declaration of the inline
18591      from the member list for the class.  If so, DECLARATION takes priority;
18592      we'll get back to the abstract instance when done with the class.  */
18593
18594   /* The class-scope declaration DIE must be the primary DIE.  */
18595   if (origin && declaration && class_or_namespace_scope_p (context_die))
18596     {
18597       origin = NULL;
18598       gcc_assert (!old_die);
18599     }
18600
18601   /* Now that the C++ front end lazily declares artificial member fns, we
18602      might need to retrofit the declaration into its class.  */
18603   if (!declaration && !origin && !old_die
18604       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
18605       && !class_or_namespace_scope_p (context_die)
18606       && debug_info_level > DINFO_LEVEL_TERSE)
18607     old_die = force_decl_die (decl);
18608
18609   if (origin != NULL)
18610     {
18611       gcc_assert (!declaration || local_scope_p (context_die));
18612
18613       /* Fixup die_parent for the abstract instance of a nested
18614          inline function.  */
18615       if (old_die && old_die->die_parent == NULL)
18616         add_child_die (context_die, old_die);
18617
18618       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18619       add_abstract_origin_attribute (subr_die, origin);
18620     }
18621   else if (old_die)
18622     {
18623       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
18624       struct dwarf_file_data * file_index = lookup_filename (s.file);
18625
18626       if (!get_AT_flag (old_die, DW_AT_declaration)
18627           /* We can have a normal definition following an inline one in the
18628              case of redefinition of GNU C extern inlines.
18629              It seems reasonable to use AT_specification in this case.  */
18630           && !get_AT (old_die, DW_AT_inline))
18631         {
18632           /* Detect and ignore this case, where we are trying to output
18633              something we have already output.  */
18634           return;
18635         }
18636
18637       /* If the definition comes from the same place as the declaration,
18638          maybe use the old DIE.  We always want the DIE for this function
18639          that has the *_pc attributes to be under comp_unit_die so the
18640          debugger can find it.  We also need to do this for abstract
18641          instances of inlines, since the spec requires the out-of-line copy
18642          to have the same parent.  For local class methods, this doesn't
18643          apply; we just use the old DIE.  */
18644       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
18645           && (DECL_ARTIFICIAL (decl)
18646               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
18647                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
18648                       == (unsigned) s.line))))
18649         {
18650           subr_die = old_die;
18651
18652           /* Clear out the declaration attribute and the formal parameters.
18653              Do not remove all children, because it is possible that this
18654              declaration die was forced using force_decl_die(). In such
18655              cases die that forced declaration die (e.g. TAG_imported_module)
18656              is one of the children that we do not want to remove.  */
18657           remove_AT (subr_die, DW_AT_declaration);
18658           remove_AT (subr_die, DW_AT_object_pointer);
18659           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
18660         }
18661       else
18662         {
18663           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18664           add_AT_specification (subr_die, old_die);
18665           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
18666             add_AT_file (subr_die, DW_AT_decl_file, file_index);
18667           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
18668             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
18669         }
18670     }
18671   else
18672     {
18673       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
18674
18675       if (TREE_PUBLIC (decl))
18676         add_AT_flag (subr_die, DW_AT_external, 1);
18677
18678       add_name_and_src_coords_attributes (subr_die, decl);
18679       if (debug_info_level > DINFO_LEVEL_TERSE)
18680         {
18681           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
18682           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
18683                               0, 0, context_die);
18684         }
18685
18686       add_pure_or_virtual_attribute (subr_die, decl);
18687       if (DECL_ARTIFICIAL (decl))
18688         add_AT_flag (subr_die, DW_AT_artificial, 1);
18689
18690       add_accessibility_attribute (subr_die, decl);
18691     }
18692
18693   if (declaration)
18694     {
18695       if (!old_die || !get_AT (old_die, DW_AT_inline))
18696         {
18697           add_AT_flag (subr_die, DW_AT_declaration, 1);
18698
18699           /* If this is an explicit function declaration then generate
18700              a DW_AT_explicit attribute.  */
18701           if (lang_hooks.decls.function_decl_explicit_p (decl)
18702               && (dwarf_version >= 3 || !dwarf_strict))
18703             add_AT_flag (subr_die, DW_AT_explicit, 1);
18704
18705           /* The first time we see a member function, it is in the context of
18706              the class to which it belongs.  We make sure of this by emitting
18707              the class first.  The next time is the definition, which is
18708              handled above.  The two may come from the same source text.
18709
18710              Note that force_decl_die() forces function declaration die. It is
18711              later reused to represent definition.  */
18712           equate_decl_number_to_die (decl, subr_die);
18713         }
18714     }
18715   else if (DECL_ABSTRACT (decl))
18716     {
18717       if (DECL_DECLARED_INLINE_P (decl))
18718         {
18719           if (cgraph_function_possibly_inlined_p (decl))
18720             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
18721           else
18722             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
18723         }
18724       else
18725         {
18726           if (cgraph_function_possibly_inlined_p (decl))
18727             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
18728           else
18729             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
18730         }
18731
18732       if (DECL_DECLARED_INLINE_P (decl)
18733           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
18734         add_AT_flag (subr_die, DW_AT_artificial, 1);
18735
18736       equate_decl_number_to_die (decl, subr_die);
18737     }
18738   else if (!DECL_EXTERNAL (decl))
18739     {
18740       HOST_WIDE_INT cfa_fb_offset;
18741
18742       if (!old_die || !get_AT (old_die, DW_AT_inline))
18743         equate_decl_number_to_die (decl, subr_die);
18744
18745       if (!flag_reorder_blocks_and_partition)
18746         {
18747           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
18748                                        current_function_funcdef_no);
18749           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
18750           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
18751                                        current_function_funcdef_no);
18752           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
18753
18754 #if VMS_DEBUGGING_INFO
18755       /* HP OpenVMS Industry Standard 64: DWARF Extensions
18756          Section 2.3 Prologue and Epilogue Attributes:
18757          When a breakpoint is set on entry to a function, it is generally
18758          desirable for execution to be suspended, not on the very first
18759          instruction of the function, but rather at a point after the
18760          function's frame has been set up, after any language defined local
18761          declaration processing has been completed, and before execution of
18762          the first statement of the function begins. Debuggers generally
18763          cannot properly determine where this point is.  Similarly for a
18764          breakpoint set on exit from a function. The prologue and epilogue
18765          attributes allow a compiler to communicate the location(s) to use.  */
18766
18767       {
18768         dw_fde_ref fde = &fde_table[current_funcdef_fde];
18769
18770         if (fde->dw_fde_vms_end_prologue)
18771           add_AT_vms_delta (subr_die, DW_AT_HP_prologue,
18772             fde->dw_fde_begin, fde->dw_fde_vms_end_prologue);
18773
18774         if (fde->dw_fde_vms_begin_epilogue)
18775           add_AT_vms_delta (subr_die, DW_AT_HP_epilogue,
18776             fde->dw_fde_begin, fde->dw_fde_vms_begin_epilogue);
18777       }
18778 #endif
18779
18780           add_pubname (decl, subr_die);
18781           add_arange (decl, subr_die);
18782         }
18783       else
18784         {  /* Do nothing for now; maybe need to duplicate die, one for
18785               hot section and one for cold section, then use the hot/cold
18786               section begin/end labels to generate the aranges...  */
18787           /*
18788             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
18789             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
18790             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
18791             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
18792
18793             add_pubname (decl, subr_die);
18794             add_arange (decl, subr_die);
18795             add_arange (decl, subr_die);
18796            */
18797         }
18798
18799 #ifdef MIPS_DEBUGGING_INFO
18800       /* Add a reference to the FDE for this routine.  */
18801       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
18802 #endif
18803
18804       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
18805
18806       /* We define the "frame base" as the function's CFA.  This is more
18807          convenient for several reasons: (1) It's stable across the prologue
18808          and epilogue, which makes it better than just a frame pointer,
18809          (2) With dwarf3, there exists a one-byte encoding that allows us
18810          to reference the .debug_frame data by proxy, but failing that,
18811          (3) We can at least reuse the code inspection and interpretation
18812          code that determines the CFA position at various points in the
18813          function.  */
18814       if (dwarf_version >= 3)
18815         {
18816           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
18817           add_AT_loc (subr_die, DW_AT_frame_base, op);
18818         }
18819       else
18820         {
18821           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
18822           if (list->dw_loc_next)
18823             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
18824           else
18825             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
18826         }
18827
18828       /* Compute a displacement from the "steady-state frame pointer" to
18829          the CFA.  The former is what all stack slots and argument slots
18830          will reference in the rtl; the later is what we've told the
18831          debugger about.  We'll need to adjust all frame_base references
18832          by this displacement.  */
18833       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
18834
18835       if (cfun->static_chain_decl)
18836         add_AT_location_description (subr_die, DW_AT_static_link,
18837                  loc_list_from_tree (cfun->static_chain_decl, 2));
18838     }
18839
18840   /* Generate child dies for template paramaters.  */
18841   if (debug_info_level > DINFO_LEVEL_TERSE)
18842     gen_generic_params_dies (decl);
18843
18844   /* Now output descriptions of the arguments for this function. This gets
18845      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
18846      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
18847      `...' at the end of the formal parameter list.  In order to find out if
18848      there was a trailing ellipsis or not, we must instead look at the type
18849      associated with the FUNCTION_DECL.  This will be a node of type
18850      FUNCTION_TYPE. If the chain of type nodes hanging off of this
18851      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
18852      an ellipsis at the end.  */
18853
18854   /* In the case where we are describing a mere function declaration, all we
18855      need to do here (and all we *can* do here) is to describe the *types* of
18856      its formal parameters.  */
18857   if (debug_info_level <= DINFO_LEVEL_TERSE)
18858     ;
18859   else if (declaration)
18860     gen_formal_types_die (decl, subr_die);
18861   else
18862     {
18863       /* Generate DIEs to represent all known formal parameters.  */
18864       tree parm = DECL_ARGUMENTS (decl);
18865       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
18866       tree generic_decl_parm = generic_decl
18867                                 ? DECL_ARGUMENTS (generic_decl)
18868                                 : NULL;
18869
18870       /* Now we want to walk the list of parameters of the function and
18871          emit their relevant DIEs.
18872
18873          We consider the case of DECL being an instance of a generic function
18874          as well as it being a normal function.
18875
18876          If DECL is an instance of a generic function we walk the
18877          parameters of the generic function declaration _and_ the parameters of
18878          DECL itself. This is useful because we want to emit specific DIEs for
18879          function parameter packs and those are declared as part of the
18880          generic function declaration. In that particular case,
18881          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
18882          That DIE has children DIEs representing the set of arguments
18883          of the pack. Note that the set of pack arguments can be empty.
18884          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
18885          children DIE.
18886
18887          Otherwise, we just consider the parameters of DECL.  */
18888       while (generic_decl_parm || parm)
18889         {
18890           if (generic_decl_parm
18891               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
18892             gen_formal_parameter_pack_die (generic_decl_parm,
18893                                            parm, subr_die,
18894                                            &parm);
18895           else if (parm)
18896             {
18897               dw_die_ref parm_die = gen_decl_die (parm, NULL, subr_die);
18898
18899               if (parm == DECL_ARGUMENTS (decl)
18900                   && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE
18901                   && parm_die
18902                   && (dwarf_version >= 3 || !dwarf_strict))
18903                 add_AT_die_ref (subr_die, DW_AT_object_pointer, parm_die);
18904
18905               parm = DECL_CHAIN (parm);
18906             }
18907
18908           if (generic_decl_parm)
18909             generic_decl_parm = DECL_CHAIN (generic_decl_parm);
18910         }
18911
18912       /* Decide whether we need an unspecified_parameters DIE at the end.
18913          There are 2 more cases to do this for: 1) the ansi ... declaration -
18914          this is detectable when the end of the arg list is not a
18915          void_type_node 2) an unprototyped function declaration (not a
18916          definition).  This just means that we have no info about the
18917          parameters at all.  */
18918       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
18919       if (fn_arg_types != NULL)
18920         {
18921           /* This is the prototyped case, check for....  */
18922           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
18923             gen_unspecified_parameters_die (decl, subr_die);
18924         }
18925       else if (DECL_INITIAL (decl) == NULL_TREE)
18926         gen_unspecified_parameters_die (decl, subr_die);
18927     }
18928
18929   /* Output Dwarf info for all of the stuff within the body of the function
18930      (if it has one - it may be just a declaration).  */
18931   outer_scope = DECL_INITIAL (decl);
18932
18933   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
18934      a function.  This BLOCK actually represents the outermost binding contour
18935      for the function, i.e. the contour in which the function's formal
18936      parameters and labels get declared. Curiously, it appears that the front
18937      end doesn't actually put the PARM_DECL nodes for the current function onto
18938      the BLOCK_VARS list for this outer scope, but are strung off of the
18939      DECL_ARGUMENTS list for the function instead.
18940
18941      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
18942      the LABEL_DECL nodes for the function however, and we output DWARF info
18943      for those in decls_for_scope.  Just within the `outer_scope' there will be
18944      a BLOCK node representing the function's outermost pair of curly braces,
18945      and any blocks used for the base and member initializers of a C++
18946      constructor function.  */
18947   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
18948     {
18949       /* Emit a DW_TAG_variable DIE for a named return value.  */
18950       if (DECL_NAME (DECL_RESULT (decl)))
18951         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
18952
18953       current_function_has_inlines = 0;
18954       decls_for_scope (outer_scope, subr_die, 0);
18955
18956 #if 0 && defined (MIPS_DEBUGGING_INFO)
18957       if (current_function_has_inlines)
18958         {
18959           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
18960           if (! comp_unit_has_inlines)
18961             {
18962               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
18963               comp_unit_has_inlines = 1;
18964             }
18965         }
18966 #endif
18967     }
18968   /* Add the calling convention attribute if requested.  */
18969   add_calling_convention_attribute (subr_die, decl);
18970
18971 }
18972
18973 /* Returns a hash value for X (which really is a die_struct).  */
18974
18975 static hashval_t
18976 common_block_die_table_hash (const void *x)
18977 {
18978   const_dw_die_ref d = (const_dw_die_ref) x;
18979   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
18980 }
18981
18982 /* Return nonzero if decl_id and die_parent of die_struct X is the same
18983    as decl_id and die_parent of die_struct Y.  */
18984
18985 static int
18986 common_block_die_table_eq (const void *x, const void *y)
18987 {
18988   const_dw_die_ref d = (const_dw_die_ref) x;
18989   const_dw_die_ref e = (const_dw_die_ref) y;
18990   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
18991 }
18992
18993 /* Generate a DIE to represent a declared data object.
18994    Either DECL or ORIGIN must be non-null.  */
18995
18996 static void
18997 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
18998 {
18999   HOST_WIDE_INT off;
19000   tree com_decl;
19001   tree decl_or_origin = decl ? decl : origin;
19002   tree ultimate_origin;
19003   dw_die_ref var_die;
19004   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
19005   dw_die_ref origin_die;
19006   bool declaration = (DECL_EXTERNAL (decl_or_origin)
19007                       || class_or_namespace_scope_p (context_die));
19008   bool specialization_p = false;
19009
19010   ultimate_origin = decl_ultimate_origin (decl_or_origin);
19011   if (decl || ultimate_origin)
19012     origin = ultimate_origin;
19013   com_decl = fortran_common (decl_or_origin, &off);
19014
19015   /* Symbol in common gets emitted as a child of the common block, in the form
19016      of a data member.  */
19017   if (com_decl)
19018     {
19019       dw_die_ref com_die;
19020       dw_loc_list_ref loc;
19021       die_node com_die_arg;
19022
19023       var_die = lookup_decl_die (decl_or_origin);
19024       if (var_die)
19025         {
19026           if (get_AT (var_die, DW_AT_location) == NULL)
19027             {
19028               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
19029               if (loc)
19030                 {
19031                   if (off)
19032                     {
19033                       /* Optimize the common case.  */
19034                       if (single_element_loc_list_p (loc)
19035                           && loc->expr->dw_loc_opc == DW_OP_addr
19036                           && loc->expr->dw_loc_next == NULL
19037                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
19038                              == SYMBOL_REF)
19039                         loc->expr->dw_loc_oprnd1.v.val_addr
19040                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19041                         else
19042                           loc_list_plus_const (loc, off);
19043                     }
19044                   add_AT_location_description (var_die, DW_AT_location, loc);
19045                   remove_AT (var_die, DW_AT_declaration);
19046                 }
19047             }
19048           return;
19049         }
19050
19051       if (common_block_die_table == NULL)
19052         common_block_die_table
19053           = htab_create_ggc (10, common_block_die_table_hash,
19054                              common_block_die_table_eq, NULL);
19055
19056       com_die_arg.decl_id = DECL_UID (com_decl);
19057       com_die_arg.die_parent = context_die;
19058       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
19059       loc = loc_list_from_tree (com_decl, 2);
19060       if (com_die == NULL)
19061         {
19062           const char *cnam
19063             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
19064           void **slot;
19065
19066           com_die = new_die (DW_TAG_common_block, context_die, decl);
19067           add_name_and_src_coords_attributes (com_die, com_decl);
19068           if (loc)
19069             {
19070               add_AT_location_description (com_die, DW_AT_location, loc);
19071               /* Avoid sharing the same loc descriptor between
19072                  DW_TAG_common_block and DW_TAG_variable.  */
19073               loc = loc_list_from_tree (com_decl, 2);
19074             }
19075           else if (DECL_EXTERNAL (decl))
19076             add_AT_flag (com_die, DW_AT_declaration, 1);
19077           add_pubname_string (cnam, com_die); /* ??? needed? */
19078           com_die->decl_id = DECL_UID (com_decl);
19079           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
19080           *slot = (void *) com_die;
19081         }
19082       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
19083         {
19084           add_AT_location_description (com_die, DW_AT_location, loc);
19085           loc = loc_list_from_tree (com_decl, 2);
19086           remove_AT (com_die, DW_AT_declaration);
19087         }
19088       var_die = new_die (DW_TAG_variable, com_die, decl);
19089       add_name_and_src_coords_attributes (var_die, decl);
19090       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
19091                           TREE_THIS_VOLATILE (decl), context_die);
19092       add_AT_flag (var_die, DW_AT_external, 1);
19093       if (loc)
19094         {
19095           if (off)
19096             {
19097               /* Optimize the common case.  */
19098               if (single_element_loc_list_p (loc)
19099                   && loc->expr->dw_loc_opc == DW_OP_addr
19100                   && loc->expr->dw_loc_next == NULL
19101                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
19102                 loc->expr->dw_loc_oprnd1.v.val_addr
19103                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
19104               else
19105                 loc_list_plus_const (loc, off);
19106             }
19107           add_AT_location_description (var_die, DW_AT_location, loc);
19108         }
19109       else if (DECL_EXTERNAL (decl))
19110         add_AT_flag (var_die, DW_AT_declaration, 1);
19111       equate_decl_number_to_die (decl, var_die);
19112       return;
19113     }
19114
19115   /* If the compiler emitted a definition for the DECL declaration
19116      and if we already emitted a DIE for it, don't emit a second
19117      DIE for it again. Allow re-declarations of DECLs that are
19118      inside functions, though.  */
19119   if (old_die && declaration && !local_scope_p (context_die))
19120     return;
19121
19122   /* For static data members, the declaration in the class is supposed
19123      to have DW_TAG_member tag; the specification should still be
19124      DW_TAG_variable referencing the DW_TAG_member DIE.  */
19125   if (declaration && class_scope_p (context_die))
19126     var_die = new_die (DW_TAG_member, context_die, decl);
19127   else
19128     var_die = new_die (DW_TAG_variable, context_die, decl);
19129
19130   origin_die = NULL;
19131   if (origin != NULL)
19132     origin_die = add_abstract_origin_attribute (var_die, origin);
19133
19134   /* Loop unrolling can create multiple blocks that refer to the same
19135      static variable, so we must test for the DW_AT_declaration flag.
19136
19137      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
19138      copy decls and set the DECL_ABSTRACT flag on them instead of
19139      sharing them.
19140
19141      ??? Duplicated blocks have been rewritten to use .debug_ranges.
19142
19143      ??? The declare_in_namespace support causes us to get two DIEs for one
19144      variable, both of which are declarations.  We want to avoid considering
19145      one to be a specification, so we must test that this DIE is not a
19146      declaration.  */
19147   else if (old_die && TREE_STATIC (decl) && ! declaration
19148            && get_AT_flag (old_die, DW_AT_declaration) == 1)
19149     {
19150       /* This is a definition of a C++ class level static.  */
19151       add_AT_specification (var_die, old_die);
19152       specialization_p = true;
19153       if (DECL_NAME (decl))
19154         {
19155           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
19156           struct dwarf_file_data * file_index = lookup_filename (s.file);
19157
19158           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
19159             add_AT_file (var_die, DW_AT_decl_file, file_index);
19160
19161           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
19162             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
19163
19164           if (old_die->die_tag == DW_TAG_member)
19165             add_linkage_name (var_die, decl);
19166         }
19167     }
19168   else
19169     add_name_and_src_coords_attributes (var_die, decl);
19170
19171   if ((origin == NULL && !specialization_p)
19172       || (origin != NULL
19173           && !DECL_ABSTRACT (decl_or_origin)
19174           && variably_modified_type_p (TREE_TYPE (decl_or_origin),
19175                                        decl_function_context
19176                                                         (decl_or_origin))))
19177     {
19178       tree type = TREE_TYPE (decl_or_origin);
19179
19180       if (decl_by_reference_p (decl_or_origin))
19181         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
19182       else
19183         add_type_attribute (var_die, type, TREE_READONLY (decl_or_origin),
19184                             TREE_THIS_VOLATILE (decl_or_origin), context_die);
19185     }
19186
19187   if (origin == NULL && !specialization_p)
19188     {
19189       if (TREE_PUBLIC (decl))
19190         add_AT_flag (var_die, DW_AT_external, 1);
19191
19192       if (DECL_ARTIFICIAL (decl))
19193         add_AT_flag (var_die, DW_AT_artificial, 1);
19194
19195       add_accessibility_attribute (var_die, decl);
19196     }
19197
19198   if (declaration)
19199     add_AT_flag (var_die, DW_AT_declaration, 1);
19200
19201   if (decl && (DECL_ABSTRACT (decl) || declaration))
19202     equate_decl_number_to_die (decl, var_die);
19203
19204   if (! declaration
19205       && (! DECL_ABSTRACT (decl_or_origin)
19206           /* Local static vars are shared between all clones/inlines,
19207              so emit DW_AT_location on the abstract DIE if DECL_RTL is
19208              already set.  */
19209           || (TREE_CODE (decl_or_origin) == VAR_DECL
19210               && TREE_STATIC (decl_or_origin)
19211               && DECL_RTL_SET_P (decl_or_origin)))
19212       /* When abstract origin already has DW_AT_location attribute, no need
19213          to add it again.  */
19214       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
19215     {
19216       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
19217           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
19218         defer_location (decl_or_origin, var_die);
19219       else
19220         add_location_or_const_value_attribute (var_die,
19221                                                decl_or_origin,
19222                                                DW_AT_location);
19223       add_pubname (decl_or_origin, var_die);
19224     }
19225   else
19226     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
19227 }
19228
19229 /* Generate a DIE to represent a named constant.  */
19230
19231 static void
19232 gen_const_die (tree decl, dw_die_ref context_die)
19233 {
19234   dw_die_ref const_die;
19235   tree type = TREE_TYPE (decl);
19236
19237   const_die = new_die (DW_TAG_constant, context_die, decl);
19238   add_name_and_src_coords_attributes (const_die, decl);
19239   add_type_attribute (const_die, type, 1, 0, context_die);
19240   if (TREE_PUBLIC (decl))
19241     add_AT_flag (const_die, DW_AT_external, 1);
19242   if (DECL_ARTIFICIAL (decl))
19243     add_AT_flag (const_die, DW_AT_artificial, 1);
19244   tree_add_const_value_attribute_for_decl (const_die, decl);
19245 }
19246
19247 /* Generate a DIE to represent a label identifier.  */
19248
19249 static void
19250 gen_label_die (tree decl, dw_die_ref context_die)
19251 {
19252   tree origin = decl_ultimate_origin (decl);
19253   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
19254   rtx insn;
19255   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19256
19257   if (origin != NULL)
19258     add_abstract_origin_attribute (lbl_die, origin);
19259   else
19260     add_name_and_src_coords_attributes (lbl_die, decl);
19261
19262   if (DECL_ABSTRACT (decl))
19263     equate_decl_number_to_die (decl, lbl_die);
19264   else
19265     {
19266       insn = DECL_RTL_IF_SET (decl);
19267
19268       /* Deleted labels are programmer specified labels which have been
19269          eliminated because of various optimizations.  We still emit them
19270          here so that it is possible to put breakpoints on them.  */
19271       if (insn
19272           && (LABEL_P (insn)
19273               || ((NOTE_P (insn)
19274                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
19275         {
19276           /* When optimization is enabled (via -O) some parts of the compiler
19277              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
19278              represent source-level labels which were explicitly declared by
19279              the user.  This really shouldn't be happening though, so catch
19280              it if it ever does happen.  */
19281           gcc_assert (!INSN_DELETED_P (insn));
19282
19283           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
19284           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
19285         }
19286     }
19287 }
19288
19289 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
19290    attributes to the DIE for a block STMT, to describe where the inlined
19291    function was called from.  This is similar to add_src_coords_attributes.  */
19292
19293 static inline void
19294 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
19295 {
19296   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
19297
19298   if (dwarf_version >= 3 || !dwarf_strict)
19299     {
19300       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
19301       add_AT_unsigned (die, DW_AT_call_line, s.line);
19302     }
19303 }
19304
19305
19306 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
19307    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
19308
19309 static inline void
19310 add_high_low_attributes (tree stmt, dw_die_ref die)
19311 {
19312   char label[MAX_ARTIFICIAL_LABEL_BYTES];
19313
19314   if (BLOCK_FRAGMENT_CHAIN (stmt)
19315       && (dwarf_version >= 3 || !dwarf_strict))
19316     {
19317       tree chain;
19318
19319       if (inlined_function_outer_scope_p (stmt))
19320         {
19321           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19322                                        BLOCK_NUMBER (stmt));
19323           add_AT_lbl_id (die, DW_AT_entry_pc, label);
19324         }
19325
19326       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
19327
19328       chain = BLOCK_FRAGMENT_CHAIN (stmt);
19329       do
19330         {
19331           add_ranges (chain);
19332           chain = BLOCK_FRAGMENT_CHAIN (chain);
19333         }
19334       while (chain);
19335       add_ranges (NULL);
19336     }
19337   else
19338     {
19339       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
19340                                    BLOCK_NUMBER (stmt));
19341       add_AT_lbl_id (die, DW_AT_low_pc, label);
19342       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
19343                                    BLOCK_NUMBER (stmt));
19344       add_AT_lbl_id (die, DW_AT_high_pc, label);
19345     }
19346 }
19347
19348 /* Generate a DIE for a lexical block.  */
19349
19350 static void
19351 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
19352 {
19353   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
19354
19355   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
19356     add_high_low_attributes (stmt, stmt_die);
19357
19358   decls_for_scope (stmt, stmt_die, depth);
19359 }
19360
19361 /* Generate a DIE for an inlined subprogram.  */
19362
19363 static void
19364 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
19365 {
19366   tree decl;
19367
19368   /* The instance of function that is effectively being inlined shall not
19369      be abstract.  */
19370   gcc_assert (! BLOCK_ABSTRACT (stmt));
19371
19372   decl = block_ultimate_origin (stmt);
19373
19374   /* Emit info for the abstract instance first, if we haven't yet.  We
19375      must emit this even if the block is abstract, otherwise when we
19376      emit the block below (or elsewhere), we may end up trying to emit
19377      a die whose origin die hasn't been emitted, and crashing.  */
19378   dwarf2out_abstract_function (decl);
19379
19380   if (! BLOCK_ABSTRACT (stmt))
19381     {
19382       dw_die_ref subr_die
19383         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
19384
19385       add_abstract_origin_attribute (subr_die, decl);
19386       if (TREE_ASM_WRITTEN (stmt))
19387         add_high_low_attributes (stmt, subr_die);
19388       add_call_src_coords_attributes (stmt, subr_die);
19389
19390       decls_for_scope (stmt, subr_die, depth);
19391       current_function_has_inlines = 1;
19392     }
19393 }
19394
19395 /* Generate a DIE for a field in a record, or structure.  */
19396
19397 static void
19398 gen_field_die (tree decl, dw_die_ref context_die)
19399 {
19400   dw_die_ref decl_die;
19401
19402   if (TREE_TYPE (decl) == error_mark_node)
19403     return;
19404
19405   decl_die = new_die (DW_TAG_member, context_die, decl);
19406   add_name_and_src_coords_attributes (decl_die, decl);
19407   add_type_attribute (decl_die, member_declared_type (decl),
19408                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
19409                       context_die);
19410
19411   if (DECL_BIT_FIELD_TYPE (decl))
19412     {
19413       add_byte_size_attribute (decl_die, decl);
19414       add_bit_size_attribute (decl_die, decl);
19415       add_bit_offset_attribute (decl_die, decl);
19416     }
19417
19418   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
19419     add_data_member_location_attribute (decl_die, decl);
19420
19421   if (DECL_ARTIFICIAL (decl))
19422     add_AT_flag (decl_die, DW_AT_artificial, 1);
19423
19424   add_accessibility_attribute (decl_die, decl);
19425
19426   /* Equate decl number to die, so that we can look up this decl later on.  */
19427   equate_decl_number_to_die (decl, decl_die);
19428 }
19429
19430 #if 0
19431 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19432    Use modified_type_die instead.
19433    We keep this code here just in case these types of DIEs may be needed to
19434    represent certain things in other languages (e.g. Pascal) someday.  */
19435
19436 static void
19437 gen_pointer_type_die (tree type, dw_die_ref context_die)
19438 {
19439   dw_die_ref ptr_die
19440     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
19441
19442   equate_type_number_to_die (type, ptr_die);
19443   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19444   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19445 }
19446
19447 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
19448    Use modified_type_die instead.
19449    We keep this code here just in case these types of DIEs may be needed to
19450    represent certain things in other languages (e.g. Pascal) someday.  */
19451
19452 static void
19453 gen_reference_type_die (tree type, dw_die_ref context_die)
19454 {
19455   dw_die_ref ref_die, scope_die = scope_die_for (type, context_die);
19456
19457   if (TYPE_REF_IS_RVALUE (type) && dwarf_version >= 4)
19458     ref_die = new_die (DW_TAG_rvalue_reference_type, scope_die, type);
19459   else
19460     ref_die = new_die (DW_TAG_reference_type, scope_die, type);
19461
19462   equate_type_number_to_die (type, ref_die);
19463   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
19464   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
19465 }
19466 #endif
19467
19468 /* Generate a DIE for a pointer to a member type.  */
19469
19470 static void
19471 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
19472 {
19473   dw_die_ref ptr_die
19474     = new_die (DW_TAG_ptr_to_member_type,
19475                scope_die_for (type, context_die), type);
19476
19477   equate_type_number_to_die (type, ptr_die);
19478   add_AT_die_ref (ptr_die, DW_AT_containing_type,
19479                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
19480   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
19481 }
19482
19483 /* Generate the DIE for the compilation unit.  */
19484
19485 static dw_die_ref
19486 gen_compile_unit_die (const char *filename)
19487 {
19488   dw_die_ref die;
19489   char producer[250];
19490   const char *language_string = lang_hooks.name;
19491   int language;
19492
19493   die = new_die (DW_TAG_compile_unit, NULL, NULL);
19494
19495   if (filename)
19496     {
19497       add_name_attribute (die, filename);
19498       /* Don't add cwd for <built-in>.  */
19499       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
19500         add_comp_dir_attribute (die);
19501     }
19502
19503   sprintf (producer, "%s %s", language_string, version_string);
19504
19505 #ifdef MIPS_DEBUGGING_INFO
19506   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
19507      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
19508      not appear in the producer string, the debugger reaches the conclusion
19509      that the object file is stripped and has no debugging information.
19510      To get the MIPS/SGI debugger to believe that there is debugging
19511      information in the object file, we add a -g to the producer string.  */
19512   if (debug_info_level > DINFO_LEVEL_TERSE)
19513     strcat (producer, " -g");
19514 #endif
19515
19516   add_AT_string (die, DW_AT_producer, producer);
19517
19518   language = DW_LANG_C89;
19519   if (strcmp (language_string, "GNU C++") == 0)
19520     language = DW_LANG_C_plus_plus;
19521   else if (strcmp (language_string, "GNU F77") == 0)
19522     language = DW_LANG_Fortran77;
19523   else if (strcmp (language_string, "GNU Pascal") == 0)
19524     language = DW_LANG_Pascal83;
19525   else if (dwarf_version >= 3 || !dwarf_strict)
19526     {
19527       if (strcmp (language_string, "GNU Ada") == 0)
19528         language = DW_LANG_Ada95;
19529       else if (strcmp (language_string, "GNU Fortran") == 0)
19530         language = DW_LANG_Fortran95;
19531       else if (strcmp (language_string, "GNU Java") == 0)
19532         language = DW_LANG_Java;
19533       else if (strcmp (language_string, "GNU Objective-C") == 0)
19534         language = DW_LANG_ObjC;
19535       else if (strcmp (language_string, "GNU Objective-C++") == 0)
19536         language = DW_LANG_ObjC_plus_plus;
19537     }
19538
19539   add_AT_unsigned (die, DW_AT_language, language);
19540
19541   switch (language)
19542     {
19543     case DW_LANG_Fortran77:
19544     case DW_LANG_Fortran90:
19545     case DW_LANG_Fortran95:
19546       /* Fortran has case insensitive identifiers and the front-end
19547          lowercases everything.  */
19548       add_AT_unsigned (die, DW_AT_identifier_case, DW_ID_down_case);
19549       break;
19550     default:
19551       /* The default DW_ID_case_sensitive doesn't need to be specified.  */
19552       break;
19553     }
19554   return die;
19555 }
19556
19557 /* Generate the DIE for a base class.  */
19558
19559 static void
19560 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
19561 {
19562   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
19563
19564   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
19565   add_data_member_location_attribute (die, binfo);
19566
19567   if (BINFO_VIRTUAL_P (binfo))
19568     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
19569
19570   if (access == access_public_node)
19571     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
19572   else if (access == access_protected_node)
19573     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
19574 }
19575
19576 /* Generate a DIE for a class member.  */
19577
19578 static void
19579 gen_member_die (tree type, dw_die_ref context_die)
19580 {
19581   tree member;
19582   tree binfo = TYPE_BINFO (type);
19583   dw_die_ref child;
19584
19585   /* If this is not an incomplete type, output descriptions of each of its
19586      members. Note that as we output the DIEs necessary to represent the
19587      members of this record or union type, we will also be trying to output
19588      DIEs to represent the *types* of those members. However the `type'
19589      function (above) will specifically avoid generating type DIEs for member
19590      types *within* the list of member DIEs for this (containing) type except
19591      for those types (of members) which are explicitly marked as also being
19592      members of this (containing) type themselves.  The g++ front- end can
19593      force any given type to be treated as a member of some other (containing)
19594      type by setting the TYPE_CONTEXT of the given (member) type to point to
19595      the TREE node representing the appropriate (containing) type.  */
19596
19597   /* First output info about the base classes.  */
19598   if (binfo)
19599     {
19600       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
19601       int i;
19602       tree base;
19603
19604       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
19605         gen_inheritance_die (base,
19606                              (accesses ? VEC_index (tree, accesses, i)
19607                               : access_public_node), context_die);
19608     }
19609
19610   /* Now output info about the data members and type members.  */
19611   for (member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
19612     {
19613       /* If we thought we were generating minimal debug info for TYPE
19614          and then changed our minds, some of the member declarations
19615          may have already been defined.  Don't define them again, but
19616          do put them in the right order.  */
19617
19618       child = lookup_decl_die (member);
19619       if (child)
19620         splice_child_die (context_die, child);
19621       else
19622         gen_decl_die (member, NULL, context_die);
19623     }
19624
19625   /* Now output info about the function members (if any).  */
19626   for (member = TYPE_METHODS (type); member; member = DECL_CHAIN (member))
19627     {
19628       /* Don't include clones in the member list.  */
19629       if (DECL_ABSTRACT_ORIGIN (member))
19630         continue;
19631
19632       child = lookup_decl_die (member);
19633       if (child)
19634         splice_child_die (context_die, child);
19635       else
19636         gen_decl_die (member, NULL, context_die);
19637     }
19638 }
19639
19640 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
19641    is set, we pretend that the type was never defined, so we only get the
19642    member DIEs needed by later specification DIEs.  */
19643
19644 static void
19645 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
19646                                 enum debug_info_usage usage)
19647 {
19648   dw_die_ref type_die = lookup_type_die (type);
19649   dw_die_ref scope_die = 0;
19650   int nested = 0;
19651   int complete = (TYPE_SIZE (type)
19652                   && (! TYPE_STUB_DECL (type)
19653                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
19654   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
19655   complete = complete && should_emit_struct_debug (type, usage);
19656
19657   if (type_die && ! complete)
19658     return;
19659
19660   if (TYPE_CONTEXT (type) != NULL_TREE
19661       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19662           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
19663     nested = 1;
19664
19665   scope_die = scope_die_for (type, context_die);
19666
19667   if (! type_die || (nested && scope_die == comp_unit_die))
19668     /* First occurrence of type or toplevel definition of nested class.  */
19669     {
19670       dw_die_ref old_die = type_die;
19671
19672       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
19673                           ? record_type_tag (type) : DW_TAG_union_type,
19674                           scope_die, type);
19675       equate_type_number_to_die (type, type_die);
19676       if (old_die)
19677         add_AT_specification (type_die, old_die);
19678       else
19679         add_name_attribute (type_die, type_tag (type));
19680     }
19681   else
19682     remove_AT (type_die, DW_AT_declaration);
19683
19684   /* Generate child dies for template paramaters.  */
19685   if (debug_info_level > DINFO_LEVEL_TERSE
19686       && COMPLETE_TYPE_P (type))
19687     gen_generic_params_dies (type);
19688
19689   /* If this type has been completed, then give it a byte_size attribute and
19690      then give a list of members.  */
19691   if (complete && !ns_decl)
19692     {
19693       /* Prevent infinite recursion in cases where the type of some member of
19694          this type is expressed in terms of this type itself.  */
19695       TREE_ASM_WRITTEN (type) = 1;
19696       add_byte_size_attribute (type_die, type);
19697       if (TYPE_STUB_DECL (type) != NULL_TREE)
19698         {
19699           add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
19700           add_accessibility_attribute (type_die, TYPE_STUB_DECL (type));
19701         }
19702
19703       /* If the first reference to this type was as the return type of an
19704          inline function, then it may not have a parent.  Fix this now.  */
19705       if (type_die->die_parent == NULL)
19706         add_child_die (scope_die, type_die);
19707
19708       push_decl_scope (type);
19709       gen_member_die (type, type_die);
19710       pop_decl_scope ();
19711
19712       /* GNU extension: Record what type our vtable lives in.  */
19713       if (TYPE_VFIELD (type))
19714         {
19715           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
19716
19717           gen_type_die (vtype, context_die);
19718           add_AT_die_ref (type_die, DW_AT_containing_type,
19719                           lookup_type_die (vtype));
19720         }
19721     }
19722   else
19723     {
19724       add_AT_flag (type_die, DW_AT_declaration, 1);
19725
19726       /* We don't need to do this for function-local types.  */
19727       if (TYPE_STUB_DECL (type)
19728           && ! decl_function_context (TYPE_STUB_DECL (type)))
19729         VEC_safe_push (tree, gc, incomplete_types, type);
19730     }
19731
19732   if (get_AT (type_die, DW_AT_name))
19733     add_pubtype (type, type_die);
19734 }
19735
19736 /* Generate a DIE for a subroutine _type_.  */
19737
19738 static void
19739 gen_subroutine_type_die (tree type, dw_die_ref context_die)
19740 {
19741   tree return_type = TREE_TYPE (type);
19742   dw_die_ref subr_die
19743     = new_die (DW_TAG_subroutine_type,
19744                scope_die_for (type, context_die), type);
19745
19746   equate_type_number_to_die (type, subr_die);
19747   add_prototyped_attribute (subr_die, type);
19748   add_type_attribute (subr_die, return_type, 0, 0, context_die);
19749   gen_formal_types_die (type, subr_die);
19750
19751   if (get_AT (subr_die, DW_AT_name))
19752     add_pubtype (type, subr_die);
19753 }
19754
19755 /* Generate a DIE for a type definition.  */
19756
19757 static void
19758 gen_typedef_die (tree decl, dw_die_ref context_die)
19759 {
19760   dw_die_ref type_die;
19761   tree origin;
19762
19763   if (TREE_ASM_WRITTEN (decl))
19764     return;
19765
19766   TREE_ASM_WRITTEN (decl) = 1;
19767   type_die = new_die (DW_TAG_typedef, context_die, decl);
19768   origin = decl_ultimate_origin (decl);
19769   if (origin != NULL)
19770     add_abstract_origin_attribute (type_die, origin);
19771   else
19772     {
19773       tree type;
19774
19775       add_name_and_src_coords_attributes (type_die, decl);
19776       if (DECL_ORIGINAL_TYPE (decl))
19777         {
19778           type = DECL_ORIGINAL_TYPE (decl);
19779
19780           gcc_assert (type != TREE_TYPE (decl));
19781           equate_type_number_to_die (TREE_TYPE (decl), type_die);
19782         }
19783       else
19784         {
19785           type = TREE_TYPE (decl);
19786
19787           if (is_naming_typedef_decl (TYPE_NAME (type)))
19788             /* 
19789                Here, we are in the case of decl being a typedef naming
19790                an anonymous type, e.g:
19791                      typedef struct {...} foo;
19792                In that case TREE_TYPE (decl) is not a typedef variant
19793                type and TYPE_NAME of the anonymous type is set to the
19794                TYPE_DECL of the typedef. This construct is emitted by
19795                the C++ FE.
19796
19797                TYPE is the anonymous struct named by the typedef
19798                DECL. As we need the DW_AT_type attribute of the
19799                DW_TAG_typedef to point to the DIE of TYPE, let's
19800                generate that DIE right away. add_type_attribute
19801                called below will then pick (via lookup_type_die) that
19802                anonymous struct DIE.  */
19803             gen_tagged_type_die (type, context_die, DINFO_USAGE_DIR_USE);
19804         }
19805
19806       add_type_attribute (type_die, type, TREE_READONLY (decl),
19807                           TREE_THIS_VOLATILE (decl), context_die);
19808
19809       if (is_naming_typedef_decl (decl))
19810         /* We want that all subsequent calls to lookup_type_die with
19811            TYPE in argument yield the DW_TAG_typedef we have just
19812            created.  */
19813         equate_type_number_to_die (type, type_die);
19814
19815       add_accessibility_attribute (type_die, decl);
19816     }
19817
19818   if (DECL_ABSTRACT (decl))
19819     equate_decl_number_to_die (decl, type_die);
19820
19821   if (get_AT (type_die, DW_AT_name))
19822     add_pubtype (decl, type_die);
19823 }
19824
19825 /* Generate a DIE for a struct, class, enum or union type.  */
19826
19827 static void
19828 gen_tagged_type_die (tree type,
19829                      dw_die_ref context_die,
19830                      enum debug_info_usage usage)
19831 {
19832   int need_pop;
19833
19834   if (type == NULL_TREE
19835       || !is_tagged_type (type))
19836     return;
19837
19838   /* If this is a nested type whose containing class hasn't been written
19839      out yet, writing it out will cover this one, too.  This does not apply
19840      to instantiations of member class templates; they need to be added to
19841      the containing class as they are generated.  FIXME: This hurts the
19842      idea of combining type decls from multiple TUs, since we can't predict
19843      what set of template instantiations we'll get.  */
19844   if (TYPE_CONTEXT (type)
19845       && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
19846       && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
19847     {
19848       gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
19849
19850       if (TREE_ASM_WRITTEN (type))
19851         return;
19852
19853       /* If that failed, attach ourselves to the stub.  */
19854       push_decl_scope (TYPE_CONTEXT (type));
19855       context_die = lookup_type_die (TYPE_CONTEXT (type));
19856       need_pop = 1;
19857     }
19858   else if (TYPE_CONTEXT (type) != NULL_TREE
19859            && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
19860     {
19861       /* If this type is local to a function that hasn't been written
19862          out yet, use a NULL context for now; it will be fixed up in
19863          decls_for_scope.  */
19864       context_die = lookup_decl_die (TYPE_CONTEXT (type));
19865       need_pop = 0;
19866     }
19867   else
19868     {
19869       context_die = declare_in_namespace (type, context_die);
19870       need_pop = 0;
19871     }
19872
19873   if (TREE_CODE (type) == ENUMERAL_TYPE)
19874     {
19875       /* This might have been written out by the call to
19876          declare_in_namespace.  */
19877       if (!TREE_ASM_WRITTEN (type))
19878         gen_enumeration_type_die (type, context_die);
19879     }
19880   else
19881     gen_struct_or_union_type_die (type, context_die, usage);
19882
19883   if (need_pop)
19884     pop_decl_scope ();
19885
19886   /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
19887      it up if it is ever completed.  gen_*_type_die will set it for us
19888      when appropriate.  */
19889 }
19890
19891 /* Generate a type description DIE.  */
19892
19893 static void
19894 gen_type_die_with_usage (tree type, dw_die_ref context_die,
19895                                 enum debug_info_usage usage)
19896 {
19897   struct array_descr_info info;
19898
19899   if (type == NULL_TREE || type == error_mark_node)
19900     return;
19901
19902   /* If TYPE is a typedef type variant, let's generate debug info
19903      for the parent typedef which TYPE is a type of.  */
19904   if (typedef_variant_p (type))
19905     {
19906       if (TREE_ASM_WRITTEN (type))
19907         return;
19908
19909       /* Prevent broken recursion; we can't hand off to the same type.  */
19910       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
19911
19912       /* Use the DIE of the containing namespace as the parent DIE of
19913          the type description DIE we want to generate.  */
19914       if (DECL_CONTEXT (TYPE_NAME (type))
19915           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19916         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19917
19918       TREE_ASM_WRITTEN (type) = 1;
19919
19920       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19921       return;
19922     }
19923
19924   /* If type is an anonymous tagged type named by a typedef, let's
19925      generate debug info for the typedef.  */
19926   if (is_naming_typedef_decl (TYPE_NAME (type)))
19927     {
19928       /* Use the DIE of the containing namespace as the parent DIE of
19929          the type description DIE we want to generate.  */
19930       if (DECL_CONTEXT (TYPE_NAME (type))
19931           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
19932         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
19933       
19934       gen_decl_die (TYPE_NAME (type), NULL, context_die);
19935       return;
19936     }
19937
19938   /* If this is an array type with hidden descriptor, handle it first.  */
19939   if (!TREE_ASM_WRITTEN (type)
19940       && lang_hooks.types.get_array_descr_info
19941       && lang_hooks.types.get_array_descr_info (type, &info)
19942       && (dwarf_version >= 3 || !dwarf_strict))
19943     {
19944       gen_descr_array_type_die (type, &info, context_die);
19945       TREE_ASM_WRITTEN (type) = 1;
19946       return;
19947     }
19948
19949   /* We are going to output a DIE to represent the unqualified version
19950      of this type (i.e. without any const or volatile qualifiers) so
19951      get the main variant (i.e. the unqualified version) of this type
19952      now.  (Vectors are special because the debugging info is in the
19953      cloned type itself).  */
19954   if (TREE_CODE (type) != VECTOR_TYPE)
19955     type = type_main_variant (type);
19956
19957   if (TREE_ASM_WRITTEN (type))
19958     return;
19959
19960   switch (TREE_CODE (type))
19961     {
19962     case ERROR_MARK:
19963       break;
19964
19965     case POINTER_TYPE:
19966     case REFERENCE_TYPE:
19967       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
19968          ensures that the gen_type_die recursion will terminate even if the
19969          type is recursive.  Recursive types are possible in Ada.  */
19970       /* ??? We could perhaps do this for all types before the switch
19971          statement.  */
19972       TREE_ASM_WRITTEN (type) = 1;
19973
19974       /* For these types, all that is required is that we output a DIE (or a
19975          set of DIEs) to represent the "basis" type.  */
19976       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19977                                 DINFO_USAGE_IND_USE);
19978       break;
19979
19980     case OFFSET_TYPE:
19981       /* This code is used for C++ pointer-to-data-member types.
19982          Output a description of the relevant class type.  */
19983       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
19984                                         DINFO_USAGE_IND_USE);
19985
19986       /* Output a description of the type of the object pointed to.  */
19987       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19988                                         DINFO_USAGE_IND_USE);
19989
19990       /* Now output a DIE to represent this pointer-to-data-member type
19991          itself.  */
19992       gen_ptr_to_mbr_type_die (type, context_die);
19993       break;
19994
19995     case FUNCTION_TYPE:
19996       /* Force out return type (in case it wasn't forced out already).  */
19997       gen_type_die_with_usage (TREE_TYPE (type), context_die,
19998                                         DINFO_USAGE_DIR_USE);
19999       gen_subroutine_type_die (type, context_die);
20000       break;
20001
20002     case METHOD_TYPE:
20003       /* Force out return type (in case it wasn't forced out already).  */
20004       gen_type_die_with_usage (TREE_TYPE (type), context_die,
20005                                         DINFO_USAGE_DIR_USE);
20006       gen_subroutine_type_die (type, context_die);
20007       break;
20008
20009     case ARRAY_TYPE:
20010       gen_array_type_die (type, context_die);
20011       break;
20012
20013     case VECTOR_TYPE:
20014       gen_array_type_die (type, context_die);
20015       break;
20016
20017     case ENUMERAL_TYPE:
20018     case RECORD_TYPE:
20019     case UNION_TYPE:
20020     case QUAL_UNION_TYPE:
20021       gen_tagged_type_die (type, context_die, usage);
20022       return;
20023
20024     case VOID_TYPE:
20025     case INTEGER_TYPE:
20026     case REAL_TYPE:
20027     case FIXED_POINT_TYPE:
20028     case COMPLEX_TYPE:
20029     case BOOLEAN_TYPE:
20030       /* No DIEs needed for fundamental types.  */
20031       break;
20032
20033     case LANG_TYPE:
20034       /* Just use DW_TAG_unspecified_type.  */
20035       {
20036         dw_die_ref type_die = lookup_type_die (type);
20037         if (type_die == NULL)
20038           {
20039             tree name = TYPE_NAME (type);
20040             if (TREE_CODE (name) == TYPE_DECL)
20041               name = DECL_NAME (name);
20042             type_die = new_die (DW_TAG_unspecified_type, comp_unit_die, type);
20043             add_name_attribute (type_die, IDENTIFIER_POINTER (name));
20044             equate_type_number_to_die (type, type_die);
20045           }
20046       }
20047       break;
20048
20049     default:
20050       gcc_unreachable ();
20051     }
20052
20053   TREE_ASM_WRITTEN (type) = 1;
20054 }
20055
20056 static void
20057 gen_type_die (tree type, dw_die_ref context_die)
20058 {
20059   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
20060 }
20061
20062 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
20063    things which are local to the given block.  */
20064
20065 static void
20066 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
20067 {
20068   int must_output_die = 0;
20069   bool inlined_func;
20070
20071   /* Ignore blocks that are NULL.  */
20072   if (stmt == NULL_TREE)
20073     return;
20074
20075   inlined_func = inlined_function_outer_scope_p (stmt);
20076
20077   /* If the block is one fragment of a non-contiguous block, do not
20078      process the variables, since they will have been done by the
20079      origin block.  Do process subblocks.  */
20080   if (BLOCK_FRAGMENT_ORIGIN (stmt))
20081     {
20082       tree sub;
20083
20084       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
20085         gen_block_die (sub, context_die, depth + 1);
20086
20087       return;
20088     }
20089
20090   /* Determine if we need to output any Dwarf DIEs at all to represent this
20091      block.  */
20092   if (inlined_func)
20093     /* The outer scopes for inlinings *must* always be represented.  We
20094        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
20095     must_output_die = 1;
20096   else
20097     {
20098       /* Determine if this block directly contains any "significant"
20099          local declarations which we will need to output DIEs for.  */
20100       if (debug_info_level > DINFO_LEVEL_TERSE)
20101         /* We are not in terse mode so *any* local declaration counts
20102            as being a "significant" one.  */
20103         must_output_die = ((BLOCK_VARS (stmt) != NULL
20104                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
20105                            && (TREE_USED (stmt)
20106                                || TREE_ASM_WRITTEN (stmt)
20107                                || BLOCK_ABSTRACT (stmt)));
20108       else if ((TREE_USED (stmt)
20109                 || TREE_ASM_WRITTEN (stmt)
20110                 || BLOCK_ABSTRACT (stmt))
20111                && !dwarf2out_ignore_block (stmt))
20112         must_output_die = 1;
20113     }
20114
20115   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
20116      DIE for any block which contains no significant local declarations at
20117      all.  Rather, in such cases we just call `decls_for_scope' so that any
20118      needed Dwarf info for any sub-blocks will get properly generated. Note
20119      that in terse mode, our definition of what constitutes a "significant"
20120      local declaration gets restricted to include only inlined function
20121      instances and local (nested) function definitions.  */
20122   if (must_output_die)
20123     {
20124       if (inlined_func)
20125         {
20126           /* If STMT block is abstract, that means we have been called
20127              indirectly from dwarf2out_abstract_function.
20128              That function rightfully marks the descendent blocks (of
20129              the abstract function it is dealing with) as being abstract,
20130              precisely to prevent us from emitting any
20131              DW_TAG_inlined_subroutine DIE as a descendent
20132              of an abstract function instance. So in that case, we should
20133              not call gen_inlined_subroutine_die.
20134
20135              Later though, when cgraph asks dwarf2out to emit info
20136              for the concrete instance of the function decl into which
20137              the concrete instance of STMT got inlined, the later will lead
20138              to the generation of a DW_TAG_inlined_subroutine DIE.  */
20139           if (! BLOCK_ABSTRACT (stmt))
20140             gen_inlined_subroutine_die (stmt, context_die, depth);
20141         }
20142       else
20143         gen_lexical_block_die (stmt, context_die, depth);
20144     }
20145   else
20146     decls_for_scope (stmt, context_die, depth);
20147 }
20148
20149 /* Process variable DECL (or variable with origin ORIGIN) within
20150    block STMT and add it to CONTEXT_DIE.  */
20151 static void
20152 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
20153 {
20154   dw_die_ref die;
20155   tree decl_or_origin = decl ? decl : origin;
20156
20157   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
20158     die = lookup_decl_die (decl_or_origin);
20159   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
20160            && TYPE_DECL_IS_STUB (decl_or_origin))
20161     die = lookup_type_die (TREE_TYPE (decl_or_origin));
20162   else
20163     die = NULL;
20164
20165   if (die != NULL && die->die_parent == NULL)
20166     add_child_die (context_die, die);
20167   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
20168     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
20169                                          stmt, context_die);
20170   else
20171     gen_decl_die (decl, origin, context_die);
20172 }
20173
20174 /* Generate all of the decls declared within a given scope and (recursively)
20175    all of its sub-blocks.  */
20176
20177 static void
20178 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
20179 {
20180   tree decl;
20181   unsigned int i;
20182   tree subblocks;
20183
20184   /* Ignore NULL blocks.  */
20185   if (stmt == NULL_TREE)
20186     return;
20187
20188   /* Output the DIEs to represent all of the data objects and typedefs
20189      declared directly within this block but not within any nested
20190      sub-blocks.  Also, nested function and tag DIEs have been
20191      generated with a parent of NULL; fix that up now.  */
20192   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = DECL_CHAIN (decl))
20193     process_scope_var (stmt, decl, NULL_TREE, context_die);
20194   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
20195     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
20196                        context_die);
20197
20198   /* If we're at -g1, we're not interested in subblocks.  */
20199   if (debug_info_level <= DINFO_LEVEL_TERSE)
20200     return;
20201
20202   /* Output the DIEs to represent all sub-blocks (and the items declared
20203      therein) of this block.  */
20204   for (subblocks = BLOCK_SUBBLOCKS (stmt);
20205        subblocks != NULL;
20206        subblocks = BLOCK_CHAIN (subblocks))
20207     gen_block_die (subblocks, context_die, depth + 1);
20208 }
20209
20210 /* Is this a typedef we can avoid emitting?  */
20211
20212 static inline int
20213 is_redundant_typedef (const_tree decl)
20214 {
20215   if (TYPE_DECL_IS_STUB (decl))
20216     return 1;
20217
20218   if (DECL_ARTIFICIAL (decl)
20219       && DECL_CONTEXT (decl)
20220       && is_tagged_type (DECL_CONTEXT (decl))
20221       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
20222       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
20223     /* Also ignore the artificial member typedef for the class name.  */
20224     return 1;
20225
20226   return 0;
20227 }
20228
20229 /* Return TRUE if TYPE is a typedef that names a type for linkage
20230    purposes. This kind of typedefs is produced by the C++ FE for
20231    constructs like:
20232
20233    typedef struct {...} foo;
20234
20235    In that case, there is no typedef variant type produced for foo.
20236    Rather, the TREE_TYPE of the TYPE_DECL of foo is the anonymous
20237    struct type.  */
20238
20239 static bool
20240 is_naming_typedef_decl (const_tree decl)
20241 {
20242   if (decl == NULL_TREE
20243       || TREE_CODE (decl) != TYPE_DECL
20244       || !is_tagged_type (TREE_TYPE (decl))
20245       || DECL_IS_BUILTIN (decl)
20246       || is_redundant_typedef (decl)
20247       /* It looks like Ada produces TYPE_DECLs that are very similar
20248          to C++ naming typedefs but that have different
20249          semantics. Let's be specific to c++ for now.  */
20250       || !is_cxx ())
20251     return FALSE;
20252
20253   return (DECL_ORIGINAL_TYPE (decl) == NULL_TREE
20254           && TYPE_NAME (TREE_TYPE (decl)) == decl
20255           && (TYPE_STUB_DECL (TREE_TYPE (decl))
20256               != TYPE_NAME (TREE_TYPE (decl))));
20257 }
20258
20259 /* Returns the DIE for a context.  */
20260
20261 static inline dw_die_ref
20262 get_context_die (tree context)
20263 {
20264   if (context)
20265     {
20266       /* Find die that represents this context.  */
20267       if (TYPE_P (context))
20268         return force_type_die (TYPE_MAIN_VARIANT (context));
20269       else
20270         return force_decl_die (context);
20271     }
20272   return comp_unit_die;
20273 }
20274
20275 /* Returns the DIE for decl.  A DIE will always be returned.  */
20276
20277 static dw_die_ref
20278 force_decl_die (tree decl)
20279 {
20280   dw_die_ref decl_die;
20281   unsigned saved_external_flag;
20282   tree save_fn = NULL_TREE;
20283   decl_die = lookup_decl_die (decl);
20284   if (!decl_die)
20285     {
20286       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
20287
20288       decl_die = lookup_decl_die (decl);
20289       if (decl_die)
20290         return decl_die;
20291
20292       switch (TREE_CODE (decl))
20293         {
20294         case FUNCTION_DECL:
20295           /* Clear current_function_decl, so that gen_subprogram_die thinks
20296              that this is a declaration. At this point, we just want to force
20297              declaration die.  */
20298           save_fn = current_function_decl;
20299           current_function_decl = NULL_TREE;
20300           gen_subprogram_die (decl, context_die);
20301           current_function_decl = save_fn;
20302           break;
20303
20304         case VAR_DECL:
20305           /* Set external flag to force declaration die. Restore it after
20306            gen_decl_die() call.  */
20307           saved_external_flag = DECL_EXTERNAL (decl);
20308           DECL_EXTERNAL (decl) = 1;
20309           gen_decl_die (decl, NULL, context_die);
20310           DECL_EXTERNAL (decl) = saved_external_flag;
20311           break;
20312
20313         case NAMESPACE_DECL:
20314           if (dwarf_version >= 3 || !dwarf_strict)
20315             dwarf2out_decl (decl);
20316           else
20317             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
20318             decl_die = comp_unit_die;
20319           break;
20320
20321         default:
20322           gcc_unreachable ();
20323         }
20324
20325       /* We should be able to find the DIE now.  */
20326       if (!decl_die)
20327         decl_die = lookup_decl_die (decl);
20328       gcc_assert (decl_die);
20329     }
20330
20331   return decl_die;
20332 }
20333
20334 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
20335    always returned.  */
20336
20337 static dw_die_ref
20338 force_type_die (tree type)
20339 {
20340   dw_die_ref type_die;
20341
20342   type_die = lookup_type_die (type);
20343   if (!type_die)
20344     {
20345       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
20346
20347       type_die = modified_type_die (type, TYPE_READONLY (type),
20348                                     TYPE_VOLATILE (type), context_die);
20349       gcc_assert (type_die);
20350     }
20351   return type_die;
20352 }
20353
20354 /* Force out any required namespaces to be able to output DECL,
20355    and return the new context_die for it, if it's changed.  */
20356
20357 static dw_die_ref
20358 setup_namespace_context (tree thing, dw_die_ref context_die)
20359 {
20360   tree context = (DECL_P (thing)
20361                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
20362   if (context && TREE_CODE (context) == NAMESPACE_DECL)
20363     /* Force out the namespace.  */
20364     context_die = force_decl_die (context);
20365
20366   return context_die;
20367 }
20368
20369 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
20370    type) within its namespace, if appropriate.
20371
20372    For compatibility with older debuggers, namespace DIEs only contain
20373    declarations; all definitions are emitted at CU scope.  */
20374
20375 static dw_die_ref
20376 declare_in_namespace (tree thing, dw_die_ref context_die)
20377 {
20378   dw_die_ref ns_context;
20379
20380   if (debug_info_level <= DINFO_LEVEL_TERSE)
20381     return context_die;
20382
20383   /* If this decl is from an inlined function, then don't try to emit it in its
20384      namespace, as we will get confused.  It would have already been emitted
20385      when the abstract instance of the inline function was emitted anyways.  */
20386   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
20387     return context_die;
20388
20389   ns_context = setup_namespace_context (thing, context_die);
20390
20391   if (ns_context != context_die)
20392     {
20393       if (is_fortran ())
20394         return ns_context;
20395       if (DECL_P (thing))
20396         gen_decl_die (thing, NULL, ns_context);
20397       else
20398         gen_type_die (thing, ns_context);
20399     }
20400   return context_die;
20401 }
20402
20403 /* Generate a DIE for a namespace or namespace alias.  */
20404
20405 static void
20406 gen_namespace_die (tree decl, dw_die_ref context_die)
20407 {
20408   dw_die_ref namespace_die;
20409
20410   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
20411      they are an alias of.  */
20412   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
20413     {
20414       /* Output a real namespace or module.  */
20415       context_die = setup_namespace_context (decl, comp_unit_die);
20416       namespace_die = new_die (is_fortran ()
20417                                ? DW_TAG_module : DW_TAG_namespace,
20418                                context_die, decl);
20419       /* For Fortran modules defined in different CU don't add src coords.  */
20420       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
20421         {
20422           const char *name = dwarf2_name (decl, 0);
20423           if (name)
20424             add_name_attribute (namespace_die, name);
20425         }
20426       else
20427         add_name_and_src_coords_attributes (namespace_die, decl);
20428       if (DECL_EXTERNAL (decl))
20429         add_AT_flag (namespace_die, DW_AT_declaration, 1);
20430       equate_decl_number_to_die (decl, namespace_die);
20431     }
20432   else
20433     {
20434       /* Output a namespace alias.  */
20435
20436       /* Force out the namespace we are an alias of, if necessary.  */
20437       dw_die_ref origin_die
20438         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
20439
20440       if (DECL_CONTEXT (decl) == NULL_TREE
20441           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
20442         context_die = setup_namespace_context (decl, comp_unit_die);
20443       /* Now create the namespace alias DIE.  */
20444       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
20445       add_name_and_src_coords_attributes (namespace_die, decl);
20446       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
20447       equate_decl_number_to_die (decl, namespace_die);
20448     }
20449 }
20450
20451 /* Generate Dwarf debug information for a decl described by DECL.
20452    The return value is currently only meaningful for PARM_DECLs,
20453    for all other decls it returns NULL.  */
20454
20455 static dw_die_ref
20456 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
20457 {
20458   tree decl_or_origin = decl ? decl : origin;
20459   tree class_origin = NULL, ultimate_origin;
20460
20461   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
20462     return NULL;
20463
20464   switch (TREE_CODE (decl_or_origin))
20465     {
20466     case ERROR_MARK:
20467       break;
20468
20469     case CONST_DECL:
20470       if (!is_fortran ())
20471         {
20472           /* The individual enumerators of an enum type get output when we output
20473              the Dwarf representation of the relevant enum type itself.  */
20474           break;
20475         }
20476
20477       /* Emit its type.  */
20478       gen_type_die (TREE_TYPE (decl), context_die);
20479
20480       /* And its containing namespace.  */
20481       context_die = declare_in_namespace (decl, context_die);
20482
20483       gen_const_die (decl, context_die);
20484       break;
20485
20486     case FUNCTION_DECL:
20487       /* Don't output any DIEs to represent mere function declarations,
20488          unless they are class members or explicit block externs.  */
20489       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
20490           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
20491           && (current_function_decl == NULL_TREE
20492               || DECL_ARTIFICIAL (decl_or_origin)))
20493         break;
20494
20495 #if 0
20496       /* FIXME */
20497       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
20498          on local redeclarations of global functions.  That seems broken.  */
20499       if (current_function_decl != decl)
20500         /* This is only a declaration.  */;
20501 #endif
20502
20503       /* If we're emitting a clone, emit info for the abstract instance.  */
20504       if (origin || DECL_ORIGIN (decl) != decl)
20505         dwarf2out_abstract_function (origin
20506                                      ? DECL_ORIGIN (origin)
20507                                      : DECL_ABSTRACT_ORIGIN (decl));
20508
20509       /* If we're emitting an out-of-line copy of an inline function,
20510          emit info for the abstract instance and set up to refer to it.  */
20511       else if (cgraph_function_possibly_inlined_p (decl)
20512                && ! DECL_ABSTRACT (decl)
20513                && ! class_or_namespace_scope_p (context_die)
20514                /* dwarf2out_abstract_function won't emit a die if this is just
20515                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
20516                   that case, because that works only if we have a die.  */
20517                && DECL_INITIAL (decl) != NULL_TREE)
20518         {
20519           dwarf2out_abstract_function (decl);
20520           set_decl_origin_self (decl);
20521         }
20522
20523       /* Otherwise we're emitting the primary DIE for this decl.  */
20524       else if (debug_info_level > DINFO_LEVEL_TERSE)
20525         {
20526           /* Before we describe the FUNCTION_DECL itself, make sure that we
20527              have described its return type.  */
20528           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
20529
20530           /* And its virtual context.  */
20531           if (DECL_VINDEX (decl) != NULL_TREE)
20532             gen_type_die (DECL_CONTEXT (decl), context_die);
20533
20534           /* And its containing type.  */
20535           if (!origin)
20536             origin = decl_class_context (decl);
20537           if (origin != NULL_TREE)
20538             gen_type_die_for_member (origin, decl, context_die);
20539
20540           /* And its containing namespace.  */
20541           context_die = declare_in_namespace (decl, context_die);
20542         }
20543
20544       /* Now output a DIE to represent the function itself.  */
20545       if (decl)
20546         gen_subprogram_die (decl, context_die);
20547       break;
20548
20549     case TYPE_DECL:
20550       /* If we are in terse mode, don't generate any DIEs to represent any
20551          actual typedefs.  */
20552       if (debug_info_level <= DINFO_LEVEL_TERSE)
20553         break;
20554
20555       /* In the special case of a TYPE_DECL node representing the declaration
20556          of some type tag, if the given TYPE_DECL is marked as having been
20557          instantiated from some other (original) TYPE_DECL node (e.g. one which
20558          was generated within the original definition of an inline function) we
20559          used to generate a special (abbreviated) DW_TAG_structure_type,
20560          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
20561          should be actually referencing those DIEs, as variable DIEs with that
20562          type would be emitted already in the abstract origin, so it was always
20563          removed during unused type prunning.  Don't add anything in this
20564          case.  */
20565       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
20566         break;
20567
20568       if (is_redundant_typedef (decl))
20569         gen_type_die (TREE_TYPE (decl), context_die);
20570       else
20571         /* Output a DIE to represent the typedef itself.  */
20572         gen_typedef_die (decl, context_die);
20573       break;
20574
20575     case LABEL_DECL:
20576       if (debug_info_level >= DINFO_LEVEL_NORMAL)
20577         gen_label_die (decl, context_die);
20578       break;
20579
20580     case VAR_DECL:
20581     case RESULT_DECL:
20582       /* If we are in terse mode, don't generate any DIEs to represent any
20583          variable declarations or definitions.  */
20584       if (debug_info_level <= DINFO_LEVEL_TERSE)
20585         break;
20586
20587       /* Output any DIEs that are needed to specify the type of this data
20588          object.  */
20589       if (decl_by_reference_p (decl_or_origin))
20590         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20591       else
20592         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20593
20594       /* And its containing type.  */
20595       class_origin = decl_class_context (decl_or_origin);
20596       if (class_origin != NULL_TREE)
20597         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
20598
20599       /* And its containing namespace.  */
20600       context_die = declare_in_namespace (decl_or_origin, context_die);
20601
20602       /* Now output the DIE to represent the data object itself.  This gets
20603          complicated because of the possibility that the VAR_DECL really
20604          represents an inlined instance of a formal parameter for an inline
20605          function.  */
20606       ultimate_origin = decl_ultimate_origin (decl_or_origin);
20607       if (ultimate_origin != NULL_TREE
20608           && TREE_CODE (ultimate_origin) == PARM_DECL)
20609         gen_formal_parameter_die (decl, origin,
20610                                   true /* Emit name attribute.  */,
20611                                   context_die);
20612       else
20613         gen_variable_die (decl, origin, context_die);
20614       break;
20615
20616     case FIELD_DECL:
20617       /* Ignore the nameless fields that are used to skip bits but handle C++
20618          anonymous unions and structs.  */
20619       if (DECL_NAME (decl) != NULL_TREE
20620           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
20621           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
20622         {
20623           gen_type_die (member_declared_type (decl), context_die);
20624           gen_field_die (decl, context_die);
20625         }
20626       break;
20627
20628     case PARM_DECL:
20629       if (DECL_BY_REFERENCE (decl_or_origin))
20630         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
20631       else
20632         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
20633       return gen_formal_parameter_die (decl, origin,
20634                                        true /* Emit name attribute.  */,
20635                                        context_die);
20636
20637     case NAMESPACE_DECL:
20638     case IMPORTED_DECL:
20639       if (dwarf_version >= 3 || !dwarf_strict)
20640         gen_namespace_die (decl, context_die);
20641       break;
20642
20643     default:
20644       /* Probably some frontend-internal decl.  Assume we don't care.  */
20645       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
20646       break;
20647     }
20648
20649   return NULL;
20650 }
20651 \f
20652 /* Output debug information for global decl DECL.  Called from toplev.c after
20653    compilation proper has finished.  */
20654
20655 static void
20656 dwarf2out_global_decl (tree decl)
20657 {
20658   /* Output DWARF2 information for file-scope tentative data object
20659      declarations, file-scope (extern) function declarations (which
20660      had no corresponding body) and file-scope tagged type declarations
20661      and definitions which have not yet been forced out.  */
20662   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
20663     dwarf2out_decl (decl);
20664 }
20665
20666 /* Output debug information for type decl DECL.  Called from toplev.c
20667    and from language front ends (to record built-in types).  */
20668 static void
20669 dwarf2out_type_decl (tree decl, int local)
20670 {
20671   if (!local)
20672     dwarf2out_decl (decl);
20673 }
20674
20675 /* Output debug information for imported module or decl DECL.
20676    NAME is non-NULL name in the lexical block if the decl has been renamed.
20677    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
20678    that DECL belongs to.
20679    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
20680 static void
20681 dwarf2out_imported_module_or_decl_1 (tree decl,
20682                                      tree name,
20683                                      tree lexical_block,
20684                                      dw_die_ref lexical_block_die)
20685 {
20686   expanded_location xloc;
20687   dw_die_ref imported_die = NULL;
20688   dw_die_ref at_import_die;
20689
20690   if (TREE_CODE (decl) == IMPORTED_DECL)
20691     {
20692       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
20693       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
20694       gcc_assert (decl);
20695     }
20696   else
20697     xloc = expand_location (input_location);
20698
20699   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
20700     {
20701       at_import_die = force_type_die (TREE_TYPE (decl));
20702       /* For namespace N { typedef void T; } using N::T; base_type_die
20703          returns NULL, but DW_TAG_imported_declaration requires
20704          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
20705       if (!at_import_die)
20706         {
20707           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
20708           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
20709           at_import_die = lookup_type_die (TREE_TYPE (decl));
20710           gcc_assert (at_import_die);
20711         }
20712     }
20713   else
20714     {
20715       at_import_die = lookup_decl_die (decl);
20716       if (!at_import_die)
20717         {
20718           /* If we're trying to avoid duplicate debug info, we may not have
20719              emitted the member decl for this field.  Emit it now.  */
20720           if (TREE_CODE (decl) == FIELD_DECL)
20721             {
20722               tree type = DECL_CONTEXT (decl);
20723
20724               if (TYPE_CONTEXT (type)
20725                   && TYPE_P (TYPE_CONTEXT (type))
20726                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
20727                                                 DINFO_USAGE_DIR_USE))
20728                 return;
20729               gen_type_die_for_member (type, decl,
20730                                        get_context_die (TYPE_CONTEXT (type)));
20731             }
20732           at_import_die = force_decl_die (decl);
20733         }
20734     }
20735
20736   if (TREE_CODE (decl) == NAMESPACE_DECL)
20737     {
20738       if (dwarf_version >= 3 || !dwarf_strict)
20739         imported_die = new_die (DW_TAG_imported_module,
20740                                 lexical_block_die,
20741                                 lexical_block);
20742       else
20743         return;
20744     }
20745   else
20746     imported_die = new_die (DW_TAG_imported_declaration,
20747                             lexical_block_die,
20748                             lexical_block);
20749
20750   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
20751   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
20752   if (name)
20753     add_AT_string (imported_die, DW_AT_name,
20754                    IDENTIFIER_POINTER (name));
20755   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
20756 }
20757
20758 /* Output debug information for imported module or decl DECL.
20759    NAME is non-NULL name in context if the decl has been renamed.
20760    CHILD is true if decl is one of the renamed decls as part of
20761    importing whole module.  */
20762
20763 static void
20764 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
20765                                    bool child)
20766 {
20767   /* dw_die_ref at_import_die;  */
20768   dw_die_ref scope_die;
20769
20770   if (debug_info_level <= DINFO_LEVEL_TERSE)
20771     return;
20772
20773   gcc_assert (decl);
20774
20775   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
20776      We need decl DIE for reference and scope die. First, get DIE for the decl
20777      itself.  */
20778
20779   /* Get the scope die for decl context. Use comp_unit_die for global module
20780      or decl. If die is not found for non globals, force new die.  */
20781   if (context
20782       && TYPE_P (context)
20783       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
20784     return;
20785
20786   if (!(dwarf_version >= 3 || !dwarf_strict))
20787     return;
20788
20789   scope_die = get_context_die (context);
20790
20791   if (child)
20792     {
20793       gcc_assert (scope_die->die_child);
20794       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
20795       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
20796       scope_die = scope_die->die_child;
20797     }
20798
20799   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
20800   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
20801
20802 }
20803
20804 /* Write the debugging output for DECL.  */
20805
20806 void
20807 dwarf2out_decl (tree decl)
20808 {
20809   dw_die_ref context_die = comp_unit_die;
20810
20811   switch (TREE_CODE (decl))
20812     {
20813     case ERROR_MARK:
20814       return;
20815
20816     case FUNCTION_DECL:
20817       /* What we would really like to do here is to filter out all mere
20818          file-scope declarations of file-scope functions which are never
20819          referenced later within this translation unit (and keep all of ones
20820          that *are* referenced later on) but we aren't clairvoyant, so we have
20821          no idea which functions will be referenced in the future (i.e. later
20822          on within the current translation unit). So here we just ignore all
20823          file-scope function declarations which are not also definitions.  If
20824          and when the debugger needs to know something about these functions,
20825          it will have to hunt around and find the DWARF information associated
20826          with the definition of the function.
20827
20828          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
20829          nodes represent definitions and which ones represent mere
20830          declarations.  We have to check DECL_INITIAL instead. That's because
20831          the C front-end supports some weird semantics for "extern inline"
20832          function definitions.  These can get inlined within the current
20833          translation unit (and thus, we need to generate Dwarf info for their
20834          abstract instances so that the Dwarf info for the concrete inlined
20835          instances can have something to refer to) but the compiler never
20836          generates any out-of-lines instances of such things (despite the fact
20837          that they *are* definitions).
20838
20839          The important point is that the C front-end marks these "extern
20840          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
20841          them anyway. Note that the C++ front-end also plays some similar games
20842          for inline function definitions appearing within include files which
20843          also contain `#pragma interface' pragmas.  */
20844       if (DECL_INITIAL (decl) == NULL_TREE)
20845         return;
20846
20847       /* If we're a nested function, initially use a parent of NULL; if we're
20848          a plain function, this will be fixed up in decls_for_scope.  If
20849          we're a method, it will be ignored, since we already have a DIE.  */
20850       if (decl_function_context (decl)
20851           /* But if we're in terse mode, we don't care about scope.  */
20852           && debug_info_level > DINFO_LEVEL_TERSE)
20853         context_die = NULL;
20854       break;
20855
20856     case VAR_DECL:
20857       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
20858          declaration and if the declaration was never even referenced from
20859          within this entire compilation unit.  We suppress these DIEs in
20860          order to save space in the .debug section (by eliminating entries
20861          which are probably useless).  Note that we must not suppress
20862          block-local extern declarations (whether used or not) because that
20863          would screw-up the debugger's name lookup mechanism and cause it to
20864          miss things which really ought to be in scope at a given point.  */
20865       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
20866         return;
20867
20868       /* For local statics lookup proper context die.  */
20869       if (TREE_STATIC (decl) && decl_function_context (decl))
20870         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20871
20872       /* If we are in terse mode, don't generate any DIEs to represent any
20873          variable declarations or definitions.  */
20874       if (debug_info_level <= DINFO_LEVEL_TERSE)
20875         return;
20876       break;
20877
20878     case CONST_DECL:
20879       if (debug_info_level <= DINFO_LEVEL_TERSE)
20880         return;
20881       if (!is_fortran ())
20882         return;
20883       if (TREE_STATIC (decl) && decl_function_context (decl))
20884         context_die = lookup_decl_die (DECL_CONTEXT (decl));
20885       break;
20886
20887     case NAMESPACE_DECL:
20888     case IMPORTED_DECL:
20889       if (debug_info_level <= DINFO_LEVEL_TERSE)
20890         return;
20891       if (lookup_decl_die (decl) != NULL)
20892         return;
20893       break;
20894
20895     case TYPE_DECL:
20896       /* Don't emit stubs for types unless they are needed by other DIEs.  */
20897       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
20898         return;
20899
20900       /* Don't bother trying to generate any DIEs to represent any of the
20901          normal built-in types for the language we are compiling.  */
20902       if (DECL_IS_BUILTIN (decl))
20903         {
20904           /* OK, we need to generate one for `bool' so GDB knows what type
20905              comparisons have.  */
20906           if (is_cxx ()
20907               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
20908               && ! DECL_IGNORED_P (decl))
20909             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
20910
20911           return;
20912         }
20913
20914       /* If we are in terse mode, don't generate any DIEs for types.  */
20915       if (debug_info_level <= DINFO_LEVEL_TERSE)
20916         return;
20917
20918       /* If we're a function-scope tag, initially use a parent of NULL;
20919          this will be fixed up in decls_for_scope.  */
20920       if (decl_function_context (decl))
20921         context_die = NULL;
20922
20923       break;
20924
20925     default:
20926       return;
20927     }
20928
20929   gen_decl_die (decl, NULL, context_die);
20930 }
20931
20932 /* Write the debugging output for DECL.  */
20933
20934 static void
20935 dwarf2out_function_decl (tree decl)
20936 {
20937   dwarf2out_decl (decl);
20938
20939   htab_empty (decl_loc_table);
20940 }
20941
20942 /* Output a marker (i.e. a label) for the beginning of the generated code for
20943    a lexical block.  */
20944
20945 static void
20946 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
20947                        unsigned int blocknum)
20948 {
20949   switch_to_section (current_function_section ());
20950   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
20951 }
20952
20953 /* Output a marker (i.e. a label) for the end of the generated code for a
20954    lexical block.  */
20955
20956 static void
20957 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
20958 {
20959   switch_to_section (current_function_section ());
20960   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
20961 }
20962
20963 /* Returns nonzero if it is appropriate not to emit any debugging
20964    information for BLOCK, because it doesn't contain any instructions.
20965
20966    Don't allow this for blocks with nested functions or local classes
20967    as we would end up with orphans, and in the presence of scheduling
20968    we may end up calling them anyway.  */
20969
20970 static bool
20971 dwarf2out_ignore_block (const_tree block)
20972 {
20973   tree decl;
20974   unsigned int i;
20975
20976   for (decl = BLOCK_VARS (block); decl; decl = DECL_CHAIN (decl))
20977     if (TREE_CODE (decl) == FUNCTION_DECL
20978         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20979       return 0;
20980   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
20981     {
20982       decl = BLOCK_NONLOCALIZED_VAR (block, i);
20983       if (TREE_CODE (decl) == FUNCTION_DECL
20984           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
20985       return 0;
20986     }
20987
20988   return 1;
20989 }
20990
20991 /* Hash table routines for file_hash.  */
20992
20993 static int
20994 file_table_eq (const void *p1_p, const void *p2_p)
20995 {
20996   const struct dwarf_file_data *const p1 =
20997     (const struct dwarf_file_data *) p1_p;
20998   const char *const p2 = (const char *) p2_p;
20999   return strcmp (p1->filename, p2) == 0;
21000 }
21001
21002 static hashval_t
21003 file_table_hash (const void *p_p)
21004 {
21005   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
21006   return htab_hash_string (p->filename);
21007 }
21008
21009 /* Lookup FILE_NAME (in the list of filenames that we know about here in
21010    dwarf2out.c) and return its "index".  The index of each (known) filename is
21011    just a unique number which is associated with only that one filename.  We
21012    need such numbers for the sake of generating labels (in the .debug_sfnames
21013    section) and references to those files numbers (in the .debug_srcinfo
21014    and.debug_macinfo sections).  If the filename given as an argument is not
21015    found in our current list, add it to the list and assign it the next
21016    available unique index number.  In order to speed up searches, we remember
21017    the index of the filename was looked up last.  This handles the majority of
21018    all searches.  */
21019
21020 static struct dwarf_file_data *
21021 lookup_filename (const char *file_name)
21022 {
21023   void ** slot;
21024   struct dwarf_file_data * created;
21025
21026   /* Check to see if the file name that was searched on the previous
21027      call matches this file name.  If so, return the index.  */
21028   if (file_table_last_lookup
21029       && (file_name == file_table_last_lookup->filename
21030           || strcmp (file_table_last_lookup->filename, file_name) == 0))
21031     return file_table_last_lookup;
21032
21033   /* Didn't match the previous lookup, search the table.  */
21034   slot = htab_find_slot_with_hash (file_table, file_name,
21035                                    htab_hash_string (file_name), INSERT);
21036   if (*slot)
21037     return (struct dwarf_file_data *) *slot;
21038
21039   created = ggc_alloc_dwarf_file_data ();
21040   created->filename = file_name;
21041   created->emitted_number = 0;
21042   *slot = created;
21043   return created;
21044 }
21045
21046 /* If the assembler will construct the file table, then translate the compiler
21047    internal file table number into the assembler file table number, and emit
21048    a .file directive if we haven't already emitted one yet.  The file table
21049    numbers are different because we prune debug info for unused variables and
21050    types, which may include filenames.  */
21051
21052 static int
21053 maybe_emit_file (struct dwarf_file_data * fd)
21054 {
21055   if (! fd->emitted_number)
21056     {
21057       if (last_emitted_file)
21058         fd->emitted_number = last_emitted_file->emitted_number + 1;
21059       else
21060         fd->emitted_number = 1;
21061       last_emitted_file = fd;
21062
21063       if (DWARF2_ASM_LINE_DEBUG_INFO)
21064         {
21065           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
21066           output_quoted_string (asm_out_file,
21067                                 remap_debug_filename (fd->filename));
21068           fputc ('\n', asm_out_file);
21069         }
21070     }
21071
21072   return fd->emitted_number;
21073 }
21074
21075 /* Schedule generation of a DW_AT_const_value attribute to DIE.
21076    That generation should happen after function debug info has been
21077    generated. The value of the attribute is the constant value of ARG.  */
21078
21079 static void
21080 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
21081 {
21082   die_arg_entry entry;
21083
21084   if (!die || !arg)
21085     return;
21086
21087   if (!tmpl_value_parm_die_table)
21088     tmpl_value_parm_die_table
21089       = VEC_alloc (die_arg_entry, gc, 32);
21090
21091   entry.die = die;
21092   entry.arg = arg;
21093   VEC_safe_push (die_arg_entry, gc,
21094                  tmpl_value_parm_die_table,
21095                  &entry);
21096 }
21097
21098 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
21099    by append_entry_to_tmpl_value_parm_die_table. This function must
21100    be called after function DIEs have been generated.  */
21101
21102 static void
21103 gen_remaining_tmpl_value_param_die_attribute (void)
21104 {
21105   if (tmpl_value_parm_die_table)
21106     {
21107       unsigned i;
21108       die_arg_entry *e;
21109
21110       for (i = 0;
21111            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
21112            i++)
21113         tree_add_const_value_attribute (e->die, e->arg);
21114     }
21115 }
21116
21117
21118 /* Replace DW_AT_name for the decl with name.  */
21119
21120 static void
21121 dwarf2out_set_name (tree decl, tree name)
21122 {
21123   dw_die_ref die;
21124   dw_attr_ref attr;
21125   const char *dname;
21126
21127   die = TYPE_SYMTAB_DIE (decl);
21128   if (!die)
21129     return;
21130
21131   dname = dwarf2_name (name, 0);
21132   if (!dname)
21133     return;
21134
21135   attr = get_AT (die, DW_AT_name);
21136   if (attr)
21137     {
21138       struct indirect_string_node *node;
21139
21140       node = find_AT_string (dname);
21141       /* replace the string.  */
21142       attr->dw_attr_val.v.val_str = node;
21143     }
21144
21145   else
21146     add_name_attribute (die, dname);
21147 }
21148
21149 /* Called by the final INSN scan whenever we see a direct function call.
21150    Make an entry into the direct call table, recording the point of call
21151    and a reference to the target function's debug entry.  */
21152
21153 static void
21154 dwarf2out_direct_call (tree targ)
21155 {
21156   dcall_entry e;
21157   tree origin = decl_ultimate_origin (targ);
21158
21159   /* If this is a clone, use the abstract origin as the target.  */
21160   if (origin)
21161     targ = origin;
21162
21163   e.poc_label_num = poc_label_num++;
21164   e.poc_decl = current_function_decl;
21165   e.targ_die = force_decl_die (targ);
21166   VEC_safe_push (dcall_entry, gc, dcall_table, &e);
21167
21168   /* Drop a label at the return point to mark the point of call.  */
21169   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21170 }
21171
21172 /* Returns a hash value for X (which really is a struct vcall_insn).  */
21173
21174 static hashval_t
21175 vcall_insn_table_hash (const void *x)
21176 {
21177   return (hashval_t) ((const struct vcall_insn *) x)->insn_uid;
21178 }
21179
21180 /* Return nonzero if insn_uid of struct vcall_insn *X is the same as
21181    insnd_uid of *Y.  */
21182
21183 static int
21184 vcall_insn_table_eq (const void *x, const void *y)
21185 {
21186   return (((const struct vcall_insn *) x)->insn_uid
21187           == ((const struct vcall_insn *) y)->insn_uid);
21188 }
21189
21190 /* Associate VTABLE_SLOT with INSN_UID in the VCALL_INSN_TABLE.  */
21191
21192 static void
21193 store_vcall_insn (unsigned int vtable_slot, int insn_uid)
21194 {
21195   struct vcall_insn *item = ggc_alloc_vcall_insn ();
21196   struct vcall_insn **slot;
21197
21198   gcc_assert (item);
21199   item->insn_uid = insn_uid;
21200   item->vtable_slot = vtable_slot;
21201   slot = (struct vcall_insn **)
21202       htab_find_slot_with_hash (vcall_insn_table, &item,
21203                                 (hashval_t) insn_uid, INSERT);
21204   *slot = item;
21205 }
21206
21207 /* Return the VTABLE_SLOT associated with INSN_UID.  */
21208
21209 static unsigned int
21210 lookup_vcall_insn (unsigned int insn_uid)
21211 {
21212   struct vcall_insn item;
21213   struct vcall_insn *p;
21214
21215   item.insn_uid = insn_uid;
21216   item.vtable_slot = 0;
21217   p = (struct vcall_insn *) htab_find_with_hash (vcall_insn_table,
21218                                                  (void *) &item,
21219                                                  (hashval_t) insn_uid);
21220   if (p == NULL)
21221     return (unsigned int) -1;
21222   return p->vtable_slot;
21223 }
21224
21225
21226 /* Called when lowering indirect calls to RTL.  We make a note of INSN_UID
21227    and the OBJ_TYPE_REF_TOKEN from ADDR.  For C++ virtual calls, the token
21228    is the vtable slot index that we will need to put in the virtual call
21229    table later.  */
21230
21231 static void
21232 dwarf2out_virtual_call_token (tree addr, int insn_uid)
21233 {
21234   if (is_cxx() && TREE_CODE (addr) == OBJ_TYPE_REF)
21235     {
21236       tree token = OBJ_TYPE_REF_TOKEN (addr);
21237       if (TREE_CODE (token) == INTEGER_CST)
21238         store_vcall_insn (TREE_INT_CST_LOW (token), insn_uid);
21239     }
21240 }
21241
21242 /* Called when scheduling RTL, when a CALL_INSN is split.  Copies the
21243    OBJ_TYPE_REF_TOKEN previously associated with OLD_INSN and associates it
21244    with NEW_INSN.  */
21245
21246 static void
21247 dwarf2out_copy_call_info (rtx old_insn, rtx new_insn)
21248 {
21249   unsigned int vtable_slot = lookup_vcall_insn (INSN_UID (old_insn));
21250
21251   if (vtable_slot != (unsigned int) -1)
21252     store_vcall_insn (vtable_slot, INSN_UID (new_insn));
21253 }
21254
21255 /* Called by the final INSN scan whenever we see a virtual function call.
21256    Make an entry into the virtual call table, recording the point of call
21257    and the slot index of the vtable entry used to call the virtual member
21258    function.  The slot index was associated with the INSN_UID during the
21259    lowering to RTL.  */
21260
21261 static void
21262 dwarf2out_virtual_call (int insn_uid)
21263 {
21264   unsigned int vtable_slot = lookup_vcall_insn (insn_uid);
21265   vcall_entry e;
21266
21267   if (vtable_slot == (unsigned int) -1)
21268     return;
21269
21270   e.poc_label_num = poc_label_num++;
21271   e.vtable_slot = vtable_slot;
21272   VEC_safe_push (vcall_entry, gc, vcall_table, &e);
21273
21274   /* Drop a label at the return point to mark the point of call.  */
21275   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LPOC", e.poc_label_num);
21276 }
21277
21278 /* Called by the final INSN scan whenever we see a var location.  We
21279    use it to drop labels in the right places, and throw the location in
21280    our lookup table.  */
21281
21282 static void
21283 dwarf2out_var_location (rtx loc_note)
21284 {
21285   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
21286   struct var_loc_node *newloc;
21287   rtx next_real;
21288   static const char *last_label;
21289   static const char *last_postcall_label;
21290   static bool last_in_cold_section_p;
21291   tree decl;
21292
21293   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
21294     return;
21295
21296   next_real = next_real_insn (loc_note);
21297   /* If there are no instructions which would be affected by this note,
21298      don't do anything.  */
21299   if (next_real == NULL_RTX && !NOTE_DURING_CALL_P (loc_note))
21300     return;
21301
21302   /* If there were any real insns between note we processed last time
21303      and this note (or if it is the first note), clear
21304      last_{,postcall_}label so that they are not reused this time.  */
21305   if (last_var_location_insn == NULL_RTX
21306       || last_var_location_insn != next_real
21307       || last_in_cold_section_p != in_cold_section_p)
21308     {
21309       last_label = NULL;
21310       last_postcall_label = NULL;
21311     }
21312
21313   decl = NOTE_VAR_LOCATION_DECL (loc_note);
21314   newloc = add_var_loc_to_decl (decl, loc_note,
21315                                 NOTE_DURING_CALL_P (loc_note)
21316                                 ? last_postcall_label : last_label);
21317   if (newloc == NULL)
21318     return;
21319
21320   /* If there were no real insns between note we processed last time
21321      and this note, use the label we emitted last time.  Otherwise
21322      create a new label and emit it.  */
21323   if (last_label == NULL)
21324     {
21325       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
21326       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
21327       loclabel_num++;
21328       last_label = ggc_strdup (loclabel);
21329     }
21330
21331   if (!NOTE_DURING_CALL_P (loc_note))
21332     newloc->label = last_label;
21333   else
21334     {
21335       if (!last_postcall_label)
21336         {
21337           sprintf (loclabel, "%s-1", last_label);
21338           last_postcall_label = ggc_strdup (loclabel);
21339         }
21340       newloc->label = last_postcall_label;
21341     }
21342
21343   last_var_location_insn = next_real;
21344   last_in_cold_section_p = in_cold_section_p;
21345 }
21346
21347 /* We need to reset the locations at the beginning of each
21348    function. We can't do this in the end_function hook, because the
21349    declarations that use the locations won't have been output when
21350    that hook is called.  Also compute have_multiple_function_sections here.  */
21351
21352 static void
21353 dwarf2out_begin_function (tree fun)
21354 {
21355   if (function_section (fun) != text_section)
21356     have_multiple_function_sections = true;
21357
21358   dwarf2out_note_section_used ();
21359 }
21360
21361 /* Output a label to mark the beginning of a source code line entry
21362    and record information relating to this source line, in
21363    'line_info_table' for later output of the .debug_line section.  */
21364
21365 static void
21366 dwarf2out_source_line (unsigned int line, const char *filename,
21367                        int discriminator, bool is_stmt)
21368 {
21369   static bool last_is_stmt = true;
21370
21371   if (debug_info_level >= DINFO_LEVEL_NORMAL
21372       && line != 0)
21373     {
21374       int file_num = maybe_emit_file (lookup_filename (filename));
21375
21376       switch_to_section (current_function_section ());
21377
21378       /* If requested, emit something human-readable.  */
21379       if (flag_debug_asm)
21380         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
21381                  filename, line);
21382
21383       if (DWARF2_ASM_LINE_DEBUG_INFO)
21384         {
21385           /* Emit the .loc directive understood by GNU as.  */
21386           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
21387           if (is_stmt != last_is_stmt)
21388             {
21389               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
21390               last_is_stmt = is_stmt;
21391             }
21392           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
21393             fprintf (asm_out_file, " discriminator %d", discriminator);
21394           fputc ('\n', asm_out_file);
21395
21396           /* Indicate that line number info exists.  */
21397           line_info_table_in_use++;
21398         }
21399       else if (function_section (current_function_decl) != text_section)
21400         {
21401           dw_separate_line_info_ref line_info;
21402           targetm.asm_out.internal_label (asm_out_file,
21403                                           SEPARATE_LINE_CODE_LABEL,
21404                                           separate_line_info_table_in_use);
21405
21406           /* Expand the line info table if necessary.  */
21407           if (separate_line_info_table_in_use
21408               == separate_line_info_table_allocated)
21409             {
21410               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21411               separate_line_info_table
21412                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
21413                                  separate_line_info_table,
21414                                  separate_line_info_table_allocated);
21415               memset (separate_line_info_table
21416                        + separate_line_info_table_in_use,
21417                       0,
21418                       (LINE_INFO_TABLE_INCREMENT
21419                        * sizeof (dw_separate_line_info_entry)));
21420             }
21421
21422           /* Add the new entry at the end of the line_info_table.  */
21423           line_info
21424             = &separate_line_info_table[separate_line_info_table_in_use++];
21425           line_info->dw_file_num = file_num;
21426           line_info->dw_line_num = line;
21427           line_info->function = current_function_funcdef_no;
21428         }
21429       else
21430         {
21431           dw_line_info_ref line_info;
21432
21433           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
21434                                      line_info_table_in_use);
21435
21436           /* Expand the line info table if necessary.  */
21437           if (line_info_table_in_use == line_info_table_allocated)
21438             {
21439               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
21440               line_info_table
21441                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
21442                                  line_info_table_allocated);
21443               memset (line_info_table + line_info_table_in_use, 0,
21444                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
21445             }
21446
21447           /* Add the new entry at the end of the line_info_table.  */
21448           line_info = &line_info_table[line_info_table_in_use++];
21449           line_info->dw_file_num = file_num;
21450           line_info->dw_line_num = line;
21451         }
21452     }
21453 }
21454
21455 /* Record the beginning of a new source file.  */
21456
21457 static void
21458 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
21459 {
21460   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21461     {
21462       /* Record the beginning of the file for break_out_includes.  */
21463       dw_die_ref bincl_die;
21464
21465       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
21466       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
21467     }
21468
21469   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21470     {
21471       int file_num = maybe_emit_file (lookup_filename (filename));
21472
21473       switch_to_section (debug_macinfo_section);
21474       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
21475       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
21476                                    lineno);
21477
21478       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
21479     }
21480 }
21481
21482 /* Record the end of a source file.  */
21483
21484 static void
21485 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
21486 {
21487   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
21488     /* Record the end of the file for break_out_includes.  */
21489     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
21490
21491   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21492     {
21493       switch_to_section (debug_macinfo_section);
21494       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
21495     }
21496 }
21497
21498 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
21499    the tail part of the directive line, i.e. the part which is past the
21500    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21501
21502 static void
21503 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
21504                   const char *buffer ATTRIBUTE_UNUSED)
21505 {
21506   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21507     {
21508       switch_to_section (debug_macinfo_section);
21509       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
21510       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21511       dw2_asm_output_nstring (buffer, -1, "The macro");
21512     }
21513 }
21514
21515 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
21516    the tail part of the directive line, i.e. the part which is past the
21517    initial whitespace, #, whitespace, directive-name, whitespace part.  */
21518
21519 static void
21520 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
21521                  const char *buffer ATTRIBUTE_UNUSED)
21522 {
21523   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21524     {
21525       switch_to_section (debug_macinfo_section);
21526       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
21527       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
21528       dw2_asm_output_nstring (buffer, -1, "The macro");
21529     }
21530 }
21531
21532 /* Set up for Dwarf output at the start of compilation.  */
21533
21534 static void
21535 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
21536 {
21537   /* Allocate the file_table.  */
21538   file_table = htab_create_ggc (50, file_table_hash,
21539                                 file_table_eq, NULL);
21540
21541   /* Allocate the decl_die_table.  */
21542   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
21543                                     decl_die_table_eq, NULL);
21544
21545   /* Allocate the decl_loc_table.  */
21546   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
21547                                     decl_loc_table_eq, NULL);
21548
21549   /* Allocate the initial hunk of the decl_scope_table.  */
21550   decl_scope_table = VEC_alloc (tree, gc, 256);
21551
21552   /* Allocate the initial hunk of the abbrev_die_table.  */
21553   abbrev_die_table = ggc_alloc_cleared_vec_dw_die_ref
21554     (ABBREV_DIE_TABLE_INCREMENT);
21555   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
21556   /* Zero-th entry is allocated, but unused.  */
21557   abbrev_die_table_in_use = 1;
21558
21559   /* Allocate the initial hunk of the line_info_table.  */
21560   line_info_table = ggc_alloc_cleared_vec_dw_line_info_entry
21561     (LINE_INFO_TABLE_INCREMENT);
21562   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
21563
21564   /* Zero-th entry is allocated, but unused.  */
21565   line_info_table_in_use = 1;
21566
21567   /* Allocate the pubtypes and pubnames vectors.  */
21568   pubname_table = VEC_alloc (pubname_entry, gc, 32);
21569   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
21570
21571   /* Allocate the table that maps insn UIDs to vtable slot indexes.  */
21572   vcall_insn_table = htab_create_ggc (10, vcall_insn_table_hash,
21573                                       vcall_insn_table_eq, NULL);
21574
21575   /* Generate the initial DIE for the .debug section.  Note that the (string)
21576      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
21577      will (typically) be a relative pathname and that this pathname should be
21578      taken as being relative to the directory from which the compiler was
21579      invoked when the given (base) source file was compiled.  We will fill
21580      in this value in dwarf2out_finish.  */
21581   comp_unit_die = gen_compile_unit_die (NULL);
21582
21583   incomplete_types = VEC_alloc (tree, gc, 64);
21584
21585   used_rtx_array = VEC_alloc (rtx, gc, 32);
21586
21587   debug_info_section = get_section (DEBUG_INFO_SECTION,
21588                                     SECTION_DEBUG, NULL);
21589   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
21590                                       SECTION_DEBUG, NULL);
21591   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
21592                                        SECTION_DEBUG, NULL);
21593   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
21594                                        SECTION_DEBUG, NULL);
21595   debug_line_section = get_section (DEBUG_LINE_SECTION,
21596                                     SECTION_DEBUG, NULL);
21597   debug_loc_section = get_section (DEBUG_LOC_SECTION,
21598                                    SECTION_DEBUG, NULL);
21599   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
21600                                         SECTION_DEBUG, NULL);
21601   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
21602                                         SECTION_DEBUG, NULL);
21603   debug_dcall_section = get_section (DEBUG_DCALL_SECTION,
21604                                      SECTION_DEBUG, NULL);
21605   debug_vcall_section = get_section (DEBUG_VCALL_SECTION,
21606                                      SECTION_DEBUG, NULL);
21607   debug_str_section = get_section (DEBUG_STR_SECTION,
21608                                    DEBUG_STR_SECTION_FLAGS, NULL);
21609   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
21610                                       SECTION_DEBUG, NULL);
21611   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
21612                                      SECTION_DEBUG, NULL);
21613
21614   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
21615   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
21616                                DEBUG_ABBREV_SECTION_LABEL, 0);
21617   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
21618   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
21619                                COLD_TEXT_SECTION_LABEL, 0);
21620   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
21621
21622   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
21623                                DEBUG_INFO_SECTION_LABEL, 0);
21624   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
21625                                DEBUG_LINE_SECTION_LABEL, 0);
21626   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
21627                                DEBUG_RANGES_SECTION_LABEL, 0);
21628   switch_to_section (debug_abbrev_section);
21629   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
21630   switch_to_section (debug_info_section);
21631   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
21632   switch_to_section (debug_line_section);
21633   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
21634
21635   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
21636     {
21637       switch_to_section (debug_macinfo_section);
21638       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
21639                                    DEBUG_MACINFO_SECTION_LABEL, 0);
21640       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
21641     }
21642
21643   switch_to_section (text_section);
21644   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
21645   if (flag_reorder_blocks_and_partition)
21646     {
21647       cold_text_section = unlikely_text_section ();
21648       switch_to_section (cold_text_section);
21649       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
21650     }
21651
21652 }
21653
21654 /* Called before cgraph_optimize starts outputtting functions, variables
21655    and toplevel asms into assembly.  */
21656
21657 static void
21658 dwarf2out_assembly_start (void)
21659 {
21660   if (HAVE_GAS_CFI_SECTIONS_DIRECTIVE && dwarf2out_do_cfi_asm ())
21661     {
21662 #ifndef TARGET_UNWIND_INFO
21663       if (USING_SJLJ_EXCEPTIONS || (!flag_unwind_tables && !flag_exceptions))
21664 #endif
21665         fprintf (asm_out_file, "\t.cfi_sections\t.debug_frame\n");
21666     }
21667 }
21668
21669 /* A helper function for dwarf2out_finish called through
21670    htab_traverse.  Emit one queued .debug_str string.  */
21671
21672 static int
21673 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21674 {
21675   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21676
21677   if (node->label && node->refcount)
21678     {
21679       switch_to_section (debug_str_section);
21680       ASM_OUTPUT_LABEL (asm_out_file, node->label);
21681       assemble_string (node->str, strlen (node->str) + 1);
21682     }
21683
21684   return 1;
21685 }
21686
21687 #if ENABLE_ASSERT_CHECKING
21688 /* Verify that all marks are clear.  */
21689
21690 static void
21691 verify_marks_clear (dw_die_ref die)
21692 {
21693   dw_die_ref c;
21694
21695   gcc_assert (! die->die_mark);
21696   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
21697 }
21698 #endif /* ENABLE_ASSERT_CHECKING */
21699
21700 /* Clear the marks for a die and its children.
21701    Be cool if the mark isn't set.  */
21702
21703 static void
21704 prune_unmark_dies (dw_die_ref die)
21705 {
21706   dw_die_ref c;
21707
21708   if (die->die_mark)
21709     die->die_mark = 0;
21710   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
21711 }
21712
21713 /* Given DIE that we're marking as used, find any other dies
21714    it references as attributes and mark them as used.  */
21715
21716 static void
21717 prune_unused_types_walk_attribs (dw_die_ref die)
21718 {
21719   dw_attr_ref a;
21720   unsigned ix;
21721
21722   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21723     {
21724       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
21725         {
21726           /* A reference to another DIE.
21727              Make sure that it will get emitted.
21728              If it was broken out into a comdat group, don't follow it.  */
21729           if (dwarf_version < 4
21730               || a->dw_attr == DW_AT_specification
21731               || a->dw_attr_val.v.val_die_ref.die->die_id.die_type_node == NULL)
21732             prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
21733         }
21734       /* Set the string's refcount to 0 so that prune_unused_types_mark
21735          accounts properly for it.  */
21736       if (AT_class (a) == dw_val_class_str)
21737         a->dw_attr_val.v.val_str->refcount = 0;
21738     }
21739 }
21740
21741
21742 /* Mark DIE as being used.  If DOKIDS is true, then walk down
21743    to DIE's children.  */
21744
21745 static void
21746 prune_unused_types_mark (dw_die_ref die, int dokids)
21747 {
21748   dw_die_ref c;
21749
21750   if (die->die_mark == 0)
21751     {
21752       /* We haven't done this node yet.  Mark it as used.  */
21753       die->die_mark = 1;
21754
21755       /* We also have to mark its parents as used.
21756          (But we don't want to mark our parents' kids due to this.)  */
21757       if (die->die_parent)
21758         prune_unused_types_mark (die->die_parent, 0);
21759
21760       /* Mark any referenced nodes.  */
21761       prune_unused_types_walk_attribs (die);
21762
21763       /* If this node is a specification,
21764          also mark the definition, if it exists.  */
21765       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
21766         prune_unused_types_mark (die->die_definition, 1);
21767     }
21768
21769   if (dokids && die->die_mark != 2)
21770     {
21771       /* We need to walk the children, but haven't done so yet.
21772          Remember that we've walked the kids.  */
21773       die->die_mark = 2;
21774
21775       /* If this is an array type, we need to make sure our
21776          kids get marked, even if they're types.  If we're
21777          breaking out types into comdat sections, do this
21778          for all type definitions.  */
21779       if (die->die_tag == DW_TAG_array_type
21780           || (dwarf_version >= 4
21781               && is_type_die (die) && ! is_declaration_die (die)))
21782         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
21783       else
21784         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21785     }
21786 }
21787
21788 /* For local classes, look if any static member functions were emitted
21789    and if so, mark them.  */
21790
21791 static void
21792 prune_unused_types_walk_local_classes (dw_die_ref die)
21793 {
21794   dw_die_ref c;
21795
21796   if (die->die_mark == 2)
21797     return;
21798
21799   switch (die->die_tag)
21800     {
21801     case DW_TAG_structure_type:
21802     case DW_TAG_union_type:
21803     case DW_TAG_class_type:
21804       break;
21805
21806     case DW_TAG_subprogram:
21807       if (!get_AT_flag (die, DW_AT_declaration)
21808           || die->die_definition != NULL)
21809         prune_unused_types_mark (die, 1);
21810       return;
21811
21812     default:
21813       return;
21814     }
21815
21816   /* Mark children.  */
21817   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
21818 }
21819
21820 /* Walk the tree DIE and mark types that we actually use.  */
21821
21822 static void
21823 prune_unused_types_walk (dw_die_ref die)
21824 {
21825   dw_die_ref c;
21826
21827   /* Don't do anything if this node is already marked and
21828      children have been marked as well.  */
21829   if (die->die_mark == 2)
21830     return;
21831
21832   switch (die->die_tag)
21833     {
21834     case DW_TAG_structure_type:
21835     case DW_TAG_union_type:
21836     case DW_TAG_class_type:
21837       if (die->die_perennial_p)
21838         break;
21839
21840       for (c = die->die_parent; c; c = c->die_parent)
21841         if (c->die_tag == DW_TAG_subprogram)
21842           break;
21843
21844       /* Finding used static member functions inside of classes
21845          is needed just for local classes, because for other classes
21846          static member function DIEs with DW_AT_specification
21847          are emitted outside of the DW_TAG_*_type.  If we ever change
21848          it, we'd need to call this even for non-local classes.  */
21849       if (c)
21850         prune_unused_types_walk_local_classes (die);
21851
21852       /* It's a type node --- don't mark it.  */
21853       return;
21854
21855     case DW_TAG_const_type:
21856     case DW_TAG_packed_type:
21857     case DW_TAG_pointer_type:
21858     case DW_TAG_reference_type:
21859     case DW_TAG_rvalue_reference_type:
21860     case DW_TAG_volatile_type:
21861     case DW_TAG_typedef:
21862     case DW_TAG_array_type:
21863     case DW_TAG_interface_type:
21864     case DW_TAG_friend:
21865     case DW_TAG_variant_part:
21866     case DW_TAG_enumeration_type:
21867     case DW_TAG_subroutine_type:
21868     case DW_TAG_string_type:
21869     case DW_TAG_set_type:
21870     case DW_TAG_subrange_type:
21871     case DW_TAG_ptr_to_member_type:
21872     case DW_TAG_file_type:
21873       if (die->die_perennial_p)
21874         break;
21875
21876       /* It's a type node --- don't mark it.  */
21877       return;
21878
21879     default:
21880       /* Mark everything else.  */
21881       break;
21882   }
21883
21884   if (die->die_mark == 0)
21885     {
21886       die->die_mark = 1;
21887
21888       /* Now, mark any dies referenced from here.  */
21889       prune_unused_types_walk_attribs (die);
21890     }
21891
21892   die->die_mark = 2;
21893
21894   /* Mark children.  */
21895   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
21896 }
21897
21898 /* Increment the string counts on strings referred to from DIE's
21899    attributes.  */
21900
21901 static void
21902 prune_unused_types_update_strings (dw_die_ref die)
21903 {
21904   dw_attr_ref a;
21905   unsigned ix;
21906
21907   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
21908     if (AT_class (a) == dw_val_class_str)
21909       {
21910         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
21911         s->refcount++;
21912         /* Avoid unnecessarily putting strings that are used less than
21913            twice in the hash table.  */
21914         if (s->refcount
21915             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
21916           {
21917             void ** slot;
21918             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
21919                                              htab_hash_string (s->str),
21920                                              INSERT);
21921             gcc_assert (*slot == NULL);
21922             *slot = s;
21923           }
21924       }
21925 }
21926
21927 /* Remove from the tree DIE any dies that aren't marked.  */
21928
21929 static void
21930 prune_unused_types_prune (dw_die_ref die)
21931 {
21932   dw_die_ref c;
21933
21934   gcc_assert (die->die_mark);
21935   prune_unused_types_update_strings (die);
21936
21937   if (! die->die_child)
21938     return;
21939
21940   c = die->die_child;
21941   do {
21942     dw_die_ref prev = c;
21943     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
21944       if (c == die->die_child)
21945         {
21946           /* No marked children between 'prev' and the end of the list.  */
21947           if (prev == c)
21948             /* No marked children at all.  */
21949             die->die_child = NULL;
21950           else
21951             {
21952               prev->die_sib = c->die_sib;
21953               die->die_child = prev;
21954             }
21955           return;
21956         }
21957
21958     if (c != prev->die_sib)
21959       prev->die_sib = c;
21960     prune_unused_types_prune (c);
21961   } while (c != die->die_child);
21962 }
21963
21964 /* A helper function for dwarf2out_finish called through
21965    htab_traverse.  Clear .debug_str strings that we haven't already
21966    decided to emit.  */
21967
21968 static int
21969 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
21970 {
21971   struct indirect_string_node *node = (struct indirect_string_node *) *h;
21972
21973   if (!node->label || !node->refcount)
21974     htab_clear_slot (debug_str_hash, h);
21975
21976   return 1;
21977 }
21978
21979 /* Remove dies representing declarations that we never use.  */
21980
21981 static void
21982 prune_unused_types (void)
21983 {
21984   unsigned int i;
21985   limbo_die_node *node;
21986   comdat_type_node *ctnode;
21987   pubname_ref pub;
21988   dcall_entry *dcall;
21989
21990 #if ENABLE_ASSERT_CHECKING
21991   /* All the marks should already be clear.  */
21992   verify_marks_clear (comp_unit_die);
21993   for (node = limbo_die_list; node; node = node->next)
21994     verify_marks_clear (node->die);
21995   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
21996     verify_marks_clear (ctnode->root_die);
21997 #endif /* ENABLE_ASSERT_CHECKING */
21998
21999   /* Mark types that are used in global variables.  */
22000   premark_types_used_by_global_vars ();
22001
22002   /* Set the mark on nodes that are actually used.  */
22003   prune_unused_types_walk (comp_unit_die);
22004   for (node = limbo_die_list; node; node = node->next)
22005     prune_unused_types_walk (node->die);
22006   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22007     {
22008       prune_unused_types_walk (ctnode->root_die);
22009       prune_unused_types_mark (ctnode->type_die, 1);
22010     }
22011
22012   /* Also set the mark on nodes referenced from the
22013      pubname_table or arange_table.  */
22014   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
22015     prune_unused_types_mark (pub->die, 1);
22016   for (i = 0; i < arange_table_in_use; i++)
22017     prune_unused_types_mark (arange_table[i], 1);
22018
22019   /* Mark nodes referenced from the direct call table.  */
22020   for (i = 0; VEC_iterate (dcall_entry, dcall_table, i, dcall); i++)
22021     prune_unused_types_mark (dcall->targ_die, 1);
22022
22023   /* Get rid of nodes that aren't marked; and update the string counts.  */
22024   if (debug_str_hash && debug_str_hash_forced)
22025     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
22026   else if (debug_str_hash)
22027     htab_empty (debug_str_hash);
22028   prune_unused_types_prune (comp_unit_die);
22029   for (node = limbo_die_list; node; node = node->next)
22030     prune_unused_types_prune (node->die);
22031   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22032     prune_unused_types_prune (ctnode->root_die);
22033
22034   /* Leave the marks clear.  */
22035   prune_unmark_dies (comp_unit_die);
22036   for (node = limbo_die_list; node; node = node->next)
22037     prune_unmark_dies (node->die);
22038   for (ctnode = comdat_type_list; ctnode; ctnode = ctnode->next)
22039     prune_unmark_dies (ctnode->root_die);
22040 }
22041
22042 /* Set the parameter to true if there are any relative pathnames in
22043    the file table.  */
22044 static int
22045 file_table_relative_p (void ** slot, void *param)
22046 {
22047   bool *p = (bool *) param;
22048   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
22049   if (!IS_ABSOLUTE_PATH (d->filename))
22050     {
22051       *p = true;
22052       return 0;
22053     }
22054   return 1;
22055 }
22056
22057 /* Routines to manipulate hash table of comdat type units.  */
22058
22059 static hashval_t
22060 htab_ct_hash (const void *of)
22061 {
22062   hashval_t h;
22063   const comdat_type_node *const type_node = (const comdat_type_node *) of;
22064
22065   memcpy (&h, type_node->signature, sizeof (h));
22066   return h;
22067 }
22068
22069 static int
22070 htab_ct_eq (const void *of1, const void *of2)
22071 {
22072   const comdat_type_node *const type_node_1 = (const comdat_type_node *) of1;
22073   const comdat_type_node *const type_node_2 = (const comdat_type_node *) of2;
22074
22075   return (! memcmp (type_node_1->signature, type_node_2->signature,
22076                     DWARF_TYPE_SIGNATURE_SIZE));
22077 }
22078
22079 /* Move a DW_AT_{,MIPS_}linkage_name attribute just added to dw_die_ref
22080    to the location it would have been added, should we know its
22081    DECL_ASSEMBLER_NAME when we added other attributes.  This will
22082    probably improve compactness of debug info, removing equivalent
22083    abbrevs, and hide any differences caused by deferring the
22084    computation of the assembler name, triggered by e.g. PCH.  */
22085
22086 static inline void
22087 move_linkage_attr (dw_die_ref die)
22088 {
22089   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
22090   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
22091
22092   gcc_assert (linkage.dw_attr == DW_AT_linkage_name
22093               || linkage.dw_attr == DW_AT_MIPS_linkage_name);
22094
22095   while (--ix > 0)
22096     {
22097       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
22098
22099       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
22100         break;
22101     }
22102
22103   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
22104     {
22105       VEC_pop (dw_attr_node, die->die_attr);
22106       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
22107     }
22108 }
22109
22110 /* Helper function for resolve_addr, attempt to resolve
22111    one CONST_STRING, return non-zero if not successful.  Similarly verify that
22112    SYMBOL_REFs refer to variables emitted in the current CU.  */
22113
22114 static int
22115 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
22116 {
22117   rtx rtl = *addr;
22118
22119   if (GET_CODE (rtl) == CONST_STRING)
22120     {
22121       size_t len = strlen (XSTR (rtl, 0)) + 1;
22122       tree t = build_string (len, XSTR (rtl, 0));
22123       tree tlen = build_int_cst (NULL_TREE, len - 1);
22124       TREE_TYPE (t)
22125         = build_array_type (char_type_node, build_index_type (tlen));
22126       rtl = lookup_constant_def (t);
22127       if (!rtl || !MEM_P (rtl))
22128         return 1;
22129       rtl = XEXP (rtl, 0);
22130       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
22131       *addr = rtl;
22132       return 0;
22133     }
22134
22135   if (GET_CODE (rtl) == SYMBOL_REF
22136       && SYMBOL_REF_DECL (rtl)
22137       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
22138     return 1;
22139
22140   if (GET_CODE (rtl) == CONST
22141       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
22142     return 1;
22143
22144   return 0;
22145 }
22146
22147 /* Helper function for resolve_addr, handle one location
22148    expression, return false if at least one CONST_STRING or SYMBOL_REF in
22149    the location list couldn't be resolved.  */
22150
22151 static bool
22152 resolve_addr_in_expr (dw_loc_descr_ref loc)
22153 {
22154   for (; loc; loc = loc->dw_loc_next)
22155     if (((loc->dw_loc_opc == DW_OP_addr || loc->dtprel)
22156          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
22157         || (loc->dw_loc_opc == DW_OP_implicit_value
22158             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
22159             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
22160       return false;
22161   return true;
22162 }
22163
22164 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
22165    an address in .rodata section if the string literal is emitted there,
22166    or remove the containing location list or replace DW_AT_const_value
22167    with DW_AT_location and empty location expression, if it isn't found
22168    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
22169    to something that has been emitted in the current CU.  */
22170
22171 static void
22172 resolve_addr (dw_die_ref die)
22173 {
22174   dw_die_ref c;
22175   dw_attr_ref a;
22176   dw_loc_list_ref *curr;
22177   unsigned ix;
22178
22179   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
22180     switch (AT_class (a))
22181       {
22182       case dw_val_class_loc_list:
22183         curr = AT_loc_list_ptr (a);
22184         while (*curr)
22185           {
22186             if (!resolve_addr_in_expr ((*curr)->expr))
22187               {
22188                 dw_loc_list_ref next = (*curr)->dw_loc_next;
22189                 if (next && (*curr)->ll_symbol)
22190                   {
22191                     gcc_assert (!next->ll_symbol);
22192                     next->ll_symbol = (*curr)->ll_symbol;
22193                   }
22194                 *curr = next;
22195               }
22196             else
22197               curr = &(*curr)->dw_loc_next;
22198           }
22199         if (!AT_loc_list (a))
22200           {
22201             remove_AT (die, a->dw_attr);
22202             ix--;
22203           }
22204         break;
22205       case dw_val_class_loc:
22206         if (!resolve_addr_in_expr (AT_loc (a)))
22207           {
22208             remove_AT (die, a->dw_attr);
22209             ix--;
22210           }
22211         break;
22212       case dw_val_class_addr:
22213         if (a->dw_attr == DW_AT_const_value
22214             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
22215           {
22216             remove_AT (die, a->dw_attr);
22217             ix--;
22218           }
22219         break;
22220       default:
22221         break;
22222       }
22223
22224   FOR_EACH_CHILD (die, c, resolve_addr (c));
22225 }
22226
22227 /* Output stuff that dwarf requires at the end of every file,
22228    and generate the DWARF-2 debugging info.  */
22229
22230 static void
22231 dwarf2out_finish (const char *filename)
22232 {
22233   limbo_die_node *node, *next_node;
22234   comdat_type_node *ctnode;
22235   htab_t comdat_type_table;
22236   dw_die_ref die = 0;
22237   unsigned int i;
22238
22239   gen_remaining_tmpl_value_param_die_attribute ();
22240
22241   /* Add the name for the main input file now.  We delayed this from
22242      dwarf2out_init to avoid complications with PCH.  */
22243   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
22244   if (!IS_ABSOLUTE_PATH (filename))
22245     add_comp_dir_attribute (comp_unit_die);
22246   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
22247     {
22248       bool p = false;
22249       htab_traverse (file_table, file_table_relative_p, &p);
22250       if (p)
22251         add_comp_dir_attribute (comp_unit_die);
22252     }
22253
22254   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
22255     {
22256       add_location_or_const_value_attribute (
22257         VEC_index (deferred_locations, deferred_locations_list, i)->die,
22258         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
22259         DW_AT_location);
22260     }
22261
22262   /* Traverse the limbo die list, and add parent/child links.  The only
22263      dies without parents that should be here are concrete instances of
22264      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
22265      For concrete instances, we can get the parent die from the abstract
22266      instance.  */
22267   for (node = limbo_die_list; node; node = next_node)
22268     {
22269       next_node = node->next;
22270       die = node->die;
22271
22272       if (die->die_parent == NULL)
22273         {
22274           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
22275
22276           if (origin)
22277             add_child_die (origin->die_parent, die);
22278           else if (die == comp_unit_die)
22279             ;
22280           else if (seen_error ())
22281             /* It's OK to be confused by errors in the input.  */
22282             add_child_die (comp_unit_die, die);
22283           else
22284             {
22285               /* In certain situations, the lexical block containing a
22286                  nested function can be optimized away, which results
22287                  in the nested function die being orphaned.  Likewise
22288                  with the return type of that nested function.  Force
22289                  this to be a child of the containing function.
22290
22291                  It may happen that even the containing function got fully
22292                  inlined and optimized out.  In that case we are lost and
22293                  assign the empty child.  This should not be big issue as
22294                  the function is likely unreachable too.  */
22295               tree context = NULL_TREE;
22296
22297               gcc_assert (node->created_for);
22298
22299               if (DECL_P (node->created_for))
22300                 context = DECL_CONTEXT (node->created_for);
22301               else if (TYPE_P (node->created_for))
22302                 context = TYPE_CONTEXT (node->created_for);
22303
22304               gcc_assert (context
22305                           && (TREE_CODE (context) == FUNCTION_DECL
22306                               || TREE_CODE (context) == NAMESPACE_DECL));
22307
22308               origin = lookup_decl_die (context);
22309               if (origin)
22310                 add_child_die (origin, die);
22311               else
22312                 add_child_die (comp_unit_die, die);
22313             }
22314         }
22315     }
22316
22317   limbo_die_list = NULL;
22318
22319   resolve_addr (comp_unit_die);
22320
22321   for (node = deferred_asm_name; node; node = node->next)
22322     {
22323       tree decl = node->created_for;
22324       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
22325         {
22326           add_linkage_attr (node->die, decl);
22327           move_linkage_attr (node->die);
22328         }
22329     }
22330
22331   deferred_asm_name = NULL;
22332
22333   /* Walk through the list of incomplete types again, trying once more to
22334      emit full debugging info for them.  */
22335   retry_incomplete_types ();
22336
22337   if (flag_eliminate_unused_debug_types)
22338     prune_unused_types ();
22339
22340   /* Generate separate CUs for each of the include files we've seen.
22341      They will go into limbo_die_list.  */
22342   if (flag_eliminate_dwarf2_dups && dwarf_version < 4)
22343     break_out_includes (comp_unit_die);
22344
22345   /* Generate separate COMDAT sections for type DIEs. */
22346   if (dwarf_version >= 4)
22347     {
22348       break_out_comdat_types (comp_unit_die);
22349
22350       /* Each new type_unit DIE was added to the limbo die list when created.
22351          Since these have all been added to comdat_type_list, clear the
22352          limbo die list.  */
22353       limbo_die_list = NULL;
22354
22355       /* For each new comdat type unit, copy declarations for incomplete
22356          types to make the new unit self-contained (i.e., no direct
22357          references to the main compile unit).  */
22358       for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22359         copy_decls_for_unworthy_types (ctnode->root_die);
22360       copy_decls_for_unworthy_types (comp_unit_die);
22361
22362       /* In the process of copying declarations from one unit to another,
22363          we may have left some declarations behind that are no longer
22364          referenced.  Prune them.  */
22365       prune_unused_types ();
22366     }
22367
22368   /* Traverse the DIE's and add add sibling attributes to those DIE's
22369      that have children.  */
22370   add_sibling_attributes (comp_unit_die);
22371   for (node = limbo_die_list; node; node = node->next)
22372     add_sibling_attributes (node->die);
22373   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22374     add_sibling_attributes (ctnode->root_die);
22375
22376   /* Output a terminator label for the .text section.  */
22377   switch_to_section (text_section);
22378   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
22379   if (flag_reorder_blocks_and_partition)
22380     {
22381       switch_to_section (unlikely_text_section ());
22382       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
22383     }
22384
22385   /* We can only use the low/high_pc attributes if all of the code was
22386      in .text.  */
22387   if (!have_multiple_function_sections
22388       || !(dwarf_version >= 3 || !dwarf_strict))
22389     {
22390       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
22391       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
22392     }
22393
22394   else
22395     {
22396       unsigned fde_idx = 0;
22397       bool range_list_added = false;
22398
22399       /* We need to give .debug_loc and .debug_ranges an appropriate
22400          "base address".  Use zero so that these addresses become
22401          absolute.  Historically, we've emitted the unexpected
22402          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
22403          Emit both to give time for other tools to adapt.  */
22404       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
22405       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
22406
22407       if (text_section_used)
22408         add_ranges_by_labels (comp_unit_die, text_section_label,
22409                               text_end_label, &range_list_added);
22410       if (flag_reorder_blocks_and_partition && cold_text_section_used)
22411         add_ranges_by_labels (comp_unit_die, cold_text_section_label,
22412                               cold_end_label, &range_list_added);
22413
22414       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
22415         {
22416           dw_fde_ref fde = &fde_table[fde_idx];
22417
22418           if (fde->dw_fde_switched_sections)
22419             {
22420               if (!fde->in_std_section)
22421                 add_ranges_by_labels (comp_unit_die,
22422                                       fde->dw_fde_hot_section_label,
22423                                       fde->dw_fde_hot_section_end_label,
22424                                       &range_list_added);
22425               if (!fde->cold_in_std_section)
22426                 add_ranges_by_labels (comp_unit_die,
22427                                       fde->dw_fde_unlikely_section_label,
22428                                       fde->dw_fde_unlikely_section_end_label,
22429                                       &range_list_added);
22430             }
22431           else if (!fde->in_std_section)
22432             add_ranges_by_labels (comp_unit_die, fde->dw_fde_begin,
22433                                   fde->dw_fde_end, &range_list_added);
22434         }
22435
22436       if (range_list_added)
22437         add_ranges (NULL);
22438     }
22439
22440   /* Output location list section if necessary.  */
22441   if (have_location_lists)
22442     {
22443       /* Output the location lists info.  */
22444       switch_to_section (debug_loc_section);
22445       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
22446                                    DEBUG_LOC_SECTION_LABEL, 0);
22447       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
22448       output_location_lists (die);
22449     }
22450
22451   if (debug_info_level >= DINFO_LEVEL_NORMAL)
22452     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
22453                     debug_line_section_label);
22454
22455   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22456     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
22457
22458   /* Output all of the compilation units.  We put the main one last so that
22459      the offsets are available to output_pubnames.  */
22460   for (node = limbo_die_list; node; node = node->next)
22461     output_comp_unit (node->die, 0);
22462
22463   comdat_type_table = htab_create (100, htab_ct_hash, htab_ct_eq, NULL);
22464   for (ctnode = comdat_type_list; ctnode != NULL; ctnode = ctnode->next)
22465     {
22466       void **slot = htab_find_slot (comdat_type_table, ctnode, INSERT);
22467
22468       /* Don't output duplicate types.  */
22469       if (*slot != HTAB_EMPTY_ENTRY)
22470         continue;
22471
22472       /* Add a pointer to the line table for the main compilation unit
22473          so that the debugger can make sense of DW_AT_decl_file
22474          attributes.  */
22475       if (debug_info_level >= DINFO_LEVEL_NORMAL)
22476         add_AT_lineptr (ctnode->root_die, DW_AT_stmt_list,
22477                         debug_line_section_label);
22478
22479       output_comdat_type_unit (ctnode);
22480       *slot = ctnode;
22481     }
22482   htab_delete (comdat_type_table);
22483
22484   /* Output the main compilation unit if non-empty or if .debug_macinfo
22485      has been emitted.  */
22486   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
22487
22488   /* Output the abbreviation table.  */
22489   switch_to_section (debug_abbrev_section);
22490   output_abbrev_section ();
22491
22492   /* Output public names table if necessary.  */
22493   if (!VEC_empty (pubname_entry, pubname_table))
22494     {
22495       switch_to_section (debug_pubnames_section);
22496       output_pubnames (pubname_table);
22497     }
22498
22499   /* Output public types table if necessary.  */
22500   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
22501      It shouldn't hurt to emit it always, since pure DWARF2 consumers
22502      simply won't look for the section.  */
22503   if (!VEC_empty (pubname_entry, pubtype_table))
22504     {
22505       switch_to_section (debug_pubtypes_section);
22506       output_pubnames (pubtype_table);
22507     }
22508
22509   /* Output direct and virtual call tables if necessary.  */
22510   if (!VEC_empty (dcall_entry, dcall_table))
22511     {
22512       switch_to_section (debug_dcall_section);
22513       output_dcall_table ();
22514     }
22515   if (!VEC_empty (vcall_entry, vcall_table))
22516     {
22517       switch_to_section (debug_vcall_section);
22518       output_vcall_table ();
22519     }
22520
22521   /* Output the address range information.  We only put functions in the arange
22522      table, so don't write it out if we don't have any.  */
22523   if (fde_table_in_use)
22524     {
22525       switch_to_section (debug_aranges_section);
22526       output_aranges ();
22527     }
22528
22529   /* Output ranges section if necessary.  */
22530   if (ranges_table_in_use)
22531     {
22532       switch_to_section (debug_ranges_section);
22533       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
22534       output_ranges ();
22535     }
22536
22537   /* Output the source line correspondence table.  We must do this
22538      even if there is no line information.  Otherwise, on an empty
22539      translation unit, we will generate a present, but empty,
22540      .debug_info section.  IRIX 6.5 `nm' will then complain when
22541      examining the file.  This is done late so that any filenames
22542      used by the debug_info section are marked as 'used'.  */
22543   if (! DWARF2_ASM_LINE_DEBUG_INFO)
22544     {
22545       switch_to_section (debug_line_section);
22546       output_line_info ();
22547     }
22548
22549   /* Have to end the macro section.  */
22550   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
22551     {
22552       switch_to_section (debug_macinfo_section);
22553       dw2_asm_output_data (1, 0, "End compilation unit");
22554     }
22555
22556   /* If we emitted any DW_FORM_strp form attribute, output the string
22557      table too.  */
22558   if (debug_str_hash)
22559     htab_traverse (debug_str_hash, output_indirect_string, NULL);
22560 }
22561 #else
22562
22563 /* This should never be used, but its address is needed for comparisons.  */
22564 const struct gcc_debug_hooks dwarf2_debug_hooks =
22565 {
22566   0,            /* init */
22567   0,            /* finish */
22568   0,            /* assembly_start */
22569   0,            /* define */
22570   0,            /* undef */
22571   0,            /* start_source_file */
22572   0,            /* end_source_file */
22573   0,            /* begin_block */
22574   0,            /* end_block */
22575   0,            /* ignore_block */
22576   0,            /* source_line */
22577   0,            /* begin_prologue */
22578   0,            /* end_prologue */
22579   0,            /* begin_epilogue */
22580   0,            /* end_epilogue */
22581   0,            /* begin_function */
22582   0,            /* end_function */
22583   0,            /* function_decl */
22584   0,            /* global_decl */
22585   0,            /* type_decl */
22586   0,            /* imported_module_or_decl */
22587   0,            /* deferred_inline_function */
22588   0,            /* outlining_inline_function */
22589   0,            /* label */
22590   0,            /* handle_pch */
22591   0,            /* var_location */
22592   0,            /* switch_text_section */
22593   0,            /* direct_call */
22594   0,            /* virtual_call_token */
22595   0,            /* copy_call_info */
22596   0,            /* virtual_call */
22597   0,            /* set_name */
22598   0             /* start_end_main_source_file */
22599 };
22600
22601 #endif /* DWARF2_DEBUGGING_INFO */
22602
22603 #include "gt-dwarf2out.h"