OSDN Git Service

gcc/ChangeLog:
[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 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.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 "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.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
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
95
96 static rtx last_var_location_insn;
97 #endif
98
99 #ifdef VMS_DEBUGGING_INFO
100 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
101
102 /* Define this macro to be a nonzero value if the directory specifications
103     which are output in the debug info should end with a separator.  */
104 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
105 /* Define this macro to evaluate to a nonzero value if GCC should refrain
106    from generating indirect strings in DWARF2 debug information, for instance
107    if your target is stuck with an old version of GDB that is unable to
108    process them properly or uses VMS Debug.  */
109 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
110 #else
111 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
112 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
113 #endif
114
115 #ifndef DWARF2_FRAME_INFO
116 # ifdef DWARF2_DEBUGGING_INFO
117 #  define DWARF2_FRAME_INFO \
118   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
119 # else
120 #  define DWARF2_FRAME_INFO 0
121 # endif
122 #endif
123
124 /* Map register numbers held in the call frame info that gcc has
125    collected using DWARF_FRAME_REGNUM to those that should be output in
126    .debug_frame and .eh_frame.  */
127 #ifndef DWARF2_FRAME_REG_OUT
128 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
129 #endif
130
131 /* Save the result of dwarf2out_do_frame across PCH.  */
132 static GTY(()) bool saved_do_cfi_asm = 0;
133
134 /* Decide whether we want to emit frame unwind information for the current
135    translation unit.  */
136
137 int
138 dwarf2out_do_frame (void)
139 {
140   /* We want to emit correct CFA location expressions or lists, so we
141      have to return true if we're going to output debug info, even if
142      we're not going to output frame or unwind info.  */
143   return (write_symbols == DWARF2_DEBUG
144           || write_symbols == VMS_AND_DWARF2_DEBUG
145           || DWARF2_FRAME_INFO || saved_do_cfi_asm
146 #ifdef DWARF2_UNWIND_INFO
147           || (DWARF2_UNWIND_INFO
148               && (flag_unwind_tables
149                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
150 #endif
151           );
152 }
153
154 /* Decide whether to emit frame unwind via assembler directives.  */
155
156 int
157 dwarf2out_do_cfi_asm (void)
158 {
159   int enc;
160
161 #ifdef MIPS_DEBUGGING_INFO
162   return false;
163 #endif
164   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
165     return false;
166   if (saved_do_cfi_asm || !eh_personality_libfunc)
167     return true;
168   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
169     return false;
170
171   /* Make sure the personality encoding is one the assembler can support.
172      In particular, aligned addresses can't be handled.  */
173   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
174   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
175     return false;
176   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
177   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
178     return false;
179
180   saved_do_cfi_asm = true;
181   return true;
182 }
183
184 /* The size of the target's pointer type.  */
185 #ifndef PTR_SIZE
186 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
187 #endif
188
189 /* Array of RTXes referenced by the debugging information, which therefore
190    must be kept around forever.  */
191 static GTY(()) VEC(rtx,gc) *used_rtx_array;
192
193 /* A pointer to the base of a list of incomplete types which might be
194    completed at some later time.  incomplete_types_list needs to be a
195    VEC(tree,gc) because we want to tell the garbage collector about
196    it.  */
197 static GTY(()) VEC(tree,gc) *incomplete_types;
198
199 /* A pointer to the base of a table of references to declaration
200    scopes.  This table is a display which tracks the nesting
201    of declaration scopes at the current scope and containing
202    scopes.  This table is used to find the proper place to
203    define type declaration DIE's.  */
204 static GTY(()) VEC(tree,gc) *decl_scope_table;
205
206 /* Pointers to various DWARF2 sections.  */
207 static GTY(()) section *debug_info_section;
208 static GTY(()) section *debug_abbrev_section;
209 static GTY(()) section *debug_aranges_section;
210 static GTY(()) section *debug_macinfo_section;
211 static GTY(()) section *debug_line_section;
212 static GTY(()) section *debug_loc_section;
213 static GTY(()) section *debug_pubnames_section;
214 static GTY(()) section *debug_pubtypes_section;
215 static GTY(()) section *debug_str_section;
216 static GTY(()) section *debug_ranges_section;
217 static GTY(()) section *debug_frame_section;
218
219 /* How to start an assembler comment.  */
220 #ifndef ASM_COMMENT_START
221 #define ASM_COMMENT_START ";#"
222 #endif
223
224 typedef struct dw_cfi_struct *dw_cfi_ref;
225 typedef struct dw_fde_struct *dw_fde_ref;
226 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
227
228 /* Call frames are described using a sequence of Call Frame
229    Information instructions.  The register number, offset
230    and address fields are provided as possible operands;
231    their use is selected by the opcode field.  */
232
233 enum dw_cfi_oprnd_type {
234   dw_cfi_oprnd_unused,
235   dw_cfi_oprnd_reg_num,
236   dw_cfi_oprnd_offset,
237   dw_cfi_oprnd_addr,
238   dw_cfi_oprnd_loc
239 };
240
241 typedef union GTY(()) dw_cfi_oprnd_struct {
242   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
243   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
244   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
245   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
246 }
247 dw_cfi_oprnd;
248
249 typedef struct GTY(()) dw_cfi_struct {
250   dw_cfi_ref dw_cfi_next;
251   enum dwarf_call_frame_info dw_cfi_opc;
252   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
253     dw_cfi_oprnd1;
254   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
255     dw_cfi_oprnd2;
256 }
257 dw_cfi_node;
258
259 /* This is how we define the location of the CFA. We use to handle it
260    as REG + OFFSET all the time,  but now it can be more complex.
261    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
262    Instead of passing around REG and OFFSET, we pass a copy
263    of this structure.  */
264 typedef struct GTY(()) cfa_loc {
265   HOST_WIDE_INT offset;
266   HOST_WIDE_INT base_offset;
267   unsigned int reg;
268   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
269   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
270 } dw_cfa_location;
271
272 /* All call frame descriptions (FDE's) in the GCC generated DWARF
273    refer to a single Common Information Entry (CIE), defined at
274    the beginning of the .debug_frame section.  This use of a single
275    CIE obviates the need to keep track of multiple CIE's
276    in the DWARF generation routines below.  */
277
278 typedef struct GTY(()) dw_fde_struct {
279   tree decl;
280   const char *dw_fde_begin;
281   const char *dw_fde_current_label;
282   const char *dw_fde_end;
283   const char *dw_fde_hot_section_label;
284   const char *dw_fde_hot_section_end_label;
285   const char *dw_fde_unlikely_section_label;
286   const char *dw_fde_unlikely_section_end_label;
287   dw_cfi_ref dw_fde_cfi;
288   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
289   unsigned funcdef_number;
290   HOST_WIDE_INT stack_realignment;
291   /* Dynamic realign argument pointer register.  */
292   unsigned int drap_reg;
293   /* Virtual dynamic realign argument pointer register.  */
294   unsigned int vdrap_reg;
295   unsigned all_throwers_are_sibcalls : 1;
296   unsigned nothrow : 1;
297   unsigned uses_eh_lsda : 1;
298   /* Whether we did stack realign in this call frame.  */
299   unsigned stack_realign : 1;
300   /* Whether dynamic realign argument pointer register has been saved.  */
301   unsigned drap_reg_saved: 1;
302   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
303   unsigned in_std_section : 1;
304   /* True iff dw_fde_unlikely_section_label is in text_section or
305      cold_text_section.  */
306   unsigned cold_in_std_section : 1;
307   /* True iff switched sections.  */
308   unsigned dw_fde_switched_sections : 1;
309   /* True iff switching from cold to hot section.  */
310   unsigned dw_fde_switched_cold_to_hot : 1;
311 }
312 dw_fde_node;
313
314 /* Maximum size (in bytes) of an artificially generated label.  */
315 #define MAX_ARTIFICIAL_LABEL_BYTES      30
316
317 /* The size of addresses as they appear in the Dwarf 2 data.
318    Some architectures use word addresses to refer to code locations,
319    but Dwarf 2 info always uses byte addresses.  On such machines,
320    Dwarf 2 addresses need to be larger than the architecture's
321    pointers.  */
322 #ifndef DWARF2_ADDR_SIZE
323 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
324 #endif
325
326 /* The size in bytes of a DWARF field indicating an offset or length
327    relative to a debug info section, specified to be 4 bytes in the
328    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
329    as PTR_SIZE.  */
330
331 #ifndef DWARF_OFFSET_SIZE
332 #define DWARF_OFFSET_SIZE 4
333 #endif
334
335 /* According to the (draft) DWARF 3 specification, the initial length
336    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
337    bytes are 0xffffffff, followed by the length stored in the next 8
338    bytes.
339
340    However, the SGI/MIPS ABI uses an initial length which is equal to
341    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
342
343 #ifndef DWARF_INITIAL_LENGTH_SIZE
344 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
345 #endif
346
347 /* Round SIZE up to the nearest BOUNDARY.  */
348 #define DWARF_ROUND(SIZE,BOUNDARY) \
349   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
350
351 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
352 #ifndef DWARF_CIE_DATA_ALIGNMENT
353 #ifdef STACK_GROWS_DOWNWARD
354 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
355 #else
356 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
357 #endif
358 #endif
359
360 /* CIE identifier.  */
361 #if HOST_BITS_PER_WIDE_INT >= 64
362 #define DWARF_CIE_ID \
363   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
364 #else
365 #define DWARF_CIE_ID DW_CIE_ID
366 #endif
367
368 /* A pointer to the base of a table that contains frame description
369    information for each routine.  */
370 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
371
372 /* Number of elements currently allocated for fde_table.  */
373 static GTY(()) unsigned fde_table_allocated;
374
375 /* Number of elements in fde_table currently in use.  */
376 static GTY(()) unsigned fde_table_in_use;
377
378 /* Size (in elements) of increments by which we may expand the
379    fde_table.  */
380 #define FDE_TABLE_INCREMENT 256
381
382 /* Get the current fde_table entry we should use.  */
383
384 static inline dw_fde_ref
385 current_fde (void)
386 {
387   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
388 }
389
390 /* A list of call frame insns for the CIE.  */
391 static GTY(()) dw_cfi_ref cie_cfi_head;
392
393 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
394 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
395    attribute that accelerates the lookup of the FDE associated
396    with the subprogram.  This variable holds the table index of the FDE
397    associated with the current function (body) definition.  */
398 static unsigned current_funcdef_fde;
399 #endif
400
401 struct GTY(()) indirect_string_node {
402   const char *str;
403   unsigned int refcount;
404   enum dwarf_form form;
405   char *label;
406 };
407
408 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
409
410 /* True if the compilation unit has location entries that reference
411    debug strings.  */
412 static GTY(()) bool debug_str_hash_forced = false;
413
414 static GTY(()) int dw2_string_counter;
415 static GTY(()) unsigned long dwarf2out_cfi_label_num;
416
417 /* True if the compilation unit places functions in more than one section.  */
418 static GTY(()) bool have_multiple_function_sections = false;
419
420 /* Whether the default text and cold text sections have been used at all.  */
421
422 static GTY(()) bool text_section_used = false;
423 static GTY(()) bool cold_text_section_used = false;
424
425 /* The default cold text section.  */
426 static GTY(()) section *cold_text_section;
427
428 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
429
430 /* Forward declarations for functions defined in this file.  */
431
432 static char *stripattributes (const char *);
433 static const char *dwarf_cfi_name (unsigned);
434 static dw_cfi_ref new_cfi (void);
435 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
436 static void add_fde_cfi (const char *, dw_cfi_ref);
437 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
438 static void lookup_cfa (dw_cfa_location *);
439 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
440 #ifdef DWARF2_UNWIND_INFO
441 static void initial_return_save (rtx);
442 #endif
443 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
444                                           HOST_WIDE_INT);
445 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
446 static void output_cfi_directive (dw_cfi_ref);
447 static void output_call_frame_info (int);
448 static void dwarf2out_note_section_used (void);
449 static void dwarf2out_stack_adjust (rtx, bool);
450 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
451 static void flush_queued_reg_saves (void);
452 static bool clobbers_queued_reg_save (const_rtx);
453 static void dwarf2out_frame_debug_expr (rtx, const char *);
454
455 /* Support for complex CFA locations.  */
456 static void output_cfa_loc (dw_cfi_ref);
457 static void output_cfa_loc_raw (dw_cfi_ref);
458 static void get_cfa_from_loc_descr (dw_cfa_location *,
459                                     struct dw_loc_descr_struct *);
460 static struct dw_loc_descr_struct *build_cfa_loc
461   (dw_cfa_location *, HOST_WIDE_INT);
462 static struct dw_loc_descr_struct *build_cfa_aligned_loc
463   (HOST_WIDE_INT, HOST_WIDE_INT);
464 static void def_cfa_1 (const char *, dw_cfa_location *);
465
466 /* How to start an assembler comment.  */
467 #ifndef ASM_COMMENT_START
468 #define ASM_COMMENT_START ";#"
469 #endif
470
471 /* Data and reference forms for relocatable data.  */
472 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
473 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
474
475 #ifndef DEBUG_FRAME_SECTION
476 #define DEBUG_FRAME_SECTION     ".debug_frame"
477 #endif
478
479 #ifndef FUNC_BEGIN_LABEL
480 #define FUNC_BEGIN_LABEL        "LFB"
481 #endif
482
483 #ifndef FUNC_END_LABEL
484 #define FUNC_END_LABEL          "LFE"
485 #endif
486
487 #ifndef FRAME_BEGIN_LABEL
488 #define FRAME_BEGIN_LABEL       "Lframe"
489 #endif
490 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
491 #define CIE_END_LABEL           "LECIE"
492 #define FDE_LABEL               "LSFDE"
493 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
494 #define FDE_END_LABEL           "LEFDE"
495 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
496 #define LINE_NUMBER_END_LABEL   "LELT"
497 #define LN_PROLOG_AS_LABEL      "LASLTP"
498 #define LN_PROLOG_END_LABEL     "LELTP"
499 #define DIE_LABEL_PREFIX        "DW"
500
501 /* The DWARF 2 CFA column which tracks the return address.  Normally this
502    is the column for PC, or the first column after all of the hard
503    registers.  */
504 #ifndef DWARF_FRAME_RETURN_COLUMN
505 #ifdef PC_REGNUM
506 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
507 #else
508 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
509 #endif
510 #endif
511
512 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
513    default, we just provide columns for all registers.  */
514 #ifndef DWARF_FRAME_REGNUM
515 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
516 #endif
517 \f
518 /* Hook used by __throw.  */
519
520 rtx
521 expand_builtin_dwarf_sp_column (void)
522 {
523   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
524   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
525 }
526
527 /* Return a pointer to a copy of the section string name S with all
528    attributes stripped off, and an asterisk prepended (for assemble_name).  */
529
530 static inline char *
531 stripattributes (const char *s)
532 {
533   char *stripped = XNEWVEC (char, strlen (s) + 2);
534   char *p = stripped;
535
536   *p++ = '*';
537
538   while (*s && *s != ',')
539     *p++ = *s++;
540
541   *p = '\0';
542   return stripped;
543 }
544
545 /* MEM is a memory reference for the register size table, each element of
546    which has mode MODE.  Initialize column C as a return address column.  */
547
548 static void
549 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
550 {
551   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
552   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
553   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
554 }
555
556 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
557
558 static inline HOST_WIDE_INT
559 div_data_align (HOST_WIDE_INT off)
560 {
561   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
562   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
563   return r;
564 }
565
566 /* Return true if we need a signed version of a given opcode
567    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
568
569 static inline bool
570 need_data_align_sf_opcode (HOST_WIDE_INT off)
571 {
572   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
573 }
574
575 /* Generate code to initialize the register size table.  */
576
577 void
578 expand_builtin_init_dwarf_reg_sizes (tree address)
579 {
580   unsigned int i;
581   enum machine_mode mode = TYPE_MODE (char_type_node);
582   rtx addr = expand_normal (address);
583   rtx mem = gen_rtx_MEM (BLKmode, addr);
584   bool wrote_return_column = false;
585
586   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
587     {
588       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
589
590       if (rnum < DWARF_FRAME_REGISTERS)
591         {
592           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
593           enum machine_mode save_mode = reg_raw_mode[i];
594           HOST_WIDE_INT size;
595
596           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
597             save_mode = choose_hard_reg_mode (i, 1, true);
598           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
599             {
600               if (save_mode == VOIDmode)
601                 continue;
602               wrote_return_column = true;
603             }
604           size = GET_MODE_SIZE (save_mode);
605           if (offset < 0)
606             continue;
607
608           emit_move_insn (adjust_address (mem, mode, offset),
609                           gen_int_mode (size, mode));
610         }
611     }
612
613   if (!wrote_return_column)
614     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
615
616 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
617   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
618 #endif
619
620   targetm.init_dwarf_reg_sizes_extra (address);
621 }
622
623 /* Convert a DWARF call frame info. operation to its string name */
624
625 static const char *
626 dwarf_cfi_name (unsigned int cfi_opc)
627 {
628   switch (cfi_opc)
629     {
630     case DW_CFA_advance_loc:
631       return "DW_CFA_advance_loc";
632     case DW_CFA_offset:
633       return "DW_CFA_offset";
634     case DW_CFA_restore:
635       return "DW_CFA_restore";
636     case DW_CFA_nop:
637       return "DW_CFA_nop";
638     case DW_CFA_set_loc:
639       return "DW_CFA_set_loc";
640     case DW_CFA_advance_loc1:
641       return "DW_CFA_advance_loc1";
642     case DW_CFA_advance_loc2:
643       return "DW_CFA_advance_loc2";
644     case DW_CFA_advance_loc4:
645       return "DW_CFA_advance_loc4";
646     case DW_CFA_offset_extended:
647       return "DW_CFA_offset_extended";
648     case DW_CFA_restore_extended:
649       return "DW_CFA_restore_extended";
650     case DW_CFA_undefined:
651       return "DW_CFA_undefined";
652     case DW_CFA_same_value:
653       return "DW_CFA_same_value";
654     case DW_CFA_register:
655       return "DW_CFA_register";
656     case DW_CFA_remember_state:
657       return "DW_CFA_remember_state";
658     case DW_CFA_restore_state:
659       return "DW_CFA_restore_state";
660     case DW_CFA_def_cfa:
661       return "DW_CFA_def_cfa";
662     case DW_CFA_def_cfa_register:
663       return "DW_CFA_def_cfa_register";
664     case DW_CFA_def_cfa_offset:
665       return "DW_CFA_def_cfa_offset";
666
667     /* DWARF 3 */
668     case DW_CFA_def_cfa_expression:
669       return "DW_CFA_def_cfa_expression";
670     case DW_CFA_expression:
671       return "DW_CFA_expression";
672     case DW_CFA_offset_extended_sf:
673       return "DW_CFA_offset_extended_sf";
674     case DW_CFA_def_cfa_sf:
675       return "DW_CFA_def_cfa_sf";
676     case DW_CFA_def_cfa_offset_sf:
677       return "DW_CFA_def_cfa_offset_sf";
678
679     /* SGI/MIPS specific */
680     case DW_CFA_MIPS_advance_loc8:
681       return "DW_CFA_MIPS_advance_loc8";
682
683     /* GNU extensions */
684     case DW_CFA_GNU_window_save:
685       return "DW_CFA_GNU_window_save";
686     case DW_CFA_GNU_args_size:
687       return "DW_CFA_GNU_args_size";
688     case DW_CFA_GNU_negative_offset_extended:
689       return "DW_CFA_GNU_negative_offset_extended";
690
691     default:
692       return "DW_CFA_<unknown>";
693     }
694 }
695
696 /* Return a pointer to a newly allocated Call Frame Instruction.  */
697
698 static inline dw_cfi_ref
699 new_cfi (void)
700 {
701   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
702
703   cfi->dw_cfi_next = NULL;
704   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
705   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
706
707   return cfi;
708 }
709
710 /* Add a Call Frame Instruction to list of instructions.  */
711
712 static inline void
713 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
714 {
715   dw_cfi_ref *p;
716   dw_fde_ref fde = current_fde ();
717
718   /* When DRAP is used, CFA is defined with an expression.  Redefine
719      CFA may lead to a different CFA value.   */
720   /* ??? Of course, this heuristic fails when we're annotating epilogues,
721      because of course we'll always want to redefine the CFA back to the
722      stack pointer on the way out.  Where should we move this check?  */
723   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
724     switch (cfi->dw_cfi_opc)
725       {
726         case DW_CFA_def_cfa_register:
727         case DW_CFA_def_cfa_offset:
728         case DW_CFA_def_cfa_offset_sf:
729         case DW_CFA_def_cfa:
730         case DW_CFA_def_cfa_sf:
731           gcc_unreachable ();
732
733         default:
734           break;
735       }
736
737   /* Find the end of the chain.  */
738   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
739     ;
740
741   *p = cfi;
742 }
743
744 /* Generate a new label for the CFI info to refer to.  FORCE is true
745    if a label needs to be output even when using .cfi_* directives.  */
746
747 char *
748 dwarf2out_cfi_label (bool force)
749 {
750   static char label[20];
751
752   if (!force && dwarf2out_do_cfi_asm ())
753     {
754       /* In this case, we will be emitting the asm directive instead of
755          the label, so just return a placeholder to keep the rest of the
756          interfaces happy.  */
757       strcpy (label, "<do not output>");
758     }
759   else
760     {
761       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
762       ASM_OUTPUT_LABEL (asm_out_file, label);
763     }
764
765   return label;
766 }
767
768 /* True if remember_state should be emitted before following CFI directive.  */
769 static bool emit_cfa_remember;
770
771 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
772    or to the CIE if LABEL is NULL.  */
773
774 static void
775 add_fde_cfi (const char *label, dw_cfi_ref cfi)
776 {
777   dw_cfi_ref *list_head;
778
779   if (emit_cfa_remember)
780     {
781       dw_cfi_ref cfi_remember;
782
783       /* Emit the state save.  */
784       emit_cfa_remember = false;
785       cfi_remember = new_cfi (); 
786       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
787       add_fde_cfi (label, cfi_remember);
788     }
789
790   list_head = &cie_cfi_head;
791
792   if (dwarf2out_do_cfi_asm ())
793     {
794       if (label)
795         {
796           dw_fde_ref fde = current_fde ();
797
798           gcc_assert (fde != NULL);
799
800           /* We still have to add the cfi to the list so that lookup_cfa
801              works later on.  When -g2 and above we even need to force
802              emitting of CFI labels and add to list a DW_CFA_set_loc for
803              convert_cfa_to_fb_loc_list purposes.  If we're generating
804              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
805              convert_cfa_to_fb_loc_list.  */
806           if (dwarf_version == 2
807               && debug_info_level > DINFO_LEVEL_TERSE
808               && (write_symbols == DWARF2_DEBUG
809                   || write_symbols == VMS_AND_DWARF2_DEBUG))
810             {
811               switch (cfi->dw_cfi_opc)
812                 {
813                 case DW_CFA_def_cfa_offset:
814                 case DW_CFA_def_cfa_offset_sf:
815                 case DW_CFA_def_cfa_register:
816                 case DW_CFA_def_cfa:
817                 case DW_CFA_def_cfa_sf:
818                 case DW_CFA_def_cfa_expression:
819                 case DW_CFA_restore_state:
820                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
821                     label = dwarf2out_cfi_label (true);
822
823                   if (fde->dw_fde_current_label == NULL
824                       || strcmp (label, fde->dw_fde_current_label) != 0)
825                     {
826                       dw_cfi_ref xcfi;
827
828                       label = xstrdup (label);
829
830                       /* Set the location counter to the new label.  */
831                       xcfi = new_cfi ();
832                       /* It doesn't metter whether DW_CFA_set_loc
833                          or DW_CFA_advance_loc4 is added here, those aren't
834                          emitted into assembly, only looked up by
835                          convert_cfa_to_fb_loc_list.  */
836                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
837                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
838                       add_cfi (&fde->dw_fde_cfi, xcfi);
839                       fde->dw_fde_current_label = label;
840                     }
841                   break;
842                 default:
843                   break;
844                 }
845             }
846
847           output_cfi_directive (cfi);
848
849           list_head = &fde->dw_fde_cfi;
850         }
851       /* ??? If this is a CFI for the CIE, we don't emit.  This
852          assumes that the standard CIE contents that the assembler
853          uses matches the standard CIE contents that the compiler
854          uses.  This is probably a bad assumption.  I'm not quite
855          sure how to address this for now.  */
856     }
857   else if (label)
858     {
859       dw_fde_ref fde = current_fde ();
860
861       gcc_assert (fde != NULL);
862
863       if (*label == 0)
864         label = dwarf2out_cfi_label (false);
865
866       if (fde->dw_fde_current_label == NULL
867           || strcmp (label, fde->dw_fde_current_label) != 0)
868         {
869           dw_cfi_ref xcfi;
870
871           label = xstrdup (label);
872
873           /* Set the location counter to the new label.  */
874           xcfi = new_cfi ();
875           /* If we have a current label, advance from there, otherwise
876              set the location directly using set_loc.  */
877           xcfi->dw_cfi_opc = fde->dw_fde_current_label
878                              ? DW_CFA_advance_loc4
879                              : DW_CFA_set_loc;
880           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
881           add_cfi (&fde->dw_fde_cfi, xcfi);
882
883           fde->dw_fde_current_label = label;
884         }
885
886       list_head = &fde->dw_fde_cfi;
887     }
888
889   add_cfi (list_head, cfi);
890 }
891
892 /* Subroutine of lookup_cfa.  */
893
894 static void
895 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
896 {
897   switch (cfi->dw_cfi_opc)
898     {
899     case DW_CFA_def_cfa_offset:
900     case DW_CFA_def_cfa_offset_sf:
901       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
902       break;
903     case DW_CFA_def_cfa_register:
904       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
905       break;
906     case DW_CFA_def_cfa:
907     case DW_CFA_def_cfa_sf:
908       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
909       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
910       break;
911     case DW_CFA_def_cfa_expression:
912       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
913       break;
914
915     case DW_CFA_remember_state:
916       gcc_assert (!remember->in_use);
917       *remember = *loc;
918       remember->in_use = 1;
919       break;
920     case DW_CFA_restore_state:
921       gcc_assert (remember->in_use);
922       *loc = *remember;
923       remember->in_use = 0;
924       break;
925
926     default:
927       break;
928     }
929 }
930
931 /* Find the previous value for the CFA.  */
932
933 static void
934 lookup_cfa (dw_cfa_location *loc)
935 {
936   dw_cfi_ref cfi;
937   dw_fde_ref fde;
938   dw_cfa_location remember;
939
940   memset (loc, 0, sizeof (*loc));
941   loc->reg = INVALID_REGNUM;
942   remember = *loc;
943
944   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
945     lookup_cfa_1 (cfi, loc, &remember);
946
947   fde = current_fde ();
948   if (fde)
949     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
950       lookup_cfa_1 (cfi, loc, &remember);
951 }
952
953 /* The current rule for calculating the DWARF2 canonical frame address.  */
954 static dw_cfa_location cfa;
955
956 /* The register used for saving registers to the stack, and its offset
957    from the CFA.  */
958 static dw_cfa_location cfa_store;
959
960 /* The current save location around an epilogue.  */
961 static dw_cfa_location cfa_remember;
962
963 /* The running total of the size of arguments pushed onto the stack.  */
964 static HOST_WIDE_INT args_size;
965
966 /* The last args_size we actually output.  */
967 static HOST_WIDE_INT old_args_size;
968
969 /* Entry point to update the canonical frame address (CFA).
970    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
971    calculated from REG+OFFSET.  */
972
973 void
974 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
975 {
976   dw_cfa_location loc;
977   loc.indirect = 0;
978   loc.base_offset = 0;
979   loc.reg = reg;
980   loc.offset = offset;
981   def_cfa_1 (label, &loc);
982 }
983
984 /* Determine if two dw_cfa_location structures define the same data.  */
985
986 static bool
987 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
988 {
989   return (loc1->reg == loc2->reg
990           && loc1->offset == loc2->offset
991           && loc1->indirect == loc2->indirect
992           && (loc1->indirect == 0
993               || loc1->base_offset == loc2->base_offset));
994 }
995
996 /* This routine does the actual work.  The CFA is now calculated from
997    the dw_cfa_location structure.  */
998
999 static void
1000 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1001 {
1002   dw_cfi_ref cfi;
1003   dw_cfa_location old_cfa, loc;
1004
1005   cfa = *loc_p;
1006   loc = *loc_p;
1007
1008   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1009     cfa_store.offset = loc.offset;
1010
1011   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1012   lookup_cfa (&old_cfa);
1013
1014   /* If nothing changed, no need to issue any call frame instructions.  */
1015   if (cfa_equal_p (&loc, &old_cfa))
1016     return;
1017
1018   cfi = new_cfi ();
1019
1020   if (loc.reg == old_cfa.reg && !loc.indirect)
1021     {
1022       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1023          the CFA register did not change but the offset did.  The data 
1024          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1025          in the assembler via the .cfi_def_cfa_offset directive.  */
1026       if (loc.offset < 0)
1027         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1028       else
1029         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1030       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1031     }
1032
1033 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1034   else if (loc.offset == old_cfa.offset
1035            && old_cfa.reg != INVALID_REGNUM
1036            && !loc.indirect)
1037     {
1038       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1039          indicating the CFA register has changed to <register> but the
1040          offset has not changed.  */
1041       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1042       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1043     }
1044 #endif
1045
1046   else if (loc.indirect == 0)
1047     {
1048       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1049          indicating the CFA register has changed to <register> with
1050          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1051          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1052          directive.  */
1053       if (loc.offset < 0)
1054         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1055       else
1056         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1057       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1058       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1059     }
1060   else
1061     {
1062       /* Construct a DW_CFA_def_cfa_expression instruction to
1063          calculate the CFA using a full location expression since no
1064          register-offset pair is available.  */
1065       struct dw_loc_descr_struct *loc_list;
1066
1067       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1068       loc_list = build_cfa_loc (&loc, 0);
1069       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1070     }
1071
1072   add_fde_cfi (label, cfi);
1073 }
1074
1075 /* Add the CFI for saving a register.  REG is the CFA column number.
1076    LABEL is passed to add_fde_cfi.
1077    If SREG is -1, the register is saved at OFFSET from the CFA;
1078    otherwise it is saved in SREG.  */
1079
1080 static void
1081 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1082 {
1083   dw_cfi_ref cfi = new_cfi ();
1084   dw_fde_ref fde = current_fde ();
1085
1086   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1087
1088   /* When stack is aligned, store REG using DW_CFA_expression with
1089      FP.  */
1090   if (fde
1091       && fde->stack_realign
1092       && sreg == INVALID_REGNUM)
1093     {
1094       cfi->dw_cfi_opc = DW_CFA_expression;
1095       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1096       cfi->dw_cfi_oprnd1.dw_cfi_loc
1097         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1098     }
1099   else if (sreg == INVALID_REGNUM)
1100     {
1101       if (need_data_align_sf_opcode (offset))
1102         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1103       else if (reg & ~0x3f)
1104         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1105       else
1106         cfi->dw_cfi_opc = DW_CFA_offset;
1107       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1108     }
1109   else if (sreg == reg)
1110     cfi->dw_cfi_opc = DW_CFA_same_value;
1111   else
1112     {
1113       cfi->dw_cfi_opc = DW_CFA_register;
1114       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1115     }
1116
1117   add_fde_cfi (label, cfi);
1118 }
1119
1120 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1121    This CFI tells the unwinder that it needs to restore the window registers
1122    from the previous frame's window save area.
1123
1124    ??? Perhaps we should note in the CIE where windows are saved (instead of
1125    assuming 0(cfa)) and what registers are in the window.  */
1126
1127 void
1128 dwarf2out_window_save (const char *label)
1129 {
1130   dw_cfi_ref cfi = new_cfi ();
1131
1132   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1133   add_fde_cfi (label, cfi);
1134 }
1135
1136 /* Add a CFI to update the running total of the size of arguments
1137    pushed onto the stack.  */
1138
1139 void
1140 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1141 {
1142   dw_cfi_ref cfi;
1143
1144   if (size == old_args_size)
1145     return;
1146
1147   old_args_size = size;
1148
1149   cfi = new_cfi ();
1150   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1151   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1152   add_fde_cfi (label, cfi);
1153 }
1154
1155 /* Entry point for saving a register to the stack.  REG is the GCC register
1156    number.  LABEL and OFFSET are passed to reg_save.  */
1157
1158 void
1159 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1160 {
1161   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1162 }
1163
1164 /* Entry point for saving the return address in the stack.
1165    LABEL and OFFSET are passed to reg_save.  */
1166
1167 void
1168 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1169 {
1170   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1171 }
1172
1173 /* Entry point for saving the return address in a register.
1174    LABEL and SREG are passed to reg_save.  */
1175
1176 void
1177 dwarf2out_return_reg (const char *label, unsigned int sreg)
1178 {
1179   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1180 }
1181
1182 #ifdef DWARF2_UNWIND_INFO
1183 /* Record the initial position of the return address.  RTL is
1184    INCOMING_RETURN_ADDR_RTX.  */
1185
1186 static void
1187 initial_return_save (rtx rtl)
1188 {
1189   unsigned int reg = INVALID_REGNUM;
1190   HOST_WIDE_INT offset = 0;
1191
1192   switch (GET_CODE (rtl))
1193     {
1194     case REG:
1195       /* RA is in a register.  */
1196       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1197       break;
1198
1199     case MEM:
1200       /* RA is on the stack.  */
1201       rtl = XEXP (rtl, 0);
1202       switch (GET_CODE (rtl))
1203         {
1204         case REG:
1205           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1206           offset = 0;
1207           break;
1208
1209         case PLUS:
1210           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1211           offset = INTVAL (XEXP (rtl, 1));
1212           break;
1213
1214         case MINUS:
1215           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1216           offset = -INTVAL (XEXP (rtl, 1));
1217           break;
1218
1219         default:
1220           gcc_unreachable ();
1221         }
1222
1223       break;
1224
1225     case PLUS:
1226       /* The return address is at some offset from any value we can
1227          actually load.  For instance, on the SPARC it is in %i7+8. Just
1228          ignore the offset for now; it doesn't matter for unwinding frames.  */
1229       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1230       initial_return_save (XEXP (rtl, 0));
1231       return;
1232
1233     default:
1234       gcc_unreachable ();
1235     }
1236
1237   if (reg != DWARF_FRAME_RETURN_COLUMN)
1238     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1239 }
1240 #endif
1241
1242 /* Given a SET, calculate the amount of stack adjustment it
1243    contains.  */
1244
1245 static HOST_WIDE_INT
1246 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1247                      HOST_WIDE_INT cur_offset)
1248 {
1249   const_rtx src = SET_SRC (pattern);
1250   const_rtx dest = SET_DEST (pattern);
1251   HOST_WIDE_INT offset = 0;
1252   enum rtx_code code;
1253
1254   if (dest == stack_pointer_rtx)
1255     {
1256       code = GET_CODE (src);
1257
1258       /* Assume (set (reg sp) (reg whatever)) sets args_size
1259          level to 0.  */
1260       if (code == REG && src != stack_pointer_rtx)
1261         {
1262           offset = -cur_args_size;
1263 #ifndef STACK_GROWS_DOWNWARD
1264           offset = -offset;
1265 #endif
1266           return offset - cur_offset;
1267         }
1268
1269       if (! (code == PLUS || code == MINUS)
1270           || XEXP (src, 0) != stack_pointer_rtx
1271           || !CONST_INT_P (XEXP (src, 1)))
1272         return 0;
1273
1274       /* (set (reg sp) (plus (reg sp) (const_int))) */
1275       offset = INTVAL (XEXP (src, 1));
1276       if (code == PLUS)
1277         offset = -offset;
1278       return offset;
1279     }
1280
1281   if (MEM_P (src) && !MEM_P (dest))
1282     dest = src;
1283   if (MEM_P (dest))
1284     {
1285       /* (set (mem (pre_dec (reg sp))) (foo)) */
1286       src = XEXP (dest, 0);
1287       code = GET_CODE (src);
1288
1289       switch (code)
1290         {
1291         case PRE_MODIFY:
1292         case POST_MODIFY:
1293           if (XEXP (src, 0) == stack_pointer_rtx)
1294             {
1295               rtx val = XEXP (XEXP (src, 1), 1);
1296               /* We handle only adjustments by constant amount.  */
1297               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1298                           && CONST_INT_P (val));
1299               offset = -INTVAL (val);
1300               break;
1301             }
1302           return 0;
1303
1304         case PRE_DEC:
1305         case POST_DEC:
1306           if (XEXP (src, 0) == stack_pointer_rtx)
1307             {
1308               offset = GET_MODE_SIZE (GET_MODE (dest));
1309               break;
1310             }
1311           return 0;
1312
1313         case PRE_INC:
1314         case POST_INC:
1315           if (XEXP (src, 0) == stack_pointer_rtx)
1316             {
1317               offset = -GET_MODE_SIZE (GET_MODE (dest));
1318               break;
1319             }
1320           return 0;
1321
1322         default:
1323           return 0;
1324         }
1325     }
1326   else
1327     return 0;
1328
1329   return offset;
1330 }
1331
1332 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1333    indexed by INSN_UID.  */
1334
1335 static HOST_WIDE_INT *barrier_args_size;
1336
1337 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1338
1339 static HOST_WIDE_INT
1340 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1341                              VEC (rtx, heap) **next)
1342 {
1343   HOST_WIDE_INT offset = 0;
1344   int i;
1345
1346   if (! RTX_FRAME_RELATED_P (insn))
1347     {
1348       if (prologue_epilogue_contains (insn))
1349         /* Nothing */;
1350       else if (GET_CODE (PATTERN (insn)) == SET)
1351         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1352       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1353                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1354         {
1355           /* There may be stack adjustments inside compound insns.  Search
1356              for them.  */
1357           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1358             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1359               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1360                                              cur_args_size, offset);
1361         }
1362     }
1363   else
1364     {
1365       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1366
1367       if (expr)
1368         {
1369           expr = XEXP (expr, 0);
1370           if (GET_CODE (expr) == PARALLEL
1371               || GET_CODE (expr) == SEQUENCE)
1372             for (i = 1; i < XVECLEN (expr, 0); i++)
1373               {
1374                 rtx elem = XVECEXP (expr, 0, i);
1375
1376                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1377                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1378               }
1379         }
1380     }
1381
1382 #ifndef STACK_GROWS_DOWNWARD
1383   offset = -offset;
1384 #endif
1385
1386   cur_args_size += offset;
1387   if (cur_args_size < 0)
1388     cur_args_size = 0;
1389
1390   if (JUMP_P (insn))
1391     {
1392       rtx dest = JUMP_LABEL (insn);
1393
1394       if (dest)
1395         {
1396           if (barrier_args_size [INSN_UID (dest)] < 0)
1397             {
1398               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1399               VEC_safe_push (rtx, heap, *next, dest);
1400             }
1401         }
1402     }
1403
1404   return cur_args_size;
1405 }
1406
1407 /* Walk the whole function and compute args_size on BARRIERs.  */
1408
1409 static void
1410 compute_barrier_args_size (void)
1411 {
1412   int max_uid = get_max_uid (), i;
1413   rtx insn;
1414   VEC (rtx, heap) *worklist, *next, *tmp;
1415
1416   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1417   for (i = 0; i < max_uid; i++)
1418     barrier_args_size[i] = -1;
1419
1420   worklist = VEC_alloc (rtx, heap, 20);
1421   next = VEC_alloc (rtx, heap, 20);
1422   insn = get_insns ();
1423   barrier_args_size[INSN_UID (insn)] = 0;
1424   VEC_quick_push (rtx, worklist, insn);
1425   for (;;)
1426     {
1427       while (!VEC_empty (rtx, worklist))
1428         {
1429           rtx prev, body, first_insn;
1430           HOST_WIDE_INT cur_args_size;
1431
1432           first_insn = insn = VEC_pop (rtx, worklist);
1433           cur_args_size = barrier_args_size[INSN_UID (insn)];
1434           prev = prev_nonnote_insn (insn);
1435           if (prev && BARRIER_P (prev))
1436             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1437
1438           for (; insn; insn = NEXT_INSN (insn))
1439             {
1440               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1441                 continue;
1442               if (BARRIER_P (insn))
1443                 break;
1444
1445               if (LABEL_P (insn))
1446                 {
1447                   if (insn == first_insn)
1448                     continue;
1449                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1450                     {
1451                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1452                       continue;
1453                     }
1454                   else
1455                     {
1456                       /* The insns starting with this label have been
1457                          already scanned or are in the worklist.  */
1458                       break;
1459                     }
1460                 }
1461
1462               body = PATTERN (insn);
1463               if (GET_CODE (body) == SEQUENCE)
1464                 {
1465                   HOST_WIDE_INT dest_args_size = cur_args_size;
1466                   for (i = 1; i < XVECLEN (body, 0); i++)
1467                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1468                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1469                       dest_args_size
1470                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1471                                                        dest_args_size, &next);
1472                     else
1473                       cur_args_size
1474                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1475                                                        cur_args_size, &next);
1476
1477                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1478                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1479                                                  dest_args_size, &next);
1480                   else
1481                     cur_args_size
1482                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1483                                                      cur_args_size, &next);
1484                 }
1485               else
1486                 cur_args_size
1487                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1488             }
1489         }
1490
1491       if (VEC_empty (rtx, next))
1492         break;
1493
1494       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1495       tmp = next;
1496       next = worklist;
1497       worklist = tmp;
1498       VEC_truncate (rtx, next, 0);
1499     }
1500
1501   VEC_free (rtx, heap, worklist);
1502   VEC_free (rtx, heap, next);
1503 }
1504
1505
1506 /* Check INSN to see if it looks like a push or a stack adjustment, and
1507    make a note of it if it does.  EH uses this information to find out how
1508    much extra space it needs to pop off the stack.  */
1509
1510 static void
1511 dwarf2out_stack_adjust (rtx insn, bool after_p)
1512 {
1513   HOST_WIDE_INT offset;
1514   const char *label;
1515   int i;
1516
1517   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1518      with this function.  Proper support would require all frame-related
1519      insns to be marked, and to be able to handle saving state around
1520      epilogues textually in the middle of the function.  */
1521   if (prologue_epilogue_contains (insn))
1522     return;
1523
1524   /* If INSN is an instruction from target of an annulled branch, the
1525      effects are for the target only and so current argument size
1526      shouldn't change at all.  */
1527   if (final_sequence
1528       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1529       && INSN_FROM_TARGET_P (insn))
1530     return;
1531
1532   /* If only calls can throw, and we have a frame pointer,
1533      save up adjustments until we see the CALL_INSN.  */
1534   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1535     {
1536       if (CALL_P (insn) && !after_p)
1537         {
1538           /* Extract the size of the args from the CALL rtx itself.  */
1539           insn = PATTERN (insn);
1540           if (GET_CODE (insn) == PARALLEL)
1541             insn = XVECEXP (insn, 0, 0);
1542           if (GET_CODE (insn) == SET)
1543             insn = SET_SRC (insn);
1544           gcc_assert (GET_CODE (insn) == CALL);
1545           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1546         }
1547       return;
1548     }
1549
1550   if (CALL_P (insn) && !after_p)
1551     {
1552       if (!flag_asynchronous_unwind_tables)
1553         dwarf2out_args_size ("", args_size);
1554       return;
1555     }
1556   else if (BARRIER_P (insn))
1557     {
1558       /* Don't call compute_barrier_args_size () if the only
1559          BARRIER is at the end of function.  */
1560       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1561         compute_barrier_args_size ();
1562       if (barrier_args_size == NULL)
1563         offset = 0;
1564       else
1565         {
1566           offset = barrier_args_size[INSN_UID (insn)];
1567           if (offset < 0)
1568             offset = 0;
1569         }
1570
1571       offset -= args_size;
1572 #ifndef STACK_GROWS_DOWNWARD
1573       offset = -offset;
1574 #endif
1575     }
1576   else if (GET_CODE (PATTERN (insn)) == SET)
1577     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1578   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1579            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1580     {
1581       /* There may be stack adjustments inside compound insns.  Search
1582          for them.  */
1583       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1584         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1585           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1586                                          args_size, offset);
1587     }
1588   else
1589     return;
1590
1591   if (offset == 0)
1592     return;
1593
1594   label = dwarf2out_cfi_label (false);
1595   dwarf2out_args_size_adjust (offset, label);
1596 }
1597
1598 /* Adjust args_size based on stack adjustment OFFSET.  */
1599
1600 static void
1601 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1602 {
1603   if (cfa.reg == STACK_POINTER_REGNUM)
1604     cfa.offset += offset;
1605
1606   if (cfa_store.reg == STACK_POINTER_REGNUM)
1607     cfa_store.offset += offset;
1608
1609 #ifndef STACK_GROWS_DOWNWARD
1610   offset = -offset;
1611 #endif
1612
1613   args_size += offset;
1614   if (args_size < 0)
1615     args_size = 0;
1616
1617   def_cfa_1 (label, &cfa);
1618   if (flag_asynchronous_unwind_tables)
1619     dwarf2out_args_size (label, args_size);
1620 }
1621
1622 #endif
1623
1624 /* We delay emitting a register save until either (a) we reach the end
1625    of the prologue or (b) the register is clobbered.  This clusters
1626    register saves so that there are fewer pc advances.  */
1627
1628 struct GTY(()) queued_reg_save {
1629   struct queued_reg_save *next;
1630   rtx reg;
1631   HOST_WIDE_INT cfa_offset;
1632   rtx saved_reg;
1633 };
1634
1635 static GTY(()) struct queued_reg_save *queued_reg_saves;
1636
1637 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1638 struct GTY(()) reg_saved_in_data {
1639   rtx orig_reg;
1640   rtx saved_in_reg;
1641 };
1642
1643 /* A list of registers saved in other registers.
1644    The list intentionally has a small maximum capacity of 4; if your
1645    port needs more than that, you might consider implementing a
1646    more efficient data structure.  */
1647 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1648 static GTY(()) size_t num_regs_saved_in_regs;
1649
1650 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1651 static const char *last_reg_save_label;
1652
1653 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1654    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1655
1656 static void
1657 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1658 {
1659   struct queued_reg_save *q;
1660
1661   /* Duplicates waste space, but it's also necessary to remove them
1662      for correctness, since the queue gets output in reverse
1663      order.  */
1664   for (q = queued_reg_saves; q != NULL; q = q->next)
1665     if (REGNO (q->reg) == REGNO (reg))
1666       break;
1667
1668   if (q == NULL)
1669     {
1670       q = GGC_NEW (struct queued_reg_save);
1671       q->next = queued_reg_saves;
1672       queued_reg_saves = q;
1673     }
1674
1675   q->reg = reg;
1676   q->cfa_offset = offset;
1677   q->saved_reg = sreg;
1678
1679   last_reg_save_label = label;
1680 }
1681
1682 /* Output all the entries in QUEUED_REG_SAVES.  */
1683
1684 static void
1685 flush_queued_reg_saves (void)
1686 {
1687   struct queued_reg_save *q;
1688
1689   for (q = queued_reg_saves; q; q = q->next)
1690     {
1691       size_t i;
1692       unsigned int reg, sreg;
1693
1694       for (i = 0; i < num_regs_saved_in_regs; i++)
1695         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1696           break;
1697       if (q->saved_reg && i == num_regs_saved_in_regs)
1698         {
1699           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1700           num_regs_saved_in_regs++;
1701         }
1702       if (i != num_regs_saved_in_regs)
1703         {
1704           regs_saved_in_regs[i].orig_reg = q->reg;
1705           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1706         }
1707
1708       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1709       if (q->saved_reg)
1710         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1711       else
1712         sreg = INVALID_REGNUM;
1713       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1714     }
1715
1716   queued_reg_saves = NULL;
1717   last_reg_save_label = NULL;
1718 }
1719
1720 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1721    location for?  Or, does it clobber a register which we've previously
1722    said that some other register is saved in, and for which we now
1723    have a new location for?  */
1724
1725 static bool
1726 clobbers_queued_reg_save (const_rtx insn)
1727 {
1728   struct queued_reg_save *q;
1729
1730   for (q = queued_reg_saves; q; q = q->next)
1731     {
1732       size_t i;
1733       if (modified_in_p (q->reg, insn))
1734         return true;
1735       for (i = 0; i < num_regs_saved_in_regs; i++)
1736         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1737             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1738           return true;
1739     }
1740
1741   return false;
1742 }
1743
1744 /* Entry point for saving the first register into the second.  */
1745
1746 void
1747 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1748 {
1749   size_t i;
1750   unsigned int regno, sregno;
1751
1752   for (i = 0; i < num_regs_saved_in_regs; i++)
1753     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1754       break;
1755   if (i == num_regs_saved_in_regs)
1756     {
1757       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1758       num_regs_saved_in_regs++;
1759     }
1760   regs_saved_in_regs[i].orig_reg = reg;
1761   regs_saved_in_regs[i].saved_in_reg = sreg;
1762
1763   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1764   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1765   reg_save (label, regno, sregno, 0);
1766 }
1767
1768 /* What register, if any, is currently saved in REG?  */
1769
1770 static rtx
1771 reg_saved_in (rtx reg)
1772 {
1773   unsigned int regn = REGNO (reg);
1774   size_t i;
1775   struct queued_reg_save *q;
1776
1777   for (q = queued_reg_saves; q; q = q->next)
1778     if (q->saved_reg && regn == REGNO (q->saved_reg))
1779       return q->reg;
1780
1781   for (i = 0; i < num_regs_saved_in_regs; i++)
1782     if (regs_saved_in_regs[i].saved_in_reg
1783         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1784       return regs_saved_in_regs[i].orig_reg;
1785
1786   return NULL_RTX;
1787 }
1788
1789
1790 /* A temporary register holding an integral value used in adjusting SP
1791    or setting up the store_reg.  The "offset" field holds the integer
1792    value, not an offset.  */
1793 static dw_cfa_location cfa_temp;
1794
1795 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1796
1797 static void
1798 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1799 {
1800   memset (&cfa, 0, sizeof (cfa));
1801
1802   switch (GET_CODE (pat))
1803     {
1804     case PLUS:
1805       cfa.reg = REGNO (XEXP (pat, 0));
1806       cfa.offset = INTVAL (XEXP (pat, 1));
1807       break;
1808
1809     case REG:
1810       cfa.reg = REGNO (pat);
1811       break;
1812
1813     default:
1814       /* Recurse and define an expression.  */
1815       gcc_unreachable ();
1816     }
1817
1818   def_cfa_1 (label, &cfa);
1819 }
1820
1821 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1822
1823 static void
1824 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1825 {
1826   rtx src, dest;
1827
1828   gcc_assert (GET_CODE (pat) == SET);
1829   dest = XEXP (pat, 0);
1830   src = XEXP (pat, 1);
1831
1832   switch (GET_CODE (src))
1833     {
1834     case PLUS:
1835       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1836       cfa.offset -= INTVAL (XEXP (src, 1));
1837       break;
1838
1839     case REG:
1840         break;
1841
1842     default:
1843         gcc_unreachable ();
1844     }
1845
1846   cfa.reg = REGNO (dest);
1847   gcc_assert (cfa.indirect == 0);
1848
1849   def_cfa_1 (label, &cfa);
1850 }
1851
1852 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1853
1854 static void
1855 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1856 {
1857   HOST_WIDE_INT offset;
1858   rtx src, addr, span;
1859
1860   src = XEXP (set, 1);
1861   addr = XEXP (set, 0);
1862   gcc_assert (MEM_P (addr));
1863   addr = XEXP (addr, 0);
1864   
1865   /* As documented, only consider extremely simple addresses.  */
1866   switch (GET_CODE (addr))
1867     {
1868     case REG:
1869       gcc_assert (REGNO (addr) == cfa.reg);
1870       offset = -cfa.offset;
1871       break;
1872     case PLUS:
1873       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1874       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1875       break;
1876     default:
1877       gcc_unreachable ();
1878     }
1879
1880   span = targetm.dwarf_register_span (src);
1881
1882   /* ??? We'd like to use queue_reg_save, but we need to come up with
1883      a different flushing heuristic for epilogues.  */
1884   if (!span)
1885     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1886   else
1887     {
1888       /* We have a PARALLEL describing where the contents of SRC live.
1889          Queue register saves for each piece of the PARALLEL.  */
1890       int par_index;
1891       int limit;
1892       HOST_WIDE_INT span_offset = offset;
1893
1894       gcc_assert (GET_CODE (span) == PARALLEL);
1895
1896       limit = XVECLEN (span, 0);
1897       for (par_index = 0; par_index < limit; par_index++)
1898         {
1899           rtx elem = XVECEXP (span, 0, par_index);
1900
1901           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1902                     INVALID_REGNUM, span_offset);
1903           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1904         }
1905     }
1906 }
1907
1908 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1909
1910 static void
1911 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1912 {
1913   rtx src, dest;
1914   unsigned sregno, dregno;
1915
1916   src = XEXP (set, 1);
1917   dest = XEXP (set, 0);
1918
1919   if (src == pc_rtx)
1920     sregno = DWARF_FRAME_RETURN_COLUMN;
1921   else
1922     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1923
1924   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1925
1926   /* ??? We'd like to use queue_reg_save, but we need to come up with
1927      a different flushing heuristic for epilogues.  */
1928   reg_save (label, sregno, dregno, 0);
1929 }
1930
1931 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1932
1933 static void
1934 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1935 {
1936   dw_cfi_ref cfi = new_cfi ();
1937   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1938
1939   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1940   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1941
1942   add_fde_cfi (label, cfi);
1943 }
1944
1945 /* Record call frame debugging information for an expression EXPR,
1946    which either sets SP or FP (adjusting how we calculate the frame
1947    address) or saves a register to the stack or another register.
1948    LABEL indicates the address of EXPR.
1949
1950    This function encodes a state machine mapping rtxes to actions on
1951    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1952    users need not read the source code.
1953
1954   The High-Level Picture
1955
1956   Changes in the register we use to calculate the CFA: Currently we
1957   assume that if you copy the CFA register into another register, we
1958   should take the other one as the new CFA register; this seems to
1959   work pretty well.  If it's wrong for some target, it's simple
1960   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1961
1962   Changes in the register we use for saving registers to the stack:
1963   This is usually SP, but not always.  Again, we deduce that if you
1964   copy SP into another register (and SP is not the CFA register),
1965   then the new register is the one we will be using for register
1966   saves.  This also seems to work.
1967
1968   Register saves: There's not much guesswork about this one; if
1969   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1970   register save, and the register used to calculate the destination
1971   had better be the one we think we're using for this purpose.
1972   It's also assumed that a copy from a call-saved register to another
1973   register is saving that register if RTX_FRAME_RELATED_P is set on
1974   that instruction.  If the copy is from a call-saved register to
1975   the *same* register, that means that the register is now the same
1976   value as in the caller.
1977
1978   Except: If the register being saved is the CFA register, and the
1979   offset is nonzero, we are saving the CFA, so we assume we have to
1980   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1981   the intent is to save the value of SP from the previous frame.
1982
1983   In addition, if a register has previously been saved to a different
1984   register,
1985
1986   Invariants / Summaries of Rules
1987
1988   cfa          current rule for calculating the CFA.  It usually
1989                consists of a register and an offset.
1990   cfa_store    register used by prologue code to save things to the stack
1991                cfa_store.offset is the offset from the value of
1992                cfa_store.reg to the actual CFA
1993   cfa_temp     register holding an integral value.  cfa_temp.offset
1994                stores the value, which will be used to adjust the
1995                stack pointer.  cfa_temp is also used like cfa_store,
1996                to track stores to the stack via fp or a temp reg.
1997
1998   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1999                with cfa.reg as the first operand changes the cfa.reg and its
2000                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2001                cfa_temp.offset.
2002
2003   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2004                expression yielding a constant.  This sets cfa_temp.reg
2005                and cfa_temp.offset.
2006
2007   Rule 5:      Create a new register cfa_store used to save items to the
2008                stack.
2009
2010   Rules 10-14: Save a register to the stack.  Define offset as the
2011                difference of the original location and cfa_store's
2012                location (or cfa_temp's location if cfa_temp is used).
2013
2014   Rules 16-20: If AND operation happens on sp in prologue, we assume
2015                stack is realigned.  We will use a group of DW_OP_XXX
2016                expressions to represent the location of the stored
2017                register instead of CFA+offset.
2018
2019   The Rules
2020
2021   "{a,b}" indicates a choice of a xor b.
2022   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2023
2024   Rule 1:
2025   (set <reg1> <reg2>:cfa.reg)
2026   effects: cfa.reg = <reg1>
2027            cfa.offset unchanged
2028            cfa_temp.reg = <reg1>
2029            cfa_temp.offset = cfa.offset
2030
2031   Rule 2:
2032   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2033                               {<const_int>,<reg>:cfa_temp.reg}))
2034   effects: cfa.reg = sp if fp used
2035            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2036            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2037              if cfa_store.reg==sp
2038
2039   Rule 3:
2040   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2041   effects: cfa.reg = fp
2042            cfa_offset += +/- <const_int>
2043
2044   Rule 4:
2045   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2046   constraints: <reg1> != fp
2047                <reg1> != sp
2048   effects: cfa.reg = <reg1>
2049            cfa_temp.reg = <reg1>
2050            cfa_temp.offset = cfa.offset
2051
2052   Rule 5:
2053   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2054   constraints: <reg1> != fp
2055                <reg1> != sp
2056   effects: cfa_store.reg = <reg1>
2057            cfa_store.offset = cfa.offset - cfa_temp.offset
2058
2059   Rule 6:
2060   (set <reg> <const_int>)
2061   effects: cfa_temp.reg = <reg>
2062            cfa_temp.offset = <const_int>
2063
2064   Rule 7:
2065   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2066   effects: cfa_temp.reg = <reg1>
2067            cfa_temp.offset |= <const_int>
2068
2069   Rule 8:
2070   (set <reg> (high <exp>))
2071   effects: none
2072
2073   Rule 9:
2074   (set <reg> (lo_sum <exp> <const_int>))
2075   effects: cfa_temp.reg = <reg>
2076            cfa_temp.offset = <const_int>
2077
2078   Rule 10:
2079   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2080   effects: cfa_store.offset -= <const_int>
2081            cfa.offset = cfa_store.offset if cfa.reg == sp
2082            cfa.reg = sp
2083            cfa.base_offset = -cfa_store.offset
2084
2085   Rule 11:
2086   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2087   effects: cfa_store.offset += -/+ mode_size(mem)
2088            cfa.offset = cfa_store.offset if cfa.reg == sp
2089            cfa.reg = sp
2090            cfa.base_offset = -cfa_store.offset
2091
2092   Rule 12:
2093   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2094
2095        <reg2>)
2096   effects: cfa.reg = <reg1>
2097            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2098
2099   Rule 13:
2100   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2101   effects: cfa.reg = <reg1>
2102            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2103
2104   Rule 14:
2105   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2106   effects: cfa.reg = <reg1>
2107            cfa.base_offset = -cfa_temp.offset
2108            cfa_temp.offset -= mode_size(mem)
2109
2110   Rule 15:
2111   (set <reg> {unspec, unspec_volatile})
2112   effects: target-dependent
2113
2114   Rule 16:
2115   (set sp (and: sp <const_int>))
2116   constraints: cfa_store.reg == sp
2117   effects: current_fde.stack_realign = 1
2118            cfa_store.offset = 0
2119            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2120
2121   Rule 17:
2122   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2123   effects: cfa_store.offset += -/+ mode_size(mem)
2124
2125   Rule 18:
2126   (set (mem ({pre_inc, pre_dec} sp)) fp)
2127   constraints: fde->stack_realign == 1
2128   effects: cfa_store.offset = 0
2129            cfa.reg != HARD_FRAME_POINTER_REGNUM
2130
2131   Rule 19:
2132   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2133   constraints: fde->stack_realign == 1
2134                && cfa.offset == 0
2135                && cfa.indirect == 0
2136                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2137   effects: Use DW_CFA_def_cfa_expression to define cfa
2138            cfa.reg == fde->drap_reg
2139
2140   Rule 20:
2141   (set reg fde->drap_reg)
2142   constraints: fde->vdrap_reg == INVALID_REGNUM
2143   effects: fde->vdrap_reg = reg.
2144   (set mem fde->drap_reg)
2145   constraints: fde->drap_reg_saved == 1
2146   effects: none.  */
2147
2148 static void
2149 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2150 {
2151   rtx src, dest, span;
2152   HOST_WIDE_INT offset;
2153   dw_fde_ref fde;
2154
2155   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2156      the PARALLEL independently. The first element is always processed if
2157      it is a SET. This is for backward compatibility.   Other elements
2158      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2159      flag is set in them.  */
2160   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2161     {
2162       int par_index;
2163       int limit = XVECLEN (expr, 0);
2164       rtx elem;
2165
2166       /* PARALLELs have strict read-modify-write semantics, so we
2167          ought to evaluate every rvalue before changing any lvalue.
2168          It's cumbersome to do that in general, but there's an
2169          easy approximation that is enough for all current users:
2170          handle register saves before register assignments.  */
2171       if (GET_CODE (expr) == PARALLEL)
2172         for (par_index = 0; par_index < limit; par_index++)
2173           {
2174             elem = XVECEXP (expr, 0, par_index);
2175             if (GET_CODE (elem) == SET
2176                 && MEM_P (SET_DEST (elem))
2177                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2178               dwarf2out_frame_debug_expr (elem, label);
2179           }
2180
2181       for (par_index = 0; par_index < limit; par_index++)
2182         {
2183           elem = XVECEXP (expr, 0, par_index);
2184           if (GET_CODE (elem) == SET
2185               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2186               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2187             dwarf2out_frame_debug_expr (elem, label);
2188           else if (GET_CODE (elem) == SET
2189                    && par_index != 0
2190                    && !RTX_FRAME_RELATED_P (elem))
2191             {
2192               /* Stack adjustment combining might combine some post-prologue
2193                  stack adjustment into a prologue stack adjustment.  */
2194               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2195
2196               if (offset != 0)
2197                 dwarf2out_args_size_adjust (offset, label);
2198             }
2199         }
2200       return;
2201     }
2202
2203   gcc_assert (GET_CODE (expr) == SET);
2204
2205   src = SET_SRC (expr);
2206   dest = SET_DEST (expr);
2207
2208   if (REG_P (src))
2209     {
2210       rtx rsi = reg_saved_in (src);
2211       if (rsi)
2212         src = rsi;
2213     }
2214
2215   fde = current_fde ();
2216
2217   if (REG_P (src)
2218       && fde
2219       && fde->drap_reg == REGNO (src)
2220       && (fde->drap_reg_saved
2221           || REG_P (dest)))
2222     {
2223       /* Rule 20 */
2224       /* If we are saving dynamic realign argument pointer to a
2225          register, the destination is virtual dynamic realign
2226          argument pointer.  It may be used to access argument.  */
2227       if (REG_P (dest))
2228         {
2229           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2230           fde->vdrap_reg = REGNO (dest);
2231         }
2232       return;
2233     }
2234
2235   switch (GET_CODE (dest))
2236     {
2237     case REG:
2238       switch (GET_CODE (src))
2239         {
2240           /* Setting FP from SP.  */
2241         case REG:
2242           if (cfa.reg == (unsigned) REGNO (src))
2243             {
2244               /* Rule 1 */
2245               /* Update the CFA rule wrt SP or FP.  Make sure src is
2246                  relative to the current CFA register.
2247
2248                  We used to require that dest be either SP or FP, but the
2249                  ARM copies SP to a temporary register, and from there to
2250                  FP.  So we just rely on the backends to only set
2251                  RTX_FRAME_RELATED_P on appropriate insns.  */
2252               cfa.reg = REGNO (dest);
2253               cfa_temp.reg = cfa.reg;
2254               cfa_temp.offset = cfa.offset;
2255             }
2256           else
2257             {
2258               /* Saving a register in a register.  */
2259               gcc_assert (!fixed_regs [REGNO (dest)]
2260                           /* For the SPARC and its register window.  */
2261                           || (DWARF_FRAME_REGNUM (REGNO (src))
2262                               == DWARF_FRAME_RETURN_COLUMN));
2263
2264               /* After stack is aligned, we can only save SP in FP
2265                  if drap register is used.  In this case, we have
2266                  to restore stack pointer with the CFA value and we
2267                  don't generate this DWARF information.  */
2268               if (fde
2269                   && fde->stack_realign
2270                   && REGNO (src) == STACK_POINTER_REGNUM)
2271                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2272                             && fde->drap_reg != INVALID_REGNUM
2273                             && cfa.reg != REGNO (src));
2274               else
2275                 queue_reg_save (label, src, dest, 0);
2276             }
2277           break;
2278
2279         case PLUS:
2280         case MINUS:
2281         case LO_SUM:
2282           if (dest == stack_pointer_rtx)
2283             {
2284               /* Rule 2 */
2285               /* Adjusting SP.  */
2286               switch (GET_CODE (XEXP (src, 1)))
2287                 {
2288                 case CONST_INT:
2289                   offset = INTVAL (XEXP (src, 1));
2290                   break;
2291                 case REG:
2292                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2293                               == cfa_temp.reg);
2294                   offset = cfa_temp.offset;
2295                   break;
2296                 default:
2297                   gcc_unreachable ();
2298                 }
2299
2300               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2301                 {
2302                   /* Restoring SP from FP in the epilogue.  */
2303                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2304                   cfa.reg = STACK_POINTER_REGNUM;
2305                 }
2306               else if (GET_CODE (src) == LO_SUM)
2307                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2308                 ;
2309               else
2310                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2311
2312               if (GET_CODE (src) != MINUS)
2313                 offset = -offset;
2314               if (cfa.reg == STACK_POINTER_REGNUM)
2315                 cfa.offset += offset;
2316               if (cfa_store.reg == STACK_POINTER_REGNUM)
2317                 cfa_store.offset += offset;
2318             }
2319           else if (dest == hard_frame_pointer_rtx)
2320             {
2321               /* Rule 3 */
2322               /* Either setting the FP from an offset of the SP,
2323                  or adjusting the FP */
2324               gcc_assert (frame_pointer_needed);
2325
2326               gcc_assert (REG_P (XEXP (src, 0))
2327                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2328                           && CONST_INT_P (XEXP (src, 1)));
2329               offset = INTVAL (XEXP (src, 1));
2330               if (GET_CODE (src) != MINUS)
2331                 offset = -offset;
2332               cfa.offset += offset;
2333               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2334             }
2335           else
2336             {
2337               gcc_assert (GET_CODE (src) != MINUS);
2338
2339               /* Rule 4 */
2340               if (REG_P (XEXP (src, 0))
2341                   && REGNO (XEXP (src, 0)) == cfa.reg
2342                   && CONST_INT_P (XEXP (src, 1)))
2343                 {
2344                   /* Setting a temporary CFA register that will be copied
2345                      into the FP later on.  */
2346                   offset = - INTVAL (XEXP (src, 1));
2347                   cfa.offset += offset;
2348                   cfa.reg = REGNO (dest);
2349                   /* Or used to save regs to the stack.  */
2350                   cfa_temp.reg = cfa.reg;
2351                   cfa_temp.offset = cfa.offset;
2352                 }
2353
2354               /* Rule 5 */
2355               else if (REG_P (XEXP (src, 0))
2356                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2357                        && XEXP (src, 1) == stack_pointer_rtx)
2358                 {
2359                   /* Setting a scratch register that we will use instead
2360                      of SP for saving registers to the stack.  */
2361                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2362                   cfa_store.reg = REGNO (dest);
2363                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2364                 }
2365
2366               /* Rule 9 */
2367               else if (GET_CODE (src) == LO_SUM
2368                        && CONST_INT_P (XEXP (src, 1)))
2369                 {
2370                   cfa_temp.reg = REGNO (dest);
2371                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2372                 }
2373               else
2374                 gcc_unreachable ();
2375             }
2376           break;
2377
2378           /* Rule 6 */
2379         case CONST_INT:
2380           cfa_temp.reg = REGNO (dest);
2381           cfa_temp.offset = INTVAL (src);
2382           break;
2383
2384           /* Rule 7 */
2385         case IOR:
2386           gcc_assert (REG_P (XEXP (src, 0))
2387                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2388                       && CONST_INT_P (XEXP (src, 1)));
2389
2390           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2391             cfa_temp.reg = REGNO (dest);
2392           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2393           break;
2394
2395           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2396              which will fill in all of the bits.  */
2397           /* Rule 8 */
2398         case HIGH:
2399           break;
2400
2401           /* Rule 15 */
2402         case UNSPEC:
2403         case UNSPEC_VOLATILE:
2404           gcc_assert (targetm.dwarf_handle_frame_unspec);
2405           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2406           return;
2407
2408           /* Rule 16 */
2409         case AND:
2410           /* If this AND operation happens on stack pointer in prologue,
2411              we assume the stack is realigned and we extract the
2412              alignment.  */
2413           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2414             {
2415               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2416               fde->stack_realign = 1;
2417               fde->stack_realignment = INTVAL (XEXP (src, 1));
2418               cfa_store.offset = 0;
2419
2420               if (cfa.reg != STACK_POINTER_REGNUM
2421                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2422                 fde->drap_reg = cfa.reg;
2423             }
2424           return;
2425
2426         default:
2427           gcc_unreachable ();
2428         }
2429
2430       def_cfa_1 (label, &cfa);
2431       break;
2432
2433     case MEM:
2434
2435       /* Saving a register to the stack.  Make sure dest is relative to the
2436          CFA register.  */
2437       switch (GET_CODE (XEXP (dest, 0)))
2438         {
2439           /* Rule 10 */
2440           /* With a push.  */
2441         case PRE_MODIFY:
2442           /* We can't handle variable size modifications.  */
2443           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2444                       == CONST_INT);
2445           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2446
2447           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2448                       && cfa_store.reg == STACK_POINTER_REGNUM);
2449
2450           cfa_store.offset += offset;
2451           if (cfa.reg == STACK_POINTER_REGNUM)
2452             cfa.offset = cfa_store.offset;
2453
2454           offset = -cfa_store.offset;
2455           break;
2456
2457           /* Rule 11 */
2458         case PRE_INC:
2459         case PRE_DEC:
2460           offset = GET_MODE_SIZE (GET_MODE (dest));
2461           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2462             offset = -offset;
2463
2464           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2465                        == STACK_POINTER_REGNUM)
2466                       && cfa_store.reg == STACK_POINTER_REGNUM);
2467
2468           cfa_store.offset += offset;
2469
2470           /* Rule 18: If stack is aligned, we will use FP as a
2471              reference to represent the address of the stored
2472              regiser.  */
2473           if (fde
2474               && fde->stack_realign
2475               && src == hard_frame_pointer_rtx)
2476             {
2477               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2478               cfa_store.offset = 0;
2479             }
2480
2481           if (cfa.reg == STACK_POINTER_REGNUM)
2482             cfa.offset = cfa_store.offset;
2483
2484           offset = -cfa_store.offset;
2485           break;
2486
2487           /* Rule 12 */
2488           /* With an offset.  */
2489         case PLUS:
2490         case MINUS:
2491         case LO_SUM:
2492           {
2493             int regno;
2494
2495             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2496                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2497             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2498             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2499               offset = -offset;
2500
2501             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2502
2503             if (cfa_store.reg == (unsigned) regno)
2504               offset -= cfa_store.offset;
2505             else
2506               {
2507                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2508                 offset -= cfa_temp.offset;
2509               }
2510           }
2511           break;
2512
2513           /* Rule 13 */
2514           /* Without an offset.  */
2515         case REG:
2516           {
2517             int regno = REGNO (XEXP (dest, 0));
2518
2519             if (cfa_store.reg == (unsigned) regno)
2520               offset = -cfa_store.offset;
2521             else
2522               {
2523                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2524                 offset = -cfa_temp.offset;
2525               }
2526           }
2527           break;
2528
2529           /* Rule 14 */
2530         case POST_INC:
2531           gcc_assert (cfa_temp.reg
2532                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2533           offset = -cfa_temp.offset;
2534           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2535           break;
2536
2537         default:
2538           gcc_unreachable ();
2539         }
2540
2541         /* Rule 17 */
2542         /* If the source operand of this MEM operation is not a
2543            register, basically the source is return address.  Here
2544            we only care how much stack grew and we don't save it.  */
2545       if (!REG_P (src))
2546         break;
2547
2548       if (REGNO (src) != STACK_POINTER_REGNUM
2549           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2550           && (unsigned) REGNO (src) == cfa.reg)
2551         {
2552           /* We're storing the current CFA reg into the stack.  */
2553
2554           if (cfa.offset == 0)
2555             {
2556               /* Rule 19 */
2557               /* If stack is aligned, putting CFA reg into stack means
2558                  we can no longer use reg + offset to represent CFA.
2559                  Here we use DW_CFA_def_cfa_expression instead.  The
2560                  result of this expression equals to the original CFA
2561                  value.  */
2562               if (fde
2563                   && fde->stack_realign
2564                   && cfa.indirect == 0
2565                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2566                 {
2567                   dw_cfa_location cfa_exp;
2568
2569                   gcc_assert (fde->drap_reg == cfa.reg);
2570
2571                   cfa_exp.indirect = 1;
2572                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2573                   cfa_exp.base_offset = offset;
2574                   cfa_exp.offset = 0;
2575
2576                   fde->drap_reg_saved = 1;
2577
2578                   def_cfa_1 (label, &cfa_exp);
2579                   break;
2580                 }
2581
2582               /* If the source register is exactly the CFA, assume
2583                  we're saving SP like any other register; this happens
2584                  on the ARM.  */
2585               def_cfa_1 (label, &cfa);
2586               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2587               break;
2588             }
2589           else
2590             {
2591               /* Otherwise, we'll need to look in the stack to
2592                  calculate the CFA.  */
2593               rtx x = XEXP (dest, 0);
2594
2595               if (!REG_P (x))
2596                 x = XEXP (x, 0);
2597               gcc_assert (REG_P (x));
2598
2599               cfa.reg = REGNO (x);
2600               cfa.base_offset = offset;
2601               cfa.indirect = 1;
2602               def_cfa_1 (label, &cfa);
2603               break;
2604             }
2605         }
2606
2607       def_cfa_1 (label, &cfa);
2608       {
2609         span = targetm.dwarf_register_span (src);
2610
2611         if (!span)
2612           queue_reg_save (label, src, NULL_RTX, offset);
2613         else
2614           {
2615             /* We have a PARALLEL describing where the contents of SRC
2616                live.  Queue register saves for each piece of the
2617                PARALLEL.  */
2618             int par_index;
2619             int limit;
2620             HOST_WIDE_INT span_offset = offset;
2621
2622             gcc_assert (GET_CODE (span) == PARALLEL);
2623
2624             limit = XVECLEN (span, 0);
2625             for (par_index = 0; par_index < limit; par_index++)
2626               {
2627                 rtx elem = XVECEXP (span, 0, par_index);
2628
2629                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2630                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2631               }
2632           }
2633       }
2634       break;
2635
2636     default:
2637       gcc_unreachable ();
2638     }
2639 }
2640
2641 /* Record call frame debugging information for INSN, which either
2642    sets SP or FP (adjusting how we calculate the frame address) or saves a
2643    register to the stack.  If INSN is NULL_RTX, initialize our state.
2644
2645    If AFTER_P is false, we're being called before the insn is emitted,
2646    otherwise after.  Call instructions get invoked twice.  */
2647
2648 void
2649 dwarf2out_frame_debug (rtx insn, bool after_p)
2650 {
2651   const char *label;
2652   rtx note, n;
2653   bool handled_one = false;
2654
2655   if (insn == NULL_RTX)
2656     {
2657       size_t i;
2658
2659       /* Flush any queued register saves.  */
2660       flush_queued_reg_saves ();
2661
2662       /* Set up state for generating call frame debug info.  */
2663       lookup_cfa (&cfa);
2664       gcc_assert (cfa.reg
2665                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2666
2667       cfa.reg = STACK_POINTER_REGNUM;
2668       cfa_store = cfa;
2669       cfa_temp.reg = -1;
2670       cfa_temp.offset = 0;
2671
2672       for (i = 0; i < num_regs_saved_in_regs; i++)
2673         {
2674           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2675           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2676         }
2677       num_regs_saved_in_regs = 0;
2678
2679       if (barrier_args_size)
2680         {
2681           XDELETEVEC (barrier_args_size);
2682           barrier_args_size = NULL;
2683         }
2684       return;
2685     }
2686
2687   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2688     flush_queued_reg_saves ();
2689
2690   if (! RTX_FRAME_RELATED_P (insn))
2691     {
2692       if (!ACCUMULATE_OUTGOING_ARGS)
2693         dwarf2out_stack_adjust (insn, after_p);
2694       return;
2695     }
2696
2697   label = dwarf2out_cfi_label (false);
2698
2699   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2700     switch (REG_NOTE_KIND (note))
2701       {
2702       case REG_FRAME_RELATED_EXPR:
2703         insn = XEXP (note, 0);
2704         goto found;
2705
2706       case REG_CFA_DEF_CFA:
2707         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2708         handled_one = true;
2709         break;
2710
2711       case REG_CFA_ADJUST_CFA:
2712         n = XEXP (note, 0);
2713         if (n == NULL)
2714           {
2715             n = PATTERN (insn);
2716             if (GET_CODE (n) == PARALLEL)
2717               n = XVECEXP (n, 0, 0);
2718           }
2719         dwarf2out_frame_debug_adjust_cfa (n, label);
2720         handled_one = true;
2721         break;
2722
2723       case REG_CFA_OFFSET:
2724         n = XEXP (note, 0);
2725         if (n == NULL)
2726           n = single_set (insn);
2727         dwarf2out_frame_debug_cfa_offset (n, label);
2728         handled_one = true;
2729         break;
2730
2731       case REG_CFA_REGISTER:
2732         n = XEXP (note, 0);
2733         if (n == NULL)
2734           {
2735             n = PATTERN (insn);
2736             if (GET_CODE (n) == PARALLEL)
2737               n = XVECEXP (n, 0, 0);
2738           }
2739         dwarf2out_frame_debug_cfa_register (n, label);
2740         handled_one = true;
2741         break;
2742
2743       case REG_CFA_RESTORE:
2744         n = XEXP (note, 0);
2745         if (n == NULL)
2746           {
2747             n = PATTERN (insn);
2748             if (GET_CODE (n) == PARALLEL)
2749               n = XVECEXP (n, 0, 0);
2750             n = XEXP (n, 0);
2751           }
2752         dwarf2out_frame_debug_cfa_restore (n, label);
2753         handled_one = true;
2754         break;
2755
2756       default:
2757         break;
2758       }
2759   if (handled_one)
2760     return;
2761
2762   insn = PATTERN (insn);
2763  found:
2764   dwarf2out_frame_debug_expr (insn, label);
2765 }
2766
2767 /* Determine if we need to save and restore CFI information around this
2768    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2769    we do need to save/restore, then emit the save now, and insert a
2770    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2771
2772 void
2773 dwarf2out_begin_epilogue (rtx insn)
2774 {
2775   bool saw_frp = false;
2776   rtx i;
2777
2778   /* Scan forward to the return insn, noticing if there are possible
2779      frame related insns.  */
2780   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2781     {
2782       if (!INSN_P (i))
2783         continue;
2784
2785       /* Look for both regular and sibcalls to end the block.  */
2786       if (returnjump_p (i))
2787         break;
2788       if (CALL_P (i) && SIBLING_CALL_P (i))
2789         break;
2790
2791       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2792         {
2793           int idx;
2794           rtx seq = PATTERN (i);
2795
2796           if (returnjump_p (XVECEXP (seq, 0, 0)))
2797             break;
2798           if (CALL_P (XVECEXP (seq, 0, 0))
2799               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2800             break;
2801
2802           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2803             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2804               saw_frp = true;
2805         }
2806
2807       if (RTX_FRAME_RELATED_P (i))
2808         saw_frp = true;
2809     }
2810
2811   /* If the port doesn't emit epilogue unwind info, we don't need a
2812      save/restore pair.  */
2813   if (!saw_frp)
2814     return;
2815
2816   /* Otherwise, search forward to see if the return insn was the last
2817      basic block of the function.  If so, we don't need save/restore.  */
2818   gcc_assert (i != NULL);
2819   i = next_real_insn (i);
2820   if (i == NULL)
2821     return;
2822
2823   /* Insert the restore before that next real insn in the stream, and before
2824      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2825      properly nested.  This should be after any label or alignment.  This
2826      will be pushed into the CFI stream by the function below.  */
2827   while (1)
2828     {
2829       rtx p = PREV_INSN (i);
2830       if (!NOTE_P (p))
2831         break;
2832       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2833         break;
2834       i = p;
2835     }
2836   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2837
2838   emit_cfa_remember = true;
2839
2840   /* And emulate the state save.  */
2841   gcc_assert (!cfa_remember.in_use);
2842   cfa_remember = cfa;
2843   cfa_remember.in_use = 1;
2844 }
2845
2846 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2847
2848 void
2849 dwarf2out_frame_debug_restore_state (void)
2850 {
2851   dw_cfi_ref cfi = new_cfi (); 
2852   const char *label = dwarf2out_cfi_label (false);
2853
2854   cfi->dw_cfi_opc = DW_CFA_restore_state;
2855   add_fde_cfi (label, cfi);
2856
2857   gcc_assert (cfa_remember.in_use);
2858   cfa = cfa_remember;
2859   cfa_remember.in_use = 0;
2860 }
2861
2862 #endif
2863
2864 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2865 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2866  (enum dwarf_call_frame_info cfi);
2867
2868 static enum dw_cfi_oprnd_type
2869 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2870 {
2871   switch (cfi)
2872     {
2873     case DW_CFA_nop:
2874     case DW_CFA_GNU_window_save:
2875     case DW_CFA_remember_state:
2876     case DW_CFA_restore_state:
2877       return dw_cfi_oprnd_unused;
2878
2879     case DW_CFA_set_loc:
2880     case DW_CFA_advance_loc1:
2881     case DW_CFA_advance_loc2:
2882     case DW_CFA_advance_loc4:
2883     case DW_CFA_MIPS_advance_loc8:
2884       return dw_cfi_oprnd_addr;
2885
2886     case DW_CFA_offset:
2887     case DW_CFA_offset_extended:
2888     case DW_CFA_def_cfa:
2889     case DW_CFA_offset_extended_sf:
2890     case DW_CFA_def_cfa_sf:
2891     case DW_CFA_restore:
2892     case DW_CFA_restore_extended:
2893     case DW_CFA_undefined:
2894     case DW_CFA_same_value:
2895     case DW_CFA_def_cfa_register:
2896     case DW_CFA_register:
2897       return dw_cfi_oprnd_reg_num;
2898
2899     case DW_CFA_def_cfa_offset:
2900     case DW_CFA_GNU_args_size:
2901     case DW_CFA_def_cfa_offset_sf:
2902       return dw_cfi_oprnd_offset;
2903
2904     case DW_CFA_def_cfa_expression:
2905     case DW_CFA_expression:
2906       return dw_cfi_oprnd_loc;
2907
2908     default:
2909       gcc_unreachable ();
2910     }
2911 }
2912
2913 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2914 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2915  (enum dwarf_call_frame_info cfi);
2916
2917 static enum dw_cfi_oprnd_type
2918 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2919 {
2920   switch (cfi)
2921     {
2922     case DW_CFA_def_cfa:
2923     case DW_CFA_def_cfa_sf:
2924     case DW_CFA_offset:
2925     case DW_CFA_offset_extended_sf:
2926     case DW_CFA_offset_extended:
2927       return dw_cfi_oprnd_offset;
2928
2929     case DW_CFA_register:
2930       return dw_cfi_oprnd_reg_num;
2931
2932     default:
2933       return dw_cfi_oprnd_unused;
2934     }
2935 }
2936
2937 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2938
2939 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2940    switch to the data section instead, and write out a synthetic start label
2941    for collect2 the first time around.  */
2942
2943 static void
2944 switch_to_eh_frame_section (bool back)
2945 {
2946   tree label;
2947
2948 #ifdef EH_FRAME_SECTION_NAME
2949   if (eh_frame_section == 0)
2950     {
2951       int flags;
2952
2953       if (EH_TABLES_CAN_BE_READ_ONLY)
2954         {
2955           int fde_encoding;
2956           int per_encoding;
2957           int lsda_encoding;
2958
2959           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2960                                                        /*global=*/0);
2961           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2962                                                        /*global=*/1);
2963           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2964                                                         /*global=*/0);
2965           flags = ((! flag_pic
2966                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2967                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2968                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2969                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2970                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2971                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2972                    ? 0 : SECTION_WRITE);
2973         }
2974       else
2975         flags = SECTION_WRITE;
2976       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2977     }
2978 #endif
2979
2980   if (eh_frame_section)
2981     switch_to_section (eh_frame_section);
2982   else
2983     {
2984       /* We have no special eh_frame section.  Put the information in
2985          the data section and emit special labels to guide collect2.  */
2986       switch_to_section (data_section);
2987
2988       if (!back)
2989         {
2990           label = get_file_function_name ("F");
2991           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2992           targetm.asm_out.globalize_label (asm_out_file,
2993                                            IDENTIFIER_POINTER (label));
2994           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2995         }
2996     }
2997 }
2998
2999 /* Switch [BACK] to the eh or debug frame table section, depending on
3000    FOR_EH.  */
3001
3002 static void
3003 switch_to_frame_table_section (int for_eh, bool back)
3004 {
3005   if (for_eh)
3006     switch_to_eh_frame_section (back);
3007   else
3008     {
3009       if (!debug_frame_section)
3010         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3011                                            SECTION_DEBUG, NULL);
3012       switch_to_section (debug_frame_section);
3013     }
3014 }
3015
3016 /* Output a Call Frame Information opcode and its operand(s).  */
3017
3018 static void
3019 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3020 {
3021   unsigned long r;
3022   HOST_WIDE_INT off;
3023
3024   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3025     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3026                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3027                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3028                          ((unsigned HOST_WIDE_INT)
3029                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3030   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3031     {
3032       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3033       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3034                            "DW_CFA_offset, column 0x%lx", r);
3035       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3036       dw2_asm_output_data_uleb128 (off, NULL);
3037     }
3038   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3039     {
3040       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3041       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3042                            "DW_CFA_restore, column 0x%lx", r);
3043     }
3044   else
3045     {
3046       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3047                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3048
3049       switch (cfi->dw_cfi_opc)
3050         {
3051         case DW_CFA_set_loc:
3052           if (for_eh)
3053             dw2_asm_output_encoded_addr_rtx (
3054                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3055                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3056                 false, NULL);
3057           else
3058             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3059                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3060           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3061           break;
3062
3063         case DW_CFA_advance_loc1:
3064           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3065                                 fde->dw_fde_current_label, NULL);
3066           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3067           break;
3068
3069         case DW_CFA_advance_loc2:
3070           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3071                                 fde->dw_fde_current_label, NULL);
3072           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3073           break;
3074
3075         case DW_CFA_advance_loc4:
3076           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3077                                 fde->dw_fde_current_label, NULL);
3078           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3079           break;
3080
3081         case DW_CFA_MIPS_advance_loc8:
3082           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3083                                 fde->dw_fde_current_label, NULL);
3084           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3085           break;
3086
3087         case DW_CFA_offset_extended:
3088           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3089           dw2_asm_output_data_uleb128 (r, NULL);
3090           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3091           dw2_asm_output_data_uleb128 (off, NULL);
3092           break;
3093
3094         case DW_CFA_def_cfa:
3095           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3096           dw2_asm_output_data_uleb128 (r, NULL);
3097           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3098           break;
3099
3100         case DW_CFA_offset_extended_sf:
3101           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3102           dw2_asm_output_data_uleb128 (r, NULL);
3103           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3104           dw2_asm_output_data_sleb128 (off, NULL);
3105           break;
3106
3107         case DW_CFA_def_cfa_sf:
3108           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3109           dw2_asm_output_data_uleb128 (r, NULL);
3110           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3111           dw2_asm_output_data_sleb128 (off, NULL);
3112           break;
3113
3114         case DW_CFA_restore_extended:
3115         case DW_CFA_undefined:
3116         case DW_CFA_same_value:
3117         case DW_CFA_def_cfa_register:
3118           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3119           dw2_asm_output_data_uleb128 (r, NULL);
3120           break;
3121
3122         case DW_CFA_register:
3123           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3124           dw2_asm_output_data_uleb128 (r, NULL);
3125           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3126           dw2_asm_output_data_uleb128 (r, NULL);
3127           break;
3128
3129         case DW_CFA_def_cfa_offset:
3130         case DW_CFA_GNU_args_size:
3131           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3132           break;
3133
3134         case DW_CFA_def_cfa_offset_sf:
3135           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3136           dw2_asm_output_data_sleb128 (off, NULL);
3137           break;
3138
3139         case DW_CFA_GNU_window_save:
3140           break;
3141
3142         case DW_CFA_def_cfa_expression:
3143         case DW_CFA_expression:
3144           output_cfa_loc (cfi);
3145           break;
3146
3147         case DW_CFA_GNU_negative_offset_extended:
3148           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3149           gcc_unreachable ();
3150
3151         default:
3152           break;
3153         }
3154     }
3155 }
3156
3157 /* Similar, but do it via assembler directives instead.  */
3158
3159 static void
3160 output_cfi_directive (dw_cfi_ref cfi)
3161 {
3162   unsigned long r, r2;
3163
3164   switch (cfi->dw_cfi_opc)
3165     {
3166     case DW_CFA_advance_loc:
3167     case DW_CFA_advance_loc1:
3168     case DW_CFA_advance_loc2:
3169     case DW_CFA_advance_loc4:
3170     case DW_CFA_MIPS_advance_loc8:
3171     case DW_CFA_set_loc:
3172       /* Should only be created by add_fde_cfi in a code path not
3173          followed when emitting via directives.  The assembler is
3174          going to take care of this for us.  */
3175       gcc_unreachable ();
3176
3177     case DW_CFA_offset:
3178     case DW_CFA_offset_extended:
3179     case DW_CFA_offset_extended_sf:
3180       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3181       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3182                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3183       break;
3184
3185     case DW_CFA_restore:
3186     case DW_CFA_restore_extended:
3187       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3188       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3189       break;
3190
3191     case DW_CFA_undefined:
3192       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3193       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3194       break;
3195
3196     case DW_CFA_same_value:
3197       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3198       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3199       break;
3200
3201     case DW_CFA_def_cfa:
3202     case DW_CFA_def_cfa_sf:
3203       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3204       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3205                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3206       break;
3207
3208     case DW_CFA_def_cfa_register:
3209       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3210       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3211       break;
3212
3213     case DW_CFA_register:
3214       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3215       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3216       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3217       break;
3218
3219     case DW_CFA_def_cfa_offset:
3220     case DW_CFA_def_cfa_offset_sf:
3221       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3222                HOST_WIDE_INT_PRINT_DEC"\n",
3223                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3224       break;
3225
3226     case DW_CFA_remember_state:
3227       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3228       break;
3229     case DW_CFA_restore_state:
3230       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3231       break;
3232
3233     case DW_CFA_GNU_args_size:
3234       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3235       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3236       if (flag_debug_asm)
3237         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3238                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3239       fputc ('\n', asm_out_file);
3240       break;
3241
3242     case DW_CFA_GNU_window_save:
3243       fprintf (asm_out_file, "\t.cfi_window_save\n");
3244       break;
3245
3246     case DW_CFA_def_cfa_expression:
3247     case DW_CFA_expression:
3248       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3249       output_cfa_loc_raw (cfi);
3250       fputc ('\n', asm_out_file);
3251       break;
3252
3253     default:
3254       gcc_unreachable ();
3255     }
3256 }
3257
3258 DEF_VEC_P (dw_cfi_ref);
3259 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3260
3261 /* Output CFIs to bring current FDE to the same state as after executing
3262    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3263    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3264    other arguments to pass to output_cfi.  */
3265
3266 static void
3267 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3268 {
3269   struct dw_cfi_struct cfi_buf;
3270   dw_cfi_ref cfi2;
3271   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3272   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3273   unsigned int len, idx;
3274
3275   for (;; cfi = cfi->dw_cfi_next)
3276     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3277       {
3278       case DW_CFA_advance_loc:
3279       case DW_CFA_advance_loc1:
3280       case DW_CFA_advance_loc2:
3281       case DW_CFA_advance_loc4:
3282       case DW_CFA_MIPS_advance_loc8:
3283       case DW_CFA_set_loc:
3284         /* All advances should be ignored.  */
3285         break;
3286       case DW_CFA_remember_state:
3287         {
3288           dw_cfi_ref args_size = cfi_args_size;
3289
3290           /* Skip everything between .cfi_remember_state and
3291              .cfi_restore_state.  */
3292           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3293             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3294               break;
3295             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3296               args_size = cfi2;
3297             else
3298               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3299
3300           if (cfi2 == NULL)
3301             goto flush_all;
3302           else
3303             {
3304               cfi = cfi2;
3305               cfi_args_size = args_size;
3306             }
3307           break;
3308         }
3309       case DW_CFA_GNU_args_size:
3310         cfi_args_size = cfi;
3311         break;
3312       case DW_CFA_GNU_window_save:
3313         goto flush_all;
3314       case DW_CFA_offset:
3315       case DW_CFA_offset_extended:
3316       case DW_CFA_offset_extended_sf:
3317       case DW_CFA_restore:
3318       case DW_CFA_restore_extended:
3319       case DW_CFA_undefined:
3320       case DW_CFA_same_value:
3321       case DW_CFA_register:
3322       case DW_CFA_val_offset:
3323       case DW_CFA_val_offset_sf:
3324       case DW_CFA_expression:
3325       case DW_CFA_val_expression:
3326       case DW_CFA_GNU_negative_offset_extended:
3327         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3328           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3329                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3330         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3331         break;
3332       case DW_CFA_def_cfa:
3333       case DW_CFA_def_cfa_sf:
3334       case DW_CFA_def_cfa_expression:
3335         cfi_cfa = cfi;
3336         cfi_cfa_offset = cfi;
3337         break;
3338       case DW_CFA_def_cfa_register:
3339         cfi_cfa = cfi;
3340         break;
3341       case DW_CFA_def_cfa_offset:
3342       case DW_CFA_def_cfa_offset_sf:
3343         cfi_cfa_offset = cfi;
3344         break;
3345       case DW_CFA_nop:
3346         gcc_assert (cfi == NULL);
3347       flush_all:
3348         len = VEC_length (dw_cfi_ref, regs);
3349         for (idx = 0; idx < len; idx++)
3350           {
3351             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3352             if (cfi2 != NULL
3353                 && cfi2->dw_cfi_opc != DW_CFA_restore
3354                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3355               {
3356                 if (do_cfi_asm)
3357                   output_cfi_directive (cfi2);
3358                 else
3359                   output_cfi (cfi2, fde, for_eh);
3360               }
3361           }
3362         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3363           {
3364             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3365             cfi_buf = *cfi_cfa;
3366             switch (cfi_cfa_offset->dw_cfi_opc)
3367               {
3368               case DW_CFA_def_cfa_offset:
3369                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3370                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3371                 break;
3372               case DW_CFA_def_cfa_offset_sf:
3373                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3374                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3375                 break;
3376               case DW_CFA_def_cfa:
3377               case DW_CFA_def_cfa_sf:
3378                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3379                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3380                 break;
3381               default:
3382                 gcc_unreachable ();
3383               }
3384             cfi_cfa = &cfi_buf;
3385           }
3386         else if (cfi_cfa_offset)
3387           cfi_cfa = cfi_cfa_offset;
3388         if (cfi_cfa)
3389           {
3390             if (do_cfi_asm)
3391               output_cfi_directive (cfi_cfa);
3392             else
3393               output_cfi (cfi_cfa, fde, for_eh);
3394           }
3395         cfi_cfa = NULL;
3396         cfi_cfa_offset = NULL;
3397         if (cfi_args_size
3398             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3399           {
3400             if (do_cfi_asm)
3401               output_cfi_directive (cfi_args_size);
3402             else
3403               output_cfi (cfi_args_size, fde, for_eh);
3404           }
3405         cfi_args_size = NULL;
3406         if (cfi == NULL)
3407           {
3408             VEC_free (dw_cfi_ref, heap, regs);
3409             return;
3410           }
3411         else if (do_cfi_asm)
3412           output_cfi_directive (cfi);
3413         else
3414           output_cfi (cfi, fde, for_eh);
3415         break;
3416       default:
3417         gcc_unreachable ();
3418     }
3419 }
3420
3421 /* Output one FDE.  */
3422
3423 static void
3424 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3425             char *section_start_label, int fde_encoding, char *augmentation,
3426             bool any_lsda_needed, int lsda_encoding)
3427 {
3428   const char *begin, *end;
3429   static unsigned int j;
3430   char l1[20], l2[20];
3431   dw_cfi_ref cfi;
3432
3433   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3434                                 /* empty */ 0);
3435   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3436                                   for_eh + j);
3437   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3438   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3439   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3440     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3441                          " indicating 64-bit DWARF extension");
3442   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3443                         "FDE Length");
3444   ASM_OUTPUT_LABEL (asm_out_file, l1);
3445
3446   if (for_eh)
3447     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3448   else
3449     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3450                            debug_frame_section, "FDE CIE offset");
3451
3452   if (!fde->dw_fde_switched_sections)
3453     {
3454       begin = fde->dw_fde_begin;
3455       end = fde->dw_fde_end;
3456     }
3457   else
3458     {
3459       /* For the first section, prefer dw_fde_begin over
3460          dw_fde_{hot,cold}_section_label, as the latter
3461          might be separated from the real start of the
3462          function by alignment padding.  */
3463       if (!second)
3464         begin = fde->dw_fde_begin;
3465       else if (fde->dw_fde_switched_cold_to_hot)
3466         begin = fde->dw_fde_hot_section_label;
3467       else
3468         begin = fde->dw_fde_unlikely_section_label;
3469       if (second ^ fde->dw_fde_switched_cold_to_hot)
3470         end = fde->dw_fde_unlikely_section_end_label;
3471       else
3472         end = fde->dw_fde_hot_section_end_label;
3473     }
3474
3475   if (for_eh)
3476     {
3477       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3478       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3479       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3480                                        "FDE initial location");
3481       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3482                             end, begin, "FDE address range");
3483     }
3484   else
3485     {
3486       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3487       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3488     }
3489
3490   if (augmentation[0])
3491     {
3492       if (any_lsda_needed)
3493         {
3494           int size = size_of_encoded_value (lsda_encoding);
3495
3496           if (lsda_encoding == DW_EH_PE_aligned)
3497             {
3498               int offset = (  4         /* Length */
3499                             + 4         /* CIE offset */
3500                             + 2 * size_of_encoded_value (fde_encoding)
3501                             + 1         /* Augmentation size */ );
3502               int pad = -offset & (PTR_SIZE - 1);
3503
3504               size += pad;
3505               gcc_assert (size_of_uleb128 (size) == 1);
3506             }
3507
3508           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3509
3510           if (fde->uses_eh_lsda)
3511             {
3512               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3513                                            fde->funcdef_number);
3514               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3515                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3516                                                false,
3517                                                "Language Specific Data Area");
3518             }
3519           else
3520             {
3521               if (lsda_encoding == DW_EH_PE_aligned)
3522                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3523               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3524                                    "Language Specific Data Area (none)");
3525             }
3526         }
3527       else
3528         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3529     }
3530
3531   /* Loop through the Call Frame Instructions associated with
3532      this FDE.  */
3533   fde->dw_fde_current_label = begin;
3534   if (!fde->dw_fde_switched_sections)
3535     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3536       output_cfi (cfi, fde, for_eh);
3537   else if (!second)
3538     {
3539       if (fde->dw_fde_switch_cfi)
3540         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3541           {
3542             output_cfi (cfi, fde, for_eh);
3543             if (cfi == fde->dw_fde_switch_cfi)
3544               break;
3545           }
3546     }
3547   else
3548     {
3549       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3550
3551       if (fde->dw_fde_switch_cfi)
3552         {
3553           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3554           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3555           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3556           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3557         }
3558       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3559         output_cfi (cfi, fde, for_eh);
3560     }
3561
3562   /* If we are to emit a ref/link from function bodies to their frame tables,
3563      do it now.  This is typically performed to make sure that tables
3564      associated with functions are dragged with them and not discarded in
3565      garbage collecting links. We need to do this on a per function basis to
3566      cope with -ffunction-sections.  */
3567
3568 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3569   /* Switch to the function section, emit the ref to the tables, and
3570      switch *back* into the table section.  */
3571   switch_to_section (function_section (fde->decl));
3572   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3573   switch_to_frame_table_section (for_eh, true);
3574 #endif
3575
3576   /* Pad the FDE out to an address sized boundary.  */
3577   ASM_OUTPUT_ALIGN (asm_out_file,
3578                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3579   ASM_OUTPUT_LABEL (asm_out_file, l2);
3580
3581   j += 2;
3582 }
3583
3584 /* Output the call frame information used to record information
3585    that relates to calculating the frame pointer, and records the
3586    location of saved registers.  */
3587
3588 static void
3589 output_call_frame_info (int for_eh)
3590 {
3591   unsigned int i;
3592   dw_fde_ref fde;
3593   dw_cfi_ref cfi;
3594   char l1[20], l2[20], section_start_label[20];
3595   bool any_lsda_needed = false;
3596   char augmentation[6];
3597   int augmentation_size;
3598   int fde_encoding = DW_EH_PE_absptr;
3599   int per_encoding = DW_EH_PE_absptr;
3600   int lsda_encoding = DW_EH_PE_absptr;
3601   int return_reg;
3602   int dw_cie_version;
3603
3604   /* Don't emit a CIE if there won't be any FDEs.  */
3605   if (fde_table_in_use == 0)
3606     return;
3607
3608   /* Nothing to do if the assembler's doing it all.  */
3609   if (dwarf2out_do_cfi_asm ())
3610     return;
3611
3612   /* If we make FDEs linkonce, we may have to emit an empty label for
3613      an FDE that wouldn't otherwise be emitted.  We want to avoid
3614      having an FDE kept around when the function it refers to is
3615      discarded.  Example where this matters: a primary function
3616      template in C++ requires EH information, but an explicit
3617      specialization doesn't.  */
3618   if (TARGET_USES_WEAK_UNWIND_INFO
3619       && ! flag_asynchronous_unwind_tables
3620       && flag_exceptions
3621       && for_eh)
3622     for (i = 0; i < fde_table_in_use; i++)
3623       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3624           && !fde_table[i].uses_eh_lsda
3625           && ! DECL_WEAK (fde_table[i].decl))
3626         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3627                                       for_eh, /* empty */ 1);
3628
3629   /* If we don't have any functions we'll want to unwind out of, don't
3630      emit any EH unwind information.  Note that if exceptions aren't
3631      enabled, we won't have collected nothrow information, and if we
3632      asked for asynchronous tables, we always want this info.  */
3633   if (for_eh)
3634     {
3635       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3636
3637       for (i = 0; i < fde_table_in_use; i++)
3638         if (fde_table[i].uses_eh_lsda)
3639           any_eh_needed = any_lsda_needed = true;
3640         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3641           any_eh_needed = true;
3642         else if (! fde_table[i].nothrow
3643                  && ! fde_table[i].all_throwers_are_sibcalls)
3644           any_eh_needed = true;
3645
3646       if (! any_eh_needed)
3647         return;
3648     }
3649
3650   /* We're going to be generating comments, so turn on app.  */
3651   if (flag_debug_asm)
3652     app_enable ();
3653
3654   /* Switch to the proper frame section, first time.  */
3655   switch_to_frame_table_section (for_eh, false);
3656
3657   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3658   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3659
3660   /* Output the CIE.  */
3661   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3662   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3663   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3664     dw2_asm_output_data (4, 0xffffffff,
3665       "Initial length escape value indicating 64-bit DWARF extension");
3666   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3667                         "Length of Common Information Entry");
3668   ASM_OUTPUT_LABEL (asm_out_file, l1);
3669
3670   /* Now that the CIE pointer is PC-relative for EH,
3671      use 0 to identify the CIE.  */
3672   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3673                        (for_eh ? 0 : DWARF_CIE_ID),
3674                        "CIE Identifier Tag");
3675
3676   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3677      use CIE version 1, unless that would produce incorrect results
3678      due to overflowing the return register column.  */
3679   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3680   dw_cie_version = 1;
3681   if (return_reg >= 256 || dwarf_version > 2)
3682     dw_cie_version = 3;
3683   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3684
3685   augmentation[0] = 0;
3686   augmentation_size = 0;
3687   if (for_eh)
3688     {
3689       char *p;
3690
3691       /* Augmentation:
3692          z      Indicates that a uleb128 is present to size the
3693                 augmentation section.
3694          L      Indicates the encoding (and thus presence) of
3695                 an LSDA pointer in the FDE augmentation.
3696          R      Indicates a non-default pointer encoding for
3697                 FDE code pointers.
3698          P      Indicates the presence of an encoding + language
3699                 personality routine in the CIE augmentation.  */
3700
3701       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3702       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3703       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3704
3705       p = augmentation + 1;
3706       if (eh_personality_libfunc)
3707         {
3708           *p++ = 'P';
3709           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3710           assemble_external_libcall (eh_personality_libfunc);
3711         }
3712       if (any_lsda_needed)
3713         {
3714           *p++ = 'L';
3715           augmentation_size += 1;
3716         }
3717       if (fde_encoding != DW_EH_PE_absptr)
3718         {
3719           *p++ = 'R';
3720           augmentation_size += 1;
3721         }
3722       if (p > augmentation + 1)
3723         {
3724           augmentation[0] = 'z';
3725           *p = '\0';
3726         }
3727
3728       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3729       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
3730         {
3731           int offset = (  4             /* Length */
3732                         + 4             /* CIE Id */
3733                         + 1             /* CIE version */
3734                         + strlen (augmentation) + 1     /* Augmentation */
3735                         + size_of_uleb128 (1)           /* Code alignment */
3736                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3737                         + 1             /* RA column */
3738                         + 1             /* Augmentation size */
3739                         + 1             /* Personality encoding */ );
3740           int pad = -offset & (PTR_SIZE - 1);
3741
3742           augmentation_size += pad;
3743
3744           /* Augmentations should be small, so there's scarce need to
3745              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3746           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3747         }
3748     }
3749
3750   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3751   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3752   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3753                                "CIE Data Alignment Factor");
3754
3755   if (dw_cie_version == 1)
3756     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3757   else
3758     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3759
3760   if (augmentation[0])
3761     {
3762       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3763       if (eh_personality_libfunc)
3764         {
3765           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3766                                eh_data_format_name (per_encoding));
3767           dw2_asm_output_encoded_addr_rtx (per_encoding,
3768                                            eh_personality_libfunc,
3769                                            true, NULL);
3770         }
3771
3772       if (any_lsda_needed)
3773         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3774                              eh_data_format_name (lsda_encoding));
3775
3776       if (fde_encoding != DW_EH_PE_absptr)
3777         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3778                              eh_data_format_name (fde_encoding));
3779     }
3780
3781   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3782     output_cfi (cfi, NULL, for_eh);
3783
3784   /* Pad the CIE out to an address sized boundary.  */
3785   ASM_OUTPUT_ALIGN (asm_out_file,
3786                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3787   ASM_OUTPUT_LABEL (asm_out_file, l2);
3788
3789   /* Loop through all of the FDE's.  */
3790   for (i = 0; i < fde_table_in_use; i++)
3791     {
3792       unsigned int k;
3793       fde = &fde_table[i];
3794
3795       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3796       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3797           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3798           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3799           && !fde->uses_eh_lsda)
3800         continue;
3801
3802       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3803         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3804                     augmentation, any_lsda_needed, lsda_encoding);
3805     }
3806
3807   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3808     dw2_asm_output_data (4, 0, "End of Table");
3809 #ifdef MIPS_DEBUGGING_INFO
3810   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3811      get a value of 0.  Putting .align 0 after the label fixes it.  */
3812   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3813 #endif
3814
3815   /* Turn off app to make assembly quicker.  */
3816   if (flag_debug_asm)
3817     app_disable ();
3818 }
3819
3820 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3821
3822 static void
3823 dwarf2out_do_cfi_startproc (bool second)
3824 {
3825   int enc;
3826   rtx ref;
3827
3828   fprintf (asm_out_file, "\t.cfi_startproc\n");
3829
3830   if (eh_personality_libfunc)
3831     {
3832       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3833       ref = eh_personality_libfunc;
3834
3835       /* ??? The GAS support isn't entirely consistent.  We have to
3836          handle indirect support ourselves, but PC-relative is done
3837          in the assembler.  Further, the assembler can't handle any
3838          of the weirder relocation types.  */
3839       if (enc & DW_EH_PE_indirect)
3840         ref = dw2_force_const_mem (ref, true);
3841
3842       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3843       output_addr_const (asm_out_file, ref);
3844       fputc ('\n', asm_out_file);
3845     }
3846
3847   if (crtl->uses_eh_lsda)
3848     {
3849       char lab[20];
3850
3851       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3852       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3853                                    current_function_funcdef_no);
3854       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3855       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3856
3857       if (enc & DW_EH_PE_indirect)
3858         ref = dw2_force_const_mem (ref, true);
3859
3860       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3861       output_addr_const (asm_out_file, ref);
3862       fputc ('\n', asm_out_file);
3863     }
3864 }
3865
3866 /* Output a marker (i.e. a label) for the beginning of a function, before
3867    the prologue.  */
3868
3869 void
3870 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3871                           const char *file ATTRIBUTE_UNUSED)
3872 {
3873   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3874   char * dup_label;
3875   dw_fde_ref fde;
3876   section *fnsec;
3877
3878   current_function_func_begin_label = NULL;
3879
3880 #ifdef TARGET_UNWIND_INFO
3881   /* ??? current_function_func_begin_label is also used by except.c
3882      for call-site information.  We must emit this label if it might
3883      be used.  */
3884   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3885       && ! dwarf2out_do_frame ())
3886     return;
3887 #else
3888   if (! dwarf2out_do_frame ())
3889     return;
3890 #endif
3891
3892   fnsec = function_section (current_function_decl);
3893   switch_to_section (fnsec);
3894   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3895                                current_function_funcdef_no);
3896   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3897                           current_function_funcdef_no);
3898   dup_label = xstrdup (label);
3899   current_function_func_begin_label = dup_label;
3900
3901 #ifdef TARGET_UNWIND_INFO
3902   /* We can elide the fde allocation if we're not emitting debug info.  */
3903   if (! dwarf2out_do_frame ())
3904     return;
3905 #endif
3906
3907   /* Expand the fde table if necessary.  */
3908   if (fde_table_in_use == fde_table_allocated)
3909     {
3910       fde_table_allocated += FDE_TABLE_INCREMENT;
3911       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3912       memset (fde_table + fde_table_in_use, 0,
3913               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3914     }
3915
3916   /* Record the FDE associated with this function.  */
3917   current_funcdef_fde = fde_table_in_use;
3918
3919   /* Add the new FDE at the end of the fde_table.  */
3920   fde = &fde_table[fde_table_in_use++];
3921   fde->decl = current_function_decl;
3922   fde->dw_fde_begin = dup_label;
3923   fde->dw_fde_current_label = dup_label;
3924   fde->dw_fde_hot_section_label = NULL;
3925   fde->dw_fde_hot_section_end_label = NULL;
3926   fde->dw_fde_unlikely_section_label = NULL;
3927   fde->dw_fde_unlikely_section_end_label = NULL;
3928   fde->dw_fde_switched_sections = 0;
3929   fde->dw_fde_switched_cold_to_hot = 0;
3930   fde->dw_fde_end = NULL;
3931   fde->dw_fde_cfi = NULL;
3932   fde->dw_fde_switch_cfi = NULL;
3933   fde->funcdef_number = current_function_funcdef_no;
3934   fde->nothrow = crtl->nothrow;
3935   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3936   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3937   fde->drap_reg = INVALID_REGNUM;
3938   fde->vdrap_reg = INVALID_REGNUM;
3939   if (flag_reorder_blocks_and_partition)
3940     {
3941       section *unlikelysec;
3942       if (first_function_block_is_cold)
3943         fde->in_std_section = 1;
3944       else
3945         fde->in_std_section
3946           = (fnsec == text_section
3947              || (cold_text_section && fnsec == cold_text_section));
3948       unlikelysec = unlikely_text_section ();
3949       fde->cold_in_std_section
3950         = (unlikelysec == text_section
3951            || (cold_text_section && unlikelysec == cold_text_section));
3952     }
3953   else
3954     {
3955       fde->in_std_section
3956         = (fnsec == text_section
3957            || (cold_text_section && fnsec == cold_text_section));
3958       fde->cold_in_std_section = 0;
3959     }
3960
3961   args_size = old_args_size = 0;
3962
3963   /* We only want to output line number information for the genuine dwarf2
3964      prologue case, not the eh frame case.  */
3965 #ifdef DWARF2_DEBUGGING_INFO
3966   if (file)
3967     dwarf2out_source_line (line, file, 0, true);
3968 #endif
3969
3970   if (dwarf2out_do_cfi_asm ())
3971     dwarf2out_do_cfi_startproc (false);
3972 }
3973
3974 /* Output a marker (i.e. a label) for the absolute end of the generated code
3975    for a function definition.  This gets called *after* the epilogue code has
3976    been generated.  */
3977
3978 void
3979 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
3980                         const char *file ATTRIBUTE_UNUSED)
3981 {
3982   dw_fde_ref fde;
3983   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3984
3985 #ifdef DWARF2_DEBUGGING_INFO
3986   last_var_location_insn = NULL_RTX;
3987 #endif
3988
3989   if (dwarf2out_do_cfi_asm ())
3990     fprintf (asm_out_file, "\t.cfi_endproc\n");
3991
3992   /* Output a label to mark the endpoint of the code generated for this
3993      function.  */
3994   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
3995                                current_function_funcdef_no);
3996   ASM_OUTPUT_LABEL (asm_out_file, label);
3997   fde = current_fde ();
3998   gcc_assert (fde != NULL);
3999   fde->dw_fde_end = xstrdup (label);
4000 }
4001
4002 void
4003 dwarf2out_frame_init (void)
4004 {
4005   /* Allocate the initial hunk of the fde_table.  */
4006   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4007   fde_table_allocated = FDE_TABLE_INCREMENT;
4008   fde_table_in_use = 0;
4009
4010   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4011      sake of lookup_cfa.  */
4012
4013   /* On entry, the Canonical Frame Address is at SP.  */
4014   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4015
4016 #ifdef DWARF2_UNWIND_INFO
4017   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4018     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4019 #endif
4020 }
4021
4022 void
4023 dwarf2out_frame_finish (void)
4024 {
4025   /* Output call frame information.  */
4026   if (DWARF2_FRAME_INFO)
4027     output_call_frame_info (0);
4028
4029 #ifndef TARGET_UNWIND_INFO
4030   /* Output another copy for the unwinder.  */
4031   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4032     output_call_frame_info (1);
4033 #endif
4034 }
4035
4036 /* Note that the current function section is being used for code.  */
4037
4038 static void
4039 dwarf2out_note_section_used (void)
4040 {
4041   section *sec = current_function_section ();
4042   if (sec == text_section)
4043     text_section_used = true;
4044   else if (sec == cold_text_section)
4045     cold_text_section_used = true;
4046 }
4047
4048 void
4049 dwarf2out_switch_text_section (void)
4050 {
4051   dw_fde_ref fde = current_fde ();
4052
4053   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4054
4055   fde->dw_fde_switched_sections = 1;
4056   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4057
4058   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4059   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4060   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4061   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4062   have_multiple_function_sections = true;
4063
4064   /* Reset the current label on switching text sections, so that we
4065      don't attempt to advance_loc4 between labels in different sections.  */
4066   fde->dw_fde_current_label = NULL;
4067
4068   /* There is no need to mark used sections when not debugging.  */
4069   if (cold_text_section != NULL)
4070     dwarf2out_note_section_used ();
4071
4072   if (dwarf2out_do_cfi_asm ())
4073     fprintf (asm_out_file, "\t.cfi_endproc\n");
4074
4075   /* Now do the real section switch.  */
4076   switch_to_section (current_function_section ());
4077
4078   if (dwarf2out_do_cfi_asm ())
4079     {
4080       dwarf2out_do_cfi_startproc (true);
4081       /* As this is a different FDE, insert all current CFI instructions
4082          again.  */
4083       output_cfis (fde->dw_fde_cfi, true, fde, true);
4084     }
4085   else
4086     {
4087       dw_cfi_ref cfi = fde->dw_fde_cfi;
4088
4089       cfi = fde->dw_fde_cfi;
4090       if (cfi)
4091         while (cfi->dw_cfi_next != NULL)
4092           cfi = cfi->dw_cfi_next;
4093       fde->dw_fde_switch_cfi = cfi;
4094     }
4095 }
4096 #endif
4097 \f
4098 /* And now, the subset of the debugging information support code necessary
4099    for emitting location expressions.  */
4100
4101 /* Data about a single source file.  */
4102 struct GTY(()) dwarf_file_data {
4103   const char * filename;
4104   int emitted_number;
4105 };
4106
4107 typedef struct dw_val_struct *dw_val_ref;
4108 typedef struct die_struct *dw_die_ref;
4109 typedef const struct die_struct *const_dw_die_ref;
4110 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4111 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4112
4113 typedef struct GTY(()) deferred_locations_struct
4114 {
4115   tree variable;
4116   dw_die_ref die;
4117 } deferred_locations;
4118
4119 DEF_VEC_O(deferred_locations);
4120 DEF_VEC_ALLOC_O(deferred_locations,gc);
4121
4122 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4123
4124 /* Each DIE may have a series of attribute/value pairs.  Values
4125    can take on several forms.  The forms that are used in this
4126    implementation are listed below.  */
4127
4128 enum dw_val_class
4129 {
4130   dw_val_class_addr,
4131   dw_val_class_offset,
4132   dw_val_class_loc,
4133   dw_val_class_loc_list,
4134   dw_val_class_range_list,
4135   dw_val_class_const,
4136   dw_val_class_unsigned_const,
4137   dw_val_class_long_long,
4138   dw_val_class_vec,
4139   dw_val_class_flag,
4140   dw_val_class_die_ref,
4141   dw_val_class_fde_ref,
4142   dw_val_class_lbl_id,
4143   dw_val_class_lineptr,
4144   dw_val_class_str,
4145   dw_val_class_macptr,
4146   dw_val_class_file
4147 };
4148
4149 /* Describe a floating point constant value, or a vector constant value.  */
4150
4151 typedef struct GTY(()) dw_vec_struct {
4152   unsigned char * GTY((length ("%h.length"))) array;
4153   unsigned length;
4154   unsigned elt_size;
4155 }
4156 dw_vec_const;
4157
4158 /* The dw_val_node describes an attribute's value, as it is
4159    represented internally.  */
4160
4161 typedef struct GTY(()) dw_val_struct {
4162   enum dw_val_class val_class;
4163   union dw_val_struct_union
4164     {
4165       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4166       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4167       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4168       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4169       HOST_WIDE_INT GTY ((default)) val_int;
4170       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4171       rtx GTY ((tag ("dw_val_class_long_long"))) val_long_long;
4172       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4173       struct dw_val_die_union
4174         {
4175           dw_die_ref die;
4176           int external;
4177         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4178       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4179       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4180       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4181       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4182       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4183     }
4184   GTY ((desc ("%1.val_class"))) v;
4185 }
4186 dw_val_node;
4187
4188 /* Locations in memory are described using a sequence of stack machine
4189    operations.  */
4190
4191 typedef struct GTY(()) dw_loc_descr_struct {
4192   dw_loc_descr_ref dw_loc_next;
4193   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4194   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4195      from DW_OP_addr with a dtp-relative symbol relocation.  */
4196   unsigned int dtprel : 1;
4197   int dw_loc_addr;
4198   dw_val_node dw_loc_oprnd1;
4199   dw_val_node dw_loc_oprnd2;
4200 }
4201 dw_loc_descr_node;
4202
4203 /* Location lists are ranges + location descriptions for that range,
4204    so you can track variables that are in different places over
4205    their entire life.  */
4206 typedef struct GTY(()) dw_loc_list_struct {
4207   dw_loc_list_ref dw_loc_next;
4208   const char *begin; /* Label for begin address of range */
4209   const char *end;  /* Label for end address of range */
4210   char *ll_symbol; /* Label for beginning of location list.
4211                       Only on head of list */
4212   const char *section; /* Section this loclist is relative to */
4213   dw_loc_descr_ref expr;
4214 } dw_loc_list_node;
4215
4216 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4217
4218 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4219
4220 /* Convert a DWARF stack opcode into its string name.  */
4221
4222 static const char *
4223 dwarf_stack_op_name (unsigned int op)
4224 {
4225   switch (op)
4226     {
4227     case DW_OP_addr:
4228       return "DW_OP_addr";
4229     case DW_OP_deref:
4230       return "DW_OP_deref";
4231     case DW_OP_const1u:
4232       return "DW_OP_const1u";
4233     case DW_OP_const1s:
4234       return "DW_OP_const1s";
4235     case DW_OP_const2u:
4236       return "DW_OP_const2u";
4237     case DW_OP_const2s:
4238       return "DW_OP_const2s";
4239     case DW_OP_const4u:
4240       return "DW_OP_const4u";
4241     case DW_OP_const4s:
4242       return "DW_OP_const4s";
4243     case DW_OP_const8u:
4244       return "DW_OP_const8u";
4245     case DW_OP_const8s:
4246       return "DW_OP_const8s";
4247     case DW_OP_constu:
4248       return "DW_OP_constu";
4249     case DW_OP_consts:
4250       return "DW_OP_consts";
4251     case DW_OP_dup:
4252       return "DW_OP_dup";
4253     case DW_OP_drop:
4254       return "DW_OP_drop";
4255     case DW_OP_over:
4256       return "DW_OP_over";
4257     case DW_OP_pick:
4258       return "DW_OP_pick";
4259     case DW_OP_swap:
4260       return "DW_OP_swap";
4261     case DW_OP_rot:
4262       return "DW_OP_rot";
4263     case DW_OP_xderef:
4264       return "DW_OP_xderef";
4265     case DW_OP_abs:
4266       return "DW_OP_abs";
4267     case DW_OP_and:
4268       return "DW_OP_and";
4269     case DW_OP_div:
4270       return "DW_OP_div";
4271     case DW_OP_minus:
4272       return "DW_OP_minus";
4273     case DW_OP_mod:
4274       return "DW_OP_mod";
4275     case DW_OP_mul:
4276       return "DW_OP_mul";
4277     case DW_OP_neg:
4278       return "DW_OP_neg";
4279     case DW_OP_not:
4280       return "DW_OP_not";
4281     case DW_OP_or:
4282       return "DW_OP_or";
4283     case DW_OP_plus:
4284       return "DW_OP_plus";
4285     case DW_OP_plus_uconst:
4286       return "DW_OP_plus_uconst";
4287     case DW_OP_shl:
4288       return "DW_OP_shl";
4289     case DW_OP_shr:
4290       return "DW_OP_shr";
4291     case DW_OP_shra:
4292       return "DW_OP_shra";
4293     case DW_OP_xor:
4294       return "DW_OP_xor";
4295     case DW_OP_bra:
4296       return "DW_OP_bra";
4297     case DW_OP_eq:
4298       return "DW_OP_eq";
4299     case DW_OP_ge:
4300       return "DW_OP_ge";
4301     case DW_OP_gt:
4302       return "DW_OP_gt";
4303     case DW_OP_le:
4304       return "DW_OP_le";
4305     case DW_OP_lt:
4306       return "DW_OP_lt";
4307     case DW_OP_ne:
4308       return "DW_OP_ne";
4309     case DW_OP_skip:
4310       return "DW_OP_skip";
4311     case DW_OP_lit0:
4312       return "DW_OP_lit0";
4313     case DW_OP_lit1:
4314       return "DW_OP_lit1";
4315     case DW_OP_lit2:
4316       return "DW_OP_lit2";
4317     case DW_OP_lit3:
4318       return "DW_OP_lit3";
4319     case DW_OP_lit4:
4320       return "DW_OP_lit4";
4321     case DW_OP_lit5:
4322       return "DW_OP_lit5";
4323     case DW_OP_lit6:
4324       return "DW_OP_lit6";
4325     case DW_OP_lit7:
4326       return "DW_OP_lit7";
4327     case DW_OP_lit8:
4328       return "DW_OP_lit8";
4329     case DW_OP_lit9:
4330       return "DW_OP_lit9";
4331     case DW_OP_lit10:
4332       return "DW_OP_lit10";
4333     case DW_OP_lit11:
4334       return "DW_OP_lit11";
4335     case DW_OP_lit12:
4336       return "DW_OP_lit12";
4337     case DW_OP_lit13:
4338       return "DW_OP_lit13";
4339     case DW_OP_lit14:
4340       return "DW_OP_lit14";
4341     case DW_OP_lit15:
4342       return "DW_OP_lit15";
4343     case DW_OP_lit16:
4344       return "DW_OP_lit16";
4345     case DW_OP_lit17:
4346       return "DW_OP_lit17";
4347     case DW_OP_lit18:
4348       return "DW_OP_lit18";
4349     case DW_OP_lit19:
4350       return "DW_OP_lit19";
4351     case DW_OP_lit20:
4352       return "DW_OP_lit20";
4353     case DW_OP_lit21:
4354       return "DW_OP_lit21";
4355     case DW_OP_lit22:
4356       return "DW_OP_lit22";
4357     case DW_OP_lit23:
4358       return "DW_OP_lit23";
4359     case DW_OP_lit24:
4360       return "DW_OP_lit24";
4361     case DW_OP_lit25:
4362       return "DW_OP_lit25";
4363     case DW_OP_lit26:
4364       return "DW_OP_lit26";
4365     case DW_OP_lit27:
4366       return "DW_OP_lit27";
4367     case DW_OP_lit28:
4368       return "DW_OP_lit28";
4369     case DW_OP_lit29:
4370       return "DW_OP_lit29";
4371     case DW_OP_lit30:
4372       return "DW_OP_lit30";
4373     case DW_OP_lit31:
4374       return "DW_OP_lit31";
4375     case DW_OP_reg0:
4376       return "DW_OP_reg0";
4377     case DW_OP_reg1:
4378       return "DW_OP_reg1";
4379     case DW_OP_reg2:
4380       return "DW_OP_reg2";
4381     case DW_OP_reg3:
4382       return "DW_OP_reg3";
4383     case DW_OP_reg4:
4384       return "DW_OP_reg4";
4385     case DW_OP_reg5:
4386       return "DW_OP_reg5";
4387     case DW_OP_reg6:
4388       return "DW_OP_reg6";
4389     case DW_OP_reg7:
4390       return "DW_OP_reg7";
4391     case DW_OP_reg8:
4392       return "DW_OP_reg8";
4393     case DW_OP_reg9:
4394       return "DW_OP_reg9";
4395     case DW_OP_reg10:
4396       return "DW_OP_reg10";
4397     case DW_OP_reg11:
4398       return "DW_OP_reg11";
4399     case DW_OP_reg12:
4400       return "DW_OP_reg12";
4401     case DW_OP_reg13:
4402       return "DW_OP_reg13";
4403     case DW_OP_reg14:
4404       return "DW_OP_reg14";
4405     case DW_OP_reg15:
4406       return "DW_OP_reg15";
4407     case DW_OP_reg16:
4408       return "DW_OP_reg16";
4409     case DW_OP_reg17:
4410       return "DW_OP_reg17";
4411     case DW_OP_reg18:
4412       return "DW_OP_reg18";
4413     case DW_OP_reg19:
4414       return "DW_OP_reg19";
4415     case DW_OP_reg20:
4416       return "DW_OP_reg20";
4417     case DW_OP_reg21:
4418       return "DW_OP_reg21";
4419     case DW_OP_reg22:
4420       return "DW_OP_reg22";
4421     case DW_OP_reg23:
4422       return "DW_OP_reg23";
4423     case DW_OP_reg24:
4424       return "DW_OP_reg24";
4425     case DW_OP_reg25:
4426       return "DW_OP_reg25";
4427     case DW_OP_reg26:
4428       return "DW_OP_reg26";
4429     case DW_OP_reg27:
4430       return "DW_OP_reg27";
4431     case DW_OP_reg28:
4432       return "DW_OP_reg28";
4433     case DW_OP_reg29:
4434       return "DW_OP_reg29";
4435     case DW_OP_reg30:
4436       return "DW_OP_reg30";
4437     case DW_OP_reg31:
4438       return "DW_OP_reg31";
4439     case DW_OP_breg0:
4440       return "DW_OP_breg0";
4441     case DW_OP_breg1:
4442       return "DW_OP_breg1";
4443     case DW_OP_breg2:
4444       return "DW_OP_breg2";
4445     case DW_OP_breg3:
4446       return "DW_OP_breg3";
4447     case DW_OP_breg4:
4448       return "DW_OP_breg4";
4449     case DW_OP_breg5:
4450       return "DW_OP_breg5";
4451     case DW_OP_breg6:
4452       return "DW_OP_breg6";
4453     case DW_OP_breg7:
4454       return "DW_OP_breg7";
4455     case DW_OP_breg8:
4456       return "DW_OP_breg8";
4457     case DW_OP_breg9:
4458       return "DW_OP_breg9";
4459     case DW_OP_breg10:
4460       return "DW_OP_breg10";
4461     case DW_OP_breg11:
4462       return "DW_OP_breg11";
4463     case DW_OP_breg12:
4464       return "DW_OP_breg12";
4465     case DW_OP_breg13:
4466       return "DW_OP_breg13";
4467     case DW_OP_breg14:
4468       return "DW_OP_breg14";
4469     case DW_OP_breg15:
4470       return "DW_OP_breg15";
4471     case DW_OP_breg16:
4472       return "DW_OP_breg16";
4473     case DW_OP_breg17:
4474       return "DW_OP_breg17";
4475     case DW_OP_breg18:
4476       return "DW_OP_breg18";
4477     case DW_OP_breg19:
4478       return "DW_OP_breg19";
4479     case DW_OP_breg20:
4480       return "DW_OP_breg20";
4481     case DW_OP_breg21:
4482       return "DW_OP_breg21";
4483     case DW_OP_breg22:
4484       return "DW_OP_breg22";
4485     case DW_OP_breg23:
4486       return "DW_OP_breg23";
4487     case DW_OP_breg24:
4488       return "DW_OP_breg24";
4489     case DW_OP_breg25:
4490       return "DW_OP_breg25";
4491     case DW_OP_breg26:
4492       return "DW_OP_breg26";
4493     case DW_OP_breg27:
4494       return "DW_OP_breg27";
4495     case DW_OP_breg28:
4496       return "DW_OP_breg28";
4497     case DW_OP_breg29:
4498       return "DW_OP_breg29";
4499     case DW_OP_breg30:
4500       return "DW_OP_breg30";
4501     case DW_OP_breg31:
4502       return "DW_OP_breg31";
4503     case DW_OP_regx:
4504       return "DW_OP_regx";
4505     case DW_OP_fbreg:
4506       return "DW_OP_fbreg";
4507     case DW_OP_bregx:
4508       return "DW_OP_bregx";
4509     case DW_OP_piece:
4510       return "DW_OP_piece";
4511     case DW_OP_deref_size:
4512       return "DW_OP_deref_size";
4513     case DW_OP_xderef_size:
4514       return "DW_OP_xderef_size";
4515     case DW_OP_nop:
4516       return "DW_OP_nop";
4517
4518     case DW_OP_push_object_address:
4519       return "DW_OP_push_object_address";
4520     case DW_OP_call2:
4521       return "DW_OP_call2";
4522     case DW_OP_call4:
4523       return "DW_OP_call4";
4524     case DW_OP_call_ref:
4525       return "DW_OP_call_ref";
4526     case DW_OP_implicit_value:
4527       return "DW_OP_implicit_value";
4528     case DW_OP_stack_value:
4529       return "DW_OP_stack_value";
4530     case DW_OP_form_tls_address:
4531       return "DW_OP_form_tls_address";
4532     case DW_OP_call_frame_cfa:
4533       return "DW_OP_call_frame_cfa";
4534     case DW_OP_bit_piece:
4535       return "DW_OP_bit_piece";
4536
4537     case DW_OP_GNU_push_tls_address:
4538       return "DW_OP_GNU_push_tls_address";
4539     case DW_OP_GNU_uninit:
4540       return "DW_OP_GNU_uninit";
4541     case DW_OP_GNU_encoded_addr:
4542       return "DW_OP_GNU_encoded_addr";
4543
4544     default:
4545       return "OP_<unknown>";
4546     }
4547 }
4548
4549 /* Return a pointer to a newly allocated location description.  Location
4550    descriptions are simple expression terms that can be strung
4551    together to form more complicated location (address) descriptions.  */
4552
4553 static inline dw_loc_descr_ref
4554 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4555                unsigned HOST_WIDE_INT oprnd2)
4556 {
4557   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4558
4559   descr->dw_loc_opc = op;
4560   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4561   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4562   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4563   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4564
4565   return descr;
4566 }
4567
4568 /* Return a pointer to a newly allocated location description for
4569    REG and OFFSET.  */
4570
4571 static inline dw_loc_descr_ref
4572 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4573 {
4574   if (reg <= 31)
4575     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4576                           offset, 0);
4577   else
4578     return new_loc_descr (DW_OP_bregx, reg, offset);
4579 }
4580
4581 /* Add a location description term to a location description expression.  */
4582
4583 static inline void
4584 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4585 {
4586   dw_loc_descr_ref *d;
4587
4588   /* Find the end of the chain.  */
4589   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4590     ;
4591
4592   *d = descr;
4593 }
4594
4595 /* Add a constant OFFSET to a location expression.  */
4596
4597 static void
4598 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4599 {
4600   dw_loc_descr_ref loc;
4601   HOST_WIDE_INT *p;
4602
4603   gcc_assert (*list_head != NULL);
4604
4605   if (!offset)
4606     return;
4607
4608   /* Find the end of the chain.  */
4609   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4610     ;
4611
4612   p = NULL;
4613   if (loc->dw_loc_opc == DW_OP_fbreg
4614       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4615     p = &loc->dw_loc_oprnd1.v.val_int;
4616   else if (loc->dw_loc_opc == DW_OP_bregx)
4617     p = &loc->dw_loc_oprnd2.v.val_int;
4618
4619   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4620      offset.  Don't optimize if an signed integer overflow would happen.  */
4621   if (p != NULL
4622       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4623           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4624     *p += offset;
4625
4626   else if (offset > 0)
4627     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4628
4629   else
4630     {
4631       loc->dw_loc_next = int_loc_descriptor (offset);
4632       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4633     }
4634 }
4635
4636 /* Return the size of a location descriptor.  */
4637
4638 static unsigned long
4639 size_of_loc_descr (dw_loc_descr_ref loc)
4640 {
4641   unsigned long size = 1;
4642
4643   switch (loc->dw_loc_opc)
4644     {
4645     case DW_OP_addr:
4646       size += DWARF2_ADDR_SIZE;
4647       break;
4648     case DW_OP_const1u:
4649     case DW_OP_const1s:
4650       size += 1;
4651       break;
4652     case DW_OP_const2u:
4653     case DW_OP_const2s:
4654       size += 2;
4655       break;
4656     case DW_OP_const4u:
4657     case DW_OP_const4s:
4658       size += 4;
4659       break;
4660     case DW_OP_const8u:
4661     case DW_OP_const8s:
4662       size += 8;
4663       break;
4664     case DW_OP_constu:
4665       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4666       break;
4667     case DW_OP_consts:
4668       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4669       break;
4670     case DW_OP_pick:
4671       size += 1;
4672       break;
4673     case DW_OP_plus_uconst:
4674       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4675       break;
4676     case DW_OP_skip:
4677     case DW_OP_bra:
4678       size += 2;
4679       break;
4680     case DW_OP_breg0:
4681     case DW_OP_breg1:
4682     case DW_OP_breg2:
4683     case DW_OP_breg3:
4684     case DW_OP_breg4:
4685     case DW_OP_breg5:
4686     case DW_OP_breg6:
4687     case DW_OP_breg7:
4688     case DW_OP_breg8:
4689     case DW_OP_breg9:
4690     case DW_OP_breg10:
4691     case DW_OP_breg11:
4692     case DW_OP_breg12:
4693     case DW_OP_breg13:
4694     case DW_OP_breg14:
4695     case DW_OP_breg15:
4696     case DW_OP_breg16:
4697     case DW_OP_breg17:
4698     case DW_OP_breg18:
4699     case DW_OP_breg19:
4700     case DW_OP_breg20:
4701     case DW_OP_breg21:
4702     case DW_OP_breg22:
4703     case DW_OP_breg23:
4704     case DW_OP_breg24:
4705     case DW_OP_breg25:
4706     case DW_OP_breg26:
4707     case DW_OP_breg27:
4708     case DW_OP_breg28:
4709     case DW_OP_breg29:
4710     case DW_OP_breg30:
4711     case DW_OP_breg31:
4712       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4713       break;
4714     case DW_OP_regx:
4715       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4716       break;
4717     case DW_OP_fbreg:
4718       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4719       break;
4720     case DW_OP_bregx:
4721       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4722       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4723       break;
4724     case DW_OP_piece:
4725       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4726       break;
4727     case DW_OP_deref_size:
4728     case DW_OP_xderef_size:
4729       size += 1;
4730       break;
4731     case DW_OP_call2:
4732       size += 2;
4733       break;
4734     case DW_OP_call4:
4735       size += 4;
4736       break;
4737     case DW_OP_call_ref:
4738       size += DWARF2_ADDR_SIZE;
4739       break;
4740     case DW_OP_implicit_value:
4741       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4742               + loc->dw_loc_oprnd1.v.val_unsigned;
4743       break;
4744     default:
4745       break;
4746     }
4747
4748   return size;
4749 }
4750
4751 /* Return the size of a series of location descriptors.  */
4752
4753 static unsigned long
4754 size_of_locs (dw_loc_descr_ref loc)
4755 {
4756   dw_loc_descr_ref l;
4757   unsigned long size;
4758
4759   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4760      field, to avoid writing to a PCH file.  */
4761   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4762     {
4763       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4764         break;
4765       size += size_of_loc_descr (l);
4766     }
4767   if (! l)
4768     return size;
4769
4770   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4771     {
4772       l->dw_loc_addr = size;
4773       size += size_of_loc_descr (l);
4774     }
4775
4776   return size;
4777 }
4778
4779 #ifdef DWARF2_DEBUGGING_INFO
4780 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4781 #endif
4782
4783 /* Output location description stack opcode's operands (if any).  */
4784
4785 static void
4786 output_loc_operands (dw_loc_descr_ref loc)
4787 {
4788   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4789   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4790
4791   switch (loc->dw_loc_opc)
4792     {
4793 #ifdef DWARF2_DEBUGGING_INFO
4794     case DW_OP_const2u:
4795     case DW_OP_const2s:
4796       dw2_asm_output_data (2, val1->v.val_int, NULL);
4797       break;
4798     case DW_OP_const4u:
4799     case DW_OP_const4s:
4800       dw2_asm_output_data (4, val1->v.val_int, NULL);
4801       break;
4802     case DW_OP_const8u:
4803     case DW_OP_const8s:
4804       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4805       dw2_asm_output_data (8, val1->v.val_int, NULL);
4806       break;
4807     case DW_OP_skip:
4808     case DW_OP_bra:
4809       {
4810         int offset;
4811
4812         gcc_assert (val1->val_class == dw_val_class_loc);
4813         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4814
4815         dw2_asm_output_data (2, offset, NULL);
4816       }
4817       break;
4818     case DW_OP_implicit_value:
4819       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4820       switch (val2->val_class)
4821         {
4822         case dw_val_class_const:
4823           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4824           break;
4825         case dw_val_class_vec:
4826           {
4827             unsigned int elt_size = val2->v.val_vec.elt_size;
4828             unsigned int len = val2->v.val_vec.length;
4829             unsigned int i;
4830             unsigned char *p;
4831
4832             if (elt_size > sizeof (HOST_WIDE_INT))
4833               {
4834                 elt_size /= 2;
4835                 len *= 2;
4836               }
4837             for (i = 0, p = val2->v.val_vec.array;
4838                  i < len;
4839                  i++, p += elt_size)
4840               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4841                                    "fp or vector constant word %u", i);
4842           }
4843           break;
4844         case dw_val_class_long_long:
4845           {
4846             unsigned HOST_WIDE_INT first, second;
4847
4848             if (WORDS_BIG_ENDIAN)
4849               {
4850                 first = CONST_DOUBLE_HIGH (val2->v.val_long_long);
4851                 second = CONST_DOUBLE_LOW (val2->v.val_long_long);
4852               }
4853             else
4854               {
4855                 first = CONST_DOUBLE_LOW (val2->v.val_long_long);
4856                 second = CONST_DOUBLE_HIGH (val2->v.val_long_long);
4857               }
4858             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4859                                  first, "long long constant");
4860             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4861                                  second, NULL);
4862           }
4863           break;
4864         case dw_val_class_addr:
4865           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4866           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4867           break;
4868         default:
4869           gcc_unreachable ();
4870         }
4871       break;
4872 #else
4873     case DW_OP_const2u:
4874     case DW_OP_const2s:
4875     case DW_OP_const4u:
4876     case DW_OP_const4s:
4877     case DW_OP_const8u:
4878     case DW_OP_const8s:
4879     case DW_OP_skip:
4880     case DW_OP_bra:
4881     case DW_OP_implicit_value:
4882       /* We currently don't make any attempt to make sure these are
4883          aligned properly like we do for the main unwind info, so
4884          don't support emitting things larger than a byte if we're
4885          only doing unwinding.  */
4886       gcc_unreachable ();
4887 #endif
4888     case DW_OP_const1u:
4889     case DW_OP_const1s:
4890       dw2_asm_output_data (1, val1->v.val_int, NULL);
4891       break;
4892     case DW_OP_constu:
4893       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4894       break;
4895     case DW_OP_consts:
4896       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4897       break;
4898     case DW_OP_pick:
4899       dw2_asm_output_data (1, val1->v.val_int, NULL);
4900       break;
4901     case DW_OP_plus_uconst:
4902       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4903       break;
4904     case DW_OP_breg0:
4905     case DW_OP_breg1:
4906     case DW_OP_breg2:
4907     case DW_OP_breg3:
4908     case DW_OP_breg4:
4909     case DW_OP_breg5:
4910     case DW_OP_breg6:
4911     case DW_OP_breg7:
4912     case DW_OP_breg8:
4913     case DW_OP_breg9:
4914     case DW_OP_breg10:
4915     case DW_OP_breg11:
4916     case DW_OP_breg12:
4917     case DW_OP_breg13:
4918     case DW_OP_breg14:
4919     case DW_OP_breg15:
4920     case DW_OP_breg16:
4921     case DW_OP_breg17:
4922     case DW_OP_breg18:
4923     case DW_OP_breg19:
4924     case DW_OP_breg20:
4925     case DW_OP_breg21:
4926     case DW_OP_breg22:
4927     case DW_OP_breg23:
4928     case DW_OP_breg24:
4929     case DW_OP_breg25:
4930     case DW_OP_breg26:
4931     case DW_OP_breg27:
4932     case DW_OP_breg28:
4933     case DW_OP_breg29:
4934     case DW_OP_breg30:
4935     case DW_OP_breg31:
4936       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4937       break;
4938     case DW_OP_regx:
4939       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4940       break;
4941     case DW_OP_fbreg:
4942       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4943       break;
4944     case DW_OP_bregx:
4945       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4946       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4947       break;
4948     case DW_OP_piece:
4949       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4950       break;
4951     case DW_OP_deref_size:
4952     case DW_OP_xderef_size:
4953       dw2_asm_output_data (1, val1->v.val_int, NULL);
4954       break;
4955
4956     case DW_OP_addr:
4957       if (loc->dtprel)
4958         {
4959           if (targetm.asm_out.output_dwarf_dtprel)
4960             {
4961               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4962                                                    DWARF2_ADDR_SIZE,
4963                                                    val1->v.val_addr);
4964               fputc ('\n', asm_out_file);
4965             }
4966           else
4967             gcc_unreachable ();
4968         }
4969       else
4970         {
4971 #ifdef DWARF2_DEBUGGING_INFO
4972           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
4973 #else
4974           gcc_unreachable ();
4975 #endif
4976         }
4977       break;
4978
4979     default:
4980       /* Other codes have no operands.  */
4981       break;
4982     }
4983 }
4984
4985 /* Output a sequence of location operations.  */
4986
4987 static void
4988 output_loc_sequence (dw_loc_descr_ref loc)
4989 {
4990   for (; loc != NULL; loc = loc->dw_loc_next)
4991     {
4992       /* Output the opcode.  */
4993       dw2_asm_output_data (1, loc->dw_loc_opc,
4994                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
4995
4996       /* Output the operand(s) (if any).  */
4997       output_loc_operands (loc);
4998     }
4999 }
5000
5001 /* Output location description stack opcode's operands (if any).
5002    The output is single bytes on a line, suitable for .cfi_escape.  */
5003
5004 static void
5005 output_loc_operands_raw (dw_loc_descr_ref loc)
5006 {
5007   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5008   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5009
5010   switch (loc->dw_loc_opc)
5011     {
5012     case DW_OP_addr:
5013     case DW_OP_implicit_value:
5014       /* We cannot output addresses in .cfi_escape, only bytes.  */
5015       gcc_unreachable ();
5016
5017     case DW_OP_const1u:
5018     case DW_OP_const1s:
5019     case DW_OP_pick:
5020     case DW_OP_deref_size:
5021     case DW_OP_xderef_size:
5022       fputc (',', asm_out_file);
5023       dw2_asm_output_data_raw (1, val1->v.val_int);
5024       break;
5025
5026     case DW_OP_const2u:
5027     case DW_OP_const2s:
5028       fputc (',', asm_out_file);
5029       dw2_asm_output_data_raw (2, val1->v.val_int);
5030       break;
5031
5032     case DW_OP_const4u:
5033     case DW_OP_const4s:
5034       fputc (',', asm_out_file);
5035       dw2_asm_output_data_raw (4, val1->v.val_int);
5036       break;
5037
5038     case DW_OP_const8u:
5039     case DW_OP_const8s:
5040       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5041       fputc (',', asm_out_file);
5042       dw2_asm_output_data_raw (8, val1->v.val_int);
5043       break;
5044
5045     case DW_OP_skip:
5046     case DW_OP_bra:
5047       {
5048         int offset;
5049
5050         gcc_assert (val1->val_class == dw_val_class_loc);
5051         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5052
5053         fputc (',', asm_out_file);
5054         dw2_asm_output_data_raw (2, offset);
5055       }
5056       break;
5057
5058     case DW_OP_constu:
5059     case DW_OP_plus_uconst:
5060     case DW_OP_regx:
5061     case DW_OP_piece:
5062       fputc (',', asm_out_file);
5063       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5064       break;
5065
5066     case DW_OP_consts:
5067     case DW_OP_breg0:
5068     case DW_OP_breg1:
5069     case DW_OP_breg2:
5070     case DW_OP_breg3:
5071     case DW_OP_breg4:
5072     case DW_OP_breg5:
5073     case DW_OP_breg6:
5074     case DW_OP_breg7:
5075     case DW_OP_breg8:
5076     case DW_OP_breg9:
5077     case DW_OP_breg10:
5078     case DW_OP_breg11:
5079     case DW_OP_breg12:
5080     case DW_OP_breg13:
5081     case DW_OP_breg14:
5082     case DW_OP_breg15:
5083     case DW_OP_breg16:
5084     case DW_OP_breg17:
5085     case DW_OP_breg18:
5086     case DW_OP_breg19:
5087     case DW_OP_breg20:
5088     case DW_OP_breg21:
5089     case DW_OP_breg22:
5090     case DW_OP_breg23:
5091     case DW_OP_breg24:
5092     case DW_OP_breg25:
5093     case DW_OP_breg26:
5094     case DW_OP_breg27:
5095     case DW_OP_breg28:
5096     case DW_OP_breg29:
5097     case DW_OP_breg30:
5098     case DW_OP_breg31:
5099     case DW_OP_fbreg:
5100       fputc (',', asm_out_file);
5101       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5102       break;
5103
5104     case DW_OP_bregx:
5105       fputc (',', asm_out_file);
5106       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5107       fputc (',', asm_out_file);
5108       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5109       break;
5110
5111     default:
5112       /* Other codes have no operands.  */
5113       break;
5114     }
5115 }
5116
5117 static void
5118 output_loc_sequence_raw (dw_loc_descr_ref loc)
5119 {
5120   while (1)
5121     {
5122       /* Output the opcode.  */
5123       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5124       output_loc_operands_raw (loc);
5125
5126       if (!loc->dw_loc_next)
5127         break;
5128       loc = loc->dw_loc_next;
5129
5130       fputc (',', asm_out_file);
5131     }
5132 }
5133
5134 /* This routine will generate the correct assembly data for a location
5135    description based on a cfi entry with a complex address.  */
5136
5137 static void
5138 output_cfa_loc (dw_cfi_ref cfi)
5139 {
5140   dw_loc_descr_ref loc;
5141   unsigned long size;
5142
5143   if (cfi->dw_cfi_opc == DW_CFA_expression)
5144     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5145
5146   /* Output the size of the block.  */
5147   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5148   size = size_of_locs (loc);
5149   dw2_asm_output_data_uleb128 (size, NULL);
5150
5151   /* Now output the operations themselves.  */
5152   output_loc_sequence (loc);
5153 }
5154
5155 /* Similar, but used for .cfi_escape.  */
5156
5157 static void
5158 output_cfa_loc_raw (dw_cfi_ref cfi)
5159 {
5160   dw_loc_descr_ref loc;
5161   unsigned long size;
5162
5163   if (cfi->dw_cfi_opc == DW_CFA_expression)
5164     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5165
5166   /* Output the size of the block.  */
5167   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5168   size = size_of_locs (loc);
5169   dw2_asm_output_data_uleb128_raw (size);
5170   fputc (',', asm_out_file);
5171
5172   /* Now output the operations themselves.  */
5173   output_loc_sequence_raw (loc);
5174 }
5175
5176 /* This function builds a dwarf location descriptor sequence from a
5177    dw_cfa_location, adding the given OFFSET to the result of the
5178    expression.  */
5179
5180 static struct dw_loc_descr_struct *
5181 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5182 {
5183   struct dw_loc_descr_struct *head, *tmp;
5184
5185   offset += cfa->offset;
5186
5187   if (cfa->indirect)
5188     {
5189       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5190       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5191       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5192       add_loc_descr (&head, tmp);
5193       if (offset != 0)
5194         {
5195           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5196           add_loc_descr (&head, tmp);
5197         }
5198     }
5199   else
5200     head = new_reg_loc_descr (cfa->reg, offset);
5201
5202   return head;
5203 }
5204
5205 /* This function builds a dwarf location descriptor sequence for
5206    the address at OFFSET from the CFA when stack is aligned to
5207    ALIGNMENT byte.  */
5208
5209 static struct dw_loc_descr_struct *
5210 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5211 {
5212   struct dw_loc_descr_struct *head;
5213   unsigned int dwarf_fp
5214     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5215
5216  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5217   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5218     {
5219       head = new_reg_loc_descr (dwarf_fp, 0);
5220       add_loc_descr (&head, int_loc_descriptor (alignment));
5221       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5222       loc_descr_plus_const (&head, offset);
5223     }
5224   else
5225     head = new_reg_loc_descr (dwarf_fp, offset);
5226   return head;
5227 }
5228
5229 /* This function fills in aa dw_cfa_location structure from a dwarf location
5230    descriptor sequence.  */
5231
5232 static void
5233 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5234 {
5235   struct dw_loc_descr_struct *ptr;
5236   cfa->offset = 0;
5237   cfa->base_offset = 0;
5238   cfa->indirect = 0;
5239   cfa->reg = -1;
5240
5241   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5242     {
5243       enum dwarf_location_atom op = ptr->dw_loc_opc;
5244
5245       switch (op)
5246         {
5247         case DW_OP_reg0:
5248         case DW_OP_reg1:
5249         case DW_OP_reg2:
5250         case DW_OP_reg3:
5251         case DW_OP_reg4:
5252         case DW_OP_reg5:
5253         case DW_OP_reg6:
5254         case DW_OP_reg7:
5255         case DW_OP_reg8:
5256         case DW_OP_reg9:
5257         case DW_OP_reg10:
5258         case DW_OP_reg11:
5259         case DW_OP_reg12:
5260         case DW_OP_reg13:
5261         case DW_OP_reg14:
5262         case DW_OP_reg15:
5263         case DW_OP_reg16:
5264         case DW_OP_reg17:
5265         case DW_OP_reg18:
5266         case DW_OP_reg19:
5267         case DW_OP_reg20:
5268         case DW_OP_reg21:
5269         case DW_OP_reg22:
5270         case DW_OP_reg23:
5271         case DW_OP_reg24:
5272         case DW_OP_reg25:
5273         case DW_OP_reg26:
5274         case DW_OP_reg27:
5275         case DW_OP_reg28:
5276         case DW_OP_reg29:
5277         case DW_OP_reg30:
5278         case DW_OP_reg31:
5279           cfa->reg = op - DW_OP_reg0;
5280           break;
5281         case DW_OP_regx:
5282           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5283           break;
5284         case DW_OP_breg0:
5285         case DW_OP_breg1:
5286         case DW_OP_breg2:
5287         case DW_OP_breg3:
5288         case DW_OP_breg4:
5289         case DW_OP_breg5:
5290         case DW_OP_breg6:
5291         case DW_OP_breg7:
5292         case DW_OP_breg8:
5293         case DW_OP_breg9:
5294         case DW_OP_breg10:
5295         case DW_OP_breg11:
5296         case DW_OP_breg12:
5297         case DW_OP_breg13:
5298         case DW_OP_breg14:
5299         case DW_OP_breg15:
5300         case DW_OP_breg16:
5301         case DW_OP_breg17:
5302         case DW_OP_breg18:
5303         case DW_OP_breg19:
5304         case DW_OP_breg20:
5305         case DW_OP_breg21:
5306         case DW_OP_breg22:
5307         case DW_OP_breg23:
5308         case DW_OP_breg24:
5309         case DW_OP_breg25:
5310         case DW_OP_breg26:
5311         case DW_OP_breg27:
5312         case DW_OP_breg28:
5313         case DW_OP_breg29:
5314         case DW_OP_breg30:
5315         case DW_OP_breg31:
5316           cfa->reg = op - DW_OP_breg0;
5317           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5318           break;
5319         case DW_OP_bregx:
5320           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5321           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5322           break;
5323         case DW_OP_deref:
5324           cfa->indirect = 1;
5325           break;
5326         case DW_OP_plus_uconst:
5327           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5328           break;
5329         default:
5330           internal_error ("DW_LOC_OP %s not implemented",
5331                           dwarf_stack_op_name (ptr->dw_loc_opc));
5332         }
5333     }
5334 }
5335 #endif /* .debug_frame support */
5336 \f
5337 /* And now, the support for symbolic debugging information.  */
5338 #ifdef DWARF2_DEBUGGING_INFO
5339
5340 /* .debug_str support.  */
5341 static int output_indirect_string (void **, void *);
5342
5343 static void dwarf2out_init (const char *);
5344 static void dwarf2out_finish (const char *);
5345 static void dwarf2out_define (unsigned int, const char *);
5346 static void dwarf2out_undef (unsigned int, const char *);
5347 static void dwarf2out_start_source_file (unsigned, const char *);
5348 static void dwarf2out_end_source_file (unsigned);
5349 static void dwarf2out_begin_block (unsigned, unsigned);
5350 static void dwarf2out_end_block (unsigned, unsigned);
5351 static bool dwarf2out_ignore_block (const_tree);
5352 static void dwarf2out_global_decl (tree);
5353 static void dwarf2out_type_decl (tree, int);
5354 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5355 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5356                                                  dw_die_ref);
5357 static void dwarf2out_abstract_function (tree);
5358 static void dwarf2out_var_location (rtx);
5359 static void dwarf2out_begin_function (tree);
5360 static void dwarf2out_set_name (tree, tree);
5361
5362 /* The debug hooks structure.  */
5363
5364 const struct gcc_debug_hooks dwarf2_debug_hooks =
5365 {
5366   dwarf2out_init,
5367   dwarf2out_finish,
5368   dwarf2out_define,
5369   dwarf2out_undef,
5370   dwarf2out_start_source_file,
5371   dwarf2out_end_source_file,
5372   dwarf2out_begin_block,
5373   dwarf2out_end_block,
5374   dwarf2out_ignore_block,
5375   dwarf2out_source_line,
5376   dwarf2out_begin_prologue,
5377   debug_nothing_int_charstar,   /* end_prologue */
5378   dwarf2out_end_epilogue,
5379   dwarf2out_begin_function,
5380   debug_nothing_int,            /* end_function */
5381   dwarf2out_decl,               /* function_decl */
5382   dwarf2out_global_decl,
5383   dwarf2out_type_decl,          /* type_decl */
5384   dwarf2out_imported_module_or_decl,
5385   debug_nothing_tree,           /* deferred_inline_function */
5386   /* The DWARF 2 backend tries to reduce debugging bloat by not
5387      emitting the abstract description of inline functions until
5388      something tries to reference them.  */
5389   dwarf2out_abstract_function,  /* outlining_inline_function */
5390   debug_nothing_rtx,            /* label */
5391   debug_nothing_int,            /* handle_pch */
5392   dwarf2out_var_location,
5393   dwarf2out_switch_text_section,
5394   dwarf2out_set_name,
5395   1                             /* start_end_main_source_file */
5396 };
5397 #endif
5398 \f
5399 /* NOTE: In the comments in this file, many references are made to
5400    "Debugging Information Entries".  This term is abbreviated as `DIE'
5401    throughout the remainder of this file.  */
5402
5403 /* An internal representation of the DWARF output is built, and then
5404    walked to generate the DWARF debugging info.  The walk of the internal
5405    representation is done after the entire program has been compiled.
5406    The types below are used to describe the internal representation.  */
5407
5408 /* Various DIE's use offsets relative to the beginning of the
5409    .debug_info section to refer to each other.  */
5410
5411 typedef long int dw_offset;
5412
5413 /* Define typedefs here to avoid circular dependencies.  */
5414
5415 typedef struct dw_attr_struct *dw_attr_ref;
5416 typedef struct dw_line_info_struct *dw_line_info_ref;
5417 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5418 typedef struct pubname_struct *pubname_ref;
5419 typedef struct dw_ranges_struct *dw_ranges_ref;
5420 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5421
5422 /* Each entry in the line_info_table maintains the file and
5423    line number associated with the label generated for that
5424    entry.  The label gives the PC value associated with
5425    the line number entry.  */
5426
5427 typedef struct GTY(()) dw_line_info_struct {
5428   unsigned long dw_file_num;
5429   unsigned long dw_line_num;
5430 }
5431 dw_line_info_entry;
5432
5433 /* Line information for functions in separate sections; each one gets its
5434    own sequence.  */
5435 typedef struct GTY(()) dw_separate_line_info_struct {
5436   unsigned long dw_file_num;
5437   unsigned long dw_line_num;
5438   unsigned long function;
5439 }
5440 dw_separate_line_info_entry;
5441
5442 /* Each DIE attribute has a field specifying the attribute kind,
5443    a link to the next attribute in the chain, and an attribute value.
5444    Attributes are typically linked below the DIE they modify.  */
5445
5446 typedef struct GTY(()) dw_attr_struct {
5447   enum dwarf_attribute dw_attr;
5448   dw_val_node dw_attr_val;
5449 }
5450 dw_attr_node;
5451
5452 DEF_VEC_O(dw_attr_node);
5453 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5454
5455 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5456    The children of each node form a circular list linked by
5457    die_sib.  die_child points to the node *before* the "first" child node.  */
5458
5459 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5460   enum dwarf_tag die_tag;
5461   char *die_symbol;
5462   VEC(dw_attr_node,gc) * die_attr;
5463   dw_die_ref die_parent;
5464   dw_die_ref die_child;
5465   dw_die_ref die_sib;
5466   dw_die_ref die_definition; /* ref from a specification to its definition */
5467   dw_offset die_offset;
5468   unsigned long die_abbrev;
5469   int die_mark;
5470   /* Die is used and must not be pruned as unused.  */
5471   int die_perennial_p;
5472   unsigned int decl_id;
5473 }
5474 die_node;
5475
5476 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5477 #define FOR_EACH_CHILD(die, c, expr) do {       \
5478   c = die->die_child;                           \
5479   if (c) do {                                   \
5480     c = c->die_sib;                             \
5481     expr;                                       \
5482   } while (c != die->die_child);                \
5483 } while (0)
5484
5485 /* The pubname structure */
5486
5487 typedef struct GTY(()) pubname_struct {
5488   dw_die_ref die;
5489   const char *name;
5490 }
5491 pubname_entry;
5492
5493 DEF_VEC_O(pubname_entry);
5494 DEF_VEC_ALLOC_O(pubname_entry, gc);
5495
5496 struct GTY(()) dw_ranges_struct {
5497   /* If this is positive, it's a block number, otherwise it's a
5498      bitwise-negated index into dw_ranges_by_label.  */
5499   int num;
5500 };
5501
5502 struct GTY(()) dw_ranges_by_label_struct {
5503   const char *begin;
5504   const char *end;
5505 };
5506
5507 /* The limbo die list structure.  */
5508 typedef struct GTY(()) limbo_die_struct {
5509   dw_die_ref die;
5510   tree created_for;
5511   struct limbo_die_struct *next;
5512 }
5513 limbo_die_node;
5514
5515 /* How to start an assembler comment.  */
5516 #ifndef ASM_COMMENT_START
5517 #define ASM_COMMENT_START ";#"
5518 #endif
5519
5520 /* Define a macro which returns nonzero for a TYPE_DECL which was
5521    implicitly generated for a tagged type.
5522
5523    Note that unlike the gcc front end (which generates a NULL named
5524    TYPE_DECL node for each complete tagged type, each array type, and
5525    each function type node created) the g++ front end generates a
5526    _named_ TYPE_DECL node for each tagged type node created.
5527    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5528    generate a DW_TAG_typedef DIE for them.  */
5529
5530 #define TYPE_DECL_IS_STUB(decl)                         \
5531   (DECL_NAME (decl) == NULL_TREE                        \
5532    || (DECL_ARTIFICIAL (decl)                           \
5533        && is_tagged_type (TREE_TYPE (decl))             \
5534        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5535            /* This is necessary for stub decls that     \
5536               appear in nested inline functions.  */    \
5537            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5538                && (decl_ultimate_origin (decl)          \
5539                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5540
5541 /* Information concerning the compilation unit's programming
5542    language, and compiler version.  */
5543
5544 /* Fixed size portion of the DWARF compilation unit header.  */
5545 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5546   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5547
5548 /* Fixed size portion of public names info.  */
5549 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5550
5551 /* Fixed size portion of the address range info.  */
5552 #define DWARF_ARANGES_HEADER_SIZE                                       \
5553   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5554                 DWARF2_ADDR_SIZE * 2)                                   \
5555    - DWARF_INITIAL_LENGTH_SIZE)
5556
5557 /* Size of padding portion in the address range info.  It must be
5558    aligned to twice the pointer size.  */
5559 #define DWARF_ARANGES_PAD_SIZE \
5560   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5561                 DWARF2_ADDR_SIZE * 2)                              \
5562    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5563
5564 /* Use assembler line directives if available.  */
5565 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5566 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5567 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5568 #else
5569 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5570 #endif
5571 #endif
5572
5573 /* Minimum line offset in a special line info. opcode.
5574    This value was chosen to give a reasonable range of values.  */
5575 #define DWARF_LINE_BASE  -10
5576
5577 /* First special line opcode - leave room for the standard opcodes.  */
5578 #define DWARF_LINE_OPCODE_BASE  10
5579
5580 /* Range of line offsets in a special line info. opcode.  */
5581 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5582
5583 /* Flag that indicates the initial value of the is_stmt_start flag.
5584    In the present implementation, we do not mark any lines as
5585    the beginning of a source statement, because that information
5586    is not made available by the GCC front-end.  */
5587 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5588
5589 #ifdef DWARF2_DEBUGGING_INFO
5590 /* This location is used by calc_die_sizes() to keep track
5591    the offset of each DIE within the .debug_info section.  */
5592 static unsigned long next_die_offset;
5593 #endif
5594
5595 /* Record the root of the DIE's built for the current compilation unit.  */
5596 static GTY(()) dw_die_ref comp_unit_die;
5597
5598 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5599 static GTY(()) limbo_die_node *limbo_die_list;
5600
5601 /* A list of DIEs for which we may have to generate
5602    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5603    set.  */
5604 static GTY(()) limbo_die_node *deferred_asm_name;
5605
5606 /* Filenames referenced by this compilation unit.  */
5607 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5608
5609 /* A hash table of references to DIE's that describe declarations.
5610    The key is a DECL_UID() which is a unique number identifying each decl.  */
5611 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5612
5613 /* A hash table of references to DIE's that describe COMMON blocks.
5614    The key is DECL_UID() ^ die_parent.  */
5615 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5616
5617 typedef struct GTY(()) die_arg_entry_struct {
5618     dw_die_ref die;
5619     tree arg;
5620 } die_arg_entry;
5621
5622 DEF_VEC_O(die_arg_entry);
5623 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5624
5625 /* Node of the variable location list.  */
5626 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5627   rtx GTY (()) var_loc_note;
5628   const char * GTY (()) label;
5629   const char * GTY (()) section_label;
5630   struct var_loc_node * GTY (()) next;
5631 };
5632
5633 /* Variable location list.  */
5634 struct GTY (()) var_loc_list_def {
5635   struct var_loc_node * GTY (()) first;
5636
5637   /* Do not mark the last element of the chained list because
5638      it is marked through the chain.  */
5639   struct var_loc_node * GTY ((skip ("%h"))) last;
5640
5641   /* DECL_UID of the variable decl.  */
5642   unsigned int decl_id;
5643 };
5644 typedef struct var_loc_list_def var_loc_list;
5645
5646
5647 /* Table of decl location linked lists.  */
5648 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5649
5650 /* A pointer to the base of a list of references to DIE's that
5651    are uniquely identified by their tag, presence/absence of
5652    children DIE's, and list of attribute/value pairs.  */
5653 static GTY((length ("abbrev_die_table_allocated")))
5654   dw_die_ref *abbrev_die_table;
5655
5656 /* Number of elements currently allocated for abbrev_die_table.  */
5657 static GTY(()) unsigned abbrev_die_table_allocated;
5658
5659 /* Number of elements in type_die_table currently in use.  */
5660 static GTY(()) unsigned abbrev_die_table_in_use;
5661
5662 /* Size (in elements) of increments by which we may expand the
5663    abbrev_die_table.  */
5664 #define ABBREV_DIE_TABLE_INCREMENT 256
5665
5666 /* A pointer to the base of a table that contains line information
5667    for each source code line in .text in the compilation unit.  */
5668 static GTY((length ("line_info_table_allocated")))
5669      dw_line_info_ref line_info_table;
5670
5671 /* Number of elements currently allocated for line_info_table.  */
5672 static GTY(()) unsigned line_info_table_allocated;
5673
5674 /* Number of elements in line_info_table currently in use.  */
5675 static GTY(()) unsigned line_info_table_in_use;
5676
5677 /* A pointer to the base of a table that contains line information
5678    for each source code line outside of .text in the compilation unit.  */
5679 static GTY ((length ("separate_line_info_table_allocated")))
5680      dw_separate_line_info_ref separate_line_info_table;
5681
5682 /* Number of elements currently allocated for separate_line_info_table.  */
5683 static GTY(()) unsigned separate_line_info_table_allocated;
5684
5685 /* Number of elements in separate_line_info_table currently in use.  */
5686 static GTY(()) unsigned separate_line_info_table_in_use;
5687
5688 /* Size (in elements) of increments by which we may expand the
5689    line_info_table.  */
5690 #define LINE_INFO_TABLE_INCREMENT 1024
5691
5692 /* A pointer to the base of a table that contains a list of publicly
5693    accessible names.  */
5694 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5695
5696 /* A pointer to the base of a table that contains a list of publicly
5697    accessible types.  */
5698 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5699
5700 /* Array of dies for which we should generate .debug_arange info.  */
5701 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5702
5703 /* Number of elements currently allocated for arange_table.  */
5704 static GTY(()) unsigned arange_table_allocated;
5705
5706 /* Number of elements in arange_table currently in use.  */
5707 static GTY(()) unsigned arange_table_in_use;
5708
5709 /* Size (in elements) of increments by which we may expand the
5710    arange_table.  */
5711 #define ARANGE_TABLE_INCREMENT 64
5712
5713 /* Array of dies for which we should generate .debug_ranges info.  */
5714 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5715
5716 /* Number of elements currently allocated for ranges_table.  */
5717 static GTY(()) unsigned ranges_table_allocated;
5718
5719 /* Number of elements in ranges_table currently in use.  */
5720 static GTY(()) unsigned ranges_table_in_use;
5721
5722 /* Array of pairs of labels referenced in ranges_table.  */
5723 static GTY ((length ("ranges_by_label_allocated")))
5724      dw_ranges_by_label_ref ranges_by_label;
5725
5726 /* Number of elements currently allocated for ranges_by_label.  */
5727 static GTY(()) unsigned ranges_by_label_allocated;
5728
5729 /* Number of elements in ranges_by_label currently in use.  */
5730 static GTY(()) unsigned ranges_by_label_in_use;
5731
5732 /* Size (in elements) of increments by which we may expand the
5733    ranges_table.  */
5734 #define RANGES_TABLE_INCREMENT 64
5735
5736 /* Whether we have location lists that need outputting */
5737 static GTY(()) bool have_location_lists;
5738
5739 /* Unique label counter.  */
5740 static GTY(()) unsigned int loclabel_num;
5741
5742 #ifdef DWARF2_DEBUGGING_INFO
5743 /* Record whether the function being analyzed contains inlined functions.  */
5744 static int current_function_has_inlines;
5745 #endif
5746 #if 0 && defined (MIPS_DEBUGGING_INFO)
5747 static int comp_unit_has_inlines;
5748 #endif
5749
5750 /* The last file entry emitted by maybe_emit_file().  */
5751 static GTY(()) struct dwarf_file_data * last_emitted_file;
5752
5753 /* Number of internal labels generated by gen_internal_sym().  */
5754 static GTY(()) int label_num;
5755
5756 /* Cached result of previous call to lookup_filename.  */
5757 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5758
5759 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5760
5761 #ifdef DWARF2_DEBUGGING_INFO
5762
5763 /* Offset from the "steady-state frame pointer" to the frame base,
5764    within the current function.  */
5765 static HOST_WIDE_INT frame_pointer_fb_offset;
5766
5767 /* Forward declarations for functions defined in this file.  */
5768
5769 static int is_pseudo_reg (const_rtx);
5770 static tree type_main_variant (tree);
5771 static int is_tagged_type (const_tree);
5772 static const char *dwarf_tag_name (unsigned);
5773 static const char *dwarf_attr_name (unsigned);
5774 static const char *dwarf_form_name (unsigned);
5775 static tree decl_ultimate_origin (const_tree);
5776 static tree decl_class_context (tree);
5777 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5778 static inline enum dw_val_class AT_class (dw_attr_ref);
5779 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5780 static inline unsigned AT_flag (dw_attr_ref);
5781 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5782 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5783 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5784 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5785 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, rtx);
5786 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5787                                unsigned int, unsigned char *);
5788 static hashval_t debug_str_do_hash (const void *);
5789 static int debug_str_eq (const void *, const void *);
5790 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5791 static inline const char *AT_string (dw_attr_ref);
5792 static enum dwarf_form AT_string_form (dw_attr_ref);
5793 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5794 static void add_AT_specification (dw_die_ref, dw_die_ref);
5795 static inline dw_die_ref AT_ref (dw_attr_ref);
5796 static inline int AT_ref_external (dw_attr_ref);
5797 static inline void set_AT_ref_external (dw_attr_ref, int);
5798 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5799 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5800 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5801 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5802                              dw_loc_list_ref);
5803 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5804 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5805 static inline rtx AT_addr (dw_attr_ref);
5806 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5807 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5808 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5809 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5810                            unsigned HOST_WIDE_INT);
5811 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5812                                unsigned long);
5813 static inline const char *AT_lbl (dw_attr_ref);
5814 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5815 static const char *get_AT_low_pc (dw_die_ref);
5816 static const char *get_AT_hi_pc (dw_die_ref);
5817 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5818 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5819 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5820 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5821 static bool is_c_family (void);
5822 static bool is_cxx (void);
5823 static bool is_java (void);
5824 static bool is_fortran (void);
5825 static bool is_ada (void);
5826 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5827 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5828 static void add_child_die (dw_die_ref, dw_die_ref);
5829 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5830 static dw_die_ref lookup_type_die (tree);
5831 static void equate_type_number_to_die (tree, dw_die_ref);
5832 static hashval_t decl_die_table_hash (const void *);
5833 static int decl_die_table_eq (const void *, const void *);
5834 static dw_die_ref lookup_decl_die (tree);
5835 static hashval_t common_block_die_table_hash (const void *);
5836 static int common_block_die_table_eq (const void *, const void *);
5837 static hashval_t decl_loc_table_hash (const void *);
5838 static int decl_loc_table_eq (const void *, const void *);
5839 static var_loc_list *lookup_decl_loc (const_tree);
5840 static void equate_decl_number_to_die (tree, dw_die_ref);
5841 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5842 static void print_spaces (FILE *);
5843 static void print_die (dw_die_ref, FILE *);
5844 static void print_dwarf_line_table (FILE *);
5845 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5846 static dw_die_ref pop_compile_unit (dw_die_ref);
5847 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5848 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5849 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5850 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5851 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5852 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5853 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5854 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5855 static void compute_section_prefix (dw_die_ref);
5856 static int is_type_die (dw_die_ref);
5857 static int is_comdat_die (dw_die_ref);
5858 static int is_symbol_die (dw_die_ref);
5859 static void assign_symbol_names (dw_die_ref);
5860 static void break_out_includes (dw_die_ref);
5861 static hashval_t htab_cu_hash (const void *);
5862 static int htab_cu_eq (const void *, const void *);
5863 static void htab_cu_del (void *);
5864 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5865 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5866 static void add_sibling_attributes (dw_die_ref);
5867 static void build_abbrev_table (dw_die_ref);
5868 static void output_location_lists (dw_die_ref);
5869 static int constant_size (unsigned HOST_WIDE_INT);
5870 static unsigned long size_of_die (dw_die_ref);
5871 static void calc_die_sizes (dw_die_ref);
5872 static void mark_dies (dw_die_ref);
5873 static void unmark_dies (dw_die_ref);
5874 static void unmark_all_dies (dw_die_ref);
5875 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5876 static unsigned long size_of_aranges (void);
5877 static enum dwarf_form value_format (dw_attr_ref);
5878 static void output_value_format (dw_attr_ref);
5879 static void output_abbrev_section (void);
5880 static void output_die_symbol (dw_die_ref);
5881 static void output_die (dw_die_ref);
5882 static void output_compilation_unit_header (void);
5883 static void output_comp_unit (dw_die_ref, int);
5884 static const char *dwarf2_name (tree, int);
5885 static void add_pubname (tree, dw_die_ref);
5886 static void add_pubname_string (const char *, dw_die_ref);
5887 static void add_pubtype (tree, dw_die_ref);
5888 static void output_pubnames (VEC (pubname_entry,gc) *);
5889 static void add_arange (tree, dw_die_ref);
5890 static void output_aranges (void);
5891 static unsigned int add_ranges_num (int);
5892 static unsigned int add_ranges (const_tree);
5893 static unsigned int add_ranges_by_labels (const char *, const char *);
5894 static void output_ranges (void);
5895 static void output_line_info (void);
5896 static void output_file_names (void);
5897 static dw_die_ref base_type_die (tree);
5898 static int is_base_type (tree);
5899 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5900 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5901 static dw_die_ref generic_parameter_die (tree, tree, dw_die_ref, int);
5902 static int type_is_enum (const_tree);
5903 static unsigned int dbx_reg_number (const_rtx);
5904 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5905 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5906 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5907                                                 enum var_init_status);
5908 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5909                                                      enum var_init_status);
5910 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5911                                          enum var_init_status);
5912 static int is_based_loc (const_rtx);
5913 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5914                                             enum var_init_status);
5915 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5916                                                enum var_init_status);
5917 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
5918                                         enum var_init_status);
5919 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
5920 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
5921 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5922 static tree field_type (const_tree);
5923 static unsigned int simple_type_align_in_bits (const_tree);
5924 static unsigned int simple_decl_align_in_bits (const_tree);
5925 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5926 static HOST_WIDE_INT field_byte_offset (const_tree);
5927 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5928                                          dw_loc_descr_ref);
5929 static void add_data_member_location_attribute (dw_die_ref, tree);
5930 static void add_const_value_attribute (dw_die_ref, rtx);
5931 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5932 static void insert_float (const_rtx, unsigned char *);
5933 static rtx rtl_for_decl_location (tree);
5934 static void add_location_or_const_value_attribute (dw_die_ref, tree,
5935                                                    enum dwarf_attribute);
5936 static void tree_add_const_value_attribute (dw_die_ref, tree);
5937 static void tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
5938 static void add_name_attribute (dw_die_ref, const char *);
5939 static void add_comp_dir_attribute (dw_die_ref);
5940 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5941 static void add_subscript_info (dw_die_ref, tree, bool);
5942 static void add_byte_size_attribute (dw_die_ref, tree);
5943 static void add_bit_offset_attribute (dw_die_ref, tree);
5944 static void add_bit_size_attribute (dw_die_ref, tree);
5945 static void add_prototyped_attribute (dw_die_ref, tree);
5946 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5947 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5948 static void add_src_coords_attributes (dw_die_ref, tree);
5949 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5950 static void push_decl_scope (tree);
5951 static void pop_decl_scope (void);
5952 static dw_die_ref scope_die_for (tree, dw_die_ref);
5953 static inline int local_scope_p (dw_die_ref);
5954 static inline int class_scope_p (dw_die_ref);
5955 static inline int class_or_namespace_scope_p (dw_die_ref);
5956 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5957 static void add_calling_convention_attribute (dw_die_ref, tree);
5958 static const char *type_tag (const_tree);
5959 static tree member_declared_type (const_tree);
5960 #if 0
5961 static const char *decl_start_label (tree);
5962 #endif
5963 static void gen_array_type_die (tree, dw_die_ref);
5964 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
5965 #if 0
5966 static void gen_entry_point_die (tree, dw_die_ref);
5967 #endif
5968 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
5969 static dw_die_ref gen_formal_parameter_die (tree, tree, dw_die_ref);
5970 static void gen_unspecified_parameters_die (tree, dw_die_ref);
5971 static void gen_formal_types_die (tree, dw_die_ref);
5972 static void gen_subprogram_die (tree, dw_die_ref);
5973 static void gen_variable_die (tree, tree, dw_die_ref);
5974 static void gen_const_die (tree, dw_die_ref);
5975 static void gen_label_die (tree, dw_die_ref);
5976 static void gen_lexical_block_die (tree, dw_die_ref, int);
5977 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
5978 static void gen_field_die (tree, dw_die_ref);
5979 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
5980 static dw_die_ref gen_compile_unit_die (const char *);
5981 static void gen_inheritance_die (tree, tree, dw_die_ref);
5982 static void gen_member_die (tree, dw_die_ref);
5983 static void gen_struct_or_union_type_die (tree, dw_die_ref,
5984                                                 enum debug_info_usage);
5985 static void gen_subroutine_type_die (tree, dw_die_ref);
5986 static void gen_typedef_die (tree, dw_die_ref);
5987 static void gen_type_die (tree, dw_die_ref);
5988 static void gen_block_die (tree, dw_die_ref, int);
5989 static void decls_for_scope (tree, dw_die_ref, int);
5990 static int is_redundant_typedef (const_tree);
5991 static inline dw_die_ref get_context_die (tree);
5992 static void gen_namespace_die (tree, dw_die_ref);
5993 static void gen_decl_die (tree, tree, dw_die_ref);
5994 static dw_die_ref force_decl_die (tree);
5995 static dw_die_ref force_type_die (tree);
5996 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
5997 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
5998 static struct dwarf_file_data * lookup_filename (const char *);
5999 static void retry_incomplete_types (void);
6000 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6001 static tree make_ith_pack_parameter_name (tree, int);
6002 static void gen_generic_params_dies (tree);
6003 static void splice_child_die (dw_die_ref, dw_die_ref);
6004 static int file_info_cmp (const void *, const void *);
6005 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6006                                      const char *, const char *, unsigned);
6007 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6008                                        const char *, const char *,
6009                                        const char *);
6010 static void output_loc_list (dw_loc_list_ref);
6011 static char *gen_internal_sym (const char *);
6012
6013 static void prune_unmark_dies (dw_die_ref);
6014 static void prune_unused_types_mark (dw_die_ref, int);
6015 static void prune_unused_types_walk (dw_die_ref);
6016 static void prune_unused_types_walk_attribs (dw_die_ref);
6017 static void prune_unused_types_prune (dw_die_ref);
6018 static void prune_unused_types (void);
6019 static int maybe_emit_file (struct dwarf_file_data *fd);
6020 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6021 static void gen_remaining_tmpl_value_param_die_attribute (void);
6022
6023 /* Section names used to hold DWARF debugging information.  */
6024 #ifndef DEBUG_INFO_SECTION
6025 #define DEBUG_INFO_SECTION      ".debug_info"
6026 #endif
6027 #ifndef DEBUG_ABBREV_SECTION
6028 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6029 #endif
6030 #ifndef DEBUG_ARANGES_SECTION
6031 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6032 #endif
6033 #ifndef DEBUG_MACINFO_SECTION
6034 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6035 #endif
6036 #ifndef DEBUG_LINE_SECTION
6037 #define DEBUG_LINE_SECTION      ".debug_line"
6038 #endif
6039 #ifndef DEBUG_LOC_SECTION
6040 #define DEBUG_LOC_SECTION       ".debug_loc"
6041 #endif
6042 #ifndef DEBUG_PUBNAMES_SECTION
6043 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6044 #endif
6045 #ifndef DEBUG_PUBTYPES_SECTION
6046 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6047 #endif
6048 #ifndef DEBUG_STR_SECTION
6049 #define DEBUG_STR_SECTION       ".debug_str"
6050 #endif
6051 #ifndef DEBUG_RANGES_SECTION
6052 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6053 #endif
6054
6055 /* Standard ELF section names for compiled code and data.  */
6056 #ifndef TEXT_SECTION_NAME
6057 #define TEXT_SECTION_NAME       ".text"
6058 #endif
6059
6060 /* Section flags for .debug_str section.  */
6061 #define DEBUG_STR_SECTION_FLAGS \
6062   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6063    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6064    : SECTION_DEBUG)
6065
6066 /* Labels we insert at beginning sections we can reference instead of
6067    the section names themselves.  */
6068
6069 #ifndef TEXT_SECTION_LABEL
6070 #define TEXT_SECTION_LABEL              "Ltext"
6071 #endif
6072 #ifndef COLD_TEXT_SECTION_LABEL
6073 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6074 #endif
6075 #ifndef DEBUG_LINE_SECTION_LABEL
6076 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6077 #endif
6078 #ifndef DEBUG_INFO_SECTION_LABEL
6079 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6080 #endif
6081 #ifndef DEBUG_ABBREV_SECTION_LABEL
6082 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6083 #endif
6084 #ifndef DEBUG_LOC_SECTION_LABEL
6085 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6086 #endif
6087 #ifndef DEBUG_RANGES_SECTION_LABEL
6088 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6089 #endif
6090 #ifndef DEBUG_MACINFO_SECTION_LABEL
6091 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6092 #endif
6093
6094 /* Definitions of defaults for formats and names of various special
6095    (artificial) labels which may be generated within this file (when the -g
6096    options is used and DWARF2_DEBUGGING_INFO is in effect.
6097    If necessary, these may be overridden from within the tm.h file, but
6098    typically, overriding these defaults is unnecessary.  */
6099
6100 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6101 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6102 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6103 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6104 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6105 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6106 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6107 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6108 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6109 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6110
6111 #ifndef TEXT_END_LABEL
6112 #define TEXT_END_LABEL          "Letext"
6113 #endif
6114 #ifndef COLD_END_LABEL
6115 #define COLD_END_LABEL          "Letext_cold"
6116 #endif
6117 #ifndef BLOCK_BEGIN_LABEL
6118 #define BLOCK_BEGIN_LABEL       "LBB"
6119 #endif
6120 #ifndef BLOCK_END_LABEL
6121 #define BLOCK_END_LABEL         "LBE"
6122 #endif
6123 #ifndef LINE_CODE_LABEL
6124 #define LINE_CODE_LABEL         "LM"
6125 #endif
6126 #ifndef SEPARATE_LINE_CODE_LABEL
6127 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6128 #endif
6129
6130 \f
6131 /* We allow a language front-end to designate a function that is to be
6132    called to "demangle" any name before it is put into a DIE.  */
6133
6134 static const char *(*demangle_name_func) (const char *);
6135
6136 void
6137 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6138 {
6139   demangle_name_func = func;
6140 }
6141
6142 /* Test if rtl node points to a pseudo register.  */
6143
6144 static inline int
6145 is_pseudo_reg (const_rtx rtl)
6146 {
6147   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6148           || (GET_CODE (rtl) == SUBREG
6149               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6150 }
6151
6152 /* Return a reference to a type, with its const and volatile qualifiers
6153    removed.  */
6154
6155 static inline tree
6156 type_main_variant (tree type)
6157 {
6158   type = TYPE_MAIN_VARIANT (type);
6159
6160   /* ??? There really should be only one main variant among any group of
6161      variants of a given type (and all of the MAIN_VARIANT values for all
6162      members of the group should point to that one type) but sometimes the C
6163      front-end messes this up for array types, so we work around that bug
6164      here.  */
6165   if (TREE_CODE (type) == ARRAY_TYPE)
6166     while (type != TYPE_MAIN_VARIANT (type))
6167       type = TYPE_MAIN_VARIANT (type);
6168
6169   return type;
6170 }
6171
6172 /* Return nonzero if the given type node represents a tagged type.  */
6173
6174 static inline int
6175 is_tagged_type (const_tree type)
6176 {
6177   enum tree_code code = TREE_CODE (type);
6178
6179   return (code == RECORD_TYPE || code == UNION_TYPE
6180           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6181 }
6182
6183 /* Convert a DIE tag into its string name.  */
6184
6185 static const char *
6186 dwarf_tag_name (unsigned int tag)
6187 {
6188   switch (tag)
6189     {
6190     case DW_TAG_padding:
6191       return "DW_TAG_padding";
6192     case DW_TAG_array_type:
6193       return "DW_TAG_array_type";
6194     case DW_TAG_class_type:
6195       return "DW_TAG_class_type";
6196     case DW_TAG_entry_point:
6197       return "DW_TAG_entry_point";
6198     case DW_TAG_enumeration_type:
6199       return "DW_TAG_enumeration_type";
6200     case DW_TAG_formal_parameter:
6201       return "DW_TAG_formal_parameter";
6202     case DW_TAG_imported_declaration:
6203       return "DW_TAG_imported_declaration";
6204     case DW_TAG_label:
6205       return "DW_TAG_label";
6206     case DW_TAG_lexical_block:
6207       return "DW_TAG_lexical_block";
6208     case DW_TAG_member:
6209       return "DW_TAG_member";
6210     case DW_TAG_pointer_type:
6211       return "DW_TAG_pointer_type";
6212     case DW_TAG_reference_type:
6213       return "DW_TAG_reference_type";
6214     case DW_TAG_compile_unit:
6215       return "DW_TAG_compile_unit";
6216     case DW_TAG_string_type:
6217       return "DW_TAG_string_type";
6218     case DW_TAG_structure_type:
6219       return "DW_TAG_structure_type";
6220     case DW_TAG_subroutine_type:
6221       return "DW_TAG_subroutine_type";
6222     case DW_TAG_typedef:
6223       return "DW_TAG_typedef";
6224     case DW_TAG_union_type:
6225       return "DW_TAG_union_type";
6226     case DW_TAG_unspecified_parameters:
6227       return "DW_TAG_unspecified_parameters";
6228     case DW_TAG_variant:
6229       return "DW_TAG_variant";
6230     case DW_TAG_common_block:
6231       return "DW_TAG_common_block";
6232     case DW_TAG_common_inclusion:
6233       return "DW_TAG_common_inclusion";
6234     case DW_TAG_inheritance:
6235       return "DW_TAG_inheritance";
6236     case DW_TAG_inlined_subroutine:
6237       return "DW_TAG_inlined_subroutine";
6238     case DW_TAG_module:
6239       return "DW_TAG_module";
6240     case DW_TAG_ptr_to_member_type:
6241       return "DW_TAG_ptr_to_member_type";
6242     case DW_TAG_set_type:
6243       return "DW_TAG_set_type";
6244     case DW_TAG_subrange_type:
6245       return "DW_TAG_subrange_type";
6246     case DW_TAG_with_stmt:
6247       return "DW_TAG_with_stmt";
6248     case DW_TAG_access_declaration:
6249       return "DW_TAG_access_declaration";
6250     case DW_TAG_base_type:
6251       return "DW_TAG_base_type";
6252     case DW_TAG_catch_block:
6253       return "DW_TAG_catch_block";
6254     case DW_TAG_const_type:
6255       return "DW_TAG_const_type";
6256     case DW_TAG_constant:
6257       return "DW_TAG_constant";
6258     case DW_TAG_enumerator:
6259       return "DW_TAG_enumerator";
6260     case DW_TAG_file_type:
6261       return "DW_TAG_file_type";
6262     case DW_TAG_friend:
6263       return "DW_TAG_friend";
6264     case DW_TAG_namelist:
6265       return "DW_TAG_namelist";
6266     case DW_TAG_namelist_item:
6267       return "DW_TAG_namelist_item";
6268     case DW_TAG_packed_type:
6269       return "DW_TAG_packed_type";
6270     case DW_TAG_subprogram:
6271       return "DW_TAG_subprogram";
6272     case DW_TAG_template_type_param:
6273       return "DW_TAG_template_type_param";
6274     case DW_TAG_template_value_param:
6275       return "DW_TAG_template_value_param";
6276     case DW_TAG_thrown_type:
6277       return "DW_TAG_thrown_type";
6278     case DW_TAG_try_block:
6279       return "DW_TAG_try_block";
6280     case DW_TAG_variant_part:
6281       return "DW_TAG_variant_part";
6282     case DW_TAG_variable:
6283       return "DW_TAG_variable";
6284     case DW_TAG_volatile_type:
6285       return "DW_TAG_volatile_type";
6286     case DW_TAG_dwarf_procedure:
6287       return "DW_TAG_dwarf_procedure";
6288     case DW_TAG_restrict_type:
6289       return "DW_TAG_restrict_type";
6290     case DW_TAG_interface_type:
6291       return "DW_TAG_interface_type";
6292     case DW_TAG_namespace:
6293       return "DW_TAG_namespace";
6294     case DW_TAG_imported_module:
6295       return "DW_TAG_imported_module";
6296     case DW_TAG_unspecified_type:
6297       return "DW_TAG_unspecified_type";
6298     case DW_TAG_partial_unit:
6299       return "DW_TAG_partial_unit";
6300     case DW_TAG_imported_unit:
6301       return "DW_TAG_imported_unit";
6302     case DW_TAG_condition:
6303       return "DW_TAG_condition";
6304     case DW_TAG_shared_type:
6305       return "DW_TAG_shared_type";
6306     case DW_TAG_MIPS_loop:
6307       return "DW_TAG_MIPS_loop";
6308     case DW_TAG_format_label:
6309       return "DW_TAG_format_label";
6310     case DW_TAG_function_template:
6311       return "DW_TAG_function_template";
6312     case DW_TAG_class_template:
6313       return "DW_TAG_class_template";
6314     case DW_TAG_GNU_BINCL:
6315       return "DW_TAG_GNU_BINCL";
6316     case DW_TAG_GNU_EINCL:
6317       return "DW_TAG_GNU_EINCL";
6318     case DW_TAG_GNU_template_template_param:
6319       return "DW_TAG_GNU_template_template_param";
6320     default:
6321       return "DW_TAG_<unknown>";
6322     }
6323 }
6324
6325 /* Convert a DWARF attribute code into its string name.  */
6326
6327 static const char *
6328 dwarf_attr_name (unsigned int attr)
6329 {
6330   switch (attr)
6331     {
6332     case DW_AT_sibling:
6333       return "DW_AT_sibling";
6334     case DW_AT_location:
6335       return "DW_AT_location";
6336     case DW_AT_name:
6337       return "DW_AT_name";
6338     case DW_AT_ordering:
6339       return "DW_AT_ordering";
6340     case DW_AT_subscr_data:
6341       return "DW_AT_subscr_data";
6342     case DW_AT_byte_size:
6343       return "DW_AT_byte_size";
6344     case DW_AT_bit_offset:
6345       return "DW_AT_bit_offset";
6346     case DW_AT_bit_size:
6347       return "DW_AT_bit_size";
6348     case DW_AT_element_list:
6349       return "DW_AT_element_list";
6350     case DW_AT_stmt_list:
6351       return "DW_AT_stmt_list";
6352     case DW_AT_low_pc:
6353       return "DW_AT_low_pc";
6354     case DW_AT_high_pc:
6355       return "DW_AT_high_pc";
6356     case DW_AT_language:
6357       return "DW_AT_language";
6358     case DW_AT_member:
6359       return "DW_AT_member";
6360     case DW_AT_discr:
6361       return "DW_AT_discr";
6362     case DW_AT_discr_value:
6363       return "DW_AT_discr_value";
6364     case DW_AT_visibility:
6365       return "DW_AT_visibility";
6366     case DW_AT_import:
6367       return "DW_AT_import";
6368     case DW_AT_string_length:
6369       return "DW_AT_string_length";
6370     case DW_AT_common_reference:
6371       return "DW_AT_common_reference";
6372     case DW_AT_comp_dir:
6373       return "DW_AT_comp_dir";
6374     case DW_AT_const_value:
6375       return "DW_AT_const_value";
6376     case DW_AT_containing_type:
6377       return "DW_AT_containing_type";
6378     case DW_AT_default_value:
6379       return "DW_AT_default_value";
6380     case DW_AT_inline:
6381       return "DW_AT_inline";
6382     case DW_AT_is_optional:
6383       return "DW_AT_is_optional";
6384     case DW_AT_lower_bound:
6385       return "DW_AT_lower_bound";
6386     case DW_AT_producer:
6387       return "DW_AT_producer";
6388     case DW_AT_prototyped:
6389       return "DW_AT_prototyped";
6390     case DW_AT_return_addr:
6391       return "DW_AT_return_addr";
6392     case DW_AT_start_scope:
6393       return "DW_AT_start_scope";
6394     case DW_AT_bit_stride:
6395       return "DW_AT_bit_stride";
6396     case DW_AT_upper_bound:
6397       return "DW_AT_upper_bound";
6398     case DW_AT_abstract_origin:
6399       return "DW_AT_abstract_origin";
6400     case DW_AT_accessibility:
6401       return "DW_AT_accessibility";
6402     case DW_AT_address_class:
6403       return "DW_AT_address_class";
6404     case DW_AT_artificial:
6405       return "DW_AT_artificial";
6406     case DW_AT_base_types:
6407       return "DW_AT_base_types";
6408     case DW_AT_calling_convention:
6409       return "DW_AT_calling_convention";
6410     case DW_AT_count:
6411       return "DW_AT_count";
6412     case DW_AT_data_member_location:
6413       return "DW_AT_data_member_location";
6414     case DW_AT_decl_column:
6415       return "DW_AT_decl_column";
6416     case DW_AT_decl_file:
6417       return "DW_AT_decl_file";
6418     case DW_AT_decl_line:
6419       return "DW_AT_decl_line";
6420     case DW_AT_declaration:
6421       return "DW_AT_declaration";
6422     case DW_AT_discr_list:
6423       return "DW_AT_discr_list";
6424     case DW_AT_encoding:
6425       return "DW_AT_encoding";
6426     case DW_AT_external:
6427       return "DW_AT_external";
6428     case DW_AT_explicit:
6429       return "DW_AT_explicit";
6430     case DW_AT_frame_base:
6431       return "DW_AT_frame_base";
6432     case DW_AT_friend:
6433       return "DW_AT_friend";
6434     case DW_AT_identifier_case:
6435       return "DW_AT_identifier_case";
6436     case DW_AT_macro_info:
6437       return "DW_AT_macro_info";
6438     case DW_AT_namelist_items:
6439       return "DW_AT_namelist_items";
6440     case DW_AT_priority:
6441       return "DW_AT_priority";
6442     case DW_AT_segment:
6443       return "DW_AT_segment";
6444     case DW_AT_specification:
6445       return "DW_AT_specification";
6446     case DW_AT_static_link:
6447       return "DW_AT_static_link";
6448     case DW_AT_type:
6449       return "DW_AT_type";
6450     case DW_AT_use_location:
6451       return "DW_AT_use_location";
6452     case DW_AT_variable_parameter:
6453       return "DW_AT_variable_parameter";
6454     case DW_AT_virtuality:
6455       return "DW_AT_virtuality";
6456     case DW_AT_vtable_elem_location:
6457       return "DW_AT_vtable_elem_location";
6458
6459     case DW_AT_allocated:
6460       return "DW_AT_allocated";
6461     case DW_AT_associated:
6462       return "DW_AT_associated";
6463     case DW_AT_data_location:
6464       return "DW_AT_data_location";
6465     case DW_AT_byte_stride:
6466       return "DW_AT_byte_stride";
6467     case DW_AT_entry_pc:
6468       return "DW_AT_entry_pc";
6469     case DW_AT_use_UTF8:
6470       return "DW_AT_use_UTF8";
6471     case DW_AT_extension:
6472       return "DW_AT_extension";
6473     case DW_AT_ranges:
6474       return "DW_AT_ranges";
6475     case DW_AT_trampoline:
6476       return "DW_AT_trampoline";
6477     case DW_AT_call_column:
6478       return "DW_AT_call_column";
6479     case DW_AT_call_file:
6480       return "DW_AT_call_file";
6481     case DW_AT_call_line:
6482       return "DW_AT_call_line";
6483
6484     case DW_AT_MIPS_fde:
6485       return "DW_AT_MIPS_fde";
6486     case DW_AT_MIPS_loop_begin:
6487       return "DW_AT_MIPS_loop_begin";
6488     case DW_AT_MIPS_tail_loop_begin:
6489       return "DW_AT_MIPS_tail_loop_begin";
6490     case DW_AT_MIPS_epilog_begin:
6491       return "DW_AT_MIPS_epilog_begin";
6492     case DW_AT_MIPS_loop_unroll_factor:
6493       return "DW_AT_MIPS_loop_unroll_factor";
6494     case DW_AT_MIPS_software_pipeline_depth:
6495       return "DW_AT_MIPS_software_pipeline_depth";
6496     case DW_AT_MIPS_linkage_name:
6497       return "DW_AT_MIPS_linkage_name";
6498     case DW_AT_MIPS_stride:
6499       return "DW_AT_MIPS_stride";
6500     case DW_AT_MIPS_abstract_name:
6501       return "DW_AT_MIPS_abstract_name";
6502     case DW_AT_MIPS_clone_origin:
6503       return "DW_AT_MIPS_clone_origin";
6504     case DW_AT_MIPS_has_inlines:
6505       return "DW_AT_MIPS_has_inlines";
6506
6507     case DW_AT_sf_names:
6508       return "DW_AT_sf_names";
6509     case DW_AT_src_info:
6510       return "DW_AT_src_info";
6511     case DW_AT_mac_info:
6512       return "DW_AT_mac_info";
6513     case DW_AT_src_coords:
6514       return "DW_AT_src_coords";
6515     case DW_AT_body_begin:
6516       return "DW_AT_body_begin";
6517     case DW_AT_body_end:
6518       return "DW_AT_body_end";
6519     case DW_AT_GNU_vector:
6520       return "DW_AT_GNU_vector";
6521     case DW_AT_GNU_template_name:
6522       return "DW_AT_GNU_template_name";
6523
6524     case DW_AT_VMS_rtnbeg_pd_address:
6525       return "DW_AT_VMS_rtnbeg_pd_address";
6526
6527     default:
6528       return "DW_AT_<unknown>";
6529     }
6530 }
6531
6532 /* Convert a DWARF value form code into its string name.  */
6533
6534 static const char *
6535 dwarf_form_name (unsigned int form)
6536 {
6537   switch (form)
6538     {
6539     case DW_FORM_addr:
6540       return "DW_FORM_addr";
6541     case DW_FORM_block2:
6542       return "DW_FORM_block2";
6543     case DW_FORM_block4:
6544       return "DW_FORM_block4";
6545     case DW_FORM_data2:
6546       return "DW_FORM_data2";
6547     case DW_FORM_data4:
6548       return "DW_FORM_data4";
6549     case DW_FORM_data8:
6550       return "DW_FORM_data8";
6551     case DW_FORM_string:
6552       return "DW_FORM_string";
6553     case DW_FORM_block:
6554       return "DW_FORM_block";
6555     case DW_FORM_block1:
6556       return "DW_FORM_block1";
6557     case DW_FORM_data1:
6558       return "DW_FORM_data1";
6559     case DW_FORM_flag:
6560       return "DW_FORM_flag";
6561     case DW_FORM_sdata:
6562       return "DW_FORM_sdata";
6563     case DW_FORM_strp:
6564       return "DW_FORM_strp";
6565     case DW_FORM_udata:
6566       return "DW_FORM_udata";
6567     case DW_FORM_ref_addr:
6568       return "DW_FORM_ref_addr";
6569     case DW_FORM_ref1:
6570       return "DW_FORM_ref1";
6571     case DW_FORM_ref2:
6572       return "DW_FORM_ref2";
6573     case DW_FORM_ref4:
6574       return "DW_FORM_ref4";
6575     case DW_FORM_ref8:
6576       return "DW_FORM_ref8";
6577     case DW_FORM_ref_udata:
6578       return "DW_FORM_ref_udata";
6579     case DW_FORM_indirect:
6580       return "DW_FORM_indirect";
6581     default:
6582       return "DW_FORM_<unknown>";
6583     }
6584 }
6585 \f
6586 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6587    instance of an inlined instance of a decl which is local to an inline
6588    function, so we have to trace all of the way back through the origin chain
6589    to find out what sort of node actually served as the original seed for the
6590    given block.  */
6591
6592 static tree
6593 decl_ultimate_origin (const_tree decl)
6594 {
6595   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6596     return NULL_TREE;
6597
6598   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6599      nodes in the function to point to themselves; ignore that if
6600      we're trying to output the abstract instance of this function.  */
6601   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6602     return NULL_TREE;
6603
6604   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6605      most distant ancestor, this should never happen.  */
6606   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6607
6608   return DECL_ABSTRACT_ORIGIN (decl);
6609 }
6610
6611 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6612    of a virtual function may refer to a base class, so we check the 'this'
6613    parameter.  */
6614
6615 static tree
6616 decl_class_context (tree decl)
6617 {
6618   tree context = NULL_TREE;
6619
6620   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6621     context = DECL_CONTEXT (decl);
6622   else
6623     context = TYPE_MAIN_VARIANT
6624       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6625
6626   if (context && !TYPE_P (context))
6627     context = NULL_TREE;
6628
6629   return context;
6630 }
6631 \f
6632 /* Add an attribute/value pair to a DIE.  */
6633
6634 static inline void
6635 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6636 {
6637   /* Maybe this should be an assert?  */
6638   if (die == NULL)
6639     return;
6640
6641   if (die->die_attr == NULL)
6642     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6643   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6644 }
6645
6646 static inline enum dw_val_class
6647 AT_class (dw_attr_ref a)
6648 {
6649   return a->dw_attr_val.val_class;
6650 }
6651
6652 /* Add a flag value attribute to a DIE.  */
6653
6654 static inline void
6655 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6656 {
6657   dw_attr_node attr;
6658
6659   attr.dw_attr = attr_kind;
6660   attr.dw_attr_val.val_class = dw_val_class_flag;
6661   attr.dw_attr_val.v.val_flag = flag;
6662   add_dwarf_attr (die, &attr);
6663 }
6664
6665 static inline unsigned
6666 AT_flag (dw_attr_ref a)
6667 {
6668   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6669   return a->dw_attr_val.v.val_flag;
6670 }
6671
6672 /* Add a signed integer attribute value to a DIE.  */
6673
6674 static inline void
6675 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6676 {
6677   dw_attr_node attr;
6678
6679   attr.dw_attr = attr_kind;
6680   attr.dw_attr_val.val_class = dw_val_class_const;
6681   attr.dw_attr_val.v.val_int = int_val;
6682   add_dwarf_attr (die, &attr);
6683 }
6684
6685 static inline HOST_WIDE_INT
6686 AT_int (dw_attr_ref a)
6687 {
6688   gcc_assert (a && AT_class (a) == dw_val_class_const);
6689   return a->dw_attr_val.v.val_int;
6690 }
6691
6692 /* Add an unsigned integer attribute value to a DIE.  */
6693
6694 static inline void
6695 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6696                  unsigned HOST_WIDE_INT unsigned_val)
6697 {
6698   dw_attr_node attr;
6699
6700   attr.dw_attr = attr_kind;
6701   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6702   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6703   add_dwarf_attr (die, &attr);
6704 }
6705
6706 static inline unsigned HOST_WIDE_INT
6707 AT_unsigned (dw_attr_ref a)
6708 {
6709   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6710   return a->dw_attr_val.v.val_unsigned;
6711 }
6712
6713 /* Add an unsigned double integer attribute value to a DIE.  */
6714
6715 static inline void
6716 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
6717                   rtx val_const_double)
6718 {
6719   dw_attr_node attr;
6720
6721   attr.dw_attr = attr_kind;
6722   attr.dw_attr_val.val_class = dw_val_class_long_long;
6723   attr.dw_attr_val.v.val_long_long = val_const_double;
6724   add_dwarf_attr (die, &attr);
6725 }
6726
6727 /* Add a floating point attribute value to a DIE and return it.  */
6728
6729 static inline void
6730 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6731             unsigned int length, unsigned int elt_size, unsigned char *array)
6732 {
6733   dw_attr_node attr;
6734
6735   attr.dw_attr = attr_kind;
6736   attr.dw_attr_val.val_class = dw_val_class_vec;
6737   attr.dw_attr_val.v.val_vec.length = length;
6738   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6739   attr.dw_attr_val.v.val_vec.array = array;
6740   add_dwarf_attr (die, &attr);
6741 }
6742
6743 /* Hash and equality functions for debug_str_hash.  */
6744
6745 static hashval_t
6746 debug_str_do_hash (const void *x)
6747 {
6748   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6749 }
6750
6751 static int
6752 debug_str_eq (const void *x1, const void *x2)
6753 {
6754   return strcmp ((((const struct indirect_string_node *)x1)->str),
6755                  (const char *)x2) == 0;
6756 }
6757
6758 /* Add STR to the indirect string hash table.  */
6759
6760 static struct indirect_string_node *
6761 find_AT_string (const char *str)
6762 {
6763   struct indirect_string_node *node;
6764   void **slot;
6765
6766   if (! debug_str_hash)
6767     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6768                                       debug_str_eq, NULL);
6769
6770   slot = htab_find_slot_with_hash (debug_str_hash, str,
6771                                    htab_hash_string (str), INSERT);
6772   if (*slot == NULL)
6773     {
6774       node = (struct indirect_string_node *)
6775                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6776       node->str = ggc_strdup (str);
6777       *slot = node;
6778     }
6779   else
6780     node = (struct indirect_string_node *) *slot;
6781
6782   node->refcount++;
6783   return node;
6784 }
6785
6786 /* Add a string attribute value to a DIE.  */
6787
6788 static inline void
6789 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6790 {
6791   dw_attr_node attr;
6792   struct indirect_string_node *node;
6793
6794   node = find_AT_string (str);
6795
6796   attr.dw_attr = attr_kind;
6797   attr.dw_attr_val.val_class = dw_val_class_str;
6798   attr.dw_attr_val.v.val_str = node;
6799   add_dwarf_attr (die, &attr);
6800 }
6801
6802 /* Create a label for an indirect string node, ensuring it is going to
6803    be output, unless its reference count goes down to zero.  */
6804
6805 static inline void
6806 gen_label_for_indirect_string (struct indirect_string_node *node)
6807 {
6808   char label[32];
6809
6810   if (node->label)
6811     return;
6812
6813   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6814   ++dw2_string_counter;
6815   node->label = xstrdup (label);
6816 }
6817
6818 /* Create a SYMBOL_REF rtx whose value is the initial address of a
6819    debug string STR.  */
6820
6821 static inline rtx
6822 get_debug_string_label (const char *str)
6823 {
6824   struct indirect_string_node *node = find_AT_string (str);
6825
6826   debug_str_hash_forced = true;
6827
6828   gen_label_for_indirect_string (node);
6829
6830   return gen_rtx_SYMBOL_REF (Pmode, node->label);
6831 }
6832
6833 static inline const char *
6834 AT_string (dw_attr_ref a)
6835 {
6836   gcc_assert (a && AT_class (a) == dw_val_class_str);
6837   return a->dw_attr_val.v.val_str->str;
6838 }
6839
6840 /* Find out whether a string should be output inline in DIE
6841    or out-of-line in .debug_str section.  */
6842
6843 static enum dwarf_form
6844 AT_string_form (dw_attr_ref a)
6845 {
6846   struct indirect_string_node *node;
6847   unsigned int len;
6848
6849   gcc_assert (a && AT_class (a) == dw_val_class_str);
6850
6851   node = a->dw_attr_val.v.val_str;
6852   if (node->form)
6853     return node->form;
6854
6855   len = strlen (node->str) + 1;
6856
6857   /* If the string is shorter or equal to the size of the reference, it is
6858      always better to put it inline.  */
6859   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6860     return node->form = DW_FORM_string;
6861
6862   /* If we cannot expect the linker to merge strings in .debug_str
6863      section, only put it into .debug_str if it is worth even in this
6864      single module.  */
6865   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6866       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6867       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6868     return node->form = DW_FORM_string;
6869
6870   gen_label_for_indirect_string (node);
6871
6872   return node->form = DW_FORM_strp;
6873 }
6874
6875 /* Add a DIE reference attribute value to a DIE.  */
6876
6877 static inline void
6878 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6879 {
6880   dw_attr_node attr;
6881
6882   attr.dw_attr = attr_kind;
6883   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6884   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6885   attr.dw_attr_val.v.val_die_ref.external = 0;
6886   add_dwarf_attr (die, &attr);
6887 }
6888
6889 /* Add an AT_specification attribute to a DIE, and also make the back
6890    pointer from the specification to the definition.  */
6891
6892 static inline void
6893 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6894 {
6895   add_AT_die_ref (die, DW_AT_specification, targ_die);
6896   gcc_assert (!targ_die->die_definition);
6897   targ_die->die_definition = die;
6898 }
6899
6900 static inline dw_die_ref
6901 AT_ref (dw_attr_ref a)
6902 {
6903   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6904   return a->dw_attr_val.v.val_die_ref.die;
6905 }
6906
6907 static inline int
6908 AT_ref_external (dw_attr_ref a)
6909 {
6910   if (a && AT_class (a) == dw_val_class_die_ref)
6911     return a->dw_attr_val.v.val_die_ref.external;
6912
6913   return 0;
6914 }
6915
6916 static inline void
6917 set_AT_ref_external (dw_attr_ref a, int i)
6918 {
6919   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6920   a->dw_attr_val.v.val_die_ref.external = i;
6921 }
6922
6923 /* Add an FDE reference attribute value to a DIE.  */
6924
6925 static inline void
6926 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6927 {
6928   dw_attr_node attr;
6929
6930   attr.dw_attr = attr_kind;
6931   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6932   attr.dw_attr_val.v.val_fde_index = targ_fde;
6933   add_dwarf_attr (die, &attr);
6934 }
6935
6936 /* Add a location description attribute value to a DIE.  */
6937
6938 static inline void
6939 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6940 {
6941   dw_attr_node attr;
6942
6943   attr.dw_attr = attr_kind;
6944   attr.dw_attr_val.val_class = dw_val_class_loc;
6945   attr.dw_attr_val.v.val_loc = loc;
6946   add_dwarf_attr (die, &attr);
6947 }
6948
6949 static inline dw_loc_descr_ref
6950 AT_loc (dw_attr_ref a)
6951 {
6952   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6953   return a->dw_attr_val.v.val_loc;
6954 }
6955
6956 static inline void
6957 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
6958 {
6959   dw_attr_node attr;
6960
6961   attr.dw_attr = attr_kind;
6962   attr.dw_attr_val.val_class = dw_val_class_loc_list;
6963   attr.dw_attr_val.v.val_loc_list = loc_list;
6964   add_dwarf_attr (die, &attr);
6965   have_location_lists = true;
6966 }
6967
6968 static inline dw_loc_list_ref
6969 AT_loc_list (dw_attr_ref a)
6970 {
6971   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
6972   return a->dw_attr_val.v.val_loc_list;
6973 }
6974
6975 /* Add an address constant attribute value to a DIE.  */
6976
6977 static inline void
6978 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
6979 {
6980   dw_attr_node attr;
6981
6982   attr.dw_attr = attr_kind;
6983   attr.dw_attr_val.val_class = dw_val_class_addr;
6984   attr.dw_attr_val.v.val_addr = addr;
6985   add_dwarf_attr (die, &attr);
6986 }
6987
6988 /* Get the RTX from to an address DIE attribute.  */
6989
6990 static inline rtx
6991 AT_addr (dw_attr_ref a)
6992 {
6993   gcc_assert (a && AT_class (a) == dw_val_class_addr);
6994   return a->dw_attr_val.v.val_addr;
6995 }
6996
6997 /* Add a file attribute value to a DIE.  */
6998
6999 static inline void
7000 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7001              struct dwarf_file_data *fd)
7002 {
7003   dw_attr_node attr;
7004
7005   attr.dw_attr = attr_kind;
7006   attr.dw_attr_val.val_class = dw_val_class_file;
7007   attr.dw_attr_val.v.val_file = fd;
7008   add_dwarf_attr (die, &attr);
7009 }
7010
7011 /* Get the dwarf_file_data from a file DIE attribute.  */
7012
7013 static inline struct dwarf_file_data *
7014 AT_file (dw_attr_ref a)
7015 {
7016   gcc_assert (a && AT_class (a) == dw_val_class_file);
7017   return a->dw_attr_val.v.val_file;
7018 }
7019
7020 /* Add a label identifier attribute value to a DIE.  */
7021
7022 static inline void
7023 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7024 {
7025   dw_attr_node attr;
7026
7027   attr.dw_attr = attr_kind;
7028   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7029   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7030   add_dwarf_attr (die, &attr);
7031 }
7032
7033 /* Add a section offset attribute value to a DIE, an offset into the
7034    debug_line section.  */
7035
7036 static inline void
7037 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7038                 const char *label)
7039 {
7040   dw_attr_node attr;
7041
7042   attr.dw_attr = attr_kind;
7043   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7044   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7045   add_dwarf_attr (die, &attr);
7046 }
7047
7048 /* Add a section offset attribute value to a DIE, an offset into the
7049    debug_macinfo section.  */
7050
7051 static inline void
7052 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7053                const char *label)
7054 {
7055   dw_attr_node attr;
7056
7057   attr.dw_attr = attr_kind;
7058   attr.dw_attr_val.val_class = dw_val_class_macptr;
7059   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7060   add_dwarf_attr (die, &attr);
7061 }
7062
7063 /* Add an offset attribute value to a DIE.  */
7064
7065 static inline void
7066 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7067                unsigned HOST_WIDE_INT offset)
7068 {
7069   dw_attr_node attr;
7070
7071   attr.dw_attr = attr_kind;
7072   attr.dw_attr_val.val_class = dw_val_class_offset;
7073   attr.dw_attr_val.v.val_offset = offset;
7074   add_dwarf_attr (die, &attr);
7075 }
7076
7077 /* Add an range_list attribute value to a DIE.  */
7078
7079 static void
7080 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7081                    long unsigned int offset)
7082 {
7083   dw_attr_node attr;
7084
7085   attr.dw_attr = attr_kind;
7086   attr.dw_attr_val.val_class = dw_val_class_range_list;
7087   attr.dw_attr_val.v.val_offset = offset;
7088   add_dwarf_attr (die, &attr);
7089 }
7090
7091 static inline const char *
7092 AT_lbl (dw_attr_ref a)
7093 {
7094   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7095                     || AT_class (a) == dw_val_class_lineptr
7096                     || AT_class (a) == dw_val_class_macptr));
7097   return a->dw_attr_val.v.val_lbl_id;
7098 }
7099
7100 /* Get the attribute of type attr_kind.  */
7101
7102 static dw_attr_ref
7103 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7104 {
7105   dw_attr_ref a;
7106   unsigned ix;
7107   dw_die_ref spec = NULL;
7108
7109   if (! die)
7110     return NULL;
7111
7112   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7113     if (a->dw_attr == attr_kind)
7114       return a;
7115     else if (a->dw_attr == DW_AT_specification
7116              || a->dw_attr == DW_AT_abstract_origin)
7117       spec = AT_ref (a);
7118
7119   if (spec)
7120     return get_AT (spec, attr_kind);
7121
7122   return NULL;
7123 }
7124
7125 /* Return the "low pc" attribute value, typically associated with a subprogram
7126    DIE.  Return null if the "low pc" attribute is either not present, or if it
7127    cannot be represented as an assembler label identifier.  */
7128
7129 static inline const char *
7130 get_AT_low_pc (dw_die_ref die)
7131 {
7132   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7133
7134   return a ? AT_lbl (a) : NULL;
7135 }
7136
7137 /* Return the "high pc" attribute value, typically associated with a subprogram
7138    DIE.  Return null if the "high pc" attribute is either not present, or if it
7139    cannot be represented as an assembler label identifier.  */
7140
7141 static inline const char *
7142 get_AT_hi_pc (dw_die_ref die)
7143 {
7144   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7145
7146   return a ? AT_lbl (a) : NULL;
7147 }
7148
7149 /* Return the value of the string attribute designated by ATTR_KIND, or
7150    NULL if it is not present.  */
7151
7152 static inline const char *
7153 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7154 {
7155   dw_attr_ref a = get_AT (die, attr_kind);
7156
7157   return a ? AT_string (a) : NULL;
7158 }
7159
7160 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7161    if it is not present.  */
7162
7163 static inline int
7164 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7165 {
7166   dw_attr_ref a = get_AT (die, attr_kind);
7167
7168   return a ? AT_flag (a) : 0;
7169 }
7170
7171 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7172    if it is not present.  */
7173
7174 static inline unsigned
7175 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7176 {
7177   dw_attr_ref a = get_AT (die, attr_kind);
7178
7179   return a ? AT_unsigned (a) : 0;
7180 }
7181
7182 static inline dw_die_ref
7183 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7184 {
7185   dw_attr_ref a = get_AT (die, attr_kind);
7186
7187   return a ? AT_ref (a) : NULL;
7188 }
7189
7190 static inline struct dwarf_file_data *
7191 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7192 {
7193   dw_attr_ref a = get_AT (die, attr_kind);
7194
7195   return a ? AT_file (a) : NULL;
7196 }
7197
7198 /* Return TRUE if the language is C or C++.  */
7199
7200 static inline bool
7201 is_c_family (void)
7202 {
7203   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7204
7205   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7206           || lang == DW_LANG_C99
7207           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7208 }
7209
7210 /* Return TRUE if the language is C++.  */
7211
7212 static inline bool
7213 is_cxx (void)
7214 {
7215   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7216
7217   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7218 }
7219
7220 /* Return TRUE if the language is Fortran.  */
7221
7222 static inline bool
7223 is_fortran (void)
7224 {
7225   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7226
7227   return (lang == DW_LANG_Fortran77
7228           || lang == DW_LANG_Fortran90
7229           || lang == DW_LANG_Fortran95);
7230 }
7231
7232 /* Return TRUE if the language is Java.  */
7233
7234 static inline bool
7235 is_java (void)
7236 {
7237   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7238
7239   return lang == DW_LANG_Java;
7240 }
7241
7242 /* Return TRUE if the language is Ada.  */
7243
7244 static inline bool
7245 is_ada (void)
7246 {
7247   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7248
7249   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7250 }
7251
7252 /* Remove the specified attribute if present.  */
7253
7254 static void
7255 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7256 {
7257   dw_attr_ref a;
7258   unsigned ix;
7259
7260   if (! die)
7261     return;
7262
7263   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7264     if (a->dw_attr == attr_kind)
7265       {
7266         if (AT_class (a) == dw_val_class_str)
7267           if (a->dw_attr_val.v.val_str->refcount)
7268             a->dw_attr_val.v.val_str->refcount--;
7269
7270         /* VEC_ordered_remove should help reduce the number of abbrevs
7271            that are needed.  */
7272         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7273         return;
7274       }
7275 }
7276
7277 /* Remove CHILD from its parent.  PREV must have the property that
7278    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7279
7280 static void
7281 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7282 {
7283   gcc_assert (child->die_parent == prev->die_parent);
7284   gcc_assert (prev->die_sib == child);
7285   if (prev == child)
7286     {
7287       gcc_assert (child->die_parent->die_child == child);
7288       prev = NULL;
7289     }
7290   else
7291     prev->die_sib = child->die_sib;
7292   if (child->die_parent->die_child == child)
7293     child->die_parent->die_child = prev;
7294 }
7295
7296 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7297    matches TAG.  */
7298
7299 static void
7300 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7301 {
7302   dw_die_ref c;
7303
7304   c = die->die_child;
7305   if (c) do {
7306     dw_die_ref prev = c;
7307     c = c->die_sib;
7308     while (c->die_tag == tag)
7309       {
7310         remove_child_with_prev (c, prev);
7311         /* Might have removed every child.  */
7312         if (c == c->die_sib)
7313           return;
7314         c = c->die_sib;
7315       }
7316   } while (c != die->die_child);
7317 }
7318
7319 /* Add a CHILD_DIE as the last child of DIE.  */
7320
7321 static void
7322 add_child_die (dw_die_ref die, dw_die_ref child_die)
7323 {
7324   /* FIXME this should probably be an assert.  */
7325   if (! die || ! child_die)
7326     return;
7327   gcc_assert (die != child_die);
7328
7329   child_die->die_parent = die;
7330   if (die->die_child)
7331     {
7332       child_die->die_sib = die->die_child->die_sib;
7333       die->die_child->die_sib = child_die;
7334     }
7335   else
7336     child_die->die_sib = child_die;
7337   die->die_child = child_die;
7338 }
7339
7340 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7341    is the specification, to the end of PARENT's list of children.
7342    This is done by removing and re-adding it.  */
7343
7344 static void
7345 splice_child_die (dw_die_ref parent, dw_die_ref child)
7346 {
7347   dw_die_ref p;
7348
7349   /* We want the declaration DIE from inside the class, not the
7350      specification DIE at toplevel.  */
7351   if (child->die_parent != parent)
7352     {
7353       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7354
7355       if (tmp)
7356         child = tmp;
7357     }
7358
7359   gcc_assert (child->die_parent == parent
7360               || (child->die_parent
7361                   == get_AT_ref (parent, DW_AT_specification)));
7362
7363   for (p = child->die_parent->die_child; ; p = p->die_sib)
7364     if (p->die_sib == child)
7365       {
7366         remove_child_with_prev (child, p);
7367         break;
7368       }
7369
7370   add_child_die (parent, child);
7371 }
7372
7373 /* Return a pointer to a newly created DIE node.  */
7374
7375 static inline dw_die_ref
7376 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7377 {
7378   dw_die_ref die = GGC_CNEW (die_node);
7379
7380   die->die_tag = tag_value;
7381
7382   if (parent_die != NULL)
7383     add_child_die (parent_die, die);
7384   else
7385     {
7386       limbo_die_node *limbo_node;
7387
7388       limbo_node = GGC_CNEW (limbo_die_node);
7389       limbo_node->die = die;
7390       limbo_node->created_for = t;
7391       limbo_node->next = limbo_die_list;
7392       limbo_die_list = limbo_node;
7393     }
7394
7395   return die;
7396 }
7397
7398 /* Return the DIE associated with the given type specifier.  */
7399
7400 static inline dw_die_ref
7401 lookup_type_die (tree type)
7402 {
7403   return TYPE_SYMTAB_DIE (type);
7404 }
7405
7406 /* Equate a DIE to a given type specifier.  */
7407
7408 static inline void
7409 equate_type_number_to_die (tree type, dw_die_ref type_die)
7410 {
7411   TYPE_SYMTAB_DIE (type) = type_die;
7412 }
7413
7414 /* Returns a hash value for X (which really is a die_struct).  */
7415
7416 static hashval_t
7417 decl_die_table_hash (const void *x)
7418 {
7419   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7420 }
7421
7422 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7423
7424 static int
7425 decl_die_table_eq (const void *x, const void *y)
7426 {
7427   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7428 }
7429
7430 /* Return the DIE associated with a given declaration.  */
7431
7432 static inline dw_die_ref
7433 lookup_decl_die (tree decl)
7434 {
7435   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7436 }
7437
7438 /* Returns a hash value for X (which really is a var_loc_list).  */
7439
7440 static hashval_t
7441 decl_loc_table_hash (const void *x)
7442 {
7443   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7444 }
7445
7446 /* Return nonzero if decl_id of var_loc_list X is the same as
7447    UID of decl *Y.  */
7448
7449 static int
7450 decl_loc_table_eq (const void *x, const void *y)
7451 {
7452   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7453 }
7454
7455 /* Return the var_loc list associated with a given declaration.  */
7456
7457 static inline var_loc_list *
7458 lookup_decl_loc (const_tree decl)
7459 {
7460   return (var_loc_list *)
7461     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7462 }
7463
7464 /* Equate a DIE to a particular declaration.  */
7465
7466 static void
7467 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7468 {
7469   unsigned int decl_id = DECL_UID (decl);
7470   void **slot;
7471
7472   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7473   *slot = decl_die;
7474   decl_die->decl_id = decl_id;
7475 }
7476
7477 /* Add a variable location node to the linked list for DECL.  */
7478
7479 static void
7480 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7481 {
7482   unsigned int decl_id = DECL_UID (decl);
7483   var_loc_list *temp;
7484   void **slot;
7485
7486   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7487   if (*slot == NULL)
7488     {
7489       temp = GGC_CNEW (var_loc_list);
7490       temp->decl_id = decl_id;
7491       *slot = temp;
7492     }
7493   else
7494     temp = (var_loc_list *) *slot;
7495
7496   if (temp->last)
7497     {
7498       /* If the current location is the same as the end of the list,
7499          and either both or neither of the locations is uninitialized,
7500          we have nothing to do.  */
7501       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7502                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7503           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7504                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7505               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7506                    == VAR_INIT_STATUS_UNINITIALIZED)
7507                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7508                       == VAR_INIT_STATUS_UNINITIALIZED))))
7509         {
7510           /* Add LOC to the end of list and update LAST.  */
7511           temp->last->next = loc;
7512           temp->last = loc;
7513         }
7514     }
7515   /* Do not add empty location to the beginning of the list.  */
7516   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7517     {
7518       temp->first = loc;
7519       temp->last = loc;
7520     }
7521 }
7522 \f
7523 /* Keep track of the number of spaces used to indent the
7524    output of the debugging routines that print the structure of
7525    the DIE internal representation.  */
7526 static int print_indent;
7527
7528 /* Indent the line the number of spaces given by print_indent.  */
7529
7530 static inline void
7531 print_spaces (FILE *outfile)
7532 {
7533   fprintf (outfile, "%*s", print_indent, "");
7534 }
7535
7536 /* Print the information associated with a given DIE, and its children.
7537    This routine is a debugging aid only.  */
7538
7539 static void
7540 print_die (dw_die_ref die, FILE *outfile)
7541 {
7542   dw_attr_ref a;
7543   dw_die_ref c;
7544   unsigned ix;
7545
7546   print_spaces (outfile);
7547   fprintf (outfile, "DIE %4ld: %s\n",
7548            die->die_offset, dwarf_tag_name (die->die_tag));
7549   print_spaces (outfile);
7550   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7551   fprintf (outfile, " offset: %ld\n", die->die_offset);
7552
7553   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7554     {
7555       print_spaces (outfile);
7556       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7557
7558       switch (AT_class (a))
7559         {
7560         case dw_val_class_addr:
7561           fprintf (outfile, "address");
7562           break;
7563         case dw_val_class_offset:
7564           fprintf (outfile, "offset");
7565           break;
7566         case dw_val_class_loc:
7567           fprintf (outfile, "location descriptor");
7568           break;
7569         case dw_val_class_loc_list:
7570           fprintf (outfile, "location list -> label:%s",
7571                    AT_loc_list (a)->ll_symbol);
7572           break;
7573         case dw_val_class_range_list:
7574           fprintf (outfile, "range list");
7575           break;
7576         case dw_val_class_const:
7577           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7578           break;
7579         case dw_val_class_unsigned_const:
7580           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7581           break;
7582         case dw_val_class_long_long:
7583           fprintf (outfile, "constant (" HOST_WIDE_INT_PRINT_UNSIGNED
7584                             "," HOST_WIDE_INT_PRINT_UNSIGNED ")",
7585                    CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long),
7586                    CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long));
7587           break;
7588         case dw_val_class_vec:
7589           fprintf (outfile, "floating-point or vector constant");
7590           break;
7591         case dw_val_class_flag:
7592           fprintf (outfile, "%u", AT_flag (a));
7593           break;
7594         case dw_val_class_die_ref:
7595           if (AT_ref (a) != NULL)
7596             {
7597               if (AT_ref (a)->die_symbol)
7598                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7599               else
7600                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7601             }
7602           else
7603             fprintf (outfile, "die -> <null>");
7604           break;
7605         case dw_val_class_lbl_id:
7606         case dw_val_class_lineptr:
7607         case dw_val_class_macptr:
7608           fprintf (outfile, "label: %s", AT_lbl (a));
7609           break;
7610         case dw_val_class_str:
7611           if (AT_string (a) != NULL)
7612             fprintf (outfile, "\"%s\"", AT_string (a));
7613           else
7614             fprintf (outfile, "<null>");
7615           break;
7616         case dw_val_class_file:
7617           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7618                    AT_file (a)->emitted_number);
7619           break;
7620         default:
7621           break;
7622         }
7623
7624       fprintf (outfile, "\n");
7625     }
7626
7627   if (die->die_child != NULL)
7628     {
7629       print_indent += 4;
7630       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7631       print_indent -= 4;
7632     }
7633   if (print_indent == 0)
7634     fprintf (outfile, "\n");
7635 }
7636
7637 /* Print the contents of the source code line number correspondence table.
7638    This routine is a debugging aid only.  */
7639
7640 static void
7641 print_dwarf_line_table (FILE *outfile)
7642 {
7643   unsigned i;
7644   dw_line_info_ref line_info;
7645
7646   fprintf (outfile, "\n\nDWARF source line information\n");
7647   for (i = 1; i < line_info_table_in_use; i++)
7648     {
7649       line_info = &line_info_table[i];
7650       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7651                line_info->dw_file_num,
7652                line_info->dw_line_num);
7653     }
7654
7655   fprintf (outfile, "\n\n");
7656 }
7657
7658 /* Print the information collected for a given DIE.  */
7659
7660 void
7661 debug_dwarf_die (dw_die_ref die)
7662 {
7663   print_die (die, stderr);
7664 }
7665
7666 /* Print all DWARF information collected for the compilation unit.
7667    This routine is a debugging aid only.  */
7668
7669 void
7670 debug_dwarf (void)
7671 {
7672   print_indent = 0;
7673   print_die (comp_unit_die, stderr);
7674   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7675     print_dwarf_line_table (stderr);
7676 }
7677 \f
7678 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7679    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7680    DIE that marks the start of the DIEs for this include file.  */
7681
7682 static dw_die_ref
7683 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7684 {
7685   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7686   dw_die_ref new_unit = gen_compile_unit_die (filename);
7687
7688   new_unit->die_sib = old_unit;
7689   return new_unit;
7690 }
7691
7692 /* Close an include-file CU and reopen the enclosing one.  */
7693
7694 static dw_die_ref
7695 pop_compile_unit (dw_die_ref old_unit)
7696 {
7697   dw_die_ref new_unit = old_unit->die_sib;
7698
7699   old_unit->die_sib = NULL;
7700   return new_unit;
7701 }
7702
7703 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7704 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7705
7706 /* Calculate the checksum of a location expression.  */
7707
7708 static inline void
7709 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7710 {
7711   int tem;
7712
7713   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7714   CHECKSUM (tem);
7715   CHECKSUM (loc->dw_loc_oprnd1);
7716   CHECKSUM (loc->dw_loc_oprnd2);
7717 }
7718
7719 /* Calculate the checksum of an attribute.  */
7720
7721 static void
7722 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7723 {
7724   dw_loc_descr_ref loc;
7725   rtx r;
7726
7727   CHECKSUM (at->dw_attr);
7728
7729   /* We don't care that this was compiled with a different compiler
7730      snapshot; if the output is the same, that's what matters.  */
7731   if (at->dw_attr == DW_AT_producer)
7732     return;
7733
7734   switch (AT_class (at))
7735     {
7736     case dw_val_class_const:
7737       CHECKSUM (at->dw_attr_val.v.val_int);
7738       break;
7739     case dw_val_class_unsigned_const:
7740       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7741       break;
7742     case dw_val_class_long_long:
7743       CHECKSUM (CONST_DOUBLE_HIGH (at->dw_attr_val.v.val_long_long));
7744       CHECKSUM (CONST_DOUBLE_LOW (at->dw_attr_val.v.val_long_long));
7745       break;
7746     case dw_val_class_vec:
7747       CHECKSUM (at->dw_attr_val.v.val_vec);
7748       break;
7749     case dw_val_class_flag:
7750       CHECKSUM (at->dw_attr_val.v.val_flag);
7751       break;
7752     case dw_val_class_str:
7753       CHECKSUM_STRING (AT_string (at));
7754       break;
7755
7756     case dw_val_class_addr:
7757       r = AT_addr (at);
7758       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7759       CHECKSUM_STRING (XSTR (r, 0));
7760       break;
7761
7762     case dw_val_class_offset:
7763       CHECKSUM (at->dw_attr_val.v.val_offset);
7764       break;
7765
7766     case dw_val_class_loc:
7767       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7768         loc_checksum (loc, ctx);
7769       break;
7770
7771     case dw_val_class_die_ref:
7772       die_checksum (AT_ref (at), ctx, mark);
7773       break;
7774
7775     case dw_val_class_fde_ref:
7776     case dw_val_class_lbl_id:
7777     case dw_val_class_lineptr:
7778     case dw_val_class_macptr:
7779       break;
7780
7781     case dw_val_class_file:
7782       CHECKSUM_STRING (AT_file (at)->filename);
7783       break;
7784
7785     default:
7786       break;
7787     }
7788 }
7789
7790 /* Calculate the checksum of a DIE.  */
7791
7792 static void
7793 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7794 {
7795   dw_die_ref c;
7796   dw_attr_ref a;
7797   unsigned ix;
7798
7799   /* To avoid infinite recursion.  */
7800   if (die->die_mark)
7801     {
7802       CHECKSUM (die->die_mark);
7803       return;
7804     }
7805   die->die_mark = ++(*mark);
7806
7807   CHECKSUM (die->die_tag);
7808
7809   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7810     attr_checksum (a, ctx, mark);
7811
7812   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7813 }
7814
7815 #undef CHECKSUM
7816 #undef CHECKSUM_STRING
7817
7818 /* Do the location expressions look same?  */
7819 static inline int
7820 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7821 {
7822   return loc1->dw_loc_opc == loc2->dw_loc_opc
7823          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7824          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7825 }
7826
7827 /* Do the values look the same?  */
7828 static int
7829 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7830 {
7831   dw_loc_descr_ref loc1, loc2;
7832   rtx r1, r2;
7833
7834   if (v1->val_class != v2->val_class)
7835     return 0;
7836
7837   switch (v1->val_class)
7838     {
7839     case dw_val_class_const:
7840       return v1->v.val_int == v2->v.val_int;
7841     case dw_val_class_unsigned_const:
7842       return v1->v.val_unsigned == v2->v.val_unsigned;
7843     case dw_val_class_long_long:
7844       return CONST_DOUBLE_HIGH (v1->v.val_long_long)
7845              == CONST_DOUBLE_HIGH (v2->v.val_long_long)
7846              && CONST_DOUBLE_LOW (v1->v.val_long_long)
7847                 == CONST_DOUBLE_LOW (v2->v.val_long_long);
7848     case dw_val_class_vec:
7849       if (v1->v.val_vec.length != v2->v.val_vec.length
7850           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7851         return 0;
7852       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7853                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7854         return 0;
7855       return 1;
7856     case dw_val_class_flag:
7857       return v1->v.val_flag == v2->v.val_flag;
7858     case dw_val_class_str:
7859       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7860
7861     case dw_val_class_addr:
7862       r1 = v1->v.val_addr;
7863       r2 = v2->v.val_addr;
7864       if (GET_CODE (r1) != GET_CODE (r2))
7865         return 0;
7866       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
7867       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
7868
7869     case dw_val_class_offset:
7870       return v1->v.val_offset == v2->v.val_offset;
7871
7872     case dw_val_class_loc:
7873       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7874            loc1 && loc2;
7875            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7876         if (!same_loc_p (loc1, loc2, mark))
7877           return 0;
7878       return !loc1 && !loc2;
7879
7880     case dw_val_class_die_ref:
7881       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7882
7883     case dw_val_class_fde_ref:
7884     case dw_val_class_lbl_id:
7885     case dw_val_class_lineptr:
7886     case dw_val_class_macptr:
7887       return 1;
7888
7889     case dw_val_class_file:
7890       return v1->v.val_file == v2->v.val_file;
7891
7892     default:
7893       return 1;
7894     }
7895 }
7896
7897 /* Do the attributes look the same?  */
7898
7899 static int
7900 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7901 {
7902   if (at1->dw_attr != at2->dw_attr)
7903     return 0;
7904
7905   /* We don't care that this was compiled with a different compiler
7906      snapshot; if the output is the same, that's what matters. */
7907   if (at1->dw_attr == DW_AT_producer)
7908     return 1;
7909
7910   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7911 }
7912
7913 /* Do the dies look the same?  */
7914
7915 static int
7916 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7917 {
7918   dw_die_ref c1, c2;
7919   dw_attr_ref a1;
7920   unsigned ix;
7921
7922   /* To avoid infinite recursion.  */
7923   if (die1->die_mark)
7924     return die1->die_mark == die2->die_mark;
7925   die1->die_mark = die2->die_mark = ++(*mark);
7926
7927   if (die1->die_tag != die2->die_tag)
7928     return 0;
7929
7930   if (VEC_length (dw_attr_node, die1->die_attr)
7931       != VEC_length (dw_attr_node, die2->die_attr))
7932     return 0;
7933
7934   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7935     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7936       return 0;
7937
7938   c1 = die1->die_child;
7939   c2 = die2->die_child;
7940   if (! c1)
7941     {
7942       if (c2)
7943         return 0;
7944     }
7945   else
7946     for (;;)
7947       {
7948         if (!same_die_p (c1, c2, mark))
7949           return 0;
7950         c1 = c1->die_sib;
7951         c2 = c2->die_sib;
7952         if (c1 == die1->die_child)
7953           {
7954             if (c2 == die2->die_child)
7955               break;
7956             else
7957               return 0;
7958           }
7959     }
7960
7961   return 1;
7962 }
7963
7964 /* Do the dies look the same?  Wrapper around same_die_p.  */
7965
7966 static int
7967 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
7968 {
7969   int mark = 0;
7970   int ret = same_die_p (die1, die2, &mark);
7971
7972   unmark_all_dies (die1);
7973   unmark_all_dies (die2);
7974
7975   return ret;
7976 }
7977
7978 /* The prefix to attach to symbols on DIEs in the current comdat debug
7979    info section.  */
7980 static char *comdat_symbol_id;
7981
7982 /* The index of the current symbol within the current comdat CU.  */
7983 static unsigned int comdat_symbol_number;
7984
7985 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
7986    children, and set comdat_symbol_id accordingly.  */
7987
7988 static void
7989 compute_section_prefix (dw_die_ref unit_die)
7990 {
7991   const char *die_name = get_AT_string (unit_die, DW_AT_name);
7992   const char *base = die_name ? lbasename (die_name) : "anonymous";
7993   char *name = XALLOCAVEC (char, strlen (base) + 64);
7994   char *p;
7995   int i, mark;
7996   unsigned char checksum[16];
7997   struct md5_ctx ctx;
7998
7999   /* Compute the checksum of the DIE, then append part of it as hex digits to
8000      the name filename of the unit.  */
8001
8002   md5_init_ctx (&ctx);
8003   mark = 0;
8004   die_checksum (unit_die, &ctx, &mark);
8005   unmark_all_dies (unit_die);
8006   md5_finish_ctx (&ctx, checksum);
8007
8008   sprintf (name, "%s.", base);
8009   clean_symbol_name (name);
8010
8011   p = name + strlen (name);
8012   for (i = 0; i < 4; i++)
8013     {
8014       sprintf (p, "%.2x", checksum[i]);
8015       p += 2;
8016     }
8017
8018   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
8019   comdat_symbol_number = 0;
8020 }
8021
8022 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8023
8024 static int
8025 is_type_die (dw_die_ref die)
8026 {
8027   switch (die->die_tag)
8028     {
8029     case DW_TAG_array_type:
8030     case DW_TAG_class_type:
8031     case DW_TAG_interface_type:
8032     case DW_TAG_enumeration_type:
8033     case DW_TAG_pointer_type:
8034     case DW_TAG_reference_type:
8035     case DW_TAG_string_type:
8036     case DW_TAG_structure_type:
8037     case DW_TAG_subroutine_type:
8038     case DW_TAG_union_type:
8039     case DW_TAG_ptr_to_member_type:
8040     case DW_TAG_set_type:
8041     case DW_TAG_subrange_type:
8042     case DW_TAG_base_type:
8043     case DW_TAG_const_type:
8044     case DW_TAG_file_type:
8045     case DW_TAG_packed_type:
8046     case DW_TAG_volatile_type:
8047     case DW_TAG_typedef:
8048       return 1;
8049     default:
8050       return 0;
8051     }
8052 }
8053
8054 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8055    Basically, we want to choose the bits that are likely to be shared between
8056    compilations (types) and leave out the bits that are specific to individual
8057    compilations (functions).  */
8058
8059 static int
8060 is_comdat_die (dw_die_ref c)
8061 {
8062   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8063      we do for stabs.  The advantage is a greater likelihood of sharing between
8064      objects that don't include headers in the same order (and therefore would
8065      put the base types in a different comdat).  jason 8/28/00 */
8066
8067   if (c->die_tag == DW_TAG_base_type)
8068     return 0;
8069
8070   if (c->die_tag == DW_TAG_pointer_type
8071       || c->die_tag == DW_TAG_reference_type
8072       || c->die_tag == DW_TAG_const_type
8073       || c->die_tag == DW_TAG_volatile_type)
8074     {
8075       dw_die_ref t = get_AT_ref (c, DW_AT_type);
8076
8077       return t ? is_comdat_die (t) : 0;
8078     }
8079
8080   return is_type_die (c);
8081 }
8082
8083 /* Returns 1 iff C is the sort of DIE that might be referred to from another
8084    compilation unit.  */
8085
8086 static int
8087 is_symbol_die (dw_die_ref c)
8088 {
8089   return (is_type_die (c)
8090           || (get_AT (c, DW_AT_declaration)
8091               && !get_AT (c, DW_AT_specification))
8092           || c->die_tag == DW_TAG_namespace
8093           || c->die_tag == DW_TAG_module);
8094 }
8095
8096 static char *
8097 gen_internal_sym (const char *prefix)
8098 {
8099   char buf[256];
8100
8101   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8102   return xstrdup (buf);
8103 }
8104
8105 /* Assign symbols to all worthy DIEs under DIE.  */
8106
8107 static void
8108 assign_symbol_names (dw_die_ref die)
8109 {
8110   dw_die_ref c;
8111
8112   if (is_symbol_die (die))
8113     {
8114       if (comdat_symbol_id)
8115         {
8116           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8117
8118           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8119                    comdat_symbol_id, comdat_symbol_number++);
8120           die->die_symbol = xstrdup (p);
8121         }
8122       else
8123         die->die_symbol = gen_internal_sym ("LDIE");
8124     }
8125
8126   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8127 }
8128
8129 struct cu_hash_table_entry
8130 {
8131   dw_die_ref cu;
8132   unsigned min_comdat_num, max_comdat_num;
8133   struct cu_hash_table_entry *next;
8134 };
8135
8136 /* Routines to manipulate hash table of CUs.  */
8137 static hashval_t
8138 htab_cu_hash (const void *of)
8139 {
8140   const struct cu_hash_table_entry *const entry =
8141     (const struct cu_hash_table_entry *) of;
8142
8143   return htab_hash_string (entry->cu->die_symbol);
8144 }
8145
8146 static int
8147 htab_cu_eq (const void *of1, const void *of2)
8148 {
8149   const struct cu_hash_table_entry *const entry1 =
8150     (const struct cu_hash_table_entry *) of1;
8151   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8152
8153   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8154 }
8155
8156 static void
8157 htab_cu_del (void *what)
8158 {
8159   struct cu_hash_table_entry *next,
8160     *entry = (struct cu_hash_table_entry *) what;
8161
8162   while (entry)
8163     {
8164       next = entry->next;
8165       free (entry);
8166       entry = next;
8167     }
8168 }
8169
8170 /* Check whether we have already seen this CU and set up SYM_NUM
8171    accordingly.  */
8172 static int
8173 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8174 {
8175   struct cu_hash_table_entry dummy;
8176   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8177
8178   dummy.max_comdat_num = 0;
8179
8180   slot = (struct cu_hash_table_entry **)
8181     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8182         INSERT);
8183   entry = *slot;
8184
8185   for (; entry; last = entry, entry = entry->next)
8186     {
8187       if (same_die_p_wrap (cu, entry->cu))
8188         break;
8189     }
8190
8191   if (entry)
8192     {
8193       *sym_num = entry->min_comdat_num;
8194       return 1;
8195     }
8196
8197   entry = XCNEW (struct cu_hash_table_entry);
8198   entry->cu = cu;
8199   entry->min_comdat_num = *sym_num = last->max_comdat_num;
8200   entry->next = *slot;
8201   *slot = entry;
8202
8203   return 0;
8204 }
8205
8206 /* Record SYM_NUM to record of CU in HTABLE.  */
8207 static void
8208 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8209 {
8210   struct cu_hash_table_entry **slot, *entry;
8211
8212   slot = (struct cu_hash_table_entry **)
8213     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8214         NO_INSERT);
8215   entry = *slot;
8216
8217   entry->max_comdat_num = sym_num;
8218 }
8219
8220 /* Traverse the DIE (which is always comp_unit_die), and set up
8221    additional compilation units for each of the include files we see
8222    bracketed by BINCL/EINCL.  */
8223
8224 static void
8225 break_out_includes (dw_die_ref die)
8226 {
8227   dw_die_ref c;
8228   dw_die_ref unit = NULL;
8229   limbo_die_node *node, **pnode;
8230   htab_t cu_hash_table;
8231
8232   c = die->die_child;
8233   if (c) do {
8234     dw_die_ref prev = c;
8235     c = c->die_sib;
8236     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8237            || (unit && is_comdat_die (c)))
8238       {
8239         dw_die_ref next = c->die_sib;
8240
8241         /* This DIE is for a secondary CU; remove it from the main one.  */
8242         remove_child_with_prev (c, prev);
8243
8244         if (c->die_tag == DW_TAG_GNU_BINCL)
8245           unit = push_new_compile_unit (unit, c);
8246         else if (c->die_tag == DW_TAG_GNU_EINCL)
8247           unit = pop_compile_unit (unit);
8248         else
8249           add_child_die (unit, c);
8250         c = next;
8251         if (c == die->die_child)
8252           break;
8253       }
8254   } while (c != die->die_child);
8255
8256 #if 0
8257   /* We can only use this in debugging, since the frontend doesn't check
8258      to make sure that we leave every include file we enter.  */
8259   gcc_assert (!unit);
8260 #endif
8261
8262   assign_symbol_names (die);
8263   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8264   for (node = limbo_die_list, pnode = &limbo_die_list;
8265        node;
8266        node = node->next)
8267     {
8268       int is_dupl;
8269
8270       compute_section_prefix (node->die);
8271       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8272                         &comdat_symbol_number);
8273       assign_symbol_names (node->die);
8274       if (is_dupl)
8275         *pnode = node->next;
8276       else
8277         {
8278           pnode = &node->next;
8279           record_comdat_symbol_number (node->die, cu_hash_table,
8280                 comdat_symbol_number);
8281         }
8282     }
8283   htab_delete (cu_hash_table);
8284 }
8285
8286 /* Traverse the DIE and add a sibling attribute if it may have the
8287    effect of speeding up access to siblings.  To save some space,
8288    avoid generating sibling attributes for DIE's without children.  */
8289
8290 static void
8291 add_sibling_attributes (dw_die_ref die)
8292 {
8293   dw_die_ref c;
8294
8295   if (! die->die_child)
8296     return;
8297
8298   if (die->die_parent && die != die->die_parent->die_child)
8299     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8300
8301   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8302 }
8303
8304 /* Output all location lists for the DIE and its children.  */
8305
8306 static void
8307 output_location_lists (dw_die_ref die)
8308 {
8309   dw_die_ref c;
8310   dw_attr_ref a;
8311   unsigned ix;
8312
8313   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8314     if (AT_class (a) == dw_val_class_loc_list)
8315       output_loc_list (AT_loc_list (a));
8316
8317   FOR_EACH_CHILD (die, c, output_location_lists (c));
8318 }
8319
8320 /* The format of each DIE (and its attribute value pairs) is encoded in an
8321    abbreviation table.  This routine builds the abbreviation table and assigns
8322    a unique abbreviation id for each abbreviation entry.  The children of each
8323    die are visited recursively.  */
8324
8325 static void
8326 build_abbrev_table (dw_die_ref die)
8327 {
8328   unsigned long abbrev_id;
8329   unsigned int n_alloc;
8330   dw_die_ref c;
8331   dw_attr_ref a;
8332   unsigned ix;
8333
8334   /* Scan the DIE references, and mark as external any that refer to
8335      DIEs from other CUs (i.e. those which are not marked).  */
8336   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8337     if (AT_class (a) == dw_val_class_die_ref
8338         && AT_ref (a)->die_mark == 0)
8339       {
8340         gcc_assert (AT_ref (a)->die_symbol);
8341         set_AT_ref_external (a, 1);
8342       }
8343
8344   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8345     {
8346       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8347       dw_attr_ref die_a, abbrev_a;
8348       unsigned ix;
8349       bool ok = true;
8350
8351       if (abbrev->die_tag != die->die_tag)
8352         continue;
8353       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8354         continue;
8355
8356       if (VEC_length (dw_attr_node, abbrev->die_attr)
8357           != VEC_length (dw_attr_node, die->die_attr))
8358         continue;
8359
8360       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8361         {
8362           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8363           if ((abbrev_a->dw_attr != die_a->dw_attr)
8364               || (value_format (abbrev_a) != value_format (die_a)))
8365             {
8366               ok = false;
8367               break;
8368             }
8369         }
8370       if (ok)
8371         break;
8372     }
8373
8374   if (abbrev_id >= abbrev_die_table_in_use)
8375     {
8376       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8377         {
8378           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8379           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8380                                             n_alloc);
8381
8382           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8383                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8384           abbrev_die_table_allocated = n_alloc;
8385         }
8386
8387       ++abbrev_die_table_in_use;
8388       abbrev_die_table[abbrev_id] = die;
8389     }
8390
8391   die->die_abbrev = abbrev_id;
8392   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8393 }
8394 \f
8395 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8396
8397 static int
8398 constant_size (unsigned HOST_WIDE_INT value)
8399 {
8400   int log;
8401
8402   if (value == 0)
8403     log = 0;
8404   else
8405     log = floor_log2 (value);
8406
8407   log = log / 8;
8408   log = 1 << (floor_log2 (log) + 1);
8409
8410   return log;
8411 }
8412
8413 /* Return the size of a DIE as it is represented in the
8414    .debug_info section.  */
8415
8416 static unsigned long
8417 size_of_die (dw_die_ref die)
8418 {
8419   unsigned long size = 0;
8420   dw_attr_ref a;
8421   unsigned ix;
8422
8423   size += size_of_uleb128 (die->die_abbrev);
8424   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8425     {
8426       switch (AT_class (a))
8427         {
8428         case dw_val_class_addr:
8429           size += DWARF2_ADDR_SIZE;
8430           break;
8431         case dw_val_class_offset:
8432           size += DWARF_OFFSET_SIZE;
8433           break;
8434         case dw_val_class_loc:
8435           {
8436             unsigned long lsize = size_of_locs (AT_loc (a));
8437
8438             /* Block length.  */
8439             size += constant_size (lsize);
8440             size += lsize;
8441           }
8442           break;
8443         case dw_val_class_loc_list:
8444           size += DWARF_OFFSET_SIZE;
8445           break;
8446         case dw_val_class_range_list:
8447           size += DWARF_OFFSET_SIZE;
8448           break;
8449         case dw_val_class_const:
8450           size += size_of_sleb128 (AT_int (a));
8451           break;
8452         case dw_val_class_unsigned_const:
8453           size += constant_size (AT_unsigned (a));
8454           break;
8455         case dw_val_class_long_long:
8456           size += 1 + 2*HOST_BITS_PER_WIDE_INT/HOST_BITS_PER_CHAR; /* block */
8457           break;
8458         case dw_val_class_vec:
8459           size += constant_size (a->dw_attr_val.v.val_vec.length
8460                                  * a->dw_attr_val.v.val_vec.elt_size)
8461                   + a->dw_attr_val.v.val_vec.length
8462                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8463           break;
8464         case dw_val_class_flag:
8465           size += 1;
8466           break;
8467         case dw_val_class_die_ref:
8468           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8469              whereas in DWARF3 it's always sized as an offset.  */
8470           if (AT_ref_external (a) && dwarf_version == 2)
8471             size += DWARF2_ADDR_SIZE;
8472           else
8473             size += DWARF_OFFSET_SIZE;
8474           break;
8475         case dw_val_class_fde_ref:
8476           size += DWARF_OFFSET_SIZE;
8477           break;
8478         case dw_val_class_lbl_id:
8479           size += DWARF2_ADDR_SIZE;
8480           break;
8481         case dw_val_class_lineptr:
8482         case dw_val_class_macptr:
8483           size += DWARF_OFFSET_SIZE;
8484           break;
8485         case dw_val_class_str:
8486           if (AT_string_form (a) == DW_FORM_strp)
8487             size += DWARF_OFFSET_SIZE;
8488           else
8489             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8490           break;
8491         case dw_val_class_file:
8492           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8493           break;
8494         default:
8495           gcc_unreachable ();
8496         }
8497     }
8498
8499   return size;
8500 }
8501
8502 /* Size the debugging information associated with a given DIE.  Visits the
8503    DIE's children recursively.  Updates the global variable next_die_offset, on
8504    each time through.  Uses the current value of next_die_offset to update the
8505    die_offset field in each DIE.  */
8506
8507 static void
8508 calc_die_sizes (dw_die_ref die)
8509 {
8510   dw_die_ref c;
8511
8512   die->die_offset = next_die_offset;
8513   next_die_offset += size_of_die (die);
8514
8515   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8516
8517   if (die->die_child != NULL)
8518     /* Count the null byte used to terminate sibling lists.  */
8519     next_die_offset += 1;
8520 }
8521
8522 /* Set the marks for a die and its children.  We do this so
8523    that we know whether or not a reference needs to use FORM_ref_addr; only
8524    DIEs in the same CU will be marked.  We used to clear out the offset
8525    and use that as the flag, but ran into ordering problems.  */
8526
8527 static void
8528 mark_dies (dw_die_ref die)
8529 {
8530   dw_die_ref c;
8531
8532   gcc_assert (!die->die_mark);
8533
8534   die->die_mark = 1;
8535   FOR_EACH_CHILD (die, c, mark_dies (c));
8536 }
8537
8538 /* Clear the marks for a die and its children.  */
8539
8540 static void
8541 unmark_dies (dw_die_ref die)
8542 {
8543   dw_die_ref c;
8544
8545   gcc_assert (die->die_mark);
8546
8547   die->die_mark = 0;
8548   FOR_EACH_CHILD (die, c, unmark_dies (c));
8549 }
8550
8551 /* Clear the marks for a die, its children and referred dies.  */
8552
8553 static void
8554 unmark_all_dies (dw_die_ref die)
8555 {
8556   dw_die_ref c;
8557   dw_attr_ref a;
8558   unsigned ix;
8559
8560   if (!die->die_mark)
8561     return;
8562   die->die_mark = 0;
8563
8564   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8565
8566   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8567     if (AT_class (a) == dw_val_class_die_ref)
8568       unmark_all_dies (AT_ref (a));
8569 }
8570
8571 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8572    generated for the compilation unit.  */
8573
8574 static unsigned long
8575 size_of_pubnames (VEC (pubname_entry, gc) * names)
8576 {
8577   unsigned long size;
8578   unsigned i;
8579   pubname_ref p;
8580
8581   size = DWARF_PUBNAMES_HEADER_SIZE;
8582   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8583     if (names != pubtype_table
8584         || p->die->die_offset != 0
8585         || !flag_eliminate_unused_debug_types)
8586       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8587
8588   size += DWARF_OFFSET_SIZE;
8589   return size;
8590 }
8591
8592 /* Return the size of the information in the .debug_aranges section.  */
8593
8594 static unsigned long
8595 size_of_aranges (void)
8596 {
8597   unsigned long size;
8598
8599   size = DWARF_ARANGES_HEADER_SIZE;
8600
8601   /* Count the address/length pair for this compilation unit.  */
8602   if (text_section_used)
8603     size += 2 * DWARF2_ADDR_SIZE;
8604   if (cold_text_section_used)
8605     size += 2 * DWARF2_ADDR_SIZE;
8606   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8607
8608   /* Count the two zero words used to terminated the address range table.  */
8609   size += 2 * DWARF2_ADDR_SIZE;
8610   return size;
8611 }
8612 \f
8613 /* Select the encoding of an attribute value.  */
8614
8615 static enum dwarf_form
8616 value_format (dw_attr_ref a)
8617 {
8618   switch (a->dw_attr_val.val_class)
8619     {
8620     case dw_val_class_addr:
8621       return DW_FORM_addr;
8622     case dw_val_class_range_list:
8623     case dw_val_class_offset:
8624     case dw_val_class_loc_list:
8625       switch (DWARF_OFFSET_SIZE)
8626         {
8627         case 4:
8628           return DW_FORM_data4;
8629         case 8:
8630           return DW_FORM_data8;
8631         default:
8632           gcc_unreachable ();
8633         }
8634     case dw_val_class_loc:
8635       switch (constant_size (size_of_locs (AT_loc (a))))
8636         {
8637         case 1:
8638           return DW_FORM_block1;
8639         case 2:
8640           return DW_FORM_block2;
8641         default:
8642           gcc_unreachable ();
8643         }
8644     case dw_val_class_const:
8645       return DW_FORM_sdata;
8646     case dw_val_class_unsigned_const:
8647       switch (constant_size (AT_unsigned (a)))
8648         {
8649         case 1:
8650           return DW_FORM_data1;
8651         case 2:
8652           return DW_FORM_data2;
8653         case 4:
8654           return DW_FORM_data4;
8655         case 8:
8656           return DW_FORM_data8;
8657         default:
8658           gcc_unreachable ();
8659         }
8660     case dw_val_class_long_long:
8661       return DW_FORM_block1;
8662     case dw_val_class_vec:
8663       switch (constant_size (a->dw_attr_val.v.val_vec.length
8664                              * a->dw_attr_val.v.val_vec.elt_size))
8665         {
8666         case 1:
8667           return DW_FORM_block1;
8668         case 2:
8669           return DW_FORM_block2;
8670         case 4:
8671           return DW_FORM_block4;
8672         default:
8673           gcc_unreachable ();
8674         }
8675     case dw_val_class_flag:
8676       return DW_FORM_flag;
8677     case dw_val_class_die_ref:
8678       if (AT_ref_external (a))
8679         return DW_FORM_ref_addr;
8680       else
8681         return DW_FORM_ref;
8682     case dw_val_class_fde_ref:
8683       return DW_FORM_data;
8684     case dw_val_class_lbl_id:
8685       return DW_FORM_addr;
8686     case dw_val_class_lineptr:
8687     case dw_val_class_macptr:
8688       return DW_FORM_data;
8689     case dw_val_class_str:
8690       return AT_string_form (a);
8691     case dw_val_class_file:
8692       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8693         {
8694         case 1:
8695           return DW_FORM_data1;
8696         case 2:
8697           return DW_FORM_data2;
8698         case 4:
8699           return DW_FORM_data4;
8700         default:
8701           gcc_unreachable ();
8702         }
8703
8704     default:
8705       gcc_unreachable ();
8706     }
8707 }
8708
8709 /* Output the encoding of an attribute value.  */
8710
8711 static void
8712 output_value_format (dw_attr_ref a)
8713 {
8714   enum dwarf_form form = value_format (a);
8715
8716   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8717 }
8718
8719 /* Output the .debug_abbrev section which defines the DIE abbreviation
8720    table.  */
8721
8722 static void
8723 output_abbrev_section (void)
8724 {
8725   unsigned long abbrev_id;
8726
8727   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8728     {
8729       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8730       unsigned ix;
8731       dw_attr_ref a_attr;
8732
8733       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8734       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8735                                    dwarf_tag_name (abbrev->die_tag));
8736
8737       if (abbrev->die_child != NULL)
8738         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8739       else
8740         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8741
8742       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8743            ix++)
8744         {
8745           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8746                                        dwarf_attr_name (a_attr->dw_attr));
8747           output_value_format (a_attr);
8748         }
8749
8750       dw2_asm_output_data (1, 0, NULL);
8751       dw2_asm_output_data (1, 0, NULL);
8752     }
8753
8754   /* Terminate the table.  */
8755   dw2_asm_output_data (1, 0, NULL);
8756 }
8757
8758 /* Output a symbol we can use to refer to this DIE from another CU.  */
8759
8760 static inline void
8761 output_die_symbol (dw_die_ref die)
8762 {
8763   char *sym = die->die_symbol;
8764
8765   if (sym == 0)
8766     return;
8767
8768   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8769     /* We make these global, not weak; if the target doesn't support
8770        .linkonce, it doesn't support combining the sections, so debugging
8771        will break.  */
8772     targetm.asm_out.globalize_label (asm_out_file, sym);
8773
8774   ASM_OUTPUT_LABEL (asm_out_file, sym);
8775 }
8776
8777 /* Return a new location list, given the begin and end range, and the
8778    expression. gensym tells us whether to generate a new internal symbol for
8779    this location list node, which is done for the head of the list only.  */
8780
8781 static inline dw_loc_list_ref
8782 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8783               const char *section, unsigned int gensym)
8784 {
8785   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8786
8787   retlist->begin = begin;
8788   retlist->end = end;
8789   retlist->expr = expr;
8790   retlist->section = section;
8791   if (gensym)
8792     retlist->ll_symbol = gen_internal_sym ("LLST");
8793
8794   return retlist;
8795 }
8796
8797 /* Add a location description expression to a location list.  */
8798
8799 static inline void
8800 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8801                            const char *begin, const char *end,
8802                            const char *section)
8803 {
8804   dw_loc_list_ref *d;
8805
8806   /* Find the end of the chain.  */
8807   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8808     ;
8809
8810   /* Add a new location list node to the list.  */
8811   *d = new_loc_list (descr, begin, end, section, 0);
8812 }
8813
8814 /* Output the location list given to us.  */
8815
8816 static void
8817 output_loc_list (dw_loc_list_ref list_head)
8818 {
8819   dw_loc_list_ref curr = list_head;
8820
8821   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8822
8823   /* Walk the location list, and output each range + expression.  */
8824   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8825     {
8826       unsigned long size;
8827       /* Don't output an entry that starts and ends at the same address.  */
8828       if (strcmp (curr->begin, curr->end) == 0)
8829         continue;
8830       if (!have_multiple_function_sections)
8831         {
8832           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8833                                 "Location list begin address (%s)",
8834                                 list_head->ll_symbol);
8835           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8836                                 "Location list end address (%s)",
8837                                 list_head->ll_symbol);
8838         }
8839       else
8840         {
8841           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8842                                "Location list begin address (%s)",
8843                                list_head->ll_symbol);
8844           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8845                                "Location list end address (%s)",
8846                                list_head->ll_symbol);
8847         }
8848       size = size_of_locs (curr->expr);
8849
8850       /* Output the block length for this list of location operations.  */
8851       gcc_assert (size <= 0xffff);
8852       dw2_asm_output_data (2, size, "%s", "Location expression size");
8853
8854       output_loc_sequence (curr->expr);
8855     }
8856
8857   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8858                        "Location list terminator begin (%s)",
8859                        list_head->ll_symbol);
8860   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8861                        "Location list terminator end (%s)",
8862                        list_head->ll_symbol);
8863 }
8864
8865 /* Output the DIE and its attributes.  Called recursively to generate
8866    the definitions of each child DIE.  */
8867
8868 static void
8869 output_die (dw_die_ref die)
8870 {
8871   dw_attr_ref a;
8872   dw_die_ref c;
8873   unsigned long size;
8874   unsigned ix;
8875
8876   /* If someone in another CU might refer to us, set up a symbol for
8877      them to point to.  */
8878   if (die->die_symbol)
8879     output_die_symbol (die);
8880
8881   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8882                                (unsigned long)die->die_offset,
8883                                dwarf_tag_name (die->die_tag));
8884
8885   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8886     {
8887       const char *name = dwarf_attr_name (a->dw_attr);
8888
8889       switch (AT_class (a))
8890         {
8891         case dw_val_class_addr:
8892           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8893           break;
8894
8895         case dw_val_class_offset:
8896           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8897                                "%s", name);
8898           break;
8899
8900         case dw_val_class_range_list:
8901           {
8902             char *p = strchr (ranges_section_label, '\0');
8903
8904             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8905                      a->dw_attr_val.v.val_offset);
8906             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8907                                    debug_ranges_section, "%s", name);
8908             *p = '\0';
8909           }
8910           break;
8911
8912         case dw_val_class_loc:
8913           size = size_of_locs (AT_loc (a));
8914
8915           /* Output the block length for this list of location operations.  */
8916           dw2_asm_output_data (constant_size (size), size, "%s", name);
8917
8918           output_loc_sequence (AT_loc (a));
8919           break;
8920
8921         case dw_val_class_const:
8922           /* ??? It would be slightly more efficient to use a scheme like is
8923              used for unsigned constants below, but gdb 4.x does not sign
8924              extend.  Gdb 5.x does sign extend.  */
8925           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
8926           break;
8927
8928         case dw_val_class_unsigned_const:
8929           dw2_asm_output_data (constant_size (AT_unsigned (a)),
8930                                AT_unsigned (a), "%s", name);
8931           break;
8932
8933         case dw_val_class_long_long:
8934           {
8935             unsigned HOST_WIDE_INT first, second;
8936
8937             dw2_asm_output_data (1,
8938                                  2 * HOST_BITS_PER_WIDE_INT
8939                                  / HOST_BITS_PER_CHAR,
8940                                  "%s", name);
8941
8942             if (WORDS_BIG_ENDIAN)
8943               {
8944                 first = CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long);
8945                 second = CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long);
8946               }
8947             else
8948               {
8949                 first = CONST_DOUBLE_LOW (a->dw_attr_val.v.val_long_long);
8950                 second = CONST_DOUBLE_HIGH (a->dw_attr_val.v.val_long_long);
8951               }
8952
8953             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8954                                  first, "long long constant");
8955             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
8956                                  second, NULL);
8957           }
8958           break;
8959
8960         case dw_val_class_vec:
8961           {
8962             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
8963             unsigned int len = a->dw_attr_val.v.val_vec.length;
8964             unsigned int i;
8965             unsigned char *p;
8966
8967             dw2_asm_output_data (constant_size (len * elt_size),
8968                                  len * elt_size, "%s", name);
8969             if (elt_size > sizeof (HOST_WIDE_INT))
8970               {
8971                 elt_size /= 2;
8972                 len *= 2;
8973               }
8974             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
8975                  i < len;
8976                  i++, p += elt_size)
8977               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
8978                                    "fp or vector constant word %u", i);
8979             break;
8980           }
8981
8982         case dw_val_class_flag:
8983           dw2_asm_output_data (1, AT_flag (a), "%s", name);
8984           break;
8985
8986         case dw_val_class_loc_list:
8987           {
8988             char *sym = AT_loc_list (a)->ll_symbol;
8989
8990             gcc_assert (sym);
8991             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
8992                                    "%s", name);
8993           }
8994           break;
8995
8996         case dw_val_class_die_ref:
8997           if (AT_ref_external (a))
8998             {
8999               char *sym = AT_ref (a)->die_symbol;
9000               int size;
9001
9002               gcc_assert (sym);
9003
9004               /* In DWARF2, DW_FORM_ref_addr is sized by target address
9005                  length, whereas in DWARF3 it's always sized as an offset.  */
9006               if (dwarf_version == 2)
9007                 size = DWARF2_ADDR_SIZE;
9008               else
9009                 size = DWARF_OFFSET_SIZE;
9010               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
9011             }
9012           else
9013             {
9014               gcc_assert (AT_ref (a)->die_offset);
9015               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9016                                    "%s", name);
9017             }
9018           break;
9019
9020         case dw_val_class_fde_ref:
9021           {
9022             char l1[20];
9023
9024             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9025                                          a->dw_attr_val.v.val_fde_index * 2);
9026             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9027                                    "%s", name);
9028           }
9029           break;
9030
9031         case dw_val_class_lbl_id:
9032           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9033           break;
9034
9035         case dw_val_class_lineptr:
9036           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9037                                  debug_line_section, "%s", name);
9038           break;
9039
9040         case dw_val_class_macptr:
9041           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9042                                  debug_macinfo_section, "%s", name);
9043           break;
9044
9045         case dw_val_class_str:
9046           if (AT_string_form (a) == DW_FORM_strp)
9047             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9048                                    a->dw_attr_val.v.val_str->label,
9049                                    debug_str_section,
9050                                    "%s: \"%s\"", name, AT_string (a));
9051           else
9052             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9053           break;
9054
9055         case dw_val_class_file:
9056           {
9057             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9058
9059             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9060                                  a->dw_attr_val.v.val_file->filename);
9061             break;
9062           }
9063
9064         default:
9065           gcc_unreachable ();
9066         }
9067     }
9068
9069   FOR_EACH_CHILD (die, c, output_die (c));
9070
9071   /* Add null byte to terminate sibling list.  */
9072   if (die->die_child != NULL)
9073     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
9074                          (unsigned long) die->die_offset);
9075 }
9076
9077 /* Output the compilation unit that appears at the beginning of the
9078    .debug_info section, and precedes the DIE descriptions.  */
9079
9080 static void
9081 output_compilation_unit_header (void)
9082 {
9083   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9084     dw2_asm_output_data (4, 0xffffffff,
9085       "Initial length escape value indicating 64-bit DWARF extension");
9086   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9087                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9088                        "Length of Compilation Unit Info");
9089   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9090   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9091                          debug_abbrev_section,
9092                          "Offset Into Abbrev. Section");
9093   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9094 }
9095
9096 /* Output the compilation unit DIE and its children.  */
9097
9098 static void
9099 output_comp_unit (dw_die_ref die, int output_if_empty)
9100 {
9101   const char *secname;
9102   char *oldsym, *tmp;
9103
9104   /* Unless we are outputting main CU, we may throw away empty ones.  */
9105   if (!output_if_empty && die->die_child == NULL)
9106     return;
9107
9108   /* Even if there are no children of this DIE, we must output the information
9109      about the compilation unit.  Otherwise, on an empty translation unit, we
9110      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9111      will then complain when examining the file.  First mark all the DIEs in
9112      this CU so we know which get local refs.  */
9113   mark_dies (die);
9114
9115   build_abbrev_table (die);
9116
9117   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9118   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9119   calc_die_sizes (die);
9120
9121   oldsym = die->die_symbol;
9122   if (oldsym)
9123     {
9124       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9125
9126       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9127       secname = tmp;
9128       die->die_symbol = NULL;
9129       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9130     }
9131   else
9132     switch_to_section (debug_info_section);
9133
9134   /* Output debugging information.  */
9135   output_compilation_unit_header ();
9136   output_die (die);
9137
9138   /* Leave the marks on the main CU, so we can check them in
9139      output_pubnames.  */
9140   if (oldsym)
9141     {
9142       unmark_dies (die);
9143       die->die_symbol = oldsym;
9144     }
9145 }
9146
9147 /* Return the DWARF2/3 pubname associated with a decl.  */
9148
9149 static const char *
9150 dwarf2_name (tree decl, int scope)
9151 {
9152   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9153 }
9154
9155 /* Add a new entry to .debug_pubnames if appropriate.  */
9156
9157 static void
9158 add_pubname_string (const char *str, dw_die_ref die)
9159 {
9160   pubname_entry e;
9161
9162   e.die = die;
9163   e.name = xstrdup (str);
9164   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9165 }
9166
9167 static void
9168 add_pubname (tree decl, dw_die_ref die)
9169 {
9170   if (TREE_PUBLIC (decl))
9171     add_pubname_string (dwarf2_name (decl, 1), die);
9172 }
9173
9174 /* Add a new entry to .debug_pubtypes if appropriate.  */
9175
9176 static void
9177 add_pubtype (tree decl, dw_die_ref die)
9178 {
9179   pubname_entry e;
9180
9181   e.name = NULL;
9182   if ((TREE_PUBLIC (decl)
9183        || die->die_parent == comp_unit_die)
9184       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9185     {
9186       e.die = die;
9187       if (TYPE_P (decl))
9188         {
9189           if (TYPE_NAME (decl))
9190             {
9191               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9192                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9193               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9194                        && DECL_NAME (TYPE_NAME (decl)))
9195                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9196               else
9197                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9198             }
9199         }
9200       else
9201         e.name = xstrdup (dwarf2_name (decl, 1));
9202
9203       /* If we don't have a name for the type, there's no point in adding
9204          it to the table.  */
9205       if (e.name && e.name[0] != '\0')
9206         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9207     }
9208 }
9209
9210 /* Output the public names table used to speed up access to externally
9211    visible names; or the public types table used to find type definitions.  */
9212
9213 static void
9214 output_pubnames (VEC (pubname_entry, gc) * names)
9215 {
9216   unsigned i;
9217   unsigned long pubnames_length = size_of_pubnames (names);
9218   pubname_ref pub;
9219
9220   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9221     dw2_asm_output_data (4, 0xffffffff,
9222       "Initial length escape value indicating 64-bit DWARF extension");
9223   if (names == pubname_table)
9224     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9225                          "Length of Public Names Info");
9226   else
9227     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9228                          "Length of Public Type Names Info");
9229   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9230   dw2_asm_output_data (2, 2, "DWARF Version");
9231   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9232                          debug_info_section,
9233                          "Offset of Compilation Unit Info");
9234   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9235                        "Compilation Unit Length");
9236
9237   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9238     {
9239       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9240       if (names == pubname_table)
9241         gcc_assert (pub->die->die_mark);
9242
9243       if (names != pubtype_table
9244           || pub->die->die_offset != 0
9245           || !flag_eliminate_unused_debug_types)
9246         {
9247           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9248                                "DIE offset");
9249
9250           dw2_asm_output_nstring (pub->name, -1, "external name");
9251         }
9252     }
9253
9254   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9255 }
9256
9257 /* Add a new entry to .debug_aranges if appropriate.  */
9258
9259 static void
9260 add_arange (tree decl, dw_die_ref die)
9261 {
9262   if (! DECL_SECTION_NAME (decl))
9263     return;
9264
9265   if (arange_table_in_use == arange_table_allocated)
9266     {
9267       arange_table_allocated += ARANGE_TABLE_INCREMENT;
9268       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9269                                     arange_table_allocated);
9270       memset (arange_table + arange_table_in_use, 0,
9271               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9272     }
9273
9274   arange_table[arange_table_in_use++] = die;
9275 }
9276
9277 /* Output the information that goes into the .debug_aranges table.
9278    Namely, define the beginning and ending address range of the
9279    text section generated for this compilation unit.  */
9280
9281 static void
9282 output_aranges (void)
9283 {
9284   unsigned i;
9285   unsigned long aranges_length = size_of_aranges ();
9286
9287   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9288     dw2_asm_output_data (4, 0xffffffff,
9289       "Initial length escape value indicating 64-bit DWARF extension");
9290   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9291                        "Length of Address Ranges Info");
9292   /* Version number for aranges is still 2, even in DWARF3.  */
9293   dw2_asm_output_data (2, 2, "DWARF Version");
9294   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9295                          debug_info_section,
9296                          "Offset of Compilation Unit Info");
9297   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9298   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9299
9300   /* We need to align to twice the pointer size here.  */
9301   if (DWARF_ARANGES_PAD_SIZE)
9302     {
9303       /* Pad using a 2 byte words so that padding is correct for any
9304          pointer size.  */
9305       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9306                            2 * DWARF2_ADDR_SIZE);
9307       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9308         dw2_asm_output_data (2, 0, NULL);
9309     }
9310
9311   /* It is necessary not to output these entries if the sections were
9312      not used; if the sections were not used, the length will be 0 and
9313      the address may end up as 0 if the section is discarded by ld
9314      --gc-sections, leaving an invalid (0, 0) entry that can be
9315      confused with the terminator.  */
9316   if (text_section_used)
9317     {
9318       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9319       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9320                             text_section_label, "Length");
9321     }
9322   if (cold_text_section_used)
9323     {
9324       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9325                            "Address");
9326       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9327                             cold_text_section_label, "Length");
9328     }
9329
9330   for (i = 0; i < arange_table_in_use; i++)
9331     {
9332       dw_die_ref die = arange_table[i];
9333
9334       /* We shouldn't see aranges for DIEs outside of the main CU.  */
9335       gcc_assert (die->die_mark);
9336
9337       if (die->die_tag == DW_TAG_subprogram)
9338         {
9339           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9340                                "Address");
9341           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9342                                 get_AT_low_pc (die), "Length");
9343         }
9344       else
9345         {
9346           /* A static variable; extract the symbol from DW_AT_location.
9347              Note that this code isn't currently hit, as we only emit
9348              aranges for functions (jason 9/23/99).  */
9349           dw_attr_ref a = get_AT (die, DW_AT_location);
9350           dw_loc_descr_ref loc;
9351
9352           gcc_assert (a && AT_class (a) == dw_val_class_loc);
9353
9354           loc = AT_loc (a);
9355           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9356
9357           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9358                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
9359           dw2_asm_output_data (DWARF2_ADDR_SIZE,
9360                                get_AT_unsigned (die, DW_AT_byte_size),
9361                                "Length");
9362         }
9363     }
9364
9365   /* Output the terminator words.  */
9366   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9367   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9368 }
9369
9370 /* Add a new entry to .debug_ranges.  Return the offset at which it
9371    was placed.  */
9372
9373 static unsigned int
9374 add_ranges_num (int num)
9375 {
9376   unsigned int in_use = ranges_table_in_use;
9377
9378   if (in_use == ranges_table_allocated)
9379     {
9380       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9381       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9382                                     ranges_table_allocated);
9383       memset (ranges_table + ranges_table_in_use, 0,
9384               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9385     }
9386
9387   ranges_table[in_use].num = num;
9388   ranges_table_in_use = in_use + 1;
9389
9390   return in_use * 2 * DWARF2_ADDR_SIZE;
9391 }
9392
9393 /* Add a new entry to .debug_ranges corresponding to a block, or a
9394    range terminator if BLOCK is NULL.  */
9395
9396 static unsigned int
9397 add_ranges (const_tree block)
9398 {
9399   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9400 }
9401
9402 /* Add a new entry to .debug_ranges corresponding to a pair of
9403    labels.  */
9404
9405 static unsigned int
9406 add_ranges_by_labels (const char *begin, const char *end)
9407 {
9408   unsigned int in_use = ranges_by_label_in_use;
9409
9410   if (in_use == ranges_by_label_allocated)
9411     {
9412       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9413       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9414                                        ranges_by_label,
9415                                        ranges_by_label_allocated);
9416       memset (ranges_by_label + ranges_by_label_in_use, 0,
9417               RANGES_TABLE_INCREMENT
9418               * sizeof (struct dw_ranges_by_label_struct));
9419     }
9420
9421   ranges_by_label[in_use].begin = begin;
9422   ranges_by_label[in_use].end = end;
9423   ranges_by_label_in_use = in_use + 1;
9424
9425   return add_ranges_num (-(int)in_use - 1);
9426 }
9427
9428 static void
9429 output_ranges (void)
9430 {
9431   unsigned i;
9432   static const char *const start_fmt = "Offset 0x%x";
9433   const char *fmt = start_fmt;
9434
9435   for (i = 0; i < ranges_table_in_use; i++)
9436     {
9437       int block_num = ranges_table[i].num;
9438
9439       if (block_num > 0)
9440         {
9441           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9442           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9443
9444           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9445           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9446
9447           /* If all code is in the text section, then the compilation
9448              unit base address defaults to DW_AT_low_pc, which is the
9449              base of the text section.  */
9450           if (!have_multiple_function_sections)
9451             {
9452               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9453                                     text_section_label,
9454                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9455               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9456                                     text_section_label, NULL);
9457             }
9458
9459           /* Otherwise, the compilation unit base address is zero,
9460              which allows us to use absolute addresses, and not worry
9461              about whether the target supports cross-section
9462              arithmetic.  */
9463           else
9464             {
9465               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9466                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9467               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9468             }
9469
9470           fmt = NULL;
9471         }
9472
9473       /* Negative block_num stands for an index into ranges_by_label.  */
9474       else if (block_num < 0)
9475         {
9476           int lab_idx = - block_num - 1;
9477
9478           if (!have_multiple_function_sections)
9479             {
9480               gcc_unreachable ();
9481 #if 0
9482               /* If we ever use add_ranges_by_labels () for a single
9483                  function section, all we have to do is to take out
9484                  the #if 0 above.  */
9485               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9486                                     ranges_by_label[lab_idx].begin,
9487                                     text_section_label,
9488                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9489               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9490                                     ranges_by_label[lab_idx].end,
9491                                     text_section_label, NULL);
9492 #endif
9493             }
9494           else
9495             {
9496               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9497                                    ranges_by_label[lab_idx].begin,
9498                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9499               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9500                                    ranges_by_label[lab_idx].end,
9501                                    NULL);
9502             }
9503         }
9504       else
9505         {
9506           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9507           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9508           fmt = start_fmt;
9509         }
9510     }
9511 }
9512
9513 /* Data structure containing information about input files.  */
9514 struct file_info
9515 {
9516   const char *path;     /* Complete file name.  */
9517   const char *fname;    /* File name part.  */
9518   int length;           /* Length of entire string.  */
9519   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9520   int dir_idx;          /* Index in directory table.  */
9521 };
9522
9523 /* Data structure containing information about directories with source
9524    files.  */
9525 struct dir_info
9526 {
9527   const char *path;     /* Path including directory name.  */
9528   int length;           /* Path length.  */
9529   int prefix;           /* Index of directory entry which is a prefix.  */
9530   int count;            /* Number of files in this directory.  */
9531   int dir_idx;          /* Index of directory used as base.  */
9532 };
9533
9534 /* Callback function for file_info comparison.  We sort by looking at
9535    the directories in the path.  */
9536
9537 static int
9538 file_info_cmp (const void *p1, const void *p2)
9539 {
9540   const struct file_info *const s1 = (const struct file_info *) p1;
9541   const struct file_info *const s2 = (const struct file_info *) p2;
9542   const unsigned char *cp1;
9543   const unsigned char *cp2;
9544
9545   /* Take care of file names without directories.  We need to make sure that
9546      we return consistent values to qsort since some will get confused if
9547      we return the same value when identical operands are passed in opposite
9548      orders.  So if neither has a directory, return 0 and otherwise return
9549      1 or -1 depending on which one has the directory.  */
9550   if ((s1->path == s1->fname || s2->path == s2->fname))
9551     return (s2->path == s2->fname) - (s1->path == s1->fname);
9552
9553   cp1 = (const unsigned char *) s1->path;
9554   cp2 = (const unsigned char *) s2->path;
9555
9556   while (1)
9557     {
9558       ++cp1;
9559       ++cp2;
9560       /* Reached the end of the first path?  If so, handle like above.  */
9561       if ((cp1 == (const unsigned char *) s1->fname)
9562           || (cp2 == (const unsigned char *) s2->fname))
9563         return ((cp2 == (const unsigned char *) s2->fname)
9564                 - (cp1 == (const unsigned char *) s1->fname));
9565
9566       /* Character of current path component the same?  */
9567       else if (*cp1 != *cp2)
9568         return *cp1 - *cp2;
9569     }
9570 }
9571
9572 struct file_name_acquire_data
9573 {
9574   struct file_info *files;
9575   int used_files;
9576   int max_files;
9577 };
9578
9579 /* Traversal function for the hash table.  */
9580
9581 static int
9582 file_name_acquire (void ** slot, void *data)
9583 {
9584   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9585   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9586   struct file_info *fi;
9587   const char *f;
9588
9589   gcc_assert (fnad->max_files >= d->emitted_number);
9590
9591   if (! d->emitted_number)
9592     return 1;
9593
9594   gcc_assert (fnad->max_files != fnad->used_files);
9595
9596   fi = fnad->files + fnad->used_files++;
9597
9598   /* Skip all leading "./".  */
9599   f = d->filename;
9600   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9601     f += 2;
9602
9603   /* Create a new array entry.  */
9604   fi->path = f;
9605   fi->length = strlen (f);
9606   fi->file_idx = d;
9607
9608   /* Search for the file name part.  */
9609   f = strrchr (f, DIR_SEPARATOR);
9610 #if defined (DIR_SEPARATOR_2)
9611   {
9612     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9613
9614     if (g != NULL)
9615       {
9616         if (f == NULL || f < g)
9617           f = g;
9618       }
9619   }
9620 #endif
9621
9622   fi->fname = f == NULL ? fi->path : f + 1;
9623   return 1;
9624 }
9625
9626 /* Output the directory table and the file name table.  We try to minimize
9627    the total amount of memory needed.  A heuristic is used to avoid large
9628    slowdowns with many input files.  */
9629
9630 static void
9631 output_file_names (void)
9632 {
9633   struct file_name_acquire_data fnad;
9634   int numfiles;
9635   struct file_info *files;
9636   struct dir_info *dirs;
9637   int *saved;
9638   int *savehere;
9639   int *backmap;
9640   int ndirs;
9641   int idx_offset;
9642   int i;
9643   int idx;
9644
9645   if (!last_emitted_file)
9646     {
9647       dw2_asm_output_data (1, 0, "End directory table");
9648       dw2_asm_output_data (1, 0, "End file name table");
9649       return;
9650     }
9651
9652   numfiles = last_emitted_file->emitted_number;
9653
9654   /* Allocate the various arrays we need.  */
9655   files = XALLOCAVEC (struct file_info, numfiles);
9656   dirs = XALLOCAVEC (struct dir_info, numfiles);
9657
9658   fnad.files = files;
9659   fnad.used_files = 0;
9660   fnad.max_files = numfiles;
9661   htab_traverse (file_table, file_name_acquire, &fnad);
9662   gcc_assert (fnad.used_files == fnad.max_files);
9663
9664   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9665
9666   /* Find all the different directories used.  */
9667   dirs[0].path = files[0].path;
9668   dirs[0].length = files[0].fname - files[0].path;
9669   dirs[0].prefix = -1;
9670   dirs[0].count = 1;
9671   dirs[0].dir_idx = 0;
9672   files[0].dir_idx = 0;
9673   ndirs = 1;
9674
9675   for (i = 1; i < numfiles; i++)
9676     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9677         && memcmp (dirs[ndirs - 1].path, files[i].path,
9678                    dirs[ndirs - 1].length) == 0)
9679       {
9680         /* Same directory as last entry.  */
9681         files[i].dir_idx = ndirs - 1;
9682         ++dirs[ndirs - 1].count;
9683       }
9684     else
9685       {
9686         int j;
9687
9688         /* This is a new directory.  */
9689         dirs[ndirs].path = files[i].path;
9690         dirs[ndirs].length = files[i].fname - files[i].path;
9691         dirs[ndirs].count = 1;
9692         dirs[ndirs].dir_idx = ndirs;
9693         files[i].dir_idx = ndirs;
9694
9695         /* Search for a prefix.  */
9696         dirs[ndirs].prefix = -1;
9697         for (j = 0; j < ndirs; j++)
9698           if (dirs[j].length < dirs[ndirs].length
9699               && dirs[j].length > 1
9700               && (dirs[ndirs].prefix == -1
9701                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9702               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9703             dirs[ndirs].prefix = j;
9704
9705         ++ndirs;
9706       }
9707
9708   /* Now to the actual work.  We have to find a subset of the directories which
9709      allow expressing the file name using references to the directory table
9710      with the least amount of characters.  We do not do an exhaustive search
9711      where we would have to check out every combination of every single
9712      possible prefix.  Instead we use a heuristic which provides nearly optimal
9713      results in most cases and never is much off.  */
9714   saved = XALLOCAVEC (int, ndirs);
9715   savehere = XALLOCAVEC (int, ndirs);
9716
9717   memset (saved, '\0', ndirs * sizeof (saved[0]));
9718   for (i = 0; i < ndirs; i++)
9719     {
9720       int j;
9721       int total;
9722
9723       /* We can always save some space for the current directory.  But this
9724          does not mean it will be enough to justify adding the directory.  */
9725       savehere[i] = dirs[i].length;
9726       total = (savehere[i] - saved[i]) * dirs[i].count;
9727
9728       for (j = i + 1; j < ndirs; j++)
9729         {
9730           savehere[j] = 0;
9731           if (saved[j] < dirs[i].length)
9732             {
9733               /* Determine whether the dirs[i] path is a prefix of the
9734                  dirs[j] path.  */
9735               int k;
9736
9737               k = dirs[j].prefix;
9738               while (k != -1 && k != (int) i)
9739                 k = dirs[k].prefix;
9740
9741               if (k == (int) i)
9742                 {
9743                   /* Yes it is.  We can possibly save some memory by
9744                      writing the filenames in dirs[j] relative to
9745                      dirs[i].  */
9746                   savehere[j] = dirs[i].length;
9747                   total += (savehere[j] - saved[j]) * dirs[j].count;
9748                 }
9749             }
9750         }
9751
9752       /* Check whether we can save enough to justify adding the dirs[i]
9753          directory.  */
9754       if (total > dirs[i].length + 1)
9755         {
9756           /* It's worthwhile adding.  */
9757           for (j = i; j < ndirs; j++)
9758             if (savehere[j] > 0)
9759               {
9760                 /* Remember how much we saved for this directory so far.  */
9761                 saved[j] = savehere[j];
9762
9763                 /* Remember the prefix directory.  */
9764                 dirs[j].dir_idx = i;
9765               }
9766         }
9767     }
9768
9769   /* Emit the directory name table.  */
9770   idx = 1;
9771   idx_offset = dirs[0].length > 0 ? 1 : 0;
9772   for (i = 1 - idx_offset; i < ndirs; i++)
9773     dw2_asm_output_nstring (dirs[i].path,
9774                             dirs[i].length
9775                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9776                             "Directory Entry: 0x%x", i + idx_offset);
9777
9778   dw2_asm_output_data (1, 0, "End directory table");
9779
9780   /* We have to emit them in the order of emitted_number since that's
9781      used in the debug info generation.  To do this efficiently we
9782      generate a back-mapping of the indices first.  */
9783   backmap = XALLOCAVEC (int, numfiles);
9784   for (i = 0; i < numfiles; i++)
9785     backmap[files[i].file_idx->emitted_number - 1] = i;
9786
9787   /* Now write all the file names.  */
9788   for (i = 0; i < numfiles; i++)
9789     {
9790       int file_idx = backmap[i];
9791       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9792
9793 #ifdef VMS_DEBUGGING_INFO
9794 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9795
9796       /* Setting these fields can lead to debugger miscomparisons,
9797          but VMS Debug requires them to be set correctly.  */
9798
9799       int ver;
9800       long long cdt;
9801       long siz;
9802       int maxfilelen = strlen (files[file_idx].path)
9803                                + dirs[dir_idx].length
9804                                + MAX_VMS_VERSION_LEN + 1;
9805       char *filebuf = XALLOCAVEC (char, maxfilelen);
9806
9807       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9808       snprintf (filebuf, maxfilelen, "%s;%d",
9809                 files[file_idx].path + dirs[dir_idx].length, ver);
9810
9811       dw2_asm_output_nstring
9812         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9813
9814       /* Include directory index.  */
9815       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9816
9817       /* Modification time.  */
9818       dw2_asm_output_data_uleb128
9819         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9820           ? cdt : 0,
9821          NULL);
9822
9823       /* File length in bytes.  */
9824       dw2_asm_output_data_uleb128
9825         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9826           ? siz : 0,
9827          NULL);
9828 #else
9829       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9830                               "File Entry: 0x%x", (unsigned) i + 1);
9831
9832       /* Include directory index.  */
9833       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9834
9835       /* Modification time.  */
9836       dw2_asm_output_data_uleb128 (0, NULL);
9837
9838       /* File length in bytes.  */
9839       dw2_asm_output_data_uleb128 (0, NULL);
9840 #endif
9841     }
9842
9843   dw2_asm_output_data (1, 0, "End file name table");
9844 }
9845
9846
9847 /* Output the source line number correspondence information.  This
9848    information goes into the .debug_line section.  */
9849
9850 static void
9851 output_line_info (void)
9852 {
9853   char l1[20], l2[20], p1[20], p2[20];
9854   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9855   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9856   unsigned opc;
9857   unsigned n_op_args;
9858   unsigned long lt_index;
9859   unsigned long current_line;
9860   long line_offset;
9861   long line_delta;
9862   unsigned long current_file;
9863   unsigned long function;
9864
9865   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9866   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9867   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9868   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9869
9870   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9871     dw2_asm_output_data (4, 0xffffffff,
9872       "Initial length escape value indicating 64-bit DWARF extension");
9873   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9874                         "Length of Source Line Info");
9875   ASM_OUTPUT_LABEL (asm_out_file, l1);
9876
9877   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9878   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9879   ASM_OUTPUT_LABEL (asm_out_file, p1);
9880
9881   /* Define the architecture-dependent minimum instruction length (in
9882    bytes).  In this implementation of DWARF, this field is used for
9883    information purposes only.  Since GCC generates assembly language,
9884    we have no a priori knowledge of how many instruction bytes are
9885    generated for each source line, and therefore can use only the
9886    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9887    commands.  Accordingly, we fix this as `1', which is "correct
9888    enough" for all architectures, and don't let the target override.  */
9889   dw2_asm_output_data (1, 1,
9890                        "Minimum Instruction Length");
9891
9892   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9893                        "Default is_stmt_start flag");
9894   dw2_asm_output_data (1, DWARF_LINE_BASE,
9895                        "Line Base Value (Special Opcodes)");
9896   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9897                        "Line Range Value (Special Opcodes)");
9898   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9899                        "Special Opcode Base");
9900
9901   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9902     {
9903       switch (opc)
9904         {
9905         case DW_LNS_advance_pc:
9906         case DW_LNS_advance_line:
9907         case DW_LNS_set_file:
9908         case DW_LNS_set_column:
9909         case DW_LNS_fixed_advance_pc:
9910           n_op_args = 1;
9911           break;
9912         default:
9913           n_op_args = 0;
9914           break;
9915         }
9916
9917       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9918                            opc, n_op_args);
9919     }
9920
9921   /* Write out the information about the files we use.  */
9922   output_file_names ();
9923   ASM_OUTPUT_LABEL (asm_out_file, p2);
9924
9925   /* We used to set the address register to the first location in the text
9926      section here, but that didn't accomplish anything since we already
9927      have a line note for the opening brace of the first function.  */
9928
9929   /* Generate the line number to PC correspondence table, encoded as
9930      a series of state machine operations.  */
9931   current_file = 1;
9932   current_line = 1;
9933
9934   if (cfun && in_cold_section_p)
9935     strcpy (prev_line_label, crtl->subsections.cold_section_label);
9936   else
9937     strcpy (prev_line_label, text_section_label);
9938   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
9939     {
9940       dw_line_info_ref line_info = &line_info_table[lt_index];
9941
9942 #if 0
9943       /* Disable this optimization for now; GDB wants to see two line notes
9944          at the beginning of a function so it can find the end of the
9945          prologue.  */
9946
9947       /* Don't emit anything for redundant notes.  Just updating the
9948          address doesn't accomplish anything, because we already assume
9949          that anything after the last address is this line.  */
9950       if (line_info->dw_line_num == current_line
9951           && line_info->dw_file_num == current_file)
9952         continue;
9953 #endif
9954
9955       /* Emit debug info for the address of the current line.
9956
9957          Unfortunately, we have little choice here currently, and must always
9958          use the most general form.  GCC does not know the address delta
9959          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
9960          attributes which will give an upper bound on the address range.  We
9961          could perhaps use length attributes to determine when it is safe to
9962          use DW_LNS_fixed_advance_pc.  */
9963
9964       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
9965       if (0)
9966         {
9967           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
9968           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
9969                                "DW_LNS_fixed_advance_pc");
9970           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
9971         }
9972       else
9973         {
9974           /* This can handle any delta.  This takes
9975              4+DWARF2_ADDR_SIZE bytes.  */
9976           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
9977           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
9978           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
9979           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
9980         }
9981
9982       strcpy (prev_line_label, line_label);
9983
9984       /* Emit debug info for the source file of the current line, if
9985          different from the previous line.  */
9986       if (line_info->dw_file_num != current_file)
9987         {
9988           current_file = line_info->dw_file_num;
9989           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
9990           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
9991         }
9992
9993       /* Emit debug info for the current line number, choosing the encoding
9994          that uses the least amount of space.  */
9995       if (line_info->dw_line_num != current_line)
9996         {
9997           line_offset = line_info->dw_line_num - current_line;
9998           line_delta = line_offset - DWARF_LINE_BASE;
9999           current_line = line_info->dw_line_num;
10000           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10001             /* This can handle deltas from -10 to 234, using the current
10002                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
10003                takes 1 byte.  */
10004             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10005                                  "line %lu", current_line);
10006           else
10007             {
10008               /* This can handle any delta.  This takes at least 4 bytes,
10009                  depending on the value being encoded.  */
10010               dw2_asm_output_data (1, DW_LNS_advance_line,
10011                                    "advance to line %lu", current_line);
10012               dw2_asm_output_data_sleb128 (line_offset, NULL);
10013               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10014             }
10015         }
10016       else
10017         /* We still need to start a new row, so output a copy insn.  */
10018         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10019     }
10020
10021   /* Emit debug info for the address of the end of the function.  */
10022   if (0)
10023     {
10024       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10025                            "DW_LNS_fixed_advance_pc");
10026       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
10027     }
10028   else
10029     {
10030       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10031       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10032       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10033       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
10034     }
10035
10036   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10037   dw2_asm_output_data_uleb128 (1, NULL);
10038   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10039
10040   function = 0;
10041   current_file = 1;
10042   current_line = 1;
10043   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
10044     {
10045       dw_separate_line_info_ref line_info
10046         = &separate_line_info_table[lt_index];
10047
10048 #if 0
10049       /* Don't emit anything for redundant notes.  */
10050       if (line_info->dw_line_num == current_line
10051           && line_info->dw_file_num == current_file
10052           && line_info->function == function)
10053         goto cont;
10054 #endif
10055
10056       /* Emit debug info for the address of the current line.  If this is
10057          a new function, or the first line of a function, then we need
10058          to handle it differently.  */
10059       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
10060                                    lt_index);
10061       if (function != line_info->function)
10062         {
10063           function = line_info->function;
10064
10065           /* Set the address register to the first line in the function.  */
10066           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10067           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10068           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10069           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10070         }
10071       else
10072         {
10073           /* ??? See the DW_LNS_advance_pc comment above.  */
10074           if (0)
10075             {
10076               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10077                                    "DW_LNS_fixed_advance_pc");
10078               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10079             }
10080           else
10081             {
10082               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10083               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10084               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10085               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10086             }
10087         }
10088
10089       strcpy (prev_line_label, line_label);
10090
10091       /* Emit debug info for the source file of the current line, if
10092          different from the previous line.  */
10093       if (line_info->dw_file_num != current_file)
10094         {
10095           current_file = line_info->dw_file_num;
10096           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10097           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10098         }
10099
10100       /* Emit debug info for the current line number, choosing the encoding
10101          that uses the least amount of space.  */
10102       if (line_info->dw_line_num != current_line)
10103         {
10104           line_offset = line_info->dw_line_num - current_line;
10105           line_delta = line_offset - DWARF_LINE_BASE;
10106           current_line = line_info->dw_line_num;
10107           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10108             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10109                                  "line %lu", current_line);
10110           else
10111             {
10112               dw2_asm_output_data (1, DW_LNS_advance_line,
10113                                    "advance to line %lu", current_line);
10114               dw2_asm_output_data_sleb128 (line_offset, NULL);
10115               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10116             }
10117         }
10118       else
10119         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10120
10121 #if 0
10122     cont:
10123 #endif
10124
10125       lt_index++;
10126
10127       /* If we're done with a function, end its sequence.  */
10128       if (lt_index == separate_line_info_table_in_use
10129           || separate_line_info_table[lt_index].function != function)
10130         {
10131           current_file = 1;
10132           current_line = 1;
10133
10134           /* Emit debug info for the address of the end of the function.  */
10135           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
10136           if (0)
10137             {
10138               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10139                                    "DW_LNS_fixed_advance_pc");
10140               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10141             }
10142           else
10143             {
10144               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10145               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10146               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10147               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10148             }
10149
10150           /* Output the marker for the end of this sequence.  */
10151           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10152           dw2_asm_output_data_uleb128 (1, NULL);
10153           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10154         }
10155     }
10156
10157   /* Output the marker for the end of the line number info.  */
10158   ASM_OUTPUT_LABEL (asm_out_file, l2);
10159 }
10160 \f
10161 /* Given a pointer to a tree node for some base type, return a pointer to
10162    a DIE that describes the given type.
10163
10164    This routine must only be called for GCC type nodes that correspond to
10165    Dwarf base (fundamental) types.  */
10166
10167 static dw_die_ref
10168 base_type_die (tree type)
10169 {
10170   dw_die_ref base_type_result;
10171   enum dwarf_type encoding;
10172
10173   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10174     return 0;
10175
10176   /* If this is a subtype that should not be emitted as a subrange type,
10177      use the base type.  See subrange_type_for_debug_p.  */
10178   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10179     type = TREE_TYPE (type);
10180
10181   switch (TREE_CODE (type))
10182     {
10183     case INTEGER_TYPE:
10184       if (TYPE_STRING_FLAG (type))
10185         {
10186           if (TYPE_UNSIGNED (type))
10187             encoding = DW_ATE_unsigned_char;
10188           else
10189             encoding = DW_ATE_signed_char;
10190         }
10191       else if (TYPE_UNSIGNED (type))
10192         encoding = DW_ATE_unsigned;
10193       else
10194         encoding = DW_ATE_signed;
10195       break;
10196
10197     case REAL_TYPE:
10198       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10199         encoding = DW_ATE_decimal_float;
10200       else
10201         encoding = DW_ATE_float;
10202       break;
10203
10204     case FIXED_POINT_TYPE:
10205       if (TYPE_UNSIGNED (type))
10206         encoding = DW_ATE_unsigned_fixed;
10207       else
10208         encoding = DW_ATE_signed_fixed;
10209       break;
10210
10211       /* Dwarf2 doesn't know anything about complex ints, so use
10212          a user defined type for it.  */
10213     case COMPLEX_TYPE:
10214       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10215         encoding = DW_ATE_complex_float;
10216       else
10217         encoding = DW_ATE_lo_user;
10218       break;
10219
10220     case BOOLEAN_TYPE:
10221       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10222       encoding = DW_ATE_boolean;
10223       break;
10224
10225     default:
10226       /* No other TREE_CODEs are Dwarf fundamental types.  */
10227       gcc_unreachable ();
10228     }
10229
10230   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10231
10232   /* This probably indicates a bug.  */
10233   if (! TYPE_NAME (type))
10234     add_name_attribute (base_type_result, "__unknown__");
10235
10236   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10237                    int_size_in_bytes (type));
10238   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10239
10240   return base_type_result;
10241 }
10242
10243 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10244    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10245
10246 static inline int
10247 is_base_type (tree type)
10248 {
10249   switch (TREE_CODE (type))
10250     {
10251     case ERROR_MARK:
10252     case VOID_TYPE:
10253     case INTEGER_TYPE:
10254     case REAL_TYPE:
10255     case FIXED_POINT_TYPE:
10256     case COMPLEX_TYPE:
10257     case BOOLEAN_TYPE:
10258       return 1;
10259
10260     case ARRAY_TYPE:
10261     case RECORD_TYPE:
10262     case UNION_TYPE:
10263     case QUAL_UNION_TYPE:
10264     case ENUMERAL_TYPE:
10265     case FUNCTION_TYPE:
10266     case METHOD_TYPE:
10267     case POINTER_TYPE:
10268     case REFERENCE_TYPE:
10269     case OFFSET_TYPE:
10270     case LANG_TYPE:
10271     case VECTOR_TYPE:
10272       return 0;
10273
10274     default:
10275       gcc_unreachable ();
10276     }
10277
10278   return 0;
10279 }
10280
10281 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10282    node, return the size in bits for the type if it is a constant, or else
10283    return the alignment for the type if the type's size is not constant, or
10284    else return BITS_PER_WORD if the type actually turns out to be an
10285    ERROR_MARK node.  */
10286
10287 static inline unsigned HOST_WIDE_INT
10288 simple_type_size_in_bits (const_tree type)
10289 {
10290   if (TREE_CODE (type) == ERROR_MARK)
10291     return BITS_PER_WORD;
10292   else if (TYPE_SIZE (type) == NULL_TREE)
10293     return 0;
10294   else if (host_integerp (TYPE_SIZE (type), 1))
10295     return tree_low_cst (TYPE_SIZE (type), 1);
10296   else
10297     return TYPE_ALIGN (type);
10298 }
10299
10300 /*  Given a pointer to a tree node for a subrange type, return a pointer
10301     to a DIE that describes the given type.  */
10302
10303 static dw_die_ref
10304 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10305 {
10306   dw_die_ref subrange_die;
10307   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10308
10309   if (context_die == NULL)
10310     context_die = comp_unit_die;
10311
10312   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10313
10314   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10315     {
10316       /* The size of the subrange type and its base type do not match,
10317          so we need to generate a size attribute for the subrange type.  */
10318       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10319     }
10320
10321   if (low)
10322     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10323   if (high)
10324     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10325
10326   return subrange_die;
10327 }
10328
10329 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10330    entry that chains various modifiers in front of the given type.  */
10331
10332 static dw_die_ref
10333 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10334                    dw_die_ref context_die)
10335 {
10336   enum tree_code code = TREE_CODE (type);
10337   dw_die_ref mod_type_die;
10338   dw_die_ref sub_die = NULL;
10339   tree item_type = NULL;
10340   tree qualified_type;
10341   tree name, low, high;
10342
10343   if (code == ERROR_MARK)
10344     return NULL;
10345
10346   /* See if we already have the appropriately qualified variant of
10347      this type.  */
10348   qualified_type
10349     = get_qualified_type (type,
10350                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10351                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10352
10353   /* If we do, then we can just use its DIE, if it exists.  */
10354   if (qualified_type)
10355     {
10356       mod_type_die = lookup_type_die (qualified_type);
10357       if (mod_type_die)
10358         return mod_type_die;
10359     }
10360
10361   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10362
10363   /* Handle C typedef types.  */
10364   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10365     {
10366       tree dtype = TREE_TYPE (name);
10367
10368       if (qualified_type == dtype)
10369         {
10370           /* For a named type, use the typedef.  */
10371           gen_type_die (qualified_type, context_die);
10372           return lookup_type_die (qualified_type);
10373         }
10374       else if (is_const_type < TYPE_READONLY (dtype)
10375                || is_volatile_type < TYPE_VOLATILE (dtype)
10376                || (is_const_type <= TYPE_READONLY (dtype)
10377                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10378                    && DECL_ORIGINAL_TYPE (name) != type))
10379         /* cv-unqualified version of named type.  Just use the unnamed
10380            type to which it refers.  */
10381         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10382                                   is_const_type, is_volatile_type,
10383                                   context_die);
10384       /* Else cv-qualified version of named type; fall through.  */
10385     }
10386
10387   if (is_const_type)
10388     {
10389       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10390       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10391     }
10392   else if (is_volatile_type)
10393     {
10394       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10395       sub_die = modified_type_die (type, 0, 0, context_die);
10396     }
10397   else if (code == POINTER_TYPE)
10398     {
10399       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10400       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10401                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10402       item_type = TREE_TYPE (type);
10403     }
10404   else if (code == REFERENCE_TYPE)
10405     {
10406       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10407       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10408                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10409       item_type = TREE_TYPE (type);
10410     }
10411   else if (code == INTEGER_TYPE
10412            && TREE_TYPE (type) != NULL_TREE
10413            && subrange_type_for_debug_p (type, &low, &high))
10414     {
10415       mod_type_die = subrange_type_die (type, low, high, context_die);
10416       item_type = TREE_TYPE (type);
10417     }
10418   else if (is_base_type (type))
10419     mod_type_die = base_type_die (type);
10420   else
10421     {
10422       gen_type_die (type, context_die);
10423
10424       /* We have to get the type_main_variant here (and pass that to the
10425          `lookup_type_die' routine) because the ..._TYPE node we have
10426          might simply be a *copy* of some original type node (where the
10427          copy was created to help us keep track of typedef names) and
10428          that copy might have a different TYPE_UID from the original
10429          ..._TYPE node.  */
10430       if (TREE_CODE (type) != VECTOR_TYPE)
10431         return lookup_type_die (type_main_variant (type));
10432       else
10433         /* Vectors have the debugging information in the type,
10434            not the main variant.  */
10435         return lookup_type_die (type);
10436     }
10437
10438   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10439      don't output a DW_TAG_typedef, since there isn't one in the
10440      user's program; just attach a DW_AT_name to the type.  */
10441   if (name
10442       && (TREE_CODE (name) != TYPE_DECL
10443           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10444     {
10445       if (TREE_CODE (name) == TYPE_DECL)
10446         /* Could just call add_name_and_src_coords_attributes here,
10447            but since this is a builtin type it doesn't have any
10448            useful source coordinates anyway.  */
10449         name = DECL_NAME (name);
10450       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10451     }
10452
10453   if (qualified_type)
10454     equate_type_number_to_die (qualified_type, mod_type_die);
10455
10456   if (item_type)
10457     /* We must do this after the equate_type_number_to_die call, in case
10458        this is a recursive type.  This ensures that the modified_type_die
10459        recursion will terminate even if the type is recursive.  Recursive
10460        types are possible in Ada.  */
10461     sub_die = modified_type_die (item_type,
10462                                  TYPE_READONLY (item_type),
10463                                  TYPE_VOLATILE (item_type),
10464                                  context_die);
10465
10466   if (sub_die != NULL)
10467     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10468
10469   return mod_type_die;
10470 }
10471
10472 /* Generate a new name for the parameter pack name NAME (an
10473    IDENTIFIER_NODE) that incorporates its */
10474
10475 static tree
10476 make_ith_pack_parameter_name (tree name, int i)
10477 {
10478   /* Munge the name to include the parameter index.  */
10479 #define NUMBUF_LEN 128
10480   char numbuf[NUMBUF_LEN];
10481   char* newname;
10482   int newname_len;
10483
10484   snprintf (numbuf, NUMBUF_LEN, "%i", i);
10485   newname_len = IDENTIFIER_LENGTH (name)
10486                 + strlen (numbuf) + 2;
10487   newname = (char*) alloca (newname_len);
10488   snprintf (newname, newname_len,
10489             "%s#%i", IDENTIFIER_POINTER (name), i);
10490   return get_identifier (newname);
10491 }
10492
10493 /* Generate DIEs for the generic parameters of T.
10494    T must be either a generic type or a generic function.
10495    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10496
10497 static void
10498 gen_generic_params_dies (tree t)
10499 {
10500   tree parms, args;
10501   int parms_num, i;
10502   dw_die_ref die = NULL;
10503
10504   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10505     return;
10506
10507   if (TYPE_P (t))
10508     die = lookup_type_die (t);
10509   else if (DECL_P (t))
10510     die = lookup_decl_die (t);
10511
10512   gcc_assert (die);
10513
10514   parms = lang_hooks.get_innermost_generic_parms (t);
10515   if (!parms)
10516     /* T has no generic parameter. It means T is neither a generic type
10517        or function. End of story.  */
10518     return;
10519
10520   parms_num = TREE_VEC_LENGTH (parms);
10521   args = lang_hooks.get_innermost_generic_args (t);
10522   for (i = 0; i < parms_num; i++)
10523     {
10524       tree parm, arg;
10525
10526       parm = TREE_VEC_ELT (parms, i);
10527       arg = TREE_VEC_ELT (args, i);
10528       if (parm && TREE_VALUE (parm) && arg)
10529         {
10530           tree pack_elems =
10531             lang_hooks.types.get_argument_pack_elems (arg);
10532           if (pack_elems)
10533             {
10534               /* So ARG is an argument pack and the elements of that pack
10535                  are stored in PACK_ELEMS.  */
10536               int i, len;
10537
10538               len = TREE_VEC_LENGTH (pack_elems);
10539               for (i = 0; i < len; i++)
10540                 generic_parameter_die (TREE_VALUE (parm),
10541                                        TREE_VEC_ELT (pack_elems, i),
10542                                        die, i);
10543             }
10544           else /* Arg is not an argument pack.  */
10545             generic_parameter_die (TREE_VALUE (parm),
10546                                    arg, die,
10547                                    -1/* Not a param pack.  */);
10548         }
10549     }
10550 }
10551
10552 /* Create and return a DIE for PARM which should be
10553    the representation of a generic type parameter.
10554    For instance, in the C++ front end, PARM would be a template parameter.
10555    ARG is the argument to PARM.
10556    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10557    as a child node.
10558    PACK_ELEM_INDEX is >= 0 if PARM is a generic parameter pack, and if ARG
10559    is one of the unpacked elements of the parameter PACK. In that case,
10560    PACK_ELEM_INDEX is the index of ARG in the parameter pack.  */
10561
10562 static dw_die_ref
10563 generic_parameter_die (tree parm, tree arg, dw_die_ref parent_die,
10564                        int pack_elem_index)
10565 {
10566   dw_die_ref tmpl_die = NULL;
10567   const char *name = NULL;
10568
10569   if (!parm || !DECL_NAME (parm) || !arg)
10570     return NULL;
10571
10572   /* We support non-type generic parameters and arguments,
10573      type generic parameters and arguments, as well as
10574      generic generic parameters (a.k.a. template template parameters in C++)
10575      and arguments.  */
10576   if (TREE_CODE (parm) == PARM_DECL)
10577     /* PARM is a nontype generic parameter  */
10578     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10579   else if (TREE_CODE (parm) == TYPE_DECL)
10580     /* PARM is a type generic parameter.  */
10581     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10582   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10583     /* PARM is a generic generic parameter.
10584        Its DIE is a GNU extension. It shall have a
10585        DW_AT_name attribute to represent the name of the template template
10586        parameter, and a DW_AT_GNU_template_name attribute to represent the
10587        name of the template template argument.  */
10588     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10589                         parent_die, parm);
10590   else
10591     gcc_unreachable ();
10592
10593   if (tmpl_die)
10594     {
10595       tree tmpl_type;
10596
10597       if (pack_elem_index >= 0)
10598         {
10599           /* PARM is an element of a parameter pack.
10600              Generate a name for it.  */
10601           tree identifier = make_ith_pack_parameter_name (DECL_NAME (parm),
10602                                                           pack_elem_index);
10603           if (identifier)
10604             name = IDENTIFIER_POINTER (identifier);
10605         }
10606       else
10607         name = IDENTIFIER_POINTER (DECL_NAME (parm));
10608
10609       gcc_assert (name);
10610       add_AT_string (tmpl_die, DW_AT_name, name);
10611
10612       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10613         {
10614           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10615              TMPL_DIE should have a child DW_AT_type attribute that is set
10616              to the type of the argument to PARM, which is ARG.
10617              If PARM is a type generic parameter, TMPL_DIE should have a
10618              child DW_AT_type that is set to ARG.  */
10619           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10620           add_type_attribute (tmpl_die, tmpl_type, 0,
10621                               TREE_THIS_VOLATILE (tmpl_type),
10622                               parent_die);
10623         }
10624       else
10625         {
10626           /* So TMPL_DIE is a DIE representing a
10627              a generic generic template parameter, a.k.a template template
10628              parameter in C++ and arg is a template.  */
10629
10630           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10631              to the name of the argument.  */
10632           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10633           add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10634         }
10635
10636       if (TREE_CODE (parm) == PARM_DECL)
10637         /* So PARM is a non-type generic parameter.
10638            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10639            attribute of TMPL_DIE which value represents the value
10640            of ARG.
10641            We must be careful here:
10642            The value of ARG might reference some function decls.
10643            We might currently be emitting debug info for a generic
10644            type and types are emitted before function decls, we don't
10645            know if the function decls referenced by ARG will actually be
10646            emitted after cgraph computations.
10647            So must defer the generation of the DW_AT_const_value to
10648            after cgraph is ready.  */
10649         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10650     }
10651
10652   return tmpl_die;
10653 }
10654
10655 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10656    an enumerated type.  */
10657
10658 static inline int
10659 type_is_enum (const_tree type)
10660 {
10661   return TREE_CODE (type) == ENUMERAL_TYPE;
10662 }
10663
10664 /* Return the DBX register number described by a given RTL node.  */
10665
10666 static unsigned int
10667 dbx_reg_number (const_rtx rtl)
10668 {
10669   unsigned regno = REGNO (rtl);
10670
10671   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10672
10673 #ifdef LEAF_REG_REMAP
10674   if (current_function_uses_only_leaf_regs)
10675     {
10676       int leaf_reg = LEAF_REG_REMAP (regno);
10677       if (leaf_reg != -1)
10678         regno = (unsigned) leaf_reg;
10679     }
10680 #endif
10681
10682   return DBX_REGISTER_NUMBER (regno);
10683 }
10684
10685 /* Optionally add a DW_OP_piece term to a location description expression.
10686    DW_OP_piece is only added if the location description expression already
10687    doesn't end with DW_OP_piece.  */
10688
10689 static void
10690 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10691 {
10692   dw_loc_descr_ref loc;
10693
10694   if (*list_head != NULL)
10695     {
10696       /* Find the end of the chain.  */
10697       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10698         ;
10699
10700       if (loc->dw_loc_opc != DW_OP_piece)
10701         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10702     }
10703 }
10704
10705 /* Return a location descriptor that designates a machine register or
10706    zero if there is none.  */
10707
10708 static dw_loc_descr_ref
10709 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10710 {
10711   rtx regs;
10712
10713   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10714     return 0;
10715
10716   regs = targetm.dwarf_register_span (rtl);
10717
10718   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10719     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10720   else
10721     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10722 }
10723
10724 /* Return a location descriptor that designates a machine register for
10725    a given hard register number.  */
10726
10727 static dw_loc_descr_ref
10728 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10729 {
10730   dw_loc_descr_ref reg_loc_descr;
10731
10732   if (regno <= 31)
10733     reg_loc_descr
10734       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10735   else
10736     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10737
10738   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10739     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10740
10741   return reg_loc_descr;
10742 }
10743
10744 /* Given an RTL of a register, return a location descriptor that
10745    designates a value that spans more than one register.  */
10746
10747 static dw_loc_descr_ref
10748 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10749                              enum var_init_status initialized)
10750 {
10751   int nregs, size, i;
10752   unsigned reg;
10753   dw_loc_descr_ref loc_result = NULL;
10754
10755   reg = REGNO (rtl);
10756 #ifdef LEAF_REG_REMAP
10757   if (current_function_uses_only_leaf_regs)
10758     {
10759       int leaf_reg = LEAF_REG_REMAP (reg);
10760       if (leaf_reg != -1)
10761         reg = (unsigned) leaf_reg;
10762     }
10763 #endif
10764   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10765   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10766
10767   /* Simple, contiguous registers.  */
10768   if (regs == NULL_RTX)
10769     {
10770       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10771
10772       loc_result = NULL;
10773       while (nregs--)
10774         {
10775           dw_loc_descr_ref t;
10776
10777           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10778                                       VAR_INIT_STATUS_INITIALIZED);
10779           add_loc_descr (&loc_result, t);
10780           add_loc_descr_op_piece (&loc_result, size);
10781           ++reg;
10782         }
10783       return loc_result;
10784     }
10785
10786   /* Now onto stupid register sets in non contiguous locations.  */
10787
10788   gcc_assert (GET_CODE (regs) == PARALLEL);
10789
10790   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10791   loc_result = NULL;
10792
10793   for (i = 0; i < XVECLEN (regs, 0); ++i)
10794     {
10795       dw_loc_descr_ref t;
10796
10797       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10798                                   VAR_INIT_STATUS_INITIALIZED);
10799       add_loc_descr (&loc_result, t);
10800       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10801       add_loc_descr_op_piece (&loc_result, size);
10802     }
10803
10804   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10805     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10806   return loc_result;
10807 }
10808
10809 #endif /* DWARF2_DEBUGGING_INFO */
10810
10811 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10812
10813 /* Return a location descriptor that designates a constant.  */
10814
10815 static dw_loc_descr_ref
10816 int_loc_descriptor (HOST_WIDE_INT i)
10817 {
10818   enum dwarf_location_atom op;
10819
10820   /* Pick the smallest representation of a constant, rather than just
10821      defaulting to the LEB encoding.  */
10822   if (i >= 0)
10823     {
10824       if (i <= 31)
10825         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10826       else if (i <= 0xff)
10827         op = DW_OP_const1u;
10828       else if (i <= 0xffff)
10829         op = DW_OP_const2u;
10830       else if (HOST_BITS_PER_WIDE_INT == 32
10831                || i <= 0xffffffff)
10832         op = DW_OP_const4u;
10833       else
10834         op = DW_OP_constu;
10835     }
10836   else
10837     {
10838       if (i >= -0x80)
10839         op = DW_OP_const1s;
10840       else if (i >= -0x8000)
10841         op = DW_OP_const2s;
10842       else if (HOST_BITS_PER_WIDE_INT == 32
10843                || i >= -0x80000000)
10844         op = DW_OP_const4s;
10845       else
10846         op = DW_OP_consts;
10847     }
10848
10849   return new_loc_descr (op, i, 0);
10850 }
10851 #endif
10852
10853 #ifdef DWARF2_DEBUGGING_INFO
10854
10855 /* Return a location descriptor that designates a base+offset location.  */
10856
10857 static dw_loc_descr_ref
10858 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
10859                  enum var_init_status initialized)
10860 {
10861   unsigned int regno;
10862   dw_loc_descr_ref result;
10863   dw_fde_ref fde = current_fde ();
10864
10865   /* We only use "frame base" when we're sure we're talking about the
10866      post-prologue local stack frame.  We do this by *not* running
10867      register elimination until this point, and recognizing the special
10868      argument pointer and soft frame pointer rtx's.  */
10869   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
10870     {
10871       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
10872
10873       if (elim != reg)
10874         {
10875           if (GET_CODE (elim) == PLUS)
10876             {
10877               offset += INTVAL (XEXP (elim, 1));
10878               elim = XEXP (elim, 0);
10879             }
10880           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
10881                        && (elim == hard_frame_pointer_rtx
10882                            || elim == stack_pointer_rtx))
10883                       || elim == (frame_pointer_needed
10884                                   ? hard_frame_pointer_rtx
10885                                   : stack_pointer_rtx));
10886
10887           /* If drap register is used to align stack, use frame
10888              pointer + offset to access stack variables.  If stack
10889              is aligned without drap, use stack pointer + offset to
10890              access stack variables.  */
10891           if (crtl->stack_realign_tried
10892               && reg == frame_pointer_rtx)
10893             {
10894               int base_reg
10895                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
10896                                       ? HARD_FRAME_POINTER_REGNUM
10897                                       : STACK_POINTER_REGNUM);
10898               return new_reg_loc_descr (base_reg, offset);
10899             }
10900
10901           offset += frame_pointer_fb_offset;
10902           return new_loc_descr (DW_OP_fbreg, offset, 0);
10903         }
10904     }
10905   else if (fde
10906            && fde->drap_reg != INVALID_REGNUM
10907            && (fde->drap_reg == REGNO (reg)
10908                || fde->vdrap_reg == REGNO (reg)))
10909     {
10910       /* Use cfa+offset to represent the location of arguments passed
10911          on stack when drap is used to align stack.  */
10912       return new_loc_descr (DW_OP_fbreg, offset, 0);
10913     }
10914
10915   regno = dbx_reg_number (reg);
10916   if (regno <= 31)
10917     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
10918                             offset, 0);
10919   else
10920     result = new_loc_descr (DW_OP_bregx, regno, offset);
10921
10922   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10923     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10924
10925   return result;
10926 }
10927
10928 /* Return true if this RTL expression describes a base+offset calculation.  */
10929
10930 static inline int
10931 is_based_loc (const_rtx rtl)
10932 {
10933   return (GET_CODE (rtl) == PLUS
10934           && ((REG_P (XEXP (rtl, 0))
10935                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
10936                && CONST_INT_P (XEXP (rtl, 1)))));
10937 }
10938
10939 /* Return a descriptor that describes the concatenation of N locations
10940    used to form the address of a memory location.  */
10941
10942 static dw_loc_descr_ref
10943 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
10944                             enum var_init_status initialized)
10945 {
10946   unsigned int i;
10947   dw_loc_descr_ref cc_loc_result = NULL;
10948   unsigned int n = XVECLEN (concatn, 0);
10949
10950   for (i = 0; i < n; ++i)
10951     {
10952       dw_loc_descr_ref ref;
10953       rtx x = XVECEXP (concatn, 0, i);
10954
10955       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
10956       if (ref == NULL)
10957         return NULL;
10958
10959       add_loc_descr (&cc_loc_result, ref);
10960       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
10961     }
10962
10963   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10964     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10965
10966   return cc_loc_result;
10967 }
10968
10969 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
10970    failed.  */
10971
10972 static dw_loc_descr_ref
10973 tls_mem_loc_descriptor (rtx mem)
10974 {
10975   tree base;
10976   dw_loc_descr_ref loc_result;
10977
10978   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
10979     return NULL;
10980
10981   base = get_base_address (MEM_EXPR (mem));
10982   if (base == NULL
10983       || TREE_CODE (base) != VAR_DECL
10984       || !DECL_THREAD_LOCAL_P (base))
10985     return NULL;
10986
10987   loc_result = loc_descriptor_from_tree_1 (MEM_EXPR (mem), 2);
10988   if (loc_result == NULL)
10989     return NULL;
10990
10991   if (INTVAL (MEM_OFFSET (mem)))
10992     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
10993
10994   return loc_result;
10995 }
10996
10997 /* The following routine converts the RTL for a variable or parameter
10998    (resident in memory) into an equivalent Dwarf representation of a
10999    mechanism for getting the address of that same variable onto the top of a
11000    hypothetical "address evaluation" stack.
11001
11002    When creating memory location descriptors, we are effectively transforming
11003    the RTL for a memory-resident object into its Dwarf postfix expression
11004    equivalent.  This routine recursively descends an RTL tree, turning
11005    it into Dwarf postfix code as it goes.
11006
11007    MODE is the mode of the memory reference, needed to handle some
11008    autoincrement addressing modes.
11009
11010    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
11011    location list for RTL.
11012
11013    Return 0 if we can't represent the location.  */
11014
11015 static dw_loc_descr_ref
11016 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11017                     enum var_init_status initialized)
11018 {
11019   dw_loc_descr_ref mem_loc_result = NULL;
11020   enum dwarf_location_atom op;
11021   dw_loc_descr_ref op0, op1;
11022
11023   /* Note that for a dynamically sized array, the location we will generate a
11024      description of here will be the lowest numbered location which is
11025      actually within the array.  That's *not* necessarily the same as the
11026      zeroth element of the array.  */
11027
11028   rtl = targetm.delegitimize_address (rtl);
11029
11030   switch (GET_CODE (rtl))
11031     {
11032     case POST_INC:
11033     case POST_DEC:
11034     case POST_MODIFY:
11035       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
11036          just fall into the SUBREG code.  */
11037
11038       /* ... fall through ...  */
11039
11040     case SUBREG:
11041       /* The case of a subreg may arise when we have a local (register)
11042          variable or a formal (register) parameter which doesn't quite fill
11043          up an entire register.  For now, just assume that it is
11044          legitimate to make the Dwarf info refer to the whole register which
11045          contains the given subreg.  */
11046       rtl = XEXP (rtl, 0);
11047       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
11048         break;
11049
11050       /* ... fall through ...  */
11051
11052     case REG:
11053       /* Whenever a register number forms a part of the description of the
11054          method for calculating the (dynamic) address of a memory resident
11055          object, DWARF rules require the register number be referred to as
11056          a "base register".  This distinction is not based in any way upon
11057          what category of register the hardware believes the given register
11058          belongs to.  This is strictly DWARF terminology we're dealing with
11059          here. Note that in cases where the location of a memory-resident
11060          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11061          OP_CONST (0)) the actual DWARF location descriptor that we generate
11062          may just be OP_BASEREG (basereg).  This may look deceptively like
11063          the object in question was allocated to a register (rather than in
11064          memory) so DWARF consumers need to be aware of the subtle
11065          distinction between OP_REG and OP_BASEREG.  */
11066       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11067         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11068       else if (stack_realign_drap
11069                && crtl->drap_reg
11070                && crtl->args.internal_arg_pointer == rtl
11071                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11072         {
11073           /* If RTL is internal_arg_pointer, which has been optimized
11074              out, use DRAP instead.  */
11075           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11076                                             VAR_INIT_STATUS_INITIALIZED);
11077         }
11078       break;
11079
11080     case SIGN_EXTEND:
11081     case ZERO_EXTEND:
11082       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11083                                 VAR_INIT_STATUS_INITIALIZED);
11084       if (op0 == 0)
11085         break;
11086       else
11087         {
11088           int shift = DWARF2_ADDR_SIZE
11089                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11090           shift *= BITS_PER_UNIT;
11091           if (GET_CODE (rtl) == SIGN_EXTEND)
11092             op = DW_OP_shra;
11093           else
11094             op = DW_OP_shr;
11095           mem_loc_result = op0;
11096           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11097           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11098           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11099           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11100         }
11101       break;
11102
11103     case MEM:
11104       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11105                                            VAR_INIT_STATUS_INITIALIZED);
11106       if (mem_loc_result == NULL)
11107         mem_loc_result = tls_mem_loc_descriptor (rtl);
11108       if (mem_loc_result != 0)
11109         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11110       break;
11111
11112     case LO_SUM:
11113          rtl = XEXP (rtl, 1);
11114
11115       /* ... fall through ...  */
11116
11117     case LABEL_REF:
11118       /* Some ports can transform a symbol ref into a label ref, because
11119          the symbol ref is too far away and has to be dumped into a constant
11120          pool.  */
11121     case CONST:
11122     case SYMBOL_REF:
11123       /* Alternatively, the symbol in the constant pool might be referenced
11124          by a different symbol.  */
11125       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
11126         {
11127           bool marked;
11128           rtx tmp = get_pool_constant_mark (rtl, &marked);
11129
11130           if (GET_CODE (tmp) == SYMBOL_REF)
11131             {
11132               rtl = tmp;
11133               if (CONSTANT_POOL_ADDRESS_P (tmp))
11134                 get_pool_constant_mark (tmp, &marked);
11135               else
11136                 marked = true;
11137             }
11138
11139           /* If all references to this pool constant were optimized away,
11140              it was not output and thus we can't represent it.
11141              FIXME: might try to use DW_OP_const_value here, though
11142              DW_OP_piece complicates it.  */
11143           if (!marked)
11144             return 0;
11145         }
11146
11147       if (GET_CODE (rtl) == SYMBOL_REF
11148           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11149         {
11150           dw_loc_descr_ref temp;
11151
11152           /* If this is not defined, we have no way to emit the data.  */
11153           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11154             break;
11155
11156           temp = new_loc_descr (DW_OP_addr, 0, 0);
11157           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11158           temp->dw_loc_oprnd1.v.val_addr = rtl;
11159           temp->dtprel = true;
11160
11161           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11162           add_loc_descr (&mem_loc_result, temp);
11163
11164           break;
11165         }
11166
11167     symref:
11168       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11169       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11170       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11171       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11172       break;
11173
11174     case PRE_MODIFY:
11175       /* Extract the PLUS expression nested inside and fall into
11176          PLUS code below.  */
11177       rtl = XEXP (rtl, 1);
11178       goto plus;
11179
11180     case PRE_INC:
11181     case PRE_DEC:
11182       /* Turn these into a PLUS expression and fall into the PLUS code
11183          below.  */
11184       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
11185                           GEN_INT (GET_CODE (rtl) == PRE_INC
11186                                    ? GET_MODE_UNIT_SIZE (mode)
11187                                    : -GET_MODE_UNIT_SIZE (mode)));
11188
11189       /* ... fall through ...  */
11190
11191     case PLUS:
11192     plus:
11193       if (is_based_loc (rtl))
11194         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11195                                           INTVAL (XEXP (rtl, 1)),
11196                                           VAR_INIT_STATUS_INITIALIZED);
11197       else
11198         {
11199           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
11200                                                VAR_INIT_STATUS_INITIALIZED);
11201           if (mem_loc_result == 0)
11202             break;
11203
11204           if (CONST_INT_P (XEXP (rtl, 1)))
11205             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11206           else
11207             {
11208               dw_loc_descr_ref mem_loc_result2
11209                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11210                                       VAR_INIT_STATUS_INITIALIZED);
11211               if (mem_loc_result2 == 0)
11212                 break;
11213               add_loc_descr (&mem_loc_result, mem_loc_result2);
11214               add_loc_descr (&mem_loc_result,
11215                              new_loc_descr (DW_OP_plus, 0, 0));
11216             }
11217         }
11218       break;
11219
11220     /* If a pseudo-reg is optimized away, it is possible for it to
11221        be replaced with a MEM containing a multiply or shift.  */
11222     case MINUS:
11223       op = DW_OP_minus;
11224       goto do_binop;
11225
11226     case MULT:
11227       op = DW_OP_mul;
11228       goto do_binop;
11229
11230     case DIV:
11231       op = DW_OP_div;
11232       goto do_binop;
11233
11234     case MOD:
11235       op = DW_OP_mod;
11236       goto do_binop;
11237
11238     case ASHIFT:
11239       op = DW_OP_shl;
11240       goto do_binop;
11241
11242     case ASHIFTRT:
11243       op = DW_OP_shra;
11244       goto do_binop;
11245
11246     case LSHIFTRT:
11247       op = DW_OP_shr;
11248       goto do_binop;
11249
11250     case AND:
11251       op = DW_OP_and;
11252       goto do_binop;
11253
11254     case IOR:
11255       op = DW_OP_or;
11256       goto do_binop;
11257
11258     case XOR:
11259       op = DW_OP_xor;
11260       goto do_binop;
11261
11262     do_binop:
11263       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11264                                 VAR_INIT_STATUS_INITIALIZED);
11265       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11266                                 VAR_INIT_STATUS_INITIALIZED);
11267
11268       if (op0 == 0 || op1 == 0)
11269         break;
11270
11271       mem_loc_result = op0;
11272       add_loc_descr (&mem_loc_result, op1);
11273       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11274       break;
11275
11276     case NOT:
11277       op = DW_OP_not;
11278       goto do_unop;
11279
11280     case ABS:
11281       op = DW_OP_abs;
11282       goto do_unop;
11283
11284     case NEG:
11285       op = DW_OP_neg;
11286       goto do_unop;
11287
11288     do_unop:
11289       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11290                                 VAR_INIT_STATUS_INITIALIZED);
11291
11292       if (op0 == 0)
11293         break;
11294
11295       mem_loc_result = op0;
11296       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11297       break;
11298
11299     case CONST_INT:
11300       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11301       break;
11302
11303     case CONCATN:
11304       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode,
11305                                                    VAR_INIT_STATUS_INITIALIZED);
11306       break;
11307
11308     case EQ:
11309       op = DW_OP_eq;
11310       goto do_scompare;
11311
11312     case GE:
11313       op = DW_OP_ge;
11314       goto do_scompare;
11315
11316     case GT:
11317       op = DW_OP_gt;
11318       goto do_scompare;
11319
11320     case LE:
11321       op = DW_OP_le;
11322       goto do_scompare;
11323
11324     case LT:
11325       op = DW_OP_lt;
11326       goto do_scompare;
11327
11328     case NE:
11329       op = DW_OP_ne;
11330       goto do_scompare;
11331
11332     do_scompare:
11333       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11334           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11335           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11336         break;
11337
11338       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11339                                 VAR_INIT_STATUS_INITIALIZED);
11340       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11341                                 VAR_INIT_STATUS_INITIALIZED);
11342
11343       if (op0 == 0 || op1 == 0)
11344         break;
11345
11346       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11347         {
11348           int shift = DWARF2_ADDR_SIZE
11349                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11350           shift *= BITS_PER_UNIT;
11351           add_loc_descr (&op0, int_loc_descriptor (shift));
11352           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11353           if (CONST_INT_P (XEXP (rtl, 1)))
11354             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11355           else
11356             {
11357               add_loc_descr (&op1, int_loc_descriptor (shift));
11358               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11359             }
11360         }
11361
11362     do_compare:
11363       mem_loc_result = op0;
11364       add_loc_descr (&mem_loc_result, op1);
11365       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11366       if (STORE_FLAG_VALUE != 1)
11367         {
11368           add_loc_descr (&mem_loc_result,
11369                          int_loc_descriptor (STORE_FLAG_VALUE));
11370           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11371         }
11372       break;
11373
11374     case GEU:
11375       op = DW_OP_ge;
11376       goto do_ucompare;
11377
11378     case GTU:
11379       op = DW_OP_gt;
11380       goto do_ucompare;
11381
11382     case LEU:
11383       op = DW_OP_le;
11384       goto do_ucompare;
11385
11386     case LTU:
11387       op = DW_OP_lt;
11388       goto do_ucompare;
11389
11390     do_ucompare:
11391       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11392           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11393           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11394         break;
11395
11396       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11397                                 VAR_INIT_STATUS_INITIALIZED);
11398       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11399                                 VAR_INIT_STATUS_INITIALIZED);
11400
11401       if (op0 == 0 || op1 == 0)
11402         break;
11403
11404       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11405         {
11406           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11407           add_loc_descr (&op0, int_loc_descriptor (mask));
11408           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11409           if (CONST_INT_P (XEXP (rtl, 1)))
11410             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11411           else
11412             {
11413               add_loc_descr (&op1, int_loc_descriptor (mask));
11414               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11415             }
11416         }
11417       else
11418         {
11419           HOST_WIDE_INT bias = 1;
11420           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11421           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11422           if (CONST_INT_P (XEXP (rtl, 1)))
11423             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11424                                       + INTVAL (XEXP (rtl, 1)));
11425           else
11426             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11427         }
11428       goto do_compare;
11429
11430     case SMIN:
11431     case SMAX:
11432     case UMIN:
11433     case UMAX:
11434       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11435           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11436           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11437         break;
11438
11439       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11440                                 VAR_INIT_STATUS_INITIALIZED);
11441       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11442                                 VAR_INIT_STATUS_INITIALIZED);
11443
11444       if (op0 == 0 || op1 == 0)
11445         break;
11446
11447       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11448       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11449       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11450       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11451         {
11452           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11453             {
11454               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11455               add_loc_descr (&op0, int_loc_descriptor (mask));
11456               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11457               add_loc_descr (&op1, int_loc_descriptor (mask));
11458               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11459             }
11460           else
11461             {
11462               HOST_WIDE_INT bias = 1;
11463               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11464               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11465               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11466             }
11467         }
11468       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11469         {
11470           int shift = DWARF2_ADDR_SIZE
11471                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11472           shift *= BITS_PER_UNIT;
11473           add_loc_descr (&op0, int_loc_descriptor (shift));
11474           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11475           add_loc_descr (&op1, int_loc_descriptor (shift));
11476           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11477         }
11478
11479       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11480         op = DW_OP_lt;
11481       else
11482         op = DW_OP_gt;
11483       mem_loc_result = op0;
11484       add_loc_descr (&mem_loc_result, op1);
11485       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11486       {
11487         dw_loc_descr_ref bra_node, drop_node;
11488
11489         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11490         add_loc_descr (&mem_loc_result, bra_node);
11491         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11492         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11493         add_loc_descr (&mem_loc_result, drop_node);
11494         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11495         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11496       }
11497       break;
11498
11499     case ZERO_EXTRACT:
11500     case SIGN_EXTRACT:
11501       if (CONST_INT_P (XEXP (rtl, 1))
11502           && CONST_INT_P (XEXP (rtl, 2))
11503           && ((unsigned) INTVAL (XEXP (rtl, 1))
11504               + (unsigned) INTVAL (XEXP (rtl, 2))
11505               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
11506           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11507           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11508         {
11509           int shift, size;
11510           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11511                                     VAR_INIT_STATUS_INITIALIZED);
11512           if (op0 == 0)
11513             break;
11514           if (GET_CODE (rtl) == SIGN_EXTRACT)
11515             op = DW_OP_shra;
11516           else
11517             op = DW_OP_shr;
11518           mem_loc_result = op0;
11519           size = INTVAL (XEXP (rtl, 1));
11520           shift = INTVAL (XEXP (rtl, 2));
11521           if (BITS_BIG_ENDIAN)
11522             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11523                     - shift - size;
11524           add_loc_descr (&mem_loc_result,
11525                          int_loc_descriptor (DWARF2_ADDR_SIZE - shift - size));
11526           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11527           add_loc_descr (&mem_loc_result,
11528                          int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11529           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11530         }
11531       break;
11532
11533     case COMPARE:
11534     case IF_THEN_ELSE:
11535     case ROTATE:
11536     case ROTATERT:
11537     case TRUNCATE:
11538       /* In theory, we could implement the above.  */
11539       /* DWARF cannot represent the unsigned compare operations
11540          natively.  */
11541     case SS_MULT:
11542     case US_MULT:
11543     case SS_DIV:
11544     case US_DIV:
11545     case UDIV:
11546     case UMOD:
11547     case UNORDERED:
11548     case ORDERED:
11549     case UNEQ:
11550     case UNGE:
11551     case UNLE:
11552     case UNLT:
11553     case LTGT:
11554     case FLOAT_EXTEND:
11555     case FLOAT_TRUNCATE:
11556     case FLOAT:
11557     case UNSIGNED_FLOAT:
11558     case FIX:
11559     case UNSIGNED_FIX:
11560     case FRACT_CONVERT:
11561     case UNSIGNED_FRACT_CONVERT:
11562     case SAT_FRACT:
11563     case UNSIGNED_SAT_FRACT:
11564     case SQRT:
11565     case BSWAP:
11566     case FFS:
11567     case CLZ:
11568     case CTZ:
11569     case POPCOUNT:
11570     case PARITY:
11571     case ASM_OPERANDS:
11572     case UNSPEC:
11573       /* If delegitimize_address couldn't do anything with the UNSPEC, we
11574          can't express it in the debug info.  This can happen e.g. with some
11575          TLS UNSPECs.  */
11576       break;
11577
11578     case CONST_STRING:
11579       rtl = get_debug_string_label (XSTR (rtl, 0));
11580       goto symref;
11581
11582     default:
11583 #ifdef ENABLE_CHECKING
11584       print_rtl (stderr, rtl);
11585       gcc_unreachable ();
11586 #else
11587       break;
11588 #endif
11589     }
11590
11591   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11592     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11593
11594   return mem_loc_result;
11595 }
11596
11597 /* Return a descriptor that describes the concatenation of two locations.
11598    This is typically a complex variable.  */
11599
11600 static dw_loc_descr_ref
11601 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
11602 {
11603   dw_loc_descr_ref cc_loc_result = NULL;
11604   dw_loc_descr_ref x0_ref
11605     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11606   dw_loc_descr_ref x1_ref
11607     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11608
11609   if (x0_ref == 0 || x1_ref == 0)
11610     return 0;
11611
11612   cc_loc_result = x0_ref;
11613   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
11614
11615   add_loc_descr (&cc_loc_result, x1_ref);
11616   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
11617
11618   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11619     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11620
11621   return cc_loc_result;
11622 }
11623
11624 /* Return a descriptor that describes the concatenation of N
11625    locations.  */
11626
11627 static dw_loc_descr_ref
11628 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
11629 {
11630   unsigned int i;
11631   dw_loc_descr_ref cc_loc_result = NULL;
11632   unsigned int n = XVECLEN (concatn, 0);
11633
11634   for (i = 0; i < n; ++i)
11635     {
11636       dw_loc_descr_ref ref;
11637       rtx x = XVECEXP (concatn, 0, i);
11638
11639       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11640       if (ref == NULL)
11641         return NULL;
11642
11643       add_loc_descr (&cc_loc_result, ref);
11644       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
11645     }
11646
11647   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11648     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11649
11650   return cc_loc_result;
11651 }
11652
11653 /* Output a proper Dwarf location descriptor for a variable or parameter
11654    which is either allocated in a register or in a memory location.  For a
11655    register, we just generate an OP_REG and the register number.  For a
11656    memory location we provide a Dwarf postfix expression describing how to
11657    generate the (dynamic) address of the object onto the address stack.
11658
11659    MODE is mode of the decl if this loc_descriptor is going to be used in
11660    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
11661    allowed, VOIDmode otherwise.
11662
11663    If we don't know how to describe it, return 0.  */
11664
11665 static dw_loc_descr_ref
11666 loc_descriptor (rtx rtl, enum machine_mode mode,
11667                 enum var_init_status initialized)
11668 {
11669   dw_loc_descr_ref loc_result = NULL;
11670
11671   switch (GET_CODE (rtl))
11672     {
11673     case SUBREG:
11674     case SIGN_EXTEND:
11675     case ZERO_EXTEND:
11676       /* The case of a subreg may arise when we have a local (register)
11677          variable or a formal (register) parameter which doesn't quite fill
11678          up an entire register.  For now, just assume that it is
11679          legitimate to make the Dwarf info refer to the whole register which
11680          contains the given subreg.  */
11681       rtl = SUBREG_REG (rtl);
11682
11683       /* ... fall through ...  */
11684
11685     case REG:
11686       loc_result = reg_loc_descriptor (rtl, initialized);
11687       break;
11688
11689     case MEM:
11690       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11691                                        initialized);
11692       if (loc_result == NULL)
11693         loc_result = tls_mem_loc_descriptor (rtl);
11694       break;
11695
11696     case CONCAT:
11697       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
11698                                           initialized);
11699       break;
11700
11701     case CONCATN:
11702       loc_result = concatn_loc_descriptor (rtl, initialized);
11703       break;
11704
11705     case VAR_LOCATION:
11706       /* Single part.  */
11707       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
11708         {
11709           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
11710                                        initialized);
11711           break;
11712         }
11713
11714       rtl = XEXP (rtl, 1);
11715       /* FALLTHRU */
11716
11717     case PARALLEL:
11718       {
11719         rtvec par_elems = XVEC (rtl, 0);
11720         int num_elem = GET_NUM_ELEM (par_elems);
11721         enum machine_mode mode;
11722         int i;
11723
11724         /* Create the first one, so we have something to add to.  */
11725         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11726                                      VOIDmode, initialized);
11727         if (loc_result == NULL)
11728           return NULL;
11729         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11730         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11731         for (i = 1; i < num_elem; i++)
11732           {
11733             dw_loc_descr_ref temp;
11734
11735             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11736                                    VOIDmode, initialized);
11737             if (temp == NULL)
11738               return NULL;
11739             add_loc_descr (&loc_result, temp);
11740             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11741             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11742           }
11743       }
11744       break;
11745
11746     case CONST_INT:
11747       if (mode != VOIDmode && mode != BLKmode && dwarf_version >= 4)
11748         {
11749           HOST_WIDE_INT i = INTVAL (rtl);
11750           int litsize;
11751           if (i >= 0)
11752             {
11753               if (i <= 31)
11754                 litsize = 1;
11755               else if (i <= 0xff)
11756                 litsize = 2;
11757               else if (i <= 0xffff)
11758                 litsize = 3;
11759               else if (HOST_BITS_PER_WIDE_INT == 32
11760                        || i <= 0xffffffff)
11761                 litsize = 5;
11762               else
11763                 litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
11764             }
11765           else
11766             {
11767               if (i >= -0x80)
11768                 litsize = 2;
11769               else if (i >= -0x8000)
11770                 litsize = 3;
11771               else if (HOST_BITS_PER_WIDE_INT == 32
11772                        || i >= -0x80000000)
11773                 litsize = 5;
11774               else
11775                 litsize = 1 + size_of_sleb128 (i);
11776             }
11777           /* Determine if DW_OP_stack_value or DW_OP_implicit_value
11778              is more compact.  For DW_OP_stack_value we need:
11779              litsize + 1 (DW_OP_stack_value) + 1 (DW_OP_bit_size)
11780              + 1 (mode size)
11781              and for DW_OP_implicit_value:
11782              1 (DW_OP_implicit_value) + 1 (length) + mode_size.  */
11783           if (DWARF2_ADDR_SIZE >= GET_MODE_SIZE (mode)
11784               && litsize + 1 + 1 + 1 < 1 + 1 + GET_MODE_SIZE (mode))
11785             {
11786               loc_result = int_loc_descriptor (i);
11787               add_loc_descr (&loc_result,
11788                              new_loc_descr (DW_OP_stack_value, 0, 0));
11789               add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11790               return loc_result;
11791             }
11792
11793           loc_result = new_loc_descr (DW_OP_implicit_value,
11794                                       GET_MODE_SIZE (mode), 0);
11795           loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
11796           loc_result->dw_loc_oprnd2.v.val_int = i;
11797         }
11798       break;
11799
11800     case CONST_DOUBLE:
11801       if (mode != VOIDmode && dwarf_version >= 4)
11802         {
11803           /* Note that a CONST_DOUBLE rtx could represent either an integer
11804              or a floating-point constant.  A CONST_DOUBLE is used whenever
11805              the constant requires more than one word in order to be
11806              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11807           if (GET_MODE (rtl) != VOIDmode)
11808             mode = GET_MODE (rtl);
11809
11810           loc_result = new_loc_descr (DW_OP_implicit_value,
11811                                       GET_MODE_SIZE (mode), 0);
11812           if (SCALAR_FLOAT_MODE_P (mode))
11813             {
11814               unsigned int length = GET_MODE_SIZE (mode);
11815               unsigned char *array = GGC_NEWVEC (unsigned char, length);
11816
11817               insert_float (rtl, array);
11818               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11819               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11820               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11821               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11822             }
11823           else
11824             {
11825               loc_result->dw_loc_oprnd2.val_class = dw_val_class_long_long;
11826               loc_result->dw_loc_oprnd2.v.val_long_long = rtl;
11827             }
11828         }
11829       break;
11830
11831     case CONST_VECTOR:
11832       if (mode != VOIDmode && dwarf_version >= 4)
11833         {
11834           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
11835           unsigned int length = CONST_VECTOR_NUNITS (rtl);
11836           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
11837           unsigned int i;
11838           unsigned char *p;
11839
11840           mode = GET_MODE (rtl);
11841           switch (GET_MODE_CLASS (mode))
11842             {
11843             case MODE_VECTOR_INT:
11844               for (i = 0, p = array; i < length; i++, p += elt_size)
11845                 {
11846                   rtx elt = CONST_VECTOR_ELT (rtl, i);
11847                   HOST_WIDE_INT lo, hi;
11848
11849                   switch (GET_CODE (elt))
11850                     {
11851                     case CONST_INT:
11852                       lo = INTVAL (elt);
11853                       hi = -(lo < 0);
11854                       break;
11855
11856                     case CONST_DOUBLE:
11857                       lo = CONST_DOUBLE_LOW (elt);
11858                       hi = CONST_DOUBLE_HIGH (elt);
11859                       break;
11860
11861                     default:
11862                       gcc_unreachable ();
11863                     }
11864
11865                   if (elt_size <= sizeof (HOST_WIDE_INT))
11866                     insert_int (lo, elt_size, p);
11867                   else
11868                     {
11869                       unsigned char *p0 = p;
11870                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
11871
11872                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
11873                       if (WORDS_BIG_ENDIAN)
11874                         {
11875                           p0 = p1;
11876                           p1 = p;
11877                         }
11878                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
11879                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
11880                     }
11881                 }
11882               break;
11883
11884             case MODE_VECTOR_FLOAT:
11885               for (i = 0, p = array; i < length; i++, p += elt_size)
11886                 {
11887                   rtx elt = CONST_VECTOR_ELT (rtl, i);
11888                   insert_float (elt, p);
11889                 }
11890               break;
11891
11892             default:
11893               gcc_unreachable ();
11894             }
11895
11896           loc_result = new_loc_descr (DW_OP_implicit_value,
11897                                       length * elt_size, 0);
11898           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11899           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
11900           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
11901           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11902         }
11903       break;
11904
11905     case CONST:
11906       if (mode == VOIDmode
11907           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
11908           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
11909           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
11910         {
11911           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11912           break;
11913         }
11914       /* FALLTHROUGH */
11915     case SYMBOL_REF:
11916       if (GET_CODE (rtl) == SYMBOL_REF
11917           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11918         break;
11919     case LABEL_REF:
11920       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
11921           && dwarf_version >= 4)
11922         {
11923           loc_result = new_loc_descr (DW_OP_implicit_value,
11924                                       DWARF2_ADDR_SIZE, 0);
11925           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
11926           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
11927           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11928         }
11929       break;
11930
11931     default:
11932       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
11933           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11934           && dwarf_version >= 4)
11935         {
11936           /* Value expression.  */
11937           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
11938           if (loc_result)
11939             {
11940               add_loc_descr (&loc_result,
11941                              new_loc_descr (DW_OP_stack_value, 0, 0));
11942               add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11943             }
11944         }
11945       break;
11946     }
11947
11948   return loc_result;
11949 }
11950
11951 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
11952    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
11953    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
11954    top-level invocation, and we require the address of LOC; is 0 if we require
11955    the value of LOC.  */
11956
11957 static dw_loc_descr_ref
11958 loc_descriptor_from_tree_1 (tree loc, int want_address)
11959 {
11960   dw_loc_descr_ref ret, ret1;
11961   int have_address = 0;
11962   enum dwarf_location_atom op;
11963
11964   /* ??? Most of the time we do not take proper care for sign/zero
11965      extending the values properly.  Hopefully this won't be a real
11966      problem...  */
11967
11968   switch (TREE_CODE (loc))
11969     {
11970     case ERROR_MARK:
11971       return 0;
11972
11973     case PLACEHOLDER_EXPR:
11974       /* This case involves extracting fields from an object to determine the
11975          position of other fields.  We don't try to encode this here.  The
11976          only user of this is Ada, which encodes the needed information using
11977          the names of types.  */
11978       return 0;
11979
11980     case CALL_EXPR:
11981       return 0;
11982
11983     case PREINCREMENT_EXPR:
11984     case PREDECREMENT_EXPR:
11985     case POSTINCREMENT_EXPR:
11986     case POSTDECREMENT_EXPR:
11987       /* There are no opcodes for these operations.  */
11988       return 0;
11989
11990     case ADDR_EXPR:
11991       /* If we already want an address, there's nothing we can do.  */
11992       if (want_address)
11993         return 0;
11994
11995       /* Otherwise, process the argument and look for the address.  */
11996       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
11997
11998     case VAR_DECL:
11999       if (DECL_THREAD_LOCAL_P (loc))
12000         {
12001           rtx rtl;
12002           enum dwarf_location_atom first_op;
12003           enum dwarf_location_atom second_op;
12004           bool dtprel = false;
12005
12006           if (targetm.have_tls)
12007             {
12008               /* If this is not defined, we have no way to emit the
12009                  data.  */
12010               if (!targetm.asm_out.output_dwarf_dtprel)
12011                 return 0;
12012
12013                /* The way DW_OP_GNU_push_tls_address is specified, we
12014                   can only look up addresses of objects in the current
12015                   module.  */
12016               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
12017                 return 0;
12018               first_op = DW_OP_addr;
12019               dtprel = true;
12020               second_op = DW_OP_GNU_push_tls_address;
12021             }
12022           else
12023             {
12024               if (!targetm.emutls.debug_form_tls_address)
12025                 return 0;
12026               loc = emutls_decl (loc);
12027               first_op = DW_OP_addr;
12028               second_op = DW_OP_form_tls_address;
12029             }
12030
12031           rtl = rtl_for_decl_location (loc);
12032           if (rtl == NULL_RTX)
12033             return 0;
12034
12035           if (!MEM_P (rtl))
12036             return 0;
12037           rtl = XEXP (rtl, 0);
12038           if (! CONSTANT_P (rtl))
12039             return 0;
12040
12041           ret = new_loc_descr (first_op, 0, 0);
12042           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12043           ret->dw_loc_oprnd1.v.val_addr = rtl;
12044           ret->dtprel = dtprel;
12045
12046           ret1 = new_loc_descr (second_op, 0, 0);
12047           add_loc_descr (&ret, ret1);
12048
12049           have_address = 1;
12050           break;
12051         }
12052       /* FALLTHRU */
12053
12054     case PARM_DECL:
12055       if (DECL_HAS_VALUE_EXPR_P (loc))
12056         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
12057                                            want_address);
12058       /* FALLTHRU */
12059
12060     case RESULT_DECL:
12061     case FUNCTION_DECL:
12062       {
12063         rtx rtl = rtl_for_decl_location (loc);
12064
12065         if (rtl == NULL_RTX)
12066           return 0;
12067         else if (CONST_INT_P (rtl))
12068           {
12069             HOST_WIDE_INT val = INTVAL (rtl);
12070             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
12071               val &= GET_MODE_MASK (DECL_MODE (loc));
12072             ret = int_loc_descriptor (val);
12073           }
12074         else if (GET_CODE (rtl) == CONST_STRING)
12075           return 0;
12076         else if (CONSTANT_P (rtl))
12077           {
12078             ret = new_loc_descr (DW_OP_addr, 0, 0);
12079             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12080             ret->dw_loc_oprnd1.v.val_addr = rtl;
12081           }
12082         else
12083           {
12084             enum machine_mode mode;
12085
12086             /* Certain constructs can only be represented at top-level.  */
12087             if (want_address == 2)
12088               return loc_descriptor (rtl, VOIDmode,
12089                                      VAR_INIT_STATUS_INITIALIZED);
12090
12091             mode = GET_MODE (rtl);
12092             if (MEM_P (rtl))
12093               {
12094                 rtl = XEXP (rtl, 0);
12095                 have_address = 1;
12096               }
12097             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12098           }
12099       }
12100       break;
12101
12102     case INDIRECT_REF:
12103       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12104       have_address = 1;
12105       break;
12106
12107     case COMPOUND_EXPR:
12108       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
12109
12110     CASE_CONVERT:
12111     case VIEW_CONVERT_EXPR:
12112     case SAVE_EXPR:
12113     case MODIFY_EXPR:
12114       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), want_address);
12115
12116     case COMPONENT_REF:
12117     case BIT_FIELD_REF:
12118     case ARRAY_REF:
12119     case ARRAY_RANGE_REF:
12120       {
12121         tree obj, offset;
12122         HOST_WIDE_INT bitsize, bitpos, bytepos;
12123         enum machine_mode mode;
12124         int volatilep;
12125         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12126
12127         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
12128                                    &unsignedp, &volatilep, false);
12129
12130         if (obj == loc)
12131           return 0;
12132
12133         ret = loc_descriptor_from_tree_1 (obj, 1);
12134         if (ret == 0
12135             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
12136           return 0;
12137
12138         if (offset != NULL_TREE)
12139           {
12140             /* Variable offset.  */
12141             ret1 = loc_descriptor_from_tree_1 (offset, 0);
12142             if (ret1 == 0)
12143               return 0;
12144             add_loc_descr (&ret, ret1);
12145             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
12146           }
12147
12148         bytepos = bitpos / BITS_PER_UNIT;
12149         loc_descr_plus_const (&ret, bytepos);
12150
12151         have_address = 1;
12152         break;
12153       }
12154
12155     case INTEGER_CST:
12156       if (host_integerp (loc, 0))
12157         ret = int_loc_descriptor (tree_low_cst (loc, 0));
12158       else
12159         return 0;
12160       break;
12161
12162     case CONSTRUCTOR:
12163       {
12164         /* Get an RTL for this, if something has been emitted.  */
12165         rtx rtl = lookup_constant_def (loc);
12166         enum machine_mode mode;
12167
12168         if (!rtl || !MEM_P (rtl))
12169           return 0;
12170         mode = GET_MODE (rtl);
12171         rtl = XEXP (rtl, 0);
12172         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12173         have_address = 1;
12174         break;
12175       }
12176
12177     case TRUTH_AND_EXPR:
12178     case TRUTH_ANDIF_EXPR:
12179     case BIT_AND_EXPR:
12180       op = DW_OP_and;
12181       goto do_binop;
12182
12183     case TRUTH_XOR_EXPR:
12184     case BIT_XOR_EXPR:
12185       op = DW_OP_xor;
12186       goto do_binop;
12187
12188     case TRUTH_OR_EXPR:
12189     case TRUTH_ORIF_EXPR:
12190     case BIT_IOR_EXPR:
12191       op = DW_OP_or;
12192       goto do_binop;
12193
12194     case FLOOR_DIV_EXPR:
12195     case CEIL_DIV_EXPR:
12196     case ROUND_DIV_EXPR:
12197     case TRUNC_DIV_EXPR:
12198       op = DW_OP_div;
12199       goto do_binop;
12200
12201     case MINUS_EXPR:
12202       op = DW_OP_minus;
12203       goto do_binop;
12204
12205     case FLOOR_MOD_EXPR:
12206     case CEIL_MOD_EXPR:
12207     case ROUND_MOD_EXPR:
12208     case TRUNC_MOD_EXPR:
12209       op = DW_OP_mod;
12210       goto do_binop;
12211
12212     case MULT_EXPR:
12213       op = DW_OP_mul;
12214       goto do_binop;
12215
12216     case LSHIFT_EXPR:
12217       op = DW_OP_shl;
12218       goto do_binop;
12219
12220     case RSHIFT_EXPR:
12221       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
12222       goto do_binop;
12223
12224     case POINTER_PLUS_EXPR:
12225     case PLUS_EXPR:
12226       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
12227           && host_integerp (TREE_OPERAND (loc, 1), 0))
12228         {
12229           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12230           if (ret == 0)
12231             return 0;
12232
12233           loc_descr_plus_const (&ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
12234           break;
12235         }
12236
12237       op = DW_OP_plus;
12238       goto do_binop;
12239
12240     case LE_EXPR:
12241       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12242         return 0;
12243
12244       op = DW_OP_le;
12245       goto do_binop;
12246
12247     case GE_EXPR:
12248       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12249         return 0;
12250
12251       op = DW_OP_ge;
12252       goto do_binop;
12253
12254     case LT_EXPR:
12255       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12256         return 0;
12257
12258       op = DW_OP_lt;
12259       goto do_binop;
12260
12261     case GT_EXPR:
12262       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12263         return 0;
12264
12265       op = DW_OP_gt;
12266       goto do_binop;
12267
12268     case EQ_EXPR:
12269       op = DW_OP_eq;
12270       goto do_binop;
12271
12272     case NE_EXPR:
12273       op = DW_OP_ne;
12274       goto do_binop;
12275
12276     do_binop:
12277       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12278       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
12279       if (ret == 0 || ret1 == 0)
12280         return 0;
12281
12282       add_loc_descr (&ret, ret1);
12283       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12284       break;
12285
12286     case TRUTH_NOT_EXPR:
12287     case BIT_NOT_EXPR:
12288       op = DW_OP_not;
12289       goto do_unop;
12290
12291     case ABS_EXPR:
12292       op = DW_OP_abs;
12293       goto do_unop;
12294
12295     case NEGATE_EXPR:
12296       op = DW_OP_neg;
12297       goto do_unop;
12298
12299     do_unop:
12300       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12301       if (ret == 0)
12302         return 0;
12303
12304       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
12305       break;
12306
12307     case MIN_EXPR:
12308     case MAX_EXPR:
12309       {
12310         const enum tree_code code =
12311           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
12312
12313         loc = build3 (COND_EXPR, TREE_TYPE (loc),
12314                       build2 (code, integer_type_node,
12315                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
12316                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
12317       }
12318
12319       /* ... fall through ...  */
12320
12321     case COND_EXPR:
12322       {
12323         dw_loc_descr_ref lhs
12324           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
12325         dw_loc_descr_ref rhs
12326           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
12327         dw_loc_descr_ref bra_node, jump_node, tmp;
12328
12329         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
12330         if (ret == 0 || lhs == 0 || rhs == 0)
12331           return 0;
12332
12333         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12334         add_loc_descr (&ret, bra_node);
12335
12336         add_loc_descr (&ret, rhs);
12337         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
12338         add_loc_descr (&ret, jump_node);
12339
12340         add_loc_descr (&ret, lhs);
12341         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12342         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
12343
12344         /* ??? Need a node to point the skip at.  Use a nop.  */
12345         tmp = new_loc_descr (DW_OP_nop, 0, 0);
12346         add_loc_descr (&ret, tmp);
12347         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12348         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
12349       }
12350       break;
12351
12352     case FIX_TRUNC_EXPR:
12353       return 0;
12354
12355     default:
12356       /* Leave front-end specific codes as simply unknown.  This comes
12357          up, for instance, with the C STMT_EXPR.  */
12358       if ((unsigned int) TREE_CODE (loc)
12359           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
12360         return 0;
12361
12362 #ifdef ENABLE_CHECKING
12363       /* Otherwise this is a generic code; we should just lists all of
12364          these explicitly.  We forgot one.  */
12365       gcc_unreachable ();
12366 #else
12367       /* In a release build, we want to degrade gracefully: better to
12368          generate incomplete debugging information than to crash.  */
12369       return NULL;
12370 #endif
12371     }
12372
12373   /* Show if we can't fill the request for an address.  */
12374   if (want_address && !have_address)
12375     return 0;
12376
12377   /* If we've got an address and don't want one, dereference.  */
12378   if (!want_address && have_address && ret)
12379     {
12380       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
12381
12382       if (size > DWARF2_ADDR_SIZE || size == -1)
12383         return 0;
12384       else if (size == DWARF2_ADDR_SIZE)
12385         op = DW_OP_deref;
12386       else
12387         op = DW_OP_deref_size;
12388
12389       add_loc_descr (&ret, new_loc_descr (op, size, 0));
12390     }
12391
12392   return ret;
12393 }
12394
12395 static inline dw_loc_descr_ref
12396 loc_descriptor_from_tree (tree loc)
12397 {
12398   return loc_descriptor_from_tree_1 (loc, 2);
12399 }
12400
12401 /* Given a value, round it up to the lowest multiple of `boundary'
12402    which is not less than the value itself.  */
12403
12404 static inline HOST_WIDE_INT
12405 ceiling (HOST_WIDE_INT value, unsigned int boundary)
12406 {
12407   return (((value + boundary - 1) / boundary) * boundary);
12408 }
12409
12410 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
12411    pointer to the declared type for the relevant field variable, or return
12412    `integer_type_node' if the given node turns out to be an
12413    ERROR_MARK node.  */
12414
12415 static inline tree
12416 field_type (const_tree decl)
12417 {
12418   tree type;
12419
12420   if (TREE_CODE (decl) == ERROR_MARK)
12421     return integer_type_node;
12422
12423   type = DECL_BIT_FIELD_TYPE (decl);
12424   if (type == NULL_TREE)
12425     type = TREE_TYPE (decl);
12426
12427   return type;
12428 }
12429
12430 /* Given a pointer to a tree node, return the alignment in bits for
12431    it, or else return BITS_PER_WORD if the node actually turns out to
12432    be an ERROR_MARK node.  */
12433
12434 static inline unsigned
12435 simple_type_align_in_bits (const_tree type)
12436 {
12437   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
12438 }
12439
12440 static inline unsigned
12441 simple_decl_align_in_bits (const_tree decl)
12442 {
12443   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
12444 }
12445
12446 /* Return the result of rounding T up to ALIGN.  */
12447
12448 static inline HOST_WIDE_INT
12449 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
12450 {
12451   /* We must be careful if T is negative because HOST_WIDE_INT can be
12452      either "above" or "below" unsigned int as per the C promotion
12453      rules, depending on the host, thus making the signedness of the
12454      direct multiplication and division unpredictable.  */
12455   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
12456
12457   u += align - 1;
12458   u /= align;
12459   u *= align;
12460
12461   return (HOST_WIDE_INT) u;
12462 }
12463
12464 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
12465    lowest addressed byte of the "containing object" for the given FIELD_DECL,
12466    or return 0 if we are unable to determine what that offset is, either
12467    because the argument turns out to be a pointer to an ERROR_MARK node, or
12468    because the offset is actually variable.  (We can't handle the latter case
12469    just yet).  */
12470
12471 static HOST_WIDE_INT
12472 field_byte_offset (const_tree decl)
12473 {
12474   HOST_WIDE_INT object_offset_in_bits;
12475   HOST_WIDE_INT bitpos_int;
12476
12477   if (TREE_CODE (decl) == ERROR_MARK)
12478     return 0;
12479
12480   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
12481
12482   /* We cannot yet cope with fields whose positions are variable, so
12483      for now, when we see such things, we simply return 0.  Someday, we may
12484      be able to handle such cases, but it will be damn difficult.  */
12485   if (! host_integerp (bit_position (decl), 0))
12486     return 0;
12487
12488   bitpos_int = int_bit_position (decl);
12489
12490 #ifdef PCC_BITFIELD_TYPE_MATTERS
12491   if (PCC_BITFIELD_TYPE_MATTERS)
12492     {
12493       tree type;
12494       tree field_size_tree;
12495       HOST_WIDE_INT deepest_bitpos;
12496       unsigned HOST_WIDE_INT field_size_in_bits;
12497       unsigned int type_align_in_bits;
12498       unsigned int decl_align_in_bits;
12499       unsigned HOST_WIDE_INT type_size_in_bits;
12500
12501       type = field_type (decl);
12502       type_size_in_bits = simple_type_size_in_bits (type);
12503       type_align_in_bits = simple_type_align_in_bits (type);
12504
12505       field_size_tree = DECL_SIZE (decl);
12506
12507       /* The size could be unspecified if there was an error, or for
12508          a flexible array member.  */
12509       if (!field_size_tree)
12510         field_size_tree = bitsize_zero_node;
12511
12512       /* If the size of the field is not constant, use the type size.  */
12513       if (host_integerp (field_size_tree, 1))
12514         field_size_in_bits = tree_low_cst (field_size_tree, 1);
12515       else
12516         field_size_in_bits = type_size_in_bits;
12517
12518       decl_align_in_bits = simple_decl_align_in_bits (decl);
12519
12520       /* The GCC front-end doesn't make any attempt to keep track of the
12521          starting bit offset (relative to the start of the containing
12522          structure type) of the hypothetical "containing object" for a
12523          bit-field.  Thus, when computing the byte offset value for the
12524          start of the "containing object" of a bit-field, we must deduce
12525          this information on our own. This can be rather tricky to do in
12526          some cases.  For example, handling the following structure type
12527          definition when compiling for an i386/i486 target (which only
12528          aligns long long's to 32-bit boundaries) can be very tricky:
12529
12530          struct S { int field1; long long field2:31; };
12531
12532          Fortunately, there is a simple rule-of-thumb which can be used
12533          in such cases.  When compiling for an i386/i486, GCC will
12534          allocate 8 bytes for the structure shown above.  It decides to
12535          do this based upon one simple rule for bit-field allocation.
12536          GCC allocates each "containing object" for each bit-field at
12537          the first (i.e. lowest addressed) legitimate alignment boundary
12538          (based upon the required minimum alignment for the declared
12539          type of the field) which it can possibly use, subject to the
12540          condition that there is still enough available space remaining
12541          in the containing object (when allocated at the selected point)
12542          to fully accommodate all of the bits of the bit-field itself.
12543
12544          This simple rule makes it obvious why GCC allocates 8 bytes for
12545          each object of the structure type shown above.  When looking
12546          for a place to allocate the "containing object" for `field2',
12547          the compiler simply tries to allocate a 64-bit "containing
12548          object" at each successive 32-bit boundary (starting at zero)
12549          until it finds a place to allocate that 64- bit field such that
12550          at least 31 contiguous (and previously unallocated) bits remain
12551          within that selected 64 bit field.  (As it turns out, for the
12552          example above, the compiler finds it is OK to allocate the
12553          "containing object" 64-bit field at bit-offset zero within the
12554          structure type.)
12555
12556          Here we attempt to work backwards from the limited set of facts
12557          we're given, and we try to deduce from those facts, where GCC
12558          must have believed that the containing object started (within
12559          the structure type). The value we deduce is then used (by the
12560          callers of this routine) to generate DW_AT_location and
12561          DW_AT_bit_offset attributes for fields (both bit-fields and, in
12562          the case of DW_AT_location, regular fields as well).  */
12563
12564       /* Figure out the bit-distance from the start of the structure to
12565          the "deepest" bit of the bit-field.  */
12566       deepest_bitpos = bitpos_int + field_size_in_bits;
12567
12568       /* This is the tricky part.  Use some fancy footwork to deduce
12569          where the lowest addressed bit of the containing object must
12570          be.  */
12571       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
12572
12573       /* Round up to type_align by default.  This works best for
12574          bitfields.  */
12575       object_offset_in_bits
12576         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
12577
12578       if (object_offset_in_bits > bitpos_int)
12579         {
12580           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
12581
12582           /* Round up to decl_align instead.  */
12583           object_offset_in_bits
12584             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
12585         }
12586     }
12587   else
12588 #endif
12589     object_offset_in_bits = bitpos_int;
12590
12591   return object_offset_in_bits / BITS_PER_UNIT;
12592 }
12593 \f
12594 /* The following routines define various Dwarf attributes and any data
12595    associated with them.  */
12596
12597 /* Add a location description attribute value to a DIE.
12598
12599    This emits location attributes suitable for whole variables and
12600    whole parameters.  Note that the location attributes for struct fields are
12601    generated by the routine `data_member_location_attribute' below.  */
12602
12603 static inline void
12604 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
12605                              dw_loc_descr_ref descr)
12606 {
12607   if (descr != 0)
12608     add_AT_loc (die, attr_kind, descr);
12609 }
12610
12611 /* Attach the specialized form of location attribute used for data members of
12612    struct and union types.  In the special case of a FIELD_DECL node which
12613    represents a bit-field, the "offset" part of this special location
12614    descriptor must indicate the distance in bytes from the lowest-addressed
12615    byte of the containing struct or union type to the lowest-addressed byte of
12616    the "containing object" for the bit-field.  (See the `field_byte_offset'
12617    function above).
12618
12619    For any given bit-field, the "containing object" is a hypothetical object
12620    (of some integral or enum type) within which the given bit-field lives.  The
12621    type of this hypothetical "containing object" is always the same as the
12622    declared type of the individual bit-field itself (for GCC anyway... the
12623    DWARF spec doesn't actually mandate this).  Note that it is the size (in
12624    bytes) of the hypothetical "containing object" which will be given in the
12625    DW_AT_byte_size attribute for this bit-field.  (See the
12626    `byte_size_attribute' function below.)  It is also used when calculating the
12627    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
12628    function below.)  */
12629
12630 static void
12631 add_data_member_location_attribute (dw_die_ref die, tree decl)
12632 {
12633   HOST_WIDE_INT offset;
12634   dw_loc_descr_ref loc_descr = 0;
12635
12636   if (TREE_CODE (decl) == TREE_BINFO)
12637     {
12638       /* We're working on the TAG_inheritance for a base class.  */
12639       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
12640         {
12641           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
12642              aren't at a fixed offset from all (sub)objects of the same
12643              type.  We need to extract the appropriate offset from our
12644              vtable.  The following dwarf expression means
12645
12646                BaseAddr = ObAddr + *((*ObAddr) - Offset)
12647
12648              This is specific to the V3 ABI, of course.  */
12649
12650           dw_loc_descr_ref tmp;
12651
12652           /* Make a copy of the object address.  */
12653           tmp = new_loc_descr (DW_OP_dup, 0, 0);
12654           add_loc_descr (&loc_descr, tmp);
12655
12656           /* Extract the vtable address.  */
12657           tmp = new_loc_descr (DW_OP_deref, 0, 0);
12658           add_loc_descr (&loc_descr, tmp);
12659
12660           /* Calculate the address of the offset.  */
12661           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
12662           gcc_assert (offset < 0);
12663
12664           tmp = int_loc_descriptor (-offset);
12665           add_loc_descr (&loc_descr, tmp);
12666           tmp = new_loc_descr (DW_OP_minus, 0, 0);
12667           add_loc_descr (&loc_descr, tmp);
12668
12669           /* Extract the offset.  */
12670           tmp = new_loc_descr (DW_OP_deref, 0, 0);
12671           add_loc_descr (&loc_descr, tmp);
12672
12673           /* Add it to the object address.  */
12674           tmp = new_loc_descr (DW_OP_plus, 0, 0);
12675           add_loc_descr (&loc_descr, tmp);
12676         }
12677       else
12678         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
12679     }
12680   else
12681     offset = field_byte_offset (decl);
12682
12683   if (! loc_descr)
12684     {
12685       if (dwarf_version > 2)
12686         {
12687           /* Don't need to output a location expression, just the constant. */
12688           add_AT_int (die, DW_AT_data_member_location, offset);
12689           return;
12690         }
12691       else
12692         {
12693           enum dwarf_location_atom op;
12694           
12695           /* The DWARF2 standard says that we should assume that the structure
12696              address is already on the stack, so we can specify a structure
12697              field address by using DW_OP_plus_uconst.  */
12698           
12699 #ifdef MIPS_DEBUGGING_INFO
12700           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
12701              operator correctly.  It works only if we leave the offset on the
12702              stack.  */
12703           op = DW_OP_constu;
12704 #else
12705           op = DW_OP_plus_uconst;
12706 #endif
12707           
12708           loc_descr = new_loc_descr (op, offset, 0);
12709         }
12710     }
12711
12712   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
12713 }
12714
12715 /* Writes integer values to dw_vec_const array.  */
12716
12717 static void
12718 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
12719 {
12720   while (size != 0)
12721     {
12722       *dest++ = val & 0xff;
12723       val >>= 8;
12724       --size;
12725     }
12726 }
12727
12728 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
12729
12730 static HOST_WIDE_INT
12731 extract_int (const unsigned char *src, unsigned int size)
12732 {
12733   HOST_WIDE_INT val = 0;
12734
12735   src += size;
12736   while (size != 0)
12737     {
12738       val <<= 8;
12739       val |= *--src & 0xff;
12740       --size;
12741     }
12742   return val;
12743 }
12744
12745 /* Writes floating point values to dw_vec_const array.  */
12746
12747 static void
12748 insert_float (const_rtx rtl, unsigned char *array)
12749 {
12750   REAL_VALUE_TYPE rv;
12751   long val[4];
12752   int i;
12753
12754   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
12755   real_to_target (val, &rv, GET_MODE (rtl));
12756
12757   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
12758   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
12759     {
12760       insert_int (val[i], 4, array);
12761       array += 4;
12762     }
12763 }
12764
12765 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
12766    does not have a "location" either in memory or in a register.  These
12767    things can arise in GNU C when a constant is passed as an actual parameter
12768    to an inlined function.  They can also arise in C++ where declared
12769    constants do not necessarily get memory "homes".  */
12770
12771 static void
12772 add_const_value_attribute (dw_die_ref die, rtx rtl)
12773 {
12774   switch (GET_CODE (rtl))
12775     {
12776     case CONST_INT:
12777       {
12778         HOST_WIDE_INT val = INTVAL (rtl);
12779
12780         if (val < 0)
12781           add_AT_int (die, DW_AT_const_value, val);
12782         else
12783           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
12784       }
12785       break;
12786
12787     case CONST_DOUBLE:
12788       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
12789          floating-point constant.  A CONST_DOUBLE is used whenever the
12790          constant requires more than one word in order to be adequately
12791          represented.  We output CONST_DOUBLEs as blocks.  */
12792       {
12793         enum machine_mode mode = GET_MODE (rtl);
12794
12795         if (SCALAR_FLOAT_MODE_P (mode))
12796           {
12797             unsigned int length = GET_MODE_SIZE (mode);
12798             unsigned char *array = GGC_NEWVEC (unsigned char, length);
12799
12800             insert_float (rtl, array);
12801             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
12802           }
12803         else
12804           add_AT_long_long (die, DW_AT_const_value, rtl);
12805       }
12806       break;
12807
12808     case CONST_VECTOR:
12809       {
12810         enum machine_mode mode = GET_MODE (rtl);
12811         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
12812         unsigned int length = CONST_VECTOR_NUNITS (rtl);
12813         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
12814         unsigned int i;
12815         unsigned char *p;
12816
12817         switch (GET_MODE_CLASS (mode))
12818           {
12819           case MODE_VECTOR_INT:
12820             for (i = 0, p = array; i < length; i++, p += elt_size)
12821               {
12822                 rtx elt = CONST_VECTOR_ELT (rtl, i);
12823                 HOST_WIDE_INT lo, hi;
12824
12825                 switch (GET_CODE (elt))
12826                   {
12827                   case CONST_INT:
12828                     lo = INTVAL (elt);
12829                     hi = -(lo < 0);
12830                     break;
12831
12832                   case CONST_DOUBLE:
12833                     lo = CONST_DOUBLE_LOW (elt);
12834                     hi = CONST_DOUBLE_HIGH (elt);
12835                     break;
12836
12837                   default:
12838                     gcc_unreachable ();
12839                   }
12840
12841                 if (elt_size <= sizeof (HOST_WIDE_INT))
12842                   insert_int (lo, elt_size, p);
12843                 else
12844                   {
12845                     unsigned char *p0 = p;
12846                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
12847
12848                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12849                     if (WORDS_BIG_ENDIAN)
12850                       {
12851                         p0 = p1;
12852                         p1 = p;
12853                       }
12854                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
12855                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
12856                   }
12857               }
12858             break;
12859
12860           case MODE_VECTOR_FLOAT:
12861             for (i = 0, p = array; i < length; i++, p += elt_size)
12862               {
12863                 rtx elt = CONST_VECTOR_ELT (rtl, i);
12864                 insert_float (elt, p);
12865               }
12866             break;
12867
12868           default:
12869             gcc_unreachable ();
12870           }
12871
12872         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
12873       }
12874       break;
12875
12876     case CONST_STRING:
12877       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
12878       break;
12879
12880     case CONST:
12881       if (CONSTANT_P (XEXP (rtl, 0)))
12882         {
12883           add_const_value_attribute (die, XEXP (rtl, 0));
12884           return;
12885         }
12886       /* FALLTHROUGH */
12887     case SYMBOL_REF:
12888       if (GET_CODE (rtl) == SYMBOL_REF
12889           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
12890         break;
12891     case LABEL_REF:
12892       add_AT_addr (die, DW_AT_const_value, rtl);
12893       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12894       break;
12895
12896     case PLUS:
12897       /* In cases where an inlined instance of an inline function is passed
12898          the address of an `auto' variable (which is local to the caller) we
12899          can get a situation where the DECL_RTL of the artificial local
12900          variable (for the inlining) which acts as a stand-in for the
12901          corresponding formal parameter (of the inline function) will look
12902          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
12903          exactly a compile-time constant expression, but it isn't the address
12904          of the (artificial) local variable either.  Rather, it represents the
12905          *value* which the artificial local variable always has during its
12906          lifetime.  We currently have no way to represent such quasi-constant
12907          values in Dwarf, so for now we just punt and generate nothing.  */
12908       break;
12909
12910     default:
12911       /* No other kinds of rtx should be possible here.  */
12912       gcc_unreachable ();
12913     }
12914
12915 }
12916
12917 /* Determine whether the evaluation of EXPR references any variables
12918    or functions which aren't otherwise used (and therefore may not be
12919    output).  */
12920 static tree
12921 reference_to_unused (tree * tp, int * walk_subtrees,
12922                      void * data ATTRIBUTE_UNUSED)
12923 {
12924   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
12925     *walk_subtrees = 0;
12926
12927   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
12928       && ! TREE_ASM_WRITTEN (*tp))
12929     return *tp;
12930   /* ???  The C++ FE emits debug information for using decls, so
12931      putting gcc_unreachable here falls over.  See PR31899.  For now
12932      be conservative.  */
12933   else if (!cgraph_global_info_ready
12934            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
12935     return *tp;
12936   else if (TREE_CODE (*tp) == VAR_DECL)
12937     {
12938       struct varpool_node *node = varpool_node (*tp);
12939       if (!node->needed)
12940         return *tp;
12941     }
12942   else if (TREE_CODE (*tp) == FUNCTION_DECL
12943            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
12944     {
12945       /* The call graph machinery must have finished analyzing,
12946          optimizing and gimplifying the CU by now.
12947          So if *TP has no call graph node associated
12948          to it, it means *TP will not be emitted.  */
12949       if (!cgraph_get_node (*tp))
12950         return *tp;
12951     }
12952   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
12953     return *tp;
12954
12955   return NULL_TREE;
12956 }
12957
12958 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
12959    for use in a later add_const_value_attribute call.  */
12960
12961 static rtx
12962 rtl_for_decl_init (tree init, tree type)
12963 {
12964   rtx rtl = NULL_RTX;
12965
12966   /* If a variable is initialized with a string constant without embedded
12967      zeros, build CONST_STRING.  */
12968   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
12969     {
12970       tree enttype = TREE_TYPE (type);
12971       tree domain = TYPE_DOMAIN (type);
12972       enum machine_mode mode = TYPE_MODE (enttype);
12973
12974       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
12975           && domain
12976           && integer_zerop (TYPE_MIN_VALUE (domain))
12977           && compare_tree_int (TYPE_MAX_VALUE (domain),
12978                                TREE_STRING_LENGTH (init) - 1) == 0
12979           && ((size_t) TREE_STRING_LENGTH (init)
12980               == strlen (TREE_STRING_POINTER (init)) + 1))
12981         rtl = gen_rtx_CONST_STRING (VOIDmode,
12982                                     ggc_strdup (TREE_STRING_POINTER (init)));
12983     }
12984   /* Other aggregates, and complex values, could be represented using
12985      CONCAT: FIXME!  */
12986   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
12987     ;
12988   /* Vectors only work if their mode is supported by the target.
12989      FIXME: generic vectors ought to work too.  */
12990   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
12991     ;
12992   /* If the initializer is something that we know will expand into an
12993      immediate RTL constant, expand it now.  We must be careful not to
12994      reference variables which won't be output.  */
12995   else if (initializer_constant_valid_p (init, type)
12996            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
12997     {
12998       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
12999          possible.  */
13000       if (TREE_CODE (type) == VECTOR_TYPE)
13001         switch (TREE_CODE (init))
13002           {
13003           case VECTOR_CST:
13004             break;
13005           case CONSTRUCTOR:
13006             if (TREE_CONSTANT (init))
13007               {
13008                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
13009                 bool constant_p = true;
13010                 tree value;
13011                 unsigned HOST_WIDE_INT ix;
13012
13013                 /* Even when ctor is constant, it might contain non-*_CST
13014                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
13015                    belong into VECTOR_CST nodes.  */
13016                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
13017                   if (!CONSTANT_CLASS_P (value))
13018                     {
13019                       constant_p = false;
13020                       break;
13021                     }
13022
13023                 if (constant_p)
13024                   {
13025                     init = build_vector_from_ctor (type, elts);
13026                     break;
13027                   }
13028               }
13029             /* FALLTHRU */
13030
13031           default:
13032             return NULL;
13033           }
13034
13035       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
13036
13037       /* If expand_expr returns a MEM, it wasn't immediate.  */
13038       gcc_assert (!rtl || !MEM_P (rtl));
13039     }
13040
13041   return rtl;
13042 }
13043
13044 /* Generate RTL for the variable DECL to represent its location.  */
13045
13046 static rtx
13047 rtl_for_decl_location (tree decl)
13048 {
13049   rtx rtl;
13050
13051   /* Here we have to decide where we are going to say the parameter "lives"
13052      (as far as the debugger is concerned).  We only have a couple of
13053      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
13054
13055      DECL_RTL normally indicates where the parameter lives during most of the
13056      activation of the function.  If optimization is enabled however, this
13057      could be either NULL or else a pseudo-reg.  Both of those cases indicate
13058      that the parameter doesn't really live anywhere (as far as the code
13059      generation parts of GCC are concerned) during most of the function's
13060      activation.  That will happen (for example) if the parameter is never
13061      referenced within the function.
13062
13063      We could just generate a location descriptor here for all non-NULL
13064      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
13065      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
13066      where DECL_RTL is NULL or is a pseudo-reg.
13067
13068      Note however that we can only get away with using DECL_INCOMING_RTL as
13069      a backup substitute for DECL_RTL in certain limited cases.  In cases
13070      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
13071      we can be sure that the parameter was passed using the same type as it is
13072      declared to have within the function, and that its DECL_INCOMING_RTL
13073      points us to a place where a value of that type is passed.
13074
13075      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
13076      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
13077      because in these cases DECL_INCOMING_RTL points us to a value of some
13078      type which is *different* from the type of the parameter itself.  Thus,
13079      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
13080      such cases, the debugger would end up (for example) trying to fetch a
13081      `float' from a place which actually contains the first part of a
13082      `double'.  That would lead to really incorrect and confusing
13083      output at debug-time.
13084
13085      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
13086      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
13087      are a couple of exceptions however.  On little-endian machines we can
13088      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
13089      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
13090      an integral type that is smaller than TREE_TYPE (decl). These cases arise
13091      when (on a little-endian machine) a non-prototyped function has a
13092      parameter declared to be of type `short' or `char'.  In such cases,
13093      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
13094      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
13095      passed `int' value.  If the debugger then uses that address to fetch
13096      a `short' or a `char' (on a little-endian machine) the result will be
13097      the correct data, so we allow for such exceptional cases below.
13098
13099      Note that our goal here is to describe the place where the given formal
13100      parameter lives during most of the function's activation (i.e. between the
13101      end of the prologue and the start of the epilogue).  We'll do that as best
13102      as we can. Note however that if the given formal parameter is modified
13103      sometime during the execution of the function, then a stack backtrace (at
13104      debug-time) will show the function as having been called with the *new*
13105      value rather than the value which was originally passed in.  This happens
13106      rarely enough that it is not a major problem, but it *is* a problem, and
13107      I'd like to fix it.
13108
13109      A future version of dwarf2out.c may generate two additional attributes for
13110      any given DW_TAG_formal_parameter DIE which will describe the "passed
13111      type" and the "passed location" for the given formal parameter in addition
13112      to the attributes we now generate to indicate the "declared type" and the
13113      "active location" for each parameter.  This additional set of attributes
13114      could be used by debuggers for stack backtraces. Separately, note that
13115      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
13116      This happens (for example) for inlined-instances of inline function formal
13117      parameters which are never referenced.  This really shouldn't be
13118      happening.  All PARM_DECL nodes should get valid non-NULL
13119      DECL_INCOMING_RTL values.  FIXME.  */
13120
13121   /* Use DECL_RTL as the "location" unless we find something better.  */
13122   rtl = DECL_RTL_IF_SET (decl);
13123
13124   /* When generating abstract instances, ignore everything except
13125      constants, symbols living in memory, and symbols living in
13126      fixed registers.  */
13127   if (! reload_completed)
13128     {
13129       if (rtl
13130           && (CONSTANT_P (rtl)
13131               || (MEM_P (rtl)
13132                   && CONSTANT_P (XEXP (rtl, 0)))
13133               || (REG_P (rtl)
13134                   && TREE_CODE (decl) == VAR_DECL
13135                   && TREE_STATIC (decl))))
13136         {
13137           rtl = targetm.delegitimize_address (rtl);
13138           return rtl;
13139         }
13140       rtl = NULL_RTX;
13141     }
13142   else if (TREE_CODE (decl) == PARM_DECL)
13143     {
13144       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
13145         {
13146           tree declared_type = TREE_TYPE (decl);
13147           tree passed_type = DECL_ARG_TYPE (decl);
13148           enum machine_mode dmode = TYPE_MODE (declared_type);
13149           enum machine_mode pmode = TYPE_MODE (passed_type);
13150
13151           /* This decl represents a formal parameter which was optimized out.
13152              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
13153              all cases where (rtl == NULL_RTX) just below.  */
13154           if (dmode == pmode)
13155             rtl = DECL_INCOMING_RTL (decl);
13156           else if (SCALAR_INT_MODE_P (dmode)
13157                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
13158                    && DECL_INCOMING_RTL (decl))
13159             {
13160               rtx inc = DECL_INCOMING_RTL (decl);
13161               if (REG_P (inc))
13162                 rtl = inc;
13163               else if (MEM_P (inc))
13164                 {
13165                   if (BYTES_BIG_ENDIAN)
13166                     rtl = adjust_address_nv (inc, dmode,
13167                                              GET_MODE_SIZE (pmode)
13168                                              - GET_MODE_SIZE (dmode));
13169                   else
13170                     rtl = inc;
13171                 }
13172             }
13173         }
13174
13175       /* If the parm was passed in registers, but lives on the stack, then
13176          make a big endian correction if the mode of the type of the
13177          parameter is not the same as the mode of the rtl.  */
13178       /* ??? This is the same series of checks that are made in dbxout.c before
13179          we reach the big endian correction code there.  It isn't clear if all
13180          of these checks are necessary here, but keeping them all is the safe
13181          thing to do.  */
13182       else if (MEM_P (rtl)
13183                && XEXP (rtl, 0) != const0_rtx
13184                && ! CONSTANT_P (XEXP (rtl, 0))
13185                /* Not passed in memory.  */
13186                && !MEM_P (DECL_INCOMING_RTL (decl))
13187                /* Not passed by invisible reference.  */
13188                && (!REG_P (XEXP (rtl, 0))
13189                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
13190                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
13191 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
13192                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
13193 #endif
13194                      )
13195                /* Big endian correction check.  */
13196                && BYTES_BIG_ENDIAN
13197                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
13198                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
13199                    < UNITS_PER_WORD))
13200         {
13201           int offset = (UNITS_PER_WORD
13202                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
13203
13204           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13205                              plus_constant (XEXP (rtl, 0), offset));
13206         }
13207     }
13208   else if (TREE_CODE (decl) == VAR_DECL
13209            && rtl
13210            && MEM_P (rtl)
13211            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
13212            && BYTES_BIG_ENDIAN)
13213     {
13214       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
13215       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
13216
13217       /* If a variable is declared "register" yet is smaller than
13218          a register, then if we store the variable to memory, it
13219          looks like we're storing a register-sized value, when in
13220          fact we are not.  We need to adjust the offset of the
13221          storage location to reflect the actual value's bytes,
13222          else gdb will not be able to display it.  */
13223       if (rsize > dsize)
13224         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13225                            plus_constant (XEXP (rtl, 0), rsize-dsize));
13226     }
13227
13228   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
13229      and will have been substituted directly into all expressions that use it.
13230      C does not have such a concept, but C++ and other languages do.  */
13231   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
13232     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
13233
13234   if (rtl)
13235     rtl = targetm.delegitimize_address (rtl);
13236
13237   /* If we don't look past the constant pool, we risk emitting a
13238      reference to a constant pool entry that isn't referenced from
13239      code, and thus is not emitted.  */
13240   if (rtl)
13241     rtl = avoid_constant_pool_reference (rtl);
13242
13243   return rtl;
13244 }
13245
13246 /* We need to figure out what section we should use as the base for the
13247    address ranges where a given location is valid.
13248    1. If this particular DECL has a section associated with it, use that.
13249    2. If this function has a section associated with it, use that.
13250    3. Otherwise, use the text section.
13251    XXX: If you split a variable across multiple sections, we won't notice.  */
13252
13253 static const char *
13254 secname_for_decl (const_tree decl)
13255 {
13256   const char *secname;
13257
13258   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
13259     {
13260       tree sectree = DECL_SECTION_NAME (decl);
13261       secname = TREE_STRING_POINTER (sectree);
13262     }
13263   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
13264     {
13265       tree sectree = DECL_SECTION_NAME (current_function_decl);
13266       secname = TREE_STRING_POINTER (sectree);
13267     }
13268   else if (cfun && in_cold_section_p)
13269     secname = crtl->subsections.cold_section_label;
13270   else
13271     secname = text_section_label;
13272
13273   return secname;
13274 }
13275
13276 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
13277    returned.  If so, the decl for the COMMON block is returned, and the
13278    value is the offset into the common block for the symbol.  */
13279
13280 static tree
13281 fortran_common (tree decl, HOST_WIDE_INT *value)
13282 {
13283   tree val_expr, cvar;
13284   enum machine_mode mode;
13285   HOST_WIDE_INT bitsize, bitpos;
13286   tree offset;
13287   int volatilep = 0, unsignedp = 0;
13288
13289   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
13290      it does not have a value (the offset into the common area), or if it
13291      is thread local (as opposed to global) then it isn't common, and shouldn't
13292      be handled as such.  */
13293   if (TREE_CODE (decl) != VAR_DECL
13294       || !TREE_PUBLIC (decl)
13295       || !TREE_STATIC (decl)
13296       || !DECL_HAS_VALUE_EXPR_P (decl)
13297       || !is_fortran ())
13298     return NULL_TREE;
13299
13300   val_expr = DECL_VALUE_EXPR (decl);
13301   if (TREE_CODE (val_expr) != COMPONENT_REF)
13302     return NULL_TREE;
13303
13304   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
13305                               &mode, &unsignedp, &volatilep, true);
13306
13307   if (cvar == NULL_TREE
13308       || TREE_CODE (cvar) != VAR_DECL
13309       || DECL_ARTIFICIAL (cvar)
13310       || !TREE_PUBLIC (cvar))
13311     return NULL_TREE;
13312
13313   *value = 0;
13314   if (offset != NULL)
13315     {
13316       if (!host_integerp (offset, 0))
13317         return NULL_TREE;
13318       *value = tree_low_cst (offset, 0);
13319     }
13320   if (bitpos != 0)
13321     *value += bitpos / BITS_PER_UNIT;
13322
13323   return cvar;
13324 }
13325
13326 /* Dereference a location expression LOC if DECL is passed by invisible
13327    reference.  */
13328
13329 static dw_loc_descr_ref
13330 loc_by_reference (dw_loc_descr_ref loc, tree decl)
13331 {
13332   HOST_WIDE_INT size;
13333   enum dwarf_location_atom op;
13334
13335   if (loc == NULL)
13336     return NULL;
13337
13338   if ((TREE_CODE (decl) != PARM_DECL
13339        && TREE_CODE (decl) != RESULT_DECL
13340        && TREE_CODE (decl) != VAR_DECL)
13341       || !DECL_BY_REFERENCE (decl))
13342     return loc;
13343
13344   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
13345      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
13346      location expression is considered to be address of a memory location,
13347      rather than the register itself.  */
13348   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
13349        || loc->dw_loc_opc == DW_OP_regx)
13350       && (loc->dw_loc_next == NULL
13351           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
13352               && loc->dw_loc_next->dw_loc_next == NULL)))
13353     {
13354       if (loc->dw_loc_opc == DW_OP_regx)
13355         {
13356           loc->dw_loc_opc = DW_OP_bregx;
13357           loc->dw_loc_oprnd2.v.val_int = 0;
13358         }
13359       else
13360         {
13361           loc->dw_loc_opc
13362             = (enum dwarf_location_atom)
13363               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
13364           loc->dw_loc_oprnd1.v.val_int = 0;
13365         }
13366       return loc;
13367     }
13368
13369   size = int_size_in_bytes (TREE_TYPE (decl));
13370   if (size > DWARF2_ADDR_SIZE || size == -1)
13371     return 0;
13372   else if (size == DWARF2_ADDR_SIZE)
13373     op = DW_OP_deref;
13374   else
13375     op = DW_OP_deref_size;
13376   add_loc_descr (&loc, new_loc_descr (op, size, 0));
13377   return loc;
13378 }
13379
13380 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
13381    data attribute for a variable or a parameter.  We generate the
13382    DW_AT_const_value attribute only in those cases where the given variable
13383    or parameter does not have a true "location" either in memory or in a
13384    register.  This can happen (for example) when a constant is passed as an
13385    actual argument in a call to an inline function.  (It's possible that
13386    these things can crop up in other ways also.)  Note that one type of
13387    constant value which can be passed into an inlined function is a constant
13388    pointer.  This can happen for example if an actual argument in an inlined
13389    function call evaluates to a compile-time constant address.  */
13390
13391 static void
13392 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
13393                                        enum dwarf_attribute attr)
13394 {
13395   rtx rtl;
13396   dw_loc_descr_ref descr;
13397   var_loc_list *loc_list;
13398   struct var_loc_node *node;
13399   if (TREE_CODE (decl) == ERROR_MARK)
13400     return;
13401
13402   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
13403               || TREE_CODE (decl) == RESULT_DECL);
13404
13405   /* See if we possibly have multiple locations for this variable.  */
13406   loc_list = lookup_decl_loc (decl);
13407
13408   /* If it truly has multiple locations, the first and last node will
13409      differ.  */
13410   if (loc_list && loc_list->first != loc_list->last)
13411     {
13412       const char *endname, *secname;
13413       dw_loc_list_ref list;
13414       rtx varloc;
13415       enum var_init_status initialized;
13416
13417       /* Now that we know what section we are using for a base,
13418          actually construct the list of locations.
13419          The first location information is what is passed to the
13420          function that creates the location list, and the remaining
13421          locations just get added on to that list.
13422          Note that we only know the start address for a location
13423          (IE location changes), so to build the range, we use
13424          the range [current location start, next location start].
13425          This means we have to special case the last node, and generate
13426          a range of [last location start, end of function label].  */
13427
13428       node = loc_list->first;
13429       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13430       secname = secname_for_decl (decl);
13431
13432       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
13433         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13434       else
13435         initialized = VAR_INIT_STATUS_INITIALIZED;
13436
13437       descr = loc_by_reference (loc_descriptor (varloc, DECL_MODE (decl),
13438                                                 initialized), decl);
13439       list = new_loc_list (descr, node->label, node->next->label, secname, 1);
13440       node = node->next;
13441
13442       for (; node->next; node = node->next)
13443         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13444           {
13445             /* The variable has a location between NODE->LABEL and
13446                NODE->NEXT->LABEL.  */
13447             enum var_init_status initialized =
13448               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13449             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13450             descr = loc_by_reference (loc_descriptor (varloc, DECL_MODE (decl),
13451                                       initialized), decl);
13452             add_loc_descr_to_loc_list (&list, descr,
13453                                        node->label, node->next->label, secname);
13454           }
13455
13456       /* If the variable has a location at the last label
13457          it keeps its location until the end of function.  */
13458       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
13459         {
13460           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
13461           enum var_init_status initialized =
13462             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13463
13464           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
13465           if (!current_function_decl)
13466             endname = text_end_label;
13467           else
13468             {
13469               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
13470                                            current_function_funcdef_no);
13471               endname = ggc_strdup (label_id);
13472             }
13473           descr = loc_by_reference (loc_descriptor (varloc,
13474                                                     DECL_MODE (decl),
13475                                                     initialized),
13476                                     decl);
13477           add_loc_descr_to_loc_list (&list, descr,
13478                                      node->label, endname, secname);
13479         }
13480
13481       /* Finally, add the location list to the DIE, and we are done.  */
13482       add_AT_loc_list (die, attr, list);
13483       return;
13484     }
13485
13486   /* Try to get some constant RTL for this decl, and use that as the value of
13487      the location.  */
13488
13489   rtl = rtl_for_decl_location (decl);
13490   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
13491     {
13492       add_const_value_attribute (die, rtl);
13493       return;
13494     }
13495
13496   /* If we have tried to generate the location otherwise, and it
13497      didn't work out (we wouldn't be here if we did), and we have a one entry
13498      location list, try generating a location from that.  */
13499   if (loc_list && loc_list->first)
13500     {
13501       enum var_init_status status;
13502       node = loc_list->first;
13503       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
13504       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
13505       if (GET_CODE (rtl) == VAR_LOCATION
13506           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
13507         rtl = XEXP (XEXP (rtl, 1), 0);
13508       if (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
13509         {
13510           add_const_value_attribute (die, rtl);
13511           return;
13512         }
13513       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note),
13514                               DECL_MODE (decl), status);
13515       if (descr)
13516         {
13517           descr = loc_by_reference (descr, decl);
13518           add_AT_location_description (die, attr, descr);
13519           return;
13520         }
13521     }
13522
13523   /* We couldn't get any rtl, so try directly generating the location
13524      description from the tree.  */
13525   descr = loc_descriptor_from_tree (decl);
13526   if (descr)
13527     {
13528       descr = loc_by_reference (descr, decl);
13529       add_AT_location_description (die, attr, descr);
13530       return;
13531     }
13532   /* None of that worked, so it must not really have a location;
13533      try adding a constant value attribute from the DECL_INITIAL.  */
13534   tree_add_const_value_attribute_for_decl (die, decl);
13535 }
13536
13537 /* Add VARIABLE and DIE into deferred locations list.  */
13538
13539 static void
13540 defer_location (tree variable, dw_die_ref die)
13541 {
13542   deferred_locations entry;
13543   entry.variable = variable;
13544   entry.die = die;
13545   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
13546 }
13547
13548 /* Helper function for tree_add_const_value_attribute.  Natively encode
13549    initializer INIT into an array.  Return true if successful.  */
13550
13551 static bool
13552 native_encode_initializer (tree init, unsigned char *array, int size)
13553 {
13554   tree type;
13555
13556   if (init == NULL_TREE)
13557     return false;
13558
13559   STRIP_NOPS (init);
13560   switch (TREE_CODE (init))
13561     {
13562     case STRING_CST:
13563       type = TREE_TYPE (init);
13564       if (TREE_CODE (type) == ARRAY_TYPE)
13565         {
13566           tree enttype = TREE_TYPE (type);
13567           enum machine_mode mode = TYPE_MODE (enttype);
13568
13569           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
13570             return false;
13571           if (int_size_in_bytes (type) != size)
13572             return false;
13573           if (size > TREE_STRING_LENGTH (init))
13574             {
13575               memcpy (array, TREE_STRING_POINTER (init),
13576                       TREE_STRING_LENGTH (init));
13577               memset (array + TREE_STRING_LENGTH (init),
13578                       '\0', size - TREE_STRING_LENGTH (init));
13579             }
13580           else
13581             memcpy (array, TREE_STRING_POINTER (init), size);
13582           return true;
13583         }
13584       return false;
13585     case CONSTRUCTOR:
13586       type = TREE_TYPE (init);
13587       if (int_size_in_bytes (type) != size)
13588         return false;
13589       if (TREE_CODE (type) == ARRAY_TYPE)
13590         {
13591           HOST_WIDE_INT min_index;
13592           unsigned HOST_WIDE_INT cnt;
13593           int curpos = 0, fieldsize;
13594           constructor_elt *ce;
13595
13596           if (TYPE_DOMAIN (type) == NULL_TREE
13597               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
13598             return false;
13599
13600           fieldsize = int_size_in_bytes (TREE_TYPE (type));
13601           if (fieldsize <= 0)
13602             return false;
13603
13604           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
13605           memset (array, '\0', size);
13606           for (cnt = 0;
13607                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
13608                cnt++)
13609             {
13610               tree val = ce->value;
13611               tree index = ce->index;
13612               int pos = curpos;
13613               if (index && TREE_CODE (index) == RANGE_EXPR)
13614                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
13615                       * fieldsize;
13616               else if (index)
13617                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
13618
13619               if (val)
13620                 {
13621                   STRIP_NOPS (val);
13622                   if (!native_encode_initializer (val, array + pos, fieldsize))
13623                     return false;
13624                 }
13625               curpos = pos + fieldsize;
13626               if (index && TREE_CODE (index) == RANGE_EXPR)
13627                 {
13628                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
13629                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
13630                   while (count > 0)
13631                     {
13632                       if (val)
13633                         memcpy (array + curpos, array + pos, fieldsize);
13634                       curpos += fieldsize;
13635                     }
13636                 }
13637               gcc_assert (curpos <= size);
13638             }
13639           return true;
13640         }
13641       else if (TREE_CODE (type) == RECORD_TYPE
13642                || TREE_CODE (type) == UNION_TYPE)
13643         {
13644           tree field = NULL_TREE;
13645           unsigned HOST_WIDE_INT cnt;
13646           constructor_elt *ce;
13647
13648           if (int_size_in_bytes (type) != size)
13649             return false;
13650
13651           if (TREE_CODE (type) == RECORD_TYPE)
13652             field = TYPE_FIELDS (type);
13653
13654           for (cnt = 0;
13655                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
13656                cnt++, field = field ? TREE_CHAIN (field) : 0)
13657             {
13658               tree val = ce->value;
13659               int pos, fieldsize;
13660
13661               if (ce->index != 0)
13662                 field = ce->index;
13663
13664               if (val)
13665                 STRIP_NOPS (val);
13666
13667               if (field == NULL_TREE || DECL_BIT_FIELD (field))
13668                 return false;
13669
13670               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
13671                   && TYPE_DOMAIN (TREE_TYPE (field))
13672                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
13673                 return false;
13674               else if (DECL_SIZE_UNIT (field) == NULL_TREE
13675                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
13676                 return false;
13677               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
13678               pos = int_byte_position (field);
13679               gcc_assert (pos + fieldsize <= size);
13680               if (val
13681                   && !native_encode_initializer (val, array + pos, fieldsize))
13682                 return false;
13683             }
13684           return true;
13685         }
13686       return false;
13687     case VIEW_CONVERT_EXPR:
13688     case NON_LVALUE_EXPR:
13689       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
13690     default:
13691       return native_encode_expr (init, array, size) == size;
13692     }
13693 }
13694
13695 /* Attach a DW_AT_const_value attribute to DIE. The value of the
13696    attribute is the const value T.  */
13697
13698 static void
13699 tree_add_const_value_attribute (dw_die_ref die, tree t)
13700 {
13701   tree init;
13702   tree type = TREE_TYPE (t);
13703   rtx rtl;
13704
13705   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
13706     return;
13707
13708   init = t;
13709   gcc_assert (!DECL_P (init));
13710
13711   rtl = rtl_for_decl_init (init, type);
13712   if (rtl)
13713     add_const_value_attribute (die, rtl);
13714   /* If the host and target are sane, try harder.  */
13715   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
13716            && initializer_constant_valid_p (init, type))
13717     {
13718       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
13719       if (size > 0 && (int) size == size)
13720         {
13721           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
13722
13723           if (native_encode_initializer (init, array, size))
13724             add_AT_vec (die, DW_AT_const_value, size, 1, array);
13725         }
13726     }
13727 }
13728
13729 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
13730    attribute is the const value of T, where T is an integral constant
13731    variable with static storage duration
13732    (so it can't be a PARM_DECL or a RESULT_DECL).  */
13733
13734 static void
13735 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
13736 {
13737
13738   if (!decl
13739       || (TREE_CODE (decl) != VAR_DECL
13740           && TREE_CODE (decl) != CONST_DECL))
13741     return;
13742
13743     if (TREE_READONLY (decl)
13744         && ! TREE_THIS_VOLATILE (decl)
13745         && DECL_INITIAL (decl))
13746       /* OK */;
13747     else
13748       return;
13749
13750     tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
13751 }
13752
13753 /* Convert the CFI instructions for the current function into a
13754    location list.  This is used for DW_AT_frame_base when we targeting
13755    a dwarf2 consumer that does not support the dwarf3
13756    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
13757    expressions.  */
13758
13759 static dw_loc_list_ref
13760 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
13761 {
13762   dw_fde_ref fde;
13763   dw_loc_list_ref list, *list_tail;
13764   dw_cfi_ref cfi;
13765   dw_cfa_location last_cfa, next_cfa;
13766   const char *start_label, *last_label, *section;
13767   dw_cfa_location remember;
13768
13769   fde = current_fde ();
13770   gcc_assert (fde != NULL);
13771
13772   section = secname_for_decl (current_function_decl);
13773   list_tail = &list;
13774   list = NULL;
13775
13776   memset (&next_cfa, 0, sizeof (next_cfa));
13777   next_cfa.reg = INVALID_REGNUM;
13778   remember = next_cfa;
13779
13780   start_label = fde->dw_fde_begin;
13781
13782   /* ??? Bald assumption that the CIE opcode list does not contain
13783      advance opcodes.  */
13784   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
13785     lookup_cfa_1 (cfi, &next_cfa, &remember);
13786
13787   last_cfa = next_cfa;
13788   last_label = start_label;
13789
13790   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
13791     switch (cfi->dw_cfi_opc)
13792       {
13793       case DW_CFA_set_loc:
13794       case DW_CFA_advance_loc1:
13795       case DW_CFA_advance_loc2:
13796       case DW_CFA_advance_loc4:
13797         if (!cfa_equal_p (&last_cfa, &next_cfa))
13798           {
13799             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
13800                                        start_label, last_label, section,
13801                                        list == NULL);
13802
13803             list_tail = &(*list_tail)->dw_loc_next;
13804             last_cfa = next_cfa;
13805             start_label = last_label;
13806           }
13807         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
13808         break;
13809
13810       case DW_CFA_advance_loc:
13811         /* The encoding is complex enough that we should never emit this.  */
13812         gcc_unreachable ();
13813
13814       default:
13815         lookup_cfa_1 (cfi, &next_cfa, &remember);
13816         break;
13817       }
13818
13819   if (!cfa_equal_p (&last_cfa, &next_cfa))
13820     {
13821       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
13822                                  start_label, last_label, section,
13823                                  list == NULL);
13824       list_tail = &(*list_tail)->dw_loc_next;
13825       start_label = last_label;
13826     }
13827   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
13828                              start_label, fde->dw_fde_end, section,
13829                              list == NULL);
13830
13831   return list;
13832 }
13833
13834 /* Compute a displacement from the "steady-state frame pointer" to the
13835    frame base (often the same as the CFA), and store it in
13836    frame_pointer_fb_offset.  OFFSET is added to the displacement
13837    before the latter is negated.  */
13838
13839 static void
13840 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
13841 {
13842   rtx reg, elim;
13843
13844 #ifdef FRAME_POINTER_CFA_OFFSET
13845   reg = frame_pointer_rtx;
13846   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
13847 #else
13848   reg = arg_pointer_rtx;
13849   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
13850 #endif
13851
13852   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
13853   if (GET_CODE (elim) == PLUS)
13854     {
13855       offset += INTVAL (XEXP (elim, 1));
13856       elim = XEXP (elim, 0);
13857     }
13858
13859   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
13860                && (elim == hard_frame_pointer_rtx
13861                    || elim == stack_pointer_rtx))
13862               || elim == (frame_pointer_needed
13863                           ? hard_frame_pointer_rtx
13864                           : stack_pointer_rtx));
13865
13866   frame_pointer_fb_offset = -offset;
13867 }
13868
13869 /* Generate a DW_AT_name attribute given some string value to be included as
13870    the value of the attribute.  */
13871
13872 static void
13873 add_name_attribute (dw_die_ref die, const char *name_string)
13874 {
13875   if (name_string != NULL && *name_string != 0)
13876     {
13877       if (demangle_name_func)
13878         name_string = (*demangle_name_func) (name_string);
13879
13880       add_AT_string (die, DW_AT_name, name_string);
13881     }
13882 }
13883
13884 /* Generate a DW_AT_comp_dir attribute for DIE.  */
13885
13886 static void
13887 add_comp_dir_attribute (dw_die_ref die)
13888 {
13889   const char *wd = get_src_pwd ();
13890   char *wd1;
13891
13892   if (wd == NULL)
13893     return;
13894
13895   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
13896     {
13897       int wdlen;
13898
13899       wdlen = strlen (wd);
13900       wd1 = GGC_NEWVEC (char, wdlen + 2);
13901       strcpy (wd1, wd);
13902       wd1 [wdlen] = DIR_SEPARATOR;
13903       wd1 [wdlen + 1] = 0;
13904       wd = wd1;
13905     }
13906
13907     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
13908 }
13909
13910 /* Given a tree node describing an array bound (either lower or upper) output
13911    a representation for that bound.  */
13912
13913 static void
13914 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
13915 {
13916   switch (TREE_CODE (bound))
13917     {
13918     case ERROR_MARK:
13919       return;
13920
13921     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
13922     case INTEGER_CST:
13923       if (! host_integerp (bound, 0)
13924           || (bound_attr == DW_AT_lower_bound
13925               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
13926                   || (is_fortran () && integer_onep (bound)))))
13927         /* Use the default.  */
13928         ;
13929       else
13930         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
13931       break;
13932
13933     CASE_CONVERT:
13934     case VIEW_CONVERT_EXPR:
13935       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
13936       break;
13937
13938     case SAVE_EXPR:
13939       break;
13940
13941     case VAR_DECL:
13942     case PARM_DECL:
13943     case RESULT_DECL:
13944       {
13945         dw_die_ref decl_die = lookup_decl_die (bound);
13946         dw_loc_descr_ref loc;
13947
13948         /* ??? Can this happen, or should the variable have been bound
13949            first?  Probably it can, since I imagine that we try to create
13950            the types of parameters in the order in which they exist in
13951            the list, and won't have created a forward reference to a
13952            later parameter.  */
13953         if (decl_die != NULL)
13954           add_AT_die_ref (subrange_die, bound_attr, decl_die);
13955         else
13956           {
13957             loc = loc_descriptor_from_tree_1 (bound, 0);
13958             add_AT_location_description (subrange_die, bound_attr, loc);
13959           }
13960         break;
13961       }
13962
13963     default:
13964       {
13965         /* Otherwise try to create a stack operation procedure to
13966            evaluate the value of the array bound.  */
13967
13968         dw_die_ref ctx, decl_die;
13969         dw_loc_descr_ref loc;
13970
13971         loc = loc_descriptor_from_tree (bound);
13972         if (loc == NULL)
13973           break;
13974
13975         if (current_function_decl == 0)
13976           ctx = comp_unit_die;
13977         else
13978           ctx = lookup_decl_die (current_function_decl);
13979
13980         decl_die = new_die (DW_TAG_variable, ctx, bound);
13981         add_AT_flag (decl_die, DW_AT_artificial, 1);
13982         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
13983         add_AT_loc (decl_die, DW_AT_location, loc);
13984
13985         add_AT_die_ref (subrange_die, bound_attr, decl_die);
13986         break;
13987       }
13988     }
13989 }
13990
13991 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
13992    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
13993    Note that the block of subscript information for an array type also
13994    includes information about the element type of the given array type.  */
13995
13996 static void
13997 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
13998 {
13999   unsigned dimension_number;
14000   tree lower, upper;
14001   dw_die_ref subrange_die;
14002
14003   for (dimension_number = 0;
14004        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
14005        type = TREE_TYPE (type), dimension_number++)
14006     {
14007       tree domain = TYPE_DOMAIN (type);
14008
14009       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
14010         break;
14011
14012       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
14013          and (in GNU C only) variable bounds.  Handle all three forms
14014          here.  */
14015       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
14016       if (domain)
14017         {
14018           /* We have an array type with specified bounds.  */
14019           lower = TYPE_MIN_VALUE (domain);
14020           upper = TYPE_MAX_VALUE (domain);
14021
14022           /* Define the index type.  */
14023           if (TREE_TYPE (domain))
14024             {
14025               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
14026                  TREE_TYPE field.  We can't emit debug info for this
14027                  because it is an unnamed integral type.  */
14028               if (TREE_CODE (domain) == INTEGER_TYPE
14029                   && TYPE_NAME (domain) == NULL_TREE
14030                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
14031                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
14032                 ;
14033               else
14034                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
14035                                     type_die);
14036             }
14037
14038           /* ??? If upper is NULL, the array has unspecified length,
14039              but it does have a lower bound.  This happens with Fortran
14040                dimension arr(N:*)
14041              Since the debugger is definitely going to need to know N
14042              to produce useful results, go ahead and output the lower
14043              bound solo, and hope the debugger can cope.  */
14044
14045           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
14046           if (upper)
14047             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
14048         }
14049
14050       /* Otherwise we have an array type with an unspecified length.  The
14051          DWARF-2 spec does not say how to handle this; let's just leave out the
14052          bounds.  */
14053     }
14054 }
14055
14056 static void
14057 add_byte_size_attribute (dw_die_ref die, tree tree_node)
14058 {
14059   unsigned size;
14060
14061   switch (TREE_CODE (tree_node))
14062     {
14063     case ERROR_MARK:
14064       size = 0;
14065       break;
14066     case ENUMERAL_TYPE:
14067     case RECORD_TYPE:
14068     case UNION_TYPE:
14069     case QUAL_UNION_TYPE:
14070       size = int_size_in_bytes (tree_node);
14071       break;
14072     case FIELD_DECL:
14073       /* For a data member of a struct or union, the DW_AT_byte_size is
14074          generally given as the number of bytes normally allocated for an
14075          object of the *declared* type of the member itself.  This is true
14076          even for bit-fields.  */
14077       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
14078       break;
14079     default:
14080       gcc_unreachable ();
14081     }
14082
14083   /* Note that `size' might be -1 when we get to this point.  If it is, that
14084      indicates that the byte size of the entity in question is variable.  We
14085      have no good way of expressing this fact in Dwarf at the present time,
14086      so just let the -1 pass on through.  */
14087   add_AT_unsigned (die, DW_AT_byte_size, size);
14088 }
14089
14090 /* For a FIELD_DECL node which represents a bit-field, output an attribute
14091    which specifies the distance in bits from the highest order bit of the
14092    "containing object" for the bit-field to the highest order bit of the
14093    bit-field itself.
14094
14095    For any given bit-field, the "containing object" is a hypothetical object
14096    (of some integral or enum type) within which the given bit-field lives.  The
14097    type of this hypothetical "containing object" is always the same as the
14098    declared type of the individual bit-field itself.  The determination of the
14099    exact location of the "containing object" for a bit-field is rather
14100    complicated.  It's handled by the `field_byte_offset' function (above).
14101
14102    Note that it is the size (in bytes) of the hypothetical "containing object"
14103    which will be given in the DW_AT_byte_size attribute for this bit-field.
14104    (See `byte_size_attribute' above).  */
14105
14106 static inline void
14107 add_bit_offset_attribute (dw_die_ref die, tree decl)
14108 {
14109   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
14110   tree type = DECL_BIT_FIELD_TYPE (decl);
14111   HOST_WIDE_INT bitpos_int;
14112   HOST_WIDE_INT highest_order_object_bit_offset;
14113   HOST_WIDE_INT highest_order_field_bit_offset;
14114   HOST_WIDE_INT unsigned bit_offset;
14115
14116   /* Must be a field and a bit field.  */
14117   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
14118
14119   /* We can't yet handle bit-fields whose offsets are variable, so if we
14120      encounter such things, just return without generating any attribute
14121      whatsoever.  Likewise for variable or too large size.  */
14122   if (! host_integerp (bit_position (decl), 0)
14123       || ! host_integerp (DECL_SIZE (decl), 1))
14124     return;
14125
14126   bitpos_int = int_bit_position (decl);
14127
14128   /* Note that the bit offset is always the distance (in bits) from the
14129      highest-order bit of the "containing object" to the highest-order bit of
14130      the bit-field itself.  Since the "high-order end" of any object or field
14131      is different on big-endian and little-endian machines, the computation
14132      below must take account of these differences.  */
14133   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
14134   highest_order_field_bit_offset = bitpos_int;
14135
14136   if (! BYTES_BIG_ENDIAN)
14137     {
14138       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
14139       highest_order_object_bit_offset += simple_type_size_in_bits (type);
14140     }
14141
14142   bit_offset
14143     = (! BYTES_BIG_ENDIAN
14144        ? highest_order_object_bit_offset - highest_order_field_bit_offset
14145        : highest_order_field_bit_offset - highest_order_object_bit_offset);
14146
14147   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
14148 }
14149
14150 /* For a FIELD_DECL node which represents a bit field, output an attribute
14151    which specifies the length in bits of the given field.  */
14152
14153 static inline void
14154 add_bit_size_attribute (dw_die_ref die, tree decl)
14155 {
14156   /* Must be a field and a bit field.  */
14157   gcc_assert (TREE_CODE (decl) == FIELD_DECL
14158               && DECL_BIT_FIELD_TYPE (decl));
14159
14160   if (host_integerp (DECL_SIZE (decl), 1))
14161     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
14162 }
14163
14164 /* If the compiled language is ANSI C, then add a 'prototyped'
14165    attribute, if arg types are given for the parameters of a function.  */
14166
14167 static inline void
14168 add_prototyped_attribute (dw_die_ref die, tree func_type)
14169 {
14170   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
14171       && TYPE_ARG_TYPES (func_type) != NULL)
14172     add_AT_flag (die, DW_AT_prototyped, 1);
14173 }
14174
14175 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
14176    by looking in either the type declaration or object declaration
14177    equate table.  */
14178
14179 static inline dw_die_ref
14180 add_abstract_origin_attribute (dw_die_ref die, tree origin)
14181 {
14182   dw_die_ref origin_die = NULL;
14183
14184   if (TREE_CODE (origin) != FUNCTION_DECL)
14185     {
14186       /* We may have gotten separated from the block for the inlined
14187          function, if we're in an exception handler or some such; make
14188          sure that the abstract function has been written out.
14189
14190          Doing this for nested functions is wrong, however; functions are
14191          distinct units, and our context might not even be inline.  */
14192       tree fn = origin;
14193
14194       if (TYPE_P (fn))
14195         fn = TYPE_STUB_DECL (fn);
14196
14197       fn = decl_function_context (fn);
14198       if (fn)
14199         dwarf2out_abstract_function (fn);
14200     }
14201
14202   if (DECL_P (origin))
14203     origin_die = lookup_decl_die (origin);
14204   else if (TYPE_P (origin))
14205     origin_die = lookup_type_die (origin);
14206
14207   /* XXX: Functions that are never lowered don't always have correct block
14208      trees (in the case of java, they simply have no block tree, in some other
14209      languages).  For these functions, there is nothing we can really do to
14210      output correct debug info for inlined functions in all cases.  Rather
14211      than die, we'll just produce deficient debug info now, in that we will
14212      have variables without a proper abstract origin.  In the future, when all
14213      functions are lowered, we should re-add a gcc_assert (origin_die)
14214      here.  */
14215
14216   if (origin_die)
14217     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
14218   return origin_die;
14219 }
14220
14221 /* We do not currently support the pure_virtual attribute.  */
14222
14223 static inline void
14224 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
14225 {
14226   if (DECL_VINDEX (func_decl))
14227     {
14228       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14229
14230       if (host_integerp (DECL_VINDEX (func_decl), 0))
14231         add_AT_loc (die, DW_AT_vtable_elem_location,
14232                     new_loc_descr (DW_OP_constu,
14233                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
14234                                    0));
14235
14236       /* GNU extension: Record what type this method came from originally.  */
14237       if (debug_info_level > DINFO_LEVEL_TERSE)
14238         add_AT_die_ref (die, DW_AT_containing_type,
14239                         lookup_type_die (DECL_CONTEXT (func_decl)));
14240     }
14241 }
14242 \f
14243 /* Add source coordinate attributes for the given decl.  */
14244
14245 static void
14246 add_src_coords_attributes (dw_die_ref die, tree decl)
14247 {
14248   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14249
14250   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
14251   add_AT_unsigned (die, DW_AT_decl_line, s.line);
14252 }
14253
14254 /* Add a DW_AT_name attribute and source coordinate attribute for the
14255    given decl, but only if it actually has a name.  */
14256
14257 static void
14258 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
14259 {
14260   tree decl_name;
14261
14262   decl_name = DECL_NAME (decl);
14263   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
14264     {
14265       add_name_attribute (die, dwarf2_name (decl, 0));
14266       if (! DECL_ARTIFICIAL (decl))
14267         add_src_coords_attributes (die, decl);
14268
14269       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
14270           && TREE_PUBLIC (decl)
14271           && !DECL_ABSTRACT (decl)
14272           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
14273           && !is_fortran ())
14274         {
14275           /* Defer until we have an assembler name set.  */
14276           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
14277             {
14278               limbo_die_node *asm_name;
14279
14280               asm_name = GGC_CNEW (limbo_die_node);
14281               asm_name->die = die;
14282               asm_name->created_for = decl;
14283               asm_name->next = deferred_asm_name;
14284               deferred_asm_name = asm_name;
14285             }
14286           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
14287             add_AT_string (die, DW_AT_MIPS_linkage_name,
14288                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
14289         }
14290     }
14291
14292 #ifdef VMS_DEBUGGING_INFO
14293   /* Get the function's name, as described by its RTL.  This may be different
14294      from the DECL_NAME name used in the source file.  */
14295   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
14296     {
14297       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
14298                    XEXP (DECL_RTL (decl), 0));
14299       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
14300     }
14301 #endif
14302 }
14303
14304 /* Push a new declaration scope.  */
14305
14306 static void
14307 push_decl_scope (tree scope)
14308 {
14309   VEC_safe_push (tree, gc, decl_scope_table, scope);
14310 }
14311
14312 /* Pop a declaration scope.  */
14313
14314 static inline void
14315 pop_decl_scope (void)
14316 {
14317   VEC_pop (tree, decl_scope_table);
14318 }
14319
14320 /* Return the DIE for the scope that immediately contains this type.
14321    Non-named types get global scope.  Named types nested in other
14322    types get their containing scope if it's open, or global scope
14323    otherwise.  All other types (i.e. function-local named types) get
14324    the current active scope.  */
14325
14326 static dw_die_ref
14327 scope_die_for (tree t, dw_die_ref context_die)
14328 {
14329   dw_die_ref scope_die = NULL;
14330   tree containing_scope;
14331   int i;
14332
14333   /* Non-types always go in the current scope.  */
14334   gcc_assert (TYPE_P (t));
14335
14336   containing_scope = TYPE_CONTEXT (t);
14337
14338   /* Use the containing namespace if it was passed in (for a declaration).  */
14339   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
14340     {
14341       if (context_die == lookup_decl_die (containing_scope))
14342         /* OK */;
14343       else
14344         containing_scope = NULL_TREE;
14345     }
14346
14347   /* Ignore function type "scopes" from the C frontend.  They mean that
14348      a tagged type is local to a parmlist of a function declarator, but
14349      that isn't useful to DWARF.  */
14350   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
14351     containing_scope = NULL_TREE;
14352
14353   if (containing_scope == NULL_TREE)
14354     scope_die = comp_unit_die;
14355   else if (TYPE_P (containing_scope))
14356     {
14357       /* For types, we can just look up the appropriate DIE.  But
14358          first we check to see if we're in the middle of emitting it
14359          so we know where the new DIE should go.  */
14360       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
14361         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
14362           break;
14363
14364       if (i < 0)
14365         {
14366           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
14367                       || TREE_ASM_WRITTEN (containing_scope));
14368
14369           /* If none of the current dies are suitable, we get file scope.  */
14370           scope_die = comp_unit_die;
14371         }
14372       else
14373         scope_die = lookup_type_die (containing_scope);
14374     }
14375   else
14376     scope_die = context_die;
14377
14378   return scope_die;
14379 }
14380
14381 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
14382
14383 static inline int
14384 local_scope_p (dw_die_ref context_die)
14385 {
14386   for (; context_die; context_die = context_die->die_parent)
14387     if (context_die->die_tag == DW_TAG_inlined_subroutine
14388         || context_die->die_tag == DW_TAG_subprogram)
14389       return 1;
14390
14391   return 0;
14392 }
14393
14394 /* Returns nonzero if CONTEXT_DIE is a class.  */
14395
14396 static inline int
14397 class_scope_p (dw_die_ref context_die)
14398 {
14399   return (context_die
14400           && (context_die->die_tag == DW_TAG_structure_type
14401               || context_die->die_tag == DW_TAG_class_type
14402               || context_die->die_tag == DW_TAG_interface_type
14403               || context_die->die_tag == DW_TAG_union_type));
14404 }
14405
14406 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
14407    whether or not to treat a DIE in this context as a declaration.  */
14408
14409 static inline int
14410 class_or_namespace_scope_p (dw_die_ref context_die)
14411 {
14412   return (class_scope_p (context_die)
14413           || (context_die && context_die->die_tag == DW_TAG_namespace));
14414 }
14415
14416 /* Many forms of DIEs require a "type description" attribute.  This
14417    routine locates the proper "type descriptor" die for the type given
14418    by 'type', and adds a DW_AT_type attribute below the given die.  */
14419
14420 static void
14421 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
14422                     int decl_volatile, dw_die_ref context_die)
14423 {
14424   enum tree_code code  = TREE_CODE (type);
14425   dw_die_ref type_die  = NULL;
14426
14427   /* ??? If this type is an unnamed subrange type of an integral, floating-point
14428      or fixed-point type, use the inner type.  This is because we have no
14429      support for unnamed types in base_type_die.  This can happen if this is
14430      an Ada subrange type.  Correct solution is emit a subrange type die.  */
14431   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
14432       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
14433     type = TREE_TYPE (type), code = TREE_CODE (type);
14434
14435   if (code == ERROR_MARK
14436       /* Handle a special case.  For functions whose return type is void, we
14437          generate *no* type attribute.  (Note that no object may have type
14438          `void', so this only applies to function return types).  */
14439       || code == VOID_TYPE)
14440     return;
14441
14442   type_die = modified_type_die (type,
14443                                 decl_const || TYPE_READONLY (type),
14444                                 decl_volatile || TYPE_VOLATILE (type),
14445                                 context_die);
14446
14447   if (type_die != NULL)
14448     add_AT_die_ref (object_die, DW_AT_type, type_die);
14449 }
14450
14451 /* Given an object die, add the calling convention attribute for the
14452    function call type.  */
14453 static void
14454 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
14455 {
14456   enum dwarf_calling_convention value = DW_CC_normal;
14457
14458   value = ((enum dwarf_calling_convention)
14459            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
14460
14461   /* DWARF doesn't provide a way to identify a program's source-level
14462      entry point.  DW_AT_calling_convention attributes are only meant
14463      to describe functions' calling conventions.  However, lacking a
14464      better way to signal the Fortran main program, we use this for the
14465      time being, following existing custom.  */
14466   if (is_fortran ()
14467       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
14468     value = DW_CC_program;
14469
14470   /* Only add the attribute if the backend requests it, and
14471      is not DW_CC_normal.  */
14472   if (value && (value != DW_CC_normal))
14473     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
14474 }
14475
14476 /* Given a tree pointer to a struct, class, union, or enum type node, return
14477    a pointer to the (string) tag name for the given type, or zero if the type
14478    was declared without a tag.  */
14479
14480 static const char *
14481 type_tag (const_tree type)
14482 {
14483   const char *name = 0;
14484
14485   if (TYPE_NAME (type) != 0)
14486     {
14487       tree t = 0;
14488
14489       /* Find the IDENTIFIER_NODE for the type name.  */
14490       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
14491         t = TYPE_NAME (type);
14492
14493       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
14494          a TYPE_DECL node, regardless of whether or not a `typedef' was
14495          involved.  */
14496       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
14497                && ! DECL_IGNORED_P (TYPE_NAME (type)))
14498         {
14499           /* We want to be extra verbose.  Don't call dwarf_name if
14500              DECL_NAME isn't set.  The default hook for decl_printable_name
14501              doesn't like that, and in this context it's correct to return
14502              0, instead of "<anonymous>" or the like.  */
14503           if (DECL_NAME (TYPE_NAME (type)))
14504             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
14505         }
14506
14507       /* Now get the name as a string, or invent one.  */
14508       if (!name && t != 0)
14509         name = IDENTIFIER_POINTER (t);
14510     }
14511
14512   return (name == 0 || *name == '\0') ? 0 : name;
14513 }
14514
14515 /* Return the type associated with a data member, make a special check
14516    for bit field types.  */
14517
14518 static inline tree
14519 member_declared_type (const_tree member)
14520 {
14521   return (DECL_BIT_FIELD_TYPE (member)
14522           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
14523 }
14524
14525 /* Get the decl's label, as described by its RTL. This may be different
14526    from the DECL_NAME name used in the source file.  */
14527
14528 #if 0
14529 static const char *
14530 decl_start_label (tree decl)
14531 {
14532   rtx x;
14533   const char *fnname;
14534
14535   x = DECL_RTL (decl);
14536   gcc_assert (MEM_P (x));
14537
14538   x = XEXP (x, 0);
14539   gcc_assert (GET_CODE (x) == SYMBOL_REF);
14540
14541   fnname = XSTR (x, 0);
14542   return fnname;
14543 }
14544 #endif
14545 \f
14546 /* These routines generate the internal representation of the DIE's for
14547    the compilation unit.  Debugging information is collected by walking
14548    the declaration trees passed in from dwarf2out_decl().  */
14549
14550 static void
14551 gen_array_type_die (tree type, dw_die_ref context_die)
14552 {
14553   dw_die_ref scope_die = scope_die_for (type, context_die);
14554   dw_die_ref array_die;
14555
14556   /* GNU compilers represent multidimensional array types as sequences of one
14557      dimensional array types whose element types are themselves array types.
14558      We sometimes squish that down to a single array_type DIE with multiple
14559      subscripts in the Dwarf debugging info.  The draft Dwarf specification
14560      say that we are allowed to do this kind of compression in C, because
14561      there is no difference between an array of arrays and a multidimensional
14562      array.  We don't do this for Ada to remain as close as possible to the
14563      actual representation, which is especially important against the language
14564      flexibilty wrt arrays of variable size.  */
14565
14566   bool collapse_nested_arrays = !is_ada ();
14567   tree element_type;
14568
14569   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
14570      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
14571   if (TYPE_STRING_FLAG (type)
14572       && TREE_CODE (type) == ARRAY_TYPE
14573       && is_fortran ()
14574       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
14575     {
14576       HOST_WIDE_INT size;
14577
14578       array_die = new_die (DW_TAG_string_type, scope_die, type);
14579       add_name_attribute (array_die, type_tag (type));
14580       equate_type_number_to_die (type, array_die);
14581       size = int_size_in_bytes (type);
14582       if (size >= 0)
14583         add_AT_unsigned (array_die, DW_AT_byte_size, size);
14584       else if (TYPE_DOMAIN (type) != NULL_TREE
14585                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
14586                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
14587         {
14588           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
14589           dw_loc_descr_ref loc = loc_descriptor_from_tree (szdecl);
14590
14591           size = int_size_in_bytes (TREE_TYPE (szdecl));
14592           if (loc && size > 0)
14593             {
14594               add_AT_loc (array_die, DW_AT_string_length, loc);
14595               if (size != DWARF2_ADDR_SIZE)
14596                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
14597             }
14598         }
14599       return;
14600     }
14601
14602   /* ??? The SGI dwarf reader fails for array of array of enum types
14603      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
14604      array type comes before the outer array type.  We thus call gen_type_die
14605      before we new_die and must prevent nested array types collapsing for this
14606      target.  */
14607
14608 #ifdef MIPS_DEBUGGING_INFO
14609   gen_type_die (TREE_TYPE (type), context_die);
14610   collapse_nested_arrays = false;
14611 #endif
14612
14613   array_die = new_die (DW_TAG_array_type, scope_die, type);
14614   add_name_attribute (array_die, type_tag (type));
14615   equate_type_number_to_die (type, array_die);
14616
14617   if (TREE_CODE (type) == VECTOR_TYPE)
14618     {
14619       /* The frontend feeds us a representation for the vector as a struct
14620          containing an array.  Pull out the array type.  */
14621       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
14622       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
14623     }
14624
14625   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
14626   if (is_fortran ()
14627       && TREE_CODE (type) == ARRAY_TYPE
14628       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
14629       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
14630     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
14631
14632 #if 0
14633   /* We default the array ordering.  SDB will probably do
14634      the right things even if DW_AT_ordering is not present.  It's not even
14635      an issue until we start to get into multidimensional arrays anyway.  If
14636      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
14637      then we'll have to put the DW_AT_ordering attribute back in.  (But if
14638      and when we find out that we need to put these in, we will only do so
14639      for multidimensional arrays.  */
14640   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
14641 #endif
14642
14643 #ifdef MIPS_DEBUGGING_INFO
14644   /* The SGI compilers handle arrays of unknown bound by setting
14645      AT_declaration and not emitting any subrange DIEs.  */
14646   if (! TYPE_DOMAIN (type))
14647     add_AT_flag (array_die, DW_AT_declaration, 1);
14648   else
14649 #endif
14650     add_subscript_info (array_die, type, collapse_nested_arrays);
14651
14652   /* Add representation of the type of the elements of this array type and
14653      emit the corresponding DIE if we haven't done it already.  */  
14654   element_type = TREE_TYPE (type);
14655   if (collapse_nested_arrays)
14656     while (TREE_CODE (element_type) == ARRAY_TYPE)
14657       {
14658         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
14659           break;
14660         element_type = TREE_TYPE (element_type);
14661       }
14662
14663 #ifndef MIPS_DEBUGGING_INFO
14664   gen_type_die (element_type, context_die);
14665 #endif
14666
14667   add_type_attribute (array_die, element_type, 0, 0, context_die);
14668
14669   if (get_AT (array_die, DW_AT_name))
14670     add_pubtype (type, array_die);
14671 }
14672
14673 static dw_loc_descr_ref
14674 descr_info_loc (tree val, tree base_decl)
14675 {
14676   HOST_WIDE_INT size;
14677   dw_loc_descr_ref loc, loc2;
14678   enum dwarf_location_atom op;
14679
14680   if (val == base_decl)
14681     return new_loc_descr (DW_OP_push_object_address, 0, 0);
14682
14683   switch (TREE_CODE (val))
14684     {
14685     CASE_CONVERT:
14686       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14687     case VAR_DECL:
14688       return loc_descriptor_from_tree_1 (val, 0);
14689     case INTEGER_CST:
14690       if (host_integerp (val, 0))
14691         return int_loc_descriptor (tree_low_cst (val, 0));
14692       break;
14693     case INDIRECT_REF:
14694       size = int_size_in_bytes (TREE_TYPE (val));
14695       if (size < 0)
14696         break;
14697       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14698       if (!loc)
14699         break;
14700       if (size == DWARF2_ADDR_SIZE)
14701         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
14702       else
14703         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
14704       return loc;
14705     case POINTER_PLUS_EXPR:
14706     case PLUS_EXPR:
14707       if (host_integerp (TREE_OPERAND (val, 1), 1)
14708           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
14709              < 16384)
14710         {
14711           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14712           if (!loc)
14713             break;
14714           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
14715         }
14716       else
14717         {
14718           op = DW_OP_plus;
14719         do_binop:
14720           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
14721           if (!loc)
14722             break;
14723           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
14724           if (!loc2)
14725             break;
14726           add_loc_descr (&loc, loc2);
14727           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
14728         }
14729       return loc;
14730     case MINUS_EXPR:
14731       op = DW_OP_minus;
14732       goto do_binop;
14733     case MULT_EXPR:
14734       op = DW_OP_mul;
14735       goto do_binop;
14736     case EQ_EXPR:
14737       op = DW_OP_eq;
14738       goto do_binop;
14739     case NE_EXPR:
14740       op = DW_OP_ne;
14741       goto do_binop;
14742     default:
14743       break;
14744     }
14745   return NULL;
14746 }
14747
14748 static void
14749 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
14750                       tree val, tree base_decl)
14751 {
14752   dw_loc_descr_ref loc;
14753
14754   if (host_integerp (val, 0))
14755     {
14756       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
14757       return;
14758     }
14759
14760   loc = descr_info_loc (val, base_decl);
14761   if (!loc)
14762     return;
14763
14764   add_AT_loc (die, attr, loc);
14765 }
14766
14767 /* This routine generates DIE for array with hidden descriptor, details
14768    are filled into *info by a langhook.  */
14769
14770 static void
14771 gen_descr_array_type_die (tree type, struct array_descr_info *info,
14772                           dw_die_ref context_die)
14773 {
14774   dw_die_ref scope_die = scope_die_for (type, context_die);
14775   dw_die_ref array_die;
14776   int dim;
14777
14778   array_die = new_die (DW_TAG_array_type, scope_die, type);
14779   add_name_attribute (array_die, type_tag (type));
14780   equate_type_number_to_die (type, array_die);
14781
14782   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
14783   if (is_fortran ()
14784       && info->ndimensions >= 2)
14785     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
14786
14787   if (info->data_location)
14788     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
14789                           info->base_decl);
14790   if (info->associated)
14791     add_descr_info_field (array_die, DW_AT_associated, info->associated,
14792                           info->base_decl);
14793   if (info->allocated)
14794     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
14795                           info->base_decl);
14796
14797   for (dim = 0; dim < info->ndimensions; dim++)
14798     {
14799       dw_die_ref subrange_die
14800         = new_die (DW_TAG_subrange_type, array_die, NULL);
14801
14802       if (info->dimen[dim].lower_bound)
14803         {
14804           /* If it is the default value, omit it.  */
14805           if ((is_c_family () || is_java ())
14806               && integer_zerop (info->dimen[dim].lower_bound))
14807             ;
14808           else if (is_fortran ()
14809                    && integer_onep (info->dimen[dim].lower_bound))
14810             ;
14811           else
14812             add_descr_info_field (subrange_die, DW_AT_lower_bound,
14813                                   info->dimen[dim].lower_bound,
14814                                   info->base_decl);
14815         }
14816       if (info->dimen[dim].upper_bound)
14817         add_descr_info_field (subrange_die, DW_AT_upper_bound,
14818                               info->dimen[dim].upper_bound,
14819                               info->base_decl);
14820       if (info->dimen[dim].stride)
14821         add_descr_info_field (subrange_die, DW_AT_byte_stride,
14822                               info->dimen[dim].stride,
14823                               info->base_decl);
14824     }
14825
14826   gen_type_die (info->element_type, context_die);
14827   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
14828
14829   if (get_AT (array_die, DW_AT_name))
14830     add_pubtype (type, array_die);
14831 }
14832
14833 #if 0
14834 static void
14835 gen_entry_point_die (tree decl, dw_die_ref context_die)
14836 {
14837   tree origin = decl_ultimate_origin (decl);
14838   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
14839
14840   if (origin != NULL)
14841     add_abstract_origin_attribute (decl_die, origin);
14842   else
14843     {
14844       add_name_and_src_coords_attributes (decl_die, decl);
14845       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
14846                           0, 0, context_die);
14847     }
14848
14849   if (DECL_ABSTRACT (decl))
14850     equate_decl_number_to_die (decl, decl_die);
14851   else
14852     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
14853 }
14854 #endif
14855
14856 /* Walk through the list of incomplete types again, trying once more to
14857    emit full debugging info for them.  */
14858
14859 static void
14860 retry_incomplete_types (void)
14861 {
14862   int i;
14863
14864   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
14865     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
14866 }
14867
14868 /* Determine what tag to use for a record type.  */
14869
14870 static enum dwarf_tag
14871 record_type_tag (tree type)
14872 {
14873   if (! lang_hooks.types.classify_record)
14874     return DW_TAG_structure_type;
14875
14876   switch (lang_hooks.types.classify_record (type))
14877     {
14878     case RECORD_IS_STRUCT:
14879       return DW_TAG_structure_type;
14880
14881     case RECORD_IS_CLASS:
14882       return DW_TAG_class_type;
14883
14884     case RECORD_IS_INTERFACE:
14885       return DW_TAG_interface_type;
14886
14887     default:
14888       gcc_unreachable ();
14889     }
14890 }
14891
14892 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
14893    include all of the information about the enumeration values also. Each
14894    enumerated type name/value is listed as a child of the enumerated type
14895    DIE.  */
14896
14897 static dw_die_ref
14898 gen_enumeration_type_die (tree type, dw_die_ref context_die)
14899 {
14900   dw_die_ref type_die = lookup_type_die (type);
14901
14902   if (type_die == NULL)
14903     {
14904       type_die = new_die (DW_TAG_enumeration_type,
14905                           scope_die_for (type, context_die), type);
14906       equate_type_number_to_die (type, type_die);
14907       add_name_attribute (type_die, type_tag (type));
14908     }
14909   else if (! TYPE_SIZE (type))
14910     return type_die;
14911   else
14912     remove_AT (type_die, DW_AT_declaration);
14913
14914   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
14915      given enum type is incomplete, do not generate the DW_AT_byte_size
14916      attribute or the DW_AT_element_list attribute.  */
14917   if (TYPE_SIZE (type))
14918     {
14919       tree link;
14920
14921       TREE_ASM_WRITTEN (type) = 1;
14922       add_byte_size_attribute (type_die, type);
14923       if (TYPE_STUB_DECL (type) != NULL_TREE)
14924         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
14925
14926       /* If the first reference to this type was as the return type of an
14927          inline function, then it may not have a parent.  Fix this now.  */
14928       if (type_die->die_parent == NULL)
14929         add_child_die (scope_die_for (type, context_die), type_die);
14930
14931       for (link = TYPE_VALUES (type);
14932            link != NULL; link = TREE_CHAIN (link))
14933         {
14934           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
14935           tree value = TREE_VALUE (link);
14936
14937           add_name_attribute (enum_die,
14938                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
14939
14940           if (TREE_CODE (value) == CONST_DECL)
14941             value = DECL_INITIAL (value);
14942
14943           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
14944             /* DWARF2 does not provide a way of indicating whether or
14945                not enumeration constants are signed or unsigned.  GDB
14946                always assumes the values are signed, so we output all
14947                values as if they were signed.  That means that
14948                enumeration constants with very large unsigned values
14949                will appear to have negative values in the debugger.  */
14950             add_AT_int (enum_die, DW_AT_const_value,
14951                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
14952         }
14953     }
14954   else
14955     add_AT_flag (type_die, DW_AT_declaration, 1);
14956
14957   if (get_AT (type_die, DW_AT_name))
14958     add_pubtype (type, type_die);
14959
14960   return type_die;
14961 }
14962
14963 /* Generate a DIE to represent either a real live formal parameter decl or to
14964    represent just the type of some formal parameter position in some function
14965    type.
14966
14967    Note that this routine is a bit unusual because its argument may be a
14968    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
14969    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
14970    node.  If it's the former then this function is being called to output a
14971    DIE to represent a formal parameter object (or some inlining thereof).  If
14972    it's the latter, then this function is only being called to output a
14973    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
14974    argument type of some subprogram type.  */
14975
14976 static dw_die_ref
14977 gen_formal_parameter_die (tree node, tree origin, dw_die_ref context_die)
14978 {
14979   tree node_or_origin = node ? node : origin;
14980   dw_die_ref parm_die
14981     = new_die (DW_TAG_formal_parameter, context_die, node);
14982
14983   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
14984     {
14985     case tcc_declaration:
14986       if (!origin)
14987         origin = decl_ultimate_origin (node);
14988       if (origin != NULL)
14989         add_abstract_origin_attribute (parm_die, origin);
14990       else
14991         {
14992           tree type = TREE_TYPE (node);
14993           add_name_and_src_coords_attributes (parm_die, node);
14994           if (DECL_BY_REFERENCE (node))
14995             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
14996                                 context_die);
14997           else
14998             add_type_attribute (parm_die, type,
14999                                 TREE_READONLY (node),
15000                                 TREE_THIS_VOLATILE (node),
15001                                 context_die);
15002           if (DECL_ARTIFICIAL (node))
15003             add_AT_flag (parm_die, DW_AT_artificial, 1);
15004         }
15005
15006       if (node && node != origin)
15007         equate_decl_number_to_die (node, parm_die);
15008       if (! DECL_ABSTRACT (node_or_origin))
15009         add_location_or_const_value_attribute (parm_die, node_or_origin,
15010                                                DW_AT_location);
15011
15012       break;
15013
15014     case tcc_type:
15015       /* We were called with some kind of a ..._TYPE node.  */
15016       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
15017       break;
15018
15019     default:
15020       gcc_unreachable ();
15021     }
15022
15023   return parm_die;
15024 }
15025
15026 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
15027    at the end of an (ANSI prototyped) formal parameters list.  */
15028
15029 static void
15030 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
15031 {
15032   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
15033 }
15034
15035 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
15036    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
15037    parameters as specified in some function type specification (except for
15038    those which appear as part of a function *definition*).  */
15039
15040 static void
15041 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
15042 {
15043   tree link;
15044   tree formal_type = NULL;
15045   tree first_parm_type;
15046   tree arg;
15047
15048   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
15049     {
15050       arg = DECL_ARGUMENTS (function_or_method_type);
15051       function_or_method_type = TREE_TYPE (function_or_method_type);
15052     }
15053   else
15054     arg = NULL_TREE;
15055
15056   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
15057
15058   /* Make our first pass over the list of formal parameter types and output a
15059      DW_TAG_formal_parameter DIE for each one.  */
15060   for (link = first_parm_type; link; )
15061     {
15062       dw_die_ref parm_die;
15063
15064       formal_type = TREE_VALUE (link);
15065       if (formal_type == void_type_node)
15066         break;
15067
15068       /* Output a (nameless) DIE to represent the formal parameter itself.  */
15069       parm_die = gen_formal_parameter_die (formal_type, NULL, context_die);
15070       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
15071            && link == first_parm_type)
15072           || (arg && DECL_ARTIFICIAL (arg)))
15073         add_AT_flag (parm_die, DW_AT_artificial, 1);
15074
15075       link = TREE_CHAIN (link);
15076       if (arg)
15077         arg = TREE_CHAIN (arg);
15078     }
15079
15080   /* If this function type has an ellipsis, add a
15081      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
15082   if (formal_type != void_type_node)
15083     gen_unspecified_parameters_die (function_or_method_type, context_die);
15084
15085   /* Make our second (and final) pass over the list of formal parameter types
15086      and output DIEs to represent those types (as necessary).  */
15087   for (link = TYPE_ARG_TYPES (function_or_method_type);
15088        link && TREE_VALUE (link);
15089        link = TREE_CHAIN (link))
15090     gen_type_die (TREE_VALUE (link), context_die);
15091 }
15092
15093 /* We want to generate the DIE for TYPE so that we can generate the
15094    die for MEMBER, which has been defined; we will need to refer back
15095    to the member declaration nested within TYPE.  If we're trying to
15096    generate minimal debug info for TYPE, processing TYPE won't do the
15097    trick; we need to attach the member declaration by hand.  */
15098
15099 static void
15100 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
15101 {
15102   gen_type_die (type, context_die);
15103
15104   /* If we're trying to avoid duplicate debug info, we may not have
15105      emitted the member decl for this function.  Emit it now.  */
15106   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
15107       && ! lookup_decl_die (member))
15108     {
15109       dw_die_ref type_die;
15110       gcc_assert (!decl_ultimate_origin (member));
15111
15112       push_decl_scope (type);
15113       type_die = lookup_type_die (type);
15114       if (TREE_CODE (member) == FUNCTION_DECL)
15115         gen_subprogram_die (member, type_die);
15116       else if (TREE_CODE (member) == FIELD_DECL)
15117         {
15118           /* Ignore the nameless fields that are used to skip bits but handle
15119              C++ anonymous unions and structs.  */
15120           if (DECL_NAME (member) != NULL_TREE
15121               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
15122               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
15123             {
15124               gen_type_die (member_declared_type (member), type_die);
15125               gen_field_die (member, type_die);
15126             }
15127         }
15128       else
15129         gen_variable_die (member, NULL_TREE, type_die);
15130
15131       pop_decl_scope ();
15132     }
15133 }
15134
15135 /* Generate the DWARF2 info for the "abstract" instance of a function which we
15136    may later generate inlined and/or out-of-line instances of.  */
15137
15138 static void
15139 dwarf2out_abstract_function (tree decl)
15140 {
15141   dw_die_ref old_die;
15142   tree save_fn;
15143   tree context;
15144   int was_abstract = DECL_ABSTRACT (decl);
15145
15146   /* Make sure we have the actual abstract inline, not a clone.  */
15147   decl = DECL_ORIGIN (decl);
15148   htab_empty (decl_loc_table);
15149
15150   old_die = lookup_decl_die (decl);
15151   if (old_die && get_AT (old_die, DW_AT_inline))
15152     /* We've already generated the abstract instance.  */
15153     return;
15154
15155   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
15156      we don't get confused by DECL_ABSTRACT.  */
15157   if (debug_info_level > DINFO_LEVEL_TERSE)
15158     {
15159       context = decl_class_context (decl);
15160       if (context)
15161         gen_type_die_for_member
15162           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
15163     }
15164
15165   /* Pretend we've just finished compiling this function.  */
15166   save_fn = current_function_decl;
15167   current_function_decl = decl;
15168   push_cfun (DECL_STRUCT_FUNCTION (decl));
15169
15170   set_decl_abstract_flags (decl, 1);
15171   dwarf2out_decl (decl);
15172   if (! was_abstract)
15173     set_decl_abstract_flags (decl, 0);
15174
15175   current_function_decl = save_fn;
15176   pop_cfun ();
15177 }
15178
15179 /* Helper function of premark_used_types() which gets called through
15180    htab_traverse_resize().
15181
15182    Marks the DIE of a given type in *SLOT as perennial, so it never gets
15183    marked as unused by prune_unused_types.  */
15184 static int
15185 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
15186 {
15187   tree type;
15188   dw_die_ref die;
15189
15190   type = (tree) *slot;
15191   die = lookup_type_die (type);
15192   if (die != NULL)
15193     die->die_perennial_p = 1;
15194   return 1;
15195 }
15196
15197 /* Mark all members of used_types_hash as perennial.  */
15198 static void
15199 premark_used_types (void)
15200 {
15201   if (cfun && cfun->used_types_hash)
15202     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
15203 }
15204
15205 /* Generate a DIE to represent a declared function (either file-scope or
15206    block-local).  */
15207
15208 static void
15209 gen_subprogram_die (tree decl, dw_die_ref context_die)
15210 {
15211   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15212   tree origin = decl_ultimate_origin (decl);
15213   dw_die_ref subr_die;
15214   tree fn_arg_types;
15215   tree outer_scope;
15216   dw_die_ref old_die = lookup_decl_die (decl);
15217   int declaration = (current_function_decl != decl
15218                      || class_or_namespace_scope_p (context_die));
15219
15220   premark_used_types ();
15221
15222   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
15223      started to generate the abstract instance of an inline, decided to output
15224      its containing class, and proceeded to emit the declaration of the inline
15225      from the member list for the class.  If so, DECLARATION takes priority;
15226      we'll get back to the abstract instance when done with the class.  */
15227
15228   /* The class-scope declaration DIE must be the primary DIE.  */
15229   if (origin && declaration && class_or_namespace_scope_p (context_die))
15230     {
15231       origin = NULL;
15232       gcc_assert (!old_die);
15233     }
15234
15235   /* Now that the C++ front end lazily declares artificial member fns, we
15236      might need to retrofit the declaration into its class.  */
15237   if (!declaration && !origin && !old_die
15238       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
15239       && !class_or_namespace_scope_p (context_die)
15240       && debug_info_level > DINFO_LEVEL_TERSE)
15241     old_die = force_decl_die (decl);
15242
15243   if (origin != NULL)
15244     {
15245       gcc_assert (!declaration || local_scope_p (context_die));
15246
15247       /* Fixup die_parent for the abstract instance of a nested
15248          inline function.  */
15249       if (old_die && old_die->die_parent == NULL)
15250         add_child_die (context_die, old_die);
15251
15252       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15253       add_abstract_origin_attribute (subr_die, origin);
15254     }
15255   else if (old_die)
15256     {
15257       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15258       struct dwarf_file_data * file_index = lookup_filename (s.file);
15259
15260       if (!get_AT_flag (old_die, DW_AT_declaration)
15261           /* We can have a normal definition following an inline one in the
15262              case of redefinition of GNU C extern inlines.
15263              It seems reasonable to use AT_specification in this case.  */
15264           && !get_AT (old_die, DW_AT_inline))
15265         {
15266           /* Detect and ignore this case, where we are trying to output
15267              something we have already output.  */
15268           return;
15269         }
15270
15271       /* If the definition comes from the same place as the declaration,
15272          maybe use the old DIE.  We always want the DIE for this function
15273          that has the *_pc attributes to be under comp_unit_die so the
15274          debugger can find it.  We also need to do this for abstract
15275          instances of inlines, since the spec requires the out-of-line copy
15276          to have the same parent.  For local class methods, this doesn't
15277          apply; we just use the old DIE.  */
15278       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
15279           && (DECL_ARTIFICIAL (decl)
15280               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
15281                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
15282                       == (unsigned) s.line))))
15283         {
15284           subr_die = old_die;
15285
15286           /* Clear out the declaration attribute and the formal parameters.
15287              Do not remove all children, because it is possible that this
15288              declaration die was forced using force_decl_die(). In such
15289              cases die that forced declaration die (e.g. TAG_imported_module)
15290              is one of the children that we do not want to remove.  */
15291           remove_AT (subr_die, DW_AT_declaration);
15292           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
15293         }
15294       else
15295         {
15296           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15297           add_AT_specification (subr_die, old_die);
15298           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15299             add_AT_file (subr_die, DW_AT_decl_file, file_index);
15300           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15301             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
15302         }
15303     }
15304   else
15305     {
15306       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15307
15308       if (TREE_PUBLIC (decl))
15309         add_AT_flag (subr_die, DW_AT_external, 1);
15310
15311       add_name_and_src_coords_attributes (subr_die, decl);
15312       if (debug_info_level > DINFO_LEVEL_TERSE)
15313         {
15314           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
15315           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
15316                               0, 0, context_die);
15317         }
15318
15319       add_pure_or_virtual_attribute (subr_die, decl);
15320       if (DECL_ARTIFICIAL (decl))
15321         add_AT_flag (subr_die, DW_AT_artificial, 1);
15322
15323       if (TREE_PROTECTED (decl))
15324         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
15325       else if (TREE_PRIVATE (decl))
15326         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
15327     }
15328
15329   if (declaration)
15330     {
15331       if (!old_die || !get_AT (old_die, DW_AT_inline))
15332         {
15333           add_AT_flag (subr_die, DW_AT_declaration, 1);
15334
15335           /* If this is an explicit function declaration then generate
15336              a DW_AT_explicit attribute.  */
15337           if (lang_hooks.decls.function_decl_explicit_p (decl))
15338             add_AT_flag (subr_die, DW_AT_explicit, 1);
15339
15340           /* The first time we see a member function, it is in the context of
15341              the class to which it belongs.  We make sure of this by emitting
15342              the class first.  The next time is the definition, which is
15343              handled above.  The two may come from the same source text.
15344
15345              Note that force_decl_die() forces function declaration die. It is
15346              later reused to represent definition.  */
15347           equate_decl_number_to_die (decl, subr_die);
15348         }
15349     }
15350   else if (DECL_ABSTRACT (decl))
15351     {
15352       if (DECL_DECLARED_INLINE_P (decl))
15353         {
15354           if (cgraph_function_possibly_inlined_p (decl))
15355             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
15356           else
15357             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
15358         }
15359       else
15360         {
15361           if (cgraph_function_possibly_inlined_p (decl))
15362             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
15363           else
15364             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
15365         }
15366
15367       if (DECL_DECLARED_INLINE_P (decl)
15368           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
15369         add_AT_flag (subr_die, DW_AT_artificial, 1);
15370
15371       equate_decl_number_to_die (decl, subr_die);
15372     }
15373   else if (!DECL_EXTERNAL (decl))
15374     {
15375       HOST_WIDE_INT cfa_fb_offset;
15376
15377       if (!old_die || !get_AT (old_die, DW_AT_inline))
15378         equate_decl_number_to_die (decl, subr_die);
15379
15380       if (!flag_reorder_blocks_and_partition)
15381         {
15382           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
15383                                        current_function_funcdef_no);
15384           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
15385           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
15386                                        current_function_funcdef_no);
15387           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
15388
15389           add_pubname (decl, subr_die);
15390           add_arange (decl, subr_die);
15391         }
15392       else
15393         {  /* Do nothing for now; maybe need to duplicate die, one for
15394               hot section and one for cold section, then use the hot/cold
15395               section begin/end labels to generate the aranges...  */
15396           /*
15397             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
15398             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
15399             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
15400             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
15401
15402             add_pubname (decl, subr_die);
15403             add_arange (decl, subr_die);
15404             add_arange (decl, subr_die);
15405            */
15406         }
15407
15408 #ifdef MIPS_DEBUGGING_INFO
15409       /* Add a reference to the FDE for this routine.  */
15410       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
15411 #endif
15412
15413       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
15414
15415       /* We define the "frame base" as the function's CFA.  This is more
15416          convenient for several reasons: (1) It's stable across the prologue
15417          and epilogue, which makes it better than just a frame pointer,
15418          (2) With dwarf3, there exists a one-byte encoding that allows us
15419          to reference the .debug_frame data by proxy, but failing that,
15420          (3) We can at least reuse the code inspection and interpretation
15421          code that determines the CFA position at various points in the
15422          function.  */
15423       if (dwarf_version >= 3)
15424         {
15425           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
15426           add_AT_loc (subr_die, DW_AT_frame_base, op);
15427         }
15428       else
15429         {
15430           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
15431           if (list->dw_loc_next)
15432             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
15433           else
15434             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
15435         }
15436
15437       /* Compute a displacement from the "steady-state frame pointer" to
15438          the CFA.  The former is what all stack slots and argument slots
15439          will reference in the rtl; the later is what we've told the
15440          debugger about.  We'll need to adjust all frame_base references
15441          by this displacement.  */
15442       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
15443
15444       if (cfun->static_chain_decl)
15445         add_AT_location_description (subr_die, DW_AT_static_link,
15446                  loc_descriptor_from_tree (cfun->static_chain_decl));
15447     }
15448
15449   /* Generate child dies for template paramaters.  */
15450   if (debug_info_level > DINFO_LEVEL_TERSE)
15451     gen_generic_params_dies (decl);
15452
15453   /* Now output descriptions of the arguments for this function. This gets
15454      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
15455      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
15456      `...' at the end of the formal parameter list.  In order to find out if
15457      there was a trailing ellipsis or not, we must instead look at the type
15458      associated with the FUNCTION_DECL.  This will be a node of type
15459      FUNCTION_TYPE. If the chain of type nodes hanging off of this
15460      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
15461      an ellipsis at the end.  */
15462
15463   /* In the case where we are describing a mere function declaration, all we
15464      need to do here (and all we *can* do here) is to describe the *types* of
15465      its formal parameters.  */
15466   if (debug_info_level <= DINFO_LEVEL_TERSE)
15467     ;
15468   else if (declaration)
15469     gen_formal_types_die (decl, subr_die);
15470   else
15471     {
15472       /* Generate DIEs to represent all known formal parameters.  */
15473       tree arg_decls = DECL_ARGUMENTS (decl);
15474       tree parm;
15475
15476       /* When generating DIEs, generate the unspecified_parameters DIE
15477          instead if we come across the arg "__builtin_va_alist" */
15478       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
15479         if (TREE_CODE (parm) == PARM_DECL)
15480           {
15481             if (DECL_NAME (parm)
15482                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
15483                             "__builtin_va_alist"))
15484               gen_unspecified_parameters_die (parm, subr_die);
15485             else
15486               gen_decl_die (parm, NULL, subr_die);
15487           }
15488
15489       /* Decide whether we need an unspecified_parameters DIE at the end.
15490          There are 2 more cases to do this for: 1) the ansi ... declaration -
15491          this is detectable when the end of the arg list is not a
15492          void_type_node 2) an unprototyped function declaration (not a
15493          definition).  This just means that we have no info about the
15494          parameters at all.  */
15495       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
15496       if (fn_arg_types != NULL)
15497         {
15498           /* This is the prototyped case, check for....  */
15499           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
15500             gen_unspecified_parameters_die (decl, subr_die);
15501         }
15502       else if (DECL_INITIAL (decl) == NULL_TREE)
15503         gen_unspecified_parameters_die (decl, subr_die);
15504     }
15505
15506   /* Output Dwarf info for all of the stuff within the body of the function
15507      (if it has one - it may be just a declaration).  */
15508   outer_scope = DECL_INITIAL (decl);
15509
15510   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
15511      a function.  This BLOCK actually represents the outermost binding contour
15512      for the function, i.e. the contour in which the function's formal
15513      parameters and labels get declared. Curiously, it appears that the front
15514      end doesn't actually put the PARM_DECL nodes for the current function onto
15515      the BLOCK_VARS list for this outer scope, but are strung off of the
15516      DECL_ARGUMENTS list for the function instead.
15517
15518      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
15519      the LABEL_DECL nodes for the function however, and we output DWARF info
15520      for those in decls_for_scope.  Just within the `outer_scope' there will be
15521      a BLOCK node representing the function's outermost pair of curly braces,
15522      and any blocks used for the base and member initializers of a C++
15523      constructor function.  */
15524   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
15525     {
15526       /* Emit a DW_TAG_variable DIE for a named return value.  */
15527       if (DECL_NAME (DECL_RESULT (decl)))
15528         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
15529
15530       current_function_has_inlines = 0;
15531       decls_for_scope (outer_scope, subr_die, 0);
15532
15533 #if 0 && defined (MIPS_DEBUGGING_INFO)
15534       if (current_function_has_inlines)
15535         {
15536           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
15537           if (! comp_unit_has_inlines)
15538             {
15539               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
15540               comp_unit_has_inlines = 1;
15541             }
15542         }
15543 #endif
15544     }
15545   /* Add the calling convention attribute if requested.  */
15546   add_calling_convention_attribute (subr_die, decl);
15547
15548 }
15549
15550 /* Returns a hash value for X (which really is a die_struct).  */
15551
15552 static hashval_t
15553 common_block_die_table_hash (const void *x)
15554 {
15555   const_dw_die_ref d = (const_dw_die_ref) x;
15556   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
15557 }
15558
15559 /* Return nonzero if decl_id and die_parent of die_struct X is the same
15560    as decl_id and die_parent of die_struct Y.  */
15561
15562 static int
15563 common_block_die_table_eq (const void *x, const void *y)
15564 {
15565   const_dw_die_ref d = (const_dw_die_ref) x;
15566   const_dw_die_ref e = (const_dw_die_ref) y;
15567   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
15568 }
15569
15570 /* Generate a DIE to represent a declared data object.
15571    Either DECL or ORIGIN must be non-null.  */
15572
15573 static void
15574 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
15575 {
15576   HOST_WIDE_INT off;
15577   tree com_decl;
15578   tree decl_or_origin = decl ? decl : origin;
15579   dw_die_ref var_die;
15580   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
15581   dw_die_ref origin_die;
15582   int declaration = (DECL_EXTERNAL (decl_or_origin)
15583                      /* If DECL is COMDAT and has not actually been
15584                         emitted, we cannot take its address; there
15585                         might end up being no definition anywhere in
15586                         the program.  For example, consider the C++
15587                         test case:
15588
15589                           template <class T>
15590                           struct S { static const int i = 7; };
15591
15592                           template <class T>
15593                           const int S<T>::i;
15594
15595                           int f() { return S<int>::i; }
15596
15597                         Here, S<int>::i is not DECL_EXTERNAL, but no
15598                         definition is required, so the compiler will
15599                         not emit a definition.  */
15600                      || (TREE_CODE (decl_or_origin) == VAR_DECL
15601                          && DECL_COMDAT (decl_or_origin)
15602                          && !TREE_ASM_WRITTEN (decl_or_origin))
15603                      || class_or_namespace_scope_p (context_die));
15604
15605   if (!origin)
15606     origin = decl_ultimate_origin (decl);
15607
15608   com_decl = fortran_common (decl_or_origin, &off);
15609
15610   /* Symbol in common gets emitted as a child of the common block, in the form
15611      of a data member.  */
15612   if (com_decl)
15613     {
15614       tree field;
15615       dw_die_ref com_die;
15616       dw_loc_descr_ref loc;
15617       die_node com_die_arg;
15618
15619       var_die = lookup_decl_die (decl_or_origin);
15620       if (var_die)
15621         {
15622           if (get_AT (var_die, DW_AT_location) == NULL)
15623             {
15624               loc = loc_descriptor_from_tree (com_decl);
15625               if (loc)
15626                 {
15627                   if (off)
15628                     {
15629                       /* Optimize the common case.  */
15630                       if (loc->dw_loc_opc == DW_OP_addr
15631                           && loc->dw_loc_next == NULL
15632                           && GET_CODE (loc->dw_loc_oprnd1.v.val_addr)
15633                              == SYMBOL_REF)
15634                         loc->dw_loc_oprnd1.v.val_addr
15635                           = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
15636                         else
15637                           loc_descr_plus_const (&loc, off);
15638                     }
15639                   add_AT_loc (var_die, DW_AT_location, loc);
15640                   remove_AT (var_die, DW_AT_declaration);
15641                 }
15642             }
15643           return;
15644         }
15645
15646       if (common_block_die_table == NULL)
15647         common_block_die_table
15648           = htab_create_ggc (10, common_block_die_table_hash,
15649                              common_block_die_table_eq, NULL);
15650
15651       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
15652       com_die_arg.decl_id = DECL_UID (com_decl);
15653       com_die_arg.die_parent = context_die;
15654       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
15655       loc = loc_descriptor_from_tree (com_decl);
15656       if (com_die == NULL)
15657         {
15658           const char *cnam
15659             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
15660           void **slot;
15661
15662           com_die = new_die (DW_TAG_common_block, context_die, decl);
15663           add_name_and_src_coords_attributes (com_die, com_decl);
15664           if (loc)
15665             {
15666               add_AT_loc (com_die, DW_AT_location, loc);
15667               /* Avoid sharing the same loc descriptor between
15668                  DW_TAG_common_block and DW_TAG_variable.  */
15669               loc = loc_descriptor_from_tree (com_decl);
15670             }
15671           else if (DECL_EXTERNAL (decl))
15672             add_AT_flag (com_die, DW_AT_declaration, 1);
15673           add_pubname_string (cnam, com_die); /* ??? needed? */
15674           com_die->decl_id = DECL_UID (com_decl);
15675           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
15676           *slot = (void *) com_die;
15677         }
15678       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
15679         {
15680           add_AT_loc (com_die, DW_AT_location, loc);
15681           loc = loc_descriptor_from_tree (com_decl);
15682           remove_AT (com_die, DW_AT_declaration);
15683         }
15684       var_die = new_die (DW_TAG_variable, com_die, decl);
15685       add_name_and_src_coords_attributes (var_die, decl);
15686       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
15687                           TREE_THIS_VOLATILE (decl), context_die);
15688       add_AT_flag (var_die, DW_AT_external, 1);
15689       if (loc)
15690         {
15691           if (off)
15692             {
15693               /* Optimize the common case.  */
15694               if (loc->dw_loc_opc == DW_OP_addr
15695                   && loc->dw_loc_next == NULL
15696                   && GET_CODE (loc->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
15697                 loc->dw_loc_oprnd1.v.val_addr
15698                   = plus_constant (loc->dw_loc_oprnd1.v.val_addr, off);
15699               else
15700                 loc_descr_plus_const (&loc, off);
15701             }
15702           add_AT_loc (var_die, DW_AT_location, loc);
15703         }
15704       else if (DECL_EXTERNAL (decl))
15705         add_AT_flag (var_die, DW_AT_declaration, 1);
15706       equate_decl_number_to_die (decl, var_die);
15707       return;
15708     }
15709
15710   /* If the compiler emitted a definition for the DECL declaration
15711      and if we already emitted a DIE for it, don't emit a second
15712      DIE for it again.  */
15713   if (old_die
15714       && declaration
15715       && old_die->die_parent == context_die)
15716     return;
15717
15718   /* For static data members, the declaration in the class is supposed
15719      to have DW_TAG_member tag; the specification should still be
15720      DW_TAG_variable referencing the DW_TAG_member DIE.  */
15721   if (declaration && class_scope_p (context_die))
15722     var_die = new_die (DW_TAG_member, context_die, decl);
15723   else
15724     var_die = new_die (DW_TAG_variable, context_die, decl);
15725
15726   origin_die = NULL;
15727   if (origin != NULL)
15728     origin_die = add_abstract_origin_attribute (var_die, origin);
15729
15730   /* Loop unrolling can create multiple blocks that refer to the same
15731      static variable, so we must test for the DW_AT_declaration flag.
15732
15733      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
15734      copy decls and set the DECL_ABSTRACT flag on them instead of
15735      sharing them.
15736
15737      ??? Duplicated blocks have been rewritten to use .debug_ranges.
15738
15739      ??? The declare_in_namespace support causes us to get two DIEs for one
15740      variable, both of which are declarations.  We want to avoid considering
15741      one to be a specification, so we must test that this DIE is not a
15742      declaration.  */
15743   else if (old_die && TREE_STATIC (decl) && ! declaration
15744            && get_AT_flag (old_die, DW_AT_declaration) == 1)
15745     {
15746       /* This is a definition of a C++ class level static.  */
15747       add_AT_specification (var_die, old_die);
15748       if (DECL_NAME (decl))
15749         {
15750           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15751           struct dwarf_file_data * file_index = lookup_filename (s.file);
15752
15753           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15754             add_AT_file (var_die, DW_AT_decl_file, file_index);
15755
15756           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15757             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
15758         }
15759     }
15760   else
15761     {
15762       tree type = TREE_TYPE (decl);
15763
15764       add_name_and_src_coords_attributes (var_die, decl);
15765       if ((TREE_CODE (decl) == PARM_DECL
15766            || TREE_CODE (decl) == RESULT_DECL
15767            || TREE_CODE (decl) == VAR_DECL)
15768           && DECL_BY_REFERENCE (decl))
15769         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
15770       else
15771         add_type_attribute (var_die, type, TREE_READONLY (decl),
15772                             TREE_THIS_VOLATILE (decl), context_die);
15773
15774       if (TREE_PUBLIC (decl))
15775         add_AT_flag (var_die, DW_AT_external, 1);
15776
15777       if (DECL_ARTIFICIAL (decl))
15778         add_AT_flag (var_die, DW_AT_artificial, 1);
15779
15780       if (TREE_PROTECTED (decl))
15781         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
15782       else if (TREE_PRIVATE (decl))
15783         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
15784     }
15785
15786   if (declaration)
15787     add_AT_flag (var_die, DW_AT_declaration, 1);
15788
15789   if (decl && (DECL_ABSTRACT (decl) || declaration))
15790     equate_decl_number_to_die (decl, var_die);
15791
15792   if (! declaration
15793       && (! DECL_ABSTRACT (decl_or_origin)
15794           /* Local static vars are shared between all clones/inlines,
15795              so emit DW_AT_location on the abstract DIE if DECL_RTL is
15796              already set.  */
15797           || (TREE_CODE (decl_or_origin) == VAR_DECL
15798               && TREE_STATIC (decl_or_origin)
15799               && DECL_RTL_SET_P (decl_or_origin)))
15800       /* When abstract origin already has DW_AT_location attribute, no need
15801          to add it again.  */
15802       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
15803     {
15804       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
15805           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
15806         defer_location (decl_or_origin, var_die);
15807       else
15808         add_location_or_const_value_attribute (var_die,
15809                                                decl_or_origin,
15810                                                DW_AT_location);
15811       add_pubname (decl_or_origin, var_die);
15812     }
15813   else
15814     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
15815 }
15816
15817 /* Generate a DIE to represent a named constant.  */
15818
15819 static void
15820 gen_const_die (tree decl, dw_die_ref context_die)
15821 {
15822   dw_die_ref const_die;
15823   tree type = TREE_TYPE (decl);
15824
15825   const_die = new_die (DW_TAG_constant, context_die, decl);
15826   add_name_and_src_coords_attributes (const_die, decl);
15827   add_type_attribute (const_die, type, 1, 0, context_die);
15828   if (TREE_PUBLIC (decl))
15829     add_AT_flag (const_die, DW_AT_external, 1);
15830   if (DECL_ARTIFICIAL (decl))
15831     add_AT_flag (const_die, DW_AT_artificial, 1);
15832   tree_add_const_value_attribute_for_decl (const_die, decl);
15833 }
15834
15835 /* Generate a DIE to represent a label identifier.  */
15836
15837 static void
15838 gen_label_die (tree decl, dw_die_ref context_die)
15839 {
15840   tree origin = decl_ultimate_origin (decl);
15841   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
15842   rtx insn;
15843   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15844
15845   if (origin != NULL)
15846     add_abstract_origin_attribute (lbl_die, origin);
15847   else
15848     add_name_and_src_coords_attributes (lbl_die, decl);
15849
15850   if (DECL_ABSTRACT (decl))
15851     equate_decl_number_to_die (decl, lbl_die);
15852   else
15853     {
15854       insn = DECL_RTL_IF_SET (decl);
15855
15856       /* Deleted labels are programmer specified labels which have been
15857          eliminated because of various optimizations.  We still emit them
15858          here so that it is possible to put breakpoints on them.  */
15859       if (insn
15860           && (LABEL_P (insn)
15861               || ((NOTE_P (insn)
15862                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
15863         {
15864           /* When optimization is enabled (via -O) some parts of the compiler
15865              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
15866              represent source-level labels which were explicitly declared by
15867              the user.  This really shouldn't be happening though, so catch
15868              it if it ever does happen.  */
15869           gcc_assert (!INSN_DELETED_P (insn));
15870
15871           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
15872           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
15873         }
15874     }
15875 }
15876
15877 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
15878    attributes to the DIE for a block STMT, to describe where the inlined
15879    function was called from.  This is similar to add_src_coords_attributes.  */
15880
15881 static inline void
15882 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
15883 {
15884   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
15885
15886   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
15887   add_AT_unsigned (die, DW_AT_call_line, s.line);
15888 }
15889
15890
15891 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
15892    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
15893
15894 static inline void
15895 add_high_low_attributes (tree stmt, dw_die_ref die)
15896 {
15897   char label[MAX_ARTIFICIAL_LABEL_BYTES];
15898
15899   if (BLOCK_FRAGMENT_CHAIN (stmt))
15900     {
15901       tree chain;
15902
15903       if (inlined_function_outer_scope_p (stmt))
15904         {
15905           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15906                                        BLOCK_NUMBER (stmt));
15907           add_AT_lbl_id (die, DW_AT_entry_pc, label);
15908         }
15909
15910       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
15911
15912       chain = BLOCK_FRAGMENT_CHAIN (stmt);
15913       do
15914         {
15915           add_ranges (chain);
15916           chain = BLOCK_FRAGMENT_CHAIN (chain);
15917         }
15918       while (chain);
15919       add_ranges (NULL);
15920     }
15921   else
15922     {
15923       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
15924                                    BLOCK_NUMBER (stmt));
15925       add_AT_lbl_id (die, DW_AT_low_pc, label);
15926       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
15927                                    BLOCK_NUMBER (stmt));
15928       add_AT_lbl_id (die, DW_AT_high_pc, label);
15929     }
15930 }
15931
15932 /* Generate a DIE for a lexical block.  */
15933
15934 static void
15935 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
15936 {
15937   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
15938
15939   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
15940     add_high_low_attributes (stmt, stmt_die);
15941
15942   decls_for_scope (stmt, stmt_die, depth);
15943 }
15944
15945 /* Generate a DIE for an inlined subprogram.  */
15946
15947 static void
15948 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
15949 {
15950   tree decl;
15951
15952   /* The instance of function that is effectively being inlined shall not
15953      be abstract.  */
15954   gcc_assert (! BLOCK_ABSTRACT (stmt));
15955
15956   decl = block_ultimate_origin (stmt);
15957
15958   /* Emit info for the abstract instance first, if we haven't yet.  We
15959      must emit this even if the block is abstract, otherwise when we
15960      emit the block below (or elsewhere), we may end up trying to emit
15961      a die whose origin die hasn't been emitted, and crashing.  */
15962   dwarf2out_abstract_function (decl);
15963
15964   if (! BLOCK_ABSTRACT (stmt))
15965     {
15966       dw_die_ref subr_die
15967         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
15968
15969       add_abstract_origin_attribute (subr_die, decl);
15970       if (TREE_ASM_WRITTEN (stmt))
15971         add_high_low_attributes (stmt, subr_die);
15972       add_call_src_coords_attributes (stmt, subr_die);
15973
15974       decls_for_scope (stmt, subr_die, depth);
15975       current_function_has_inlines = 1;
15976     }
15977 }
15978
15979 /* Generate a DIE for a field in a record, or structure.  */
15980
15981 static void
15982 gen_field_die (tree decl, dw_die_ref context_die)
15983 {
15984   dw_die_ref decl_die;
15985
15986   if (TREE_TYPE (decl) == error_mark_node)
15987     return;
15988
15989   decl_die = new_die (DW_TAG_member, context_die, decl);
15990   add_name_and_src_coords_attributes (decl_die, decl);
15991   add_type_attribute (decl_die, member_declared_type (decl),
15992                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
15993                       context_die);
15994
15995   if (DECL_BIT_FIELD_TYPE (decl))
15996     {
15997       add_byte_size_attribute (decl_die, decl);
15998       add_bit_size_attribute (decl_die, decl);
15999       add_bit_offset_attribute (decl_die, decl);
16000     }
16001
16002   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
16003     add_data_member_location_attribute (decl_die, decl);
16004
16005   if (DECL_ARTIFICIAL (decl))
16006     add_AT_flag (decl_die, DW_AT_artificial, 1);
16007
16008   if (TREE_PROTECTED (decl))
16009     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
16010   else if (TREE_PRIVATE (decl))
16011     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
16012
16013   /* Equate decl number to die, so that we can look up this decl later on.  */
16014   equate_decl_number_to_die (decl, decl_die);
16015 }
16016
16017 #if 0
16018 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16019    Use modified_type_die instead.
16020    We keep this code here just in case these types of DIEs may be needed to
16021    represent certain things in other languages (e.g. Pascal) someday.  */
16022
16023 static void
16024 gen_pointer_type_die (tree type, dw_die_ref context_die)
16025 {
16026   dw_die_ref ptr_die
16027     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
16028
16029   equate_type_number_to_die (type, ptr_die);
16030   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16031   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16032 }
16033
16034 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16035    Use modified_type_die instead.
16036    We keep this code here just in case these types of DIEs may be needed to
16037    represent certain things in other languages (e.g. Pascal) someday.  */
16038
16039 static void
16040 gen_reference_type_die (tree type, dw_die_ref context_die)
16041 {
16042   dw_die_ref ref_die
16043     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
16044
16045   equate_type_number_to_die (type, ref_die);
16046   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
16047   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16048 }
16049 #endif
16050
16051 /* Generate a DIE for a pointer to a member type.  */
16052
16053 static void
16054 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
16055 {
16056   dw_die_ref ptr_die
16057     = new_die (DW_TAG_ptr_to_member_type,
16058                scope_die_for (type, context_die), type);
16059
16060   equate_type_number_to_die (type, ptr_die);
16061   add_AT_die_ref (ptr_die, DW_AT_containing_type,
16062                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
16063   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16064 }
16065
16066 /* Generate the DIE for the compilation unit.  */
16067
16068 static dw_die_ref
16069 gen_compile_unit_die (const char *filename)
16070 {
16071   dw_die_ref die;
16072   char producer[250];
16073   const char *language_string = lang_hooks.name;
16074   int language;
16075
16076   die = new_die (DW_TAG_compile_unit, NULL, NULL);
16077
16078   if (filename)
16079     {
16080       add_name_attribute (die, filename);
16081       /* Don't add cwd for <built-in>.  */
16082       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
16083         add_comp_dir_attribute (die);
16084     }
16085
16086   sprintf (producer, "%s %s", language_string, version_string);
16087
16088 #ifdef MIPS_DEBUGGING_INFO
16089   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
16090      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
16091      not appear in the producer string, the debugger reaches the conclusion
16092      that the object file is stripped and has no debugging information.
16093      To get the MIPS/SGI debugger to believe that there is debugging
16094      information in the object file, we add a -g to the producer string.  */
16095   if (debug_info_level > DINFO_LEVEL_TERSE)
16096     strcat (producer, " -g");
16097 #endif
16098
16099   add_AT_string (die, DW_AT_producer, producer);
16100
16101   if (strcmp (language_string, "GNU C++") == 0)
16102     language = DW_LANG_C_plus_plus;
16103   else if (strcmp (language_string, "GNU Ada") == 0)
16104     language = DW_LANG_Ada95;
16105   else if (strcmp (language_string, "GNU F77") == 0)
16106     language = DW_LANG_Fortran77;
16107   else if (strcmp (language_string, "GNU Fortran") == 0)
16108     language = DW_LANG_Fortran95;
16109   else if (strcmp (language_string, "GNU Pascal") == 0)
16110     language = DW_LANG_Pascal83;
16111   else if (strcmp (language_string, "GNU Java") == 0)
16112     language = DW_LANG_Java;
16113   else if (strcmp (language_string, "GNU Objective-C") == 0)
16114     language = DW_LANG_ObjC;
16115   else if (strcmp (language_string, "GNU Objective-C++") == 0)
16116     language = DW_LANG_ObjC_plus_plus;
16117   else
16118     language = DW_LANG_C89;
16119
16120   add_AT_unsigned (die, DW_AT_language, language);
16121   return die;
16122 }
16123
16124 /* Generate the DIE for a base class.  */
16125
16126 static void
16127 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
16128 {
16129   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
16130
16131   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
16132   add_data_member_location_attribute (die, binfo);
16133
16134   if (BINFO_VIRTUAL_P (binfo))
16135     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16136
16137   if (access == access_public_node)
16138     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16139   else if (access == access_protected_node)
16140     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16141 }
16142
16143 /* Generate a DIE for a class member.  */
16144
16145 static void
16146 gen_member_die (tree type, dw_die_ref context_die)
16147 {
16148   tree member;
16149   tree binfo = TYPE_BINFO (type);
16150   dw_die_ref child;
16151
16152   /* If this is not an incomplete type, output descriptions of each of its
16153      members. Note that as we output the DIEs necessary to represent the
16154      members of this record or union type, we will also be trying to output
16155      DIEs to represent the *types* of those members. However the `type'
16156      function (above) will specifically avoid generating type DIEs for member
16157      types *within* the list of member DIEs for this (containing) type except
16158      for those types (of members) which are explicitly marked as also being
16159      members of this (containing) type themselves.  The g++ front- end can
16160      force any given type to be treated as a member of some other (containing)
16161      type by setting the TYPE_CONTEXT of the given (member) type to point to
16162      the TREE node representing the appropriate (containing) type.  */
16163
16164   /* First output info about the base classes.  */
16165   if (binfo)
16166     {
16167       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
16168       int i;
16169       tree base;
16170
16171       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
16172         gen_inheritance_die (base,
16173                              (accesses ? VEC_index (tree, accesses, i)
16174                               : access_public_node), context_die);
16175     }
16176
16177   /* Now output info about the data members and type members.  */
16178   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
16179     {
16180       /* If we thought we were generating minimal debug info for TYPE
16181          and then changed our minds, some of the member declarations
16182          may have already been defined.  Don't define them again, but
16183          do put them in the right order.  */
16184
16185       child = lookup_decl_die (member);
16186       if (child)
16187         splice_child_die (context_die, child);
16188       else
16189         gen_decl_die (member, NULL, context_die);
16190     }
16191
16192   /* Now output info about the function members (if any).  */
16193   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
16194     {
16195       /* Don't include clones in the member list.  */
16196       if (DECL_ABSTRACT_ORIGIN (member))
16197         continue;
16198
16199       child = lookup_decl_die (member);
16200       if (child)
16201         splice_child_die (context_die, child);
16202       else
16203         gen_decl_die (member, NULL, context_die);
16204     }
16205 }
16206
16207 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
16208    is set, we pretend that the type was never defined, so we only get the
16209    member DIEs needed by later specification DIEs.  */
16210
16211 static void
16212 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
16213                                 enum debug_info_usage usage)
16214 {
16215   dw_die_ref type_die = lookup_type_die (type);
16216   dw_die_ref scope_die = 0;
16217   int nested = 0;
16218   int complete = (TYPE_SIZE (type)
16219                   && (! TYPE_STUB_DECL (type)
16220                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
16221   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
16222   complete = complete && should_emit_struct_debug (type, usage);
16223
16224   if (type_die && ! complete)
16225     return;
16226
16227   if (TYPE_CONTEXT (type) != NULL_TREE
16228       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16229           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
16230     nested = 1;
16231
16232   scope_die = scope_die_for (type, context_die);
16233
16234   if (! type_die || (nested && scope_die == comp_unit_die))
16235     /* First occurrence of type or toplevel definition of nested class.  */
16236     {
16237       dw_die_ref old_die = type_die;
16238
16239       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
16240                           ? record_type_tag (type) : DW_TAG_union_type,
16241                           scope_die, type);
16242       equate_type_number_to_die (type, type_die);
16243       if (old_die)
16244         add_AT_specification (type_die, old_die);
16245       else
16246         add_name_attribute (type_die, type_tag (type));
16247     }
16248   else
16249     remove_AT (type_die, DW_AT_declaration);
16250
16251   /* Generate child dies for template paramaters.  */
16252   if (debug_info_level > DINFO_LEVEL_TERSE
16253       && COMPLETE_TYPE_P (type))
16254     gen_generic_params_dies (type);
16255
16256   /* If this type has been completed, then give it a byte_size attribute and
16257      then give a list of members.  */
16258   if (complete && !ns_decl)
16259     {
16260       /* Prevent infinite recursion in cases where the type of some member of
16261          this type is expressed in terms of this type itself.  */
16262       TREE_ASM_WRITTEN (type) = 1;
16263       add_byte_size_attribute (type_die, type);
16264       if (TYPE_STUB_DECL (type) != NULL_TREE)
16265         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16266
16267       /* If the first reference to this type was as the return type of an
16268          inline function, then it may not have a parent.  Fix this now.  */
16269       if (type_die->die_parent == NULL)
16270         add_child_die (scope_die, type_die);
16271
16272       push_decl_scope (type);
16273       gen_member_die (type, type_die);
16274       pop_decl_scope ();
16275
16276       /* GNU extension: Record what type our vtable lives in.  */
16277       if (TYPE_VFIELD (type))
16278         {
16279           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
16280
16281           gen_type_die (vtype, context_die);
16282           add_AT_die_ref (type_die, DW_AT_containing_type,
16283                           lookup_type_die (vtype));
16284         }
16285     }
16286   else
16287     {
16288       add_AT_flag (type_die, DW_AT_declaration, 1);
16289
16290       /* We don't need to do this for function-local types.  */
16291       if (TYPE_STUB_DECL (type)
16292           && ! decl_function_context (TYPE_STUB_DECL (type)))
16293         VEC_safe_push (tree, gc, incomplete_types, type);
16294     }
16295
16296   if (get_AT (type_die, DW_AT_name))
16297     add_pubtype (type, type_die);
16298 }
16299
16300 /* Generate a DIE for a subroutine _type_.  */
16301
16302 static void
16303 gen_subroutine_type_die (tree type, dw_die_ref context_die)
16304 {
16305   tree return_type = TREE_TYPE (type);
16306   dw_die_ref subr_die
16307     = new_die (DW_TAG_subroutine_type,
16308                scope_die_for (type, context_die), type);
16309
16310   equate_type_number_to_die (type, subr_die);
16311   add_prototyped_attribute (subr_die, type);
16312   add_type_attribute (subr_die, return_type, 0, 0, context_die);
16313   gen_formal_types_die (type, subr_die);
16314
16315   if (get_AT (subr_die, DW_AT_name))
16316     add_pubtype (type, subr_die);
16317 }
16318
16319 /* Generate a DIE for a type definition.  */
16320
16321 static void
16322 gen_typedef_die (tree decl, dw_die_ref context_die)
16323 {
16324   dw_die_ref type_die;
16325   tree origin;
16326
16327   if (TREE_ASM_WRITTEN (decl))
16328     return;
16329
16330   TREE_ASM_WRITTEN (decl) = 1;
16331   type_die = new_die (DW_TAG_typedef, context_die, decl);
16332   origin = decl_ultimate_origin (decl);
16333   if (origin != NULL)
16334     add_abstract_origin_attribute (type_die, origin);
16335   else
16336     {
16337       tree type;
16338
16339       add_name_and_src_coords_attributes (type_die, decl);
16340       if (DECL_ORIGINAL_TYPE (decl))
16341         {
16342           type = DECL_ORIGINAL_TYPE (decl);
16343
16344           gcc_assert (type != TREE_TYPE (decl));
16345           equate_type_number_to_die (TREE_TYPE (decl), type_die);
16346         }
16347       else
16348         type = TREE_TYPE (decl);
16349
16350       add_type_attribute (type_die, type, TREE_READONLY (decl),
16351                           TREE_THIS_VOLATILE (decl), context_die);
16352     }
16353
16354   if (DECL_ABSTRACT (decl))
16355     equate_decl_number_to_die (decl, type_die);
16356
16357   if (get_AT (type_die, DW_AT_name))
16358     add_pubtype (decl, type_die);
16359 }
16360
16361 /* Generate a type description DIE.  */
16362
16363 static void
16364 gen_type_die_with_usage (tree type, dw_die_ref context_die,
16365                                 enum debug_info_usage usage)
16366 {
16367   int need_pop;
16368   struct array_descr_info info;
16369
16370   if (type == NULL_TREE || type == error_mark_node)
16371     return;
16372
16373   /* If TYPE is a typedef type variant, let's generate debug info
16374      for the parent typedef which TYPE is a type of.  */
16375   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
16376       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
16377     {
16378       if (TREE_ASM_WRITTEN (type))
16379         return;
16380
16381       /* Prevent broken recursion; we can't hand off to the same type.  */
16382       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
16383
16384       /* Use the DIE of the containing namespace as the parent DIE of
16385          the type description DIE we want to generate.  */
16386       if (DECL_CONTEXT (TYPE_NAME (type))
16387           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
16388         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
16389
16390       TREE_ASM_WRITTEN (type) = 1;
16391       gen_decl_die (TYPE_NAME (type), NULL, context_die);
16392       return;
16393     }
16394
16395   /* If this is an array type with hidden descriptor, handle it first.  */
16396   if (!TREE_ASM_WRITTEN (type)
16397       && lang_hooks.types.get_array_descr_info
16398       && lang_hooks.types.get_array_descr_info (type, &info))
16399     {
16400       gen_descr_array_type_die (type, &info, context_die);
16401       TREE_ASM_WRITTEN (type) = 1;
16402       return;
16403     }
16404
16405   /* We are going to output a DIE to represent the unqualified version
16406      of this type (i.e. without any const or volatile qualifiers) so
16407      get the main variant (i.e. the unqualified version) of this type
16408      now.  (Vectors are special because the debugging info is in the
16409      cloned type itself).  */
16410   if (TREE_CODE (type) != VECTOR_TYPE)
16411     type = type_main_variant (type);
16412
16413   if (TREE_ASM_WRITTEN (type))
16414     return;
16415
16416   switch (TREE_CODE (type))
16417     {
16418     case ERROR_MARK:
16419       break;
16420
16421     case POINTER_TYPE:
16422     case REFERENCE_TYPE:
16423       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
16424          ensures that the gen_type_die recursion will terminate even if the
16425          type is recursive.  Recursive types are possible in Ada.  */
16426       /* ??? We could perhaps do this for all types before the switch
16427          statement.  */
16428       TREE_ASM_WRITTEN (type) = 1;
16429
16430       /* For these types, all that is required is that we output a DIE (or a
16431          set of DIEs) to represent the "basis" type.  */
16432       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16433                                 DINFO_USAGE_IND_USE);
16434       break;
16435
16436     case OFFSET_TYPE:
16437       /* This code is used for C++ pointer-to-data-member types.
16438          Output a description of the relevant class type.  */
16439       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
16440                                         DINFO_USAGE_IND_USE);
16441
16442       /* Output a description of the type of the object pointed to.  */
16443       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16444                                         DINFO_USAGE_IND_USE);
16445
16446       /* Now output a DIE to represent this pointer-to-data-member type
16447          itself.  */
16448       gen_ptr_to_mbr_type_die (type, context_die);
16449       break;
16450
16451     case FUNCTION_TYPE:
16452       /* Force out return type (in case it wasn't forced out already).  */
16453       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16454                                         DINFO_USAGE_DIR_USE);
16455       gen_subroutine_type_die (type, context_die);
16456       break;
16457
16458     case METHOD_TYPE:
16459       /* Force out return type (in case it wasn't forced out already).  */
16460       gen_type_die_with_usage (TREE_TYPE (type), context_die,
16461                                         DINFO_USAGE_DIR_USE);
16462       gen_subroutine_type_die (type, context_die);
16463       break;
16464
16465     case ARRAY_TYPE:
16466       gen_array_type_die (type, context_die);
16467       break;
16468
16469     case VECTOR_TYPE:
16470       gen_array_type_die (type, context_die);
16471       break;
16472
16473     case ENUMERAL_TYPE:
16474     case RECORD_TYPE:
16475     case UNION_TYPE:
16476     case QUAL_UNION_TYPE:
16477       /* If this is a nested type whose containing class hasn't been written
16478          out yet, writing it out will cover this one, too.  This does not apply
16479          to instantiations of member class templates; they need to be added to
16480          the containing class as they are generated.  FIXME: This hurts the
16481          idea of combining type decls from multiple TUs, since we can't predict
16482          what set of template instantiations we'll get.  */
16483       if (TYPE_CONTEXT (type)
16484           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16485           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
16486         {
16487           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
16488
16489           if (TREE_ASM_WRITTEN (type))
16490             return;
16491
16492           /* If that failed, attach ourselves to the stub.  */
16493           push_decl_scope (TYPE_CONTEXT (type));
16494           context_die = lookup_type_die (TYPE_CONTEXT (type));
16495           need_pop = 1;
16496         }
16497       else if (TYPE_CONTEXT (type) != NULL_TREE
16498                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
16499         {
16500           /* If this type is local to a function that hasn't been written
16501              out yet, use a NULL context for now; it will be fixed up in
16502              decls_for_scope.  */
16503           context_die = lookup_decl_die (TYPE_CONTEXT (type));
16504           need_pop = 0;
16505         }
16506       else
16507         {
16508           context_die = declare_in_namespace (type, context_die);
16509           need_pop = 0;
16510         }
16511
16512       if (TREE_CODE (type) == ENUMERAL_TYPE)
16513         {
16514           /* This might have been written out by the call to
16515              declare_in_namespace.  */
16516           if (!TREE_ASM_WRITTEN (type))
16517             gen_enumeration_type_die (type, context_die);
16518         }
16519       else
16520         gen_struct_or_union_type_die (type, context_die, usage);
16521
16522       if (need_pop)
16523         pop_decl_scope ();
16524
16525       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
16526          it up if it is ever completed.  gen_*_type_die will set it for us
16527          when appropriate.  */
16528       return;
16529
16530     case VOID_TYPE:
16531     case INTEGER_TYPE:
16532     case REAL_TYPE:
16533     case FIXED_POINT_TYPE:
16534     case COMPLEX_TYPE:
16535     case BOOLEAN_TYPE:
16536       /* No DIEs needed for fundamental types.  */
16537       break;
16538
16539     case LANG_TYPE:
16540       /* No Dwarf representation currently defined.  */
16541       break;
16542
16543     default:
16544       gcc_unreachable ();
16545     }
16546
16547   TREE_ASM_WRITTEN (type) = 1;
16548 }
16549
16550 static void
16551 gen_type_die (tree type, dw_die_ref context_die)
16552 {
16553   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
16554 }
16555
16556 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
16557    things which are local to the given block.  */
16558
16559 static void
16560 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
16561 {
16562   int must_output_die = 0;
16563   bool inlined_func;
16564
16565   /* Ignore blocks that are NULL.  */
16566   if (stmt == NULL_TREE)
16567     return;
16568
16569   inlined_func = inlined_function_outer_scope_p (stmt);
16570
16571   /* If the block is one fragment of a non-contiguous block, do not
16572      process the variables, since they will have been done by the
16573      origin block.  Do process subblocks.  */
16574   if (BLOCK_FRAGMENT_ORIGIN (stmt))
16575     {
16576       tree sub;
16577
16578       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
16579         gen_block_die (sub, context_die, depth + 1);
16580
16581       return;
16582     }
16583
16584   /* Determine if we need to output any Dwarf DIEs at all to represent this
16585      block.  */
16586   if (inlined_func)
16587     /* The outer scopes for inlinings *must* always be represented.  We
16588        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
16589     must_output_die = 1;
16590   else
16591     {
16592       /* Determine if this block directly contains any "significant"
16593          local declarations which we will need to output DIEs for.  */
16594       if (debug_info_level > DINFO_LEVEL_TERSE)
16595         /* We are not in terse mode so *any* local declaration counts
16596            as being a "significant" one.  */
16597         must_output_die = ((BLOCK_VARS (stmt) != NULL
16598                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
16599                            && (TREE_USED (stmt)
16600                                || TREE_ASM_WRITTEN (stmt)
16601                                || BLOCK_ABSTRACT (stmt)));
16602       else if ((TREE_USED (stmt)
16603                 || TREE_ASM_WRITTEN (stmt)
16604                 || BLOCK_ABSTRACT (stmt))
16605                && !dwarf2out_ignore_block (stmt))
16606         must_output_die = 1;
16607     }
16608
16609   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
16610      DIE for any block which contains no significant local declarations at
16611      all.  Rather, in such cases we just call `decls_for_scope' so that any
16612      needed Dwarf info for any sub-blocks will get properly generated. Note
16613      that in terse mode, our definition of what constitutes a "significant"
16614      local declaration gets restricted to include only inlined function
16615      instances and local (nested) function definitions.  */
16616   if (must_output_die)
16617     {
16618       if (inlined_func)
16619         {
16620           /* If STMT block is abstract, that means we have been called
16621              indirectly from dwarf2out_abstract_function.
16622              That function rightfully marks the descendent blocks (of
16623              the abstract function it is dealing with) as being abstract,
16624              precisely to prevent us from emitting any
16625              DW_TAG_inlined_subroutine DIE as a descendent
16626              of an abstract function instance. So in that case, we should
16627              not call gen_inlined_subroutine_die.
16628
16629              Later though, when cgraph asks dwarf2out to emit info
16630              for the concrete instance of the function decl into which
16631              the concrete instance of STMT got inlined, the later will lead
16632              to the generation of a DW_TAG_inlined_subroutine DIE.  */
16633           if (! BLOCK_ABSTRACT (stmt))
16634             gen_inlined_subroutine_die (stmt, context_die, depth);
16635         }
16636       else
16637         gen_lexical_block_die (stmt, context_die, depth);
16638     }
16639   else
16640     decls_for_scope (stmt, context_die, depth);
16641 }
16642
16643 /* Process variable DECL (or variable with origin ORIGIN) within
16644    block STMT and add it to CONTEXT_DIE.  */
16645 static void
16646 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
16647 {
16648   dw_die_ref die;
16649   tree decl_or_origin = decl ? decl : origin;
16650   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
16651
16652   if (ultimate_origin)
16653     origin = ultimate_origin;
16654
16655   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
16656     die = lookup_decl_die (decl_or_origin);
16657   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
16658            && TYPE_DECL_IS_STUB (decl_or_origin))
16659     die = lookup_type_die (TREE_TYPE (decl_or_origin));
16660   else
16661     die = NULL;
16662
16663   if (die != NULL && die->die_parent == NULL)
16664     add_child_die (context_die, die);
16665   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
16666     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
16667                                          stmt, context_die);
16668   else
16669     gen_decl_die (decl, origin, context_die);
16670 }
16671
16672 /* Generate all of the decls declared within a given scope and (recursively)
16673    all of its sub-blocks.  */
16674
16675 static void
16676 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
16677 {
16678   tree decl;
16679   unsigned int i;
16680   tree subblocks;
16681
16682   /* Ignore NULL blocks.  */
16683   if (stmt == NULL_TREE)
16684     return;
16685
16686   /* Output the DIEs to represent all of the data objects and typedefs
16687      declared directly within this block but not within any nested
16688      sub-blocks.  Also, nested function and tag DIEs have been
16689      generated with a parent of NULL; fix that up now.  */
16690   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
16691     process_scope_var (stmt, decl, NULL_TREE, context_die);
16692   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
16693     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
16694                        context_die);
16695
16696   /* If we're at -g1, we're not interested in subblocks.  */
16697   if (debug_info_level <= DINFO_LEVEL_TERSE)
16698     return;
16699
16700   /* Output the DIEs to represent all sub-blocks (and the items declared
16701      therein) of this block.  */
16702   for (subblocks = BLOCK_SUBBLOCKS (stmt);
16703        subblocks != NULL;
16704        subblocks = BLOCK_CHAIN (subblocks))
16705     gen_block_die (subblocks, context_die, depth + 1);
16706 }
16707
16708 /* Is this a typedef we can avoid emitting?  */
16709
16710 static inline int
16711 is_redundant_typedef (const_tree decl)
16712 {
16713   if (TYPE_DECL_IS_STUB (decl))
16714     return 1;
16715
16716   if (DECL_ARTIFICIAL (decl)
16717       && DECL_CONTEXT (decl)
16718       && is_tagged_type (DECL_CONTEXT (decl))
16719       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
16720       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
16721     /* Also ignore the artificial member typedef for the class name.  */
16722     return 1;
16723
16724   return 0;
16725 }
16726
16727 /* Returns the DIE for a context.  */
16728
16729 static inline dw_die_ref
16730 get_context_die (tree context)
16731 {
16732   if (context)
16733     {
16734       /* Find die that represents this context.  */
16735       if (TYPE_P (context))
16736         return force_type_die (context);
16737       else
16738         return force_decl_die (context);
16739     }
16740   return comp_unit_die;
16741 }
16742
16743 /* Returns the DIE for decl.  A DIE will always be returned.  */
16744
16745 static dw_die_ref
16746 force_decl_die (tree decl)
16747 {
16748   dw_die_ref decl_die;
16749   unsigned saved_external_flag;
16750   tree save_fn = NULL_TREE;
16751   decl_die = lookup_decl_die (decl);
16752   if (!decl_die)
16753     {
16754       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
16755
16756       decl_die = lookup_decl_die (decl);
16757       if (decl_die)
16758         return decl_die;
16759
16760       switch (TREE_CODE (decl))
16761         {
16762         case FUNCTION_DECL:
16763           /* Clear current_function_decl, so that gen_subprogram_die thinks
16764              that this is a declaration. At this point, we just want to force
16765              declaration die.  */
16766           save_fn = current_function_decl;
16767           current_function_decl = NULL_TREE;
16768           gen_subprogram_die (decl, context_die);
16769           current_function_decl = save_fn;
16770           break;
16771
16772         case VAR_DECL:
16773           /* Set external flag to force declaration die. Restore it after
16774            gen_decl_die() call.  */
16775           saved_external_flag = DECL_EXTERNAL (decl);
16776           DECL_EXTERNAL (decl) = 1;
16777           gen_decl_die (decl, NULL, context_die);
16778           DECL_EXTERNAL (decl) = saved_external_flag;
16779           break;
16780
16781         case NAMESPACE_DECL:
16782           dwarf2out_decl (decl);
16783           break;
16784
16785         default:
16786           gcc_unreachable ();
16787         }
16788
16789       /* We should be able to find the DIE now.  */
16790       if (!decl_die)
16791         decl_die = lookup_decl_die (decl);
16792       gcc_assert (decl_die);
16793     }
16794
16795   return decl_die;
16796 }
16797
16798 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
16799    always returned.  */
16800
16801 static dw_die_ref
16802 force_type_die (tree type)
16803 {
16804   dw_die_ref type_die;
16805
16806   type_die = lookup_type_die (type);
16807   if (!type_die)
16808     {
16809       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
16810
16811       type_die = modified_type_die (type, TYPE_READONLY (type),
16812                                     TYPE_VOLATILE (type), context_die);
16813       gcc_assert (type_die);
16814     }
16815   return type_die;
16816 }
16817
16818 /* Force out any required namespaces to be able to output DECL,
16819    and return the new context_die for it, if it's changed.  */
16820
16821 static dw_die_ref
16822 setup_namespace_context (tree thing, dw_die_ref context_die)
16823 {
16824   tree context = (DECL_P (thing)
16825                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
16826   if (context && TREE_CODE (context) == NAMESPACE_DECL)
16827     /* Force out the namespace.  */
16828     context_die = force_decl_die (context);
16829
16830   return context_die;
16831 }
16832
16833 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
16834    type) within its namespace, if appropriate.
16835
16836    For compatibility with older debuggers, namespace DIEs only contain
16837    declarations; all definitions are emitted at CU scope.  */
16838
16839 static dw_die_ref
16840 declare_in_namespace (tree thing, dw_die_ref context_die)
16841 {
16842   dw_die_ref ns_context;
16843
16844   if (debug_info_level <= DINFO_LEVEL_TERSE)
16845     return context_die;
16846
16847   /* If this decl is from an inlined function, then don't try to emit it in its
16848      namespace, as we will get confused.  It would have already been emitted
16849      when the abstract instance of the inline function was emitted anyways.  */
16850   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
16851     return context_die;
16852
16853   ns_context = setup_namespace_context (thing, context_die);
16854
16855   if (ns_context != context_die)
16856     {
16857       if (is_fortran ())
16858         return ns_context;
16859       if (DECL_P (thing))
16860         gen_decl_die (thing, NULL, ns_context);
16861       else
16862         gen_type_die (thing, ns_context);
16863     }
16864   return context_die;
16865 }
16866
16867 /* Generate a DIE for a namespace or namespace alias.  */
16868
16869 static void
16870 gen_namespace_die (tree decl, dw_die_ref context_die)
16871 {
16872   dw_die_ref namespace_die;
16873
16874   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
16875      they are an alias of.  */
16876   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
16877     {
16878       /* Output a real namespace or module.  */
16879       context_die = setup_namespace_context (decl, comp_unit_die);
16880       namespace_die = new_die (is_fortran ()
16881                                ? DW_TAG_module : DW_TAG_namespace,
16882                                context_die, decl);
16883       /* For Fortran modules defined in different CU don't add src coords.  */
16884       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
16885         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
16886       else
16887         add_name_and_src_coords_attributes (namespace_die, decl);
16888       if (DECL_EXTERNAL (decl))
16889         add_AT_flag (namespace_die, DW_AT_declaration, 1);
16890       equate_decl_number_to_die (decl, namespace_die);
16891     }
16892   else
16893     {
16894       /* Output a namespace alias.  */
16895
16896       /* Force out the namespace we are an alias of, if necessary.  */
16897       dw_die_ref origin_die
16898         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
16899
16900       if (DECL_CONTEXT (decl) == NULL_TREE
16901           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
16902         context_die = setup_namespace_context (decl, comp_unit_die);
16903       /* Now create the namespace alias DIE.  */
16904       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
16905       add_name_and_src_coords_attributes (namespace_die, decl);
16906       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
16907       equate_decl_number_to_die (decl, namespace_die);
16908     }
16909 }
16910
16911 /* Generate Dwarf debug information for a decl described by DECL.  */
16912
16913 static void
16914 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
16915 {
16916   tree decl_or_origin = decl ? decl : origin;
16917   tree class_origin = NULL;
16918
16919   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
16920     return;
16921
16922   switch (TREE_CODE (decl_or_origin))
16923     {
16924     case ERROR_MARK:
16925       break;
16926
16927     case CONST_DECL:
16928       if (!is_fortran ())
16929         {
16930           /* The individual enumerators of an enum type get output when we output
16931              the Dwarf representation of the relevant enum type itself.  */
16932           break;
16933         }
16934
16935       /* Emit its type.  */
16936       gen_type_die (TREE_TYPE (decl), context_die);
16937
16938       /* And its containing namespace.  */
16939       context_die = declare_in_namespace (decl, context_die);
16940
16941       gen_const_die (decl, context_die);
16942       break;
16943
16944     case FUNCTION_DECL:
16945       /* Don't output any DIEs to represent mere function declarations,
16946          unless they are class members or explicit block externs.  */
16947       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
16948           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
16949           && (current_function_decl == NULL_TREE
16950               || DECL_ARTIFICIAL (decl_or_origin)))
16951         break;
16952
16953 #if 0
16954       /* FIXME */
16955       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
16956          on local redeclarations of global functions.  That seems broken.  */
16957       if (current_function_decl != decl)
16958         /* This is only a declaration.  */;
16959 #endif
16960
16961       /* If we're emitting a clone, emit info for the abstract instance.  */
16962       if (origin || DECL_ORIGIN (decl) != decl)
16963         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
16964
16965       /* If we're emitting an out-of-line copy of an inline function,
16966          emit info for the abstract instance and set up to refer to it.  */
16967       else if (cgraph_function_possibly_inlined_p (decl)
16968                && ! DECL_ABSTRACT (decl)
16969                && ! class_or_namespace_scope_p (context_die)
16970                /* dwarf2out_abstract_function won't emit a die if this is just
16971                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
16972                   that case, because that works only if we have a die.  */
16973                && DECL_INITIAL (decl) != NULL_TREE)
16974         {
16975           dwarf2out_abstract_function (decl);
16976           set_decl_origin_self (decl);
16977         }
16978
16979       /* Otherwise we're emitting the primary DIE for this decl.  */
16980       else if (debug_info_level > DINFO_LEVEL_TERSE)
16981         {
16982           /* Before we describe the FUNCTION_DECL itself, make sure that we
16983              have described its return type.  */
16984           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
16985
16986           /* And its virtual context.  */
16987           if (DECL_VINDEX (decl) != NULL_TREE)
16988             gen_type_die (DECL_CONTEXT (decl), context_die);
16989
16990           /* And its containing type.  */
16991           if (!origin)
16992             origin = decl_class_context (decl);
16993           if (origin != NULL_TREE)
16994             gen_type_die_for_member (origin, decl, context_die);
16995
16996           /* And its containing namespace.  */
16997           context_die = declare_in_namespace (decl, context_die);
16998         }
16999
17000       /* Now output a DIE to represent the function itself.  */
17001       if (decl)
17002         gen_subprogram_die (decl, context_die);
17003       break;
17004
17005     case TYPE_DECL:
17006       /* If we are in terse mode, don't generate any DIEs to represent any
17007          actual typedefs.  */
17008       if (debug_info_level <= DINFO_LEVEL_TERSE)
17009         break;
17010
17011       /* In the special case of a TYPE_DECL node representing the declaration
17012          of some type tag, if the given TYPE_DECL is marked as having been
17013          instantiated from some other (original) TYPE_DECL node (e.g. one which
17014          was generated within the original definition of an inline function) we
17015          used to generate a special (abbreviated) DW_TAG_structure_type,
17016          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
17017          should be actually referencing those DIEs, as variable DIEs with that
17018          type would be emitted already in the abstract origin, so it was always
17019          removed during unused type prunning.  Don't add anything in this
17020          case.  */
17021       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
17022         break;
17023
17024       if (is_redundant_typedef (decl))
17025         gen_type_die (TREE_TYPE (decl), context_die);
17026       else
17027         /* Output a DIE to represent the typedef itself.  */
17028         gen_typedef_die (decl, context_die);
17029       break;
17030
17031     case LABEL_DECL:
17032       if (debug_info_level >= DINFO_LEVEL_NORMAL)
17033         gen_label_die (decl, context_die);
17034       break;
17035
17036     case VAR_DECL:
17037     case RESULT_DECL:
17038       /* If we are in terse mode, don't generate any DIEs to represent any
17039          variable declarations or definitions.  */
17040       if (debug_info_level <= DINFO_LEVEL_TERSE)
17041         break;
17042
17043       /* Output any DIEs that are needed to specify the type of this data
17044          object.  */
17045       if ((TREE_CODE (decl_or_origin) == RESULT_DECL
17046            || TREE_CODE (decl_or_origin) == VAR_DECL)
17047           && DECL_BY_REFERENCE (decl_or_origin))
17048         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17049       else
17050         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17051
17052       /* And its containing type.  */
17053       class_origin = decl_class_context (decl_or_origin);
17054       if (class_origin != NULL_TREE)
17055         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
17056
17057       /* And its containing namespace.  */
17058       context_die = declare_in_namespace (decl_or_origin, context_die);
17059
17060       /* Now output the DIE to represent the data object itself.  This gets
17061          complicated because of the possibility that the VAR_DECL really
17062          represents an inlined instance of a formal parameter for an inline
17063          function.  */
17064       if (!origin)
17065         origin = decl_ultimate_origin (decl);
17066       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
17067         gen_formal_parameter_die (decl, origin, context_die);
17068       else
17069         gen_variable_die (decl, origin, context_die);
17070       break;
17071
17072     case FIELD_DECL:
17073       /* Ignore the nameless fields that are used to skip bits but handle C++
17074          anonymous unions and structs.  */
17075       if (DECL_NAME (decl) != NULL_TREE
17076           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
17077           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
17078         {
17079           gen_type_die (member_declared_type (decl), context_die);
17080           gen_field_die (decl, context_die);
17081         }
17082       break;
17083
17084     case PARM_DECL:
17085       if (DECL_BY_REFERENCE (decl_or_origin))
17086         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17087       else
17088         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17089       gen_formal_parameter_die (decl, origin, context_die);
17090       break;
17091
17092     case NAMESPACE_DECL:
17093     case IMPORTED_DECL:
17094       gen_namespace_die (decl, context_die);
17095       break;
17096
17097     default:
17098       /* Probably some frontend-internal decl.  Assume we don't care.  */
17099       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
17100       break;
17101     }
17102 }
17103 \f
17104 /* Output debug information for global decl DECL.  Called from toplev.c after
17105    compilation proper has finished.  */
17106
17107 static void
17108 dwarf2out_global_decl (tree decl)
17109 {
17110   /* Output DWARF2 information for file-scope tentative data object
17111      declarations, file-scope (extern) function declarations (which
17112      had no corresponding body) and file-scope tagged type declarations
17113      and definitions which have not yet been forced out.  */
17114   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
17115     dwarf2out_decl (decl);
17116 }
17117
17118 /* Output debug information for type decl DECL.  Called from toplev.c
17119    and from language front ends (to record built-in types).  */
17120 static void
17121 dwarf2out_type_decl (tree decl, int local)
17122 {
17123   if (!local)
17124     dwarf2out_decl (decl);
17125 }
17126
17127 /* Output debug information for imported module or decl DECL.
17128    NAME is non-NULL name in the lexical block if the decl has been renamed.
17129    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
17130    that DECL belongs to.
17131    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
17132 static void
17133 dwarf2out_imported_module_or_decl_1 (tree decl,
17134                                      tree name,
17135                                      tree lexical_block,
17136                                      dw_die_ref lexical_block_die)
17137 {
17138   expanded_location xloc;
17139   dw_die_ref imported_die = NULL;
17140   dw_die_ref at_import_die;
17141
17142   if (TREE_CODE (decl) == IMPORTED_DECL)
17143     {
17144       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
17145       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
17146       gcc_assert (decl);
17147     }
17148   else
17149     xloc = expand_location (input_location);
17150
17151   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
17152     {
17153       if (is_base_type (TREE_TYPE (decl)))
17154         at_import_die = base_type_die (TREE_TYPE (decl));
17155       else
17156         at_import_die = force_type_die (TREE_TYPE (decl));
17157       /* For namespace N { typedef void T; } using N::T; base_type_die
17158          returns NULL, but DW_TAG_imported_declaration requires
17159          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
17160       if (!at_import_die)
17161         {
17162           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
17163           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
17164           at_import_die = lookup_type_die (TREE_TYPE (decl));
17165           gcc_assert (at_import_die);
17166         }
17167     }
17168   else
17169     {
17170       at_import_die = lookup_decl_die (decl);
17171       if (!at_import_die)
17172         {
17173           /* If we're trying to avoid duplicate debug info, we may not have
17174              emitted the member decl for this field.  Emit it now.  */
17175           if (TREE_CODE (decl) == FIELD_DECL)
17176             {
17177               tree type = DECL_CONTEXT (decl);
17178
17179               if (TYPE_CONTEXT (type)
17180                   && TYPE_P (TYPE_CONTEXT (type))
17181                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
17182                                                 DINFO_USAGE_DIR_USE))
17183                 return;
17184               gen_type_die_for_member (type, decl,
17185                                        get_context_die (TYPE_CONTEXT (type)));
17186             }
17187           at_import_die = force_decl_die (decl);
17188         }
17189     }
17190
17191   if (TREE_CODE (decl) == NAMESPACE_DECL)
17192     imported_die = new_die (DW_TAG_imported_module,
17193                             lexical_block_die,
17194                             lexical_block);
17195   else
17196     imported_die = new_die (DW_TAG_imported_declaration,
17197                             lexical_block_die,
17198                             lexical_block);
17199
17200   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
17201   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
17202   if (name)
17203     add_AT_string (imported_die, DW_AT_name,
17204                    IDENTIFIER_POINTER (name));
17205   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
17206 }
17207
17208 /* Output debug information for imported module or decl DECL.
17209    NAME is non-NULL name in context if the decl has been renamed.
17210    CHILD is true if decl is one of the renamed decls as part of
17211    importing whole module.  */
17212
17213 static void
17214 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
17215                                    bool child)
17216 {
17217   /* dw_die_ref at_import_die;  */
17218   dw_die_ref scope_die;
17219
17220   if (debug_info_level <= DINFO_LEVEL_TERSE)
17221     return;
17222
17223   gcc_assert (decl);
17224
17225   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
17226      We need decl DIE for reference and scope die. First, get DIE for the decl
17227      itself.  */
17228
17229   /* Get the scope die for decl context. Use comp_unit_die for global module
17230      or decl. If die is not found for non globals, force new die.  */
17231   if (context
17232       && TYPE_P (context)
17233       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
17234     return;
17235   scope_die = get_context_die (context);
17236
17237   if (child)
17238     {
17239       gcc_assert (scope_die->die_child);
17240       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
17241       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
17242       scope_die = scope_die->die_child;
17243     }
17244
17245   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
17246   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
17247
17248 }
17249
17250 /* Write the debugging output for DECL.  */
17251
17252 void
17253 dwarf2out_decl (tree decl)
17254 {
17255   dw_die_ref context_die = comp_unit_die;
17256
17257   switch (TREE_CODE (decl))
17258     {
17259     case ERROR_MARK:
17260       return;
17261
17262     case FUNCTION_DECL:
17263       /* What we would really like to do here is to filter out all mere
17264          file-scope declarations of file-scope functions which are never
17265          referenced later within this translation unit (and keep all of ones
17266          that *are* referenced later on) but we aren't clairvoyant, so we have
17267          no idea which functions will be referenced in the future (i.e. later
17268          on within the current translation unit). So here we just ignore all
17269          file-scope function declarations which are not also definitions.  If
17270          and when the debugger needs to know something about these functions,
17271          it will have to hunt around and find the DWARF information associated
17272          with the definition of the function.
17273
17274          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
17275          nodes represent definitions and which ones represent mere
17276          declarations.  We have to check DECL_INITIAL instead. That's because
17277          the C front-end supports some weird semantics for "extern inline"
17278          function definitions.  These can get inlined within the current
17279          translation unit (and thus, we need to generate Dwarf info for their
17280          abstract instances so that the Dwarf info for the concrete inlined
17281          instances can have something to refer to) but the compiler never
17282          generates any out-of-lines instances of such things (despite the fact
17283          that they *are* definitions).
17284
17285          The important point is that the C front-end marks these "extern
17286          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
17287          them anyway. Note that the C++ front-end also plays some similar games
17288          for inline function definitions appearing within include files which
17289          also contain `#pragma interface' pragmas.  */
17290       if (DECL_INITIAL (decl) == NULL_TREE)
17291         return;
17292
17293       /* If we're a nested function, initially use a parent of NULL; if we're
17294          a plain function, this will be fixed up in decls_for_scope.  If
17295          we're a method, it will be ignored, since we already have a DIE.  */
17296       if (decl_function_context (decl)
17297           /* But if we're in terse mode, we don't care about scope.  */
17298           && debug_info_level > DINFO_LEVEL_TERSE)
17299         context_die = NULL;
17300       break;
17301
17302     case VAR_DECL:
17303       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
17304          declaration and if the declaration was never even referenced from
17305          within this entire compilation unit.  We suppress these DIEs in
17306          order to save space in the .debug section (by eliminating entries
17307          which are probably useless).  Note that we must not suppress
17308          block-local extern declarations (whether used or not) because that
17309          would screw-up the debugger's name lookup mechanism and cause it to
17310          miss things which really ought to be in scope at a given point.  */
17311       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
17312         return;
17313
17314       /* For local statics lookup proper context die.  */
17315       if (TREE_STATIC (decl) && decl_function_context (decl))
17316         context_die = lookup_decl_die (DECL_CONTEXT (decl));
17317
17318       /* If we are in terse mode, don't generate any DIEs to represent any
17319          variable declarations or definitions.  */
17320       if (debug_info_level <= DINFO_LEVEL_TERSE)
17321         return;
17322       break;
17323
17324     case CONST_DECL:
17325       if (debug_info_level <= DINFO_LEVEL_TERSE)
17326         return;
17327       if (!is_fortran ())
17328         return;
17329       if (TREE_STATIC (decl) && decl_function_context (decl))
17330         context_die = lookup_decl_die (DECL_CONTEXT (decl));
17331       break;
17332
17333     case NAMESPACE_DECL:
17334     case IMPORTED_DECL:
17335       if (debug_info_level <= DINFO_LEVEL_TERSE)
17336         return;
17337       if (lookup_decl_die (decl) != NULL)
17338         return;
17339       break;
17340
17341     case TYPE_DECL:
17342       /* Don't emit stubs for types unless they are needed by other DIEs.  */
17343       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
17344         return;
17345
17346       /* Don't bother trying to generate any DIEs to represent any of the
17347          normal built-in types for the language we are compiling.  */
17348       if (DECL_IS_BUILTIN (decl))
17349         {
17350           /* OK, we need to generate one for `bool' so GDB knows what type
17351              comparisons have.  */
17352           if (is_cxx ()
17353               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
17354               && ! DECL_IGNORED_P (decl))
17355             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
17356
17357           return;
17358         }
17359
17360       /* If we are in terse mode, don't generate any DIEs for types.  */
17361       if (debug_info_level <= DINFO_LEVEL_TERSE)
17362         return;
17363
17364       /* If we're a function-scope tag, initially use a parent of NULL;
17365          this will be fixed up in decls_for_scope.  */
17366       if (decl_function_context (decl))
17367         context_die = NULL;
17368
17369       break;
17370
17371     default:
17372       return;
17373     }
17374
17375   gen_decl_die (decl, NULL, context_die);
17376 }
17377
17378 /* Output a marker (i.e. a label) for the beginning of the generated code for
17379    a lexical block.  */
17380
17381 static void
17382 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
17383                        unsigned int blocknum)
17384 {
17385   switch_to_section (current_function_section ());
17386   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
17387 }
17388
17389 /* Output a marker (i.e. a label) for the end of the generated code for a
17390    lexical block.  */
17391
17392 static void
17393 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
17394 {
17395   switch_to_section (current_function_section ());
17396   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
17397 }
17398
17399 /* Returns nonzero if it is appropriate not to emit any debugging
17400    information for BLOCK, because it doesn't contain any instructions.
17401
17402    Don't allow this for blocks with nested functions or local classes
17403    as we would end up with orphans, and in the presence of scheduling
17404    we may end up calling them anyway.  */
17405
17406 static bool
17407 dwarf2out_ignore_block (const_tree block)
17408 {
17409   tree decl;
17410   unsigned int i;
17411
17412   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
17413     if (TREE_CODE (decl) == FUNCTION_DECL
17414         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
17415       return 0;
17416   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
17417     {
17418       decl = BLOCK_NONLOCALIZED_VAR (block, i);
17419       if (TREE_CODE (decl) == FUNCTION_DECL
17420           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
17421       return 0;
17422     }
17423
17424   return 1;
17425 }
17426
17427 /* Hash table routines for file_hash.  */
17428
17429 static int
17430 file_table_eq (const void *p1_p, const void *p2_p)
17431 {
17432   const struct dwarf_file_data *const p1 =
17433     (const struct dwarf_file_data *) p1_p;
17434   const char *const p2 = (const char *) p2_p;
17435   return strcmp (p1->filename, p2) == 0;
17436 }
17437
17438 static hashval_t
17439 file_table_hash (const void *p_p)
17440 {
17441   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
17442   return htab_hash_string (p->filename);
17443 }
17444
17445 /* Lookup FILE_NAME (in the list of filenames that we know about here in
17446    dwarf2out.c) and return its "index".  The index of each (known) filename is
17447    just a unique number which is associated with only that one filename.  We
17448    need such numbers for the sake of generating labels (in the .debug_sfnames
17449    section) and references to those files numbers (in the .debug_srcinfo
17450    and.debug_macinfo sections).  If the filename given as an argument is not
17451    found in our current list, add it to the list and assign it the next
17452    available unique index number.  In order to speed up searches, we remember
17453    the index of the filename was looked up last.  This handles the majority of
17454    all searches.  */
17455
17456 static struct dwarf_file_data *
17457 lookup_filename (const char *file_name)
17458 {
17459   void ** slot;
17460   struct dwarf_file_data * created;
17461
17462   /* Check to see if the file name that was searched on the previous
17463      call matches this file name.  If so, return the index.  */
17464   if (file_table_last_lookup
17465       && (file_name == file_table_last_lookup->filename
17466           || strcmp (file_table_last_lookup->filename, file_name) == 0))
17467     return file_table_last_lookup;
17468
17469   /* Didn't match the previous lookup, search the table.  */
17470   slot = htab_find_slot_with_hash (file_table, file_name,
17471                                    htab_hash_string (file_name), INSERT);
17472   if (*slot)
17473     return (struct dwarf_file_data *) *slot;
17474
17475   created = GGC_NEW (struct dwarf_file_data);
17476   created->filename = file_name;
17477   created->emitted_number = 0;
17478   *slot = created;
17479   return created;
17480 }
17481
17482 /* If the assembler will construct the file table, then translate the compiler
17483    internal file table number into the assembler file table number, and emit
17484    a .file directive if we haven't already emitted one yet.  The file table
17485    numbers are different because we prune debug info for unused variables and
17486    types, which may include filenames.  */
17487
17488 static int
17489 maybe_emit_file (struct dwarf_file_data * fd)
17490 {
17491   if (! fd->emitted_number)
17492     {
17493       if (last_emitted_file)
17494         fd->emitted_number = last_emitted_file->emitted_number + 1;
17495       else
17496         fd->emitted_number = 1;
17497       last_emitted_file = fd;
17498
17499       if (DWARF2_ASM_LINE_DEBUG_INFO)
17500         {
17501           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
17502           output_quoted_string (asm_out_file,
17503                                 remap_debug_filename (fd->filename));
17504           fputc ('\n', asm_out_file);
17505         }
17506     }
17507
17508   return fd->emitted_number;
17509 }
17510
17511 /* Schedule generation of a DW_AT_const_value attribute to DIE.
17512    That generation should happen after function debug info has been
17513    generated. The value of the attribute is the constant value of ARG.  */
17514
17515 static void
17516 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
17517 {
17518   die_arg_entry entry;
17519
17520   if (!die || !arg)
17521     return;
17522
17523   if (!tmpl_value_parm_die_table)
17524     tmpl_value_parm_die_table
17525       = VEC_alloc (die_arg_entry, gc, 32);
17526
17527   entry.die = die;
17528   entry.arg = arg;
17529   VEC_safe_push (die_arg_entry, gc,
17530                  tmpl_value_parm_die_table,
17531                  &entry);
17532 }
17533
17534 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
17535    by append_entry_to_tmpl_value_parm_die_table. This function must
17536    be called after function DIEs have been generated.  */
17537
17538 static void
17539 gen_remaining_tmpl_value_param_die_attribute (void)
17540 {
17541   if (tmpl_value_parm_die_table)
17542     {
17543       unsigned i;
17544       die_arg_entry *e;
17545
17546       for (i = 0;
17547            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
17548            i++)
17549         tree_add_const_value_attribute (e->die, e->arg);
17550     }
17551 }
17552
17553
17554 /* Replace DW_AT_name for the decl with name.  */
17555  
17556 static void
17557 dwarf2out_set_name (tree decl, tree name)
17558 {
17559   dw_die_ref die;
17560   dw_attr_ref attr;
17561
17562   die = TYPE_SYMTAB_DIE (decl);
17563   if (!die)
17564     return;
17565
17566   attr = get_AT (die, DW_AT_name);
17567   if (attr)
17568     {
17569       struct indirect_string_node *node;
17570
17571       node = find_AT_string (dwarf2_name (name, 0));
17572       /* replace the string.  */
17573       attr->dw_attr_val.v.val_str = node;
17574     }
17575
17576   else
17577     add_name_attribute (die, dwarf2_name (name, 0));
17578 }
17579
17580 /* Called by the final INSN scan whenever we see a var location.  We
17581    use it to drop labels in the right places, and throw the location in
17582    our lookup table.  */
17583
17584 static void
17585 dwarf2out_var_location (rtx loc_note)
17586 {
17587   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
17588   struct var_loc_node *newloc;
17589   rtx next_real;
17590   static const char *last_label;
17591   static const char *last_postcall_label;
17592   static bool last_in_cold_section_p;
17593   tree decl;
17594
17595   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
17596     return;
17597
17598   next_real = next_real_insn (loc_note);
17599   /* If there are no instructions which would be affected by this note,
17600      don't do anything.  */
17601   if (next_real == NULL_RTX)
17602     return;
17603
17604   newloc = GGC_CNEW (struct var_loc_node);
17605   /* If there were no real insns between note we processed last time
17606      and this note, use the label we emitted last time.  */
17607   if (last_var_location_insn == NULL_RTX
17608       || last_var_location_insn != next_real
17609       || last_in_cold_section_p != in_cold_section_p)
17610     {
17611       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
17612       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
17613       loclabel_num++;
17614       last_label = ggc_strdup (loclabel);
17615       if (!NOTE_DURING_CALL_P (loc_note))
17616         last_postcall_label = NULL;
17617     }
17618   newloc->var_loc_note = loc_note;
17619   newloc->next = NULL;
17620
17621   if (!NOTE_DURING_CALL_P (loc_note))
17622     newloc->label = last_label;
17623   else
17624     {
17625       if (!last_postcall_label)
17626         {
17627           sprintf (loclabel, "%s-1", last_label);
17628           last_postcall_label = ggc_strdup (loclabel);
17629         }
17630       newloc->label = last_postcall_label;
17631     }
17632
17633   if (cfun && in_cold_section_p)
17634     newloc->section_label = crtl->subsections.cold_section_label;
17635   else
17636     newloc->section_label = text_section_label;
17637
17638   last_var_location_insn = next_real;
17639   last_in_cold_section_p = in_cold_section_p;
17640   decl = NOTE_VAR_LOCATION_DECL (loc_note);
17641   add_var_loc_to_decl (decl, newloc);
17642 }
17643
17644 /* We need to reset the locations at the beginning of each
17645    function. We can't do this in the end_function hook, because the
17646    declarations that use the locations won't have been output when
17647    that hook is called.  Also compute have_multiple_function_sections here.  */
17648
17649 static void
17650 dwarf2out_begin_function (tree fun)
17651 {
17652   htab_empty (decl_loc_table);
17653
17654   if (function_section (fun) != text_section)
17655     have_multiple_function_sections = true;
17656
17657   dwarf2out_note_section_used ();
17658 }
17659
17660 /* Output a label to mark the beginning of a source code line entry
17661    and record information relating to this source line, in
17662    'line_info_table' for later output of the .debug_line section.  */
17663
17664 static void
17665 dwarf2out_source_line (unsigned int line, const char *filename,
17666                        int discriminator, bool is_stmt)
17667 {
17668   static bool last_is_stmt = true;
17669
17670   if (debug_info_level >= DINFO_LEVEL_NORMAL
17671       && line != 0)
17672     {
17673       int file_num = maybe_emit_file (lookup_filename (filename));
17674
17675       switch_to_section (current_function_section ());
17676
17677       /* If requested, emit something human-readable.  */
17678       if (flag_debug_asm)
17679         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
17680                  filename, line);
17681
17682       if (DWARF2_ASM_LINE_DEBUG_INFO)
17683         {
17684           /* Emit the .loc directive understood by GNU as.  */
17685           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
17686           if (is_stmt != last_is_stmt)
17687             {
17688               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
17689               last_is_stmt = is_stmt;
17690             }
17691           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
17692             fprintf (asm_out_file, " discriminator %d", discriminator);
17693           fputc ('\n', asm_out_file);
17694
17695           /* Indicate that line number info exists.  */
17696           line_info_table_in_use++;
17697         }
17698       else if (function_section (current_function_decl) != text_section)
17699         {
17700           dw_separate_line_info_ref line_info;
17701           targetm.asm_out.internal_label (asm_out_file,
17702                                           SEPARATE_LINE_CODE_LABEL,
17703                                           separate_line_info_table_in_use);
17704
17705           /* Expand the line info table if necessary.  */
17706           if (separate_line_info_table_in_use
17707               == separate_line_info_table_allocated)
17708             {
17709               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
17710               separate_line_info_table
17711                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
17712                                  separate_line_info_table,
17713                                  separate_line_info_table_allocated);
17714               memset (separate_line_info_table
17715                        + separate_line_info_table_in_use,
17716                       0,
17717                       (LINE_INFO_TABLE_INCREMENT
17718                        * sizeof (dw_separate_line_info_entry)));
17719             }
17720
17721           /* Add the new entry at the end of the line_info_table.  */
17722           line_info
17723             = &separate_line_info_table[separate_line_info_table_in_use++];
17724           line_info->dw_file_num = file_num;
17725           line_info->dw_line_num = line;
17726           line_info->function = current_function_funcdef_no;
17727         }
17728       else
17729         {
17730           dw_line_info_ref line_info;
17731
17732           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
17733                                      line_info_table_in_use);
17734
17735           /* Expand the line info table if necessary.  */
17736           if (line_info_table_in_use == line_info_table_allocated)
17737             {
17738               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
17739               line_info_table
17740                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
17741                                  line_info_table_allocated);
17742               memset (line_info_table + line_info_table_in_use, 0,
17743                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
17744             }
17745
17746           /* Add the new entry at the end of the line_info_table.  */
17747           line_info = &line_info_table[line_info_table_in_use++];
17748           line_info->dw_file_num = file_num;
17749           line_info->dw_line_num = line;
17750         }
17751     }
17752 }
17753
17754 /* Record the beginning of a new source file.  */
17755
17756 static void
17757 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
17758 {
17759   if (flag_eliminate_dwarf2_dups)
17760     {
17761       /* Record the beginning of the file for break_out_includes.  */
17762       dw_die_ref bincl_die;
17763
17764       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
17765       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
17766     }
17767
17768   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17769     {
17770       int file_num = maybe_emit_file (lookup_filename (filename));
17771
17772       switch_to_section (debug_macinfo_section);
17773       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
17774       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
17775                                    lineno);
17776
17777       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
17778     }
17779 }
17780
17781 /* Record the end of a source file.  */
17782
17783 static void
17784 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
17785 {
17786   if (flag_eliminate_dwarf2_dups)
17787     /* Record the end of the file for break_out_includes.  */
17788     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
17789
17790   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17791     {
17792       switch_to_section (debug_macinfo_section);
17793       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
17794     }
17795 }
17796
17797 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
17798    the tail part of the directive line, i.e. the part which is past the
17799    initial whitespace, #, whitespace, directive-name, whitespace part.  */
17800
17801 static void
17802 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
17803                   const char *buffer ATTRIBUTE_UNUSED)
17804 {
17805   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17806     {
17807       switch_to_section (debug_macinfo_section);
17808       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
17809       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
17810       dw2_asm_output_nstring (buffer, -1, "The macro");
17811     }
17812 }
17813
17814 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
17815    the tail part of the directive line, i.e. the part which is past the
17816    initial whitespace, #, whitespace, directive-name, whitespace part.  */
17817
17818 static void
17819 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
17820                  const char *buffer ATTRIBUTE_UNUSED)
17821 {
17822   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17823     {
17824       switch_to_section (debug_macinfo_section);
17825       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
17826       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
17827       dw2_asm_output_nstring (buffer, -1, "The macro");
17828     }
17829 }
17830
17831 /* Set up for Dwarf output at the start of compilation.  */
17832
17833 static void
17834 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
17835 {
17836   /* Allocate the file_table.  */
17837   file_table = htab_create_ggc (50, file_table_hash,
17838                                 file_table_eq, NULL);
17839
17840   /* Allocate the decl_die_table.  */
17841   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
17842                                     decl_die_table_eq, NULL);
17843
17844   /* Allocate the decl_loc_table.  */
17845   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
17846                                     decl_loc_table_eq, NULL);
17847
17848   /* Allocate the initial hunk of the decl_scope_table.  */
17849   decl_scope_table = VEC_alloc (tree, gc, 256);
17850
17851   /* Allocate the initial hunk of the abbrev_die_table.  */
17852   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
17853   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
17854   /* Zero-th entry is allocated, but unused.  */
17855   abbrev_die_table_in_use = 1;
17856
17857   /* Allocate the initial hunk of the line_info_table.  */
17858   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
17859   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
17860
17861   /* Zero-th entry is allocated, but unused.  */
17862   line_info_table_in_use = 1;
17863
17864   /* Allocate the pubtypes and pubnames vectors.  */
17865   pubname_table = VEC_alloc (pubname_entry, gc, 32);
17866   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
17867
17868   /* Generate the initial DIE for the .debug section.  Note that the (string)
17869      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
17870      will (typically) be a relative pathname and that this pathname should be
17871      taken as being relative to the directory from which the compiler was
17872      invoked when the given (base) source file was compiled.  We will fill
17873      in this value in dwarf2out_finish.  */
17874   comp_unit_die = gen_compile_unit_die (NULL);
17875
17876   incomplete_types = VEC_alloc (tree, gc, 64);
17877
17878   used_rtx_array = VEC_alloc (rtx, gc, 32);
17879
17880   debug_info_section = get_section (DEBUG_INFO_SECTION,
17881                                     SECTION_DEBUG, NULL);
17882   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
17883                                       SECTION_DEBUG, NULL);
17884   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
17885                                        SECTION_DEBUG, NULL);
17886   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
17887                                        SECTION_DEBUG, NULL);
17888   debug_line_section = get_section (DEBUG_LINE_SECTION,
17889                                     SECTION_DEBUG, NULL);
17890   debug_loc_section = get_section (DEBUG_LOC_SECTION,
17891                                    SECTION_DEBUG, NULL);
17892   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
17893                                         SECTION_DEBUG, NULL);
17894   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
17895                                         SECTION_DEBUG, NULL);
17896   debug_str_section = get_section (DEBUG_STR_SECTION,
17897                                    DEBUG_STR_SECTION_FLAGS, NULL);
17898   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
17899                                       SECTION_DEBUG, NULL);
17900   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
17901                                      SECTION_DEBUG, NULL);
17902
17903   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
17904   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
17905                                DEBUG_ABBREV_SECTION_LABEL, 0);
17906   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
17907   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
17908                                COLD_TEXT_SECTION_LABEL, 0);
17909   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
17910
17911   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
17912                                DEBUG_INFO_SECTION_LABEL, 0);
17913   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
17914                                DEBUG_LINE_SECTION_LABEL, 0);
17915   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
17916                                DEBUG_RANGES_SECTION_LABEL, 0);
17917   switch_to_section (debug_abbrev_section);
17918   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
17919   switch_to_section (debug_info_section);
17920   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
17921   switch_to_section (debug_line_section);
17922   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
17923
17924   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
17925     {
17926       switch_to_section (debug_macinfo_section);
17927       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
17928                                    DEBUG_MACINFO_SECTION_LABEL, 0);
17929       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
17930     }
17931
17932   switch_to_section (text_section);
17933   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
17934   if (flag_reorder_blocks_and_partition)
17935     {
17936       cold_text_section = unlikely_text_section ();
17937       switch_to_section (cold_text_section);
17938       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
17939     }
17940 }
17941
17942 /* A helper function for dwarf2out_finish called through
17943    htab_traverse.  Emit one queued .debug_str string.  */
17944
17945 static int
17946 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
17947 {
17948   struct indirect_string_node *node = (struct indirect_string_node *) *h;
17949
17950   if (node->label && node->refcount)
17951     {
17952       switch_to_section (debug_str_section);
17953       ASM_OUTPUT_LABEL (asm_out_file, node->label);
17954       assemble_string (node->str, strlen (node->str) + 1);
17955     }
17956
17957   return 1;
17958 }
17959
17960 #if ENABLE_ASSERT_CHECKING
17961 /* Verify that all marks are clear.  */
17962
17963 static void
17964 verify_marks_clear (dw_die_ref die)
17965 {
17966   dw_die_ref c;
17967
17968   gcc_assert (! die->die_mark);
17969   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
17970 }
17971 #endif /* ENABLE_ASSERT_CHECKING */
17972
17973 /* Clear the marks for a die and its children.
17974    Be cool if the mark isn't set.  */
17975
17976 static void
17977 prune_unmark_dies (dw_die_ref die)
17978 {
17979   dw_die_ref c;
17980
17981   if (die->die_mark)
17982     die->die_mark = 0;
17983   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
17984 }
17985
17986 /* Given DIE that we're marking as used, find any other dies
17987    it references as attributes and mark them as used.  */
17988
17989 static void
17990 prune_unused_types_walk_attribs (dw_die_ref die)
17991 {
17992   dw_attr_ref a;
17993   unsigned ix;
17994
17995   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
17996     {
17997       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
17998         {
17999           /* A reference to another DIE.
18000              Make sure that it will get emitted.  */
18001           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
18002         }
18003       /* Set the string's refcount to 0 so that prune_unused_types_mark
18004          accounts properly for it.  */
18005       if (AT_class (a) == dw_val_class_str)
18006         a->dw_attr_val.v.val_str->refcount = 0;
18007     }
18008 }
18009
18010
18011 /* Mark DIE as being used.  If DOKIDS is true, then walk down
18012    to DIE's children.  */
18013
18014 static void
18015 prune_unused_types_mark (dw_die_ref die, int dokids)
18016 {
18017   dw_die_ref c;
18018
18019   if (die->die_mark == 0)
18020     {
18021       /* We haven't done this node yet.  Mark it as used.  */
18022       die->die_mark = 1;
18023
18024       /* We also have to mark its parents as used.
18025          (But we don't want to mark our parents' kids due to this.)  */
18026       if (die->die_parent)
18027         prune_unused_types_mark (die->die_parent, 0);
18028
18029       /* Mark any referenced nodes.  */
18030       prune_unused_types_walk_attribs (die);
18031
18032       /* If this node is a specification,
18033          also mark the definition, if it exists.  */
18034       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
18035         prune_unused_types_mark (die->die_definition, 1);
18036     }
18037
18038   if (dokids && die->die_mark != 2)
18039     {
18040       /* We need to walk the children, but haven't done so yet.
18041          Remember that we've walked the kids.  */
18042       die->die_mark = 2;
18043
18044       /* If this is an array type, we need to make sure our
18045          kids get marked, even if they're types.  */
18046       if (die->die_tag == DW_TAG_array_type)
18047         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
18048       else
18049         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18050     }
18051 }
18052
18053 /* For local classes, look if any static member functions were emitted
18054    and if so, mark them.  */
18055
18056 static void
18057 prune_unused_types_walk_local_classes (dw_die_ref die)
18058 {
18059   dw_die_ref c;
18060
18061   if (die->die_mark == 2)
18062     return;
18063
18064   switch (die->die_tag)
18065     {
18066     case DW_TAG_structure_type:
18067     case DW_TAG_union_type:
18068     case DW_TAG_class_type:
18069       break;
18070
18071     case DW_TAG_subprogram:
18072       if (!get_AT_flag (die, DW_AT_declaration)
18073           || die->die_definition != NULL)
18074         prune_unused_types_mark (die, 1);
18075       return;
18076
18077     default:
18078       return;
18079     }
18080
18081   /* Mark children.  */
18082   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
18083 }
18084
18085 /* Walk the tree DIE and mark types that we actually use.  */
18086
18087 static void
18088 prune_unused_types_walk (dw_die_ref die)
18089 {
18090   dw_die_ref c;
18091
18092   /* Don't do anything if this node is already marked and
18093      children have been marked as well.  */
18094   if (die->die_mark == 2)
18095     return;
18096
18097   switch (die->die_tag)
18098     {
18099     case DW_TAG_structure_type:
18100     case DW_TAG_union_type:
18101     case DW_TAG_class_type:
18102       if (die->die_perennial_p)
18103         break;
18104
18105       for (c = die->die_parent; c; c = c->die_parent)
18106         if (c->die_tag == DW_TAG_subprogram)
18107           break;
18108
18109       /* Finding used static member functions inside of classes
18110          is needed just for local classes, because for other classes
18111          static member function DIEs with DW_AT_specification
18112          are emitted outside of the DW_TAG_*_type.  If we ever change
18113          it, we'd need to call this even for non-local classes.  */
18114       if (c)
18115         prune_unused_types_walk_local_classes (die);
18116
18117       /* It's a type node --- don't mark it.  */
18118       return;
18119
18120     case DW_TAG_const_type:
18121     case DW_TAG_packed_type:
18122     case DW_TAG_pointer_type:
18123     case DW_TAG_reference_type:
18124     case DW_TAG_volatile_type:
18125     case DW_TAG_typedef:
18126     case DW_TAG_array_type:
18127     case DW_TAG_interface_type:
18128     case DW_TAG_friend:
18129     case DW_TAG_variant_part:
18130     case DW_TAG_enumeration_type:
18131     case DW_TAG_subroutine_type:
18132     case DW_TAG_string_type:
18133     case DW_TAG_set_type:
18134     case DW_TAG_subrange_type:
18135     case DW_TAG_ptr_to_member_type:
18136     case DW_TAG_file_type:
18137       if (die->die_perennial_p)
18138         break;
18139
18140       /* It's a type node --- don't mark it.  */
18141       return;
18142
18143     default:
18144       /* Mark everything else.  */
18145       break;
18146   }
18147
18148   if (die->die_mark == 0)
18149     {
18150       die->die_mark = 1;
18151
18152       /* Now, mark any dies referenced from here.  */
18153       prune_unused_types_walk_attribs (die);
18154     }
18155
18156   die->die_mark = 2;
18157
18158   /* Mark children.  */
18159   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18160 }
18161
18162 /* Increment the string counts on strings referred to from DIE's
18163    attributes.  */
18164
18165 static void
18166 prune_unused_types_update_strings (dw_die_ref die)
18167 {
18168   dw_attr_ref a;
18169   unsigned ix;
18170
18171   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18172     if (AT_class (a) == dw_val_class_str)
18173       {
18174         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
18175         s->refcount++;
18176         /* Avoid unnecessarily putting strings that are used less than
18177            twice in the hash table.  */
18178         if (s->refcount
18179             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
18180           {
18181             void ** slot;
18182             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
18183                                              htab_hash_string (s->str),
18184                                              INSERT);
18185             gcc_assert (*slot == NULL);
18186             *slot = s;
18187           }
18188       }
18189 }
18190
18191 /* Remove from the tree DIE any dies that aren't marked.  */
18192
18193 static void
18194 prune_unused_types_prune (dw_die_ref die)
18195 {
18196   dw_die_ref c;
18197
18198   gcc_assert (die->die_mark);
18199   prune_unused_types_update_strings (die);
18200
18201   if (! die->die_child)
18202     return;
18203
18204   c = die->die_child;
18205   do {
18206     dw_die_ref prev = c;
18207     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
18208       if (c == die->die_child)
18209         {
18210           /* No marked children between 'prev' and the end of the list.  */
18211           if (prev == c)
18212             /* No marked children at all.  */
18213             die->die_child = NULL;
18214           else
18215             {
18216               prev->die_sib = c->die_sib;
18217               die->die_child = prev;
18218             }
18219           return;
18220         }
18221
18222     if (c != prev->die_sib)
18223       prev->die_sib = c;
18224     prune_unused_types_prune (c);
18225   } while (c != die->die_child);
18226 }
18227
18228 /* A helper function for dwarf2out_finish called through
18229    htab_traverse.  Clear .debug_str strings that we haven't already
18230    decided to emit.  */
18231
18232 static int
18233 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18234 {
18235   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18236
18237   if (!node->label || !node->refcount)
18238     htab_clear_slot (debug_str_hash, h);
18239
18240   return 1;
18241 }
18242
18243 /* Remove dies representing declarations that we never use.  */
18244
18245 static void
18246 prune_unused_types (void)
18247 {
18248   unsigned int i;
18249   limbo_die_node *node;
18250   pubname_ref pub;
18251
18252 #if ENABLE_ASSERT_CHECKING
18253   /* All the marks should already be clear.  */
18254   verify_marks_clear (comp_unit_die);
18255   for (node = limbo_die_list; node; node = node->next)
18256     verify_marks_clear (node->die);
18257 #endif /* ENABLE_ASSERT_CHECKING */
18258
18259   /* Set the mark on nodes that are actually used.  */
18260   prune_unused_types_walk (comp_unit_die);
18261   for (node = limbo_die_list; node; node = node->next)
18262     prune_unused_types_walk (node->die);
18263
18264   /* Also set the mark on nodes referenced from the
18265      pubname_table or arange_table.  */
18266   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
18267     prune_unused_types_mark (pub->die, 1);
18268   for (i = 0; i < arange_table_in_use; i++)
18269     prune_unused_types_mark (arange_table[i], 1);
18270
18271   /* Get rid of nodes that aren't marked; and update the string counts.  */
18272   if (debug_str_hash && debug_str_hash_forced)
18273     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
18274   else if (debug_str_hash)
18275     htab_empty (debug_str_hash);
18276   prune_unused_types_prune (comp_unit_die);
18277   for (node = limbo_die_list; node; node = node->next)
18278     prune_unused_types_prune (node->die);
18279
18280   /* Leave the marks clear.  */
18281   prune_unmark_dies (comp_unit_die);
18282   for (node = limbo_die_list; node; node = node->next)
18283     prune_unmark_dies (node->die);
18284 }
18285
18286 /* Set the parameter to true if there are any relative pathnames in
18287    the file table.  */
18288 static int
18289 file_table_relative_p (void ** slot, void *param)
18290 {
18291   bool *p = (bool *) param;
18292   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
18293   if (!IS_ABSOLUTE_PATH (d->filename))
18294     {
18295       *p = true;
18296       return 0;
18297     }
18298   return 1;
18299 }
18300
18301 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
18302    to the location it would have been added, should we know its
18303    DECL_ASSEMBLER_NAME when we added other attributes.  This will
18304    probably improve compactness of debug info, removing equivalent
18305    abbrevs, and hide any differences caused by deferring the
18306    computation of the assembler name, triggered by e.g. PCH.  */
18307
18308 static inline void
18309 move_linkage_attr (dw_die_ref die)
18310 {
18311   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
18312   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
18313
18314   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
18315
18316   while (--ix > 0)
18317     {
18318       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
18319
18320       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
18321         break;
18322     }
18323
18324   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
18325     {
18326       VEC_pop (dw_attr_node, die->die_attr);
18327       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
18328     }
18329 }
18330
18331 /* Output stuff that dwarf requires at the end of every file,
18332    and generate the DWARF-2 debugging info.  */
18333
18334 static void
18335 dwarf2out_finish (const char *filename)
18336 {
18337   limbo_die_node *node, *next_node;
18338   dw_die_ref die = 0;
18339   unsigned int i;
18340
18341   gen_remaining_tmpl_value_param_die_attribute ();
18342
18343   /* Add the name for the main input file now.  We delayed this from
18344      dwarf2out_init to avoid complications with PCH.  */
18345   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
18346   if (!IS_ABSOLUTE_PATH (filename))
18347     add_comp_dir_attribute (comp_unit_die);
18348   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
18349     {
18350       bool p = false;
18351       htab_traverse (file_table, file_table_relative_p, &p);
18352       if (p)
18353         add_comp_dir_attribute (comp_unit_die);
18354     }
18355
18356   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
18357     {
18358       add_location_or_const_value_attribute (
18359         VEC_index (deferred_locations, deferred_locations_list, i)->die,
18360         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
18361         DW_AT_location);
18362     }
18363
18364   /* Traverse the limbo die list, and add parent/child links.  The only
18365      dies without parents that should be here are concrete instances of
18366      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
18367      For concrete instances, we can get the parent die from the abstract
18368      instance.  */
18369   for (node = limbo_die_list; node; node = next_node)
18370     {
18371       next_node = node->next;
18372       die = node->die;
18373
18374       if (die->die_parent == NULL)
18375         {
18376           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
18377
18378           if (origin)
18379             add_child_die (origin->die_parent, die);
18380           else if (die == comp_unit_die)
18381             ;
18382           else if (errorcount > 0 || sorrycount > 0)
18383             /* It's OK to be confused by errors in the input.  */
18384             add_child_die (comp_unit_die, die);
18385           else
18386             {
18387               /* In certain situations, the lexical block containing a
18388                  nested function can be optimized away, which results
18389                  in the nested function die being orphaned.  Likewise
18390                  with the return type of that nested function.  Force
18391                  this to be a child of the containing function.
18392
18393                  It may happen that even the containing function got fully
18394                  inlined and optimized out.  In that case we are lost and
18395                  assign the empty child.  This should not be big issue as
18396                  the function is likely unreachable too.  */
18397               tree context = NULL_TREE;
18398
18399               gcc_assert (node->created_for);
18400
18401               if (DECL_P (node->created_for))
18402                 context = DECL_CONTEXT (node->created_for);
18403               else if (TYPE_P (node->created_for))
18404                 context = TYPE_CONTEXT (node->created_for);
18405
18406               gcc_assert (context
18407                           && (TREE_CODE (context) == FUNCTION_DECL
18408                               || TREE_CODE (context) == NAMESPACE_DECL));
18409
18410               origin = lookup_decl_die (context);
18411               if (origin)
18412                 add_child_die (origin, die);
18413               else
18414                 add_child_die (comp_unit_die, die);
18415             }
18416         }
18417     }
18418
18419   limbo_die_list = NULL;
18420
18421   for (node = deferred_asm_name; node; node = node->next)
18422     {
18423       tree decl = node->created_for;
18424       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
18425         {
18426           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
18427                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
18428           move_linkage_attr (node->die);
18429         }
18430     }
18431
18432   deferred_asm_name = NULL;
18433
18434   /* Walk through the list of incomplete types again, trying once more to
18435      emit full debugging info for them.  */
18436   retry_incomplete_types ();
18437
18438   if (flag_eliminate_unused_debug_types)
18439     prune_unused_types ();
18440
18441   /* Generate separate CUs for each of the include files we've seen.
18442      They will go into limbo_die_list.  */
18443   if (flag_eliminate_dwarf2_dups)
18444     break_out_includes (comp_unit_die);
18445
18446   /* Traverse the DIE's and add add sibling attributes to those DIE's
18447      that have children.  */
18448   add_sibling_attributes (comp_unit_die);
18449   for (node = limbo_die_list; node; node = node->next)
18450     add_sibling_attributes (node->die);
18451
18452   /* Output a terminator label for the .text section.  */
18453   switch_to_section (text_section);
18454   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
18455   if (flag_reorder_blocks_and_partition)
18456     {
18457       switch_to_section (unlikely_text_section ());
18458       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
18459     }
18460
18461   /* We can only use the low/high_pc attributes if all of the code was
18462      in .text.  */
18463   if (!have_multiple_function_sections)
18464     {
18465       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
18466       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
18467     }
18468
18469   else
18470     {
18471       unsigned fde_idx = 0;
18472
18473       /* We need to give .debug_loc and .debug_ranges an appropriate
18474          "base address".  Use zero so that these addresses become
18475          absolute.  Historically, we've emitted the unexpected
18476          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
18477          Emit both to give time for other tools to adapt.  */
18478       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
18479       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
18480
18481       add_AT_range_list (comp_unit_die, DW_AT_ranges,
18482                          add_ranges_by_labels (text_section_label,
18483                                                text_end_label));
18484       if (flag_reorder_blocks_and_partition)
18485         add_ranges_by_labels (cold_text_section_label,
18486                               cold_end_label);
18487
18488       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
18489         {
18490           dw_fde_ref fde = &fde_table[fde_idx];
18491
18492           if (fde->dw_fde_switched_sections)
18493             {
18494               if (!fde->in_std_section)
18495                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
18496                                       fde->dw_fde_hot_section_end_label);
18497               if (!fde->cold_in_std_section)
18498                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
18499                                       fde->dw_fde_unlikely_section_end_label);
18500             }
18501           else if (!fde->in_std_section)
18502             add_ranges_by_labels (fde->dw_fde_begin,
18503                                   fde->dw_fde_end);
18504         }
18505
18506       add_ranges (NULL);
18507     }
18508
18509   /* Output location list section if necessary.  */
18510   if (have_location_lists)
18511     {
18512       /* Output the location lists info.  */
18513       switch_to_section (debug_loc_section);
18514       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
18515                                    DEBUG_LOC_SECTION_LABEL, 0);
18516       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
18517       output_location_lists (die);
18518     }
18519
18520   if (debug_info_level >= DINFO_LEVEL_NORMAL)
18521     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
18522                     debug_line_section_label);
18523
18524   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18525     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
18526
18527   /* Output all of the compilation units.  We put the main one last so that
18528      the offsets are available to output_pubnames.  */
18529   for (node = limbo_die_list; node; node = node->next)
18530     output_comp_unit (node->die, 0);
18531
18532   /* Output the main compilation unit if non-empty or if .debug_macinfo
18533      has been emitted.  */
18534   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
18535
18536   /* Output the abbreviation table.  */
18537   switch_to_section (debug_abbrev_section);
18538   output_abbrev_section ();
18539
18540   /* Output public names table if necessary.  */
18541   if (!VEC_empty (pubname_entry, pubname_table))
18542     {
18543       switch_to_section (debug_pubnames_section);
18544       output_pubnames (pubname_table);
18545     }
18546
18547   /* Output public types table if necessary.  */
18548   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
18549      It shouldn't hurt to emit it always, since pure DWARF2 consumers
18550      simply won't look for the section.  */
18551   if (!VEC_empty (pubname_entry, pubtype_table))
18552     {
18553       switch_to_section (debug_pubtypes_section);
18554       output_pubnames (pubtype_table);
18555     }
18556
18557   /* Output the address range information.  We only put functions in the arange
18558      table, so don't write it out if we don't have any.  */
18559   if (fde_table_in_use)
18560     {
18561       switch_to_section (debug_aranges_section);
18562       output_aranges ();
18563     }
18564
18565   /* Output ranges section if necessary.  */
18566   if (ranges_table_in_use)
18567     {
18568       switch_to_section (debug_ranges_section);
18569       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
18570       output_ranges ();
18571     }
18572
18573   /* Output the source line correspondence table.  We must do this
18574      even if there is no line information.  Otherwise, on an empty
18575      translation unit, we will generate a present, but empty,
18576      .debug_info section.  IRIX 6.5 `nm' will then complain when
18577      examining the file.  This is done late so that any filenames
18578      used by the debug_info section are marked as 'used'.  */
18579   if (! DWARF2_ASM_LINE_DEBUG_INFO)
18580     {
18581       switch_to_section (debug_line_section);
18582       output_line_info ();
18583     }
18584
18585   /* Have to end the macro section.  */
18586   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18587     {
18588       switch_to_section (debug_macinfo_section);
18589       dw2_asm_output_data (1, 0, "End compilation unit");
18590     }
18591
18592   /* If we emitted any DW_FORM_strp form attribute, output the string
18593      table too.  */
18594   if (debug_str_hash)
18595     htab_traverse (debug_str_hash, output_indirect_string, NULL);
18596 }
18597 #else
18598
18599 /* This should never be used, but its address is needed for comparisons.  */
18600 const struct gcc_debug_hooks dwarf2_debug_hooks =
18601 {
18602   0,            /* init */
18603   0,            /* finish */
18604   0,            /* define */
18605   0,            /* undef */
18606   0,            /* start_source_file */
18607   0,            /* end_source_file */
18608   0,            /* begin_block */
18609   0,            /* end_block */
18610   0,            /* ignore_block */
18611   0,            /* source_line */
18612   0,            /* begin_prologue */
18613   0,            /* end_prologue */
18614   0,            /* end_epilogue */
18615   0,            /* begin_function */
18616   0,            /* end_function */
18617   0,            /* function_decl */
18618   0,            /* global_decl */
18619   0,            /* type_decl */
18620   0,            /* imported_module_or_decl */
18621   0,            /* deferred_inline_function */
18622   0,            /* outlining_inline_function */
18623   0,            /* label */
18624   0,            /* handle_pch */
18625   0,            /* var_location */
18626   0,            /* switch_text_section */
18627   0,            /* set_name */
18628   0             /* start_end_main_source_file */
18629 };
18630
18631 #endif /* DWARF2_DEBUGGING_INFO */
18632
18633 #include "gt-dwarf2out.h"