OSDN Git Service

3a0e4f39af1e8425f8bf33abd8f8cccf199ca656
[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 #include "gimple.h"
93 #include "tree-pass.h"
94
95 #ifdef DWARF2_DEBUGGING_INFO
96 static void dwarf2out_source_line (unsigned int, const char *, int, bool);
97
98 static rtx last_var_location_insn;
99 #endif
100
101 #ifdef VMS_DEBUGGING_INFO
102 int vms_file_stats_name (const char *, long long *, long *, char *, int *);
103
104 /* Define this macro to be a nonzero value if the directory specifications
105     which are output in the debug info should end with a separator.  */
106 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 1
107 /* Define this macro to evaluate to a nonzero value if GCC should refrain
108    from generating indirect strings in DWARF2 debug information, for instance
109    if your target is stuck with an old version of GDB that is unable to
110    process them properly or uses VMS Debug.  */
111 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 1
112 #else
113 #define DWARF2_DIR_SHOULD_END_WITH_SEPARATOR 0
114 #define DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET 0
115 #endif
116
117 #ifndef DWARF2_FRAME_INFO
118 # ifdef DWARF2_DEBUGGING_INFO
119 #  define DWARF2_FRAME_INFO \
120   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
121 # else
122 #  define DWARF2_FRAME_INFO 0
123 # endif
124 #endif
125
126 /* Map register numbers held in the call frame info that gcc has
127    collected using DWARF_FRAME_REGNUM to those that should be output in
128    .debug_frame and .eh_frame.  */
129 #ifndef DWARF2_FRAME_REG_OUT
130 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
131 #endif
132
133 /* Save the result of dwarf2out_do_frame across PCH.  */
134 static GTY(()) bool saved_do_cfi_asm = 0;
135
136 /* Decide whether we want to emit frame unwind information for the current
137    translation unit.  */
138
139 int
140 dwarf2out_do_frame (void)
141 {
142   /* We want to emit correct CFA location expressions or lists, so we
143      have to return true if we're going to output debug info, even if
144      we're not going to output frame or unwind info.  */
145   return (write_symbols == DWARF2_DEBUG
146           || write_symbols == VMS_AND_DWARF2_DEBUG
147           || DWARF2_FRAME_INFO || saved_do_cfi_asm
148 #ifdef DWARF2_UNWIND_INFO
149           || (DWARF2_UNWIND_INFO
150               && (flag_unwind_tables
151                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
152 #endif
153           );
154 }
155
156 /* Decide whether to emit frame unwind via assembler directives.  */
157
158 int
159 dwarf2out_do_cfi_asm (void)
160 {
161   int enc;
162
163 #ifdef MIPS_DEBUGGING_INFO
164   return false;
165 #endif
166   if (!flag_dwarf2_cfi_asm || !dwarf2out_do_frame ())
167     return false;
168   if (saved_do_cfi_asm)
169     return true;
170   if (!HAVE_GAS_CFI_PERSONALITY_DIRECTIVE)
171     return false;
172
173   /* Make sure the personality encoding is one the assembler can support.
174      In particular, aligned addresses can't be handled.  */
175   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,/*global=*/1);
176   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
177     return false;
178   enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,/*global=*/0);
179   if ((enc & 0x70) != 0 && (enc & 0x70) != DW_EH_PE_pcrel)
180     return false;
181
182   saved_do_cfi_asm = true;
183   return true;
184 }
185
186 /* The size of the target's pointer type.  */
187 #ifndef PTR_SIZE
188 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
189 #endif
190
191 /* Array of RTXes referenced by the debugging information, which therefore
192    must be kept around forever.  */
193 static GTY(()) VEC(rtx,gc) *used_rtx_array;
194
195 /* A pointer to the base of a list of incomplete types which might be
196    completed at some later time.  incomplete_types_list needs to be a
197    VEC(tree,gc) because we want to tell the garbage collector about
198    it.  */
199 static GTY(()) VEC(tree,gc) *incomplete_types;
200
201 /* A pointer to the base of a table of references to declaration
202    scopes.  This table is a display which tracks the nesting
203    of declaration scopes at the current scope and containing
204    scopes.  This table is used to find the proper place to
205    define type declaration DIE's.  */
206 static GTY(()) VEC(tree,gc) *decl_scope_table;
207
208 /* Pointers to various DWARF2 sections.  */
209 static GTY(()) section *debug_info_section;
210 static GTY(()) section *debug_abbrev_section;
211 static GTY(()) section *debug_aranges_section;
212 static GTY(()) section *debug_macinfo_section;
213 static GTY(()) section *debug_line_section;
214 static GTY(()) section *debug_loc_section;
215 static GTY(()) section *debug_pubnames_section;
216 static GTY(()) section *debug_pubtypes_section;
217 static GTY(()) section *debug_str_section;
218 static GTY(()) section *debug_ranges_section;
219 static GTY(()) section *debug_frame_section;
220
221 /* Personality decl of current unit.  Used only when assembler does not support
222    personality CFI.  */
223 static GTY(()) rtx current_unit_personality;
224
225 /* How to start an assembler comment.  */
226 #ifndef ASM_COMMENT_START
227 #define ASM_COMMENT_START ";#"
228 #endif
229
230 typedef struct dw_cfi_struct *dw_cfi_ref;
231 typedef struct dw_fde_struct *dw_fde_ref;
232 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
233
234 /* Call frames are described using a sequence of Call Frame
235    Information instructions.  The register number, offset
236    and address fields are provided as possible operands;
237    their use is selected by the opcode field.  */
238
239 enum dw_cfi_oprnd_type {
240   dw_cfi_oprnd_unused,
241   dw_cfi_oprnd_reg_num,
242   dw_cfi_oprnd_offset,
243   dw_cfi_oprnd_addr,
244   dw_cfi_oprnd_loc
245 };
246
247 typedef union GTY(()) dw_cfi_oprnd_struct {
248   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
249   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
250   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
251   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
252 }
253 dw_cfi_oprnd;
254
255 typedef struct GTY(()) dw_cfi_struct {
256   dw_cfi_ref dw_cfi_next;
257   enum dwarf_call_frame_info dw_cfi_opc;
258   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
259     dw_cfi_oprnd1;
260   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
261     dw_cfi_oprnd2;
262 }
263 dw_cfi_node;
264
265 /* This is how we define the location of the CFA. We use to handle it
266    as REG + OFFSET all the time,  but now it can be more complex.
267    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
268    Instead of passing around REG and OFFSET, we pass a copy
269    of this structure.  */
270 typedef struct GTY(()) cfa_loc {
271   HOST_WIDE_INT offset;
272   HOST_WIDE_INT base_offset;
273   unsigned int reg;
274   BOOL_BITFIELD indirect : 1;  /* 1 if CFA is accessed via a dereference.  */
275   BOOL_BITFIELD in_use : 1;    /* 1 if a saved cfa is stored here.  */
276 } dw_cfa_location;
277
278 /* All call frame descriptions (FDE's) in the GCC generated DWARF
279    refer to a single Common Information Entry (CIE), defined at
280    the beginning of the .debug_frame section.  This use of a single
281    CIE obviates the need to keep track of multiple CIE's
282    in the DWARF generation routines below.  */
283
284 typedef struct GTY(()) dw_fde_struct {
285   tree decl;
286   const char *dw_fde_begin;
287   const char *dw_fde_current_label;
288   const char *dw_fde_end;
289   const char *dw_fde_hot_section_label;
290   const char *dw_fde_hot_section_end_label;
291   const char *dw_fde_unlikely_section_label;
292   const char *dw_fde_unlikely_section_end_label;
293   dw_cfi_ref dw_fde_cfi;
294   dw_cfi_ref dw_fde_switch_cfi; /* Last CFI before switching sections.  */
295   unsigned funcdef_number;
296   HOST_WIDE_INT stack_realignment;
297   /* Dynamic realign argument pointer register.  */
298   unsigned int drap_reg;
299   /* Virtual dynamic realign argument pointer register.  */
300   unsigned int vdrap_reg;
301   unsigned all_throwers_are_sibcalls : 1;
302   unsigned nothrow : 1;
303   unsigned uses_eh_lsda : 1;
304   /* Whether we did stack realign in this call frame.  */
305   unsigned stack_realign : 1;
306   /* Whether dynamic realign argument pointer register has been saved.  */
307   unsigned drap_reg_saved: 1;
308   /* True iff dw_fde_begin label is in text_section or cold_text_section.  */
309   unsigned in_std_section : 1;
310   /* True iff dw_fde_unlikely_section_label is in text_section or
311      cold_text_section.  */
312   unsigned cold_in_std_section : 1;
313   /* True iff switched sections.  */
314   unsigned dw_fde_switched_sections : 1;
315   /* True iff switching from cold to hot section.  */
316   unsigned dw_fde_switched_cold_to_hot : 1;
317 }
318 dw_fde_node;
319
320 /* Maximum size (in bytes) of an artificially generated label.  */
321 #define MAX_ARTIFICIAL_LABEL_BYTES      30
322
323 /* The size of addresses as they appear in the Dwarf 2 data.
324    Some architectures use word addresses to refer to code locations,
325    but Dwarf 2 info always uses byte addresses.  On such machines,
326    Dwarf 2 addresses need to be larger than the architecture's
327    pointers.  */
328 #ifndef DWARF2_ADDR_SIZE
329 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
330 #endif
331
332 /* The size in bytes of a DWARF field indicating an offset or length
333    relative to a debug info section, specified to be 4 bytes in the
334    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
335    as PTR_SIZE.  */
336
337 #ifndef DWARF_OFFSET_SIZE
338 #define DWARF_OFFSET_SIZE 4
339 #endif
340
341 /* According to the (draft) DWARF 3 specification, the initial length
342    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
343    bytes are 0xffffffff, followed by the length stored in the next 8
344    bytes.
345
346    However, the SGI/MIPS ABI uses an initial length which is equal to
347    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
348
349 #ifndef DWARF_INITIAL_LENGTH_SIZE
350 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
351 #endif
352
353 /* Round SIZE up to the nearest BOUNDARY.  */
354 #define DWARF_ROUND(SIZE,BOUNDARY) \
355   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
356
357 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
358 #ifndef DWARF_CIE_DATA_ALIGNMENT
359 #ifdef STACK_GROWS_DOWNWARD
360 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
361 #else
362 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
363 #endif
364 #endif
365
366 /* CIE identifier.  */
367 #if HOST_BITS_PER_WIDE_INT >= 64
368 #define DWARF_CIE_ID \
369   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
370 #else
371 #define DWARF_CIE_ID DW_CIE_ID
372 #endif
373
374 /* A pointer to the base of a table that contains frame description
375    information for each routine.  */
376 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
377
378 /* Number of elements currently allocated for fde_table.  */
379 static GTY(()) unsigned fde_table_allocated;
380
381 /* Number of elements in fde_table currently in use.  */
382 static GTY(()) unsigned fde_table_in_use;
383
384 /* Size (in elements) of increments by which we may expand the
385    fde_table.  */
386 #define FDE_TABLE_INCREMENT 256
387
388 /* Get the current fde_table entry we should use.  */
389
390 static inline dw_fde_ref
391 current_fde (void)
392 {
393   return fde_table_in_use ? &fde_table[fde_table_in_use - 1] : NULL;
394 }
395
396 /* A list of call frame insns for the CIE.  */
397 static GTY(()) dw_cfi_ref cie_cfi_head;
398
399 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
400 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
401    attribute that accelerates the lookup of the FDE associated
402    with the subprogram.  This variable holds the table index of the FDE
403    associated with the current function (body) definition.  */
404 static unsigned current_funcdef_fde;
405 #endif
406
407 struct GTY(()) indirect_string_node {
408   const char *str;
409   unsigned int refcount;
410   enum dwarf_form form;
411   char *label;
412 };
413
414 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
415
416 /* True if the compilation unit has location entries that reference
417    debug strings.  */
418 static GTY(()) bool debug_str_hash_forced = false;
419
420 static GTY(()) int dw2_string_counter;
421 static GTY(()) unsigned long dwarf2out_cfi_label_num;
422
423 /* True if the compilation unit places functions in more than one section.  */
424 static GTY(()) bool have_multiple_function_sections = false;
425
426 /* Whether the default text and cold text sections have been used at all.  */
427
428 static GTY(()) bool text_section_used = false;
429 static GTY(()) bool cold_text_section_used = false;
430
431 /* The default cold text section.  */
432 static GTY(()) section *cold_text_section;
433
434 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
435
436 /* Forward declarations for functions defined in this file.  */
437
438 static char *stripattributes (const char *);
439 static const char *dwarf_cfi_name (unsigned);
440 static dw_cfi_ref new_cfi (void);
441 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
442 static void add_fde_cfi (const char *, dw_cfi_ref);
443 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *, dw_cfa_location *);
444 static void lookup_cfa (dw_cfa_location *);
445 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
446 #ifdef DWARF2_UNWIND_INFO
447 static void initial_return_save (rtx);
448 #endif
449 static HOST_WIDE_INT stack_adjust_offset (const_rtx, HOST_WIDE_INT,
450                                           HOST_WIDE_INT);
451 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
452 static void output_cfi_directive (dw_cfi_ref);
453 static void output_call_frame_info (int);
454 static void dwarf2out_note_section_used (void);
455 static void dwarf2out_stack_adjust (rtx, bool);
456 static void dwarf2out_args_size_adjust (HOST_WIDE_INT, const char *);
457 static void flush_queued_reg_saves (void);
458 static bool clobbers_queued_reg_save (const_rtx);
459 static void dwarf2out_frame_debug_expr (rtx, const char *);
460
461 /* Support for complex CFA locations.  */
462 static void output_cfa_loc (dw_cfi_ref);
463 static void output_cfa_loc_raw (dw_cfi_ref);
464 static void get_cfa_from_loc_descr (dw_cfa_location *,
465                                     struct dw_loc_descr_struct *);
466 static struct dw_loc_descr_struct *build_cfa_loc
467   (dw_cfa_location *, HOST_WIDE_INT);
468 static struct dw_loc_descr_struct *build_cfa_aligned_loc
469   (HOST_WIDE_INT, HOST_WIDE_INT);
470 static void def_cfa_1 (const char *, dw_cfa_location *);
471
472 /* How to start an assembler comment.  */
473 #ifndef ASM_COMMENT_START
474 #define ASM_COMMENT_START ";#"
475 #endif
476
477 /* Data and reference forms for relocatable data.  */
478 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
479 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
480
481 #ifndef DEBUG_FRAME_SECTION
482 #define DEBUG_FRAME_SECTION     ".debug_frame"
483 #endif
484
485 #ifndef FUNC_BEGIN_LABEL
486 #define FUNC_BEGIN_LABEL        "LFB"
487 #endif
488
489 #ifndef FUNC_END_LABEL
490 #define FUNC_END_LABEL          "LFE"
491 #endif
492
493 #ifndef FRAME_BEGIN_LABEL
494 #define FRAME_BEGIN_LABEL       "Lframe"
495 #endif
496 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
497 #define CIE_END_LABEL           "LECIE"
498 #define FDE_LABEL               "LSFDE"
499 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
500 #define FDE_END_LABEL           "LEFDE"
501 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
502 #define LINE_NUMBER_END_LABEL   "LELT"
503 #define LN_PROLOG_AS_LABEL      "LASLTP"
504 #define LN_PROLOG_END_LABEL     "LELTP"
505 #define DIE_LABEL_PREFIX        "DW"
506
507 /* The DWARF 2 CFA column which tracks the return address.  Normally this
508    is the column for PC, or the first column after all of the hard
509    registers.  */
510 #ifndef DWARF_FRAME_RETURN_COLUMN
511 #ifdef PC_REGNUM
512 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
513 #else
514 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
515 #endif
516 #endif
517
518 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
519    default, we just provide columns for all registers.  */
520 #ifndef DWARF_FRAME_REGNUM
521 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
522 #endif
523 \f
524 /* Hook used by __throw.  */
525
526 rtx
527 expand_builtin_dwarf_sp_column (void)
528 {
529   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
530   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
531 }
532
533 /* Return a pointer to a copy of the section string name S with all
534    attributes stripped off, and an asterisk prepended (for assemble_name).  */
535
536 static inline char *
537 stripattributes (const char *s)
538 {
539   char *stripped = XNEWVEC (char, strlen (s) + 2);
540   char *p = stripped;
541
542   *p++ = '*';
543
544   while (*s && *s != ',')
545     *p++ = *s++;
546
547   *p = '\0';
548   return stripped;
549 }
550
551 /* MEM is a memory reference for the register size table, each element of
552    which has mode MODE.  Initialize column C as a return address column.  */
553
554 static void
555 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
556 {
557   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
558   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
559   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
560 }
561
562 /* Divide OFF by DWARF_CIE_DATA_ALIGNMENT, asserting no remainder.  */
563
564 static inline HOST_WIDE_INT
565 div_data_align (HOST_WIDE_INT off)
566 {
567   HOST_WIDE_INT r = off / DWARF_CIE_DATA_ALIGNMENT;
568   gcc_assert (r * DWARF_CIE_DATA_ALIGNMENT == off);
569   return r;
570 }
571
572 /* Return true if we need a signed version of a given opcode
573    (e.g. DW_CFA_offset_extended_sf vs DW_CFA_offset_extended).  */
574
575 static inline bool
576 need_data_align_sf_opcode (HOST_WIDE_INT off)
577 {
578   return DWARF_CIE_DATA_ALIGNMENT < 0 ? off > 0 : off < 0;
579 }
580
581 /* Generate code to initialize the register size table.  */
582
583 void
584 expand_builtin_init_dwarf_reg_sizes (tree address)
585 {
586   unsigned int i;
587   enum machine_mode mode = TYPE_MODE (char_type_node);
588   rtx addr = expand_normal (address);
589   rtx mem = gen_rtx_MEM (BLKmode, addr);
590   bool wrote_return_column = false;
591
592   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
593     {
594       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
595
596       if (rnum < DWARF_FRAME_REGISTERS)
597         {
598           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
599           enum machine_mode save_mode = reg_raw_mode[i];
600           HOST_WIDE_INT size;
601
602           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
603             save_mode = choose_hard_reg_mode (i, 1, true);
604           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
605             {
606               if (save_mode == VOIDmode)
607                 continue;
608               wrote_return_column = true;
609             }
610           size = GET_MODE_SIZE (save_mode);
611           if (offset < 0)
612             continue;
613
614           emit_move_insn (adjust_address (mem, mode, offset),
615                           gen_int_mode (size, mode));
616         }
617     }
618
619   if (!wrote_return_column)
620     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
621
622 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
623   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
624 #endif
625
626   targetm.init_dwarf_reg_sizes_extra (address);
627 }
628
629 /* Convert a DWARF call frame info. operation to its string name */
630
631 static const char *
632 dwarf_cfi_name (unsigned int cfi_opc)
633 {
634   switch (cfi_opc)
635     {
636     case DW_CFA_advance_loc:
637       return "DW_CFA_advance_loc";
638     case DW_CFA_offset:
639       return "DW_CFA_offset";
640     case DW_CFA_restore:
641       return "DW_CFA_restore";
642     case DW_CFA_nop:
643       return "DW_CFA_nop";
644     case DW_CFA_set_loc:
645       return "DW_CFA_set_loc";
646     case DW_CFA_advance_loc1:
647       return "DW_CFA_advance_loc1";
648     case DW_CFA_advance_loc2:
649       return "DW_CFA_advance_loc2";
650     case DW_CFA_advance_loc4:
651       return "DW_CFA_advance_loc4";
652     case DW_CFA_offset_extended:
653       return "DW_CFA_offset_extended";
654     case DW_CFA_restore_extended:
655       return "DW_CFA_restore_extended";
656     case DW_CFA_undefined:
657       return "DW_CFA_undefined";
658     case DW_CFA_same_value:
659       return "DW_CFA_same_value";
660     case DW_CFA_register:
661       return "DW_CFA_register";
662     case DW_CFA_remember_state:
663       return "DW_CFA_remember_state";
664     case DW_CFA_restore_state:
665       return "DW_CFA_restore_state";
666     case DW_CFA_def_cfa:
667       return "DW_CFA_def_cfa";
668     case DW_CFA_def_cfa_register:
669       return "DW_CFA_def_cfa_register";
670     case DW_CFA_def_cfa_offset:
671       return "DW_CFA_def_cfa_offset";
672
673     /* DWARF 3 */
674     case DW_CFA_def_cfa_expression:
675       return "DW_CFA_def_cfa_expression";
676     case DW_CFA_expression:
677       return "DW_CFA_expression";
678     case DW_CFA_offset_extended_sf:
679       return "DW_CFA_offset_extended_sf";
680     case DW_CFA_def_cfa_sf:
681       return "DW_CFA_def_cfa_sf";
682     case DW_CFA_def_cfa_offset_sf:
683       return "DW_CFA_def_cfa_offset_sf";
684
685     /* SGI/MIPS specific */
686     case DW_CFA_MIPS_advance_loc8:
687       return "DW_CFA_MIPS_advance_loc8";
688
689     /* GNU extensions */
690     case DW_CFA_GNU_window_save:
691       return "DW_CFA_GNU_window_save";
692     case DW_CFA_GNU_args_size:
693       return "DW_CFA_GNU_args_size";
694     case DW_CFA_GNU_negative_offset_extended:
695       return "DW_CFA_GNU_negative_offset_extended";
696
697     default:
698       return "DW_CFA_<unknown>";
699     }
700 }
701
702 /* Return a pointer to a newly allocated Call Frame Instruction.  */
703
704 static inline dw_cfi_ref
705 new_cfi (void)
706 {
707   dw_cfi_ref cfi = GGC_NEW (dw_cfi_node);
708
709   cfi->dw_cfi_next = NULL;
710   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
711   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
712
713   return cfi;
714 }
715
716 /* Add a Call Frame Instruction to list of instructions.  */
717
718 static inline void
719 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
720 {
721   dw_cfi_ref *p;
722   dw_fde_ref fde = current_fde ();
723
724   /* When DRAP is used, CFA is defined with an expression.  Redefine
725      CFA may lead to a different CFA value.   */
726   /* ??? Of course, this heuristic fails when we're annotating epilogues,
727      because of course we'll always want to redefine the CFA back to the
728      stack pointer on the way out.  Where should we move this check?  */
729   if (0 && fde && fde->drap_reg != INVALID_REGNUM)
730     switch (cfi->dw_cfi_opc)
731       {
732         case DW_CFA_def_cfa_register:
733         case DW_CFA_def_cfa_offset:
734         case DW_CFA_def_cfa_offset_sf:
735         case DW_CFA_def_cfa:
736         case DW_CFA_def_cfa_sf:
737           gcc_unreachable ();
738
739         default:
740           break;
741       }
742
743   /* Find the end of the chain.  */
744   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
745     ;
746
747   *p = cfi;
748 }
749
750 /* Generate a new label for the CFI info to refer to.  FORCE is true
751    if a label needs to be output even when using .cfi_* directives.  */
752
753 char *
754 dwarf2out_cfi_label (bool force)
755 {
756   static char label[20];
757
758   if (!force && dwarf2out_do_cfi_asm ())
759     {
760       /* In this case, we will be emitting the asm directive instead of
761          the label, so just return a placeholder to keep the rest of the
762          interfaces happy.  */
763       strcpy (label, "<do not output>");
764     }
765   else
766     {
767       ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
768       ASM_OUTPUT_LABEL (asm_out_file, label);
769     }
770
771   return label;
772 }
773
774 /* True if remember_state should be emitted before following CFI directive.  */
775 static bool emit_cfa_remember;
776
777 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
778    or to the CIE if LABEL is NULL.  */
779
780 static void
781 add_fde_cfi (const char *label, dw_cfi_ref cfi)
782 {
783   dw_cfi_ref *list_head;
784
785   if (emit_cfa_remember)
786     {
787       dw_cfi_ref cfi_remember;
788
789       /* Emit the state save.  */
790       emit_cfa_remember = false;
791       cfi_remember = new_cfi (); 
792       cfi_remember->dw_cfi_opc = DW_CFA_remember_state;
793       add_fde_cfi (label, cfi_remember);
794     }
795
796   list_head = &cie_cfi_head;
797
798   if (dwarf2out_do_cfi_asm ())
799     {
800       if (label)
801         {
802           dw_fde_ref fde = current_fde ();
803
804           gcc_assert (fde != NULL);
805
806           /* We still have to add the cfi to the list so that lookup_cfa
807              works later on.  When -g2 and above we even need to force
808              emitting of CFI labels and add to list a DW_CFA_set_loc for
809              convert_cfa_to_fb_loc_list purposes.  If we're generating
810              DWARF3 output we use DW_OP_call_frame_cfa and so don't use
811              convert_cfa_to_fb_loc_list.  */
812           if (dwarf_version == 2
813               && debug_info_level > DINFO_LEVEL_TERSE
814               && (write_symbols == DWARF2_DEBUG
815                   || write_symbols == VMS_AND_DWARF2_DEBUG))
816             {
817               switch (cfi->dw_cfi_opc)
818                 {
819                 case DW_CFA_def_cfa_offset:
820                 case DW_CFA_def_cfa_offset_sf:
821                 case DW_CFA_def_cfa_register:
822                 case DW_CFA_def_cfa:
823                 case DW_CFA_def_cfa_sf:
824                 case DW_CFA_def_cfa_expression:
825                 case DW_CFA_restore_state:
826                   if (*label == 0 || strcmp (label, "<do not output>") == 0)
827                     label = dwarf2out_cfi_label (true);
828
829                   if (fde->dw_fde_current_label == NULL
830                       || strcmp (label, fde->dw_fde_current_label) != 0)
831                     {
832                       dw_cfi_ref xcfi;
833
834                       label = xstrdup (label);
835
836                       /* Set the location counter to the new label.  */
837                       xcfi = new_cfi ();
838                       /* It doesn't metter whether DW_CFA_set_loc
839                          or DW_CFA_advance_loc4 is added here, those aren't
840                          emitted into assembly, only looked up by
841                          convert_cfa_to_fb_loc_list.  */
842                       xcfi->dw_cfi_opc = DW_CFA_set_loc;
843                       xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
844                       add_cfi (&fde->dw_fde_cfi, xcfi);
845                       fde->dw_fde_current_label = label;
846                     }
847                   break;
848                 default:
849                   break;
850                 }
851             }
852
853           output_cfi_directive (cfi);
854
855           list_head = &fde->dw_fde_cfi;
856         }
857       /* ??? If this is a CFI for the CIE, we don't emit.  This
858          assumes that the standard CIE contents that the assembler
859          uses matches the standard CIE contents that the compiler
860          uses.  This is probably a bad assumption.  I'm not quite
861          sure how to address this for now.  */
862     }
863   else if (label)
864     {
865       dw_fde_ref fde = current_fde ();
866
867       gcc_assert (fde != NULL);
868
869       if (*label == 0)
870         label = dwarf2out_cfi_label (false);
871
872       if (fde->dw_fde_current_label == NULL
873           || strcmp (label, fde->dw_fde_current_label) != 0)
874         {
875           dw_cfi_ref xcfi;
876
877           label = xstrdup (label);
878
879           /* Set the location counter to the new label.  */
880           xcfi = new_cfi ();
881           /* If we have a current label, advance from there, otherwise
882              set the location directly using set_loc.  */
883           xcfi->dw_cfi_opc = fde->dw_fde_current_label
884                              ? DW_CFA_advance_loc4
885                              : DW_CFA_set_loc;
886           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
887           add_cfi (&fde->dw_fde_cfi, xcfi);
888
889           fde->dw_fde_current_label = label;
890         }
891
892       list_head = &fde->dw_fde_cfi;
893     }
894
895   add_cfi (list_head, cfi);
896 }
897
898 /* Subroutine of lookup_cfa.  */
899
900 static void
901 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc, dw_cfa_location *remember)
902 {
903   switch (cfi->dw_cfi_opc)
904     {
905     case DW_CFA_def_cfa_offset:
906     case DW_CFA_def_cfa_offset_sf:
907       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
908       break;
909     case DW_CFA_def_cfa_register:
910       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
911       break;
912     case DW_CFA_def_cfa:
913     case DW_CFA_def_cfa_sf:
914       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
915       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
916       break;
917     case DW_CFA_def_cfa_expression:
918       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
919       break;
920
921     case DW_CFA_remember_state:
922       gcc_assert (!remember->in_use);
923       *remember = *loc;
924       remember->in_use = 1;
925       break;
926     case DW_CFA_restore_state:
927       gcc_assert (remember->in_use);
928       *loc = *remember;
929       remember->in_use = 0;
930       break;
931
932     default:
933       break;
934     }
935 }
936
937 /* Find the previous value for the CFA.  */
938
939 static void
940 lookup_cfa (dw_cfa_location *loc)
941 {
942   dw_cfi_ref cfi;
943   dw_fde_ref fde;
944   dw_cfa_location remember;
945
946   memset (loc, 0, sizeof (*loc));
947   loc->reg = INVALID_REGNUM;
948   remember = *loc;
949
950   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
951     lookup_cfa_1 (cfi, loc, &remember);
952
953   fde = current_fde ();
954   if (fde)
955     for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
956       lookup_cfa_1 (cfi, loc, &remember);
957 }
958
959 /* The current rule for calculating the DWARF2 canonical frame address.  */
960 static dw_cfa_location cfa;
961
962 /* The register used for saving registers to the stack, and its offset
963    from the CFA.  */
964 static dw_cfa_location cfa_store;
965
966 /* The current save location around an epilogue.  */
967 static dw_cfa_location cfa_remember;
968
969 /* The running total of the size of arguments pushed onto the stack.  */
970 static HOST_WIDE_INT args_size;
971
972 /* The last args_size we actually output.  */
973 static HOST_WIDE_INT old_args_size;
974
975 /* Entry point to update the canonical frame address (CFA).
976    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
977    calculated from REG+OFFSET.  */
978
979 void
980 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
981 {
982   dw_cfa_location loc;
983   loc.indirect = 0;
984   loc.base_offset = 0;
985   loc.reg = reg;
986   loc.offset = offset;
987   def_cfa_1 (label, &loc);
988 }
989
990 /* Determine if two dw_cfa_location structures define the same data.  */
991
992 static bool
993 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
994 {
995   return (loc1->reg == loc2->reg
996           && loc1->offset == loc2->offset
997           && loc1->indirect == loc2->indirect
998           && (loc1->indirect == 0
999               || loc1->base_offset == loc2->base_offset));
1000 }
1001
1002 /* This routine does the actual work.  The CFA is now calculated from
1003    the dw_cfa_location structure.  */
1004
1005 static void
1006 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
1007 {
1008   dw_cfi_ref cfi;
1009   dw_cfa_location old_cfa, loc;
1010
1011   cfa = *loc_p;
1012   loc = *loc_p;
1013
1014   if (cfa_store.reg == loc.reg && loc.indirect == 0)
1015     cfa_store.offset = loc.offset;
1016
1017   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
1018   lookup_cfa (&old_cfa);
1019
1020   /* If nothing changed, no need to issue any call frame instructions.  */
1021   if (cfa_equal_p (&loc, &old_cfa))
1022     return;
1023
1024   cfi = new_cfi ();
1025
1026   if (loc.reg == old_cfa.reg && !loc.indirect)
1027     {
1028       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
1029          the CFA register did not change but the offset did.  The data 
1030          factoring for DW_CFA_def_cfa_offset_sf happens in output_cfi, or
1031          in the assembler via the .cfi_def_cfa_offset directive.  */
1032       if (loc.offset < 0)
1033         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
1034       else
1035         cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
1036       cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
1037     }
1038
1039 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
1040   else if (loc.offset == old_cfa.offset
1041            && old_cfa.reg != INVALID_REGNUM
1042            && !loc.indirect)
1043     {
1044       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
1045          indicating the CFA register has changed to <register> but the
1046          offset has not changed.  */
1047       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
1048       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1049     }
1050 #endif
1051
1052   else if (loc.indirect == 0)
1053     {
1054       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
1055          indicating the CFA register has changed to <register> with
1056          the specified offset.  The data factoring for DW_CFA_def_cfa_sf
1057          happens in output_cfi, or in the assembler via the .cfi_def_cfa
1058          directive.  */
1059       if (loc.offset < 0)
1060         cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
1061       else
1062         cfi->dw_cfi_opc = DW_CFA_def_cfa;
1063       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
1064       cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
1065     }
1066   else
1067     {
1068       /* Construct a DW_CFA_def_cfa_expression instruction to
1069          calculate the CFA using a full location expression since no
1070          register-offset pair is available.  */
1071       struct dw_loc_descr_struct *loc_list;
1072
1073       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
1074       loc_list = build_cfa_loc (&loc, 0);
1075       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
1076     }
1077
1078   add_fde_cfi (label, cfi);
1079 }
1080
1081 /* Add the CFI for saving a register.  REG is the CFA column number.
1082    LABEL is passed to add_fde_cfi.
1083    If SREG is -1, the register is saved at OFFSET from the CFA;
1084    otherwise it is saved in SREG.  */
1085
1086 static void
1087 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
1088 {
1089   dw_cfi_ref cfi = new_cfi ();
1090   dw_fde_ref fde = current_fde ();
1091
1092   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
1093
1094   /* When stack is aligned, store REG using DW_CFA_expression with
1095      FP.  */
1096   if (fde
1097       && fde->stack_realign
1098       && sreg == INVALID_REGNUM)
1099     {
1100       cfi->dw_cfi_opc = DW_CFA_expression;
1101       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = reg;
1102       cfi->dw_cfi_oprnd1.dw_cfi_loc
1103         = build_cfa_aligned_loc (offset, fde->stack_realignment);
1104     }
1105   else if (sreg == INVALID_REGNUM)
1106     {
1107       if (need_data_align_sf_opcode (offset))
1108         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
1109       else if (reg & ~0x3f)
1110         cfi->dw_cfi_opc = DW_CFA_offset_extended;
1111       else
1112         cfi->dw_cfi_opc = DW_CFA_offset;
1113       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
1114     }
1115   else if (sreg == reg)
1116     cfi->dw_cfi_opc = DW_CFA_same_value;
1117   else
1118     {
1119       cfi->dw_cfi_opc = DW_CFA_register;
1120       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
1121     }
1122
1123   add_fde_cfi (label, cfi);
1124 }
1125
1126 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
1127    This CFI tells the unwinder that it needs to restore the window registers
1128    from the previous frame's window save area.
1129
1130    ??? Perhaps we should note in the CIE where windows are saved (instead of
1131    assuming 0(cfa)) and what registers are in the window.  */
1132
1133 void
1134 dwarf2out_window_save (const char *label)
1135 {
1136   dw_cfi_ref cfi = new_cfi ();
1137
1138   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
1139   add_fde_cfi (label, cfi);
1140 }
1141
1142 /* Add a CFI to update the running total of the size of arguments
1143    pushed onto the stack.  */
1144
1145 void
1146 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
1147 {
1148   dw_cfi_ref cfi;
1149
1150   if (size == old_args_size)
1151     return;
1152
1153   old_args_size = size;
1154
1155   cfi = new_cfi ();
1156   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
1157   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
1158   add_fde_cfi (label, cfi);
1159 }
1160
1161 /* Entry point for saving a register to the stack.  REG is the GCC register
1162    number.  LABEL and OFFSET are passed to reg_save.  */
1163
1164 void
1165 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
1166 {
1167   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
1168 }
1169
1170 /* Entry point for saving the return address in the stack.
1171    LABEL and OFFSET are passed to reg_save.  */
1172
1173 void
1174 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
1175 {
1176   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
1177 }
1178
1179 /* Entry point for saving the return address in a register.
1180    LABEL and SREG are passed to reg_save.  */
1181
1182 void
1183 dwarf2out_return_reg (const char *label, unsigned int sreg)
1184 {
1185   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
1186 }
1187
1188 #ifdef DWARF2_UNWIND_INFO
1189 /* Record the initial position of the return address.  RTL is
1190    INCOMING_RETURN_ADDR_RTX.  */
1191
1192 static void
1193 initial_return_save (rtx rtl)
1194 {
1195   unsigned int reg = INVALID_REGNUM;
1196   HOST_WIDE_INT offset = 0;
1197
1198   switch (GET_CODE (rtl))
1199     {
1200     case REG:
1201       /* RA is in a register.  */
1202       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
1203       break;
1204
1205     case MEM:
1206       /* RA is on the stack.  */
1207       rtl = XEXP (rtl, 0);
1208       switch (GET_CODE (rtl))
1209         {
1210         case REG:
1211           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1212           offset = 0;
1213           break;
1214
1215         case PLUS:
1216           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1217           offset = INTVAL (XEXP (rtl, 1));
1218           break;
1219
1220         case MINUS:
1221           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1222           offset = -INTVAL (XEXP (rtl, 1));
1223           break;
1224
1225         default:
1226           gcc_unreachable ();
1227         }
1228
1229       break;
1230
1231     case PLUS:
1232       /* The return address is at some offset from any value we can
1233          actually load.  For instance, on the SPARC it is in %i7+8. Just
1234          ignore the offset for now; it doesn't matter for unwinding frames.  */
1235       gcc_assert (CONST_INT_P (XEXP (rtl, 1)));
1236       initial_return_save (XEXP (rtl, 0));
1237       return;
1238
1239     default:
1240       gcc_unreachable ();
1241     }
1242
1243   if (reg != DWARF_FRAME_RETURN_COLUMN)
1244     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1245 }
1246 #endif
1247
1248 /* Given a SET, calculate the amount of stack adjustment it
1249    contains.  */
1250
1251 static HOST_WIDE_INT
1252 stack_adjust_offset (const_rtx pattern, HOST_WIDE_INT cur_args_size,
1253                      HOST_WIDE_INT cur_offset)
1254 {
1255   const_rtx src = SET_SRC (pattern);
1256   const_rtx dest = SET_DEST (pattern);
1257   HOST_WIDE_INT offset = 0;
1258   enum rtx_code code;
1259
1260   if (dest == stack_pointer_rtx)
1261     {
1262       code = GET_CODE (src);
1263
1264       /* Assume (set (reg sp) (reg whatever)) sets args_size
1265          level to 0.  */
1266       if (code == REG && src != stack_pointer_rtx)
1267         {
1268           offset = -cur_args_size;
1269 #ifndef STACK_GROWS_DOWNWARD
1270           offset = -offset;
1271 #endif
1272           return offset - cur_offset;
1273         }
1274
1275       if (! (code == PLUS || code == MINUS)
1276           || XEXP (src, 0) != stack_pointer_rtx
1277           || !CONST_INT_P (XEXP (src, 1)))
1278         return 0;
1279
1280       /* (set (reg sp) (plus (reg sp) (const_int))) */
1281       offset = INTVAL (XEXP (src, 1));
1282       if (code == PLUS)
1283         offset = -offset;
1284       return offset;
1285     }
1286
1287   if (MEM_P (src) && !MEM_P (dest))
1288     dest = src;
1289   if (MEM_P (dest))
1290     {
1291       /* (set (mem (pre_dec (reg sp))) (foo)) */
1292       src = XEXP (dest, 0);
1293       code = GET_CODE (src);
1294
1295       switch (code)
1296         {
1297         case PRE_MODIFY:
1298         case POST_MODIFY:
1299           if (XEXP (src, 0) == stack_pointer_rtx)
1300             {
1301               rtx val = XEXP (XEXP (src, 1), 1);
1302               /* We handle only adjustments by constant amount.  */
1303               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1304                           && CONST_INT_P (val));
1305               offset = -INTVAL (val);
1306               break;
1307             }
1308           return 0;
1309
1310         case PRE_DEC:
1311         case POST_DEC:
1312           if (XEXP (src, 0) == stack_pointer_rtx)
1313             {
1314               offset = GET_MODE_SIZE (GET_MODE (dest));
1315               break;
1316             }
1317           return 0;
1318
1319         case PRE_INC:
1320         case POST_INC:
1321           if (XEXP (src, 0) == stack_pointer_rtx)
1322             {
1323               offset = -GET_MODE_SIZE (GET_MODE (dest));
1324               break;
1325             }
1326           return 0;
1327
1328         default:
1329           return 0;
1330         }
1331     }
1332   else
1333     return 0;
1334
1335   return offset;
1336 }
1337
1338 /* Precomputed args_size for CODE_LABELs and BARRIERs preceeding them,
1339    indexed by INSN_UID.  */
1340
1341 static HOST_WIDE_INT *barrier_args_size;
1342
1343 /* Helper function for compute_barrier_args_size.  Handle one insn.  */
1344
1345 static HOST_WIDE_INT
1346 compute_barrier_args_size_1 (rtx insn, HOST_WIDE_INT cur_args_size,
1347                              VEC (rtx, heap) **next)
1348 {
1349   HOST_WIDE_INT offset = 0;
1350   int i;
1351
1352   if (! RTX_FRAME_RELATED_P (insn))
1353     {
1354       if (prologue_epilogue_contains (insn))
1355         /* Nothing */;
1356       else if (GET_CODE (PATTERN (insn)) == SET)
1357         offset = stack_adjust_offset (PATTERN (insn), cur_args_size, 0);
1358       else if (GET_CODE (PATTERN (insn)) == PARALLEL
1359                || GET_CODE (PATTERN (insn)) == SEQUENCE)
1360         {
1361           /* There may be stack adjustments inside compound insns.  Search
1362              for them.  */
1363           for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1364             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1365               offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1366                                              cur_args_size, offset);
1367         }
1368     }
1369   else
1370     {
1371       rtx expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1372
1373       if (expr)
1374         {
1375           expr = XEXP (expr, 0);
1376           if (GET_CODE (expr) == PARALLEL
1377               || GET_CODE (expr) == SEQUENCE)
1378             for (i = 1; i < XVECLEN (expr, 0); i++)
1379               {
1380                 rtx elem = XVECEXP (expr, 0, i);
1381
1382                 if (GET_CODE (elem) == SET && !RTX_FRAME_RELATED_P (elem))
1383                   offset += stack_adjust_offset (elem, cur_args_size, offset);
1384               }
1385         }
1386     }
1387
1388 #ifndef STACK_GROWS_DOWNWARD
1389   offset = -offset;
1390 #endif
1391
1392   cur_args_size += offset;
1393   if (cur_args_size < 0)
1394     cur_args_size = 0;
1395
1396   if (JUMP_P (insn))
1397     {
1398       rtx dest = JUMP_LABEL (insn);
1399
1400       if (dest)
1401         {
1402           if (barrier_args_size [INSN_UID (dest)] < 0)
1403             {
1404               barrier_args_size [INSN_UID (dest)] = cur_args_size;
1405               VEC_safe_push (rtx, heap, *next, dest);
1406             }
1407         }
1408     }
1409
1410   return cur_args_size;
1411 }
1412
1413 /* Walk the whole function and compute args_size on BARRIERs.  */
1414
1415 static void
1416 compute_barrier_args_size (void)
1417 {
1418   int max_uid = get_max_uid (), i;
1419   rtx insn;
1420   VEC (rtx, heap) *worklist, *next, *tmp;
1421
1422   barrier_args_size = XNEWVEC (HOST_WIDE_INT, max_uid);
1423   for (i = 0; i < max_uid; i++)
1424     barrier_args_size[i] = -1;
1425
1426   worklist = VEC_alloc (rtx, heap, 20);
1427   next = VEC_alloc (rtx, heap, 20);
1428   insn = get_insns ();
1429   barrier_args_size[INSN_UID (insn)] = 0;
1430   VEC_quick_push (rtx, worklist, insn);
1431   for (;;)
1432     {
1433       while (!VEC_empty (rtx, worklist))
1434         {
1435           rtx prev, body, first_insn;
1436           HOST_WIDE_INT cur_args_size;
1437
1438           first_insn = insn = VEC_pop (rtx, worklist);
1439           cur_args_size = barrier_args_size[INSN_UID (insn)];
1440           prev = prev_nonnote_insn (insn);
1441           if (prev && BARRIER_P (prev))
1442             barrier_args_size[INSN_UID (prev)] = cur_args_size;
1443
1444           for (; insn; insn = NEXT_INSN (insn))
1445             {
1446               if (INSN_DELETED_P (insn) || NOTE_P (insn))
1447                 continue;
1448               if (BARRIER_P (insn))
1449                 break;
1450
1451               if (LABEL_P (insn))
1452                 {
1453                   if (insn == first_insn)
1454                     continue;
1455                   else if (barrier_args_size[INSN_UID (insn)] < 0)
1456                     {
1457                       barrier_args_size[INSN_UID (insn)] = cur_args_size;
1458                       continue;
1459                     }
1460                   else
1461                     {
1462                       /* The insns starting with this label have been
1463                          already scanned or are in the worklist.  */
1464                       break;
1465                     }
1466                 }
1467
1468               body = PATTERN (insn);
1469               if (GET_CODE (body) == SEQUENCE)
1470                 {
1471                   HOST_WIDE_INT dest_args_size = cur_args_size;
1472                   for (i = 1; i < XVECLEN (body, 0); i++)
1473                     if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0))
1474                         && INSN_FROM_TARGET_P (XVECEXP (body, 0, i)))
1475                       dest_args_size
1476                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1477                                                        dest_args_size, &next);
1478                     else
1479                       cur_args_size
1480                         = compute_barrier_args_size_1 (XVECEXP (body, 0, i),
1481                                                        cur_args_size, &next);
1482
1483                   if (INSN_ANNULLED_BRANCH_P (XVECEXP (body, 0, 0)))
1484                     compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1485                                                  dest_args_size, &next);
1486                   else
1487                     cur_args_size
1488                       = compute_barrier_args_size_1 (XVECEXP (body, 0, 0),
1489                                                      cur_args_size, &next);
1490                 }
1491               else
1492                 cur_args_size
1493                   = compute_barrier_args_size_1 (insn, cur_args_size, &next);
1494             }
1495         }
1496
1497       if (VEC_empty (rtx, next))
1498         break;
1499
1500       /* Swap WORKLIST with NEXT and truncate NEXT for next iteration.  */
1501       tmp = next;
1502       next = worklist;
1503       worklist = tmp;
1504       VEC_truncate (rtx, next, 0);
1505     }
1506
1507   VEC_free (rtx, heap, worklist);
1508   VEC_free (rtx, heap, next);
1509 }
1510
1511
1512 /* Check INSN to see if it looks like a push or a stack adjustment, and
1513    make a note of it if it does.  EH uses this information to find out how
1514    much extra space it needs to pop off the stack.  */
1515
1516 static void
1517 dwarf2out_stack_adjust (rtx insn, bool after_p)
1518 {
1519   HOST_WIDE_INT offset;
1520   const char *label;
1521   int i;
1522
1523   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1524      with this function.  Proper support would require all frame-related
1525      insns to be marked, and to be able to handle saving state around
1526      epilogues textually in the middle of the function.  */
1527   if (prologue_epilogue_contains (insn))
1528     return;
1529
1530   /* If INSN is an instruction from target of an annulled branch, the
1531      effects are for the target only and so current argument size
1532      shouldn't change at all.  */
1533   if (final_sequence
1534       && INSN_ANNULLED_BRANCH_P (XVECEXP (final_sequence, 0, 0))
1535       && INSN_FROM_TARGET_P (insn))
1536     return;
1537
1538   /* If only calls can throw, and we have a frame pointer,
1539      save up adjustments until we see the CALL_INSN.  */
1540   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1541     {
1542       if (CALL_P (insn) && !after_p)
1543         {
1544           /* Extract the size of the args from the CALL rtx itself.  */
1545           insn = PATTERN (insn);
1546           if (GET_CODE (insn) == PARALLEL)
1547             insn = XVECEXP (insn, 0, 0);
1548           if (GET_CODE (insn) == SET)
1549             insn = SET_SRC (insn);
1550           gcc_assert (GET_CODE (insn) == CALL);
1551           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1552         }
1553       return;
1554     }
1555
1556   if (CALL_P (insn) && !after_p)
1557     {
1558       if (!flag_asynchronous_unwind_tables)
1559         dwarf2out_args_size ("", args_size);
1560       return;
1561     }
1562   else if (BARRIER_P (insn))
1563     {
1564       /* Don't call compute_barrier_args_size () if the only
1565          BARRIER is at the end of function.  */
1566       if (barrier_args_size == NULL && next_nonnote_insn (insn))
1567         compute_barrier_args_size ();
1568       if (barrier_args_size == NULL)
1569         offset = 0;
1570       else
1571         {
1572           offset = barrier_args_size[INSN_UID (insn)];
1573           if (offset < 0)
1574             offset = 0;
1575         }
1576
1577       offset -= args_size;
1578 #ifndef STACK_GROWS_DOWNWARD
1579       offset = -offset;
1580 #endif
1581     }
1582   else if (GET_CODE (PATTERN (insn)) == SET)
1583     offset = stack_adjust_offset (PATTERN (insn), args_size, 0);
1584   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1585            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1586     {
1587       /* There may be stack adjustments inside compound insns.  Search
1588          for them.  */
1589       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1590         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1591           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i),
1592                                          args_size, offset);
1593     }
1594   else
1595     return;
1596
1597   if (offset == 0)
1598     return;
1599
1600   label = dwarf2out_cfi_label (false);
1601   dwarf2out_args_size_adjust (offset, label);
1602 }
1603
1604 /* Adjust args_size based on stack adjustment OFFSET.  */
1605
1606 static void
1607 dwarf2out_args_size_adjust (HOST_WIDE_INT offset, const char *label)
1608 {
1609   if (cfa.reg == STACK_POINTER_REGNUM)
1610     cfa.offset += offset;
1611
1612   if (cfa_store.reg == STACK_POINTER_REGNUM)
1613     cfa_store.offset += offset;
1614
1615 #ifndef STACK_GROWS_DOWNWARD
1616   offset = -offset;
1617 #endif
1618
1619   args_size += offset;
1620   if (args_size < 0)
1621     args_size = 0;
1622
1623   def_cfa_1 (label, &cfa);
1624   if (flag_asynchronous_unwind_tables)
1625     dwarf2out_args_size (label, args_size);
1626 }
1627
1628 #endif
1629
1630 /* We delay emitting a register save until either (a) we reach the end
1631    of the prologue or (b) the register is clobbered.  This clusters
1632    register saves so that there are fewer pc advances.  */
1633
1634 struct GTY(()) queued_reg_save {
1635   struct queued_reg_save *next;
1636   rtx reg;
1637   HOST_WIDE_INT cfa_offset;
1638   rtx saved_reg;
1639 };
1640
1641 static GTY(()) struct queued_reg_save *queued_reg_saves;
1642
1643 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1644 struct GTY(()) reg_saved_in_data {
1645   rtx orig_reg;
1646   rtx saved_in_reg;
1647 };
1648
1649 /* A list of registers saved in other registers.
1650    The list intentionally has a small maximum capacity of 4; if your
1651    port needs more than that, you might consider implementing a
1652    more efficient data structure.  */
1653 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1654 static GTY(()) size_t num_regs_saved_in_regs;
1655
1656 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1657 static const char *last_reg_save_label;
1658
1659 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1660    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1661
1662 static void
1663 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1664 {
1665   struct queued_reg_save *q;
1666
1667   /* Duplicates waste space, but it's also necessary to remove them
1668      for correctness, since the queue gets output in reverse
1669      order.  */
1670   for (q = queued_reg_saves; q != NULL; q = q->next)
1671     if (REGNO (q->reg) == REGNO (reg))
1672       break;
1673
1674   if (q == NULL)
1675     {
1676       q = GGC_NEW (struct queued_reg_save);
1677       q->next = queued_reg_saves;
1678       queued_reg_saves = q;
1679     }
1680
1681   q->reg = reg;
1682   q->cfa_offset = offset;
1683   q->saved_reg = sreg;
1684
1685   last_reg_save_label = label;
1686 }
1687
1688 /* Output all the entries in QUEUED_REG_SAVES.  */
1689
1690 static void
1691 flush_queued_reg_saves (void)
1692 {
1693   struct queued_reg_save *q;
1694
1695   for (q = queued_reg_saves; q; q = q->next)
1696     {
1697       size_t i;
1698       unsigned int reg, sreg;
1699
1700       for (i = 0; i < num_regs_saved_in_regs; i++)
1701         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1702           break;
1703       if (q->saved_reg && i == num_regs_saved_in_regs)
1704         {
1705           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1706           num_regs_saved_in_regs++;
1707         }
1708       if (i != num_regs_saved_in_regs)
1709         {
1710           regs_saved_in_regs[i].orig_reg = q->reg;
1711           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1712         }
1713
1714       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1715       if (q->saved_reg)
1716         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1717       else
1718         sreg = INVALID_REGNUM;
1719       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1720     }
1721
1722   queued_reg_saves = NULL;
1723   last_reg_save_label = NULL;
1724 }
1725
1726 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1727    location for?  Or, does it clobber a register which we've previously
1728    said that some other register is saved in, and for which we now
1729    have a new location for?  */
1730
1731 static bool
1732 clobbers_queued_reg_save (const_rtx insn)
1733 {
1734   struct queued_reg_save *q;
1735
1736   for (q = queued_reg_saves; q; q = q->next)
1737     {
1738       size_t i;
1739       if (modified_in_p (q->reg, insn))
1740         return true;
1741       for (i = 0; i < num_regs_saved_in_regs; i++)
1742         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1743             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1744           return true;
1745     }
1746
1747   return false;
1748 }
1749
1750 /* Entry point for saving the first register into the second.  */
1751
1752 void
1753 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1754 {
1755   size_t i;
1756   unsigned int regno, sregno;
1757
1758   for (i = 0; i < num_regs_saved_in_regs; i++)
1759     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1760       break;
1761   if (i == num_regs_saved_in_regs)
1762     {
1763       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1764       num_regs_saved_in_regs++;
1765     }
1766   regs_saved_in_regs[i].orig_reg = reg;
1767   regs_saved_in_regs[i].saved_in_reg = sreg;
1768
1769   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1770   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1771   reg_save (label, regno, sregno, 0);
1772 }
1773
1774 /* What register, if any, is currently saved in REG?  */
1775
1776 static rtx
1777 reg_saved_in (rtx reg)
1778 {
1779   unsigned int regn = REGNO (reg);
1780   size_t i;
1781   struct queued_reg_save *q;
1782
1783   for (q = queued_reg_saves; q; q = q->next)
1784     if (q->saved_reg && regn == REGNO (q->saved_reg))
1785       return q->reg;
1786
1787   for (i = 0; i < num_regs_saved_in_regs; i++)
1788     if (regs_saved_in_regs[i].saved_in_reg
1789         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1790       return regs_saved_in_regs[i].orig_reg;
1791
1792   return NULL_RTX;
1793 }
1794
1795
1796 /* A temporary register holding an integral value used in adjusting SP
1797    or setting up the store_reg.  The "offset" field holds the integer
1798    value, not an offset.  */
1799 static dw_cfa_location cfa_temp;
1800
1801 /* A subroutine of dwarf2out_frame_debug, process a REG_DEF_CFA note.  */
1802
1803 static void
1804 dwarf2out_frame_debug_def_cfa (rtx pat, const char *label)
1805 {
1806   memset (&cfa, 0, sizeof (cfa));
1807
1808   switch (GET_CODE (pat))
1809     {
1810     case PLUS:
1811       cfa.reg = REGNO (XEXP (pat, 0));
1812       cfa.offset = INTVAL (XEXP (pat, 1));
1813       break;
1814
1815     case REG:
1816       cfa.reg = REGNO (pat);
1817       break;
1818
1819     default:
1820       /* Recurse and define an expression.  */
1821       gcc_unreachable ();
1822     }
1823
1824   def_cfa_1 (label, &cfa);
1825 }
1826
1827 /* A subroutine of dwarf2out_frame_debug, process a REG_ADJUST_CFA note.  */
1828
1829 static void
1830 dwarf2out_frame_debug_adjust_cfa (rtx pat, const char *label)
1831 {
1832   rtx src, dest;
1833
1834   gcc_assert (GET_CODE (pat) == SET);
1835   dest = XEXP (pat, 0);
1836   src = XEXP (pat, 1);
1837
1838   switch (GET_CODE (src))
1839     {
1840     case PLUS:
1841       gcc_assert (REGNO (XEXP (src, 0)) == cfa.reg);
1842       cfa.offset -= INTVAL (XEXP (src, 1));
1843       break;
1844
1845     case REG:
1846         break;
1847
1848     default:
1849         gcc_unreachable ();
1850     }
1851
1852   cfa.reg = REGNO (dest);
1853   gcc_assert (cfa.indirect == 0);
1854
1855   def_cfa_1 (label, &cfa);
1856 }
1857
1858 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_OFFSET note.  */
1859
1860 static void
1861 dwarf2out_frame_debug_cfa_offset (rtx set, const char *label)
1862 {
1863   HOST_WIDE_INT offset;
1864   rtx src, addr, span;
1865
1866   src = XEXP (set, 1);
1867   addr = XEXP (set, 0);
1868   gcc_assert (MEM_P (addr));
1869   addr = XEXP (addr, 0);
1870   
1871   /* As documented, only consider extremely simple addresses.  */
1872   switch (GET_CODE (addr))
1873     {
1874     case REG:
1875       gcc_assert (REGNO (addr) == cfa.reg);
1876       offset = -cfa.offset;
1877       break;
1878     case PLUS:
1879       gcc_assert (REGNO (XEXP (addr, 0)) == cfa.reg);
1880       offset = INTVAL (XEXP (addr, 1)) - cfa.offset;
1881       break;
1882     default:
1883       gcc_unreachable ();
1884     }
1885
1886   span = targetm.dwarf_register_span (src);
1887
1888   /* ??? We'd like to use queue_reg_save, but we need to come up with
1889      a different flushing heuristic for epilogues.  */
1890   if (!span)
1891     reg_save (label, DWARF_FRAME_REGNUM (REGNO (src)), INVALID_REGNUM, offset);
1892   else
1893     {
1894       /* We have a PARALLEL describing where the contents of SRC live.
1895          Queue register saves for each piece of the PARALLEL.  */
1896       int par_index;
1897       int limit;
1898       HOST_WIDE_INT span_offset = offset;
1899
1900       gcc_assert (GET_CODE (span) == PARALLEL);
1901
1902       limit = XVECLEN (span, 0);
1903       for (par_index = 0; par_index < limit; par_index++)
1904         {
1905           rtx elem = XVECEXP (span, 0, par_index);
1906
1907           reg_save (label, DWARF_FRAME_REGNUM (REGNO (elem)),
1908                     INVALID_REGNUM, span_offset);
1909           span_offset += GET_MODE_SIZE (GET_MODE (elem));
1910         }
1911     }
1912 }
1913
1914 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_REGISTER note.  */
1915
1916 static void
1917 dwarf2out_frame_debug_cfa_register (rtx set, const char *label)
1918 {
1919   rtx src, dest;
1920   unsigned sregno, dregno;
1921
1922   src = XEXP (set, 1);
1923   dest = XEXP (set, 0);
1924
1925   if (src == pc_rtx)
1926     sregno = DWARF_FRAME_RETURN_COLUMN;
1927   else
1928     sregno = DWARF_FRAME_REGNUM (REGNO (src));
1929
1930   dregno = DWARF_FRAME_REGNUM (REGNO (dest));
1931
1932   /* ??? We'd like to use queue_reg_save, but we need to come up with
1933      a different flushing heuristic for epilogues.  */
1934   reg_save (label, sregno, dregno, 0);
1935 }
1936
1937 /* A subroutine of dwarf2out_frame_debug, process a REG_CFA_RESTORE note.  */
1938
1939 static void
1940 dwarf2out_frame_debug_cfa_restore (rtx reg, const char *label)
1941 {
1942   dw_cfi_ref cfi = new_cfi ();
1943   unsigned int regno = DWARF_FRAME_REGNUM (REGNO (reg));
1944
1945   cfi->dw_cfi_opc = (regno & ~0x3f ? DW_CFA_restore_extended : DW_CFA_restore);
1946   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = regno;
1947
1948   add_fde_cfi (label, cfi);
1949 }
1950
1951 /* Record call frame debugging information for an expression EXPR,
1952    which either sets SP or FP (adjusting how we calculate the frame
1953    address) or saves a register to the stack or another register.
1954    LABEL indicates the address of EXPR.
1955
1956    This function encodes a state machine mapping rtxes to actions on
1957    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1958    users need not read the source code.
1959
1960   The High-Level Picture
1961
1962   Changes in the register we use to calculate the CFA: Currently we
1963   assume that if you copy the CFA register into another register, we
1964   should take the other one as the new CFA register; this seems to
1965   work pretty well.  If it's wrong for some target, it's simple
1966   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1967
1968   Changes in the register we use for saving registers to the stack:
1969   This is usually SP, but not always.  Again, we deduce that if you
1970   copy SP into another register (and SP is not the CFA register),
1971   then the new register is the one we will be using for register
1972   saves.  This also seems to work.
1973
1974   Register saves: There's not much guesswork about this one; if
1975   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1976   register save, and the register used to calculate the destination
1977   had better be the one we think we're using for this purpose.
1978   It's also assumed that a copy from a call-saved register to another
1979   register is saving that register if RTX_FRAME_RELATED_P is set on
1980   that instruction.  If the copy is from a call-saved register to
1981   the *same* register, that means that the register is now the same
1982   value as in the caller.
1983
1984   Except: If the register being saved is the CFA register, and the
1985   offset is nonzero, we are saving the CFA, so we assume we have to
1986   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1987   the intent is to save the value of SP from the previous frame.
1988
1989   In addition, if a register has previously been saved to a different
1990   register,
1991
1992   Invariants / Summaries of Rules
1993
1994   cfa          current rule for calculating the CFA.  It usually
1995                consists of a register and an offset.
1996   cfa_store    register used by prologue code to save things to the stack
1997                cfa_store.offset is the offset from the value of
1998                cfa_store.reg to the actual CFA
1999   cfa_temp     register holding an integral value.  cfa_temp.offset
2000                stores the value, which will be used to adjust the
2001                stack pointer.  cfa_temp is also used like cfa_store,
2002                to track stores to the stack via fp or a temp reg.
2003
2004   Rules  1- 4: Setting a register's value to cfa.reg or an expression
2005                with cfa.reg as the first operand changes the cfa.reg and its
2006                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
2007                cfa_temp.offset.
2008
2009   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
2010                expression yielding a constant.  This sets cfa_temp.reg
2011                and cfa_temp.offset.
2012
2013   Rule 5:      Create a new register cfa_store used to save items to the
2014                stack.
2015
2016   Rules 10-14: Save a register to the stack.  Define offset as the
2017                difference of the original location and cfa_store's
2018                location (or cfa_temp's location if cfa_temp is used).
2019
2020   Rules 16-20: If AND operation happens on sp in prologue, we assume
2021                stack is realigned.  We will use a group of DW_OP_XXX
2022                expressions to represent the location of the stored
2023                register instead of CFA+offset.
2024
2025   The Rules
2026
2027   "{a,b}" indicates a choice of a xor b.
2028   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
2029
2030   Rule 1:
2031   (set <reg1> <reg2>:cfa.reg)
2032   effects: cfa.reg = <reg1>
2033            cfa.offset unchanged
2034            cfa_temp.reg = <reg1>
2035            cfa_temp.offset = cfa.offset
2036
2037   Rule 2:
2038   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
2039                               {<const_int>,<reg>:cfa_temp.reg}))
2040   effects: cfa.reg = sp if fp used
2041            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
2042            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
2043              if cfa_store.reg==sp
2044
2045   Rule 3:
2046   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
2047   effects: cfa.reg = fp
2048            cfa_offset += +/- <const_int>
2049
2050   Rule 4:
2051   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
2052   constraints: <reg1> != fp
2053                <reg1> != sp
2054   effects: cfa.reg = <reg1>
2055            cfa_temp.reg = <reg1>
2056            cfa_temp.offset = cfa.offset
2057
2058   Rule 5:
2059   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
2060   constraints: <reg1> != fp
2061                <reg1> != sp
2062   effects: cfa_store.reg = <reg1>
2063            cfa_store.offset = cfa.offset - cfa_temp.offset
2064
2065   Rule 6:
2066   (set <reg> <const_int>)
2067   effects: cfa_temp.reg = <reg>
2068            cfa_temp.offset = <const_int>
2069
2070   Rule 7:
2071   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
2072   effects: cfa_temp.reg = <reg1>
2073            cfa_temp.offset |= <const_int>
2074
2075   Rule 8:
2076   (set <reg> (high <exp>))
2077   effects: none
2078
2079   Rule 9:
2080   (set <reg> (lo_sum <exp> <const_int>))
2081   effects: cfa_temp.reg = <reg>
2082            cfa_temp.offset = <const_int>
2083
2084   Rule 10:
2085   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
2086   effects: cfa_store.offset -= <const_int>
2087            cfa.offset = cfa_store.offset if cfa.reg == sp
2088            cfa.reg = sp
2089            cfa.base_offset = -cfa_store.offset
2090
2091   Rule 11:
2092   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
2093   effects: cfa_store.offset += -/+ mode_size(mem)
2094            cfa.offset = cfa_store.offset if cfa.reg == sp
2095            cfa.reg = sp
2096            cfa.base_offset = -cfa_store.offset
2097
2098   Rule 12:
2099   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
2100
2101        <reg2>)
2102   effects: cfa.reg = <reg1>
2103            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
2104
2105   Rule 13:
2106   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
2107   effects: cfa.reg = <reg1>
2108            cfa.base_offset = -{cfa_store,cfa_temp}.offset
2109
2110   Rule 14:
2111   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
2112   effects: cfa.reg = <reg1>
2113            cfa.base_offset = -cfa_temp.offset
2114            cfa_temp.offset -= mode_size(mem)
2115
2116   Rule 15:
2117   (set <reg> {unspec, unspec_volatile})
2118   effects: target-dependent
2119
2120   Rule 16:
2121   (set sp (and: sp <const_int>))
2122   constraints: cfa_store.reg == sp
2123   effects: current_fde.stack_realign = 1
2124            cfa_store.offset = 0
2125            fde->drap_reg = cfa.reg if cfa.reg != sp and cfa.reg != fp
2126
2127   Rule 17:
2128   (set (mem ({pre_inc, pre_dec} sp)) (mem (plus (cfa.reg) (const_int))))
2129   effects: cfa_store.offset += -/+ mode_size(mem)
2130
2131   Rule 18:
2132   (set (mem ({pre_inc, pre_dec} sp)) fp)
2133   constraints: fde->stack_realign == 1
2134   effects: cfa_store.offset = 0
2135            cfa.reg != HARD_FRAME_POINTER_REGNUM
2136
2137   Rule 19:
2138   (set (mem ({pre_inc, pre_dec} sp)) cfa.reg)
2139   constraints: fde->stack_realign == 1
2140                && cfa.offset == 0
2141                && cfa.indirect == 0
2142                && cfa.reg != HARD_FRAME_POINTER_REGNUM
2143   effects: Use DW_CFA_def_cfa_expression to define cfa
2144            cfa.reg == fde->drap_reg
2145
2146   Rule 20:
2147   (set reg fde->drap_reg)
2148   constraints: fde->vdrap_reg == INVALID_REGNUM
2149   effects: fde->vdrap_reg = reg.
2150   (set mem fde->drap_reg)
2151   constraints: fde->drap_reg_saved == 1
2152   effects: none.  */
2153
2154 static void
2155 dwarf2out_frame_debug_expr (rtx expr, const char *label)
2156 {
2157   rtx src, dest, span;
2158   HOST_WIDE_INT offset;
2159   dw_fde_ref fde;
2160
2161   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
2162      the PARALLEL independently. The first element is always processed if
2163      it is a SET. This is for backward compatibility.   Other elements
2164      are processed only if they are SETs and the RTX_FRAME_RELATED_P
2165      flag is set in them.  */
2166   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
2167     {
2168       int par_index;
2169       int limit = XVECLEN (expr, 0);
2170       rtx elem;
2171
2172       /* PARALLELs have strict read-modify-write semantics, so we
2173          ought to evaluate every rvalue before changing any lvalue.
2174          It's cumbersome to do that in general, but there's an
2175          easy approximation that is enough for all current users:
2176          handle register saves before register assignments.  */
2177       if (GET_CODE (expr) == PARALLEL)
2178         for (par_index = 0; par_index < limit; par_index++)
2179           {
2180             elem = XVECEXP (expr, 0, par_index);
2181             if (GET_CODE (elem) == SET
2182                 && MEM_P (SET_DEST (elem))
2183                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2184               dwarf2out_frame_debug_expr (elem, label);
2185           }
2186
2187       for (par_index = 0; par_index < limit; par_index++)
2188         {
2189           elem = XVECEXP (expr, 0, par_index);
2190           if (GET_CODE (elem) == SET
2191               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
2192               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
2193             dwarf2out_frame_debug_expr (elem, label);
2194           else if (GET_CODE (elem) == SET
2195                    && par_index != 0
2196                    && !RTX_FRAME_RELATED_P (elem))
2197             {
2198               /* Stack adjustment combining might combine some post-prologue
2199                  stack adjustment into a prologue stack adjustment.  */
2200               HOST_WIDE_INT offset = stack_adjust_offset (elem, args_size, 0);
2201
2202               if (offset != 0)
2203                 dwarf2out_args_size_adjust (offset, label);
2204             }
2205         }
2206       return;
2207     }
2208
2209   gcc_assert (GET_CODE (expr) == SET);
2210
2211   src = SET_SRC (expr);
2212   dest = SET_DEST (expr);
2213
2214   if (REG_P (src))
2215     {
2216       rtx rsi = reg_saved_in (src);
2217       if (rsi)
2218         src = rsi;
2219     }
2220
2221   fde = current_fde ();
2222
2223   if (REG_P (src)
2224       && fde
2225       && fde->drap_reg == REGNO (src)
2226       && (fde->drap_reg_saved
2227           || REG_P (dest)))
2228     {
2229       /* Rule 20 */
2230       /* If we are saving dynamic realign argument pointer to a
2231          register, the destination is virtual dynamic realign
2232          argument pointer.  It may be used to access argument.  */
2233       if (REG_P (dest))
2234         {
2235           gcc_assert (fde->vdrap_reg == INVALID_REGNUM);
2236           fde->vdrap_reg = REGNO (dest);
2237         }
2238       return;
2239     }
2240
2241   switch (GET_CODE (dest))
2242     {
2243     case REG:
2244       switch (GET_CODE (src))
2245         {
2246           /* Setting FP from SP.  */
2247         case REG:
2248           if (cfa.reg == (unsigned) REGNO (src))
2249             {
2250               /* Rule 1 */
2251               /* Update the CFA rule wrt SP or FP.  Make sure src is
2252                  relative to the current CFA register.
2253
2254                  We used to require that dest be either SP or FP, but the
2255                  ARM copies SP to a temporary register, and from there to
2256                  FP.  So we just rely on the backends to only set
2257                  RTX_FRAME_RELATED_P on appropriate insns.  */
2258               cfa.reg = REGNO (dest);
2259               cfa_temp.reg = cfa.reg;
2260               cfa_temp.offset = cfa.offset;
2261             }
2262           else
2263             {
2264               /* Saving a register in a register.  */
2265               gcc_assert (!fixed_regs [REGNO (dest)]
2266                           /* For the SPARC and its register window.  */
2267                           || (DWARF_FRAME_REGNUM (REGNO (src))
2268                               == DWARF_FRAME_RETURN_COLUMN));
2269
2270               /* After stack is aligned, we can only save SP in FP
2271                  if drap register is used.  In this case, we have
2272                  to restore stack pointer with the CFA value and we
2273                  don't generate this DWARF information.  */
2274               if (fde
2275                   && fde->stack_realign
2276                   && REGNO (src) == STACK_POINTER_REGNUM)
2277                 gcc_assert (REGNO (dest) == HARD_FRAME_POINTER_REGNUM
2278                             && fde->drap_reg != INVALID_REGNUM
2279                             && cfa.reg != REGNO (src));
2280               else
2281                 queue_reg_save (label, src, dest, 0);
2282             }
2283           break;
2284
2285         case PLUS:
2286         case MINUS:
2287         case LO_SUM:
2288           if (dest == stack_pointer_rtx)
2289             {
2290               /* Rule 2 */
2291               /* Adjusting SP.  */
2292               switch (GET_CODE (XEXP (src, 1)))
2293                 {
2294                 case CONST_INT:
2295                   offset = INTVAL (XEXP (src, 1));
2296                   break;
2297                 case REG:
2298                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
2299                               == cfa_temp.reg);
2300                   offset = cfa_temp.offset;
2301                   break;
2302                 default:
2303                   gcc_unreachable ();
2304                 }
2305
2306               if (XEXP (src, 0) == hard_frame_pointer_rtx)
2307                 {
2308                   /* Restoring SP from FP in the epilogue.  */
2309                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
2310                   cfa.reg = STACK_POINTER_REGNUM;
2311                 }
2312               else if (GET_CODE (src) == LO_SUM)
2313                 /* Assume we've set the source reg of the LO_SUM from sp.  */
2314                 ;
2315               else
2316                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
2317
2318               if (GET_CODE (src) != MINUS)
2319                 offset = -offset;
2320               if (cfa.reg == STACK_POINTER_REGNUM)
2321                 cfa.offset += offset;
2322               if (cfa_store.reg == STACK_POINTER_REGNUM)
2323                 cfa_store.offset += offset;
2324             }
2325           else if (dest == hard_frame_pointer_rtx)
2326             {
2327               /* Rule 3 */
2328               /* Either setting the FP from an offset of the SP,
2329                  or adjusting the FP */
2330               gcc_assert (frame_pointer_needed);
2331
2332               gcc_assert (REG_P (XEXP (src, 0))
2333                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
2334                           && CONST_INT_P (XEXP (src, 1)));
2335               offset = INTVAL (XEXP (src, 1));
2336               if (GET_CODE (src) != MINUS)
2337                 offset = -offset;
2338               cfa.offset += offset;
2339               cfa.reg = HARD_FRAME_POINTER_REGNUM;
2340             }
2341           else
2342             {
2343               gcc_assert (GET_CODE (src) != MINUS);
2344
2345               /* Rule 4 */
2346               if (REG_P (XEXP (src, 0))
2347                   && REGNO (XEXP (src, 0)) == cfa.reg
2348                   && CONST_INT_P (XEXP (src, 1)))
2349                 {
2350                   /* Setting a temporary CFA register that will be copied
2351                      into the FP later on.  */
2352                   offset = - INTVAL (XEXP (src, 1));
2353                   cfa.offset += offset;
2354                   cfa.reg = REGNO (dest);
2355                   /* Or used to save regs to the stack.  */
2356                   cfa_temp.reg = cfa.reg;
2357                   cfa_temp.offset = cfa.offset;
2358                 }
2359
2360               /* Rule 5 */
2361               else if (REG_P (XEXP (src, 0))
2362                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
2363                        && XEXP (src, 1) == stack_pointer_rtx)
2364                 {
2365                   /* Setting a scratch register that we will use instead
2366                      of SP for saving registers to the stack.  */
2367                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
2368                   cfa_store.reg = REGNO (dest);
2369                   cfa_store.offset = cfa.offset - cfa_temp.offset;
2370                 }
2371
2372               /* Rule 9 */
2373               else if (GET_CODE (src) == LO_SUM
2374                        && CONST_INT_P (XEXP (src, 1)))
2375                 {
2376                   cfa_temp.reg = REGNO (dest);
2377                   cfa_temp.offset = INTVAL (XEXP (src, 1));
2378                 }
2379               else
2380                 gcc_unreachable ();
2381             }
2382           break;
2383
2384           /* Rule 6 */
2385         case CONST_INT:
2386           cfa_temp.reg = REGNO (dest);
2387           cfa_temp.offset = INTVAL (src);
2388           break;
2389
2390           /* Rule 7 */
2391         case IOR:
2392           gcc_assert (REG_P (XEXP (src, 0))
2393                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
2394                       && CONST_INT_P (XEXP (src, 1)));
2395
2396           if ((unsigned) REGNO (dest) != cfa_temp.reg)
2397             cfa_temp.reg = REGNO (dest);
2398           cfa_temp.offset |= INTVAL (XEXP (src, 1));
2399           break;
2400
2401           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
2402              which will fill in all of the bits.  */
2403           /* Rule 8 */
2404         case HIGH:
2405           break;
2406
2407           /* Rule 15 */
2408         case UNSPEC:
2409         case UNSPEC_VOLATILE:
2410           gcc_assert (targetm.dwarf_handle_frame_unspec);
2411           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
2412           return;
2413
2414           /* Rule 16 */
2415         case AND:
2416           /* If this AND operation happens on stack pointer in prologue,
2417              we assume the stack is realigned and we extract the
2418              alignment.  */
2419           if (fde && XEXP (src, 0) == stack_pointer_rtx)
2420             {
2421               gcc_assert (cfa_store.reg == REGNO (XEXP (src, 0)));
2422               fde->stack_realign = 1;
2423               fde->stack_realignment = INTVAL (XEXP (src, 1));
2424               cfa_store.offset = 0;
2425
2426               if (cfa.reg != STACK_POINTER_REGNUM
2427                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2428                 fde->drap_reg = cfa.reg;
2429             }
2430           return;
2431
2432         default:
2433           gcc_unreachable ();
2434         }
2435
2436       def_cfa_1 (label, &cfa);
2437       break;
2438
2439     case MEM:
2440
2441       /* Saving a register to the stack.  Make sure dest is relative to the
2442          CFA register.  */
2443       switch (GET_CODE (XEXP (dest, 0)))
2444         {
2445           /* Rule 10 */
2446           /* With a push.  */
2447         case PRE_MODIFY:
2448           /* We can't handle variable size modifications.  */
2449           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2450                       == CONST_INT);
2451           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
2452
2453           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
2454                       && cfa_store.reg == STACK_POINTER_REGNUM);
2455
2456           cfa_store.offset += offset;
2457           if (cfa.reg == STACK_POINTER_REGNUM)
2458             cfa.offset = cfa_store.offset;
2459
2460           offset = -cfa_store.offset;
2461           break;
2462
2463           /* Rule 11 */
2464         case PRE_INC:
2465         case PRE_DEC:
2466           offset = GET_MODE_SIZE (GET_MODE (dest));
2467           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
2468             offset = -offset;
2469
2470           gcc_assert ((REGNO (XEXP (XEXP (dest, 0), 0))
2471                        == STACK_POINTER_REGNUM)
2472                       && cfa_store.reg == STACK_POINTER_REGNUM);
2473
2474           cfa_store.offset += offset;
2475
2476           /* Rule 18: If stack is aligned, we will use FP as a
2477              reference to represent the address of the stored
2478              regiser.  */
2479           if (fde
2480               && fde->stack_realign
2481               && src == hard_frame_pointer_rtx)
2482             {
2483               gcc_assert (cfa.reg != HARD_FRAME_POINTER_REGNUM);
2484               cfa_store.offset = 0;
2485             }
2486
2487           if (cfa.reg == STACK_POINTER_REGNUM)
2488             cfa.offset = cfa_store.offset;
2489
2490           offset = -cfa_store.offset;
2491           break;
2492
2493           /* Rule 12 */
2494           /* With an offset.  */
2495         case PLUS:
2496         case MINUS:
2497         case LO_SUM:
2498           {
2499             int regno;
2500
2501             gcc_assert (CONST_INT_P (XEXP (XEXP (dest, 0), 1))
2502                         && REG_P (XEXP (XEXP (dest, 0), 0)));
2503             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
2504             if (GET_CODE (XEXP (dest, 0)) == MINUS)
2505               offset = -offset;
2506
2507             regno = REGNO (XEXP (XEXP (dest, 0), 0));
2508
2509             if (cfa_store.reg == (unsigned) regno)
2510               offset -= cfa_store.offset;
2511             else
2512               {
2513                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2514                 offset -= cfa_temp.offset;
2515               }
2516           }
2517           break;
2518
2519           /* Rule 13 */
2520           /* Without an offset.  */
2521         case REG:
2522           {
2523             int regno = REGNO (XEXP (dest, 0));
2524
2525             if (cfa_store.reg == (unsigned) regno)
2526               offset = -cfa_store.offset;
2527             else
2528               {
2529                 gcc_assert (cfa_temp.reg == (unsigned) regno);
2530                 offset = -cfa_temp.offset;
2531               }
2532           }
2533           break;
2534
2535           /* Rule 14 */
2536         case POST_INC:
2537           gcc_assert (cfa_temp.reg
2538                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
2539           offset = -cfa_temp.offset;
2540           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
2541           break;
2542
2543         default:
2544           gcc_unreachable ();
2545         }
2546
2547         /* Rule 17 */
2548         /* If the source operand of this MEM operation is not a
2549            register, basically the source is return address.  Here
2550            we only care how much stack grew and we don't save it.  */
2551       if (!REG_P (src))
2552         break;
2553
2554       if (REGNO (src) != STACK_POINTER_REGNUM
2555           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
2556           && (unsigned) REGNO (src) == cfa.reg)
2557         {
2558           /* We're storing the current CFA reg into the stack.  */
2559
2560           if (cfa.offset == 0)
2561             {
2562               /* Rule 19 */
2563               /* If stack is aligned, putting CFA reg into stack means
2564                  we can no longer use reg + offset to represent CFA.
2565                  Here we use DW_CFA_def_cfa_expression instead.  The
2566                  result of this expression equals to the original CFA
2567                  value.  */
2568               if (fde
2569                   && fde->stack_realign
2570                   && cfa.indirect == 0
2571                   && cfa.reg != HARD_FRAME_POINTER_REGNUM)
2572                 {
2573                   dw_cfa_location cfa_exp;
2574
2575                   gcc_assert (fde->drap_reg == cfa.reg);
2576
2577                   cfa_exp.indirect = 1;
2578                   cfa_exp.reg = HARD_FRAME_POINTER_REGNUM;
2579                   cfa_exp.base_offset = offset;
2580                   cfa_exp.offset = 0;
2581
2582                   fde->drap_reg_saved = 1;
2583
2584                   def_cfa_1 (label, &cfa_exp);
2585                   break;
2586                 }
2587
2588               /* If the source register is exactly the CFA, assume
2589                  we're saving SP like any other register; this happens
2590                  on the ARM.  */
2591               def_cfa_1 (label, &cfa);
2592               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
2593               break;
2594             }
2595           else
2596             {
2597               /* Otherwise, we'll need to look in the stack to
2598                  calculate the CFA.  */
2599               rtx x = XEXP (dest, 0);
2600
2601               if (!REG_P (x))
2602                 x = XEXP (x, 0);
2603               gcc_assert (REG_P (x));
2604
2605               cfa.reg = REGNO (x);
2606               cfa.base_offset = offset;
2607               cfa.indirect = 1;
2608               def_cfa_1 (label, &cfa);
2609               break;
2610             }
2611         }
2612
2613       def_cfa_1 (label, &cfa);
2614       {
2615         span = targetm.dwarf_register_span (src);
2616
2617         if (!span)
2618           queue_reg_save (label, src, NULL_RTX, offset);
2619         else
2620           {
2621             /* We have a PARALLEL describing where the contents of SRC
2622                live.  Queue register saves for each piece of the
2623                PARALLEL.  */
2624             int par_index;
2625             int limit;
2626             HOST_WIDE_INT span_offset = offset;
2627
2628             gcc_assert (GET_CODE (span) == PARALLEL);
2629
2630             limit = XVECLEN (span, 0);
2631             for (par_index = 0; par_index < limit; par_index++)
2632               {
2633                 rtx elem = XVECEXP (span, 0, par_index);
2634
2635                 queue_reg_save (label, elem, NULL_RTX, span_offset);
2636                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
2637               }
2638           }
2639       }
2640       break;
2641
2642     default:
2643       gcc_unreachable ();
2644     }
2645 }
2646
2647 /* Record call frame debugging information for INSN, which either
2648    sets SP or FP (adjusting how we calculate the frame address) or saves a
2649    register to the stack.  If INSN is NULL_RTX, initialize our state.
2650
2651    If AFTER_P is false, we're being called before the insn is emitted,
2652    otherwise after.  Call instructions get invoked twice.  */
2653
2654 void
2655 dwarf2out_frame_debug (rtx insn, bool after_p)
2656 {
2657   const char *label;
2658   rtx note, n;
2659   bool handled_one = false;
2660
2661   if (insn == NULL_RTX)
2662     {
2663       size_t i;
2664
2665       /* Flush any queued register saves.  */
2666       flush_queued_reg_saves ();
2667
2668       /* Set up state for generating call frame debug info.  */
2669       lookup_cfa (&cfa);
2670       gcc_assert (cfa.reg
2671                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
2672
2673       cfa.reg = STACK_POINTER_REGNUM;
2674       cfa_store = cfa;
2675       cfa_temp.reg = -1;
2676       cfa_temp.offset = 0;
2677
2678       for (i = 0; i < num_regs_saved_in_regs; i++)
2679         {
2680           regs_saved_in_regs[i].orig_reg = NULL_RTX;
2681           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
2682         }
2683       num_regs_saved_in_regs = 0;
2684
2685       if (barrier_args_size)
2686         {
2687           XDELETEVEC (barrier_args_size);
2688           barrier_args_size = NULL;
2689         }
2690       return;
2691     }
2692
2693   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
2694     flush_queued_reg_saves ();
2695
2696   if (! RTX_FRAME_RELATED_P (insn))
2697     {
2698       if (!ACCUMULATE_OUTGOING_ARGS)
2699         dwarf2out_stack_adjust (insn, after_p);
2700       return;
2701     }
2702
2703   label = dwarf2out_cfi_label (false);
2704
2705   for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2706     switch (REG_NOTE_KIND (note))
2707       {
2708       case REG_FRAME_RELATED_EXPR:
2709         insn = XEXP (note, 0);
2710         goto found;
2711
2712       case REG_CFA_DEF_CFA:
2713         dwarf2out_frame_debug_def_cfa (XEXP (note, 0), label);
2714         handled_one = true;
2715         break;
2716
2717       case REG_CFA_ADJUST_CFA:
2718         n = XEXP (note, 0);
2719         if (n == NULL)
2720           {
2721             n = PATTERN (insn);
2722             if (GET_CODE (n) == PARALLEL)
2723               n = XVECEXP (n, 0, 0);
2724           }
2725         dwarf2out_frame_debug_adjust_cfa (n, label);
2726         handled_one = true;
2727         break;
2728
2729       case REG_CFA_OFFSET:
2730         n = XEXP (note, 0);
2731         if (n == NULL)
2732           n = single_set (insn);
2733         dwarf2out_frame_debug_cfa_offset (n, label);
2734         handled_one = true;
2735         break;
2736
2737       case REG_CFA_REGISTER:
2738         n = XEXP (note, 0);
2739         if (n == NULL)
2740           {
2741             n = PATTERN (insn);
2742             if (GET_CODE (n) == PARALLEL)
2743               n = XVECEXP (n, 0, 0);
2744           }
2745         dwarf2out_frame_debug_cfa_register (n, label);
2746         handled_one = true;
2747         break;
2748
2749       case REG_CFA_RESTORE:
2750         n = XEXP (note, 0);
2751         if (n == NULL)
2752           {
2753             n = PATTERN (insn);
2754             if (GET_CODE (n) == PARALLEL)
2755               n = XVECEXP (n, 0, 0);
2756             n = XEXP (n, 0);
2757           }
2758         dwarf2out_frame_debug_cfa_restore (n, label);
2759         handled_one = true;
2760         break;
2761
2762       default:
2763         break;
2764       }
2765   if (handled_one)
2766     return;
2767
2768   insn = PATTERN (insn);
2769  found:
2770   dwarf2out_frame_debug_expr (insn, label);
2771 }
2772
2773 /* Determine if we need to save and restore CFI information around this
2774    epilogue.  If SIBCALL is true, then this is a sibcall epilogue.  If
2775    we do need to save/restore, then emit the save now, and insert a
2776    NOTE_INSN_CFA_RESTORE_STATE at the appropriate place in the stream.  */
2777
2778 void
2779 dwarf2out_begin_epilogue (rtx insn)
2780 {
2781   bool saw_frp = false;
2782   rtx i;
2783
2784   /* Scan forward to the return insn, noticing if there are possible
2785      frame related insns.  */
2786   for (i = NEXT_INSN (insn); i ; i = NEXT_INSN (i))
2787     {
2788       if (!INSN_P (i))
2789         continue;
2790
2791       /* Look for both regular and sibcalls to end the block.  */
2792       if (returnjump_p (i))
2793         break;
2794       if (CALL_P (i) && SIBLING_CALL_P (i))
2795         break;
2796
2797       if (GET_CODE (PATTERN (i)) == SEQUENCE)
2798         {
2799           int idx;
2800           rtx seq = PATTERN (i);
2801
2802           if (returnjump_p (XVECEXP (seq, 0, 0)))
2803             break;
2804           if (CALL_P (XVECEXP (seq, 0, 0))
2805               && SIBLING_CALL_P (XVECEXP (seq, 0, 0)))
2806             break;
2807
2808           for (idx = 0; idx < XVECLEN (seq, 0); idx++)
2809             if (RTX_FRAME_RELATED_P (XVECEXP (seq, 0, idx)))
2810               saw_frp = true;
2811         }
2812
2813       if (RTX_FRAME_RELATED_P (i))
2814         saw_frp = true;
2815     }
2816
2817   /* If the port doesn't emit epilogue unwind info, we don't need a
2818      save/restore pair.  */
2819   if (!saw_frp)
2820     return;
2821
2822   /* Otherwise, search forward to see if the return insn was the last
2823      basic block of the function.  If so, we don't need save/restore.  */
2824   gcc_assert (i != NULL);
2825   i = next_real_insn (i);
2826   if (i == NULL)
2827     return;
2828
2829   /* Insert the restore before that next real insn in the stream, and before
2830      a potential NOTE_INSN_EPILOGUE_BEG -- we do need these notes to be
2831      properly nested.  This should be after any label or alignment.  This
2832      will be pushed into the CFI stream by the function below.  */
2833   while (1)
2834     {
2835       rtx p = PREV_INSN (i);
2836       if (!NOTE_P (p))
2837         break;
2838       if (NOTE_KIND (p) == NOTE_INSN_BASIC_BLOCK)
2839         break;
2840       i = p;
2841     }
2842   emit_note_before (NOTE_INSN_CFA_RESTORE_STATE, i);
2843
2844   emit_cfa_remember = true;
2845
2846   /* And emulate the state save.  */
2847   gcc_assert (!cfa_remember.in_use);
2848   cfa_remember = cfa;
2849   cfa_remember.in_use = 1;
2850 }
2851
2852 /* A "subroutine" of dwarf2out_begin_epilogue.  Emit the restore required.  */
2853
2854 void
2855 dwarf2out_frame_debug_restore_state (void)
2856 {
2857   dw_cfi_ref cfi = new_cfi (); 
2858   const char *label = dwarf2out_cfi_label (false);
2859
2860   cfi->dw_cfi_opc = DW_CFA_restore_state;
2861   add_fde_cfi (label, cfi);
2862
2863   gcc_assert (cfa_remember.in_use);
2864   cfa = cfa_remember;
2865   cfa_remember.in_use = 0;
2866 }
2867
2868 #endif
2869
2870 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
2871 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
2872  (enum dwarf_call_frame_info cfi);
2873
2874 static enum dw_cfi_oprnd_type
2875 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
2876 {
2877   switch (cfi)
2878     {
2879     case DW_CFA_nop:
2880     case DW_CFA_GNU_window_save:
2881     case DW_CFA_remember_state:
2882     case DW_CFA_restore_state:
2883       return dw_cfi_oprnd_unused;
2884
2885     case DW_CFA_set_loc:
2886     case DW_CFA_advance_loc1:
2887     case DW_CFA_advance_loc2:
2888     case DW_CFA_advance_loc4:
2889     case DW_CFA_MIPS_advance_loc8:
2890       return dw_cfi_oprnd_addr;
2891
2892     case DW_CFA_offset:
2893     case DW_CFA_offset_extended:
2894     case DW_CFA_def_cfa:
2895     case DW_CFA_offset_extended_sf:
2896     case DW_CFA_def_cfa_sf:
2897     case DW_CFA_restore:
2898     case DW_CFA_restore_extended:
2899     case DW_CFA_undefined:
2900     case DW_CFA_same_value:
2901     case DW_CFA_def_cfa_register:
2902     case DW_CFA_register:
2903       return dw_cfi_oprnd_reg_num;
2904
2905     case DW_CFA_def_cfa_offset:
2906     case DW_CFA_GNU_args_size:
2907     case DW_CFA_def_cfa_offset_sf:
2908       return dw_cfi_oprnd_offset;
2909
2910     case DW_CFA_def_cfa_expression:
2911     case DW_CFA_expression:
2912       return dw_cfi_oprnd_loc;
2913
2914     default:
2915       gcc_unreachable ();
2916     }
2917 }
2918
2919 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2920 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2921  (enum dwarf_call_frame_info cfi);
2922
2923 static enum dw_cfi_oprnd_type
2924 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2925 {
2926   switch (cfi)
2927     {
2928     case DW_CFA_def_cfa:
2929     case DW_CFA_def_cfa_sf:
2930     case DW_CFA_offset:
2931     case DW_CFA_offset_extended_sf:
2932     case DW_CFA_offset_extended:
2933       return dw_cfi_oprnd_offset;
2934
2935     case DW_CFA_register:
2936       return dw_cfi_oprnd_reg_num;
2937
2938     default:
2939       return dw_cfi_oprnd_unused;
2940     }
2941 }
2942
2943 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2944
2945 /* Switch [BACK] to eh_frame_section.  If we don't have an eh_frame_section,
2946    switch to the data section instead, and write out a synthetic start label
2947    for collect2 the first time around.  */
2948
2949 static void
2950 switch_to_eh_frame_section (bool back)
2951 {
2952   tree label;
2953
2954 #ifdef EH_FRAME_SECTION_NAME
2955   if (eh_frame_section == 0)
2956     {
2957       int flags;
2958
2959       if (EH_TABLES_CAN_BE_READ_ONLY)
2960         {
2961           int fde_encoding;
2962           int per_encoding;
2963           int lsda_encoding;
2964
2965           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2966                                                        /*global=*/0);
2967           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2968                                                        /*global=*/1);
2969           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2970                                                         /*global=*/0);
2971           flags = ((! flag_pic
2972                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2973                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2974                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2975                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2976                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2977                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2978                    ? 0 : SECTION_WRITE);
2979         }
2980       else
2981         flags = SECTION_WRITE;
2982       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2983     }
2984 #endif
2985
2986   if (eh_frame_section)
2987     switch_to_section (eh_frame_section);
2988   else
2989     {
2990       /* We have no special eh_frame section.  Put the information in
2991          the data section and emit special labels to guide collect2.  */
2992       switch_to_section (data_section);
2993
2994       if (!back)
2995         {
2996           label = get_file_function_name ("F");
2997           ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2998           targetm.asm_out.globalize_label (asm_out_file,
2999                                            IDENTIFIER_POINTER (label));
3000           ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
3001         }
3002     }
3003 }
3004
3005 /* Switch [BACK] to the eh or debug frame table section, depending on
3006    FOR_EH.  */
3007
3008 static void
3009 switch_to_frame_table_section (int for_eh, bool back)
3010 {
3011   if (for_eh)
3012     switch_to_eh_frame_section (back);
3013   else
3014     {
3015       if (!debug_frame_section)
3016         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
3017                                            SECTION_DEBUG, NULL);
3018       switch_to_section (debug_frame_section);
3019     }
3020 }
3021
3022 /* Output a Call Frame Information opcode and its operand(s).  */
3023
3024 static void
3025 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
3026 {
3027   unsigned long r;
3028   HOST_WIDE_INT off;
3029
3030   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
3031     dw2_asm_output_data (1, (cfi->dw_cfi_opc
3032                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
3033                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
3034                          ((unsigned HOST_WIDE_INT)
3035                           cfi->dw_cfi_oprnd1.dw_cfi_offset));
3036   else if (cfi->dw_cfi_opc == DW_CFA_offset)
3037     {
3038       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3039       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3040                            "DW_CFA_offset, column 0x%lx", r);
3041       off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3042       dw2_asm_output_data_uleb128 (off, NULL);
3043     }
3044   else if (cfi->dw_cfi_opc == DW_CFA_restore)
3045     {
3046       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3047       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
3048                            "DW_CFA_restore, column 0x%lx", r);
3049     }
3050   else
3051     {
3052       dw2_asm_output_data (1, cfi->dw_cfi_opc,
3053                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
3054
3055       switch (cfi->dw_cfi_opc)
3056         {
3057         case DW_CFA_set_loc:
3058           if (for_eh)
3059             dw2_asm_output_encoded_addr_rtx (
3060                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
3061                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
3062                 false, NULL);
3063           else
3064             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
3065                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
3066           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3067           break;
3068
3069         case DW_CFA_advance_loc1:
3070           dw2_asm_output_delta (1, 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_loc2:
3076           dw2_asm_output_delta (2, 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_advance_loc4:
3082           dw2_asm_output_delta (4, 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_MIPS_advance_loc8:
3088           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
3089                                 fde->dw_fde_current_label, NULL);
3090           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
3091           break;
3092
3093         case DW_CFA_offset_extended:
3094           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3095           dw2_asm_output_data_uleb128 (r, NULL);
3096           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3097           dw2_asm_output_data_uleb128 (off, NULL);
3098           break;
3099
3100         case DW_CFA_def_cfa:
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           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
3104           break;
3105
3106         case DW_CFA_offset_extended_sf:
3107           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3108           dw2_asm_output_data_uleb128 (r, NULL);
3109           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3110           dw2_asm_output_data_sleb128 (off, NULL);
3111           break;
3112
3113         case DW_CFA_def_cfa_sf:
3114           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3115           dw2_asm_output_data_uleb128 (r, NULL);
3116           off = div_data_align (cfi->dw_cfi_oprnd2.dw_cfi_offset);
3117           dw2_asm_output_data_sleb128 (off, NULL);
3118           break;
3119
3120         case DW_CFA_restore_extended:
3121         case DW_CFA_undefined:
3122         case DW_CFA_same_value:
3123         case DW_CFA_def_cfa_register:
3124           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3125           dw2_asm_output_data_uleb128 (r, NULL);
3126           break;
3127
3128         case DW_CFA_register:
3129           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
3130           dw2_asm_output_data_uleb128 (r, NULL);
3131           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
3132           dw2_asm_output_data_uleb128 (r, NULL);
3133           break;
3134
3135         case DW_CFA_def_cfa_offset:
3136         case DW_CFA_GNU_args_size:
3137           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
3138           break;
3139
3140         case DW_CFA_def_cfa_offset_sf:
3141           off = div_data_align (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3142           dw2_asm_output_data_sleb128 (off, NULL);
3143           break;
3144
3145         case DW_CFA_GNU_window_save:
3146           break;
3147
3148         case DW_CFA_def_cfa_expression:
3149         case DW_CFA_expression:
3150           output_cfa_loc (cfi);
3151           break;
3152
3153         case DW_CFA_GNU_negative_offset_extended:
3154           /* Obsoleted by DW_CFA_offset_extended_sf.  */
3155           gcc_unreachable ();
3156
3157         default:
3158           break;
3159         }
3160     }
3161 }
3162
3163 /* Similar, but do it via assembler directives instead.  */
3164
3165 static void
3166 output_cfi_directive (dw_cfi_ref cfi)
3167 {
3168   unsigned long r, r2;
3169
3170   switch (cfi->dw_cfi_opc)
3171     {
3172     case DW_CFA_advance_loc:
3173     case DW_CFA_advance_loc1:
3174     case DW_CFA_advance_loc2:
3175     case DW_CFA_advance_loc4:
3176     case DW_CFA_MIPS_advance_loc8:
3177     case DW_CFA_set_loc:
3178       /* Should only be created by add_fde_cfi in a code path not
3179          followed when emitting via directives.  The assembler is
3180          going to take care of this for us.  */
3181       gcc_unreachable ();
3182
3183     case DW_CFA_offset:
3184     case DW_CFA_offset_extended:
3185     case DW_CFA_offset_extended_sf:
3186       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3187       fprintf (asm_out_file, "\t.cfi_offset %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3188                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3189       break;
3190
3191     case DW_CFA_restore:
3192     case DW_CFA_restore_extended:
3193       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3194       fprintf (asm_out_file, "\t.cfi_restore %lu\n", r);
3195       break;
3196
3197     case DW_CFA_undefined:
3198       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3199       fprintf (asm_out_file, "\t.cfi_undefined %lu\n", r);
3200       break;
3201
3202     case DW_CFA_same_value:
3203       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3204       fprintf (asm_out_file, "\t.cfi_same_value %lu\n", r);
3205       break;
3206
3207     case DW_CFA_def_cfa:
3208     case DW_CFA_def_cfa_sf:
3209       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3210       fprintf (asm_out_file, "\t.cfi_def_cfa %lu, "HOST_WIDE_INT_PRINT_DEC"\n",
3211                r, cfi->dw_cfi_oprnd2.dw_cfi_offset);
3212       break;
3213
3214     case DW_CFA_def_cfa_register:
3215       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3216       fprintf (asm_out_file, "\t.cfi_def_cfa_register %lu\n", r);
3217       break;
3218
3219     case DW_CFA_register:
3220       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, 1);
3221       r2 = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, 1);
3222       fprintf (asm_out_file, "\t.cfi_register %lu, %lu\n", r, r2);
3223       break;
3224
3225     case DW_CFA_def_cfa_offset:
3226     case DW_CFA_def_cfa_offset_sf:
3227       fprintf (asm_out_file, "\t.cfi_def_cfa_offset "
3228                HOST_WIDE_INT_PRINT_DEC"\n",
3229                cfi->dw_cfi_oprnd1.dw_cfi_offset);
3230       break;
3231
3232     case DW_CFA_remember_state:
3233       fprintf (asm_out_file, "\t.cfi_remember_state\n");
3234       break;
3235     case DW_CFA_restore_state:
3236       fprintf (asm_out_file, "\t.cfi_restore_state\n");
3237       break;
3238
3239     case DW_CFA_GNU_args_size:
3240       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", DW_CFA_GNU_args_size);
3241       dw2_asm_output_data_uleb128_raw (cfi->dw_cfi_oprnd1.dw_cfi_offset);
3242       if (flag_debug_asm)
3243         fprintf (asm_out_file, "\t%s args_size "HOST_WIDE_INT_PRINT_DEC,
3244                  ASM_COMMENT_START, cfi->dw_cfi_oprnd1.dw_cfi_offset);
3245       fputc ('\n', asm_out_file);
3246       break;
3247
3248     case DW_CFA_GNU_window_save:
3249       fprintf (asm_out_file, "\t.cfi_window_save\n");
3250       break;
3251
3252     case DW_CFA_def_cfa_expression:
3253     case DW_CFA_expression:
3254       fprintf (asm_out_file, "\t.cfi_escape 0x%x,", cfi->dw_cfi_opc);
3255       output_cfa_loc_raw (cfi);
3256       fputc ('\n', asm_out_file);
3257       break;
3258
3259     default:
3260       gcc_unreachable ();
3261     }
3262 }
3263
3264 DEF_VEC_P (dw_cfi_ref);
3265 DEF_VEC_ALLOC_P (dw_cfi_ref, heap);
3266
3267 /* Output CFIs to bring current FDE to the same state as after executing
3268    CFIs in CFI chain.  DO_CFI_ASM is true if .cfi_* directives shall
3269    be emitted, false otherwise.  If it is false, FDE and FOR_EH are the
3270    other arguments to pass to output_cfi.  */
3271
3272 static void
3273 output_cfis (dw_cfi_ref cfi, bool do_cfi_asm, dw_fde_ref fde, bool for_eh)
3274 {
3275   struct dw_cfi_struct cfi_buf;
3276   dw_cfi_ref cfi2;
3277   dw_cfi_ref cfi_args_size = NULL, cfi_cfa = NULL, cfi_cfa_offset = NULL;
3278   VEC (dw_cfi_ref, heap) *regs = VEC_alloc (dw_cfi_ref, heap, 32);
3279   unsigned int len, idx;
3280
3281   for (;; cfi = cfi->dw_cfi_next)
3282     switch (cfi ? cfi->dw_cfi_opc : DW_CFA_nop)
3283       {
3284       case DW_CFA_advance_loc:
3285       case DW_CFA_advance_loc1:
3286       case DW_CFA_advance_loc2:
3287       case DW_CFA_advance_loc4:
3288       case DW_CFA_MIPS_advance_loc8:
3289       case DW_CFA_set_loc:
3290         /* All advances should be ignored.  */
3291         break;
3292       case DW_CFA_remember_state:
3293         {
3294           dw_cfi_ref args_size = cfi_args_size;
3295
3296           /* Skip everything between .cfi_remember_state and
3297              .cfi_restore_state.  */
3298           for (cfi2 = cfi->dw_cfi_next; cfi2; cfi2 = cfi2->dw_cfi_next)
3299             if (cfi2->dw_cfi_opc == DW_CFA_restore_state)
3300               break;
3301             else if (cfi2->dw_cfi_opc == DW_CFA_GNU_args_size)
3302               args_size = cfi2;
3303             else
3304               gcc_assert (cfi2->dw_cfi_opc != DW_CFA_remember_state);
3305
3306           if (cfi2 == NULL)
3307             goto flush_all;
3308           else
3309             {
3310               cfi = cfi2;
3311               cfi_args_size = args_size;
3312             }
3313           break;
3314         }
3315       case DW_CFA_GNU_args_size:
3316         cfi_args_size = cfi;
3317         break;
3318       case DW_CFA_GNU_window_save:
3319         goto flush_all;
3320       case DW_CFA_offset:
3321       case DW_CFA_offset_extended:
3322       case DW_CFA_offset_extended_sf:
3323       case DW_CFA_restore:
3324       case DW_CFA_restore_extended:
3325       case DW_CFA_undefined:
3326       case DW_CFA_same_value:
3327       case DW_CFA_register:
3328       case DW_CFA_val_offset:
3329       case DW_CFA_val_offset_sf:
3330       case DW_CFA_expression:
3331       case DW_CFA_val_expression:
3332       case DW_CFA_GNU_negative_offset_extended:
3333         if (VEC_length (dw_cfi_ref, regs) <= cfi->dw_cfi_oprnd1.dw_cfi_reg_num)
3334           VEC_safe_grow_cleared (dw_cfi_ref, heap, regs,
3335                                  cfi->dw_cfi_oprnd1.dw_cfi_reg_num + 1);
3336         VEC_replace (dw_cfi_ref, regs, cfi->dw_cfi_oprnd1.dw_cfi_reg_num, cfi);
3337         break;
3338       case DW_CFA_def_cfa:
3339       case DW_CFA_def_cfa_sf:
3340       case DW_CFA_def_cfa_expression:
3341         cfi_cfa = cfi;
3342         cfi_cfa_offset = cfi;
3343         break;
3344       case DW_CFA_def_cfa_register:
3345         cfi_cfa = cfi;
3346         break;
3347       case DW_CFA_def_cfa_offset:
3348       case DW_CFA_def_cfa_offset_sf:
3349         cfi_cfa_offset = cfi;
3350         break;
3351       case DW_CFA_nop:
3352         gcc_assert (cfi == NULL);
3353       flush_all:
3354         len = VEC_length (dw_cfi_ref, regs);
3355         for (idx = 0; idx < len; idx++)
3356           {
3357             cfi2 = VEC_replace (dw_cfi_ref, regs, idx, NULL);
3358             if (cfi2 != NULL
3359                 && cfi2->dw_cfi_opc != DW_CFA_restore
3360                 && cfi2->dw_cfi_opc != DW_CFA_restore_extended)
3361               {
3362                 if (do_cfi_asm)
3363                   output_cfi_directive (cfi2);
3364                 else
3365                   output_cfi (cfi2, fde, for_eh);
3366               }
3367           }
3368         if (cfi_cfa && cfi_cfa_offset && cfi_cfa_offset != cfi_cfa)
3369           {
3370             gcc_assert (cfi_cfa->dw_cfi_opc != DW_CFA_def_cfa_expression);
3371             cfi_buf = *cfi_cfa;
3372             switch (cfi_cfa_offset->dw_cfi_opc)
3373               {
3374               case DW_CFA_def_cfa_offset:
3375                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa;
3376                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3377                 break;
3378               case DW_CFA_def_cfa_offset_sf:
3379                 cfi_buf.dw_cfi_opc = DW_CFA_def_cfa_sf;
3380                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd1;
3381                 break;
3382               case DW_CFA_def_cfa:
3383               case DW_CFA_def_cfa_sf:
3384                 cfi_buf.dw_cfi_opc = cfi_cfa_offset->dw_cfi_opc;
3385                 cfi_buf.dw_cfi_oprnd2 = cfi_cfa_offset->dw_cfi_oprnd2;
3386                 break;
3387               default:
3388                 gcc_unreachable ();
3389               }
3390             cfi_cfa = &cfi_buf;
3391           }
3392         else if (cfi_cfa_offset)
3393           cfi_cfa = cfi_cfa_offset;
3394         if (cfi_cfa)
3395           {
3396             if (do_cfi_asm)
3397               output_cfi_directive (cfi_cfa);
3398             else
3399               output_cfi (cfi_cfa, fde, for_eh);
3400           }
3401         cfi_cfa = NULL;
3402         cfi_cfa_offset = NULL;
3403         if (cfi_args_size
3404             && cfi_args_size->dw_cfi_oprnd1.dw_cfi_offset)
3405           {
3406             if (do_cfi_asm)
3407               output_cfi_directive (cfi_args_size);
3408             else
3409               output_cfi (cfi_args_size, fde, for_eh);
3410           }
3411         cfi_args_size = NULL;
3412         if (cfi == NULL)
3413           {
3414             VEC_free (dw_cfi_ref, heap, regs);
3415             return;
3416           }
3417         else if (do_cfi_asm)
3418           output_cfi_directive (cfi);
3419         else
3420           output_cfi (cfi, fde, for_eh);
3421         break;
3422       default:
3423         gcc_unreachable ();
3424     }
3425 }
3426
3427 /* Output one FDE.  */
3428
3429 static void
3430 output_fde (dw_fde_ref fde, bool for_eh, bool second,
3431             char *section_start_label, int fde_encoding, char *augmentation,
3432             bool any_lsda_needed, int lsda_encoding)
3433 {
3434   const char *begin, *end;
3435   static unsigned int j;
3436   char l1[20], l2[20];
3437   dw_cfi_ref cfi;
3438
3439   targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh,
3440                                 /* empty */ 0);
3441   targetm.asm_out.internal_label (asm_out_file, FDE_LABEL,
3442                                   for_eh + j);
3443   ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + j);
3444   ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + j);
3445   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3446     dw2_asm_output_data (4, 0xffffffff, "Initial length escape value"
3447                          " indicating 64-bit DWARF extension");
3448   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3449                         "FDE Length");
3450   ASM_OUTPUT_LABEL (asm_out_file, l1);
3451
3452   if (for_eh)
3453     dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
3454   else
3455     dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
3456                            debug_frame_section, "FDE CIE offset");
3457
3458   if (!fde->dw_fde_switched_sections)
3459     {
3460       begin = fde->dw_fde_begin;
3461       end = fde->dw_fde_end;
3462     }
3463   else
3464     {
3465       /* For the first section, prefer dw_fde_begin over
3466          dw_fde_{hot,cold}_section_label, as the latter
3467          might be separated from the real start of the
3468          function by alignment padding.  */
3469       if (!second)
3470         begin = fde->dw_fde_begin;
3471       else if (fde->dw_fde_switched_cold_to_hot)
3472         begin = fde->dw_fde_hot_section_label;
3473       else
3474         begin = fde->dw_fde_unlikely_section_label;
3475       if (second ^ fde->dw_fde_switched_cold_to_hot)
3476         end = fde->dw_fde_unlikely_section_end_label;
3477       else
3478         end = fde->dw_fde_hot_section_end_label;
3479     }
3480
3481   if (for_eh)
3482     {
3483       rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, begin);
3484       SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
3485       dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref, false,
3486                                        "FDE initial location");
3487       dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
3488                             end, begin, "FDE address range");
3489     }
3490   else
3491     {
3492       dw2_asm_output_addr (DWARF2_ADDR_SIZE, begin, "FDE initial location");
3493       dw2_asm_output_delta (DWARF2_ADDR_SIZE, end, begin, "FDE address range");
3494     }
3495
3496   if (augmentation[0])
3497     {
3498       if (any_lsda_needed)
3499         {
3500           int size = size_of_encoded_value (lsda_encoding);
3501
3502           if (lsda_encoding == DW_EH_PE_aligned)
3503             {
3504               int offset = (  4         /* Length */
3505                             + 4         /* CIE offset */
3506                             + 2 * size_of_encoded_value (fde_encoding)
3507                             + 1         /* Augmentation size */ );
3508               int pad = -offset & (PTR_SIZE - 1);
3509
3510               size += pad;
3511               gcc_assert (size_of_uleb128 (size) == 1);
3512             }
3513
3514           dw2_asm_output_data_uleb128 (size, "Augmentation size");
3515
3516           if (fde->uses_eh_lsda)
3517             {
3518               ASM_GENERATE_INTERNAL_LABEL (l1, second ? "LLSDAC" : "LLSDA",
3519                                            fde->funcdef_number);
3520               dw2_asm_output_encoded_addr_rtx (lsda_encoding,
3521                                                gen_rtx_SYMBOL_REF (Pmode, l1),
3522                                                false,
3523                                                "Language Specific Data Area");
3524             }
3525           else
3526             {
3527               if (lsda_encoding == DW_EH_PE_aligned)
3528                 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
3529               dw2_asm_output_data (size_of_encoded_value (lsda_encoding), 0,
3530                                    "Language Specific Data Area (none)");
3531             }
3532         }
3533       else
3534         dw2_asm_output_data_uleb128 (0, "Augmentation size");
3535     }
3536
3537   /* Loop through the Call Frame Instructions associated with
3538      this FDE.  */
3539   fde->dw_fde_current_label = begin;
3540   if (!fde->dw_fde_switched_sections)
3541     for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3542       output_cfi (cfi, fde, for_eh);
3543   else if (!second)
3544     {
3545       if (fde->dw_fde_switch_cfi)
3546         for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
3547           {
3548             output_cfi (cfi, fde, for_eh);
3549             if (cfi == fde->dw_fde_switch_cfi)
3550               break;
3551           }
3552     }
3553   else
3554     {
3555       dw_cfi_ref cfi_next = fde->dw_fde_cfi;
3556
3557       if (fde->dw_fde_switch_cfi)
3558         {
3559           cfi_next = fde->dw_fde_switch_cfi->dw_cfi_next;
3560           fde->dw_fde_switch_cfi->dw_cfi_next = NULL;
3561           output_cfis (fde->dw_fde_cfi, false, fde, for_eh);
3562           fde->dw_fde_switch_cfi->dw_cfi_next = cfi_next;
3563         }
3564       for (cfi = cfi_next; cfi != NULL; cfi = cfi->dw_cfi_next)
3565         output_cfi (cfi, fde, for_eh);
3566     }
3567
3568   /* If we are to emit a ref/link from function bodies to their frame tables,
3569      do it now.  This is typically performed to make sure that tables
3570      associated with functions are dragged with them and not discarded in
3571      garbage collecting links. We need to do this on a per function basis to
3572      cope with -ffunction-sections.  */
3573
3574 #ifdef ASM_OUTPUT_DWARF_TABLE_REF
3575   /* Switch to the function section, emit the ref to the tables, and
3576      switch *back* into the table section.  */
3577   switch_to_section (function_section (fde->decl));
3578   ASM_OUTPUT_DWARF_TABLE_REF (section_start_label);
3579   switch_to_frame_table_section (for_eh, true);
3580 #endif
3581
3582   /* Pad the FDE out to an address sized boundary.  */
3583   ASM_OUTPUT_ALIGN (asm_out_file,
3584                     floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
3585   ASM_OUTPUT_LABEL (asm_out_file, l2);
3586
3587   j += 2;
3588 }
3589
3590 /* Output the call frame information used to record information
3591    that relates to calculating the frame pointer, and records the
3592    location of saved registers.  */
3593
3594 static void
3595 output_call_frame_info (int for_eh)
3596 {
3597   unsigned int i;
3598   dw_fde_ref fde;
3599   dw_cfi_ref cfi;
3600   char l1[20], l2[20], section_start_label[20];
3601   bool any_lsda_needed = false;
3602   char augmentation[6];
3603   int augmentation_size;
3604   int fde_encoding = DW_EH_PE_absptr;
3605   int per_encoding = DW_EH_PE_absptr;
3606   int lsda_encoding = DW_EH_PE_absptr;
3607   int return_reg;
3608   rtx personality = NULL;
3609   int dw_cie_version;
3610
3611   /* Don't emit a CIE if there won't be any FDEs.  */
3612   if (fde_table_in_use == 0)
3613     return;
3614
3615   /* Nothing to do if the assembler's doing it all.  */
3616   if (dwarf2out_do_cfi_asm ())
3617     return;
3618
3619   /* If we make FDEs linkonce, we may have to emit an empty label for
3620      an FDE that wouldn't otherwise be emitted.  We want to avoid
3621      having an FDE kept around when the function it refers to is
3622      discarded.  Example where this matters: a primary function
3623      template in C++ requires EH information, but an explicit
3624      specialization doesn't.  */
3625   if (TARGET_USES_WEAK_UNWIND_INFO
3626       && ! flag_asynchronous_unwind_tables
3627       && flag_exceptions
3628       && for_eh)
3629     for (i = 0; i < fde_table_in_use; i++)
3630       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
3631           && !fde_table[i].uses_eh_lsda
3632           && ! DECL_WEAK (fde_table[i].decl))
3633         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
3634                                       for_eh, /* empty */ 1);
3635
3636   /* If we don't have any functions we'll want to unwind out of, don't
3637      emit any EH unwind information.  Note that if exceptions aren't
3638      enabled, we won't have collected nothrow information, and if we
3639      asked for asynchronous tables, we always want this info.  */
3640   if (for_eh)
3641     {
3642       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
3643
3644       for (i = 0; i < fde_table_in_use; i++)
3645         if (fde_table[i].uses_eh_lsda)
3646           any_eh_needed = any_lsda_needed = true;
3647         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3648           any_eh_needed = true;
3649         else if (! fde_table[i].nothrow
3650                  && ! fde_table[i].all_throwers_are_sibcalls)
3651           any_eh_needed = true;
3652
3653       if (! any_eh_needed)
3654         return;
3655     }
3656
3657   /* We're going to be generating comments, so turn on app.  */
3658   if (flag_debug_asm)
3659     app_enable ();
3660
3661   /* Switch to the proper frame section, first time.  */
3662   switch_to_frame_table_section (for_eh, false);
3663
3664   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
3665   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
3666
3667   /* Output the CIE.  */
3668   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
3669   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
3670   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
3671     dw2_asm_output_data (4, 0xffffffff,
3672       "Initial length escape value indicating 64-bit DWARF extension");
3673   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
3674                         "Length of Common Information Entry");
3675   ASM_OUTPUT_LABEL (asm_out_file, l1);
3676
3677   /* Now that the CIE pointer is PC-relative for EH,
3678      use 0 to identify the CIE.  */
3679   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
3680                        (for_eh ? 0 : DWARF_CIE_ID),
3681                        "CIE Identifier Tag");
3682
3683   /* Use the CIE version 3 for DWARF3; allow DWARF2 to continue to
3684      use CIE version 1, unless that would produce incorrect results
3685      due to overflowing the return register column.  */
3686   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
3687   dw_cie_version = 1;
3688   if (return_reg >= 256 || dwarf_version > 2)
3689     dw_cie_version = 3;
3690   dw2_asm_output_data (1, dw_cie_version, "CIE Version");
3691
3692   augmentation[0] = 0;
3693   augmentation_size = 0;
3694
3695   personality = current_unit_personality;
3696   if (for_eh)
3697     {
3698       char *p;
3699
3700       /* Augmentation:
3701          z      Indicates that a uleb128 is present to size the
3702                 augmentation section.
3703          L      Indicates the encoding (and thus presence) of
3704                 an LSDA pointer in the FDE augmentation.
3705          R      Indicates a non-default pointer encoding for
3706                 FDE code pointers.
3707          P      Indicates the presence of an encoding + language
3708                 personality routine in the CIE augmentation.  */
3709
3710       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
3711       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3712       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3713
3714       p = augmentation + 1;
3715       if (personality)
3716         {
3717           *p++ = 'P';
3718           augmentation_size += 1 + size_of_encoded_value (per_encoding);
3719           assemble_external_libcall (personality);
3720         }
3721       if (any_lsda_needed)
3722         {
3723           *p++ = 'L';
3724           augmentation_size += 1;
3725         }
3726       if (fde_encoding != DW_EH_PE_absptr)
3727         {
3728           *p++ = 'R';
3729           augmentation_size += 1;
3730         }
3731       if (p > augmentation + 1)
3732         {
3733           augmentation[0] = 'z';
3734           *p = '\0';
3735         }
3736
3737       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
3738       if (personality && per_encoding == DW_EH_PE_aligned)
3739         {
3740           int offset = (  4             /* Length */
3741                         + 4             /* CIE Id */
3742                         + 1             /* CIE version */
3743                         + strlen (augmentation) + 1     /* Augmentation */
3744                         + size_of_uleb128 (1)           /* Code alignment */
3745                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
3746                         + 1             /* RA column */
3747                         + 1             /* Augmentation size */
3748                         + 1             /* Personality encoding */ );
3749           int pad = -offset & (PTR_SIZE - 1);
3750
3751           augmentation_size += pad;
3752
3753           /* Augmentations should be small, so there's scarce need to
3754              iterate for a solution.  Die if we exceed one uleb128 byte.  */
3755           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
3756         }
3757     }
3758
3759   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
3760   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
3761   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
3762                                "CIE Data Alignment Factor");
3763
3764   if (dw_cie_version == 1)
3765     dw2_asm_output_data (1, return_reg, "CIE RA Column");
3766   else
3767     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
3768
3769   if (augmentation[0])
3770     {
3771       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
3772       if (personality)
3773         {
3774           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
3775                                eh_data_format_name (per_encoding));
3776           dw2_asm_output_encoded_addr_rtx (per_encoding,
3777                                            personality,
3778                                            true, NULL);
3779         }
3780
3781       if (any_lsda_needed)
3782         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
3783                              eh_data_format_name (lsda_encoding));
3784
3785       if (fde_encoding != DW_EH_PE_absptr)
3786         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
3787                              eh_data_format_name (fde_encoding));
3788     }
3789
3790   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
3791     output_cfi (cfi, NULL, for_eh);
3792
3793   /* Pad the CIE out to an address sized boundary.  */
3794   ASM_OUTPUT_ALIGN (asm_out_file,
3795                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
3796   ASM_OUTPUT_LABEL (asm_out_file, l2);
3797
3798   /* Loop through all of the FDE's.  */
3799   for (i = 0; i < fde_table_in_use; i++)
3800     {
3801       unsigned int k;
3802       fde = &fde_table[i];
3803
3804       /* Don't emit EH unwind info for leaf functions that don't need it.  */
3805       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
3806           && (fde->nothrow || fde->all_throwers_are_sibcalls)
3807           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
3808           && !fde->uses_eh_lsda)
3809         continue;
3810
3811       for (k = 0; k < (fde->dw_fde_switched_sections ? 2 : 1); k++)
3812         output_fde (fde, for_eh, k, section_start_label, fde_encoding,
3813                     augmentation, any_lsda_needed, lsda_encoding);
3814     }
3815
3816   if (for_eh && targetm.terminate_dw2_eh_frame_info)
3817     dw2_asm_output_data (4, 0, "End of Table");
3818 #ifdef MIPS_DEBUGGING_INFO
3819   /* Work around Irix 6 assembler bug whereby labels at the end of a section
3820      get a value of 0.  Putting .align 0 after the label fixes it.  */
3821   ASM_OUTPUT_ALIGN (asm_out_file, 0);
3822 #endif
3823
3824   /* Turn off app to make assembly quicker.  */
3825   if (flag_debug_asm)
3826     app_disable ();
3827 }
3828
3829 /* Emit .cfi_startproc and .cfi_personality/.cfi_lsda if needed.  */
3830
3831 static void
3832 dwarf2out_do_cfi_startproc (bool second)
3833 {
3834   int enc;
3835   rtx ref;
3836   rtx personality = get_personality_function (current_function_decl);
3837
3838   fprintf (asm_out_file, "\t.cfi_startproc\n");
3839
3840   if (personality)
3841     {
3842       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
3843       ref = personality;
3844
3845       /* ??? The GAS support isn't entirely consistent.  We have to
3846          handle indirect support ourselves, but PC-relative is done
3847          in the assembler.  Further, the assembler can't handle any
3848          of the weirder relocation types.  */
3849       if (enc & DW_EH_PE_indirect)
3850         ref = dw2_force_const_mem (ref, true);
3851
3852       fprintf (asm_out_file, "\t.cfi_personality 0x%x,", enc);
3853       output_addr_const (asm_out_file, ref);
3854       fputc ('\n', asm_out_file);
3855     }
3856
3857   if (crtl->uses_eh_lsda)
3858     {
3859       char lab[20];
3860
3861       enc = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
3862       ASM_GENERATE_INTERNAL_LABEL (lab, second ? "LLSDAC" : "LLSDA",
3863                                    current_function_funcdef_no);
3864       ref = gen_rtx_SYMBOL_REF (Pmode, lab);
3865       SYMBOL_REF_FLAGS (ref) = SYMBOL_FLAG_LOCAL;
3866
3867       if (enc & DW_EH_PE_indirect)
3868         ref = dw2_force_const_mem (ref, true);
3869
3870       fprintf (asm_out_file, "\t.cfi_lsda 0x%x,", enc);
3871       output_addr_const (asm_out_file, ref);
3872       fputc ('\n', asm_out_file);
3873     }
3874 }
3875
3876 /* Output a marker (i.e. a label) for the beginning of a function, before
3877    the prologue.  */
3878
3879 void
3880 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
3881                           const char *file ATTRIBUTE_UNUSED)
3882 {
3883   char label[MAX_ARTIFICIAL_LABEL_BYTES];
3884   char * dup_label;
3885   dw_fde_ref fde;
3886   section *fnsec;
3887
3888   current_function_func_begin_label = NULL;
3889
3890 #ifdef TARGET_UNWIND_INFO
3891   /* ??? current_function_func_begin_label is also used by except.c
3892      for call-site information.  We must emit this label if it might
3893      be used.  */
3894   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
3895       && ! dwarf2out_do_frame ())
3896     return;
3897 #else
3898   if (! dwarf2out_do_frame ())
3899     return;
3900 #endif
3901
3902   fnsec = function_section (current_function_decl);
3903   switch_to_section (fnsec);
3904   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
3905                                current_function_funcdef_no);
3906   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
3907                           current_function_funcdef_no);
3908   dup_label = xstrdup (label);
3909   current_function_func_begin_label = dup_label;
3910
3911 #ifdef TARGET_UNWIND_INFO
3912   /* We can elide the fde allocation if we're not emitting debug info.  */
3913   if (! dwarf2out_do_frame ())
3914     return;
3915 #endif
3916
3917   /* Expand the fde table if necessary.  */
3918   if (fde_table_in_use == fde_table_allocated)
3919     {
3920       fde_table_allocated += FDE_TABLE_INCREMENT;
3921       fde_table = GGC_RESIZEVEC (dw_fde_node, fde_table, fde_table_allocated);
3922       memset (fde_table + fde_table_in_use, 0,
3923               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
3924     }
3925
3926   /* Record the FDE associated with this function.  */
3927   current_funcdef_fde = fde_table_in_use;
3928
3929   /* Add the new FDE at the end of the fde_table.  */
3930   fde = &fde_table[fde_table_in_use++];
3931   fde->decl = current_function_decl;
3932   fde->dw_fde_begin = dup_label;
3933   fde->dw_fde_current_label = dup_label;
3934   fde->dw_fde_hot_section_label = NULL;
3935   fde->dw_fde_hot_section_end_label = NULL;
3936   fde->dw_fde_unlikely_section_label = NULL;
3937   fde->dw_fde_unlikely_section_end_label = NULL;
3938   fde->dw_fde_switched_sections = 0;
3939   fde->dw_fde_switched_cold_to_hot = 0;
3940   fde->dw_fde_end = NULL;
3941   fde->dw_fde_cfi = NULL;
3942   fde->dw_fde_switch_cfi = NULL;
3943   fde->funcdef_number = current_function_funcdef_no;
3944   fde->nothrow = crtl->nothrow;
3945   fde->uses_eh_lsda = crtl->uses_eh_lsda;
3946   fde->all_throwers_are_sibcalls = crtl->all_throwers_are_sibcalls;
3947   fde->drap_reg = INVALID_REGNUM;
3948   fde->vdrap_reg = INVALID_REGNUM;
3949   if (flag_reorder_blocks_and_partition)
3950     {
3951       section *unlikelysec;
3952       if (first_function_block_is_cold)
3953         fde->in_std_section = 1;
3954       else
3955         fde->in_std_section
3956           = (fnsec == text_section
3957              || (cold_text_section && fnsec == cold_text_section));
3958       unlikelysec = unlikely_text_section ();
3959       fde->cold_in_std_section
3960         = (unlikelysec == text_section
3961            || (cold_text_section && unlikelysec == cold_text_section));
3962     }
3963   else
3964     {
3965       fde->in_std_section
3966         = (fnsec == text_section
3967            || (cold_text_section && fnsec == cold_text_section));
3968       fde->cold_in_std_section = 0;
3969     }
3970
3971   args_size = old_args_size = 0;
3972
3973   /* We only want to output line number information for the genuine dwarf2
3974      prologue case, not the eh frame case.  */
3975 #ifdef DWARF2_DEBUGGING_INFO
3976   if (file)
3977     dwarf2out_source_line (line, file, 0, true);
3978 #endif
3979
3980   if (dwarf2out_do_cfi_asm ())
3981     dwarf2out_do_cfi_startproc (false);
3982   else
3983     {
3984       rtx personality = get_personality_function (current_function_decl);
3985       if (!current_unit_personality)
3986         current_unit_personality = personality;
3987
3988       /* We cannot keep a current personality per function as without CFI
3989          asm at the point where we emit the CFI data there is no current
3990          function anymore.  */
3991       if (personality
3992           && current_unit_personality != personality)
3993         sorry ("Multiple EH personalities are supported only with assemblers "
3994                "supporting .cfi.personality directive.");
3995     }
3996 }
3997
3998 /* Output a marker (i.e. a label) for the absolute end of the generated code
3999    for a function definition.  This gets called *after* the epilogue code has
4000    been generated.  */
4001
4002 void
4003 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
4004                         const char *file ATTRIBUTE_UNUSED)
4005 {
4006   dw_fde_ref fde;
4007   char label[MAX_ARTIFICIAL_LABEL_BYTES];
4008
4009 #ifdef DWARF2_DEBUGGING_INFO
4010   last_var_location_insn = NULL_RTX;
4011 #endif
4012
4013   if (dwarf2out_do_cfi_asm ())
4014     fprintf (asm_out_file, "\t.cfi_endproc\n");
4015
4016   /* Output a label to mark the endpoint of the code generated for this
4017      function.  */
4018   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
4019                                current_function_funcdef_no);
4020   ASM_OUTPUT_LABEL (asm_out_file, label);
4021   fde = current_fde ();
4022   gcc_assert (fde != NULL);
4023   fde->dw_fde_end = xstrdup (label);
4024 }
4025
4026 void
4027 dwarf2out_frame_init (void)
4028 {
4029   /* Allocate the initial hunk of the fde_table.  */
4030   fde_table = GGC_CNEWVEC (dw_fde_node, FDE_TABLE_INCREMENT);
4031   fde_table_allocated = FDE_TABLE_INCREMENT;
4032   fde_table_in_use = 0;
4033
4034   /* Generate the CFA instructions common to all FDE's.  Do it now for the
4035      sake of lookup_cfa.  */
4036
4037   /* On entry, the Canonical Frame Address is at SP.  */
4038   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
4039
4040 #ifdef DWARF2_UNWIND_INFO
4041   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
4042     initial_return_save (INCOMING_RETURN_ADDR_RTX);
4043 #endif
4044 }
4045
4046 void
4047 dwarf2out_frame_finish (void)
4048 {
4049   /* Output call frame information.  */
4050   if (DWARF2_FRAME_INFO)
4051     output_call_frame_info (0);
4052
4053 #ifndef TARGET_UNWIND_INFO
4054   /* Output another copy for the unwinder.  */
4055   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
4056     output_call_frame_info (1);
4057 #endif
4058 }
4059
4060 /* Note that the current function section is being used for code.  */
4061
4062 static void
4063 dwarf2out_note_section_used (void)
4064 {
4065   section *sec = current_function_section ();
4066   if (sec == text_section)
4067     text_section_used = true;
4068   else if (sec == cold_text_section)
4069     cold_text_section_used = true;
4070 }
4071
4072 void
4073 dwarf2out_switch_text_section (void)
4074 {
4075   dw_fde_ref fde = current_fde ();
4076
4077   gcc_assert (cfun && fde && !fde->dw_fde_switched_sections);
4078
4079   fde->dw_fde_switched_sections = 1;
4080   fde->dw_fde_switched_cold_to_hot = !in_cold_section_p;
4081
4082   fde->dw_fde_hot_section_label = crtl->subsections.hot_section_label;
4083   fde->dw_fde_hot_section_end_label = crtl->subsections.hot_section_end_label;
4084   fde->dw_fde_unlikely_section_label = crtl->subsections.cold_section_label;
4085   fde->dw_fde_unlikely_section_end_label = crtl->subsections.cold_section_end_label;
4086   have_multiple_function_sections = true;
4087
4088   /* Reset the current label on switching text sections, so that we
4089      don't attempt to advance_loc4 between labels in different sections.  */
4090   fde->dw_fde_current_label = NULL;
4091
4092   /* There is no need to mark used sections when not debugging.  */
4093   if (cold_text_section != NULL)
4094     dwarf2out_note_section_used ();
4095
4096   if (dwarf2out_do_cfi_asm ())
4097     fprintf (asm_out_file, "\t.cfi_endproc\n");
4098
4099   /* Now do the real section switch.  */
4100   switch_to_section (current_function_section ());
4101
4102   if (dwarf2out_do_cfi_asm ())
4103     {
4104       dwarf2out_do_cfi_startproc (true);
4105       /* As this is a different FDE, insert all current CFI instructions
4106          again.  */
4107       output_cfis (fde->dw_fde_cfi, true, fde, true);
4108     }
4109   else
4110     {
4111       dw_cfi_ref cfi = fde->dw_fde_cfi;
4112
4113       cfi = fde->dw_fde_cfi;
4114       if (cfi)
4115         while (cfi->dw_cfi_next != NULL)
4116           cfi = cfi->dw_cfi_next;
4117       fde->dw_fde_switch_cfi = cfi;
4118     }
4119 }
4120 #endif
4121 \f
4122 /* And now, the subset of the debugging information support code necessary
4123    for emitting location expressions.  */
4124
4125 /* Data about a single source file.  */
4126 struct GTY(()) dwarf_file_data {
4127   const char * filename;
4128   int emitted_number;
4129 };
4130
4131 typedef struct dw_val_struct *dw_val_ref;
4132 typedef struct die_struct *dw_die_ref;
4133 typedef const struct die_struct *const_dw_die_ref;
4134 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
4135 typedef struct dw_loc_list_struct *dw_loc_list_ref;
4136
4137 typedef struct GTY(()) deferred_locations_struct
4138 {
4139   tree variable;
4140   dw_die_ref die;
4141 } deferred_locations;
4142
4143 DEF_VEC_O(deferred_locations);
4144 DEF_VEC_ALLOC_O(deferred_locations,gc);
4145
4146 static GTY(()) VEC(deferred_locations, gc) *deferred_locations_list;
4147
4148 /* Each DIE may have a series of attribute/value pairs.  Values
4149    can take on several forms.  The forms that are used in this
4150    implementation are listed below.  */
4151
4152 enum dw_val_class
4153 {
4154   dw_val_class_addr,
4155   dw_val_class_offset,
4156   dw_val_class_loc,
4157   dw_val_class_loc_list,
4158   dw_val_class_range_list,
4159   dw_val_class_const,
4160   dw_val_class_unsigned_const,
4161   dw_val_class_const_double,
4162   dw_val_class_vec,
4163   dw_val_class_flag,
4164   dw_val_class_die_ref,
4165   dw_val_class_fde_ref,
4166   dw_val_class_lbl_id,
4167   dw_val_class_lineptr,
4168   dw_val_class_str,
4169   dw_val_class_macptr,
4170   dw_val_class_file
4171 };
4172
4173 /* Describe a floating point constant value, or a vector constant value.  */
4174
4175 typedef struct GTY(()) dw_vec_struct {
4176   unsigned char * GTY((length ("%h.length"))) array;
4177   unsigned length;
4178   unsigned elt_size;
4179 }
4180 dw_vec_const;
4181
4182 /* The dw_val_node describes an attribute's value, as it is
4183    represented internally.  */
4184
4185 typedef struct GTY(()) dw_val_struct {
4186   enum dw_val_class val_class;
4187   union dw_val_struct_union
4188     {
4189       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
4190       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
4191       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
4192       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
4193       HOST_WIDE_INT GTY ((default)) val_int;
4194       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
4195       double_int GTY ((tag ("dw_val_class_const_double"))) val_double;
4196       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
4197       struct dw_val_die_union
4198         {
4199           dw_die_ref die;
4200           int external;
4201         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
4202       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
4203       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
4204       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
4205       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
4206       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
4207     }
4208   GTY ((desc ("%1.val_class"))) v;
4209 }
4210 dw_val_node;
4211
4212 /* Locations in memory are described using a sequence of stack machine
4213    operations.  */
4214
4215 typedef struct GTY(()) dw_loc_descr_struct {
4216   dw_loc_descr_ref dw_loc_next;
4217   ENUM_BITFIELD (dwarf_location_atom) dw_loc_opc : 8;
4218   /* Used to distinguish DW_OP_addr with a direct symbol relocation
4219      from DW_OP_addr with a dtp-relative symbol relocation.  */
4220   unsigned int dtprel : 1;
4221   int dw_loc_addr;
4222   dw_val_node dw_loc_oprnd1;
4223   dw_val_node dw_loc_oprnd2;
4224 }
4225 dw_loc_descr_node;
4226
4227 /* Location lists are ranges + location descriptions for that range,
4228    so you can track variables that are in different places over
4229    their entire life.  */
4230 typedef struct GTY(()) dw_loc_list_struct {
4231   dw_loc_list_ref dw_loc_next;
4232   const char *begin; /* Label for begin address of range */
4233   const char *end;  /* Label for end address of range */
4234   char *ll_symbol; /* Label for beginning of location list.
4235                       Only on head of list */
4236   const char *section; /* Section this loclist is relative to */
4237   dw_loc_descr_ref expr;
4238 } dw_loc_list_node;
4239
4240 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
4241
4242 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4243
4244 /* Convert a DWARF stack opcode into its string name.  */
4245
4246 static const char *
4247 dwarf_stack_op_name (unsigned int op)
4248 {
4249   switch (op)
4250     {
4251     case DW_OP_addr:
4252       return "DW_OP_addr";
4253     case DW_OP_deref:
4254       return "DW_OP_deref";
4255     case DW_OP_const1u:
4256       return "DW_OP_const1u";
4257     case DW_OP_const1s:
4258       return "DW_OP_const1s";
4259     case DW_OP_const2u:
4260       return "DW_OP_const2u";
4261     case DW_OP_const2s:
4262       return "DW_OP_const2s";
4263     case DW_OP_const4u:
4264       return "DW_OP_const4u";
4265     case DW_OP_const4s:
4266       return "DW_OP_const4s";
4267     case DW_OP_const8u:
4268       return "DW_OP_const8u";
4269     case DW_OP_const8s:
4270       return "DW_OP_const8s";
4271     case DW_OP_constu:
4272       return "DW_OP_constu";
4273     case DW_OP_consts:
4274       return "DW_OP_consts";
4275     case DW_OP_dup:
4276       return "DW_OP_dup";
4277     case DW_OP_drop:
4278       return "DW_OP_drop";
4279     case DW_OP_over:
4280       return "DW_OP_over";
4281     case DW_OP_pick:
4282       return "DW_OP_pick";
4283     case DW_OP_swap:
4284       return "DW_OP_swap";
4285     case DW_OP_rot:
4286       return "DW_OP_rot";
4287     case DW_OP_xderef:
4288       return "DW_OP_xderef";
4289     case DW_OP_abs:
4290       return "DW_OP_abs";
4291     case DW_OP_and:
4292       return "DW_OP_and";
4293     case DW_OP_div:
4294       return "DW_OP_div";
4295     case DW_OP_minus:
4296       return "DW_OP_minus";
4297     case DW_OP_mod:
4298       return "DW_OP_mod";
4299     case DW_OP_mul:
4300       return "DW_OP_mul";
4301     case DW_OP_neg:
4302       return "DW_OP_neg";
4303     case DW_OP_not:
4304       return "DW_OP_not";
4305     case DW_OP_or:
4306       return "DW_OP_or";
4307     case DW_OP_plus:
4308       return "DW_OP_plus";
4309     case DW_OP_plus_uconst:
4310       return "DW_OP_plus_uconst";
4311     case DW_OP_shl:
4312       return "DW_OP_shl";
4313     case DW_OP_shr:
4314       return "DW_OP_shr";
4315     case DW_OP_shra:
4316       return "DW_OP_shra";
4317     case DW_OP_xor:
4318       return "DW_OP_xor";
4319     case DW_OP_bra:
4320       return "DW_OP_bra";
4321     case DW_OP_eq:
4322       return "DW_OP_eq";
4323     case DW_OP_ge:
4324       return "DW_OP_ge";
4325     case DW_OP_gt:
4326       return "DW_OP_gt";
4327     case DW_OP_le:
4328       return "DW_OP_le";
4329     case DW_OP_lt:
4330       return "DW_OP_lt";
4331     case DW_OP_ne:
4332       return "DW_OP_ne";
4333     case DW_OP_skip:
4334       return "DW_OP_skip";
4335     case DW_OP_lit0:
4336       return "DW_OP_lit0";
4337     case DW_OP_lit1:
4338       return "DW_OP_lit1";
4339     case DW_OP_lit2:
4340       return "DW_OP_lit2";
4341     case DW_OP_lit3:
4342       return "DW_OP_lit3";
4343     case DW_OP_lit4:
4344       return "DW_OP_lit4";
4345     case DW_OP_lit5:
4346       return "DW_OP_lit5";
4347     case DW_OP_lit6:
4348       return "DW_OP_lit6";
4349     case DW_OP_lit7:
4350       return "DW_OP_lit7";
4351     case DW_OP_lit8:
4352       return "DW_OP_lit8";
4353     case DW_OP_lit9:
4354       return "DW_OP_lit9";
4355     case DW_OP_lit10:
4356       return "DW_OP_lit10";
4357     case DW_OP_lit11:
4358       return "DW_OP_lit11";
4359     case DW_OP_lit12:
4360       return "DW_OP_lit12";
4361     case DW_OP_lit13:
4362       return "DW_OP_lit13";
4363     case DW_OP_lit14:
4364       return "DW_OP_lit14";
4365     case DW_OP_lit15:
4366       return "DW_OP_lit15";
4367     case DW_OP_lit16:
4368       return "DW_OP_lit16";
4369     case DW_OP_lit17:
4370       return "DW_OP_lit17";
4371     case DW_OP_lit18:
4372       return "DW_OP_lit18";
4373     case DW_OP_lit19:
4374       return "DW_OP_lit19";
4375     case DW_OP_lit20:
4376       return "DW_OP_lit20";
4377     case DW_OP_lit21:
4378       return "DW_OP_lit21";
4379     case DW_OP_lit22:
4380       return "DW_OP_lit22";
4381     case DW_OP_lit23:
4382       return "DW_OP_lit23";
4383     case DW_OP_lit24:
4384       return "DW_OP_lit24";
4385     case DW_OP_lit25:
4386       return "DW_OP_lit25";
4387     case DW_OP_lit26:
4388       return "DW_OP_lit26";
4389     case DW_OP_lit27:
4390       return "DW_OP_lit27";
4391     case DW_OP_lit28:
4392       return "DW_OP_lit28";
4393     case DW_OP_lit29:
4394       return "DW_OP_lit29";
4395     case DW_OP_lit30:
4396       return "DW_OP_lit30";
4397     case DW_OP_lit31:
4398       return "DW_OP_lit31";
4399     case DW_OP_reg0:
4400       return "DW_OP_reg0";
4401     case DW_OP_reg1:
4402       return "DW_OP_reg1";
4403     case DW_OP_reg2:
4404       return "DW_OP_reg2";
4405     case DW_OP_reg3:
4406       return "DW_OP_reg3";
4407     case DW_OP_reg4:
4408       return "DW_OP_reg4";
4409     case DW_OP_reg5:
4410       return "DW_OP_reg5";
4411     case DW_OP_reg6:
4412       return "DW_OP_reg6";
4413     case DW_OP_reg7:
4414       return "DW_OP_reg7";
4415     case DW_OP_reg8:
4416       return "DW_OP_reg8";
4417     case DW_OP_reg9:
4418       return "DW_OP_reg9";
4419     case DW_OP_reg10:
4420       return "DW_OP_reg10";
4421     case DW_OP_reg11:
4422       return "DW_OP_reg11";
4423     case DW_OP_reg12:
4424       return "DW_OP_reg12";
4425     case DW_OP_reg13:
4426       return "DW_OP_reg13";
4427     case DW_OP_reg14:
4428       return "DW_OP_reg14";
4429     case DW_OP_reg15:
4430       return "DW_OP_reg15";
4431     case DW_OP_reg16:
4432       return "DW_OP_reg16";
4433     case DW_OP_reg17:
4434       return "DW_OP_reg17";
4435     case DW_OP_reg18:
4436       return "DW_OP_reg18";
4437     case DW_OP_reg19:
4438       return "DW_OP_reg19";
4439     case DW_OP_reg20:
4440       return "DW_OP_reg20";
4441     case DW_OP_reg21:
4442       return "DW_OP_reg21";
4443     case DW_OP_reg22:
4444       return "DW_OP_reg22";
4445     case DW_OP_reg23:
4446       return "DW_OP_reg23";
4447     case DW_OP_reg24:
4448       return "DW_OP_reg24";
4449     case DW_OP_reg25:
4450       return "DW_OP_reg25";
4451     case DW_OP_reg26:
4452       return "DW_OP_reg26";
4453     case DW_OP_reg27:
4454       return "DW_OP_reg27";
4455     case DW_OP_reg28:
4456       return "DW_OP_reg28";
4457     case DW_OP_reg29:
4458       return "DW_OP_reg29";
4459     case DW_OP_reg30:
4460       return "DW_OP_reg30";
4461     case DW_OP_reg31:
4462       return "DW_OP_reg31";
4463     case DW_OP_breg0:
4464       return "DW_OP_breg0";
4465     case DW_OP_breg1:
4466       return "DW_OP_breg1";
4467     case DW_OP_breg2:
4468       return "DW_OP_breg2";
4469     case DW_OP_breg3:
4470       return "DW_OP_breg3";
4471     case DW_OP_breg4:
4472       return "DW_OP_breg4";
4473     case DW_OP_breg5:
4474       return "DW_OP_breg5";
4475     case DW_OP_breg6:
4476       return "DW_OP_breg6";
4477     case DW_OP_breg7:
4478       return "DW_OP_breg7";
4479     case DW_OP_breg8:
4480       return "DW_OP_breg8";
4481     case DW_OP_breg9:
4482       return "DW_OP_breg9";
4483     case DW_OP_breg10:
4484       return "DW_OP_breg10";
4485     case DW_OP_breg11:
4486       return "DW_OP_breg11";
4487     case DW_OP_breg12:
4488       return "DW_OP_breg12";
4489     case DW_OP_breg13:
4490       return "DW_OP_breg13";
4491     case DW_OP_breg14:
4492       return "DW_OP_breg14";
4493     case DW_OP_breg15:
4494       return "DW_OP_breg15";
4495     case DW_OP_breg16:
4496       return "DW_OP_breg16";
4497     case DW_OP_breg17:
4498       return "DW_OP_breg17";
4499     case DW_OP_breg18:
4500       return "DW_OP_breg18";
4501     case DW_OP_breg19:
4502       return "DW_OP_breg19";
4503     case DW_OP_breg20:
4504       return "DW_OP_breg20";
4505     case DW_OP_breg21:
4506       return "DW_OP_breg21";
4507     case DW_OP_breg22:
4508       return "DW_OP_breg22";
4509     case DW_OP_breg23:
4510       return "DW_OP_breg23";
4511     case DW_OP_breg24:
4512       return "DW_OP_breg24";
4513     case DW_OP_breg25:
4514       return "DW_OP_breg25";
4515     case DW_OP_breg26:
4516       return "DW_OP_breg26";
4517     case DW_OP_breg27:
4518       return "DW_OP_breg27";
4519     case DW_OP_breg28:
4520       return "DW_OP_breg28";
4521     case DW_OP_breg29:
4522       return "DW_OP_breg29";
4523     case DW_OP_breg30:
4524       return "DW_OP_breg30";
4525     case DW_OP_breg31:
4526       return "DW_OP_breg31";
4527     case DW_OP_regx:
4528       return "DW_OP_regx";
4529     case DW_OP_fbreg:
4530       return "DW_OP_fbreg";
4531     case DW_OP_bregx:
4532       return "DW_OP_bregx";
4533     case DW_OP_piece:
4534       return "DW_OP_piece";
4535     case DW_OP_deref_size:
4536       return "DW_OP_deref_size";
4537     case DW_OP_xderef_size:
4538       return "DW_OP_xderef_size";
4539     case DW_OP_nop:
4540       return "DW_OP_nop";
4541
4542     case DW_OP_push_object_address:
4543       return "DW_OP_push_object_address";
4544     case DW_OP_call2:
4545       return "DW_OP_call2";
4546     case DW_OP_call4:
4547       return "DW_OP_call4";
4548     case DW_OP_call_ref:
4549       return "DW_OP_call_ref";
4550     case DW_OP_implicit_value:
4551       return "DW_OP_implicit_value";
4552     case DW_OP_stack_value:
4553       return "DW_OP_stack_value";
4554     case DW_OP_form_tls_address:
4555       return "DW_OP_form_tls_address";
4556     case DW_OP_call_frame_cfa:
4557       return "DW_OP_call_frame_cfa";
4558     case DW_OP_bit_piece:
4559       return "DW_OP_bit_piece";
4560
4561     case DW_OP_GNU_push_tls_address:
4562       return "DW_OP_GNU_push_tls_address";
4563     case DW_OP_GNU_uninit:
4564       return "DW_OP_GNU_uninit";
4565     case DW_OP_GNU_encoded_addr:
4566       return "DW_OP_GNU_encoded_addr";
4567
4568     default:
4569       return "OP_<unknown>";
4570     }
4571 }
4572
4573 /* Return a pointer to a newly allocated location description.  Location
4574    descriptions are simple expression terms that can be strung
4575    together to form more complicated location (address) descriptions.  */
4576
4577 static inline dw_loc_descr_ref
4578 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
4579                unsigned HOST_WIDE_INT oprnd2)
4580 {
4581   dw_loc_descr_ref descr = GGC_CNEW (dw_loc_descr_node);
4582
4583   descr->dw_loc_opc = op;
4584   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
4585   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
4586   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
4587   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
4588
4589   return descr;
4590 }
4591
4592 /* Return a pointer to a newly allocated location description for
4593    REG and OFFSET.  */
4594
4595 static inline dw_loc_descr_ref
4596 new_reg_loc_descr (unsigned int reg,  unsigned HOST_WIDE_INT offset)
4597 {
4598   if (reg <= 31)
4599     return new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + reg),
4600                           offset, 0);
4601   else
4602     return new_loc_descr (DW_OP_bregx, reg, offset);
4603 }
4604
4605 /* Add a location description term to a location description expression.  */
4606
4607 static inline void
4608 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
4609 {
4610   dw_loc_descr_ref *d;
4611
4612   /* Find the end of the chain.  */
4613   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
4614     ;
4615
4616   *d = descr;
4617 }
4618
4619 /* Add a constant OFFSET to a location expression.  */
4620
4621 static void
4622 loc_descr_plus_const (dw_loc_descr_ref *list_head, HOST_WIDE_INT offset)
4623 {
4624   dw_loc_descr_ref loc;
4625   HOST_WIDE_INT *p;
4626
4627   gcc_assert (*list_head != NULL);
4628
4629   if (!offset)
4630     return;
4631
4632   /* Find the end of the chain.  */
4633   for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
4634     ;
4635
4636   p = NULL;
4637   if (loc->dw_loc_opc == DW_OP_fbreg
4638       || (loc->dw_loc_opc >= DW_OP_breg0 && loc->dw_loc_opc <= DW_OP_breg31))
4639     p = &loc->dw_loc_oprnd1.v.val_int;
4640   else if (loc->dw_loc_opc == DW_OP_bregx)
4641     p = &loc->dw_loc_oprnd2.v.val_int;
4642
4643   /* If the last operation is fbreg, breg{0..31,x}, optimize by adjusting its
4644      offset.  Don't optimize if an signed integer overflow would happen.  */
4645   if (p != NULL
4646       && ((offset > 0 && *p <= INTTYPE_MAXIMUM (HOST_WIDE_INT) - offset)
4647           || (offset < 0 && *p >= INTTYPE_MINIMUM (HOST_WIDE_INT) - offset)))
4648     *p += offset;
4649
4650   else if (offset > 0)
4651     loc->dw_loc_next = new_loc_descr (DW_OP_plus_uconst, offset, 0);
4652
4653   else
4654     {
4655       loc->dw_loc_next = int_loc_descriptor (offset);
4656       add_loc_descr (&loc->dw_loc_next, new_loc_descr (DW_OP_plus, 0, 0));
4657     }
4658 }
4659
4660 #ifdef DWARF2_DEBUGGING_INFO
4661 /* Add a constant OFFSET to a location list.  */
4662
4663 static void
4664 loc_list_plus_const (dw_loc_list_ref list_head, HOST_WIDE_INT offset)
4665 {
4666   dw_loc_list_ref d;
4667   for (d = list_head; d != NULL; d = d->dw_loc_next)
4668     loc_descr_plus_const (&d->expr, offset);
4669 }
4670 #endif
4671
4672 /* Return the size of a location descriptor.  */
4673
4674 static unsigned long
4675 size_of_loc_descr (dw_loc_descr_ref loc)
4676 {
4677   unsigned long size = 1;
4678
4679   switch (loc->dw_loc_opc)
4680     {
4681     case DW_OP_addr:
4682       size += DWARF2_ADDR_SIZE;
4683       break;
4684     case DW_OP_const1u:
4685     case DW_OP_const1s:
4686       size += 1;
4687       break;
4688     case DW_OP_const2u:
4689     case DW_OP_const2s:
4690       size += 2;
4691       break;
4692     case DW_OP_const4u:
4693     case DW_OP_const4s:
4694       size += 4;
4695       break;
4696     case DW_OP_const8u:
4697     case DW_OP_const8s:
4698       size += 8;
4699       break;
4700     case DW_OP_constu:
4701       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4702       break;
4703     case DW_OP_consts:
4704       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4705       break;
4706     case DW_OP_pick:
4707       size += 1;
4708       break;
4709     case DW_OP_plus_uconst:
4710       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4711       break;
4712     case DW_OP_skip:
4713     case DW_OP_bra:
4714       size += 2;
4715       break;
4716     case DW_OP_breg0:
4717     case DW_OP_breg1:
4718     case DW_OP_breg2:
4719     case DW_OP_breg3:
4720     case DW_OP_breg4:
4721     case DW_OP_breg5:
4722     case DW_OP_breg6:
4723     case DW_OP_breg7:
4724     case DW_OP_breg8:
4725     case DW_OP_breg9:
4726     case DW_OP_breg10:
4727     case DW_OP_breg11:
4728     case DW_OP_breg12:
4729     case DW_OP_breg13:
4730     case DW_OP_breg14:
4731     case DW_OP_breg15:
4732     case DW_OP_breg16:
4733     case DW_OP_breg17:
4734     case DW_OP_breg18:
4735     case DW_OP_breg19:
4736     case DW_OP_breg20:
4737     case DW_OP_breg21:
4738     case DW_OP_breg22:
4739     case DW_OP_breg23:
4740     case DW_OP_breg24:
4741     case DW_OP_breg25:
4742     case DW_OP_breg26:
4743     case DW_OP_breg27:
4744     case DW_OP_breg28:
4745     case DW_OP_breg29:
4746     case DW_OP_breg30:
4747     case DW_OP_breg31:
4748       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4749       break;
4750     case DW_OP_regx:
4751       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4752       break;
4753     case DW_OP_fbreg:
4754       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
4755       break;
4756     case DW_OP_bregx:
4757       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4758       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
4759       break;
4760     case DW_OP_piece:
4761       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
4762       break;
4763     case DW_OP_deref_size:
4764     case DW_OP_xderef_size:
4765       size += 1;
4766       break;
4767     case DW_OP_call2:
4768       size += 2;
4769       break;
4770     case DW_OP_call4:
4771       size += 4;
4772       break;
4773     case DW_OP_call_ref:
4774       size += DWARF2_ADDR_SIZE;
4775       break;
4776     case DW_OP_implicit_value:
4777       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned)
4778               + loc->dw_loc_oprnd1.v.val_unsigned;
4779       break;
4780     default:
4781       break;
4782     }
4783
4784   return size;
4785 }
4786
4787 /* Return the size of a series of location descriptors.  */
4788
4789 static unsigned long
4790 size_of_locs (dw_loc_descr_ref loc)
4791 {
4792   dw_loc_descr_ref l;
4793   unsigned long size;
4794
4795   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
4796      field, to avoid writing to a PCH file.  */
4797   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4798     {
4799       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
4800         break;
4801       size += size_of_loc_descr (l);
4802     }
4803   if (! l)
4804     return size;
4805
4806   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
4807     {
4808       l->dw_loc_addr = size;
4809       size += size_of_loc_descr (l);
4810     }
4811
4812   return size;
4813 }
4814
4815 #ifdef DWARF2_DEBUGGING_INFO
4816 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4817 #endif
4818
4819 /* Output location description stack opcode's operands (if any).  */
4820
4821 static void
4822 output_loc_operands (dw_loc_descr_ref loc)
4823 {
4824   dw_val_ref val1 = &loc->dw_loc_oprnd1;
4825   dw_val_ref val2 = &loc->dw_loc_oprnd2;
4826
4827   switch (loc->dw_loc_opc)
4828     {
4829 #ifdef DWARF2_DEBUGGING_INFO
4830     case DW_OP_const2u:
4831     case DW_OP_const2s:
4832       dw2_asm_output_data (2, val1->v.val_int, NULL);
4833       break;
4834     case DW_OP_const4u:
4835     case DW_OP_const4s:
4836       dw2_asm_output_data (4, val1->v.val_int, NULL);
4837       break;
4838     case DW_OP_const8u:
4839     case DW_OP_const8s:
4840       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
4841       dw2_asm_output_data (8, val1->v.val_int, NULL);
4842       break;
4843     case DW_OP_skip:
4844     case DW_OP_bra:
4845       {
4846         int offset;
4847
4848         gcc_assert (val1->val_class == dw_val_class_loc);
4849         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
4850
4851         dw2_asm_output_data (2, offset, NULL);
4852       }
4853       break;
4854     case DW_OP_implicit_value:
4855       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4856       switch (val2->val_class)
4857         {
4858         case dw_val_class_const:
4859           dw2_asm_output_data (val1->v.val_unsigned, val2->v.val_int, NULL);
4860           break;
4861         case dw_val_class_vec:
4862           {
4863             unsigned int elt_size = val2->v.val_vec.elt_size;
4864             unsigned int len = val2->v.val_vec.length;
4865             unsigned int i;
4866             unsigned char *p;
4867
4868             if (elt_size > sizeof (HOST_WIDE_INT))
4869               {
4870                 elt_size /= 2;
4871                 len *= 2;
4872               }
4873             for (i = 0, p = val2->v.val_vec.array;
4874                  i < len;
4875                  i++, p += elt_size)
4876               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
4877                                    "fp or vector constant word %u", i);
4878           }
4879           break;
4880         case dw_val_class_const_double:
4881           {
4882             unsigned HOST_WIDE_INT first, second;
4883
4884             if (WORDS_BIG_ENDIAN)
4885               {
4886                 first = val2->v.val_double.high;
4887                 second = val2->v.val_double.low;
4888               }
4889             else
4890               {
4891                 first = val2->v.val_double.low;
4892                 second = val2->v.val_double.high;
4893               }
4894             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4895                                  first, NULL);
4896             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
4897                                  second, NULL);
4898           }
4899           break;
4900         case dw_val_class_addr:
4901           gcc_assert (val1->v.val_unsigned == DWARF2_ADDR_SIZE);
4902           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val2->v.val_addr, NULL);
4903           break;
4904         default:
4905           gcc_unreachable ();
4906         }
4907       break;
4908 #else
4909     case DW_OP_const2u:
4910     case DW_OP_const2s:
4911     case DW_OP_const4u:
4912     case DW_OP_const4s:
4913     case DW_OP_const8u:
4914     case DW_OP_const8s:
4915     case DW_OP_skip:
4916     case DW_OP_bra:
4917     case DW_OP_implicit_value:
4918       /* We currently don't make any attempt to make sure these are
4919          aligned properly like we do for the main unwind info, so
4920          don't support emitting things larger than a byte if we're
4921          only doing unwinding.  */
4922       gcc_unreachable ();
4923 #endif
4924     case DW_OP_const1u:
4925     case DW_OP_const1s:
4926       dw2_asm_output_data (1, val1->v.val_int, NULL);
4927       break;
4928     case DW_OP_constu:
4929       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4930       break;
4931     case DW_OP_consts:
4932       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4933       break;
4934     case DW_OP_pick:
4935       dw2_asm_output_data (1, val1->v.val_int, NULL);
4936       break;
4937     case DW_OP_plus_uconst:
4938       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4939       break;
4940     case DW_OP_breg0:
4941     case DW_OP_breg1:
4942     case DW_OP_breg2:
4943     case DW_OP_breg3:
4944     case DW_OP_breg4:
4945     case DW_OP_breg5:
4946     case DW_OP_breg6:
4947     case DW_OP_breg7:
4948     case DW_OP_breg8:
4949     case DW_OP_breg9:
4950     case DW_OP_breg10:
4951     case DW_OP_breg11:
4952     case DW_OP_breg12:
4953     case DW_OP_breg13:
4954     case DW_OP_breg14:
4955     case DW_OP_breg15:
4956     case DW_OP_breg16:
4957     case DW_OP_breg17:
4958     case DW_OP_breg18:
4959     case DW_OP_breg19:
4960     case DW_OP_breg20:
4961     case DW_OP_breg21:
4962     case DW_OP_breg22:
4963     case DW_OP_breg23:
4964     case DW_OP_breg24:
4965     case DW_OP_breg25:
4966     case DW_OP_breg26:
4967     case DW_OP_breg27:
4968     case DW_OP_breg28:
4969     case DW_OP_breg29:
4970     case DW_OP_breg30:
4971     case DW_OP_breg31:
4972       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4973       break;
4974     case DW_OP_regx:
4975       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4976       break;
4977     case DW_OP_fbreg:
4978       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
4979       break;
4980     case DW_OP_bregx:
4981       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4982       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
4983       break;
4984     case DW_OP_piece:
4985       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
4986       break;
4987     case DW_OP_deref_size:
4988     case DW_OP_xderef_size:
4989       dw2_asm_output_data (1, val1->v.val_int, NULL);
4990       break;
4991
4992     case DW_OP_addr:
4993       if (loc->dtprel)
4994         {
4995           if (targetm.asm_out.output_dwarf_dtprel)
4996             {
4997               targetm.asm_out.output_dwarf_dtprel (asm_out_file,
4998                                                    DWARF2_ADDR_SIZE,
4999                                                    val1->v.val_addr);
5000               fputc ('\n', asm_out_file);
5001             }
5002           else
5003             gcc_unreachable ();
5004         }
5005       else
5006         {
5007 #ifdef DWARF2_DEBUGGING_INFO
5008           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
5009 #else
5010           gcc_unreachable ();
5011 #endif
5012         }
5013       break;
5014
5015     default:
5016       /* Other codes have no operands.  */
5017       break;
5018     }
5019 }
5020
5021 /* Output a sequence of location operations.  */
5022
5023 static void
5024 output_loc_sequence (dw_loc_descr_ref loc)
5025 {
5026   for (; loc != NULL; loc = loc->dw_loc_next)
5027     {
5028       /* Output the opcode.  */
5029       dw2_asm_output_data (1, loc->dw_loc_opc,
5030                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
5031
5032       /* Output the operand(s) (if any).  */
5033       output_loc_operands (loc);
5034     }
5035 }
5036
5037 /* Output location description stack opcode's operands (if any).
5038    The output is single bytes on a line, suitable for .cfi_escape.  */
5039
5040 static void
5041 output_loc_operands_raw (dw_loc_descr_ref loc)
5042 {
5043   dw_val_ref val1 = &loc->dw_loc_oprnd1;
5044   dw_val_ref val2 = &loc->dw_loc_oprnd2;
5045
5046   switch (loc->dw_loc_opc)
5047     {
5048     case DW_OP_addr:
5049     case DW_OP_implicit_value:
5050       /* We cannot output addresses in .cfi_escape, only bytes.  */
5051       gcc_unreachable ();
5052
5053     case DW_OP_const1u:
5054     case DW_OP_const1s:
5055     case DW_OP_pick:
5056     case DW_OP_deref_size:
5057     case DW_OP_xderef_size:
5058       fputc (',', asm_out_file);
5059       dw2_asm_output_data_raw (1, val1->v.val_int);
5060       break;
5061
5062     case DW_OP_const2u:
5063     case DW_OP_const2s:
5064       fputc (',', asm_out_file);
5065       dw2_asm_output_data_raw (2, val1->v.val_int);
5066       break;
5067
5068     case DW_OP_const4u:
5069     case DW_OP_const4s:
5070       fputc (',', asm_out_file);
5071       dw2_asm_output_data_raw (4, val1->v.val_int);
5072       break;
5073
5074     case DW_OP_const8u:
5075     case DW_OP_const8s:
5076       gcc_assert (HOST_BITS_PER_WIDE_INT >= 64);
5077       fputc (',', asm_out_file);
5078       dw2_asm_output_data_raw (8, val1->v.val_int);
5079       break;
5080
5081     case DW_OP_skip:
5082     case DW_OP_bra:
5083       {
5084         int offset;
5085
5086         gcc_assert (val1->val_class == dw_val_class_loc);
5087         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
5088
5089         fputc (',', asm_out_file);
5090         dw2_asm_output_data_raw (2, offset);
5091       }
5092       break;
5093
5094     case DW_OP_constu:
5095     case DW_OP_plus_uconst:
5096     case DW_OP_regx:
5097     case DW_OP_piece:
5098       fputc (',', asm_out_file);
5099       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5100       break;
5101
5102     case DW_OP_consts:
5103     case DW_OP_breg0:
5104     case DW_OP_breg1:
5105     case DW_OP_breg2:
5106     case DW_OP_breg3:
5107     case DW_OP_breg4:
5108     case DW_OP_breg5:
5109     case DW_OP_breg6:
5110     case DW_OP_breg7:
5111     case DW_OP_breg8:
5112     case DW_OP_breg9:
5113     case DW_OP_breg10:
5114     case DW_OP_breg11:
5115     case DW_OP_breg12:
5116     case DW_OP_breg13:
5117     case DW_OP_breg14:
5118     case DW_OP_breg15:
5119     case DW_OP_breg16:
5120     case DW_OP_breg17:
5121     case DW_OP_breg18:
5122     case DW_OP_breg19:
5123     case DW_OP_breg20:
5124     case DW_OP_breg21:
5125     case DW_OP_breg22:
5126     case DW_OP_breg23:
5127     case DW_OP_breg24:
5128     case DW_OP_breg25:
5129     case DW_OP_breg26:
5130     case DW_OP_breg27:
5131     case DW_OP_breg28:
5132     case DW_OP_breg29:
5133     case DW_OP_breg30:
5134     case DW_OP_breg31:
5135     case DW_OP_fbreg:
5136       fputc (',', asm_out_file);
5137       dw2_asm_output_data_sleb128_raw (val1->v.val_int);
5138       break;
5139
5140     case DW_OP_bregx:
5141       fputc (',', asm_out_file);
5142       dw2_asm_output_data_uleb128_raw (val1->v.val_unsigned);
5143       fputc (',', asm_out_file);
5144       dw2_asm_output_data_sleb128_raw (val2->v.val_int);
5145       break;
5146
5147     default:
5148       /* Other codes have no operands.  */
5149       break;
5150     }
5151 }
5152
5153 static void
5154 output_loc_sequence_raw (dw_loc_descr_ref loc)
5155 {
5156   while (1)
5157     {
5158       /* Output the opcode.  */
5159       fprintf (asm_out_file, "0x%x", loc->dw_loc_opc);
5160       output_loc_operands_raw (loc);
5161
5162       if (!loc->dw_loc_next)
5163         break;
5164       loc = loc->dw_loc_next;
5165
5166       fputc (',', asm_out_file);
5167     }
5168 }
5169
5170 /* This routine will generate the correct assembly data for a location
5171    description based on a cfi entry with a complex address.  */
5172
5173 static void
5174 output_cfa_loc (dw_cfi_ref cfi)
5175 {
5176   dw_loc_descr_ref loc;
5177   unsigned long size;
5178
5179   if (cfi->dw_cfi_opc == DW_CFA_expression)
5180     dw2_asm_output_data (1, cfi->dw_cfi_oprnd2.dw_cfi_reg_num, NULL);
5181
5182   /* Output the size of the block.  */
5183   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5184   size = size_of_locs (loc);
5185   dw2_asm_output_data_uleb128 (size, NULL);
5186
5187   /* Now output the operations themselves.  */
5188   output_loc_sequence (loc);
5189 }
5190
5191 /* Similar, but used for .cfi_escape.  */
5192
5193 static void
5194 output_cfa_loc_raw (dw_cfi_ref cfi)
5195 {
5196   dw_loc_descr_ref loc;
5197   unsigned long size;
5198
5199   if (cfi->dw_cfi_opc == DW_CFA_expression)
5200     fprintf (asm_out_file, "0x%x,", cfi->dw_cfi_oprnd2.dw_cfi_reg_num);
5201
5202   /* Output the size of the block.  */
5203   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
5204   size = size_of_locs (loc);
5205   dw2_asm_output_data_uleb128_raw (size);
5206   fputc (',', asm_out_file);
5207
5208   /* Now output the operations themselves.  */
5209   output_loc_sequence_raw (loc);
5210 }
5211
5212 /* This function builds a dwarf location descriptor sequence from a
5213    dw_cfa_location, adding the given OFFSET to the result of the
5214    expression.  */
5215
5216 static struct dw_loc_descr_struct *
5217 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
5218 {
5219   struct dw_loc_descr_struct *head, *tmp;
5220
5221   offset += cfa->offset;
5222
5223   if (cfa->indirect)
5224     {
5225       head = new_reg_loc_descr (cfa->reg, cfa->base_offset);
5226       head->dw_loc_oprnd1.val_class = dw_val_class_const;
5227       tmp = new_loc_descr (DW_OP_deref, 0, 0);
5228       add_loc_descr (&head, tmp);
5229       if (offset != 0)
5230         {
5231           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
5232           add_loc_descr (&head, tmp);
5233         }
5234     }
5235   else
5236     head = new_reg_loc_descr (cfa->reg, offset);
5237
5238   return head;
5239 }
5240
5241 /* This function builds a dwarf location descriptor sequence for
5242    the address at OFFSET from the CFA when stack is aligned to
5243    ALIGNMENT byte.  */
5244
5245 static struct dw_loc_descr_struct *
5246 build_cfa_aligned_loc (HOST_WIDE_INT offset, HOST_WIDE_INT alignment)
5247 {
5248   struct dw_loc_descr_struct *head;
5249   unsigned int dwarf_fp
5250     = DWARF_FRAME_REGNUM (HARD_FRAME_POINTER_REGNUM);
5251
5252  /* When CFA is defined as FP+OFFSET, emulate stack alignment.  */
5253   if (cfa.reg == HARD_FRAME_POINTER_REGNUM && cfa.indirect == 0)
5254     {
5255       head = new_reg_loc_descr (dwarf_fp, 0);
5256       add_loc_descr (&head, int_loc_descriptor (alignment));
5257       add_loc_descr (&head, new_loc_descr (DW_OP_and, 0, 0));
5258       loc_descr_plus_const (&head, offset);
5259     }
5260   else
5261     head = new_reg_loc_descr (dwarf_fp, offset);
5262   return head;
5263 }
5264
5265 /* This function fills in aa dw_cfa_location structure from a dwarf location
5266    descriptor sequence.  */
5267
5268 static void
5269 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
5270 {
5271   struct dw_loc_descr_struct *ptr;
5272   cfa->offset = 0;
5273   cfa->base_offset = 0;
5274   cfa->indirect = 0;
5275   cfa->reg = -1;
5276
5277   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
5278     {
5279       enum dwarf_location_atom op = ptr->dw_loc_opc;
5280
5281       switch (op)
5282         {
5283         case DW_OP_reg0:
5284         case DW_OP_reg1:
5285         case DW_OP_reg2:
5286         case DW_OP_reg3:
5287         case DW_OP_reg4:
5288         case DW_OP_reg5:
5289         case DW_OP_reg6:
5290         case DW_OP_reg7:
5291         case DW_OP_reg8:
5292         case DW_OP_reg9:
5293         case DW_OP_reg10:
5294         case DW_OP_reg11:
5295         case DW_OP_reg12:
5296         case DW_OP_reg13:
5297         case DW_OP_reg14:
5298         case DW_OP_reg15:
5299         case DW_OP_reg16:
5300         case DW_OP_reg17:
5301         case DW_OP_reg18:
5302         case DW_OP_reg19:
5303         case DW_OP_reg20:
5304         case DW_OP_reg21:
5305         case DW_OP_reg22:
5306         case DW_OP_reg23:
5307         case DW_OP_reg24:
5308         case DW_OP_reg25:
5309         case DW_OP_reg26:
5310         case DW_OP_reg27:
5311         case DW_OP_reg28:
5312         case DW_OP_reg29:
5313         case DW_OP_reg30:
5314         case DW_OP_reg31:
5315           cfa->reg = op - DW_OP_reg0;
5316           break;
5317         case DW_OP_regx:
5318           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5319           break;
5320         case DW_OP_breg0:
5321         case DW_OP_breg1:
5322         case DW_OP_breg2:
5323         case DW_OP_breg3:
5324         case DW_OP_breg4:
5325         case DW_OP_breg5:
5326         case DW_OP_breg6:
5327         case DW_OP_breg7:
5328         case DW_OP_breg8:
5329         case DW_OP_breg9:
5330         case DW_OP_breg10:
5331         case DW_OP_breg11:
5332         case DW_OP_breg12:
5333         case DW_OP_breg13:
5334         case DW_OP_breg14:
5335         case DW_OP_breg15:
5336         case DW_OP_breg16:
5337         case DW_OP_breg17:
5338         case DW_OP_breg18:
5339         case DW_OP_breg19:
5340         case DW_OP_breg20:
5341         case DW_OP_breg21:
5342         case DW_OP_breg22:
5343         case DW_OP_breg23:
5344         case DW_OP_breg24:
5345         case DW_OP_breg25:
5346         case DW_OP_breg26:
5347         case DW_OP_breg27:
5348         case DW_OP_breg28:
5349         case DW_OP_breg29:
5350         case DW_OP_breg30:
5351         case DW_OP_breg31:
5352           cfa->reg = op - DW_OP_breg0;
5353           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
5354           break;
5355         case DW_OP_bregx:
5356           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
5357           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
5358           break;
5359         case DW_OP_deref:
5360           cfa->indirect = 1;
5361           break;
5362         case DW_OP_plus_uconst:
5363           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
5364           break;
5365         default:
5366           internal_error ("DW_LOC_OP %s not implemented",
5367                           dwarf_stack_op_name (ptr->dw_loc_opc));
5368         }
5369     }
5370 }
5371 #endif /* .debug_frame support */
5372 \f
5373 /* And now, the support for symbolic debugging information.  */
5374 #ifdef DWARF2_DEBUGGING_INFO
5375
5376 /* .debug_str support.  */
5377 static int output_indirect_string (void **, void *);
5378
5379 static void dwarf2out_init (const char *);
5380 static void dwarf2out_finish (const char *);
5381 static void dwarf2out_define (unsigned int, const char *);
5382 static void dwarf2out_undef (unsigned int, const char *);
5383 static void dwarf2out_start_source_file (unsigned, const char *);
5384 static void dwarf2out_end_source_file (unsigned);
5385 static void dwarf2out_begin_block (unsigned, unsigned);
5386 static void dwarf2out_end_block (unsigned, unsigned);
5387 static bool dwarf2out_ignore_block (const_tree);
5388 static void dwarf2out_global_decl (tree);
5389 static void dwarf2out_type_decl (tree, int);
5390 static void dwarf2out_imported_module_or_decl (tree, tree, tree, bool);
5391 static void dwarf2out_imported_module_or_decl_1 (tree, tree, tree,
5392                                                  dw_die_ref);
5393 static void dwarf2out_abstract_function (tree);
5394 static void dwarf2out_var_location (rtx);
5395 static void dwarf2out_begin_function (tree);
5396 static void dwarf2out_set_name (tree, tree);
5397
5398 /* The debug hooks structure.  */
5399
5400 const struct gcc_debug_hooks dwarf2_debug_hooks =
5401 {
5402   dwarf2out_init,
5403   dwarf2out_finish,
5404   dwarf2out_define,
5405   dwarf2out_undef,
5406   dwarf2out_start_source_file,
5407   dwarf2out_end_source_file,
5408   dwarf2out_begin_block,
5409   dwarf2out_end_block,
5410   dwarf2out_ignore_block,
5411   dwarf2out_source_line,
5412   dwarf2out_begin_prologue,
5413   debug_nothing_int_charstar,   /* end_prologue */
5414   dwarf2out_end_epilogue,
5415   dwarf2out_begin_function,
5416   debug_nothing_int,            /* end_function */
5417   dwarf2out_decl,               /* function_decl */
5418   dwarf2out_global_decl,
5419   dwarf2out_type_decl,          /* type_decl */
5420   dwarf2out_imported_module_or_decl,
5421   debug_nothing_tree,           /* deferred_inline_function */
5422   /* The DWARF 2 backend tries to reduce debugging bloat by not
5423      emitting the abstract description of inline functions until
5424      something tries to reference them.  */
5425   dwarf2out_abstract_function,  /* outlining_inline_function */
5426   debug_nothing_rtx,            /* label */
5427   debug_nothing_int,            /* handle_pch */
5428   dwarf2out_var_location,
5429   dwarf2out_switch_text_section,
5430   dwarf2out_set_name,
5431   1                             /* start_end_main_source_file */
5432 };
5433 #endif
5434 \f
5435 /* NOTE: In the comments in this file, many references are made to
5436    "Debugging Information Entries".  This term is abbreviated as `DIE'
5437    throughout the remainder of this file.  */
5438
5439 /* An internal representation of the DWARF output is built, and then
5440    walked to generate the DWARF debugging info.  The walk of the internal
5441    representation is done after the entire program has been compiled.
5442    The types below are used to describe the internal representation.  */
5443
5444 /* Various DIE's use offsets relative to the beginning of the
5445    .debug_info section to refer to each other.  */
5446
5447 typedef long int dw_offset;
5448
5449 /* Define typedefs here to avoid circular dependencies.  */
5450
5451 typedef struct dw_attr_struct *dw_attr_ref;
5452 typedef struct dw_line_info_struct *dw_line_info_ref;
5453 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
5454 typedef struct pubname_struct *pubname_ref;
5455 typedef struct dw_ranges_struct *dw_ranges_ref;
5456 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
5457
5458 /* Each entry in the line_info_table maintains the file and
5459    line number associated with the label generated for that
5460    entry.  The label gives the PC value associated with
5461    the line number entry.  */
5462
5463 typedef struct GTY(()) dw_line_info_struct {
5464   unsigned long dw_file_num;
5465   unsigned long dw_line_num;
5466 }
5467 dw_line_info_entry;
5468
5469 /* Line information for functions in separate sections; each one gets its
5470    own sequence.  */
5471 typedef struct GTY(()) dw_separate_line_info_struct {
5472   unsigned long dw_file_num;
5473   unsigned long dw_line_num;
5474   unsigned long function;
5475 }
5476 dw_separate_line_info_entry;
5477
5478 /* Each DIE attribute has a field specifying the attribute kind,
5479    a link to the next attribute in the chain, and an attribute value.
5480    Attributes are typically linked below the DIE they modify.  */
5481
5482 typedef struct GTY(()) dw_attr_struct {
5483   enum dwarf_attribute dw_attr;
5484   dw_val_node dw_attr_val;
5485 }
5486 dw_attr_node;
5487
5488 DEF_VEC_O(dw_attr_node);
5489 DEF_VEC_ALLOC_O(dw_attr_node,gc);
5490
5491 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
5492    The children of each node form a circular list linked by
5493    die_sib.  die_child points to the node *before* the "first" child node.  */
5494
5495 typedef struct GTY((chain_circular ("%h.die_sib"))) die_struct {
5496   enum dwarf_tag die_tag;
5497   char *die_symbol;
5498   VEC(dw_attr_node,gc) * die_attr;
5499   dw_die_ref die_parent;
5500   dw_die_ref die_child;
5501   dw_die_ref die_sib;
5502   dw_die_ref die_definition; /* ref from a specification to its definition */
5503   dw_offset die_offset;
5504   unsigned long die_abbrev;
5505   int die_mark;
5506   /* Die is used and must not be pruned as unused.  */
5507   int die_perennial_p;
5508   unsigned int decl_id;
5509 }
5510 die_node;
5511
5512 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
5513 #define FOR_EACH_CHILD(die, c, expr) do {       \
5514   c = die->die_child;                           \
5515   if (c) do {                                   \
5516     c = c->die_sib;                             \
5517     expr;                                       \
5518   } while (c != die->die_child);                \
5519 } while (0)
5520
5521 /* The pubname structure */
5522
5523 typedef struct GTY(()) pubname_struct {
5524   dw_die_ref die;
5525   const char *name;
5526 }
5527 pubname_entry;
5528
5529 DEF_VEC_O(pubname_entry);
5530 DEF_VEC_ALLOC_O(pubname_entry, gc);
5531
5532 struct GTY(()) dw_ranges_struct {
5533   /* If this is positive, it's a block number, otherwise it's a
5534      bitwise-negated index into dw_ranges_by_label.  */
5535   int num;
5536 };
5537
5538 struct GTY(()) dw_ranges_by_label_struct {
5539   const char *begin;
5540   const char *end;
5541 };
5542
5543 /* The limbo die list structure.  */
5544 typedef struct GTY(()) limbo_die_struct {
5545   dw_die_ref die;
5546   tree created_for;
5547   struct limbo_die_struct *next;
5548 }
5549 limbo_die_node;
5550
5551 /* How to start an assembler comment.  */
5552 #ifndef ASM_COMMENT_START
5553 #define ASM_COMMENT_START ";#"
5554 #endif
5555
5556 /* Define a macro which returns nonzero for a TYPE_DECL which was
5557    implicitly generated for a tagged type.
5558
5559    Note that unlike the gcc front end (which generates a NULL named
5560    TYPE_DECL node for each complete tagged type, each array type, and
5561    each function type node created) the g++ front end generates a
5562    _named_ TYPE_DECL node for each tagged type node created.
5563    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
5564    generate a DW_TAG_typedef DIE for them.  */
5565
5566 #define TYPE_DECL_IS_STUB(decl)                         \
5567   (DECL_NAME (decl) == NULL_TREE                        \
5568    || (DECL_ARTIFICIAL (decl)                           \
5569        && is_tagged_type (TREE_TYPE (decl))             \
5570        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
5571            /* This is necessary for stub decls that     \
5572               appear in nested inline functions.  */    \
5573            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
5574                && (decl_ultimate_origin (decl)          \
5575                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
5576
5577 /* Information concerning the compilation unit's programming
5578    language, and compiler version.  */
5579
5580 /* Fixed size portion of the DWARF compilation unit header.  */
5581 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
5582   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
5583
5584 /* Fixed size portion of public names info.  */
5585 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
5586
5587 /* Fixed size portion of the address range info.  */
5588 #define DWARF_ARANGES_HEADER_SIZE                                       \
5589   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
5590                 DWARF2_ADDR_SIZE * 2)                                   \
5591    - DWARF_INITIAL_LENGTH_SIZE)
5592
5593 /* Size of padding portion in the address range info.  It must be
5594    aligned to twice the pointer size.  */
5595 #define DWARF_ARANGES_PAD_SIZE \
5596   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
5597                 DWARF2_ADDR_SIZE * 2)                              \
5598    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
5599
5600 /* Use assembler line directives if available.  */
5601 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
5602 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
5603 #define DWARF2_ASM_LINE_DEBUG_INFO 1
5604 #else
5605 #define DWARF2_ASM_LINE_DEBUG_INFO 0
5606 #endif
5607 #endif
5608
5609 /* Minimum line offset in a special line info. opcode.
5610    This value was chosen to give a reasonable range of values.  */
5611 #define DWARF_LINE_BASE  -10
5612
5613 /* First special line opcode - leave room for the standard opcodes.  */
5614 #define DWARF_LINE_OPCODE_BASE  10
5615
5616 /* Range of line offsets in a special line info. opcode.  */
5617 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
5618
5619 /* Flag that indicates the initial value of the is_stmt_start flag.
5620    In the present implementation, we do not mark any lines as
5621    the beginning of a source statement, because that information
5622    is not made available by the GCC front-end.  */
5623 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
5624
5625 #ifdef DWARF2_DEBUGGING_INFO
5626 /* This location is used by calc_die_sizes() to keep track
5627    the offset of each DIE within the .debug_info section.  */
5628 static unsigned long next_die_offset;
5629 #endif
5630
5631 /* Record the root of the DIE's built for the current compilation unit.  */
5632 static GTY(()) dw_die_ref comp_unit_die;
5633
5634 /* A list of DIEs with a NULL parent waiting to be relocated.  */
5635 static GTY(()) limbo_die_node *limbo_die_list;
5636
5637 /* A list of DIEs for which we may have to generate
5638    DW_AT_MIPS_linkage_name once their DECL_ASSEMBLER_NAMEs are
5639    set.  */
5640 static GTY(()) limbo_die_node *deferred_asm_name;
5641
5642 /* Filenames referenced by this compilation unit.  */
5643 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
5644
5645 /* A hash table of references to DIE's that describe declarations.
5646    The key is a DECL_UID() which is a unique number identifying each decl.  */
5647 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
5648
5649 /* A hash table of references to DIE's that describe COMMON blocks.
5650    The key is DECL_UID() ^ die_parent.  */
5651 static GTY ((param_is (struct die_struct))) htab_t common_block_die_table;
5652
5653 typedef struct GTY(()) die_arg_entry_struct {
5654     dw_die_ref die;
5655     tree arg;
5656 } die_arg_entry;
5657
5658 DEF_VEC_O(die_arg_entry);
5659 DEF_VEC_ALLOC_O(die_arg_entry,gc);
5660
5661 /* Node of the variable location list.  */
5662 struct GTY ((chain_next ("%h.next"))) var_loc_node {
5663   rtx GTY (()) var_loc_note;
5664   const char * GTY (()) label;
5665   const char * GTY (()) section_label;
5666   struct var_loc_node * GTY (()) next;
5667 };
5668
5669 /* Variable location list.  */
5670 struct GTY (()) var_loc_list_def {
5671   struct var_loc_node * GTY (()) first;
5672
5673   /* Do not mark the last element of the chained list because
5674      it is marked through the chain.  */
5675   struct var_loc_node * GTY ((skip ("%h"))) last;
5676
5677   /* DECL_UID of the variable decl.  */
5678   unsigned int decl_id;
5679 };
5680 typedef struct var_loc_list_def var_loc_list;
5681
5682
5683 /* Table of decl location linked lists.  */
5684 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
5685
5686 /* A pointer to the base of a list of references to DIE's that
5687    are uniquely identified by their tag, presence/absence of
5688    children DIE's, and list of attribute/value pairs.  */
5689 static GTY((length ("abbrev_die_table_allocated")))
5690   dw_die_ref *abbrev_die_table;
5691
5692 /* Number of elements currently allocated for abbrev_die_table.  */
5693 static GTY(()) unsigned abbrev_die_table_allocated;
5694
5695 /* Number of elements in type_die_table currently in use.  */
5696 static GTY(()) unsigned abbrev_die_table_in_use;
5697
5698 /* Size (in elements) of increments by which we may expand the
5699    abbrev_die_table.  */
5700 #define ABBREV_DIE_TABLE_INCREMENT 256
5701
5702 /* A pointer to the base of a table that contains line information
5703    for each source code line in .text in the compilation unit.  */
5704 static GTY((length ("line_info_table_allocated")))
5705      dw_line_info_ref line_info_table;
5706
5707 /* Number of elements currently allocated for line_info_table.  */
5708 static GTY(()) unsigned line_info_table_allocated;
5709
5710 /* Number of elements in line_info_table currently in use.  */
5711 static GTY(()) unsigned line_info_table_in_use;
5712
5713 /* A pointer to the base of a table that contains line information
5714    for each source code line outside of .text in the compilation unit.  */
5715 static GTY ((length ("separate_line_info_table_allocated")))
5716      dw_separate_line_info_ref separate_line_info_table;
5717
5718 /* Number of elements currently allocated for separate_line_info_table.  */
5719 static GTY(()) unsigned separate_line_info_table_allocated;
5720
5721 /* Number of elements in separate_line_info_table currently in use.  */
5722 static GTY(()) unsigned separate_line_info_table_in_use;
5723
5724 /* Size (in elements) of increments by which we may expand the
5725    line_info_table.  */
5726 #define LINE_INFO_TABLE_INCREMENT 1024
5727
5728 /* A pointer to the base of a table that contains a list of publicly
5729    accessible names.  */
5730 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
5731
5732 /* A pointer to the base of a table that contains a list of publicly
5733    accessible types.  */
5734 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
5735
5736 /* Array of dies for which we should generate .debug_arange info.  */
5737 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
5738
5739 /* Number of elements currently allocated for arange_table.  */
5740 static GTY(()) unsigned arange_table_allocated;
5741
5742 /* Number of elements in arange_table currently in use.  */
5743 static GTY(()) unsigned arange_table_in_use;
5744
5745 /* Size (in elements) of increments by which we may expand the
5746    arange_table.  */
5747 #define ARANGE_TABLE_INCREMENT 64
5748
5749 /* Array of dies for which we should generate .debug_ranges info.  */
5750 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
5751
5752 /* Number of elements currently allocated for ranges_table.  */
5753 static GTY(()) unsigned ranges_table_allocated;
5754
5755 /* Number of elements in ranges_table currently in use.  */
5756 static GTY(()) unsigned ranges_table_in_use;
5757
5758 /* Array of pairs of labels referenced in ranges_table.  */
5759 static GTY ((length ("ranges_by_label_allocated")))
5760      dw_ranges_by_label_ref ranges_by_label;
5761
5762 /* Number of elements currently allocated for ranges_by_label.  */
5763 static GTY(()) unsigned ranges_by_label_allocated;
5764
5765 /* Number of elements in ranges_by_label currently in use.  */
5766 static GTY(()) unsigned ranges_by_label_in_use;
5767
5768 /* Size (in elements) of increments by which we may expand the
5769    ranges_table.  */
5770 #define RANGES_TABLE_INCREMENT 64
5771
5772 /* Whether we have location lists that need outputting */
5773 static GTY(()) bool have_location_lists;
5774
5775 /* Unique label counter.  */
5776 static GTY(()) unsigned int loclabel_num;
5777
5778 #ifdef DWARF2_DEBUGGING_INFO
5779 /* Record whether the function being analyzed contains inlined functions.  */
5780 static int current_function_has_inlines;
5781 #endif
5782 #if 0 && defined (MIPS_DEBUGGING_INFO)
5783 static int comp_unit_has_inlines;
5784 #endif
5785
5786 /* The last file entry emitted by maybe_emit_file().  */
5787 static GTY(()) struct dwarf_file_data * last_emitted_file;
5788
5789 /* Number of internal labels generated by gen_internal_sym().  */
5790 static GTY(()) int label_num;
5791
5792 /* Cached result of previous call to lookup_filename.  */
5793 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
5794
5795 static GTY(()) VEC(die_arg_entry,gc) *tmpl_value_parm_die_table;
5796
5797 #ifdef DWARF2_DEBUGGING_INFO
5798
5799 /* Offset from the "steady-state frame pointer" to the frame base,
5800    within the current function.  */
5801 static HOST_WIDE_INT frame_pointer_fb_offset;
5802
5803 /* Forward declarations for functions defined in this file.  */
5804
5805 static int is_pseudo_reg (const_rtx);
5806 static tree type_main_variant (tree);
5807 static int is_tagged_type (const_tree);
5808 static const char *dwarf_tag_name (unsigned);
5809 static const char *dwarf_attr_name (unsigned);
5810 static const char *dwarf_form_name (unsigned);
5811 static tree decl_ultimate_origin (const_tree);
5812 static tree decl_class_context (tree);
5813 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
5814 static inline enum dw_val_class AT_class (dw_attr_ref);
5815 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
5816 static inline unsigned AT_flag (dw_attr_ref);
5817 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
5818 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
5819 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
5820 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
5821 static void add_AT_double (dw_die_ref, enum dwarf_attribute,
5822                            HOST_WIDE_INT, unsigned HOST_WIDE_INT);
5823 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
5824                                unsigned int, unsigned char *);
5825 static hashval_t debug_str_do_hash (const void *);
5826 static int debug_str_eq (const void *, const void *);
5827 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
5828 static inline const char *AT_string (dw_attr_ref);
5829 static enum dwarf_form AT_string_form (dw_attr_ref);
5830 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
5831 static void add_AT_specification (dw_die_ref, dw_die_ref);
5832 static inline dw_die_ref AT_ref (dw_attr_ref);
5833 static inline int AT_ref_external (dw_attr_ref);
5834 static inline void set_AT_ref_external (dw_attr_ref, int);
5835 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
5836 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
5837 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
5838 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
5839                              dw_loc_list_ref);
5840 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
5841 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
5842 static inline rtx AT_addr (dw_attr_ref);
5843 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
5844 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
5845 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
5846 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
5847                            unsigned HOST_WIDE_INT);
5848 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
5849                                unsigned long);
5850 static inline const char *AT_lbl (dw_attr_ref);
5851 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
5852 static const char *get_AT_low_pc (dw_die_ref);
5853 static const char *get_AT_hi_pc (dw_die_ref);
5854 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
5855 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
5856 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
5857 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
5858 static bool is_c_family (void);
5859 static bool is_cxx (void);
5860 static bool is_java (void);
5861 static bool is_fortran (void);
5862 static bool is_ada (void);
5863 static void remove_AT (dw_die_ref, enum dwarf_attribute);
5864 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
5865 static void add_child_die (dw_die_ref, dw_die_ref);
5866 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
5867 static dw_die_ref lookup_type_die (tree);
5868 static void equate_type_number_to_die (tree, dw_die_ref);
5869 static hashval_t decl_die_table_hash (const void *);
5870 static int decl_die_table_eq (const void *, const void *);
5871 static dw_die_ref lookup_decl_die (tree);
5872 static hashval_t common_block_die_table_hash (const void *);
5873 static int common_block_die_table_eq (const void *, const void *);
5874 static hashval_t decl_loc_table_hash (const void *);
5875 static int decl_loc_table_eq (const void *, const void *);
5876 static var_loc_list *lookup_decl_loc (const_tree);
5877 static void equate_decl_number_to_die (tree, dw_die_ref);
5878 static void add_var_loc_to_decl (tree, struct var_loc_node *);
5879 static void print_spaces (FILE *);
5880 static void print_die (dw_die_ref, FILE *);
5881 static void print_dwarf_line_table (FILE *);
5882 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
5883 static dw_die_ref pop_compile_unit (dw_die_ref);
5884 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
5885 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
5886 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
5887 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
5888 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
5889 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
5890 static int same_die_p (dw_die_ref, dw_die_ref, int *);
5891 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
5892 static void compute_section_prefix (dw_die_ref);
5893 static int is_type_die (dw_die_ref);
5894 static int is_comdat_die (dw_die_ref);
5895 static int is_symbol_die (dw_die_ref);
5896 static void assign_symbol_names (dw_die_ref);
5897 static void break_out_includes (dw_die_ref);
5898 static hashval_t htab_cu_hash (const void *);
5899 static int htab_cu_eq (const void *, const void *);
5900 static void htab_cu_del (void *);
5901 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
5902 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
5903 static void add_sibling_attributes (dw_die_ref);
5904 static void build_abbrev_table (dw_die_ref);
5905 static void output_location_lists (dw_die_ref);
5906 static int constant_size (unsigned HOST_WIDE_INT);
5907 static unsigned long size_of_die (dw_die_ref);
5908 static void calc_die_sizes (dw_die_ref);
5909 static void mark_dies (dw_die_ref);
5910 static void unmark_dies (dw_die_ref);
5911 static void unmark_all_dies (dw_die_ref);
5912 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
5913 static unsigned long size_of_aranges (void);
5914 static enum dwarf_form value_format (dw_attr_ref);
5915 static void output_value_format (dw_attr_ref);
5916 static void output_abbrev_section (void);
5917 static void output_die_symbol (dw_die_ref);
5918 static void output_die (dw_die_ref);
5919 static void output_compilation_unit_header (void);
5920 static void output_comp_unit (dw_die_ref, int);
5921 static const char *dwarf2_name (tree, int);
5922 static void add_pubname (tree, dw_die_ref);
5923 static void add_pubname_string (const char *, dw_die_ref);
5924 static void add_pubtype (tree, dw_die_ref);
5925 static void output_pubnames (VEC (pubname_entry,gc) *);
5926 static void add_arange (tree, dw_die_ref);
5927 static void output_aranges (void);
5928 static unsigned int add_ranges_num (int);
5929 static unsigned int add_ranges (const_tree);
5930 static unsigned int add_ranges_by_labels (const char *, const char *);
5931 static void output_ranges (void);
5932 static void output_line_info (void);
5933 static void output_file_names (void);
5934 static dw_die_ref base_type_die (tree);
5935 static int is_base_type (tree);
5936 static dw_die_ref subrange_type_die (tree, tree, tree, dw_die_ref);
5937 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
5938 static dw_die_ref generic_parameter_die (tree, tree, bool, dw_die_ref);
5939 static dw_die_ref template_parameter_pack_die (tree, tree, dw_die_ref);
5940 static int type_is_enum (const_tree);
5941 static unsigned int dbx_reg_number (const_rtx);
5942 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
5943 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
5944 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int,
5945                                                 enum var_init_status);
5946 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
5947                                                      enum var_init_status);
5948 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
5949                                          enum var_init_status);
5950 static int is_based_loc (const_rtx);
5951 static int resolve_one_addr (rtx *, void *);
5952 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
5953                                             enum var_init_status);
5954 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
5955                                                enum var_init_status);
5956 static dw_loc_descr_ref loc_descriptor (rtx, enum machine_mode mode,
5957                                         enum var_init_status);
5958 static dw_loc_list_ref loc_list_from_tree (tree, int);
5959 static dw_loc_descr_ref loc_descriptor_from_tree (tree, int);
5960 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
5961 static tree field_type (const_tree);
5962 static unsigned int simple_type_align_in_bits (const_tree);
5963 static unsigned int simple_decl_align_in_bits (const_tree);
5964 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
5965 static HOST_WIDE_INT field_byte_offset (const_tree);
5966 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
5967                                          dw_loc_list_ref);
5968 static void add_data_member_location_attribute (dw_die_ref, tree);
5969 static bool add_const_value_attribute (dw_die_ref, rtx);
5970 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
5971 static void insert_float (const_rtx, unsigned char *);
5972 static rtx rtl_for_decl_location (tree);
5973 static bool add_location_or_const_value_attribute (dw_die_ref, tree,
5974                                                    enum dwarf_attribute);
5975 static bool tree_add_const_value_attribute (dw_die_ref, tree);
5976 static bool tree_add_const_value_attribute_for_decl (dw_die_ref, tree);
5977 static void add_name_attribute (dw_die_ref, const char *);
5978 static void add_comp_dir_attribute (dw_die_ref);
5979 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
5980 static void add_subscript_info (dw_die_ref, tree, bool);
5981 static void add_byte_size_attribute (dw_die_ref, tree);
5982 static void add_bit_offset_attribute (dw_die_ref, tree);
5983 static void add_bit_size_attribute (dw_die_ref, tree);
5984 static void add_prototyped_attribute (dw_die_ref, tree);
5985 static dw_die_ref add_abstract_origin_attribute (dw_die_ref, tree);
5986 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
5987 static void add_src_coords_attributes (dw_die_ref, tree);
5988 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
5989 static void push_decl_scope (tree);
5990 static void pop_decl_scope (void);
5991 static dw_die_ref scope_die_for (tree, dw_die_ref);
5992 static inline int local_scope_p (dw_die_ref);
5993 static inline int class_scope_p (dw_die_ref);
5994 static inline int class_or_namespace_scope_p (dw_die_ref);
5995 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
5996 static void add_calling_convention_attribute (dw_die_ref, tree);
5997 static const char *type_tag (const_tree);
5998 static tree member_declared_type (const_tree);
5999 #if 0
6000 static const char *decl_start_label (tree);
6001 #endif
6002 static void gen_array_type_die (tree, dw_die_ref);
6003 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
6004 #if 0
6005 static void gen_entry_point_die (tree, dw_die_ref);
6006 #endif
6007 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
6008 static dw_die_ref gen_formal_parameter_die (tree, tree, bool, dw_die_ref);
6009 static dw_die_ref gen_formal_parameter_pack_die  (tree, tree, dw_die_ref, tree*);
6010 static void gen_unspecified_parameters_die (tree, dw_die_ref);
6011 static void gen_formal_types_die (tree, dw_die_ref);
6012 static void gen_subprogram_die (tree, dw_die_ref);
6013 static void gen_variable_die (tree, tree, dw_die_ref);
6014 static void gen_const_die (tree, dw_die_ref);
6015 static void gen_label_die (tree, dw_die_ref);
6016 static void gen_lexical_block_die (tree, dw_die_ref, int);
6017 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
6018 static void gen_field_die (tree, dw_die_ref);
6019 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
6020 static dw_die_ref gen_compile_unit_die (const char *);
6021 static void gen_inheritance_die (tree, tree, dw_die_ref);
6022 static void gen_member_die (tree, dw_die_ref);
6023 static void gen_struct_or_union_type_die (tree, dw_die_ref,
6024                                                 enum debug_info_usage);
6025 static void gen_subroutine_type_die (tree, dw_die_ref);
6026 static void gen_typedef_die (tree, dw_die_ref);
6027 static void gen_type_die (tree, dw_die_ref);
6028 static void gen_block_die (tree, dw_die_ref, int);
6029 static void decls_for_scope (tree, dw_die_ref, int);
6030 static int is_redundant_typedef (const_tree);
6031 static inline dw_die_ref get_context_die (tree);
6032 static void gen_namespace_die (tree, dw_die_ref);
6033 static void gen_decl_die (tree, tree, dw_die_ref);
6034 static dw_die_ref force_decl_die (tree);
6035 static dw_die_ref force_type_die (tree);
6036 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
6037 static dw_die_ref declare_in_namespace (tree, dw_die_ref);
6038 static struct dwarf_file_data * lookup_filename (const char *);
6039 static void retry_incomplete_types (void);
6040 static void gen_type_die_for_member (tree, tree, dw_die_ref);
6041 static void gen_generic_params_dies (tree);
6042 static void splice_child_die (dw_die_ref, dw_die_ref);
6043 static int file_info_cmp (const void *, const void *);
6044 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
6045                                      const char *, const char *, unsigned);
6046 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
6047                                        const char *, const char *,
6048                                        const char *);
6049 static void output_loc_list (dw_loc_list_ref);
6050 static char *gen_internal_sym (const char *);
6051
6052 static void prune_unmark_dies (dw_die_ref);
6053 static void prune_unused_types_mark (dw_die_ref, int);
6054 static void prune_unused_types_walk (dw_die_ref);
6055 static void prune_unused_types_walk_attribs (dw_die_ref);
6056 static void prune_unused_types_prune (dw_die_ref);
6057 static void prune_unused_types (void);
6058 static int maybe_emit_file (struct dwarf_file_data *fd);
6059 static void append_entry_to_tmpl_value_parm_die_table (dw_die_ref, tree);
6060 static void gen_remaining_tmpl_value_param_die_attribute (void);
6061
6062 /* Section names used to hold DWARF debugging information.  */
6063 #ifndef DEBUG_INFO_SECTION
6064 #define DEBUG_INFO_SECTION      ".debug_info"
6065 #endif
6066 #ifndef DEBUG_ABBREV_SECTION
6067 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
6068 #endif
6069 #ifndef DEBUG_ARANGES_SECTION
6070 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
6071 #endif
6072 #ifndef DEBUG_MACINFO_SECTION
6073 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
6074 #endif
6075 #ifndef DEBUG_LINE_SECTION
6076 #define DEBUG_LINE_SECTION      ".debug_line"
6077 #endif
6078 #ifndef DEBUG_LOC_SECTION
6079 #define DEBUG_LOC_SECTION       ".debug_loc"
6080 #endif
6081 #ifndef DEBUG_PUBNAMES_SECTION
6082 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
6083 #endif
6084 #ifndef DEBUG_PUBTYPES_SECTION
6085 #define DEBUG_PUBTYPES_SECTION  ".debug_pubtypes"
6086 #endif
6087 #ifndef DEBUG_STR_SECTION
6088 #define DEBUG_STR_SECTION       ".debug_str"
6089 #endif
6090 #ifndef DEBUG_RANGES_SECTION
6091 #define DEBUG_RANGES_SECTION    ".debug_ranges"
6092 #endif
6093
6094 /* Standard ELF section names for compiled code and data.  */
6095 #ifndef TEXT_SECTION_NAME
6096 #define TEXT_SECTION_NAME       ".text"
6097 #endif
6098
6099 /* Section flags for .debug_str section.  */
6100 #define DEBUG_STR_SECTION_FLAGS \
6101   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
6102    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
6103    : SECTION_DEBUG)
6104
6105 /* Labels we insert at beginning sections we can reference instead of
6106    the section names themselves.  */
6107
6108 #ifndef TEXT_SECTION_LABEL
6109 #define TEXT_SECTION_LABEL              "Ltext"
6110 #endif
6111 #ifndef COLD_TEXT_SECTION_LABEL
6112 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
6113 #endif
6114 #ifndef DEBUG_LINE_SECTION_LABEL
6115 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
6116 #endif
6117 #ifndef DEBUG_INFO_SECTION_LABEL
6118 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
6119 #endif
6120 #ifndef DEBUG_ABBREV_SECTION_LABEL
6121 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
6122 #endif
6123 #ifndef DEBUG_LOC_SECTION_LABEL
6124 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
6125 #endif
6126 #ifndef DEBUG_RANGES_SECTION_LABEL
6127 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
6128 #endif
6129 #ifndef DEBUG_MACINFO_SECTION_LABEL
6130 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
6131 #endif
6132
6133 /* Definitions of defaults for formats and names of various special
6134    (artificial) labels which may be generated within this file (when the -g
6135    options is used and DWARF2_DEBUGGING_INFO is in effect.
6136    If necessary, these may be overridden from within the tm.h file, but
6137    typically, overriding these defaults is unnecessary.  */
6138
6139 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6140 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6141 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6142 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
6143 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6144 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6145 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6146 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6147 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
6148 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
6149
6150 #ifndef TEXT_END_LABEL
6151 #define TEXT_END_LABEL          "Letext"
6152 #endif
6153 #ifndef COLD_END_LABEL
6154 #define COLD_END_LABEL          "Letext_cold"
6155 #endif
6156 #ifndef BLOCK_BEGIN_LABEL
6157 #define BLOCK_BEGIN_LABEL       "LBB"
6158 #endif
6159 #ifndef BLOCK_END_LABEL
6160 #define BLOCK_END_LABEL         "LBE"
6161 #endif
6162 #ifndef LINE_CODE_LABEL
6163 #define LINE_CODE_LABEL         "LM"
6164 #endif
6165 #ifndef SEPARATE_LINE_CODE_LABEL
6166 #define SEPARATE_LINE_CODE_LABEL        "LSM"
6167 #endif
6168
6169 \f
6170 /* We allow a language front-end to designate a function that is to be
6171    called to "demangle" any name before it is put into a DIE.  */
6172
6173 static const char *(*demangle_name_func) (const char *);
6174
6175 void
6176 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
6177 {
6178   demangle_name_func = func;
6179 }
6180
6181 /* Test if rtl node points to a pseudo register.  */
6182
6183 static inline int
6184 is_pseudo_reg (const_rtx rtl)
6185 {
6186   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
6187           || (GET_CODE (rtl) == SUBREG
6188               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
6189 }
6190
6191 /* Return a reference to a type, with its const and volatile qualifiers
6192    removed.  */
6193
6194 static inline tree
6195 type_main_variant (tree type)
6196 {
6197   type = TYPE_MAIN_VARIANT (type);
6198
6199   /* ??? There really should be only one main variant among any group of
6200      variants of a given type (and all of the MAIN_VARIANT values for all
6201      members of the group should point to that one type) but sometimes the C
6202      front-end messes this up for array types, so we work around that bug
6203      here.  */
6204   if (TREE_CODE (type) == ARRAY_TYPE)
6205     while (type != TYPE_MAIN_VARIANT (type))
6206       type = TYPE_MAIN_VARIANT (type);
6207
6208   return type;
6209 }
6210
6211 /* Return nonzero if the given type node represents a tagged type.  */
6212
6213 static inline int
6214 is_tagged_type (const_tree type)
6215 {
6216   enum tree_code code = TREE_CODE (type);
6217
6218   return (code == RECORD_TYPE || code == UNION_TYPE
6219           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
6220 }
6221
6222 /* Convert a DIE tag into its string name.  */
6223
6224 static const char *
6225 dwarf_tag_name (unsigned int tag)
6226 {
6227   switch (tag)
6228     {
6229     case DW_TAG_padding:
6230       return "DW_TAG_padding";
6231     case DW_TAG_array_type:
6232       return "DW_TAG_array_type";
6233     case DW_TAG_class_type:
6234       return "DW_TAG_class_type";
6235     case DW_TAG_entry_point:
6236       return "DW_TAG_entry_point";
6237     case DW_TAG_enumeration_type:
6238       return "DW_TAG_enumeration_type";
6239     case DW_TAG_formal_parameter:
6240       return "DW_TAG_formal_parameter";
6241     case DW_TAG_imported_declaration:
6242       return "DW_TAG_imported_declaration";
6243     case DW_TAG_label:
6244       return "DW_TAG_label";
6245     case DW_TAG_lexical_block:
6246       return "DW_TAG_lexical_block";
6247     case DW_TAG_member:
6248       return "DW_TAG_member";
6249     case DW_TAG_pointer_type:
6250       return "DW_TAG_pointer_type";
6251     case DW_TAG_reference_type:
6252       return "DW_TAG_reference_type";
6253     case DW_TAG_compile_unit:
6254       return "DW_TAG_compile_unit";
6255     case DW_TAG_string_type:
6256       return "DW_TAG_string_type";
6257     case DW_TAG_structure_type:
6258       return "DW_TAG_structure_type";
6259     case DW_TAG_subroutine_type:
6260       return "DW_TAG_subroutine_type";
6261     case DW_TAG_typedef:
6262       return "DW_TAG_typedef";
6263     case DW_TAG_union_type:
6264       return "DW_TAG_union_type";
6265     case DW_TAG_unspecified_parameters:
6266       return "DW_TAG_unspecified_parameters";
6267     case DW_TAG_variant:
6268       return "DW_TAG_variant";
6269     case DW_TAG_common_block:
6270       return "DW_TAG_common_block";
6271     case DW_TAG_common_inclusion:
6272       return "DW_TAG_common_inclusion";
6273     case DW_TAG_inheritance:
6274       return "DW_TAG_inheritance";
6275     case DW_TAG_inlined_subroutine:
6276       return "DW_TAG_inlined_subroutine";
6277     case DW_TAG_module:
6278       return "DW_TAG_module";
6279     case DW_TAG_ptr_to_member_type:
6280       return "DW_TAG_ptr_to_member_type";
6281     case DW_TAG_set_type:
6282       return "DW_TAG_set_type";
6283     case DW_TAG_subrange_type:
6284       return "DW_TAG_subrange_type";
6285     case DW_TAG_with_stmt:
6286       return "DW_TAG_with_stmt";
6287     case DW_TAG_access_declaration:
6288       return "DW_TAG_access_declaration";
6289     case DW_TAG_base_type:
6290       return "DW_TAG_base_type";
6291     case DW_TAG_catch_block:
6292       return "DW_TAG_catch_block";
6293     case DW_TAG_const_type:
6294       return "DW_TAG_const_type";
6295     case DW_TAG_constant:
6296       return "DW_TAG_constant";
6297     case DW_TAG_enumerator:
6298       return "DW_TAG_enumerator";
6299     case DW_TAG_file_type:
6300       return "DW_TAG_file_type";
6301     case DW_TAG_friend:
6302       return "DW_TAG_friend";
6303     case DW_TAG_namelist:
6304       return "DW_TAG_namelist";
6305     case DW_TAG_namelist_item:
6306       return "DW_TAG_namelist_item";
6307     case DW_TAG_packed_type:
6308       return "DW_TAG_packed_type";
6309     case DW_TAG_subprogram:
6310       return "DW_TAG_subprogram";
6311     case DW_TAG_template_type_param:
6312       return "DW_TAG_template_type_param";
6313     case DW_TAG_template_value_param:
6314       return "DW_TAG_template_value_param";
6315     case DW_TAG_thrown_type:
6316       return "DW_TAG_thrown_type";
6317     case DW_TAG_try_block:
6318       return "DW_TAG_try_block";
6319     case DW_TAG_variant_part:
6320       return "DW_TAG_variant_part";
6321     case DW_TAG_variable:
6322       return "DW_TAG_variable";
6323     case DW_TAG_volatile_type:
6324       return "DW_TAG_volatile_type";
6325     case DW_TAG_dwarf_procedure:
6326       return "DW_TAG_dwarf_procedure";
6327     case DW_TAG_restrict_type:
6328       return "DW_TAG_restrict_type";
6329     case DW_TAG_interface_type:
6330       return "DW_TAG_interface_type";
6331     case DW_TAG_namespace:
6332       return "DW_TAG_namespace";
6333     case DW_TAG_imported_module:
6334       return "DW_TAG_imported_module";
6335     case DW_TAG_unspecified_type:
6336       return "DW_TAG_unspecified_type";
6337     case DW_TAG_partial_unit:
6338       return "DW_TAG_partial_unit";
6339     case DW_TAG_imported_unit:
6340       return "DW_TAG_imported_unit";
6341     case DW_TAG_condition:
6342       return "DW_TAG_condition";
6343     case DW_TAG_shared_type:
6344       return "DW_TAG_shared_type";
6345     case DW_TAG_GNU_template_parameter_pack:
6346       return "DW_TAG_GNU_template_parameter_pack";
6347     case DW_TAG_GNU_formal_parameter_pack:
6348       return "DW_TAG_GNU_formal_parameter_pack";
6349     case DW_TAG_MIPS_loop:
6350       return "DW_TAG_MIPS_loop";
6351     case DW_TAG_format_label:
6352       return "DW_TAG_format_label";
6353     case DW_TAG_function_template:
6354       return "DW_TAG_function_template";
6355     case DW_TAG_class_template:
6356       return "DW_TAG_class_template";
6357     case DW_TAG_GNU_BINCL:
6358       return "DW_TAG_GNU_BINCL";
6359     case DW_TAG_GNU_EINCL:
6360       return "DW_TAG_GNU_EINCL";
6361     case DW_TAG_GNU_template_template_param:
6362       return "DW_TAG_GNU_template_template_param";
6363     default:
6364       return "DW_TAG_<unknown>";
6365     }
6366 }
6367
6368 /* Convert a DWARF attribute code into its string name.  */
6369
6370 static const char *
6371 dwarf_attr_name (unsigned int attr)
6372 {
6373   switch (attr)
6374     {
6375     case DW_AT_sibling:
6376       return "DW_AT_sibling";
6377     case DW_AT_location:
6378       return "DW_AT_location";
6379     case DW_AT_name:
6380       return "DW_AT_name";
6381     case DW_AT_ordering:
6382       return "DW_AT_ordering";
6383     case DW_AT_subscr_data:
6384       return "DW_AT_subscr_data";
6385     case DW_AT_byte_size:
6386       return "DW_AT_byte_size";
6387     case DW_AT_bit_offset:
6388       return "DW_AT_bit_offset";
6389     case DW_AT_bit_size:
6390       return "DW_AT_bit_size";
6391     case DW_AT_element_list:
6392       return "DW_AT_element_list";
6393     case DW_AT_stmt_list:
6394       return "DW_AT_stmt_list";
6395     case DW_AT_low_pc:
6396       return "DW_AT_low_pc";
6397     case DW_AT_high_pc:
6398       return "DW_AT_high_pc";
6399     case DW_AT_language:
6400       return "DW_AT_language";
6401     case DW_AT_member:
6402       return "DW_AT_member";
6403     case DW_AT_discr:
6404       return "DW_AT_discr";
6405     case DW_AT_discr_value:
6406       return "DW_AT_discr_value";
6407     case DW_AT_visibility:
6408       return "DW_AT_visibility";
6409     case DW_AT_import:
6410       return "DW_AT_import";
6411     case DW_AT_string_length:
6412       return "DW_AT_string_length";
6413     case DW_AT_common_reference:
6414       return "DW_AT_common_reference";
6415     case DW_AT_comp_dir:
6416       return "DW_AT_comp_dir";
6417     case DW_AT_const_value:
6418       return "DW_AT_const_value";
6419     case DW_AT_containing_type:
6420       return "DW_AT_containing_type";
6421     case DW_AT_default_value:
6422       return "DW_AT_default_value";
6423     case DW_AT_inline:
6424       return "DW_AT_inline";
6425     case DW_AT_is_optional:
6426       return "DW_AT_is_optional";
6427     case DW_AT_lower_bound:
6428       return "DW_AT_lower_bound";
6429     case DW_AT_producer:
6430       return "DW_AT_producer";
6431     case DW_AT_prototyped:
6432       return "DW_AT_prototyped";
6433     case DW_AT_return_addr:
6434       return "DW_AT_return_addr";
6435     case DW_AT_start_scope:
6436       return "DW_AT_start_scope";
6437     case DW_AT_bit_stride:
6438       return "DW_AT_bit_stride";
6439     case DW_AT_upper_bound:
6440       return "DW_AT_upper_bound";
6441     case DW_AT_abstract_origin:
6442       return "DW_AT_abstract_origin";
6443     case DW_AT_accessibility:
6444       return "DW_AT_accessibility";
6445     case DW_AT_address_class:
6446       return "DW_AT_address_class";
6447     case DW_AT_artificial:
6448       return "DW_AT_artificial";
6449     case DW_AT_base_types:
6450       return "DW_AT_base_types";
6451     case DW_AT_calling_convention:
6452       return "DW_AT_calling_convention";
6453     case DW_AT_count:
6454       return "DW_AT_count";
6455     case DW_AT_data_member_location:
6456       return "DW_AT_data_member_location";
6457     case DW_AT_decl_column:
6458       return "DW_AT_decl_column";
6459     case DW_AT_decl_file:
6460       return "DW_AT_decl_file";
6461     case DW_AT_decl_line:
6462       return "DW_AT_decl_line";
6463     case DW_AT_declaration:
6464       return "DW_AT_declaration";
6465     case DW_AT_discr_list:
6466       return "DW_AT_discr_list";
6467     case DW_AT_encoding:
6468       return "DW_AT_encoding";
6469     case DW_AT_external:
6470       return "DW_AT_external";
6471     case DW_AT_explicit:
6472       return "DW_AT_explicit";
6473     case DW_AT_frame_base:
6474       return "DW_AT_frame_base";
6475     case DW_AT_friend:
6476       return "DW_AT_friend";
6477     case DW_AT_identifier_case:
6478       return "DW_AT_identifier_case";
6479     case DW_AT_macro_info:
6480       return "DW_AT_macro_info";
6481     case DW_AT_namelist_items:
6482       return "DW_AT_namelist_items";
6483     case DW_AT_priority:
6484       return "DW_AT_priority";
6485     case DW_AT_segment:
6486       return "DW_AT_segment";
6487     case DW_AT_specification:
6488       return "DW_AT_specification";
6489     case DW_AT_static_link:
6490       return "DW_AT_static_link";
6491     case DW_AT_type:
6492       return "DW_AT_type";
6493     case DW_AT_use_location:
6494       return "DW_AT_use_location";
6495     case DW_AT_variable_parameter:
6496       return "DW_AT_variable_parameter";
6497     case DW_AT_virtuality:
6498       return "DW_AT_virtuality";
6499     case DW_AT_vtable_elem_location:
6500       return "DW_AT_vtable_elem_location";
6501
6502     case DW_AT_allocated:
6503       return "DW_AT_allocated";
6504     case DW_AT_associated:
6505       return "DW_AT_associated";
6506     case DW_AT_data_location:
6507       return "DW_AT_data_location";
6508     case DW_AT_byte_stride:
6509       return "DW_AT_byte_stride";
6510     case DW_AT_entry_pc:
6511       return "DW_AT_entry_pc";
6512     case DW_AT_use_UTF8:
6513       return "DW_AT_use_UTF8";
6514     case DW_AT_extension:
6515       return "DW_AT_extension";
6516     case DW_AT_ranges:
6517       return "DW_AT_ranges";
6518     case DW_AT_trampoline:
6519       return "DW_AT_trampoline";
6520     case DW_AT_call_column:
6521       return "DW_AT_call_column";
6522     case DW_AT_call_file:
6523       return "DW_AT_call_file";
6524     case DW_AT_call_line:
6525       return "DW_AT_call_line";
6526
6527     case DW_AT_MIPS_fde:
6528       return "DW_AT_MIPS_fde";
6529     case DW_AT_MIPS_loop_begin:
6530       return "DW_AT_MIPS_loop_begin";
6531     case DW_AT_MIPS_tail_loop_begin:
6532       return "DW_AT_MIPS_tail_loop_begin";
6533     case DW_AT_MIPS_epilog_begin:
6534       return "DW_AT_MIPS_epilog_begin";
6535     case DW_AT_MIPS_loop_unroll_factor:
6536       return "DW_AT_MIPS_loop_unroll_factor";
6537     case DW_AT_MIPS_software_pipeline_depth:
6538       return "DW_AT_MIPS_software_pipeline_depth";
6539     case DW_AT_MIPS_linkage_name:
6540       return "DW_AT_MIPS_linkage_name";
6541     case DW_AT_MIPS_stride:
6542       return "DW_AT_MIPS_stride";
6543     case DW_AT_MIPS_abstract_name:
6544       return "DW_AT_MIPS_abstract_name";
6545     case DW_AT_MIPS_clone_origin:
6546       return "DW_AT_MIPS_clone_origin";
6547     case DW_AT_MIPS_has_inlines:
6548       return "DW_AT_MIPS_has_inlines";
6549
6550     case DW_AT_sf_names:
6551       return "DW_AT_sf_names";
6552     case DW_AT_src_info:
6553       return "DW_AT_src_info";
6554     case DW_AT_mac_info:
6555       return "DW_AT_mac_info";
6556     case DW_AT_src_coords:
6557       return "DW_AT_src_coords";
6558     case DW_AT_body_begin:
6559       return "DW_AT_body_begin";
6560     case DW_AT_body_end:
6561       return "DW_AT_body_end";
6562     case DW_AT_GNU_vector:
6563       return "DW_AT_GNU_vector";
6564     case DW_AT_GNU_template_name:
6565       return "DW_AT_GNU_template_name";
6566
6567     case DW_AT_VMS_rtnbeg_pd_address:
6568       return "DW_AT_VMS_rtnbeg_pd_address";
6569
6570     default:
6571       return "DW_AT_<unknown>";
6572     }
6573 }
6574
6575 /* Convert a DWARF value form code into its string name.  */
6576
6577 static const char *
6578 dwarf_form_name (unsigned int form)
6579 {
6580   switch (form)
6581     {
6582     case DW_FORM_addr:
6583       return "DW_FORM_addr";
6584     case DW_FORM_block2:
6585       return "DW_FORM_block2";
6586     case DW_FORM_block4:
6587       return "DW_FORM_block4";
6588     case DW_FORM_data2:
6589       return "DW_FORM_data2";
6590     case DW_FORM_data4:
6591       return "DW_FORM_data4";
6592     case DW_FORM_data8:
6593       return "DW_FORM_data8";
6594     case DW_FORM_string:
6595       return "DW_FORM_string";
6596     case DW_FORM_block:
6597       return "DW_FORM_block";
6598     case DW_FORM_block1:
6599       return "DW_FORM_block1";
6600     case DW_FORM_data1:
6601       return "DW_FORM_data1";
6602     case DW_FORM_flag:
6603       return "DW_FORM_flag";
6604     case DW_FORM_sdata:
6605       return "DW_FORM_sdata";
6606     case DW_FORM_strp:
6607       return "DW_FORM_strp";
6608     case DW_FORM_udata:
6609       return "DW_FORM_udata";
6610     case DW_FORM_ref_addr:
6611       return "DW_FORM_ref_addr";
6612     case DW_FORM_ref1:
6613       return "DW_FORM_ref1";
6614     case DW_FORM_ref2:
6615       return "DW_FORM_ref2";
6616     case DW_FORM_ref4:
6617       return "DW_FORM_ref4";
6618     case DW_FORM_ref8:
6619       return "DW_FORM_ref8";
6620     case DW_FORM_ref_udata:
6621       return "DW_FORM_ref_udata";
6622     case DW_FORM_indirect:
6623       return "DW_FORM_indirect";
6624     default:
6625       return "DW_FORM_<unknown>";
6626     }
6627 }
6628 \f
6629 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
6630    instance of an inlined instance of a decl which is local to an inline
6631    function, so we have to trace all of the way back through the origin chain
6632    to find out what sort of node actually served as the original seed for the
6633    given block.  */
6634
6635 static tree
6636 decl_ultimate_origin (const_tree decl)
6637 {
6638   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
6639     return NULL_TREE;
6640
6641   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
6642      nodes in the function to point to themselves; ignore that if
6643      we're trying to output the abstract instance of this function.  */
6644   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
6645     return NULL_TREE;
6646
6647   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
6648      most distant ancestor, this should never happen.  */
6649   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
6650
6651   return DECL_ABSTRACT_ORIGIN (decl);
6652 }
6653
6654 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
6655    of a virtual function may refer to a base class, so we check the 'this'
6656    parameter.  */
6657
6658 static tree
6659 decl_class_context (tree decl)
6660 {
6661   tree context = NULL_TREE;
6662
6663   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
6664     context = DECL_CONTEXT (decl);
6665   else
6666     context = TYPE_MAIN_VARIANT
6667       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
6668
6669   if (context && !TYPE_P (context))
6670     context = NULL_TREE;
6671
6672   return context;
6673 }
6674 \f
6675 /* Add an attribute/value pair to a DIE.  */
6676
6677 static inline void
6678 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
6679 {
6680   /* Maybe this should be an assert?  */
6681   if (die == NULL)
6682     return;
6683
6684   if (die->die_attr == NULL)
6685     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
6686   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
6687 }
6688
6689 static inline enum dw_val_class
6690 AT_class (dw_attr_ref a)
6691 {
6692   return a->dw_attr_val.val_class;
6693 }
6694
6695 /* Add a flag value attribute to a DIE.  */
6696
6697 static inline void
6698 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
6699 {
6700   dw_attr_node attr;
6701
6702   attr.dw_attr = attr_kind;
6703   attr.dw_attr_val.val_class = dw_val_class_flag;
6704   attr.dw_attr_val.v.val_flag = flag;
6705   add_dwarf_attr (die, &attr);
6706 }
6707
6708 static inline unsigned
6709 AT_flag (dw_attr_ref a)
6710 {
6711   gcc_assert (a && AT_class (a) == dw_val_class_flag);
6712   return a->dw_attr_val.v.val_flag;
6713 }
6714
6715 /* Add a signed integer attribute value to a DIE.  */
6716
6717 static inline void
6718 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
6719 {
6720   dw_attr_node attr;
6721
6722   attr.dw_attr = attr_kind;
6723   attr.dw_attr_val.val_class = dw_val_class_const;
6724   attr.dw_attr_val.v.val_int = int_val;
6725   add_dwarf_attr (die, &attr);
6726 }
6727
6728 static inline HOST_WIDE_INT
6729 AT_int (dw_attr_ref a)
6730 {
6731   gcc_assert (a && AT_class (a) == dw_val_class_const);
6732   return a->dw_attr_val.v.val_int;
6733 }
6734
6735 /* Add an unsigned integer attribute value to a DIE.  */
6736
6737 static inline void
6738 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
6739                  unsigned HOST_WIDE_INT unsigned_val)
6740 {
6741   dw_attr_node attr;
6742
6743   attr.dw_attr = attr_kind;
6744   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
6745   attr.dw_attr_val.v.val_unsigned = unsigned_val;
6746   add_dwarf_attr (die, &attr);
6747 }
6748
6749 static inline unsigned HOST_WIDE_INT
6750 AT_unsigned (dw_attr_ref a)
6751 {
6752   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
6753   return a->dw_attr_val.v.val_unsigned;
6754 }
6755
6756 /* Add an unsigned double integer attribute value to a DIE.  */
6757
6758 static inline void
6759 add_AT_double (dw_die_ref die, enum dwarf_attribute attr_kind,
6760                HOST_WIDE_INT high, unsigned HOST_WIDE_INT low)
6761 {
6762   dw_attr_node attr;
6763
6764   attr.dw_attr = attr_kind;
6765   attr.dw_attr_val.val_class = dw_val_class_const_double;
6766   attr.dw_attr_val.v.val_double.high = high;
6767   attr.dw_attr_val.v.val_double.low = low;
6768   add_dwarf_attr (die, &attr);
6769 }
6770
6771 /* Add a floating point attribute value to a DIE and return it.  */
6772
6773 static inline void
6774 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
6775             unsigned int length, unsigned int elt_size, unsigned char *array)
6776 {
6777   dw_attr_node attr;
6778
6779   attr.dw_attr = attr_kind;
6780   attr.dw_attr_val.val_class = dw_val_class_vec;
6781   attr.dw_attr_val.v.val_vec.length = length;
6782   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
6783   attr.dw_attr_val.v.val_vec.array = array;
6784   add_dwarf_attr (die, &attr);
6785 }
6786
6787 /* Hash and equality functions for debug_str_hash.  */
6788
6789 static hashval_t
6790 debug_str_do_hash (const void *x)
6791 {
6792   return htab_hash_string (((const struct indirect_string_node *)x)->str);
6793 }
6794
6795 static int
6796 debug_str_eq (const void *x1, const void *x2)
6797 {
6798   return strcmp ((((const struct indirect_string_node *)x1)->str),
6799                  (const char *)x2) == 0;
6800 }
6801
6802 /* Add STR to the indirect string hash table.  */
6803
6804 static struct indirect_string_node *
6805 find_AT_string (const char *str)
6806 {
6807   struct indirect_string_node *node;
6808   void **slot;
6809
6810   if (! debug_str_hash)
6811     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
6812                                       debug_str_eq, NULL);
6813
6814   slot = htab_find_slot_with_hash (debug_str_hash, str,
6815                                    htab_hash_string (str), INSERT);
6816   if (*slot == NULL)
6817     {
6818       node = (struct indirect_string_node *)
6819                ggc_alloc_cleared (sizeof (struct indirect_string_node));
6820       node->str = ggc_strdup (str);
6821       *slot = node;
6822     }
6823   else
6824     node = (struct indirect_string_node *) *slot;
6825
6826   node->refcount++;
6827   return node;
6828 }
6829
6830 /* Add a string attribute value to a DIE.  */
6831
6832 static inline void
6833 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
6834 {
6835   dw_attr_node attr;
6836   struct indirect_string_node *node;
6837
6838   node = find_AT_string (str);
6839
6840   attr.dw_attr = attr_kind;
6841   attr.dw_attr_val.val_class = dw_val_class_str;
6842   attr.dw_attr_val.v.val_str = node;
6843   add_dwarf_attr (die, &attr);
6844 }
6845
6846 /* Create a label for an indirect string node, ensuring it is going to
6847    be output, unless its reference count goes down to zero.  */
6848
6849 static inline void
6850 gen_label_for_indirect_string (struct indirect_string_node *node)
6851 {
6852   char label[32];
6853
6854   if (node->label)
6855     return;
6856
6857   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
6858   ++dw2_string_counter;
6859   node->label = xstrdup (label);
6860 }
6861
6862 /* Create a SYMBOL_REF rtx whose value is the initial address of a
6863    debug string STR.  */
6864
6865 static inline rtx
6866 get_debug_string_label (const char *str)
6867 {
6868   struct indirect_string_node *node = find_AT_string (str);
6869
6870   debug_str_hash_forced = true;
6871
6872   gen_label_for_indirect_string (node);
6873
6874   return gen_rtx_SYMBOL_REF (Pmode, node->label);
6875 }
6876
6877 static inline const char *
6878 AT_string (dw_attr_ref a)
6879 {
6880   gcc_assert (a && AT_class (a) == dw_val_class_str);
6881   return a->dw_attr_val.v.val_str->str;
6882 }
6883
6884 /* Find out whether a string should be output inline in DIE
6885    or out-of-line in .debug_str section.  */
6886
6887 static enum dwarf_form
6888 AT_string_form (dw_attr_ref a)
6889 {
6890   struct indirect_string_node *node;
6891   unsigned int len;
6892
6893   gcc_assert (a && AT_class (a) == dw_val_class_str);
6894
6895   node = a->dw_attr_val.v.val_str;
6896   if (node->form)
6897     return node->form;
6898
6899   len = strlen (node->str) + 1;
6900
6901   /* If the string is shorter or equal to the size of the reference, it is
6902      always better to put it inline.  */
6903   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
6904     return node->form = DW_FORM_string;
6905
6906   /* If we cannot expect the linker to merge strings in .debug_str
6907      section, only put it into .debug_str if it is worth even in this
6908      single module.  */
6909   if (DWARF2_INDIRECT_STRING_SUPPORT_MISSING_ON_TARGET
6910       || ((debug_str_section->common.flags & SECTION_MERGE) == 0
6911       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len))
6912     return node->form = DW_FORM_string;
6913
6914   gen_label_for_indirect_string (node);
6915
6916   return node->form = DW_FORM_strp;
6917 }
6918
6919 /* Add a DIE reference attribute value to a DIE.  */
6920
6921 static inline void
6922 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
6923 {
6924   dw_attr_node attr;
6925
6926   attr.dw_attr = attr_kind;
6927   attr.dw_attr_val.val_class = dw_val_class_die_ref;
6928   attr.dw_attr_val.v.val_die_ref.die = targ_die;
6929   attr.dw_attr_val.v.val_die_ref.external = 0;
6930   add_dwarf_attr (die, &attr);
6931 }
6932
6933 /* Add an AT_specification attribute to a DIE, and also make the back
6934    pointer from the specification to the definition.  */
6935
6936 static inline void
6937 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
6938 {
6939   add_AT_die_ref (die, DW_AT_specification, targ_die);
6940   gcc_assert (!targ_die->die_definition);
6941   targ_die->die_definition = die;
6942 }
6943
6944 static inline dw_die_ref
6945 AT_ref (dw_attr_ref a)
6946 {
6947   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6948   return a->dw_attr_val.v.val_die_ref.die;
6949 }
6950
6951 static inline int
6952 AT_ref_external (dw_attr_ref a)
6953 {
6954   if (a && AT_class (a) == dw_val_class_die_ref)
6955     return a->dw_attr_val.v.val_die_ref.external;
6956
6957   return 0;
6958 }
6959
6960 static inline void
6961 set_AT_ref_external (dw_attr_ref a, int i)
6962 {
6963   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
6964   a->dw_attr_val.v.val_die_ref.external = i;
6965 }
6966
6967 /* Add an FDE reference attribute value to a DIE.  */
6968
6969 static inline void
6970 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
6971 {
6972   dw_attr_node attr;
6973
6974   attr.dw_attr = attr_kind;
6975   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
6976   attr.dw_attr_val.v.val_fde_index = targ_fde;
6977   add_dwarf_attr (die, &attr);
6978 }
6979
6980 /* Add a location description attribute value to a DIE.  */
6981
6982 static inline void
6983 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
6984 {
6985   dw_attr_node attr;
6986
6987   attr.dw_attr = attr_kind;
6988   attr.dw_attr_val.val_class = dw_val_class_loc;
6989   attr.dw_attr_val.v.val_loc = loc;
6990   add_dwarf_attr (die, &attr);
6991 }
6992
6993 static inline dw_loc_descr_ref
6994 AT_loc (dw_attr_ref a)
6995 {
6996   gcc_assert (a && AT_class (a) == dw_val_class_loc);
6997   return a->dw_attr_val.v.val_loc;
6998 }
6999
7000 static inline void
7001 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
7002 {
7003   dw_attr_node attr;
7004
7005   attr.dw_attr = attr_kind;
7006   attr.dw_attr_val.val_class = dw_val_class_loc_list;
7007   attr.dw_attr_val.v.val_loc_list = loc_list;
7008   add_dwarf_attr (die, &attr);
7009   have_location_lists = true;
7010 }
7011
7012 static inline dw_loc_list_ref
7013 AT_loc_list (dw_attr_ref a)
7014 {
7015   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
7016   return a->dw_attr_val.v.val_loc_list;
7017 }
7018
7019 /* Add an address constant attribute value to a DIE.  */
7020
7021 static inline void
7022 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
7023 {
7024   dw_attr_node attr;
7025
7026   attr.dw_attr = attr_kind;
7027   attr.dw_attr_val.val_class = dw_val_class_addr;
7028   attr.dw_attr_val.v.val_addr = addr;
7029   add_dwarf_attr (die, &attr);
7030 }
7031
7032 /* Get the RTX from to an address DIE attribute.  */
7033
7034 static inline rtx
7035 AT_addr (dw_attr_ref a)
7036 {
7037   gcc_assert (a && AT_class (a) == dw_val_class_addr);
7038   return a->dw_attr_val.v.val_addr;
7039 }
7040
7041 /* Add a file attribute value to a DIE.  */
7042
7043 static inline void
7044 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
7045              struct dwarf_file_data *fd)
7046 {
7047   dw_attr_node attr;
7048
7049   attr.dw_attr = attr_kind;
7050   attr.dw_attr_val.val_class = dw_val_class_file;
7051   attr.dw_attr_val.v.val_file = fd;
7052   add_dwarf_attr (die, &attr);
7053 }
7054
7055 /* Get the dwarf_file_data from a file DIE attribute.  */
7056
7057 static inline struct dwarf_file_data *
7058 AT_file (dw_attr_ref a)
7059 {
7060   gcc_assert (a && AT_class (a) == dw_val_class_file);
7061   return a->dw_attr_val.v.val_file;
7062 }
7063
7064 /* Add a label identifier attribute value to a DIE.  */
7065
7066 static inline void
7067 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
7068 {
7069   dw_attr_node attr;
7070
7071   attr.dw_attr = attr_kind;
7072   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
7073   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
7074   add_dwarf_attr (die, &attr);
7075 }
7076
7077 /* Add a section offset attribute value to a DIE, an offset into the
7078    debug_line section.  */
7079
7080 static inline void
7081 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7082                 const char *label)
7083 {
7084   dw_attr_node attr;
7085
7086   attr.dw_attr = attr_kind;
7087   attr.dw_attr_val.val_class = dw_val_class_lineptr;
7088   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7089   add_dwarf_attr (die, &attr);
7090 }
7091
7092 /* Add a section offset attribute value to a DIE, an offset into the
7093    debug_macinfo section.  */
7094
7095 static inline void
7096 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
7097                const char *label)
7098 {
7099   dw_attr_node attr;
7100
7101   attr.dw_attr = attr_kind;
7102   attr.dw_attr_val.val_class = dw_val_class_macptr;
7103   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
7104   add_dwarf_attr (die, &attr);
7105 }
7106
7107 /* Add an offset attribute value to a DIE.  */
7108
7109 static inline void
7110 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
7111                unsigned HOST_WIDE_INT offset)
7112 {
7113   dw_attr_node attr;
7114
7115   attr.dw_attr = attr_kind;
7116   attr.dw_attr_val.val_class = dw_val_class_offset;
7117   attr.dw_attr_val.v.val_offset = offset;
7118   add_dwarf_attr (die, &attr);
7119 }
7120
7121 /* Add an range_list attribute value to a DIE.  */
7122
7123 static void
7124 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
7125                    long unsigned int offset)
7126 {
7127   dw_attr_node attr;
7128
7129   attr.dw_attr = attr_kind;
7130   attr.dw_attr_val.val_class = dw_val_class_range_list;
7131   attr.dw_attr_val.v.val_offset = offset;
7132   add_dwarf_attr (die, &attr);
7133 }
7134
7135 static inline const char *
7136 AT_lbl (dw_attr_ref a)
7137 {
7138   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
7139                     || AT_class (a) == dw_val_class_lineptr
7140                     || AT_class (a) == dw_val_class_macptr));
7141   return a->dw_attr_val.v.val_lbl_id;
7142 }
7143
7144 /* Get the attribute of type attr_kind.  */
7145
7146 static dw_attr_ref
7147 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7148 {
7149   dw_attr_ref a;
7150   unsigned ix;
7151   dw_die_ref spec = NULL;
7152
7153   if (! die)
7154     return NULL;
7155
7156   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7157     if (a->dw_attr == attr_kind)
7158       return a;
7159     else if (a->dw_attr == DW_AT_specification
7160              || a->dw_attr == DW_AT_abstract_origin)
7161       spec = AT_ref (a);
7162
7163   if (spec)
7164     return get_AT (spec, attr_kind);
7165
7166   return NULL;
7167 }
7168
7169 /* Return the "low pc" attribute value, typically associated with a subprogram
7170    DIE.  Return null if the "low pc" attribute is either not present, or if it
7171    cannot be represented as an assembler label identifier.  */
7172
7173 static inline const char *
7174 get_AT_low_pc (dw_die_ref die)
7175 {
7176   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
7177
7178   return a ? AT_lbl (a) : NULL;
7179 }
7180
7181 /* Return the "high pc" attribute value, typically associated with a subprogram
7182    DIE.  Return null if the "high pc" attribute is either not present, or if it
7183    cannot be represented as an assembler label identifier.  */
7184
7185 static inline const char *
7186 get_AT_hi_pc (dw_die_ref die)
7187 {
7188   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
7189
7190   return a ? AT_lbl (a) : NULL;
7191 }
7192
7193 /* Return the value of the string attribute designated by ATTR_KIND, or
7194    NULL if it is not present.  */
7195
7196 static inline const char *
7197 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
7198 {
7199   dw_attr_ref a = get_AT (die, attr_kind);
7200
7201   return a ? AT_string (a) : NULL;
7202 }
7203
7204 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
7205    if it is not present.  */
7206
7207 static inline int
7208 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
7209 {
7210   dw_attr_ref a = get_AT (die, attr_kind);
7211
7212   return a ? AT_flag (a) : 0;
7213 }
7214
7215 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
7216    if it is not present.  */
7217
7218 static inline unsigned
7219 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
7220 {
7221   dw_attr_ref a = get_AT (die, attr_kind);
7222
7223   return a ? AT_unsigned (a) : 0;
7224 }
7225
7226 static inline dw_die_ref
7227 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
7228 {
7229   dw_attr_ref a = get_AT (die, attr_kind);
7230
7231   return a ? AT_ref (a) : NULL;
7232 }
7233
7234 static inline struct dwarf_file_data *
7235 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
7236 {
7237   dw_attr_ref a = get_AT (die, attr_kind);
7238
7239   return a ? AT_file (a) : NULL;
7240 }
7241
7242 /* Return TRUE if the language is C or C++.  */
7243
7244 static inline bool
7245 is_c_family (void)
7246 {
7247   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7248
7249   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
7250           || lang == DW_LANG_C99
7251           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
7252 }
7253
7254 /* Return TRUE if the language is C++.  */
7255
7256 static inline bool
7257 is_cxx (void)
7258 {
7259   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7260
7261   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
7262 }
7263
7264 /* Return TRUE if the language is Fortran.  */
7265
7266 static inline bool
7267 is_fortran (void)
7268 {
7269   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7270
7271   return (lang == DW_LANG_Fortran77
7272           || lang == DW_LANG_Fortran90
7273           || lang == DW_LANG_Fortran95);
7274 }
7275
7276 /* Return TRUE if the language is Java.  */
7277
7278 static inline bool
7279 is_java (void)
7280 {
7281   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7282
7283   return lang == DW_LANG_Java;
7284 }
7285
7286 /* Return TRUE if the language is Ada.  */
7287
7288 static inline bool
7289 is_ada (void)
7290 {
7291   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
7292
7293   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
7294 }
7295
7296 /* Remove the specified attribute if present.  */
7297
7298 static void
7299 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
7300 {
7301   dw_attr_ref a;
7302   unsigned ix;
7303
7304   if (! die)
7305     return;
7306
7307   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7308     if (a->dw_attr == attr_kind)
7309       {
7310         if (AT_class (a) == dw_val_class_str)
7311           if (a->dw_attr_val.v.val_str->refcount)
7312             a->dw_attr_val.v.val_str->refcount--;
7313
7314         /* VEC_ordered_remove should help reduce the number of abbrevs
7315            that are needed.  */
7316         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
7317         return;
7318       }
7319 }
7320
7321 /* Remove CHILD from its parent.  PREV must have the property that
7322    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
7323
7324 static void
7325 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
7326 {
7327   gcc_assert (child->die_parent == prev->die_parent);
7328   gcc_assert (prev->die_sib == child);
7329   if (prev == child)
7330     {
7331       gcc_assert (child->die_parent->die_child == child);
7332       prev = NULL;
7333     }
7334   else
7335     prev->die_sib = child->die_sib;
7336   if (child->die_parent->die_child == child)
7337     child->die_parent->die_child = prev;
7338 }
7339
7340 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
7341    matches TAG.  */
7342
7343 static void
7344 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
7345 {
7346   dw_die_ref c;
7347
7348   c = die->die_child;
7349   if (c) do {
7350     dw_die_ref prev = c;
7351     c = c->die_sib;
7352     while (c->die_tag == tag)
7353       {
7354         remove_child_with_prev (c, prev);
7355         /* Might have removed every child.  */
7356         if (c == c->die_sib)
7357           return;
7358         c = c->die_sib;
7359       }
7360   } while (c != die->die_child);
7361 }
7362
7363 /* Add a CHILD_DIE as the last child of DIE.  */
7364
7365 static void
7366 add_child_die (dw_die_ref die, dw_die_ref child_die)
7367 {
7368   /* FIXME this should probably be an assert.  */
7369   if (! die || ! child_die)
7370     return;
7371   gcc_assert (die != child_die);
7372
7373   child_die->die_parent = die;
7374   if (die->die_child)
7375     {
7376       child_die->die_sib = die->die_child->die_sib;
7377       die->die_child->die_sib = child_die;
7378     }
7379   else
7380     child_die->die_sib = child_die;
7381   die->die_child = child_die;
7382 }
7383
7384 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
7385    is the specification, to the end of PARENT's list of children.
7386    This is done by removing and re-adding it.  */
7387
7388 static void
7389 splice_child_die (dw_die_ref parent, dw_die_ref child)
7390 {
7391   dw_die_ref p;
7392
7393   /* We want the declaration DIE from inside the class, not the
7394      specification DIE at toplevel.  */
7395   if (child->die_parent != parent)
7396     {
7397       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
7398
7399       if (tmp)
7400         child = tmp;
7401     }
7402
7403   gcc_assert (child->die_parent == parent
7404               || (child->die_parent
7405                   == get_AT_ref (parent, DW_AT_specification)));
7406
7407   for (p = child->die_parent->die_child; ; p = p->die_sib)
7408     if (p->die_sib == child)
7409       {
7410         remove_child_with_prev (child, p);
7411         break;
7412       }
7413
7414   add_child_die (parent, child);
7415 }
7416
7417 /* Return a pointer to a newly created DIE node.  */
7418
7419 static inline dw_die_ref
7420 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
7421 {
7422   dw_die_ref die = GGC_CNEW (die_node);
7423
7424   die->die_tag = tag_value;
7425
7426   if (parent_die != NULL)
7427     add_child_die (parent_die, die);
7428   else
7429     {
7430       limbo_die_node *limbo_node;
7431
7432       limbo_node = GGC_CNEW (limbo_die_node);
7433       limbo_node->die = die;
7434       limbo_node->created_for = t;
7435       limbo_node->next = limbo_die_list;
7436       limbo_die_list = limbo_node;
7437     }
7438
7439   return die;
7440 }
7441
7442 /* Return the DIE associated with the given type specifier.  */
7443
7444 static inline dw_die_ref
7445 lookup_type_die (tree type)
7446 {
7447   return TYPE_SYMTAB_DIE (type);
7448 }
7449
7450 /* Equate a DIE to a given type specifier.  */
7451
7452 static inline void
7453 equate_type_number_to_die (tree type, dw_die_ref type_die)
7454 {
7455   TYPE_SYMTAB_DIE (type) = type_die;
7456 }
7457
7458 /* Returns a hash value for X (which really is a die_struct).  */
7459
7460 static hashval_t
7461 decl_die_table_hash (const void *x)
7462 {
7463   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
7464 }
7465
7466 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
7467
7468 static int
7469 decl_die_table_eq (const void *x, const void *y)
7470 {
7471   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
7472 }
7473
7474 /* Return the DIE associated with a given declaration.  */
7475
7476 static inline dw_die_ref
7477 lookup_decl_die (tree decl)
7478 {
7479   return (dw_die_ref) htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
7480 }
7481
7482 /* Returns a hash value for X (which really is a var_loc_list).  */
7483
7484 static hashval_t
7485 decl_loc_table_hash (const void *x)
7486 {
7487   return (hashval_t) ((const var_loc_list *) x)->decl_id;
7488 }
7489
7490 /* Return nonzero if decl_id of var_loc_list X is the same as
7491    UID of decl *Y.  */
7492
7493 static int
7494 decl_loc_table_eq (const void *x, const void *y)
7495 {
7496   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
7497 }
7498
7499 /* Return the var_loc list associated with a given declaration.  */
7500
7501 static inline var_loc_list *
7502 lookup_decl_loc (const_tree decl)
7503 {
7504   if (!decl_loc_table)
7505     return NULL;
7506   return (var_loc_list *)
7507     htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
7508 }
7509
7510 /* Equate a DIE to a particular declaration.  */
7511
7512 static void
7513 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
7514 {
7515   unsigned int decl_id = DECL_UID (decl);
7516   void **slot;
7517
7518   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
7519   *slot = decl_die;
7520   decl_die->decl_id = decl_id;
7521 }
7522
7523 /* Add a variable location node to the linked list for DECL.  */
7524
7525 static void
7526 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
7527 {
7528   unsigned int decl_id = DECL_UID (decl);
7529   var_loc_list *temp;
7530   void **slot;
7531
7532   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
7533   if (*slot == NULL)
7534     {
7535       temp = GGC_CNEW (var_loc_list);
7536       temp->decl_id = decl_id;
7537       *slot = temp;
7538     }
7539   else
7540     temp = (var_loc_list *) *slot;
7541
7542   if (temp->last)
7543     {
7544       /* If the current location is the same as the end of the list,
7545          and either both or neither of the locations is uninitialized,
7546          we have nothing to do.  */
7547       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
7548                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
7549           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7550                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
7551               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
7552                    == VAR_INIT_STATUS_UNINITIALIZED)
7553                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
7554                       == VAR_INIT_STATUS_UNINITIALIZED))))
7555         {
7556           /* Add LOC to the end of list and update LAST.  */
7557           temp->last->next = loc;
7558           temp->last = loc;
7559         }
7560     }
7561   /* Do not add empty location to the beginning of the list.  */
7562   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
7563     {
7564       temp->first = loc;
7565       temp->last = loc;
7566     }
7567 }
7568 \f
7569 /* Keep track of the number of spaces used to indent the
7570    output of the debugging routines that print the structure of
7571    the DIE internal representation.  */
7572 static int print_indent;
7573
7574 /* Indent the line the number of spaces given by print_indent.  */
7575
7576 static inline void
7577 print_spaces (FILE *outfile)
7578 {
7579   fprintf (outfile, "%*s", print_indent, "");
7580 }
7581
7582 /* Print the information associated with a given DIE, and its children.
7583    This routine is a debugging aid only.  */
7584
7585 static void
7586 print_die (dw_die_ref die, FILE *outfile)
7587 {
7588   dw_attr_ref a;
7589   dw_die_ref c;
7590   unsigned ix;
7591
7592   print_spaces (outfile);
7593   fprintf (outfile, "DIE %4ld: %s\n",
7594            die->die_offset, dwarf_tag_name (die->die_tag));
7595   print_spaces (outfile);
7596   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
7597   fprintf (outfile, " offset: %ld\n", die->die_offset);
7598
7599   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7600     {
7601       print_spaces (outfile);
7602       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
7603
7604       switch (AT_class (a))
7605         {
7606         case dw_val_class_addr:
7607           fprintf (outfile, "address");
7608           break;
7609         case dw_val_class_offset:
7610           fprintf (outfile, "offset");
7611           break;
7612         case dw_val_class_loc:
7613           fprintf (outfile, "location descriptor");
7614           break;
7615         case dw_val_class_loc_list:
7616           fprintf (outfile, "location list -> label:%s",
7617                    AT_loc_list (a)->ll_symbol);
7618           break;
7619         case dw_val_class_range_list:
7620           fprintf (outfile, "range list");
7621           break;
7622         case dw_val_class_const:
7623           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
7624           break;
7625         case dw_val_class_unsigned_const:
7626           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
7627           break;
7628         case dw_val_class_const_double:
7629           fprintf (outfile, "constant ("HOST_WIDE_INT_PRINT_DEC","\
7630                             HOST_WIDE_INT_PRINT_UNSIGNED")",
7631                    a->dw_attr_val.v.val_double.high,
7632                    a->dw_attr_val.v.val_double.low);
7633           break;
7634         case dw_val_class_vec:
7635           fprintf (outfile, "floating-point or vector constant");
7636           break;
7637         case dw_val_class_flag:
7638           fprintf (outfile, "%u", AT_flag (a));
7639           break;
7640         case dw_val_class_die_ref:
7641           if (AT_ref (a) != NULL)
7642             {
7643               if (AT_ref (a)->die_symbol)
7644                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
7645               else
7646                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
7647             }
7648           else
7649             fprintf (outfile, "die -> <null>");
7650           break;
7651         case dw_val_class_lbl_id:
7652         case dw_val_class_lineptr:
7653         case dw_val_class_macptr:
7654           fprintf (outfile, "label: %s", AT_lbl (a));
7655           break;
7656         case dw_val_class_str:
7657           if (AT_string (a) != NULL)
7658             fprintf (outfile, "\"%s\"", AT_string (a));
7659           else
7660             fprintf (outfile, "<null>");
7661           break;
7662         case dw_val_class_file:
7663           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
7664                    AT_file (a)->emitted_number);
7665           break;
7666         default:
7667           break;
7668         }
7669
7670       fprintf (outfile, "\n");
7671     }
7672
7673   if (die->die_child != NULL)
7674     {
7675       print_indent += 4;
7676       FOR_EACH_CHILD (die, c, print_die (c, outfile));
7677       print_indent -= 4;
7678     }
7679   if (print_indent == 0)
7680     fprintf (outfile, "\n");
7681 }
7682
7683 /* Print the contents of the source code line number correspondence table.
7684    This routine is a debugging aid only.  */
7685
7686 static void
7687 print_dwarf_line_table (FILE *outfile)
7688 {
7689   unsigned i;
7690   dw_line_info_ref line_info;
7691
7692   fprintf (outfile, "\n\nDWARF source line information\n");
7693   for (i = 1; i < line_info_table_in_use; i++)
7694     {
7695       line_info = &line_info_table[i];
7696       fprintf (outfile, "%5d: %4ld %6ld\n", i,
7697                line_info->dw_file_num,
7698                line_info->dw_line_num);
7699     }
7700
7701   fprintf (outfile, "\n\n");
7702 }
7703
7704 /* Print the information collected for a given DIE.  */
7705
7706 void
7707 debug_dwarf_die (dw_die_ref die)
7708 {
7709   print_die (die, stderr);
7710 }
7711
7712 /* Print all DWARF information collected for the compilation unit.
7713    This routine is a debugging aid only.  */
7714
7715 void
7716 debug_dwarf (void)
7717 {
7718   print_indent = 0;
7719   print_die (comp_unit_die, stderr);
7720   if (! DWARF2_ASM_LINE_DEBUG_INFO)
7721     print_dwarf_line_table (stderr);
7722 }
7723 \f
7724 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
7725    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
7726    DIE that marks the start of the DIEs for this include file.  */
7727
7728 static dw_die_ref
7729 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
7730 {
7731   const char *filename = get_AT_string (bincl_die, DW_AT_name);
7732   dw_die_ref new_unit = gen_compile_unit_die (filename);
7733
7734   new_unit->die_sib = old_unit;
7735   return new_unit;
7736 }
7737
7738 /* Close an include-file CU and reopen the enclosing one.  */
7739
7740 static dw_die_ref
7741 pop_compile_unit (dw_die_ref old_unit)
7742 {
7743   dw_die_ref new_unit = old_unit->die_sib;
7744
7745   old_unit->die_sib = NULL;
7746   return new_unit;
7747 }
7748
7749 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
7750 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
7751
7752 /* Calculate the checksum of a location expression.  */
7753
7754 static inline void
7755 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
7756 {
7757   int tem;
7758
7759   tem = (loc->dtprel << 8) | ((unsigned int) loc->dw_loc_opc);
7760   CHECKSUM (tem);
7761   CHECKSUM (loc->dw_loc_oprnd1);
7762   CHECKSUM (loc->dw_loc_oprnd2);
7763 }
7764
7765 /* Calculate the checksum of an attribute.  */
7766
7767 static void
7768 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
7769 {
7770   dw_loc_descr_ref loc;
7771   rtx r;
7772
7773   CHECKSUM (at->dw_attr);
7774
7775   /* We don't care that this was compiled with a different compiler
7776      snapshot; if the output is the same, that's what matters.  */
7777   if (at->dw_attr == DW_AT_producer)
7778     return;
7779
7780   switch (AT_class (at))
7781     {
7782     case dw_val_class_const:
7783       CHECKSUM (at->dw_attr_val.v.val_int);
7784       break;
7785     case dw_val_class_unsigned_const:
7786       CHECKSUM (at->dw_attr_val.v.val_unsigned);
7787       break;
7788     case dw_val_class_const_double:
7789       CHECKSUM (at->dw_attr_val.v.val_double);
7790       break;
7791     case dw_val_class_vec:
7792       CHECKSUM (at->dw_attr_val.v.val_vec);
7793       break;
7794     case dw_val_class_flag:
7795       CHECKSUM (at->dw_attr_val.v.val_flag);
7796       break;
7797     case dw_val_class_str:
7798       CHECKSUM_STRING (AT_string (at));
7799       break;
7800
7801     case dw_val_class_addr:
7802       r = AT_addr (at);
7803       gcc_assert (GET_CODE (r) == SYMBOL_REF);
7804       CHECKSUM_STRING (XSTR (r, 0));
7805       break;
7806
7807     case dw_val_class_offset:
7808       CHECKSUM (at->dw_attr_val.v.val_offset);
7809       break;
7810
7811     case dw_val_class_loc:
7812       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
7813         loc_checksum (loc, ctx);
7814       break;
7815
7816     case dw_val_class_die_ref:
7817       die_checksum (AT_ref (at), ctx, mark);
7818       break;
7819
7820     case dw_val_class_fde_ref:
7821     case dw_val_class_lbl_id:
7822     case dw_val_class_lineptr:
7823     case dw_val_class_macptr:
7824       break;
7825
7826     case dw_val_class_file:
7827       CHECKSUM_STRING (AT_file (at)->filename);
7828       break;
7829
7830     default:
7831       break;
7832     }
7833 }
7834
7835 /* Calculate the checksum of a DIE.  */
7836
7837 static void
7838 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
7839 {
7840   dw_die_ref c;
7841   dw_attr_ref a;
7842   unsigned ix;
7843
7844   /* To avoid infinite recursion.  */
7845   if (die->die_mark)
7846     {
7847       CHECKSUM (die->die_mark);
7848       return;
7849     }
7850   die->die_mark = ++(*mark);
7851
7852   CHECKSUM (die->die_tag);
7853
7854   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7855     attr_checksum (a, ctx, mark);
7856
7857   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
7858 }
7859
7860 #undef CHECKSUM
7861 #undef CHECKSUM_STRING
7862
7863 /* Do the location expressions look same?  */
7864 static inline int
7865 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
7866 {
7867   return loc1->dw_loc_opc == loc2->dw_loc_opc
7868          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
7869          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
7870 }
7871
7872 /* Do the values look the same?  */
7873 static int
7874 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
7875 {
7876   dw_loc_descr_ref loc1, loc2;
7877   rtx r1, r2;
7878
7879   if (v1->val_class != v2->val_class)
7880     return 0;
7881
7882   switch (v1->val_class)
7883     {
7884     case dw_val_class_const:
7885       return v1->v.val_int == v2->v.val_int;
7886     case dw_val_class_unsigned_const:
7887       return v1->v.val_unsigned == v2->v.val_unsigned;
7888     case dw_val_class_const_double:
7889       return v1->v.val_double.high == v2->v.val_double.high
7890              && v1->v.val_double.low == v2->v.val_double.low;
7891     case dw_val_class_vec:
7892       if (v1->v.val_vec.length != v2->v.val_vec.length
7893           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
7894         return 0;
7895       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
7896                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
7897         return 0;
7898       return 1;
7899     case dw_val_class_flag:
7900       return v1->v.val_flag == v2->v.val_flag;
7901     case dw_val_class_str:
7902       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
7903
7904     case dw_val_class_addr:
7905       r1 = v1->v.val_addr;
7906       r2 = v2->v.val_addr;
7907       if (GET_CODE (r1) != GET_CODE (r2))
7908         return 0;
7909       return !rtx_equal_p (r1, r2);
7910
7911     case dw_val_class_offset:
7912       return v1->v.val_offset == v2->v.val_offset;
7913
7914     case dw_val_class_loc:
7915       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
7916            loc1 && loc2;
7917            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
7918         if (!same_loc_p (loc1, loc2, mark))
7919           return 0;
7920       return !loc1 && !loc2;
7921
7922     case dw_val_class_die_ref:
7923       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
7924
7925     case dw_val_class_fde_ref:
7926     case dw_val_class_lbl_id:
7927     case dw_val_class_lineptr:
7928     case dw_val_class_macptr:
7929       return 1;
7930
7931     case dw_val_class_file:
7932       return v1->v.val_file == v2->v.val_file;
7933
7934     default:
7935       return 1;
7936     }
7937 }
7938
7939 /* Do the attributes look the same?  */
7940
7941 static int
7942 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
7943 {
7944   if (at1->dw_attr != at2->dw_attr)
7945     return 0;
7946
7947   /* We don't care that this was compiled with a different compiler
7948      snapshot; if the output is the same, that's what matters. */
7949   if (at1->dw_attr == DW_AT_producer)
7950     return 1;
7951
7952   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
7953 }
7954
7955 /* Do the dies look the same?  */
7956
7957 static int
7958 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
7959 {
7960   dw_die_ref c1, c2;
7961   dw_attr_ref a1;
7962   unsigned ix;
7963
7964   /* To avoid infinite recursion.  */
7965   if (die1->die_mark)
7966     return die1->die_mark == die2->die_mark;
7967   die1->die_mark = die2->die_mark = ++(*mark);
7968
7969   if (die1->die_tag != die2->die_tag)
7970     return 0;
7971
7972   if (VEC_length (dw_attr_node, die1->die_attr)
7973       != VEC_length (dw_attr_node, die2->die_attr))
7974     return 0;
7975
7976   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
7977     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
7978       return 0;
7979
7980   c1 = die1->die_child;
7981   c2 = die2->die_child;
7982   if (! c1)
7983     {
7984       if (c2)
7985         return 0;
7986     }
7987   else
7988     for (;;)
7989       {
7990         if (!same_die_p (c1, c2, mark))
7991           return 0;
7992         c1 = c1->die_sib;
7993         c2 = c2->die_sib;
7994         if (c1 == die1->die_child)
7995           {
7996             if (c2 == die2->die_child)
7997               break;
7998             else
7999               return 0;
8000           }
8001     }
8002
8003   return 1;
8004 }
8005
8006 /* Do the dies look the same?  Wrapper around same_die_p.  */
8007
8008 static int
8009 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
8010 {
8011   int mark = 0;
8012   int ret = same_die_p (die1, die2, &mark);
8013
8014   unmark_all_dies (die1);
8015   unmark_all_dies (die2);
8016
8017   return ret;
8018 }
8019
8020 /* The prefix to attach to symbols on DIEs in the current comdat debug
8021    info section.  */
8022 static char *comdat_symbol_id;
8023
8024 /* The index of the current symbol within the current comdat CU.  */
8025 static unsigned int comdat_symbol_number;
8026
8027 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
8028    children, and set comdat_symbol_id accordingly.  */
8029
8030 static void
8031 compute_section_prefix (dw_die_ref unit_die)
8032 {
8033   const char *die_name = get_AT_string (unit_die, DW_AT_name);
8034   const char *base = die_name ? lbasename (die_name) : "anonymous";
8035   char *name = XALLOCAVEC (char, strlen (base) + 64);
8036   char *p;
8037   int i, mark;
8038   unsigned char checksum[16];
8039   struct md5_ctx ctx;
8040
8041   /* Compute the checksum of the DIE, then append part of it as hex digits to
8042      the name filename of the unit.  */
8043
8044   md5_init_ctx (&ctx);
8045   mark = 0;
8046   die_checksum (unit_die, &ctx, &mark);
8047   unmark_all_dies (unit_die);
8048   md5_finish_ctx (&ctx, checksum);
8049
8050   sprintf (name, "%s.", base);
8051   clean_symbol_name (name);
8052
8053   p = name + strlen (name);
8054   for (i = 0; i < 4; i++)
8055     {
8056       sprintf (p, "%.2x", checksum[i]);
8057       p += 2;
8058     }
8059
8060   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
8061   comdat_symbol_number = 0;
8062 }
8063
8064 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
8065
8066 static int
8067 is_type_die (dw_die_ref die)
8068 {
8069   switch (die->die_tag)
8070     {
8071     case DW_TAG_array_type:
8072     case DW_TAG_class_type:
8073     case DW_TAG_interface_type:
8074     case DW_TAG_enumeration_type:
8075     case DW_TAG_pointer_type:
8076     case DW_TAG_reference_type:
8077     case DW_TAG_string_type:
8078     case DW_TAG_structure_type:
8079     case DW_TAG_subroutine_type:
8080     case DW_TAG_union_type:
8081     case DW_TAG_ptr_to_member_type:
8082     case DW_TAG_set_type:
8083     case DW_TAG_subrange_type:
8084     case DW_TAG_base_type:
8085     case DW_TAG_const_type:
8086     case DW_TAG_file_type:
8087     case DW_TAG_packed_type:
8088     case DW_TAG_volatile_type:
8089     case DW_TAG_typedef:
8090       return 1;
8091     default:
8092       return 0;
8093     }
8094 }
8095
8096 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
8097    Basically, we want to choose the bits that are likely to be shared between
8098    compilations (types) and leave out the bits that are specific to individual
8099    compilations (functions).  */
8100
8101 static int
8102 is_comdat_die (dw_die_ref c)
8103 {
8104   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
8105      we do for stabs.  The advantage is a greater likelihood of sharing between
8106      objects that don't include headers in the same order (and therefore would
8107      put the base types in a different comdat).  jason 8/28/00 */
8108
8109   if (c->die_tag == DW_TAG_base_type)
8110     return 0;
8111
8112   if (c->die_tag == DW_TAG_pointer_type
8113       || c->die_tag == DW_TAG_reference_type
8114       || c->die_tag == DW_TAG_const_type
8115       || c->die_tag == DW_TAG_volatile_type)
8116     {
8117       dw_die_ref t = get_AT_ref (c, DW_AT_type);
8118
8119       return t ? is_comdat_die (t) : 0;
8120     }
8121
8122   return is_type_die (c);
8123 }
8124
8125 /* Returns 1 iff C is the sort of DIE that might be referred to from another
8126    compilation unit.  */
8127
8128 static int
8129 is_symbol_die (dw_die_ref c)
8130 {
8131   return (is_type_die (c)
8132           || (get_AT (c, DW_AT_declaration)
8133               && !get_AT (c, DW_AT_specification))
8134           || c->die_tag == DW_TAG_namespace
8135           || c->die_tag == DW_TAG_module);
8136 }
8137
8138 static char *
8139 gen_internal_sym (const char *prefix)
8140 {
8141   char buf[256];
8142
8143   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
8144   return xstrdup (buf);
8145 }
8146
8147 /* Assign symbols to all worthy DIEs under DIE.  */
8148
8149 static void
8150 assign_symbol_names (dw_die_ref die)
8151 {
8152   dw_die_ref c;
8153
8154   if (is_symbol_die (die))
8155     {
8156       if (comdat_symbol_id)
8157         {
8158           char *p = XALLOCAVEC (char, strlen (comdat_symbol_id) + 64);
8159
8160           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
8161                    comdat_symbol_id, comdat_symbol_number++);
8162           die->die_symbol = xstrdup (p);
8163         }
8164       else
8165         die->die_symbol = gen_internal_sym ("LDIE");
8166     }
8167
8168   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
8169 }
8170
8171 struct cu_hash_table_entry
8172 {
8173   dw_die_ref cu;
8174   unsigned min_comdat_num, max_comdat_num;
8175   struct cu_hash_table_entry *next;
8176 };
8177
8178 /* Routines to manipulate hash table of CUs.  */
8179 static hashval_t
8180 htab_cu_hash (const void *of)
8181 {
8182   const struct cu_hash_table_entry *const entry =
8183     (const struct cu_hash_table_entry *) of;
8184
8185   return htab_hash_string (entry->cu->die_symbol);
8186 }
8187
8188 static int
8189 htab_cu_eq (const void *of1, const void *of2)
8190 {
8191   const struct cu_hash_table_entry *const entry1 =
8192     (const struct cu_hash_table_entry *) of1;
8193   const struct die_struct *const entry2 = (const struct die_struct *) of2;
8194
8195   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
8196 }
8197
8198 static void
8199 htab_cu_del (void *what)
8200 {
8201   struct cu_hash_table_entry *next,
8202     *entry = (struct cu_hash_table_entry *) what;
8203
8204   while (entry)
8205     {
8206       next = entry->next;
8207       free (entry);
8208       entry = next;
8209     }
8210 }
8211
8212 /* Check whether we have already seen this CU and set up SYM_NUM
8213    accordingly.  */
8214 static int
8215 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
8216 {
8217   struct cu_hash_table_entry dummy;
8218   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
8219
8220   dummy.max_comdat_num = 0;
8221
8222   slot = (struct cu_hash_table_entry **)
8223     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8224         INSERT);
8225   entry = *slot;
8226
8227   for (; entry; last = entry, entry = entry->next)
8228     {
8229       if (same_die_p_wrap (cu, entry->cu))
8230         break;
8231     }
8232
8233   if (entry)
8234     {
8235       *sym_num = entry->min_comdat_num;
8236       return 1;
8237     }
8238
8239   entry = XCNEW (struct cu_hash_table_entry);
8240   entry->cu = cu;
8241   entry->min_comdat_num = *sym_num = last->max_comdat_num;
8242   entry->next = *slot;
8243   *slot = entry;
8244
8245   return 0;
8246 }
8247
8248 /* Record SYM_NUM to record of CU in HTABLE.  */
8249 static void
8250 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
8251 {
8252   struct cu_hash_table_entry **slot, *entry;
8253
8254   slot = (struct cu_hash_table_entry **)
8255     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
8256         NO_INSERT);
8257   entry = *slot;
8258
8259   entry->max_comdat_num = sym_num;
8260 }
8261
8262 /* Traverse the DIE (which is always comp_unit_die), and set up
8263    additional compilation units for each of the include files we see
8264    bracketed by BINCL/EINCL.  */
8265
8266 static void
8267 break_out_includes (dw_die_ref die)
8268 {
8269   dw_die_ref c;
8270   dw_die_ref unit = NULL;
8271   limbo_die_node *node, **pnode;
8272   htab_t cu_hash_table;
8273
8274   c = die->die_child;
8275   if (c) do {
8276     dw_die_ref prev = c;
8277     c = c->die_sib;
8278     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
8279            || (unit && is_comdat_die (c)))
8280       {
8281         dw_die_ref next = c->die_sib;
8282
8283         /* This DIE is for a secondary CU; remove it from the main one.  */
8284         remove_child_with_prev (c, prev);
8285
8286         if (c->die_tag == DW_TAG_GNU_BINCL)
8287           unit = push_new_compile_unit (unit, c);
8288         else if (c->die_tag == DW_TAG_GNU_EINCL)
8289           unit = pop_compile_unit (unit);
8290         else
8291           add_child_die (unit, c);
8292         c = next;
8293         if (c == die->die_child)
8294           break;
8295       }
8296   } while (c != die->die_child);
8297
8298 #if 0
8299   /* We can only use this in debugging, since the frontend doesn't check
8300      to make sure that we leave every include file we enter.  */
8301   gcc_assert (!unit);
8302 #endif
8303
8304   assign_symbol_names (die);
8305   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
8306   for (node = limbo_die_list, pnode = &limbo_die_list;
8307        node;
8308        node = node->next)
8309     {
8310       int is_dupl;
8311
8312       compute_section_prefix (node->die);
8313       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
8314                         &comdat_symbol_number);
8315       assign_symbol_names (node->die);
8316       if (is_dupl)
8317         *pnode = node->next;
8318       else
8319         {
8320           pnode = &node->next;
8321           record_comdat_symbol_number (node->die, cu_hash_table,
8322                 comdat_symbol_number);
8323         }
8324     }
8325   htab_delete (cu_hash_table);
8326 }
8327
8328 /* Traverse the DIE and add a sibling attribute if it may have the
8329    effect of speeding up access to siblings.  To save some space,
8330    avoid generating sibling attributes for DIE's without children.  */
8331
8332 static void
8333 add_sibling_attributes (dw_die_ref die)
8334 {
8335   dw_die_ref c;
8336
8337   if (! die->die_child)
8338     return;
8339
8340   if (die->die_parent && die != die->die_parent->die_child)
8341     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
8342
8343   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
8344 }
8345
8346 /* Output all location lists for the DIE and its children.  */
8347
8348 static void
8349 output_location_lists (dw_die_ref die)
8350 {
8351   dw_die_ref c;
8352   dw_attr_ref a;
8353   unsigned ix;
8354
8355   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8356     if (AT_class (a) == dw_val_class_loc_list)
8357       output_loc_list (AT_loc_list (a));
8358
8359   FOR_EACH_CHILD (die, c, output_location_lists (c));
8360 }
8361
8362 /* The format of each DIE (and its attribute value pairs) is encoded in an
8363    abbreviation table.  This routine builds the abbreviation table and assigns
8364    a unique abbreviation id for each abbreviation entry.  The children of each
8365    die are visited recursively.  */
8366
8367 static void
8368 build_abbrev_table (dw_die_ref die)
8369 {
8370   unsigned long abbrev_id;
8371   unsigned int n_alloc;
8372   dw_die_ref c;
8373   dw_attr_ref a;
8374   unsigned ix;
8375
8376   /* Scan the DIE references, and mark as external any that refer to
8377      DIEs from other CUs (i.e. those which are not marked).  */
8378   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8379     if (AT_class (a) == dw_val_class_die_ref
8380         && AT_ref (a)->die_mark == 0)
8381       {
8382         gcc_assert (AT_ref (a)->die_symbol);
8383         set_AT_ref_external (a, 1);
8384       }
8385
8386   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8387     {
8388       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8389       dw_attr_ref die_a, abbrev_a;
8390       unsigned ix;
8391       bool ok = true;
8392
8393       if (abbrev->die_tag != die->die_tag)
8394         continue;
8395       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
8396         continue;
8397
8398       if (VEC_length (dw_attr_node, abbrev->die_attr)
8399           != VEC_length (dw_attr_node, die->die_attr))
8400         continue;
8401
8402       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
8403         {
8404           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
8405           if ((abbrev_a->dw_attr != die_a->dw_attr)
8406               || (value_format (abbrev_a) != value_format (die_a)))
8407             {
8408               ok = false;
8409               break;
8410             }
8411         }
8412       if (ok)
8413         break;
8414     }
8415
8416   if (abbrev_id >= abbrev_die_table_in_use)
8417     {
8418       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
8419         {
8420           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
8421           abbrev_die_table = GGC_RESIZEVEC (dw_die_ref, abbrev_die_table,
8422                                             n_alloc);
8423
8424           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
8425                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
8426           abbrev_die_table_allocated = n_alloc;
8427         }
8428
8429       ++abbrev_die_table_in_use;
8430       abbrev_die_table[abbrev_id] = die;
8431     }
8432
8433   die->die_abbrev = abbrev_id;
8434   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
8435 }
8436 \f
8437 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
8438
8439 static int
8440 constant_size (unsigned HOST_WIDE_INT value)
8441 {
8442   int log;
8443
8444   if (value == 0)
8445     log = 0;
8446   else
8447     log = floor_log2 (value);
8448
8449   log = log / 8;
8450   log = 1 << (floor_log2 (log) + 1);
8451
8452   return log;
8453 }
8454
8455 /* Return the size of a DIE as it is represented in the
8456    .debug_info section.  */
8457
8458 static unsigned long
8459 size_of_die (dw_die_ref die)
8460 {
8461   unsigned long size = 0;
8462   dw_attr_ref a;
8463   unsigned ix;
8464
8465   size += size_of_uleb128 (die->die_abbrev);
8466   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8467     {
8468       switch (AT_class (a))
8469         {
8470         case dw_val_class_addr:
8471           size += DWARF2_ADDR_SIZE;
8472           break;
8473         case dw_val_class_offset:
8474           size += DWARF_OFFSET_SIZE;
8475           break;
8476         case dw_val_class_loc:
8477           {
8478             unsigned long lsize = size_of_locs (AT_loc (a));
8479
8480             /* Block length.  */
8481             size += constant_size (lsize);
8482             size += lsize;
8483           }
8484           break;
8485         case dw_val_class_loc_list:
8486           size += DWARF_OFFSET_SIZE;
8487           break;
8488         case dw_val_class_range_list:
8489           size += DWARF_OFFSET_SIZE;
8490           break;
8491         case dw_val_class_const:
8492           size += size_of_sleb128 (AT_int (a));
8493           break;
8494         case dw_val_class_unsigned_const:
8495           size += constant_size (AT_unsigned (a));
8496           break;
8497         case dw_val_class_const_double:
8498           size += 2 * HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR;
8499           if (HOST_BITS_PER_WIDE_INT >= 64)
8500             size++; /* block */
8501           break;
8502         case dw_val_class_vec:
8503           size += constant_size (a->dw_attr_val.v.val_vec.length
8504                                  * a->dw_attr_val.v.val_vec.elt_size)
8505                   + a->dw_attr_val.v.val_vec.length
8506                     * a->dw_attr_val.v.val_vec.elt_size; /* block */
8507           break;
8508         case dw_val_class_flag:
8509           size += 1;
8510           break;
8511         case dw_val_class_die_ref:
8512           /* In DWARF2, DW_FORM_ref_addr is sized by target address length,
8513              whereas in DWARF3 it's always sized as an offset.  */
8514           if (AT_ref_external (a) && dwarf_version == 2)
8515             size += DWARF2_ADDR_SIZE;
8516           else
8517             size += DWARF_OFFSET_SIZE;
8518           break;
8519         case dw_val_class_fde_ref:
8520           size += DWARF_OFFSET_SIZE;
8521           break;
8522         case dw_val_class_lbl_id:
8523           size += DWARF2_ADDR_SIZE;
8524           break;
8525         case dw_val_class_lineptr:
8526         case dw_val_class_macptr:
8527           size += DWARF_OFFSET_SIZE;
8528           break;
8529         case dw_val_class_str:
8530           if (AT_string_form (a) == DW_FORM_strp)
8531             size += DWARF_OFFSET_SIZE;
8532           else
8533             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
8534           break;
8535         case dw_val_class_file:
8536           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
8537           break;
8538         default:
8539           gcc_unreachable ();
8540         }
8541     }
8542
8543   return size;
8544 }
8545
8546 /* Size the debugging information associated with a given DIE.  Visits the
8547    DIE's children recursively.  Updates the global variable next_die_offset, on
8548    each time through.  Uses the current value of next_die_offset to update the
8549    die_offset field in each DIE.  */
8550
8551 static void
8552 calc_die_sizes (dw_die_ref die)
8553 {
8554   dw_die_ref c;
8555
8556   die->die_offset = next_die_offset;
8557   next_die_offset += size_of_die (die);
8558
8559   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
8560
8561   if (die->die_child != NULL)
8562     /* Count the null byte used to terminate sibling lists.  */
8563     next_die_offset += 1;
8564 }
8565
8566 /* Set the marks for a die and its children.  We do this so
8567    that we know whether or not a reference needs to use FORM_ref_addr; only
8568    DIEs in the same CU will be marked.  We used to clear out the offset
8569    and use that as the flag, but ran into ordering problems.  */
8570
8571 static void
8572 mark_dies (dw_die_ref die)
8573 {
8574   dw_die_ref c;
8575
8576   gcc_assert (!die->die_mark);
8577
8578   die->die_mark = 1;
8579   FOR_EACH_CHILD (die, c, mark_dies (c));
8580 }
8581
8582 /* Clear the marks for a die and its children.  */
8583
8584 static void
8585 unmark_dies (dw_die_ref die)
8586 {
8587   dw_die_ref c;
8588
8589   gcc_assert (die->die_mark);
8590
8591   die->die_mark = 0;
8592   FOR_EACH_CHILD (die, c, unmark_dies (c));
8593 }
8594
8595 /* Clear the marks for a die, its children and referred dies.  */
8596
8597 static void
8598 unmark_all_dies (dw_die_ref die)
8599 {
8600   dw_die_ref c;
8601   dw_attr_ref a;
8602   unsigned ix;
8603
8604   if (!die->die_mark)
8605     return;
8606   die->die_mark = 0;
8607
8608   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
8609
8610   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8611     if (AT_class (a) == dw_val_class_die_ref)
8612       unmark_all_dies (AT_ref (a));
8613 }
8614
8615 /* Return the size of the .debug_pubnames or .debug_pubtypes table
8616    generated for the compilation unit.  */
8617
8618 static unsigned long
8619 size_of_pubnames (VEC (pubname_entry, gc) * names)
8620 {
8621   unsigned long size;
8622   unsigned i;
8623   pubname_ref p;
8624
8625   size = DWARF_PUBNAMES_HEADER_SIZE;
8626   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
8627     if (names != pubtype_table
8628         || p->die->die_offset != 0
8629         || !flag_eliminate_unused_debug_types)
8630       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
8631
8632   size += DWARF_OFFSET_SIZE;
8633   return size;
8634 }
8635
8636 /* Return the size of the information in the .debug_aranges section.  */
8637
8638 static unsigned long
8639 size_of_aranges (void)
8640 {
8641   unsigned long size;
8642
8643   size = DWARF_ARANGES_HEADER_SIZE;
8644
8645   /* Count the address/length pair for this compilation unit.  */
8646   if (text_section_used)
8647     size += 2 * DWARF2_ADDR_SIZE;
8648   if (cold_text_section_used)
8649     size += 2 * DWARF2_ADDR_SIZE;
8650   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
8651
8652   /* Count the two zero words used to terminated the address range table.  */
8653   size += 2 * DWARF2_ADDR_SIZE;
8654   return size;
8655 }
8656 \f
8657 /* Select the encoding of an attribute value.  */
8658
8659 static enum dwarf_form
8660 value_format (dw_attr_ref a)
8661 {
8662   switch (a->dw_attr_val.val_class)
8663     {
8664     case dw_val_class_addr:
8665       /* Only very few attributes allow DW_FORM_addr.  */
8666       switch (a->dw_attr)
8667         {
8668         case DW_AT_low_pc:
8669         case DW_AT_high_pc:
8670         case DW_AT_entry_pc:
8671         case DW_AT_trampoline:
8672           return DW_FORM_addr;
8673         default:
8674           break;
8675         }
8676       switch (DWARF2_ADDR_SIZE)
8677         {
8678         case 1:
8679           return DW_FORM_data1;
8680         case 2:
8681           return DW_FORM_data2;
8682         case 4:
8683           return DW_FORM_data4;
8684         case 8:
8685           return DW_FORM_data8;
8686         default:
8687           gcc_unreachable ();
8688         }
8689     case dw_val_class_range_list:
8690     case dw_val_class_offset:
8691     case dw_val_class_loc_list:
8692       switch (DWARF_OFFSET_SIZE)
8693         {
8694         case 4:
8695           return DW_FORM_data4;
8696         case 8:
8697           return DW_FORM_data8;
8698         default:
8699           gcc_unreachable ();
8700         }
8701     case dw_val_class_loc:
8702       switch (constant_size (size_of_locs (AT_loc (a))))
8703         {
8704         case 1:
8705           return DW_FORM_block1;
8706         case 2:
8707           return DW_FORM_block2;
8708         default:
8709           gcc_unreachable ();
8710         }
8711     case dw_val_class_const:
8712       return DW_FORM_sdata;
8713     case dw_val_class_unsigned_const:
8714       switch (constant_size (AT_unsigned (a)))
8715         {
8716         case 1:
8717           return DW_FORM_data1;
8718         case 2:
8719           return DW_FORM_data2;
8720         case 4:
8721           return DW_FORM_data4;
8722         case 8:
8723           return DW_FORM_data8;
8724         default:
8725           gcc_unreachable ();
8726         }
8727     case dw_val_class_const_double:
8728       switch (HOST_BITS_PER_WIDE_INT)
8729         {
8730         case 8:
8731           return DW_FORM_data2;
8732         case 16:
8733           return DW_FORM_data4;
8734         case 32:
8735           return DW_FORM_data8;
8736         case 64:
8737         default:
8738           return DW_FORM_block1;
8739         }
8740     case dw_val_class_vec:
8741       switch (constant_size (a->dw_attr_val.v.val_vec.length
8742                              * a->dw_attr_val.v.val_vec.elt_size))
8743         {
8744         case 1:
8745           return DW_FORM_block1;
8746         case 2:
8747           return DW_FORM_block2;
8748         case 4:
8749           return DW_FORM_block4;
8750         default:
8751           gcc_unreachable ();
8752         }
8753     case dw_val_class_flag:
8754       return DW_FORM_flag;
8755     case dw_val_class_die_ref:
8756       if (AT_ref_external (a))
8757         return DW_FORM_ref_addr;
8758       else
8759         return DW_FORM_ref;
8760     case dw_val_class_fde_ref:
8761       return DW_FORM_data;
8762     case dw_val_class_lbl_id:
8763       return DW_FORM_addr;
8764     case dw_val_class_lineptr:
8765     case dw_val_class_macptr:
8766       return DW_FORM_data;
8767     case dw_val_class_str:
8768       return AT_string_form (a);
8769     case dw_val_class_file:
8770       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
8771         {
8772         case 1:
8773           return DW_FORM_data1;
8774         case 2:
8775           return DW_FORM_data2;
8776         case 4:
8777           return DW_FORM_data4;
8778         default:
8779           gcc_unreachable ();
8780         }
8781
8782     default:
8783       gcc_unreachable ();
8784     }
8785 }
8786
8787 /* Output the encoding of an attribute value.  */
8788
8789 static void
8790 output_value_format (dw_attr_ref a)
8791 {
8792   enum dwarf_form form = value_format (a);
8793
8794   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
8795 }
8796
8797 /* Output the .debug_abbrev section which defines the DIE abbreviation
8798    table.  */
8799
8800 static void
8801 output_abbrev_section (void)
8802 {
8803   unsigned long abbrev_id;
8804
8805   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
8806     {
8807       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
8808       unsigned ix;
8809       dw_attr_ref a_attr;
8810
8811       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
8812       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
8813                                    dwarf_tag_name (abbrev->die_tag));
8814
8815       if (abbrev->die_child != NULL)
8816         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
8817       else
8818         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
8819
8820       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
8821            ix++)
8822         {
8823           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
8824                                        dwarf_attr_name (a_attr->dw_attr));
8825           output_value_format (a_attr);
8826         }
8827
8828       dw2_asm_output_data (1, 0, NULL);
8829       dw2_asm_output_data (1, 0, NULL);
8830     }
8831
8832   /* Terminate the table.  */
8833   dw2_asm_output_data (1, 0, NULL);
8834 }
8835
8836 /* Output a symbol we can use to refer to this DIE from another CU.  */
8837
8838 static inline void
8839 output_die_symbol (dw_die_ref die)
8840 {
8841   char *sym = die->die_symbol;
8842
8843   if (sym == 0)
8844     return;
8845
8846   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
8847     /* We make these global, not weak; if the target doesn't support
8848        .linkonce, it doesn't support combining the sections, so debugging
8849        will break.  */
8850     targetm.asm_out.globalize_label (asm_out_file, sym);
8851
8852   ASM_OUTPUT_LABEL (asm_out_file, sym);
8853 }
8854
8855 /* Return a new location list, given the begin and end range, and the
8856    expression. gensym tells us whether to generate a new internal symbol for
8857    this location list node, which is done for the head of the list only.  */
8858
8859 static inline dw_loc_list_ref
8860 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
8861               const char *section, unsigned int gensym)
8862 {
8863   dw_loc_list_ref retlist = GGC_CNEW (dw_loc_list_node);
8864
8865   retlist->begin = begin;
8866   retlist->end = end;
8867   retlist->expr = expr;
8868   retlist->section = section;
8869   if (gensym)
8870     retlist->ll_symbol = gen_internal_sym ("LLST");
8871
8872   return retlist;
8873 }
8874
8875 /* Add a location description expression to a location list.  */
8876
8877 static inline void
8878 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
8879                            const char *begin, const char *end,
8880                            const char *section)
8881 {
8882   dw_loc_list_ref *d;
8883
8884   /* Find the end of the chain.  */
8885   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
8886     ;
8887
8888   /* Add a new location list node to the list.  */
8889   *d = new_loc_list (descr, begin, end, section, 0);
8890 }
8891
8892 /* Output the location list given to us.  */
8893
8894 static void
8895 output_loc_list (dw_loc_list_ref list_head)
8896 {
8897   dw_loc_list_ref curr = list_head;
8898
8899   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
8900
8901   /* Walk the location list, and output each range + expression.  */
8902   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
8903     {
8904       unsigned long size;
8905       /* Don't output an entry that starts and ends at the same address.  */
8906       if (strcmp (curr->begin, curr->end) == 0)
8907         continue;
8908       if (!have_multiple_function_sections)
8909         {
8910           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
8911                                 "Location list begin address (%s)",
8912                                 list_head->ll_symbol);
8913           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
8914                                 "Location list end address (%s)",
8915                                 list_head->ll_symbol);
8916         }
8917       else
8918         {
8919           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
8920                                "Location list begin address (%s)",
8921                                list_head->ll_symbol);
8922           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
8923                                "Location list end address (%s)",
8924                                list_head->ll_symbol);
8925         }
8926       size = size_of_locs (curr->expr);
8927
8928       /* Output the block length for this list of location operations.  */
8929       gcc_assert (size <= 0xffff);
8930       dw2_asm_output_data (2, size, "%s", "Location expression size");
8931
8932       output_loc_sequence (curr->expr);
8933     }
8934
8935   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8936                        "Location list terminator begin (%s)",
8937                        list_head->ll_symbol);
8938   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
8939                        "Location list terminator end (%s)",
8940                        list_head->ll_symbol);
8941 }
8942
8943 /* Output the DIE and its attributes.  Called recursively to generate
8944    the definitions of each child DIE.  */
8945
8946 static void
8947 output_die (dw_die_ref die)
8948 {
8949   dw_attr_ref a;
8950   dw_die_ref c;
8951   unsigned long size;
8952   unsigned ix;
8953
8954   /* If someone in another CU might refer to us, set up a symbol for
8955      them to point to.  */
8956   if (die->die_symbol)
8957     output_die_symbol (die);
8958
8959   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
8960                                (unsigned long)die->die_offset,
8961                                dwarf_tag_name (die->die_tag));
8962
8963   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
8964     {
8965       const char *name = dwarf_attr_name (a->dw_attr);
8966
8967       switch (AT_class (a))
8968         {
8969         case dw_val_class_addr:
8970           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
8971           break;
8972
8973         case dw_val_class_offset:
8974           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
8975                                "%s", name);
8976           break;
8977
8978         case dw_val_class_range_list:
8979           {
8980             char *p = strchr (ranges_section_label, '\0');
8981
8982             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
8983                      a->dw_attr_val.v.val_offset);
8984             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
8985                                    debug_ranges_section, "%s", name);
8986             *p = '\0';
8987           }
8988           break;
8989
8990         case dw_val_class_loc:
8991           size = size_of_locs (AT_loc (a));
8992
8993           /* Output the block length for this list of location operations.  */
8994           dw2_asm_output_data (constant_size (size), size, "%s", name);
8995
8996           output_loc_sequence (AT_loc (a));
8997           break;
8998
8999         case dw_val_class_const:
9000           /* ??? It would be slightly more efficient to use a scheme like is
9001              used for unsigned constants below, but gdb 4.x does not sign
9002              extend.  Gdb 5.x does sign extend.  */
9003           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
9004           break;
9005
9006         case dw_val_class_unsigned_const:
9007           dw2_asm_output_data (constant_size (AT_unsigned (a)),
9008                                AT_unsigned (a), "%s", name);
9009           break;
9010
9011         case dw_val_class_const_double:
9012           {
9013             unsigned HOST_WIDE_INT first, second;
9014
9015             if (HOST_BITS_PER_WIDE_INT >= 64)
9016               dw2_asm_output_data (1,
9017                                    2 * HOST_BITS_PER_WIDE_INT
9018                                    / HOST_BITS_PER_CHAR,
9019                                    NULL);
9020
9021             if (WORDS_BIG_ENDIAN)
9022               {
9023                 first = a->dw_attr_val.v.val_double.high;
9024                 second = a->dw_attr_val.v.val_double.low;
9025               }
9026             else
9027               {
9028                 first = a->dw_attr_val.v.val_double.low;
9029                 second = a->dw_attr_val.v.val_double.high;
9030               }
9031
9032             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9033                                  first, name);
9034             dw2_asm_output_data (HOST_BITS_PER_WIDE_INT / HOST_BITS_PER_CHAR,
9035                                  second, NULL);
9036           }
9037           break;
9038
9039         case dw_val_class_vec:
9040           {
9041             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
9042             unsigned int len = a->dw_attr_val.v.val_vec.length;
9043             unsigned int i;
9044             unsigned char *p;
9045
9046             dw2_asm_output_data (constant_size (len * elt_size),
9047                                  len * elt_size, "%s", name);
9048             if (elt_size > sizeof (HOST_WIDE_INT))
9049               {
9050                 elt_size /= 2;
9051                 len *= 2;
9052               }
9053             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
9054                  i < len;
9055                  i++, p += elt_size)
9056               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
9057                                    "fp or vector constant word %u", i);
9058             break;
9059           }
9060
9061         case dw_val_class_flag:
9062           dw2_asm_output_data (1, AT_flag (a), "%s", name);
9063           break;
9064
9065         case dw_val_class_loc_list:
9066           {
9067             char *sym = AT_loc_list (a)->ll_symbol;
9068
9069             gcc_assert (sym);
9070             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
9071                                    "%s", name);
9072           }
9073           break;
9074
9075         case dw_val_class_die_ref:
9076           if (AT_ref_external (a))
9077             {
9078               char *sym = AT_ref (a)->die_symbol;
9079               int size;
9080
9081               gcc_assert (sym);
9082
9083               /* In DWARF2, DW_FORM_ref_addr is sized by target address
9084                  length, whereas in DWARF3 it's always sized as an offset.  */
9085               if (dwarf_version == 2)
9086                 size = DWARF2_ADDR_SIZE;
9087               else
9088                 size = DWARF_OFFSET_SIZE;
9089               dw2_asm_output_offset (size, sym, debug_info_section, "%s", name);
9090             }
9091           else
9092             {
9093               gcc_assert (AT_ref (a)->die_offset);
9094               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
9095                                    "%s", name);
9096             }
9097           break;
9098
9099         case dw_val_class_fde_ref:
9100           {
9101             char l1[20];
9102
9103             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
9104                                          a->dw_attr_val.v.val_fde_index * 2);
9105             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
9106                                    "%s", name);
9107           }
9108           break;
9109
9110         case dw_val_class_lbl_id:
9111           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
9112           break;
9113
9114         case dw_val_class_lineptr:
9115           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9116                                  debug_line_section, "%s", name);
9117           break;
9118
9119         case dw_val_class_macptr:
9120           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
9121                                  debug_macinfo_section, "%s", name);
9122           break;
9123
9124         case dw_val_class_str:
9125           if (AT_string_form (a) == DW_FORM_strp)
9126             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
9127                                    a->dw_attr_val.v.val_str->label,
9128                                    debug_str_section,
9129                                    "%s: \"%s\"", name, AT_string (a));
9130           else
9131             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
9132           break;
9133
9134         case dw_val_class_file:
9135           {
9136             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
9137
9138             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
9139                                  a->dw_attr_val.v.val_file->filename);
9140             break;
9141           }
9142
9143         default:
9144           gcc_unreachable ();
9145         }
9146     }
9147
9148   FOR_EACH_CHILD (die, c, output_die (c));
9149
9150   /* Add null byte to terminate sibling list.  */
9151   if (die->die_child != NULL)
9152     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
9153                          (unsigned long) die->die_offset);
9154 }
9155
9156 /* Output the compilation unit that appears at the beginning of the
9157    .debug_info section, and precedes the DIE descriptions.  */
9158
9159 static void
9160 output_compilation_unit_header (void)
9161 {
9162   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9163     dw2_asm_output_data (4, 0xffffffff,
9164       "Initial length escape value indicating 64-bit DWARF extension");
9165   dw2_asm_output_data (DWARF_OFFSET_SIZE,
9166                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
9167                        "Length of Compilation Unit Info");
9168   dw2_asm_output_data (2, dwarf_version, "DWARF version number");
9169   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
9170                          debug_abbrev_section,
9171                          "Offset Into Abbrev. Section");
9172   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
9173 }
9174
9175 /* Output the compilation unit DIE and its children.  */
9176
9177 static void
9178 output_comp_unit (dw_die_ref die, int output_if_empty)
9179 {
9180   const char *secname;
9181   char *oldsym, *tmp;
9182
9183   /* Unless we are outputting main CU, we may throw away empty ones.  */
9184   if (!output_if_empty && die->die_child == NULL)
9185     return;
9186
9187   /* Even if there are no children of this DIE, we must output the information
9188      about the compilation unit.  Otherwise, on an empty translation unit, we
9189      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
9190      will then complain when examining the file.  First mark all the DIEs in
9191      this CU so we know which get local refs.  */
9192   mark_dies (die);
9193
9194   build_abbrev_table (die);
9195
9196   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
9197   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
9198   calc_die_sizes (die);
9199
9200   oldsym = die->die_symbol;
9201   if (oldsym)
9202     {
9203       tmp = XALLOCAVEC (char, strlen (oldsym) + 24);
9204
9205       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
9206       secname = tmp;
9207       die->die_symbol = NULL;
9208       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
9209     }
9210   else
9211     switch_to_section (debug_info_section);
9212
9213   /* Output debugging information.  */
9214   output_compilation_unit_header ();
9215   output_die (die);
9216
9217   /* Leave the marks on the main CU, so we can check them in
9218      output_pubnames.  */
9219   if (oldsym)
9220     {
9221       unmark_dies (die);
9222       die->die_symbol = oldsym;
9223     }
9224 }
9225
9226 /* Return the DWARF2/3 pubname associated with a decl.  */
9227
9228 static const char *
9229 dwarf2_name (tree decl, int scope)
9230 {
9231   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
9232 }
9233
9234 /* Add a new entry to .debug_pubnames if appropriate.  */
9235
9236 static void
9237 add_pubname_string (const char *str, dw_die_ref die)
9238 {
9239   pubname_entry e;
9240
9241   e.die = die;
9242   e.name = xstrdup (str);
9243   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
9244 }
9245
9246 static void
9247 add_pubname (tree decl, dw_die_ref die)
9248 {
9249   if (TREE_PUBLIC (decl))
9250     add_pubname_string (dwarf2_name (decl, 1), die);
9251 }
9252
9253 /* Add a new entry to .debug_pubtypes if appropriate.  */
9254
9255 static void
9256 add_pubtype (tree decl, dw_die_ref die)
9257 {
9258   pubname_entry e;
9259
9260   e.name = NULL;
9261   if ((TREE_PUBLIC (decl)
9262        || die->die_parent == comp_unit_die)
9263       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
9264     {
9265       e.die = die;
9266       if (TYPE_P (decl))
9267         {
9268           if (TYPE_NAME (decl))
9269             {
9270               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
9271                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
9272               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
9273                        && DECL_NAME (TYPE_NAME (decl)))
9274                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
9275               else
9276                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
9277             }
9278         }
9279       else
9280         e.name = xstrdup (dwarf2_name (decl, 1));
9281
9282       /* If we don't have a name for the type, there's no point in adding
9283          it to the table.  */
9284       if (e.name && e.name[0] != '\0')
9285         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
9286     }
9287 }
9288
9289 /* Output the public names table used to speed up access to externally
9290    visible names; or the public types table used to find type definitions.  */
9291
9292 static void
9293 output_pubnames (VEC (pubname_entry, gc) * names)
9294 {
9295   unsigned i;
9296   unsigned long pubnames_length = size_of_pubnames (names);
9297   pubname_ref pub;
9298
9299   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9300     dw2_asm_output_data (4, 0xffffffff,
9301       "Initial length escape value indicating 64-bit DWARF extension");
9302   if (names == pubname_table)
9303     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9304                          "Length of Public Names Info");
9305   else
9306     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
9307                          "Length of Public Type Names Info");
9308   /* Version number for pubnames/pubtypes is still 2, even in DWARF3.  */
9309   dw2_asm_output_data (2, 2, "DWARF Version");
9310   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9311                          debug_info_section,
9312                          "Offset of Compilation Unit Info");
9313   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
9314                        "Compilation Unit Length");
9315
9316   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
9317     {
9318       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
9319       if (names == pubname_table)
9320         gcc_assert (pub->die->die_mark);
9321
9322       if (names != pubtype_table
9323           || pub->die->die_offset != 0
9324           || !flag_eliminate_unused_debug_types)
9325         {
9326           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
9327                                "DIE offset");
9328
9329           dw2_asm_output_nstring (pub->name, -1, "external name");
9330         }
9331     }
9332
9333   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
9334 }
9335
9336 /* Add a new entry to .debug_aranges if appropriate.  */
9337
9338 static void
9339 add_arange (tree decl, dw_die_ref die)
9340 {
9341   if (! DECL_SECTION_NAME (decl))
9342     return;
9343
9344   if (arange_table_in_use == arange_table_allocated)
9345     {
9346       arange_table_allocated += ARANGE_TABLE_INCREMENT;
9347       arange_table = GGC_RESIZEVEC (dw_die_ref, arange_table,
9348                                     arange_table_allocated);
9349       memset (arange_table + arange_table_in_use, 0,
9350               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
9351     }
9352
9353   arange_table[arange_table_in_use++] = die;
9354 }
9355
9356 /* Output the information that goes into the .debug_aranges table.
9357    Namely, define the beginning and ending address range of the
9358    text section generated for this compilation unit.  */
9359
9360 static void
9361 output_aranges (void)
9362 {
9363   unsigned i;
9364   unsigned long aranges_length = size_of_aranges ();
9365
9366   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9367     dw2_asm_output_data (4, 0xffffffff,
9368       "Initial length escape value indicating 64-bit DWARF extension");
9369   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
9370                        "Length of Address Ranges Info");
9371   /* Version number for aranges is still 2, even in DWARF3.  */
9372   dw2_asm_output_data (2, 2, "DWARF Version");
9373   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
9374                          debug_info_section,
9375                          "Offset of Compilation Unit Info");
9376   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
9377   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
9378
9379   /* We need to align to twice the pointer size here.  */
9380   if (DWARF_ARANGES_PAD_SIZE)
9381     {
9382       /* Pad using a 2 byte words so that padding is correct for any
9383          pointer size.  */
9384       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
9385                            2 * DWARF2_ADDR_SIZE);
9386       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
9387         dw2_asm_output_data (2, 0, NULL);
9388     }
9389
9390   /* It is necessary not to output these entries if the sections were
9391      not used; if the sections were not used, the length will be 0 and
9392      the address may end up as 0 if the section is discarded by ld
9393      --gc-sections, leaving an invalid (0, 0) entry that can be
9394      confused with the terminator.  */
9395   if (text_section_used)
9396     {
9397       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
9398       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
9399                             text_section_label, "Length");
9400     }
9401   if (cold_text_section_used)
9402     {
9403       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
9404                            "Address");
9405       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
9406                             cold_text_section_label, "Length");
9407     }
9408
9409   for (i = 0; i < arange_table_in_use; i++)
9410     {
9411       dw_die_ref die = arange_table[i];
9412
9413       /* We shouldn't see aranges for DIEs outside of the main CU.  */
9414       gcc_assert (die->die_mark);
9415
9416       if (die->die_tag == DW_TAG_subprogram)
9417         {
9418           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
9419                                "Address");
9420           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
9421                                 get_AT_low_pc (die), "Length");
9422         }
9423       else
9424         {
9425           /* A static variable; extract the symbol from DW_AT_location.
9426              Note that this code isn't currently hit, as we only emit
9427              aranges for functions (jason 9/23/99).  */
9428           dw_attr_ref a = get_AT (die, DW_AT_location);
9429           dw_loc_descr_ref loc;
9430
9431           gcc_assert (a && AT_class (a) == dw_val_class_loc);
9432
9433           loc = AT_loc (a);
9434           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
9435
9436           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
9437                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
9438           dw2_asm_output_data (DWARF2_ADDR_SIZE,
9439                                get_AT_unsigned (die, DW_AT_byte_size),
9440                                "Length");
9441         }
9442     }
9443
9444   /* Output the terminator words.  */
9445   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9446   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9447 }
9448
9449 /* Add a new entry to .debug_ranges.  Return the offset at which it
9450    was placed.  */
9451
9452 static unsigned int
9453 add_ranges_num (int num)
9454 {
9455   unsigned int in_use = ranges_table_in_use;
9456
9457   if (in_use == ranges_table_allocated)
9458     {
9459       ranges_table_allocated += RANGES_TABLE_INCREMENT;
9460       ranges_table = GGC_RESIZEVEC (struct dw_ranges_struct, ranges_table,
9461                                     ranges_table_allocated);
9462       memset (ranges_table + ranges_table_in_use, 0,
9463               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
9464     }
9465
9466   ranges_table[in_use].num = num;
9467   ranges_table_in_use = in_use + 1;
9468
9469   return in_use * 2 * DWARF2_ADDR_SIZE;
9470 }
9471
9472 /* Add a new entry to .debug_ranges corresponding to a block, or a
9473    range terminator if BLOCK is NULL.  */
9474
9475 static unsigned int
9476 add_ranges (const_tree block)
9477 {
9478   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
9479 }
9480
9481 /* Add a new entry to .debug_ranges corresponding to a pair of
9482    labels.  */
9483
9484 static unsigned int
9485 add_ranges_by_labels (const char *begin, const char *end)
9486 {
9487   unsigned int in_use = ranges_by_label_in_use;
9488
9489   if (in_use == ranges_by_label_allocated)
9490     {
9491       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
9492       ranges_by_label = GGC_RESIZEVEC (struct dw_ranges_by_label_struct,
9493                                        ranges_by_label,
9494                                        ranges_by_label_allocated);
9495       memset (ranges_by_label + ranges_by_label_in_use, 0,
9496               RANGES_TABLE_INCREMENT
9497               * sizeof (struct dw_ranges_by_label_struct));
9498     }
9499
9500   ranges_by_label[in_use].begin = begin;
9501   ranges_by_label[in_use].end = end;
9502   ranges_by_label_in_use = in_use + 1;
9503
9504   return add_ranges_num (-(int)in_use - 1);
9505 }
9506
9507 static void
9508 output_ranges (void)
9509 {
9510   unsigned i;
9511   static const char *const start_fmt = "Offset 0x%x";
9512   const char *fmt = start_fmt;
9513
9514   for (i = 0; i < ranges_table_in_use; i++)
9515     {
9516       int block_num = ranges_table[i].num;
9517
9518       if (block_num > 0)
9519         {
9520           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
9521           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
9522
9523           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
9524           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
9525
9526           /* If all code is in the text section, then the compilation
9527              unit base address defaults to DW_AT_low_pc, which is the
9528              base of the text section.  */
9529           if (!have_multiple_function_sections)
9530             {
9531               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
9532                                     text_section_label,
9533                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9534               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
9535                                     text_section_label, NULL);
9536             }
9537
9538           /* Otherwise, the compilation unit base address is zero,
9539              which allows us to use absolute addresses, and not worry
9540              about whether the target supports cross-section
9541              arithmetic.  */
9542           else
9543             {
9544               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
9545                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9546               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
9547             }
9548
9549           fmt = NULL;
9550         }
9551
9552       /* Negative block_num stands for an index into ranges_by_label.  */
9553       else if (block_num < 0)
9554         {
9555           int lab_idx = - block_num - 1;
9556
9557           if (!have_multiple_function_sections)
9558             {
9559               gcc_unreachable ();
9560 #if 0
9561               /* If we ever use add_ranges_by_labels () for a single
9562                  function section, all we have to do is to take out
9563                  the #if 0 above.  */
9564               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9565                                     ranges_by_label[lab_idx].begin,
9566                                     text_section_label,
9567                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
9568               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
9569                                     ranges_by_label[lab_idx].end,
9570                                     text_section_label, NULL);
9571 #endif
9572             }
9573           else
9574             {
9575               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9576                                    ranges_by_label[lab_idx].begin,
9577                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
9578               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
9579                                    ranges_by_label[lab_idx].end,
9580                                    NULL);
9581             }
9582         }
9583       else
9584         {
9585           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9586           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
9587           fmt = start_fmt;
9588         }
9589     }
9590 }
9591
9592 /* Data structure containing information about input files.  */
9593 struct file_info
9594 {
9595   const char *path;     /* Complete file name.  */
9596   const char *fname;    /* File name part.  */
9597   int length;           /* Length of entire string.  */
9598   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
9599   int dir_idx;          /* Index in directory table.  */
9600 };
9601
9602 /* Data structure containing information about directories with source
9603    files.  */
9604 struct dir_info
9605 {
9606   const char *path;     /* Path including directory name.  */
9607   int length;           /* Path length.  */
9608   int prefix;           /* Index of directory entry which is a prefix.  */
9609   int count;            /* Number of files in this directory.  */
9610   int dir_idx;          /* Index of directory used as base.  */
9611 };
9612
9613 /* Callback function for file_info comparison.  We sort by looking at
9614    the directories in the path.  */
9615
9616 static int
9617 file_info_cmp (const void *p1, const void *p2)
9618 {
9619   const struct file_info *const s1 = (const struct file_info *) p1;
9620   const struct file_info *const s2 = (const struct file_info *) p2;
9621   const unsigned char *cp1;
9622   const unsigned char *cp2;
9623
9624   /* Take care of file names without directories.  We need to make sure that
9625      we return consistent values to qsort since some will get confused if
9626      we return the same value when identical operands are passed in opposite
9627      orders.  So if neither has a directory, return 0 and otherwise return
9628      1 or -1 depending on which one has the directory.  */
9629   if ((s1->path == s1->fname || s2->path == s2->fname))
9630     return (s2->path == s2->fname) - (s1->path == s1->fname);
9631
9632   cp1 = (const unsigned char *) s1->path;
9633   cp2 = (const unsigned char *) s2->path;
9634
9635   while (1)
9636     {
9637       ++cp1;
9638       ++cp2;
9639       /* Reached the end of the first path?  If so, handle like above.  */
9640       if ((cp1 == (const unsigned char *) s1->fname)
9641           || (cp2 == (const unsigned char *) s2->fname))
9642         return ((cp2 == (const unsigned char *) s2->fname)
9643                 - (cp1 == (const unsigned char *) s1->fname));
9644
9645       /* Character of current path component the same?  */
9646       else if (*cp1 != *cp2)
9647         return *cp1 - *cp2;
9648     }
9649 }
9650
9651 struct file_name_acquire_data
9652 {
9653   struct file_info *files;
9654   int used_files;
9655   int max_files;
9656 };
9657
9658 /* Traversal function for the hash table.  */
9659
9660 static int
9661 file_name_acquire (void ** slot, void *data)
9662 {
9663   struct file_name_acquire_data *fnad = (struct file_name_acquire_data *) data;
9664   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
9665   struct file_info *fi;
9666   const char *f;
9667
9668   gcc_assert (fnad->max_files >= d->emitted_number);
9669
9670   if (! d->emitted_number)
9671     return 1;
9672
9673   gcc_assert (fnad->max_files != fnad->used_files);
9674
9675   fi = fnad->files + fnad->used_files++;
9676
9677   /* Skip all leading "./".  */
9678   f = d->filename;
9679   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
9680     f += 2;
9681
9682   /* Create a new array entry.  */
9683   fi->path = f;
9684   fi->length = strlen (f);
9685   fi->file_idx = d;
9686
9687   /* Search for the file name part.  */
9688   f = strrchr (f, DIR_SEPARATOR);
9689 #if defined (DIR_SEPARATOR_2)
9690   {
9691     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
9692
9693     if (g != NULL)
9694       {
9695         if (f == NULL || f < g)
9696           f = g;
9697       }
9698   }
9699 #endif
9700
9701   fi->fname = f == NULL ? fi->path : f + 1;
9702   return 1;
9703 }
9704
9705 /* Output the directory table and the file name table.  We try to minimize
9706    the total amount of memory needed.  A heuristic is used to avoid large
9707    slowdowns with many input files.  */
9708
9709 static void
9710 output_file_names (void)
9711 {
9712   struct file_name_acquire_data fnad;
9713   int numfiles;
9714   struct file_info *files;
9715   struct dir_info *dirs;
9716   int *saved;
9717   int *savehere;
9718   int *backmap;
9719   int ndirs;
9720   int idx_offset;
9721   int i;
9722   int idx;
9723
9724   if (!last_emitted_file)
9725     {
9726       dw2_asm_output_data (1, 0, "End directory table");
9727       dw2_asm_output_data (1, 0, "End file name table");
9728       return;
9729     }
9730
9731   numfiles = last_emitted_file->emitted_number;
9732
9733   /* Allocate the various arrays we need.  */
9734   files = XALLOCAVEC (struct file_info, numfiles);
9735   dirs = XALLOCAVEC (struct dir_info, numfiles);
9736
9737   fnad.files = files;
9738   fnad.used_files = 0;
9739   fnad.max_files = numfiles;
9740   htab_traverse (file_table, file_name_acquire, &fnad);
9741   gcc_assert (fnad.used_files == fnad.max_files);
9742
9743   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
9744
9745   /* Find all the different directories used.  */
9746   dirs[0].path = files[0].path;
9747   dirs[0].length = files[0].fname - files[0].path;
9748   dirs[0].prefix = -1;
9749   dirs[0].count = 1;
9750   dirs[0].dir_idx = 0;
9751   files[0].dir_idx = 0;
9752   ndirs = 1;
9753
9754   for (i = 1; i < numfiles; i++)
9755     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
9756         && memcmp (dirs[ndirs - 1].path, files[i].path,
9757                    dirs[ndirs - 1].length) == 0)
9758       {
9759         /* Same directory as last entry.  */
9760         files[i].dir_idx = ndirs - 1;
9761         ++dirs[ndirs - 1].count;
9762       }
9763     else
9764       {
9765         int j;
9766
9767         /* This is a new directory.  */
9768         dirs[ndirs].path = files[i].path;
9769         dirs[ndirs].length = files[i].fname - files[i].path;
9770         dirs[ndirs].count = 1;
9771         dirs[ndirs].dir_idx = ndirs;
9772         files[i].dir_idx = ndirs;
9773
9774         /* Search for a prefix.  */
9775         dirs[ndirs].prefix = -1;
9776         for (j = 0; j < ndirs; j++)
9777           if (dirs[j].length < dirs[ndirs].length
9778               && dirs[j].length > 1
9779               && (dirs[ndirs].prefix == -1
9780                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
9781               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
9782             dirs[ndirs].prefix = j;
9783
9784         ++ndirs;
9785       }
9786
9787   /* Now to the actual work.  We have to find a subset of the directories which
9788      allow expressing the file name using references to the directory table
9789      with the least amount of characters.  We do not do an exhaustive search
9790      where we would have to check out every combination of every single
9791      possible prefix.  Instead we use a heuristic which provides nearly optimal
9792      results in most cases and never is much off.  */
9793   saved = XALLOCAVEC (int, ndirs);
9794   savehere = XALLOCAVEC (int, ndirs);
9795
9796   memset (saved, '\0', ndirs * sizeof (saved[0]));
9797   for (i = 0; i < ndirs; i++)
9798     {
9799       int j;
9800       int total;
9801
9802       /* We can always save some space for the current directory.  But this
9803          does not mean it will be enough to justify adding the directory.  */
9804       savehere[i] = dirs[i].length;
9805       total = (savehere[i] - saved[i]) * dirs[i].count;
9806
9807       for (j = i + 1; j < ndirs; j++)
9808         {
9809           savehere[j] = 0;
9810           if (saved[j] < dirs[i].length)
9811             {
9812               /* Determine whether the dirs[i] path is a prefix of the
9813                  dirs[j] path.  */
9814               int k;
9815
9816               k = dirs[j].prefix;
9817               while (k != -1 && k != (int) i)
9818                 k = dirs[k].prefix;
9819
9820               if (k == (int) i)
9821                 {
9822                   /* Yes it is.  We can possibly save some memory by
9823                      writing the filenames in dirs[j] relative to
9824                      dirs[i].  */
9825                   savehere[j] = dirs[i].length;
9826                   total += (savehere[j] - saved[j]) * dirs[j].count;
9827                 }
9828             }
9829         }
9830
9831       /* Check whether we can save enough to justify adding the dirs[i]
9832          directory.  */
9833       if (total > dirs[i].length + 1)
9834         {
9835           /* It's worthwhile adding.  */
9836           for (j = i; j < ndirs; j++)
9837             if (savehere[j] > 0)
9838               {
9839                 /* Remember how much we saved for this directory so far.  */
9840                 saved[j] = savehere[j];
9841
9842                 /* Remember the prefix directory.  */
9843                 dirs[j].dir_idx = i;
9844               }
9845         }
9846     }
9847
9848   /* Emit the directory name table.  */
9849   idx = 1;
9850   idx_offset = dirs[0].length > 0 ? 1 : 0;
9851   for (i = 1 - idx_offset; i < ndirs; i++)
9852     dw2_asm_output_nstring (dirs[i].path,
9853                             dirs[i].length
9854                              - !DWARF2_DIR_SHOULD_END_WITH_SEPARATOR,
9855                             "Directory Entry: 0x%x", i + idx_offset);
9856
9857   dw2_asm_output_data (1, 0, "End directory table");
9858
9859   /* We have to emit them in the order of emitted_number since that's
9860      used in the debug info generation.  To do this efficiently we
9861      generate a back-mapping of the indices first.  */
9862   backmap = XALLOCAVEC (int, numfiles);
9863   for (i = 0; i < numfiles; i++)
9864     backmap[files[i].file_idx->emitted_number - 1] = i;
9865
9866   /* Now write all the file names.  */
9867   for (i = 0; i < numfiles; i++)
9868     {
9869       int file_idx = backmap[i];
9870       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
9871
9872 #ifdef VMS_DEBUGGING_INFO
9873 #define MAX_VMS_VERSION_LEN 6 /* ";32768" */
9874
9875       /* Setting these fields can lead to debugger miscomparisons,
9876          but VMS Debug requires them to be set correctly.  */
9877
9878       int ver;
9879       long long cdt;
9880       long siz;
9881       int maxfilelen = strlen (files[file_idx].path)
9882                                + dirs[dir_idx].length
9883                                + MAX_VMS_VERSION_LEN + 1;
9884       char *filebuf = XALLOCAVEC (char, maxfilelen);
9885
9886       vms_file_stats_name (files[file_idx].path, 0, 0, 0, &ver);
9887       snprintf (filebuf, maxfilelen, "%s;%d",
9888                 files[file_idx].path + dirs[dir_idx].length, ver);
9889
9890       dw2_asm_output_nstring
9891         (filebuf, -1, "File Entry: 0x%x", (unsigned) i + 1);
9892
9893       /* Include directory index.  */
9894       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9895
9896       /* Modification time.  */
9897       dw2_asm_output_data_uleb128
9898         ((vms_file_stats_name (files[file_idx].path, &cdt, 0, 0, 0) == 0)
9899           ? cdt : 0,
9900          NULL);
9901
9902       /* File length in bytes.  */
9903       dw2_asm_output_data_uleb128
9904         ((vms_file_stats_name (files[file_idx].path, 0, &siz, 0, 0) == 0)
9905           ? siz : 0,
9906          NULL);
9907 #else
9908       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
9909                               "File Entry: 0x%x", (unsigned) i + 1);
9910
9911       /* Include directory index.  */
9912       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
9913
9914       /* Modification time.  */
9915       dw2_asm_output_data_uleb128 (0, NULL);
9916
9917       /* File length in bytes.  */
9918       dw2_asm_output_data_uleb128 (0, NULL);
9919 #endif
9920     }
9921
9922   dw2_asm_output_data (1, 0, "End file name table");
9923 }
9924
9925
9926 /* Output the source line number correspondence information.  This
9927    information goes into the .debug_line section.  */
9928
9929 static void
9930 output_line_info (void)
9931 {
9932   char l1[20], l2[20], p1[20], p2[20];
9933   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9934   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
9935   unsigned opc;
9936   unsigned n_op_args;
9937   unsigned long lt_index;
9938   unsigned long current_line;
9939   long line_offset;
9940   long line_delta;
9941   unsigned long current_file;
9942   unsigned long function;
9943
9944   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
9945   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
9946   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
9947   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
9948
9949   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
9950     dw2_asm_output_data (4, 0xffffffff,
9951       "Initial length escape value indicating 64-bit DWARF extension");
9952   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
9953                         "Length of Source Line Info");
9954   ASM_OUTPUT_LABEL (asm_out_file, l1);
9955
9956   dw2_asm_output_data (2, dwarf_version, "DWARF Version");
9957   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
9958   ASM_OUTPUT_LABEL (asm_out_file, p1);
9959
9960   /* Define the architecture-dependent minimum instruction length (in
9961    bytes).  In this implementation of DWARF, this field is used for
9962    information purposes only.  Since GCC generates assembly language,
9963    we have no a priori knowledge of how many instruction bytes are
9964    generated for each source line, and therefore can use only the
9965    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
9966    commands.  Accordingly, we fix this as `1', which is "correct
9967    enough" for all architectures, and don't let the target override.  */
9968   dw2_asm_output_data (1, 1,
9969                        "Minimum Instruction Length");
9970
9971   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
9972                        "Default is_stmt_start flag");
9973   dw2_asm_output_data (1, DWARF_LINE_BASE,
9974                        "Line Base Value (Special Opcodes)");
9975   dw2_asm_output_data (1, DWARF_LINE_RANGE,
9976                        "Line Range Value (Special Opcodes)");
9977   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
9978                        "Special Opcode Base");
9979
9980   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
9981     {
9982       switch (opc)
9983         {
9984         case DW_LNS_advance_pc:
9985         case DW_LNS_advance_line:
9986         case DW_LNS_set_file:
9987         case DW_LNS_set_column:
9988         case DW_LNS_fixed_advance_pc:
9989           n_op_args = 1;
9990           break;
9991         default:
9992           n_op_args = 0;
9993           break;
9994         }
9995
9996       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
9997                            opc, n_op_args);
9998     }
9999
10000   /* Write out the information about the files we use.  */
10001   output_file_names ();
10002   ASM_OUTPUT_LABEL (asm_out_file, p2);
10003
10004   /* We used to set the address register to the first location in the text
10005      section here, but that didn't accomplish anything since we already
10006      have a line note for the opening brace of the first function.  */
10007
10008   /* Generate the line number to PC correspondence table, encoded as
10009      a series of state machine operations.  */
10010   current_file = 1;
10011   current_line = 1;
10012
10013   if (cfun && in_cold_section_p)
10014     strcpy (prev_line_label, crtl->subsections.cold_section_label);
10015   else
10016     strcpy (prev_line_label, text_section_label);
10017   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
10018     {
10019       dw_line_info_ref line_info = &line_info_table[lt_index];
10020
10021 #if 0
10022       /* Disable this optimization for now; GDB wants to see two line notes
10023          at the beginning of a function so it can find the end of the
10024          prologue.  */
10025
10026       /* Don't emit anything for redundant notes.  Just updating the
10027          address doesn't accomplish anything, because we already assume
10028          that anything after the last address is this line.  */
10029       if (line_info->dw_line_num == current_line
10030           && line_info->dw_file_num == current_file)
10031         continue;
10032 #endif
10033
10034       /* Emit debug info for the address of the current line.
10035
10036          Unfortunately, we have little choice here currently, and must always
10037          use the most general form.  GCC does not know the address delta
10038          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
10039          attributes which will give an upper bound on the address range.  We
10040          could perhaps use length attributes to determine when it is safe to
10041          use DW_LNS_fixed_advance_pc.  */
10042
10043       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
10044       if (0)
10045         {
10046           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
10047           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10048                                "DW_LNS_fixed_advance_pc");
10049           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10050         }
10051       else
10052         {
10053           /* This can handle any delta.  This takes
10054              4+DWARF2_ADDR_SIZE bytes.  */
10055           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10056           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10057           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10058           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10059         }
10060
10061       strcpy (prev_line_label, line_label);
10062
10063       /* Emit debug info for the source file of the current line, if
10064          different from the previous line.  */
10065       if (line_info->dw_file_num != current_file)
10066         {
10067           current_file = line_info->dw_file_num;
10068           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10069           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10070         }
10071
10072       /* Emit debug info for the current line number, choosing the encoding
10073          that uses the least amount of space.  */
10074       if (line_info->dw_line_num != current_line)
10075         {
10076           line_offset = line_info->dw_line_num - current_line;
10077           line_delta = line_offset - DWARF_LINE_BASE;
10078           current_line = line_info->dw_line_num;
10079           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10080             /* This can handle deltas from -10 to 234, using the current
10081                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
10082                takes 1 byte.  */
10083             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10084                                  "line %lu", current_line);
10085           else
10086             {
10087               /* This can handle any delta.  This takes at least 4 bytes,
10088                  depending on the value being encoded.  */
10089               dw2_asm_output_data (1, DW_LNS_advance_line,
10090                                    "advance to line %lu", current_line);
10091               dw2_asm_output_data_sleb128 (line_offset, NULL);
10092               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10093             }
10094         }
10095       else
10096         /* We still need to start a new row, so output a copy insn.  */
10097         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10098     }
10099
10100   /* Emit debug info for the address of the end of the function.  */
10101   if (0)
10102     {
10103       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10104                            "DW_LNS_fixed_advance_pc");
10105       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
10106     }
10107   else
10108     {
10109       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10110       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10111       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10112       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
10113     }
10114
10115   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10116   dw2_asm_output_data_uleb128 (1, NULL);
10117   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10118
10119   function = 0;
10120   current_file = 1;
10121   current_line = 1;
10122   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
10123     {
10124       dw_separate_line_info_ref line_info
10125         = &separate_line_info_table[lt_index];
10126
10127 #if 0
10128       /* Don't emit anything for redundant notes.  */
10129       if (line_info->dw_line_num == current_line
10130           && line_info->dw_file_num == current_file
10131           && line_info->function == function)
10132         goto cont;
10133 #endif
10134
10135       /* Emit debug info for the address of the current line.  If this is
10136          a new function, or the first line of a function, then we need
10137          to handle it differently.  */
10138       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
10139                                    lt_index);
10140       if (function != line_info->function)
10141         {
10142           function = line_info->function;
10143
10144           /* Set the address register to the first line in the function.  */
10145           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10146           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10147           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10148           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10149         }
10150       else
10151         {
10152           /* ??? See the DW_LNS_advance_pc comment above.  */
10153           if (0)
10154             {
10155               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10156                                    "DW_LNS_fixed_advance_pc");
10157               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10158             }
10159           else
10160             {
10161               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10162               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10163               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10164               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10165             }
10166         }
10167
10168       strcpy (prev_line_label, line_label);
10169
10170       /* Emit debug info for the source file of the current line, if
10171          different from the previous line.  */
10172       if (line_info->dw_file_num != current_file)
10173         {
10174           current_file = line_info->dw_file_num;
10175           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
10176           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
10177         }
10178
10179       /* Emit debug info for the current line number, choosing the encoding
10180          that uses the least amount of space.  */
10181       if (line_info->dw_line_num != current_line)
10182         {
10183           line_offset = line_info->dw_line_num - current_line;
10184           line_delta = line_offset - DWARF_LINE_BASE;
10185           current_line = line_info->dw_line_num;
10186           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
10187             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
10188                                  "line %lu", current_line);
10189           else
10190             {
10191               dw2_asm_output_data (1, DW_LNS_advance_line,
10192                                    "advance to line %lu", current_line);
10193               dw2_asm_output_data_sleb128 (line_offset, NULL);
10194               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10195             }
10196         }
10197       else
10198         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
10199
10200 #if 0
10201     cont:
10202 #endif
10203
10204       lt_index++;
10205
10206       /* If we're done with a function, end its sequence.  */
10207       if (lt_index == separate_line_info_table_in_use
10208           || separate_line_info_table[lt_index].function != function)
10209         {
10210           current_file = 1;
10211           current_line = 1;
10212
10213           /* Emit debug info for the address of the end of the function.  */
10214           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
10215           if (0)
10216             {
10217               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
10218                                    "DW_LNS_fixed_advance_pc");
10219               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
10220             }
10221           else
10222             {
10223               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
10224               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
10225               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
10226               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
10227             }
10228
10229           /* Output the marker for the end of this sequence.  */
10230           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
10231           dw2_asm_output_data_uleb128 (1, NULL);
10232           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
10233         }
10234     }
10235
10236   /* Output the marker for the end of the line number info.  */
10237   ASM_OUTPUT_LABEL (asm_out_file, l2);
10238 }
10239 \f
10240 /* Given a pointer to a tree node for some base type, return a pointer to
10241    a DIE that describes the given type.
10242
10243    This routine must only be called for GCC type nodes that correspond to
10244    Dwarf base (fundamental) types.  */
10245
10246 static dw_die_ref
10247 base_type_die (tree type)
10248 {
10249   dw_die_ref base_type_result;
10250   enum dwarf_type encoding;
10251
10252   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
10253     return 0;
10254
10255   /* If this is a subtype that should not be emitted as a subrange type,
10256      use the base type.  See subrange_type_for_debug_p.  */
10257   if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != NULL_TREE)
10258     type = TREE_TYPE (type);
10259
10260   switch (TREE_CODE (type))
10261     {
10262     case INTEGER_TYPE:
10263       if (TYPE_STRING_FLAG (type))
10264         {
10265           if (TYPE_UNSIGNED (type))
10266             encoding = DW_ATE_unsigned_char;
10267           else
10268             encoding = DW_ATE_signed_char;
10269         }
10270       else if (TYPE_UNSIGNED (type))
10271         encoding = DW_ATE_unsigned;
10272       else
10273         encoding = DW_ATE_signed;
10274       break;
10275
10276     case REAL_TYPE:
10277       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
10278         {
10279           if (dwarf_version >= 3 || !dwarf_strict)
10280             encoding = DW_ATE_decimal_float;
10281           else
10282             encoding = DW_ATE_lo_user;
10283         }
10284       else
10285         encoding = DW_ATE_float;
10286       break;
10287
10288     case FIXED_POINT_TYPE:
10289       if (!(dwarf_version >= 3 || !dwarf_strict))
10290         encoding = DW_ATE_lo_user;
10291       else if (TYPE_UNSIGNED (type))
10292         encoding = DW_ATE_unsigned_fixed;
10293       else
10294         encoding = DW_ATE_signed_fixed;
10295       break;
10296
10297       /* Dwarf2 doesn't know anything about complex ints, so use
10298          a user defined type for it.  */
10299     case COMPLEX_TYPE:
10300       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
10301         encoding = DW_ATE_complex_float;
10302       else
10303         encoding = DW_ATE_lo_user;
10304       break;
10305
10306     case BOOLEAN_TYPE:
10307       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
10308       encoding = DW_ATE_boolean;
10309       break;
10310
10311     default:
10312       /* No other TREE_CODEs are Dwarf fundamental types.  */
10313       gcc_unreachable ();
10314     }
10315
10316   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
10317
10318   /* This probably indicates a bug.  */
10319   if (! TYPE_NAME (type))
10320     add_name_attribute (base_type_result, "__unknown__");
10321
10322   add_AT_unsigned (base_type_result, DW_AT_byte_size,
10323                    int_size_in_bytes (type));
10324   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
10325
10326   return base_type_result;
10327 }
10328
10329 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
10330    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
10331
10332 static inline int
10333 is_base_type (tree type)
10334 {
10335   switch (TREE_CODE (type))
10336     {
10337     case ERROR_MARK:
10338     case VOID_TYPE:
10339     case INTEGER_TYPE:
10340     case REAL_TYPE:
10341     case FIXED_POINT_TYPE:
10342     case COMPLEX_TYPE:
10343     case BOOLEAN_TYPE:
10344       return 1;
10345
10346     case ARRAY_TYPE:
10347     case RECORD_TYPE:
10348     case UNION_TYPE:
10349     case QUAL_UNION_TYPE:
10350     case ENUMERAL_TYPE:
10351     case FUNCTION_TYPE:
10352     case METHOD_TYPE:
10353     case POINTER_TYPE:
10354     case REFERENCE_TYPE:
10355     case OFFSET_TYPE:
10356     case LANG_TYPE:
10357     case VECTOR_TYPE:
10358       return 0;
10359
10360     default:
10361       gcc_unreachable ();
10362     }
10363
10364   return 0;
10365 }
10366
10367 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
10368    node, return the size in bits for the type if it is a constant, or else
10369    return the alignment for the type if the type's size is not constant, or
10370    else return BITS_PER_WORD if the type actually turns out to be an
10371    ERROR_MARK node.  */
10372
10373 static inline unsigned HOST_WIDE_INT
10374 simple_type_size_in_bits (const_tree type)
10375 {
10376   if (TREE_CODE (type) == ERROR_MARK)
10377     return BITS_PER_WORD;
10378   else if (TYPE_SIZE (type) == NULL_TREE)
10379     return 0;
10380   else if (host_integerp (TYPE_SIZE (type), 1))
10381     return tree_low_cst (TYPE_SIZE (type), 1);
10382   else
10383     return TYPE_ALIGN (type);
10384 }
10385
10386 /*  Given a pointer to a tree node for a subrange type, return a pointer
10387     to a DIE that describes the given type.  */
10388
10389 static dw_die_ref
10390 subrange_type_die (tree type, tree low, tree high, dw_die_ref context_die)
10391 {
10392   dw_die_ref subrange_die;
10393   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
10394
10395   if (context_die == NULL)
10396     context_die = comp_unit_die;
10397
10398   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
10399
10400   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
10401     {
10402       /* The size of the subrange type and its base type do not match,
10403          so we need to generate a size attribute for the subrange type.  */
10404       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
10405     }
10406
10407   if (low)
10408     add_bound_info (subrange_die, DW_AT_lower_bound, low);
10409   if (high)
10410     add_bound_info (subrange_die, DW_AT_upper_bound, high);
10411
10412   return subrange_die;
10413 }
10414
10415 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
10416    entry that chains various modifiers in front of the given type.  */
10417
10418 static dw_die_ref
10419 modified_type_die (tree type, int is_const_type, int is_volatile_type,
10420                    dw_die_ref context_die)
10421 {
10422   enum tree_code code = TREE_CODE (type);
10423   dw_die_ref mod_type_die;
10424   dw_die_ref sub_die = NULL;
10425   tree item_type = NULL;
10426   tree qualified_type;
10427   tree name, low, high;
10428
10429   if (code == ERROR_MARK)
10430     return NULL;
10431
10432   /* See if we already have the appropriately qualified variant of
10433      this type.  */
10434   qualified_type
10435     = get_qualified_type (type,
10436                           ((is_const_type ? TYPE_QUAL_CONST : 0)
10437                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
10438
10439   /* If we do, then we can just use its DIE, if it exists.  */
10440   if (qualified_type)
10441     {
10442       mod_type_die = lookup_type_die (qualified_type);
10443       if (mod_type_die)
10444         return mod_type_die;
10445     }
10446
10447   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
10448
10449   /* Handle C typedef types.  */
10450   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
10451     {
10452       tree dtype = TREE_TYPE (name);
10453
10454       if (qualified_type == dtype)
10455         {
10456           /* For a named type, use the typedef.  */
10457           gen_type_die (qualified_type, context_die);
10458           return lookup_type_die (qualified_type);
10459         }
10460       else if (is_const_type < TYPE_READONLY (dtype)
10461                || is_volatile_type < TYPE_VOLATILE (dtype)
10462                || (is_const_type <= TYPE_READONLY (dtype)
10463                    && is_volatile_type <= TYPE_VOLATILE (dtype)
10464                    && DECL_ORIGINAL_TYPE (name) != type))
10465         /* cv-unqualified version of named type.  Just use the unnamed
10466            type to which it refers.  */
10467         return modified_type_die (DECL_ORIGINAL_TYPE (name),
10468                                   is_const_type, is_volatile_type,
10469                                   context_die);
10470       /* Else cv-qualified version of named type; fall through.  */
10471     }
10472
10473   if (is_const_type)
10474     {
10475       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
10476       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
10477     }
10478   else if (is_volatile_type)
10479     {
10480       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
10481       sub_die = modified_type_die (type, 0, 0, context_die);
10482     }
10483   else if (code == POINTER_TYPE)
10484     {
10485       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
10486       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10487                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10488       item_type = TREE_TYPE (type);
10489     }
10490   else if (code == REFERENCE_TYPE)
10491     {
10492       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
10493       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
10494                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
10495       item_type = TREE_TYPE (type);
10496     }
10497   else if (code == INTEGER_TYPE
10498            && TREE_TYPE (type) != NULL_TREE
10499            && subrange_type_for_debug_p (type, &low, &high))
10500     {
10501       mod_type_die = subrange_type_die (type, low, high, context_die);
10502       item_type = TREE_TYPE (type);
10503     }
10504   else if (is_base_type (type))
10505     mod_type_die = base_type_die (type);
10506   else
10507     {
10508       gen_type_die (type, context_die);
10509
10510       /* We have to get the type_main_variant here (and pass that to the
10511          `lookup_type_die' routine) because the ..._TYPE node we have
10512          might simply be a *copy* of some original type node (where the
10513          copy was created to help us keep track of typedef names) and
10514          that copy might have a different TYPE_UID from the original
10515          ..._TYPE node.  */
10516       if (TREE_CODE (type) != VECTOR_TYPE)
10517         return lookup_type_die (type_main_variant (type));
10518       else
10519         /* Vectors have the debugging information in the type,
10520            not the main variant.  */
10521         return lookup_type_die (type);
10522     }
10523
10524   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
10525      don't output a DW_TAG_typedef, since there isn't one in the
10526      user's program; just attach a DW_AT_name to the type.  */
10527   if (name
10528       && (TREE_CODE (name) != TYPE_DECL
10529           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
10530     {
10531       if (TREE_CODE (name) == TYPE_DECL)
10532         /* Could just call add_name_and_src_coords_attributes here,
10533            but since this is a builtin type it doesn't have any
10534            useful source coordinates anyway.  */
10535         name = DECL_NAME (name);
10536       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
10537     }
10538
10539   if (qualified_type)
10540     equate_type_number_to_die (qualified_type, mod_type_die);
10541
10542   if (item_type)
10543     /* We must do this after the equate_type_number_to_die call, in case
10544        this is a recursive type.  This ensures that the modified_type_die
10545        recursion will terminate even if the type is recursive.  Recursive
10546        types are possible in Ada.  */
10547     sub_die = modified_type_die (item_type,
10548                                  TYPE_READONLY (item_type),
10549                                  TYPE_VOLATILE (item_type),
10550                                  context_die);
10551
10552   if (sub_die != NULL)
10553     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
10554
10555   return mod_type_die;
10556 }
10557
10558 /* Generate DIEs for the generic parameters of T.
10559    T must be either a generic type or a generic function.
10560    See http://gcc.gnu.org/wiki/TemplateParmsDwarf for more.  */
10561
10562 static void
10563 gen_generic_params_dies (tree t)
10564 {
10565   tree parms, args;
10566   int parms_num, i;
10567   dw_die_ref die = NULL;
10568
10569   if (!t || (TYPE_P (t) && !COMPLETE_TYPE_P (t)))
10570     return;
10571
10572   if (TYPE_P (t))
10573     die = lookup_type_die (t);
10574   else if (DECL_P (t))
10575     die = lookup_decl_die (t);
10576
10577   gcc_assert (die);
10578
10579   parms = lang_hooks.get_innermost_generic_parms (t);
10580   if (!parms)
10581     /* T has no generic parameter. It means T is neither a generic type
10582        or function. End of story.  */
10583     return;
10584
10585   parms_num = TREE_VEC_LENGTH (parms);
10586   args = lang_hooks.get_innermost_generic_args (t);
10587   for (i = 0; i < parms_num; i++)
10588     {
10589       tree parm, arg, arg_pack_elems;
10590
10591       parm = TREE_VEC_ELT (parms, i);
10592       arg = TREE_VEC_ELT (args, i);
10593       arg_pack_elems = lang_hooks.types.get_argument_pack_elems (arg);
10594       gcc_assert (parm && TREE_VALUE (parm) && arg);
10595
10596       if (parm && TREE_VALUE (parm) && arg)
10597         {
10598           /* If PARM represents a template parameter pack,
10599              emit a DW_TAG_GNU_template_parameter_pack DIE, followed
10600              by DW_TAG_template_*_parameter DIEs for the argument
10601              pack elements of ARG. Note that ARG would then be
10602              an argument pack.  */
10603           if (arg_pack_elems)
10604             template_parameter_pack_die (TREE_VALUE (parm),
10605                                          arg_pack_elems,
10606                                          die);
10607           else
10608             generic_parameter_die (TREE_VALUE (parm), arg,
10609                                    true /* Emit DW_AT_name */, die);
10610         }
10611     }
10612 }
10613
10614 /* Create and return a DIE for PARM which should be
10615    the representation of a generic type parameter.
10616    For instance, in the C++ front end, PARM would be a template parameter.
10617    ARG is the argument to PARM.
10618    EMIT_NAME_P if tree, the DIE will have DW_AT_name attribute set to the
10619    name of the PARM.
10620    PARENT_DIE is the parent DIE which the new created DIE should be added to,
10621    as a child node.  */
10622
10623 static dw_die_ref
10624 generic_parameter_die (tree parm, tree arg,
10625                        bool emit_name_p,
10626                        dw_die_ref parent_die)
10627 {
10628   dw_die_ref tmpl_die = NULL;
10629   const char *name = NULL;
10630
10631   if (!parm || !DECL_NAME (parm) || !arg)
10632     return NULL;
10633
10634   /* We support non-type generic parameters and arguments,
10635      type generic parameters and arguments, as well as
10636      generic generic parameters (a.k.a. template template parameters in C++)
10637      and arguments.  */
10638   if (TREE_CODE (parm) == PARM_DECL)
10639     /* PARM is a nontype generic parameter  */
10640     tmpl_die = new_die (DW_TAG_template_value_param, parent_die, parm);
10641   else if (TREE_CODE (parm) == TYPE_DECL)
10642     /* PARM is a type generic parameter.  */
10643     tmpl_die = new_die (DW_TAG_template_type_param, parent_die, parm);
10644   else if (lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10645     /* PARM is a generic generic parameter.
10646        Its DIE is a GNU extension. It shall have a
10647        DW_AT_name attribute to represent the name of the template template
10648        parameter, and a DW_AT_GNU_template_name attribute to represent the
10649        name of the template template argument.  */
10650     tmpl_die = new_die (DW_TAG_GNU_template_template_param,
10651                         parent_die, parm);
10652   else
10653     gcc_unreachable ();
10654
10655   if (tmpl_die)
10656     {
10657       tree tmpl_type;
10658
10659       /* If PARM is a generic parameter pack, it means we are
10660          emitting debug info for a template argument pack element.
10661          In other terms, ARG is a template argument pack element.
10662          In that case, we don't emit any DW_AT_name attribute for
10663          the die.  */
10664       if (emit_name_p)
10665         {
10666           name = IDENTIFIER_POINTER (DECL_NAME (parm));
10667           gcc_assert (name);
10668           add_AT_string (tmpl_die, DW_AT_name, name);
10669         }
10670
10671       if (!lang_hooks.decls.generic_generic_parameter_decl_p (parm))
10672         {
10673           /* DWARF3, 5.6.8 says if PARM is a non-type generic parameter
10674              TMPL_DIE should have a child DW_AT_type attribute that is set
10675              to the type of the argument to PARM, which is ARG.
10676              If PARM is a type generic parameter, TMPL_DIE should have a
10677              child DW_AT_type that is set to ARG.  */
10678           tmpl_type = TYPE_P (arg) ? arg : TREE_TYPE (arg);
10679           add_type_attribute (tmpl_die, tmpl_type, 0,
10680                               TREE_THIS_VOLATILE (tmpl_type),
10681                               parent_die);
10682         }
10683       else
10684         {
10685           /* So TMPL_DIE is a DIE representing a
10686              a generic generic template parameter, a.k.a template template
10687              parameter in C++ and arg is a template.  */
10688
10689           /* The DW_AT_GNU_template_name attribute of the DIE must be set
10690              to the name of the argument.  */
10691           name = dwarf2_name (TYPE_P (arg) ? TYPE_NAME (arg) : arg, 1);
10692           add_AT_string (tmpl_die, DW_AT_GNU_template_name, name);
10693         }
10694
10695       if (TREE_CODE (parm) == PARM_DECL)
10696         /* So PARM is a non-type generic parameter.
10697            DWARF3 5.6.8 says we must set a DW_AT_const_value child
10698            attribute of TMPL_DIE which value represents the value
10699            of ARG.
10700            We must be careful here:
10701            The value of ARG might reference some function decls.
10702            We might currently be emitting debug info for a generic
10703            type and types are emitted before function decls, we don't
10704            know if the function decls referenced by ARG will actually be
10705            emitted after cgraph computations.
10706            So must defer the generation of the DW_AT_const_value to
10707            after cgraph is ready.  */
10708         append_entry_to_tmpl_value_parm_die_table (tmpl_die, arg);
10709     }
10710
10711   return tmpl_die;
10712 }
10713
10714 /* Generate and return a  DW_TAG_GNU_template_parameter_pack DIE representing.
10715    PARM_PACK must be a template parameter pack. The returned DIE
10716    will be child DIE of PARENT_DIE.  */
10717
10718 static dw_die_ref
10719 template_parameter_pack_die (tree parm_pack,
10720                              tree parm_pack_args,
10721                              dw_die_ref parent_die)
10722 {
10723   dw_die_ref die;
10724   int j;
10725
10726   gcc_assert (parent_die
10727               && parm_pack
10728               && DECL_NAME (parm_pack));
10729
10730   die = new_die (DW_TAG_GNU_template_parameter_pack, parent_die, parm_pack);
10731   add_AT_string (die, DW_AT_name, IDENTIFIER_POINTER (DECL_NAME (parm_pack)));
10732
10733   for (j = 0; j < TREE_VEC_LENGTH (parm_pack_args); j++)
10734     generic_parameter_die (parm_pack,
10735                            TREE_VEC_ELT (parm_pack_args, j),
10736                            false /* Don't emit DW_AT_name */,
10737                            die);
10738   return die;
10739 }
10740
10741 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
10742    an enumerated type.  */
10743
10744 static inline int
10745 type_is_enum (const_tree type)
10746 {
10747   return TREE_CODE (type) == ENUMERAL_TYPE;
10748 }
10749
10750 /* Return the DBX register number described by a given RTL node.  */
10751
10752 static unsigned int
10753 dbx_reg_number (const_rtx rtl)
10754 {
10755   unsigned regno = REGNO (rtl);
10756
10757   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
10758
10759 #ifdef LEAF_REG_REMAP
10760   if (current_function_uses_only_leaf_regs)
10761     {
10762       int leaf_reg = LEAF_REG_REMAP (regno);
10763       if (leaf_reg != -1)
10764         regno = (unsigned) leaf_reg;
10765     }
10766 #endif
10767
10768   return DBX_REGISTER_NUMBER (regno);
10769 }
10770
10771 /* Optionally add a DW_OP_piece term to a location description expression.
10772    DW_OP_piece is only added if the location description expression already
10773    doesn't end with DW_OP_piece.  */
10774
10775 static void
10776 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
10777 {
10778   dw_loc_descr_ref loc;
10779
10780   if (*list_head != NULL)
10781     {
10782       /* Find the end of the chain.  */
10783       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
10784         ;
10785
10786       if (loc->dw_loc_opc != DW_OP_piece)
10787         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
10788     }
10789 }
10790
10791 /* Return a location descriptor that designates a machine register or
10792    zero if there is none.  */
10793
10794 static dw_loc_descr_ref
10795 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
10796 {
10797   rtx regs;
10798
10799   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
10800     return 0;
10801
10802   regs = targetm.dwarf_register_span (rtl);
10803
10804   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
10805     return multiple_reg_loc_descriptor (rtl, regs, initialized);
10806   else
10807     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
10808 }
10809
10810 /* Return a location descriptor that designates a machine register for
10811    a given hard register number.  */
10812
10813 static dw_loc_descr_ref
10814 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
10815 {
10816   dw_loc_descr_ref reg_loc_descr;
10817
10818   if (regno <= 31)
10819     reg_loc_descr
10820       = new_loc_descr ((enum dwarf_location_atom) (DW_OP_reg0 + regno), 0, 0);
10821   else
10822     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
10823
10824   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
10825     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10826
10827   return reg_loc_descr;
10828 }
10829
10830 /* Given an RTL of a register, return a location descriptor that
10831    designates a value that spans more than one register.  */
10832
10833 static dw_loc_descr_ref
10834 multiple_reg_loc_descriptor (rtx rtl, rtx regs,
10835                              enum var_init_status initialized)
10836 {
10837   int nregs, size, i;
10838   unsigned reg;
10839   dw_loc_descr_ref loc_result = NULL;
10840
10841   reg = REGNO (rtl);
10842 #ifdef LEAF_REG_REMAP
10843   if (current_function_uses_only_leaf_regs)
10844     {
10845       int leaf_reg = LEAF_REG_REMAP (reg);
10846       if (leaf_reg != -1)
10847         reg = (unsigned) leaf_reg;
10848     }
10849 #endif
10850   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
10851   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
10852
10853   /* Simple, contiguous registers.  */
10854   if (regs == NULL_RTX)
10855     {
10856       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
10857
10858       loc_result = NULL;
10859       while (nregs--)
10860         {
10861           dw_loc_descr_ref t;
10862
10863           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
10864                                       VAR_INIT_STATUS_INITIALIZED);
10865           add_loc_descr (&loc_result, t);
10866           add_loc_descr_op_piece (&loc_result, size);
10867           ++reg;
10868         }
10869       return loc_result;
10870     }
10871
10872   /* Now onto stupid register sets in non contiguous locations.  */
10873
10874   gcc_assert (GET_CODE (regs) == PARALLEL);
10875
10876   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10877   loc_result = NULL;
10878
10879   for (i = 0; i < XVECLEN (regs, 0); ++i)
10880     {
10881       dw_loc_descr_ref t;
10882
10883       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
10884                                   VAR_INIT_STATUS_INITIALIZED);
10885       add_loc_descr (&loc_result, t);
10886       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
10887       add_loc_descr_op_piece (&loc_result, size);
10888     }
10889
10890   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
10891     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
10892   return loc_result;
10893 }
10894
10895 #endif /* DWARF2_DEBUGGING_INFO */
10896
10897 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
10898
10899 /* Return a location descriptor that designates a constant.  */
10900
10901 static dw_loc_descr_ref
10902 int_loc_descriptor (HOST_WIDE_INT i)
10903 {
10904   enum dwarf_location_atom op;
10905
10906   /* Pick the smallest representation of a constant, rather than just
10907      defaulting to the LEB encoding.  */
10908   if (i >= 0)
10909     {
10910       if (i <= 31)
10911         op = (enum dwarf_location_atom) (DW_OP_lit0 + i);
10912       else if (i <= 0xff)
10913         op = DW_OP_const1u;
10914       else if (i <= 0xffff)
10915         op = DW_OP_const2u;
10916       else if (HOST_BITS_PER_WIDE_INT == 32
10917                || i <= 0xffffffff)
10918         op = DW_OP_const4u;
10919       else
10920         op = DW_OP_constu;
10921     }
10922   else
10923     {
10924       if (i >= -0x80)
10925         op = DW_OP_const1s;
10926       else if (i >= -0x8000)
10927         op = DW_OP_const2s;
10928       else if (HOST_BITS_PER_WIDE_INT == 32
10929                || i >= -0x80000000)
10930         op = DW_OP_const4s;
10931       else
10932         op = DW_OP_consts;
10933     }
10934
10935   return new_loc_descr (op, i, 0);
10936 }
10937 #endif
10938
10939 #ifdef DWARF2_DEBUGGING_INFO
10940 /* Return loc description representing "address" of integer value.
10941    This can appear only as toplevel expression.  */
10942
10943 static dw_loc_descr_ref
10944 address_of_int_loc_descriptor (int size, HOST_WIDE_INT i)
10945 {
10946   int litsize;
10947   dw_loc_descr_ref loc_result = NULL;
10948
10949   if (!(dwarf_version >= 4 || !dwarf_strict))
10950     return NULL;
10951
10952   if (i >= 0)
10953     {
10954       if (i <= 31)
10955         litsize = 1;
10956       else if (i <= 0xff)
10957         litsize = 2;
10958       else if (i <= 0xffff)
10959         litsize = 3;
10960       else if (HOST_BITS_PER_WIDE_INT == 32
10961                || i <= 0xffffffff)
10962         litsize = 5;
10963       else
10964         litsize = 1 + size_of_uleb128 ((unsigned HOST_WIDE_INT) i);
10965     }
10966   else
10967     {
10968       if (i >= -0x80)
10969         litsize = 2;
10970       else if (i >= -0x8000)
10971         litsize = 3;
10972       else if (HOST_BITS_PER_WIDE_INT == 32
10973                || i >= -0x80000000)
10974         litsize = 5;
10975       else
10976         litsize = 1 + size_of_sleb128 (i);
10977     }
10978   /* Determine if DW_OP_stack_value or DW_OP_implicit_value
10979      is more compact.  For DW_OP_stack_value we need:
10980      litsize + 1 (DW_OP_stack_value)
10981      and for DW_OP_implicit_value:
10982      1 (DW_OP_implicit_value) + 1 (length) + size.  */
10983   if ((int) DWARF2_ADDR_SIZE >= size && litsize + 1 <= 1 + 1 + size)
10984     {
10985       loc_result = int_loc_descriptor (i);
10986       add_loc_descr (&loc_result,
10987                      new_loc_descr (DW_OP_stack_value, 0, 0));
10988       return loc_result;
10989     }
10990
10991   loc_result = new_loc_descr (DW_OP_implicit_value,
10992                               size, 0);
10993   loc_result->dw_loc_oprnd2.val_class = dw_val_class_const;
10994   loc_result->dw_loc_oprnd2.v.val_int = i;
10995   return loc_result;
10996 }
10997
10998 /* Return a location descriptor that designates a base+offset location.  */
10999
11000 static dw_loc_descr_ref
11001 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
11002                  enum var_init_status initialized)
11003 {
11004   unsigned int regno;
11005   dw_loc_descr_ref result;
11006   dw_fde_ref fde = current_fde ();
11007
11008   /* We only use "frame base" when we're sure we're talking about the
11009      post-prologue local stack frame.  We do this by *not* running
11010      register elimination until this point, and recognizing the special
11011      argument pointer and soft frame pointer rtx's.  */
11012   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
11013     {
11014       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11015
11016       if (elim != reg)
11017         {
11018           if (GET_CODE (elim) == PLUS)
11019             {
11020               offset += INTVAL (XEXP (elim, 1));
11021               elim = XEXP (elim, 0);
11022             }
11023           gcc_assert ((SUPPORTS_STACK_ALIGNMENT
11024                        && (elim == hard_frame_pointer_rtx
11025                            || elim == stack_pointer_rtx))
11026                       || elim == (frame_pointer_needed
11027                                   ? hard_frame_pointer_rtx
11028                                   : stack_pointer_rtx));
11029
11030           /* If drap register is used to align stack, use frame
11031              pointer + offset to access stack variables.  If stack
11032              is aligned without drap, use stack pointer + offset to
11033              access stack variables.  */
11034           if (crtl->stack_realign_tried
11035               && reg == frame_pointer_rtx)
11036             {
11037               int base_reg
11038                 = DWARF_FRAME_REGNUM ((fde && fde->drap_reg != INVALID_REGNUM)
11039                                       ? HARD_FRAME_POINTER_REGNUM
11040                                       : STACK_POINTER_REGNUM);
11041               return new_reg_loc_descr (base_reg, offset);
11042             }
11043
11044           offset += frame_pointer_fb_offset;
11045           return new_loc_descr (DW_OP_fbreg, offset, 0);
11046         }
11047     }
11048   else if (fde
11049            && fde->drap_reg != INVALID_REGNUM
11050            && (fde->drap_reg == REGNO (reg)
11051                || fde->vdrap_reg == REGNO (reg)))
11052     {
11053       /* Use cfa+offset to represent the location of arguments passed
11054          on stack when drap is used to align stack.  */
11055       return new_loc_descr (DW_OP_fbreg, offset, 0);
11056     }
11057
11058   regno = dbx_reg_number (reg);
11059   if (regno <= 31)
11060     result = new_loc_descr ((enum dwarf_location_atom) (DW_OP_breg0 + regno),
11061                             offset, 0);
11062   else
11063     result = new_loc_descr (DW_OP_bregx, regno, offset);
11064
11065   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11066     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11067
11068   return result;
11069 }
11070
11071 /* Return true if this RTL expression describes a base+offset calculation.  */
11072
11073 static inline int
11074 is_based_loc (const_rtx rtl)
11075 {
11076   return (GET_CODE (rtl) == PLUS
11077           && ((REG_P (XEXP (rtl, 0))
11078                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
11079                && CONST_INT_P (XEXP (rtl, 1)))));
11080 }
11081
11082 /* Try to handle TLS MEMs, for which mem_loc_descriptor on XEXP (mem, 0)
11083    failed.  */
11084
11085 static dw_loc_descr_ref
11086 tls_mem_loc_descriptor (rtx mem)
11087 {
11088   tree base;
11089   dw_loc_descr_ref loc_result;
11090
11091   if (MEM_EXPR (mem) == NULL_TREE || MEM_OFFSET (mem) == NULL_RTX)
11092     return NULL;
11093
11094   base = get_base_address (MEM_EXPR (mem));
11095   if (base == NULL
11096       || TREE_CODE (base) != VAR_DECL
11097       || !DECL_THREAD_LOCAL_P (base))
11098     return NULL;
11099
11100   loc_result = loc_descriptor_from_tree (MEM_EXPR (mem), 1);
11101   if (loc_result == NULL)
11102     return NULL;
11103
11104   if (INTVAL (MEM_OFFSET (mem)))
11105     loc_descr_plus_const (&loc_result, INTVAL (MEM_OFFSET (mem)));
11106
11107   return loc_result;
11108 }
11109
11110 /* Output debug info about reason why we failed to expand expression as dwarf
11111    expression.  */
11112
11113 static void
11114 expansion_failed (tree expr, rtx rtl, char const *reason)
11115 {
11116   if (dump_file && (dump_flags & TDF_DETAILS))
11117     {
11118       fprintf (dump_file, "Failed to expand as dwarf: ");
11119       if (expr)
11120         print_generic_expr (dump_file, expr, dump_flags);
11121       if (rtl)
11122         {
11123           fprintf (dump_file, "\n");
11124           print_rtl (dump_file, rtl);
11125         }
11126       fprintf (dump_file, "\nReason: %s\n", reason);
11127     }
11128 }
11129
11130 /* Helper function for const_ok_for_output, called either directly
11131    or via for_each_rtx.  */
11132
11133 static int
11134 const_ok_for_output_1 (rtx *rtlp, void *data ATTRIBUTE_UNUSED)
11135 {
11136   rtx rtl = *rtlp;
11137
11138   if (GET_CODE (rtl) != SYMBOL_REF)
11139     return 0;
11140
11141   if (CONSTANT_POOL_ADDRESS_P (rtl))
11142     {
11143       bool marked;
11144       get_pool_constant_mark (rtl, &marked);
11145       /* If all references to this pool constant were optimized away,
11146          it was not output and thus we can't represent it.  */
11147       if (!marked)
11148         {
11149           expansion_failed (NULL_TREE, rtl,
11150                             "Constant was removed from constant pool.\n");
11151           return 1;
11152         }
11153     }
11154
11155   if (SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11156     return 1;
11157
11158   /* Avoid references to external symbols in debug info, on several targets
11159      the linker might even refuse to link when linking a shared library,
11160      and in many other cases the relocations for .debug_info/.debug_loc are
11161      dropped, so the address becomes zero anyway.  Hidden symbols, guaranteed
11162      to be defined within the same shared library or executable are fine.  */
11163   if (SYMBOL_REF_EXTERNAL_P (rtl))
11164     {
11165       tree decl = SYMBOL_REF_DECL (rtl);
11166
11167       if (decl == NULL || !targetm.binds_local_p (decl))
11168         {
11169           expansion_failed (NULL_TREE, rtl,
11170                             "Symbol not defined in current TU.\n");
11171           return 1;
11172         }
11173     }
11174
11175   return 0;
11176 }
11177
11178 /* Return true if constant RTL can be emitted in DW_OP_addr or
11179    DW_AT_const_value.  TLS SYMBOL_REFs, external SYMBOL_REFs or
11180    non-marked constant pool SYMBOL_REFs can't be referenced in it.  */
11181
11182 static bool
11183 const_ok_for_output (rtx rtl)
11184 {
11185   if (GET_CODE (rtl) == SYMBOL_REF)
11186     return const_ok_for_output_1 (&rtl, NULL) == 0;
11187
11188   if (GET_CODE (rtl) == CONST)
11189     return for_each_rtx (&XEXP (rtl, 0), const_ok_for_output_1, NULL) == 0;
11190
11191   return true;
11192 }
11193
11194 /* The following routine converts the RTL for a variable or parameter
11195    (resident in memory) into an equivalent Dwarf representation of a
11196    mechanism for getting the address of that same variable onto the top of a
11197    hypothetical "address evaluation" stack.
11198
11199    When creating memory location descriptors, we are effectively transforming
11200    the RTL for a memory-resident object into its Dwarf postfix expression
11201    equivalent.  This routine recursively descends an RTL tree, turning
11202    it into Dwarf postfix code as it goes.
11203
11204    MODE is the mode of the memory reference, needed to handle some
11205    autoincrement addressing modes.
11206
11207    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
11208    location list for RTL.
11209
11210    Return 0 if we can't represent the location.  */
11211
11212 static dw_loc_descr_ref
11213 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
11214                     enum var_init_status initialized)
11215 {
11216   dw_loc_descr_ref mem_loc_result = NULL;
11217   enum dwarf_location_atom op;
11218   dw_loc_descr_ref op0, op1;
11219
11220   /* Note that for a dynamically sized array, the location we will generate a
11221      description of here will be the lowest numbered location which is
11222      actually within the array.  That's *not* necessarily the same as the
11223      zeroth element of the array.  */
11224
11225   rtl = targetm.delegitimize_address (rtl);
11226
11227   switch (GET_CODE (rtl))
11228     {
11229     case POST_INC:
11230     case POST_DEC:
11231     case POST_MODIFY:
11232       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
11233          just fall into the SUBREG code.  */
11234
11235       /* ... fall through ...  */
11236
11237     case SUBREG:
11238       /* The case of a subreg may arise when we have a local (register)
11239          variable or a formal (register) parameter which doesn't quite fill
11240          up an entire register.  For now, just assume that it is
11241          legitimate to make the Dwarf info refer to the whole register which
11242          contains the given subreg.  */
11243       rtl = XEXP (rtl, 0);
11244       if (GET_MODE_SIZE (GET_MODE (rtl)) > DWARF2_ADDR_SIZE)
11245         break;
11246       mem_loc_result = mem_loc_descriptor (rtl, mode, initialized);
11247       break;
11248
11249     case REG:
11250       /* Whenever a register number forms a part of the description of the
11251          method for calculating the (dynamic) address of a memory resident
11252          object, DWARF rules require the register number be referred to as
11253          a "base register".  This distinction is not based in any way upon
11254          what category of register the hardware believes the given register
11255          belongs to.  This is strictly DWARF terminology we're dealing with
11256          here. Note that in cases where the location of a memory-resident
11257          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
11258          OP_CONST (0)) the actual DWARF location descriptor that we generate
11259          may just be OP_BASEREG (basereg).  This may look deceptively like
11260          the object in question was allocated to a register (rather than in
11261          memory) so DWARF consumers need to be aware of the subtle
11262          distinction between OP_REG and OP_BASEREG.  */
11263       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
11264         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
11265       else if (stack_realign_drap
11266                && crtl->drap_reg
11267                && crtl->args.internal_arg_pointer == rtl
11268                && REGNO (crtl->drap_reg) < FIRST_PSEUDO_REGISTER)
11269         {
11270           /* If RTL is internal_arg_pointer, which has been optimized
11271              out, use DRAP instead.  */
11272           mem_loc_result = based_loc_descr (crtl->drap_reg, 0,
11273                                             VAR_INIT_STATUS_INITIALIZED);
11274         }
11275       break;
11276
11277     case SIGN_EXTEND:
11278     case ZERO_EXTEND:
11279       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11280                                 VAR_INIT_STATUS_INITIALIZED);
11281       if (op0 == 0)
11282         break;
11283       else
11284         {
11285           int shift = DWARF2_ADDR_SIZE
11286                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11287           shift *= BITS_PER_UNIT;
11288           if (GET_CODE (rtl) == SIGN_EXTEND)
11289             op = DW_OP_shra;
11290           else
11291             op = DW_OP_shr;
11292           mem_loc_result = op0;
11293           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11294           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11295           add_loc_descr (&mem_loc_result, int_loc_descriptor (shift));
11296           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11297         }
11298       break;
11299
11300     case MEM:
11301       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11302                                            VAR_INIT_STATUS_INITIALIZED);
11303       if (mem_loc_result == NULL)
11304         mem_loc_result = tls_mem_loc_descriptor (rtl);
11305       if (mem_loc_result != 0)
11306         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
11307       break;
11308
11309     case LO_SUM:
11310          rtl = XEXP (rtl, 1);
11311
11312       /* ... fall through ...  */
11313
11314     case LABEL_REF:
11315       /* Some ports can transform a symbol ref into a label ref, because
11316          the symbol ref is too far away and has to be dumped into a constant
11317          pool.  */
11318     case CONST:
11319     case SYMBOL_REF:
11320       /* Alternatively, the symbol in the constant pool might be referenced
11321          by a different symbol.  */
11322       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
11323         {
11324           bool marked;
11325           rtx tmp = get_pool_constant_mark (rtl, &marked);
11326
11327           if (GET_CODE (tmp) == SYMBOL_REF)
11328             {
11329               rtl = tmp;
11330               if (CONSTANT_POOL_ADDRESS_P (tmp))
11331                 get_pool_constant_mark (tmp, &marked);
11332               else
11333                 marked = true;
11334             }
11335
11336           /* If all references to this pool constant were optimized away,
11337              it was not output and thus we can't represent it.
11338              FIXME: might try to use DW_OP_const_value here, though
11339              DW_OP_piece complicates it.  */
11340           if (!marked)
11341             {
11342               expansion_failed (NULL_TREE, rtl,
11343                                 "Constant was removed from constant pool.\n");
11344               return 0;
11345             }
11346         }
11347
11348       if (GET_CODE (rtl) == SYMBOL_REF
11349           && SYMBOL_REF_TLS_MODEL (rtl) != TLS_MODEL_NONE)
11350         {
11351           dw_loc_descr_ref temp;
11352
11353           /* If this is not defined, we have no way to emit the data.  */
11354           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
11355             break;
11356
11357           temp = new_loc_descr (DW_OP_addr, 0, 0);
11358           temp->dw_loc_oprnd1.val_class = dw_val_class_addr;
11359           temp->dw_loc_oprnd1.v.val_addr = rtl;
11360           temp->dtprel = true;
11361
11362           mem_loc_result = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
11363           add_loc_descr (&mem_loc_result, temp);
11364
11365           break;
11366         }
11367
11368       if (!const_ok_for_output (rtl))
11369         break;
11370
11371     symref:
11372       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
11373       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
11374       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
11375       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
11376       break;
11377
11378     case CONCAT:
11379     case CONCATN:
11380     case VAR_LOCATION:
11381       expansion_failed (NULL_TREE, rtl,
11382                         "CONCAT/CONCATN/VAR_LOCATION is handled only by loc_descriptor");
11383       return 0;
11384
11385     case PRE_MODIFY:
11386       /* Extract the PLUS expression nested inside and fall into
11387          PLUS code below.  */
11388       rtl = XEXP (rtl, 1);
11389       goto plus;
11390
11391     case PRE_INC:
11392     case PRE_DEC:
11393       /* Turn these into a PLUS expression and fall into the PLUS code
11394          below.  */
11395       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
11396                           GEN_INT (GET_CODE (rtl) == PRE_INC
11397                                    ? GET_MODE_UNIT_SIZE (mode)
11398                                    : -GET_MODE_UNIT_SIZE (mode)));
11399
11400       /* ... fall through ...  */
11401
11402     case PLUS:
11403     plus:
11404       if (is_based_loc (rtl))
11405         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
11406                                           INTVAL (XEXP (rtl, 1)),
11407                                           VAR_INIT_STATUS_INITIALIZED);
11408       else
11409         {
11410           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
11411                                                VAR_INIT_STATUS_INITIALIZED);
11412           if (mem_loc_result == 0)
11413             break;
11414
11415           if (CONST_INT_P (XEXP (rtl, 1)))
11416             loc_descr_plus_const (&mem_loc_result, INTVAL (XEXP (rtl, 1)));
11417           else
11418             {
11419               dw_loc_descr_ref mem_loc_result2
11420                 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11421                                       VAR_INIT_STATUS_INITIALIZED);
11422               if (mem_loc_result2 == 0)
11423                 break;
11424               add_loc_descr (&mem_loc_result, mem_loc_result2);
11425               add_loc_descr (&mem_loc_result,
11426                              new_loc_descr (DW_OP_plus, 0, 0));
11427             }
11428         }
11429       break;
11430
11431     /* If a pseudo-reg is optimized away, it is possible for it to
11432        be replaced with a MEM containing a multiply or shift.  */
11433     case MINUS:
11434       op = DW_OP_minus;
11435       goto do_binop;
11436
11437     case MULT:
11438       op = DW_OP_mul;
11439       goto do_binop;
11440
11441     case DIV:
11442       op = DW_OP_div;
11443       goto do_binop;
11444
11445     case MOD:
11446       op = DW_OP_mod;
11447       goto do_binop;
11448
11449     case ASHIFT:
11450       op = DW_OP_shl;
11451       goto do_binop;
11452
11453     case ASHIFTRT:
11454       op = DW_OP_shra;
11455       goto do_binop;
11456
11457     case LSHIFTRT:
11458       op = DW_OP_shr;
11459       goto do_binop;
11460
11461     case AND:
11462       op = DW_OP_and;
11463       goto do_binop;
11464
11465     case IOR:
11466       op = DW_OP_or;
11467       goto do_binop;
11468
11469     case XOR:
11470       op = DW_OP_xor;
11471       goto do_binop;
11472
11473     do_binop:
11474       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11475                                 VAR_INIT_STATUS_INITIALIZED);
11476       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11477                                 VAR_INIT_STATUS_INITIALIZED);
11478
11479       if (op0 == 0 || op1 == 0)
11480         break;
11481
11482       mem_loc_result = op0;
11483       add_loc_descr (&mem_loc_result, op1);
11484       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11485       break;
11486
11487     case NOT:
11488       op = DW_OP_not;
11489       goto do_unop;
11490
11491     case ABS:
11492       op = DW_OP_abs;
11493       goto do_unop;
11494
11495     case NEG:
11496       op = DW_OP_neg;
11497       goto do_unop;
11498
11499     do_unop:
11500       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11501                                 VAR_INIT_STATUS_INITIALIZED);
11502
11503       if (op0 == 0)
11504         break;
11505
11506       mem_loc_result = op0;
11507       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11508       break;
11509
11510     case CONST_INT:
11511       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
11512       break;
11513
11514     case EQ:
11515       op = DW_OP_eq;
11516       goto do_scompare;
11517
11518     case GE:
11519       op = DW_OP_ge;
11520       goto do_scompare;
11521
11522     case GT:
11523       op = DW_OP_gt;
11524       goto do_scompare;
11525
11526     case LE:
11527       op = DW_OP_le;
11528       goto do_scompare;
11529
11530     case LT:
11531       op = DW_OP_lt;
11532       goto do_scompare;
11533
11534     case NE:
11535       op = DW_OP_ne;
11536       goto do_scompare;
11537
11538     do_scompare:
11539       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11540           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11541           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11542         break;
11543
11544       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11545                                 VAR_INIT_STATUS_INITIALIZED);
11546       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11547                                 VAR_INIT_STATUS_INITIALIZED);
11548
11549       if (op0 == 0 || op1 == 0)
11550         break;
11551
11552       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11553         {
11554           int shift = DWARF2_ADDR_SIZE
11555                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11556           shift *= BITS_PER_UNIT;
11557           add_loc_descr (&op0, int_loc_descriptor (shift));
11558           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11559           if (CONST_INT_P (XEXP (rtl, 1)))
11560             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) << shift);
11561           else
11562             {
11563               add_loc_descr (&op1, int_loc_descriptor (shift));
11564               add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11565             }
11566         }
11567
11568     do_compare:
11569       mem_loc_result = op0;
11570       add_loc_descr (&mem_loc_result, op1);
11571       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11572       if (STORE_FLAG_VALUE != 1)
11573         {
11574           add_loc_descr (&mem_loc_result,
11575                          int_loc_descriptor (STORE_FLAG_VALUE));
11576           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_mul, 0, 0));
11577         }
11578       break;
11579
11580     case GEU:
11581       op = DW_OP_ge;
11582       goto do_ucompare;
11583
11584     case GTU:
11585       op = DW_OP_gt;
11586       goto do_ucompare;
11587
11588     case LEU:
11589       op = DW_OP_le;
11590       goto do_ucompare;
11591
11592     case LTU:
11593       op = DW_OP_lt;
11594       goto do_ucompare;
11595
11596     do_ucompare:
11597       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11598           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11599           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11600         break;
11601
11602       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11603                                 VAR_INIT_STATUS_INITIALIZED);
11604       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11605                                 VAR_INIT_STATUS_INITIALIZED);
11606
11607       if (op0 == 0 || op1 == 0)
11608         break;
11609
11610       if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11611         {
11612           HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11613           add_loc_descr (&op0, int_loc_descriptor (mask));
11614           add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11615           if (CONST_INT_P (XEXP (rtl, 1)))
11616             op1 = int_loc_descriptor (INTVAL (XEXP (rtl, 1)) & mask);
11617           else
11618             {
11619               add_loc_descr (&op1, int_loc_descriptor (mask));
11620               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11621             }
11622         }
11623       else
11624         {
11625           HOST_WIDE_INT bias = 1;
11626           bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11627           add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11628           if (CONST_INT_P (XEXP (rtl, 1)))
11629             op1 = int_loc_descriptor ((unsigned HOST_WIDE_INT) bias
11630                                       + INTVAL (XEXP (rtl, 1)));
11631           else
11632             add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11633         }
11634       goto do_compare;
11635
11636     case SMIN:
11637     case SMAX:
11638     case UMIN:
11639     case UMAX:
11640       if (GET_MODE_CLASS (GET_MODE (XEXP (rtl, 0))) != MODE_INT
11641           || GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) > DWARF2_ADDR_SIZE
11642           || GET_MODE (XEXP (rtl, 0)) != GET_MODE (XEXP (rtl, 1)))
11643         break;
11644
11645       op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11646                                 VAR_INIT_STATUS_INITIALIZED);
11647       op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
11648                                 VAR_INIT_STATUS_INITIALIZED);
11649
11650       if (op0 == 0 || op1 == 0)
11651         break;
11652
11653       add_loc_descr (&op0, new_loc_descr (DW_OP_dup, 0, 0));
11654       add_loc_descr (&op1, new_loc_descr (DW_OP_swap, 0, 0));
11655       add_loc_descr (&op1, new_loc_descr (DW_OP_over, 0, 0));
11656       if (GET_CODE (rtl) == UMIN || GET_CODE (rtl) == UMAX)
11657         {
11658           if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11659             {
11660               HOST_WIDE_INT mask = GET_MODE_MASK (GET_MODE (XEXP (rtl, 0)));
11661               add_loc_descr (&op0, int_loc_descriptor (mask));
11662               add_loc_descr (&op0, new_loc_descr (DW_OP_and, 0, 0));
11663               add_loc_descr (&op1, int_loc_descriptor (mask));
11664               add_loc_descr (&op1, new_loc_descr (DW_OP_and, 0, 0));
11665             }
11666           else
11667             {
11668               HOST_WIDE_INT bias = 1;
11669               bias <<= (DWARF2_ADDR_SIZE * BITS_PER_UNIT - 1);
11670               add_loc_descr (&op0, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11671               add_loc_descr (&op1, new_loc_descr (DW_OP_plus_uconst, bias, 0));
11672             }
11673         }
11674       else if (GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0))) < DWARF2_ADDR_SIZE)
11675         {
11676           int shift = DWARF2_ADDR_SIZE
11677                       - GET_MODE_SIZE (GET_MODE (XEXP (rtl, 0)));
11678           shift *= BITS_PER_UNIT;
11679           add_loc_descr (&op0, int_loc_descriptor (shift));
11680           add_loc_descr (&op0, new_loc_descr (DW_OP_shl, 0, 0));
11681           add_loc_descr (&op1, int_loc_descriptor (shift));
11682           add_loc_descr (&op1, new_loc_descr (DW_OP_shl, 0, 0));
11683         }
11684
11685       if (GET_CODE (rtl) == SMIN || GET_CODE (rtl) == UMIN)
11686         op = DW_OP_lt;
11687       else
11688         op = DW_OP_gt;
11689       mem_loc_result = op0;
11690       add_loc_descr (&mem_loc_result, op1);
11691       add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11692       {
11693         dw_loc_descr_ref bra_node, drop_node;
11694
11695         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
11696         add_loc_descr (&mem_loc_result, bra_node);
11697         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_swap, 0, 0));
11698         drop_node = new_loc_descr (DW_OP_drop, 0, 0);
11699         add_loc_descr (&mem_loc_result, drop_node);
11700         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
11701         bra_node->dw_loc_oprnd1.v.val_loc = drop_node;
11702       }
11703       break;
11704
11705     case ZERO_EXTRACT:
11706     case SIGN_EXTRACT:
11707       if (CONST_INT_P (XEXP (rtl, 1))
11708           && CONST_INT_P (XEXP (rtl, 2))
11709           && ((unsigned) INTVAL (XEXP (rtl, 1))
11710               + (unsigned) INTVAL (XEXP (rtl, 2))
11711               <= GET_MODE_BITSIZE (GET_MODE (rtl)))
11712           && GET_MODE_BITSIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
11713           && GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0))) <= DWARF2_ADDR_SIZE)
11714         {
11715           int shift, size;
11716           op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
11717                                     VAR_INIT_STATUS_INITIALIZED);
11718           if (op0 == 0)
11719             break;
11720           if (GET_CODE (rtl) == SIGN_EXTRACT)
11721             op = DW_OP_shra;
11722           else
11723             op = DW_OP_shr;
11724           mem_loc_result = op0;
11725           size = INTVAL (XEXP (rtl, 1));
11726           shift = INTVAL (XEXP (rtl, 2));
11727           if (BITS_BIG_ENDIAN)
11728             shift = GET_MODE_BITSIZE (GET_MODE (XEXP (rtl, 0)))
11729                     - shift - size;
11730           add_loc_descr (&mem_loc_result,
11731                          int_loc_descriptor (DWARF2_ADDR_SIZE - shift - size));
11732           add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_shl, 0, 0));
11733           add_loc_descr (&mem_loc_result,
11734                          int_loc_descriptor (DWARF2_ADDR_SIZE - size));
11735           add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
11736         }
11737       break;
11738
11739     case COMPARE:
11740     case IF_THEN_ELSE:
11741     case ROTATE:
11742     case ROTATERT:
11743     case TRUNCATE:
11744       /* In theory, we could implement the above.  */
11745       /* DWARF cannot represent the unsigned compare operations
11746          natively.  */
11747     case SS_MULT:
11748     case US_MULT:
11749     case SS_DIV:
11750     case US_DIV:
11751     case UDIV:
11752     case UMOD:
11753     case UNORDERED:
11754     case ORDERED:
11755     case UNEQ:
11756     case UNGE:
11757     case UNLE:
11758     case UNLT:
11759     case LTGT:
11760     case FLOAT_EXTEND:
11761     case FLOAT_TRUNCATE:
11762     case FLOAT:
11763     case UNSIGNED_FLOAT:
11764     case FIX:
11765     case UNSIGNED_FIX:
11766     case FRACT_CONVERT:
11767     case UNSIGNED_FRACT_CONVERT:
11768     case SAT_FRACT:
11769     case UNSIGNED_SAT_FRACT:
11770     case SQRT:
11771     case BSWAP:
11772     case FFS:
11773     case CLZ:
11774     case CTZ:
11775     case POPCOUNT:
11776     case PARITY:
11777     case ASM_OPERANDS:
11778     case UNSPEC:
11779     case HIGH:
11780       /* If delegitimize_address couldn't do anything with the UNSPEC, we
11781          can't express it in the debug info.  This can happen e.g. with some
11782          TLS UNSPECs.  */
11783       break;
11784
11785     case CONST_STRING:
11786       resolve_one_addr (&rtl, NULL);
11787       goto symref;
11788
11789     default:
11790 #ifdef ENABLE_CHECKING
11791       print_rtl (stderr, rtl);
11792       gcc_unreachable ();
11793 #else
11794       break;
11795 #endif
11796     }
11797
11798   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11799     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11800
11801   return mem_loc_result;
11802 }
11803
11804 /* Return a descriptor that describes the concatenation of two locations.
11805    This is typically a complex variable.  */
11806
11807 static dw_loc_descr_ref
11808 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
11809 {
11810   dw_loc_descr_ref cc_loc_result = NULL;
11811   dw_loc_descr_ref x0_ref
11812     = loc_descriptor (x0, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11813   dw_loc_descr_ref x1_ref
11814     = loc_descriptor (x1, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11815
11816   if (x0_ref == 0 || x1_ref == 0)
11817     return 0;
11818
11819   cc_loc_result = x0_ref;
11820   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
11821
11822   add_loc_descr (&cc_loc_result, x1_ref);
11823   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
11824
11825   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
11826     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11827
11828   return cc_loc_result;
11829 }
11830
11831 /* Return a descriptor that describes the concatenation of N
11832    locations.  */
11833
11834 static dw_loc_descr_ref
11835 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
11836 {
11837   unsigned int i;
11838   dw_loc_descr_ref cc_loc_result = NULL;
11839   unsigned int n = XVECLEN (concatn, 0);
11840
11841   for (i = 0; i < n; ++i)
11842     {
11843       dw_loc_descr_ref ref;
11844       rtx x = XVECEXP (concatn, 0, i);
11845
11846       ref = loc_descriptor (x, VOIDmode, VAR_INIT_STATUS_INITIALIZED);
11847       if (ref == NULL)
11848         return NULL;
11849
11850       add_loc_descr (&cc_loc_result, ref);
11851       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
11852     }
11853
11854   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
11855     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
11856
11857   return cc_loc_result;
11858 }
11859
11860 /* Output a proper Dwarf location descriptor for a variable or parameter
11861    which is either allocated in a register or in a memory location.  For a
11862    register, we just generate an OP_REG and the register number.  For a
11863    memory location we provide a Dwarf postfix expression describing how to
11864    generate the (dynamic) address of the object onto the address stack.
11865
11866    MODE is mode of the decl if this loc_descriptor is going to be used in
11867    .debug_loc section where DW_OP_stack_value and DW_OP_implicit_value are
11868    allowed, VOIDmode otherwise.
11869
11870    If we don't know how to describe it, return 0.  */
11871
11872 static dw_loc_descr_ref
11873 loc_descriptor (rtx rtl, enum machine_mode mode,
11874                 enum var_init_status initialized)
11875 {
11876   dw_loc_descr_ref loc_result = NULL;
11877
11878   switch (GET_CODE (rtl))
11879     {
11880     case SUBREG:
11881       /* The case of a subreg may arise when we have a local (register)
11882          variable or a formal (register) parameter which doesn't quite fill
11883          up an entire register.  For now, just assume that it is
11884          legitimate to make the Dwarf info refer to the whole register which
11885          contains the given subreg.  */
11886       loc_result = loc_descriptor (SUBREG_REG (rtl), mode, initialized);
11887       break;
11888
11889     case REG:
11890       loc_result = reg_loc_descriptor (rtl, initialized);
11891       break;
11892
11893     case SIGN_EXTEND:
11894     case ZERO_EXTEND:
11895       loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
11896       break;
11897
11898     case MEM:
11899       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
11900                                        initialized);
11901       if (loc_result == NULL)
11902         loc_result = tls_mem_loc_descriptor (rtl);
11903       break;
11904
11905     case CONCAT:
11906       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
11907                                           initialized);
11908       break;
11909
11910     case CONCATN:
11911       loc_result = concatn_loc_descriptor (rtl, initialized);
11912       break;
11913
11914     case VAR_LOCATION:
11915       /* Single part.  */
11916       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
11917         {
11918           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), mode,
11919                                        initialized);
11920           break;
11921         }
11922
11923       rtl = XEXP (rtl, 1);
11924       /* FALLTHRU */
11925
11926     case PARALLEL:
11927       {
11928         rtvec par_elems = XVEC (rtl, 0);
11929         int num_elem = GET_NUM_ELEM (par_elems);
11930         enum machine_mode mode;
11931         int i;
11932
11933         /* Create the first one, so we have something to add to.  */
11934         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
11935                                      VOIDmode, initialized);
11936         if (loc_result == NULL)
11937           return NULL;
11938         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
11939         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11940         for (i = 1; i < num_elem; i++)
11941           {
11942             dw_loc_descr_ref temp;
11943
11944             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
11945                                    VOIDmode, initialized);
11946             if (temp == NULL)
11947               return NULL;
11948             add_loc_descr (&loc_result, temp);
11949             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
11950             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
11951           }
11952       }
11953       break;
11954
11955     case CONST_INT:
11956       if (mode != VOIDmode && mode != BLKmode)
11957         loc_result = address_of_int_loc_descriptor (GET_MODE_SIZE (mode),
11958                                                     INTVAL (rtl));
11959       break;
11960
11961     case CONST_DOUBLE:
11962       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
11963         {
11964           /* Note that a CONST_DOUBLE rtx could represent either an integer
11965              or a floating-point constant.  A CONST_DOUBLE is used whenever
11966              the constant requires more than one word in order to be
11967              adequately represented.  We output CONST_DOUBLEs as blocks.  */
11968           if (GET_MODE (rtl) != VOIDmode)
11969             mode = GET_MODE (rtl);
11970
11971           loc_result = new_loc_descr (DW_OP_implicit_value,
11972                                       GET_MODE_SIZE (mode), 0);
11973           if (SCALAR_FLOAT_MODE_P (mode))
11974             {
11975               unsigned int length = GET_MODE_SIZE (mode);
11976               unsigned char *array = GGC_NEWVEC (unsigned char, length);
11977
11978               insert_float (rtl, array);
11979               loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
11980               loc_result->dw_loc_oprnd2.v.val_vec.length = length / 4;
11981               loc_result->dw_loc_oprnd2.v.val_vec.elt_size = 4;
11982               loc_result->dw_loc_oprnd2.v.val_vec.array = array;
11983             }
11984           else
11985             {
11986               loc_result->dw_loc_oprnd2.val_class = dw_val_class_const_double;
11987               loc_result->dw_loc_oprnd2.v.val_double.high
11988                 = CONST_DOUBLE_HIGH (rtl);
11989               loc_result->dw_loc_oprnd2.v.val_double.low
11990                 = CONST_DOUBLE_LOW (rtl);
11991             }
11992         }
11993       break;
11994
11995     case CONST_VECTOR:
11996       if (mode != VOIDmode && (dwarf_version >= 4 || !dwarf_strict))
11997         {
11998           unsigned int elt_size = GET_MODE_UNIT_SIZE (GET_MODE (rtl));
11999           unsigned int length = CONST_VECTOR_NUNITS (rtl);
12000           unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
12001           unsigned int i;
12002           unsigned char *p;
12003
12004           mode = GET_MODE (rtl);
12005           switch (GET_MODE_CLASS (mode))
12006             {
12007             case MODE_VECTOR_INT:
12008               for (i = 0, p = array; i < length; i++, p += elt_size)
12009                 {
12010                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12011                   HOST_WIDE_INT lo, hi;
12012
12013                   switch (GET_CODE (elt))
12014                     {
12015                     case CONST_INT:
12016                       lo = INTVAL (elt);
12017                       hi = -(lo < 0);
12018                       break;
12019
12020                     case CONST_DOUBLE:
12021                       lo = CONST_DOUBLE_LOW (elt);
12022                       hi = CONST_DOUBLE_HIGH (elt);
12023                       break;
12024
12025                     default:
12026                       gcc_unreachable ();
12027                     }
12028
12029                   if (elt_size <= sizeof (HOST_WIDE_INT))
12030                     insert_int (lo, elt_size, p);
12031                   else
12032                     {
12033                       unsigned char *p0 = p;
12034                       unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
12035
12036                       gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
12037                       if (WORDS_BIG_ENDIAN)
12038                         {
12039                           p0 = p1;
12040                           p1 = p;
12041                         }
12042                       insert_int (lo, sizeof (HOST_WIDE_INT), p0);
12043                       insert_int (hi, sizeof (HOST_WIDE_INT), p1);
12044                     }
12045                 }
12046               break;
12047
12048             case MODE_VECTOR_FLOAT:
12049               for (i = 0, p = array; i < length; i++, p += elt_size)
12050                 {
12051                   rtx elt = CONST_VECTOR_ELT (rtl, i);
12052                   insert_float (elt, p);
12053                 }
12054               break;
12055
12056             default:
12057               gcc_unreachable ();
12058             }
12059
12060           loc_result = new_loc_descr (DW_OP_implicit_value,
12061                                       length * elt_size, 0);
12062           loc_result->dw_loc_oprnd2.val_class = dw_val_class_vec;
12063           loc_result->dw_loc_oprnd2.v.val_vec.length = length;
12064           loc_result->dw_loc_oprnd2.v.val_vec.elt_size = elt_size;
12065           loc_result->dw_loc_oprnd2.v.val_vec.array = array;
12066         }
12067       break;
12068
12069     case CONST:
12070       if (mode == VOIDmode
12071           || GET_CODE (XEXP (rtl, 0)) == CONST_INT
12072           || GET_CODE (XEXP (rtl, 0)) == CONST_DOUBLE
12073           || GET_CODE (XEXP (rtl, 0)) == CONST_VECTOR)
12074         {
12075           loc_result = loc_descriptor (XEXP (rtl, 0), mode, initialized);
12076           break;
12077         }
12078       /* FALLTHROUGH */
12079     case SYMBOL_REF:
12080       if (!const_ok_for_output (rtl))
12081         break;
12082     case LABEL_REF:
12083       if (mode != VOIDmode && GET_MODE_SIZE (mode) == DWARF2_ADDR_SIZE
12084           && (dwarf_version >= 4 || !dwarf_strict))
12085         {
12086           loc_result = new_loc_descr (DW_OP_implicit_value,
12087                                       DWARF2_ADDR_SIZE, 0);
12088           loc_result->dw_loc_oprnd2.val_class = dw_val_class_addr;
12089           loc_result->dw_loc_oprnd2.v.val_addr = rtl;
12090           VEC_safe_push (rtx, gc, used_rtx_array, rtl);
12091         }
12092       break;
12093
12094     default:
12095       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (rtl) == mode
12096           && GET_MODE_SIZE (GET_MODE (rtl)) <= DWARF2_ADDR_SIZE
12097           && (dwarf_version >= 4 || !dwarf_strict))
12098         {
12099           /* Value expression.  */
12100           loc_result = mem_loc_descriptor (rtl, VOIDmode, initialized);
12101           if (loc_result)
12102             add_loc_descr (&loc_result,
12103                            new_loc_descr (DW_OP_stack_value, 0, 0));
12104         }
12105       break;
12106     }
12107
12108   return loc_result;
12109 }
12110
12111 /* We need to figure out what section we should use as the base for the
12112    address ranges where a given location is valid.
12113    1. If this particular DECL has a section associated with it, use that.
12114    2. If this function has a section associated with it, use that.
12115    3. Otherwise, use the text section.
12116    XXX: If you split a variable across multiple sections, we won't notice.  */
12117
12118 static const char *
12119 secname_for_decl (const_tree decl)
12120 {
12121   const char *secname;
12122
12123   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
12124     {
12125       tree sectree = DECL_SECTION_NAME (decl);
12126       secname = TREE_STRING_POINTER (sectree);
12127     }
12128   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
12129     {
12130       tree sectree = DECL_SECTION_NAME (current_function_decl);
12131       secname = TREE_STRING_POINTER (sectree);
12132     }
12133   else if (cfun && in_cold_section_p)
12134     secname = crtl->subsections.cold_section_label;
12135   else
12136     secname = text_section_label;
12137
12138   return secname;
12139 }
12140
12141 /* Return true when DECL_BY_REFERENCE is defined and set for DECL.  */
12142
12143 static bool
12144 decl_by_reference_p (tree decl)
12145 {
12146   return ((TREE_CODE (decl) == PARM_DECL || TREE_CODE (decl) == RESULT_DECL
12147            || TREE_CODE (decl) == VAR_DECL)
12148           && DECL_BY_REFERENCE (decl));
12149 }
12150
12151
12152 /* Dereference a location expression LOC if DECL is passed by invisible
12153    reference.  */
12154
12155 static dw_loc_descr_ref
12156 loc_by_reference (dw_loc_descr_ref loc, tree decl)
12157 {
12158   HOST_WIDE_INT size;
12159   enum dwarf_location_atom op;
12160
12161   if (loc == NULL)
12162     return NULL;
12163
12164   if (!decl_by_reference_p (decl))
12165     return loc;
12166
12167   /* If loc is DW_OP_reg{0...31,x}, don't add DW_OP_deref, instead
12168      change it into corresponding DW_OP_breg{0...31,x} 0.  Then the
12169      location expression is considered to be address of a memory location,
12170      rather than the register itself.  */
12171   if (((loc->dw_loc_opc >= DW_OP_reg0 && loc->dw_loc_opc <= DW_OP_reg31)
12172        || loc->dw_loc_opc == DW_OP_regx)
12173       && (loc->dw_loc_next == NULL
12174           || (loc->dw_loc_next->dw_loc_opc == DW_OP_GNU_uninit
12175               && loc->dw_loc_next->dw_loc_next == NULL)))
12176     {
12177       if (loc->dw_loc_opc == DW_OP_regx)
12178         {
12179           loc->dw_loc_opc = DW_OP_bregx;
12180           loc->dw_loc_oprnd2.v.val_int = 0;
12181         }
12182       else
12183         {
12184           loc->dw_loc_opc
12185             = (enum dwarf_location_atom)
12186               (loc->dw_loc_opc + (DW_OP_breg0 - DW_OP_reg0));
12187           loc->dw_loc_oprnd1.v.val_int = 0;
12188         }
12189       return loc;
12190     }
12191
12192   size = int_size_in_bytes (TREE_TYPE (decl));
12193   if (size > DWARF2_ADDR_SIZE || size == -1)
12194     return 0;
12195   else if (size == DWARF2_ADDR_SIZE)
12196     op = DW_OP_deref;
12197   else
12198     op = DW_OP_deref_size;
12199   add_loc_descr (&loc, new_loc_descr (op, size, 0));
12200   return loc;
12201 }
12202
12203 /* Return single element location list containing loc descr REF.  */
12204
12205 static dw_loc_list_ref
12206 single_element_loc_list (dw_loc_descr_ref ref)
12207 {
12208   return new_loc_list (ref, NULL, NULL, NULL, 0);
12209 }
12210
12211 /* Return dwarf representation of location list representing for
12212    LOC_LIST of DECL.  */
12213
12214 static dw_loc_list_ref
12215 dw_loc_list (var_loc_list * loc_list, tree decl, bool toplevel)
12216 {
12217   const char *endname, *secname;
12218   dw_loc_list_ref list;
12219   rtx varloc;
12220   enum var_init_status initialized;
12221   struct var_loc_node *node;
12222   dw_loc_descr_ref descr;
12223   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12224
12225   bool by_reference = decl_by_reference_p (decl);
12226
12227   /* Now that we know what section we are using for a base,
12228      actually construct the list of locations.
12229      The first location information is what is passed to the
12230      function that creates the location list, and the remaining
12231      locations just get added on to that list.
12232      Note that we only know the start address for a location
12233      (IE location changes), so to build the range, we use
12234      the range [current location start, next location start].
12235      This means we have to special case the last node, and generate
12236      a range of [last location start, end of function label].  */
12237
12238   node = loc_list->first;
12239   varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12240   secname = secname_for_decl (decl);
12241
12242   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
12243     initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12244   else
12245     initialized = VAR_INIT_STATUS_INITIALIZED;
12246
12247   if (!toplevel || by_reference)
12248     {
12249       gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12250       /* Single part.  */
12251       if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12252         descr = loc_by_reference (mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12253                                                       TYPE_MODE (TREE_TYPE (decl)),
12254                                                       initialized),
12255                                   decl);
12256       else
12257         descr = NULL;
12258     }
12259   else
12260     descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12261
12262   if (loc_list && loc_list->first != loc_list->last)
12263     list = new_loc_list (descr, node->label, node->next->label, secname, 1);
12264   else
12265     return single_element_loc_list (descr);
12266   node = node->next;
12267
12268   if (!node)
12269     return NULL;
12270
12271   for (; node->next; node = node->next)
12272     if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12273       {
12274         /* The variable has a location between NODE->LABEL and
12275            NODE->NEXT->LABEL.  */
12276         enum var_init_status initialized =
12277           NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12278         varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12279         if (!toplevel || by_reference)
12280           {
12281             gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12282             /* Single part.  */
12283             if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12284               descr = mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12285                                           TYPE_MODE (TREE_TYPE (decl)), initialized);
12286             else
12287               descr = NULL;
12288             descr = loc_by_reference (descr, decl);
12289           }
12290         else
12291           descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12292         add_loc_descr_to_loc_list (&list, descr,
12293                                    node->label, node->next->label, secname);
12294       }
12295
12296   /* If the variable has a location at the last label
12297      it keeps its location until the end of function.  */
12298   if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
12299     {
12300       enum var_init_status initialized =
12301         NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
12302
12303       if (!current_function_decl)
12304         endname = text_end_label;
12305       else
12306         {
12307           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12308                                        current_function_funcdef_no);
12309           endname = ggc_strdup (label_id);
12310         }
12311
12312       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
12313       if (!toplevel || by_reference)
12314         {
12315           gcc_assert (GET_CODE (varloc) == VAR_LOCATION);
12316           /* Single part.  */
12317           if (GET_CODE (XEXP (varloc, 1)) != PARALLEL)
12318             descr = mem_loc_descriptor (XEXP (XEXP (varloc, 1), 0),
12319                                         TYPE_MODE (TREE_TYPE (decl)), initialized);
12320           else
12321             descr = NULL;
12322           descr = loc_by_reference (descr, decl);
12323         }
12324       else
12325         descr = loc_descriptor (varloc, DECL_MODE (decl), initialized);
12326       add_loc_descr_to_loc_list (&list, descr, node->label, endname, secname);
12327     }
12328   return list;
12329 }
12330
12331 /* Return if the loc_list has only single element and thus can be represented
12332    as location description.   */
12333
12334 static bool
12335 single_element_loc_list_p (dw_loc_list_ref list)
12336 {
12337   return (!list->dw_loc_next && !list->begin && !list->end);
12338 }
12339
12340 /* To each location in list LIST add loc descr REF.  */
12341
12342 static void
12343 add_loc_descr_to_each (dw_loc_list_ref list, dw_loc_descr_ref ref)
12344 {
12345   dw_loc_descr_ref copy;
12346   add_loc_descr (&list->expr, ref);
12347   list = list->dw_loc_next;
12348   while (list)
12349     {
12350       copy = GGC_CNEW (dw_loc_descr_node);
12351       memcpy (copy, ref, sizeof (dw_loc_descr_node));
12352       add_loc_descr (&list->expr, copy);
12353       while (copy->dw_loc_next)
12354         {
12355           dw_loc_descr_ref new_copy = GGC_CNEW (dw_loc_descr_node);
12356           memcpy (new_copy, copy->dw_loc_next, sizeof (dw_loc_descr_node));
12357           copy->dw_loc_next = new_copy;
12358           copy = new_copy;
12359         }
12360       list = list->dw_loc_next;
12361     }
12362 }
12363
12364 /* Given two lists RET and LIST
12365    produce location list that is result of adding expression in LIST
12366    to expression in RET on each possition in program.
12367    Might be destructive on both RET and LIST.
12368
12369    TODO: We handle only simple cases of RET or LIST having at most one
12370    element. General case would inolve sorting the lists in program order
12371    and merging them that will need some additional work.  
12372    Adding that will improve quality of debug info especially for SRA-ed
12373    structures.  */
12374
12375 static void
12376 add_loc_list (dw_loc_list_ref *ret, dw_loc_list_ref list)
12377 {
12378   if (!list)
12379     return;
12380   if (!*ret)
12381     {
12382       *ret = list;
12383       return;
12384     }
12385   if (!list->dw_loc_next)
12386     {
12387       add_loc_descr_to_each (*ret, list->expr);
12388       return;
12389     }
12390   if (!(*ret)->dw_loc_next)
12391     {
12392       add_loc_descr_to_each (list, (*ret)->expr);
12393       *ret = list;
12394       return;
12395     }
12396   expansion_failed (NULL_TREE, NULL_RTX,
12397                     "Don't know how to merge two non-trivial"
12398                     " location lists.\n");
12399   *ret = NULL;
12400   return;
12401 }
12402
12403 /* LOC is constant expression.  Try a luck, look it up in constant
12404    pool and return its loc_descr of its address.  */
12405
12406 static dw_loc_descr_ref
12407 cst_pool_loc_descr (tree loc)
12408 {
12409   /* Get an RTL for this, if something has been emitted.  */
12410   rtx rtl = lookup_constant_def (loc);
12411   enum machine_mode mode;
12412
12413   if (!rtl || !MEM_P (rtl))
12414     {
12415       gcc_assert (!rtl);
12416       return 0;
12417     }
12418   gcc_assert (GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF);
12419
12420   /* TODO: We might get more coverage if we was actually delaying expansion
12421      of all expressions till end of compilation when constant pools are fully
12422      populated.  */
12423   if (!TREE_ASM_WRITTEN (SYMBOL_REF_DECL (XEXP (rtl, 0))))
12424     {
12425       expansion_failed (loc, NULL_RTX,
12426                         "CST value in contant pool but not marked.");
12427       return 0;
12428     }
12429   mode = GET_MODE (rtl);
12430   rtl = XEXP (rtl, 0);
12431   return mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12432 }
12433
12434 /* Return dw_loc_list representing address of addr_expr LOC
12435    by looking for innder INDIRECT_REF expression and turing it
12436    into simple arithmetics.  */
12437
12438 static dw_loc_list_ref
12439 loc_list_for_address_of_addr_expr_of_indirect_ref (tree loc, bool toplev)
12440 {
12441   tree obj, offset;
12442   HOST_WIDE_INT bitsize, bitpos, bytepos;
12443   enum machine_mode mode;
12444   int volatilep;
12445   int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12446   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12447
12448   obj = get_inner_reference (TREE_OPERAND (loc, 0),
12449                              &bitsize, &bitpos, &offset, &mode,
12450                              &unsignedp, &volatilep, false);
12451   STRIP_NOPS (obj);
12452   if (bitpos % BITS_PER_UNIT)
12453     {
12454       expansion_failed (loc, NULL_RTX, "bitfield access");
12455       return 0;
12456     }
12457   if (!INDIRECT_REF_P (obj))
12458     {
12459       expansion_failed (obj,
12460                         NULL_RTX, "no indirect ref in inner refrence");
12461       return 0;
12462     }
12463   if (!offset && !bitpos)
12464     list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), toplev ? 2 : 1);
12465   else if (toplev
12466            && int_size_in_bytes (TREE_TYPE (loc)) <= DWARF2_ADDR_SIZE
12467            && (dwarf_version >= 4 || !dwarf_strict))
12468     {
12469       list_ret = loc_list_from_tree (TREE_OPERAND (obj, 0), 0);
12470       if (!list_ret)
12471         return 0;
12472       if (offset)
12473         {
12474           /* Variable offset.  */
12475           list_ret1 = loc_list_from_tree (offset, 0);
12476           if (list_ret1 == 0)
12477             return 0;
12478           add_loc_list (&list_ret, list_ret1);
12479           if (!list_ret)
12480             return 0;
12481           add_loc_descr_to_each (list_ret,
12482                                  new_loc_descr (DW_OP_plus, 0, 0));
12483         }
12484       bytepos = bitpos / BITS_PER_UNIT;
12485       if (bytepos > 0)
12486         add_loc_descr_to_each (list_ret,
12487                                new_loc_descr (DW_OP_plus_uconst,
12488                                               bytepos, 0));
12489       else if (bytepos < 0)
12490         loc_list_plus_const (list_ret, bytepos);
12491       add_loc_descr_to_each (list_ret,
12492                              new_loc_descr (DW_OP_stack_value, 0, 0));
12493     }
12494   return list_ret;
12495 }
12496
12497
12498 /* Generate Dwarf location list representing LOC.
12499    If WANT_ADDRESS is false, expression computing LOC will be computed
12500    If WANT_ADDRESS is 1, expression computing address of LOC will be returned
12501    if WANT_ADDRESS is 2, expression computing address useable in location
12502      will be returned (i.e. DW_OP_reg can be used
12503      to refer to register values) 
12504    TODO: Dwarf4 adds types to the stack machine that ought to be used here
12505    DW_OP_stack_value will help in cases where we fail to find address of the
12506    expression.
12507  */
12508
12509 static dw_loc_list_ref
12510 loc_list_from_tree (tree loc, int want_address)
12511 {
12512   dw_loc_descr_ref ret = NULL, ret1 = NULL;
12513   dw_loc_list_ref list_ret = NULL, list_ret1 = NULL;
12514   int have_address = 0;
12515   enum dwarf_location_atom op;
12516
12517   /* ??? Most of the time we do not take proper care for sign/zero
12518      extending the values properly.  Hopefully this won't be a real
12519      problem...  */
12520
12521   switch (TREE_CODE (loc))
12522     {
12523     case ERROR_MARK:
12524       expansion_failed (loc, NULL_RTX, "ERROR_MARK");
12525       return 0;
12526
12527     case PLACEHOLDER_EXPR:
12528       /* This case involves extracting fields from an object to determine the
12529          position of other fields.  We don't try to encode this here.  The
12530          only user of this is Ada, which encodes the needed information using
12531          the names of types.  */
12532       expansion_failed (loc, NULL_RTX, "PLACEHOLDER_EXPR");
12533       return 0;
12534
12535     case CALL_EXPR:
12536       expansion_failed (loc, NULL_RTX, "CALL_EXPR");
12537       /* There are no opcodes for these operations.  */
12538       return 0;
12539
12540     case PREINCREMENT_EXPR:
12541     case PREDECREMENT_EXPR:
12542     case POSTINCREMENT_EXPR:
12543     case POSTDECREMENT_EXPR:
12544       expansion_failed (loc, NULL_RTX, "PRE/POST INDCREMENT/DECREMENT");
12545       /* There are no opcodes for these operations.  */
12546       return 0;
12547
12548     case ADDR_EXPR:
12549       /* If we already want an address, see if there is INDIRECT_REF inside
12550          e.g. for &this->field.  */
12551       if (want_address)
12552         {
12553           list_ret = loc_list_for_address_of_addr_expr_of_indirect_ref
12554                        (loc, want_address == 2);
12555           if (list_ret)
12556             have_address = 1;
12557           else if (decl_address_ip_invariant_p (TREE_OPERAND (loc, 0))
12558                    && (ret = cst_pool_loc_descr (loc)))
12559             have_address = 1;
12560         }
12561         /* Otherwise, process the argument and look for the address.  */
12562       if (!list_ret && !ret)
12563         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 1);
12564       else
12565         {
12566           if (want_address)
12567             expansion_failed (loc, NULL_RTX, "need address of ADDR_EXPR");
12568           return NULL;
12569         }
12570       break;
12571
12572     case VAR_DECL:
12573       if (DECL_THREAD_LOCAL_P (loc))
12574         {
12575           rtx rtl;
12576           enum dwarf_location_atom first_op;
12577           enum dwarf_location_atom second_op;
12578           bool dtprel = false;
12579
12580           if (targetm.have_tls)
12581             {
12582               /* If this is not defined, we have no way to emit the
12583                  data.  */
12584               if (!targetm.asm_out.output_dwarf_dtprel)
12585                 return 0;
12586
12587                /* The way DW_OP_GNU_push_tls_address is specified, we
12588                   can only look up addresses of objects in the current
12589                   module.  */
12590               if (DECL_EXTERNAL (loc) && !targetm.binds_local_p (loc))
12591                 return 0;
12592               first_op = DW_OP_addr;
12593               dtprel = true;
12594               second_op = DW_OP_GNU_push_tls_address;
12595             }
12596           else
12597             {
12598               if (!targetm.emutls.debug_form_tls_address
12599                   || !(dwarf_version >= 3 || !dwarf_strict))
12600                 return 0;
12601               loc = emutls_decl (loc);
12602               first_op = DW_OP_addr;
12603               second_op = DW_OP_form_tls_address;
12604             }
12605
12606           rtl = rtl_for_decl_location (loc);
12607           if (rtl == NULL_RTX)
12608             return 0;
12609
12610           if (!MEM_P (rtl))
12611             return 0;
12612           rtl = XEXP (rtl, 0);
12613           if (! CONSTANT_P (rtl))
12614             return 0;
12615
12616           ret = new_loc_descr (first_op, 0, 0);
12617           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12618           ret->dw_loc_oprnd1.v.val_addr = rtl;
12619           ret->dtprel = dtprel;
12620
12621           ret1 = new_loc_descr (second_op, 0, 0);
12622           add_loc_descr (&ret, ret1);
12623
12624           have_address = 1;
12625           break;
12626         }
12627       /* FALLTHRU */
12628
12629     case PARM_DECL:
12630       if (DECL_HAS_VALUE_EXPR_P (loc))
12631         return loc_list_from_tree (DECL_VALUE_EXPR (loc),
12632                                    want_address);
12633       /* FALLTHRU */
12634
12635     case RESULT_DECL:
12636     case FUNCTION_DECL:
12637       {
12638         rtx rtl = rtl_for_decl_location (loc);
12639         var_loc_list *loc_list = lookup_decl_loc (loc);
12640
12641         if (loc_list && loc_list->first
12642             && (list_ret = dw_loc_list (loc_list, loc, want_address == 2)))
12643           have_address = 1;
12644         else if (rtl == NULL_RTX)
12645           {
12646             expansion_failed (loc, NULL_RTX, "DECL has no RTL");
12647             return 0;
12648           }
12649         else if (CONST_INT_P (rtl))
12650           {
12651             HOST_WIDE_INT val = INTVAL (rtl);
12652             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
12653               val &= GET_MODE_MASK (DECL_MODE (loc));
12654             ret = int_loc_descriptor (val);
12655           }
12656         else if (GET_CODE (rtl) == CONST_STRING)
12657           {
12658             expansion_failed (loc, NULL_RTX, "CONST_STRING");
12659             return 0;
12660           }
12661         else if (CONSTANT_P (rtl) && const_ok_for_output (rtl))
12662           {
12663             ret = new_loc_descr (DW_OP_addr, 0, 0);
12664             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
12665             ret->dw_loc_oprnd1.v.val_addr = rtl;
12666           }
12667         else
12668           {
12669             enum machine_mode mode;
12670
12671             /* Certain constructs can only be represented at top-level.  */
12672             if (want_address == 2)
12673               {
12674                 ret = loc_descriptor (rtl, VOIDmode,
12675                                       VAR_INIT_STATUS_INITIALIZED);
12676                 have_address = 1;
12677               }
12678             else
12679               {
12680                 mode = GET_MODE (rtl);
12681                 if (MEM_P (rtl))
12682                   {
12683                     rtl = XEXP (rtl, 0);
12684                     have_address = 1;
12685                   }
12686                 ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
12687               }
12688             if (!ret)
12689               expansion_failed (loc, rtl,
12690                                 "failed to produce loc descriptor for rtl");
12691           }
12692       }
12693       break;
12694
12695     case INDIRECT_REF:
12696     case ALIGN_INDIRECT_REF:
12697     case MISALIGNED_INDIRECT_REF:
12698       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12699       have_address = 1;
12700       break;
12701
12702     case COMPOUND_EXPR:
12703       return loc_list_from_tree (TREE_OPERAND (loc, 1), want_address);
12704
12705     CASE_CONVERT:
12706     case VIEW_CONVERT_EXPR:
12707     case SAVE_EXPR:
12708     case MODIFY_EXPR:
12709       return loc_list_from_tree (TREE_OPERAND (loc, 0), want_address);
12710
12711     case COMPONENT_REF:
12712     case BIT_FIELD_REF:
12713     case ARRAY_REF:
12714     case ARRAY_RANGE_REF:
12715     case REALPART_EXPR:
12716     case IMAGPART_EXPR:
12717       {
12718         tree obj, offset;
12719         HOST_WIDE_INT bitsize, bitpos, bytepos;
12720         enum machine_mode mode;
12721         int volatilep;
12722         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
12723
12724         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
12725                                    &unsignedp, &volatilep, false);
12726
12727         gcc_assert (obj != loc);
12728
12729         list_ret = loc_list_from_tree (obj,
12730                                        want_address == 2
12731                                        && !bitpos && !offset ? 2 : 1);
12732         /* TODO: We can extract value of the small expression via shifting even
12733            for nonzero bitpos.  */
12734         if (list_ret == 0)
12735           return 0;
12736         if (bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
12737           {
12738             expansion_failed (loc, NULL_RTX,
12739                               "bitfield access");
12740             return 0;
12741           }
12742
12743         if (offset != NULL_TREE)
12744           {
12745             /* Variable offset.  */
12746             list_ret1 = loc_list_from_tree (offset, 0);
12747             if (list_ret1 == 0)
12748               return 0;
12749             add_loc_list (&list_ret, list_ret1);
12750             if (!list_ret)
12751               return 0;
12752             add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus, 0, 0));
12753           }
12754
12755         bytepos = bitpos / BITS_PER_UNIT;
12756         if (bytepos > 0)
12757           add_loc_descr_to_each (list_ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
12758         else if (bytepos < 0)
12759           loc_list_plus_const (list_ret, bytepos); 
12760
12761         have_address = 1;
12762         break;
12763       }
12764
12765     case INTEGER_CST:
12766       if ((want_address || !host_integerp (loc, 0))
12767           && (ret = cst_pool_loc_descr (loc)))
12768         have_address = 1;
12769       else if (want_address == 2
12770                && host_integerp (loc, 0)
12771                && (ret = address_of_int_loc_descriptor
12772                            (int_size_in_bytes (TREE_TYPE (loc)),
12773                             tree_low_cst (loc, 0))))
12774         have_address = 1;
12775       else if (host_integerp (loc, 0))
12776         ret = int_loc_descriptor (tree_low_cst (loc, 0));
12777       else
12778         {
12779           expansion_failed (loc, NULL_RTX,
12780                             "Integer operand is not host integer");
12781           return 0;
12782         }
12783       break;
12784
12785     case CONSTRUCTOR:
12786     case REAL_CST:
12787     case STRING_CST:
12788     case COMPLEX_CST:
12789       if ((ret = cst_pool_loc_descr (loc)))
12790         have_address = 1;
12791       else
12792       /* We can construct small constants here using int_loc_descriptor.  */
12793         expansion_failed (loc, NULL_RTX,
12794                           "constructor or constant not in constant pool");
12795       break;
12796
12797     case TRUTH_AND_EXPR:
12798     case TRUTH_ANDIF_EXPR:
12799     case BIT_AND_EXPR:
12800       op = DW_OP_and;
12801       goto do_binop;
12802
12803     case TRUTH_XOR_EXPR:
12804     case BIT_XOR_EXPR:
12805       op = DW_OP_xor;
12806       goto do_binop;
12807
12808     case TRUTH_OR_EXPR:
12809     case TRUTH_ORIF_EXPR:
12810     case BIT_IOR_EXPR:
12811       op = DW_OP_or;
12812       goto do_binop;
12813
12814     case FLOOR_DIV_EXPR:
12815     case CEIL_DIV_EXPR:
12816     case ROUND_DIV_EXPR:
12817     case TRUNC_DIV_EXPR:
12818       op = DW_OP_div;
12819       goto do_binop;
12820
12821     case MINUS_EXPR:
12822       op = DW_OP_minus;
12823       goto do_binop;
12824
12825     case FLOOR_MOD_EXPR:
12826     case CEIL_MOD_EXPR:
12827     case ROUND_MOD_EXPR:
12828     case TRUNC_MOD_EXPR:
12829       op = DW_OP_mod;
12830       goto do_binop;
12831
12832     case MULT_EXPR:
12833       op = DW_OP_mul;
12834       goto do_binop;
12835
12836     case LSHIFT_EXPR:
12837       op = DW_OP_shl;
12838       goto do_binop;
12839
12840     case RSHIFT_EXPR:
12841       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
12842       goto do_binop;
12843
12844     case POINTER_PLUS_EXPR:
12845     case PLUS_EXPR:
12846       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
12847           && host_integerp (TREE_OPERAND (loc, 1), 0))
12848         {
12849           list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12850           if (list_ret == 0)
12851             return 0;
12852
12853           loc_list_plus_const (list_ret, tree_low_cst (TREE_OPERAND (loc, 1), 0));
12854           break;
12855         }
12856
12857       op = DW_OP_plus;
12858       goto do_binop;
12859
12860     case LE_EXPR:
12861       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12862         return 0;
12863
12864       op = DW_OP_le;
12865       goto do_binop;
12866
12867     case GE_EXPR:
12868       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12869         return 0;
12870
12871       op = DW_OP_ge;
12872       goto do_binop;
12873
12874     case LT_EXPR:
12875       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12876         return 0;
12877
12878       op = DW_OP_lt;
12879       goto do_binop;
12880
12881     case GT_EXPR:
12882       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
12883         return 0;
12884
12885       op = DW_OP_gt;
12886       goto do_binop;
12887
12888     case EQ_EXPR:
12889       op = DW_OP_eq;
12890       goto do_binop;
12891
12892     case NE_EXPR:
12893       op = DW_OP_ne;
12894       goto do_binop;
12895
12896     do_binop:
12897       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12898       list_ret1 = loc_list_from_tree (TREE_OPERAND (loc, 1), 0);
12899       if (list_ret == 0 || list_ret1 == 0)
12900         return 0;
12901
12902       add_loc_list (&list_ret, list_ret1);
12903       if (list_ret == 0)
12904         return 0;
12905       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
12906       break;
12907
12908     case TRUTH_NOT_EXPR:
12909     case BIT_NOT_EXPR:
12910       op = DW_OP_not;
12911       goto do_unop;
12912
12913     case ABS_EXPR:
12914       op = DW_OP_abs;
12915       goto do_unop;
12916
12917     case NEGATE_EXPR:
12918       op = DW_OP_neg;
12919       goto do_unop;
12920
12921     do_unop:
12922       list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12923       if (list_ret == 0)
12924         return 0;
12925
12926       add_loc_descr_to_each (list_ret, new_loc_descr (op, 0, 0));
12927       break;
12928
12929     case MIN_EXPR:
12930     case MAX_EXPR:
12931       {
12932         const enum tree_code code =
12933           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
12934
12935         loc = build3 (COND_EXPR, TREE_TYPE (loc),
12936                       build2 (code, integer_type_node,
12937                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
12938                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
12939       }
12940
12941       /* ... fall through ...  */
12942
12943     case COND_EXPR:
12944       {
12945         dw_loc_descr_ref lhs
12946           = loc_descriptor_from_tree (TREE_OPERAND (loc, 1), 0);
12947         dw_loc_list_ref rhs
12948           = loc_list_from_tree (TREE_OPERAND (loc, 2), 0);
12949         dw_loc_descr_ref bra_node, jump_node, tmp;
12950
12951         list_ret = loc_list_from_tree (TREE_OPERAND (loc, 0), 0);
12952         if (list_ret == 0 || lhs == 0 || rhs == 0)
12953           return 0;
12954
12955         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
12956         add_loc_descr_to_each (list_ret, bra_node);
12957
12958         add_loc_list (&list_ret, rhs);
12959         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
12960         add_loc_descr_to_each (list_ret, jump_node);
12961
12962         add_loc_descr_to_each (list_ret, lhs);
12963         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12964         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
12965
12966         /* ??? Need a node to point the skip at.  Use a nop.  */
12967         tmp = new_loc_descr (DW_OP_nop, 0, 0);
12968         add_loc_descr_to_each (list_ret, tmp);
12969         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
12970         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
12971       }
12972       break;
12973
12974     case FIX_TRUNC_EXPR:
12975       return 0;
12976
12977     default:
12978       /* Leave front-end specific codes as simply unknown.  This comes
12979          up, for instance, with the C STMT_EXPR.  */
12980       if ((unsigned int) TREE_CODE (loc)
12981           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
12982         {
12983           expansion_failed (loc, NULL_RTX,
12984                             "language specific tree node");
12985           return 0;
12986         }
12987
12988 #ifdef ENABLE_CHECKING
12989       /* Otherwise this is a generic code; we should just lists all of
12990          these explicitly.  We forgot one.  */
12991       gcc_unreachable ();
12992 #else
12993       /* In a release build, we want to degrade gracefully: better to
12994          generate incomplete debugging information than to crash.  */
12995       return NULL;
12996 #endif
12997     }
12998
12999   if (!ret && !list_ret)
13000     return 0;
13001
13002   if (want_address == 2 && !have_address
13003       && (dwarf_version >= 4 || !dwarf_strict))
13004     {
13005       if (int_size_in_bytes (TREE_TYPE (loc)) > DWARF2_ADDR_SIZE)
13006         {
13007           expansion_failed (loc, NULL_RTX,
13008                             "DWARF address size mismatch");
13009           return 0;
13010         }
13011       if (ret)
13012         add_loc_descr (&ret, new_loc_descr (DW_OP_stack_value, 0, 0));
13013       else
13014         add_loc_descr_to_each (list_ret,
13015                                new_loc_descr (DW_OP_stack_value, 0, 0));
13016       have_address = 1;
13017     }
13018   /* Show if we can't fill the request for an address.  */
13019   if (want_address && !have_address)
13020     {
13021       expansion_failed (loc, NULL_RTX,
13022                         "Want address and only have value");
13023       return 0;
13024     }
13025
13026   gcc_assert (!ret || !list_ret);
13027
13028   /* If we've got an address and don't want one, dereference.  */
13029   if (!want_address && have_address)
13030     {
13031       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
13032
13033       if (size > DWARF2_ADDR_SIZE || size == -1)
13034         {
13035           expansion_failed (loc, NULL_RTX,
13036                             "DWARF address size mismatch");
13037           return 0;
13038         }
13039       else if (size == DWARF2_ADDR_SIZE)
13040         op = DW_OP_deref;
13041       else
13042         op = DW_OP_deref_size;
13043
13044       if (ret)
13045         add_loc_descr (&ret, new_loc_descr (op, size, 0));
13046       else
13047         add_loc_descr_to_each (list_ret, new_loc_descr (op, size, 0));
13048     }
13049   if (ret)
13050     list_ret = single_element_loc_list (ret);
13051
13052   return list_ret;
13053 }
13054
13055 /* Same as above but return only single location expression.  */
13056 static dw_loc_descr_ref
13057 loc_descriptor_from_tree (tree loc, int want_address)
13058 {
13059   dw_loc_list_ref ret = loc_list_from_tree (loc, want_address);
13060   if (!ret)
13061     return NULL;
13062   if (ret->dw_loc_next)
13063     {
13064       expansion_failed (loc, NULL_RTX,
13065                         "Location list where only loc descriptor needed");
13066       return NULL;
13067     }
13068   return ret->expr;
13069 }
13070
13071 /* Given a value, round it up to the lowest multiple of `boundary'
13072    which is not less than the value itself.  */
13073
13074 static inline HOST_WIDE_INT
13075 ceiling (HOST_WIDE_INT value, unsigned int boundary)
13076 {
13077   return (((value + boundary - 1) / boundary) * boundary);
13078 }
13079
13080 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
13081    pointer to the declared type for the relevant field variable, or return
13082    `integer_type_node' if the given node turns out to be an
13083    ERROR_MARK node.  */
13084
13085 static inline tree
13086 field_type (const_tree decl)
13087 {
13088   tree type;
13089
13090   if (TREE_CODE (decl) == ERROR_MARK)
13091     return integer_type_node;
13092
13093   type = DECL_BIT_FIELD_TYPE (decl);
13094   if (type == NULL_TREE)
13095     type = TREE_TYPE (decl);
13096
13097   return type;
13098 }
13099
13100 /* Given a pointer to a tree node, return the alignment in bits for
13101    it, or else return BITS_PER_WORD if the node actually turns out to
13102    be an ERROR_MARK node.  */
13103
13104 static inline unsigned
13105 simple_type_align_in_bits (const_tree type)
13106 {
13107   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
13108 }
13109
13110 static inline unsigned
13111 simple_decl_align_in_bits (const_tree decl)
13112 {
13113   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
13114 }
13115
13116 /* Return the result of rounding T up to ALIGN.  */
13117
13118 static inline HOST_WIDE_INT
13119 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
13120 {
13121   /* We must be careful if T is negative because HOST_WIDE_INT can be
13122      either "above" or "below" unsigned int as per the C promotion
13123      rules, depending on the host, thus making the signedness of the
13124      direct multiplication and division unpredictable.  */
13125   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
13126
13127   u += align - 1;
13128   u /= align;
13129   u *= align;
13130
13131   return (HOST_WIDE_INT) u;
13132 }
13133
13134 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
13135    lowest addressed byte of the "containing object" for the given FIELD_DECL,
13136    or return 0 if we are unable to determine what that offset is, either
13137    because the argument turns out to be a pointer to an ERROR_MARK node, or
13138    because the offset is actually variable.  (We can't handle the latter case
13139    just yet).  */
13140
13141 static HOST_WIDE_INT
13142 field_byte_offset (const_tree decl)
13143 {
13144   HOST_WIDE_INT object_offset_in_bits;
13145   HOST_WIDE_INT bitpos_int;
13146
13147   if (TREE_CODE (decl) == ERROR_MARK)
13148     return 0;
13149
13150   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
13151
13152   /* We cannot yet cope with fields whose positions are variable, so
13153      for now, when we see such things, we simply return 0.  Someday, we may
13154      be able to handle such cases, but it will be damn difficult.  */
13155   if (! host_integerp (bit_position (decl), 0))
13156     return 0;
13157
13158   bitpos_int = int_bit_position (decl);
13159
13160 #ifdef PCC_BITFIELD_TYPE_MATTERS
13161   if (PCC_BITFIELD_TYPE_MATTERS)
13162     {
13163       tree type;
13164       tree field_size_tree;
13165       HOST_WIDE_INT deepest_bitpos;
13166       unsigned HOST_WIDE_INT field_size_in_bits;
13167       unsigned int type_align_in_bits;
13168       unsigned int decl_align_in_bits;
13169       unsigned HOST_WIDE_INT type_size_in_bits;
13170
13171       type = field_type (decl);
13172       type_size_in_bits = simple_type_size_in_bits (type);
13173       type_align_in_bits = simple_type_align_in_bits (type);
13174
13175       field_size_tree = DECL_SIZE (decl);
13176
13177       /* The size could be unspecified if there was an error, or for
13178          a flexible array member.  */
13179       if (!field_size_tree)
13180         field_size_tree = bitsize_zero_node;
13181
13182       /* If the size of the field is not constant, use the type size.  */
13183       if (host_integerp (field_size_tree, 1))
13184         field_size_in_bits = tree_low_cst (field_size_tree, 1);
13185       else
13186         field_size_in_bits = type_size_in_bits;
13187
13188       decl_align_in_bits = simple_decl_align_in_bits (decl);
13189
13190       /* The GCC front-end doesn't make any attempt to keep track of the
13191          starting bit offset (relative to the start of the containing
13192          structure type) of the hypothetical "containing object" for a
13193          bit-field.  Thus, when computing the byte offset value for the
13194          start of the "containing object" of a bit-field, we must deduce
13195          this information on our own. This can be rather tricky to do in
13196          some cases.  For example, handling the following structure type
13197          definition when compiling for an i386/i486 target (which only
13198          aligns long long's to 32-bit boundaries) can be very tricky:
13199
13200          struct S { int field1; long long field2:31; };
13201
13202          Fortunately, there is a simple rule-of-thumb which can be used
13203          in such cases.  When compiling for an i386/i486, GCC will
13204          allocate 8 bytes for the structure shown above.  It decides to
13205          do this based upon one simple rule for bit-field allocation.
13206          GCC allocates each "containing object" for each bit-field at
13207          the first (i.e. lowest addressed) legitimate alignment boundary
13208          (based upon the required minimum alignment for the declared
13209          type of the field) which it can possibly use, subject to the
13210          condition that there is still enough available space remaining
13211          in the containing object (when allocated at the selected point)
13212          to fully accommodate all of the bits of the bit-field itself.
13213
13214          This simple rule makes it obvious why GCC allocates 8 bytes for
13215          each object of the structure type shown above.  When looking
13216          for a place to allocate the "containing object" for `field2',
13217          the compiler simply tries to allocate a 64-bit "containing
13218          object" at each successive 32-bit boundary (starting at zero)
13219          until it finds a place to allocate that 64- bit field such that
13220          at least 31 contiguous (and previously unallocated) bits remain
13221          within that selected 64 bit field.  (As it turns out, for the
13222          example above, the compiler finds it is OK to allocate the
13223          "containing object" 64-bit field at bit-offset zero within the
13224          structure type.)
13225
13226          Here we attempt to work backwards from the limited set of facts
13227          we're given, and we try to deduce from those facts, where GCC
13228          must have believed that the containing object started (within
13229          the structure type). The value we deduce is then used (by the
13230          callers of this routine) to generate DW_AT_location and
13231          DW_AT_bit_offset attributes for fields (both bit-fields and, in
13232          the case of DW_AT_location, regular fields as well).  */
13233
13234       /* Figure out the bit-distance from the start of the structure to
13235          the "deepest" bit of the bit-field.  */
13236       deepest_bitpos = bitpos_int + field_size_in_bits;
13237
13238       /* This is the tricky part.  Use some fancy footwork to deduce
13239          where the lowest addressed bit of the containing object must
13240          be.  */
13241       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13242
13243       /* Round up to type_align by default.  This works best for
13244          bitfields.  */
13245       object_offset_in_bits
13246         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
13247
13248       if (object_offset_in_bits > bitpos_int)
13249         {
13250           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
13251
13252           /* Round up to decl_align instead.  */
13253           object_offset_in_bits
13254             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
13255         }
13256     }
13257   else
13258 #endif
13259     object_offset_in_bits = bitpos_int;
13260
13261   return object_offset_in_bits / BITS_PER_UNIT;
13262 }
13263 \f
13264 /* The following routines define various Dwarf attributes and any data
13265    associated with them.  */
13266
13267 /* Add a location description attribute value to a DIE.
13268
13269    This emits location attributes suitable for whole variables and
13270    whole parameters.  Note that the location attributes for struct fields are
13271    generated by the routine `data_member_location_attribute' below.  */
13272
13273 static inline void
13274 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
13275                              dw_loc_list_ref descr)
13276 {
13277   if (descr == 0)
13278     return;
13279   if (single_element_loc_list_p (descr))
13280     add_AT_loc (die, attr_kind, descr->expr);
13281   else
13282     add_AT_loc_list (die, attr_kind, descr);
13283 }
13284
13285 /* Attach the specialized form of location attribute used for data members of
13286    struct and union types.  In the special case of a FIELD_DECL node which
13287    represents a bit-field, the "offset" part of this special location
13288    descriptor must indicate the distance in bytes from the lowest-addressed
13289    byte of the containing struct or union type to the lowest-addressed byte of
13290    the "containing object" for the bit-field.  (See the `field_byte_offset'
13291    function above).
13292
13293    For any given bit-field, the "containing object" is a hypothetical object
13294    (of some integral or enum type) within which the given bit-field lives.  The
13295    type of this hypothetical "containing object" is always the same as the
13296    declared type of the individual bit-field itself (for GCC anyway... the
13297    DWARF spec doesn't actually mandate this).  Note that it is the size (in
13298    bytes) of the hypothetical "containing object" which will be given in the
13299    DW_AT_byte_size attribute for this bit-field.  (See the
13300    `byte_size_attribute' function below.)  It is also used when calculating the
13301    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
13302    function below.)  */
13303
13304 static void
13305 add_data_member_location_attribute (dw_die_ref die, tree decl)
13306 {
13307   HOST_WIDE_INT offset;
13308   dw_loc_descr_ref loc_descr = 0;
13309
13310   if (TREE_CODE (decl) == TREE_BINFO)
13311     {
13312       /* We're working on the TAG_inheritance for a base class.  */
13313       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
13314         {
13315           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
13316              aren't at a fixed offset from all (sub)objects of the same
13317              type.  We need to extract the appropriate offset from our
13318              vtable.  The following dwarf expression means
13319
13320                BaseAddr = ObAddr + *((*ObAddr) - Offset)
13321
13322              This is specific to the V3 ABI, of course.  */
13323
13324           dw_loc_descr_ref tmp;
13325
13326           /* Make a copy of the object address.  */
13327           tmp = new_loc_descr (DW_OP_dup, 0, 0);
13328           add_loc_descr (&loc_descr, tmp);
13329
13330           /* Extract the vtable address.  */
13331           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13332           add_loc_descr (&loc_descr, tmp);
13333
13334           /* Calculate the address of the offset.  */
13335           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
13336           gcc_assert (offset < 0);
13337
13338           tmp = int_loc_descriptor (-offset);
13339           add_loc_descr (&loc_descr, tmp);
13340           tmp = new_loc_descr (DW_OP_minus, 0, 0);
13341           add_loc_descr (&loc_descr, tmp);
13342
13343           /* Extract the offset.  */
13344           tmp = new_loc_descr (DW_OP_deref, 0, 0);
13345           add_loc_descr (&loc_descr, tmp);
13346
13347           /* Add it to the object address.  */
13348           tmp = new_loc_descr (DW_OP_plus, 0, 0);
13349           add_loc_descr (&loc_descr, tmp);
13350         }
13351       else
13352         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
13353     }
13354   else
13355     offset = field_byte_offset (decl);
13356
13357   if (! loc_descr)
13358     {
13359       if (dwarf_version > 2)
13360         {
13361           /* Don't need to output a location expression, just the constant. */
13362           add_AT_int (die, DW_AT_data_member_location, offset);
13363           return;
13364         }
13365       else
13366         {
13367           enum dwarf_location_atom op;
13368           
13369           /* The DWARF2 standard says that we should assume that the structure
13370              address is already on the stack, so we can specify a structure
13371              field address by using DW_OP_plus_uconst.  */
13372           
13373 #ifdef MIPS_DEBUGGING_INFO
13374           /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
13375              operator correctly.  It works only if we leave the offset on the
13376              stack.  */
13377           op = DW_OP_constu;
13378 #else
13379           op = DW_OP_plus_uconst;
13380 #endif
13381           
13382           loc_descr = new_loc_descr (op, offset, 0);
13383         }
13384     }
13385
13386   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
13387 }
13388
13389 /* Writes integer values to dw_vec_const array.  */
13390
13391 static void
13392 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
13393 {
13394   while (size != 0)
13395     {
13396       *dest++ = val & 0xff;
13397       val >>= 8;
13398       --size;
13399     }
13400 }
13401
13402 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
13403
13404 static HOST_WIDE_INT
13405 extract_int (const unsigned char *src, unsigned int size)
13406 {
13407   HOST_WIDE_INT val = 0;
13408
13409   src += size;
13410   while (size != 0)
13411     {
13412       val <<= 8;
13413       val |= *--src & 0xff;
13414       --size;
13415     }
13416   return val;
13417 }
13418
13419 /* Writes floating point values to dw_vec_const array.  */
13420
13421 static void
13422 insert_float (const_rtx rtl, unsigned char *array)
13423 {
13424   REAL_VALUE_TYPE rv;
13425   long val[4];
13426   int i;
13427
13428   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
13429   real_to_target (val, &rv, GET_MODE (rtl));
13430
13431   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
13432   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
13433     {
13434       insert_int (val[i], 4, array);
13435       array += 4;
13436     }
13437 }
13438
13439 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
13440    does not have a "location" either in memory or in a register.  These
13441    things can arise in GNU C when a constant is passed as an actual parameter
13442    to an inlined function.  They can also arise in C++ where declared
13443    constants do not necessarily get memory "homes".  */
13444
13445 static bool
13446 add_const_value_attribute (dw_die_ref die, rtx rtl)
13447 {
13448   switch (GET_CODE (rtl))
13449     {
13450     case CONST_INT:
13451       {
13452         HOST_WIDE_INT val = INTVAL (rtl);
13453
13454         if (val < 0)
13455           add_AT_int (die, DW_AT_const_value, val);
13456         else
13457           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
13458       }
13459       return true;
13460
13461     case CONST_DOUBLE:
13462       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
13463          floating-point constant.  A CONST_DOUBLE is used whenever the
13464          constant requires more than one word in order to be adequately
13465          represented.  */
13466       {
13467         enum machine_mode mode = GET_MODE (rtl);
13468
13469         if (SCALAR_FLOAT_MODE_P (mode))
13470           {
13471             unsigned int length = GET_MODE_SIZE (mode);
13472             unsigned char *array = GGC_NEWVEC (unsigned char, length);
13473
13474             insert_float (rtl, array);
13475             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
13476           }
13477         else
13478           add_AT_double (die, DW_AT_const_value,
13479                          CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
13480       }
13481       return true;
13482
13483     case CONST_VECTOR:
13484       {
13485         enum machine_mode mode = GET_MODE (rtl);
13486         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
13487         unsigned int length = CONST_VECTOR_NUNITS (rtl);
13488         unsigned char *array = GGC_NEWVEC (unsigned char, length * elt_size);
13489         unsigned int i;
13490         unsigned char *p;
13491
13492         switch (GET_MODE_CLASS (mode))
13493           {
13494           case MODE_VECTOR_INT:
13495             for (i = 0, p = array; i < length; i++, p += elt_size)
13496               {
13497                 rtx elt = CONST_VECTOR_ELT (rtl, i);
13498                 HOST_WIDE_INT lo, hi;
13499
13500                 switch (GET_CODE (elt))
13501                   {
13502                   case CONST_INT:
13503                     lo = INTVAL (elt);
13504                     hi = -(lo < 0);
13505                     break;
13506
13507                   case CONST_DOUBLE:
13508                     lo = CONST_DOUBLE_LOW (elt);
13509                     hi = CONST_DOUBLE_HIGH (elt);
13510                     break;
13511
13512                   default:
13513                     gcc_unreachable ();
13514                   }
13515
13516                 if (elt_size <= sizeof (HOST_WIDE_INT))
13517                   insert_int (lo, elt_size, p);
13518                 else
13519                   {
13520                     unsigned char *p0 = p;
13521                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
13522
13523                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
13524                     if (WORDS_BIG_ENDIAN)
13525                       {
13526                         p0 = p1;
13527                         p1 = p;
13528                       }
13529                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
13530                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
13531                   }
13532               }
13533             break;
13534
13535           case MODE_VECTOR_FLOAT:
13536             for (i = 0, p = array; i < length; i++, p += elt_size)
13537               {
13538                 rtx elt = CONST_VECTOR_ELT (rtl, i);
13539                 insert_float (elt, p);
13540               }
13541             break;
13542
13543           default:
13544             gcc_unreachable ();
13545           }
13546
13547         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
13548       }
13549       return true;
13550
13551     case CONST_STRING:
13552       resolve_one_addr (&rtl, NULL);
13553       add_AT_addr (die, DW_AT_const_value, rtl);
13554       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13555       return true;
13556
13557     case CONST:
13558       if (CONSTANT_P (XEXP (rtl, 0)))
13559         return add_const_value_attribute (die, XEXP (rtl, 0));
13560       /* FALLTHROUGH */
13561     case SYMBOL_REF:
13562       if (!const_ok_for_output (rtl))
13563         return false;
13564     case LABEL_REF:
13565       add_AT_addr (die, DW_AT_const_value, rtl);
13566       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
13567       return true;
13568
13569     case PLUS:
13570       /* In cases where an inlined instance of an inline function is passed
13571          the address of an `auto' variable (which is local to the caller) we
13572          can get a situation where the DECL_RTL of the artificial local
13573          variable (for the inlining) which acts as a stand-in for the
13574          corresponding formal parameter (of the inline function) will look
13575          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
13576          exactly a compile-time constant expression, but it isn't the address
13577          of the (artificial) local variable either.  Rather, it represents the
13578          *value* which the artificial local variable always has during its
13579          lifetime.  We currently have no way to represent such quasi-constant
13580          values in Dwarf, so for now we just punt and generate nothing.  */
13581       return false;
13582
13583     case HIGH:
13584     case CONST_FIXED:
13585       return false;
13586
13587     case MEM:
13588       if (GET_CODE (XEXP (rtl, 0)) == CONST_STRING
13589           && MEM_READONLY_P (rtl)
13590           && GET_MODE (rtl) == BLKmode)
13591         {
13592           add_AT_string (die, DW_AT_const_value, XSTR (XEXP (rtl, 0), 0));
13593           return true;
13594         }
13595       return false;
13596
13597     default:
13598       /* No other kinds of rtx should be possible here.  */
13599       gcc_unreachable ();
13600     }
13601   return false;
13602 }
13603
13604 /* Determine whether the evaluation of EXPR references any variables
13605    or functions which aren't otherwise used (and therefore may not be
13606    output).  */
13607 static tree
13608 reference_to_unused (tree * tp, int * walk_subtrees,
13609                      void * data ATTRIBUTE_UNUSED)
13610 {
13611   if (! EXPR_P (*tp) && ! CONSTANT_CLASS_P (*tp))
13612     *walk_subtrees = 0;
13613
13614   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
13615       && ! TREE_ASM_WRITTEN (*tp))
13616     return *tp;
13617   /* ???  The C++ FE emits debug information for using decls, so
13618      putting gcc_unreachable here falls over.  See PR31899.  For now
13619      be conservative.  */
13620   else if (!cgraph_global_info_ready
13621            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
13622     return *tp;
13623   else if (TREE_CODE (*tp) == VAR_DECL)
13624     {
13625       struct varpool_node *node = varpool_node (*tp);
13626       if (!node->needed)
13627         return *tp;
13628     }
13629   else if (TREE_CODE (*tp) == FUNCTION_DECL
13630            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
13631     {
13632       /* The call graph machinery must have finished analyzing,
13633          optimizing and gimplifying the CU by now.
13634          So if *TP has no call graph node associated
13635          to it, it means *TP will not be emitted.  */
13636       if (!cgraph_get_node (*tp))
13637         return *tp;
13638     }
13639   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
13640     return *tp;
13641
13642   return NULL_TREE;
13643 }
13644
13645 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
13646    for use in a later add_const_value_attribute call.  */
13647
13648 static rtx
13649 rtl_for_decl_init (tree init, tree type)
13650 {
13651   rtx rtl = NULL_RTX;
13652
13653   /* If a variable is initialized with a string constant without embedded
13654      zeros, build CONST_STRING.  */
13655   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
13656     {
13657       tree enttype = TREE_TYPE (type);
13658       tree domain = TYPE_DOMAIN (type);
13659       enum machine_mode mode = TYPE_MODE (enttype);
13660
13661       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
13662           && domain
13663           && integer_zerop (TYPE_MIN_VALUE (domain))
13664           && compare_tree_int (TYPE_MAX_VALUE (domain),
13665                                TREE_STRING_LENGTH (init) - 1) == 0
13666           && ((size_t) TREE_STRING_LENGTH (init)
13667               == strlen (TREE_STRING_POINTER (init)) + 1))
13668         {
13669           rtl = gen_rtx_CONST_STRING (VOIDmode,
13670                                       ggc_strdup (TREE_STRING_POINTER (init)));
13671           rtl = gen_rtx_MEM (BLKmode, rtl);
13672           MEM_READONLY_P (rtl) = 1;
13673         }
13674     }
13675   /* Other aggregates, and complex values, could be represented using
13676      CONCAT: FIXME!  */
13677   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
13678     ;
13679   /* Vectors only work if their mode is supported by the target.
13680      FIXME: generic vectors ought to work too.  */
13681   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
13682     ;
13683   /* If the initializer is something that we know will expand into an
13684      immediate RTL constant, expand it now.  We must be careful not to
13685      reference variables which won't be output.  */
13686   else if (initializer_constant_valid_p (init, type)
13687            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
13688     {
13689       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
13690          possible.  */
13691       if (TREE_CODE (type) == VECTOR_TYPE)
13692         switch (TREE_CODE (init))
13693           {
13694           case VECTOR_CST:
13695             break;
13696           case CONSTRUCTOR:
13697             if (TREE_CONSTANT (init))
13698               {
13699                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
13700                 bool constant_p = true;
13701                 tree value;
13702                 unsigned HOST_WIDE_INT ix;
13703
13704                 /* Even when ctor is constant, it might contain non-*_CST
13705                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
13706                    belong into VECTOR_CST nodes.  */
13707                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
13708                   if (!CONSTANT_CLASS_P (value))
13709                     {
13710                       constant_p = false;
13711                       break;
13712                     }
13713
13714                 if (constant_p)
13715                   {
13716                     init = build_vector_from_ctor (type, elts);
13717                     break;
13718                   }
13719               }
13720             /* FALLTHRU */
13721
13722           default:
13723             return NULL;
13724           }
13725
13726       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
13727
13728       /* If expand_expr returns a MEM, it wasn't immediate.  */
13729       gcc_assert (!rtl || !MEM_P (rtl));
13730     }
13731
13732   return rtl;
13733 }
13734
13735 /* Generate RTL for the variable DECL to represent its location.  */
13736
13737 static rtx
13738 rtl_for_decl_location (tree decl)
13739 {
13740   rtx rtl;
13741
13742   /* Here we have to decide where we are going to say the parameter "lives"
13743      (as far as the debugger is concerned).  We only have a couple of
13744      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
13745
13746      DECL_RTL normally indicates where the parameter lives during most of the
13747      activation of the function.  If optimization is enabled however, this
13748      could be either NULL or else a pseudo-reg.  Both of those cases indicate
13749      that the parameter doesn't really live anywhere (as far as the code
13750      generation parts of GCC are concerned) during most of the function's
13751      activation.  That will happen (for example) if the parameter is never
13752      referenced within the function.
13753
13754      We could just generate a location descriptor here for all non-NULL
13755      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
13756      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
13757      where DECL_RTL is NULL or is a pseudo-reg.
13758
13759      Note however that we can only get away with using DECL_INCOMING_RTL as
13760      a backup substitute for DECL_RTL in certain limited cases.  In cases
13761      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
13762      we can be sure that the parameter was passed using the same type as it is
13763      declared to have within the function, and that its DECL_INCOMING_RTL
13764      points us to a place where a value of that type is passed.
13765
13766      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
13767      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
13768      because in these cases DECL_INCOMING_RTL points us to a value of some
13769      type which is *different* from the type of the parameter itself.  Thus,
13770      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
13771      such cases, the debugger would end up (for example) trying to fetch a
13772      `float' from a place which actually contains the first part of a
13773      `double'.  That would lead to really incorrect and confusing
13774      output at debug-time.
13775
13776      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
13777      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
13778      are a couple of exceptions however.  On little-endian machines we can
13779      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
13780      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
13781      an integral type that is smaller than TREE_TYPE (decl). These cases arise
13782      when (on a little-endian machine) a non-prototyped function has a
13783      parameter declared to be of type `short' or `char'.  In such cases,
13784      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
13785      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
13786      passed `int' value.  If the debugger then uses that address to fetch
13787      a `short' or a `char' (on a little-endian machine) the result will be
13788      the correct data, so we allow for such exceptional cases below.
13789
13790      Note that our goal here is to describe the place where the given formal
13791      parameter lives during most of the function's activation (i.e. between the
13792      end of the prologue and the start of the epilogue).  We'll do that as best
13793      as we can. Note however that if the given formal parameter is modified
13794      sometime during the execution of the function, then a stack backtrace (at
13795      debug-time) will show the function as having been called with the *new*
13796      value rather than the value which was originally passed in.  This happens
13797      rarely enough that it is not a major problem, but it *is* a problem, and
13798      I'd like to fix it.
13799
13800      A future version of dwarf2out.c may generate two additional attributes for
13801      any given DW_TAG_formal_parameter DIE which will describe the "passed
13802      type" and the "passed location" for the given formal parameter in addition
13803      to the attributes we now generate to indicate the "declared type" and the
13804      "active location" for each parameter.  This additional set of attributes
13805      could be used by debuggers for stack backtraces. Separately, note that
13806      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
13807      This happens (for example) for inlined-instances of inline function formal
13808      parameters which are never referenced.  This really shouldn't be
13809      happening.  All PARM_DECL nodes should get valid non-NULL
13810      DECL_INCOMING_RTL values.  FIXME.  */
13811
13812   /* Use DECL_RTL as the "location" unless we find something better.  */
13813   rtl = DECL_RTL_IF_SET (decl);
13814
13815   /* When generating abstract instances, ignore everything except
13816      constants, symbols living in memory, and symbols living in
13817      fixed registers.  */
13818   if (! reload_completed)
13819     {
13820       if (rtl
13821           && (CONSTANT_P (rtl)
13822               || (MEM_P (rtl)
13823                   && CONSTANT_P (XEXP (rtl, 0)))
13824               || (REG_P (rtl)
13825                   && TREE_CODE (decl) == VAR_DECL
13826                   && TREE_STATIC (decl))))
13827         {
13828           rtl = targetm.delegitimize_address (rtl);
13829           return rtl;
13830         }
13831       rtl = NULL_RTX;
13832     }
13833   else if (TREE_CODE (decl) == PARM_DECL)
13834     {
13835       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
13836         {
13837           tree declared_type = TREE_TYPE (decl);
13838           tree passed_type = DECL_ARG_TYPE (decl);
13839           enum machine_mode dmode = TYPE_MODE (declared_type);
13840           enum machine_mode pmode = TYPE_MODE (passed_type);
13841
13842           /* This decl represents a formal parameter which was optimized out.
13843              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
13844              all cases where (rtl == NULL_RTX) just below.  */
13845           if (dmode == pmode)
13846             rtl = DECL_INCOMING_RTL (decl);
13847           else if (SCALAR_INT_MODE_P (dmode)
13848                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
13849                    && DECL_INCOMING_RTL (decl))
13850             {
13851               rtx inc = DECL_INCOMING_RTL (decl);
13852               if (REG_P (inc))
13853                 rtl = inc;
13854               else if (MEM_P (inc))
13855                 {
13856                   if (BYTES_BIG_ENDIAN)
13857                     rtl = adjust_address_nv (inc, dmode,
13858                                              GET_MODE_SIZE (pmode)
13859                                              - GET_MODE_SIZE (dmode));
13860                   else
13861                     rtl = inc;
13862                 }
13863             }
13864         }
13865
13866       /* If the parm was passed in registers, but lives on the stack, then
13867          make a big endian correction if the mode of the type of the
13868          parameter is not the same as the mode of the rtl.  */
13869       /* ??? This is the same series of checks that are made in dbxout.c before
13870          we reach the big endian correction code there.  It isn't clear if all
13871          of these checks are necessary here, but keeping them all is the safe
13872          thing to do.  */
13873       else if (MEM_P (rtl)
13874                && XEXP (rtl, 0) != const0_rtx
13875                && ! CONSTANT_P (XEXP (rtl, 0))
13876                /* Not passed in memory.  */
13877                && !MEM_P (DECL_INCOMING_RTL (decl))
13878                /* Not passed by invisible reference.  */
13879                && (!REG_P (XEXP (rtl, 0))
13880                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
13881                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
13882 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
13883                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
13884 #endif
13885                      )
13886                /* Big endian correction check.  */
13887                && BYTES_BIG_ENDIAN
13888                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
13889                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
13890                    < UNITS_PER_WORD))
13891         {
13892           int offset = (UNITS_PER_WORD
13893                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
13894
13895           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13896                              plus_constant (XEXP (rtl, 0), offset));
13897         }
13898     }
13899   else if (TREE_CODE (decl) == VAR_DECL
13900            && rtl
13901            && MEM_P (rtl)
13902            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
13903            && BYTES_BIG_ENDIAN)
13904     {
13905       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
13906       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
13907
13908       /* If a variable is declared "register" yet is smaller than
13909          a register, then if we store the variable to memory, it
13910          looks like we're storing a register-sized value, when in
13911          fact we are not.  We need to adjust the offset of the
13912          storage location to reflect the actual value's bytes,
13913          else gdb will not be able to display it.  */
13914       if (rsize > dsize)
13915         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
13916                            plus_constant (XEXP (rtl, 0), rsize-dsize));
13917     }
13918
13919   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
13920      and will have been substituted directly into all expressions that use it.
13921      C does not have such a concept, but C++ and other languages do.  */
13922   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
13923     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
13924
13925   if (rtl)
13926     rtl = targetm.delegitimize_address (rtl);
13927
13928   /* If we don't look past the constant pool, we risk emitting a
13929      reference to a constant pool entry that isn't referenced from
13930      code, and thus is not emitted.  */
13931   if (rtl)
13932     rtl = avoid_constant_pool_reference (rtl);
13933
13934   return rtl;
13935 }
13936
13937 /* Check whether decl is a Fortran COMMON symbol.  If not, NULL_TREE is
13938    returned.  If so, the decl for the COMMON block is returned, and the
13939    value is the offset into the common block for the symbol.  */
13940
13941 static tree
13942 fortran_common (tree decl, HOST_WIDE_INT *value)
13943 {
13944   tree val_expr, cvar;
13945   enum machine_mode mode;
13946   HOST_WIDE_INT bitsize, bitpos;
13947   tree offset;
13948   int volatilep = 0, unsignedp = 0;
13949
13950   /* If the decl isn't a VAR_DECL, or if it isn't public or static, or if
13951      it does not have a value (the offset into the common area), or if it
13952      is thread local (as opposed to global) then it isn't common, and shouldn't
13953      be handled as such.  */
13954   if (TREE_CODE (decl) != VAR_DECL
13955       || !TREE_PUBLIC (decl)
13956       || !TREE_STATIC (decl)
13957       || !DECL_HAS_VALUE_EXPR_P (decl)
13958       || !is_fortran ())
13959     return NULL_TREE;
13960
13961   val_expr = DECL_VALUE_EXPR (decl);
13962   if (TREE_CODE (val_expr) != COMPONENT_REF)
13963     return NULL_TREE;
13964
13965   cvar = get_inner_reference (val_expr, &bitsize, &bitpos, &offset,
13966                               &mode, &unsignedp, &volatilep, true);
13967
13968   if (cvar == NULL_TREE
13969       || TREE_CODE (cvar) != VAR_DECL
13970       || DECL_ARTIFICIAL (cvar)
13971       || !TREE_PUBLIC (cvar))
13972     return NULL_TREE;
13973
13974   *value = 0;
13975   if (offset != NULL)
13976     {
13977       if (!host_integerp (offset, 0))
13978         return NULL_TREE;
13979       *value = tree_low_cst (offset, 0);
13980     }
13981   if (bitpos != 0)
13982     *value += bitpos / BITS_PER_UNIT;
13983
13984   return cvar;
13985 }
13986
13987 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
13988    data attribute for a variable or a parameter.  We generate the
13989    DW_AT_const_value attribute only in those cases where the given variable
13990    or parameter does not have a true "location" either in memory or in a
13991    register.  This can happen (for example) when a constant is passed as an
13992    actual argument in a call to an inline function.  (It's possible that
13993    these things can crop up in other ways also.)  Note that one type of
13994    constant value which can be passed into an inlined function is a constant
13995    pointer.  This can happen for example if an actual argument in an inlined
13996    function call evaluates to a compile-time constant address.  */
13997
13998 static bool
13999 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
14000                                        enum dwarf_attribute attr)
14001 {
14002   rtx rtl;
14003   dw_loc_list_ref list;
14004   var_loc_list *loc_list;
14005
14006   if (TREE_CODE (decl) == ERROR_MARK)
14007     return false;
14008
14009   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
14010               || TREE_CODE (decl) == RESULT_DECL);
14011
14012   /* Try to get some constant RTL for this decl, and use that as the value of
14013      the location.  */
14014
14015   rtl = rtl_for_decl_location (decl);
14016   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14017       && add_const_value_attribute (die, rtl))
14018     return true;
14019
14020   /* See if we have single element location list that is equivalent to
14021      a constant value.  That way we are better to use add_const_value_attribute
14022      rather than expanding constant value equivalent.  */
14023   loc_list = lookup_decl_loc (decl);
14024   if (loc_list && loc_list->first && loc_list->first == loc_list->last)
14025     {
14026       enum var_init_status status;
14027       struct var_loc_node *node;
14028
14029       node = loc_list->first;
14030       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
14031       rtl = NOTE_VAR_LOCATION (node->var_loc_note);
14032       if (GET_CODE (rtl) == VAR_LOCATION
14033           && GET_CODE (XEXP (rtl, 1)) != PARALLEL)
14034         rtl = XEXP (XEXP (rtl, 1), 0);
14035       if ((CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING)
14036           && add_const_value_attribute (die, rtl))
14037          return true;
14038     }
14039   list = loc_list_from_tree (decl, decl_by_reference_p (decl) ? 0 : 2);
14040   if (list)
14041     {
14042       add_AT_location_description (die, attr, list);
14043       return true;
14044     }
14045   /* None of that worked, so it must not really have a location;
14046      try adding a constant value attribute from the DECL_INITIAL.  */
14047   return tree_add_const_value_attribute_for_decl (die, decl);
14048 }
14049
14050 /* Add VARIABLE and DIE into deferred locations list.  */
14051
14052 static void
14053 defer_location (tree variable, dw_die_ref die)
14054 {
14055   deferred_locations entry;
14056   entry.variable = variable;
14057   entry.die = die;
14058   VEC_safe_push (deferred_locations, gc, deferred_locations_list, &entry);
14059 }
14060
14061 /* Helper function for tree_add_const_value_attribute.  Natively encode
14062    initializer INIT into an array.  Return true if successful.  */
14063
14064 static bool
14065 native_encode_initializer (tree init, unsigned char *array, int size)
14066 {
14067   tree type;
14068
14069   if (init == NULL_TREE)
14070     return false;
14071
14072   STRIP_NOPS (init);
14073   switch (TREE_CODE (init))
14074     {
14075     case STRING_CST:
14076       type = TREE_TYPE (init);
14077       if (TREE_CODE (type) == ARRAY_TYPE)
14078         {
14079           tree enttype = TREE_TYPE (type);
14080           enum machine_mode mode = TYPE_MODE (enttype);
14081
14082           if (GET_MODE_CLASS (mode) != MODE_INT || GET_MODE_SIZE (mode) != 1)
14083             return false;
14084           if (int_size_in_bytes (type) != size)
14085             return false;
14086           if (size > TREE_STRING_LENGTH (init))
14087             {
14088               memcpy (array, TREE_STRING_POINTER (init),
14089                       TREE_STRING_LENGTH (init));
14090               memset (array + TREE_STRING_LENGTH (init),
14091                       '\0', size - TREE_STRING_LENGTH (init));
14092             }
14093           else
14094             memcpy (array, TREE_STRING_POINTER (init), size);
14095           return true;
14096         }
14097       return false;
14098     case CONSTRUCTOR:
14099       type = TREE_TYPE (init);
14100       if (int_size_in_bytes (type) != size)
14101         return false;
14102       if (TREE_CODE (type) == ARRAY_TYPE)
14103         {
14104           HOST_WIDE_INT min_index;
14105           unsigned HOST_WIDE_INT cnt;
14106           int curpos = 0, fieldsize;
14107           constructor_elt *ce;
14108
14109           if (TYPE_DOMAIN (type) == NULL_TREE
14110               || !host_integerp (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0))
14111             return false;
14112
14113           fieldsize = int_size_in_bytes (TREE_TYPE (type));
14114           if (fieldsize <= 0)
14115             return false;
14116
14117           min_index = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0);
14118           memset (array, '\0', size);
14119           for (cnt = 0;
14120                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
14121                cnt++)
14122             {
14123               tree val = ce->value;
14124               tree index = ce->index;
14125               int pos = curpos;
14126               if (index && TREE_CODE (index) == RANGE_EXPR)
14127                 pos = (tree_low_cst (TREE_OPERAND (index, 0), 0) - min_index)
14128                       * fieldsize;
14129               else if (index)
14130                 pos = (tree_low_cst (index, 0) - min_index) * fieldsize;
14131
14132               if (val)
14133                 {
14134                   STRIP_NOPS (val);
14135                   if (!native_encode_initializer (val, array + pos, fieldsize))
14136                     return false;
14137                 }
14138               curpos = pos + fieldsize;
14139               if (index && TREE_CODE (index) == RANGE_EXPR)
14140                 {
14141                   int count = tree_low_cst (TREE_OPERAND (index, 1), 0)
14142                               - tree_low_cst (TREE_OPERAND (index, 0), 0);
14143                   while (count > 0)
14144                     {
14145                       if (val)
14146                         memcpy (array + curpos, array + pos, fieldsize);
14147                       curpos += fieldsize;
14148                     }
14149                 }
14150               gcc_assert (curpos <= size);
14151             }
14152           return true;
14153         }
14154       else if (TREE_CODE (type) == RECORD_TYPE
14155                || TREE_CODE (type) == UNION_TYPE)
14156         {
14157           tree field = NULL_TREE;
14158           unsigned HOST_WIDE_INT cnt;
14159           constructor_elt *ce;
14160
14161           if (int_size_in_bytes (type) != size)
14162             return false;
14163
14164           if (TREE_CODE (type) == RECORD_TYPE)
14165             field = TYPE_FIELDS (type);
14166
14167           for (cnt = 0;
14168                VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (init), cnt, ce);
14169                cnt++, field = field ? TREE_CHAIN (field) : 0)
14170             {
14171               tree val = ce->value;
14172               int pos, fieldsize;
14173
14174               if (ce->index != 0)
14175                 field = ce->index;
14176
14177               if (val)
14178                 STRIP_NOPS (val);
14179
14180               if (field == NULL_TREE || DECL_BIT_FIELD (field))
14181                 return false;
14182
14183               if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
14184                   && TYPE_DOMAIN (TREE_TYPE (field))
14185                   && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
14186                 return false;
14187               else if (DECL_SIZE_UNIT (field) == NULL_TREE
14188                        || !host_integerp (DECL_SIZE_UNIT (field), 0))
14189                 return false;
14190               fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 0);
14191               pos = int_byte_position (field);
14192               gcc_assert (pos + fieldsize <= size);
14193               if (val
14194                   && !native_encode_initializer (val, array + pos, fieldsize))
14195                 return false;
14196             }
14197           return true;
14198         }
14199       return false;
14200     case VIEW_CONVERT_EXPR:
14201     case NON_LVALUE_EXPR:
14202       return native_encode_initializer (TREE_OPERAND (init, 0), array, size);
14203     default:
14204       return native_encode_expr (init, array, size) == size;
14205     }
14206 }
14207
14208 /* Attach a DW_AT_const_value attribute to DIE. The value of the
14209    attribute is the const value T.  */
14210
14211 static bool
14212 tree_add_const_value_attribute (dw_die_ref die, tree t)
14213 {
14214   tree init;
14215   tree type = TREE_TYPE (t);
14216   rtx rtl;
14217
14218   if (!t || !TREE_TYPE (t) || TREE_TYPE (t) == error_mark_node)
14219     return false;
14220
14221   init = t;
14222   gcc_assert (!DECL_P (init));
14223
14224   rtl = rtl_for_decl_init (init, type);
14225   if (rtl)
14226     return add_const_value_attribute (die, rtl);
14227   /* If the host and target are sane, try harder.  */
14228   else if (CHAR_BIT == 8 && BITS_PER_UNIT == 8
14229            && initializer_constant_valid_p (init, type))
14230     {
14231       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (init));
14232       if (size > 0 && (int) size == size)
14233         {
14234           unsigned char *array = GGC_CNEWVEC (unsigned char, size);
14235
14236           if (native_encode_initializer (init, array, size))
14237             {
14238               add_AT_vec (die, DW_AT_const_value, size, 1, array);
14239               return true;
14240             }
14241         }
14242     }
14243   return false;
14244 }
14245
14246 /* Attach a DW_AT_const_value attribute to VAR_DIE. The value of the
14247    attribute is the const value of T, where T is an integral constant
14248    variable with static storage duration
14249    (so it can't be a PARM_DECL or a RESULT_DECL).  */
14250
14251 static bool
14252 tree_add_const_value_attribute_for_decl (dw_die_ref var_die, tree decl)
14253 {
14254
14255   if (!decl
14256       || (TREE_CODE (decl) != VAR_DECL
14257           && TREE_CODE (decl) != CONST_DECL))
14258     return false;
14259
14260     if (TREE_READONLY (decl)
14261         && ! TREE_THIS_VOLATILE (decl)
14262         && DECL_INITIAL (decl))
14263       /* OK */;
14264     else
14265       return false;
14266
14267   return tree_add_const_value_attribute (var_die, DECL_INITIAL (decl));
14268 }
14269
14270 /* Convert the CFI instructions for the current function into a
14271    location list.  This is used for DW_AT_frame_base when we targeting
14272    a dwarf2 consumer that does not support the dwarf3
14273    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
14274    expressions.  */
14275
14276 static dw_loc_list_ref
14277 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
14278 {
14279   dw_fde_ref fde;
14280   dw_loc_list_ref list, *list_tail;
14281   dw_cfi_ref cfi;
14282   dw_cfa_location last_cfa, next_cfa;
14283   const char *start_label, *last_label, *section;
14284   dw_cfa_location remember;
14285
14286   fde = current_fde ();
14287   gcc_assert (fde != NULL);
14288
14289   section = secname_for_decl (current_function_decl);
14290   list_tail = &list;
14291   list = NULL;
14292
14293   memset (&next_cfa, 0, sizeof (next_cfa));
14294   next_cfa.reg = INVALID_REGNUM;
14295   remember = next_cfa;
14296
14297   start_label = fde->dw_fde_begin;
14298
14299   /* ??? Bald assumption that the CIE opcode list does not contain
14300      advance opcodes.  */
14301   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
14302     lookup_cfa_1 (cfi, &next_cfa, &remember);
14303
14304   last_cfa = next_cfa;
14305   last_label = start_label;
14306
14307   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
14308     switch (cfi->dw_cfi_opc)
14309       {
14310       case DW_CFA_set_loc:
14311       case DW_CFA_advance_loc1:
14312       case DW_CFA_advance_loc2:
14313       case DW_CFA_advance_loc4:
14314         if (!cfa_equal_p (&last_cfa, &next_cfa))
14315           {
14316             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14317                                        start_label, last_label, section,
14318                                        list == NULL);
14319
14320             list_tail = &(*list_tail)->dw_loc_next;
14321             last_cfa = next_cfa;
14322             start_label = last_label;
14323           }
14324         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
14325         break;
14326
14327       case DW_CFA_advance_loc:
14328         /* The encoding is complex enough that we should never emit this.  */
14329         gcc_unreachable ();
14330
14331       default:
14332         lookup_cfa_1 (cfi, &next_cfa, &remember);
14333         break;
14334       }
14335
14336   if (!cfa_equal_p (&last_cfa, &next_cfa))
14337     {
14338       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
14339                                  start_label, last_label, section,
14340                                  list == NULL);
14341       list_tail = &(*list_tail)->dw_loc_next;
14342       start_label = last_label;
14343     }
14344   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
14345                              start_label, fde->dw_fde_end, section,
14346                              list == NULL);
14347
14348   return list;
14349 }
14350
14351 /* Compute a displacement from the "steady-state frame pointer" to the
14352    frame base (often the same as the CFA), and store it in
14353    frame_pointer_fb_offset.  OFFSET is added to the displacement
14354    before the latter is negated.  */
14355
14356 static void
14357 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
14358 {
14359   rtx reg, elim;
14360
14361 #ifdef FRAME_POINTER_CFA_OFFSET
14362   reg = frame_pointer_rtx;
14363   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
14364 #else
14365   reg = arg_pointer_rtx;
14366   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
14367 #endif
14368
14369   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
14370   if (GET_CODE (elim) == PLUS)
14371     {
14372       offset += INTVAL (XEXP (elim, 1));
14373       elim = XEXP (elim, 0);
14374     }
14375
14376   gcc_assert ((SUPPORTS_STACK_ALIGNMENT
14377                && (elim == hard_frame_pointer_rtx
14378                    || elim == stack_pointer_rtx))
14379               || elim == (frame_pointer_needed
14380                           ? hard_frame_pointer_rtx
14381                           : stack_pointer_rtx));
14382
14383   frame_pointer_fb_offset = -offset;
14384 }
14385
14386 /* Generate a DW_AT_name attribute given some string value to be included as
14387    the value of the attribute.  */
14388
14389 static void
14390 add_name_attribute (dw_die_ref die, const char *name_string)
14391 {
14392   if (name_string != NULL && *name_string != 0)
14393     {
14394       if (demangle_name_func)
14395         name_string = (*demangle_name_func) (name_string);
14396
14397       add_AT_string (die, DW_AT_name, name_string);
14398     }
14399 }
14400
14401 /* Generate a DW_AT_comp_dir attribute for DIE.  */
14402
14403 static void
14404 add_comp_dir_attribute (dw_die_ref die)
14405 {
14406   const char *wd = get_src_pwd ();
14407   char *wd1;
14408
14409   if (wd == NULL)
14410     return;
14411
14412   if (DWARF2_DIR_SHOULD_END_WITH_SEPARATOR)
14413     {
14414       int wdlen;
14415
14416       wdlen = strlen (wd);
14417       wd1 = GGC_NEWVEC (char, wdlen + 2);
14418       strcpy (wd1, wd);
14419       wd1 [wdlen] = DIR_SEPARATOR;
14420       wd1 [wdlen + 1] = 0;
14421       wd = wd1;
14422     }
14423
14424     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
14425 }
14426
14427 /* Given a tree node describing an array bound (either lower or upper) output
14428    a representation for that bound.  */
14429
14430 static void
14431 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
14432 {
14433   switch (TREE_CODE (bound))
14434     {
14435     case ERROR_MARK:
14436       return;
14437
14438     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
14439     case INTEGER_CST:
14440       {
14441         unsigned int prec = simple_type_size_in_bits (TREE_TYPE (bound));
14442
14443         /* Use the default if possible.  */
14444         if (bound_attr == DW_AT_lower_bound
14445             && (((is_c_family () || is_java ()) && integer_zerop (bound))
14446                 || (is_fortran () && integer_onep (bound))))
14447           ;
14448
14449         /* Otherwise represent the bound as an unsigned value with the
14450            precision of its type.  The precision and signedness of the
14451            type will be necessary to re-interpret it unambiguously.  */
14452         else if (prec < HOST_BITS_PER_WIDE_INT)
14453           {
14454             unsigned HOST_WIDE_INT mask
14455               = ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
14456             add_AT_unsigned (subrange_die, bound_attr,
14457                              TREE_INT_CST_LOW (bound) & mask);
14458           }
14459         else if (prec == HOST_BITS_PER_WIDE_INT
14460                  || TREE_INT_CST_HIGH (bound) == 0)
14461           add_AT_unsigned (subrange_die, bound_attr,
14462                            TREE_INT_CST_LOW (bound));
14463         else
14464           add_AT_double (subrange_die, bound_attr, TREE_INT_CST_HIGH (bound),
14465                          TREE_INT_CST_LOW (bound));
14466       }
14467       break;
14468
14469     CASE_CONVERT:
14470     case VIEW_CONVERT_EXPR:
14471       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
14472       break;
14473
14474     case SAVE_EXPR:
14475       break;
14476
14477     case VAR_DECL:
14478     case PARM_DECL:
14479     case RESULT_DECL:
14480       {
14481         dw_die_ref decl_die = lookup_decl_die (bound);
14482         dw_loc_list_ref loc;
14483
14484         /* ??? Can this happen, or should the variable have been bound
14485            first?  Probably it can, since I imagine that we try to create
14486            the types of parameters in the order in which they exist in
14487            the list, and won't have created a forward reference to a
14488            later parameter.  */
14489         if (decl_die != NULL)
14490           add_AT_die_ref (subrange_die, bound_attr, decl_die);
14491         else
14492           {
14493             loc = loc_list_from_tree (bound, 0);
14494             add_AT_location_description (subrange_die, bound_attr, loc);
14495           }
14496         break;
14497       }
14498
14499     default:
14500       {
14501         /* Otherwise try to create a stack operation procedure to
14502            evaluate the value of the array bound.  */
14503
14504         dw_die_ref ctx, decl_die;
14505         dw_loc_list_ref list;
14506
14507         list = loc_list_from_tree (bound, 2);
14508         if (list == NULL)
14509           break;
14510
14511         if (current_function_decl == 0)
14512           ctx = comp_unit_die;
14513         else
14514           ctx = lookup_decl_die (current_function_decl);
14515
14516         decl_die = new_die (DW_TAG_variable, ctx, bound);
14517         add_AT_flag (decl_die, DW_AT_artificial, 1);
14518         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
14519         if (list->dw_loc_next)
14520           add_AT_loc_list (decl_die, DW_AT_location, list);
14521         else
14522           add_AT_loc (decl_die, DW_AT_location, list->expr);
14523
14524         add_AT_die_ref (subrange_die, bound_attr, decl_die);
14525         break;
14526       }
14527     }
14528 }
14529
14530 /* Add subscript info to TYPE_DIE, describing an array TYPE, collapsing
14531    possibly nested array subscripts in a flat sequence if COLLAPSE_P is true.
14532    Note that the block of subscript information for an array type also
14533    includes information about the element type of the given array type.  */
14534
14535 static void
14536 add_subscript_info (dw_die_ref type_die, tree type, bool collapse_p)
14537 {
14538   unsigned dimension_number;
14539   tree lower, upper;
14540   dw_die_ref subrange_die;
14541
14542   for (dimension_number = 0;
14543        TREE_CODE (type) == ARRAY_TYPE && (dimension_number == 0 || collapse_p);
14544        type = TREE_TYPE (type), dimension_number++)
14545     {
14546       tree domain = TYPE_DOMAIN (type);
14547
14548       if (TYPE_STRING_FLAG (type) && is_fortran () && dimension_number > 0)
14549         break;
14550
14551       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
14552          and (in GNU C only) variable bounds.  Handle all three forms
14553          here.  */
14554       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
14555       if (domain)
14556         {
14557           /* We have an array type with specified bounds.  */
14558           lower = TYPE_MIN_VALUE (domain);
14559           upper = TYPE_MAX_VALUE (domain);
14560
14561           /* Define the index type.  */
14562           if (TREE_TYPE (domain))
14563             {
14564               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
14565                  TREE_TYPE field.  We can't emit debug info for this
14566                  because it is an unnamed integral type.  */
14567               if (TREE_CODE (domain) == INTEGER_TYPE
14568                   && TYPE_NAME (domain) == NULL_TREE
14569                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
14570                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
14571                 ;
14572               else
14573                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
14574                                     type_die);
14575             }
14576
14577           /* ??? If upper is NULL, the array has unspecified length,
14578              but it does have a lower bound.  This happens with Fortran
14579                dimension arr(N:*)
14580              Since the debugger is definitely going to need to know N
14581              to produce useful results, go ahead and output the lower
14582              bound solo, and hope the debugger can cope.  */
14583
14584           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
14585           if (upper)
14586             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
14587         }
14588
14589       /* Otherwise we have an array type with an unspecified length.  The
14590          DWARF-2 spec does not say how to handle this; let's just leave out the
14591          bounds.  */
14592     }
14593 }
14594
14595 static void
14596 add_byte_size_attribute (dw_die_ref die, tree tree_node)
14597 {
14598   unsigned size;
14599
14600   switch (TREE_CODE (tree_node))
14601     {
14602     case ERROR_MARK:
14603       size = 0;
14604       break;
14605     case ENUMERAL_TYPE:
14606     case RECORD_TYPE:
14607     case UNION_TYPE:
14608     case QUAL_UNION_TYPE:
14609       size = int_size_in_bytes (tree_node);
14610       break;
14611     case FIELD_DECL:
14612       /* For a data member of a struct or union, the DW_AT_byte_size is
14613          generally given as the number of bytes normally allocated for an
14614          object of the *declared* type of the member itself.  This is true
14615          even for bit-fields.  */
14616       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
14617       break;
14618     default:
14619       gcc_unreachable ();
14620     }
14621
14622   /* Note that `size' might be -1 when we get to this point.  If it is, that
14623      indicates that the byte size of the entity in question is variable.  We
14624      have no good way of expressing this fact in Dwarf at the present time,
14625      so just let the -1 pass on through.  */
14626   add_AT_unsigned (die, DW_AT_byte_size, size);
14627 }
14628
14629 /* For a FIELD_DECL node which represents a bit-field, output an attribute
14630    which specifies the distance in bits from the highest order bit of the
14631    "containing object" for the bit-field to the highest order bit of the
14632    bit-field itself.
14633
14634    For any given bit-field, the "containing object" is a hypothetical object
14635    (of some integral or enum type) within which the given bit-field lives.  The
14636    type of this hypothetical "containing object" is always the same as the
14637    declared type of the individual bit-field itself.  The determination of the
14638    exact location of the "containing object" for a bit-field is rather
14639    complicated.  It's handled by the `field_byte_offset' function (above).
14640
14641    Note that it is the size (in bytes) of the hypothetical "containing object"
14642    which will be given in the DW_AT_byte_size attribute for this bit-field.
14643    (See `byte_size_attribute' above).  */
14644
14645 static inline void
14646 add_bit_offset_attribute (dw_die_ref die, tree decl)
14647 {
14648   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
14649   tree type = DECL_BIT_FIELD_TYPE (decl);
14650   HOST_WIDE_INT bitpos_int;
14651   HOST_WIDE_INT highest_order_object_bit_offset;
14652   HOST_WIDE_INT highest_order_field_bit_offset;
14653   HOST_WIDE_INT unsigned bit_offset;
14654
14655   /* Must be a field and a bit field.  */
14656   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
14657
14658   /* We can't yet handle bit-fields whose offsets are variable, so if we
14659      encounter such things, just return without generating any attribute
14660      whatsoever.  Likewise for variable or too large size.  */
14661   if (! host_integerp (bit_position (decl), 0)
14662       || ! host_integerp (DECL_SIZE (decl), 1))
14663     return;
14664
14665   bitpos_int = int_bit_position (decl);
14666
14667   /* Note that the bit offset is always the distance (in bits) from the
14668      highest-order bit of the "containing object" to the highest-order bit of
14669      the bit-field itself.  Since the "high-order end" of any object or field
14670      is different on big-endian and little-endian machines, the computation
14671      below must take account of these differences.  */
14672   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
14673   highest_order_field_bit_offset = bitpos_int;
14674
14675   if (! BYTES_BIG_ENDIAN)
14676     {
14677       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
14678       highest_order_object_bit_offset += simple_type_size_in_bits (type);
14679     }
14680
14681   bit_offset
14682     = (! BYTES_BIG_ENDIAN
14683        ? highest_order_object_bit_offset - highest_order_field_bit_offset
14684        : highest_order_field_bit_offset - highest_order_object_bit_offset);
14685
14686   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
14687 }
14688
14689 /* For a FIELD_DECL node which represents a bit field, output an attribute
14690    which specifies the length in bits of the given field.  */
14691
14692 static inline void
14693 add_bit_size_attribute (dw_die_ref die, tree decl)
14694 {
14695   /* Must be a field and a bit field.  */
14696   gcc_assert (TREE_CODE (decl) == FIELD_DECL
14697               && DECL_BIT_FIELD_TYPE (decl));
14698
14699   if (host_integerp (DECL_SIZE (decl), 1))
14700     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
14701 }
14702
14703 /* If the compiled language is ANSI C, then add a 'prototyped'
14704    attribute, if arg types are given for the parameters of a function.  */
14705
14706 static inline void
14707 add_prototyped_attribute (dw_die_ref die, tree func_type)
14708 {
14709   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
14710       && TYPE_ARG_TYPES (func_type) != NULL)
14711     add_AT_flag (die, DW_AT_prototyped, 1);
14712 }
14713
14714 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
14715    by looking in either the type declaration or object declaration
14716    equate table.  */
14717
14718 static inline dw_die_ref
14719 add_abstract_origin_attribute (dw_die_ref die, tree origin)
14720 {
14721   dw_die_ref origin_die = NULL;
14722
14723   if (TREE_CODE (origin) != FUNCTION_DECL)
14724     {
14725       /* We may have gotten separated from the block for the inlined
14726          function, if we're in an exception handler or some such; make
14727          sure that the abstract function has been written out.
14728
14729          Doing this for nested functions is wrong, however; functions are
14730          distinct units, and our context might not even be inline.  */
14731       tree fn = origin;
14732
14733       if (TYPE_P (fn))
14734         fn = TYPE_STUB_DECL (fn);
14735
14736       fn = decl_function_context (fn);
14737       if (fn)
14738         dwarf2out_abstract_function (fn);
14739     }
14740
14741   if (DECL_P (origin))
14742     origin_die = lookup_decl_die (origin);
14743   else if (TYPE_P (origin))
14744     origin_die = lookup_type_die (origin);
14745
14746   /* XXX: Functions that are never lowered don't always have correct block
14747      trees (in the case of java, they simply have no block tree, in some other
14748      languages).  For these functions, there is nothing we can really do to
14749      output correct debug info for inlined functions in all cases.  Rather
14750      than die, we'll just produce deficient debug info now, in that we will
14751      have variables without a proper abstract origin.  In the future, when all
14752      functions are lowered, we should re-add a gcc_assert (origin_die)
14753      here.  */
14754
14755   if (origin_die)
14756     add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
14757   return origin_die;
14758 }
14759
14760 /* We do not currently support the pure_virtual attribute.  */
14761
14762 static inline void
14763 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
14764 {
14765   if (DECL_VINDEX (func_decl))
14766     {
14767       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
14768
14769       if (host_integerp (DECL_VINDEX (func_decl), 0))
14770         add_AT_loc (die, DW_AT_vtable_elem_location,
14771                     new_loc_descr (DW_OP_constu,
14772                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
14773                                    0));
14774
14775       /* GNU extension: Record what type this method came from originally.  */
14776       if (debug_info_level > DINFO_LEVEL_TERSE)
14777         add_AT_die_ref (die, DW_AT_containing_type,
14778                         lookup_type_die (DECL_CONTEXT (func_decl)));
14779     }
14780 }
14781 \f
14782 /* Add source coordinate attributes for the given decl.  */
14783
14784 static void
14785 add_src_coords_attributes (dw_die_ref die, tree decl)
14786 {
14787   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
14788
14789   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
14790   add_AT_unsigned (die, DW_AT_decl_line, s.line);
14791 }
14792
14793 /* Add a DW_AT_name attribute and source coordinate attribute for the
14794    given decl, but only if it actually has a name.  */
14795
14796 static void
14797 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
14798 {
14799   tree decl_name;
14800
14801   decl_name = DECL_NAME (decl);
14802   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
14803     {
14804       add_name_attribute (die, dwarf2_name (decl, 0));
14805       if (! DECL_ARTIFICIAL (decl))
14806         add_src_coords_attributes (die, decl);
14807
14808       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
14809           && TREE_PUBLIC (decl)
14810           && !DECL_ABSTRACT (decl)
14811           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
14812           && !is_fortran ())
14813         {
14814           /* Defer until we have an assembler name set.  */
14815           if (!DECL_ASSEMBLER_NAME_SET_P (decl))
14816             {
14817               limbo_die_node *asm_name;
14818
14819               asm_name = GGC_CNEW (limbo_die_node);
14820               asm_name->die = die;
14821               asm_name->created_for = decl;
14822               asm_name->next = deferred_asm_name;
14823               deferred_asm_name = asm_name;
14824             }
14825           else if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
14826             add_AT_string (die, DW_AT_MIPS_linkage_name,
14827                            IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
14828         }
14829     }
14830
14831 #ifdef VMS_DEBUGGING_INFO
14832   /* Get the function's name, as described by its RTL.  This may be different
14833      from the DECL_NAME name used in the source file.  */
14834   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
14835     {
14836       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
14837                    XEXP (DECL_RTL (decl), 0));
14838       VEC_safe_push (rtx, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
14839     }
14840 #endif
14841 }
14842
14843 /* Push a new declaration scope.  */
14844
14845 static void
14846 push_decl_scope (tree scope)
14847 {
14848   VEC_safe_push (tree, gc, decl_scope_table, scope);
14849 }
14850
14851 /* Pop a declaration scope.  */
14852
14853 static inline void
14854 pop_decl_scope (void)
14855 {
14856   VEC_pop (tree, decl_scope_table);
14857 }
14858
14859 /* Return the DIE for the scope that immediately contains this type.
14860    Non-named types get global scope.  Named types nested in other
14861    types get their containing scope if it's open, or global scope
14862    otherwise.  All other types (i.e. function-local named types) get
14863    the current active scope.  */
14864
14865 static dw_die_ref
14866 scope_die_for (tree t, dw_die_ref context_die)
14867 {
14868   dw_die_ref scope_die = NULL;
14869   tree containing_scope;
14870   int i;
14871
14872   /* Non-types always go in the current scope.  */
14873   gcc_assert (TYPE_P (t));
14874
14875   containing_scope = TYPE_CONTEXT (t);
14876
14877   /* Use the containing namespace if it was passed in (for a declaration).  */
14878   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
14879     {
14880       if (context_die == lookup_decl_die (containing_scope))
14881         /* OK */;
14882       else
14883         containing_scope = NULL_TREE;
14884     }
14885
14886   /* Ignore function type "scopes" from the C frontend.  They mean that
14887      a tagged type is local to a parmlist of a function declarator, but
14888      that isn't useful to DWARF.  */
14889   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
14890     containing_scope = NULL_TREE;
14891
14892   if (containing_scope == NULL_TREE)
14893     scope_die = comp_unit_die;
14894   else if (TYPE_P (containing_scope))
14895     {
14896       /* For types, we can just look up the appropriate DIE.  But
14897          first we check to see if we're in the middle of emitting it
14898          so we know where the new DIE should go.  */
14899       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
14900         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
14901           break;
14902
14903       if (i < 0)
14904         {
14905           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
14906                       || TREE_ASM_WRITTEN (containing_scope));
14907
14908           /* If none of the current dies are suitable, we get file scope.  */
14909           scope_die = comp_unit_die;
14910         }
14911       else
14912         scope_die = lookup_type_die (containing_scope);
14913     }
14914   else
14915     scope_die = context_die;
14916
14917   return scope_die;
14918 }
14919
14920 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
14921
14922 static inline int
14923 local_scope_p (dw_die_ref context_die)
14924 {
14925   for (; context_die; context_die = context_die->die_parent)
14926     if (context_die->die_tag == DW_TAG_inlined_subroutine
14927         || context_die->die_tag == DW_TAG_subprogram)
14928       return 1;
14929
14930   return 0;
14931 }
14932
14933 /* Returns nonzero if CONTEXT_DIE is a class.  */
14934
14935 static inline int
14936 class_scope_p (dw_die_ref context_die)
14937 {
14938   return (context_die
14939           && (context_die->die_tag == DW_TAG_structure_type
14940               || context_die->die_tag == DW_TAG_class_type
14941               || context_die->die_tag == DW_TAG_interface_type
14942               || context_die->die_tag == DW_TAG_union_type));
14943 }
14944
14945 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
14946    whether or not to treat a DIE in this context as a declaration.  */
14947
14948 static inline int
14949 class_or_namespace_scope_p (dw_die_ref context_die)
14950 {
14951   return (class_scope_p (context_die)
14952           || (context_die && context_die->die_tag == DW_TAG_namespace));
14953 }
14954
14955 /* Many forms of DIEs require a "type description" attribute.  This
14956    routine locates the proper "type descriptor" die for the type given
14957    by 'type', and adds a DW_AT_type attribute below the given die.  */
14958
14959 static void
14960 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
14961                     int decl_volatile, dw_die_ref context_die)
14962 {
14963   enum tree_code code  = TREE_CODE (type);
14964   dw_die_ref type_die  = NULL;
14965
14966   /* ??? If this type is an unnamed subrange type of an integral, floating-point
14967      or fixed-point type, use the inner type.  This is because we have no
14968      support for unnamed types in base_type_die.  This can happen if this is
14969      an Ada subrange type.  Correct solution is emit a subrange type die.  */
14970   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
14971       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
14972     type = TREE_TYPE (type), code = TREE_CODE (type);
14973
14974   if (code == ERROR_MARK
14975       /* Handle a special case.  For functions whose return type is void, we
14976          generate *no* type attribute.  (Note that no object may have type
14977          `void', so this only applies to function return types).  */
14978       || code == VOID_TYPE)
14979     return;
14980
14981   type_die = modified_type_die (type,
14982                                 decl_const || TYPE_READONLY (type),
14983                                 decl_volatile || TYPE_VOLATILE (type),
14984                                 context_die);
14985
14986   if (type_die != NULL)
14987     add_AT_die_ref (object_die, DW_AT_type, type_die);
14988 }
14989
14990 /* Given an object die, add the calling convention attribute for the
14991    function call type.  */
14992 static void
14993 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
14994 {
14995   enum dwarf_calling_convention value = DW_CC_normal;
14996
14997   value = ((enum dwarf_calling_convention)
14998            targetm.dwarf_calling_convention (TREE_TYPE (decl)));
14999
15000   /* DWARF doesn't provide a way to identify a program's source-level
15001      entry point.  DW_AT_calling_convention attributes are only meant
15002      to describe functions' calling conventions.  However, lacking a
15003      better way to signal the Fortran main program, we use this for the
15004      time being, following existing custom.  */
15005   if (is_fortran ()
15006       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
15007     value = DW_CC_program;
15008
15009   /* Only add the attribute if the backend requests it, and
15010      is not DW_CC_normal.  */
15011   if (value && (value != DW_CC_normal))
15012     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
15013 }
15014
15015 /* Given a tree pointer to a struct, class, union, or enum type node, return
15016    a pointer to the (string) tag name for the given type, or zero if the type
15017    was declared without a tag.  */
15018
15019 static const char *
15020 type_tag (const_tree type)
15021 {
15022   const char *name = 0;
15023
15024   if (TYPE_NAME (type) != 0)
15025     {
15026       tree t = 0;
15027
15028       /* Find the IDENTIFIER_NODE for the type name.  */
15029       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
15030         t = TYPE_NAME (type);
15031
15032       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
15033          a TYPE_DECL node, regardless of whether or not a `typedef' was
15034          involved.  */
15035       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
15036                && ! DECL_IGNORED_P (TYPE_NAME (type)))
15037         {
15038           /* We want to be extra verbose.  Don't call dwarf_name if
15039              DECL_NAME isn't set.  The default hook for decl_printable_name
15040              doesn't like that, and in this context it's correct to return
15041              0, instead of "<anonymous>" or the like.  */
15042           if (DECL_NAME (TYPE_NAME (type)))
15043             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
15044         }
15045
15046       /* Now get the name as a string, or invent one.  */
15047       if (!name && t != 0)
15048         name = IDENTIFIER_POINTER (t);
15049     }
15050
15051   return (name == 0 || *name == '\0') ? 0 : name;
15052 }
15053
15054 /* Return the type associated with a data member, make a special check
15055    for bit field types.  */
15056
15057 static inline tree
15058 member_declared_type (const_tree member)
15059 {
15060   return (DECL_BIT_FIELD_TYPE (member)
15061           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
15062 }
15063
15064 /* Get the decl's label, as described by its RTL. This may be different
15065    from the DECL_NAME name used in the source file.  */
15066
15067 #if 0
15068 static const char *
15069 decl_start_label (tree decl)
15070 {
15071   rtx x;
15072   const char *fnname;
15073
15074   x = DECL_RTL (decl);
15075   gcc_assert (MEM_P (x));
15076
15077   x = XEXP (x, 0);
15078   gcc_assert (GET_CODE (x) == SYMBOL_REF);
15079
15080   fnname = XSTR (x, 0);
15081   return fnname;
15082 }
15083 #endif
15084 \f
15085 /* These routines generate the internal representation of the DIE's for
15086    the compilation unit.  Debugging information is collected by walking
15087    the declaration trees passed in from dwarf2out_decl().  */
15088
15089 static void
15090 gen_array_type_die (tree type, dw_die_ref context_die)
15091 {
15092   dw_die_ref scope_die = scope_die_for (type, context_die);
15093   dw_die_ref array_die;
15094
15095   /* GNU compilers represent multidimensional array types as sequences of one
15096      dimensional array types whose element types are themselves array types.
15097      We sometimes squish that down to a single array_type DIE with multiple
15098      subscripts in the Dwarf debugging info.  The draft Dwarf specification
15099      say that we are allowed to do this kind of compression in C, because
15100      there is no difference between an array of arrays and a multidimensional
15101      array.  We don't do this for Ada to remain as close as possible to the
15102      actual representation, which is especially important against the language
15103      flexibilty wrt arrays of variable size.  */
15104
15105   bool collapse_nested_arrays = !is_ada ();
15106   tree element_type;
15107
15108   /* Emit DW_TAG_string_type for Fortran character types (with kind 1 only, as
15109      DW_TAG_string_type doesn't have DW_AT_type attribute).  */
15110   if (TYPE_STRING_FLAG (type)
15111       && TREE_CODE (type) == ARRAY_TYPE
15112       && is_fortran ()
15113       && TYPE_MODE (TREE_TYPE (type)) == TYPE_MODE (char_type_node))
15114     {
15115       HOST_WIDE_INT size;
15116
15117       array_die = new_die (DW_TAG_string_type, scope_die, type);
15118       add_name_attribute (array_die, type_tag (type));
15119       equate_type_number_to_die (type, array_die);
15120       size = int_size_in_bytes (type);
15121       if (size >= 0)
15122         add_AT_unsigned (array_die, DW_AT_byte_size, size);
15123       else if (TYPE_DOMAIN (type) != NULL_TREE
15124                && TYPE_MAX_VALUE (TYPE_DOMAIN (type)) != NULL_TREE
15125                && DECL_P (TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
15126         {
15127           tree szdecl = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
15128           dw_loc_list_ref loc = loc_list_from_tree (szdecl, 2);
15129
15130           size = int_size_in_bytes (TREE_TYPE (szdecl));
15131           if (loc && size > 0)
15132             {
15133               add_AT_location_description (array_die, DW_AT_string_length, loc);
15134               if (size != DWARF2_ADDR_SIZE)
15135                 add_AT_unsigned (array_die, DW_AT_byte_size, size);
15136             }
15137         }
15138       return;
15139     }
15140
15141   /* ??? The SGI dwarf reader fails for array of array of enum types
15142      (e.g. const enum machine_mode insn_operand_mode[2][10]) unless the inner
15143      array type comes before the outer array type.  We thus call gen_type_die
15144      before we new_die and must prevent nested array types collapsing for this
15145      target.  */
15146
15147 #ifdef MIPS_DEBUGGING_INFO
15148   gen_type_die (TREE_TYPE (type), context_die);
15149   collapse_nested_arrays = false;
15150 #endif
15151
15152   array_die = new_die (DW_TAG_array_type, scope_die, type);
15153   add_name_attribute (array_die, type_tag (type));
15154   equate_type_number_to_die (type, array_die);
15155
15156   if (TREE_CODE (type) == VECTOR_TYPE)
15157     {
15158       /* The frontend feeds us a representation for the vector as a struct
15159          containing an array.  Pull out the array type.  */
15160       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
15161       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
15162     }
15163
15164   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
15165   if (is_fortran ()
15166       && TREE_CODE (type) == ARRAY_TYPE
15167       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE
15168       && !TYPE_STRING_FLAG (TREE_TYPE (type)))
15169     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
15170
15171 #if 0
15172   /* We default the array ordering.  SDB will probably do
15173      the right things even if DW_AT_ordering is not present.  It's not even
15174      an issue until we start to get into multidimensional arrays anyway.  If
15175      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
15176      then we'll have to put the DW_AT_ordering attribute back in.  (But if
15177      and when we find out that we need to put these in, we will only do so
15178      for multidimensional arrays.  */
15179   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
15180 #endif
15181
15182 #ifdef MIPS_DEBUGGING_INFO
15183   /* The SGI compilers handle arrays of unknown bound by setting
15184      AT_declaration and not emitting any subrange DIEs.  */
15185   if (! TYPE_DOMAIN (type))
15186     add_AT_flag (array_die, DW_AT_declaration, 1);
15187   else
15188 #endif
15189     add_subscript_info (array_die, type, collapse_nested_arrays);
15190
15191   /* Add representation of the type of the elements of this array type and
15192      emit the corresponding DIE if we haven't done it already.  */  
15193   element_type = TREE_TYPE (type);
15194   if (collapse_nested_arrays)
15195     while (TREE_CODE (element_type) == ARRAY_TYPE)
15196       {
15197         if (TYPE_STRING_FLAG (element_type) && is_fortran ())
15198           break;
15199         element_type = TREE_TYPE (element_type);
15200       }
15201
15202 #ifndef MIPS_DEBUGGING_INFO
15203   gen_type_die (element_type, context_die);
15204 #endif
15205
15206   add_type_attribute (array_die, element_type, 0, 0, context_die);
15207
15208   if (get_AT (array_die, DW_AT_name))
15209     add_pubtype (type, array_die);
15210 }
15211
15212 static dw_loc_descr_ref
15213 descr_info_loc (tree val, tree base_decl)
15214 {
15215   HOST_WIDE_INT size;
15216   dw_loc_descr_ref loc, loc2;
15217   enum dwarf_location_atom op;
15218
15219   if (val == base_decl)
15220     return new_loc_descr (DW_OP_push_object_address, 0, 0);
15221
15222   switch (TREE_CODE (val))
15223     {
15224     CASE_CONVERT:
15225       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15226     case VAR_DECL:
15227       return loc_descriptor_from_tree (val, 0);
15228     case INTEGER_CST:
15229       if (host_integerp (val, 0))
15230         return int_loc_descriptor (tree_low_cst (val, 0));
15231       break;
15232     case INDIRECT_REF:
15233       size = int_size_in_bytes (TREE_TYPE (val));
15234       if (size < 0)
15235         break;
15236       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15237       if (!loc)
15238         break;
15239       if (size == DWARF2_ADDR_SIZE)
15240         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
15241       else
15242         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
15243       return loc;
15244     case POINTER_PLUS_EXPR:
15245     case PLUS_EXPR:
15246       if (host_integerp (TREE_OPERAND (val, 1), 1)
15247           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
15248              < 16384)
15249         {
15250           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15251           if (!loc)
15252             break;
15253           loc_descr_plus_const (&loc, tree_low_cst (TREE_OPERAND (val, 1), 0));
15254         }
15255       else
15256         {
15257           op = DW_OP_plus;
15258         do_binop:
15259           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
15260           if (!loc)
15261             break;
15262           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
15263           if (!loc2)
15264             break;
15265           add_loc_descr (&loc, loc2);
15266           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
15267         }
15268       return loc;
15269     case MINUS_EXPR:
15270       op = DW_OP_minus;
15271       goto do_binop;
15272     case MULT_EXPR:
15273       op = DW_OP_mul;
15274       goto do_binop;
15275     case EQ_EXPR:
15276       op = DW_OP_eq;
15277       goto do_binop;
15278     case NE_EXPR:
15279       op = DW_OP_ne;
15280       goto do_binop;
15281     default:
15282       break;
15283     }
15284   return NULL;
15285 }
15286
15287 static void
15288 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
15289                       tree val, tree base_decl)
15290 {
15291   dw_loc_descr_ref loc;
15292
15293   if (host_integerp (val, 0))
15294     {
15295       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
15296       return;
15297     }
15298
15299   loc = descr_info_loc (val, base_decl);
15300   if (!loc)
15301     return;
15302
15303   add_AT_loc (die, attr, loc);
15304 }
15305
15306 /* This routine generates DIE for array with hidden descriptor, details
15307    are filled into *info by a langhook.  */
15308
15309 static void
15310 gen_descr_array_type_die (tree type, struct array_descr_info *info,
15311                           dw_die_ref context_die)
15312 {
15313   dw_die_ref scope_die = scope_die_for (type, context_die);
15314   dw_die_ref array_die;
15315   int dim;
15316
15317   array_die = new_die (DW_TAG_array_type, scope_die, type);
15318   add_name_attribute (array_die, type_tag (type));
15319   equate_type_number_to_die (type, array_die);
15320
15321   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
15322   if (is_fortran ()
15323       && info->ndimensions >= 2)
15324     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
15325
15326   if (info->data_location)
15327     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
15328                           info->base_decl);
15329   if (info->associated)
15330     add_descr_info_field (array_die, DW_AT_associated, info->associated,
15331                           info->base_decl);
15332   if (info->allocated)
15333     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
15334                           info->base_decl);
15335
15336   for (dim = 0; dim < info->ndimensions; dim++)
15337     {
15338       dw_die_ref subrange_die
15339         = new_die (DW_TAG_subrange_type, array_die, NULL);
15340
15341       if (info->dimen[dim].lower_bound)
15342         {
15343           /* If it is the default value, omit it.  */
15344           if ((is_c_family () || is_java ())
15345               && integer_zerop (info->dimen[dim].lower_bound))
15346             ;
15347           else if (is_fortran ()
15348                    && integer_onep (info->dimen[dim].lower_bound))
15349             ;
15350           else
15351             add_descr_info_field (subrange_die, DW_AT_lower_bound,
15352                                   info->dimen[dim].lower_bound,
15353                                   info->base_decl);
15354         }
15355       if (info->dimen[dim].upper_bound)
15356         add_descr_info_field (subrange_die, DW_AT_upper_bound,
15357                               info->dimen[dim].upper_bound,
15358                               info->base_decl);
15359       if (info->dimen[dim].stride)
15360         add_descr_info_field (subrange_die, DW_AT_byte_stride,
15361                               info->dimen[dim].stride,
15362                               info->base_decl);
15363     }
15364
15365   gen_type_die (info->element_type, context_die);
15366   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
15367
15368   if (get_AT (array_die, DW_AT_name))
15369     add_pubtype (type, array_die);
15370 }
15371
15372 #if 0
15373 static void
15374 gen_entry_point_die (tree decl, dw_die_ref context_die)
15375 {
15376   tree origin = decl_ultimate_origin (decl);
15377   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
15378
15379   if (origin != NULL)
15380     add_abstract_origin_attribute (decl_die, origin);
15381   else
15382     {
15383       add_name_and_src_coords_attributes (decl_die, decl);
15384       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
15385                           0, 0, context_die);
15386     }
15387
15388   if (DECL_ABSTRACT (decl))
15389     equate_decl_number_to_die (decl, decl_die);
15390   else
15391     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
15392 }
15393 #endif
15394
15395 /* Walk through the list of incomplete types again, trying once more to
15396    emit full debugging info for them.  */
15397
15398 static void
15399 retry_incomplete_types (void)
15400 {
15401   int i;
15402
15403   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
15404     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
15405 }
15406
15407 /* Determine what tag to use for a record type.  */
15408
15409 static enum dwarf_tag
15410 record_type_tag (tree type)
15411 {
15412   if (! lang_hooks.types.classify_record)
15413     return DW_TAG_structure_type;
15414
15415   switch (lang_hooks.types.classify_record (type))
15416     {
15417     case RECORD_IS_STRUCT:
15418       return DW_TAG_structure_type;
15419
15420     case RECORD_IS_CLASS:
15421       return DW_TAG_class_type;
15422
15423     case RECORD_IS_INTERFACE:
15424       if (dwarf_version >= 3 || !dwarf_strict)
15425         return DW_TAG_interface_type;
15426       return DW_TAG_structure_type;
15427
15428     default:
15429       gcc_unreachable ();
15430     }
15431 }
15432
15433 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
15434    include all of the information about the enumeration values also. Each
15435    enumerated type name/value is listed as a child of the enumerated type
15436    DIE.  */
15437
15438 static dw_die_ref
15439 gen_enumeration_type_die (tree type, dw_die_ref context_die)
15440 {
15441   dw_die_ref type_die = lookup_type_die (type);
15442
15443   if (type_die == NULL)
15444     {
15445       type_die = new_die (DW_TAG_enumeration_type,
15446                           scope_die_for (type, context_die), type);
15447       equate_type_number_to_die (type, type_die);
15448       add_name_attribute (type_die, type_tag (type));
15449     }
15450   else if (! TYPE_SIZE (type))
15451     return type_die;
15452   else
15453     remove_AT (type_die, DW_AT_declaration);
15454
15455   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
15456      given enum type is incomplete, do not generate the DW_AT_byte_size
15457      attribute or the DW_AT_element_list attribute.  */
15458   if (TYPE_SIZE (type))
15459     {
15460       tree link;
15461
15462       TREE_ASM_WRITTEN (type) = 1;
15463       add_byte_size_attribute (type_die, type);
15464       if (TYPE_STUB_DECL (type) != NULL_TREE)
15465         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
15466
15467       /* If the first reference to this type was as the return type of an
15468          inline function, then it may not have a parent.  Fix this now.  */
15469       if (type_die->die_parent == NULL)
15470         add_child_die (scope_die_for (type, context_die), type_die);
15471
15472       for (link = TYPE_VALUES (type);
15473            link != NULL; link = TREE_CHAIN (link))
15474         {
15475           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
15476           tree value = TREE_VALUE (link);
15477
15478           add_name_attribute (enum_die,
15479                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
15480
15481           if (TREE_CODE (value) == CONST_DECL)
15482             value = DECL_INITIAL (value);
15483
15484           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
15485             /* DWARF2 does not provide a way of indicating whether or
15486                not enumeration constants are signed or unsigned.  GDB
15487                always assumes the values are signed, so we output all
15488                values as if they were signed.  That means that
15489                enumeration constants with very large unsigned values
15490                will appear to have negative values in the debugger.  */
15491             add_AT_int (enum_die, DW_AT_const_value,
15492                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
15493         }
15494     }
15495   else
15496     add_AT_flag (type_die, DW_AT_declaration, 1);
15497
15498   if (get_AT (type_die, DW_AT_name))
15499     add_pubtype (type, type_die);
15500
15501   return type_die;
15502 }
15503
15504 /* Generate a DIE to represent either a real live formal parameter decl or to
15505    represent just the type of some formal parameter position in some function
15506    type.
15507
15508    Note that this routine is a bit unusual because its argument may be a
15509    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
15510    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
15511    node.  If it's the former then this function is being called to output a
15512    DIE to represent a formal parameter object (or some inlining thereof).  If
15513    it's the latter, then this function is only being called to output a
15514    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
15515    argument type of some subprogram type.
15516    If EMIT_NAME_P is true, name and source coordinate attributes
15517    are emitted.  */
15518
15519 static dw_die_ref
15520 gen_formal_parameter_die (tree node, tree origin, bool emit_name_p,
15521                           dw_die_ref context_die)
15522 {
15523   tree node_or_origin = node ? node : origin;
15524   dw_die_ref parm_die
15525     = new_die (DW_TAG_formal_parameter, context_die, node);
15526
15527   switch (TREE_CODE_CLASS (TREE_CODE (node_or_origin)))
15528     {
15529     case tcc_declaration:
15530       if (!origin)
15531         origin = decl_ultimate_origin (node);
15532       if (origin != NULL)
15533         add_abstract_origin_attribute (parm_die, origin);
15534       else
15535         {
15536           tree type = TREE_TYPE (node);
15537           if (emit_name_p)
15538             add_name_and_src_coords_attributes (parm_die, node);
15539           if (decl_by_reference_p (node))
15540             add_type_attribute (parm_die, TREE_TYPE (type), 0, 0,
15541                                 context_die);
15542           else
15543             add_type_attribute (parm_die, type,
15544                                 TREE_READONLY (node),
15545                                 TREE_THIS_VOLATILE (node),
15546                                 context_die);
15547           if (DECL_ARTIFICIAL (node))
15548             add_AT_flag (parm_die, DW_AT_artificial, 1);
15549         }
15550
15551       if (node && node != origin)
15552         equate_decl_number_to_die (node, parm_die);
15553       if (! DECL_ABSTRACT (node_or_origin))
15554         add_location_or_const_value_attribute (parm_die, node_or_origin,
15555                                                DW_AT_location);
15556
15557       break;
15558
15559     case tcc_type:
15560       /* We were called with some kind of a ..._TYPE node.  */
15561       add_type_attribute (parm_die, node_or_origin, 0, 0, context_die);
15562       break;
15563
15564     default:
15565       gcc_unreachable ();
15566     }
15567
15568   return parm_die;
15569 }
15570
15571 /* Generate and return a DW_TAG_GNU_formal_parameter_pack. Also generate
15572    children DW_TAG_formal_parameter DIEs representing the arguments of the
15573    parameter pack.
15574
15575    PARM_PACK must be a function parameter pack.
15576    PACK_ARG is the first argument of the parameter pack. Its TREE_CHAIN
15577    must point to the subsequent arguments of the function PACK_ARG belongs to.
15578    SUBR_DIE is the DIE of the function PACK_ARG belongs to.
15579    If NEXT_ARG is non NULL, *NEXT_ARG is set to the function argument
15580    following the last one for which a DIE was generated.  */
15581
15582 static dw_die_ref
15583 gen_formal_parameter_pack_die  (tree parm_pack,
15584                                 tree pack_arg,
15585                                 dw_die_ref subr_die,
15586                                 tree *next_arg)
15587 {
15588   tree arg;
15589   dw_die_ref parm_pack_die;
15590
15591   gcc_assert (parm_pack
15592               && lang_hooks.function_parameter_pack_p (parm_pack)
15593               && DECL_NAME (parm_pack)
15594               && subr_die);
15595
15596   parm_pack_die = new_die (DW_TAG_GNU_formal_parameter_pack, subr_die, parm_pack);
15597   add_AT_string (parm_pack_die, DW_AT_name,
15598                  IDENTIFIER_POINTER (DECL_NAME (parm_pack)));
15599
15600   for (arg = pack_arg; arg; arg = TREE_CHAIN (arg))
15601     {
15602       if (! lang_hooks.decls.function_parm_expanded_from_pack_p (arg,
15603                                                                  parm_pack))
15604         break;
15605       gen_formal_parameter_die (arg, NULL,
15606                                 false /* Don't emit name attribute.  */,
15607                                 parm_pack_die);
15608     }
15609   if (next_arg)
15610     *next_arg = arg;
15611   return parm_pack_die;
15612 }
15613
15614 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
15615    at the end of an (ANSI prototyped) formal parameters list.  */
15616
15617 static void
15618 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
15619 {
15620   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
15621 }
15622
15623 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
15624    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
15625    parameters as specified in some function type specification (except for
15626    those which appear as part of a function *definition*).  */
15627
15628 static void
15629 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
15630 {
15631   tree link;
15632   tree formal_type = NULL;
15633   tree first_parm_type;
15634   tree arg;
15635
15636   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
15637     {
15638       arg = DECL_ARGUMENTS (function_or_method_type);
15639       function_or_method_type = TREE_TYPE (function_or_method_type);
15640     }
15641   else
15642     arg = NULL_TREE;
15643
15644   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
15645
15646   /* Make our first pass over the list of formal parameter types and output a
15647      DW_TAG_formal_parameter DIE for each one.  */
15648   for (link = first_parm_type; link; )
15649     {
15650       dw_die_ref parm_die;
15651
15652       formal_type = TREE_VALUE (link);
15653       if (formal_type == void_type_node)
15654         break;
15655
15656       /* Output a (nameless) DIE to represent the formal parameter itself.  */
15657       parm_die = gen_formal_parameter_die (formal_type, NULL,
15658                                            true /* Emit name attribute.  */,
15659                                            context_die);
15660       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
15661            && link == first_parm_type)
15662           || (arg && DECL_ARTIFICIAL (arg)))
15663         add_AT_flag (parm_die, DW_AT_artificial, 1);
15664
15665       link = TREE_CHAIN (link);
15666       if (arg)
15667         arg = TREE_CHAIN (arg);
15668     }
15669
15670   /* If this function type has an ellipsis, add a
15671      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
15672   if (formal_type != void_type_node)
15673     gen_unspecified_parameters_die (function_or_method_type, context_die);
15674
15675   /* Make our second (and final) pass over the list of formal parameter types
15676      and output DIEs to represent those types (as necessary).  */
15677   for (link = TYPE_ARG_TYPES (function_or_method_type);
15678        link && TREE_VALUE (link);
15679        link = TREE_CHAIN (link))
15680     gen_type_die (TREE_VALUE (link), context_die);
15681 }
15682
15683 /* We want to generate the DIE for TYPE so that we can generate the
15684    die for MEMBER, which has been defined; we will need to refer back
15685    to the member declaration nested within TYPE.  If we're trying to
15686    generate minimal debug info for TYPE, processing TYPE won't do the
15687    trick; we need to attach the member declaration by hand.  */
15688
15689 static void
15690 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
15691 {
15692   gen_type_die (type, context_die);
15693
15694   /* If we're trying to avoid duplicate debug info, we may not have
15695      emitted the member decl for this function.  Emit it now.  */
15696   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
15697       && ! lookup_decl_die (member))
15698     {
15699       dw_die_ref type_die;
15700       gcc_assert (!decl_ultimate_origin (member));
15701
15702       push_decl_scope (type);
15703       type_die = lookup_type_die (type);
15704       if (TREE_CODE (member) == FUNCTION_DECL)
15705         gen_subprogram_die (member, type_die);
15706       else if (TREE_CODE (member) == FIELD_DECL)
15707         {
15708           /* Ignore the nameless fields that are used to skip bits but handle
15709              C++ anonymous unions and structs.  */
15710           if (DECL_NAME (member) != NULL_TREE
15711               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
15712               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
15713             {
15714               gen_type_die (member_declared_type (member), type_die);
15715               gen_field_die (member, type_die);
15716             }
15717         }
15718       else
15719         gen_variable_die (member, NULL_TREE, type_die);
15720
15721       pop_decl_scope ();
15722     }
15723 }
15724
15725 /* Generate the DWARF2 info for the "abstract" instance of a function which we
15726    may later generate inlined and/or out-of-line instances of.  */
15727
15728 static void
15729 dwarf2out_abstract_function (tree decl)
15730 {
15731   dw_die_ref old_die;
15732   tree save_fn;
15733   tree context;
15734   int was_abstract = DECL_ABSTRACT (decl);
15735   htab_t old_decl_loc_table;
15736
15737   /* Make sure we have the actual abstract inline, not a clone.  */
15738   decl = DECL_ORIGIN (decl);
15739
15740   old_die = lookup_decl_die (decl);
15741   if (old_die && get_AT (old_die, DW_AT_inline))
15742     /* We've already generated the abstract instance.  */
15743     return;
15744
15745   /* We can be called while recursively when seeing block defining inlined subroutine
15746      DIE.  Be sure to not clobber the outer location table nor use it or we would
15747      get locations in abstract instantces.  */
15748   old_decl_loc_table = decl_loc_table;
15749   decl_loc_table = NULL;
15750
15751   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
15752      we don't get confused by DECL_ABSTRACT.  */
15753   if (debug_info_level > DINFO_LEVEL_TERSE)
15754     {
15755       context = decl_class_context (decl);
15756       if (context)
15757         gen_type_die_for_member
15758           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
15759     }
15760
15761   /* Pretend we've just finished compiling this function.  */
15762   save_fn = current_function_decl;
15763   current_function_decl = decl;
15764   push_cfun (DECL_STRUCT_FUNCTION (decl));
15765
15766   set_decl_abstract_flags (decl, 1);
15767   dwarf2out_decl (decl);
15768   if (! was_abstract)
15769     set_decl_abstract_flags (decl, 0);
15770
15771   current_function_decl = save_fn;
15772   decl_loc_table = old_decl_loc_table;
15773   pop_cfun ();
15774 }
15775
15776 /* Helper function of premark_used_types() which gets called through
15777    htab_traverse.
15778
15779    Marks the DIE of a given type in *SLOT as perennial, so it never gets
15780    marked as unused by prune_unused_types.  */
15781
15782 static int
15783 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
15784 {
15785   tree type;
15786   dw_die_ref die;
15787
15788   type = (tree) *slot;
15789   die = lookup_type_die (type);
15790   if (die != NULL)
15791     die->die_perennial_p = 1;
15792   return 1;
15793 }
15794
15795 /* Helper function of premark_types_used_by_global_vars which gets called
15796    through htab_traverse.
15797
15798    Marks the DIE of a given type in *SLOT as perennial, so it never gets
15799    marked as unused by prune_unused_types. The DIE of the type is marked
15800    only if the global variable using the type will actually be emitted.  */
15801
15802 static int
15803 premark_types_used_by_global_vars_helper (void **slot,
15804                                           void *data ATTRIBUTE_UNUSED)
15805 {
15806   struct types_used_by_vars_entry *entry;
15807   dw_die_ref die;
15808
15809   entry = (struct types_used_by_vars_entry *) *slot;
15810   gcc_assert (entry->type != NULL
15811               && entry->var_decl != NULL);
15812   die = lookup_type_die (entry->type);
15813   if (die)
15814     {
15815       /* Ask cgraph if the global variable really is to be emitted.
15816          If yes, then we'll keep the DIE of ENTRY->TYPE.  */
15817       struct varpool_node *node = varpool_node (entry->var_decl);
15818       if (node->needed)
15819         {
15820           die->die_perennial_p = 1;
15821           /* Keep the parent DIEs as well.  */
15822           while ((die = die->die_parent) && die->die_perennial_p == 0)
15823             die->die_perennial_p = 1;
15824         }
15825     }
15826   return 1;
15827 }
15828
15829 /* Mark all members of used_types_hash as perennial.  */
15830
15831 static void
15832 premark_used_types (void)
15833 {
15834   if (cfun && cfun->used_types_hash)
15835     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
15836 }
15837
15838 /* Mark all members of types_used_by_vars_entry as perennial.  */
15839
15840 static void
15841 premark_types_used_by_global_vars (void)
15842 {
15843   if (types_used_by_vars_hash)
15844     htab_traverse (types_used_by_vars_hash,
15845                    premark_types_used_by_global_vars_helper, NULL);
15846 }
15847
15848 /* Generate a DIE to represent a declared function (either file-scope or
15849    block-local).  */
15850
15851 static void
15852 gen_subprogram_die (tree decl, dw_die_ref context_die)
15853 {
15854   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
15855   tree origin = decl_ultimate_origin (decl);
15856   dw_die_ref subr_die;
15857   tree fn_arg_types;
15858   tree outer_scope;
15859   dw_die_ref old_die = lookup_decl_die (decl);
15860   int declaration = (current_function_decl != decl
15861                      || class_or_namespace_scope_p (context_die));
15862
15863   premark_used_types ();
15864
15865   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
15866      started to generate the abstract instance of an inline, decided to output
15867      its containing class, and proceeded to emit the declaration of the inline
15868      from the member list for the class.  If so, DECLARATION takes priority;
15869      we'll get back to the abstract instance when done with the class.  */
15870
15871   /* The class-scope declaration DIE must be the primary DIE.  */
15872   if (origin && declaration && class_or_namespace_scope_p (context_die))
15873     {
15874       origin = NULL;
15875       gcc_assert (!old_die);
15876     }
15877
15878   /* Now that the C++ front end lazily declares artificial member fns, we
15879      might need to retrofit the declaration into its class.  */
15880   if (!declaration && !origin && !old_die
15881       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
15882       && !class_or_namespace_scope_p (context_die)
15883       && debug_info_level > DINFO_LEVEL_TERSE)
15884     old_die = force_decl_die (decl);
15885
15886   if (origin != NULL)
15887     {
15888       gcc_assert (!declaration || local_scope_p (context_die));
15889
15890       /* Fixup die_parent for the abstract instance of a nested
15891          inline function.  */
15892       if (old_die && old_die->die_parent == NULL)
15893         add_child_die (context_die, old_die);
15894
15895       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15896       add_abstract_origin_attribute (subr_die, origin);
15897     }
15898   else if (old_die)
15899     {
15900       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
15901       struct dwarf_file_data * file_index = lookup_filename (s.file);
15902
15903       if (!get_AT_flag (old_die, DW_AT_declaration)
15904           /* We can have a normal definition following an inline one in the
15905              case of redefinition of GNU C extern inlines.
15906              It seems reasonable to use AT_specification in this case.  */
15907           && !get_AT (old_die, DW_AT_inline))
15908         {
15909           /* Detect and ignore this case, where we are trying to output
15910              something we have already output.  */
15911           return;
15912         }
15913
15914       /* If the definition comes from the same place as the declaration,
15915          maybe use the old DIE.  We always want the DIE for this function
15916          that has the *_pc attributes to be under comp_unit_die so the
15917          debugger can find it.  We also need to do this for abstract
15918          instances of inlines, since the spec requires the out-of-line copy
15919          to have the same parent.  For local class methods, this doesn't
15920          apply; we just use the old DIE.  */
15921       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
15922           && (DECL_ARTIFICIAL (decl)
15923               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
15924                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
15925                       == (unsigned) s.line))))
15926         {
15927           subr_die = old_die;
15928
15929           /* Clear out the declaration attribute and the formal parameters.
15930              Do not remove all children, because it is possible that this
15931              declaration die was forced using force_decl_die(). In such
15932              cases die that forced declaration die (e.g. TAG_imported_module)
15933              is one of the children that we do not want to remove.  */
15934           remove_AT (subr_die, DW_AT_declaration);
15935           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
15936         }
15937       else
15938         {
15939           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15940           add_AT_specification (subr_die, old_die);
15941           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
15942             add_AT_file (subr_die, DW_AT_decl_file, file_index);
15943           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
15944             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
15945         }
15946     }
15947   else
15948     {
15949       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
15950
15951       if (TREE_PUBLIC (decl))
15952         add_AT_flag (subr_die, DW_AT_external, 1);
15953
15954       add_name_and_src_coords_attributes (subr_die, decl);
15955       if (debug_info_level > DINFO_LEVEL_TERSE)
15956         {
15957           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
15958           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
15959                               0, 0, context_die);
15960         }
15961
15962       add_pure_or_virtual_attribute (subr_die, decl);
15963       if (DECL_ARTIFICIAL (decl))
15964         add_AT_flag (subr_die, DW_AT_artificial, 1);
15965
15966       if (TREE_PROTECTED (decl))
15967         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
15968       else if (TREE_PRIVATE (decl))
15969         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
15970     }
15971
15972   if (declaration)
15973     {
15974       if (!old_die || !get_AT (old_die, DW_AT_inline))
15975         {
15976           add_AT_flag (subr_die, DW_AT_declaration, 1);
15977
15978           /* If this is an explicit function declaration then generate
15979              a DW_AT_explicit attribute.  */
15980           if (lang_hooks.decls.function_decl_explicit_p (decl)
15981               && (dwarf_version >= 3 || !dwarf_strict))
15982             add_AT_flag (subr_die, DW_AT_explicit, 1);
15983
15984           /* The first time we see a member function, it is in the context of
15985              the class to which it belongs.  We make sure of this by emitting
15986              the class first.  The next time is the definition, which is
15987              handled above.  The two may come from the same source text.
15988
15989              Note that force_decl_die() forces function declaration die. It is
15990              later reused to represent definition.  */
15991           equate_decl_number_to_die (decl, subr_die);
15992         }
15993     }
15994   else if (DECL_ABSTRACT (decl))
15995     {
15996       if (DECL_DECLARED_INLINE_P (decl))
15997         {
15998           if (cgraph_function_possibly_inlined_p (decl))
15999             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
16000           else
16001             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
16002         }
16003       else
16004         {
16005           if (cgraph_function_possibly_inlined_p (decl))
16006             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
16007           else
16008             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
16009         }
16010
16011       if (DECL_DECLARED_INLINE_P (decl)
16012           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
16013         add_AT_flag (subr_die, DW_AT_artificial, 1);
16014
16015       equate_decl_number_to_die (decl, subr_die);
16016     }
16017   else if (!DECL_EXTERNAL (decl))
16018     {
16019       HOST_WIDE_INT cfa_fb_offset;
16020
16021       if (!old_die || !get_AT (old_die, DW_AT_inline))
16022         equate_decl_number_to_die (decl, subr_die);
16023
16024       if (!flag_reorder_blocks_and_partition)
16025         {
16026           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
16027                                        current_function_funcdef_no);
16028           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
16029           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
16030                                        current_function_funcdef_no);
16031           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
16032
16033           add_pubname (decl, subr_die);
16034           add_arange (decl, subr_die);
16035         }
16036       else
16037         {  /* Do nothing for now; maybe need to duplicate die, one for
16038               hot section and one for cold section, then use the hot/cold
16039               section begin/end labels to generate the aranges...  */
16040           /*
16041             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
16042             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
16043             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
16044             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
16045
16046             add_pubname (decl, subr_die);
16047             add_arange (decl, subr_die);
16048             add_arange (decl, subr_die);
16049            */
16050         }
16051
16052 #ifdef MIPS_DEBUGGING_INFO
16053       /* Add a reference to the FDE for this routine.  */
16054       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
16055 #endif
16056
16057       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
16058
16059       /* We define the "frame base" as the function's CFA.  This is more
16060          convenient for several reasons: (1) It's stable across the prologue
16061          and epilogue, which makes it better than just a frame pointer,
16062          (2) With dwarf3, there exists a one-byte encoding that allows us
16063          to reference the .debug_frame data by proxy, but failing that,
16064          (3) We can at least reuse the code inspection and interpretation
16065          code that determines the CFA position at various points in the
16066          function.  */
16067       if (dwarf_version >= 3)
16068         {
16069           dw_loc_descr_ref op = new_loc_descr (DW_OP_call_frame_cfa, 0, 0);
16070           add_AT_loc (subr_die, DW_AT_frame_base, op);
16071         }
16072       else
16073         {
16074           dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
16075           if (list->dw_loc_next)
16076             add_AT_loc_list (subr_die, DW_AT_frame_base, list);
16077           else
16078             add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
16079         }
16080
16081       /* Compute a displacement from the "steady-state frame pointer" to
16082          the CFA.  The former is what all stack slots and argument slots
16083          will reference in the rtl; the later is what we've told the
16084          debugger about.  We'll need to adjust all frame_base references
16085          by this displacement.  */
16086       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
16087
16088       if (cfun->static_chain_decl)
16089         add_AT_location_description (subr_die, DW_AT_static_link,
16090                  loc_list_from_tree (cfun->static_chain_decl, 2));
16091     }
16092
16093   /* Generate child dies for template paramaters.  */
16094   if (debug_info_level > DINFO_LEVEL_TERSE)
16095     gen_generic_params_dies (decl);
16096
16097   /* Now output descriptions of the arguments for this function. This gets
16098      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
16099      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
16100      `...' at the end of the formal parameter list.  In order to find out if
16101      there was a trailing ellipsis or not, we must instead look at the type
16102      associated with the FUNCTION_DECL.  This will be a node of type
16103      FUNCTION_TYPE. If the chain of type nodes hanging off of this
16104      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
16105      an ellipsis at the end.  */
16106
16107   /* In the case where we are describing a mere function declaration, all we
16108      need to do here (and all we *can* do here) is to describe the *types* of
16109      its formal parameters.  */
16110   if (debug_info_level <= DINFO_LEVEL_TERSE)
16111     ;
16112   else if (declaration)
16113     gen_formal_types_die (decl, subr_die);
16114   else
16115     {
16116       /* Generate DIEs to represent all known formal parameters.  */
16117       tree parm = DECL_ARGUMENTS (decl);
16118       tree generic_decl = lang_hooks.decls.get_generic_function_decl (decl);
16119       tree generic_decl_parm = generic_decl
16120                                 ? DECL_ARGUMENTS (generic_decl)
16121                                 : NULL;
16122
16123       /* Now we want to walk the list of parameters of the function and
16124          emit their relevant DIEs.
16125
16126          We consider the case of DECL being an instance of a generic function
16127          as well as it being a normal function.
16128
16129          If DECL is an instance of a generic function we walk the
16130          parameters of the generic function declaration _and_ the parameters of
16131          DECL itself. This is useful because we want to emit specific DIEs for
16132          function parameter packs and those are declared as part of the
16133          generic function declaration. In that particular case,
16134          the parameter pack yields a DW_TAG_GNU_formal_parameter_pack DIE.
16135          That DIE has children DIEs representing the set of arguments
16136          of the pack. Note that the set of pack arguments can be empty.
16137          In that case, the DW_TAG_GNU_formal_parameter_pack DIE will not have any
16138          children DIE.
16139         
16140          Otherwise, we just consider the parameters of DECL.  */
16141       while (generic_decl_parm || parm)
16142         {
16143           if (generic_decl_parm
16144               && lang_hooks.function_parameter_pack_p (generic_decl_parm))
16145             gen_formal_parameter_pack_die (generic_decl_parm,
16146                                            parm, subr_die,
16147                                            &parm);
16148           else if (parm)
16149             {
16150               gen_decl_die (parm, NULL, subr_die);
16151               parm = TREE_CHAIN (parm);
16152             }
16153
16154           if (generic_decl_parm)
16155             generic_decl_parm = TREE_CHAIN (generic_decl_parm);
16156         }
16157
16158       /* Decide whether we need an unspecified_parameters DIE at the end.
16159          There are 2 more cases to do this for: 1) the ansi ... declaration -
16160          this is detectable when the end of the arg list is not a
16161          void_type_node 2) an unprototyped function declaration (not a
16162          definition).  This just means that we have no info about the
16163          parameters at all.  */
16164       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
16165       if (fn_arg_types != NULL)
16166         {
16167           /* This is the prototyped case, check for....  */
16168           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
16169             gen_unspecified_parameters_die (decl, subr_die);
16170         }
16171       else if (DECL_INITIAL (decl) == NULL_TREE)
16172         gen_unspecified_parameters_die (decl, subr_die);
16173     }
16174
16175   /* Output Dwarf info for all of the stuff within the body of the function
16176      (if it has one - it may be just a declaration).  */
16177   outer_scope = DECL_INITIAL (decl);
16178
16179   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
16180      a function.  This BLOCK actually represents the outermost binding contour
16181      for the function, i.e. the contour in which the function's formal
16182      parameters and labels get declared. Curiously, it appears that the front
16183      end doesn't actually put the PARM_DECL nodes for the current function onto
16184      the BLOCK_VARS list for this outer scope, but are strung off of the
16185      DECL_ARGUMENTS list for the function instead.
16186
16187      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
16188      the LABEL_DECL nodes for the function however, and we output DWARF info
16189      for those in decls_for_scope.  Just within the `outer_scope' there will be
16190      a BLOCK node representing the function's outermost pair of curly braces,
16191      and any blocks used for the base and member initializers of a C++
16192      constructor function.  */
16193   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
16194     {
16195       /* Emit a DW_TAG_variable DIE for a named return value.  */
16196       if (DECL_NAME (DECL_RESULT (decl)))
16197         gen_decl_die (DECL_RESULT (decl), NULL, subr_die);
16198
16199       current_function_has_inlines = 0;
16200       decls_for_scope (outer_scope, subr_die, 0);
16201
16202 #if 0 && defined (MIPS_DEBUGGING_INFO)
16203       if (current_function_has_inlines)
16204         {
16205           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
16206           if (! comp_unit_has_inlines)
16207             {
16208               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
16209               comp_unit_has_inlines = 1;
16210             }
16211         }
16212 #endif
16213     }
16214   /* Add the calling convention attribute if requested.  */
16215   add_calling_convention_attribute (subr_die, decl);
16216
16217 }
16218
16219 /* Returns a hash value for X (which really is a die_struct).  */
16220
16221 static hashval_t
16222 common_block_die_table_hash (const void *x)
16223 {
16224   const_dw_die_ref d = (const_dw_die_ref) x;
16225   return (hashval_t) d->decl_id ^ htab_hash_pointer (d->die_parent);
16226 }
16227
16228 /* Return nonzero if decl_id and die_parent of die_struct X is the same
16229    as decl_id and die_parent of die_struct Y.  */
16230
16231 static int
16232 common_block_die_table_eq (const void *x, const void *y)
16233 {
16234   const_dw_die_ref d = (const_dw_die_ref) x;
16235   const_dw_die_ref e = (const_dw_die_ref) y;
16236   return d->decl_id == e->decl_id && d->die_parent == e->die_parent;
16237 }
16238
16239 /* Generate a DIE to represent a declared data object.
16240    Either DECL or ORIGIN must be non-null.  */
16241
16242 static void
16243 gen_variable_die (tree decl, tree origin, dw_die_ref context_die)
16244 {
16245   HOST_WIDE_INT off;
16246   tree com_decl;
16247   tree decl_or_origin = decl ? decl : origin;
16248   dw_die_ref var_die;
16249   dw_die_ref old_die = decl ? lookup_decl_die (decl) : NULL;
16250   dw_die_ref origin_die;
16251   int declaration = (DECL_EXTERNAL (decl_or_origin)
16252                      /* If DECL is COMDAT and has not actually been
16253                         emitted, we cannot take its address; there
16254                         might end up being no definition anywhere in
16255                         the program.  For example, consider the C++
16256                         test case:
16257
16258                           template <class T>
16259                           struct S { static const int i = 7; };
16260
16261                           template <class T>
16262                           const int S<T>::i;
16263
16264                           int f() { return S<int>::i; }
16265
16266                         Here, S<int>::i is not DECL_EXTERNAL, but no
16267                         definition is required, so the compiler will
16268                         not emit a definition.  */
16269                      || (TREE_CODE (decl_or_origin) == VAR_DECL
16270                          && DECL_COMDAT (decl_or_origin)
16271                          && !TREE_ASM_WRITTEN (decl_or_origin))
16272                      || class_or_namespace_scope_p (context_die));
16273
16274   if (!origin)
16275     origin = decl_ultimate_origin (decl);
16276
16277   com_decl = fortran_common (decl_or_origin, &off);
16278
16279   /* Symbol in common gets emitted as a child of the common block, in the form
16280      of a data member.  */
16281   if (com_decl)
16282     {
16283       tree field;
16284       dw_die_ref com_die;
16285       dw_loc_list_ref loc;
16286       die_node com_die_arg;
16287
16288       var_die = lookup_decl_die (decl_or_origin);
16289       if (var_die)
16290         {
16291           if (get_AT (var_die, DW_AT_location) == NULL)
16292             {
16293               loc = loc_list_from_tree (com_decl, off ? 1 : 2);
16294               if (loc)
16295                 {
16296                   if (off)
16297                     {
16298                       /* Optimize the common case.  */
16299                       if (single_element_loc_list_p (loc)
16300                           && loc->expr->dw_loc_opc == DW_OP_addr
16301                           && loc->expr->dw_loc_next == NULL
16302                           && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr)
16303                              == SYMBOL_REF)
16304                         loc->expr->dw_loc_oprnd1.v.val_addr
16305                           = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
16306                         else
16307                           loc_list_plus_const (loc, off);
16308                     }
16309                   add_AT_location_description (var_die, DW_AT_location, loc);
16310                   remove_AT (var_die, DW_AT_declaration);
16311                 }
16312             }
16313           return;
16314         }
16315
16316       if (common_block_die_table == NULL)
16317         common_block_die_table
16318           = htab_create_ggc (10, common_block_die_table_hash,
16319                              common_block_die_table_eq, NULL);
16320
16321       field = TREE_OPERAND (DECL_VALUE_EXPR (decl), 0);
16322       com_die_arg.decl_id = DECL_UID (com_decl);
16323       com_die_arg.die_parent = context_die;
16324       com_die = (dw_die_ref) htab_find (common_block_die_table, &com_die_arg);
16325       loc = loc_list_from_tree (com_decl, 2);
16326       if (com_die == NULL)
16327         {
16328           const char *cnam
16329             = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (com_decl));
16330           void **slot;
16331
16332           com_die = new_die (DW_TAG_common_block, context_die, decl);
16333           add_name_and_src_coords_attributes (com_die, com_decl);
16334           if (loc)
16335             {
16336               add_AT_location_description (com_die, DW_AT_location, loc);
16337               /* Avoid sharing the same loc descriptor between
16338                  DW_TAG_common_block and DW_TAG_variable.  */
16339               loc = loc_list_from_tree (com_decl, 2);
16340             }
16341           else if (DECL_EXTERNAL (decl))
16342             add_AT_flag (com_die, DW_AT_declaration, 1);
16343           add_pubname_string (cnam, com_die); /* ??? needed? */
16344           com_die->decl_id = DECL_UID (com_decl);
16345           slot = htab_find_slot (common_block_die_table, com_die, INSERT);
16346           *slot = (void *) com_die;
16347         }
16348       else if (get_AT (com_die, DW_AT_location) == NULL && loc)
16349         {
16350           add_AT_location_description (com_die, DW_AT_location, loc);
16351           loc = loc_list_from_tree (com_decl, 2);
16352           remove_AT (com_die, DW_AT_declaration);
16353         }
16354       var_die = new_die (DW_TAG_variable, com_die, decl);
16355       add_name_and_src_coords_attributes (var_die, decl);
16356       add_type_attribute (var_die, TREE_TYPE (decl), TREE_READONLY (decl),
16357                           TREE_THIS_VOLATILE (decl), context_die);
16358       add_AT_flag (var_die, DW_AT_external, 1);
16359       if (loc)
16360         {
16361           if (off)
16362             {
16363               /* Optimize the common case.  */
16364               if (single_element_loc_list_p (loc)
16365                   && loc->expr->dw_loc_opc == DW_OP_addr
16366                   && loc->expr->dw_loc_next == NULL
16367                   && GET_CODE (loc->expr->dw_loc_oprnd1.v.val_addr) == SYMBOL_REF)
16368                 loc->expr->dw_loc_oprnd1.v.val_addr
16369                   = plus_constant (loc->expr->dw_loc_oprnd1.v.val_addr, off);
16370               else
16371                 loc_list_plus_const (loc, off);
16372             }
16373           add_AT_location_description (var_die, DW_AT_location, loc);
16374         }
16375       else if (DECL_EXTERNAL (decl))
16376         add_AT_flag (var_die, DW_AT_declaration, 1);
16377       equate_decl_number_to_die (decl, var_die);
16378       return;
16379     }
16380
16381   /* If the compiler emitted a definition for the DECL declaration
16382      and if we already emitted a DIE for it, don't emit a second
16383      DIE for it again.  */
16384   if (old_die
16385       && declaration
16386       && old_die->die_parent == context_die)
16387     return;
16388
16389   /* For static data members, the declaration in the class is supposed
16390      to have DW_TAG_member tag; the specification should still be
16391      DW_TAG_variable referencing the DW_TAG_member DIE.  */
16392   if (declaration && class_scope_p (context_die))
16393     var_die = new_die (DW_TAG_member, context_die, decl);
16394   else
16395     var_die = new_die (DW_TAG_variable, context_die, decl);
16396
16397   origin_die = NULL;
16398   if (origin != NULL)
16399     origin_die = add_abstract_origin_attribute (var_die, origin);
16400
16401   /* Loop unrolling can create multiple blocks that refer to the same
16402      static variable, so we must test for the DW_AT_declaration flag.
16403
16404      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
16405      copy decls and set the DECL_ABSTRACT flag on them instead of
16406      sharing them.
16407
16408      ??? Duplicated blocks have been rewritten to use .debug_ranges.
16409
16410      ??? The declare_in_namespace support causes us to get two DIEs for one
16411      variable, both of which are declarations.  We want to avoid considering
16412      one to be a specification, so we must test that this DIE is not a
16413      declaration.  */
16414   else if (old_die && TREE_STATIC (decl) && ! declaration
16415            && get_AT_flag (old_die, DW_AT_declaration) == 1)
16416     {
16417       /* This is a definition of a C++ class level static.  */
16418       add_AT_specification (var_die, old_die);
16419       if (DECL_NAME (decl))
16420         {
16421           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
16422           struct dwarf_file_data * file_index = lookup_filename (s.file);
16423
16424           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
16425             add_AT_file (var_die, DW_AT_decl_file, file_index);
16426
16427           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
16428             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
16429         }
16430     }
16431   else
16432     {
16433       tree type = TREE_TYPE (decl);
16434
16435       add_name_and_src_coords_attributes (var_die, decl);
16436       if (decl_by_reference_p (decl))
16437         add_type_attribute (var_die, TREE_TYPE (type), 0, 0, context_die);
16438       else
16439         add_type_attribute (var_die, type, TREE_READONLY (decl),
16440                             TREE_THIS_VOLATILE (decl), context_die);
16441
16442       if (TREE_PUBLIC (decl))
16443         add_AT_flag (var_die, DW_AT_external, 1);
16444
16445       if (DECL_ARTIFICIAL (decl))
16446         add_AT_flag (var_die, DW_AT_artificial, 1);
16447
16448       if (TREE_PROTECTED (decl))
16449         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
16450       else if (TREE_PRIVATE (decl))
16451         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
16452     }
16453
16454   if (declaration)
16455     add_AT_flag (var_die, DW_AT_declaration, 1);
16456
16457   if (decl && (DECL_ABSTRACT (decl) || declaration))
16458     equate_decl_number_to_die (decl, var_die);
16459
16460   if (! declaration
16461       && (! DECL_ABSTRACT (decl_or_origin)
16462           /* Local static vars are shared between all clones/inlines,
16463              so emit DW_AT_location on the abstract DIE if DECL_RTL is
16464              already set.  */
16465           || (TREE_CODE (decl_or_origin) == VAR_DECL
16466               && TREE_STATIC (decl_or_origin)
16467               && DECL_RTL_SET_P (decl_or_origin)))
16468       /* When abstract origin already has DW_AT_location attribute, no need
16469          to add it again.  */
16470       && (origin_die == NULL || get_AT (origin_die, DW_AT_location) == NULL))
16471     {
16472       if (TREE_CODE (decl_or_origin) == VAR_DECL && TREE_STATIC (decl_or_origin)
16473           && !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl_or_origin)))
16474         defer_location (decl_or_origin, var_die);
16475       else
16476         add_location_or_const_value_attribute (var_die,
16477                                                decl_or_origin,
16478                                                DW_AT_location);
16479       add_pubname (decl_or_origin, var_die);
16480     }
16481   else
16482     tree_add_const_value_attribute_for_decl (var_die, decl_or_origin);
16483 }
16484
16485 /* Generate a DIE to represent a named constant.  */
16486
16487 static void
16488 gen_const_die (tree decl, dw_die_ref context_die)
16489 {
16490   dw_die_ref const_die;
16491   tree type = TREE_TYPE (decl);
16492
16493   const_die = new_die (DW_TAG_constant, context_die, decl);
16494   add_name_and_src_coords_attributes (const_die, decl);
16495   add_type_attribute (const_die, type, 1, 0, context_die);
16496   if (TREE_PUBLIC (decl))
16497     add_AT_flag (const_die, DW_AT_external, 1);
16498   if (DECL_ARTIFICIAL (decl))
16499     add_AT_flag (const_die, DW_AT_artificial, 1);
16500   tree_add_const_value_attribute_for_decl (const_die, decl);
16501 }
16502
16503 /* Generate a DIE to represent a label identifier.  */
16504
16505 static void
16506 gen_label_die (tree decl, dw_die_ref context_die)
16507 {
16508   tree origin = decl_ultimate_origin (decl);
16509   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
16510   rtx insn;
16511   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16512
16513   if (origin != NULL)
16514     add_abstract_origin_attribute (lbl_die, origin);
16515   else
16516     add_name_and_src_coords_attributes (lbl_die, decl);
16517
16518   if (DECL_ABSTRACT (decl))
16519     equate_decl_number_to_die (decl, lbl_die);
16520   else
16521     {
16522       insn = DECL_RTL_IF_SET (decl);
16523
16524       /* Deleted labels are programmer specified labels which have been
16525          eliminated because of various optimizations.  We still emit them
16526          here so that it is possible to put breakpoints on them.  */
16527       if (insn
16528           && (LABEL_P (insn)
16529               || ((NOTE_P (insn)
16530                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
16531         {
16532           /* When optimization is enabled (via -O) some parts of the compiler
16533              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
16534              represent source-level labels which were explicitly declared by
16535              the user.  This really shouldn't be happening though, so catch
16536              it if it ever does happen.  */
16537           gcc_assert (!INSN_DELETED_P (insn));
16538
16539           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
16540           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
16541         }
16542     }
16543 }
16544
16545 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
16546    attributes to the DIE for a block STMT, to describe where the inlined
16547    function was called from.  This is similar to add_src_coords_attributes.  */
16548
16549 static inline void
16550 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
16551 {
16552   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
16553
16554   if (dwarf_version >= 3 || !dwarf_strict)
16555     {
16556       add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
16557       add_AT_unsigned (die, DW_AT_call_line, s.line);
16558     }
16559 }
16560
16561
16562 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
16563    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
16564
16565 static inline void
16566 add_high_low_attributes (tree stmt, dw_die_ref die)
16567 {
16568   char label[MAX_ARTIFICIAL_LABEL_BYTES];
16569
16570   if (BLOCK_FRAGMENT_CHAIN (stmt)
16571       && (dwarf_version >= 3 || !dwarf_strict))
16572     {
16573       tree chain;
16574
16575       if (inlined_function_outer_scope_p (stmt))
16576         {
16577           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
16578                                        BLOCK_NUMBER (stmt));
16579           add_AT_lbl_id (die, DW_AT_entry_pc, label);
16580         }
16581
16582       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
16583
16584       chain = BLOCK_FRAGMENT_CHAIN (stmt);
16585       do
16586         {
16587           add_ranges (chain);
16588           chain = BLOCK_FRAGMENT_CHAIN (chain);
16589         }
16590       while (chain);
16591       add_ranges (NULL);
16592     }
16593   else
16594     {
16595       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
16596                                    BLOCK_NUMBER (stmt));
16597       add_AT_lbl_id (die, DW_AT_low_pc, label);
16598       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
16599                                    BLOCK_NUMBER (stmt));
16600       add_AT_lbl_id (die, DW_AT_high_pc, label);
16601     }
16602 }
16603
16604 /* Generate a DIE for a lexical block.  */
16605
16606 static void
16607 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
16608 {
16609   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
16610
16611   if (! BLOCK_ABSTRACT (stmt) && TREE_ASM_WRITTEN (stmt))
16612     add_high_low_attributes (stmt, stmt_die);
16613
16614   decls_for_scope (stmt, stmt_die, depth);
16615 }
16616
16617 /* Generate a DIE for an inlined subprogram.  */
16618
16619 static void
16620 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
16621 {
16622   tree decl;
16623
16624   /* The instance of function that is effectively being inlined shall not
16625      be abstract.  */
16626   gcc_assert (! BLOCK_ABSTRACT (stmt));
16627
16628   decl = block_ultimate_origin (stmt);
16629
16630   /* Emit info for the abstract instance first, if we haven't yet.  We
16631      must emit this even if the block is abstract, otherwise when we
16632      emit the block below (or elsewhere), we may end up trying to emit
16633      a die whose origin die hasn't been emitted, and crashing.  */
16634   dwarf2out_abstract_function (decl);
16635
16636   if (! BLOCK_ABSTRACT (stmt))
16637     {
16638       dw_die_ref subr_die
16639         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
16640
16641       add_abstract_origin_attribute (subr_die, decl);
16642       if (TREE_ASM_WRITTEN (stmt))
16643         add_high_low_attributes (stmt, subr_die);
16644       add_call_src_coords_attributes (stmt, subr_die);
16645
16646       decls_for_scope (stmt, subr_die, depth);
16647       current_function_has_inlines = 1;
16648     }
16649 }
16650
16651 /* Generate a DIE for a field in a record, or structure.  */
16652
16653 static void
16654 gen_field_die (tree decl, dw_die_ref context_die)
16655 {
16656   dw_die_ref decl_die;
16657
16658   if (TREE_TYPE (decl) == error_mark_node)
16659     return;
16660
16661   decl_die = new_die (DW_TAG_member, context_die, decl);
16662   add_name_and_src_coords_attributes (decl_die, decl);
16663   add_type_attribute (decl_die, member_declared_type (decl),
16664                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
16665                       context_die);
16666
16667   if (DECL_BIT_FIELD_TYPE (decl))
16668     {
16669       add_byte_size_attribute (decl_die, decl);
16670       add_bit_size_attribute (decl_die, decl);
16671       add_bit_offset_attribute (decl_die, decl);
16672     }
16673
16674   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
16675     add_data_member_location_attribute (decl_die, decl);
16676
16677   if (DECL_ARTIFICIAL (decl))
16678     add_AT_flag (decl_die, DW_AT_artificial, 1);
16679
16680   if (TREE_PROTECTED (decl))
16681     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
16682   else if (TREE_PRIVATE (decl))
16683     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
16684
16685   /* Equate decl number to die, so that we can look up this decl later on.  */
16686   equate_decl_number_to_die (decl, decl_die);
16687 }
16688
16689 #if 0
16690 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16691    Use modified_type_die instead.
16692    We keep this code here just in case these types of DIEs may be needed to
16693    represent certain things in other languages (e.g. Pascal) someday.  */
16694
16695 static void
16696 gen_pointer_type_die (tree type, dw_die_ref context_die)
16697 {
16698   dw_die_ref ptr_die
16699     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
16700
16701   equate_type_number_to_die (type, ptr_die);
16702   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16703   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16704 }
16705
16706 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
16707    Use modified_type_die instead.
16708    We keep this code here just in case these types of DIEs may be needed to
16709    represent certain things in other languages (e.g. Pascal) someday.  */
16710
16711 static void
16712 gen_reference_type_die (tree type, dw_die_ref context_die)
16713 {
16714   dw_die_ref ref_die
16715     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
16716
16717   equate_type_number_to_die (type, ref_die);
16718   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
16719   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
16720 }
16721 #endif
16722
16723 /* Generate a DIE for a pointer to a member type.  */
16724
16725 static void
16726 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
16727 {
16728   dw_die_ref ptr_die
16729     = new_die (DW_TAG_ptr_to_member_type,
16730                scope_die_for (type, context_die), type);
16731
16732   equate_type_number_to_die (type, ptr_die);
16733   add_AT_die_ref (ptr_die, DW_AT_containing_type,
16734                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
16735   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
16736 }
16737
16738 /* Generate the DIE for the compilation unit.  */
16739
16740 static dw_die_ref
16741 gen_compile_unit_die (const char *filename)
16742 {
16743   dw_die_ref die;
16744   char producer[250];
16745   const char *language_string = lang_hooks.name;
16746   int language;
16747
16748   die = new_die (DW_TAG_compile_unit, NULL, NULL);
16749
16750   if (filename)
16751     {
16752       add_name_attribute (die, filename);
16753       /* Don't add cwd for <built-in>.  */
16754       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
16755         add_comp_dir_attribute (die);
16756     }
16757
16758   sprintf (producer, "%s %s", language_string, version_string);
16759
16760 #ifdef MIPS_DEBUGGING_INFO
16761   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
16762      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
16763      not appear in the producer string, the debugger reaches the conclusion
16764      that the object file is stripped and has no debugging information.
16765      To get the MIPS/SGI debugger to believe that there is debugging
16766      information in the object file, we add a -g to the producer string.  */
16767   if (debug_info_level > DINFO_LEVEL_TERSE)
16768     strcat (producer, " -g");
16769 #endif
16770
16771   add_AT_string (die, DW_AT_producer, producer);
16772
16773   language = DW_LANG_C89;
16774   if (strcmp (language_string, "GNU C++") == 0)
16775     language = DW_LANG_C_plus_plus;
16776   else if (strcmp (language_string, "GNU F77") == 0)
16777     language = DW_LANG_Fortran77;
16778   else if (strcmp (language_string, "GNU Pascal") == 0)
16779     language = DW_LANG_Pascal83;
16780   else if (dwarf_version >= 3 || !dwarf_strict)
16781     {
16782       if (strcmp (language_string, "GNU Ada") == 0)
16783         language = DW_LANG_Ada95;
16784       else if (strcmp (language_string, "GNU Fortran") == 0)
16785         language = DW_LANG_Fortran95;
16786       else if (strcmp (language_string, "GNU Java") == 0)
16787         language = DW_LANG_Java;
16788       else if (strcmp (language_string, "GNU Objective-C") == 0)
16789         language = DW_LANG_ObjC;
16790       else if (strcmp (language_string, "GNU Objective-C++") == 0)
16791         language = DW_LANG_ObjC_plus_plus;
16792     }
16793
16794   add_AT_unsigned (die, DW_AT_language, language);
16795   return die;
16796 }
16797
16798 /* Generate the DIE for a base class.  */
16799
16800 static void
16801 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
16802 {
16803   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
16804
16805   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
16806   add_data_member_location_attribute (die, binfo);
16807
16808   if (BINFO_VIRTUAL_P (binfo))
16809     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
16810
16811   if (access == access_public_node)
16812     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
16813   else if (access == access_protected_node)
16814     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
16815 }
16816
16817 /* Generate a DIE for a class member.  */
16818
16819 static void
16820 gen_member_die (tree type, dw_die_ref context_die)
16821 {
16822   tree member;
16823   tree binfo = TYPE_BINFO (type);
16824   dw_die_ref child;
16825
16826   /* If this is not an incomplete type, output descriptions of each of its
16827      members. Note that as we output the DIEs necessary to represent the
16828      members of this record or union type, we will also be trying to output
16829      DIEs to represent the *types* of those members. However the `type'
16830      function (above) will specifically avoid generating type DIEs for member
16831      types *within* the list of member DIEs for this (containing) type except
16832      for those types (of members) which are explicitly marked as also being
16833      members of this (containing) type themselves.  The g++ front- end can
16834      force any given type to be treated as a member of some other (containing)
16835      type by setting the TYPE_CONTEXT of the given (member) type to point to
16836      the TREE node representing the appropriate (containing) type.  */
16837
16838   /* First output info about the base classes.  */
16839   if (binfo)
16840     {
16841       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
16842       int i;
16843       tree base;
16844
16845       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
16846         gen_inheritance_die (base,
16847                              (accesses ? VEC_index (tree, accesses, i)
16848                               : access_public_node), context_die);
16849     }
16850
16851   /* Now output info about the data members and type members.  */
16852   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
16853     {
16854       /* If we thought we were generating minimal debug info for TYPE
16855          and then changed our minds, some of the member declarations
16856          may have already been defined.  Don't define them again, but
16857          do put them in the right order.  */
16858
16859       child = lookup_decl_die (member);
16860       if (child)
16861         splice_child_die (context_die, child);
16862       else
16863         gen_decl_die (member, NULL, context_die);
16864     }
16865
16866   /* Now output info about the function members (if any).  */
16867   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
16868     {
16869       /* Don't include clones in the member list.  */
16870       if (DECL_ABSTRACT_ORIGIN (member))
16871         continue;
16872
16873       child = lookup_decl_die (member);
16874       if (child)
16875         splice_child_die (context_die, child);
16876       else
16877         gen_decl_die (member, NULL, context_die);
16878     }
16879 }
16880
16881 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
16882    is set, we pretend that the type was never defined, so we only get the
16883    member DIEs needed by later specification DIEs.  */
16884
16885 static void
16886 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
16887                                 enum debug_info_usage usage)
16888 {
16889   dw_die_ref type_die = lookup_type_die (type);
16890   dw_die_ref scope_die = 0;
16891   int nested = 0;
16892   int complete = (TYPE_SIZE (type)
16893                   && (! TYPE_STUB_DECL (type)
16894                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
16895   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
16896   complete = complete && should_emit_struct_debug (type, usage);
16897
16898   if (type_die && ! complete)
16899     return;
16900
16901   if (TYPE_CONTEXT (type) != NULL_TREE
16902       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
16903           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
16904     nested = 1;
16905
16906   scope_die = scope_die_for (type, context_die);
16907
16908   if (! type_die || (nested && scope_die == comp_unit_die))
16909     /* First occurrence of type or toplevel definition of nested class.  */
16910     {
16911       dw_die_ref old_die = type_die;
16912
16913       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
16914                           ? record_type_tag (type) : DW_TAG_union_type,
16915                           scope_die, type);
16916       equate_type_number_to_die (type, type_die);
16917       if (old_die)
16918         add_AT_specification (type_die, old_die);
16919       else
16920         add_name_attribute (type_die, type_tag (type));
16921     }
16922   else
16923     remove_AT (type_die, DW_AT_declaration);
16924
16925   /* Generate child dies for template paramaters.  */
16926   if (debug_info_level > DINFO_LEVEL_TERSE
16927       && COMPLETE_TYPE_P (type))
16928     gen_generic_params_dies (type);
16929
16930   /* If this type has been completed, then give it a byte_size attribute and
16931      then give a list of members.  */
16932   if (complete && !ns_decl)
16933     {
16934       /* Prevent infinite recursion in cases where the type of some member of
16935          this type is expressed in terms of this type itself.  */
16936       TREE_ASM_WRITTEN (type) = 1;
16937       add_byte_size_attribute (type_die, type);
16938       if (TYPE_STUB_DECL (type) != NULL_TREE)
16939         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
16940
16941       /* If the first reference to this type was as the return type of an
16942          inline function, then it may not have a parent.  Fix this now.  */
16943       if (type_die->die_parent == NULL)
16944         add_child_die (scope_die, type_die);
16945
16946       push_decl_scope (type);
16947       gen_member_die (type, type_die);
16948       pop_decl_scope ();
16949
16950       /* GNU extension: Record what type our vtable lives in.  */
16951       if (TYPE_VFIELD (type))
16952         {
16953           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
16954
16955           gen_type_die (vtype, context_die);
16956           add_AT_die_ref (type_die, DW_AT_containing_type,
16957                           lookup_type_die (vtype));
16958         }
16959     }
16960   else
16961     {
16962       add_AT_flag (type_die, DW_AT_declaration, 1);
16963
16964       /* We don't need to do this for function-local types.  */
16965       if (TYPE_STUB_DECL (type)
16966           && ! decl_function_context (TYPE_STUB_DECL (type)))
16967         VEC_safe_push (tree, gc, incomplete_types, type);
16968     }
16969
16970   if (get_AT (type_die, DW_AT_name))
16971     add_pubtype (type, type_die);
16972 }
16973
16974 /* Generate a DIE for a subroutine _type_.  */
16975
16976 static void
16977 gen_subroutine_type_die (tree type, dw_die_ref context_die)
16978 {
16979   tree return_type = TREE_TYPE (type);
16980   dw_die_ref subr_die
16981     = new_die (DW_TAG_subroutine_type,
16982                scope_die_for (type, context_die), type);
16983
16984   equate_type_number_to_die (type, subr_die);
16985   add_prototyped_attribute (subr_die, type);
16986   add_type_attribute (subr_die, return_type, 0, 0, context_die);
16987   gen_formal_types_die (type, subr_die);
16988
16989   if (get_AT (subr_die, DW_AT_name))
16990     add_pubtype (type, subr_die);
16991 }
16992
16993 /* Generate a DIE for a type definition.  */
16994
16995 static void
16996 gen_typedef_die (tree decl, dw_die_ref context_die)
16997 {
16998   dw_die_ref type_die;
16999   tree origin;
17000
17001   if (TREE_ASM_WRITTEN (decl))
17002     return;
17003
17004   TREE_ASM_WRITTEN (decl) = 1;
17005   type_die = new_die (DW_TAG_typedef, context_die, decl);
17006   origin = decl_ultimate_origin (decl);
17007   if (origin != NULL)
17008     add_abstract_origin_attribute (type_die, origin);
17009   else
17010     {
17011       tree type;
17012
17013       add_name_and_src_coords_attributes (type_die, decl);
17014       if (DECL_ORIGINAL_TYPE (decl))
17015         {
17016           type = DECL_ORIGINAL_TYPE (decl);
17017
17018           gcc_assert (type != TREE_TYPE (decl));
17019           equate_type_number_to_die (TREE_TYPE (decl), type_die);
17020         }
17021       else
17022         type = TREE_TYPE (decl);
17023
17024       add_type_attribute (type_die, type, TREE_READONLY (decl),
17025                           TREE_THIS_VOLATILE (decl), context_die);
17026     }
17027
17028   if (DECL_ABSTRACT (decl))
17029     equate_decl_number_to_die (decl, type_die);
17030
17031   if (get_AT (type_die, DW_AT_name))
17032     add_pubtype (decl, type_die);
17033 }
17034
17035 /* Generate a type description DIE.  */
17036
17037 static void
17038 gen_type_die_with_usage (tree type, dw_die_ref context_die,
17039                                 enum debug_info_usage usage)
17040 {
17041   int need_pop;
17042   struct array_descr_info info;
17043
17044   if (type == NULL_TREE || type == error_mark_node)
17045     return;
17046
17047   /* If TYPE is a typedef type variant, let's generate debug info
17048      for the parent typedef which TYPE is a type of.  */
17049   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
17050       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
17051     {
17052       if (TREE_ASM_WRITTEN (type))
17053         return;
17054
17055       /* Prevent broken recursion; we can't hand off to the same type.  */
17056       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
17057
17058       /* Use the DIE of the containing namespace as the parent DIE of
17059          the type description DIE we want to generate.  */
17060       if (DECL_CONTEXT (TYPE_NAME (type))
17061           && TREE_CODE (DECL_CONTEXT (TYPE_NAME (type))) == NAMESPACE_DECL)
17062         context_die = get_context_die (DECL_CONTEXT (TYPE_NAME (type)));
17063
17064       TREE_ASM_WRITTEN (type) = 1;
17065       gen_decl_die (TYPE_NAME (type), NULL, context_die);
17066       return;
17067     }
17068
17069   /* If this is an array type with hidden descriptor, handle it first.  */
17070   if (!TREE_ASM_WRITTEN (type)
17071       && lang_hooks.types.get_array_descr_info
17072       && lang_hooks.types.get_array_descr_info (type, &info)
17073       && (dwarf_version >= 3 || !dwarf_strict))
17074     {
17075       gen_descr_array_type_die (type, &info, context_die);
17076       TREE_ASM_WRITTEN (type) = 1;
17077       return;
17078     }
17079
17080   /* We are going to output a DIE to represent the unqualified version
17081      of this type (i.e. without any const or volatile qualifiers) so
17082      get the main variant (i.e. the unqualified version) of this type
17083      now.  (Vectors are special because the debugging info is in the
17084      cloned type itself).  */
17085   if (TREE_CODE (type) != VECTOR_TYPE)
17086     type = type_main_variant (type);
17087
17088   if (TREE_ASM_WRITTEN (type))
17089     return;
17090
17091   switch (TREE_CODE (type))
17092     {
17093     case ERROR_MARK:
17094       break;
17095
17096     case POINTER_TYPE:
17097     case REFERENCE_TYPE:
17098       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
17099          ensures that the gen_type_die recursion will terminate even if the
17100          type is recursive.  Recursive types are possible in Ada.  */
17101       /* ??? We could perhaps do this for all types before the switch
17102          statement.  */
17103       TREE_ASM_WRITTEN (type) = 1;
17104
17105       /* For these types, all that is required is that we output a DIE (or a
17106          set of DIEs) to represent the "basis" type.  */
17107       gen_type_die_with_usage (TREE_TYPE (type), context_die,
17108                                 DINFO_USAGE_IND_USE);
17109       break;
17110
17111     case OFFSET_TYPE:
17112       /* This code is used for C++ pointer-to-data-member types.
17113          Output a description of the relevant class type.  */
17114       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
17115                                         DINFO_USAGE_IND_USE);
17116
17117       /* Output a description of the type of the object pointed to.  */
17118       gen_type_die_with_usage (TREE_TYPE (type), context_die,
17119                                         DINFO_USAGE_IND_USE);
17120
17121       /* Now output a DIE to represent this pointer-to-data-member type
17122          itself.  */
17123       gen_ptr_to_mbr_type_die (type, context_die);
17124       break;
17125
17126     case FUNCTION_TYPE:
17127       /* Force out return type (in case it wasn't forced out already).  */
17128       gen_type_die_with_usage (TREE_TYPE (type), context_die,
17129                                         DINFO_USAGE_DIR_USE);
17130       gen_subroutine_type_die (type, context_die);
17131       break;
17132
17133     case METHOD_TYPE:
17134       /* Force out return type (in case it wasn't forced out already).  */
17135       gen_type_die_with_usage (TREE_TYPE (type), context_die,
17136                                         DINFO_USAGE_DIR_USE);
17137       gen_subroutine_type_die (type, context_die);
17138       break;
17139
17140     case ARRAY_TYPE:
17141       gen_array_type_die (type, context_die);
17142       break;
17143
17144     case VECTOR_TYPE:
17145       gen_array_type_die (type, context_die);
17146       break;
17147
17148     case ENUMERAL_TYPE:
17149     case RECORD_TYPE:
17150     case UNION_TYPE:
17151     case QUAL_UNION_TYPE:
17152       /* If this is a nested type whose containing class hasn't been written
17153          out yet, writing it out will cover this one, too.  This does not apply
17154          to instantiations of member class templates; they need to be added to
17155          the containing class as they are generated.  FIXME: This hurts the
17156          idea of combining type decls from multiple TUs, since we can't predict
17157          what set of template instantiations we'll get.  */
17158       if (TYPE_CONTEXT (type)
17159           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
17160           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
17161         {
17162           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
17163
17164           if (TREE_ASM_WRITTEN (type))
17165             return;
17166
17167           /* If that failed, attach ourselves to the stub.  */
17168           push_decl_scope (TYPE_CONTEXT (type));
17169           context_die = lookup_type_die (TYPE_CONTEXT (type));
17170           need_pop = 1;
17171         }
17172       else if (TYPE_CONTEXT (type) != NULL_TREE
17173                && (TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL))
17174         {
17175           /* If this type is local to a function that hasn't been written
17176              out yet, use a NULL context for now; it will be fixed up in
17177              decls_for_scope.  */
17178           context_die = lookup_decl_die (TYPE_CONTEXT (type));
17179           need_pop = 0;
17180         }
17181       else
17182         {
17183           context_die = declare_in_namespace (type, context_die);
17184           need_pop = 0;
17185         }
17186
17187       if (TREE_CODE (type) == ENUMERAL_TYPE)
17188         {
17189           /* This might have been written out by the call to
17190              declare_in_namespace.  */
17191           if (!TREE_ASM_WRITTEN (type))
17192             gen_enumeration_type_die (type, context_die);
17193         }
17194       else
17195         gen_struct_or_union_type_die (type, context_die, usage);
17196
17197       if (need_pop)
17198         pop_decl_scope ();
17199
17200       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
17201          it up if it is ever completed.  gen_*_type_die will set it for us
17202          when appropriate.  */
17203       return;
17204
17205     case VOID_TYPE:
17206     case INTEGER_TYPE:
17207     case REAL_TYPE:
17208     case FIXED_POINT_TYPE:
17209     case COMPLEX_TYPE:
17210     case BOOLEAN_TYPE:
17211       /* No DIEs needed for fundamental types.  */
17212       break;
17213
17214     case LANG_TYPE:
17215       /* No Dwarf representation currently defined.  */
17216       break;
17217
17218     default:
17219       gcc_unreachable ();
17220     }
17221
17222   TREE_ASM_WRITTEN (type) = 1;
17223 }
17224
17225 static void
17226 gen_type_die (tree type, dw_die_ref context_die)
17227 {
17228   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
17229 }
17230
17231 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
17232    things which are local to the given block.  */
17233
17234 static void
17235 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
17236 {
17237   int must_output_die = 0;
17238   bool inlined_func;
17239
17240   /* Ignore blocks that are NULL.  */
17241   if (stmt == NULL_TREE)
17242     return;
17243
17244   inlined_func = inlined_function_outer_scope_p (stmt);
17245
17246   /* If the block is one fragment of a non-contiguous block, do not
17247      process the variables, since they will have been done by the
17248      origin block.  Do process subblocks.  */
17249   if (BLOCK_FRAGMENT_ORIGIN (stmt))
17250     {
17251       tree sub;
17252
17253       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
17254         gen_block_die (sub, context_die, depth + 1);
17255
17256       return;
17257     }
17258
17259   /* Determine if we need to output any Dwarf DIEs at all to represent this
17260      block.  */
17261   if (inlined_func)
17262     /* The outer scopes for inlinings *must* always be represented.  We
17263        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
17264     must_output_die = 1;
17265   else
17266     {
17267       /* Determine if this block directly contains any "significant"
17268          local declarations which we will need to output DIEs for.  */
17269       if (debug_info_level > DINFO_LEVEL_TERSE)
17270         /* We are not in terse mode so *any* local declaration counts
17271            as being a "significant" one.  */
17272         must_output_die = ((BLOCK_VARS (stmt) != NULL
17273                             || BLOCK_NUM_NONLOCALIZED_VARS (stmt))
17274                            && (TREE_USED (stmt)
17275                                || TREE_ASM_WRITTEN (stmt)
17276                                || BLOCK_ABSTRACT (stmt)));
17277       else if ((TREE_USED (stmt)
17278                 || TREE_ASM_WRITTEN (stmt)
17279                 || BLOCK_ABSTRACT (stmt))
17280                && !dwarf2out_ignore_block (stmt))
17281         must_output_die = 1;
17282     }
17283
17284   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
17285      DIE for any block which contains no significant local declarations at
17286      all.  Rather, in such cases we just call `decls_for_scope' so that any
17287      needed Dwarf info for any sub-blocks will get properly generated. Note
17288      that in terse mode, our definition of what constitutes a "significant"
17289      local declaration gets restricted to include only inlined function
17290      instances and local (nested) function definitions.  */
17291   if (must_output_die)
17292     {
17293       if (inlined_func)
17294         {
17295           /* If STMT block is abstract, that means we have been called
17296              indirectly from dwarf2out_abstract_function.
17297              That function rightfully marks the descendent blocks (of
17298              the abstract function it is dealing with) as being abstract,
17299              precisely to prevent us from emitting any
17300              DW_TAG_inlined_subroutine DIE as a descendent
17301              of an abstract function instance. So in that case, we should
17302              not call gen_inlined_subroutine_die.
17303
17304              Later though, when cgraph asks dwarf2out to emit info
17305              for the concrete instance of the function decl into which
17306              the concrete instance of STMT got inlined, the later will lead
17307              to the generation of a DW_TAG_inlined_subroutine DIE.  */
17308           if (! BLOCK_ABSTRACT (stmt))
17309             gen_inlined_subroutine_die (stmt, context_die, depth);
17310         }
17311       else
17312         gen_lexical_block_die (stmt, context_die, depth);
17313     }
17314   else
17315     decls_for_scope (stmt, context_die, depth);
17316 }
17317
17318 /* Process variable DECL (or variable with origin ORIGIN) within
17319    block STMT and add it to CONTEXT_DIE.  */
17320 static void
17321 process_scope_var (tree stmt, tree decl, tree origin, dw_die_ref context_die)
17322 {
17323   dw_die_ref die;
17324   tree decl_or_origin = decl ? decl : origin;
17325   tree ultimate_origin = origin ? decl_ultimate_origin (origin) : NULL;
17326
17327   if (ultimate_origin)
17328     origin = ultimate_origin;
17329
17330   if (TREE_CODE (decl_or_origin) == FUNCTION_DECL)
17331     die = lookup_decl_die (decl_or_origin);
17332   else if (TREE_CODE (decl_or_origin) == TYPE_DECL
17333            && TYPE_DECL_IS_STUB (decl_or_origin))
17334     die = lookup_type_die (TREE_TYPE (decl_or_origin));
17335   else
17336     die = NULL;
17337
17338   if (die != NULL && die->die_parent == NULL)
17339     add_child_die (context_die, die);
17340   else if (TREE_CODE (decl_or_origin) == IMPORTED_DECL)
17341     dwarf2out_imported_module_or_decl_1 (decl_or_origin, DECL_NAME (decl_or_origin),
17342                                          stmt, context_die);
17343   else
17344     gen_decl_die (decl, origin, context_die);
17345 }
17346
17347 /* Generate all of the decls declared within a given scope and (recursively)
17348    all of its sub-blocks.  */
17349
17350 static void
17351 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
17352 {
17353   tree decl;
17354   unsigned int i;
17355   tree subblocks;
17356
17357   /* Ignore NULL blocks.  */
17358   if (stmt == NULL_TREE)
17359     return;
17360
17361   /* Output the DIEs to represent all of the data objects and typedefs
17362      declared directly within this block but not within any nested
17363      sub-blocks.  Also, nested function and tag DIEs have been
17364      generated with a parent of NULL; fix that up now.  */
17365   for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
17366     process_scope_var (stmt, decl, NULL_TREE, context_die);
17367   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (stmt); i++)
17368     process_scope_var (stmt, NULL, BLOCK_NONLOCALIZED_VAR (stmt, i),
17369                        context_die);
17370
17371   /* If we're at -g1, we're not interested in subblocks.  */
17372   if (debug_info_level <= DINFO_LEVEL_TERSE)
17373     return;
17374
17375   /* Output the DIEs to represent all sub-blocks (and the items declared
17376      therein) of this block.  */
17377   for (subblocks = BLOCK_SUBBLOCKS (stmt);
17378        subblocks != NULL;
17379        subblocks = BLOCK_CHAIN (subblocks))
17380     gen_block_die (subblocks, context_die, depth + 1);
17381 }
17382
17383 /* Is this a typedef we can avoid emitting?  */
17384
17385 static inline int
17386 is_redundant_typedef (const_tree decl)
17387 {
17388   if (TYPE_DECL_IS_STUB (decl))
17389     return 1;
17390
17391   if (DECL_ARTIFICIAL (decl)
17392       && DECL_CONTEXT (decl)
17393       && is_tagged_type (DECL_CONTEXT (decl))
17394       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
17395       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
17396     /* Also ignore the artificial member typedef for the class name.  */
17397     return 1;
17398
17399   return 0;
17400 }
17401
17402 /* Returns the DIE for a context.  */
17403
17404 static inline dw_die_ref
17405 get_context_die (tree context)
17406 {
17407   if (context)
17408     {
17409       /* Find die that represents this context.  */
17410       if (TYPE_P (context))
17411         return force_type_die (context);
17412       else
17413         return force_decl_die (context);
17414     }
17415   return comp_unit_die;
17416 }
17417
17418 /* Returns the DIE for decl.  A DIE will always be returned.  */
17419
17420 static dw_die_ref
17421 force_decl_die (tree decl)
17422 {
17423   dw_die_ref decl_die;
17424   unsigned saved_external_flag;
17425   tree save_fn = NULL_TREE;
17426   decl_die = lookup_decl_die (decl);
17427   if (!decl_die)
17428     {
17429       dw_die_ref context_die = get_context_die (DECL_CONTEXT (decl));
17430
17431       decl_die = lookup_decl_die (decl);
17432       if (decl_die)
17433         return decl_die;
17434
17435       switch (TREE_CODE (decl))
17436         {
17437         case FUNCTION_DECL:
17438           /* Clear current_function_decl, so that gen_subprogram_die thinks
17439              that this is a declaration. At this point, we just want to force
17440              declaration die.  */
17441           save_fn = current_function_decl;
17442           current_function_decl = NULL_TREE;
17443           gen_subprogram_die (decl, context_die);
17444           current_function_decl = save_fn;
17445           break;
17446
17447         case VAR_DECL:
17448           /* Set external flag to force declaration die. Restore it after
17449            gen_decl_die() call.  */
17450           saved_external_flag = DECL_EXTERNAL (decl);
17451           DECL_EXTERNAL (decl) = 1;
17452           gen_decl_die (decl, NULL, context_die);
17453           DECL_EXTERNAL (decl) = saved_external_flag;
17454           break;
17455
17456         case NAMESPACE_DECL:
17457           if (dwarf_version >= 3 || !dwarf_strict)
17458             dwarf2out_decl (decl);
17459           else
17460             /* DWARF2 has neither DW_TAG_module, nor DW_TAG_namespace.  */
17461             decl_die = comp_unit_die;
17462           break;
17463
17464         default:
17465           gcc_unreachable ();
17466         }
17467
17468       /* We should be able to find the DIE now.  */
17469       if (!decl_die)
17470         decl_die = lookup_decl_die (decl);
17471       gcc_assert (decl_die);
17472     }
17473
17474   return decl_die;
17475 }
17476
17477 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
17478    always returned.  */
17479
17480 static dw_die_ref
17481 force_type_die (tree type)
17482 {
17483   dw_die_ref type_die;
17484
17485   type_die = lookup_type_die (type);
17486   if (!type_die)
17487     {
17488       dw_die_ref context_die = get_context_die (TYPE_CONTEXT (type));
17489
17490       type_die = modified_type_die (type, TYPE_READONLY (type),
17491                                     TYPE_VOLATILE (type), context_die);
17492       gcc_assert (type_die);
17493     }
17494   return type_die;
17495 }
17496
17497 /* Force out any required namespaces to be able to output DECL,
17498    and return the new context_die for it, if it's changed.  */
17499
17500 static dw_die_ref
17501 setup_namespace_context (tree thing, dw_die_ref context_die)
17502 {
17503   tree context = (DECL_P (thing)
17504                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
17505   if (context && TREE_CODE (context) == NAMESPACE_DECL)
17506     /* Force out the namespace.  */
17507     context_die = force_decl_die (context);
17508
17509   return context_die;
17510 }
17511
17512 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
17513    type) within its namespace, if appropriate.
17514
17515    For compatibility with older debuggers, namespace DIEs only contain
17516    declarations; all definitions are emitted at CU scope.  */
17517
17518 static dw_die_ref
17519 declare_in_namespace (tree thing, dw_die_ref context_die)
17520 {
17521   dw_die_ref ns_context;
17522
17523   if (debug_info_level <= DINFO_LEVEL_TERSE)
17524     return context_die;
17525
17526   /* If this decl is from an inlined function, then don't try to emit it in its
17527      namespace, as we will get confused.  It would have already been emitted
17528      when the abstract instance of the inline function was emitted anyways.  */
17529   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
17530     return context_die;
17531
17532   ns_context = setup_namespace_context (thing, context_die);
17533
17534   if (ns_context != context_die)
17535     {
17536       if (is_fortran ())
17537         return ns_context;
17538       if (DECL_P (thing))
17539         gen_decl_die (thing, NULL, ns_context);
17540       else
17541         gen_type_die (thing, ns_context);
17542     }
17543   return context_die;
17544 }
17545
17546 /* Generate a DIE for a namespace or namespace alias.  */
17547
17548 static void
17549 gen_namespace_die (tree decl, dw_die_ref context_die)
17550 {
17551   dw_die_ref namespace_die;
17552
17553   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
17554      they are an alias of.  */
17555   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
17556     {
17557       /* Output a real namespace or module.  */
17558       context_die = setup_namespace_context (decl, comp_unit_die);
17559       namespace_die = new_die (is_fortran ()
17560                                ? DW_TAG_module : DW_TAG_namespace,
17561                                context_die, decl);
17562       /* For Fortran modules defined in different CU don't add src coords.  */
17563       if (namespace_die->die_tag == DW_TAG_module && DECL_EXTERNAL (decl))
17564         add_name_attribute (namespace_die, dwarf2_name (decl, 0));
17565       else
17566         add_name_and_src_coords_attributes (namespace_die, decl);
17567       if (DECL_EXTERNAL (decl))
17568         add_AT_flag (namespace_die, DW_AT_declaration, 1);
17569       equate_decl_number_to_die (decl, namespace_die);
17570     }
17571   else
17572     {
17573       /* Output a namespace alias.  */
17574
17575       /* Force out the namespace we are an alias of, if necessary.  */
17576       dw_die_ref origin_die
17577         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
17578
17579       if (DECL_CONTEXT (decl) == NULL_TREE
17580           || TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
17581         context_die = setup_namespace_context (decl, comp_unit_die);
17582       /* Now create the namespace alias DIE.  */
17583       namespace_die = new_die (DW_TAG_imported_declaration, context_die, decl);
17584       add_name_and_src_coords_attributes (namespace_die, decl);
17585       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
17586       equate_decl_number_to_die (decl, namespace_die);
17587     }
17588 }
17589
17590 /* Generate Dwarf debug information for a decl described by DECL.  */
17591
17592 static void
17593 gen_decl_die (tree decl, tree origin, dw_die_ref context_die)
17594 {
17595   tree decl_or_origin = decl ? decl : origin;
17596   tree class_origin = NULL;
17597
17598   if (DECL_P (decl_or_origin) && DECL_IGNORED_P (decl_or_origin))
17599     return;
17600
17601   switch (TREE_CODE (decl_or_origin))
17602     {
17603     case ERROR_MARK:
17604       break;
17605
17606     case CONST_DECL:
17607       if (!is_fortran ())
17608         {
17609           /* The individual enumerators of an enum type get output when we output
17610              the Dwarf representation of the relevant enum type itself.  */
17611           break;
17612         }
17613
17614       /* Emit its type.  */
17615       gen_type_die (TREE_TYPE (decl), context_die);
17616
17617       /* And its containing namespace.  */
17618       context_die = declare_in_namespace (decl, context_die);
17619
17620       gen_const_die (decl, context_die);
17621       break;
17622
17623     case FUNCTION_DECL:
17624       /* Don't output any DIEs to represent mere function declarations,
17625          unless they are class members or explicit block externs.  */
17626       if (DECL_INITIAL (decl_or_origin) == NULL_TREE
17627           && DECL_CONTEXT (decl_or_origin) == NULL_TREE
17628           && (current_function_decl == NULL_TREE
17629               || DECL_ARTIFICIAL (decl_or_origin)))
17630         break;
17631
17632 #if 0
17633       /* FIXME */
17634       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
17635          on local redeclarations of global functions.  That seems broken.  */
17636       if (current_function_decl != decl)
17637         /* This is only a declaration.  */;
17638 #endif
17639
17640       /* If we're emitting a clone, emit info for the abstract instance.  */
17641       if (origin || DECL_ORIGIN (decl) != decl)
17642         dwarf2out_abstract_function (origin ? origin : DECL_ABSTRACT_ORIGIN (decl));
17643
17644       /* If we're emitting an out-of-line copy of an inline function,
17645          emit info for the abstract instance and set up to refer to it.  */
17646       else if (cgraph_function_possibly_inlined_p (decl)
17647                && ! DECL_ABSTRACT (decl)
17648                && ! class_or_namespace_scope_p (context_die)
17649                /* dwarf2out_abstract_function won't emit a die if this is just
17650                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
17651                   that case, because that works only if we have a die.  */
17652                && DECL_INITIAL (decl) != NULL_TREE)
17653         {
17654           dwarf2out_abstract_function (decl);
17655           set_decl_origin_self (decl);
17656         }
17657
17658       /* Otherwise we're emitting the primary DIE for this decl.  */
17659       else if (debug_info_level > DINFO_LEVEL_TERSE)
17660         {
17661           /* Before we describe the FUNCTION_DECL itself, make sure that we
17662              have described its return type.  */
17663           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
17664
17665           /* And its virtual context.  */
17666           if (DECL_VINDEX (decl) != NULL_TREE)
17667             gen_type_die (DECL_CONTEXT (decl), context_die);
17668
17669           /* And its containing type.  */
17670           if (!origin)
17671             origin = decl_class_context (decl);
17672           if (origin != NULL_TREE)
17673             gen_type_die_for_member (origin, decl, context_die);
17674
17675           /* And its containing namespace.  */
17676           context_die = declare_in_namespace (decl, context_die);
17677         }
17678
17679       /* Now output a DIE to represent the function itself.  */
17680       if (decl)
17681         gen_subprogram_die (decl, context_die);
17682       break;
17683
17684     case TYPE_DECL:
17685       /* If we are in terse mode, don't generate any DIEs to represent any
17686          actual typedefs.  */
17687       if (debug_info_level <= DINFO_LEVEL_TERSE)
17688         break;
17689
17690       /* In the special case of a TYPE_DECL node representing the declaration
17691          of some type tag, if the given TYPE_DECL is marked as having been
17692          instantiated from some other (original) TYPE_DECL node (e.g. one which
17693          was generated within the original definition of an inline function) we
17694          used to generate a special (abbreviated) DW_TAG_structure_type,
17695          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  But nothing
17696          should be actually referencing those DIEs, as variable DIEs with that
17697          type would be emitted already in the abstract origin, so it was always
17698          removed during unused type prunning.  Don't add anything in this
17699          case.  */
17700       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE)
17701         break;
17702
17703       if (is_redundant_typedef (decl))
17704         gen_type_die (TREE_TYPE (decl), context_die);
17705       else
17706         /* Output a DIE to represent the typedef itself.  */
17707         gen_typedef_die (decl, context_die);
17708       break;
17709
17710     case LABEL_DECL:
17711       if (debug_info_level >= DINFO_LEVEL_NORMAL)
17712         gen_label_die (decl, context_die);
17713       break;
17714
17715     case VAR_DECL:
17716     case RESULT_DECL:
17717       /* If we are in terse mode, don't generate any DIEs to represent any
17718          variable declarations or definitions.  */
17719       if (debug_info_level <= DINFO_LEVEL_TERSE)
17720         break;
17721
17722       /* Output any DIEs that are needed to specify the type of this data
17723          object.  */
17724       if (decl_by_reference_p (decl_or_origin))
17725         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17726       else
17727         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17728
17729       /* And its containing type.  */
17730       class_origin = decl_class_context (decl_or_origin);
17731       if (class_origin != NULL_TREE)
17732         gen_type_die_for_member (class_origin, decl_or_origin, context_die);
17733
17734       /* And its containing namespace.  */
17735       context_die = declare_in_namespace (decl_or_origin, context_die);
17736
17737       /* Now output the DIE to represent the data object itself.  This gets
17738          complicated because of the possibility that the VAR_DECL really
17739          represents an inlined instance of a formal parameter for an inline
17740          function.  */
17741       if (!origin)
17742         origin = decl_ultimate_origin (decl);
17743       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
17744         gen_formal_parameter_die (decl, origin,
17745                                   true /* Emit name attribute.  */,
17746                                   context_die);
17747       else
17748         gen_variable_die (decl, origin, context_die);
17749       break;
17750
17751     case FIELD_DECL:
17752       /* Ignore the nameless fields that are used to skip bits but handle C++
17753          anonymous unions and structs.  */
17754       if (DECL_NAME (decl) != NULL_TREE
17755           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
17756           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
17757         {
17758           gen_type_die (member_declared_type (decl), context_die);
17759           gen_field_die (decl, context_die);
17760         }
17761       break;
17762
17763     case PARM_DECL:
17764       if (DECL_BY_REFERENCE (decl_or_origin))
17765         gen_type_die (TREE_TYPE (TREE_TYPE (decl_or_origin)), context_die);
17766       else
17767         gen_type_die (TREE_TYPE (decl_or_origin), context_die);
17768       gen_formal_parameter_die (decl, origin,
17769                                 true /* Emit name attribute.  */,
17770                                 context_die);
17771       break;
17772
17773     case NAMESPACE_DECL:
17774     case IMPORTED_DECL:
17775       if (dwarf_version >= 3 || !dwarf_strict)
17776         gen_namespace_die (decl, context_die);
17777       break;
17778
17779     default:
17780       /* Probably some frontend-internal decl.  Assume we don't care.  */
17781       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
17782       break;
17783     }
17784 }
17785 \f
17786 /* Output debug information for global decl DECL.  Called from toplev.c after
17787    compilation proper has finished.  */
17788
17789 static void
17790 dwarf2out_global_decl (tree decl)
17791 {
17792   /* Output DWARF2 information for file-scope tentative data object
17793      declarations, file-scope (extern) function declarations (which
17794      had no corresponding body) and file-scope tagged type declarations
17795      and definitions which have not yet been forced out.  */
17796   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
17797     dwarf2out_decl (decl);
17798 }
17799
17800 /* Output debug information for type decl DECL.  Called from toplev.c
17801    and from language front ends (to record built-in types).  */
17802 static void
17803 dwarf2out_type_decl (tree decl, int local)
17804 {
17805   if (!local)
17806     dwarf2out_decl (decl);
17807 }
17808
17809 /* Output debug information for imported module or decl DECL.
17810    NAME is non-NULL name in the lexical block if the decl has been renamed.
17811    LEXICAL_BLOCK is the lexical block (which TREE_CODE is a BLOCK)
17812    that DECL belongs to.
17813    LEXICAL_BLOCK_DIE is the DIE of LEXICAL_BLOCK.  */
17814 static void
17815 dwarf2out_imported_module_or_decl_1 (tree decl,
17816                                      tree name,
17817                                      tree lexical_block,
17818                                      dw_die_ref lexical_block_die)
17819 {
17820   expanded_location xloc;
17821   dw_die_ref imported_die = NULL;
17822   dw_die_ref at_import_die;
17823
17824   if (TREE_CODE (decl) == IMPORTED_DECL)
17825     {
17826       xloc = expand_location (DECL_SOURCE_LOCATION (decl));
17827       decl = IMPORTED_DECL_ASSOCIATED_DECL (decl);
17828       gcc_assert (decl);
17829     }
17830   else
17831     xloc = expand_location (input_location);
17832
17833   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
17834     {
17835       if (is_base_type (TREE_TYPE (decl)))
17836         at_import_die = base_type_die (TREE_TYPE (decl));
17837       else
17838         at_import_die = force_type_die (TREE_TYPE (decl));
17839       /* For namespace N { typedef void T; } using N::T; base_type_die
17840          returns NULL, but DW_TAG_imported_declaration requires
17841          the DW_AT_import tag.  Force creation of DW_TAG_typedef.  */
17842       if (!at_import_die)
17843         {
17844           gcc_assert (TREE_CODE (decl) == TYPE_DECL);
17845           gen_typedef_die (decl, get_context_die (DECL_CONTEXT (decl)));
17846           at_import_die = lookup_type_die (TREE_TYPE (decl));
17847           gcc_assert (at_import_die);
17848         }
17849     }
17850   else
17851     {
17852       at_import_die = lookup_decl_die (decl);
17853       if (!at_import_die)
17854         {
17855           /* If we're trying to avoid duplicate debug info, we may not have
17856              emitted the member decl for this field.  Emit it now.  */
17857           if (TREE_CODE (decl) == FIELD_DECL)
17858             {
17859               tree type = DECL_CONTEXT (decl);
17860
17861               if (TYPE_CONTEXT (type)
17862                   && TYPE_P (TYPE_CONTEXT (type))
17863                   && !should_emit_struct_debug (TYPE_CONTEXT (type),
17864                                                 DINFO_USAGE_DIR_USE))
17865                 return;
17866               gen_type_die_for_member (type, decl,
17867                                        get_context_die (TYPE_CONTEXT (type)));
17868             }
17869           at_import_die = force_decl_die (decl);
17870         }
17871     }
17872
17873   if (TREE_CODE (decl) == NAMESPACE_DECL)
17874     {
17875       if (dwarf_version >= 3 || !dwarf_strict)
17876         imported_die = new_die (DW_TAG_imported_module,
17877                                 lexical_block_die,
17878                                 lexical_block);
17879       else
17880         return;
17881     }
17882   else
17883     imported_die = new_die (DW_TAG_imported_declaration,
17884                             lexical_block_die,
17885                             lexical_block);
17886
17887   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
17888   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
17889   if (name)
17890     add_AT_string (imported_die, DW_AT_name,
17891                    IDENTIFIER_POINTER (name));
17892   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
17893 }
17894
17895 /* Output debug information for imported module or decl DECL.
17896    NAME is non-NULL name in context if the decl has been renamed.
17897    CHILD is true if decl is one of the renamed decls as part of
17898    importing whole module.  */
17899
17900 static void
17901 dwarf2out_imported_module_or_decl (tree decl, tree name, tree context,
17902                                    bool child)
17903 {
17904   /* dw_die_ref at_import_die;  */
17905   dw_die_ref scope_die;
17906
17907   if (debug_info_level <= DINFO_LEVEL_TERSE)
17908     return;
17909
17910   gcc_assert (decl);
17911
17912   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
17913      We need decl DIE for reference and scope die. First, get DIE for the decl
17914      itself.  */
17915
17916   /* Get the scope die for decl context. Use comp_unit_die for global module
17917      or decl. If die is not found for non globals, force new die.  */
17918   if (context
17919       && TYPE_P (context)
17920       && !should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
17921     return;
17922
17923   if (!(dwarf_version >= 3 || !dwarf_strict))
17924     return;
17925
17926   scope_die = get_context_die (context);
17927
17928   if (child)
17929     {
17930       gcc_assert (scope_die->die_child);
17931       gcc_assert (scope_die->die_child->die_tag == DW_TAG_imported_module);
17932       gcc_assert (TREE_CODE (decl) != NAMESPACE_DECL);
17933       scope_die = scope_die->die_child;
17934     }
17935
17936   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
17937   dwarf2out_imported_module_or_decl_1 (decl, name, context, scope_die);
17938
17939 }
17940
17941 /* Write the debugging output for DECL.  */
17942
17943 void
17944 dwarf2out_decl (tree decl)
17945 {
17946   dw_die_ref context_die = comp_unit_die;
17947
17948   switch (TREE_CODE (decl))
17949     {
17950     case ERROR_MARK:
17951       return;
17952
17953     case FUNCTION_DECL:
17954       /* What we would really like to do here is to filter out all mere
17955          file-scope declarations of file-scope functions which are never
17956          referenced later within this translation unit (and keep all of ones
17957          that *are* referenced later on) but we aren't clairvoyant, so we have
17958          no idea which functions will be referenced in the future (i.e. later
17959          on within the current translation unit). So here we just ignore all
17960          file-scope function declarations which are not also definitions.  If
17961          and when the debugger needs to know something about these functions,
17962          it will have to hunt around and find the DWARF information associated
17963          with the definition of the function.
17964
17965          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
17966          nodes represent definitions and which ones represent mere
17967          declarations.  We have to check DECL_INITIAL instead. That's because
17968          the C front-end supports some weird semantics for "extern inline"
17969          function definitions.  These can get inlined within the current
17970          translation unit (and thus, we need to generate Dwarf info for their
17971          abstract instances so that the Dwarf info for the concrete inlined
17972          instances can have something to refer to) but the compiler never
17973          generates any out-of-lines instances of such things (despite the fact
17974          that they *are* definitions).
17975
17976          The important point is that the C front-end marks these "extern
17977          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
17978          them anyway. Note that the C++ front-end also plays some similar games
17979          for inline function definitions appearing within include files which
17980          also contain `#pragma interface' pragmas.  */
17981       if (DECL_INITIAL (decl) == NULL_TREE)
17982         return;
17983
17984       /* If we're a nested function, initially use a parent of NULL; if we're
17985          a plain function, this will be fixed up in decls_for_scope.  If
17986          we're a method, it will be ignored, since we already have a DIE.  */
17987       if (decl_function_context (decl)
17988           /* But if we're in terse mode, we don't care about scope.  */
17989           && debug_info_level > DINFO_LEVEL_TERSE)
17990         context_die = NULL;
17991       break;
17992
17993     case VAR_DECL:
17994       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
17995          declaration and if the declaration was never even referenced from
17996          within this entire compilation unit.  We suppress these DIEs in
17997          order to save space in the .debug section (by eliminating entries
17998          which are probably useless).  Note that we must not suppress
17999          block-local extern declarations (whether used or not) because that
18000          would screw-up the debugger's name lookup mechanism and cause it to
18001          miss things which really ought to be in scope at a given point.  */
18002       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
18003         return;
18004
18005       /* For local statics lookup proper context die.  */
18006       if (TREE_STATIC (decl) && decl_function_context (decl))
18007         context_die = lookup_decl_die (DECL_CONTEXT (decl));
18008
18009       /* If we are in terse mode, don't generate any DIEs to represent any
18010          variable declarations or definitions.  */
18011       if (debug_info_level <= DINFO_LEVEL_TERSE)
18012         return;
18013       break;
18014
18015     case CONST_DECL:
18016       if (debug_info_level <= DINFO_LEVEL_TERSE)
18017         return;
18018       if (!is_fortran ())
18019         return;
18020       if (TREE_STATIC (decl) && decl_function_context (decl))
18021         context_die = lookup_decl_die (DECL_CONTEXT (decl));
18022       break;
18023
18024     case NAMESPACE_DECL:
18025     case IMPORTED_DECL:
18026       if (debug_info_level <= DINFO_LEVEL_TERSE)
18027         return;
18028       if (lookup_decl_die (decl) != NULL)
18029         return;
18030       break;
18031
18032     case TYPE_DECL:
18033       /* Don't emit stubs for types unless they are needed by other DIEs.  */
18034       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
18035         return;
18036
18037       /* Don't bother trying to generate any DIEs to represent any of the
18038          normal built-in types for the language we are compiling.  */
18039       if (DECL_IS_BUILTIN (decl))
18040         {
18041           /* OK, we need to generate one for `bool' so GDB knows what type
18042              comparisons have.  */
18043           if (is_cxx ()
18044               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
18045               && ! DECL_IGNORED_P (decl))
18046             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
18047
18048           return;
18049         }
18050
18051       /* If we are in terse mode, don't generate any DIEs for types.  */
18052       if (debug_info_level <= DINFO_LEVEL_TERSE)
18053         return;
18054
18055       /* If we're a function-scope tag, initially use a parent of NULL;
18056          this will be fixed up in decls_for_scope.  */
18057       if (decl_function_context (decl))
18058         context_die = NULL;
18059
18060       break;
18061
18062     default:
18063       return;
18064     }
18065
18066   gen_decl_die (decl, NULL, context_die);
18067 }
18068
18069 /* Output a marker (i.e. a label) for the beginning of the generated code for
18070    a lexical block.  */
18071
18072 static void
18073 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
18074                        unsigned int blocknum)
18075 {
18076   switch_to_section (current_function_section ());
18077   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
18078 }
18079
18080 /* Output a marker (i.e. a label) for the end of the generated code for a
18081    lexical block.  */
18082
18083 static void
18084 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
18085 {
18086   switch_to_section (current_function_section ());
18087   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
18088 }
18089
18090 /* Returns nonzero if it is appropriate not to emit any debugging
18091    information for BLOCK, because it doesn't contain any instructions.
18092
18093    Don't allow this for blocks with nested functions or local classes
18094    as we would end up with orphans, and in the presence of scheduling
18095    we may end up calling them anyway.  */
18096
18097 static bool
18098 dwarf2out_ignore_block (const_tree block)
18099 {
18100   tree decl;
18101   unsigned int i;
18102
18103   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
18104     if (TREE_CODE (decl) == FUNCTION_DECL
18105         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
18106       return 0;
18107   for (i = 0; i < BLOCK_NUM_NONLOCALIZED_VARS (block); i++)
18108     {
18109       decl = BLOCK_NONLOCALIZED_VAR (block, i);
18110       if (TREE_CODE (decl) == FUNCTION_DECL
18111           || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
18112       return 0;
18113     }
18114
18115   return 1;
18116 }
18117
18118 /* Hash table routines for file_hash.  */
18119
18120 static int
18121 file_table_eq (const void *p1_p, const void *p2_p)
18122 {
18123   const struct dwarf_file_data *const p1 =
18124     (const struct dwarf_file_data *) p1_p;
18125   const char *const p2 = (const char *) p2_p;
18126   return strcmp (p1->filename, p2) == 0;
18127 }
18128
18129 static hashval_t
18130 file_table_hash (const void *p_p)
18131 {
18132   const struct dwarf_file_data *const p = (const struct dwarf_file_data *) p_p;
18133   return htab_hash_string (p->filename);
18134 }
18135
18136 /* Lookup FILE_NAME (in the list of filenames that we know about here in
18137    dwarf2out.c) and return its "index".  The index of each (known) filename is
18138    just a unique number which is associated with only that one filename.  We
18139    need such numbers for the sake of generating labels (in the .debug_sfnames
18140    section) and references to those files numbers (in the .debug_srcinfo
18141    and.debug_macinfo sections).  If the filename given as an argument is not
18142    found in our current list, add it to the list and assign it the next
18143    available unique index number.  In order to speed up searches, we remember
18144    the index of the filename was looked up last.  This handles the majority of
18145    all searches.  */
18146
18147 static struct dwarf_file_data *
18148 lookup_filename (const char *file_name)
18149 {
18150   void ** slot;
18151   struct dwarf_file_data * created;
18152
18153   /* Check to see if the file name that was searched on the previous
18154      call matches this file name.  If so, return the index.  */
18155   if (file_table_last_lookup
18156       && (file_name == file_table_last_lookup->filename
18157           || strcmp (file_table_last_lookup->filename, file_name) == 0))
18158     return file_table_last_lookup;
18159
18160   /* Didn't match the previous lookup, search the table.  */
18161   slot = htab_find_slot_with_hash (file_table, file_name,
18162                                    htab_hash_string (file_name), INSERT);
18163   if (*slot)
18164     return (struct dwarf_file_data *) *slot;
18165
18166   created = GGC_NEW (struct dwarf_file_data);
18167   created->filename = file_name;
18168   created->emitted_number = 0;
18169   *slot = created;
18170   return created;
18171 }
18172
18173 /* If the assembler will construct the file table, then translate the compiler
18174    internal file table number into the assembler file table number, and emit
18175    a .file directive if we haven't already emitted one yet.  The file table
18176    numbers are different because we prune debug info for unused variables and
18177    types, which may include filenames.  */
18178
18179 static int
18180 maybe_emit_file (struct dwarf_file_data * fd)
18181 {
18182   if (! fd->emitted_number)
18183     {
18184       if (last_emitted_file)
18185         fd->emitted_number = last_emitted_file->emitted_number + 1;
18186       else
18187         fd->emitted_number = 1;
18188       last_emitted_file = fd;
18189
18190       if (DWARF2_ASM_LINE_DEBUG_INFO)
18191         {
18192           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
18193           output_quoted_string (asm_out_file,
18194                                 remap_debug_filename (fd->filename));
18195           fputc ('\n', asm_out_file);
18196         }
18197     }
18198
18199   return fd->emitted_number;
18200 }
18201
18202 /* Schedule generation of a DW_AT_const_value attribute to DIE.
18203    That generation should happen after function debug info has been
18204    generated. The value of the attribute is the constant value of ARG.  */
18205
18206 static void
18207 append_entry_to_tmpl_value_parm_die_table (dw_die_ref die, tree arg)
18208 {
18209   die_arg_entry entry;
18210
18211   if (!die || !arg)
18212     return;
18213
18214   if (!tmpl_value_parm_die_table)
18215     tmpl_value_parm_die_table
18216       = VEC_alloc (die_arg_entry, gc, 32);
18217
18218   entry.die = die;
18219   entry.arg = arg;
18220   VEC_safe_push (die_arg_entry, gc,
18221                  tmpl_value_parm_die_table,
18222                  &entry);
18223 }
18224
18225 /* Add a DW_AT_const_value attribute to DIEs that were scheduled
18226    by append_entry_to_tmpl_value_parm_die_table. This function must
18227    be called after function DIEs have been generated.  */
18228
18229 static void
18230 gen_remaining_tmpl_value_param_die_attribute (void)
18231 {
18232   if (tmpl_value_parm_die_table)
18233     {
18234       unsigned i;
18235       die_arg_entry *e;
18236
18237       for (i = 0;
18238            VEC_iterate (die_arg_entry, tmpl_value_parm_die_table, i, e);
18239            i++)
18240         tree_add_const_value_attribute (e->die, e->arg);
18241     }
18242 }
18243
18244
18245 /* Replace DW_AT_name for the decl with name.  */
18246  
18247 static void
18248 dwarf2out_set_name (tree decl, tree name)
18249 {
18250   dw_die_ref die;
18251   dw_attr_ref attr;
18252
18253   die = TYPE_SYMTAB_DIE (decl);
18254   if (!die)
18255     return;
18256
18257   attr = get_AT (die, DW_AT_name);
18258   if (attr)
18259     {
18260       struct indirect_string_node *node;
18261
18262       node = find_AT_string (dwarf2_name (name, 0));
18263       /* replace the string.  */
18264       attr->dw_attr_val.v.val_str = node;
18265     }
18266
18267   else
18268     add_name_attribute (die, dwarf2_name (name, 0));
18269 }
18270
18271 /* Called by the final INSN scan whenever we see a var location.  We
18272    use it to drop labels in the right places, and throw the location in
18273    our lookup table.  */
18274
18275 static void
18276 dwarf2out_var_location (rtx loc_note)
18277 {
18278   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES + 2];
18279   struct var_loc_node *newloc;
18280   rtx next_real;
18281   static const char *last_label;
18282   static const char *last_postcall_label;
18283   static bool last_in_cold_section_p;
18284   tree decl;
18285
18286   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
18287     return;
18288
18289   next_real = next_real_insn (loc_note);
18290   /* If there are no instructions which would be affected by this note,
18291      don't do anything.  */
18292   if (next_real == NULL_RTX)
18293     return;
18294
18295   newloc = GGC_CNEW (struct var_loc_node);
18296   /* If there were no real insns between note we processed last time
18297      and this note, use the label we emitted last time.  */
18298   if (last_var_location_insn == NULL_RTX
18299       || last_var_location_insn != next_real
18300       || last_in_cold_section_p != in_cold_section_p)
18301     {
18302       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
18303       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
18304       loclabel_num++;
18305       last_label = ggc_strdup (loclabel);
18306       if (!NOTE_DURING_CALL_P (loc_note))
18307         last_postcall_label = NULL;
18308     }
18309   newloc->var_loc_note = loc_note;
18310   newloc->next = NULL;
18311
18312   if (!NOTE_DURING_CALL_P (loc_note))
18313     newloc->label = last_label;
18314   else
18315     {
18316       if (!last_postcall_label)
18317         {
18318           sprintf (loclabel, "%s-1", last_label);
18319           last_postcall_label = ggc_strdup (loclabel);
18320         }
18321       newloc->label = last_postcall_label;
18322     }
18323
18324   if (cfun && in_cold_section_p)
18325     newloc->section_label = crtl->subsections.cold_section_label;
18326   else
18327     newloc->section_label = text_section_label;
18328
18329   last_var_location_insn = next_real;
18330   last_in_cold_section_p = in_cold_section_p;
18331   decl = NOTE_VAR_LOCATION_DECL (loc_note);
18332   add_var_loc_to_decl (decl, newloc);
18333 }
18334
18335 /* We need to reset the locations at the beginning of each
18336    function. We can't do this in the end_function hook, because the
18337    declarations that use the locations won't have been output when
18338    that hook is called.  Also compute have_multiple_function_sections here.  */
18339
18340 static void
18341 dwarf2out_begin_function (tree fun)
18342 {
18343   htab_empty (decl_loc_table);
18344
18345   if (function_section (fun) != text_section)
18346     have_multiple_function_sections = true;
18347
18348   dwarf2out_note_section_used ();
18349 }
18350
18351 /* Output a label to mark the beginning of a source code line entry
18352    and record information relating to this source line, in
18353    'line_info_table' for later output of the .debug_line section.  */
18354
18355 static void
18356 dwarf2out_source_line (unsigned int line, const char *filename,
18357                        int discriminator, bool is_stmt)
18358 {
18359   static bool last_is_stmt = true;
18360
18361   if (debug_info_level >= DINFO_LEVEL_NORMAL
18362       && line != 0)
18363     {
18364       int file_num = maybe_emit_file (lookup_filename (filename));
18365
18366       switch_to_section (current_function_section ());
18367
18368       /* If requested, emit something human-readable.  */
18369       if (flag_debug_asm)
18370         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
18371                  filename, line);
18372
18373       if (DWARF2_ASM_LINE_DEBUG_INFO)
18374         {
18375           /* Emit the .loc directive understood by GNU as.  */
18376           fprintf (asm_out_file, "\t.loc %d %d 0", file_num, line);
18377           if (is_stmt != last_is_stmt)
18378             {
18379               fprintf (asm_out_file, " is_stmt %d", is_stmt ? 1 : 0);
18380               last_is_stmt = is_stmt;
18381             }
18382           if (SUPPORTS_DISCRIMINATOR && discriminator != 0)
18383             fprintf (asm_out_file, " discriminator %d", discriminator);
18384           fputc ('\n', asm_out_file);
18385
18386           /* Indicate that line number info exists.  */
18387           line_info_table_in_use++;
18388         }
18389       else if (function_section (current_function_decl) != text_section)
18390         {
18391           dw_separate_line_info_ref line_info;
18392           targetm.asm_out.internal_label (asm_out_file,
18393                                           SEPARATE_LINE_CODE_LABEL,
18394                                           separate_line_info_table_in_use);
18395
18396           /* Expand the line info table if necessary.  */
18397           if (separate_line_info_table_in_use
18398               == separate_line_info_table_allocated)
18399             {
18400               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
18401               separate_line_info_table
18402                 = GGC_RESIZEVEC (dw_separate_line_info_entry,
18403                                  separate_line_info_table,
18404                                  separate_line_info_table_allocated);
18405               memset (separate_line_info_table
18406                        + separate_line_info_table_in_use,
18407                       0,
18408                       (LINE_INFO_TABLE_INCREMENT
18409                        * sizeof (dw_separate_line_info_entry)));
18410             }
18411
18412           /* Add the new entry at the end of the line_info_table.  */
18413           line_info
18414             = &separate_line_info_table[separate_line_info_table_in_use++];
18415           line_info->dw_file_num = file_num;
18416           line_info->dw_line_num = line;
18417           line_info->function = current_function_funcdef_no;
18418         }
18419       else
18420         {
18421           dw_line_info_ref line_info;
18422
18423           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
18424                                      line_info_table_in_use);
18425
18426           /* Expand the line info table if necessary.  */
18427           if (line_info_table_in_use == line_info_table_allocated)
18428             {
18429               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
18430               line_info_table
18431                 = GGC_RESIZEVEC (dw_line_info_entry, line_info_table,
18432                                  line_info_table_allocated);
18433               memset (line_info_table + line_info_table_in_use, 0,
18434                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
18435             }
18436
18437           /* Add the new entry at the end of the line_info_table.  */
18438           line_info = &line_info_table[line_info_table_in_use++];
18439           line_info->dw_file_num = file_num;
18440           line_info->dw_line_num = line;
18441         }
18442     }
18443 }
18444
18445 /* Record the beginning of a new source file.  */
18446
18447 static void
18448 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
18449 {
18450   if (flag_eliminate_dwarf2_dups)
18451     {
18452       /* Record the beginning of the file for break_out_includes.  */
18453       dw_die_ref bincl_die;
18454
18455       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
18456       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
18457     }
18458
18459   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18460     {
18461       int file_num = maybe_emit_file (lookup_filename (filename));
18462
18463       switch_to_section (debug_macinfo_section);
18464       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
18465       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
18466                                    lineno);
18467
18468       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
18469     }
18470 }
18471
18472 /* Record the end of a source file.  */
18473
18474 static void
18475 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
18476 {
18477   if (flag_eliminate_dwarf2_dups)
18478     /* Record the end of the file for break_out_includes.  */
18479     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
18480
18481   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18482     {
18483       switch_to_section (debug_macinfo_section);
18484       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
18485     }
18486 }
18487
18488 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
18489    the tail part of the directive line, i.e. the part which is past the
18490    initial whitespace, #, whitespace, directive-name, whitespace part.  */
18491
18492 static void
18493 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
18494                   const char *buffer ATTRIBUTE_UNUSED)
18495 {
18496   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18497     {
18498       switch_to_section (debug_macinfo_section);
18499       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
18500       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
18501       dw2_asm_output_nstring (buffer, -1, "The macro");
18502     }
18503 }
18504
18505 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
18506    the tail part of the directive line, i.e. the part which is past the
18507    initial whitespace, #, whitespace, directive-name, whitespace part.  */
18508
18509 static void
18510 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
18511                  const char *buffer ATTRIBUTE_UNUSED)
18512 {
18513   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18514     {
18515       switch_to_section (debug_macinfo_section);
18516       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
18517       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
18518       dw2_asm_output_nstring (buffer, -1, "The macro");
18519     }
18520 }
18521
18522 /* Set up for Dwarf output at the start of compilation.  */
18523
18524 static void
18525 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
18526 {
18527   /* Allocate the file_table.  */
18528   file_table = htab_create_ggc (50, file_table_hash,
18529                                 file_table_eq, NULL);
18530
18531   /* Allocate the decl_die_table.  */
18532   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
18533                                     decl_die_table_eq, NULL);
18534
18535   /* Allocate the decl_loc_table.  */
18536   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
18537                                     decl_loc_table_eq, NULL);
18538
18539   /* Allocate the initial hunk of the decl_scope_table.  */
18540   decl_scope_table = VEC_alloc (tree, gc, 256);
18541
18542   /* Allocate the initial hunk of the abbrev_die_table.  */
18543   abbrev_die_table = GGC_CNEWVEC (dw_die_ref, ABBREV_DIE_TABLE_INCREMENT);
18544   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
18545   /* Zero-th entry is allocated, but unused.  */
18546   abbrev_die_table_in_use = 1;
18547
18548   /* Allocate the initial hunk of the line_info_table.  */
18549   line_info_table = GGC_CNEWVEC (dw_line_info_entry, LINE_INFO_TABLE_INCREMENT);
18550   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
18551
18552   /* Zero-th entry is allocated, but unused.  */
18553   line_info_table_in_use = 1;
18554
18555   /* Allocate the pubtypes and pubnames vectors.  */
18556   pubname_table = VEC_alloc (pubname_entry, gc, 32);
18557   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
18558
18559   /* Generate the initial DIE for the .debug section.  Note that the (string)
18560      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
18561      will (typically) be a relative pathname and that this pathname should be
18562      taken as being relative to the directory from which the compiler was
18563      invoked when the given (base) source file was compiled.  We will fill
18564      in this value in dwarf2out_finish.  */
18565   comp_unit_die = gen_compile_unit_die (NULL);
18566
18567   incomplete_types = VEC_alloc (tree, gc, 64);
18568
18569   used_rtx_array = VEC_alloc (rtx, gc, 32);
18570
18571   debug_info_section = get_section (DEBUG_INFO_SECTION,
18572                                     SECTION_DEBUG, NULL);
18573   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
18574                                       SECTION_DEBUG, NULL);
18575   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
18576                                        SECTION_DEBUG, NULL);
18577   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
18578                                        SECTION_DEBUG, NULL);
18579   debug_line_section = get_section (DEBUG_LINE_SECTION,
18580                                     SECTION_DEBUG, NULL);
18581   debug_loc_section = get_section (DEBUG_LOC_SECTION,
18582                                    SECTION_DEBUG, NULL);
18583   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
18584                                         SECTION_DEBUG, NULL);
18585   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
18586                                         SECTION_DEBUG, NULL);
18587   debug_str_section = get_section (DEBUG_STR_SECTION,
18588                                    DEBUG_STR_SECTION_FLAGS, NULL);
18589   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
18590                                       SECTION_DEBUG, NULL);
18591   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
18592                                      SECTION_DEBUG, NULL);
18593
18594   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
18595   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
18596                                DEBUG_ABBREV_SECTION_LABEL, 0);
18597   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
18598   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
18599                                COLD_TEXT_SECTION_LABEL, 0);
18600   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
18601
18602   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
18603                                DEBUG_INFO_SECTION_LABEL, 0);
18604   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
18605                                DEBUG_LINE_SECTION_LABEL, 0);
18606   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
18607                                DEBUG_RANGES_SECTION_LABEL, 0);
18608   switch_to_section (debug_abbrev_section);
18609   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
18610   switch_to_section (debug_info_section);
18611   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
18612   switch_to_section (debug_line_section);
18613   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
18614
18615   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
18616     {
18617       switch_to_section (debug_macinfo_section);
18618       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
18619                                    DEBUG_MACINFO_SECTION_LABEL, 0);
18620       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
18621     }
18622
18623   switch_to_section (text_section);
18624   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
18625   if (flag_reorder_blocks_and_partition)
18626     {
18627       cold_text_section = unlikely_text_section ();
18628       switch_to_section (cold_text_section);
18629       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
18630     }
18631 }
18632
18633 /* A helper function for dwarf2out_finish called through
18634    htab_traverse.  Emit one queued .debug_str string.  */
18635
18636 static int
18637 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18638 {
18639   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18640
18641   if (node->label && node->refcount)
18642     {
18643       switch_to_section (debug_str_section);
18644       ASM_OUTPUT_LABEL (asm_out_file, node->label);
18645       assemble_string (node->str, strlen (node->str) + 1);
18646     }
18647
18648   return 1;
18649 }
18650
18651 #if ENABLE_ASSERT_CHECKING
18652 /* Verify that all marks are clear.  */
18653
18654 static void
18655 verify_marks_clear (dw_die_ref die)
18656 {
18657   dw_die_ref c;
18658
18659   gcc_assert (! die->die_mark);
18660   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
18661 }
18662 #endif /* ENABLE_ASSERT_CHECKING */
18663
18664 /* Clear the marks for a die and its children.
18665    Be cool if the mark isn't set.  */
18666
18667 static void
18668 prune_unmark_dies (dw_die_ref die)
18669 {
18670   dw_die_ref c;
18671
18672   if (die->die_mark)
18673     die->die_mark = 0;
18674   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
18675 }
18676
18677 /* Given DIE that we're marking as used, find any other dies
18678    it references as attributes and mark them as used.  */
18679
18680 static void
18681 prune_unused_types_walk_attribs (dw_die_ref die)
18682 {
18683   dw_attr_ref a;
18684   unsigned ix;
18685
18686   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18687     {
18688       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
18689         {
18690           /* A reference to another DIE.
18691              Make sure that it will get emitted.  */
18692           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
18693         }
18694       /* Set the string's refcount to 0 so that prune_unused_types_mark
18695          accounts properly for it.  */
18696       if (AT_class (a) == dw_val_class_str)
18697         a->dw_attr_val.v.val_str->refcount = 0;
18698     }
18699 }
18700
18701
18702 /* Mark DIE as being used.  If DOKIDS is true, then walk down
18703    to DIE's children.  */
18704
18705 static void
18706 prune_unused_types_mark (dw_die_ref die, int dokids)
18707 {
18708   dw_die_ref c;
18709
18710   if (die->die_mark == 0)
18711     {
18712       /* We haven't done this node yet.  Mark it as used.  */
18713       die->die_mark = 1;
18714
18715       /* We also have to mark its parents as used.
18716          (But we don't want to mark our parents' kids due to this.)  */
18717       if (die->die_parent)
18718         prune_unused_types_mark (die->die_parent, 0);
18719
18720       /* Mark any referenced nodes.  */
18721       prune_unused_types_walk_attribs (die);
18722
18723       /* If this node is a specification,
18724          also mark the definition, if it exists.  */
18725       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
18726         prune_unused_types_mark (die->die_definition, 1);
18727     }
18728
18729   if (dokids && die->die_mark != 2)
18730     {
18731       /* We need to walk the children, but haven't done so yet.
18732          Remember that we've walked the kids.  */
18733       die->die_mark = 2;
18734
18735       /* If this is an array type, we need to make sure our
18736          kids get marked, even if they're types.  */
18737       if (die->die_tag == DW_TAG_array_type)
18738         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
18739       else
18740         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18741     }
18742 }
18743
18744 /* For local classes, look if any static member functions were emitted
18745    and if so, mark them.  */
18746
18747 static void
18748 prune_unused_types_walk_local_classes (dw_die_ref die)
18749 {
18750   dw_die_ref c;
18751
18752   if (die->die_mark == 2)
18753     return;
18754
18755   switch (die->die_tag)
18756     {
18757     case DW_TAG_structure_type:
18758     case DW_TAG_union_type:
18759     case DW_TAG_class_type:
18760       break;
18761
18762     case DW_TAG_subprogram:
18763       if (!get_AT_flag (die, DW_AT_declaration)
18764           || die->die_definition != NULL)
18765         prune_unused_types_mark (die, 1);
18766       return;
18767
18768     default:
18769       return;
18770     }
18771
18772   /* Mark children.  */
18773   FOR_EACH_CHILD (die, c, prune_unused_types_walk_local_classes (c));
18774 }
18775
18776 /* Walk the tree DIE and mark types that we actually use.  */
18777
18778 static void
18779 prune_unused_types_walk (dw_die_ref die)
18780 {
18781   dw_die_ref c;
18782
18783   /* Don't do anything if this node is already marked and
18784      children have been marked as well.  */
18785   if (die->die_mark == 2)
18786     return;
18787
18788   switch (die->die_tag)
18789     {
18790     case DW_TAG_structure_type:
18791     case DW_TAG_union_type:
18792     case DW_TAG_class_type:
18793       if (die->die_perennial_p)
18794         break;
18795
18796       for (c = die->die_parent; c; c = c->die_parent)
18797         if (c->die_tag == DW_TAG_subprogram)
18798           break;
18799
18800       /* Finding used static member functions inside of classes
18801          is needed just for local classes, because for other classes
18802          static member function DIEs with DW_AT_specification
18803          are emitted outside of the DW_TAG_*_type.  If we ever change
18804          it, we'd need to call this even for non-local classes.  */
18805       if (c)
18806         prune_unused_types_walk_local_classes (die);
18807
18808       /* It's a type node --- don't mark it.  */
18809       return;
18810
18811     case DW_TAG_const_type:
18812     case DW_TAG_packed_type:
18813     case DW_TAG_pointer_type:
18814     case DW_TAG_reference_type:
18815     case DW_TAG_volatile_type:
18816     case DW_TAG_typedef:
18817     case DW_TAG_array_type:
18818     case DW_TAG_interface_type:
18819     case DW_TAG_friend:
18820     case DW_TAG_variant_part:
18821     case DW_TAG_enumeration_type:
18822     case DW_TAG_subroutine_type:
18823     case DW_TAG_string_type:
18824     case DW_TAG_set_type:
18825     case DW_TAG_subrange_type:
18826     case DW_TAG_ptr_to_member_type:
18827     case DW_TAG_file_type:
18828       if (die->die_perennial_p)
18829         break;
18830
18831       /* It's a type node --- don't mark it.  */
18832       return;
18833
18834     default:
18835       /* Mark everything else.  */
18836       break;
18837   }
18838
18839   if (die->die_mark == 0)
18840     {
18841       die->die_mark = 1;
18842
18843       /* Now, mark any dies referenced from here.  */
18844       prune_unused_types_walk_attribs (die);
18845     }
18846
18847   die->die_mark = 2;
18848
18849   /* Mark children.  */
18850   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
18851 }
18852
18853 /* Increment the string counts on strings referred to from DIE's
18854    attributes.  */
18855
18856 static void
18857 prune_unused_types_update_strings (dw_die_ref die)
18858 {
18859   dw_attr_ref a;
18860   unsigned ix;
18861
18862   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
18863     if (AT_class (a) == dw_val_class_str)
18864       {
18865         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
18866         s->refcount++;
18867         /* Avoid unnecessarily putting strings that are used less than
18868            twice in the hash table.  */
18869         if (s->refcount
18870             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
18871           {
18872             void ** slot;
18873             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
18874                                              htab_hash_string (s->str),
18875                                              INSERT);
18876             gcc_assert (*slot == NULL);
18877             *slot = s;
18878           }
18879       }
18880 }
18881
18882 /* Remove from the tree DIE any dies that aren't marked.  */
18883
18884 static void
18885 prune_unused_types_prune (dw_die_ref die)
18886 {
18887   dw_die_ref c;
18888
18889   gcc_assert (die->die_mark);
18890   prune_unused_types_update_strings (die);
18891
18892   if (! die->die_child)
18893     return;
18894
18895   c = die->die_child;
18896   do {
18897     dw_die_ref prev = c;
18898     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
18899       if (c == die->die_child)
18900         {
18901           /* No marked children between 'prev' and the end of the list.  */
18902           if (prev == c)
18903             /* No marked children at all.  */
18904             die->die_child = NULL;
18905           else
18906             {
18907               prev->die_sib = c->die_sib;
18908               die->die_child = prev;
18909             }
18910           return;
18911         }
18912
18913     if (c != prev->die_sib)
18914       prev->die_sib = c;
18915     prune_unused_types_prune (c);
18916   } while (c != die->die_child);
18917 }
18918
18919 /* A helper function for dwarf2out_finish called through
18920    htab_traverse.  Clear .debug_str strings that we haven't already
18921    decided to emit.  */
18922
18923 static int
18924 prune_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
18925 {
18926   struct indirect_string_node *node = (struct indirect_string_node *) *h;
18927
18928   if (!node->label || !node->refcount)
18929     htab_clear_slot (debug_str_hash, h);
18930
18931   return 1;
18932 }
18933
18934 /* Remove dies representing declarations that we never use.  */
18935
18936 static void
18937 prune_unused_types (void)
18938 {
18939   unsigned int i;
18940   limbo_die_node *node;
18941   pubname_ref pub;
18942
18943 #if ENABLE_ASSERT_CHECKING
18944   /* All the marks should already be clear.  */
18945   verify_marks_clear (comp_unit_die);
18946   for (node = limbo_die_list; node; node = node->next)
18947     verify_marks_clear (node->die);
18948 #endif /* ENABLE_ASSERT_CHECKING */
18949
18950   /* Mark types that are used in global variables.  */
18951   premark_types_used_by_global_vars ();
18952
18953   /* Set the mark on nodes that are actually used.  */
18954   prune_unused_types_walk (comp_unit_die);
18955   for (node = limbo_die_list; node; node = node->next)
18956     prune_unused_types_walk (node->die);
18957
18958   /* Also set the mark on nodes referenced from the
18959      pubname_table or arange_table.  */
18960   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
18961     prune_unused_types_mark (pub->die, 1);
18962   for (i = 0; i < arange_table_in_use; i++)
18963     prune_unused_types_mark (arange_table[i], 1);
18964
18965   /* Get rid of nodes that aren't marked; and update the string counts.  */
18966   if (debug_str_hash && debug_str_hash_forced)
18967     htab_traverse (debug_str_hash, prune_indirect_string, NULL);
18968   else if (debug_str_hash)
18969     htab_empty (debug_str_hash);
18970   prune_unused_types_prune (comp_unit_die);
18971   for (node = limbo_die_list; node; node = node->next)
18972     prune_unused_types_prune (node->die);
18973
18974   /* Leave the marks clear.  */
18975   prune_unmark_dies (comp_unit_die);
18976   for (node = limbo_die_list; node; node = node->next)
18977     prune_unmark_dies (node->die);
18978 }
18979
18980 /* Set the parameter to true if there are any relative pathnames in
18981    the file table.  */
18982 static int
18983 file_table_relative_p (void ** slot, void *param)
18984 {
18985   bool *p = (bool *) param;
18986   struct dwarf_file_data *d = (struct dwarf_file_data *) *slot;
18987   if (!IS_ABSOLUTE_PATH (d->filename))
18988     {
18989       *p = true;
18990       return 0;
18991     }
18992   return 1;
18993 }
18994
18995 /* Move a DW_AT_MIPS_linkage_name attribute just added to dw_die_ref
18996    to the location it would have been added, should we know its
18997    DECL_ASSEMBLER_NAME when we added other attributes.  This will
18998    probably improve compactness of debug info, removing equivalent
18999    abbrevs, and hide any differences caused by deferring the
19000    computation of the assembler name, triggered by e.g. PCH.  */
19001
19002 static inline void
19003 move_linkage_attr (dw_die_ref die)
19004 {
19005   unsigned ix = VEC_length (dw_attr_node, die->die_attr);
19006   dw_attr_node linkage = *VEC_index (dw_attr_node, die->die_attr, ix - 1);
19007
19008   gcc_assert (linkage.dw_attr == DW_AT_MIPS_linkage_name);
19009
19010   while (--ix > 0)
19011     {
19012       dw_attr_node *prev = VEC_index (dw_attr_node, die->die_attr, ix - 1);
19013
19014       if (prev->dw_attr == DW_AT_decl_line || prev->dw_attr == DW_AT_name)
19015         break;
19016     }
19017
19018   if (ix != VEC_length (dw_attr_node, die->die_attr) - 1)
19019     {
19020       VEC_pop (dw_attr_node, die->die_attr);
19021       VEC_quick_insert (dw_attr_node, die->die_attr, ix, &linkage);
19022     }
19023 }
19024
19025 /* Helper function for resolve_addr, attempt to resolve
19026    one CONST_STRING, return non-zero if not successful.  Similarly verify that
19027    SYMBOL_REFs refer to variables emitted in the current CU.  */
19028
19029 static int
19030 resolve_one_addr (rtx *addr, void *data ATTRIBUTE_UNUSED)
19031 {
19032   rtx rtl = *addr;
19033
19034   if (GET_CODE (rtl) == CONST_STRING)
19035     {
19036       size_t len = strlen (XSTR (rtl, 0)) + 1;
19037       tree t = build_string (len, XSTR (rtl, 0));
19038       tree tlen = build_int_cst (NULL_TREE, len - 1);
19039       TREE_TYPE (t)
19040         = build_array_type (char_type_node, build_index_type (tlen));
19041       rtl = lookup_constant_def (t);
19042       if (!rtl || !MEM_P (rtl))
19043         return 1;
19044       rtl = XEXP (rtl, 0);
19045       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
19046       *addr = rtl;
19047       return 0;
19048     }
19049
19050   if (GET_CODE (rtl) == SYMBOL_REF
19051       && SYMBOL_REF_DECL (rtl)
19052       && TREE_CODE (SYMBOL_REF_DECL (rtl)) == VAR_DECL
19053       && !TREE_ASM_WRITTEN (SYMBOL_REF_DECL (rtl)))
19054     return 1;
19055
19056   if (GET_CODE (rtl) == CONST
19057       && for_each_rtx (&XEXP (rtl, 0), resolve_one_addr, NULL))
19058     return 1;
19059
19060   return 0;
19061 }
19062
19063 /* Helper function for resolve_addr, handle one location
19064    expression, return false if at least one CONST_STRING or SYMBOL_REF in
19065    the location list couldn't be resolved.  */
19066
19067 static bool
19068 resolve_addr_in_expr (dw_loc_descr_ref loc)
19069 {
19070   for (; loc; loc = loc->dw_loc_next)
19071     if ((loc->dw_loc_opc == DW_OP_addr
19072          && resolve_one_addr (&loc->dw_loc_oprnd1.v.val_addr, NULL))
19073         || (loc->dw_loc_opc == DW_OP_implicit_value
19074             && loc->dw_loc_oprnd2.val_class == dw_val_class_addr
19075             && resolve_one_addr (&loc->dw_loc_oprnd2.v.val_addr, NULL)))
19076       return false;
19077   return true;
19078 }
19079
19080 /* Resolve DW_OP_addr and DW_AT_const_value CONST_STRING arguments to
19081    an address in .rodata section if the string literal is emitted there,
19082    or remove the containing location list or replace DW_AT_const_value
19083    with DW_AT_location and empty location expression, if it isn't found
19084    in .rodata.  Similarly for SYMBOL_REFs, keep only those that refer
19085    to something that has been emitted in the current CU.  */
19086
19087 static void
19088 resolve_addr (dw_die_ref die)
19089 {
19090   dw_die_ref c;
19091   dw_attr_ref a;
19092   dw_loc_list_ref curr;
19093   unsigned ix;
19094
19095   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
19096     switch (AT_class (a))
19097       {
19098       case dw_val_class_loc_list:
19099         for (curr = AT_loc_list (a); curr != NULL; curr = curr->dw_loc_next)
19100           if (!resolve_addr_in_expr (curr->expr))
19101             curr->expr = NULL;
19102         break;
19103       case dw_val_class_loc:
19104         if (!resolve_addr_in_expr (AT_loc (a)))
19105           a->dw_attr_val.v.val_loc = NULL;
19106         break;
19107       case dw_val_class_addr:
19108         if (a->dw_attr == DW_AT_const_value
19109             && resolve_one_addr (&a->dw_attr_val.v.val_addr, NULL))
19110           {
19111             a->dw_attr = DW_AT_location;
19112             a->dw_attr_val.val_class = dw_val_class_loc;
19113             a->dw_attr_val.v.val_loc = NULL;
19114           }
19115         break;
19116       default:
19117         break;
19118       }
19119
19120   FOR_EACH_CHILD (die, c, resolve_addr (c));
19121 }
19122
19123 /* Output stuff that dwarf requires at the end of every file,
19124    and generate the DWARF-2 debugging info.  */
19125
19126 static void
19127 dwarf2out_finish (const char *filename)
19128 {
19129   limbo_die_node *node, *next_node;
19130   dw_die_ref die = 0;
19131   unsigned int i;
19132
19133   gen_remaining_tmpl_value_param_die_attribute ();
19134
19135   /* Add the name for the main input file now.  We delayed this from
19136      dwarf2out_init to avoid complications with PCH.  */
19137   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
19138   if (!IS_ABSOLUTE_PATH (filename))
19139     add_comp_dir_attribute (comp_unit_die);
19140   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
19141     {
19142       bool p = false;
19143       htab_traverse (file_table, file_table_relative_p, &p);
19144       if (p)
19145         add_comp_dir_attribute (comp_unit_die);
19146     }
19147
19148   for (i = 0; i < VEC_length (deferred_locations, deferred_locations_list); i++)
19149     {
19150       add_location_or_const_value_attribute (
19151         VEC_index (deferred_locations, deferred_locations_list, i)->die,
19152         VEC_index (deferred_locations, deferred_locations_list, i)->variable,
19153         DW_AT_location);
19154     }
19155
19156   /* Traverse the limbo die list, and add parent/child links.  The only
19157      dies without parents that should be here are concrete instances of
19158      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
19159      For concrete instances, we can get the parent die from the abstract
19160      instance.  */
19161   for (node = limbo_die_list; node; node = next_node)
19162     {
19163       next_node = node->next;
19164       die = node->die;
19165
19166       if (die->die_parent == NULL)
19167         {
19168           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
19169
19170           if (origin)
19171             add_child_die (origin->die_parent, die);
19172           else if (die == comp_unit_die)
19173             ;
19174           else if (errorcount > 0 || sorrycount > 0)
19175             /* It's OK to be confused by errors in the input.  */
19176             add_child_die (comp_unit_die, die);
19177           else
19178             {
19179               /* In certain situations, the lexical block containing a
19180                  nested function can be optimized away, which results
19181                  in the nested function die being orphaned.  Likewise
19182                  with the return type of that nested function.  Force
19183                  this to be a child of the containing function.
19184
19185                  It may happen that even the containing function got fully
19186                  inlined and optimized out.  In that case we are lost and
19187                  assign the empty child.  This should not be big issue as
19188                  the function is likely unreachable too.  */
19189               tree context = NULL_TREE;
19190
19191               gcc_assert (node->created_for);
19192
19193               if (DECL_P (node->created_for))
19194                 context = DECL_CONTEXT (node->created_for);
19195               else if (TYPE_P (node->created_for))
19196                 context = TYPE_CONTEXT (node->created_for);
19197
19198               gcc_assert (context
19199                           && (TREE_CODE (context) == FUNCTION_DECL
19200                               || TREE_CODE (context) == NAMESPACE_DECL));
19201
19202               origin = lookup_decl_die (context);
19203               if (origin)
19204                 add_child_die (origin, die);
19205               else
19206                 add_child_die (comp_unit_die, die);
19207             }
19208         }
19209     }
19210
19211   limbo_die_list = NULL;
19212
19213   resolve_addr (comp_unit_die);
19214
19215   for (node = deferred_asm_name; node; node = node->next)
19216     {
19217       tree decl = node->created_for;
19218       if (DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl))
19219         {
19220           add_AT_string (node->die, DW_AT_MIPS_linkage_name,
19221                          IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
19222           move_linkage_attr (node->die);
19223         }
19224     }
19225
19226   deferred_asm_name = NULL;
19227
19228   /* Walk through the list of incomplete types again, trying once more to
19229      emit full debugging info for them.  */
19230   retry_incomplete_types ();
19231
19232   if (flag_eliminate_unused_debug_types)
19233     prune_unused_types ();
19234
19235   /* Generate separate CUs for each of the include files we've seen.
19236      They will go into limbo_die_list.  */
19237   if (flag_eliminate_dwarf2_dups)
19238     break_out_includes (comp_unit_die);
19239
19240   /* Traverse the DIE's and add add sibling attributes to those DIE's
19241      that have children.  */
19242   add_sibling_attributes (comp_unit_die);
19243   for (node = limbo_die_list; node; node = node->next)
19244     add_sibling_attributes (node->die);
19245
19246   /* Output a terminator label for the .text section.  */
19247   switch_to_section (text_section);
19248   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
19249   if (flag_reorder_blocks_and_partition)
19250     {
19251       switch_to_section (unlikely_text_section ());
19252       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
19253     }
19254
19255   /* We can only use the low/high_pc attributes if all of the code was
19256      in .text.  */
19257   if (!have_multiple_function_sections
19258       || !(dwarf_version >= 3 || !dwarf_strict))
19259     {
19260       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
19261       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
19262     }
19263
19264   else
19265     {
19266       unsigned fde_idx = 0;
19267
19268       /* We need to give .debug_loc and .debug_ranges an appropriate
19269          "base address".  Use zero so that these addresses become
19270          absolute.  Historically, we've emitted the unexpected
19271          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
19272          Emit both to give time for other tools to adapt.  */
19273       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
19274       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
19275
19276       add_AT_range_list (comp_unit_die, DW_AT_ranges,
19277                          add_ranges_by_labels (text_section_label,
19278                                                text_end_label));
19279       if (flag_reorder_blocks_and_partition)
19280         add_ranges_by_labels (cold_text_section_label,
19281                               cold_end_label);
19282
19283       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
19284         {
19285           dw_fde_ref fde = &fde_table[fde_idx];
19286
19287           if (fde->dw_fde_switched_sections)
19288             {
19289               if (!fde->in_std_section)
19290                 add_ranges_by_labels (fde->dw_fde_hot_section_label,
19291                                       fde->dw_fde_hot_section_end_label);
19292               if (!fde->cold_in_std_section)
19293                 add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
19294                                       fde->dw_fde_unlikely_section_end_label);
19295             }
19296           else if (!fde->in_std_section)
19297             add_ranges_by_labels (fde->dw_fde_begin,
19298                                   fde->dw_fde_end);
19299         }
19300
19301       add_ranges (NULL);
19302     }
19303
19304   /* Output location list section if necessary.  */
19305   if (have_location_lists)
19306     {
19307       /* Output the location lists info.  */
19308       switch_to_section (debug_loc_section);
19309       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
19310                                    DEBUG_LOC_SECTION_LABEL, 0);
19311       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
19312       output_location_lists (die);
19313     }
19314
19315   if (debug_info_level >= DINFO_LEVEL_NORMAL)
19316     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
19317                     debug_line_section_label);
19318
19319   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19320     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
19321
19322   /* Output all of the compilation units.  We put the main one last so that
19323      the offsets are available to output_pubnames.  */
19324   for (node = limbo_die_list; node; node = node->next)
19325     output_comp_unit (node->die, 0);
19326
19327   /* Output the main compilation unit if non-empty or if .debug_macinfo
19328      has been emitted.  */
19329   output_comp_unit (comp_unit_die, debug_info_level >= DINFO_LEVEL_VERBOSE);
19330
19331   /* Output the abbreviation table.  */
19332   switch_to_section (debug_abbrev_section);
19333   output_abbrev_section ();
19334
19335   /* Output public names table if necessary.  */
19336   if (!VEC_empty (pubname_entry, pubname_table))
19337     {
19338       switch_to_section (debug_pubnames_section);
19339       output_pubnames (pubname_table);
19340     }
19341
19342   /* Output public types table if necessary.  */
19343   /* ??? Only defined by DWARF3, but emitted by Darwin for DWARF2.
19344      It shouldn't hurt to emit it always, since pure DWARF2 consumers
19345      simply won't look for the section.  */
19346   if (!VEC_empty (pubname_entry, pubtype_table))
19347     {
19348       switch_to_section (debug_pubtypes_section);
19349       output_pubnames (pubtype_table);
19350     }
19351
19352   /* Output the address range information.  We only put functions in the arange
19353      table, so don't write it out if we don't have any.  */
19354   if (fde_table_in_use)
19355     {
19356       switch_to_section (debug_aranges_section);
19357       output_aranges ();
19358     }
19359
19360   /* Output ranges section if necessary.  */
19361   if (ranges_table_in_use)
19362     {
19363       switch_to_section (debug_ranges_section);
19364       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
19365       output_ranges ();
19366     }
19367
19368   /* Output the source line correspondence table.  We must do this
19369      even if there is no line information.  Otherwise, on an empty
19370      translation unit, we will generate a present, but empty,
19371      .debug_info section.  IRIX 6.5 `nm' will then complain when
19372      examining the file.  This is done late so that any filenames
19373      used by the debug_info section are marked as 'used'.  */
19374   if (! DWARF2_ASM_LINE_DEBUG_INFO)
19375     {
19376       switch_to_section (debug_line_section);
19377       output_line_info ();
19378     }
19379
19380   /* Have to end the macro section.  */
19381   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
19382     {
19383       switch_to_section (debug_macinfo_section);
19384       dw2_asm_output_data (1, 0, "End compilation unit");
19385     }
19386
19387   /* If we emitted any DW_FORM_strp form attribute, output the string
19388      table too.  */
19389   if (debug_str_hash)
19390     htab_traverse (debug_str_hash, output_indirect_string, NULL);
19391 }
19392 #else
19393
19394 /* This should never be used, but its address is needed for comparisons.  */
19395 const struct gcc_debug_hooks dwarf2_debug_hooks =
19396 {
19397   0,            /* init */
19398   0,            /* finish */
19399   0,            /* define */
19400   0,            /* undef */
19401   0,            /* start_source_file */
19402   0,            /* end_source_file */
19403   0,            /* begin_block */
19404   0,            /* end_block */
19405   0,            /* ignore_block */
19406   0,            /* source_line */
19407   0,            /* begin_prologue */
19408   0,            /* end_prologue */
19409   0,            /* end_epilogue */
19410   0,            /* begin_function */
19411   0,            /* end_function */
19412   0,            /* function_decl */
19413   0,            /* global_decl */
19414   0,            /* type_decl */
19415   0,            /* imported_module_or_decl */
19416   0,            /* deferred_inline_function */
19417   0,            /* outlining_inline_function */
19418   0,            /* label */
19419   0,            /* handle_pch */
19420   0,            /* var_location */
19421   0,            /* switch_text_section */
19422   0,            /* set_name */
19423   0             /* start_end_main_source_file */
19424 };
19425
19426 #endif /* DWARF2_DEBUGGING_INFO */
19427
19428 #include "gt-dwarf2out.h"