OSDN Git Service

PR testsuite/34168
[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 Free Software Foundation, Inc.
4    Contributed by Gary Funck (gary@intrepid.com).
5    Derived from DWARF 1 implementation of Ron Guilmette (rfg@monkeys.com).
6    Extensively modified by Jason Merrill (jason@cygnus.com).
7
8 This file is part of GCC.
9
10 GCC is free software; you can redistribute it and/or modify it under
11 the terms of the GNU General Public License as published by the Free
12 Software Foundation; either version 3, or (at your option) any later
13 version.
14
15 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
16 WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
18 for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GCC; see the file COPYING3.  If not see
22 <http://www.gnu.org/licenses/>.  */
23
24 /* TODO: Emit .debug_line header even when there are no functions, since
25            the file numbers are used by .debug_info.  Alternately, leave
26            out locations for types and decls.
27          Avoid talking about ctors and op= for PODs.
28          Factor out common prologue sequences into multiple CIEs.  */
29
30 /* The first part of this file deals with the DWARF 2 frame unwind
31    information, which is also used by the GCC efficient exception handling
32    mechanism.  The second part, controlled only by an #ifdef
33    DWARF2_DEBUGGING_INFO, deals with the other DWARF 2 debugging
34    information.  */
35
36 /* DWARF2 Abbreviation Glossary:
37
38    CFA = Canonical Frame Address
39            a fixed address on the stack which identifies a call frame.
40            We define it to be the value of SP just before the call insn.
41            The CFA register and offset, which may change during the course
42            of the function, are used to calculate its value at runtime.
43
44    CFI = Call Frame Instruction
45            an instruction for the DWARF2 abstract machine
46
47    CIE = Common Information Entry
48            information describing information common to one or more FDEs
49
50    DIE = Debugging Information Entry
51
52    FDE = Frame Description Entry
53            information describing the stack call frame, in particular,
54            how to restore registers
55
56    DW_CFA_... = DWARF2 CFA call frame instruction
57    DW_TAG_... = DWARF2 DIE tag */
58
59 #include "config.h"
60 #include "system.h"
61 #include "coretypes.h"
62 #include "tm.h"
63 #include "tree.h"
64 #include "version.h"
65 #include "flags.h"
66 #include "real.h"
67 #include "rtl.h"
68 #include "hard-reg-set.h"
69 #include "regs.h"
70 #include "insn-config.h"
71 #include "reload.h"
72 #include "function.h"
73 #include "output.h"
74 #include "expr.h"
75 #include "libfuncs.h"
76 #include "except.h"
77 #include "dwarf2.h"
78 #include "dwarf2out.h"
79 #include "dwarf2asm.h"
80 #include "toplev.h"
81 #include "varray.h"
82 #include "ggc.h"
83 #include "md5.h"
84 #include "tm_p.h"
85 #include "diagnostic.h"
86 #include "debug.h"
87 #include "target.h"
88 #include "langhooks.h"
89 #include "hashtab.h"
90 #include "cgraph.h"
91 #include "input.h"
92
93 #ifdef DWARF2_DEBUGGING_INFO
94 static void dwarf2out_source_line (unsigned int, const char *);
95 #endif
96
97 #ifndef DWARF2_FRAME_INFO
98 # ifdef DWARF2_DEBUGGING_INFO
99 #  define DWARF2_FRAME_INFO \
100   (write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
101 # else
102 #  define DWARF2_FRAME_INFO 0
103 # endif
104 #endif
105
106 /* Map register numbers held in the call frame info that gcc has
107    collected using DWARF_FRAME_REGNUM to those that should be output in
108    .debug_frame and .eh_frame.  */
109 #ifndef DWARF2_FRAME_REG_OUT
110 #define DWARF2_FRAME_REG_OUT(REGNO, FOR_EH) (REGNO)
111 #endif
112
113 /* Decide whether we want to emit frame unwind information for the current
114    translation unit.  */
115
116 int
117 dwarf2out_do_frame (void)
118 {
119   /* We want to emit correct CFA location expressions or lists, so we
120      have to return true if we're going to output debug info, even if
121      we're not going to output frame or unwind info.  */
122   return (write_symbols == DWARF2_DEBUG
123           || write_symbols == VMS_AND_DWARF2_DEBUG
124           || DWARF2_FRAME_INFO
125 #ifdef DWARF2_UNWIND_INFO
126           || (DWARF2_UNWIND_INFO
127               && (flag_unwind_tables
128                   || (flag_exceptions && ! USING_SJLJ_EXCEPTIONS)))
129 #endif
130           );
131 }
132
133 /* The size of the target's pointer type.  */
134 #ifndef PTR_SIZE
135 #define PTR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
136 #endif
137
138 /* Array of RTXes referenced by the debugging information, which therefore
139    must be kept around forever.  */
140 static GTY(()) VEC(rtx,gc) *used_rtx_array;
141
142 /* A pointer to the base of a list of incomplete types which might be
143    completed at some later time.  incomplete_types_list needs to be a
144    VEC(tree,gc) because we want to tell the garbage collector about
145    it.  */
146 static GTY(()) VEC(tree,gc) *incomplete_types;
147
148 /* A pointer to the base of a table of references to declaration
149    scopes.  This table is a display which tracks the nesting
150    of declaration scopes at the current scope and containing
151    scopes.  This table is used to find the proper place to
152    define type declaration DIE's.  */
153 static GTY(()) VEC(tree,gc) *decl_scope_table;
154
155 /* Pointers to various DWARF2 sections.  */
156 static GTY(()) section *debug_info_section;
157 static GTY(()) section *debug_abbrev_section;
158 static GTY(()) section *debug_aranges_section;
159 static GTY(()) section *debug_macinfo_section;
160 static GTY(()) section *debug_line_section;
161 static GTY(()) section *debug_loc_section;
162 static GTY(()) section *debug_pubnames_section;
163 static GTY(()) section *debug_pubtypes_section;
164 static GTY(()) section *debug_str_section;
165 static GTY(()) section *debug_ranges_section;
166 static GTY(()) section *debug_frame_section;
167
168 /* How to start an assembler comment.  */
169 #ifndef ASM_COMMENT_START
170 #define ASM_COMMENT_START ";#"
171 #endif
172
173 typedef struct dw_cfi_struct *dw_cfi_ref;
174 typedef struct dw_fde_struct *dw_fde_ref;
175 typedef union  dw_cfi_oprnd_struct *dw_cfi_oprnd_ref;
176
177 /* Call frames are described using a sequence of Call Frame
178    Information instructions.  The register number, offset
179    and address fields are provided as possible operands;
180    their use is selected by the opcode field.  */
181
182 enum dw_cfi_oprnd_type {
183   dw_cfi_oprnd_unused,
184   dw_cfi_oprnd_reg_num,
185   dw_cfi_oprnd_offset,
186   dw_cfi_oprnd_addr,
187   dw_cfi_oprnd_loc
188 };
189
190 typedef union dw_cfi_oprnd_struct GTY(())
191 {
192   unsigned int GTY ((tag ("dw_cfi_oprnd_reg_num"))) dw_cfi_reg_num;
193   HOST_WIDE_INT GTY ((tag ("dw_cfi_oprnd_offset"))) dw_cfi_offset;
194   const char * GTY ((tag ("dw_cfi_oprnd_addr"))) dw_cfi_addr;
195   struct dw_loc_descr_struct * GTY ((tag ("dw_cfi_oprnd_loc"))) dw_cfi_loc;
196 }
197 dw_cfi_oprnd;
198
199 typedef struct dw_cfi_struct GTY(())
200 {
201   dw_cfi_ref dw_cfi_next;
202   enum dwarf_call_frame_info dw_cfi_opc;
203   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd1_desc (%1.dw_cfi_opc)")))
204     dw_cfi_oprnd1;
205   dw_cfi_oprnd GTY ((desc ("dw_cfi_oprnd2_desc (%1.dw_cfi_opc)")))
206     dw_cfi_oprnd2;
207 }
208 dw_cfi_node;
209
210 /* This is how we define the location of the CFA. We use to handle it
211    as REG + OFFSET all the time,  but now it can be more complex.
212    It can now be either REG + CFA_OFFSET or *(REG + BASE_OFFSET) + CFA_OFFSET.
213    Instead of passing around REG and OFFSET, we pass a copy
214    of this structure.  */
215 typedef struct cfa_loc GTY(())
216 {
217   HOST_WIDE_INT offset;
218   HOST_WIDE_INT base_offset;
219   unsigned int reg;
220   int indirect;            /* 1 if CFA is accessed via a dereference.  */
221 } dw_cfa_location;
222
223 /* All call frame descriptions (FDE's) in the GCC generated DWARF
224    refer to a single Common Information Entry (CIE), defined at
225    the beginning of the .debug_frame section.  This use of a single
226    CIE obviates the need to keep track of multiple CIE's
227    in the DWARF generation routines below.  */
228
229 typedef struct dw_fde_struct GTY(())
230 {
231   tree decl;
232   const char *dw_fde_begin;
233   const char *dw_fde_current_label;
234   const char *dw_fde_end;
235   const char *dw_fde_hot_section_label;
236   const char *dw_fde_hot_section_end_label;
237   const char *dw_fde_unlikely_section_label;
238   const char *dw_fde_unlikely_section_end_label;
239   bool dw_fde_switched_sections;
240   dw_cfi_ref dw_fde_cfi;
241   unsigned funcdef_number;
242   unsigned all_throwers_are_sibcalls : 1;
243   unsigned nothrow : 1;
244   unsigned uses_eh_lsda : 1;
245 }
246 dw_fde_node;
247
248 /* Maximum size (in bytes) of an artificially generated label.  */
249 #define MAX_ARTIFICIAL_LABEL_BYTES      30
250
251 /* The size of addresses as they appear in the Dwarf 2 data.
252    Some architectures use word addresses to refer to code locations,
253    but Dwarf 2 info always uses byte addresses.  On such machines,
254    Dwarf 2 addresses need to be larger than the architecture's
255    pointers.  */
256 #ifndef DWARF2_ADDR_SIZE
257 #define DWARF2_ADDR_SIZE (POINTER_SIZE / BITS_PER_UNIT)
258 #endif
259
260 /* The size in bytes of a DWARF field indicating an offset or length
261    relative to a debug info section, specified to be 4 bytes in the
262    DWARF-2 specification.  The SGI/MIPS ABI defines it to be the same
263    as PTR_SIZE.  */
264
265 #ifndef DWARF_OFFSET_SIZE
266 #define DWARF_OFFSET_SIZE 4
267 #endif
268
269 /* According to the (draft) DWARF 3 specification, the initial length
270    should either be 4 or 12 bytes.  When it's 12 bytes, the first 4
271    bytes are 0xffffffff, followed by the length stored in the next 8
272    bytes.
273
274    However, the SGI/MIPS ABI uses an initial length which is equal to
275    DWARF_OFFSET_SIZE.  It is defined (elsewhere) accordingly.  */
276
277 #ifndef DWARF_INITIAL_LENGTH_SIZE
278 #define DWARF_INITIAL_LENGTH_SIZE (DWARF_OFFSET_SIZE == 4 ? 4 : 12)
279 #endif
280
281 #define DWARF_VERSION 2
282
283 /* Round SIZE up to the nearest BOUNDARY.  */
284 #define DWARF_ROUND(SIZE,BOUNDARY) \
285   ((((SIZE) + (BOUNDARY) - 1) / (BOUNDARY)) * (BOUNDARY))
286
287 /* Offsets recorded in opcodes are a multiple of this alignment factor.  */
288 #ifndef DWARF_CIE_DATA_ALIGNMENT
289 #ifdef STACK_GROWS_DOWNWARD
290 #define DWARF_CIE_DATA_ALIGNMENT (-((int) UNITS_PER_WORD))
291 #else
292 #define DWARF_CIE_DATA_ALIGNMENT ((int) UNITS_PER_WORD)
293 #endif
294 #endif
295
296 /* CIE identifier.  */
297 #if HOST_BITS_PER_WIDE_INT >= 64
298 #define DWARF_CIE_ID \
299   (unsigned HOST_WIDE_INT) (DWARF_OFFSET_SIZE == 4 ? DW_CIE_ID : DW64_CIE_ID)
300 #else
301 #define DWARF_CIE_ID DW_CIE_ID
302 #endif
303
304 /* A pointer to the base of a table that contains frame description
305    information for each routine.  */
306 static GTY((length ("fde_table_allocated"))) dw_fde_ref fde_table;
307
308 /* Number of elements currently allocated for fde_table.  */
309 static GTY(()) unsigned fde_table_allocated;
310
311 /* Number of elements in fde_table currently in use.  */
312 static GTY(()) unsigned fde_table_in_use;
313
314 /* Size (in elements) of increments by which we may expand the
315    fde_table.  */
316 #define FDE_TABLE_INCREMENT 256
317
318 /* A list of call frame insns for the CIE.  */
319 static GTY(()) dw_cfi_ref cie_cfi_head;
320
321 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
322 /* Some DWARF extensions (e.g., MIPS/SGI) implement a subprogram
323    attribute that accelerates the lookup of the FDE associated
324    with the subprogram.  This variable holds the table index of the FDE
325    associated with the current function (body) definition.  */
326 static unsigned current_funcdef_fde;
327 #endif
328
329 struct indirect_string_node GTY(())
330 {
331   const char *str;
332   unsigned int refcount;
333   unsigned int form;
334   char *label;
335 };
336
337 static GTY ((param_is (struct indirect_string_node))) htab_t debug_str_hash;
338
339 static GTY(()) int dw2_string_counter;
340 static GTY(()) unsigned long dwarf2out_cfi_label_num;
341
342 /* True if the compilation unit places functions in more than one section.  */
343 static GTY(()) bool have_multiple_function_sections = false;
344
345 /* Whether the default text and cold text sections have been used at all.  */
346
347 static GTY(()) bool text_section_used = false;
348 static GTY(()) bool cold_text_section_used = false;
349
350 /* The default cold text section.  */
351 static GTY(()) section *cold_text_section;
352
353 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
354
355 /* Forward declarations for functions defined in this file.  */
356
357 static char *stripattributes (const char *);
358 static const char *dwarf_cfi_name (unsigned);
359 static dw_cfi_ref new_cfi (void);
360 static void add_cfi (dw_cfi_ref *, dw_cfi_ref);
361 static void add_fde_cfi (const char *, dw_cfi_ref);
362 static void lookup_cfa_1 (dw_cfi_ref, dw_cfa_location *);
363 static void lookup_cfa (dw_cfa_location *);
364 static void reg_save (const char *, unsigned, unsigned, HOST_WIDE_INT);
365 #ifdef DWARF2_UNWIND_INFO
366 static void initial_return_save (rtx);
367 #endif
368 static HOST_WIDE_INT stack_adjust_offset (const_rtx);
369 static void output_cfi (dw_cfi_ref, dw_fde_ref, int);
370 static void output_call_frame_info (int);
371 static void dwarf2out_note_section_used (void);
372 static void dwarf2out_stack_adjust (rtx, bool);
373 static void flush_queued_reg_saves (void);
374 static bool clobbers_queued_reg_save (const_rtx);
375 static void dwarf2out_frame_debug_expr (rtx, const char *);
376
377 /* Support for complex CFA locations.  */
378 static void output_cfa_loc (dw_cfi_ref);
379 static void get_cfa_from_loc_descr (dw_cfa_location *,
380                                     struct dw_loc_descr_struct *);
381 static struct dw_loc_descr_struct *build_cfa_loc
382   (dw_cfa_location *, HOST_WIDE_INT);
383 static void def_cfa_1 (const char *, dw_cfa_location *);
384
385 /* How to start an assembler comment.  */
386 #ifndef ASM_COMMENT_START
387 #define ASM_COMMENT_START ";#"
388 #endif
389
390 /* Data and reference forms for relocatable data.  */
391 #define DW_FORM_data (DWARF_OFFSET_SIZE == 8 ? DW_FORM_data8 : DW_FORM_data4)
392 #define DW_FORM_ref (DWARF_OFFSET_SIZE == 8 ? DW_FORM_ref8 : DW_FORM_ref4)
393
394 #ifndef DEBUG_FRAME_SECTION
395 #define DEBUG_FRAME_SECTION     ".debug_frame"
396 #endif
397
398 #ifndef FUNC_BEGIN_LABEL
399 #define FUNC_BEGIN_LABEL        "LFB"
400 #endif
401
402 #ifndef FUNC_END_LABEL
403 #define FUNC_END_LABEL          "LFE"
404 #endif
405
406 #ifndef FRAME_BEGIN_LABEL
407 #define FRAME_BEGIN_LABEL       "Lframe"
408 #endif
409 #define CIE_AFTER_SIZE_LABEL    "LSCIE"
410 #define CIE_END_LABEL           "LECIE"
411 #define FDE_LABEL               "LSFDE"
412 #define FDE_AFTER_SIZE_LABEL    "LASFDE"
413 #define FDE_END_LABEL           "LEFDE"
414 #define LINE_NUMBER_BEGIN_LABEL "LSLT"
415 #define LINE_NUMBER_END_LABEL   "LELT"
416 #define LN_PROLOG_AS_LABEL      "LASLTP"
417 #define LN_PROLOG_END_LABEL     "LELTP"
418 #define DIE_LABEL_PREFIX        "DW"
419
420 /* The DWARF 2 CFA column which tracks the return address.  Normally this
421    is the column for PC, or the first column after all of the hard
422    registers.  */
423 #ifndef DWARF_FRAME_RETURN_COLUMN
424 #ifdef PC_REGNUM
425 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGNUM (PC_REGNUM)
426 #else
427 #define DWARF_FRAME_RETURN_COLUMN       DWARF_FRAME_REGISTERS
428 #endif
429 #endif
430
431 /* The mapping from gcc register number to DWARF 2 CFA column number.  By
432    default, we just provide columns for all registers.  */
433 #ifndef DWARF_FRAME_REGNUM
434 #define DWARF_FRAME_REGNUM(REG) DBX_REGISTER_NUMBER (REG)
435 #endif
436 \f
437 /* Hook used by __throw.  */
438
439 rtx
440 expand_builtin_dwarf_sp_column (void)
441 {
442   unsigned int dwarf_regnum = DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM);
443   return GEN_INT (DWARF2_FRAME_REG_OUT (dwarf_regnum, 1));
444 }
445
446 /* Return a pointer to a copy of the section string name S with all
447    attributes stripped off, and an asterisk prepended (for assemble_name).  */
448
449 static inline char *
450 stripattributes (const char *s)
451 {
452   char *stripped = XNEWVEC (char, strlen (s) + 2);
453   char *p = stripped;
454
455   *p++ = '*';
456
457   while (*s && *s != ',')
458     *p++ = *s++;
459
460   *p = '\0';
461   return stripped;
462 }
463
464 /* MEM is a memory reference for the register size table, each element of
465    which has mode MODE.  Initialize column C as a return address column.  */
466
467 static void
468 init_return_column_size (enum machine_mode mode, rtx mem, unsigned int c)
469 {
470   HOST_WIDE_INT offset = c * GET_MODE_SIZE (mode);
471   HOST_WIDE_INT size = GET_MODE_SIZE (Pmode);
472   emit_move_insn (adjust_address (mem, mode, offset), GEN_INT (size));
473 }
474
475 /* Generate code to initialize the register size table.  */
476
477 void
478 expand_builtin_init_dwarf_reg_sizes (tree address)
479 {
480   unsigned int i;
481   enum machine_mode mode = TYPE_MODE (char_type_node);
482   rtx addr = expand_normal (address);
483   rtx mem = gen_rtx_MEM (BLKmode, addr);
484   bool wrote_return_column = false;
485
486   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
487     {
488       int rnum = DWARF2_FRAME_REG_OUT (DWARF_FRAME_REGNUM (i), 1);
489
490       if (rnum < DWARF_FRAME_REGISTERS)
491         {
492           HOST_WIDE_INT offset = rnum * GET_MODE_SIZE (mode);
493           enum machine_mode save_mode = reg_raw_mode[i];
494           HOST_WIDE_INT size;
495
496           if (HARD_REGNO_CALL_PART_CLOBBERED (i, save_mode))
497             save_mode = choose_hard_reg_mode (i, 1, true);
498           if (DWARF_FRAME_REGNUM (i) == DWARF_FRAME_RETURN_COLUMN)
499             {
500               if (save_mode == VOIDmode)
501                 continue;
502               wrote_return_column = true;
503             }
504           size = GET_MODE_SIZE (save_mode);
505           if (offset < 0)
506             continue;
507
508           emit_move_insn (adjust_address (mem, mode, offset),
509                           gen_int_mode (size, mode));
510         }
511     }
512
513   if (!wrote_return_column)
514     init_return_column_size (mode, mem, DWARF_FRAME_RETURN_COLUMN);
515
516 #ifdef DWARF_ALT_FRAME_RETURN_COLUMN
517   init_return_column_size (mode, mem, DWARF_ALT_FRAME_RETURN_COLUMN);
518 #endif
519
520   targetm.init_dwarf_reg_sizes_extra (address);
521 }
522
523 /* Convert a DWARF call frame info. operation to its string name */
524
525 static const char *
526 dwarf_cfi_name (unsigned int cfi_opc)
527 {
528   switch (cfi_opc)
529     {
530     case DW_CFA_advance_loc:
531       return "DW_CFA_advance_loc";
532     case DW_CFA_offset:
533       return "DW_CFA_offset";
534     case DW_CFA_restore:
535       return "DW_CFA_restore";
536     case DW_CFA_nop:
537       return "DW_CFA_nop";
538     case DW_CFA_set_loc:
539       return "DW_CFA_set_loc";
540     case DW_CFA_advance_loc1:
541       return "DW_CFA_advance_loc1";
542     case DW_CFA_advance_loc2:
543       return "DW_CFA_advance_loc2";
544     case DW_CFA_advance_loc4:
545       return "DW_CFA_advance_loc4";
546     case DW_CFA_offset_extended:
547       return "DW_CFA_offset_extended";
548     case DW_CFA_restore_extended:
549       return "DW_CFA_restore_extended";
550     case DW_CFA_undefined:
551       return "DW_CFA_undefined";
552     case DW_CFA_same_value:
553       return "DW_CFA_same_value";
554     case DW_CFA_register:
555       return "DW_CFA_register";
556     case DW_CFA_remember_state:
557       return "DW_CFA_remember_state";
558     case DW_CFA_restore_state:
559       return "DW_CFA_restore_state";
560     case DW_CFA_def_cfa:
561       return "DW_CFA_def_cfa";
562     case DW_CFA_def_cfa_register:
563       return "DW_CFA_def_cfa_register";
564     case DW_CFA_def_cfa_offset:
565       return "DW_CFA_def_cfa_offset";
566
567     /* DWARF 3 */
568     case DW_CFA_def_cfa_expression:
569       return "DW_CFA_def_cfa_expression";
570     case DW_CFA_expression:
571       return "DW_CFA_expression";
572     case DW_CFA_offset_extended_sf:
573       return "DW_CFA_offset_extended_sf";
574     case DW_CFA_def_cfa_sf:
575       return "DW_CFA_def_cfa_sf";
576     case DW_CFA_def_cfa_offset_sf:
577       return "DW_CFA_def_cfa_offset_sf";
578
579     /* SGI/MIPS specific */
580     case DW_CFA_MIPS_advance_loc8:
581       return "DW_CFA_MIPS_advance_loc8";
582
583     /* GNU extensions */
584     case DW_CFA_GNU_window_save:
585       return "DW_CFA_GNU_window_save";
586     case DW_CFA_GNU_args_size:
587       return "DW_CFA_GNU_args_size";
588     case DW_CFA_GNU_negative_offset_extended:
589       return "DW_CFA_GNU_negative_offset_extended";
590
591     default:
592       return "DW_CFA_<unknown>";
593     }
594 }
595
596 /* Return a pointer to a newly allocated Call Frame Instruction.  */
597
598 static inline dw_cfi_ref
599 new_cfi (void)
600 {
601   dw_cfi_ref cfi = ggc_alloc (sizeof (dw_cfi_node));
602
603   cfi->dw_cfi_next = NULL;
604   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = 0;
605   cfi->dw_cfi_oprnd2.dw_cfi_reg_num = 0;
606
607   return cfi;
608 }
609
610 /* Add a Call Frame Instruction to list of instructions.  */
611
612 static inline void
613 add_cfi (dw_cfi_ref *list_head, dw_cfi_ref cfi)
614 {
615   dw_cfi_ref *p;
616
617   /* Find the end of the chain.  */
618   for (p = list_head; (*p) != NULL; p = &(*p)->dw_cfi_next)
619     ;
620
621   *p = cfi;
622 }
623
624 /* Generate a new label for the CFI info to refer to.  */
625
626 char *
627 dwarf2out_cfi_label (void)
628 {
629   static char label[20];
630
631   ASM_GENERATE_INTERNAL_LABEL (label, "LCFI", dwarf2out_cfi_label_num++);
632   ASM_OUTPUT_LABEL (asm_out_file, label);
633   return label;
634 }
635
636 /* Add CFI to the current fde at the PC value indicated by LABEL if specified,
637    or to the CIE if LABEL is NULL.  */
638
639 static void
640 add_fde_cfi (const char *label, dw_cfi_ref cfi)
641 {
642   if (label)
643     {
644       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
645
646       if (*label == 0)
647         label = dwarf2out_cfi_label ();
648
649       if (fde->dw_fde_current_label == NULL
650           || strcmp (label, fde->dw_fde_current_label) != 0)
651         {
652           dw_cfi_ref xcfi;
653
654           label = xstrdup (label);
655
656           /* Set the location counter to the new label.  */
657           xcfi = new_cfi ();
658           /* If we have a current label, advance from there, otherwise
659              set the location directly using set_loc.  */
660           xcfi->dw_cfi_opc = fde->dw_fde_current_label
661                              ? DW_CFA_advance_loc4
662                              : DW_CFA_set_loc;
663           xcfi->dw_cfi_oprnd1.dw_cfi_addr = label;
664           add_cfi (&fde->dw_fde_cfi, xcfi);
665
666           fde->dw_fde_current_label = label;
667         }
668
669       add_cfi (&fde->dw_fde_cfi, cfi);
670     }
671
672   else
673     add_cfi (&cie_cfi_head, cfi);
674 }
675
676 /* Subroutine of lookup_cfa.  */
677
678 static void
679 lookup_cfa_1 (dw_cfi_ref cfi, dw_cfa_location *loc)
680 {
681   switch (cfi->dw_cfi_opc)
682     {
683     case DW_CFA_def_cfa_offset:
684       loc->offset = cfi->dw_cfi_oprnd1.dw_cfi_offset;
685       break;
686     case DW_CFA_def_cfa_offset_sf:
687       loc->offset
688         = cfi->dw_cfi_oprnd1.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
689       break;
690     case DW_CFA_def_cfa_register:
691       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
692       break;
693     case DW_CFA_def_cfa:
694       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
695       loc->offset = cfi->dw_cfi_oprnd2.dw_cfi_offset;
696       break;
697     case DW_CFA_def_cfa_sf:
698       loc->reg = cfi->dw_cfi_oprnd1.dw_cfi_reg_num;
699       loc->offset
700         = cfi->dw_cfi_oprnd2.dw_cfi_offset * DWARF_CIE_DATA_ALIGNMENT;
701       break;
702     case DW_CFA_def_cfa_expression:
703       get_cfa_from_loc_descr (loc, cfi->dw_cfi_oprnd1.dw_cfi_loc);
704       break;
705     default:
706       break;
707     }
708 }
709
710 /* Find the previous value for the CFA.  */
711
712 static void
713 lookup_cfa (dw_cfa_location *loc)
714 {
715   dw_cfi_ref cfi;
716
717   loc->reg = INVALID_REGNUM;
718   loc->offset = 0;
719   loc->indirect = 0;
720   loc->base_offset = 0;
721
722   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
723     lookup_cfa_1 (cfi, loc);
724
725   if (fde_table_in_use)
726     {
727       dw_fde_ref fde = &fde_table[fde_table_in_use - 1];
728       for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
729         lookup_cfa_1 (cfi, loc);
730     }
731 }
732
733 /* The current rule for calculating the DWARF2 canonical frame address.  */
734 static dw_cfa_location cfa;
735
736 /* The register used for saving registers to the stack, and its offset
737    from the CFA.  */
738 static dw_cfa_location cfa_store;
739
740 /* The running total of the size of arguments pushed onto the stack.  */
741 static HOST_WIDE_INT args_size;
742
743 /* The last args_size we actually output.  */
744 static HOST_WIDE_INT old_args_size;
745
746 /* Entry point to update the canonical frame address (CFA).
747    LABEL is passed to add_fde_cfi.  The value of CFA is now to be
748    calculated from REG+OFFSET.  */
749
750 void
751 dwarf2out_def_cfa (const char *label, unsigned int reg, HOST_WIDE_INT offset)
752 {
753   dw_cfa_location loc;
754   loc.indirect = 0;
755   loc.base_offset = 0;
756   loc.reg = reg;
757   loc.offset = offset;
758   def_cfa_1 (label, &loc);
759 }
760
761 /* Determine if two dw_cfa_location structures define the same data.  */
762
763 static bool
764 cfa_equal_p (const dw_cfa_location *loc1, const dw_cfa_location *loc2)
765 {
766   return (loc1->reg == loc2->reg
767           && loc1->offset == loc2->offset
768           && loc1->indirect == loc2->indirect
769           && (loc1->indirect == 0
770               || loc1->base_offset == loc2->base_offset));
771 }
772
773 /* This routine does the actual work.  The CFA is now calculated from
774    the dw_cfa_location structure.  */
775
776 static void
777 def_cfa_1 (const char *label, dw_cfa_location *loc_p)
778 {
779   dw_cfi_ref cfi;
780   dw_cfa_location old_cfa, loc;
781
782   cfa = *loc_p;
783   loc = *loc_p;
784
785   if (cfa_store.reg == loc.reg && loc.indirect == 0)
786     cfa_store.offset = loc.offset;
787
788   loc.reg = DWARF_FRAME_REGNUM (loc.reg);
789   lookup_cfa (&old_cfa);
790
791   /* If nothing changed, no need to issue any call frame instructions.  */
792   if (cfa_equal_p (&loc, &old_cfa))
793     return;
794
795   cfi = new_cfi ();
796
797   if (loc.reg == old_cfa.reg && !loc.indirect)
798     {
799       /* Construct a "DW_CFA_def_cfa_offset <offset>" instruction, indicating
800          the CFA register did not change but the offset did.  */
801       if (loc.offset < 0)
802         {
803           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
804           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
805
806           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset_sf;
807           cfi->dw_cfi_oprnd1.dw_cfi_offset = f_offset;
808         }
809       else
810         {
811           cfi->dw_cfi_opc = DW_CFA_def_cfa_offset;
812           cfi->dw_cfi_oprnd1.dw_cfi_offset = loc.offset;
813         }
814     }
815
816 #ifndef MIPS_DEBUGGING_INFO  /* SGI dbx thinks this means no offset.  */
817   else if (loc.offset == old_cfa.offset
818            && old_cfa.reg != INVALID_REGNUM
819            && !loc.indirect)
820     {
821       /* Construct a "DW_CFA_def_cfa_register <register>" instruction,
822          indicating the CFA register has changed to <register> but the
823          offset has not changed.  */
824       cfi->dw_cfi_opc = DW_CFA_def_cfa_register;
825       cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
826     }
827 #endif
828
829   else if (loc.indirect == 0)
830     {
831       /* Construct a "DW_CFA_def_cfa <register> <offset>" instruction,
832          indicating the CFA register has changed to <register> with
833          the specified offset.  */
834       if (loc.offset < 0)
835         {
836           HOST_WIDE_INT f_offset = loc.offset / DWARF_CIE_DATA_ALIGNMENT;
837           gcc_assert (f_offset * DWARF_CIE_DATA_ALIGNMENT == loc.offset);
838
839           cfi->dw_cfi_opc = DW_CFA_def_cfa_sf;
840           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
841           cfi->dw_cfi_oprnd2.dw_cfi_offset = f_offset;
842         }
843       else
844         {
845           cfi->dw_cfi_opc = DW_CFA_def_cfa;
846           cfi->dw_cfi_oprnd1.dw_cfi_reg_num = loc.reg;
847           cfi->dw_cfi_oprnd2.dw_cfi_offset = loc.offset;
848         }
849     }
850   else
851     {
852       /* Construct a DW_CFA_def_cfa_expression instruction to
853          calculate the CFA using a full location expression since no
854          register-offset pair is available.  */
855       struct dw_loc_descr_struct *loc_list;
856
857       cfi->dw_cfi_opc = DW_CFA_def_cfa_expression;
858       loc_list = build_cfa_loc (&loc, 0);
859       cfi->dw_cfi_oprnd1.dw_cfi_loc = loc_list;
860     }
861
862   add_fde_cfi (label, cfi);
863 }
864
865 /* Add the CFI for saving a register.  REG is the CFA column number.
866    LABEL is passed to add_fde_cfi.
867    If SREG is -1, the register is saved at OFFSET from the CFA;
868    otherwise it is saved in SREG.  */
869
870 static void
871 reg_save (const char *label, unsigned int reg, unsigned int sreg, HOST_WIDE_INT offset)
872 {
873   dw_cfi_ref cfi = new_cfi ();
874
875   cfi->dw_cfi_oprnd1.dw_cfi_reg_num = reg;
876
877   if (sreg == INVALID_REGNUM)
878     {
879       if (reg & ~0x3f)
880         /* The register number won't fit in 6 bits, so we have to use
881            the long form.  */
882         cfi->dw_cfi_opc = DW_CFA_offset_extended;
883       else
884         cfi->dw_cfi_opc = DW_CFA_offset;
885
886 #ifdef ENABLE_CHECKING
887       {
888         /* If we get an offset that is not a multiple of
889            DWARF_CIE_DATA_ALIGNMENT, there is either a bug in the
890            definition of DWARF_CIE_DATA_ALIGNMENT, or a bug in the machine
891            description.  */
892         HOST_WIDE_INT check_offset = offset / DWARF_CIE_DATA_ALIGNMENT;
893
894         gcc_assert (check_offset * DWARF_CIE_DATA_ALIGNMENT == offset);
895       }
896 #endif
897       offset /= DWARF_CIE_DATA_ALIGNMENT;
898       if (offset < 0)
899         cfi->dw_cfi_opc = DW_CFA_offset_extended_sf;
900
901       cfi->dw_cfi_oprnd2.dw_cfi_offset = offset;
902     }
903   else if (sreg == reg)
904     cfi->dw_cfi_opc = DW_CFA_same_value;
905   else
906     {
907       cfi->dw_cfi_opc = DW_CFA_register;
908       cfi->dw_cfi_oprnd2.dw_cfi_reg_num = sreg;
909     }
910
911   add_fde_cfi (label, cfi);
912 }
913
914 /* Add the CFI for saving a register window.  LABEL is passed to reg_save.
915    This CFI tells the unwinder that it needs to restore the window registers
916    from the previous frame's window save area.
917
918    ??? Perhaps we should note in the CIE where windows are saved (instead of
919    assuming 0(cfa)) and what registers are in the window.  */
920
921 void
922 dwarf2out_window_save (const char *label)
923 {
924   dw_cfi_ref cfi = new_cfi ();
925
926   cfi->dw_cfi_opc = DW_CFA_GNU_window_save;
927   add_fde_cfi (label, cfi);
928 }
929
930 /* Add a CFI to update the running total of the size of arguments
931    pushed onto the stack.  */
932
933 void
934 dwarf2out_args_size (const char *label, HOST_WIDE_INT size)
935 {
936   dw_cfi_ref cfi;
937
938   if (size == old_args_size)
939     return;
940
941   old_args_size = size;
942
943   cfi = new_cfi ();
944   cfi->dw_cfi_opc = DW_CFA_GNU_args_size;
945   cfi->dw_cfi_oprnd1.dw_cfi_offset = size;
946   add_fde_cfi (label, cfi);
947 }
948
949 /* Entry point for saving a register to the stack.  REG is the GCC register
950    number.  LABEL and OFFSET are passed to reg_save.  */
951
952 void
953 dwarf2out_reg_save (const char *label, unsigned int reg, HOST_WIDE_INT offset)
954 {
955   reg_save (label, DWARF_FRAME_REGNUM (reg), INVALID_REGNUM, offset);
956 }
957
958 /* Entry point for saving the return address in the stack.
959    LABEL and OFFSET are passed to reg_save.  */
960
961 void
962 dwarf2out_return_save (const char *label, HOST_WIDE_INT offset)
963 {
964   reg_save (label, DWARF_FRAME_RETURN_COLUMN, INVALID_REGNUM, offset);
965 }
966
967 /* Entry point for saving the return address in a register.
968    LABEL and SREG are passed to reg_save.  */
969
970 void
971 dwarf2out_return_reg (const char *label, unsigned int sreg)
972 {
973   reg_save (label, DWARF_FRAME_RETURN_COLUMN, DWARF_FRAME_REGNUM (sreg), 0);
974 }
975
976 #ifdef DWARF2_UNWIND_INFO
977 /* Record the initial position of the return address.  RTL is
978    INCOMING_RETURN_ADDR_RTX.  */
979
980 static void
981 initial_return_save (rtx rtl)
982 {
983   unsigned int reg = INVALID_REGNUM;
984   HOST_WIDE_INT offset = 0;
985
986   switch (GET_CODE (rtl))
987     {
988     case REG:
989       /* RA is in a register.  */
990       reg = DWARF_FRAME_REGNUM (REGNO (rtl));
991       break;
992
993     case MEM:
994       /* RA is on the stack.  */
995       rtl = XEXP (rtl, 0);
996       switch (GET_CODE (rtl))
997         {
998         case REG:
999           gcc_assert (REGNO (rtl) == STACK_POINTER_REGNUM);
1000           offset = 0;
1001           break;
1002
1003         case PLUS:
1004           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1005           offset = INTVAL (XEXP (rtl, 1));
1006           break;
1007
1008         case MINUS:
1009           gcc_assert (REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM);
1010           offset = -INTVAL (XEXP (rtl, 1));
1011           break;
1012
1013         default:
1014           gcc_unreachable ();
1015         }
1016
1017       break;
1018
1019     case PLUS:
1020       /* The return address is at some offset from any value we can
1021          actually load.  For instance, on the SPARC it is in %i7+8. Just
1022          ignore the offset for now; it doesn't matter for unwinding frames.  */
1023       gcc_assert (GET_CODE (XEXP (rtl, 1)) == CONST_INT);
1024       initial_return_save (XEXP (rtl, 0));
1025       return;
1026
1027     default:
1028       gcc_unreachable ();
1029     }
1030
1031   if (reg != DWARF_FRAME_RETURN_COLUMN)
1032     reg_save (NULL, DWARF_FRAME_RETURN_COLUMN, reg, offset - cfa.offset);
1033 }
1034 #endif
1035
1036 /* Given a SET, calculate the amount of stack adjustment it
1037    contains.  */
1038
1039 static HOST_WIDE_INT
1040 stack_adjust_offset (const_rtx pattern)
1041 {
1042   const_rtx src = SET_SRC (pattern);
1043   const_rtx dest = SET_DEST (pattern);
1044   HOST_WIDE_INT offset = 0;
1045   enum rtx_code code;
1046
1047   if (dest == stack_pointer_rtx)
1048     {
1049       /* (set (reg sp) (plus (reg sp) (const_int))) */
1050       code = GET_CODE (src);
1051       if (! (code == PLUS || code == MINUS)
1052           || XEXP (src, 0) != stack_pointer_rtx
1053           || GET_CODE (XEXP (src, 1)) != CONST_INT)
1054         return 0;
1055
1056       offset = INTVAL (XEXP (src, 1));
1057       if (code == PLUS)
1058         offset = -offset;
1059     }
1060   else if (MEM_P (dest))
1061     {
1062       /* (set (mem (pre_dec (reg sp))) (foo)) */
1063       src = XEXP (dest, 0);
1064       code = GET_CODE (src);
1065
1066       switch (code)
1067         {
1068         case PRE_MODIFY:
1069         case POST_MODIFY:
1070           if (XEXP (src, 0) == stack_pointer_rtx)
1071             {
1072               rtx val = XEXP (XEXP (src, 1), 1);
1073               /* We handle only adjustments by constant amount.  */
1074               gcc_assert (GET_CODE (XEXP (src, 1)) == PLUS
1075                           && GET_CODE (val) == CONST_INT);
1076               offset = -INTVAL (val);
1077               break;
1078             }
1079           return 0;
1080
1081         case PRE_DEC:
1082         case POST_DEC:
1083           if (XEXP (src, 0) == stack_pointer_rtx)
1084             {
1085               offset = GET_MODE_SIZE (GET_MODE (dest));
1086               break;
1087             }
1088           return 0;
1089
1090         case PRE_INC:
1091         case POST_INC:
1092           if (XEXP (src, 0) == stack_pointer_rtx)
1093             {
1094               offset = -GET_MODE_SIZE (GET_MODE (dest));
1095               break;
1096             }
1097           return 0;
1098
1099         default:
1100           return 0;
1101         }
1102     }
1103   else
1104     return 0;
1105
1106   return offset;
1107 }
1108
1109 /* Check INSN to see if it looks like a push or a stack adjustment, and
1110    make a note of it if it does.  EH uses this information to find out how
1111    much extra space it needs to pop off the stack.  */
1112
1113 static void
1114 dwarf2out_stack_adjust (rtx insn, bool after_p)
1115 {
1116   HOST_WIDE_INT offset;
1117   const char *label;
1118   int i;
1119
1120   /* Don't handle epilogues at all.  Certainly it would be wrong to do so
1121      with this function.  Proper support would require all frame-related
1122      insns to be marked, and to be able to handle saving state around
1123      epilogues textually in the middle of the function.  */
1124   if (prologue_epilogue_contains (insn) || sibcall_epilogue_contains (insn))
1125     return;
1126
1127   /* If only calls can throw, and we have a frame pointer,
1128      save up adjustments until we see the CALL_INSN.  */
1129   if (!flag_asynchronous_unwind_tables && cfa.reg != STACK_POINTER_REGNUM)
1130     {
1131       if (CALL_P (insn) && !after_p)
1132         {
1133           /* Extract the size of the args from the CALL rtx itself.  */
1134           insn = PATTERN (insn);
1135           if (GET_CODE (insn) == PARALLEL)
1136             insn = XVECEXP (insn, 0, 0);
1137           if (GET_CODE (insn) == SET)
1138             insn = SET_SRC (insn);
1139           gcc_assert (GET_CODE (insn) == CALL);
1140           dwarf2out_args_size ("", INTVAL (XEXP (insn, 1)));
1141         }
1142       return;
1143     }
1144
1145   if (CALL_P (insn) && !after_p)
1146     {
1147       if (!flag_asynchronous_unwind_tables)
1148         dwarf2out_args_size ("", args_size);
1149       return;
1150     }
1151   else if (BARRIER_P (insn))
1152     {
1153       /* When we see a BARRIER, we know to reset args_size to 0.  Usually
1154          the compiler will have already emitted a stack adjustment, but
1155          doesn't bother for calls to noreturn functions.  */
1156 #ifdef STACK_GROWS_DOWNWARD
1157       offset = -args_size;
1158 #else
1159       offset = args_size;
1160 #endif
1161     }
1162   else if (GET_CODE (PATTERN (insn)) == SET)
1163     offset = stack_adjust_offset (PATTERN (insn));
1164   else if (GET_CODE (PATTERN (insn)) == PARALLEL
1165            || GET_CODE (PATTERN (insn)) == SEQUENCE)
1166     {
1167       /* There may be stack adjustments inside compound insns.  Search
1168          for them.  */
1169       for (offset = 0, i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
1170         if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
1171           offset += stack_adjust_offset (XVECEXP (PATTERN (insn), 0, i));
1172     }
1173   else
1174     return;
1175
1176   if (offset == 0)
1177     return;
1178
1179   if (cfa.reg == STACK_POINTER_REGNUM)
1180     cfa.offset += offset;
1181
1182 #ifndef STACK_GROWS_DOWNWARD
1183   offset = -offset;
1184 #endif
1185
1186   args_size += offset;
1187   if (args_size < 0)
1188     args_size = 0;
1189
1190   label = dwarf2out_cfi_label ();
1191   def_cfa_1 (label, &cfa);
1192   if (flag_asynchronous_unwind_tables)
1193     dwarf2out_args_size (label, args_size);
1194 }
1195
1196 #endif
1197
1198 /* We delay emitting a register save until either (a) we reach the end
1199    of the prologue or (b) the register is clobbered.  This clusters
1200    register saves so that there are fewer pc advances.  */
1201
1202 struct queued_reg_save GTY(())
1203 {
1204   struct queued_reg_save *next;
1205   rtx reg;
1206   HOST_WIDE_INT cfa_offset;
1207   rtx saved_reg;
1208 };
1209
1210 static GTY(()) struct queued_reg_save *queued_reg_saves;
1211
1212 /* The caller's ORIG_REG is saved in SAVED_IN_REG.  */
1213 struct reg_saved_in_data GTY(()) {
1214   rtx orig_reg;
1215   rtx saved_in_reg;
1216 };
1217
1218 /* A list of registers saved in other registers.
1219    The list intentionally has a small maximum capacity of 4; if your
1220    port needs more than that, you might consider implementing a
1221    more efficient data structure.  */
1222 static GTY(()) struct reg_saved_in_data regs_saved_in_regs[4];
1223 static GTY(()) size_t num_regs_saved_in_regs;
1224
1225 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
1226 static const char *last_reg_save_label;
1227
1228 /* Add an entry to QUEUED_REG_SAVES saying that REG is now saved at
1229    SREG, or if SREG is NULL then it is saved at OFFSET to the CFA.  */
1230
1231 static void
1232 queue_reg_save (const char *label, rtx reg, rtx sreg, HOST_WIDE_INT offset)
1233 {
1234   struct queued_reg_save *q;
1235
1236   /* Duplicates waste space, but it's also necessary to remove them
1237      for correctness, since the queue gets output in reverse
1238      order.  */
1239   for (q = queued_reg_saves; q != NULL; q = q->next)
1240     if (REGNO (q->reg) == REGNO (reg))
1241       break;
1242
1243   if (q == NULL)
1244     {
1245       q = ggc_alloc (sizeof (*q));
1246       q->next = queued_reg_saves;
1247       queued_reg_saves = q;
1248     }
1249
1250   q->reg = reg;
1251   q->cfa_offset = offset;
1252   q->saved_reg = sreg;
1253
1254   last_reg_save_label = label;
1255 }
1256
1257 /* Output all the entries in QUEUED_REG_SAVES.  */
1258
1259 static void
1260 flush_queued_reg_saves (void)
1261 {
1262   struct queued_reg_save *q;
1263
1264   for (q = queued_reg_saves; q; q = q->next)
1265     {
1266       size_t i;
1267       unsigned int reg, sreg;
1268
1269       for (i = 0; i < num_regs_saved_in_regs; i++)
1270         if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (q->reg))
1271           break;
1272       if (q->saved_reg && i == num_regs_saved_in_regs)
1273         {
1274           gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1275           num_regs_saved_in_regs++;
1276         }
1277       if (i != num_regs_saved_in_regs)
1278         {
1279           regs_saved_in_regs[i].orig_reg = q->reg;
1280           regs_saved_in_regs[i].saved_in_reg = q->saved_reg;
1281         }
1282
1283       reg = DWARF_FRAME_REGNUM (REGNO (q->reg));
1284       if (q->saved_reg)
1285         sreg = DWARF_FRAME_REGNUM (REGNO (q->saved_reg));
1286       else
1287         sreg = INVALID_REGNUM;
1288       reg_save (last_reg_save_label, reg, sreg, q->cfa_offset);
1289     }
1290
1291   queued_reg_saves = NULL;
1292   last_reg_save_label = NULL;
1293 }
1294
1295 /* Does INSN clobber any register which QUEUED_REG_SAVES lists a saved
1296    location for?  Or, does it clobber a register which we've previously
1297    said that some other register is saved in, and for which we now
1298    have a new location for?  */
1299
1300 static bool
1301 clobbers_queued_reg_save (const_rtx insn)
1302 {
1303   struct queued_reg_save *q;
1304
1305   for (q = queued_reg_saves; q; q = q->next)
1306     {
1307       size_t i;
1308       if (modified_in_p (q->reg, insn))
1309         return true;
1310       for (i = 0; i < num_regs_saved_in_regs; i++)
1311         if (REGNO (q->reg) == REGNO (regs_saved_in_regs[i].orig_reg)
1312             && modified_in_p (regs_saved_in_regs[i].saved_in_reg, insn))
1313           return true;
1314     }
1315
1316   return false;
1317 }
1318
1319 /* Entry point for saving the first register into the second.  */
1320
1321 void
1322 dwarf2out_reg_save_reg (const char *label, rtx reg, rtx sreg)
1323 {
1324   size_t i;
1325   unsigned int regno, sregno;
1326
1327   for (i = 0; i < num_regs_saved_in_regs; i++)
1328     if (REGNO (regs_saved_in_regs[i].orig_reg) == REGNO (reg))
1329       break;
1330   if (i == num_regs_saved_in_regs)
1331     {
1332       gcc_assert (i != ARRAY_SIZE (regs_saved_in_regs));
1333       num_regs_saved_in_regs++;
1334     }
1335   regs_saved_in_regs[i].orig_reg = reg;
1336   regs_saved_in_regs[i].saved_in_reg = sreg;
1337
1338   regno = DWARF_FRAME_REGNUM (REGNO (reg));
1339   sregno = DWARF_FRAME_REGNUM (REGNO (sreg));
1340   reg_save (label, regno, sregno, 0);
1341 }
1342
1343 /* What register, if any, is currently saved in REG?  */
1344
1345 static rtx
1346 reg_saved_in (rtx reg)
1347 {
1348   unsigned int regn = REGNO (reg);
1349   size_t i;
1350   struct queued_reg_save *q;
1351
1352   for (q = queued_reg_saves; q; q = q->next)
1353     if (q->saved_reg && regn == REGNO (q->saved_reg))
1354       return q->reg;
1355
1356   for (i = 0; i < num_regs_saved_in_regs; i++)
1357     if (regs_saved_in_regs[i].saved_in_reg
1358         && regn == REGNO (regs_saved_in_regs[i].saved_in_reg))
1359       return regs_saved_in_regs[i].orig_reg;
1360
1361   return NULL_RTX;
1362 }
1363
1364
1365 /* A temporary register holding an integral value used in adjusting SP
1366    or setting up the store_reg.  The "offset" field holds the integer
1367    value, not an offset.  */
1368 static dw_cfa_location cfa_temp;
1369
1370 /* Record call frame debugging information for an expression EXPR,
1371    which either sets SP or FP (adjusting how we calculate the frame
1372    address) or saves a register to the stack or another register.
1373    LABEL indicates the address of EXPR.
1374
1375    This function encodes a state machine mapping rtxes to actions on
1376    cfa, cfa_store, and cfa_temp.reg.  We describe these rules so
1377    users need not read the source code.
1378
1379   The High-Level Picture
1380
1381   Changes in the register we use to calculate the CFA: Currently we
1382   assume that if you copy the CFA register into another register, we
1383   should take the other one as the new CFA register; this seems to
1384   work pretty well.  If it's wrong for some target, it's simple
1385   enough not to set RTX_FRAME_RELATED_P on the insn in question.
1386
1387   Changes in the register we use for saving registers to the stack:
1388   This is usually SP, but not always.  Again, we deduce that if you
1389   copy SP into another register (and SP is not the CFA register),
1390   then the new register is the one we will be using for register
1391   saves.  This also seems to work.
1392
1393   Register saves: There's not much guesswork about this one; if
1394   RTX_FRAME_RELATED_P is set on an insn which modifies memory, it's a
1395   register save, and the register used to calculate the destination
1396   had better be the one we think we're using for this purpose.
1397   It's also assumed that a copy from a call-saved register to another
1398   register is saving that register if RTX_FRAME_RELATED_P is set on
1399   that instruction.  If the copy is from a call-saved register to
1400   the *same* register, that means that the register is now the same
1401   value as in the caller.
1402
1403   Except: If the register being saved is the CFA register, and the
1404   offset is nonzero, we are saving the CFA, so we assume we have to
1405   use DW_CFA_def_cfa_expression.  If the offset is 0, we assume that
1406   the intent is to save the value of SP from the previous frame.
1407
1408   In addition, if a register has previously been saved to a different
1409   register,
1410
1411   Invariants / Summaries of Rules
1412
1413   cfa          current rule for calculating the CFA.  It usually
1414                consists of a register and an offset.
1415   cfa_store    register used by prologue code to save things to the stack
1416                cfa_store.offset is the offset from the value of
1417                cfa_store.reg to the actual CFA
1418   cfa_temp     register holding an integral value.  cfa_temp.offset
1419                stores the value, which will be used to adjust the
1420                stack pointer.  cfa_temp is also used like cfa_store,
1421                to track stores to the stack via fp or a temp reg.
1422
1423   Rules  1- 4: Setting a register's value to cfa.reg or an expression
1424                with cfa.reg as the first operand changes the cfa.reg and its
1425                cfa.offset.  Rule 1 and 4 also set cfa_temp.reg and
1426                cfa_temp.offset.
1427
1428   Rules  6- 9: Set a non-cfa.reg register value to a constant or an
1429                expression yielding a constant.  This sets cfa_temp.reg
1430                and cfa_temp.offset.
1431
1432   Rule 5:      Create a new register cfa_store used to save items to the
1433                stack.
1434
1435   Rules 10-14: Save a register to the stack.  Define offset as the
1436                difference of the original location and cfa_store's
1437                location (or cfa_temp's location if cfa_temp is used).
1438
1439   The Rules
1440
1441   "{a,b}" indicates a choice of a xor b.
1442   "<reg>:cfa.reg" indicates that <reg> must equal cfa.reg.
1443
1444   Rule 1:
1445   (set <reg1> <reg2>:cfa.reg)
1446   effects: cfa.reg = <reg1>
1447            cfa.offset unchanged
1448            cfa_temp.reg = <reg1>
1449            cfa_temp.offset = cfa.offset
1450
1451   Rule 2:
1452   (set sp ({minus,plus,losum} {sp,fp}:cfa.reg
1453                               {<const_int>,<reg>:cfa_temp.reg}))
1454   effects: cfa.reg = sp if fp used
1455            cfa.offset += {+/- <const_int>, cfa_temp.offset} if cfa.reg==sp
1456            cfa_store.offset += {+/- <const_int>, cfa_temp.offset}
1457              if cfa_store.reg==sp
1458
1459   Rule 3:
1460   (set fp ({minus,plus,losum} <reg>:cfa.reg <const_int>))
1461   effects: cfa.reg = fp
1462            cfa_offset += +/- <const_int>
1463
1464   Rule 4:
1465   (set <reg1> ({plus,losum} <reg2>:cfa.reg <const_int>))
1466   constraints: <reg1> != fp
1467                <reg1> != sp
1468   effects: cfa.reg = <reg1>
1469            cfa_temp.reg = <reg1>
1470            cfa_temp.offset = cfa.offset
1471
1472   Rule 5:
1473   (set <reg1> (plus <reg2>:cfa_temp.reg sp:cfa.reg))
1474   constraints: <reg1> != fp
1475                <reg1> != sp
1476   effects: cfa_store.reg = <reg1>
1477            cfa_store.offset = cfa.offset - cfa_temp.offset
1478
1479   Rule 6:
1480   (set <reg> <const_int>)
1481   effects: cfa_temp.reg = <reg>
1482            cfa_temp.offset = <const_int>
1483
1484   Rule 7:
1485   (set <reg1>:cfa_temp.reg (ior <reg2>:cfa_temp.reg <const_int>))
1486   effects: cfa_temp.reg = <reg1>
1487            cfa_temp.offset |= <const_int>
1488
1489   Rule 8:
1490   (set <reg> (high <exp>))
1491   effects: none
1492
1493   Rule 9:
1494   (set <reg> (lo_sum <exp> <const_int>))
1495   effects: cfa_temp.reg = <reg>
1496            cfa_temp.offset = <const_int>
1497
1498   Rule 10:
1499   (set (mem (pre_modify sp:cfa_store (???? <reg1> <const_int>))) <reg2>)
1500   effects: cfa_store.offset -= <const_int>
1501            cfa.offset = cfa_store.offset if cfa.reg == sp
1502            cfa.reg = sp
1503            cfa.base_offset = -cfa_store.offset
1504
1505   Rule 11:
1506   (set (mem ({pre_inc,pre_dec} sp:cfa_store.reg)) <reg>)
1507   effects: cfa_store.offset += -/+ mode_size(mem)
1508            cfa.offset = cfa_store.offset if cfa.reg == sp
1509            cfa.reg = sp
1510            cfa.base_offset = -cfa_store.offset
1511
1512   Rule 12:
1513   (set (mem ({minus,plus,losum} <reg1>:{cfa_store,cfa_temp} <const_int>))
1514
1515        <reg2>)
1516   effects: cfa.reg = <reg1>
1517            cfa.base_offset = -/+ <const_int> - {cfa_store,cfa_temp}.offset
1518
1519   Rule 13:
1520   (set (mem <reg1>:{cfa_store,cfa_temp}) <reg2>)
1521   effects: cfa.reg = <reg1>
1522            cfa.base_offset = -{cfa_store,cfa_temp}.offset
1523
1524   Rule 14:
1525   (set (mem (postinc <reg1>:cfa_temp <const_int>)) <reg2>)
1526   effects: cfa.reg = <reg1>
1527            cfa.base_offset = -cfa_temp.offset
1528            cfa_temp.offset -= mode_size(mem)
1529
1530   Rule 15:
1531   (set <reg> {unspec, unspec_volatile})
1532   effects: target-dependent  */
1533
1534 static void
1535 dwarf2out_frame_debug_expr (rtx expr, const char *label)
1536 {
1537   rtx src, dest, span;
1538   HOST_WIDE_INT offset;
1539
1540   /* If RTX_FRAME_RELATED_P is set on a PARALLEL, process each member of
1541      the PARALLEL independently. The first element is always processed if
1542      it is a SET. This is for backward compatibility.   Other elements
1543      are processed only if they are SETs and the RTX_FRAME_RELATED_P
1544      flag is set in them.  */
1545   if (GET_CODE (expr) == PARALLEL || GET_CODE (expr) == SEQUENCE)
1546     {
1547       int par_index;
1548       int limit = XVECLEN (expr, 0);
1549       rtx elem;
1550
1551       /* PARALLELs have strict read-modify-write semantics, so we
1552          ought to evaluate every rvalue before changing any lvalue.
1553          It's cumbersome to do that in general, but there's an
1554          easy approximation that is enough for all current users:
1555          handle register saves before register assignments.  */
1556       if (GET_CODE (expr) == PARALLEL)
1557         for (par_index = 0; par_index < limit; par_index++)
1558           {
1559             elem = XVECEXP (expr, 0, par_index);
1560             if (GET_CODE (elem) == SET
1561                 && MEM_P (SET_DEST (elem))
1562                 && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1563               dwarf2out_frame_debug_expr (elem, label);
1564           }
1565
1566       for (par_index = 0; par_index < limit; par_index++)
1567         {
1568           elem = XVECEXP (expr, 0, par_index);
1569           if (GET_CODE (elem) == SET
1570               && (!MEM_P (SET_DEST (elem)) || GET_CODE (expr) == SEQUENCE)
1571               && (RTX_FRAME_RELATED_P (elem) || par_index == 0))
1572             dwarf2out_frame_debug_expr (elem, label);
1573         }
1574       return;
1575     }
1576
1577   gcc_assert (GET_CODE (expr) == SET);
1578
1579   src = SET_SRC (expr);
1580   dest = SET_DEST (expr);
1581
1582   if (REG_P (src))
1583     {
1584       rtx rsi = reg_saved_in (src);
1585       if (rsi)
1586         src = rsi;
1587     }
1588
1589   switch (GET_CODE (dest))
1590     {
1591     case REG:
1592       switch (GET_CODE (src))
1593         {
1594           /* Setting FP from SP.  */
1595         case REG:
1596           if (cfa.reg == (unsigned) REGNO (src))
1597             {
1598               /* Rule 1 */
1599               /* Update the CFA rule wrt SP or FP.  Make sure src is
1600                  relative to the current CFA register.
1601
1602                  We used to require that dest be either SP or FP, but the
1603                  ARM copies SP to a temporary register, and from there to
1604                  FP.  So we just rely on the backends to only set
1605                  RTX_FRAME_RELATED_P on appropriate insns.  */
1606               cfa.reg = REGNO (dest);
1607               cfa_temp.reg = cfa.reg;
1608               cfa_temp.offset = cfa.offset;
1609             }
1610           else
1611             {
1612               /* Saving a register in a register.  */
1613               gcc_assert (!fixed_regs [REGNO (dest)]
1614                           /* For the SPARC and its register window.  */
1615                           || (DWARF_FRAME_REGNUM (REGNO (src))
1616                               == DWARF_FRAME_RETURN_COLUMN));
1617               queue_reg_save (label, src, dest, 0);
1618             }
1619           break;
1620
1621         case PLUS:
1622         case MINUS:
1623         case LO_SUM:
1624           if (dest == stack_pointer_rtx)
1625             {
1626               /* Rule 2 */
1627               /* Adjusting SP.  */
1628               switch (GET_CODE (XEXP (src, 1)))
1629                 {
1630                 case CONST_INT:
1631                   offset = INTVAL (XEXP (src, 1));
1632                   break;
1633                 case REG:
1634                   gcc_assert ((unsigned) REGNO (XEXP (src, 1))
1635                               == cfa_temp.reg);
1636                   offset = cfa_temp.offset;
1637                   break;
1638                 default:
1639                   gcc_unreachable ();
1640                 }
1641
1642               if (XEXP (src, 0) == hard_frame_pointer_rtx)
1643                 {
1644                   /* Restoring SP from FP in the epilogue.  */
1645                   gcc_assert (cfa.reg == (unsigned) HARD_FRAME_POINTER_REGNUM);
1646                   cfa.reg = STACK_POINTER_REGNUM;
1647                 }
1648               else if (GET_CODE (src) == LO_SUM)
1649                 /* Assume we've set the source reg of the LO_SUM from sp.  */
1650                 ;
1651               else
1652                 gcc_assert (XEXP (src, 0) == stack_pointer_rtx);
1653
1654               if (GET_CODE (src) != MINUS)
1655                 offset = -offset;
1656               if (cfa.reg == STACK_POINTER_REGNUM)
1657                 cfa.offset += offset;
1658               if (cfa_store.reg == STACK_POINTER_REGNUM)
1659                 cfa_store.offset += offset;
1660             }
1661           else if (dest == hard_frame_pointer_rtx)
1662             {
1663               /* Rule 3 */
1664               /* Either setting the FP from an offset of the SP,
1665                  or adjusting the FP */
1666               gcc_assert (frame_pointer_needed);
1667
1668               gcc_assert (REG_P (XEXP (src, 0))
1669                           && (unsigned) REGNO (XEXP (src, 0)) == cfa.reg
1670                           && GET_CODE (XEXP (src, 1)) == CONST_INT);
1671               offset = INTVAL (XEXP (src, 1));
1672               if (GET_CODE (src) != MINUS)
1673                 offset = -offset;
1674               cfa.offset += offset;
1675               cfa.reg = HARD_FRAME_POINTER_REGNUM;
1676             }
1677           else
1678             {
1679               gcc_assert (GET_CODE (src) != MINUS);
1680
1681               /* Rule 4 */
1682               if (REG_P (XEXP (src, 0))
1683                   && REGNO (XEXP (src, 0)) == cfa.reg
1684                   && GET_CODE (XEXP (src, 1)) == CONST_INT)
1685                 {
1686                   /* Setting a temporary CFA register that will be copied
1687                      into the FP later on.  */
1688                   offset = - INTVAL (XEXP (src, 1));
1689                   cfa.offset += offset;
1690                   cfa.reg = REGNO (dest);
1691                   /* Or used to save regs to the stack.  */
1692                   cfa_temp.reg = cfa.reg;
1693                   cfa_temp.offset = cfa.offset;
1694                 }
1695
1696               /* Rule 5 */
1697               else if (REG_P (XEXP (src, 0))
1698                        && REGNO (XEXP (src, 0)) == cfa_temp.reg
1699                        && XEXP (src, 1) == stack_pointer_rtx)
1700                 {
1701                   /* Setting a scratch register that we will use instead
1702                      of SP for saving registers to the stack.  */
1703                   gcc_assert (cfa.reg == STACK_POINTER_REGNUM);
1704                   cfa_store.reg = REGNO (dest);
1705                   cfa_store.offset = cfa.offset - cfa_temp.offset;
1706                 }
1707
1708               /* Rule 9 */
1709               else if (GET_CODE (src) == LO_SUM
1710                        && GET_CODE (XEXP (src, 1)) == CONST_INT)
1711                 {
1712                   cfa_temp.reg = REGNO (dest);
1713                   cfa_temp.offset = INTVAL (XEXP (src, 1));
1714                 }
1715               else
1716                 gcc_unreachable ();
1717             }
1718           break;
1719
1720           /* Rule 6 */
1721         case CONST_INT:
1722           cfa_temp.reg = REGNO (dest);
1723           cfa_temp.offset = INTVAL (src);
1724           break;
1725
1726           /* Rule 7 */
1727         case IOR:
1728           gcc_assert (REG_P (XEXP (src, 0))
1729                       && (unsigned) REGNO (XEXP (src, 0)) == cfa_temp.reg
1730                       && GET_CODE (XEXP (src, 1)) == CONST_INT);
1731
1732           if ((unsigned) REGNO (dest) != cfa_temp.reg)
1733             cfa_temp.reg = REGNO (dest);
1734           cfa_temp.offset |= INTVAL (XEXP (src, 1));
1735           break;
1736
1737           /* Skip over HIGH, assuming it will be followed by a LO_SUM,
1738              which will fill in all of the bits.  */
1739           /* Rule 8 */
1740         case HIGH:
1741           break;
1742
1743           /* Rule 15 */
1744         case UNSPEC:
1745         case UNSPEC_VOLATILE:
1746           gcc_assert (targetm.dwarf_handle_frame_unspec);
1747           targetm.dwarf_handle_frame_unspec (label, expr, XINT (src, 1));
1748           return;
1749
1750         default:
1751           gcc_unreachable ();
1752         }
1753
1754       def_cfa_1 (label, &cfa);
1755       break;
1756
1757     case MEM:
1758       gcc_assert (REG_P (src));
1759
1760       /* Saving a register to the stack.  Make sure dest is relative to the
1761          CFA register.  */
1762       switch (GET_CODE (XEXP (dest, 0)))
1763         {
1764           /* Rule 10 */
1765           /* With a push.  */
1766         case PRE_MODIFY:
1767           /* We can't handle variable size modifications.  */
1768           gcc_assert (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
1769                       == CONST_INT);
1770           offset = -INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1));
1771
1772           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1773                       && cfa_store.reg == STACK_POINTER_REGNUM);
1774
1775           cfa_store.offset += offset;
1776           if (cfa.reg == STACK_POINTER_REGNUM)
1777             cfa.offset = cfa_store.offset;
1778
1779           offset = -cfa_store.offset;
1780           break;
1781
1782           /* Rule 11 */
1783         case PRE_INC:
1784         case PRE_DEC:
1785           offset = GET_MODE_SIZE (GET_MODE (dest));
1786           if (GET_CODE (XEXP (dest, 0)) == PRE_INC)
1787             offset = -offset;
1788
1789           gcc_assert (REGNO (XEXP (XEXP (dest, 0), 0)) == STACK_POINTER_REGNUM
1790                       && cfa_store.reg == STACK_POINTER_REGNUM);
1791
1792           cfa_store.offset += offset;
1793           if (cfa.reg == STACK_POINTER_REGNUM)
1794             cfa.offset = cfa_store.offset;
1795
1796           offset = -cfa_store.offset;
1797           break;
1798
1799           /* Rule 12 */
1800           /* With an offset.  */
1801         case PLUS:
1802         case MINUS:
1803         case LO_SUM:
1804           {
1805             int regno;
1806
1807             gcc_assert (GET_CODE (XEXP (XEXP (dest, 0), 1)) == CONST_INT
1808                         && REG_P (XEXP (XEXP (dest, 0), 0)));
1809             offset = INTVAL (XEXP (XEXP (dest, 0), 1));
1810             if (GET_CODE (XEXP (dest, 0)) == MINUS)
1811               offset = -offset;
1812
1813             regno = REGNO (XEXP (XEXP (dest, 0), 0));
1814
1815             if (cfa_store.reg == (unsigned) regno)
1816               offset -= cfa_store.offset;
1817             else
1818               {
1819                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1820                 offset -= cfa_temp.offset;
1821               }
1822           }
1823           break;
1824
1825           /* Rule 13 */
1826           /* Without an offset.  */
1827         case REG:
1828           {
1829             int regno = REGNO (XEXP (dest, 0));
1830
1831             if (cfa_store.reg == (unsigned) regno)
1832               offset = -cfa_store.offset;
1833             else
1834               {
1835                 gcc_assert (cfa_temp.reg == (unsigned) regno);
1836                 offset = -cfa_temp.offset;
1837               }
1838           }
1839           break;
1840
1841           /* Rule 14 */
1842         case POST_INC:
1843           gcc_assert (cfa_temp.reg
1844                       == (unsigned) REGNO (XEXP (XEXP (dest, 0), 0)));
1845           offset = -cfa_temp.offset;
1846           cfa_temp.offset -= GET_MODE_SIZE (GET_MODE (dest));
1847           break;
1848
1849         default:
1850           gcc_unreachable ();
1851         }
1852
1853       if (REGNO (src) != STACK_POINTER_REGNUM
1854           && REGNO (src) != HARD_FRAME_POINTER_REGNUM
1855           && (unsigned) REGNO (src) == cfa.reg)
1856         {
1857           /* We're storing the current CFA reg into the stack.  */
1858
1859           if (cfa.offset == 0)
1860             {
1861               /* If the source register is exactly the CFA, assume
1862                  we're saving SP like any other register; this happens
1863                  on the ARM.  */
1864               def_cfa_1 (label, &cfa);
1865               queue_reg_save (label, stack_pointer_rtx, NULL_RTX, offset);
1866               break;
1867             }
1868           else
1869             {
1870               /* Otherwise, we'll need to look in the stack to
1871                  calculate the CFA.  */
1872               rtx x = XEXP (dest, 0);
1873
1874               if (!REG_P (x))
1875                 x = XEXP (x, 0);
1876               gcc_assert (REG_P (x));
1877
1878               cfa.reg = REGNO (x);
1879               cfa.base_offset = offset;
1880               cfa.indirect = 1;
1881               def_cfa_1 (label, &cfa);
1882               break;
1883             }
1884         }
1885
1886       def_cfa_1 (label, &cfa);
1887       {
1888         span = targetm.dwarf_register_span (src);
1889
1890         if (!span)
1891           queue_reg_save (label, src, NULL_RTX, offset);
1892         else
1893           {
1894             /* We have a PARALLEL describing where the contents of SRC
1895                live.  Queue register saves for each piece of the
1896                PARALLEL.  */
1897             int par_index;
1898             int limit;
1899             HOST_WIDE_INT span_offset = offset;
1900
1901             gcc_assert (GET_CODE (span) == PARALLEL);
1902
1903             limit = XVECLEN (span, 0);
1904             for (par_index = 0; par_index < limit; par_index++)
1905               {
1906                 rtx elem = XVECEXP (span, 0, par_index);
1907
1908                 queue_reg_save (label, elem, NULL_RTX, span_offset);
1909                 span_offset += GET_MODE_SIZE (GET_MODE (elem));
1910               }
1911           }
1912       }
1913       break;
1914
1915     default:
1916       gcc_unreachable ();
1917     }
1918 }
1919
1920 /* Record call frame debugging information for INSN, which either
1921    sets SP or FP (adjusting how we calculate the frame address) or saves a
1922    register to the stack.  If INSN is NULL_RTX, initialize our state.
1923
1924    If AFTER_P is false, we're being called before the insn is emitted,
1925    otherwise after.  Call instructions get invoked twice.  */
1926
1927 void
1928 dwarf2out_frame_debug (rtx insn, bool after_p)
1929 {
1930   const char *label;
1931   rtx src;
1932
1933   if (insn == NULL_RTX)
1934     {
1935       size_t i;
1936
1937       /* Flush any queued register saves.  */
1938       flush_queued_reg_saves ();
1939
1940       /* Set up state for generating call frame debug info.  */
1941       lookup_cfa (&cfa);
1942       gcc_assert (cfa.reg
1943                   == (unsigned long)DWARF_FRAME_REGNUM (STACK_POINTER_REGNUM));
1944
1945       cfa.reg = STACK_POINTER_REGNUM;
1946       cfa_store = cfa;
1947       cfa_temp.reg = -1;
1948       cfa_temp.offset = 0;
1949
1950       for (i = 0; i < num_regs_saved_in_regs; i++)
1951         {
1952           regs_saved_in_regs[i].orig_reg = NULL_RTX;
1953           regs_saved_in_regs[i].saved_in_reg = NULL_RTX;
1954         }
1955       num_regs_saved_in_regs = 0;
1956       return;
1957     }
1958
1959   if (!NONJUMP_INSN_P (insn) || clobbers_queued_reg_save (insn))
1960     flush_queued_reg_saves ();
1961
1962   if (! RTX_FRAME_RELATED_P (insn))
1963     {
1964       if (!ACCUMULATE_OUTGOING_ARGS)
1965         dwarf2out_stack_adjust (insn, after_p);
1966       return;
1967     }
1968
1969   label = dwarf2out_cfi_label ();
1970   src = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
1971   if (src)
1972     insn = XEXP (src, 0);
1973   else
1974     insn = PATTERN (insn);
1975
1976   dwarf2out_frame_debug_expr (insn, label);
1977 }
1978
1979 #endif
1980
1981 /* Describe for the GTY machinery what parts of dw_cfi_oprnd1 are used.  */
1982 static enum dw_cfi_oprnd_type dw_cfi_oprnd1_desc
1983  (enum dwarf_call_frame_info cfi);
1984
1985 static enum dw_cfi_oprnd_type
1986 dw_cfi_oprnd1_desc (enum dwarf_call_frame_info cfi)
1987 {
1988   switch (cfi)
1989     {
1990     case DW_CFA_nop:
1991     case DW_CFA_GNU_window_save:
1992       return dw_cfi_oprnd_unused;
1993
1994     case DW_CFA_set_loc:
1995     case DW_CFA_advance_loc1:
1996     case DW_CFA_advance_loc2:
1997     case DW_CFA_advance_loc4:
1998     case DW_CFA_MIPS_advance_loc8:
1999       return dw_cfi_oprnd_addr;
2000
2001     case DW_CFA_offset:
2002     case DW_CFA_offset_extended:
2003     case DW_CFA_def_cfa:
2004     case DW_CFA_offset_extended_sf:
2005     case DW_CFA_def_cfa_sf:
2006     case DW_CFA_restore_extended:
2007     case DW_CFA_undefined:
2008     case DW_CFA_same_value:
2009     case DW_CFA_def_cfa_register:
2010     case DW_CFA_register:
2011       return dw_cfi_oprnd_reg_num;
2012
2013     case DW_CFA_def_cfa_offset:
2014     case DW_CFA_GNU_args_size:
2015     case DW_CFA_def_cfa_offset_sf:
2016       return dw_cfi_oprnd_offset;
2017
2018     case DW_CFA_def_cfa_expression:
2019     case DW_CFA_expression:
2020       return dw_cfi_oprnd_loc;
2021
2022     default:
2023       gcc_unreachable ();
2024     }
2025 }
2026
2027 /* Describe for the GTY machinery what parts of dw_cfi_oprnd2 are used.  */
2028 static enum dw_cfi_oprnd_type dw_cfi_oprnd2_desc
2029  (enum dwarf_call_frame_info cfi);
2030
2031 static enum dw_cfi_oprnd_type
2032 dw_cfi_oprnd2_desc (enum dwarf_call_frame_info cfi)
2033 {
2034   switch (cfi)
2035     {
2036     case DW_CFA_def_cfa:
2037     case DW_CFA_def_cfa_sf:
2038     case DW_CFA_offset:
2039     case DW_CFA_offset_extended_sf:
2040     case DW_CFA_offset_extended:
2041       return dw_cfi_oprnd_offset;
2042
2043     case DW_CFA_register:
2044       return dw_cfi_oprnd_reg_num;
2045
2046     default:
2047       return dw_cfi_oprnd_unused;
2048     }
2049 }
2050
2051 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2052
2053 /* Switch to eh_frame_section.  If we don't have an eh_frame_section,
2054    switch to the data section instead, and write out a synthetic label
2055    for collect2.  */
2056
2057 static void
2058 switch_to_eh_frame_section (void)
2059 {
2060   tree label;
2061
2062 #ifdef EH_FRAME_SECTION_NAME
2063   if (eh_frame_section == 0)
2064     {
2065       int flags;
2066
2067       if (EH_TABLES_CAN_BE_READ_ONLY)
2068         {
2069           int fde_encoding;
2070           int per_encoding;
2071           int lsda_encoding;
2072
2073           fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1,
2074                                                        /*global=*/0);
2075           per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2,
2076                                                        /*global=*/1);
2077           lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0,
2078                                                         /*global=*/0);
2079           flags = ((! flag_pic
2080                     || ((fde_encoding & 0x70) != DW_EH_PE_absptr
2081                         && (fde_encoding & 0x70) != DW_EH_PE_aligned
2082                         && (per_encoding & 0x70) != DW_EH_PE_absptr
2083                         && (per_encoding & 0x70) != DW_EH_PE_aligned
2084                         && (lsda_encoding & 0x70) != DW_EH_PE_absptr
2085                         && (lsda_encoding & 0x70) != DW_EH_PE_aligned))
2086                    ? 0 : SECTION_WRITE);
2087         }
2088       else
2089         flags = SECTION_WRITE;
2090       eh_frame_section = get_section (EH_FRAME_SECTION_NAME, flags, NULL);
2091     }
2092 #endif
2093
2094   if (eh_frame_section)
2095     switch_to_section (eh_frame_section);
2096   else
2097     {
2098       /* We have no special eh_frame section.  Put the information in
2099          the data section and emit special labels to guide collect2.  */
2100       switch_to_section (data_section);
2101       label = get_file_function_name ("F");
2102       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2103       targetm.asm_out.globalize_label (asm_out_file,
2104                                        IDENTIFIER_POINTER (label));
2105       ASM_OUTPUT_LABEL (asm_out_file, IDENTIFIER_POINTER (label));
2106     }
2107 }
2108
2109 /* Output a Call Frame Information opcode and its operand(s).  */
2110
2111 static void
2112 output_cfi (dw_cfi_ref cfi, dw_fde_ref fde, int for_eh)
2113 {
2114   unsigned long r;
2115   if (cfi->dw_cfi_opc == DW_CFA_advance_loc)
2116     dw2_asm_output_data (1, (cfi->dw_cfi_opc
2117                              | (cfi->dw_cfi_oprnd1.dw_cfi_offset & 0x3f)),
2118                          "DW_CFA_advance_loc " HOST_WIDE_INT_PRINT_HEX,
2119                          cfi->dw_cfi_oprnd1.dw_cfi_offset);
2120   else if (cfi->dw_cfi_opc == DW_CFA_offset)
2121     {
2122       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2123       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2124                            "DW_CFA_offset, column 0x%lx", r);
2125       dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2126     }
2127   else if (cfi->dw_cfi_opc == DW_CFA_restore)
2128     {
2129       r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2130       dw2_asm_output_data (1, (cfi->dw_cfi_opc | (r & 0x3f)),
2131                            "DW_CFA_restore, column 0x%lx", r);
2132     }
2133   else
2134     {
2135       dw2_asm_output_data (1, cfi->dw_cfi_opc,
2136                            "%s", dwarf_cfi_name (cfi->dw_cfi_opc));
2137
2138       switch (cfi->dw_cfi_opc)
2139         {
2140         case DW_CFA_set_loc:
2141           if (for_eh)
2142             dw2_asm_output_encoded_addr_rtx (
2143                 ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0),
2144                 gen_rtx_SYMBOL_REF (Pmode, cfi->dw_cfi_oprnd1.dw_cfi_addr),
2145                 false, NULL);
2146           else
2147             dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2148                                  cfi->dw_cfi_oprnd1.dw_cfi_addr, NULL);
2149           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2150           break;
2151
2152         case DW_CFA_advance_loc1:
2153           dw2_asm_output_delta (1, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2154                                 fde->dw_fde_current_label, NULL);
2155           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2156           break;
2157
2158         case DW_CFA_advance_loc2:
2159           dw2_asm_output_delta (2, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2160                                 fde->dw_fde_current_label, NULL);
2161           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2162           break;
2163
2164         case DW_CFA_advance_loc4:
2165           dw2_asm_output_delta (4, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2166                                 fde->dw_fde_current_label, NULL);
2167           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2168           break;
2169
2170         case DW_CFA_MIPS_advance_loc8:
2171           dw2_asm_output_delta (8, cfi->dw_cfi_oprnd1.dw_cfi_addr,
2172                                 fde->dw_fde_current_label, NULL);
2173           fde->dw_fde_current_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
2174           break;
2175
2176         case DW_CFA_offset_extended:
2177         case DW_CFA_def_cfa:
2178           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2179           dw2_asm_output_data_uleb128 (r, NULL);
2180           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2181           break;
2182
2183         case DW_CFA_offset_extended_sf:
2184         case DW_CFA_def_cfa_sf:
2185           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2186           dw2_asm_output_data_uleb128 (r, NULL);
2187           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd2.dw_cfi_offset, NULL);
2188           break;
2189
2190         case DW_CFA_restore_extended:
2191         case DW_CFA_undefined:
2192         case DW_CFA_same_value:
2193         case DW_CFA_def_cfa_register:
2194           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2195           dw2_asm_output_data_uleb128 (r, NULL);
2196           break;
2197
2198         case DW_CFA_register:
2199           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd1.dw_cfi_reg_num, for_eh);
2200           dw2_asm_output_data_uleb128 (r, NULL);
2201           r = DWARF2_FRAME_REG_OUT (cfi->dw_cfi_oprnd2.dw_cfi_reg_num, for_eh);
2202           dw2_asm_output_data_uleb128 (r, NULL);
2203           break;
2204
2205         case DW_CFA_def_cfa_offset:
2206         case DW_CFA_GNU_args_size:
2207           dw2_asm_output_data_uleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2208           break;
2209
2210         case DW_CFA_def_cfa_offset_sf:
2211           dw2_asm_output_data_sleb128 (cfi->dw_cfi_oprnd1.dw_cfi_offset, NULL);
2212           break;
2213
2214         case DW_CFA_GNU_window_save:
2215           break;
2216
2217         case DW_CFA_def_cfa_expression:
2218         case DW_CFA_expression:
2219           output_cfa_loc (cfi);
2220           break;
2221
2222         case DW_CFA_GNU_negative_offset_extended:
2223           /* Obsoleted by DW_CFA_offset_extended_sf.  */
2224           gcc_unreachable ();
2225
2226         default:
2227           break;
2228         }
2229     }
2230 }
2231
2232 /* Output the call frame information used to record information
2233    that relates to calculating the frame pointer, and records the
2234    location of saved registers.  */
2235
2236 static void
2237 output_call_frame_info (int for_eh)
2238 {
2239   unsigned int i;
2240   dw_fde_ref fde;
2241   dw_cfi_ref cfi;
2242   char l1[20], l2[20], section_start_label[20];
2243   bool any_lsda_needed = false;
2244   char augmentation[6];
2245   int augmentation_size;
2246   int fde_encoding = DW_EH_PE_absptr;
2247   int per_encoding = DW_EH_PE_absptr;
2248   int lsda_encoding = DW_EH_PE_absptr;
2249   int return_reg;
2250
2251   /* Don't emit a CIE if there won't be any FDEs.  */
2252   if (fde_table_in_use == 0)
2253     return;
2254
2255   /* If we make FDEs linkonce, we may have to emit an empty label for
2256      an FDE that wouldn't otherwise be emitted.  We want to avoid
2257      having an FDE kept around when the function it refers to is
2258      discarded.  Example where this matters: a primary function
2259      template in C++ requires EH information, but an explicit
2260      specialization doesn't.  */
2261   if (TARGET_USES_WEAK_UNWIND_INFO
2262       && ! flag_asynchronous_unwind_tables
2263       && flag_exceptions
2264       && for_eh)
2265     for (i = 0; i < fde_table_in_use; i++)
2266       if ((fde_table[i].nothrow || fde_table[i].all_throwers_are_sibcalls)
2267           && !fde_table[i].uses_eh_lsda
2268           && ! DECL_WEAK (fde_table[i].decl))
2269         targetm.asm_out.unwind_label (asm_out_file, fde_table[i].decl,
2270                                       for_eh, /* empty */ 1);
2271
2272   /* If we don't have any functions we'll want to unwind out of, don't
2273      emit any EH unwind information.  Note that if exceptions aren't
2274      enabled, we won't have collected nothrow information, and if we
2275      asked for asynchronous tables, we always want this info.  */
2276   if (for_eh)
2277     {
2278       bool any_eh_needed = !flag_exceptions || flag_asynchronous_unwind_tables;
2279
2280       for (i = 0; i < fde_table_in_use; i++)
2281         if (fde_table[i].uses_eh_lsda)
2282           any_eh_needed = any_lsda_needed = true;
2283         else if (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2284           any_eh_needed = true;
2285         else if (! fde_table[i].nothrow
2286                  && ! fde_table[i].all_throwers_are_sibcalls)
2287           any_eh_needed = true;
2288
2289       if (! any_eh_needed)
2290         return;
2291     }
2292
2293   /* We're going to be generating comments, so turn on app.  */
2294   if (flag_debug_asm)
2295     app_enable ();
2296
2297   if (for_eh)
2298     switch_to_eh_frame_section ();
2299   else
2300     {
2301       if (!debug_frame_section)
2302         debug_frame_section = get_section (DEBUG_FRAME_SECTION,
2303                                            SECTION_DEBUG, NULL);
2304       switch_to_section (debug_frame_section);
2305     }
2306
2307   ASM_GENERATE_INTERNAL_LABEL (section_start_label, FRAME_BEGIN_LABEL, for_eh);
2308   ASM_OUTPUT_LABEL (asm_out_file, section_start_label);
2309
2310   /* Output the CIE.  */
2311   ASM_GENERATE_INTERNAL_LABEL (l1, CIE_AFTER_SIZE_LABEL, for_eh);
2312   ASM_GENERATE_INTERNAL_LABEL (l2, CIE_END_LABEL, for_eh);
2313   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2314     dw2_asm_output_data (4, 0xffffffff,
2315       "Initial length escape value indicating 64-bit DWARF extension");
2316   dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2317                         "Length of Common Information Entry");
2318   ASM_OUTPUT_LABEL (asm_out_file, l1);
2319
2320   /* Now that the CIE pointer is PC-relative for EH,
2321      use 0 to identify the CIE.  */
2322   dw2_asm_output_data ((for_eh ? 4 : DWARF_OFFSET_SIZE),
2323                        (for_eh ? 0 : DWARF_CIE_ID),
2324                        "CIE Identifier Tag");
2325
2326   dw2_asm_output_data (1, DW_CIE_VERSION, "CIE Version");
2327
2328   augmentation[0] = 0;
2329   augmentation_size = 0;
2330   if (for_eh)
2331     {
2332       char *p;
2333
2334       /* Augmentation:
2335          z      Indicates that a uleb128 is present to size the
2336                 augmentation section.
2337          L      Indicates the encoding (and thus presence) of
2338                 an LSDA pointer in the FDE augmentation.
2339          R      Indicates a non-default pointer encoding for
2340                 FDE code pointers.
2341          P      Indicates the presence of an encoding + language
2342                 personality routine in the CIE augmentation.  */
2343
2344       fde_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/1, /*global=*/0);
2345       per_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/2, /*global=*/1);
2346       lsda_encoding = ASM_PREFERRED_EH_DATA_FORMAT (/*code=*/0, /*global=*/0);
2347
2348       p = augmentation + 1;
2349       if (eh_personality_libfunc)
2350         {
2351           *p++ = 'P';
2352           augmentation_size += 1 + size_of_encoded_value (per_encoding);
2353           assemble_external_libcall (eh_personality_libfunc);
2354         }
2355       if (any_lsda_needed)
2356         {
2357           *p++ = 'L';
2358           augmentation_size += 1;
2359         }
2360       if (fde_encoding != DW_EH_PE_absptr)
2361         {
2362           *p++ = 'R';
2363           augmentation_size += 1;
2364         }
2365       if (p > augmentation + 1)
2366         {
2367           augmentation[0] = 'z';
2368           *p = '\0';
2369         }
2370
2371       /* Ug.  Some platforms can't do unaligned dynamic relocations at all.  */
2372       if (eh_personality_libfunc && per_encoding == DW_EH_PE_aligned)
2373         {
2374           int offset = (  4             /* Length */
2375                         + 4             /* CIE Id */
2376                         + 1             /* CIE version */
2377                         + strlen (augmentation) + 1     /* Augmentation */
2378                         + size_of_uleb128 (1)           /* Code alignment */
2379                         + size_of_sleb128 (DWARF_CIE_DATA_ALIGNMENT)
2380                         + 1             /* RA column */
2381                         + 1             /* Augmentation size */
2382                         + 1             /* Personality encoding */ );
2383           int pad = -offset & (PTR_SIZE - 1);
2384
2385           augmentation_size += pad;
2386
2387           /* Augmentations should be small, so there's scarce need to
2388              iterate for a solution.  Die if we exceed one uleb128 byte.  */
2389           gcc_assert (size_of_uleb128 (augmentation_size) == 1);
2390         }
2391     }
2392
2393   dw2_asm_output_nstring (augmentation, -1, "CIE Augmentation");
2394   dw2_asm_output_data_uleb128 (1, "CIE Code Alignment Factor");
2395   dw2_asm_output_data_sleb128 (DWARF_CIE_DATA_ALIGNMENT,
2396                                "CIE Data Alignment Factor");
2397
2398   return_reg = DWARF2_FRAME_REG_OUT (DWARF_FRAME_RETURN_COLUMN, for_eh);
2399   if (DW_CIE_VERSION == 1)
2400     dw2_asm_output_data (1, return_reg, "CIE RA Column");
2401   else
2402     dw2_asm_output_data_uleb128 (return_reg, "CIE RA Column");
2403
2404   if (augmentation[0])
2405     {
2406       dw2_asm_output_data_uleb128 (augmentation_size, "Augmentation size");
2407       if (eh_personality_libfunc)
2408         {
2409           dw2_asm_output_data (1, per_encoding, "Personality (%s)",
2410                                eh_data_format_name (per_encoding));
2411           dw2_asm_output_encoded_addr_rtx (per_encoding,
2412                                            eh_personality_libfunc,
2413                                            true, NULL);
2414         }
2415
2416       if (any_lsda_needed)
2417         dw2_asm_output_data (1, lsda_encoding, "LSDA Encoding (%s)",
2418                              eh_data_format_name (lsda_encoding));
2419
2420       if (fde_encoding != DW_EH_PE_absptr)
2421         dw2_asm_output_data (1, fde_encoding, "FDE Encoding (%s)",
2422                              eh_data_format_name (fde_encoding));
2423     }
2424
2425   for (cfi = cie_cfi_head; cfi != NULL; cfi = cfi->dw_cfi_next)
2426     output_cfi (cfi, NULL, for_eh);
2427
2428   /* Pad the CIE out to an address sized boundary.  */
2429   ASM_OUTPUT_ALIGN (asm_out_file,
2430                     floor_log2 (for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE));
2431   ASM_OUTPUT_LABEL (asm_out_file, l2);
2432
2433   /* Loop through all of the FDE's.  */
2434   for (i = 0; i < fde_table_in_use; i++)
2435     {
2436       fde = &fde_table[i];
2437
2438       /* Don't emit EH unwind info for leaf functions that don't need it.  */
2439       if (for_eh && !flag_asynchronous_unwind_tables && flag_exceptions
2440           && (fde->nothrow || fde->all_throwers_are_sibcalls)
2441           && ! (TARGET_USES_WEAK_UNWIND_INFO && DECL_WEAK (fde_table[i].decl))
2442           && !fde->uses_eh_lsda)
2443         continue;
2444
2445       targetm.asm_out.unwind_label (asm_out_file, fde->decl, for_eh, /* empty */ 0);
2446       targetm.asm_out.internal_label (asm_out_file, FDE_LABEL, for_eh + i * 2);
2447       ASM_GENERATE_INTERNAL_LABEL (l1, FDE_AFTER_SIZE_LABEL, for_eh + i * 2);
2448       ASM_GENERATE_INTERNAL_LABEL (l2, FDE_END_LABEL, for_eh + i * 2);
2449       if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4 && !for_eh)
2450         dw2_asm_output_data (4, 0xffffffff,
2451                              "Initial length escape value indicating 64-bit DWARF extension");
2452       dw2_asm_output_delta (for_eh ? 4 : DWARF_OFFSET_SIZE, l2, l1,
2453                             "FDE Length");
2454       ASM_OUTPUT_LABEL (asm_out_file, l1);
2455
2456       if (for_eh)
2457         dw2_asm_output_delta (4, l1, section_start_label, "FDE CIE offset");
2458       else
2459         dw2_asm_output_offset (DWARF_OFFSET_SIZE, section_start_label,
2460                                debug_frame_section, "FDE CIE offset");
2461
2462       if (for_eh)
2463         {
2464           if (fde->dw_fde_switched_sections)
2465             {
2466               rtx sym_ref2 = gen_rtx_SYMBOL_REF (Pmode,
2467                                       fde->dw_fde_unlikely_section_label);
2468               rtx sym_ref3= gen_rtx_SYMBOL_REF (Pmode,
2469                                       fde->dw_fde_hot_section_label);
2470               SYMBOL_REF_FLAGS (sym_ref2) |= SYMBOL_FLAG_LOCAL;
2471               SYMBOL_REF_FLAGS (sym_ref3) |= SYMBOL_FLAG_LOCAL;
2472               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref3, false,
2473                                                "FDE initial location");
2474               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2475                                     fde->dw_fde_hot_section_end_label,
2476                                     fde->dw_fde_hot_section_label,
2477                                     "FDE address range");
2478               dw2_asm_output_encoded_addr_rtx (fde_encoding, sym_ref2, false,
2479                                                "FDE initial location");
2480               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2481                                     fde->dw_fde_unlikely_section_end_label,
2482                                     fde->dw_fde_unlikely_section_label,
2483                                     "FDE address range");
2484             }
2485           else
2486             {
2487               rtx sym_ref = gen_rtx_SYMBOL_REF (Pmode, fde->dw_fde_begin);
2488               SYMBOL_REF_FLAGS (sym_ref) |= SYMBOL_FLAG_LOCAL;
2489               dw2_asm_output_encoded_addr_rtx (fde_encoding,
2490                                                sym_ref,
2491                                                false,
2492                                                "FDE initial location");
2493               dw2_asm_output_delta (size_of_encoded_value (fde_encoding),
2494                                     fde->dw_fde_end, fde->dw_fde_begin,
2495                                     "FDE address range");
2496             }
2497         }
2498       else
2499         {
2500           if (fde->dw_fde_switched_sections)
2501             {
2502               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2503                                    fde->dw_fde_hot_section_label,
2504                                    "FDE initial location");
2505               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2506                                     fde->dw_fde_hot_section_end_label,
2507                                     fde->dw_fde_hot_section_label,
2508                                     "FDE address range");
2509               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
2510                                    fde->dw_fde_unlikely_section_label,
2511                                    "FDE initial location");
2512               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2513                                     fde->dw_fde_unlikely_section_end_label,
2514                                     fde->dw_fde_unlikely_section_label,
2515                                     "FDE address range");
2516             }
2517           else
2518             {
2519               dw2_asm_output_addr (DWARF2_ADDR_SIZE, fde->dw_fde_begin,
2520                                    "FDE initial location");
2521               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
2522                                     fde->dw_fde_end, fde->dw_fde_begin,
2523                                     "FDE address range");
2524             }
2525         }
2526
2527       if (augmentation[0])
2528         {
2529           if (any_lsda_needed)
2530             {
2531               int size = size_of_encoded_value (lsda_encoding);
2532
2533               if (lsda_encoding == DW_EH_PE_aligned)
2534                 {
2535                   int offset = (  4             /* Length */
2536                                 + 4             /* CIE offset */
2537                                 + 2 * size_of_encoded_value (fde_encoding)
2538                                 + 1             /* Augmentation size */ );
2539                   int pad = -offset & (PTR_SIZE - 1);
2540
2541                   size += pad;
2542                   gcc_assert (size_of_uleb128 (size) == 1);
2543                 }
2544
2545               dw2_asm_output_data_uleb128 (size, "Augmentation size");
2546
2547               if (fde->uses_eh_lsda)
2548                 {
2549                   ASM_GENERATE_INTERNAL_LABEL (l1, "LLSDA",
2550                                                fde->funcdef_number);
2551                   dw2_asm_output_encoded_addr_rtx (
2552                         lsda_encoding, gen_rtx_SYMBOL_REF (Pmode, l1),
2553                         false, "Language Specific Data Area");
2554                 }
2555               else
2556                 {
2557                   if (lsda_encoding == DW_EH_PE_aligned)
2558                     ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (PTR_SIZE));
2559                   dw2_asm_output_data
2560                     (size_of_encoded_value (lsda_encoding), 0,
2561                      "Language Specific Data Area (none)");
2562                 }
2563             }
2564           else
2565             dw2_asm_output_data_uleb128 (0, "Augmentation size");
2566         }
2567
2568       /* Loop through the Call Frame Instructions associated with
2569          this FDE.  */
2570       fde->dw_fde_current_label = fde->dw_fde_begin;
2571       for (cfi = fde->dw_fde_cfi; cfi != NULL; cfi = cfi->dw_cfi_next)
2572         output_cfi (cfi, fde, for_eh);
2573
2574       /* Pad the FDE out to an address sized boundary.  */
2575       ASM_OUTPUT_ALIGN (asm_out_file,
2576                         floor_log2 ((for_eh ? PTR_SIZE : DWARF2_ADDR_SIZE)));
2577       ASM_OUTPUT_LABEL (asm_out_file, l2);
2578     }
2579
2580   if (for_eh && targetm.terminate_dw2_eh_frame_info)
2581     dw2_asm_output_data (4, 0, "End of Table");
2582 #ifdef MIPS_DEBUGGING_INFO
2583   /* Work around Irix 6 assembler bug whereby labels at the end of a section
2584      get a value of 0.  Putting .align 0 after the label fixes it.  */
2585   ASM_OUTPUT_ALIGN (asm_out_file, 0);
2586 #endif
2587
2588   /* Turn off app to make assembly quicker.  */
2589   if (flag_debug_asm)
2590     app_disable ();
2591 }
2592
2593 /* Output a marker (i.e. a label) for the beginning of a function, before
2594    the prologue.  */
2595
2596 void
2597 dwarf2out_begin_prologue (unsigned int line ATTRIBUTE_UNUSED,
2598                           const char *file ATTRIBUTE_UNUSED)
2599 {
2600   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2601   char * dup_label;
2602   dw_fde_ref fde;
2603
2604   current_function_func_begin_label = NULL;
2605
2606 #ifdef TARGET_UNWIND_INFO
2607   /* ??? current_function_func_begin_label is also used by except.c
2608      for call-site information.  We must emit this label if it might
2609      be used.  */
2610   if ((! flag_exceptions || USING_SJLJ_EXCEPTIONS)
2611       && ! dwarf2out_do_frame ())
2612     return;
2613 #else
2614   if (! dwarf2out_do_frame ())
2615     return;
2616 #endif
2617
2618   switch_to_section (function_section (current_function_decl));
2619   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_BEGIN_LABEL,
2620                                current_function_funcdef_no);
2621   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, FUNC_BEGIN_LABEL,
2622                           current_function_funcdef_no);
2623   dup_label = xstrdup (label);
2624   current_function_func_begin_label = dup_label;
2625
2626 #ifdef TARGET_UNWIND_INFO
2627   /* We can elide the fde allocation if we're not emitting debug info.  */
2628   if (! dwarf2out_do_frame ())
2629     return;
2630 #endif
2631
2632   /* Expand the fde table if necessary.  */
2633   if (fde_table_in_use == fde_table_allocated)
2634     {
2635       fde_table_allocated += FDE_TABLE_INCREMENT;
2636       fde_table = ggc_realloc (fde_table,
2637                                fde_table_allocated * sizeof (dw_fde_node));
2638       memset (fde_table + fde_table_in_use, 0,
2639               FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2640     }
2641
2642   /* Record the FDE associated with this function.  */
2643   current_funcdef_fde = fde_table_in_use;
2644
2645   /* Add the new FDE at the end of the fde_table.  */
2646   fde = &fde_table[fde_table_in_use++];
2647   fde->decl = current_function_decl;
2648   fde->dw_fde_begin = dup_label;
2649   fde->dw_fde_current_label = dup_label;
2650   fde->dw_fde_hot_section_label = NULL;
2651   fde->dw_fde_hot_section_end_label = NULL;
2652   fde->dw_fde_unlikely_section_label = NULL;
2653   fde->dw_fde_unlikely_section_end_label = NULL;
2654   fde->dw_fde_switched_sections = false;
2655   fde->dw_fde_end = NULL;
2656   fde->dw_fde_cfi = NULL;
2657   fde->funcdef_number = current_function_funcdef_no;
2658   fde->nothrow = TREE_NOTHROW (current_function_decl);
2659   fde->uses_eh_lsda = cfun->uses_eh_lsda;
2660   fde->all_throwers_are_sibcalls = cfun->all_throwers_are_sibcalls;
2661
2662   args_size = old_args_size = 0;
2663
2664   /* We only want to output line number information for the genuine dwarf2
2665      prologue case, not the eh frame case.  */
2666 #ifdef DWARF2_DEBUGGING_INFO
2667   if (file)
2668     dwarf2out_source_line (line, file);
2669 #endif
2670 }
2671
2672 /* Output a marker (i.e. a label) for the absolute end of the generated code
2673    for a function definition.  This gets called *after* the epilogue code has
2674    been generated.  */
2675
2676 void
2677 dwarf2out_end_epilogue (unsigned int line ATTRIBUTE_UNUSED,
2678                         const char *file ATTRIBUTE_UNUSED)
2679 {
2680   dw_fde_ref fde;
2681   char label[MAX_ARTIFICIAL_LABEL_BYTES];
2682
2683   /* Output a label to mark the endpoint of the code generated for this
2684      function.  */
2685   ASM_GENERATE_INTERNAL_LABEL (label, FUNC_END_LABEL,
2686                                current_function_funcdef_no);
2687   ASM_OUTPUT_LABEL (asm_out_file, label);
2688   fde = &fde_table[fde_table_in_use - 1];
2689   fde->dw_fde_end = xstrdup (label);
2690 }
2691
2692 void
2693 dwarf2out_frame_init (void)
2694 {
2695   /* Allocate the initial hunk of the fde_table.  */
2696   fde_table = ggc_alloc_cleared (FDE_TABLE_INCREMENT * sizeof (dw_fde_node));
2697   fde_table_allocated = FDE_TABLE_INCREMENT;
2698   fde_table_in_use = 0;
2699
2700   /* Generate the CFA instructions common to all FDE's.  Do it now for the
2701      sake of lookup_cfa.  */
2702
2703   /* On entry, the Canonical Frame Address is at SP.  */
2704   dwarf2out_def_cfa (NULL, STACK_POINTER_REGNUM, INCOMING_FRAME_SP_OFFSET);
2705
2706 #ifdef DWARF2_UNWIND_INFO
2707   if (DWARF2_UNWIND_INFO || DWARF2_FRAME_INFO)
2708     initial_return_save (INCOMING_RETURN_ADDR_RTX);
2709 #endif
2710 }
2711
2712 void
2713 dwarf2out_frame_finish (void)
2714 {
2715   /* Output call frame information.  */
2716   if (DWARF2_FRAME_INFO)
2717     output_call_frame_info (0);
2718
2719 #ifndef TARGET_UNWIND_INFO
2720   /* Output another copy for the unwinder.  */
2721   if (! USING_SJLJ_EXCEPTIONS && (flag_unwind_tables || flag_exceptions))
2722     output_call_frame_info (1);
2723 #endif
2724 }
2725
2726 /* Note that the current function section is being used for code.  */
2727
2728 static void
2729 dwarf2out_note_section_used (void)
2730 {
2731   section *sec = current_function_section ();
2732   if (sec == text_section)
2733     text_section_used = true;
2734   else if (sec == cold_text_section)
2735     cold_text_section_used = true;
2736 }
2737
2738 void
2739 dwarf2out_switch_text_section (void)
2740 {
2741   dw_fde_ref fde;
2742
2743   gcc_assert (cfun);
2744
2745   fde = &fde_table[fde_table_in_use - 1];
2746   fde->dw_fde_switched_sections = true;
2747   fde->dw_fde_hot_section_label = cfun->hot_section_label;
2748   fde->dw_fde_hot_section_end_label = cfun->hot_section_end_label;
2749   fde->dw_fde_unlikely_section_label = cfun->cold_section_label;
2750   fde->dw_fde_unlikely_section_end_label = cfun->cold_section_end_label;
2751   have_multiple_function_sections = true;
2752
2753   /* Reset the current label on switching text sections, so that we
2754      don't attempt to advance_loc4 between labels in different sections.  */
2755   fde->dw_fde_current_label = NULL;
2756
2757   /* There is no need to mark used sections when not debugging.  */
2758   if (cold_text_section != NULL)
2759     dwarf2out_note_section_used ();
2760 }
2761 #endif
2762 \f
2763 /* And now, the subset of the debugging information support code necessary
2764    for emitting location expressions.  */
2765
2766 /* Data about a single source file.  */
2767 struct dwarf_file_data GTY(())
2768 {
2769   const char * filename;
2770   int emitted_number;
2771 };
2772
2773 /* We need some way to distinguish DW_OP_addr with a direct symbol
2774    relocation from DW_OP_addr with a dtp-relative symbol relocation.  */
2775 #define INTERNAL_DW_OP_tls_addr         (0x100 + DW_OP_addr)
2776
2777
2778 typedef struct dw_val_struct *dw_val_ref;
2779 typedef struct die_struct *dw_die_ref;
2780 typedef const struct die_struct *const_dw_die_ref;
2781 typedef struct dw_loc_descr_struct *dw_loc_descr_ref;
2782 typedef struct dw_loc_list_struct *dw_loc_list_ref;
2783
2784 /* Each DIE may have a series of attribute/value pairs.  Values
2785    can take on several forms.  The forms that are used in this
2786    implementation are listed below.  */
2787
2788 enum dw_val_class
2789 {
2790   dw_val_class_addr,
2791   dw_val_class_offset,
2792   dw_val_class_loc,
2793   dw_val_class_loc_list,
2794   dw_val_class_range_list,
2795   dw_val_class_const,
2796   dw_val_class_unsigned_const,
2797   dw_val_class_long_long,
2798   dw_val_class_vec,
2799   dw_val_class_flag,
2800   dw_val_class_die_ref,
2801   dw_val_class_fde_ref,
2802   dw_val_class_lbl_id,
2803   dw_val_class_lineptr,
2804   dw_val_class_str,
2805   dw_val_class_macptr,
2806   dw_val_class_file
2807 };
2808
2809 /* Describe a double word constant value.  */
2810 /* ??? Every instance of long_long in the code really means CONST_DOUBLE.  */
2811
2812 typedef struct dw_long_long_struct GTY(())
2813 {
2814   unsigned long hi;
2815   unsigned long low;
2816 }
2817 dw_long_long_const;
2818
2819 /* Describe a floating point constant value, or a vector constant value.  */
2820
2821 typedef struct dw_vec_struct GTY(())
2822 {
2823   unsigned char * GTY((length ("%h.length"))) array;
2824   unsigned length;
2825   unsigned elt_size;
2826 }
2827 dw_vec_const;
2828
2829 /* The dw_val_node describes an attribute's value, as it is
2830    represented internally.  */
2831
2832 typedef struct dw_val_struct GTY(())
2833 {
2834   enum dw_val_class val_class;
2835   union dw_val_struct_union
2836     {
2837       rtx GTY ((tag ("dw_val_class_addr"))) val_addr;
2838       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_offset"))) val_offset;
2839       dw_loc_list_ref GTY ((tag ("dw_val_class_loc_list"))) val_loc_list;
2840       dw_loc_descr_ref GTY ((tag ("dw_val_class_loc"))) val_loc;
2841       HOST_WIDE_INT GTY ((default)) val_int;
2842       unsigned HOST_WIDE_INT GTY ((tag ("dw_val_class_unsigned_const"))) val_unsigned;
2843       dw_long_long_const GTY ((tag ("dw_val_class_long_long"))) val_long_long;
2844       dw_vec_const GTY ((tag ("dw_val_class_vec"))) val_vec;
2845       struct dw_val_die_union
2846         {
2847           dw_die_ref die;
2848           int external;
2849         } GTY ((tag ("dw_val_class_die_ref"))) val_die_ref;
2850       unsigned GTY ((tag ("dw_val_class_fde_ref"))) val_fde_index;
2851       struct indirect_string_node * GTY ((tag ("dw_val_class_str"))) val_str;
2852       char * GTY ((tag ("dw_val_class_lbl_id"))) val_lbl_id;
2853       unsigned char GTY ((tag ("dw_val_class_flag"))) val_flag;
2854       struct dwarf_file_data * GTY ((tag ("dw_val_class_file"))) val_file;
2855     }
2856   GTY ((desc ("%1.val_class"))) v;
2857 }
2858 dw_val_node;
2859
2860 /* Locations in memory are described using a sequence of stack machine
2861    operations.  */
2862
2863 typedef struct dw_loc_descr_struct GTY(())
2864 {
2865   dw_loc_descr_ref dw_loc_next;
2866   enum dwarf_location_atom dw_loc_opc;
2867   dw_val_node dw_loc_oprnd1;
2868   dw_val_node dw_loc_oprnd2;
2869   int dw_loc_addr;
2870 }
2871 dw_loc_descr_node;
2872
2873 /* Location lists are ranges + location descriptions for that range,
2874    so you can track variables that are in different places over
2875    their entire life.  */
2876 typedef struct dw_loc_list_struct GTY(())
2877 {
2878   dw_loc_list_ref dw_loc_next;
2879   const char *begin; /* Label for begin address of range */
2880   const char *end;  /* Label for end address of range */
2881   char *ll_symbol; /* Label for beginning of location list.
2882                       Only on head of list */
2883   const char *section; /* Section this loclist is relative to */
2884   dw_loc_descr_ref expr;
2885 } dw_loc_list_node;
2886
2887 #if defined (DWARF2_DEBUGGING_INFO) || defined (DWARF2_UNWIND_INFO)
2888
2889 static const char *dwarf_stack_op_name (unsigned);
2890 static dw_loc_descr_ref new_loc_descr (enum dwarf_location_atom,
2891                                        unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
2892 static void add_loc_descr (dw_loc_descr_ref *, dw_loc_descr_ref);
2893 static unsigned long size_of_loc_descr (dw_loc_descr_ref);
2894 static unsigned long size_of_locs (dw_loc_descr_ref);
2895 static void output_loc_operands (dw_loc_descr_ref);
2896 static void output_loc_sequence (dw_loc_descr_ref);
2897
2898 /* Convert a DWARF stack opcode into its string name.  */
2899
2900 static const char *
2901 dwarf_stack_op_name (unsigned int op)
2902 {
2903   switch (op)
2904     {
2905     case DW_OP_addr:
2906     case INTERNAL_DW_OP_tls_addr:
2907       return "DW_OP_addr";
2908     case DW_OP_deref:
2909       return "DW_OP_deref";
2910     case DW_OP_const1u:
2911       return "DW_OP_const1u";
2912     case DW_OP_const1s:
2913       return "DW_OP_const1s";
2914     case DW_OP_const2u:
2915       return "DW_OP_const2u";
2916     case DW_OP_const2s:
2917       return "DW_OP_const2s";
2918     case DW_OP_const4u:
2919       return "DW_OP_const4u";
2920     case DW_OP_const4s:
2921       return "DW_OP_const4s";
2922     case DW_OP_const8u:
2923       return "DW_OP_const8u";
2924     case DW_OP_const8s:
2925       return "DW_OP_const8s";
2926     case DW_OP_constu:
2927       return "DW_OP_constu";
2928     case DW_OP_consts:
2929       return "DW_OP_consts";
2930     case DW_OP_dup:
2931       return "DW_OP_dup";
2932     case DW_OP_drop:
2933       return "DW_OP_drop";
2934     case DW_OP_over:
2935       return "DW_OP_over";
2936     case DW_OP_pick:
2937       return "DW_OP_pick";
2938     case DW_OP_swap:
2939       return "DW_OP_swap";
2940     case DW_OP_rot:
2941       return "DW_OP_rot";
2942     case DW_OP_xderef:
2943       return "DW_OP_xderef";
2944     case DW_OP_abs:
2945       return "DW_OP_abs";
2946     case DW_OP_and:
2947       return "DW_OP_and";
2948     case DW_OP_div:
2949       return "DW_OP_div";
2950     case DW_OP_minus:
2951       return "DW_OP_minus";
2952     case DW_OP_mod:
2953       return "DW_OP_mod";
2954     case DW_OP_mul:
2955       return "DW_OP_mul";
2956     case DW_OP_neg:
2957       return "DW_OP_neg";
2958     case DW_OP_not:
2959       return "DW_OP_not";
2960     case DW_OP_or:
2961       return "DW_OP_or";
2962     case DW_OP_plus:
2963       return "DW_OP_plus";
2964     case DW_OP_plus_uconst:
2965       return "DW_OP_plus_uconst";
2966     case DW_OP_shl:
2967       return "DW_OP_shl";
2968     case DW_OP_shr:
2969       return "DW_OP_shr";
2970     case DW_OP_shra:
2971       return "DW_OP_shra";
2972     case DW_OP_xor:
2973       return "DW_OP_xor";
2974     case DW_OP_bra:
2975       return "DW_OP_bra";
2976     case DW_OP_eq:
2977       return "DW_OP_eq";
2978     case DW_OP_ge:
2979       return "DW_OP_ge";
2980     case DW_OP_gt:
2981       return "DW_OP_gt";
2982     case DW_OP_le:
2983       return "DW_OP_le";
2984     case DW_OP_lt:
2985       return "DW_OP_lt";
2986     case DW_OP_ne:
2987       return "DW_OP_ne";
2988     case DW_OP_skip:
2989       return "DW_OP_skip";
2990     case DW_OP_lit0:
2991       return "DW_OP_lit0";
2992     case DW_OP_lit1:
2993       return "DW_OP_lit1";
2994     case DW_OP_lit2:
2995       return "DW_OP_lit2";
2996     case DW_OP_lit3:
2997       return "DW_OP_lit3";
2998     case DW_OP_lit4:
2999       return "DW_OP_lit4";
3000     case DW_OP_lit5:
3001       return "DW_OP_lit5";
3002     case DW_OP_lit6:
3003       return "DW_OP_lit6";
3004     case DW_OP_lit7:
3005       return "DW_OP_lit7";
3006     case DW_OP_lit8:
3007       return "DW_OP_lit8";
3008     case DW_OP_lit9:
3009       return "DW_OP_lit9";
3010     case DW_OP_lit10:
3011       return "DW_OP_lit10";
3012     case DW_OP_lit11:
3013       return "DW_OP_lit11";
3014     case DW_OP_lit12:
3015       return "DW_OP_lit12";
3016     case DW_OP_lit13:
3017       return "DW_OP_lit13";
3018     case DW_OP_lit14:
3019       return "DW_OP_lit14";
3020     case DW_OP_lit15:
3021       return "DW_OP_lit15";
3022     case DW_OP_lit16:
3023       return "DW_OP_lit16";
3024     case DW_OP_lit17:
3025       return "DW_OP_lit17";
3026     case DW_OP_lit18:
3027       return "DW_OP_lit18";
3028     case DW_OP_lit19:
3029       return "DW_OP_lit19";
3030     case DW_OP_lit20:
3031       return "DW_OP_lit20";
3032     case DW_OP_lit21:
3033       return "DW_OP_lit21";
3034     case DW_OP_lit22:
3035       return "DW_OP_lit22";
3036     case DW_OP_lit23:
3037       return "DW_OP_lit23";
3038     case DW_OP_lit24:
3039       return "DW_OP_lit24";
3040     case DW_OP_lit25:
3041       return "DW_OP_lit25";
3042     case DW_OP_lit26:
3043       return "DW_OP_lit26";
3044     case DW_OP_lit27:
3045       return "DW_OP_lit27";
3046     case DW_OP_lit28:
3047       return "DW_OP_lit28";
3048     case DW_OP_lit29:
3049       return "DW_OP_lit29";
3050     case DW_OP_lit30:
3051       return "DW_OP_lit30";
3052     case DW_OP_lit31:
3053       return "DW_OP_lit31";
3054     case DW_OP_reg0:
3055       return "DW_OP_reg0";
3056     case DW_OP_reg1:
3057       return "DW_OP_reg1";
3058     case DW_OP_reg2:
3059       return "DW_OP_reg2";
3060     case DW_OP_reg3:
3061       return "DW_OP_reg3";
3062     case DW_OP_reg4:
3063       return "DW_OP_reg4";
3064     case DW_OP_reg5:
3065       return "DW_OP_reg5";
3066     case DW_OP_reg6:
3067       return "DW_OP_reg6";
3068     case DW_OP_reg7:
3069       return "DW_OP_reg7";
3070     case DW_OP_reg8:
3071       return "DW_OP_reg8";
3072     case DW_OP_reg9:
3073       return "DW_OP_reg9";
3074     case DW_OP_reg10:
3075       return "DW_OP_reg10";
3076     case DW_OP_reg11:
3077       return "DW_OP_reg11";
3078     case DW_OP_reg12:
3079       return "DW_OP_reg12";
3080     case DW_OP_reg13:
3081       return "DW_OP_reg13";
3082     case DW_OP_reg14:
3083       return "DW_OP_reg14";
3084     case DW_OP_reg15:
3085       return "DW_OP_reg15";
3086     case DW_OP_reg16:
3087       return "DW_OP_reg16";
3088     case DW_OP_reg17:
3089       return "DW_OP_reg17";
3090     case DW_OP_reg18:
3091       return "DW_OP_reg18";
3092     case DW_OP_reg19:
3093       return "DW_OP_reg19";
3094     case DW_OP_reg20:
3095       return "DW_OP_reg20";
3096     case DW_OP_reg21:
3097       return "DW_OP_reg21";
3098     case DW_OP_reg22:
3099       return "DW_OP_reg22";
3100     case DW_OP_reg23:
3101       return "DW_OP_reg23";
3102     case DW_OP_reg24:
3103       return "DW_OP_reg24";
3104     case DW_OP_reg25:
3105       return "DW_OP_reg25";
3106     case DW_OP_reg26:
3107       return "DW_OP_reg26";
3108     case DW_OP_reg27:
3109       return "DW_OP_reg27";
3110     case DW_OP_reg28:
3111       return "DW_OP_reg28";
3112     case DW_OP_reg29:
3113       return "DW_OP_reg29";
3114     case DW_OP_reg30:
3115       return "DW_OP_reg30";
3116     case DW_OP_reg31:
3117       return "DW_OP_reg31";
3118     case DW_OP_breg0:
3119       return "DW_OP_breg0";
3120     case DW_OP_breg1:
3121       return "DW_OP_breg1";
3122     case DW_OP_breg2:
3123       return "DW_OP_breg2";
3124     case DW_OP_breg3:
3125       return "DW_OP_breg3";
3126     case DW_OP_breg4:
3127       return "DW_OP_breg4";
3128     case DW_OP_breg5:
3129       return "DW_OP_breg5";
3130     case DW_OP_breg6:
3131       return "DW_OP_breg6";
3132     case DW_OP_breg7:
3133       return "DW_OP_breg7";
3134     case DW_OP_breg8:
3135       return "DW_OP_breg8";
3136     case DW_OP_breg9:
3137       return "DW_OP_breg9";
3138     case DW_OP_breg10:
3139       return "DW_OP_breg10";
3140     case DW_OP_breg11:
3141       return "DW_OP_breg11";
3142     case DW_OP_breg12:
3143       return "DW_OP_breg12";
3144     case DW_OP_breg13:
3145       return "DW_OP_breg13";
3146     case DW_OP_breg14:
3147       return "DW_OP_breg14";
3148     case DW_OP_breg15:
3149       return "DW_OP_breg15";
3150     case DW_OP_breg16:
3151       return "DW_OP_breg16";
3152     case DW_OP_breg17:
3153       return "DW_OP_breg17";
3154     case DW_OP_breg18:
3155       return "DW_OP_breg18";
3156     case DW_OP_breg19:
3157       return "DW_OP_breg19";
3158     case DW_OP_breg20:
3159       return "DW_OP_breg20";
3160     case DW_OP_breg21:
3161       return "DW_OP_breg21";
3162     case DW_OP_breg22:
3163       return "DW_OP_breg22";
3164     case DW_OP_breg23:
3165       return "DW_OP_breg23";
3166     case DW_OP_breg24:
3167       return "DW_OP_breg24";
3168     case DW_OP_breg25:
3169       return "DW_OP_breg25";
3170     case DW_OP_breg26:
3171       return "DW_OP_breg26";
3172     case DW_OP_breg27:
3173       return "DW_OP_breg27";
3174     case DW_OP_breg28:
3175       return "DW_OP_breg28";
3176     case DW_OP_breg29:
3177       return "DW_OP_breg29";
3178     case DW_OP_breg30:
3179       return "DW_OP_breg30";
3180     case DW_OP_breg31:
3181       return "DW_OP_breg31";
3182     case DW_OP_regx:
3183       return "DW_OP_regx";
3184     case DW_OP_fbreg:
3185       return "DW_OP_fbreg";
3186     case DW_OP_bregx:
3187       return "DW_OP_bregx";
3188     case DW_OP_piece:
3189       return "DW_OP_piece";
3190     case DW_OP_deref_size:
3191       return "DW_OP_deref_size";
3192     case DW_OP_xderef_size:
3193       return "DW_OP_xderef_size";
3194     case DW_OP_nop:
3195       return "DW_OP_nop";
3196     case DW_OP_push_object_address:
3197       return "DW_OP_push_object_address";
3198     case DW_OP_call2:
3199       return "DW_OP_call2";
3200     case DW_OP_call4:
3201       return "DW_OP_call4";
3202     case DW_OP_call_ref:
3203       return "DW_OP_call_ref";
3204     case DW_OP_GNU_push_tls_address:
3205       return "DW_OP_GNU_push_tls_address";
3206     case DW_OP_GNU_uninit:
3207       return "DW_OP_GNU_uninit";
3208     default:
3209       return "OP_<unknown>";
3210     }
3211 }
3212
3213 /* Return a pointer to a newly allocated location description.  Location
3214    descriptions are simple expression terms that can be strung
3215    together to form more complicated location (address) descriptions.  */
3216
3217 static inline dw_loc_descr_ref
3218 new_loc_descr (enum dwarf_location_atom op, unsigned HOST_WIDE_INT oprnd1,
3219                unsigned HOST_WIDE_INT oprnd2)
3220 {
3221   dw_loc_descr_ref descr = ggc_alloc_cleared (sizeof (dw_loc_descr_node));
3222
3223   descr->dw_loc_opc = op;
3224   descr->dw_loc_oprnd1.val_class = dw_val_class_unsigned_const;
3225   descr->dw_loc_oprnd1.v.val_unsigned = oprnd1;
3226   descr->dw_loc_oprnd2.val_class = dw_val_class_unsigned_const;
3227   descr->dw_loc_oprnd2.v.val_unsigned = oprnd2;
3228
3229   return descr;
3230 }
3231
3232 /* Add a location description term to a location description expression.  */
3233
3234 static inline void
3235 add_loc_descr (dw_loc_descr_ref *list_head, dw_loc_descr_ref descr)
3236 {
3237   dw_loc_descr_ref *d;
3238
3239   /* Find the end of the chain.  */
3240   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
3241     ;
3242
3243   *d = descr;
3244 }
3245
3246 /* Return the size of a location descriptor.  */
3247
3248 static unsigned long
3249 size_of_loc_descr (dw_loc_descr_ref loc)
3250 {
3251   unsigned long size = 1;
3252
3253   switch (loc->dw_loc_opc)
3254     {
3255     case DW_OP_addr:
3256     case INTERNAL_DW_OP_tls_addr:
3257       size += DWARF2_ADDR_SIZE;
3258       break;
3259     case DW_OP_const1u:
3260     case DW_OP_const1s:
3261       size += 1;
3262       break;
3263     case DW_OP_const2u:
3264     case DW_OP_const2s:
3265       size += 2;
3266       break;
3267     case DW_OP_const4u:
3268     case DW_OP_const4s:
3269       size += 4;
3270       break;
3271     case DW_OP_const8u:
3272     case DW_OP_const8s:
3273       size += 8;
3274       break;
3275     case DW_OP_constu:
3276       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3277       break;
3278     case DW_OP_consts:
3279       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3280       break;
3281     case DW_OP_pick:
3282       size += 1;
3283       break;
3284     case DW_OP_plus_uconst:
3285       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3286       break;
3287     case DW_OP_skip:
3288     case DW_OP_bra:
3289       size += 2;
3290       break;
3291     case DW_OP_breg0:
3292     case DW_OP_breg1:
3293     case DW_OP_breg2:
3294     case DW_OP_breg3:
3295     case DW_OP_breg4:
3296     case DW_OP_breg5:
3297     case DW_OP_breg6:
3298     case DW_OP_breg7:
3299     case DW_OP_breg8:
3300     case DW_OP_breg9:
3301     case DW_OP_breg10:
3302     case DW_OP_breg11:
3303     case DW_OP_breg12:
3304     case DW_OP_breg13:
3305     case DW_OP_breg14:
3306     case DW_OP_breg15:
3307     case DW_OP_breg16:
3308     case DW_OP_breg17:
3309     case DW_OP_breg18:
3310     case DW_OP_breg19:
3311     case DW_OP_breg20:
3312     case DW_OP_breg21:
3313     case DW_OP_breg22:
3314     case DW_OP_breg23:
3315     case DW_OP_breg24:
3316     case DW_OP_breg25:
3317     case DW_OP_breg26:
3318     case DW_OP_breg27:
3319     case DW_OP_breg28:
3320     case DW_OP_breg29:
3321     case DW_OP_breg30:
3322     case DW_OP_breg31:
3323       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3324       break;
3325     case DW_OP_regx:
3326       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3327       break;
3328     case DW_OP_fbreg:
3329       size += size_of_sleb128 (loc->dw_loc_oprnd1.v.val_int);
3330       break;
3331     case DW_OP_bregx:
3332       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3333       size += size_of_sleb128 (loc->dw_loc_oprnd2.v.val_int);
3334       break;
3335     case DW_OP_piece:
3336       size += size_of_uleb128 (loc->dw_loc_oprnd1.v.val_unsigned);
3337       break;
3338     case DW_OP_deref_size:
3339     case DW_OP_xderef_size:
3340       size += 1;
3341       break;
3342     case DW_OP_call2:
3343       size += 2;
3344       break;
3345     case DW_OP_call4:
3346       size += 4;
3347       break;
3348     case DW_OP_call_ref:
3349       size += DWARF2_ADDR_SIZE;
3350       break;
3351     default:
3352       break;
3353     }
3354
3355   return size;
3356 }
3357
3358 /* Return the size of a series of location descriptors.  */
3359
3360 static unsigned long
3361 size_of_locs (dw_loc_descr_ref loc)
3362 {
3363   dw_loc_descr_ref l;
3364   unsigned long size;
3365
3366   /* If there are no skip or bra opcodes, don't fill in the dw_loc_addr
3367      field, to avoid writing to a PCH file.  */
3368   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3369     {
3370       if (l->dw_loc_opc == DW_OP_skip || l->dw_loc_opc == DW_OP_bra)
3371         break;
3372       size += size_of_loc_descr (l);
3373     }
3374   if (! l)
3375     return size;
3376
3377   for (size = 0, l = loc; l != NULL; l = l->dw_loc_next)
3378     {
3379       l->dw_loc_addr = size;
3380       size += size_of_loc_descr (l);
3381     }
3382
3383   return size;
3384 }
3385
3386 /* Output location description stack opcode's operands (if any).  */
3387
3388 static void
3389 output_loc_operands (dw_loc_descr_ref loc)
3390 {
3391   dw_val_ref val1 = &loc->dw_loc_oprnd1;
3392   dw_val_ref val2 = &loc->dw_loc_oprnd2;
3393
3394   switch (loc->dw_loc_opc)
3395     {
3396 #ifdef DWARF2_DEBUGGING_INFO
3397     case DW_OP_addr:
3398       dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, val1->v.val_addr, NULL);
3399       break;
3400     case DW_OP_const2u:
3401     case DW_OP_const2s:
3402       dw2_asm_output_data (2, val1->v.val_int, NULL);
3403       break;
3404     case DW_OP_const4u:
3405     case DW_OP_const4s:
3406       dw2_asm_output_data (4, val1->v.val_int, NULL);
3407       break;
3408     case DW_OP_const8u:
3409     case DW_OP_const8s:
3410       gcc_assert (HOST_BITS_PER_LONG >= 64);
3411       dw2_asm_output_data (8, val1->v.val_int, NULL);
3412       break;
3413     case DW_OP_skip:
3414     case DW_OP_bra:
3415       {
3416         int offset;
3417
3418         gcc_assert (val1->val_class == dw_val_class_loc);
3419         offset = val1->v.val_loc->dw_loc_addr - (loc->dw_loc_addr + 3);
3420
3421         dw2_asm_output_data (2, offset, NULL);
3422       }
3423       break;
3424 #else
3425     case DW_OP_addr:
3426     case DW_OP_const2u:
3427     case DW_OP_const2s:
3428     case DW_OP_const4u:
3429     case DW_OP_const4s:
3430     case DW_OP_const8u:
3431     case DW_OP_const8s:
3432     case DW_OP_skip:
3433     case DW_OP_bra:
3434       /* We currently don't make any attempt to make sure these are
3435          aligned properly like we do for the main unwind info, so
3436          don't support emitting things larger than a byte if we're
3437          only doing unwinding.  */
3438       gcc_unreachable ();
3439 #endif
3440     case DW_OP_const1u:
3441     case DW_OP_const1s:
3442       dw2_asm_output_data (1, val1->v.val_int, NULL);
3443       break;
3444     case DW_OP_constu:
3445       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3446       break;
3447     case DW_OP_consts:
3448       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3449       break;
3450     case DW_OP_pick:
3451       dw2_asm_output_data (1, val1->v.val_int, NULL);
3452       break;
3453     case DW_OP_plus_uconst:
3454       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3455       break;
3456     case DW_OP_breg0:
3457     case DW_OP_breg1:
3458     case DW_OP_breg2:
3459     case DW_OP_breg3:
3460     case DW_OP_breg4:
3461     case DW_OP_breg5:
3462     case DW_OP_breg6:
3463     case DW_OP_breg7:
3464     case DW_OP_breg8:
3465     case DW_OP_breg9:
3466     case DW_OP_breg10:
3467     case DW_OP_breg11:
3468     case DW_OP_breg12:
3469     case DW_OP_breg13:
3470     case DW_OP_breg14:
3471     case DW_OP_breg15:
3472     case DW_OP_breg16:
3473     case DW_OP_breg17:
3474     case DW_OP_breg18:
3475     case DW_OP_breg19:
3476     case DW_OP_breg20:
3477     case DW_OP_breg21:
3478     case DW_OP_breg22:
3479     case DW_OP_breg23:
3480     case DW_OP_breg24:
3481     case DW_OP_breg25:
3482     case DW_OP_breg26:
3483     case DW_OP_breg27:
3484     case DW_OP_breg28:
3485     case DW_OP_breg29:
3486     case DW_OP_breg30:
3487     case DW_OP_breg31:
3488       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3489       break;
3490     case DW_OP_regx:
3491       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3492       break;
3493     case DW_OP_fbreg:
3494       dw2_asm_output_data_sleb128 (val1->v.val_int, NULL);
3495       break;
3496     case DW_OP_bregx:
3497       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3498       dw2_asm_output_data_sleb128 (val2->v.val_int, NULL);
3499       break;
3500     case DW_OP_piece:
3501       dw2_asm_output_data_uleb128 (val1->v.val_unsigned, NULL);
3502       break;
3503     case DW_OP_deref_size:
3504     case DW_OP_xderef_size:
3505       dw2_asm_output_data (1, val1->v.val_int, NULL);
3506       break;
3507
3508     case INTERNAL_DW_OP_tls_addr:
3509       if (targetm.asm_out.output_dwarf_dtprel)
3510         {
3511           targetm.asm_out.output_dwarf_dtprel (asm_out_file,
3512                                                DWARF2_ADDR_SIZE,
3513                                                val1->v.val_addr);
3514           fputc ('\n', asm_out_file);
3515         }
3516       else
3517         gcc_unreachable ();
3518       break;
3519
3520     default:
3521       /* Other codes have no operands.  */
3522       break;
3523     }
3524 }
3525
3526 /* Output a sequence of location operations.  */
3527
3528 static void
3529 output_loc_sequence (dw_loc_descr_ref loc)
3530 {
3531   for (; loc != NULL; loc = loc->dw_loc_next)
3532     {
3533       /* Output the opcode.  */
3534       dw2_asm_output_data (1, loc->dw_loc_opc,
3535                            "%s", dwarf_stack_op_name (loc->dw_loc_opc));
3536
3537       /* Output the operand(s) (if any).  */
3538       output_loc_operands (loc);
3539     }
3540 }
3541
3542 /* This routine will generate the correct assembly data for a location
3543    description based on a cfi entry with a complex address.  */
3544
3545 static void
3546 output_cfa_loc (dw_cfi_ref cfi)
3547 {
3548   dw_loc_descr_ref loc;
3549   unsigned long size;
3550
3551   /* Output the size of the block.  */
3552   loc = cfi->dw_cfi_oprnd1.dw_cfi_loc;
3553   size = size_of_locs (loc);
3554   dw2_asm_output_data_uleb128 (size, NULL);
3555
3556   /* Now output the operations themselves.  */
3557   output_loc_sequence (loc);
3558 }
3559
3560 /* This function builds a dwarf location descriptor sequence from a
3561    dw_cfa_location, adding the given OFFSET to the result of the
3562    expression.  */
3563
3564 static struct dw_loc_descr_struct *
3565 build_cfa_loc (dw_cfa_location *cfa, HOST_WIDE_INT offset)
3566 {
3567   struct dw_loc_descr_struct *head, *tmp;
3568
3569   offset += cfa->offset;
3570
3571   if (cfa->indirect)
3572     {
3573       if (cfa->base_offset)
3574         {
3575           if (cfa->reg <= 31)
3576             head = new_loc_descr (DW_OP_breg0 + cfa->reg, cfa->base_offset, 0);
3577           else
3578             head = new_loc_descr (DW_OP_bregx, cfa->reg, cfa->base_offset);
3579         }
3580       else if (cfa->reg <= 31)
3581         head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3582       else
3583         head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3584
3585       head->dw_loc_oprnd1.val_class = dw_val_class_const;
3586       tmp = new_loc_descr (DW_OP_deref, 0, 0);
3587       add_loc_descr (&head, tmp);
3588       if (offset != 0)
3589         {
3590           tmp = new_loc_descr (DW_OP_plus_uconst, offset, 0);
3591           add_loc_descr (&head, tmp);
3592         }
3593     }
3594   else
3595     {
3596       if (offset == 0)
3597         if (cfa->reg <= 31)
3598           head = new_loc_descr (DW_OP_reg0 + cfa->reg, 0, 0);
3599         else
3600           head = new_loc_descr (DW_OP_regx, cfa->reg, 0);
3601       else if (cfa->reg <= 31)
3602         head = new_loc_descr (DW_OP_breg0 + cfa->reg, offset, 0);
3603       else
3604         head = new_loc_descr (DW_OP_bregx, cfa->reg, offset);
3605     }
3606
3607   return head;
3608 }
3609
3610 /* This function fills in aa dw_cfa_location structure from a dwarf location
3611    descriptor sequence.  */
3612
3613 static void
3614 get_cfa_from_loc_descr (dw_cfa_location *cfa, struct dw_loc_descr_struct *loc)
3615 {
3616   struct dw_loc_descr_struct *ptr;
3617   cfa->offset = 0;
3618   cfa->base_offset = 0;
3619   cfa->indirect = 0;
3620   cfa->reg = -1;
3621
3622   for (ptr = loc; ptr != NULL; ptr = ptr->dw_loc_next)
3623     {
3624       enum dwarf_location_atom op = ptr->dw_loc_opc;
3625
3626       switch (op)
3627         {
3628         case DW_OP_reg0:
3629         case DW_OP_reg1:
3630         case DW_OP_reg2:
3631         case DW_OP_reg3:
3632         case DW_OP_reg4:
3633         case DW_OP_reg5:
3634         case DW_OP_reg6:
3635         case DW_OP_reg7:
3636         case DW_OP_reg8:
3637         case DW_OP_reg9:
3638         case DW_OP_reg10:
3639         case DW_OP_reg11:
3640         case DW_OP_reg12:
3641         case DW_OP_reg13:
3642         case DW_OP_reg14:
3643         case DW_OP_reg15:
3644         case DW_OP_reg16:
3645         case DW_OP_reg17:
3646         case DW_OP_reg18:
3647         case DW_OP_reg19:
3648         case DW_OP_reg20:
3649         case DW_OP_reg21:
3650         case DW_OP_reg22:
3651         case DW_OP_reg23:
3652         case DW_OP_reg24:
3653         case DW_OP_reg25:
3654         case DW_OP_reg26:
3655         case DW_OP_reg27:
3656         case DW_OP_reg28:
3657         case DW_OP_reg29:
3658         case DW_OP_reg30:
3659         case DW_OP_reg31:
3660           cfa->reg = op - DW_OP_reg0;
3661           break;
3662         case DW_OP_regx:
3663           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3664           break;
3665         case DW_OP_breg0:
3666         case DW_OP_breg1:
3667         case DW_OP_breg2:
3668         case DW_OP_breg3:
3669         case DW_OP_breg4:
3670         case DW_OP_breg5:
3671         case DW_OP_breg6:
3672         case DW_OP_breg7:
3673         case DW_OP_breg8:
3674         case DW_OP_breg9:
3675         case DW_OP_breg10:
3676         case DW_OP_breg11:
3677         case DW_OP_breg12:
3678         case DW_OP_breg13:
3679         case DW_OP_breg14:
3680         case DW_OP_breg15:
3681         case DW_OP_breg16:
3682         case DW_OP_breg17:
3683         case DW_OP_breg18:
3684         case DW_OP_breg19:
3685         case DW_OP_breg20:
3686         case DW_OP_breg21:
3687         case DW_OP_breg22:
3688         case DW_OP_breg23:
3689         case DW_OP_breg24:
3690         case DW_OP_breg25:
3691         case DW_OP_breg26:
3692         case DW_OP_breg27:
3693         case DW_OP_breg28:
3694         case DW_OP_breg29:
3695         case DW_OP_breg30:
3696         case DW_OP_breg31:
3697           cfa->reg = op - DW_OP_breg0;
3698           cfa->base_offset = ptr->dw_loc_oprnd1.v.val_int;
3699           break;
3700         case DW_OP_bregx:
3701           cfa->reg = ptr->dw_loc_oprnd1.v.val_int;
3702           cfa->base_offset = ptr->dw_loc_oprnd2.v.val_int;
3703           break;
3704         case DW_OP_deref:
3705           cfa->indirect = 1;
3706           break;
3707         case DW_OP_plus_uconst:
3708           cfa->offset = ptr->dw_loc_oprnd1.v.val_unsigned;
3709           break;
3710         default:
3711           internal_error ("DW_LOC_OP %s not implemented",
3712                           dwarf_stack_op_name (ptr->dw_loc_opc));
3713         }
3714     }
3715 }
3716 #endif /* .debug_frame support */
3717 \f
3718 /* And now, the support for symbolic debugging information.  */
3719 #ifdef DWARF2_DEBUGGING_INFO
3720
3721 /* .debug_str support.  */
3722 static int output_indirect_string (void **, void *);
3723
3724 static void dwarf2out_init (const char *);
3725 static void dwarf2out_finish (const char *);
3726 static void dwarf2out_define (unsigned int, const char *);
3727 static void dwarf2out_undef (unsigned int, const char *);
3728 static void dwarf2out_start_source_file (unsigned, const char *);
3729 static void dwarf2out_end_source_file (unsigned);
3730 static void dwarf2out_begin_block (unsigned, unsigned);
3731 static void dwarf2out_end_block (unsigned, unsigned);
3732 static bool dwarf2out_ignore_block (const_tree);
3733 static void dwarf2out_global_decl (tree);
3734 static void dwarf2out_type_decl (tree, int);
3735 static void dwarf2out_imported_module_or_decl (tree, tree);
3736 static void dwarf2out_abstract_function (tree);
3737 static void dwarf2out_var_location (rtx);
3738 static void dwarf2out_begin_function (tree);
3739
3740 /* The debug hooks structure.  */
3741
3742 const struct gcc_debug_hooks dwarf2_debug_hooks =
3743 {
3744   dwarf2out_init,
3745   dwarf2out_finish,
3746   dwarf2out_define,
3747   dwarf2out_undef,
3748   dwarf2out_start_source_file,
3749   dwarf2out_end_source_file,
3750   dwarf2out_begin_block,
3751   dwarf2out_end_block,
3752   dwarf2out_ignore_block,
3753   dwarf2out_source_line,
3754   dwarf2out_begin_prologue,
3755   debug_nothing_int_charstar,   /* end_prologue */
3756   dwarf2out_end_epilogue,
3757   dwarf2out_begin_function,
3758   debug_nothing_int,            /* end_function */
3759   dwarf2out_decl,               /* function_decl */
3760   dwarf2out_global_decl,
3761   dwarf2out_type_decl,          /* type_decl */
3762   dwarf2out_imported_module_or_decl,
3763   debug_nothing_tree,           /* deferred_inline_function */
3764   /* The DWARF 2 backend tries to reduce debugging bloat by not
3765      emitting the abstract description of inline functions until
3766      something tries to reference them.  */
3767   dwarf2out_abstract_function,  /* outlining_inline_function */
3768   debug_nothing_rtx,            /* label */
3769   debug_nothing_int,            /* handle_pch */
3770   dwarf2out_var_location,
3771   dwarf2out_switch_text_section,
3772   1                             /* start_end_main_source_file */
3773 };
3774 #endif
3775 \f
3776 /* NOTE: In the comments in this file, many references are made to
3777    "Debugging Information Entries".  This term is abbreviated as `DIE'
3778    throughout the remainder of this file.  */
3779
3780 /* An internal representation of the DWARF output is built, and then
3781    walked to generate the DWARF debugging info.  The walk of the internal
3782    representation is done after the entire program has been compiled.
3783    The types below are used to describe the internal representation.  */
3784
3785 /* Various DIE's use offsets relative to the beginning of the
3786    .debug_info section to refer to each other.  */
3787
3788 typedef long int dw_offset;
3789
3790 /* Define typedefs here to avoid circular dependencies.  */
3791
3792 typedef struct dw_attr_struct *dw_attr_ref;
3793 typedef struct dw_line_info_struct *dw_line_info_ref;
3794 typedef struct dw_separate_line_info_struct *dw_separate_line_info_ref;
3795 typedef struct pubname_struct *pubname_ref;
3796 typedef struct dw_ranges_struct *dw_ranges_ref;
3797 typedef struct dw_ranges_by_label_struct *dw_ranges_by_label_ref;
3798
3799 /* Each entry in the line_info_table maintains the file and
3800    line number associated with the label generated for that
3801    entry.  The label gives the PC value associated with
3802    the line number entry.  */
3803
3804 typedef struct dw_line_info_struct GTY(())
3805 {
3806   unsigned long dw_file_num;
3807   unsigned long dw_line_num;
3808 }
3809 dw_line_info_entry;
3810
3811 /* Line information for functions in separate sections; each one gets its
3812    own sequence.  */
3813 typedef struct dw_separate_line_info_struct GTY(())
3814 {
3815   unsigned long dw_file_num;
3816   unsigned long dw_line_num;
3817   unsigned long function;
3818 }
3819 dw_separate_line_info_entry;
3820
3821 /* Each DIE attribute has a field specifying the attribute kind,
3822    a link to the next attribute in the chain, and an attribute value.
3823    Attributes are typically linked below the DIE they modify.  */
3824
3825 typedef struct dw_attr_struct GTY(())
3826 {
3827   enum dwarf_attribute dw_attr;
3828   dw_val_node dw_attr_val;
3829 }
3830 dw_attr_node;
3831
3832 DEF_VEC_O(dw_attr_node);
3833 DEF_VEC_ALLOC_O(dw_attr_node,gc);
3834
3835 /* The Debugging Information Entry (DIE) structure.  DIEs form a tree.
3836    The children of each node form a circular list linked by
3837    die_sib.  die_child points to the node *before* the "first" child node.  */
3838
3839 typedef struct die_struct GTY(())
3840 {
3841   enum dwarf_tag die_tag;
3842   char *die_symbol;
3843   VEC(dw_attr_node,gc) * die_attr;
3844   dw_die_ref die_parent;
3845   dw_die_ref die_child;
3846   dw_die_ref die_sib;
3847   dw_die_ref die_definition; /* ref from a specification to its definition */
3848   dw_offset die_offset;
3849   unsigned long die_abbrev;
3850   int die_mark;
3851   /* Die is used and must not be pruned as unused.  */
3852   int die_perennial_p;
3853   unsigned int decl_id;
3854 }
3855 die_node;
3856
3857 /* Evaluate 'expr' while 'c' is set to each child of DIE in order.  */
3858 #define FOR_EACH_CHILD(die, c, expr) do {       \
3859   c = die->die_child;                           \
3860   if (c) do {                                   \
3861     c = c->die_sib;                             \
3862     expr;                                       \
3863   } while (c != die->die_child);                \
3864 } while (0)
3865
3866 /* The pubname structure */
3867
3868 typedef struct pubname_struct GTY(())
3869 {
3870   dw_die_ref die;
3871   const char *name;
3872 }
3873 pubname_entry;
3874
3875 DEF_VEC_O(pubname_entry);
3876 DEF_VEC_ALLOC_O(pubname_entry, gc);
3877
3878 struct dw_ranges_struct GTY(())
3879 {
3880   /* If this is positive, it's a block number, otherwise it's a
3881      bitwise-negated index into dw_ranges_by_label.  */
3882   int num;
3883 };
3884
3885 struct dw_ranges_by_label_struct GTY(())
3886 {
3887   const char *begin;
3888   const char *end;
3889 };
3890
3891 /* The limbo die list structure.  */
3892 typedef struct limbo_die_struct GTY(())
3893 {
3894   dw_die_ref die;
3895   tree created_for;
3896   struct limbo_die_struct *next;
3897 }
3898 limbo_die_node;
3899
3900 /* How to start an assembler comment.  */
3901 #ifndef ASM_COMMENT_START
3902 #define ASM_COMMENT_START ";#"
3903 #endif
3904
3905 /* Define a macro which returns nonzero for a TYPE_DECL which was
3906    implicitly generated for a tagged type.
3907
3908    Note that unlike the gcc front end (which generates a NULL named
3909    TYPE_DECL node for each complete tagged type, each array type, and
3910    each function type node created) the g++ front end generates a
3911    _named_ TYPE_DECL node for each tagged type node created.
3912    These TYPE_DECLs have DECL_ARTIFICIAL set, so we know not to
3913    generate a DW_TAG_typedef DIE for them.  */
3914
3915 #define TYPE_DECL_IS_STUB(decl)                         \
3916   (DECL_NAME (decl) == NULL_TREE                        \
3917    || (DECL_ARTIFICIAL (decl)                           \
3918        && is_tagged_type (TREE_TYPE (decl))             \
3919        && ((decl == TYPE_STUB_DECL (TREE_TYPE (decl)))  \
3920            /* This is necessary for stub decls that     \
3921               appear in nested inline functions.  */    \
3922            || (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE \
3923                && (decl_ultimate_origin (decl)          \
3924                    == TYPE_STUB_DECL (TREE_TYPE (decl)))))))
3925
3926 /* Information concerning the compilation unit's programming
3927    language, and compiler version.  */
3928
3929 /* Fixed size portion of the DWARF compilation unit header.  */
3930 #define DWARF_COMPILE_UNIT_HEADER_SIZE \
3931   (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 3)
3932
3933 /* Fixed size portion of public names info.  */
3934 #define DWARF_PUBNAMES_HEADER_SIZE (2 * DWARF_OFFSET_SIZE + 2)
3935
3936 /* Fixed size portion of the address range info.  */
3937 #define DWARF_ARANGES_HEADER_SIZE                                       \
3938   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4,      \
3939                 DWARF2_ADDR_SIZE * 2)                                   \
3940    - DWARF_INITIAL_LENGTH_SIZE)
3941
3942 /* Size of padding portion in the address range info.  It must be
3943    aligned to twice the pointer size.  */
3944 #define DWARF_ARANGES_PAD_SIZE \
3945   (DWARF_ROUND (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4, \
3946                 DWARF2_ADDR_SIZE * 2)                              \
3947    - (DWARF_INITIAL_LENGTH_SIZE + DWARF_OFFSET_SIZE + 4))
3948
3949 /* Use assembler line directives if available.  */
3950 #ifndef DWARF2_ASM_LINE_DEBUG_INFO
3951 #ifdef HAVE_AS_DWARF2_DEBUG_LINE
3952 #define DWARF2_ASM_LINE_DEBUG_INFO 1
3953 #else
3954 #define DWARF2_ASM_LINE_DEBUG_INFO 0
3955 #endif
3956 #endif
3957
3958 /* Minimum line offset in a special line info. opcode.
3959    This value was chosen to give a reasonable range of values.  */
3960 #define DWARF_LINE_BASE  -10
3961
3962 /* First special line opcode - leave room for the standard opcodes.  */
3963 #define DWARF_LINE_OPCODE_BASE  10
3964
3965 /* Range of line offsets in a special line info. opcode.  */
3966 #define DWARF_LINE_RANGE  (254-DWARF_LINE_OPCODE_BASE+1)
3967
3968 /* Flag that indicates the initial value of the is_stmt_start flag.
3969    In the present implementation, we do not mark any lines as
3970    the beginning of a source statement, because that information
3971    is not made available by the GCC front-end.  */
3972 #define DWARF_LINE_DEFAULT_IS_STMT_START 1
3973
3974 #ifdef DWARF2_DEBUGGING_INFO
3975 /* This location is used by calc_die_sizes() to keep track
3976    the offset of each DIE within the .debug_info section.  */
3977 static unsigned long next_die_offset;
3978 #endif
3979
3980 /* Record the root of the DIE's built for the current compilation unit.  */
3981 static GTY(()) dw_die_ref comp_unit_die;
3982
3983 /* A list of DIEs with a NULL parent waiting to be relocated.  */
3984 static GTY(()) limbo_die_node *limbo_die_list;
3985
3986 /* Filenames referenced by this compilation unit.  */
3987 static GTY((param_is (struct dwarf_file_data))) htab_t file_table;
3988
3989 /* A hash table of references to DIE's that describe declarations.
3990    The key is a DECL_UID() which is a unique number identifying each decl.  */
3991 static GTY ((param_is (struct die_struct))) htab_t decl_die_table;
3992
3993 /* Node of the variable location list.  */
3994 struct var_loc_node GTY ((chain_next ("%h.next")))
3995 {
3996   rtx GTY (()) var_loc_note;
3997   const char * GTY (()) label;
3998   const char * GTY (()) section_label;
3999   struct var_loc_node * GTY (()) next;
4000 };
4001
4002 /* Variable location list.  */
4003 struct var_loc_list_def GTY (())
4004 {
4005   struct var_loc_node * GTY (()) first;
4006
4007   /* Do not mark the last element of the chained list because
4008      it is marked through the chain.  */
4009   struct var_loc_node * GTY ((skip ("%h"))) last;
4010
4011   /* DECL_UID of the variable decl.  */
4012   unsigned int decl_id;
4013 };
4014 typedef struct var_loc_list_def var_loc_list;
4015
4016
4017 /* Table of decl location linked lists.  */
4018 static GTY ((param_is (var_loc_list))) htab_t decl_loc_table;
4019
4020 /* A pointer to the base of a list of references to DIE's that
4021    are uniquely identified by their tag, presence/absence of
4022    children DIE's, and list of attribute/value pairs.  */
4023 static GTY((length ("abbrev_die_table_allocated")))
4024   dw_die_ref *abbrev_die_table;
4025
4026 /* Number of elements currently allocated for abbrev_die_table.  */
4027 static GTY(()) unsigned abbrev_die_table_allocated;
4028
4029 /* Number of elements in type_die_table currently in use.  */
4030 static GTY(()) unsigned abbrev_die_table_in_use;
4031
4032 /* Size (in elements) of increments by which we may expand the
4033    abbrev_die_table.  */
4034 #define ABBREV_DIE_TABLE_INCREMENT 256
4035
4036 /* A pointer to the base of a table that contains line information
4037    for each source code line in .text in the compilation unit.  */
4038 static GTY((length ("line_info_table_allocated")))
4039      dw_line_info_ref line_info_table;
4040
4041 /* Number of elements currently allocated for line_info_table.  */
4042 static GTY(()) unsigned line_info_table_allocated;
4043
4044 /* Number of elements in line_info_table currently in use.  */
4045 static GTY(()) unsigned line_info_table_in_use;
4046
4047 /* A pointer to the base of a table that contains line information
4048    for each source code line outside of .text in the compilation unit.  */
4049 static GTY ((length ("separate_line_info_table_allocated")))
4050      dw_separate_line_info_ref separate_line_info_table;
4051
4052 /* Number of elements currently allocated for separate_line_info_table.  */
4053 static GTY(()) unsigned separate_line_info_table_allocated;
4054
4055 /* Number of elements in separate_line_info_table currently in use.  */
4056 static GTY(()) unsigned separate_line_info_table_in_use;
4057
4058 /* Size (in elements) of increments by which we may expand the
4059    line_info_table.  */
4060 #define LINE_INFO_TABLE_INCREMENT 1024
4061
4062 /* A pointer to the base of a table that contains a list of publicly
4063    accessible names.  */
4064 static GTY (()) VEC (pubname_entry, gc) *  pubname_table;
4065
4066 /* A pointer to the base of a table that contains a list of publicly
4067    accessible types.  */
4068 static GTY (()) VEC (pubname_entry, gc) * pubtype_table;
4069
4070 /* Array of dies for which we should generate .debug_arange info.  */
4071 static GTY((length ("arange_table_allocated"))) dw_die_ref *arange_table;
4072
4073 /* Number of elements currently allocated for arange_table.  */
4074 static GTY(()) unsigned arange_table_allocated;
4075
4076 /* Number of elements in arange_table currently in use.  */
4077 static GTY(()) unsigned arange_table_in_use;
4078
4079 /* Size (in elements) of increments by which we may expand the
4080    arange_table.  */
4081 #define ARANGE_TABLE_INCREMENT 64
4082
4083 /* Array of dies for which we should generate .debug_ranges info.  */
4084 static GTY ((length ("ranges_table_allocated"))) dw_ranges_ref ranges_table;
4085
4086 /* Number of elements currently allocated for ranges_table.  */
4087 static GTY(()) unsigned ranges_table_allocated;
4088
4089 /* Number of elements in ranges_table currently in use.  */
4090 static GTY(()) unsigned ranges_table_in_use;
4091
4092 /* Array of pairs of labels referenced in ranges_table.  */
4093 static GTY ((length ("ranges_by_label_allocated")))
4094      dw_ranges_by_label_ref ranges_by_label;
4095
4096 /* Number of elements currently allocated for ranges_by_label.  */
4097 static GTY(()) unsigned ranges_by_label_allocated;
4098
4099 /* Number of elements in ranges_by_label currently in use.  */
4100 static GTY(()) unsigned ranges_by_label_in_use;
4101
4102 /* Size (in elements) of increments by which we may expand the
4103    ranges_table.  */
4104 #define RANGES_TABLE_INCREMENT 64
4105
4106 /* Whether we have location lists that need outputting */
4107 static GTY(()) bool have_location_lists;
4108
4109 /* Unique label counter.  */
4110 static GTY(()) unsigned int loclabel_num;
4111
4112 #ifdef DWARF2_DEBUGGING_INFO
4113 /* Record whether the function being analyzed contains inlined functions.  */
4114 static int current_function_has_inlines;
4115 #endif
4116 #if 0 && defined (MIPS_DEBUGGING_INFO)
4117 static int comp_unit_has_inlines;
4118 #endif
4119
4120 /* The last file entry emitted by maybe_emit_file().  */
4121 static GTY(()) struct dwarf_file_data * last_emitted_file;
4122
4123 /* Number of internal labels generated by gen_internal_sym().  */
4124 static GTY(()) int label_num;
4125
4126 /* Cached result of previous call to lookup_filename.  */
4127 static GTY(()) struct dwarf_file_data * file_table_last_lookup;
4128
4129 #ifdef DWARF2_DEBUGGING_INFO
4130
4131 /* Offset from the "steady-state frame pointer" to the frame base,
4132    within the current function.  */
4133 static HOST_WIDE_INT frame_pointer_fb_offset;
4134
4135 /* Forward declarations for functions defined in this file.  */
4136
4137 static int is_pseudo_reg (const_rtx);
4138 static tree type_main_variant (tree);
4139 static int is_tagged_type (const_tree);
4140 static const char *dwarf_tag_name (unsigned);
4141 static const char *dwarf_attr_name (unsigned);
4142 static const char *dwarf_form_name (unsigned);
4143 static tree decl_ultimate_origin (const_tree);
4144 static tree block_ultimate_origin (const_tree);
4145 static tree decl_class_context (tree);
4146 static void add_dwarf_attr (dw_die_ref, dw_attr_ref);
4147 static inline enum dw_val_class AT_class (dw_attr_ref);
4148 static void add_AT_flag (dw_die_ref, enum dwarf_attribute, unsigned);
4149 static inline unsigned AT_flag (dw_attr_ref);
4150 static void add_AT_int (dw_die_ref, enum dwarf_attribute, HOST_WIDE_INT);
4151 static inline HOST_WIDE_INT AT_int (dw_attr_ref);
4152 static void add_AT_unsigned (dw_die_ref, enum dwarf_attribute, unsigned HOST_WIDE_INT);
4153 static inline unsigned HOST_WIDE_INT AT_unsigned (dw_attr_ref);
4154 static void add_AT_long_long (dw_die_ref, enum dwarf_attribute, unsigned long,
4155                               unsigned long);
4156 static inline void add_AT_vec (dw_die_ref, enum dwarf_attribute, unsigned int,
4157                                unsigned int, unsigned char *);
4158 static hashval_t debug_str_do_hash (const void *);
4159 static int debug_str_eq (const void *, const void *);
4160 static void add_AT_string (dw_die_ref, enum dwarf_attribute, const char *);
4161 static inline const char *AT_string (dw_attr_ref);
4162 static int AT_string_form (dw_attr_ref);
4163 static void add_AT_die_ref (dw_die_ref, enum dwarf_attribute, dw_die_ref);
4164 static void add_AT_specification (dw_die_ref, dw_die_ref);
4165 static inline dw_die_ref AT_ref (dw_attr_ref);
4166 static inline int AT_ref_external (dw_attr_ref);
4167 static inline void set_AT_ref_external (dw_attr_ref, int);
4168 static void add_AT_fde_ref (dw_die_ref, enum dwarf_attribute, unsigned);
4169 static void add_AT_loc (dw_die_ref, enum dwarf_attribute, dw_loc_descr_ref);
4170 static inline dw_loc_descr_ref AT_loc (dw_attr_ref);
4171 static void add_AT_loc_list (dw_die_ref, enum dwarf_attribute,
4172                              dw_loc_list_ref);
4173 static inline dw_loc_list_ref AT_loc_list (dw_attr_ref);
4174 static void add_AT_addr (dw_die_ref, enum dwarf_attribute, rtx);
4175 static inline rtx AT_addr (dw_attr_ref);
4176 static void add_AT_lbl_id (dw_die_ref, enum dwarf_attribute, const char *);
4177 static void add_AT_lineptr (dw_die_ref, enum dwarf_attribute, const char *);
4178 static void add_AT_macptr (dw_die_ref, enum dwarf_attribute, const char *);
4179 static void add_AT_offset (dw_die_ref, enum dwarf_attribute,
4180                            unsigned HOST_WIDE_INT);
4181 static void add_AT_range_list (dw_die_ref, enum dwarf_attribute,
4182                                unsigned long);
4183 static inline const char *AT_lbl (dw_attr_ref);
4184 static dw_attr_ref get_AT (dw_die_ref, enum dwarf_attribute);
4185 static const char *get_AT_low_pc (dw_die_ref);
4186 static const char *get_AT_hi_pc (dw_die_ref);
4187 static const char *get_AT_string (dw_die_ref, enum dwarf_attribute);
4188 static int get_AT_flag (dw_die_ref, enum dwarf_attribute);
4189 static unsigned get_AT_unsigned (dw_die_ref, enum dwarf_attribute);
4190 static inline dw_die_ref get_AT_ref (dw_die_ref, enum dwarf_attribute);
4191 static bool is_c_family (void);
4192 static bool is_cxx (void);
4193 static bool is_java (void);
4194 static bool is_fortran (void);
4195 static bool is_ada (void);
4196 static void remove_AT (dw_die_ref, enum dwarf_attribute);
4197 static void remove_child_TAG (dw_die_ref, enum dwarf_tag);
4198 static void add_child_die (dw_die_ref, dw_die_ref);
4199 static dw_die_ref new_die (enum dwarf_tag, dw_die_ref, tree);
4200 static dw_die_ref lookup_type_die (tree);
4201 static void equate_type_number_to_die (tree, dw_die_ref);
4202 static hashval_t decl_die_table_hash (const void *);
4203 static int decl_die_table_eq (const void *, const void *);
4204 static dw_die_ref lookup_decl_die (tree);
4205 static hashval_t decl_loc_table_hash (const void *);
4206 static int decl_loc_table_eq (const void *, const void *);
4207 static var_loc_list *lookup_decl_loc (const_tree);
4208 static void equate_decl_number_to_die (tree, dw_die_ref);
4209 static void add_var_loc_to_decl (tree, struct var_loc_node *);
4210 static void print_spaces (FILE *);
4211 static void print_die (dw_die_ref, FILE *);
4212 static void print_dwarf_line_table (FILE *);
4213 static dw_die_ref push_new_compile_unit (dw_die_ref, dw_die_ref);
4214 static dw_die_ref pop_compile_unit (dw_die_ref);
4215 static void loc_checksum (dw_loc_descr_ref, struct md5_ctx *);
4216 static void attr_checksum (dw_attr_ref, struct md5_ctx *, int *);
4217 static void die_checksum (dw_die_ref, struct md5_ctx *, int *);
4218 static int same_loc_p (dw_loc_descr_ref, dw_loc_descr_ref, int *);
4219 static int same_dw_val_p (const dw_val_node *, const dw_val_node *, int *);
4220 static int same_attr_p (dw_attr_ref, dw_attr_ref, int *);
4221 static int same_die_p (dw_die_ref, dw_die_ref, int *);
4222 static int same_die_p_wrap (dw_die_ref, dw_die_ref);
4223 static void compute_section_prefix (dw_die_ref);
4224 static int is_type_die (dw_die_ref);
4225 static int is_comdat_die (dw_die_ref);
4226 static int is_symbol_die (dw_die_ref);
4227 static void assign_symbol_names (dw_die_ref);
4228 static void break_out_includes (dw_die_ref);
4229 static hashval_t htab_cu_hash (const void *);
4230 static int htab_cu_eq (const void *, const void *);
4231 static void htab_cu_del (void *);
4232 static int check_duplicate_cu (dw_die_ref, htab_t, unsigned *);
4233 static void record_comdat_symbol_number (dw_die_ref, htab_t, unsigned);
4234 static void add_sibling_attributes (dw_die_ref);
4235 static void build_abbrev_table (dw_die_ref);
4236 static void output_location_lists (dw_die_ref);
4237 static int constant_size (long unsigned);
4238 static unsigned long size_of_die (dw_die_ref);
4239 static void calc_die_sizes (dw_die_ref);
4240 static void mark_dies (dw_die_ref);
4241 static void unmark_dies (dw_die_ref);
4242 static void unmark_all_dies (dw_die_ref);
4243 static unsigned long size_of_pubnames (VEC (pubname_entry,gc) *);
4244 static unsigned long size_of_aranges (void);
4245 static enum dwarf_form value_format (dw_attr_ref);
4246 static void output_value_format (dw_attr_ref);
4247 static void output_abbrev_section (void);
4248 static void output_die_symbol (dw_die_ref);
4249 static void output_die (dw_die_ref);
4250 static void output_compilation_unit_header (void);
4251 static void output_comp_unit (dw_die_ref, int);
4252 static const char *dwarf2_name (tree, int);
4253 static void add_pubname (tree, dw_die_ref);
4254 static void add_pubtype (tree, dw_die_ref);
4255 static void output_pubnames (VEC (pubname_entry,gc) *);
4256 static void add_arange (tree, dw_die_ref);
4257 static void output_aranges (void);
4258 static unsigned int add_ranges_num (int);
4259 static unsigned int add_ranges (const_tree);
4260 static unsigned int add_ranges_by_labels (const char *, const char *);
4261 static void output_ranges (void);
4262 static void output_line_info (void);
4263 static void output_file_names (void);
4264 static dw_die_ref base_type_die (tree);
4265 static int is_base_type (tree);
4266 static bool is_subrange_type (const_tree);
4267 static dw_die_ref subrange_type_die (tree, dw_die_ref);
4268 static dw_die_ref modified_type_die (tree, int, int, dw_die_ref);
4269 static int type_is_enum (const_tree);
4270 static unsigned int dbx_reg_number (const_rtx);
4271 static void add_loc_descr_op_piece (dw_loc_descr_ref *, int);
4272 static dw_loc_descr_ref reg_loc_descriptor (rtx, enum var_init_status);
4273 static dw_loc_descr_ref one_reg_loc_descriptor (unsigned int, 
4274                                                 enum var_init_status);
4275 static dw_loc_descr_ref multiple_reg_loc_descriptor (rtx, rtx,
4276                                                      enum var_init_status);
4277 static dw_loc_descr_ref int_loc_descriptor (HOST_WIDE_INT);
4278 static dw_loc_descr_ref based_loc_descr (rtx, HOST_WIDE_INT,
4279                                          enum var_init_status);
4280 static int is_based_loc (const_rtx);
4281 static dw_loc_descr_ref mem_loc_descriptor (rtx, enum machine_mode mode,
4282                                             enum var_init_status);
4283 static dw_loc_descr_ref concat_loc_descriptor (rtx, rtx,
4284                                                enum var_init_status);
4285 static dw_loc_descr_ref loc_descriptor (rtx, enum var_init_status);
4286 static dw_loc_descr_ref loc_descriptor_from_tree_1 (tree, int);
4287 static dw_loc_descr_ref loc_descriptor_from_tree (tree);
4288 static HOST_WIDE_INT ceiling (HOST_WIDE_INT, unsigned int);
4289 static tree field_type (const_tree);
4290 static unsigned int simple_type_align_in_bits (const_tree);
4291 static unsigned int simple_decl_align_in_bits (const_tree);
4292 static unsigned HOST_WIDE_INT simple_type_size_in_bits (const_tree);
4293 static HOST_WIDE_INT field_byte_offset (const_tree);
4294 static void add_AT_location_description (dw_die_ref, enum dwarf_attribute,
4295                                          dw_loc_descr_ref);
4296 static void add_data_member_location_attribute (dw_die_ref, tree);
4297 static void add_const_value_attribute (dw_die_ref, rtx);
4298 static void insert_int (HOST_WIDE_INT, unsigned, unsigned char *);
4299 static HOST_WIDE_INT extract_int (const unsigned char *, unsigned);
4300 static void insert_float (const_rtx, unsigned char *);
4301 static rtx rtl_for_decl_location (tree);
4302 static void add_location_or_const_value_attribute (dw_die_ref, tree,
4303                                                    enum dwarf_attribute);
4304 static void tree_add_const_value_attribute (dw_die_ref, tree);
4305 static void add_name_attribute (dw_die_ref, const char *);
4306 static void add_comp_dir_attribute (dw_die_ref);
4307 static void add_bound_info (dw_die_ref, enum dwarf_attribute, tree);
4308 static void add_subscript_info (dw_die_ref, tree);
4309 static void add_byte_size_attribute (dw_die_ref, tree);
4310 static void add_bit_offset_attribute (dw_die_ref, tree);
4311 static void add_bit_size_attribute (dw_die_ref, tree);
4312 static void add_prototyped_attribute (dw_die_ref, tree);
4313 static void add_abstract_origin_attribute (dw_die_ref, tree);
4314 static void add_pure_or_virtual_attribute (dw_die_ref, tree);
4315 static void add_src_coords_attributes (dw_die_ref, tree);
4316 static void add_name_and_src_coords_attributes (dw_die_ref, tree);
4317 static void push_decl_scope (tree);
4318 static void pop_decl_scope (void);
4319 static dw_die_ref scope_die_for (tree, dw_die_ref);
4320 static inline int local_scope_p (dw_die_ref);
4321 static inline int class_or_namespace_scope_p (dw_die_ref);
4322 static void add_type_attribute (dw_die_ref, tree, int, int, dw_die_ref);
4323 static void add_calling_convention_attribute (dw_die_ref, tree);
4324 static const char *type_tag (const_tree);
4325 static tree member_declared_type (const_tree);
4326 #if 0
4327 static const char *decl_start_label (tree);
4328 #endif
4329 static void gen_array_type_die (tree, dw_die_ref);
4330 static void gen_descr_array_type_die (tree, struct array_descr_info *, dw_die_ref);
4331 #if 0
4332 static void gen_entry_point_die (tree, dw_die_ref);
4333 #endif
4334 static void gen_inlined_enumeration_type_die (tree, dw_die_ref);
4335 static void gen_inlined_structure_type_die (tree, dw_die_ref);
4336 static void gen_inlined_union_type_die (tree, dw_die_ref);
4337 static dw_die_ref gen_enumeration_type_die (tree, dw_die_ref);
4338 static dw_die_ref gen_formal_parameter_die (tree, dw_die_ref);
4339 static void gen_unspecified_parameters_die (tree, dw_die_ref);
4340 static void gen_formal_types_die (tree, dw_die_ref);
4341 static void gen_subprogram_die (tree, dw_die_ref);
4342 static void gen_variable_die (tree, dw_die_ref);
4343 static void gen_label_die (tree, dw_die_ref);
4344 static void gen_lexical_block_die (tree, dw_die_ref, int);
4345 static void gen_inlined_subroutine_die (tree, dw_die_ref, int);
4346 static void gen_field_die (tree, dw_die_ref);
4347 static void gen_ptr_to_mbr_type_die (tree, dw_die_ref);
4348 static dw_die_ref gen_compile_unit_die (const char *);
4349 static void gen_inheritance_die (tree, tree, dw_die_ref);
4350 static void gen_member_die (tree, dw_die_ref);
4351 static void gen_struct_or_union_type_die (tree, dw_die_ref,
4352                                                 enum debug_info_usage);
4353 static void gen_subroutine_type_die (tree, dw_die_ref);
4354 static void gen_typedef_die (tree, dw_die_ref);
4355 static void gen_type_die (tree, dw_die_ref);
4356 static void gen_tagged_type_instantiation_die (tree, dw_die_ref);
4357 static void gen_block_die (tree, dw_die_ref, int);
4358 static void decls_for_scope (tree, dw_die_ref, int);
4359 static int is_redundant_typedef (const_tree);
4360 static void gen_namespace_die (tree);
4361 static void gen_decl_die (tree, dw_die_ref);
4362 static dw_die_ref force_decl_die (tree);
4363 static dw_die_ref force_type_die (tree);
4364 static dw_die_ref setup_namespace_context (tree, dw_die_ref);
4365 static void declare_in_namespace (tree, dw_die_ref);
4366 static struct dwarf_file_data * lookup_filename (const char *);
4367 static void retry_incomplete_types (void);
4368 static void gen_type_die_for_member (tree, tree, dw_die_ref);
4369 static void splice_child_die (dw_die_ref, dw_die_ref);
4370 static int file_info_cmp (const void *, const void *);
4371 static dw_loc_list_ref new_loc_list (dw_loc_descr_ref, const char *,
4372                                      const char *, const char *, unsigned);
4373 static void add_loc_descr_to_loc_list (dw_loc_list_ref *, dw_loc_descr_ref,
4374                                        const char *, const char *,
4375                                        const char *);
4376 static void output_loc_list (dw_loc_list_ref);
4377 static char *gen_internal_sym (const char *);
4378
4379 static void prune_unmark_dies (dw_die_ref);
4380 static void prune_unused_types_mark (dw_die_ref, int);
4381 static void prune_unused_types_walk (dw_die_ref);
4382 static void prune_unused_types_walk_attribs (dw_die_ref);
4383 static void prune_unused_types_prune (dw_die_ref);
4384 static void prune_unused_types (void);
4385 static int maybe_emit_file (struct dwarf_file_data *fd);
4386
4387 /* Section names used to hold DWARF debugging information.  */
4388 #ifndef DEBUG_INFO_SECTION
4389 #define DEBUG_INFO_SECTION      ".debug_info"
4390 #endif
4391 #ifndef DEBUG_ABBREV_SECTION
4392 #define DEBUG_ABBREV_SECTION    ".debug_abbrev"
4393 #endif
4394 #ifndef DEBUG_ARANGES_SECTION
4395 #define DEBUG_ARANGES_SECTION   ".debug_aranges"
4396 #endif
4397 #ifndef DEBUG_MACINFO_SECTION
4398 #define DEBUG_MACINFO_SECTION   ".debug_macinfo"
4399 #endif
4400 #ifndef DEBUG_LINE_SECTION
4401 #define DEBUG_LINE_SECTION      ".debug_line"
4402 #endif
4403 #ifndef DEBUG_LOC_SECTION
4404 #define DEBUG_LOC_SECTION       ".debug_loc"
4405 #endif
4406 #ifndef DEBUG_PUBNAMES_SECTION
4407 #define DEBUG_PUBNAMES_SECTION  ".debug_pubnames"
4408 #endif
4409 #ifndef DEBUG_STR_SECTION
4410 #define DEBUG_STR_SECTION       ".debug_str"
4411 #endif
4412 #ifndef DEBUG_RANGES_SECTION
4413 #define DEBUG_RANGES_SECTION    ".debug_ranges"
4414 #endif
4415
4416 /* Standard ELF section names for compiled code and data.  */
4417 #ifndef TEXT_SECTION_NAME
4418 #define TEXT_SECTION_NAME       ".text"
4419 #endif
4420
4421 /* Section flags for .debug_str section.  */
4422 #define DEBUG_STR_SECTION_FLAGS \
4423   (HAVE_GAS_SHF_MERGE && flag_merge_debug_strings               \
4424    ? SECTION_DEBUG | SECTION_MERGE | SECTION_STRINGS | 1        \
4425    : SECTION_DEBUG)
4426
4427 /* Labels we insert at beginning sections we can reference instead of
4428    the section names themselves.  */
4429
4430 #ifndef TEXT_SECTION_LABEL
4431 #define TEXT_SECTION_LABEL              "Ltext"
4432 #endif
4433 #ifndef COLD_TEXT_SECTION_LABEL
4434 #define COLD_TEXT_SECTION_LABEL         "Ltext_cold"
4435 #endif
4436 #ifndef DEBUG_LINE_SECTION_LABEL
4437 #define DEBUG_LINE_SECTION_LABEL        "Ldebug_line"
4438 #endif
4439 #ifndef DEBUG_INFO_SECTION_LABEL
4440 #define DEBUG_INFO_SECTION_LABEL        "Ldebug_info"
4441 #endif
4442 #ifndef DEBUG_ABBREV_SECTION_LABEL
4443 #define DEBUG_ABBREV_SECTION_LABEL      "Ldebug_abbrev"
4444 #endif
4445 #ifndef DEBUG_LOC_SECTION_LABEL
4446 #define DEBUG_LOC_SECTION_LABEL         "Ldebug_loc"
4447 #endif
4448 #ifndef DEBUG_RANGES_SECTION_LABEL
4449 #define DEBUG_RANGES_SECTION_LABEL      "Ldebug_ranges"
4450 #endif
4451 #ifndef DEBUG_MACINFO_SECTION_LABEL
4452 #define DEBUG_MACINFO_SECTION_LABEL     "Ldebug_macinfo"
4453 #endif
4454
4455 /* Definitions of defaults for formats and names of various special
4456    (artificial) labels which may be generated within this file (when the -g
4457    options is used and DWARF2_DEBUGGING_INFO is in effect.
4458    If necessary, these may be overridden from within the tm.h file, but
4459    typically, overriding these defaults is unnecessary.  */
4460
4461 static char text_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4462 static char text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4463 static char cold_text_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4464 static char cold_end_label[MAX_ARTIFICIAL_LABEL_BYTES];
4465 static char abbrev_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4466 static char debug_info_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4467 static char debug_line_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4468 static char macinfo_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4469 static char loc_section_label[MAX_ARTIFICIAL_LABEL_BYTES];
4470 static char ranges_section_label[2 * MAX_ARTIFICIAL_LABEL_BYTES];
4471
4472 #ifndef TEXT_END_LABEL
4473 #define TEXT_END_LABEL          "Letext"
4474 #endif
4475 #ifndef COLD_END_LABEL
4476 #define COLD_END_LABEL          "Letext_cold"
4477 #endif
4478 #ifndef BLOCK_BEGIN_LABEL
4479 #define BLOCK_BEGIN_LABEL       "LBB"
4480 #endif
4481 #ifndef BLOCK_END_LABEL
4482 #define BLOCK_END_LABEL         "LBE"
4483 #endif
4484 #ifndef LINE_CODE_LABEL
4485 #define LINE_CODE_LABEL         "LM"
4486 #endif
4487 #ifndef SEPARATE_LINE_CODE_LABEL
4488 #define SEPARATE_LINE_CODE_LABEL        "LSM"
4489 #endif
4490
4491 \f
4492 /* We allow a language front-end to designate a function that is to be
4493    called to "demangle" any name before it is put into a DIE.  */
4494
4495 static const char *(*demangle_name_func) (const char *);
4496
4497 void
4498 dwarf2out_set_demangle_name_func (const char *(*func) (const char *))
4499 {
4500   demangle_name_func = func;
4501 }
4502
4503 /* Test if rtl node points to a pseudo register.  */
4504
4505 static inline int
4506 is_pseudo_reg (const_rtx rtl)
4507 {
4508   return ((REG_P (rtl) && REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
4509           || (GET_CODE (rtl) == SUBREG
4510               && REGNO (SUBREG_REG (rtl)) >= FIRST_PSEUDO_REGISTER));
4511 }
4512
4513 /* Return a reference to a type, with its const and volatile qualifiers
4514    removed.  */
4515
4516 static inline tree
4517 type_main_variant (tree type)
4518 {
4519   type = TYPE_MAIN_VARIANT (type);
4520
4521   /* ??? There really should be only one main variant among any group of
4522      variants of a given type (and all of the MAIN_VARIANT values for all
4523      members of the group should point to that one type) but sometimes the C
4524      front-end messes this up for array types, so we work around that bug
4525      here.  */
4526   if (TREE_CODE (type) == ARRAY_TYPE)
4527     while (type != TYPE_MAIN_VARIANT (type))
4528       type = TYPE_MAIN_VARIANT (type);
4529
4530   return type;
4531 }
4532
4533 /* Return nonzero if the given type node represents a tagged type.  */
4534
4535 static inline int
4536 is_tagged_type (const_tree type)
4537 {
4538   enum tree_code code = TREE_CODE (type);
4539
4540   return (code == RECORD_TYPE || code == UNION_TYPE
4541           || code == QUAL_UNION_TYPE || code == ENUMERAL_TYPE);
4542 }
4543
4544 /* Convert a DIE tag into its string name.  */
4545
4546 static const char *
4547 dwarf_tag_name (unsigned int tag)
4548 {
4549   switch (tag)
4550     {
4551     case DW_TAG_padding:
4552       return "DW_TAG_padding";
4553     case DW_TAG_array_type:
4554       return "DW_TAG_array_type";
4555     case DW_TAG_class_type:
4556       return "DW_TAG_class_type";
4557     case DW_TAG_entry_point:
4558       return "DW_TAG_entry_point";
4559     case DW_TAG_enumeration_type:
4560       return "DW_TAG_enumeration_type";
4561     case DW_TAG_formal_parameter:
4562       return "DW_TAG_formal_parameter";
4563     case DW_TAG_imported_declaration:
4564       return "DW_TAG_imported_declaration";
4565     case DW_TAG_label:
4566       return "DW_TAG_label";
4567     case DW_TAG_lexical_block:
4568       return "DW_TAG_lexical_block";
4569     case DW_TAG_member:
4570       return "DW_TAG_member";
4571     case DW_TAG_pointer_type:
4572       return "DW_TAG_pointer_type";
4573     case DW_TAG_reference_type:
4574       return "DW_TAG_reference_type";
4575     case DW_TAG_compile_unit:
4576       return "DW_TAG_compile_unit";
4577     case DW_TAG_string_type:
4578       return "DW_TAG_string_type";
4579     case DW_TAG_structure_type:
4580       return "DW_TAG_structure_type";
4581     case DW_TAG_subroutine_type:
4582       return "DW_TAG_subroutine_type";
4583     case DW_TAG_typedef:
4584       return "DW_TAG_typedef";
4585     case DW_TAG_union_type:
4586       return "DW_TAG_union_type";
4587     case DW_TAG_unspecified_parameters:
4588       return "DW_TAG_unspecified_parameters";
4589     case DW_TAG_variant:
4590       return "DW_TAG_variant";
4591     case DW_TAG_common_block:
4592       return "DW_TAG_common_block";
4593     case DW_TAG_common_inclusion:
4594       return "DW_TAG_common_inclusion";
4595     case DW_TAG_inheritance:
4596       return "DW_TAG_inheritance";
4597     case DW_TAG_inlined_subroutine:
4598       return "DW_TAG_inlined_subroutine";
4599     case DW_TAG_module:
4600       return "DW_TAG_module";
4601     case DW_TAG_ptr_to_member_type:
4602       return "DW_TAG_ptr_to_member_type";
4603     case DW_TAG_set_type:
4604       return "DW_TAG_set_type";
4605     case DW_TAG_subrange_type:
4606       return "DW_TAG_subrange_type";
4607     case DW_TAG_with_stmt:
4608       return "DW_TAG_with_stmt";
4609     case DW_TAG_access_declaration:
4610       return "DW_TAG_access_declaration";
4611     case DW_TAG_base_type:
4612       return "DW_TAG_base_type";
4613     case DW_TAG_catch_block:
4614       return "DW_TAG_catch_block";
4615     case DW_TAG_const_type:
4616       return "DW_TAG_const_type";
4617     case DW_TAG_constant:
4618       return "DW_TAG_constant";
4619     case DW_TAG_enumerator:
4620       return "DW_TAG_enumerator";
4621     case DW_TAG_file_type:
4622       return "DW_TAG_file_type";
4623     case DW_TAG_friend:
4624       return "DW_TAG_friend";
4625     case DW_TAG_namelist:
4626       return "DW_TAG_namelist";
4627     case DW_TAG_namelist_item:
4628       return "DW_TAG_namelist_item";
4629     case DW_TAG_packed_type:
4630       return "DW_TAG_packed_type";
4631     case DW_TAG_subprogram:
4632       return "DW_TAG_subprogram";
4633     case DW_TAG_template_type_param:
4634       return "DW_TAG_template_type_param";
4635     case DW_TAG_template_value_param:
4636       return "DW_TAG_template_value_param";
4637     case DW_TAG_thrown_type:
4638       return "DW_TAG_thrown_type";
4639     case DW_TAG_try_block:
4640       return "DW_TAG_try_block";
4641     case DW_TAG_variant_part:
4642       return "DW_TAG_variant_part";
4643     case DW_TAG_variable:
4644       return "DW_TAG_variable";
4645     case DW_TAG_volatile_type:
4646       return "DW_TAG_volatile_type";
4647     case DW_TAG_dwarf_procedure:
4648       return "DW_TAG_dwarf_procedure";
4649     case DW_TAG_restrict_type:
4650       return "DW_TAG_restrict_type";
4651     case DW_TAG_interface_type:
4652       return "DW_TAG_interface_type";
4653     case DW_TAG_namespace:
4654       return "DW_TAG_namespace";
4655     case DW_TAG_imported_module:
4656       return "DW_TAG_imported_module";
4657     case DW_TAG_unspecified_type:
4658       return "DW_TAG_unspecified_type";
4659     case DW_TAG_partial_unit:
4660       return "DW_TAG_partial_unit";
4661     case DW_TAG_imported_unit:
4662       return "DW_TAG_imported_unit";
4663     case DW_TAG_condition:
4664       return "DW_TAG_condition";
4665     case DW_TAG_shared_type:
4666       return "DW_TAG_shared_type";
4667     case DW_TAG_MIPS_loop:
4668       return "DW_TAG_MIPS_loop";
4669     case DW_TAG_format_label:
4670       return "DW_TAG_format_label";
4671     case DW_TAG_function_template:
4672       return "DW_TAG_function_template";
4673     case DW_TAG_class_template:
4674       return "DW_TAG_class_template";
4675     case DW_TAG_GNU_BINCL:
4676       return "DW_TAG_GNU_BINCL";
4677     case DW_TAG_GNU_EINCL:
4678       return "DW_TAG_GNU_EINCL";
4679     default:
4680       return "DW_TAG_<unknown>";
4681     }
4682 }
4683
4684 /* Convert a DWARF attribute code into its string name.  */
4685
4686 static const char *
4687 dwarf_attr_name (unsigned int attr)
4688 {
4689   switch (attr)
4690     {
4691     case DW_AT_sibling:
4692       return "DW_AT_sibling";
4693     case DW_AT_location:
4694       return "DW_AT_location";
4695     case DW_AT_name:
4696       return "DW_AT_name";
4697     case DW_AT_ordering:
4698       return "DW_AT_ordering";
4699     case DW_AT_subscr_data:
4700       return "DW_AT_subscr_data";
4701     case DW_AT_byte_size:
4702       return "DW_AT_byte_size";
4703     case DW_AT_bit_offset:
4704       return "DW_AT_bit_offset";
4705     case DW_AT_bit_size:
4706       return "DW_AT_bit_size";
4707     case DW_AT_element_list:
4708       return "DW_AT_element_list";
4709     case DW_AT_stmt_list:
4710       return "DW_AT_stmt_list";
4711     case DW_AT_low_pc:
4712       return "DW_AT_low_pc";
4713     case DW_AT_high_pc:
4714       return "DW_AT_high_pc";
4715     case DW_AT_language:
4716       return "DW_AT_language";
4717     case DW_AT_member:
4718       return "DW_AT_member";
4719     case DW_AT_discr:
4720       return "DW_AT_discr";
4721     case DW_AT_discr_value:
4722       return "DW_AT_discr_value";
4723     case DW_AT_visibility:
4724       return "DW_AT_visibility";
4725     case DW_AT_import:
4726       return "DW_AT_import";
4727     case DW_AT_string_length:
4728       return "DW_AT_string_length";
4729     case DW_AT_common_reference:
4730       return "DW_AT_common_reference";
4731     case DW_AT_comp_dir:
4732       return "DW_AT_comp_dir";
4733     case DW_AT_const_value:
4734       return "DW_AT_const_value";
4735     case DW_AT_containing_type:
4736       return "DW_AT_containing_type";
4737     case DW_AT_default_value:
4738       return "DW_AT_default_value";
4739     case DW_AT_inline:
4740       return "DW_AT_inline";
4741     case DW_AT_is_optional:
4742       return "DW_AT_is_optional";
4743     case DW_AT_lower_bound:
4744       return "DW_AT_lower_bound";
4745     case DW_AT_producer:
4746       return "DW_AT_producer";
4747     case DW_AT_prototyped:
4748       return "DW_AT_prototyped";
4749     case DW_AT_return_addr:
4750       return "DW_AT_return_addr";
4751     case DW_AT_start_scope:
4752       return "DW_AT_start_scope";
4753     case DW_AT_bit_stride:
4754       return "DW_AT_bit_stride";
4755     case DW_AT_upper_bound:
4756       return "DW_AT_upper_bound";
4757     case DW_AT_abstract_origin:
4758       return "DW_AT_abstract_origin";
4759     case DW_AT_accessibility:
4760       return "DW_AT_accessibility";
4761     case DW_AT_address_class:
4762       return "DW_AT_address_class";
4763     case DW_AT_artificial:
4764       return "DW_AT_artificial";
4765     case DW_AT_base_types:
4766       return "DW_AT_base_types";
4767     case DW_AT_calling_convention:
4768       return "DW_AT_calling_convention";
4769     case DW_AT_count:
4770       return "DW_AT_count";
4771     case DW_AT_data_member_location:
4772       return "DW_AT_data_member_location";
4773     case DW_AT_decl_column:
4774       return "DW_AT_decl_column";
4775     case DW_AT_decl_file:
4776       return "DW_AT_decl_file";
4777     case DW_AT_decl_line:
4778       return "DW_AT_decl_line";
4779     case DW_AT_declaration:
4780       return "DW_AT_declaration";
4781     case DW_AT_discr_list:
4782       return "DW_AT_discr_list";
4783     case DW_AT_encoding:
4784       return "DW_AT_encoding";
4785     case DW_AT_external:
4786       return "DW_AT_external";
4787     case DW_AT_frame_base:
4788       return "DW_AT_frame_base";
4789     case DW_AT_friend:
4790       return "DW_AT_friend";
4791     case DW_AT_identifier_case:
4792       return "DW_AT_identifier_case";
4793     case DW_AT_macro_info:
4794       return "DW_AT_macro_info";
4795     case DW_AT_namelist_items:
4796       return "DW_AT_namelist_items";
4797     case DW_AT_priority:
4798       return "DW_AT_priority";
4799     case DW_AT_segment:
4800       return "DW_AT_segment";
4801     case DW_AT_specification:
4802       return "DW_AT_specification";
4803     case DW_AT_static_link:
4804       return "DW_AT_static_link";
4805     case DW_AT_type:
4806       return "DW_AT_type";
4807     case DW_AT_use_location:
4808       return "DW_AT_use_location";
4809     case DW_AT_variable_parameter:
4810       return "DW_AT_variable_parameter";
4811     case DW_AT_virtuality:
4812       return "DW_AT_virtuality";
4813     case DW_AT_vtable_elem_location:
4814       return "DW_AT_vtable_elem_location";
4815
4816     case DW_AT_allocated:
4817       return "DW_AT_allocated";
4818     case DW_AT_associated:
4819       return "DW_AT_associated";
4820     case DW_AT_data_location:
4821       return "DW_AT_data_location";
4822     case DW_AT_byte_stride:
4823       return "DW_AT_byte_stride";
4824     case DW_AT_entry_pc:
4825       return "DW_AT_entry_pc";
4826     case DW_AT_use_UTF8:
4827       return "DW_AT_use_UTF8";
4828     case DW_AT_extension:
4829       return "DW_AT_extension";
4830     case DW_AT_ranges:
4831       return "DW_AT_ranges";
4832     case DW_AT_trampoline:
4833       return "DW_AT_trampoline";
4834     case DW_AT_call_column:
4835       return "DW_AT_call_column";
4836     case DW_AT_call_file:
4837       return "DW_AT_call_file";
4838     case DW_AT_call_line:
4839       return "DW_AT_call_line";
4840
4841     case DW_AT_MIPS_fde:
4842       return "DW_AT_MIPS_fde";
4843     case DW_AT_MIPS_loop_begin:
4844       return "DW_AT_MIPS_loop_begin";
4845     case DW_AT_MIPS_tail_loop_begin:
4846       return "DW_AT_MIPS_tail_loop_begin";
4847     case DW_AT_MIPS_epilog_begin:
4848       return "DW_AT_MIPS_epilog_begin";
4849     case DW_AT_MIPS_loop_unroll_factor:
4850       return "DW_AT_MIPS_loop_unroll_factor";
4851     case DW_AT_MIPS_software_pipeline_depth:
4852       return "DW_AT_MIPS_software_pipeline_depth";
4853     case DW_AT_MIPS_linkage_name:
4854       return "DW_AT_MIPS_linkage_name";
4855     case DW_AT_MIPS_stride:
4856       return "DW_AT_MIPS_stride";
4857     case DW_AT_MIPS_abstract_name:
4858       return "DW_AT_MIPS_abstract_name";
4859     case DW_AT_MIPS_clone_origin:
4860       return "DW_AT_MIPS_clone_origin";
4861     case DW_AT_MIPS_has_inlines:
4862       return "DW_AT_MIPS_has_inlines";
4863
4864     case DW_AT_sf_names:
4865       return "DW_AT_sf_names";
4866     case DW_AT_src_info:
4867       return "DW_AT_src_info";
4868     case DW_AT_mac_info:
4869       return "DW_AT_mac_info";
4870     case DW_AT_src_coords:
4871       return "DW_AT_src_coords";
4872     case DW_AT_body_begin:
4873       return "DW_AT_body_begin";
4874     case DW_AT_body_end:
4875       return "DW_AT_body_end";
4876     case DW_AT_GNU_vector:
4877       return "DW_AT_GNU_vector";
4878
4879     case DW_AT_VMS_rtnbeg_pd_address:
4880       return "DW_AT_VMS_rtnbeg_pd_address";
4881
4882     default:
4883       return "DW_AT_<unknown>";
4884     }
4885 }
4886
4887 /* Convert a DWARF value form code into its string name.  */
4888
4889 static const char *
4890 dwarf_form_name (unsigned int form)
4891 {
4892   switch (form)
4893     {
4894     case DW_FORM_addr:
4895       return "DW_FORM_addr";
4896     case DW_FORM_block2:
4897       return "DW_FORM_block2";
4898     case DW_FORM_block4:
4899       return "DW_FORM_block4";
4900     case DW_FORM_data2:
4901       return "DW_FORM_data2";
4902     case DW_FORM_data4:
4903       return "DW_FORM_data4";
4904     case DW_FORM_data8:
4905       return "DW_FORM_data8";
4906     case DW_FORM_string:
4907       return "DW_FORM_string";
4908     case DW_FORM_block:
4909       return "DW_FORM_block";
4910     case DW_FORM_block1:
4911       return "DW_FORM_block1";
4912     case DW_FORM_data1:
4913       return "DW_FORM_data1";
4914     case DW_FORM_flag:
4915       return "DW_FORM_flag";
4916     case DW_FORM_sdata:
4917       return "DW_FORM_sdata";
4918     case DW_FORM_strp:
4919       return "DW_FORM_strp";
4920     case DW_FORM_udata:
4921       return "DW_FORM_udata";
4922     case DW_FORM_ref_addr:
4923       return "DW_FORM_ref_addr";
4924     case DW_FORM_ref1:
4925       return "DW_FORM_ref1";
4926     case DW_FORM_ref2:
4927       return "DW_FORM_ref2";
4928     case DW_FORM_ref4:
4929       return "DW_FORM_ref4";
4930     case DW_FORM_ref8:
4931       return "DW_FORM_ref8";
4932     case DW_FORM_ref_udata:
4933       return "DW_FORM_ref_udata";
4934     case DW_FORM_indirect:
4935       return "DW_FORM_indirect";
4936     default:
4937       return "DW_FORM_<unknown>";
4938     }
4939 }
4940 \f
4941 /* Determine the "ultimate origin" of a decl.  The decl may be an inlined
4942    instance of an inlined instance of a decl which is local to an inline
4943    function, so we have to trace all of the way back through the origin chain
4944    to find out what sort of node actually served as the original seed for the
4945    given block.  */
4946
4947 static tree
4948 decl_ultimate_origin (const_tree decl)
4949 {
4950   if (!CODE_CONTAINS_STRUCT (TREE_CODE (decl), TS_DECL_COMMON))
4951     return NULL_TREE;
4952
4953   /* output_inline_function sets DECL_ABSTRACT_ORIGIN for all the
4954      nodes in the function to point to themselves; ignore that if
4955      we're trying to output the abstract instance of this function.  */
4956   if (DECL_ABSTRACT (decl) && DECL_ABSTRACT_ORIGIN (decl) == decl)
4957     return NULL_TREE;
4958
4959   /* Since the DECL_ABSTRACT_ORIGIN for a DECL is supposed to be the
4960      most distant ancestor, this should never happen.  */
4961   gcc_assert (!DECL_FROM_INLINE (DECL_ORIGIN (decl)));
4962
4963   return DECL_ABSTRACT_ORIGIN (decl);
4964 }
4965
4966 /* Determine the "ultimate origin" of a block.  The block may be an inlined
4967    instance of an inlined instance of a block which is local to an inline
4968    function, so we have to trace all of the way back through the origin chain
4969    to find out what sort of node actually served as the original seed for the
4970    given block.  */
4971
4972 static tree
4973 block_ultimate_origin (const_tree block)
4974 {
4975   tree immediate_origin = BLOCK_ABSTRACT_ORIGIN (block);
4976
4977   /* output_inline_function sets BLOCK_ABSTRACT_ORIGIN for all the
4978      nodes in the function to point to themselves; ignore that if
4979      we're trying to output the abstract instance of this function.  */
4980   if (BLOCK_ABSTRACT (block) && immediate_origin == block)
4981     return NULL_TREE;
4982
4983   if (immediate_origin == NULL_TREE)
4984     return NULL_TREE;
4985   else
4986     {
4987       tree ret_val;
4988       tree lookahead = immediate_origin;
4989
4990       do
4991         {
4992           ret_val = lookahead;
4993           lookahead = (TREE_CODE (ret_val) == BLOCK
4994                        ? BLOCK_ABSTRACT_ORIGIN (ret_val) : NULL);
4995         }
4996       while (lookahead != NULL && lookahead != ret_val);
4997
4998       /* The block's abstract origin chain may not be the *ultimate* origin of
4999          the block. It could lead to a DECL that has an abstract origin set.
5000          If so, we want that DECL's abstract origin (which is what DECL_ORIGIN
5001          will give us if it has one).  Note that DECL's abstract origins are
5002          supposed to be the most distant ancestor (or so decl_ultimate_origin
5003          claims), so we don't need to loop following the DECL origins.  */
5004       if (DECL_P (ret_val))
5005         return DECL_ORIGIN (ret_val);
5006
5007       return ret_val;
5008     }
5009 }
5010
5011 /* Get the class to which DECL belongs, if any.  In g++, the DECL_CONTEXT
5012    of a virtual function may refer to a base class, so we check the 'this'
5013    parameter.  */
5014
5015 static tree
5016 decl_class_context (tree decl)
5017 {
5018   tree context = NULL_TREE;
5019
5020   if (TREE_CODE (decl) != FUNCTION_DECL || ! DECL_VINDEX (decl))
5021     context = DECL_CONTEXT (decl);
5022   else
5023     context = TYPE_MAIN_VARIANT
5024       (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
5025
5026   if (context && !TYPE_P (context))
5027     context = NULL_TREE;
5028
5029   return context;
5030 }
5031 \f
5032 /* Add an attribute/value pair to a DIE.  */
5033
5034 static inline void
5035 add_dwarf_attr (dw_die_ref die, dw_attr_ref attr)
5036 {
5037   /* Maybe this should be an assert?  */
5038   if (die == NULL)
5039     return;
5040
5041   if (die->die_attr == NULL)
5042     die->die_attr = VEC_alloc (dw_attr_node, gc, 1);
5043   VEC_safe_push (dw_attr_node, gc, die->die_attr, attr);
5044 }
5045
5046 static inline enum dw_val_class
5047 AT_class (dw_attr_ref a)
5048 {
5049   return a->dw_attr_val.val_class;
5050 }
5051
5052 /* Add a flag value attribute to a DIE.  */
5053
5054 static inline void
5055 add_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int flag)
5056 {
5057   dw_attr_node attr;
5058
5059   attr.dw_attr = attr_kind;
5060   attr.dw_attr_val.val_class = dw_val_class_flag;
5061   attr.dw_attr_val.v.val_flag = flag;
5062   add_dwarf_attr (die, &attr);
5063 }
5064
5065 static inline unsigned
5066 AT_flag (dw_attr_ref a)
5067 {
5068   gcc_assert (a && AT_class (a) == dw_val_class_flag);
5069   return a->dw_attr_val.v.val_flag;
5070 }
5071
5072 /* Add a signed integer attribute value to a DIE.  */
5073
5074 static inline void
5075 add_AT_int (dw_die_ref die, enum dwarf_attribute attr_kind, HOST_WIDE_INT int_val)
5076 {
5077   dw_attr_node attr;
5078
5079   attr.dw_attr = attr_kind;
5080   attr.dw_attr_val.val_class = dw_val_class_const;
5081   attr.dw_attr_val.v.val_int = int_val;
5082   add_dwarf_attr (die, &attr);
5083 }
5084
5085 static inline HOST_WIDE_INT
5086 AT_int (dw_attr_ref a)
5087 {
5088   gcc_assert (a && AT_class (a) == dw_val_class_const);
5089   return a->dw_attr_val.v.val_int;
5090 }
5091
5092 /* Add an unsigned integer attribute value to a DIE.  */
5093
5094 static inline void
5095 add_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind,
5096                  unsigned HOST_WIDE_INT unsigned_val)
5097 {
5098   dw_attr_node attr;
5099
5100   attr.dw_attr = attr_kind;
5101   attr.dw_attr_val.val_class = dw_val_class_unsigned_const;
5102   attr.dw_attr_val.v.val_unsigned = unsigned_val;
5103   add_dwarf_attr (die, &attr);
5104 }
5105
5106 static inline unsigned HOST_WIDE_INT
5107 AT_unsigned (dw_attr_ref a)
5108 {
5109   gcc_assert (a && AT_class (a) == dw_val_class_unsigned_const);
5110   return a->dw_attr_val.v.val_unsigned;
5111 }
5112
5113 /* Add an unsigned double integer attribute value to a DIE.  */
5114
5115 static inline void
5116 add_AT_long_long (dw_die_ref die, enum dwarf_attribute attr_kind,
5117                   long unsigned int val_hi, long unsigned int val_low)
5118 {
5119   dw_attr_node attr;
5120
5121   attr.dw_attr = attr_kind;
5122   attr.dw_attr_val.val_class = dw_val_class_long_long;
5123   attr.dw_attr_val.v.val_long_long.hi = val_hi;
5124   attr.dw_attr_val.v.val_long_long.low = val_low;
5125   add_dwarf_attr (die, &attr);
5126 }
5127
5128 /* Add a floating point attribute value to a DIE and return it.  */
5129
5130 static inline void
5131 add_AT_vec (dw_die_ref die, enum dwarf_attribute attr_kind,
5132             unsigned int length, unsigned int elt_size, unsigned char *array)
5133 {
5134   dw_attr_node attr;
5135
5136   attr.dw_attr = attr_kind;
5137   attr.dw_attr_val.val_class = dw_val_class_vec;
5138   attr.dw_attr_val.v.val_vec.length = length;
5139   attr.dw_attr_val.v.val_vec.elt_size = elt_size;
5140   attr.dw_attr_val.v.val_vec.array = array;
5141   add_dwarf_attr (die, &attr);
5142 }
5143
5144 /* Hash and equality functions for debug_str_hash.  */
5145
5146 static hashval_t
5147 debug_str_do_hash (const void *x)
5148 {
5149   return htab_hash_string (((const struct indirect_string_node *)x)->str);
5150 }
5151
5152 static int
5153 debug_str_eq (const void *x1, const void *x2)
5154 {
5155   return strcmp ((((const struct indirect_string_node *)x1)->str),
5156                  (const char *)x2) == 0;
5157 }
5158
5159 /* Add a string attribute value to a DIE.  */
5160
5161 static inline void
5162 add_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind, const char *str)
5163 {
5164   dw_attr_node attr;
5165   struct indirect_string_node *node;
5166   void **slot;
5167
5168   if (! debug_str_hash)
5169     debug_str_hash = htab_create_ggc (10, debug_str_do_hash,
5170                                       debug_str_eq, NULL);
5171
5172   slot = htab_find_slot_with_hash (debug_str_hash, str,
5173                                    htab_hash_string (str), INSERT);
5174   if (*slot == NULL)
5175     {
5176       node = (struct indirect_string_node *)
5177                ggc_alloc_cleared (sizeof (struct indirect_string_node));
5178       node->str = ggc_strdup (str);
5179       *slot = node;
5180     }
5181   else
5182     node = (struct indirect_string_node *) *slot;
5183
5184   node->refcount++;
5185
5186   attr.dw_attr = attr_kind;
5187   attr.dw_attr_val.val_class = dw_val_class_str;
5188   attr.dw_attr_val.v.val_str = node;
5189   add_dwarf_attr (die, &attr);
5190 }
5191
5192 static inline const char *
5193 AT_string (dw_attr_ref a)
5194 {
5195   gcc_assert (a && AT_class (a) == dw_val_class_str);
5196   return a->dw_attr_val.v.val_str->str;
5197 }
5198
5199 /* Find out whether a string should be output inline in DIE
5200    or out-of-line in .debug_str section.  */
5201
5202 static int
5203 AT_string_form (dw_attr_ref a)
5204 {
5205   struct indirect_string_node *node;
5206   unsigned int len;
5207   char label[32];
5208
5209   gcc_assert (a && AT_class (a) == dw_val_class_str);
5210
5211   node = a->dw_attr_val.v.val_str;
5212   if (node->form)
5213     return node->form;
5214
5215   len = strlen (node->str) + 1;
5216
5217   /* If the string is shorter or equal to the size of the reference, it is
5218      always better to put it inline.  */
5219   if (len <= DWARF_OFFSET_SIZE || node->refcount == 0)
5220     return node->form = DW_FORM_string;
5221
5222   /* If we cannot expect the linker to merge strings in .debug_str
5223      section, only put it into .debug_str if it is worth even in this
5224      single module.  */
5225   if ((debug_str_section->common.flags & SECTION_MERGE) == 0
5226       && (len - DWARF_OFFSET_SIZE) * node->refcount <= len)
5227     return node->form = DW_FORM_string;
5228
5229   ASM_GENERATE_INTERNAL_LABEL (label, "LASF", dw2_string_counter);
5230   ++dw2_string_counter;
5231   node->label = xstrdup (label);
5232
5233   return node->form = DW_FORM_strp;
5234 }
5235
5236 /* Add a DIE reference attribute value to a DIE.  */
5237
5238 static inline void
5239 add_AT_die_ref (dw_die_ref die, enum dwarf_attribute attr_kind, dw_die_ref targ_die)
5240 {
5241   dw_attr_node attr;
5242
5243   attr.dw_attr = attr_kind;
5244   attr.dw_attr_val.val_class = dw_val_class_die_ref;
5245   attr.dw_attr_val.v.val_die_ref.die = targ_die;
5246   attr.dw_attr_val.v.val_die_ref.external = 0;
5247   add_dwarf_attr (die, &attr);
5248 }
5249
5250 /* Add an AT_specification attribute to a DIE, and also make the back
5251    pointer from the specification to the definition.  */
5252
5253 static inline void
5254 add_AT_specification (dw_die_ref die, dw_die_ref targ_die)
5255 {
5256   add_AT_die_ref (die, DW_AT_specification, targ_die);
5257   gcc_assert (!targ_die->die_definition);
5258   targ_die->die_definition = die;
5259 }
5260
5261 static inline dw_die_ref
5262 AT_ref (dw_attr_ref a)
5263 {
5264   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5265   return a->dw_attr_val.v.val_die_ref.die;
5266 }
5267
5268 static inline int
5269 AT_ref_external (dw_attr_ref a)
5270 {
5271   if (a && AT_class (a) == dw_val_class_die_ref)
5272     return a->dw_attr_val.v.val_die_ref.external;
5273
5274   return 0;
5275 }
5276
5277 static inline void
5278 set_AT_ref_external (dw_attr_ref a, int i)
5279 {
5280   gcc_assert (a && AT_class (a) == dw_val_class_die_ref);
5281   a->dw_attr_val.v.val_die_ref.external = i;
5282 }
5283
5284 /* Add an FDE reference attribute value to a DIE.  */
5285
5286 static inline void
5287 add_AT_fde_ref (dw_die_ref die, enum dwarf_attribute attr_kind, unsigned int targ_fde)
5288 {
5289   dw_attr_node attr;
5290
5291   attr.dw_attr = attr_kind;
5292   attr.dw_attr_val.val_class = dw_val_class_fde_ref;
5293   attr.dw_attr_val.v.val_fde_index = targ_fde;
5294   add_dwarf_attr (die, &attr);
5295 }
5296
5297 /* Add a location description attribute value to a DIE.  */
5298
5299 static inline void
5300 add_AT_loc (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_descr_ref loc)
5301 {
5302   dw_attr_node attr;
5303
5304   attr.dw_attr = attr_kind;
5305   attr.dw_attr_val.val_class = dw_val_class_loc;
5306   attr.dw_attr_val.v.val_loc = loc;
5307   add_dwarf_attr (die, &attr);
5308 }
5309
5310 static inline dw_loc_descr_ref
5311 AT_loc (dw_attr_ref a)
5312 {
5313   gcc_assert (a && AT_class (a) == dw_val_class_loc);
5314   return a->dw_attr_val.v.val_loc;
5315 }
5316
5317 static inline void
5318 add_AT_loc_list (dw_die_ref die, enum dwarf_attribute attr_kind, dw_loc_list_ref loc_list)
5319 {
5320   dw_attr_node attr;
5321
5322   attr.dw_attr = attr_kind;
5323   attr.dw_attr_val.val_class = dw_val_class_loc_list;
5324   attr.dw_attr_val.v.val_loc_list = loc_list;
5325   add_dwarf_attr (die, &attr);
5326   have_location_lists = true;
5327 }
5328
5329 static inline dw_loc_list_ref
5330 AT_loc_list (dw_attr_ref a)
5331 {
5332   gcc_assert (a && AT_class (a) == dw_val_class_loc_list);
5333   return a->dw_attr_val.v.val_loc_list;
5334 }
5335
5336 /* Add an address constant attribute value to a DIE.  */
5337
5338 static inline void
5339 add_AT_addr (dw_die_ref die, enum dwarf_attribute attr_kind, rtx addr)
5340 {
5341   dw_attr_node attr;
5342
5343   attr.dw_attr = attr_kind;
5344   attr.dw_attr_val.val_class = dw_val_class_addr;
5345   attr.dw_attr_val.v.val_addr = addr;
5346   add_dwarf_attr (die, &attr);
5347 }
5348
5349 /* Get the RTX from to an address DIE attribute.  */
5350
5351 static inline rtx
5352 AT_addr (dw_attr_ref a)
5353 {
5354   gcc_assert (a && AT_class (a) == dw_val_class_addr);
5355   return a->dw_attr_val.v.val_addr;
5356 }
5357
5358 /* Add a file attribute value to a DIE.  */
5359
5360 static inline void
5361 add_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind,
5362              struct dwarf_file_data *fd)
5363 {
5364   dw_attr_node attr;
5365
5366   attr.dw_attr = attr_kind;
5367   attr.dw_attr_val.val_class = dw_val_class_file;
5368   attr.dw_attr_val.v.val_file = fd;
5369   add_dwarf_attr (die, &attr);
5370 }
5371
5372 /* Get the dwarf_file_data from a file DIE attribute.  */
5373
5374 static inline struct dwarf_file_data *
5375 AT_file (dw_attr_ref a)
5376 {
5377   gcc_assert (a && AT_class (a) == dw_val_class_file);
5378   return a->dw_attr_val.v.val_file;
5379 }
5380
5381 /* Add a label identifier attribute value to a DIE.  */
5382
5383 static inline void
5384 add_AT_lbl_id (dw_die_ref die, enum dwarf_attribute attr_kind, const char *lbl_id)
5385 {
5386   dw_attr_node attr;
5387
5388   attr.dw_attr = attr_kind;
5389   attr.dw_attr_val.val_class = dw_val_class_lbl_id;
5390   attr.dw_attr_val.v.val_lbl_id = xstrdup (lbl_id);
5391   add_dwarf_attr (die, &attr);
5392 }
5393
5394 /* Add a section offset attribute value to a DIE, an offset into the
5395    debug_line section.  */
5396
5397 static inline void
5398 add_AT_lineptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5399                 const char *label)
5400 {
5401   dw_attr_node attr;
5402
5403   attr.dw_attr = attr_kind;
5404   attr.dw_attr_val.val_class = dw_val_class_lineptr;
5405   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5406   add_dwarf_attr (die, &attr);
5407 }
5408
5409 /* Add a section offset attribute value to a DIE, an offset into the
5410    debug_macinfo section.  */
5411
5412 static inline void
5413 add_AT_macptr (dw_die_ref die, enum dwarf_attribute attr_kind,
5414                const char *label)
5415 {
5416   dw_attr_node attr;
5417
5418   attr.dw_attr = attr_kind;
5419   attr.dw_attr_val.val_class = dw_val_class_macptr;
5420   attr.dw_attr_val.v.val_lbl_id = xstrdup (label);
5421   add_dwarf_attr (die, &attr);
5422 }
5423
5424 /* Add an offset attribute value to a DIE.  */
5425
5426 static inline void
5427 add_AT_offset (dw_die_ref die, enum dwarf_attribute attr_kind,
5428                unsigned HOST_WIDE_INT offset)
5429 {
5430   dw_attr_node attr;
5431
5432   attr.dw_attr = attr_kind;
5433   attr.dw_attr_val.val_class = dw_val_class_offset;
5434   attr.dw_attr_val.v.val_offset = offset;
5435   add_dwarf_attr (die, &attr);
5436 }
5437
5438 /* Add an range_list attribute value to a DIE.  */
5439
5440 static void
5441 add_AT_range_list (dw_die_ref die, enum dwarf_attribute attr_kind,
5442                    long unsigned int offset)
5443 {
5444   dw_attr_node attr;
5445
5446   attr.dw_attr = attr_kind;
5447   attr.dw_attr_val.val_class = dw_val_class_range_list;
5448   attr.dw_attr_val.v.val_offset = offset;
5449   add_dwarf_attr (die, &attr);
5450 }
5451
5452 static inline const char *
5453 AT_lbl (dw_attr_ref a)
5454 {
5455   gcc_assert (a && (AT_class (a) == dw_val_class_lbl_id
5456                     || AT_class (a) == dw_val_class_lineptr
5457                     || AT_class (a) == dw_val_class_macptr));
5458   return a->dw_attr_val.v.val_lbl_id;
5459 }
5460
5461 /* Get the attribute of type attr_kind.  */
5462
5463 static dw_attr_ref
5464 get_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5465 {
5466   dw_attr_ref a;
5467   unsigned ix;
5468   dw_die_ref spec = NULL;
5469
5470   if (! die)
5471     return NULL;
5472
5473   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5474     if (a->dw_attr == attr_kind)
5475       return a;
5476     else if (a->dw_attr == DW_AT_specification
5477              || a->dw_attr == DW_AT_abstract_origin)
5478       spec = AT_ref (a);
5479
5480   if (spec)
5481     return get_AT (spec, attr_kind);
5482
5483   return NULL;
5484 }
5485
5486 /* Return the "low pc" attribute value, typically associated with a subprogram
5487    DIE.  Return null if the "low pc" attribute is either not present, or if it
5488    cannot be represented as an assembler label identifier.  */
5489
5490 static inline const char *
5491 get_AT_low_pc (dw_die_ref die)
5492 {
5493   dw_attr_ref a = get_AT (die, DW_AT_low_pc);
5494
5495   return a ? AT_lbl (a) : NULL;
5496 }
5497
5498 /* Return the "high pc" attribute value, typically associated with a subprogram
5499    DIE.  Return null if the "high pc" attribute is either not present, or if it
5500    cannot be represented as an assembler label identifier.  */
5501
5502 static inline const char *
5503 get_AT_hi_pc (dw_die_ref die)
5504 {
5505   dw_attr_ref a = get_AT (die, DW_AT_high_pc);
5506
5507   return a ? AT_lbl (a) : NULL;
5508 }
5509
5510 /* Return the value of the string attribute designated by ATTR_KIND, or
5511    NULL if it is not present.  */
5512
5513 static inline const char *
5514 get_AT_string (dw_die_ref die, enum dwarf_attribute attr_kind)
5515 {
5516   dw_attr_ref a = get_AT (die, attr_kind);
5517
5518   return a ? AT_string (a) : NULL;
5519 }
5520
5521 /* Return the value of the flag attribute designated by ATTR_KIND, or -1
5522    if it is not present.  */
5523
5524 static inline int
5525 get_AT_flag (dw_die_ref die, enum dwarf_attribute attr_kind)
5526 {
5527   dw_attr_ref a = get_AT (die, attr_kind);
5528
5529   return a ? AT_flag (a) : 0;
5530 }
5531
5532 /* Return the value of the unsigned attribute designated by ATTR_KIND, or 0
5533    if it is not present.  */
5534
5535 static inline unsigned
5536 get_AT_unsigned (dw_die_ref die, enum dwarf_attribute attr_kind)
5537 {
5538   dw_attr_ref a = get_AT (die, attr_kind);
5539
5540   return a ? AT_unsigned (a) : 0;
5541 }
5542
5543 static inline dw_die_ref
5544 get_AT_ref (dw_die_ref die, enum dwarf_attribute attr_kind)
5545 {
5546   dw_attr_ref a = get_AT (die, attr_kind);
5547
5548   return a ? AT_ref (a) : NULL;
5549 }
5550
5551 static inline struct dwarf_file_data *
5552 get_AT_file (dw_die_ref die, enum dwarf_attribute attr_kind)
5553 {
5554   dw_attr_ref a = get_AT (die, attr_kind);
5555
5556   return a ? AT_file (a) : NULL;
5557 }
5558
5559 /* Return TRUE if the language is C or C++.  */
5560
5561 static inline bool
5562 is_c_family (void)
5563 {
5564   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5565
5566   return (lang == DW_LANG_C || lang == DW_LANG_C89 || lang == DW_LANG_ObjC
5567           || lang == DW_LANG_C99
5568           || lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus);
5569 }
5570
5571 /* Return TRUE if the language is C++.  */
5572
5573 static inline bool
5574 is_cxx (void)
5575 {
5576   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5577
5578   return lang == DW_LANG_C_plus_plus || lang == DW_LANG_ObjC_plus_plus;
5579 }
5580
5581 /* Return TRUE if the language is Fortran.  */
5582
5583 static inline bool
5584 is_fortran (void)
5585 {
5586   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5587
5588   return (lang == DW_LANG_Fortran77
5589           || lang == DW_LANG_Fortran90
5590           || lang == DW_LANG_Fortran95);
5591 }
5592
5593 /* Return TRUE if the language is Java.  */
5594
5595 static inline bool
5596 is_java (void)
5597 {
5598   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5599
5600   return lang == DW_LANG_Java;
5601 }
5602
5603 /* Return TRUE if the language is Ada.  */
5604
5605 static inline bool
5606 is_ada (void)
5607 {
5608   unsigned int lang = get_AT_unsigned (comp_unit_die, DW_AT_language);
5609
5610   return lang == DW_LANG_Ada95 || lang == DW_LANG_Ada83;
5611 }
5612
5613 /* Remove the specified attribute if present.  */
5614
5615 static void
5616 remove_AT (dw_die_ref die, enum dwarf_attribute attr_kind)
5617 {
5618   dw_attr_ref a;
5619   unsigned ix;
5620
5621   if (! die)
5622     return;
5623
5624   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5625     if (a->dw_attr == attr_kind)
5626       {
5627         if (AT_class (a) == dw_val_class_str)
5628           if (a->dw_attr_val.v.val_str->refcount)
5629             a->dw_attr_val.v.val_str->refcount--;
5630
5631         /* VEC_ordered_remove should help reduce the number of abbrevs
5632            that are needed.  */
5633         VEC_ordered_remove (dw_attr_node, die->die_attr, ix);
5634         return;
5635       }
5636 }
5637
5638 /* Remove CHILD from its parent.  PREV must have the property that
5639    PREV->DIE_SIB == CHILD.  Does not alter CHILD.  */
5640
5641 static void
5642 remove_child_with_prev (dw_die_ref child, dw_die_ref prev)
5643 {
5644   gcc_assert (child->die_parent == prev->die_parent);
5645   gcc_assert (prev->die_sib == child);
5646   if (prev == child)
5647     {
5648       gcc_assert (child->die_parent->die_child == child);
5649       prev = NULL;
5650     }
5651   else
5652     prev->die_sib = child->die_sib;
5653   if (child->die_parent->die_child == child)
5654     child->die_parent->die_child = prev;
5655 }
5656
5657 /* Remove child DIE whose die_tag is TAG.  Do nothing if no child
5658    matches TAG.  */
5659
5660 static void
5661 remove_child_TAG (dw_die_ref die, enum dwarf_tag tag)
5662 {
5663   dw_die_ref c;
5664
5665   c = die->die_child;
5666   if (c) do {
5667     dw_die_ref prev = c;
5668     c = c->die_sib;
5669     while (c->die_tag == tag)
5670       {
5671         remove_child_with_prev (c, prev);
5672         /* Might have removed every child.  */
5673         if (c == c->die_sib)
5674           return;
5675         c = c->die_sib;
5676       }
5677   } while (c != die->die_child);
5678 }
5679
5680 /* Add a CHILD_DIE as the last child of DIE.  */
5681
5682 static void
5683 add_child_die (dw_die_ref die, dw_die_ref child_die)
5684 {
5685   /* FIXME this should probably be an assert.  */
5686   if (! die || ! child_die)
5687     return;
5688   gcc_assert (die != child_die);
5689
5690   child_die->die_parent = die;
5691   if (die->die_child)
5692     {
5693       child_die->die_sib = die->die_child->die_sib;
5694       die->die_child->die_sib = child_die;
5695     }
5696   else
5697     child_die->die_sib = child_die;
5698   die->die_child = child_die;
5699 }
5700
5701 /* Move CHILD, which must be a child of PARENT or the DIE for which PARENT
5702    is the specification, to the end of PARENT's list of children.
5703    This is done by removing and re-adding it.  */
5704
5705 static void
5706 splice_child_die (dw_die_ref parent, dw_die_ref child)
5707 {
5708   dw_die_ref p;
5709
5710   /* We want the declaration DIE from inside the class, not the
5711      specification DIE at toplevel.  */
5712   if (child->die_parent != parent)
5713     {
5714       dw_die_ref tmp = get_AT_ref (child, DW_AT_specification);
5715
5716       if (tmp)
5717         child = tmp;
5718     }
5719
5720   gcc_assert (child->die_parent == parent
5721               || (child->die_parent
5722                   == get_AT_ref (parent, DW_AT_specification)));
5723
5724   for (p = child->die_parent->die_child; ; p = p->die_sib)
5725     if (p->die_sib == child)
5726       {
5727         remove_child_with_prev (child, p);
5728         break;
5729       }
5730
5731   add_child_die (parent, child);
5732 }
5733
5734 /* Return a pointer to a newly created DIE node.  */
5735
5736 static inline dw_die_ref
5737 new_die (enum dwarf_tag tag_value, dw_die_ref parent_die, tree t)
5738 {
5739   dw_die_ref die = ggc_alloc_cleared (sizeof (die_node));
5740
5741   die->die_tag = tag_value;
5742
5743   if (parent_die != NULL)
5744     add_child_die (parent_die, die);
5745   else
5746     {
5747       limbo_die_node *limbo_node;
5748
5749       limbo_node = ggc_alloc_cleared (sizeof (limbo_die_node));
5750       limbo_node->die = die;
5751       limbo_node->created_for = t;
5752       limbo_node->next = limbo_die_list;
5753       limbo_die_list = limbo_node;
5754     }
5755
5756   return die;
5757 }
5758
5759 /* Return the DIE associated with the given type specifier.  */
5760
5761 static inline dw_die_ref
5762 lookup_type_die (tree type)
5763 {
5764   return TYPE_SYMTAB_DIE (type);
5765 }
5766
5767 /* Equate a DIE to a given type specifier.  */
5768
5769 static inline void
5770 equate_type_number_to_die (tree type, dw_die_ref type_die)
5771 {
5772   TYPE_SYMTAB_DIE (type) = type_die;
5773 }
5774
5775 /* Returns a hash value for X (which really is a die_struct).  */
5776
5777 static hashval_t
5778 decl_die_table_hash (const void *x)
5779 {
5780   return (hashval_t) ((const_dw_die_ref) x)->decl_id;
5781 }
5782
5783 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y.  */
5784
5785 static int
5786 decl_die_table_eq (const void *x, const void *y)
5787 {
5788   return (((const_dw_die_ref) x)->decl_id == DECL_UID ((const_tree) y));
5789 }
5790
5791 /* Return the DIE associated with a given declaration.  */
5792
5793 static inline dw_die_ref
5794 lookup_decl_die (tree decl)
5795 {
5796   return htab_find_with_hash (decl_die_table, decl, DECL_UID (decl));
5797 }
5798
5799 /* Returns a hash value for X (which really is a var_loc_list).  */
5800
5801 static hashval_t
5802 decl_loc_table_hash (const void *x)
5803 {
5804   return (hashval_t) ((const var_loc_list *) x)->decl_id;
5805 }
5806
5807 /* Return nonzero if decl_id of var_loc_list X is the same as
5808    UID of decl *Y.  */
5809
5810 static int
5811 decl_loc_table_eq (const void *x, const void *y)
5812 {
5813   return (((const var_loc_list *) x)->decl_id == DECL_UID ((const_tree) y));
5814 }
5815
5816 /* Return the var_loc list associated with a given declaration.  */
5817
5818 static inline var_loc_list *
5819 lookup_decl_loc (const_tree decl)
5820 {
5821   return htab_find_with_hash (decl_loc_table, decl, DECL_UID (decl));
5822 }
5823
5824 /* Equate a DIE to a particular declaration.  */
5825
5826 static void
5827 equate_decl_number_to_die (tree decl, dw_die_ref decl_die)
5828 {
5829   unsigned int decl_id = DECL_UID (decl);
5830   void **slot;
5831
5832   slot = htab_find_slot_with_hash (decl_die_table, decl, decl_id, INSERT);
5833   *slot = decl_die;
5834   decl_die->decl_id = decl_id;
5835 }
5836
5837 /* Add a variable location node to the linked list for DECL.  */
5838
5839 static void
5840 add_var_loc_to_decl (tree decl, struct var_loc_node *loc)
5841 {
5842   unsigned int decl_id = DECL_UID (decl);
5843   var_loc_list *temp;
5844   void **slot;
5845
5846   slot = htab_find_slot_with_hash (decl_loc_table, decl, decl_id, INSERT);
5847   if (*slot == NULL)
5848     {
5849       temp = ggc_alloc_cleared (sizeof (var_loc_list));
5850       temp->decl_id = decl_id;
5851       *slot = temp;
5852     }
5853   else
5854     temp = *slot;
5855
5856   if (temp->last)
5857     {
5858       /* If the current location is the same as the end of the list,
5859          and either both or neither of the locations is uninitialized,
5860          we have nothing to do.  */
5861       if ((!rtx_equal_p (NOTE_VAR_LOCATION_LOC (temp->last->var_loc_note),
5862                          NOTE_VAR_LOCATION_LOC (loc->var_loc_note)))
5863           || ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5864                != NOTE_VAR_LOCATION_STATUS (loc->var_loc_note))
5865               && ((NOTE_VAR_LOCATION_STATUS (temp->last->var_loc_note)
5866                    == VAR_INIT_STATUS_UNINITIALIZED)
5867                   || (NOTE_VAR_LOCATION_STATUS (loc->var_loc_note)
5868                       == VAR_INIT_STATUS_UNINITIALIZED))))
5869         {
5870           /* Add LOC to the end of list and update LAST.  */
5871           temp->last->next = loc;
5872           temp->last = loc;
5873         }
5874     }
5875   /* Do not add empty location to the beginning of the list.  */
5876   else if (NOTE_VAR_LOCATION_LOC (loc->var_loc_note) != NULL_RTX)
5877     {
5878       temp->first = loc;
5879       temp->last = loc;
5880     }
5881 }
5882 \f
5883 /* Keep track of the number of spaces used to indent the
5884    output of the debugging routines that print the structure of
5885    the DIE internal representation.  */
5886 static int print_indent;
5887
5888 /* Indent the line the number of spaces given by print_indent.  */
5889
5890 static inline void
5891 print_spaces (FILE *outfile)
5892 {
5893   fprintf (outfile, "%*s", print_indent, "");
5894 }
5895
5896 /* Print the information associated with a given DIE, and its children.
5897    This routine is a debugging aid only.  */
5898
5899 static void
5900 print_die (dw_die_ref die, FILE *outfile)
5901 {
5902   dw_attr_ref a;
5903   dw_die_ref c;
5904   unsigned ix;
5905
5906   print_spaces (outfile);
5907   fprintf (outfile, "DIE %4ld: %s\n",
5908            die->die_offset, dwarf_tag_name (die->die_tag));
5909   print_spaces (outfile);
5910   fprintf (outfile, "  abbrev id: %lu", die->die_abbrev);
5911   fprintf (outfile, " offset: %ld\n", die->die_offset);
5912
5913   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
5914     {
5915       print_spaces (outfile);
5916       fprintf (outfile, "  %s: ", dwarf_attr_name (a->dw_attr));
5917
5918       switch (AT_class (a))
5919         {
5920         case dw_val_class_addr:
5921           fprintf (outfile, "address");
5922           break;
5923         case dw_val_class_offset:
5924           fprintf (outfile, "offset");
5925           break;
5926         case dw_val_class_loc:
5927           fprintf (outfile, "location descriptor");
5928           break;
5929         case dw_val_class_loc_list:
5930           fprintf (outfile, "location list -> label:%s",
5931                    AT_loc_list (a)->ll_symbol);
5932           break;
5933         case dw_val_class_range_list:
5934           fprintf (outfile, "range list");
5935           break;
5936         case dw_val_class_const:
5937           fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, AT_int (a));
5938           break;
5939         case dw_val_class_unsigned_const:
5940           fprintf (outfile, HOST_WIDE_INT_PRINT_UNSIGNED, AT_unsigned (a));
5941           break;
5942         case dw_val_class_long_long:
5943           fprintf (outfile, "constant (%lu,%lu)",
5944                    a->dw_attr_val.v.val_long_long.hi,
5945                    a->dw_attr_val.v.val_long_long.low);
5946           break;
5947         case dw_val_class_vec:
5948           fprintf (outfile, "floating-point or vector constant");
5949           break;
5950         case dw_val_class_flag:
5951           fprintf (outfile, "%u", AT_flag (a));
5952           break;
5953         case dw_val_class_die_ref:
5954           if (AT_ref (a) != NULL)
5955             {
5956               if (AT_ref (a)->die_symbol)
5957                 fprintf (outfile, "die -> label: %s", AT_ref (a)->die_symbol);
5958               else
5959                 fprintf (outfile, "die -> %ld", AT_ref (a)->die_offset);
5960             }
5961           else
5962             fprintf (outfile, "die -> <null>");
5963           break;
5964         case dw_val_class_lbl_id:
5965         case dw_val_class_lineptr:
5966         case dw_val_class_macptr:
5967           fprintf (outfile, "label: %s", AT_lbl (a));
5968           break;
5969         case dw_val_class_str:
5970           if (AT_string (a) != NULL)
5971             fprintf (outfile, "\"%s\"", AT_string (a));
5972           else
5973             fprintf (outfile, "<null>");
5974           break;
5975         case dw_val_class_file:
5976           fprintf (outfile, "\"%s\" (%d)", AT_file (a)->filename,
5977                    AT_file (a)->emitted_number);
5978           break;
5979         default:
5980           break;
5981         }
5982
5983       fprintf (outfile, "\n");
5984     }
5985
5986   if (die->die_child != NULL)
5987     {
5988       print_indent += 4;
5989       FOR_EACH_CHILD (die, c, print_die (c, outfile));
5990       print_indent -= 4;
5991     }
5992   if (print_indent == 0)
5993     fprintf (outfile, "\n");
5994 }
5995
5996 /* Print the contents of the source code line number correspondence table.
5997    This routine is a debugging aid only.  */
5998
5999 static void
6000 print_dwarf_line_table (FILE *outfile)
6001 {
6002   unsigned i;
6003   dw_line_info_ref line_info;
6004
6005   fprintf (outfile, "\n\nDWARF source line information\n");
6006   for (i = 1; i < line_info_table_in_use; i++)
6007     {
6008       line_info = &line_info_table[i];
6009       fprintf (outfile, "%5d: %4ld %6ld\n", i,
6010                line_info->dw_file_num,
6011                line_info->dw_line_num);
6012     }
6013
6014   fprintf (outfile, "\n\n");
6015 }
6016
6017 /* Print the information collected for a given DIE.  */
6018
6019 void
6020 debug_dwarf_die (dw_die_ref die)
6021 {
6022   print_die (die, stderr);
6023 }
6024
6025 /* Print all DWARF information collected for the compilation unit.
6026    This routine is a debugging aid only.  */
6027
6028 void
6029 debug_dwarf (void)
6030 {
6031   print_indent = 0;
6032   print_die (comp_unit_die, stderr);
6033   if (! DWARF2_ASM_LINE_DEBUG_INFO)
6034     print_dwarf_line_table (stderr);
6035 }
6036 \f
6037 /* Start a new compilation unit DIE for an include file.  OLD_UNIT is the CU
6038    for the enclosing include file, if any.  BINCL_DIE is the DW_TAG_GNU_BINCL
6039    DIE that marks the start of the DIEs for this include file.  */
6040
6041 static dw_die_ref
6042 push_new_compile_unit (dw_die_ref old_unit, dw_die_ref bincl_die)
6043 {
6044   const char *filename = get_AT_string (bincl_die, DW_AT_name);
6045   dw_die_ref new_unit = gen_compile_unit_die (filename);
6046
6047   new_unit->die_sib = old_unit;
6048   return new_unit;
6049 }
6050
6051 /* Close an include-file CU and reopen the enclosing one.  */
6052
6053 static dw_die_ref
6054 pop_compile_unit (dw_die_ref old_unit)
6055 {
6056   dw_die_ref new_unit = old_unit->die_sib;
6057
6058   old_unit->die_sib = NULL;
6059   return new_unit;
6060 }
6061
6062 #define CHECKSUM(FOO) md5_process_bytes (&(FOO), sizeof (FOO), ctx)
6063 #define CHECKSUM_STRING(FOO) md5_process_bytes ((FOO), strlen (FOO), ctx)
6064
6065 /* Calculate the checksum of a location expression.  */
6066
6067 static inline void
6068 loc_checksum (dw_loc_descr_ref loc, struct md5_ctx *ctx)
6069 {
6070   CHECKSUM (loc->dw_loc_opc);
6071   CHECKSUM (loc->dw_loc_oprnd1);
6072   CHECKSUM (loc->dw_loc_oprnd2);
6073 }
6074
6075 /* Calculate the checksum of an attribute.  */
6076
6077 static void
6078 attr_checksum (dw_attr_ref at, struct md5_ctx *ctx, int *mark)
6079 {
6080   dw_loc_descr_ref loc;
6081   rtx r;
6082
6083   CHECKSUM (at->dw_attr);
6084
6085   /* We don't care that this was compiled with a different compiler
6086      snapshot; if the output is the same, that's what matters.  */
6087   if (at->dw_attr == DW_AT_producer)
6088     return;
6089
6090   switch (AT_class (at))
6091     {
6092     case dw_val_class_const:
6093       CHECKSUM (at->dw_attr_val.v.val_int);
6094       break;
6095     case dw_val_class_unsigned_const:
6096       CHECKSUM (at->dw_attr_val.v.val_unsigned);
6097       break;
6098     case dw_val_class_long_long:
6099       CHECKSUM (at->dw_attr_val.v.val_long_long);
6100       break;
6101     case dw_val_class_vec:
6102       CHECKSUM (at->dw_attr_val.v.val_vec);
6103       break;
6104     case dw_val_class_flag:
6105       CHECKSUM (at->dw_attr_val.v.val_flag);
6106       break;
6107     case dw_val_class_str:
6108       CHECKSUM_STRING (AT_string (at));
6109       break;
6110
6111     case dw_val_class_addr:
6112       r = AT_addr (at);
6113       gcc_assert (GET_CODE (r) == SYMBOL_REF);
6114       CHECKSUM_STRING (XSTR (r, 0));
6115       break;
6116
6117     case dw_val_class_offset:
6118       CHECKSUM (at->dw_attr_val.v.val_offset);
6119       break;
6120
6121     case dw_val_class_loc:
6122       for (loc = AT_loc (at); loc; loc = loc->dw_loc_next)
6123         loc_checksum (loc, ctx);
6124       break;
6125
6126     case dw_val_class_die_ref:
6127       die_checksum (AT_ref (at), ctx, mark);
6128       break;
6129
6130     case dw_val_class_fde_ref:
6131     case dw_val_class_lbl_id:
6132     case dw_val_class_lineptr:
6133     case dw_val_class_macptr:
6134       break;
6135
6136     case dw_val_class_file:
6137       CHECKSUM_STRING (AT_file (at)->filename);
6138       break;
6139
6140     default:
6141       break;
6142     }
6143 }
6144
6145 /* Calculate the checksum of a DIE.  */
6146
6147 static void
6148 die_checksum (dw_die_ref die, struct md5_ctx *ctx, int *mark)
6149 {
6150   dw_die_ref c;
6151   dw_attr_ref a;
6152   unsigned ix;
6153
6154   /* To avoid infinite recursion.  */
6155   if (die->die_mark)
6156     {
6157       CHECKSUM (die->die_mark);
6158       return;
6159     }
6160   die->die_mark = ++(*mark);
6161
6162   CHECKSUM (die->die_tag);
6163
6164   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6165     attr_checksum (a, ctx, mark);
6166
6167   FOR_EACH_CHILD (die, c, die_checksum (c, ctx, mark));
6168 }
6169
6170 #undef CHECKSUM
6171 #undef CHECKSUM_STRING
6172
6173 /* Do the location expressions look same?  */
6174 static inline int
6175 same_loc_p (dw_loc_descr_ref loc1, dw_loc_descr_ref loc2, int *mark)
6176 {
6177   return loc1->dw_loc_opc == loc2->dw_loc_opc
6178          && same_dw_val_p (&loc1->dw_loc_oprnd1, &loc2->dw_loc_oprnd1, mark)
6179          && same_dw_val_p (&loc1->dw_loc_oprnd2, &loc2->dw_loc_oprnd2, mark);
6180 }
6181
6182 /* Do the values look the same?  */
6183 static int
6184 same_dw_val_p (const dw_val_node *v1, const dw_val_node *v2, int *mark)
6185 {
6186   dw_loc_descr_ref loc1, loc2;
6187   rtx r1, r2;
6188
6189   if (v1->val_class != v2->val_class)
6190     return 0;
6191
6192   switch (v1->val_class)
6193     {
6194     case dw_val_class_const:
6195       return v1->v.val_int == v2->v.val_int;
6196     case dw_val_class_unsigned_const:
6197       return v1->v.val_unsigned == v2->v.val_unsigned;
6198     case dw_val_class_long_long:
6199       return v1->v.val_long_long.hi == v2->v.val_long_long.hi
6200              && v1->v.val_long_long.low == v2->v.val_long_long.low;
6201     case dw_val_class_vec:
6202       if (v1->v.val_vec.length != v2->v.val_vec.length
6203           || v1->v.val_vec.elt_size != v2->v.val_vec.elt_size)
6204         return 0;
6205       if (memcmp (v1->v.val_vec.array, v2->v.val_vec.array,
6206                   v1->v.val_vec.length * v1->v.val_vec.elt_size))
6207         return 0;
6208       return 1;
6209     case dw_val_class_flag:
6210       return v1->v.val_flag == v2->v.val_flag;
6211     case dw_val_class_str:
6212       return !strcmp(v1->v.val_str->str, v2->v.val_str->str);
6213
6214     case dw_val_class_addr:
6215       r1 = v1->v.val_addr;
6216       r2 = v2->v.val_addr;
6217       if (GET_CODE (r1) != GET_CODE (r2))
6218         return 0;
6219       gcc_assert (GET_CODE (r1) == SYMBOL_REF);
6220       return !strcmp (XSTR (r1, 0), XSTR (r2, 0));
6221
6222     case dw_val_class_offset:
6223       return v1->v.val_offset == v2->v.val_offset;
6224
6225     case dw_val_class_loc:
6226       for (loc1 = v1->v.val_loc, loc2 = v2->v.val_loc;
6227            loc1 && loc2;
6228            loc1 = loc1->dw_loc_next, loc2 = loc2->dw_loc_next)
6229         if (!same_loc_p (loc1, loc2, mark))
6230           return 0;
6231       return !loc1 && !loc2;
6232
6233     case dw_val_class_die_ref:
6234       return same_die_p (v1->v.val_die_ref.die, v2->v.val_die_ref.die, mark);
6235
6236     case dw_val_class_fde_ref:
6237     case dw_val_class_lbl_id:
6238     case dw_val_class_lineptr:
6239     case dw_val_class_macptr:
6240       return 1;
6241
6242     case dw_val_class_file:
6243       return v1->v.val_file == v2->v.val_file;
6244
6245     default:
6246       return 1;
6247     }
6248 }
6249
6250 /* Do the attributes look the same?  */
6251
6252 static int
6253 same_attr_p (dw_attr_ref at1, dw_attr_ref at2, int *mark)
6254 {
6255   if (at1->dw_attr != at2->dw_attr)
6256     return 0;
6257
6258   /* We don't care that this was compiled with a different compiler
6259      snapshot; if the output is the same, that's what matters. */
6260   if (at1->dw_attr == DW_AT_producer)
6261     return 1;
6262
6263   return same_dw_val_p (&at1->dw_attr_val, &at2->dw_attr_val, mark);
6264 }
6265
6266 /* Do the dies look the same?  */
6267
6268 static int
6269 same_die_p (dw_die_ref die1, dw_die_ref die2, int *mark)
6270 {
6271   dw_die_ref c1, c2;
6272   dw_attr_ref a1;
6273   unsigned ix;
6274
6275   /* To avoid infinite recursion.  */
6276   if (die1->die_mark)
6277     return die1->die_mark == die2->die_mark;
6278   die1->die_mark = die2->die_mark = ++(*mark);
6279
6280   if (die1->die_tag != die2->die_tag)
6281     return 0;
6282
6283   if (VEC_length (dw_attr_node, die1->die_attr)
6284       != VEC_length (dw_attr_node, die2->die_attr))
6285     return 0;
6286
6287   for (ix = 0; VEC_iterate (dw_attr_node, die1->die_attr, ix, a1); ix++)
6288     if (!same_attr_p (a1, VEC_index (dw_attr_node, die2->die_attr, ix), mark))
6289       return 0;
6290
6291   c1 = die1->die_child;
6292   c2 = die2->die_child;
6293   if (! c1)
6294     {
6295       if (c2)
6296         return 0;
6297     }
6298   else
6299     for (;;)
6300       {
6301         if (!same_die_p (c1, c2, mark))
6302           return 0;
6303         c1 = c1->die_sib;
6304         c2 = c2->die_sib;
6305         if (c1 == die1->die_child)
6306           {
6307             if (c2 == die2->die_child)
6308               break;
6309             else
6310               return 0;
6311           }
6312     }
6313
6314   return 1;
6315 }
6316
6317 /* Do the dies look the same?  Wrapper around same_die_p.  */
6318
6319 static int
6320 same_die_p_wrap (dw_die_ref die1, dw_die_ref die2)
6321 {
6322   int mark = 0;
6323   int ret = same_die_p (die1, die2, &mark);
6324
6325   unmark_all_dies (die1);
6326   unmark_all_dies (die2);
6327
6328   return ret;
6329 }
6330
6331 /* The prefix to attach to symbols on DIEs in the current comdat debug
6332    info section.  */
6333 static char *comdat_symbol_id;
6334
6335 /* The index of the current symbol within the current comdat CU.  */
6336 static unsigned int comdat_symbol_number;
6337
6338 /* Calculate the MD5 checksum of the compilation unit DIE UNIT_DIE and its
6339    children, and set comdat_symbol_id accordingly.  */
6340
6341 static void
6342 compute_section_prefix (dw_die_ref unit_die)
6343 {
6344   const char *die_name = get_AT_string (unit_die, DW_AT_name);
6345   const char *base = die_name ? lbasename (die_name) : "anonymous";
6346   char *name = alloca (strlen (base) + 64);
6347   char *p;
6348   int i, mark;
6349   unsigned char checksum[16];
6350   struct md5_ctx ctx;
6351
6352   /* Compute the checksum of the DIE, then append part of it as hex digits to
6353      the name filename of the unit.  */
6354
6355   md5_init_ctx (&ctx);
6356   mark = 0;
6357   die_checksum (unit_die, &ctx, &mark);
6358   unmark_all_dies (unit_die);
6359   md5_finish_ctx (&ctx, checksum);
6360
6361   sprintf (name, "%s.", base);
6362   clean_symbol_name (name);
6363
6364   p = name + strlen (name);
6365   for (i = 0; i < 4; i++)
6366     {
6367       sprintf (p, "%.2x", checksum[i]);
6368       p += 2;
6369     }
6370
6371   comdat_symbol_id = unit_die->die_symbol = xstrdup (name);
6372   comdat_symbol_number = 0;
6373 }
6374
6375 /* Returns nonzero if DIE represents a type, in the sense of TYPE_P.  */
6376
6377 static int
6378 is_type_die (dw_die_ref die)
6379 {
6380   switch (die->die_tag)
6381     {
6382     case DW_TAG_array_type:
6383     case DW_TAG_class_type:
6384     case DW_TAG_interface_type:
6385     case DW_TAG_enumeration_type:
6386     case DW_TAG_pointer_type:
6387     case DW_TAG_reference_type:
6388     case DW_TAG_string_type:
6389     case DW_TAG_structure_type:
6390     case DW_TAG_subroutine_type:
6391     case DW_TAG_union_type:
6392     case DW_TAG_ptr_to_member_type:
6393     case DW_TAG_set_type:
6394     case DW_TAG_subrange_type:
6395     case DW_TAG_base_type:
6396     case DW_TAG_const_type:
6397     case DW_TAG_file_type:
6398     case DW_TAG_packed_type:
6399     case DW_TAG_volatile_type:
6400     case DW_TAG_typedef:
6401       return 1;
6402     default:
6403       return 0;
6404     }
6405 }
6406
6407 /* Returns 1 iff C is the sort of DIE that should go into a COMDAT CU.
6408    Basically, we want to choose the bits that are likely to be shared between
6409    compilations (types) and leave out the bits that are specific to individual
6410    compilations (functions).  */
6411
6412 static int
6413 is_comdat_die (dw_die_ref c)
6414 {
6415   /* I think we want to leave base types and __vtbl_ptr_type in the main CU, as
6416      we do for stabs.  The advantage is a greater likelihood of sharing between
6417      objects that don't include headers in the same order (and therefore would
6418      put the base types in a different comdat).  jason 8/28/00 */
6419
6420   if (c->die_tag == DW_TAG_base_type)
6421     return 0;
6422
6423   if (c->die_tag == DW_TAG_pointer_type
6424       || c->die_tag == DW_TAG_reference_type
6425       || c->die_tag == DW_TAG_const_type
6426       || c->die_tag == DW_TAG_volatile_type)
6427     {
6428       dw_die_ref t = get_AT_ref (c, DW_AT_type);
6429
6430       return t ? is_comdat_die (t) : 0;
6431     }
6432
6433   return is_type_die (c);
6434 }
6435
6436 /* Returns 1 iff C is the sort of DIE that might be referred to from another
6437    compilation unit.  */
6438
6439 static int
6440 is_symbol_die (dw_die_ref c)
6441 {
6442   return (is_type_die (c)
6443           || (get_AT (c, DW_AT_declaration)
6444               && !get_AT (c, DW_AT_specification))
6445           || c->die_tag == DW_TAG_namespace);
6446 }
6447
6448 static char *
6449 gen_internal_sym (const char *prefix)
6450 {
6451   char buf[256];
6452
6453   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, label_num++);
6454   return xstrdup (buf);
6455 }
6456
6457 /* Assign symbols to all worthy DIEs under DIE.  */
6458
6459 static void
6460 assign_symbol_names (dw_die_ref die)
6461 {
6462   dw_die_ref c;
6463
6464   if (is_symbol_die (die))
6465     {
6466       if (comdat_symbol_id)
6467         {
6468           char *p = alloca (strlen (comdat_symbol_id) + 64);
6469
6470           sprintf (p, "%s.%s.%x", DIE_LABEL_PREFIX,
6471                    comdat_symbol_id, comdat_symbol_number++);
6472           die->die_symbol = xstrdup (p);
6473         }
6474       else
6475         die->die_symbol = gen_internal_sym ("LDIE");
6476     }
6477
6478   FOR_EACH_CHILD (die, c, assign_symbol_names (c));
6479 }
6480
6481 struct cu_hash_table_entry
6482 {
6483   dw_die_ref cu;
6484   unsigned min_comdat_num, max_comdat_num;
6485   struct cu_hash_table_entry *next;
6486 };
6487
6488 /* Routines to manipulate hash table of CUs.  */
6489 static hashval_t
6490 htab_cu_hash (const void *of)
6491 {
6492   const struct cu_hash_table_entry *entry = of;
6493
6494   return htab_hash_string (entry->cu->die_symbol);
6495 }
6496
6497 static int
6498 htab_cu_eq (const void *of1, const void *of2)
6499 {
6500   const struct cu_hash_table_entry *entry1 = of1;
6501   const struct die_struct *entry2 = of2;
6502
6503   return !strcmp (entry1->cu->die_symbol, entry2->die_symbol);
6504 }
6505
6506 static void
6507 htab_cu_del (void *what)
6508 {
6509   struct cu_hash_table_entry *next, *entry = what;
6510
6511   while (entry)
6512     {
6513       next = entry->next;
6514       free (entry);
6515       entry = next;
6516     }
6517 }
6518
6519 /* Check whether we have already seen this CU and set up SYM_NUM
6520    accordingly.  */
6521 static int
6522 check_duplicate_cu (dw_die_ref cu, htab_t htable, unsigned int *sym_num)
6523 {
6524   struct cu_hash_table_entry dummy;
6525   struct cu_hash_table_entry **slot, *entry, *last = &dummy;
6526
6527   dummy.max_comdat_num = 0;
6528
6529   slot = (struct cu_hash_table_entry **)
6530     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6531         INSERT);
6532   entry = *slot;
6533
6534   for (; entry; last = entry, entry = entry->next)
6535     {
6536       if (same_die_p_wrap (cu, entry->cu))
6537         break;
6538     }
6539
6540   if (entry)
6541     {
6542       *sym_num = entry->min_comdat_num;
6543       return 1;
6544     }
6545
6546   entry = XCNEW (struct cu_hash_table_entry);
6547   entry->cu = cu;
6548   entry->min_comdat_num = *sym_num = last->max_comdat_num;
6549   entry->next = *slot;
6550   *slot = entry;
6551
6552   return 0;
6553 }
6554
6555 /* Record SYM_NUM to record of CU in HTABLE.  */
6556 static void
6557 record_comdat_symbol_number (dw_die_ref cu, htab_t htable, unsigned int sym_num)
6558 {
6559   struct cu_hash_table_entry **slot, *entry;
6560
6561   slot = (struct cu_hash_table_entry **)
6562     htab_find_slot_with_hash (htable, cu, htab_hash_string (cu->die_symbol),
6563         NO_INSERT);
6564   entry = *slot;
6565
6566   entry->max_comdat_num = sym_num;
6567 }
6568
6569 /* Traverse the DIE (which is always comp_unit_die), and set up
6570    additional compilation units for each of the include files we see
6571    bracketed by BINCL/EINCL.  */
6572
6573 static void
6574 break_out_includes (dw_die_ref die)
6575 {
6576   dw_die_ref c;
6577   dw_die_ref unit = NULL;
6578   limbo_die_node *node, **pnode;
6579   htab_t cu_hash_table;
6580
6581   c = die->die_child;
6582   if (c) do {
6583     dw_die_ref prev = c;
6584     c = c->die_sib;
6585     while (c->die_tag == DW_TAG_GNU_BINCL || c->die_tag == DW_TAG_GNU_EINCL
6586            || (unit && is_comdat_die (c)))
6587       {
6588         dw_die_ref next = c->die_sib;
6589
6590         /* This DIE is for a secondary CU; remove it from the main one.  */
6591         remove_child_with_prev (c, prev);
6592
6593         if (c->die_tag == DW_TAG_GNU_BINCL)
6594           unit = push_new_compile_unit (unit, c);
6595         else if (c->die_tag == DW_TAG_GNU_EINCL)
6596           unit = pop_compile_unit (unit);
6597         else
6598           add_child_die (unit, c);
6599         c = next;
6600         if (c == die->die_child)
6601           break;
6602       }
6603   } while (c != die->die_child);
6604
6605 #if 0
6606   /* We can only use this in debugging, since the frontend doesn't check
6607      to make sure that we leave every include file we enter.  */
6608   gcc_assert (!unit);
6609 #endif
6610
6611   assign_symbol_names (die);
6612   cu_hash_table = htab_create (10, htab_cu_hash, htab_cu_eq, htab_cu_del);
6613   for (node = limbo_die_list, pnode = &limbo_die_list;
6614        node;
6615        node = node->next)
6616     {
6617       int is_dupl;
6618
6619       compute_section_prefix (node->die);
6620       is_dupl = check_duplicate_cu (node->die, cu_hash_table,
6621                         &comdat_symbol_number);
6622       assign_symbol_names (node->die);
6623       if (is_dupl)
6624         *pnode = node->next;
6625       else
6626         {
6627           pnode = &node->next;
6628           record_comdat_symbol_number (node->die, cu_hash_table,
6629                 comdat_symbol_number);
6630         }
6631     }
6632   htab_delete (cu_hash_table);
6633 }
6634
6635 /* Traverse the DIE and add a sibling attribute if it may have the
6636    effect of speeding up access to siblings.  To save some space,
6637    avoid generating sibling attributes for DIE's without children.  */
6638
6639 static void
6640 add_sibling_attributes (dw_die_ref die)
6641 {
6642   dw_die_ref c;
6643
6644   if (! die->die_child)
6645     return;
6646
6647   if (die->die_parent && die != die->die_parent->die_child)
6648     add_AT_die_ref (die, DW_AT_sibling, die->die_sib);
6649
6650   FOR_EACH_CHILD (die, c, add_sibling_attributes (c));
6651 }
6652
6653 /* Output all location lists for the DIE and its children.  */
6654
6655 static void
6656 output_location_lists (dw_die_ref die)
6657 {
6658   dw_die_ref c;
6659   dw_attr_ref a;
6660   unsigned ix;
6661
6662   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6663     if (AT_class (a) == dw_val_class_loc_list)
6664       output_loc_list (AT_loc_list (a));
6665
6666   FOR_EACH_CHILD (die, c, output_location_lists (c));
6667 }
6668
6669 /* The format of each DIE (and its attribute value pairs) is encoded in an
6670    abbreviation table.  This routine builds the abbreviation table and assigns
6671    a unique abbreviation id for each abbreviation entry.  The children of each
6672    die are visited recursively.  */
6673
6674 static void
6675 build_abbrev_table (dw_die_ref die)
6676 {
6677   unsigned long abbrev_id;
6678   unsigned int n_alloc;
6679   dw_die_ref c;
6680   dw_attr_ref a;
6681   unsigned ix;
6682
6683   /* Scan the DIE references, and mark as external any that refer to
6684      DIEs from other CUs (i.e. those which are not marked).  */
6685   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6686     if (AT_class (a) == dw_val_class_die_ref
6687         && AT_ref (a)->die_mark == 0)
6688       {
6689         gcc_assert (AT_ref (a)->die_symbol);
6690
6691         set_AT_ref_external (a, 1);
6692       }
6693
6694   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
6695     {
6696       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
6697       dw_attr_ref die_a, abbrev_a;
6698       unsigned ix;
6699       bool ok = true;
6700
6701       if (abbrev->die_tag != die->die_tag)
6702         continue;
6703       if ((abbrev->die_child != NULL) != (die->die_child != NULL))
6704         continue;
6705
6706       if (VEC_length (dw_attr_node, abbrev->die_attr)
6707           != VEC_length (dw_attr_node, die->die_attr))
6708         continue;
6709
6710       for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, die_a); ix++)
6711         {
6712           abbrev_a = VEC_index (dw_attr_node, abbrev->die_attr, ix);
6713           if ((abbrev_a->dw_attr != die_a->dw_attr)
6714               || (value_format (abbrev_a) != value_format (die_a)))
6715             {
6716               ok = false;
6717               break;
6718             }
6719         }
6720       if (ok)
6721         break;
6722     }
6723
6724   if (abbrev_id >= abbrev_die_table_in_use)
6725     {
6726       if (abbrev_die_table_in_use >= abbrev_die_table_allocated)
6727         {
6728           n_alloc = abbrev_die_table_allocated + ABBREV_DIE_TABLE_INCREMENT;
6729           abbrev_die_table = ggc_realloc (abbrev_die_table,
6730                                           sizeof (dw_die_ref) * n_alloc);
6731
6732           memset (&abbrev_die_table[abbrev_die_table_allocated], 0,
6733                  (n_alloc - abbrev_die_table_allocated) * sizeof (dw_die_ref));
6734           abbrev_die_table_allocated = n_alloc;
6735         }
6736
6737       ++abbrev_die_table_in_use;
6738       abbrev_die_table[abbrev_id] = die;
6739     }
6740
6741   die->die_abbrev = abbrev_id;
6742   FOR_EACH_CHILD (die, c, build_abbrev_table (c));
6743 }
6744 \f
6745 /* Return the power-of-two number of bytes necessary to represent VALUE.  */
6746
6747 static int
6748 constant_size (long unsigned int value)
6749 {
6750   int log;
6751
6752   if (value == 0)
6753     log = 0;
6754   else
6755     log = floor_log2 (value);
6756
6757   log = log / 8;
6758   log = 1 << (floor_log2 (log) + 1);
6759
6760   return log;
6761 }
6762
6763 /* Return the size of a DIE as it is represented in the
6764    .debug_info section.  */
6765
6766 static unsigned long
6767 size_of_die (dw_die_ref die)
6768 {
6769   unsigned long size = 0;
6770   dw_attr_ref a;
6771   unsigned ix;
6772
6773   size += size_of_uleb128 (die->die_abbrev);
6774   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6775     {
6776       switch (AT_class (a))
6777         {
6778         case dw_val_class_addr:
6779           size += DWARF2_ADDR_SIZE;
6780           break;
6781         case dw_val_class_offset:
6782           size += DWARF_OFFSET_SIZE;
6783           break;
6784         case dw_val_class_loc:
6785           {
6786             unsigned long lsize = size_of_locs (AT_loc (a));
6787
6788             /* Block length.  */
6789             size += constant_size (lsize);
6790             size += lsize;
6791           }
6792           break;
6793         case dw_val_class_loc_list:
6794           size += DWARF_OFFSET_SIZE;
6795           break;
6796         case dw_val_class_range_list:
6797           size += DWARF_OFFSET_SIZE;
6798           break;
6799         case dw_val_class_const:
6800           size += size_of_sleb128 (AT_int (a));
6801           break;
6802         case dw_val_class_unsigned_const:
6803           size += constant_size (AT_unsigned (a));
6804           break;
6805         case dw_val_class_long_long:
6806           size += 1 + 2*HOST_BITS_PER_LONG/HOST_BITS_PER_CHAR; /* block */
6807           break;
6808         case dw_val_class_vec:
6809           size += 1 + (a->dw_attr_val.v.val_vec.length
6810                        * a->dw_attr_val.v.val_vec.elt_size); /* block */
6811           break;
6812         case dw_val_class_flag:
6813           size += 1;
6814           break;
6815         case dw_val_class_die_ref:
6816           if (AT_ref_external (a))
6817             size += DWARF2_ADDR_SIZE;
6818           else
6819             size += DWARF_OFFSET_SIZE;
6820           break;
6821         case dw_val_class_fde_ref:
6822           size += DWARF_OFFSET_SIZE;
6823           break;
6824         case dw_val_class_lbl_id:
6825           size += DWARF2_ADDR_SIZE;
6826           break;
6827         case dw_val_class_lineptr:
6828         case dw_val_class_macptr:
6829           size += DWARF_OFFSET_SIZE;
6830           break;
6831         case dw_val_class_str:
6832           if (AT_string_form (a) == DW_FORM_strp)
6833             size += DWARF_OFFSET_SIZE;
6834           else
6835             size += strlen (a->dw_attr_val.v.val_str->str) + 1;
6836           break;
6837         case dw_val_class_file:
6838           size += constant_size (maybe_emit_file (a->dw_attr_val.v.val_file));
6839           break;
6840         default:
6841           gcc_unreachable ();
6842         }
6843     }
6844
6845   return size;
6846 }
6847
6848 /* Size the debugging information associated with a given DIE.  Visits the
6849    DIE's children recursively.  Updates the global variable next_die_offset, on
6850    each time through.  Uses the current value of next_die_offset to update the
6851    die_offset field in each DIE.  */
6852
6853 static void
6854 calc_die_sizes (dw_die_ref die)
6855 {
6856   dw_die_ref c;
6857
6858   die->die_offset = next_die_offset;
6859   next_die_offset += size_of_die (die);
6860
6861   FOR_EACH_CHILD (die, c, calc_die_sizes (c));
6862
6863   if (die->die_child != NULL)
6864     /* Count the null byte used to terminate sibling lists.  */
6865     next_die_offset += 1;
6866 }
6867
6868 /* Set the marks for a die and its children.  We do this so
6869    that we know whether or not a reference needs to use FORM_ref_addr; only
6870    DIEs in the same CU will be marked.  We used to clear out the offset
6871    and use that as the flag, but ran into ordering problems.  */
6872
6873 static void
6874 mark_dies (dw_die_ref die)
6875 {
6876   dw_die_ref c;
6877
6878   gcc_assert (!die->die_mark);
6879
6880   die->die_mark = 1;
6881   FOR_EACH_CHILD (die, c, mark_dies (c));
6882 }
6883
6884 /* Clear the marks for a die and its children.  */
6885
6886 static void
6887 unmark_dies (dw_die_ref die)
6888 {
6889   dw_die_ref c;
6890
6891   gcc_assert (die->die_mark);
6892
6893   die->die_mark = 0;
6894   FOR_EACH_CHILD (die, c, unmark_dies (c));
6895 }
6896
6897 /* Clear the marks for a die, its children and referred dies.  */
6898
6899 static void
6900 unmark_all_dies (dw_die_ref die)
6901 {
6902   dw_die_ref c;
6903   dw_attr_ref a;
6904   unsigned ix;
6905
6906   if (!die->die_mark)
6907     return;
6908   die->die_mark = 0;
6909
6910   FOR_EACH_CHILD (die, c, unmark_all_dies (c));
6911
6912   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
6913     if (AT_class (a) == dw_val_class_die_ref)
6914       unmark_all_dies (AT_ref (a));
6915 }
6916
6917 /* Return the size of the .debug_pubnames or .debug_pubtypes table
6918    generated for the compilation unit.  */
6919
6920 static unsigned long
6921 size_of_pubnames (VEC (pubname_entry, gc) * names)
6922 {
6923   unsigned long size;
6924   unsigned i;
6925   pubname_ref p;
6926
6927   size = DWARF_PUBNAMES_HEADER_SIZE;
6928   for (i = 0; VEC_iterate (pubname_entry, names, i, p); i++)
6929     if (names != pubtype_table
6930         || p->die->die_offset != 0
6931         || !flag_eliminate_unused_debug_types)
6932       size += strlen (p->name) + DWARF_OFFSET_SIZE + 1;
6933
6934   size += DWARF_OFFSET_SIZE;
6935   return size;
6936 }
6937
6938 /* Return the size of the information in the .debug_aranges section.  */
6939
6940 static unsigned long
6941 size_of_aranges (void)
6942 {
6943   unsigned long size;
6944
6945   size = DWARF_ARANGES_HEADER_SIZE;
6946
6947   /* Count the address/length pair for this compilation unit.  */
6948   if (text_section_used)
6949     size += 2 * DWARF2_ADDR_SIZE;
6950   if (cold_text_section_used)
6951     size += 2 * DWARF2_ADDR_SIZE;
6952   size += 2 * DWARF2_ADDR_SIZE * arange_table_in_use;
6953
6954   /* Count the two zero words used to terminated the address range table.  */
6955   size += 2 * DWARF2_ADDR_SIZE;
6956   return size;
6957 }
6958 \f
6959 /* Select the encoding of an attribute value.  */
6960
6961 static enum dwarf_form
6962 value_format (dw_attr_ref a)
6963 {
6964   switch (a->dw_attr_val.val_class)
6965     {
6966     case dw_val_class_addr:
6967       return DW_FORM_addr;
6968     case dw_val_class_range_list:
6969     case dw_val_class_offset:
6970     case dw_val_class_loc_list:
6971       switch (DWARF_OFFSET_SIZE)
6972         {
6973         case 4:
6974           return DW_FORM_data4;
6975         case 8:
6976           return DW_FORM_data8;
6977         default:
6978           gcc_unreachable ();
6979         }
6980     case dw_val_class_loc:
6981       switch (constant_size (size_of_locs (AT_loc (a))))
6982         {
6983         case 1:
6984           return DW_FORM_block1;
6985         case 2:
6986           return DW_FORM_block2;
6987         default:
6988           gcc_unreachable ();
6989         }
6990     case dw_val_class_const:
6991       return DW_FORM_sdata;
6992     case dw_val_class_unsigned_const:
6993       switch (constant_size (AT_unsigned (a)))
6994         {
6995         case 1:
6996           return DW_FORM_data1;
6997         case 2:
6998           return DW_FORM_data2;
6999         case 4:
7000           return DW_FORM_data4;
7001         case 8:
7002           return DW_FORM_data8;
7003         default:
7004           gcc_unreachable ();
7005         }
7006     case dw_val_class_long_long:
7007       return DW_FORM_block1;
7008     case dw_val_class_vec:
7009       return DW_FORM_block1;
7010     case dw_val_class_flag:
7011       return DW_FORM_flag;
7012     case dw_val_class_die_ref:
7013       if (AT_ref_external (a))
7014         return DW_FORM_ref_addr;
7015       else
7016         return DW_FORM_ref;
7017     case dw_val_class_fde_ref:
7018       return DW_FORM_data;
7019     case dw_val_class_lbl_id:
7020       return DW_FORM_addr;
7021     case dw_val_class_lineptr:
7022     case dw_val_class_macptr:
7023       return DW_FORM_data;
7024     case dw_val_class_str:
7025       return AT_string_form (a);
7026     case dw_val_class_file:
7027       switch (constant_size (maybe_emit_file (a->dw_attr_val.v.val_file)))
7028         {
7029         case 1:
7030           return DW_FORM_data1;
7031         case 2:
7032           return DW_FORM_data2;
7033         case 4:
7034           return DW_FORM_data4;
7035         default:
7036           gcc_unreachable ();
7037         }
7038
7039     default:
7040       gcc_unreachable ();
7041     }
7042 }
7043
7044 /* Output the encoding of an attribute value.  */
7045
7046 static void
7047 output_value_format (dw_attr_ref a)
7048 {
7049   enum dwarf_form form = value_format (a);
7050
7051   dw2_asm_output_data_uleb128 (form, "(%s)", dwarf_form_name (form));
7052 }
7053
7054 /* Output the .debug_abbrev section which defines the DIE abbreviation
7055    table.  */
7056
7057 static void
7058 output_abbrev_section (void)
7059 {
7060   unsigned long abbrev_id;
7061
7062   for (abbrev_id = 1; abbrev_id < abbrev_die_table_in_use; ++abbrev_id)
7063     {
7064       dw_die_ref abbrev = abbrev_die_table[abbrev_id];
7065       unsigned ix;
7066       dw_attr_ref a_attr;
7067
7068       dw2_asm_output_data_uleb128 (abbrev_id, "(abbrev code)");
7069       dw2_asm_output_data_uleb128 (abbrev->die_tag, "(TAG: %s)",
7070                                    dwarf_tag_name (abbrev->die_tag));
7071
7072       if (abbrev->die_child != NULL)
7073         dw2_asm_output_data (1, DW_children_yes, "DW_children_yes");
7074       else
7075         dw2_asm_output_data (1, DW_children_no, "DW_children_no");
7076
7077       for (ix = 0; VEC_iterate (dw_attr_node, abbrev->die_attr, ix, a_attr);
7078            ix++)
7079         {
7080           dw2_asm_output_data_uleb128 (a_attr->dw_attr, "(%s)",
7081                                        dwarf_attr_name (a_attr->dw_attr));
7082           output_value_format (a_attr);
7083         }
7084
7085       dw2_asm_output_data (1, 0, NULL);
7086       dw2_asm_output_data (1, 0, NULL);
7087     }
7088
7089   /* Terminate the table.  */
7090   dw2_asm_output_data (1, 0, NULL);
7091 }
7092
7093 /* Output a symbol we can use to refer to this DIE from another CU.  */
7094
7095 static inline void
7096 output_die_symbol (dw_die_ref die)
7097 {
7098   char *sym = die->die_symbol;
7099
7100   if (sym == 0)
7101     return;
7102
7103   if (strncmp (sym, DIE_LABEL_PREFIX, sizeof (DIE_LABEL_PREFIX) - 1) == 0)
7104     /* We make these global, not weak; if the target doesn't support
7105        .linkonce, it doesn't support combining the sections, so debugging
7106        will break.  */
7107     targetm.asm_out.globalize_label (asm_out_file, sym);
7108
7109   ASM_OUTPUT_LABEL (asm_out_file, sym);
7110 }
7111
7112 /* Return a new location list, given the begin and end range, and the
7113    expression. gensym tells us whether to generate a new internal symbol for
7114    this location list node, which is done for the head of the list only.  */
7115
7116 static inline dw_loc_list_ref
7117 new_loc_list (dw_loc_descr_ref expr, const char *begin, const char *end,
7118               const char *section, unsigned int gensym)
7119 {
7120   dw_loc_list_ref retlist = ggc_alloc_cleared (sizeof (dw_loc_list_node));
7121
7122   retlist->begin = begin;
7123   retlist->end = end;
7124   retlist->expr = expr;
7125   retlist->section = section;
7126   if (gensym)
7127     retlist->ll_symbol = gen_internal_sym ("LLST");
7128
7129   return retlist;
7130 }
7131
7132 /* Add a location description expression to a location list.  */
7133
7134 static inline void
7135 add_loc_descr_to_loc_list (dw_loc_list_ref *list_head, dw_loc_descr_ref descr,
7136                            const char *begin, const char *end,
7137                            const char *section)
7138 {
7139   dw_loc_list_ref *d;
7140
7141   /* Find the end of the chain.  */
7142   for (d = list_head; (*d) != NULL; d = &(*d)->dw_loc_next)
7143     ;
7144
7145   /* Add a new location list node to the list.  */
7146   *d = new_loc_list (descr, begin, end, section, 0);
7147 }
7148
7149 /* Output the location list given to us.  */
7150
7151 static void
7152 output_loc_list (dw_loc_list_ref list_head)
7153 {
7154   dw_loc_list_ref curr = list_head;
7155
7156   ASM_OUTPUT_LABEL (asm_out_file, list_head->ll_symbol);
7157
7158   /* Walk the location list, and output each range + expression.  */
7159   for (curr = list_head; curr != NULL; curr = curr->dw_loc_next)
7160     {
7161       unsigned long size;
7162       /* Don't output an entry that starts and ends at the same address.  */
7163       if (strcmp (curr->begin, curr->end) == 0)
7164         continue;
7165       if (!have_multiple_function_sections)
7166         {
7167           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->begin, curr->section,
7168                                 "Location list begin address (%s)",
7169                                 list_head->ll_symbol);
7170           dw2_asm_output_delta (DWARF2_ADDR_SIZE, curr->end, curr->section,
7171                                 "Location list end address (%s)",
7172                                 list_head->ll_symbol);
7173         }
7174       else
7175         {
7176           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->begin,
7177                                "Location list begin address (%s)",
7178                                list_head->ll_symbol);
7179           dw2_asm_output_addr (DWARF2_ADDR_SIZE, curr->end,
7180                                "Location list end address (%s)",
7181                                list_head->ll_symbol);
7182         }
7183       size = size_of_locs (curr->expr);
7184
7185       /* Output the block length for this list of location operations.  */
7186       gcc_assert (size <= 0xffff);
7187       dw2_asm_output_data (2, size, "%s", "Location expression size");
7188
7189       output_loc_sequence (curr->expr);
7190     }
7191
7192   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7193                        "Location list terminator begin (%s)",
7194                        list_head->ll_symbol);
7195   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0,
7196                        "Location list terminator end (%s)",
7197                        list_head->ll_symbol);
7198 }
7199
7200 /* Output the DIE and its attributes.  Called recursively to generate
7201    the definitions of each child DIE.  */
7202
7203 static void
7204 output_die (dw_die_ref die)
7205 {
7206   dw_attr_ref a;
7207   dw_die_ref c;
7208   unsigned long size;
7209   unsigned ix;
7210
7211   /* If someone in another CU might refer to us, set up a symbol for
7212      them to point to.  */
7213   if (die->die_symbol)
7214     output_die_symbol (die);
7215
7216   dw2_asm_output_data_uleb128 (die->die_abbrev, "(DIE (0x%lx) %s)",
7217                                (unsigned long)die->die_offset,
7218                                dwarf_tag_name (die->die_tag));
7219
7220   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
7221     {
7222       const char *name = dwarf_attr_name (a->dw_attr);
7223
7224       switch (AT_class (a))
7225         {
7226         case dw_val_class_addr:
7227           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE, AT_addr (a), "%s", name);
7228           break;
7229
7230         case dw_val_class_offset:
7231           dw2_asm_output_data (DWARF_OFFSET_SIZE, a->dw_attr_val.v.val_offset,
7232                                "%s", name);
7233           break;
7234
7235         case dw_val_class_range_list:
7236           {
7237             char *p = strchr (ranges_section_label, '\0');
7238
7239             sprintf (p, "+" HOST_WIDE_INT_PRINT_HEX,
7240                      a->dw_attr_val.v.val_offset);
7241             dw2_asm_output_offset (DWARF_OFFSET_SIZE, ranges_section_label,
7242                                    debug_ranges_section, "%s", name);
7243             *p = '\0';
7244           }
7245           break;
7246
7247         case dw_val_class_loc:
7248           size = size_of_locs (AT_loc (a));
7249
7250           /* Output the block length for this list of location operations.  */
7251           dw2_asm_output_data (constant_size (size), size, "%s", name);
7252
7253           output_loc_sequence (AT_loc (a));
7254           break;
7255
7256         case dw_val_class_const:
7257           /* ??? It would be slightly more efficient to use a scheme like is
7258              used for unsigned constants below, but gdb 4.x does not sign
7259              extend.  Gdb 5.x does sign extend.  */
7260           dw2_asm_output_data_sleb128 (AT_int (a), "%s", name);
7261           break;
7262
7263         case dw_val_class_unsigned_const:
7264           dw2_asm_output_data (constant_size (AT_unsigned (a)),
7265                                AT_unsigned (a), "%s", name);
7266           break;
7267
7268         case dw_val_class_long_long:
7269           {
7270             unsigned HOST_WIDE_INT first, second;
7271
7272             dw2_asm_output_data (1,
7273                                  2 * HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7274                                  "%s", name);
7275
7276             if (WORDS_BIG_ENDIAN)
7277               {
7278                 first = a->dw_attr_val.v.val_long_long.hi;
7279                 second = a->dw_attr_val.v.val_long_long.low;
7280               }
7281             else
7282               {
7283                 first = a->dw_attr_val.v.val_long_long.low;
7284                 second = a->dw_attr_val.v.val_long_long.hi;
7285               }
7286
7287             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7288                                  first, "long long constant");
7289             dw2_asm_output_data (HOST_BITS_PER_LONG / HOST_BITS_PER_CHAR,
7290                                  second, NULL);
7291           }
7292           break;
7293
7294         case dw_val_class_vec:
7295           {
7296             unsigned int elt_size = a->dw_attr_val.v.val_vec.elt_size;
7297             unsigned int len = a->dw_attr_val.v.val_vec.length;
7298             unsigned int i;
7299             unsigned char *p;
7300
7301             dw2_asm_output_data (1, len * elt_size, "%s", name);
7302             if (elt_size > sizeof (HOST_WIDE_INT))
7303               {
7304                 elt_size /= 2;
7305                 len *= 2;
7306               }
7307             for (i = 0, p = a->dw_attr_val.v.val_vec.array;
7308                  i < len;
7309                  i++, p += elt_size)
7310               dw2_asm_output_data (elt_size, extract_int (p, elt_size),
7311                                    "fp or vector constant word %u", i);
7312             break;
7313           }
7314
7315         case dw_val_class_flag:
7316           dw2_asm_output_data (1, AT_flag (a), "%s", name);
7317           break;
7318
7319         case dw_val_class_loc_list:
7320           {
7321             char *sym = AT_loc_list (a)->ll_symbol;
7322
7323             gcc_assert (sym);
7324             dw2_asm_output_offset (DWARF_OFFSET_SIZE, sym, debug_loc_section,
7325                                    "%s", name);
7326           }
7327           break;
7328
7329         case dw_val_class_die_ref:
7330           if (AT_ref_external (a))
7331             {
7332               char *sym = AT_ref (a)->die_symbol;
7333
7334               gcc_assert (sym);
7335               dw2_asm_output_offset (DWARF2_ADDR_SIZE, sym, debug_info_section,
7336                                      "%s", name);
7337             }
7338           else
7339             {
7340               gcc_assert (AT_ref (a)->die_offset);
7341               dw2_asm_output_data (DWARF_OFFSET_SIZE, AT_ref (a)->die_offset,
7342                                    "%s", name);
7343             }
7344           break;
7345
7346         case dw_val_class_fde_ref:
7347           {
7348             char l1[20];
7349
7350             ASM_GENERATE_INTERNAL_LABEL (l1, FDE_LABEL,
7351                                          a->dw_attr_val.v.val_fde_index * 2);
7352             dw2_asm_output_offset (DWARF_OFFSET_SIZE, l1, debug_frame_section,
7353                                    "%s", name);
7354           }
7355           break;
7356
7357         case dw_val_class_lbl_id:
7358           dw2_asm_output_addr (DWARF2_ADDR_SIZE, AT_lbl (a), "%s", name);
7359           break;
7360
7361         case dw_val_class_lineptr:
7362           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7363                                  debug_line_section, "%s", name);
7364           break;
7365
7366         case dw_val_class_macptr:
7367           dw2_asm_output_offset (DWARF_OFFSET_SIZE, AT_lbl (a),
7368                                  debug_macinfo_section, "%s", name);
7369           break;
7370
7371         case dw_val_class_str:
7372           if (AT_string_form (a) == DW_FORM_strp)
7373             dw2_asm_output_offset (DWARF_OFFSET_SIZE,
7374                                    a->dw_attr_val.v.val_str->label,
7375                                    debug_str_section,
7376                                    "%s: \"%s\"", name, AT_string (a));
7377           else
7378             dw2_asm_output_nstring (AT_string (a), -1, "%s", name);
7379           break;
7380
7381         case dw_val_class_file:
7382           {
7383             int f = maybe_emit_file (a->dw_attr_val.v.val_file);
7384
7385             dw2_asm_output_data (constant_size (f), f, "%s (%s)", name,
7386                                  a->dw_attr_val.v.val_file->filename);
7387             break;
7388           }
7389
7390         default:
7391           gcc_unreachable ();
7392         }
7393     }
7394
7395   FOR_EACH_CHILD (die, c, output_die (c));
7396
7397   /* Add null byte to terminate sibling list.  */
7398   if (die->die_child != NULL)
7399     dw2_asm_output_data (1, 0, "end of children of DIE 0x%lx",
7400                          (unsigned long) die->die_offset);
7401 }
7402
7403 /* Output the compilation unit that appears at the beginning of the
7404    .debug_info section, and precedes the DIE descriptions.  */
7405
7406 static void
7407 output_compilation_unit_header (void)
7408 {
7409   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7410     dw2_asm_output_data (4, 0xffffffff,
7411       "Initial length escape value indicating 64-bit DWARF extension");
7412   dw2_asm_output_data (DWARF_OFFSET_SIZE,
7413                        next_die_offset - DWARF_INITIAL_LENGTH_SIZE,
7414                        "Length of Compilation Unit Info");
7415   dw2_asm_output_data (2, DWARF_VERSION, "DWARF version number");
7416   dw2_asm_output_offset (DWARF_OFFSET_SIZE, abbrev_section_label,
7417                          debug_abbrev_section,
7418                          "Offset Into Abbrev. Section");
7419   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Pointer Size (in bytes)");
7420 }
7421
7422 /* Output the compilation unit DIE and its children.  */
7423
7424 static void
7425 output_comp_unit (dw_die_ref die, int output_if_empty)
7426 {
7427   const char *secname;
7428   char *oldsym, *tmp;
7429
7430   /* Unless we are outputting main CU, we may throw away empty ones.  */
7431   if (!output_if_empty && die->die_child == NULL)
7432     return;
7433
7434   /* Even if there are no children of this DIE, we must output the information
7435      about the compilation unit.  Otherwise, on an empty translation unit, we
7436      will generate a present, but empty, .debug_info section.  IRIX 6.5 `nm'
7437      will then complain when examining the file.  First mark all the DIEs in
7438      this CU so we know which get local refs.  */
7439   mark_dies (die);
7440
7441   build_abbrev_table (die);
7442
7443   /* Initialize the beginning DIE offset - and calculate sizes/offsets.  */
7444   next_die_offset = DWARF_COMPILE_UNIT_HEADER_SIZE;
7445   calc_die_sizes (die);
7446
7447   oldsym = die->die_symbol;
7448   if (oldsym)
7449     {
7450       tmp = alloca (strlen (oldsym) + 24);
7451
7452       sprintf (tmp, ".gnu.linkonce.wi.%s", oldsym);
7453       secname = tmp;
7454       die->die_symbol = NULL;
7455       switch_to_section (get_section (secname, SECTION_DEBUG, NULL));
7456     }
7457   else
7458     switch_to_section (debug_info_section);
7459
7460   /* Output debugging information.  */
7461   output_compilation_unit_header ();
7462   output_die (die);
7463
7464   /* Leave the marks on the main CU, so we can check them in
7465      output_pubnames.  */
7466   if (oldsym)
7467     {
7468       unmark_dies (die);
7469       die->die_symbol = oldsym;
7470     }
7471 }
7472
7473 /* Return the DWARF2/3 pubname associated with a decl.  */
7474
7475 static const char *
7476 dwarf2_name (tree decl, int scope)
7477 {
7478   return lang_hooks.dwarf_name (decl, scope ? 1 : 0);
7479 }
7480
7481 /* Add a new entry to .debug_pubnames if appropriate.  */
7482
7483 static void
7484 add_pubname (tree decl, dw_die_ref die)
7485 {
7486   pubname_entry e;
7487
7488   if (! TREE_PUBLIC (decl))
7489     return;
7490
7491   e.die = die;
7492   e.name = xstrdup (dwarf2_name (decl, 1));
7493   VEC_safe_push (pubname_entry, gc, pubname_table, &e);
7494 }
7495
7496 /* Add a new entry to .debug_pubtypes if appropriate.  */
7497
7498 static void
7499 add_pubtype (tree decl, dw_die_ref die)
7500 {
7501   pubname_entry e;
7502
7503   e.name = NULL;
7504   if ((TREE_PUBLIC (decl)
7505        || die->die_parent == comp_unit_die)
7506       && (die->die_tag == DW_TAG_typedef || COMPLETE_TYPE_P (decl)))
7507     {
7508       e.die = die;
7509       if (TYPE_P (decl))
7510         {
7511           if (TYPE_NAME (decl))
7512             {
7513               if (TREE_CODE (TYPE_NAME (decl)) == IDENTIFIER_NODE)
7514                 e.name = IDENTIFIER_POINTER (TYPE_NAME (decl));
7515               else if (TREE_CODE (TYPE_NAME (decl)) == TYPE_DECL
7516                        && DECL_NAME (TYPE_NAME (decl)))
7517                 e.name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (decl)));
7518               else
7519                e.name = xstrdup ((const char *) get_AT_string (die, DW_AT_name));
7520             }
7521         }
7522       else
7523         e.name = xstrdup (dwarf2_name (decl, 1));
7524
7525       /* If we don't have a name for the type, there's no point in adding
7526          it to the table.  */
7527       if (e.name && e.name[0] != '\0')
7528         VEC_safe_push (pubname_entry, gc, pubtype_table, &e);
7529     }
7530 }
7531
7532 /* Output the public names table used to speed up access to externally
7533    visible names; or the public types table used to find type definitions.  */
7534
7535 static void
7536 output_pubnames (VEC (pubname_entry, gc) * names)
7537 {
7538   unsigned i;
7539   unsigned long pubnames_length = size_of_pubnames (names);
7540   pubname_ref pub;
7541
7542   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7543     dw2_asm_output_data (4, 0xffffffff,
7544       "Initial length escape value indicating 64-bit DWARF extension");
7545   if (names == pubname_table)
7546     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7547                          "Length of Public Names Info");
7548   else
7549     dw2_asm_output_data (DWARF_OFFSET_SIZE, pubnames_length,
7550                          "Length of Public Type Names Info");
7551   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7552   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7553                          debug_info_section,
7554                          "Offset of Compilation Unit Info");
7555   dw2_asm_output_data (DWARF_OFFSET_SIZE, next_die_offset,
7556                        "Compilation Unit Length");
7557
7558   for (i = 0; VEC_iterate (pubname_entry, names, i, pub); i++)
7559     {
7560       /* We shouldn't see pubnames for DIEs outside of the main CU.  */
7561       if (names == pubname_table)
7562         gcc_assert (pub->die->die_mark);
7563
7564       if (names != pubtype_table
7565           || pub->die->die_offset != 0
7566           || !flag_eliminate_unused_debug_types)
7567         {
7568           dw2_asm_output_data (DWARF_OFFSET_SIZE, pub->die->die_offset,
7569                                "DIE offset");
7570
7571           dw2_asm_output_nstring (pub->name, -1, "external name");
7572         }
7573     }
7574
7575   dw2_asm_output_data (DWARF_OFFSET_SIZE, 0, NULL);
7576 }
7577
7578 /* Add a new entry to .debug_aranges if appropriate.  */
7579
7580 static void
7581 add_arange (tree decl, dw_die_ref die)
7582 {
7583   if (! DECL_SECTION_NAME (decl))
7584     return;
7585
7586   if (arange_table_in_use == arange_table_allocated)
7587     {
7588       arange_table_allocated += ARANGE_TABLE_INCREMENT;
7589       arange_table = ggc_realloc (arange_table,
7590                                   (arange_table_allocated
7591                                    * sizeof (dw_die_ref)));
7592       memset (arange_table + arange_table_in_use, 0,
7593               ARANGE_TABLE_INCREMENT * sizeof (dw_die_ref));
7594     }
7595
7596   arange_table[arange_table_in_use++] = die;
7597 }
7598
7599 /* Output the information that goes into the .debug_aranges table.
7600    Namely, define the beginning and ending address range of the
7601    text section generated for this compilation unit.  */
7602
7603 static void
7604 output_aranges (void)
7605 {
7606   unsigned i;
7607   unsigned long aranges_length = size_of_aranges ();
7608
7609   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
7610     dw2_asm_output_data (4, 0xffffffff,
7611       "Initial length escape value indicating 64-bit DWARF extension");
7612   dw2_asm_output_data (DWARF_OFFSET_SIZE, aranges_length,
7613                        "Length of Address Ranges Info");
7614   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
7615   dw2_asm_output_offset (DWARF_OFFSET_SIZE, debug_info_section_label,
7616                          debug_info_section,
7617                          "Offset of Compilation Unit Info");
7618   dw2_asm_output_data (1, DWARF2_ADDR_SIZE, "Size of Address");
7619   dw2_asm_output_data (1, 0, "Size of Segment Descriptor");
7620
7621   /* We need to align to twice the pointer size here.  */
7622   if (DWARF_ARANGES_PAD_SIZE)
7623     {
7624       /* Pad using a 2 byte words so that padding is correct for any
7625          pointer size.  */
7626       dw2_asm_output_data (2, 0, "Pad to %d byte boundary",
7627                            2 * DWARF2_ADDR_SIZE);
7628       for (i = 2; i < (unsigned) DWARF_ARANGES_PAD_SIZE; i += 2)
7629         dw2_asm_output_data (2, 0, NULL);
7630     }
7631
7632   /* It is necessary not to output these entries if the sections were
7633      not used; if the sections were not used, the length will be 0 and
7634      the address may end up as 0 if the section is discarded by ld
7635      --gc-sections, leaving an invalid (0, 0) entry that can be
7636      confused with the terminator.  */
7637   if (text_section_used)
7638     {
7639       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_section_label, "Address");
7640       dw2_asm_output_delta (DWARF2_ADDR_SIZE, text_end_label,
7641                             text_section_label, "Length");
7642     }
7643   if (cold_text_section_used)
7644     {
7645       dw2_asm_output_addr (DWARF2_ADDR_SIZE, cold_text_section_label,
7646                            "Address");
7647       dw2_asm_output_delta (DWARF2_ADDR_SIZE, cold_end_label,
7648                             cold_text_section_label, "Length");
7649     }
7650
7651   for (i = 0; i < arange_table_in_use; i++)
7652     {
7653       dw_die_ref die = arange_table[i];
7654
7655       /* We shouldn't see aranges for DIEs outside of the main CU.  */
7656       gcc_assert (die->die_mark);
7657
7658       if (die->die_tag == DW_TAG_subprogram)
7659         {
7660           dw2_asm_output_addr (DWARF2_ADDR_SIZE, get_AT_low_pc (die),
7661                                "Address");
7662           dw2_asm_output_delta (DWARF2_ADDR_SIZE, get_AT_hi_pc (die),
7663                                 get_AT_low_pc (die), "Length");
7664         }
7665       else
7666         {
7667           /* A static variable; extract the symbol from DW_AT_location.
7668              Note that this code isn't currently hit, as we only emit
7669              aranges for functions (jason 9/23/99).  */
7670           dw_attr_ref a = get_AT (die, DW_AT_location);
7671           dw_loc_descr_ref loc;
7672
7673           gcc_assert (a && AT_class (a) == dw_val_class_loc);
7674
7675           loc = AT_loc (a);
7676           gcc_assert (loc->dw_loc_opc == DW_OP_addr);
7677
7678           dw2_asm_output_addr_rtx (DWARF2_ADDR_SIZE,
7679                                    loc->dw_loc_oprnd1.v.val_addr, "Address");
7680           dw2_asm_output_data (DWARF2_ADDR_SIZE,
7681                                get_AT_unsigned (die, DW_AT_byte_size),
7682                                "Length");
7683         }
7684     }
7685
7686   /* Output the terminator words.  */
7687   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7688   dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7689 }
7690
7691 /* Add a new entry to .debug_ranges.  Return the offset at which it
7692    was placed.  */
7693
7694 static unsigned int
7695 add_ranges_num (int num)
7696 {
7697   unsigned int in_use = ranges_table_in_use;
7698
7699   if (in_use == ranges_table_allocated)
7700     {
7701       ranges_table_allocated += RANGES_TABLE_INCREMENT;
7702       ranges_table
7703         = ggc_realloc (ranges_table, (ranges_table_allocated
7704                                       * sizeof (struct dw_ranges_struct)));
7705       memset (ranges_table + ranges_table_in_use, 0,
7706               RANGES_TABLE_INCREMENT * sizeof (struct dw_ranges_struct));
7707     }
7708
7709   ranges_table[in_use].num = num;
7710   ranges_table_in_use = in_use + 1;
7711
7712   return in_use * 2 * DWARF2_ADDR_SIZE;
7713 }
7714
7715 /* Add a new entry to .debug_ranges corresponding to a block, or a
7716    range terminator if BLOCK is NULL.  */
7717
7718 static unsigned int
7719 add_ranges (const_tree block)
7720 {
7721   return add_ranges_num (block ? BLOCK_NUMBER (block) : 0);
7722 }
7723
7724 /* Add a new entry to .debug_ranges corresponding to a pair of
7725    labels.  */
7726
7727 static unsigned int
7728 add_ranges_by_labels (const char *begin, const char *end)
7729 {
7730   unsigned int in_use = ranges_by_label_in_use;
7731
7732   if (in_use == ranges_by_label_allocated)
7733     {
7734       ranges_by_label_allocated += RANGES_TABLE_INCREMENT;
7735       ranges_by_label
7736         = ggc_realloc (ranges_by_label,
7737                        (ranges_by_label_allocated
7738                         * sizeof (struct dw_ranges_by_label_struct)));
7739       memset (ranges_by_label + ranges_by_label_in_use, 0,
7740               RANGES_TABLE_INCREMENT
7741               * sizeof (struct dw_ranges_by_label_struct));
7742     }
7743
7744   ranges_by_label[in_use].begin = begin;
7745   ranges_by_label[in_use].end = end;
7746   ranges_by_label_in_use = in_use + 1;
7747
7748   return add_ranges_num (-(int)in_use - 1);
7749 }
7750
7751 static void
7752 output_ranges (void)
7753 {
7754   unsigned i;
7755   static const char *const start_fmt = "Offset 0x%x";
7756   const char *fmt = start_fmt;
7757
7758   for (i = 0; i < ranges_table_in_use; i++)
7759     {
7760       int block_num = ranges_table[i].num;
7761
7762       if (block_num > 0)
7763         {
7764           char blabel[MAX_ARTIFICIAL_LABEL_BYTES];
7765           char elabel[MAX_ARTIFICIAL_LABEL_BYTES];
7766
7767           ASM_GENERATE_INTERNAL_LABEL (blabel, BLOCK_BEGIN_LABEL, block_num);
7768           ASM_GENERATE_INTERNAL_LABEL (elabel, BLOCK_END_LABEL, block_num);
7769
7770           /* If all code is in the text section, then the compilation
7771              unit base address defaults to DW_AT_low_pc, which is the
7772              base of the text section.  */
7773           if (!have_multiple_function_sections)
7774             {
7775               dw2_asm_output_delta (DWARF2_ADDR_SIZE, blabel,
7776                                     text_section_label,
7777                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7778               dw2_asm_output_delta (DWARF2_ADDR_SIZE, elabel,
7779                                     text_section_label, NULL);
7780             }
7781
7782           /* Otherwise, the compilation unit base address is zero,
7783              which allows us to use absolute addresses, and not worry
7784              about whether the target supports cross-section
7785              arithmetic.  */
7786           else
7787             {
7788               dw2_asm_output_addr (DWARF2_ADDR_SIZE, blabel,
7789                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7790               dw2_asm_output_addr (DWARF2_ADDR_SIZE, elabel, NULL);
7791             }
7792
7793           fmt = NULL;
7794         }
7795
7796       /* Negative block_num stands for an index into ranges_by_label.  */
7797       else if (block_num < 0)
7798         {
7799           int lab_idx = - block_num - 1;
7800
7801           if (!have_multiple_function_sections)
7802             {
7803               gcc_unreachable ();
7804 #if 0
7805               /* If we ever use add_ranges_by_labels () for a single
7806                  function section, all we have to do is to take out
7807                  the #if 0 above.  */
7808               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7809                                     ranges_by_label[lab_idx].begin,
7810                                     text_section_label,
7811                                     fmt, i * 2 * DWARF2_ADDR_SIZE);
7812               dw2_asm_output_delta (DWARF2_ADDR_SIZE,
7813                                     ranges_by_label[lab_idx].end,
7814                                     text_section_label, NULL);
7815 #endif
7816             }
7817           else
7818             {
7819               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7820                                    ranges_by_label[lab_idx].begin,
7821                                    fmt, i * 2 * DWARF2_ADDR_SIZE);
7822               dw2_asm_output_addr (DWARF2_ADDR_SIZE,
7823                                    ranges_by_label[lab_idx].end,
7824                                    NULL);
7825             }
7826         }
7827       else
7828         {
7829           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7830           dw2_asm_output_data (DWARF2_ADDR_SIZE, 0, NULL);
7831           fmt = start_fmt;
7832         }
7833     }
7834 }
7835
7836 /* Data structure containing information about input files.  */
7837 struct file_info
7838 {
7839   const char *path;     /* Complete file name.  */
7840   const char *fname;    /* File name part.  */
7841   int length;           /* Length of entire string.  */
7842   struct dwarf_file_data * file_idx;    /* Index in input file table.  */
7843   int dir_idx;          /* Index in directory table.  */
7844 };
7845
7846 /* Data structure containing information about directories with source
7847    files.  */
7848 struct dir_info
7849 {
7850   const char *path;     /* Path including directory name.  */
7851   int length;           /* Path length.  */
7852   int prefix;           /* Index of directory entry which is a prefix.  */
7853   int count;            /* Number of files in this directory.  */
7854   int dir_idx;          /* Index of directory used as base.  */
7855 };
7856
7857 /* Callback function for file_info comparison.  We sort by looking at
7858    the directories in the path.  */
7859
7860 static int
7861 file_info_cmp (const void *p1, const void *p2)
7862 {
7863   const struct file_info *s1 = p1;
7864   const struct file_info *s2 = p2;
7865   const unsigned char *cp1;
7866   const unsigned char *cp2;
7867
7868   /* Take care of file names without directories.  We need to make sure that
7869      we return consistent values to qsort since some will get confused if
7870      we return the same value when identical operands are passed in opposite
7871      orders.  So if neither has a directory, return 0 and otherwise return
7872      1 or -1 depending on which one has the directory.  */
7873   if ((s1->path == s1->fname || s2->path == s2->fname))
7874     return (s2->path == s2->fname) - (s1->path == s1->fname);
7875
7876   cp1 = (const unsigned char *) s1->path;
7877   cp2 = (const unsigned char *) s2->path;
7878
7879   while (1)
7880     {
7881       ++cp1;
7882       ++cp2;
7883       /* Reached the end of the first path?  If so, handle like above.  */
7884       if ((cp1 == (const unsigned char *) s1->fname)
7885           || (cp2 == (const unsigned char *) s2->fname))
7886         return ((cp2 == (const unsigned char *) s2->fname)
7887                 - (cp1 == (const unsigned char *) s1->fname));
7888
7889       /* Character of current path component the same?  */
7890       else if (*cp1 != *cp2)
7891         return *cp1 - *cp2;
7892     }
7893 }
7894
7895 struct file_name_acquire_data
7896 {
7897   struct file_info *files;
7898   int used_files;
7899   int max_files;
7900 };
7901
7902 /* Traversal function for the hash table.  */
7903
7904 static int
7905 file_name_acquire (void ** slot, void *data)
7906 {
7907   struct file_name_acquire_data *fnad = data;
7908   struct dwarf_file_data *d = *slot;
7909   struct file_info *fi;
7910   const char *f;
7911
7912   gcc_assert (fnad->max_files >= d->emitted_number);
7913
7914   if (! d->emitted_number)
7915     return 1;
7916
7917   gcc_assert (fnad->max_files != fnad->used_files);
7918
7919   fi = fnad->files + fnad->used_files++;
7920
7921   /* Skip all leading "./".  */
7922   f = d->filename;
7923   while (f[0] == '.' && IS_DIR_SEPARATOR (f[1]))
7924     f += 2;
7925
7926   /* Create a new array entry.  */
7927   fi->path = f;
7928   fi->length = strlen (f);
7929   fi->file_idx = d;
7930
7931   /* Search for the file name part.  */
7932   f = strrchr (f, DIR_SEPARATOR);
7933 #if defined (DIR_SEPARATOR_2)
7934   {
7935     char *g = strrchr (fi->path, DIR_SEPARATOR_2);
7936
7937     if (g != NULL)
7938       {
7939         if (f == NULL || f < g)
7940           f = g;
7941       }
7942   }
7943 #endif
7944
7945   fi->fname = f == NULL ? fi->path : f + 1;
7946   return 1;
7947 }
7948
7949 /* Output the directory table and the file name table.  We try to minimize
7950    the total amount of memory needed.  A heuristic is used to avoid large
7951    slowdowns with many input files.  */
7952
7953 static void
7954 output_file_names (void)
7955 {
7956   struct file_name_acquire_data fnad;
7957   int numfiles;
7958   struct file_info *files;
7959   struct dir_info *dirs;
7960   int *saved;
7961   int *savehere;
7962   int *backmap;
7963   int ndirs;
7964   int idx_offset;
7965   int i;
7966   int idx;
7967
7968   if (!last_emitted_file)
7969     {
7970       dw2_asm_output_data (1, 0, "End directory table");
7971       dw2_asm_output_data (1, 0, "End file name table");
7972       return;
7973     }
7974
7975   numfiles = last_emitted_file->emitted_number;
7976
7977   /* Allocate the various arrays we need.  */
7978   files = alloca (numfiles * sizeof (struct file_info));
7979   dirs = alloca (numfiles * sizeof (struct dir_info));
7980
7981   fnad.files = files;
7982   fnad.used_files = 0;
7983   fnad.max_files = numfiles;
7984   htab_traverse (file_table, file_name_acquire, &fnad);
7985   gcc_assert (fnad.used_files == fnad.max_files);
7986
7987   qsort (files, numfiles, sizeof (files[0]), file_info_cmp);
7988
7989   /* Find all the different directories used.  */
7990   dirs[0].path = files[0].path;
7991   dirs[0].length = files[0].fname - files[0].path;
7992   dirs[0].prefix = -1;
7993   dirs[0].count = 1;
7994   dirs[0].dir_idx = 0;
7995   files[0].dir_idx = 0;
7996   ndirs = 1;
7997
7998   for (i = 1; i < numfiles; i++)
7999     if (files[i].fname - files[i].path == dirs[ndirs - 1].length
8000         && memcmp (dirs[ndirs - 1].path, files[i].path,
8001                    dirs[ndirs - 1].length) == 0)
8002       {
8003         /* Same directory as last entry.  */
8004         files[i].dir_idx = ndirs - 1;
8005         ++dirs[ndirs - 1].count;
8006       }
8007     else
8008       {
8009         int j;
8010
8011         /* This is a new directory.  */
8012         dirs[ndirs].path = files[i].path;
8013         dirs[ndirs].length = files[i].fname - files[i].path;
8014         dirs[ndirs].count = 1;
8015         dirs[ndirs].dir_idx = ndirs;
8016         files[i].dir_idx = ndirs;
8017
8018         /* Search for a prefix.  */
8019         dirs[ndirs].prefix = -1;
8020         for (j = 0; j < ndirs; j++)
8021           if (dirs[j].length < dirs[ndirs].length
8022               && dirs[j].length > 1
8023               && (dirs[ndirs].prefix == -1
8024                   || dirs[j].length > dirs[dirs[ndirs].prefix].length)
8025               && memcmp (dirs[j].path, dirs[ndirs].path, dirs[j].length) == 0)
8026             dirs[ndirs].prefix = j;
8027
8028         ++ndirs;
8029       }
8030
8031   /* Now to the actual work.  We have to find a subset of the directories which
8032      allow expressing the file name using references to the directory table
8033      with the least amount of characters.  We do not do an exhaustive search
8034      where we would have to check out every combination of every single
8035      possible prefix.  Instead we use a heuristic which provides nearly optimal
8036      results in most cases and never is much off.  */
8037   saved = alloca (ndirs * sizeof (int));
8038   savehere = alloca (ndirs * sizeof (int));
8039
8040   memset (saved, '\0', ndirs * sizeof (saved[0]));
8041   for (i = 0; i < ndirs; i++)
8042     {
8043       int j;
8044       int total;
8045
8046       /* We can always save some space for the current directory.  But this
8047          does not mean it will be enough to justify adding the directory.  */
8048       savehere[i] = dirs[i].length;
8049       total = (savehere[i] - saved[i]) * dirs[i].count;
8050
8051       for (j = i + 1; j < ndirs; j++)
8052         {
8053           savehere[j] = 0;
8054           if (saved[j] < dirs[i].length)
8055             {
8056               /* Determine whether the dirs[i] path is a prefix of the
8057                  dirs[j] path.  */
8058               int k;
8059
8060               k = dirs[j].prefix;
8061               while (k != -1 && k != (int) i)
8062                 k = dirs[k].prefix;
8063
8064               if (k == (int) i)
8065                 {
8066                   /* Yes it is.  We can possibly save some memory by
8067                      writing the filenames in dirs[j] relative to
8068                      dirs[i].  */
8069                   savehere[j] = dirs[i].length;
8070                   total += (savehere[j] - saved[j]) * dirs[j].count;
8071                 }
8072             }
8073         }
8074
8075       /* Check whether we can save enough to justify adding the dirs[i]
8076          directory.  */
8077       if (total > dirs[i].length + 1)
8078         {
8079           /* It's worthwhile adding.  */
8080           for (j = i; j < ndirs; j++)
8081             if (savehere[j] > 0)
8082               {
8083                 /* Remember how much we saved for this directory so far.  */
8084                 saved[j] = savehere[j];
8085
8086                 /* Remember the prefix directory.  */
8087                 dirs[j].dir_idx = i;
8088               }
8089         }
8090     }
8091
8092   /* Emit the directory name table.  */
8093   idx = 1;
8094   idx_offset = dirs[0].length > 0 ? 1 : 0;
8095   for (i = 1 - idx_offset; i < ndirs; i++)
8096     dw2_asm_output_nstring (dirs[i].path, dirs[i].length - 1,
8097                             "Directory Entry: 0x%x", i + idx_offset);
8098
8099   dw2_asm_output_data (1, 0, "End directory table");
8100
8101   /* We have to emit them in the order of emitted_number since that's
8102      used in the debug info generation.  To do this efficiently we
8103      generate a back-mapping of the indices first.  */
8104   backmap = alloca (numfiles * sizeof (int));
8105   for (i = 0; i < numfiles; i++)
8106     backmap[files[i].file_idx->emitted_number - 1] = i;
8107
8108   /* Now write all the file names.  */
8109   for (i = 0; i < numfiles; i++)
8110     {
8111       int file_idx = backmap[i];
8112       int dir_idx = dirs[files[file_idx].dir_idx].dir_idx;
8113
8114       dw2_asm_output_nstring (files[file_idx].path + dirs[dir_idx].length, -1,
8115                               "File Entry: 0x%x", (unsigned) i + 1);
8116
8117       /* Include directory index.  */
8118       dw2_asm_output_data_uleb128 (dir_idx + idx_offset, NULL);
8119
8120       /* Modification time.  */
8121       dw2_asm_output_data_uleb128 (0, NULL);
8122
8123       /* File length in bytes.  */
8124       dw2_asm_output_data_uleb128 (0, NULL);
8125     }
8126
8127   dw2_asm_output_data (1, 0, "End file name table");
8128 }
8129
8130
8131 /* Output the source line number correspondence information.  This
8132    information goes into the .debug_line section.  */
8133
8134 static void
8135 output_line_info (void)
8136 {
8137   char l1[20], l2[20], p1[20], p2[20];
8138   char line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8139   char prev_line_label[MAX_ARTIFICIAL_LABEL_BYTES];
8140   unsigned opc;
8141   unsigned n_op_args;
8142   unsigned long lt_index;
8143   unsigned long current_line;
8144   long line_offset;
8145   long line_delta;
8146   unsigned long current_file;
8147   unsigned long function;
8148
8149   ASM_GENERATE_INTERNAL_LABEL (l1, LINE_NUMBER_BEGIN_LABEL, 0);
8150   ASM_GENERATE_INTERNAL_LABEL (l2, LINE_NUMBER_END_LABEL, 0);
8151   ASM_GENERATE_INTERNAL_LABEL (p1, LN_PROLOG_AS_LABEL, 0);
8152   ASM_GENERATE_INTERNAL_LABEL (p2, LN_PROLOG_END_LABEL, 0);
8153
8154   if (DWARF_INITIAL_LENGTH_SIZE - DWARF_OFFSET_SIZE == 4)
8155     dw2_asm_output_data (4, 0xffffffff,
8156       "Initial length escape value indicating 64-bit DWARF extension");
8157   dw2_asm_output_delta (DWARF_OFFSET_SIZE, l2, l1,
8158                         "Length of Source Line Info");
8159   ASM_OUTPUT_LABEL (asm_out_file, l1);
8160
8161   dw2_asm_output_data (2, DWARF_VERSION, "DWARF Version");
8162   dw2_asm_output_delta (DWARF_OFFSET_SIZE, p2, p1, "Prolog Length");
8163   ASM_OUTPUT_LABEL (asm_out_file, p1);
8164
8165   /* Define the architecture-dependent minimum instruction length (in
8166    bytes).  In this implementation of DWARF, this field is used for
8167    information purposes only.  Since GCC generates assembly language,
8168    we have no a priori knowledge of how many instruction bytes are
8169    generated for each source line, and therefore can use only the
8170    DW_LNE_set_address and DW_LNS_fixed_advance_pc line information
8171    commands.  Accordingly, we fix this as `1', which is "correct
8172    enough" for all architectures, and don't let the target override.  */
8173   dw2_asm_output_data (1, 1,
8174                        "Minimum Instruction Length");
8175
8176   dw2_asm_output_data (1, DWARF_LINE_DEFAULT_IS_STMT_START,
8177                        "Default is_stmt_start flag");
8178   dw2_asm_output_data (1, DWARF_LINE_BASE,
8179                        "Line Base Value (Special Opcodes)");
8180   dw2_asm_output_data (1, DWARF_LINE_RANGE,
8181                        "Line Range Value (Special Opcodes)");
8182   dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE,
8183                        "Special Opcode Base");
8184
8185   for (opc = 1; opc < DWARF_LINE_OPCODE_BASE; opc++)
8186     {
8187       switch (opc)
8188         {
8189         case DW_LNS_advance_pc:
8190         case DW_LNS_advance_line:
8191         case DW_LNS_set_file:
8192         case DW_LNS_set_column:
8193         case DW_LNS_fixed_advance_pc:
8194           n_op_args = 1;
8195           break;
8196         default:
8197           n_op_args = 0;
8198           break;
8199         }
8200
8201       dw2_asm_output_data (1, n_op_args, "opcode: 0x%x has %d args",
8202                            opc, n_op_args);
8203     }
8204
8205   /* Write out the information about the files we use.  */
8206   output_file_names ();
8207   ASM_OUTPUT_LABEL (asm_out_file, p2);
8208
8209   /* We used to set the address register to the first location in the text
8210      section here, but that didn't accomplish anything since we already
8211      have a line note for the opening brace of the first function.  */
8212
8213   /* Generate the line number to PC correspondence table, encoded as
8214      a series of state machine operations.  */
8215   current_file = 1;
8216   current_line = 1;
8217
8218   if (cfun && in_cold_section_p)
8219     strcpy (prev_line_label, cfun->cold_section_label);
8220   else
8221     strcpy (prev_line_label, text_section_label);
8222   for (lt_index = 1; lt_index < line_info_table_in_use; ++lt_index)
8223     {
8224       dw_line_info_ref line_info = &line_info_table[lt_index];
8225
8226 #if 0
8227       /* Disable this optimization for now; GDB wants to see two line notes
8228          at the beginning of a function so it can find the end of the
8229          prologue.  */
8230
8231       /* Don't emit anything for redundant notes.  Just updating the
8232          address doesn't accomplish anything, because we already assume
8233          that anything after the last address is this line.  */
8234       if (line_info->dw_line_num == current_line
8235           && line_info->dw_file_num == current_file)
8236         continue;
8237 #endif
8238
8239       /* Emit debug info for the address of the current line.
8240
8241          Unfortunately, we have little choice here currently, and must always
8242          use the most general form.  GCC does not know the address delta
8243          itself, so we can't use DW_LNS_advance_pc.  Many ports do have length
8244          attributes which will give an upper bound on the address range.  We
8245          could perhaps use length attributes to determine when it is safe to
8246          use DW_LNS_fixed_advance_pc.  */
8247
8248       ASM_GENERATE_INTERNAL_LABEL (line_label, LINE_CODE_LABEL, lt_index);
8249       if (0)
8250         {
8251           /* This can handle deltas up to 0xffff.  This takes 3 bytes.  */
8252           dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8253                                "DW_LNS_fixed_advance_pc");
8254           dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8255         }
8256       else
8257         {
8258           /* This can handle any delta.  This takes
8259              4+DWARF2_ADDR_SIZE bytes.  */
8260           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8261           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8262           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8263           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8264         }
8265
8266       strcpy (prev_line_label, line_label);
8267
8268       /* Emit debug info for the source file of the current line, if
8269          different from the previous line.  */
8270       if (line_info->dw_file_num != current_file)
8271         {
8272           current_file = line_info->dw_file_num;
8273           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8274           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8275         }
8276
8277       /* Emit debug info for the current line number, choosing the encoding
8278          that uses the least amount of space.  */
8279       if (line_info->dw_line_num != current_line)
8280         {
8281           line_offset = line_info->dw_line_num - current_line;
8282           line_delta = line_offset - DWARF_LINE_BASE;
8283           current_line = line_info->dw_line_num;
8284           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8285             /* This can handle deltas from -10 to 234, using the current
8286                definitions of DWARF_LINE_BASE and DWARF_LINE_RANGE.  This
8287                takes 1 byte.  */
8288             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8289                                  "line %lu", current_line);
8290           else
8291             {
8292               /* This can handle any delta.  This takes at least 4 bytes,
8293                  depending on the value being encoded.  */
8294               dw2_asm_output_data (1, DW_LNS_advance_line,
8295                                    "advance to line %lu", current_line);
8296               dw2_asm_output_data_sleb128 (line_offset, NULL);
8297               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8298             }
8299         }
8300       else
8301         /* We still need to start a new row, so output a copy insn.  */
8302         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8303     }
8304
8305   /* Emit debug info for the address of the end of the function.  */
8306   if (0)
8307     {
8308       dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8309                            "DW_LNS_fixed_advance_pc");
8310       dw2_asm_output_delta (2, text_end_label, prev_line_label, NULL);
8311     }
8312   else
8313     {
8314       dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8315       dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8316       dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8317       dw2_asm_output_addr (DWARF2_ADDR_SIZE, text_end_label, NULL);
8318     }
8319
8320   dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8321   dw2_asm_output_data_uleb128 (1, NULL);
8322   dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8323
8324   function = 0;
8325   current_file = 1;
8326   current_line = 1;
8327   for (lt_index = 0; lt_index < separate_line_info_table_in_use;)
8328     {
8329       dw_separate_line_info_ref line_info
8330         = &separate_line_info_table[lt_index];
8331
8332 #if 0
8333       /* Don't emit anything for redundant notes.  */
8334       if (line_info->dw_line_num == current_line
8335           && line_info->dw_file_num == current_file
8336           && line_info->function == function)
8337         goto cont;
8338 #endif
8339
8340       /* Emit debug info for the address of the current line.  If this is
8341          a new function, or the first line of a function, then we need
8342          to handle it differently.  */
8343       ASM_GENERATE_INTERNAL_LABEL (line_label, SEPARATE_LINE_CODE_LABEL,
8344                                    lt_index);
8345       if (function != line_info->function)
8346         {
8347           function = line_info->function;
8348
8349           /* Set the address register to the first line in the function.  */
8350           dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8351           dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8352           dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8353           dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8354         }
8355       else
8356         {
8357           /* ??? See the DW_LNS_advance_pc comment above.  */
8358           if (0)
8359             {
8360               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8361                                    "DW_LNS_fixed_advance_pc");
8362               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8363             }
8364           else
8365             {
8366               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8367               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8368               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8369               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8370             }
8371         }
8372
8373       strcpy (prev_line_label, line_label);
8374
8375       /* Emit debug info for the source file of the current line, if
8376          different from the previous line.  */
8377       if (line_info->dw_file_num != current_file)
8378         {
8379           current_file = line_info->dw_file_num;
8380           dw2_asm_output_data (1, DW_LNS_set_file, "DW_LNS_set_file");
8381           dw2_asm_output_data_uleb128 (current_file, "%lu", current_file);
8382         }
8383
8384       /* Emit debug info for the current line number, choosing the encoding
8385          that uses the least amount of space.  */
8386       if (line_info->dw_line_num != current_line)
8387         {
8388           line_offset = line_info->dw_line_num - current_line;
8389           line_delta = line_offset - DWARF_LINE_BASE;
8390           current_line = line_info->dw_line_num;
8391           if (line_delta >= 0 && line_delta < (DWARF_LINE_RANGE - 1))
8392             dw2_asm_output_data (1, DWARF_LINE_OPCODE_BASE + line_delta,
8393                                  "line %lu", current_line);
8394           else
8395             {
8396               dw2_asm_output_data (1, DW_LNS_advance_line,
8397                                    "advance to line %lu", current_line);
8398               dw2_asm_output_data_sleb128 (line_offset, NULL);
8399               dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8400             }
8401         }
8402       else
8403         dw2_asm_output_data (1, DW_LNS_copy, "DW_LNS_copy");
8404
8405 #if 0
8406     cont:
8407 #endif
8408
8409       lt_index++;
8410
8411       /* If we're done with a function, end its sequence.  */
8412       if (lt_index == separate_line_info_table_in_use
8413           || separate_line_info_table[lt_index].function != function)
8414         {
8415           current_file = 1;
8416           current_line = 1;
8417
8418           /* Emit debug info for the address of the end of the function.  */
8419           ASM_GENERATE_INTERNAL_LABEL (line_label, FUNC_END_LABEL, function);
8420           if (0)
8421             {
8422               dw2_asm_output_data (1, DW_LNS_fixed_advance_pc,
8423                                    "DW_LNS_fixed_advance_pc");
8424               dw2_asm_output_delta (2, line_label, prev_line_label, NULL);
8425             }
8426           else
8427             {
8428               dw2_asm_output_data (1, 0, "DW_LNE_set_address");
8429               dw2_asm_output_data_uleb128 (1 + DWARF2_ADDR_SIZE, NULL);
8430               dw2_asm_output_data (1, DW_LNE_set_address, NULL);
8431               dw2_asm_output_addr (DWARF2_ADDR_SIZE, line_label, NULL);
8432             }
8433
8434           /* Output the marker for the end of this sequence.  */
8435           dw2_asm_output_data (1, 0, "DW_LNE_end_sequence");
8436           dw2_asm_output_data_uleb128 (1, NULL);
8437           dw2_asm_output_data (1, DW_LNE_end_sequence, NULL);
8438         }
8439     }
8440
8441   /* Output the marker for the end of the line number info.  */
8442   ASM_OUTPUT_LABEL (asm_out_file, l2);
8443 }
8444 \f
8445 /* Given a pointer to a tree node for some base type, return a pointer to
8446    a DIE that describes the given type.
8447
8448    This routine must only be called for GCC type nodes that correspond to
8449    Dwarf base (fundamental) types.  */
8450
8451 static dw_die_ref
8452 base_type_die (tree type)
8453 {
8454   dw_die_ref base_type_result;
8455   enum dwarf_type encoding;
8456
8457   if (TREE_CODE (type) == ERROR_MARK || TREE_CODE (type) == VOID_TYPE)
8458     return 0;
8459
8460   switch (TREE_CODE (type))
8461     {
8462     case INTEGER_TYPE:
8463       if (TYPE_STRING_FLAG (type))
8464         {
8465           if (TYPE_UNSIGNED (type))
8466             encoding = DW_ATE_unsigned_char;
8467           else
8468             encoding = DW_ATE_signed_char;
8469         }
8470       else if (TYPE_UNSIGNED (type))
8471         encoding = DW_ATE_unsigned;
8472       else
8473         encoding = DW_ATE_signed;
8474       break;
8475
8476     case REAL_TYPE:
8477       if (DECIMAL_FLOAT_MODE_P (TYPE_MODE (type)))
8478         encoding = DW_ATE_decimal_float;
8479       else
8480         encoding = DW_ATE_float;
8481       break;
8482
8483     case FIXED_POINT_TYPE:
8484       if (TYPE_UNSIGNED (type))
8485         encoding = DW_ATE_unsigned_fixed;
8486       else
8487         encoding = DW_ATE_signed_fixed;
8488       break;
8489
8490       /* Dwarf2 doesn't know anything about complex ints, so use
8491          a user defined type for it.  */
8492     case COMPLEX_TYPE:
8493       if (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE)
8494         encoding = DW_ATE_complex_float;
8495       else
8496         encoding = DW_ATE_lo_user;
8497       break;
8498
8499     case BOOLEAN_TYPE:
8500       /* GNU FORTRAN/Ada/C++ BOOLEAN type.  */
8501       encoding = DW_ATE_boolean;
8502       break;
8503
8504     default:
8505       /* No other TREE_CODEs are Dwarf fundamental types.  */
8506       gcc_unreachable ();
8507     }
8508
8509   base_type_result = new_die (DW_TAG_base_type, comp_unit_die, type);
8510
8511   /* This probably indicates a bug.  */
8512   if (! TYPE_NAME (type))
8513     add_name_attribute (base_type_result, "__unknown__");
8514
8515   add_AT_unsigned (base_type_result, DW_AT_byte_size,
8516                    int_size_in_bytes (type));
8517   add_AT_unsigned (base_type_result, DW_AT_encoding, encoding);
8518
8519   return base_type_result;
8520 }
8521
8522 /* Given a pointer to an arbitrary ..._TYPE tree node, return nonzero if the
8523    given input type is a Dwarf "fundamental" type.  Otherwise return null.  */
8524
8525 static inline int
8526 is_base_type (tree type)
8527 {
8528   switch (TREE_CODE (type))
8529     {
8530     case ERROR_MARK:
8531     case VOID_TYPE:
8532     case INTEGER_TYPE:
8533     case REAL_TYPE:
8534     case FIXED_POINT_TYPE:
8535     case COMPLEX_TYPE:
8536     case BOOLEAN_TYPE:
8537       return 1;
8538
8539     case ARRAY_TYPE:
8540     case RECORD_TYPE:
8541     case UNION_TYPE:
8542     case QUAL_UNION_TYPE:
8543     case ENUMERAL_TYPE:
8544     case FUNCTION_TYPE:
8545     case METHOD_TYPE:
8546     case POINTER_TYPE:
8547     case REFERENCE_TYPE:
8548     case OFFSET_TYPE:
8549     case LANG_TYPE:
8550     case VECTOR_TYPE:
8551       return 0;
8552
8553     default:
8554       gcc_unreachable ();
8555     }
8556
8557   return 0;
8558 }
8559
8560 /* Given a pointer to a tree node, assumed to be some kind of a ..._TYPE
8561    node, return the size in bits for the type if it is a constant, or else
8562    return the alignment for the type if the type's size is not constant, or
8563    else return BITS_PER_WORD if the type actually turns out to be an
8564    ERROR_MARK node.  */
8565
8566 static inline unsigned HOST_WIDE_INT
8567 simple_type_size_in_bits (const_tree type)
8568 {
8569   if (TREE_CODE (type) == ERROR_MARK)
8570     return BITS_PER_WORD;
8571   else if (TYPE_SIZE (type) == NULL_TREE)
8572     return 0;
8573   else if (host_integerp (TYPE_SIZE (type), 1))
8574     return tree_low_cst (TYPE_SIZE (type), 1);
8575   else
8576     return TYPE_ALIGN (type);
8577 }
8578
8579 /* Return true if the debug information for the given type should be
8580    emitted as a subrange type.  */
8581
8582 static inline bool
8583 is_subrange_type (const_tree type)
8584 {
8585   tree subtype = TREE_TYPE (type);
8586
8587   /* Subrange types are identified by the fact that they are integer
8588      types, and that they have a subtype which is either an integer type
8589      or an enumeral type.  */
8590
8591   if (TREE_CODE (type) != INTEGER_TYPE
8592       || subtype == NULL_TREE)
8593     return false;
8594
8595   if (TREE_CODE (subtype) != INTEGER_TYPE
8596       && TREE_CODE (subtype) != ENUMERAL_TYPE)
8597     return false;
8598
8599   if (TREE_CODE (type) == TREE_CODE (subtype)
8600       && int_size_in_bytes (type) == int_size_in_bytes (subtype)
8601       && TYPE_MIN_VALUE (type) != NULL
8602       && TYPE_MIN_VALUE (subtype) != NULL
8603       && tree_int_cst_equal (TYPE_MIN_VALUE (type), TYPE_MIN_VALUE (subtype))
8604       && TYPE_MAX_VALUE (type) != NULL
8605       && TYPE_MAX_VALUE (subtype) != NULL
8606       && tree_int_cst_equal (TYPE_MAX_VALUE (type), TYPE_MAX_VALUE (subtype)))
8607     {
8608       /* The type and its subtype have the same representation.  If in
8609          addition the two types also have the same name, then the given
8610          type is not a subrange type, but rather a plain base type.  */
8611       /* FIXME: brobecker/2004-03-22:
8612          Sizetype INTEGER_CSTs nodes are canonicalized.  It should
8613          therefore be sufficient to check the TYPE_SIZE node pointers
8614          rather than checking the actual size.  Unfortunately, we have
8615          found some cases, such as in the Ada "integer" type, where
8616          this is not the case.  Until this problem is solved, we need to
8617          keep checking the actual size.  */
8618       tree type_name = TYPE_NAME (type);
8619       tree subtype_name = TYPE_NAME (subtype);
8620
8621       if (type_name != NULL && TREE_CODE (type_name) == TYPE_DECL)
8622         type_name = DECL_NAME (type_name);
8623
8624       if (subtype_name != NULL && TREE_CODE (subtype_name) == TYPE_DECL)
8625         subtype_name = DECL_NAME (subtype_name);
8626
8627       if (type_name == subtype_name)
8628         return false;
8629     }
8630
8631   return true;
8632 }
8633
8634 /*  Given a pointer to a tree node for a subrange type, return a pointer
8635     to a DIE that describes the given type.  */
8636
8637 static dw_die_ref
8638 subrange_type_die (tree type, dw_die_ref context_die)
8639 {
8640   dw_die_ref subrange_die;
8641   const HOST_WIDE_INT size_in_bytes = int_size_in_bytes (type);
8642
8643   if (context_die == NULL)
8644     context_die = comp_unit_die;
8645
8646   subrange_die = new_die (DW_TAG_subrange_type, context_die, type);
8647
8648   if (int_size_in_bytes (TREE_TYPE (type)) != size_in_bytes)
8649     {
8650       /* The size of the subrange type and its base type do not match,
8651          so we need to generate a size attribute for the subrange type.  */
8652       add_AT_unsigned (subrange_die, DW_AT_byte_size, size_in_bytes);
8653     }
8654
8655   if (TYPE_MIN_VALUE (type) != NULL)
8656     add_bound_info (subrange_die, DW_AT_lower_bound,
8657                     TYPE_MIN_VALUE (type));
8658   if (TYPE_MAX_VALUE (type) != NULL)
8659     add_bound_info (subrange_die, DW_AT_upper_bound,
8660                     TYPE_MAX_VALUE (type));
8661
8662   return subrange_die;
8663 }
8664
8665 /* Given a pointer to an arbitrary ..._TYPE tree node, return a debugging
8666    entry that chains various modifiers in front of the given type.  */
8667
8668 static dw_die_ref
8669 modified_type_die (tree type, int is_const_type, int is_volatile_type,
8670                    dw_die_ref context_die)
8671 {
8672   enum tree_code code = TREE_CODE (type);
8673   dw_die_ref mod_type_die;
8674   dw_die_ref sub_die = NULL;
8675   tree item_type = NULL;
8676   tree qualified_type;
8677   tree name;
8678
8679   if (code == ERROR_MARK)
8680     return NULL;
8681
8682   /* See if we already have the appropriately qualified variant of
8683      this type.  */
8684   qualified_type
8685     = get_qualified_type (type,
8686                           ((is_const_type ? TYPE_QUAL_CONST : 0)
8687                            | (is_volatile_type ? TYPE_QUAL_VOLATILE : 0)));
8688
8689   /* If we do, then we can just use its DIE, if it exists.  */
8690   if (qualified_type)
8691     {
8692       mod_type_die = lookup_type_die (qualified_type);
8693       if (mod_type_die)
8694         return mod_type_die;
8695     }
8696
8697   name = qualified_type ? TYPE_NAME (qualified_type) : NULL;
8698
8699   /* Handle C typedef types.  */
8700   if (name && TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
8701     {
8702       tree dtype = TREE_TYPE (name);
8703
8704       if (qualified_type == dtype)
8705         {
8706           /* For a named type, use the typedef.  */
8707           gen_type_die (qualified_type, context_die);
8708           return lookup_type_die (qualified_type);
8709         }
8710       else if (is_const_type < TYPE_READONLY (dtype)
8711                || is_volatile_type < TYPE_VOLATILE (dtype)
8712                || (is_const_type <= TYPE_READONLY (dtype)
8713                    && is_volatile_type <= TYPE_VOLATILE (dtype)
8714                    && DECL_ORIGINAL_TYPE (name) != type))
8715         /* cv-unqualified version of named type.  Just use the unnamed
8716            type to which it refers.  */
8717         return modified_type_die (DECL_ORIGINAL_TYPE (name),
8718                                   is_const_type, is_volatile_type,
8719                                   context_die);
8720       /* Else cv-qualified version of named type; fall through.  */
8721     }
8722
8723   if (is_const_type)
8724     {
8725       mod_type_die = new_die (DW_TAG_const_type, comp_unit_die, type);
8726       sub_die = modified_type_die (type, 0, is_volatile_type, context_die);
8727     }
8728   else if (is_volatile_type)
8729     {
8730       mod_type_die = new_die (DW_TAG_volatile_type, comp_unit_die, type);
8731       sub_die = modified_type_die (type, 0, 0, context_die);
8732     }
8733   else if (code == POINTER_TYPE)
8734     {
8735       mod_type_die = new_die (DW_TAG_pointer_type, comp_unit_die, type);
8736       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8737                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8738       item_type = TREE_TYPE (type);
8739     }
8740   else if (code == REFERENCE_TYPE)
8741     {
8742       mod_type_die = new_die (DW_TAG_reference_type, comp_unit_die, type);
8743       add_AT_unsigned (mod_type_die, DW_AT_byte_size,
8744                        simple_type_size_in_bits (type) / BITS_PER_UNIT);
8745       item_type = TREE_TYPE (type);
8746     }
8747   else if (is_subrange_type (type))
8748     {
8749       mod_type_die = subrange_type_die (type, context_die);
8750       item_type = TREE_TYPE (type);
8751     }
8752   else if (is_base_type (type))
8753     mod_type_die = base_type_die (type);
8754   else
8755     {
8756       gen_type_die (type, context_die);
8757
8758       /* We have to get the type_main_variant here (and pass that to the
8759          `lookup_type_die' routine) because the ..._TYPE node we have
8760          might simply be a *copy* of some original type node (where the
8761          copy was created to help us keep track of typedef names) and
8762          that copy might have a different TYPE_UID from the original
8763          ..._TYPE node.  */
8764       if (TREE_CODE (type) != VECTOR_TYPE)
8765         return lookup_type_die (type_main_variant (type));
8766       else
8767         /* Vectors have the debugging information in the type,
8768            not the main variant.  */
8769         return lookup_type_die (type);
8770     }
8771
8772   /* Builtin types don't have a DECL_ORIGINAL_TYPE.  For those,
8773      don't output a DW_TAG_typedef, since there isn't one in the
8774      user's program; just attach a DW_AT_name to the type.  */
8775   if (name
8776       && (TREE_CODE (name) != TYPE_DECL
8777           || (TREE_TYPE (name) == qualified_type && DECL_NAME (name))))
8778     {
8779       if (TREE_CODE (name) == TYPE_DECL)
8780         /* Could just call add_name_and_src_coords_attributes here,
8781            but since this is a builtin type it doesn't have any
8782            useful source coordinates anyway.  */
8783         name = DECL_NAME (name);
8784       add_name_attribute (mod_type_die, IDENTIFIER_POINTER (name));
8785     }
8786
8787   if (qualified_type)
8788     equate_type_number_to_die (qualified_type, mod_type_die);
8789
8790   if (item_type)
8791     /* We must do this after the equate_type_number_to_die call, in case
8792        this is a recursive type.  This ensures that the modified_type_die
8793        recursion will terminate even if the type is recursive.  Recursive
8794        types are possible in Ada.  */
8795     sub_die = modified_type_die (item_type,
8796                                  TYPE_READONLY (item_type),
8797                                  TYPE_VOLATILE (item_type),
8798                                  context_die);
8799
8800   if (sub_die != NULL)
8801     add_AT_die_ref (mod_type_die, DW_AT_type, sub_die);
8802
8803   return mod_type_die;
8804 }
8805
8806 /* Given a pointer to an arbitrary ..._TYPE tree node, return true if it is
8807    an enumerated type.  */
8808
8809 static inline int
8810 type_is_enum (const_tree type)
8811 {
8812   return TREE_CODE (type) == ENUMERAL_TYPE;
8813 }
8814
8815 /* Return the DBX register number described by a given RTL node.  */
8816
8817 static unsigned int
8818 dbx_reg_number (const_rtx rtl)
8819 {
8820   unsigned regno = REGNO (rtl);
8821
8822   gcc_assert (regno < FIRST_PSEUDO_REGISTER);
8823
8824 #ifdef LEAF_REG_REMAP
8825   if (current_function_uses_only_leaf_regs)
8826     {
8827       int leaf_reg = LEAF_REG_REMAP (regno);
8828       if (leaf_reg != -1)
8829         regno = (unsigned) leaf_reg;
8830     }
8831 #endif
8832
8833   return DBX_REGISTER_NUMBER (regno);
8834 }
8835
8836 /* Optionally add a DW_OP_piece term to a location description expression.
8837    DW_OP_piece is only added if the location description expression already
8838    doesn't end with DW_OP_piece.  */
8839
8840 static void
8841 add_loc_descr_op_piece (dw_loc_descr_ref *list_head, int size)
8842 {
8843   dw_loc_descr_ref loc;
8844
8845   if (*list_head != NULL)
8846     {
8847       /* Find the end of the chain.  */
8848       for (loc = *list_head; loc->dw_loc_next != NULL; loc = loc->dw_loc_next)
8849         ;
8850
8851       if (loc->dw_loc_opc != DW_OP_piece)
8852         loc->dw_loc_next = new_loc_descr (DW_OP_piece, size, 0);
8853     }
8854 }
8855
8856 /* Return a location descriptor that designates a machine register or
8857    zero if there is none.  */
8858
8859 static dw_loc_descr_ref
8860 reg_loc_descriptor (rtx rtl, enum var_init_status initialized)
8861 {
8862   rtx regs;
8863
8864   if (REGNO (rtl) >= FIRST_PSEUDO_REGISTER)
8865     return 0;
8866
8867   regs = targetm.dwarf_register_span (rtl);
8868
8869   if (hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)] > 1 || regs)
8870     return multiple_reg_loc_descriptor (rtl, regs, initialized);
8871   else
8872     return one_reg_loc_descriptor (dbx_reg_number (rtl), initialized);
8873 }
8874
8875 /* Return a location descriptor that designates a machine register for
8876    a given hard register number.  */
8877
8878 static dw_loc_descr_ref
8879 one_reg_loc_descriptor (unsigned int regno, enum var_init_status initialized)
8880 {
8881   dw_loc_descr_ref reg_loc_descr;
8882   if (regno <= 31)
8883     reg_loc_descr = new_loc_descr (DW_OP_reg0 + regno, 0, 0);
8884   else
8885     reg_loc_descr = new_loc_descr (DW_OP_regx, regno, 0);
8886
8887   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
8888     add_loc_descr (&reg_loc_descr, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8889
8890   return reg_loc_descr;
8891 }
8892
8893 /* Given an RTL of a register, return a location descriptor that
8894    designates a value that spans more than one register.  */
8895
8896 static dw_loc_descr_ref
8897 multiple_reg_loc_descriptor (rtx rtl, rtx regs, 
8898                              enum var_init_status initialized)
8899 {
8900   int nregs, size, i;
8901   unsigned reg;
8902   dw_loc_descr_ref loc_result = NULL;
8903
8904   reg = REGNO (rtl);
8905 #ifdef LEAF_REG_REMAP
8906   if (current_function_uses_only_leaf_regs)
8907     {
8908       int leaf_reg = LEAF_REG_REMAP (reg);
8909       if (leaf_reg != -1)
8910         reg = (unsigned) leaf_reg;
8911     }
8912 #endif
8913   gcc_assert ((unsigned) DBX_REGISTER_NUMBER (reg) == dbx_reg_number (rtl));
8914   nregs = hard_regno_nregs[REGNO (rtl)][GET_MODE (rtl)];
8915
8916   /* Simple, contiguous registers.  */
8917   if (regs == NULL_RTX)
8918     {
8919       size = GET_MODE_SIZE (GET_MODE (rtl)) / nregs;
8920
8921       loc_result = NULL;
8922       while (nregs--)
8923         {
8924           dw_loc_descr_ref t;
8925
8926           t = one_reg_loc_descriptor (DBX_REGISTER_NUMBER (reg),
8927                                       VAR_INIT_STATUS_INITIALIZED);
8928           add_loc_descr (&loc_result, t);
8929           add_loc_descr_op_piece (&loc_result, size);
8930           ++reg;
8931         }
8932       return loc_result;
8933     }
8934
8935   /* Now onto stupid register sets in non contiguous locations.  */
8936
8937   gcc_assert (GET_CODE (regs) == PARALLEL);
8938
8939   size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8940   loc_result = NULL;
8941
8942   for (i = 0; i < XVECLEN (regs, 0); ++i)
8943     {
8944       dw_loc_descr_ref t;
8945
8946       t = one_reg_loc_descriptor (REGNO (XVECEXP (regs, 0, i)),
8947                                   VAR_INIT_STATUS_INITIALIZED);
8948       add_loc_descr (&loc_result, t);
8949       size = GET_MODE_SIZE (GET_MODE (XVECEXP (regs, 0, 0)));
8950       add_loc_descr_op_piece (&loc_result, size);
8951     }
8952
8953   if (loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
8954     add_loc_descr (&loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
8955   return loc_result;
8956 }
8957
8958 /* Return a location descriptor that designates a constant.  */
8959
8960 static dw_loc_descr_ref
8961 int_loc_descriptor (HOST_WIDE_INT i)
8962 {
8963   enum dwarf_location_atom op;
8964
8965   /* Pick the smallest representation of a constant, rather than just
8966      defaulting to the LEB encoding.  */
8967   if (i >= 0)
8968     {
8969       if (i <= 31)
8970         op = DW_OP_lit0 + i;
8971       else if (i <= 0xff)
8972         op = DW_OP_const1u;
8973       else if (i <= 0xffff)
8974         op = DW_OP_const2u;
8975       else if (HOST_BITS_PER_WIDE_INT == 32
8976                || i <= 0xffffffff)
8977         op = DW_OP_const4u;
8978       else
8979         op = DW_OP_constu;
8980     }
8981   else
8982     {
8983       if (i >= -0x80)
8984         op = DW_OP_const1s;
8985       else if (i >= -0x8000)
8986         op = DW_OP_const2s;
8987       else if (HOST_BITS_PER_WIDE_INT == 32
8988                || i >= -0x80000000)
8989         op = DW_OP_const4s;
8990       else
8991         op = DW_OP_consts;
8992     }
8993
8994   return new_loc_descr (op, i, 0);
8995 }
8996
8997 /* Return a location descriptor that designates a base+offset location.  */
8998
8999 static dw_loc_descr_ref
9000 based_loc_descr (rtx reg, HOST_WIDE_INT offset,
9001                  enum var_init_status initialized)
9002 {
9003   unsigned int regno;
9004   dw_loc_descr_ref result;
9005
9006   /* We only use "frame base" when we're sure we're talking about the
9007      post-prologue local stack frame.  We do this by *not* running
9008      register elimination until this point, and recognizing the special
9009      argument pointer and soft frame pointer rtx's.  */
9010   if (reg == arg_pointer_rtx || reg == frame_pointer_rtx)
9011     {
9012       rtx elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
9013
9014       if (elim != reg)
9015         {
9016           if (GET_CODE (elim) == PLUS)
9017             {
9018               offset += INTVAL (XEXP (elim, 1));
9019               elim = XEXP (elim, 0);
9020             }
9021           gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
9022                       : stack_pointer_rtx));
9023           offset += frame_pointer_fb_offset;
9024
9025           return new_loc_descr (DW_OP_fbreg, offset, 0);
9026         }
9027     }
9028
9029   regno = dbx_reg_number (reg);
9030   if (regno <= 31)
9031     result = new_loc_descr (DW_OP_breg0 + regno, offset, 0);
9032   else
9033     result = new_loc_descr (DW_OP_bregx, regno, offset);
9034
9035   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9036     add_loc_descr (&result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9037
9038   return result;
9039 }
9040
9041 /* Return true if this RTL expression describes a base+offset calculation.  */
9042
9043 static inline int
9044 is_based_loc (const_rtx rtl)
9045 {
9046   return (GET_CODE (rtl) == PLUS
9047           && ((REG_P (XEXP (rtl, 0))
9048                && REGNO (XEXP (rtl, 0)) < FIRST_PSEUDO_REGISTER
9049                && GET_CODE (XEXP (rtl, 1)) == CONST_INT)));
9050 }
9051
9052 /* Return a descriptor that describes the concatenation of N locations
9053    used to form the address of a memory location.  */
9054
9055 static dw_loc_descr_ref
9056 concatn_mem_loc_descriptor (rtx concatn, enum machine_mode mode,
9057                             enum var_init_status initialized)
9058 {
9059   unsigned int i;
9060   dw_loc_descr_ref cc_loc_result = NULL;
9061   unsigned int n = XVECLEN (concatn, 0);
9062
9063   for (i = 0; i < n; ++i)
9064     {
9065       dw_loc_descr_ref ref;
9066       rtx x = XVECEXP (concatn, 0, i);
9067
9068       ref = mem_loc_descriptor (x, mode, VAR_INIT_STATUS_INITIALIZED);
9069       if (ref == NULL)
9070         return NULL;
9071
9072       add_loc_descr (&cc_loc_result, ref);
9073       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9074     }
9075
9076   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9077     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9078
9079   return cc_loc_result;
9080 }
9081
9082 /* The following routine converts the RTL for a variable or parameter
9083    (resident in memory) into an equivalent Dwarf representation of a
9084    mechanism for getting the address of that same variable onto the top of a
9085    hypothetical "address evaluation" stack.
9086
9087    When creating memory location descriptors, we are effectively transforming
9088    the RTL for a memory-resident object into its Dwarf postfix expression
9089    equivalent.  This routine recursively descends an RTL tree, turning
9090    it into Dwarf postfix code as it goes.
9091
9092    MODE is the mode of the memory reference, needed to handle some
9093    autoincrement addressing modes.
9094
9095    CAN_USE_FBREG is a flag whether we can use DW_AT_frame_base in the
9096    location list for RTL.
9097
9098    Return 0 if we can't represent the location.  */
9099
9100 static dw_loc_descr_ref
9101 mem_loc_descriptor (rtx rtl, enum machine_mode mode,
9102                     enum var_init_status initialized)
9103 {
9104   dw_loc_descr_ref mem_loc_result = NULL;
9105   enum dwarf_location_atom op;
9106
9107   /* Note that for a dynamically sized array, the location we will generate a
9108      description of here will be the lowest numbered location which is
9109      actually within the array.  That's *not* necessarily the same as the
9110      zeroth element of the array.  */
9111
9112   rtl = targetm.delegitimize_address (rtl);
9113
9114   switch (GET_CODE (rtl))
9115     {
9116     case POST_INC:
9117     case POST_DEC:
9118     case POST_MODIFY:
9119       /* POST_INC and POST_DEC can be handled just like a SUBREG.  So we
9120          just fall into the SUBREG code.  */
9121
9122       /* ... fall through ...  */
9123
9124     case SUBREG:
9125       /* The case of a subreg may arise when we have a local (register)
9126          variable or a formal (register) parameter which doesn't quite fill
9127          up an entire register.  For now, just assume that it is
9128          legitimate to make the Dwarf info refer to the whole register which
9129          contains the given subreg.  */
9130       rtl = XEXP (rtl, 0);
9131
9132       /* ... fall through ...  */
9133
9134     case REG:
9135       /* Whenever a register number forms a part of the description of the
9136          method for calculating the (dynamic) address of a memory resident
9137          object, DWARF rules require the register number be referred to as
9138          a "base register".  This distinction is not based in any way upon
9139          what category of register the hardware believes the given register
9140          belongs to.  This is strictly DWARF terminology we're dealing with
9141          here. Note that in cases where the location of a memory-resident
9142          data object could be expressed as: OP_ADD (OP_BASEREG (basereg),
9143          OP_CONST (0)) the actual DWARF location descriptor that we generate
9144          may just be OP_BASEREG (basereg).  This may look deceptively like
9145          the object in question was allocated to a register (rather than in
9146          memory) so DWARF consumers need to be aware of the subtle
9147          distinction between OP_REG and OP_BASEREG.  */
9148       if (REGNO (rtl) < FIRST_PSEUDO_REGISTER)
9149         mem_loc_result = based_loc_descr (rtl, 0, VAR_INIT_STATUS_INITIALIZED);
9150       break;
9151
9152     case MEM:
9153       mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9154                                            VAR_INIT_STATUS_INITIALIZED);
9155       if (mem_loc_result != 0)
9156         add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_deref, 0, 0));
9157       break;
9158
9159     case LO_SUM:
9160          rtl = XEXP (rtl, 1);
9161
9162       /* ... fall through ...  */
9163
9164     case LABEL_REF:
9165       /* Some ports can transform a symbol ref into a label ref, because
9166          the symbol ref is too far away and has to be dumped into a constant
9167          pool.  */
9168     case CONST:
9169     case SYMBOL_REF:
9170       /* Alternatively, the symbol in the constant pool might be referenced
9171          by a different symbol.  */
9172       if (GET_CODE (rtl) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (rtl))
9173         {
9174           bool marked;
9175           rtx tmp = get_pool_constant_mark (rtl, &marked);
9176
9177           if (GET_CODE (tmp) == SYMBOL_REF)
9178             {
9179               rtl = tmp;
9180               if (CONSTANT_POOL_ADDRESS_P (tmp))
9181                 get_pool_constant_mark (tmp, &marked);
9182               else
9183                 marked = true;
9184             }
9185
9186           /* If all references to this pool constant were optimized away,
9187              it was not output and thus we can't represent it.
9188              FIXME: might try to use DW_OP_const_value here, though
9189              DW_OP_piece complicates it.  */
9190           if (!marked)
9191             return 0;
9192         }
9193
9194       mem_loc_result = new_loc_descr (DW_OP_addr, 0, 0);
9195       mem_loc_result->dw_loc_oprnd1.val_class = dw_val_class_addr;
9196       mem_loc_result->dw_loc_oprnd1.v.val_addr = rtl;
9197       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
9198       break;
9199
9200     case PRE_MODIFY:
9201       /* Extract the PLUS expression nested inside and fall into
9202          PLUS code below.  */
9203       rtl = XEXP (rtl, 1);
9204       goto plus;
9205
9206     case PRE_INC:
9207     case PRE_DEC:
9208       /* Turn these into a PLUS expression and fall into the PLUS code
9209          below.  */
9210       rtl = gen_rtx_PLUS (word_mode, XEXP (rtl, 0),
9211                           GEN_INT (GET_CODE (rtl) == PRE_INC
9212                                    ? GET_MODE_UNIT_SIZE (mode)
9213                                    : -GET_MODE_UNIT_SIZE (mode)));
9214
9215       /* ... fall through ...  */
9216
9217     case PLUS:
9218     plus:
9219       if (is_based_loc (rtl))
9220         mem_loc_result = based_loc_descr (XEXP (rtl, 0),
9221                                           INTVAL (XEXP (rtl, 1)),
9222                                           VAR_INIT_STATUS_INITIALIZED);
9223       else
9224         {
9225           mem_loc_result = mem_loc_descriptor (XEXP (rtl, 0), mode,
9226                                                VAR_INIT_STATUS_INITIALIZED);
9227           if (mem_loc_result == 0)
9228             break;
9229
9230           if (GET_CODE (XEXP (rtl, 1)) == CONST_INT
9231               && INTVAL (XEXP (rtl, 1)) >= 0)
9232             add_loc_descr (&mem_loc_result,
9233                            new_loc_descr (DW_OP_plus_uconst,
9234                                           INTVAL (XEXP (rtl, 1)), 0));
9235           else
9236             {
9237               add_loc_descr (&mem_loc_result,
9238                              mem_loc_descriptor (XEXP (rtl, 1), mode,
9239                                                  VAR_INIT_STATUS_INITIALIZED));
9240               add_loc_descr (&mem_loc_result,
9241                              new_loc_descr (DW_OP_plus, 0, 0));
9242             }
9243         }
9244       break;
9245
9246     /* If a pseudo-reg is optimized away, it is possible for it to
9247        be replaced with a MEM containing a multiply or shift.  */
9248     case MULT:
9249       op = DW_OP_mul;
9250       goto do_binop;
9251
9252     case ASHIFT:
9253       op = DW_OP_shl;
9254       goto do_binop;
9255
9256     case ASHIFTRT:
9257       op = DW_OP_shra;
9258       goto do_binop;
9259
9260     case LSHIFTRT:
9261       op = DW_OP_shr;
9262       goto do_binop;
9263
9264     do_binop:
9265       {
9266         dw_loc_descr_ref op0 = mem_loc_descriptor (XEXP (rtl, 0), mode,
9267                                                    VAR_INIT_STATUS_INITIALIZED);
9268         dw_loc_descr_ref op1 = mem_loc_descriptor (XEXP (rtl, 1), mode,
9269                                                    VAR_INIT_STATUS_INITIALIZED);
9270
9271         if (op0 == 0 || op1 == 0)
9272           break;
9273
9274         mem_loc_result = op0;
9275         add_loc_descr (&mem_loc_result, op1);
9276         add_loc_descr (&mem_loc_result, new_loc_descr (op, 0, 0));
9277         break;
9278       }
9279
9280     case CONST_INT:
9281       mem_loc_result = int_loc_descriptor (INTVAL (rtl));
9282       break;
9283
9284     case CONCATN:
9285       mem_loc_result = concatn_mem_loc_descriptor (rtl, mode, 
9286                                                    VAR_INIT_STATUS_INITIALIZED);
9287       break;
9288
9289     default:
9290       gcc_unreachable ();
9291     }
9292
9293   if (mem_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9294     add_loc_descr (&mem_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9295
9296   return mem_loc_result;
9297 }
9298
9299 /* Return a descriptor that describes the concatenation of two locations.
9300    This is typically a complex variable.  */
9301
9302 static dw_loc_descr_ref
9303 concat_loc_descriptor (rtx x0, rtx x1, enum var_init_status initialized)
9304 {
9305   dw_loc_descr_ref cc_loc_result = NULL;
9306   dw_loc_descr_ref x0_ref = loc_descriptor (x0, VAR_INIT_STATUS_INITIALIZED);
9307   dw_loc_descr_ref x1_ref = loc_descriptor (x1, VAR_INIT_STATUS_INITIALIZED);
9308
9309   if (x0_ref == 0 || x1_ref == 0)
9310     return 0;
9311
9312   cc_loc_result = x0_ref;
9313   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x0)));
9314
9315   add_loc_descr (&cc_loc_result, x1_ref);
9316   add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x1)));
9317
9318   if (initialized == VAR_INIT_STATUS_UNINITIALIZED)
9319     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9320
9321   return cc_loc_result;
9322 }
9323
9324 /* Return a descriptor that describes the concatenation of N
9325    locations.  */
9326
9327 static dw_loc_descr_ref
9328 concatn_loc_descriptor (rtx concatn, enum var_init_status initialized)
9329 {
9330   unsigned int i;
9331   dw_loc_descr_ref cc_loc_result = NULL;
9332   unsigned int n = XVECLEN (concatn, 0);
9333
9334   for (i = 0; i < n; ++i)
9335     {
9336       dw_loc_descr_ref ref;
9337       rtx x = XVECEXP (concatn, 0, i);
9338
9339       ref = loc_descriptor (x, VAR_INIT_STATUS_INITIALIZED);
9340       if (ref == NULL)
9341         return NULL;
9342
9343       add_loc_descr (&cc_loc_result, ref);
9344       add_loc_descr_op_piece (&cc_loc_result, GET_MODE_SIZE (GET_MODE (x)));
9345     }
9346
9347   if (cc_loc_result && initialized == VAR_INIT_STATUS_UNINITIALIZED)
9348     add_loc_descr (&cc_loc_result, new_loc_descr (DW_OP_GNU_uninit, 0, 0));
9349
9350   return cc_loc_result;
9351 }
9352
9353 /* Output a proper Dwarf location descriptor for a variable or parameter
9354    which is either allocated in a register or in a memory location.  For a
9355    register, we just generate an OP_REG and the register number.  For a
9356    memory location we provide a Dwarf postfix expression describing how to
9357    generate the (dynamic) address of the object onto the address stack.
9358
9359    If we don't know how to describe it, return 0.  */
9360
9361 static dw_loc_descr_ref
9362 loc_descriptor (rtx rtl, enum var_init_status initialized)
9363 {
9364   dw_loc_descr_ref loc_result = NULL;
9365
9366   switch (GET_CODE (rtl))
9367     {
9368     case SUBREG:
9369       /* The case of a subreg may arise when we have a local (register)
9370          variable or a formal (register) parameter which doesn't quite fill
9371          up an entire register.  For now, just assume that it is
9372          legitimate to make the Dwarf info refer to the whole register which
9373          contains the given subreg.  */
9374       rtl = SUBREG_REG (rtl);
9375
9376       /* ... fall through ...  */
9377
9378     case REG:
9379       loc_result = reg_loc_descriptor (rtl, initialized);
9380       break;
9381
9382     case MEM:
9383       loc_result = mem_loc_descriptor (XEXP (rtl, 0), GET_MODE (rtl),
9384                                        initialized);
9385       break;
9386
9387     case CONCAT:
9388       loc_result = concat_loc_descriptor (XEXP (rtl, 0), XEXP (rtl, 1),
9389                                           initialized);
9390       break;
9391
9392     case CONCATN:
9393       loc_result = concatn_loc_descriptor (rtl, initialized);
9394       break;
9395
9396     case VAR_LOCATION:
9397       /* Single part.  */
9398       if (GET_CODE (XEXP (rtl, 1)) != PARALLEL)
9399         {
9400           loc_result = loc_descriptor (XEXP (XEXP (rtl, 1), 0), initialized);
9401           break;
9402         }
9403
9404       rtl = XEXP (rtl, 1);
9405       /* FALLTHRU */
9406
9407     case PARALLEL:
9408       {
9409         rtvec par_elems = XVEC (rtl, 0);
9410         int num_elem = GET_NUM_ELEM (par_elems);
9411         enum machine_mode mode;
9412         int i;
9413
9414         /* Create the first one, so we have something to add to.  */
9415         loc_result = loc_descriptor (XEXP (RTVEC_ELT (par_elems, 0), 0),
9416                                      initialized);
9417         mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, 0), 0));
9418         add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9419         for (i = 1; i < num_elem; i++)
9420           {
9421             dw_loc_descr_ref temp;
9422
9423             temp = loc_descriptor (XEXP (RTVEC_ELT (par_elems, i), 0),
9424                                    initialized);
9425             add_loc_descr (&loc_result, temp);
9426             mode = GET_MODE (XEXP (RTVEC_ELT (par_elems, i), 0));
9427             add_loc_descr_op_piece (&loc_result, GET_MODE_SIZE (mode));
9428           }
9429       }
9430       break;
9431
9432     default:
9433       gcc_unreachable ();
9434     }
9435
9436   return loc_result;
9437 }
9438
9439 /* Similar, but generate the descriptor from trees instead of rtl.  This comes
9440    up particularly with variable length arrays.  WANT_ADDRESS is 2 if this is
9441    a top-level invocation of loc_descriptor_from_tree; is 1 if this is not a
9442    top-level invocation, and we require the address of LOC; is 0 if we require
9443    the value of LOC.  */
9444
9445 static dw_loc_descr_ref
9446 loc_descriptor_from_tree_1 (tree loc, int want_address)
9447 {
9448   dw_loc_descr_ref ret, ret1;
9449   int have_address = 0;
9450   enum dwarf_location_atom op;
9451
9452   /* ??? Most of the time we do not take proper care for sign/zero
9453      extending the values properly.  Hopefully this won't be a real
9454      problem...  */
9455
9456   switch (TREE_CODE (loc))
9457     {
9458     case ERROR_MARK:
9459       return 0;
9460
9461     case PLACEHOLDER_EXPR:
9462       /* This case involves extracting fields from an object to determine the
9463          position of other fields.  We don't try to encode this here.  The
9464          only user of this is Ada, which encodes the needed information using
9465          the names of types.  */
9466       return 0;
9467
9468     case CALL_EXPR:
9469       return 0;
9470
9471     case PREINCREMENT_EXPR:
9472     case PREDECREMENT_EXPR:
9473     case POSTINCREMENT_EXPR:
9474     case POSTDECREMENT_EXPR:
9475       /* There are no opcodes for these operations.  */
9476       return 0;
9477
9478     case ADDR_EXPR:
9479       /* If we already want an address, there's nothing we can do.  */
9480       if (want_address)
9481         return 0;
9482
9483       /* Otherwise, process the argument and look for the address.  */
9484       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 1);
9485
9486     case VAR_DECL:
9487       if (DECL_THREAD_LOCAL_P (loc))
9488         {
9489           rtx rtl;
9490
9491           /* If this is not defined, we have no way to emit the data.  */
9492           if (!targetm.have_tls || !targetm.asm_out.output_dwarf_dtprel)
9493             return 0;
9494
9495           /* The way DW_OP_GNU_push_tls_address is specified, we can only
9496              look up addresses of objects in the current module.  */
9497           if (DECL_EXTERNAL (loc))
9498             return 0;
9499
9500           rtl = rtl_for_decl_location (loc);
9501           if (rtl == NULL_RTX)
9502             return 0;
9503
9504           if (!MEM_P (rtl))
9505             return 0;
9506           rtl = XEXP (rtl, 0);
9507           if (! CONSTANT_P (rtl))
9508             return 0;
9509
9510           ret = new_loc_descr (INTERNAL_DW_OP_tls_addr, 0, 0);
9511           ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9512           ret->dw_loc_oprnd1.v.val_addr = rtl;
9513
9514           ret1 = new_loc_descr (DW_OP_GNU_push_tls_address, 0, 0);
9515           add_loc_descr (&ret, ret1);
9516
9517           have_address = 1;
9518           break;
9519         }
9520       /* FALLTHRU */
9521
9522     case PARM_DECL:
9523       if (DECL_HAS_VALUE_EXPR_P (loc))
9524         return loc_descriptor_from_tree_1 (DECL_VALUE_EXPR (loc),
9525                                            want_address);
9526       /* FALLTHRU */
9527
9528     case RESULT_DECL:
9529     case FUNCTION_DECL:
9530       {
9531         rtx rtl = rtl_for_decl_location (loc);
9532
9533         if (rtl == NULL_RTX)
9534           return 0;
9535         else if (GET_CODE (rtl) == CONST_INT)
9536           {
9537             HOST_WIDE_INT val = INTVAL (rtl);
9538             if (TYPE_UNSIGNED (TREE_TYPE (loc)))
9539               val &= GET_MODE_MASK (DECL_MODE (loc));
9540             ret = int_loc_descriptor (val);
9541           }
9542         else if (GET_CODE (rtl) == CONST_STRING)
9543           return 0;
9544         else if (CONSTANT_P (rtl))
9545           {
9546             ret = new_loc_descr (DW_OP_addr, 0, 0);
9547             ret->dw_loc_oprnd1.val_class = dw_val_class_addr;
9548             ret->dw_loc_oprnd1.v.val_addr = rtl;
9549           }
9550         else
9551           {
9552             enum machine_mode mode;
9553
9554             /* Certain constructs can only be represented at top-level.  */
9555             if (want_address == 2)
9556               return loc_descriptor (rtl, VAR_INIT_STATUS_INITIALIZED);
9557
9558             mode = GET_MODE (rtl);
9559             if (MEM_P (rtl))
9560               {
9561                 rtl = XEXP (rtl, 0);
9562                 have_address = 1;
9563               }
9564             ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9565           }
9566       }
9567       break;
9568
9569     case INDIRECT_REF:
9570       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9571       have_address = 1;
9572       break;
9573
9574     case COMPOUND_EXPR:
9575       return loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), want_address);
9576
9577     case NOP_EXPR:
9578     case CONVERT_EXPR:
9579     case NON_LVALUE_EXPR:
9580     case VIEW_CONVERT_EXPR:
9581     case SAVE_EXPR:
9582     case GIMPLE_MODIFY_STMT:
9583       return loc_descriptor_from_tree_1 (GENERIC_TREE_OPERAND (loc, 0),
9584                                          want_address);
9585
9586     case COMPONENT_REF:
9587     case BIT_FIELD_REF:
9588     case ARRAY_REF:
9589     case ARRAY_RANGE_REF:
9590       {
9591         tree obj, offset;
9592         HOST_WIDE_INT bitsize, bitpos, bytepos;
9593         enum machine_mode mode;
9594         int volatilep;
9595         int unsignedp = TYPE_UNSIGNED (TREE_TYPE (loc));
9596
9597         obj = get_inner_reference (loc, &bitsize, &bitpos, &offset, &mode,
9598                                    &unsignedp, &volatilep, false);
9599
9600         if (obj == loc)
9601           return 0;
9602
9603         ret = loc_descriptor_from_tree_1 (obj, 1);
9604         if (ret == 0
9605             || bitpos % BITS_PER_UNIT != 0 || bitsize % BITS_PER_UNIT != 0)
9606           return 0;
9607
9608         if (offset != NULL_TREE)
9609           {
9610             /* Variable offset.  */
9611             add_loc_descr (&ret, loc_descriptor_from_tree_1 (offset, 0));
9612             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9613           }
9614
9615         bytepos = bitpos / BITS_PER_UNIT;
9616         if (bytepos > 0)
9617           add_loc_descr (&ret, new_loc_descr (DW_OP_plus_uconst, bytepos, 0));
9618         else if (bytepos < 0)
9619           {
9620             add_loc_descr (&ret, int_loc_descriptor (bytepos));
9621             add_loc_descr (&ret, new_loc_descr (DW_OP_plus, 0, 0));
9622           }
9623
9624         have_address = 1;
9625         break;
9626       }
9627
9628     case INTEGER_CST:
9629       if (host_integerp (loc, 0))
9630         ret = int_loc_descriptor (tree_low_cst (loc, 0));
9631       else
9632         return 0;
9633       break;
9634
9635     case CONSTRUCTOR:
9636       {
9637         /* Get an RTL for this, if something has been emitted.  */
9638         rtx rtl = lookup_constant_def (loc);
9639         enum machine_mode mode;
9640
9641         if (!rtl || !MEM_P (rtl))
9642           return 0;
9643         mode = GET_MODE (rtl);
9644         rtl = XEXP (rtl, 0);
9645         ret = mem_loc_descriptor (rtl, mode, VAR_INIT_STATUS_INITIALIZED);
9646         have_address = 1;
9647         break;
9648       }
9649
9650     case TRUTH_AND_EXPR:
9651     case TRUTH_ANDIF_EXPR:
9652     case BIT_AND_EXPR:
9653       op = DW_OP_and;
9654       goto do_binop;
9655
9656     case TRUTH_XOR_EXPR:
9657     case BIT_XOR_EXPR:
9658       op = DW_OP_xor;
9659       goto do_binop;
9660
9661     case TRUTH_OR_EXPR:
9662     case TRUTH_ORIF_EXPR:
9663     case BIT_IOR_EXPR:
9664       op = DW_OP_or;
9665       goto do_binop;
9666
9667     case FLOOR_DIV_EXPR:
9668     case CEIL_DIV_EXPR:
9669     case ROUND_DIV_EXPR:
9670     case TRUNC_DIV_EXPR:
9671       op = DW_OP_div;
9672       goto do_binop;
9673
9674     case MINUS_EXPR:
9675       op = DW_OP_minus;
9676       goto do_binop;
9677
9678     case FLOOR_MOD_EXPR:
9679     case CEIL_MOD_EXPR:
9680     case ROUND_MOD_EXPR:
9681     case TRUNC_MOD_EXPR:
9682       op = DW_OP_mod;
9683       goto do_binop;
9684
9685     case MULT_EXPR:
9686       op = DW_OP_mul;
9687       goto do_binop;
9688
9689     case LSHIFT_EXPR:
9690       op = DW_OP_shl;
9691       goto do_binop;
9692
9693     case RSHIFT_EXPR:
9694       op = (TYPE_UNSIGNED (TREE_TYPE (loc)) ? DW_OP_shr : DW_OP_shra);
9695       goto do_binop;
9696
9697     case POINTER_PLUS_EXPR:
9698     case PLUS_EXPR:
9699       if (TREE_CODE (TREE_OPERAND (loc, 1)) == INTEGER_CST
9700           && host_integerp (TREE_OPERAND (loc, 1), 0))
9701         {
9702           ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9703           if (ret == 0)
9704             return 0;
9705
9706           add_loc_descr (&ret,
9707                          new_loc_descr (DW_OP_plus_uconst,
9708                                         tree_low_cst (TREE_OPERAND (loc, 1),
9709                                                       0),
9710                                         0));
9711           break;
9712         }
9713
9714       op = DW_OP_plus;
9715       goto do_binop;
9716
9717     case LE_EXPR:
9718       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9719         return 0;
9720
9721       op = DW_OP_le;
9722       goto do_binop;
9723
9724     case GE_EXPR:
9725       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9726         return 0;
9727
9728       op = DW_OP_ge;
9729       goto do_binop;
9730
9731     case LT_EXPR:
9732       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9733         return 0;
9734
9735       op = DW_OP_lt;
9736       goto do_binop;
9737
9738     case GT_EXPR:
9739       if (TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (loc, 0))))
9740         return 0;
9741
9742       op = DW_OP_gt;
9743       goto do_binop;
9744
9745     case EQ_EXPR:
9746       op = DW_OP_eq;
9747       goto do_binop;
9748
9749     case NE_EXPR:
9750       op = DW_OP_ne;
9751       goto do_binop;
9752
9753     do_binop:
9754       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9755       ret1 = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9756       if (ret == 0 || ret1 == 0)
9757         return 0;
9758
9759       add_loc_descr (&ret, ret1);
9760       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9761       break;
9762
9763     case TRUTH_NOT_EXPR:
9764     case BIT_NOT_EXPR:
9765       op = DW_OP_not;
9766       goto do_unop;
9767
9768     case ABS_EXPR:
9769       op = DW_OP_abs;
9770       goto do_unop;
9771
9772     case NEGATE_EXPR:
9773       op = DW_OP_neg;
9774       goto do_unop;
9775
9776     do_unop:
9777       ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9778       if (ret == 0)
9779         return 0;
9780
9781       add_loc_descr (&ret, new_loc_descr (op, 0, 0));
9782       break;
9783
9784     case MIN_EXPR:
9785     case MAX_EXPR:
9786       {
9787         const enum tree_code code =
9788           TREE_CODE (loc) == MIN_EXPR ? GT_EXPR : LT_EXPR;
9789
9790         loc = build3 (COND_EXPR, TREE_TYPE (loc),
9791                       build2 (code, integer_type_node,
9792                               TREE_OPERAND (loc, 0), TREE_OPERAND (loc, 1)),
9793                       TREE_OPERAND (loc, 1), TREE_OPERAND (loc, 0));
9794       }
9795
9796       /* ... fall through ...  */
9797
9798     case COND_EXPR:
9799       {
9800         dw_loc_descr_ref lhs
9801           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 1), 0);
9802         dw_loc_descr_ref rhs
9803           = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 2), 0);
9804         dw_loc_descr_ref bra_node, jump_node, tmp;
9805
9806         ret = loc_descriptor_from_tree_1 (TREE_OPERAND (loc, 0), 0);
9807         if (ret == 0 || lhs == 0 || rhs == 0)
9808           return 0;
9809
9810         bra_node = new_loc_descr (DW_OP_bra, 0, 0);
9811         add_loc_descr (&ret, bra_node);
9812
9813         add_loc_descr (&ret, rhs);
9814         jump_node = new_loc_descr (DW_OP_skip, 0, 0);
9815         add_loc_descr (&ret, jump_node);
9816
9817         add_loc_descr (&ret, lhs);
9818         bra_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9819         bra_node->dw_loc_oprnd1.v.val_loc = lhs;
9820
9821         /* ??? Need a node to point the skip at.  Use a nop.  */
9822         tmp = new_loc_descr (DW_OP_nop, 0, 0);
9823         add_loc_descr (&ret, tmp);
9824         jump_node->dw_loc_oprnd1.val_class = dw_val_class_loc;
9825         jump_node->dw_loc_oprnd1.v.val_loc = tmp;
9826       }
9827       break;
9828
9829     case FIX_TRUNC_EXPR:
9830       return 0;
9831
9832     default:
9833       /* Leave front-end specific codes as simply unknown.  This comes
9834          up, for instance, with the C STMT_EXPR.  */
9835       if ((unsigned int) TREE_CODE (loc)
9836           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
9837         return 0;
9838
9839 #ifdef ENABLE_CHECKING
9840       /* Otherwise this is a generic code; we should just lists all of
9841          these explicitly.  We forgot one.  */
9842       gcc_unreachable ();
9843 #else
9844       /* In a release build, we want to degrade gracefully: better to
9845          generate incomplete debugging information than to crash.  */
9846       return NULL;
9847 #endif
9848     }
9849
9850   /* Show if we can't fill the request for an address.  */
9851   if (want_address && !have_address)
9852     return 0;
9853
9854   /* If we've got an address and don't want one, dereference.  */
9855   if (!want_address && have_address && ret)
9856     {
9857       HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (loc));
9858
9859       if (size > DWARF2_ADDR_SIZE || size == -1)
9860         return 0;
9861       else if (size == DWARF2_ADDR_SIZE)
9862         op = DW_OP_deref;
9863       else
9864         op = DW_OP_deref_size;
9865
9866       add_loc_descr (&ret, new_loc_descr (op, size, 0));
9867     }
9868
9869   return ret;
9870 }
9871
9872 static inline dw_loc_descr_ref
9873 loc_descriptor_from_tree (tree loc)
9874 {
9875   return loc_descriptor_from_tree_1 (loc, 2);
9876 }
9877
9878 /* Given a value, round it up to the lowest multiple of `boundary'
9879    which is not less than the value itself.  */
9880
9881 static inline HOST_WIDE_INT
9882 ceiling (HOST_WIDE_INT value, unsigned int boundary)
9883 {
9884   return (((value + boundary - 1) / boundary) * boundary);
9885 }
9886
9887 /* Given a pointer to what is assumed to be a FIELD_DECL node, return a
9888    pointer to the declared type for the relevant field variable, or return
9889    `integer_type_node' if the given node turns out to be an
9890    ERROR_MARK node.  */
9891
9892 static inline tree
9893 field_type (const_tree decl)
9894 {
9895   tree type;
9896
9897   if (TREE_CODE (decl) == ERROR_MARK)
9898     return integer_type_node;
9899
9900   type = DECL_BIT_FIELD_TYPE (decl);
9901   if (type == NULL_TREE)
9902     type = TREE_TYPE (decl);
9903
9904   return type;
9905 }
9906
9907 /* Given a pointer to a tree node, return the alignment in bits for
9908    it, or else return BITS_PER_WORD if the node actually turns out to
9909    be an ERROR_MARK node.  */
9910
9911 static inline unsigned
9912 simple_type_align_in_bits (const_tree type)
9913 {
9914   return (TREE_CODE (type) != ERROR_MARK) ? TYPE_ALIGN (type) : BITS_PER_WORD;
9915 }
9916
9917 static inline unsigned
9918 simple_decl_align_in_bits (const_tree decl)
9919 {
9920   return (TREE_CODE (decl) != ERROR_MARK) ? DECL_ALIGN (decl) : BITS_PER_WORD;
9921 }
9922
9923 /* Return the result of rounding T up to ALIGN.  */
9924
9925 static inline HOST_WIDE_INT
9926 round_up_to_align (HOST_WIDE_INT t, unsigned int align)
9927 {
9928   /* We must be careful if T is negative because HOST_WIDE_INT can be
9929      either "above" or "below" unsigned int as per the C promotion
9930      rules, depending on the host, thus making the signedness of the
9931      direct multiplication and division unpredictable.  */
9932   unsigned HOST_WIDE_INT u = (unsigned HOST_WIDE_INT) t;
9933
9934   u += align - 1;
9935   u /= align;
9936   u *= align;
9937
9938   return (HOST_WIDE_INT) u;
9939 }
9940
9941 /* Given a pointer to a FIELD_DECL, compute and return the byte offset of the
9942    lowest addressed byte of the "containing object" for the given FIELD_DECL,
9943    or return 0 if we are unable to determine what that offset is, either
9944    because the argument turns out to be a pointer to an ERROR_MARK node, or
9945    because the offset is actually variable.  (We can't handle the latter case
9946    just yet).  */
9947
9948 static HOST_WIDE_INT
9949 field_byte_offset (const_tree decl)
9950 {
9951   HOST_WIDE_INT object_offset_in_bits;
9952   HOST_WIDE_INT bitpos_int;
9953
9954   if (TREE_CODE (decl) == ERROR_MARK)
9955     return 0;
9956
9957   gcc_assert (TREE_CODE (decl) == FIELD_DECL);
9958
9959   /* We cannot yet cope with fields whose positions are variable, so
9960      for now, when we see such things, we simply return 0.  Someday, we may
9961      be able to handle such cases, but it will be damn difficult.  */
9962   if (! host_integerp (bit_position (decl), 0))
9963     return 0;
9964
9965   bitpos_int = int_bit_position (decl);
9966
9967 #ifdef PCC_BITFIELD_TYPE_MATTERS
9968   if (PCC_BITFIELD_TYPE_MATTERS)
9969     {
9970       tree type;
9971       tree field_size_tree;
9972       HOST_WIDE_INT deepest_bitpos;
9973       unsigned HOST_WIDE_INT field_size_in_bits;
9974       unsigned int type_align_in_bits;
9975       unsigned int decl_align_in_bits;
9976       unsigned HOST_WIDE_INT type_size_in_bits;
9977
9978       type = field_type (decl);
9979       field_size_tree = DECL_SIZE (decl);
9980
9981       /* The size could be unspecified if there was an error, or for
9982          a flexible array member.  */
9983       if (! field_size_tree)
9984         field_size_tree = bitsize_zero_node;
9985
9986       /* If we don't know the size of the field, pretend it's a full word.  */
9987       if (host_integerp (field_size_tree, 1))
9988         field_size_in_bits = tree_low_cst (field_size_tree, 1);
9989       else
9990         field_size_in_bits = BITS_PER_WORD;
9991
9992       type_size_in_bits = simple_type_size_in_bits (type);
9993       type_align_in_bits = simple_type_align_in_bits (type);
9994       decl_align_in_bits = simple_decl_align_in_bits (decl);
9995
9996       /* The GCC front-end doesn't make any attempt to keep track of the
9997          starting bit offset (relative to the start of the containing
9998          structure type) of the hypothetical "containing object" for a
9999          bit-field.  Thus, when computing the byte offset value for the
10000          start of the "containing object" of a bit-field, we must deduce
10001          this information on our own. This can be rather tricky to do in
10002          some cases.  For example, handling the following structure type
10003          definition when compiling for an i386/i486 target (which only
10004          aligns long long's to 32-bit boundaries) can be very tricky:
10005
10006          struct S { int field1; long long field2:31; };
10007
10008          Fortunately, there is a simple rule-of-thumb which can be used
10009          in such cases.  When compiling for an i386/i486, GCC will
10010          allocate 8 bytes for the structure shown above.  It decides to
10011          do this based upon one simple rule for bit-field allocation.
10012          GCC allocates each "containing object" for each bit-field at
10013          the first (i.e. lowest addressed) legitimate alignment boundary
10014          (based upon the required minimum alignment for the declared
10015          type of the field) which it can possibly use, subject to the
10016          condition that there is still enough available space remaining
10017          in the containing object (when allocated at the selected point)
10018          to fully accommodate all of the bits of the bit-field itself.
10019
10020          This simple rule makes it obvious why GCC allocates 8 bytes for
10021          each object of the structure type shown above.  When looking
10022          for a place to allocate the "containing object" for `field2',
10023          the compiler simply tries to allocate a 64-bit "containing
10024          object" at each successive 32-bit boundary (starting at zero)
10025          until it finds a place to allocate that 64- bit field such that
10026          at least 31 contiguous (and previously unallocated) bits remain
10027          within that selected 64 bit field.  (As it turns out, for the
10028          example above, the compiler finds it is OK to allocate the
10029          "containing object" 64-bit field at bit-offset zero within the
10030          structure type.)
10031
10032          Here we attempt to work backwards from the limited set of facts
10033          we're given, and we try to deduce from those facts, where GCC
10034          must have believed that the containing object started (within
10035          the structure type). The value we deduce is then used (by the
10036          callers of this routine) to generate DW_AT_location and
10037          DW_AT_bit_offset attributes for fields (both bit-fields and, in
10038          the case of DW_AT_location, regular fields as well).  */
10039
10040       /* Figure out the bit-distance from the start of the structure to
10041          the "deepest" bit of the bit-field.  */
10042       deepest_bitpos = bitpos_int + field_size_in_bits;
10043
10044       /* This is the tricky part.  Use some fancy footwork to deduce
10045          where the lowest addressed bit of the containing object must
10046          be.  */
10047       object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10048
10049       /* Round up to type_align by default.  This works best for
10050          bitfields.  */
10051       object_offset_in_bits
10052         = round_up_to_align (object_offset_in_bits, type_align_in_bits);
10053
10054       if (object_offset_in_bits > bitpos_int)
10055         {
10056           object_offset_in_bits = deepest_bitpos - type_size_in_bits;
10057
10058           /* Round up to decl_align instead.  */
10059           object_offset_in_bits
10060             = round_up_to_align (object_offset_in_bits, decl_align_in_bits);
10061         }
10062     }
10063   else
10064 #endif
10065     object_offset_in_bits = bitpos_int;
10066
10067   return object_offset_in_bits / BITS_PER_UNIT;
10068 }
10069 \f
10070 /* The following routines define various Dwarf attributes and any data
10071    associated with them.  */
10072
10073 /* Add a location description attribute value to a DIE.
10074
10075    This emits location attributes suitable for whole variables and
10076    whole parameters.  Note that the location attributes for struct fields are
10077    generated by the routine `data_member_location_attribute' below.  */
10078
10079 static inline void
10080 add_AT_location_description (dw_die_ref die, enum dwarf_attribute attr_kind,
10081                              dw_loc_descr_ref descr)
10082 {
10083   if (descr != 0)
10084     add_AT_loc (die, attr_kind, descr);
10085 }
10086
10087 /* Attach the specialized form of location attribute used for data members of
10088    struct and union types.  In the special case of a FIELD_DECL node which
10089    represents a bit-field, the "offset" part of this special location
10090    descriptor must indicate the distance in bytes from the lowest-addressed
10091    byte of the containing struct or union type to the lowest-addressed byte of
10092    the "containing object" for the bit-field.  (See the `field_byte_offset'
10093    function above).
10094
10095    For any given bit-field, the "containing object" is a hypothetical object
10096    (of some integral or enum type) within which the given bit-field lives.  The
10097    type of this hypothetical "containing object" is always the same as the
10098    declared type of the individual bit-field itself (for GCC anyway... the
10099    DWARF spec doesn't actually mandate this).  Note that it is the size (in
10100    bytes) of the hypothetical "containing object" which will be given in the
10101    DW_AT_byte_size attribute for this bit-field.  (See the
10102    `byte_size_attribute' function below.)  It is also used when calculating the
10103    value of the DW_AT_bit_offset attribute.  (See the `bit_offset_attribute'
10104    function below.)  */
10105
10106 static void
10107 add_data_member_location_attribute (dw_die_ref die, tree decl)
10108 {
10109   HOST_WIDE_INT offset;
10110   dw_loc_descr_ref loc_descr = 0;
10111
10112   if (TREE_CODE (decl) == TREE_BINFO)
10113     {
10114       /* We're working on the TAG_inheritance for a base class.  */
10115       if (BINFO_VIRTUAL_P (decl) && is_cxx ())
10116         {
10117           /* For C++ virtual bases we can't just use BINFO_OFFSET, as they
10118              aren't at a fixed offset from all (sub)objects of the same
10119              type.  We need to extract the appropriate offset from our
10120              vtable.  The following dwarf expression means
10121
10122                BaseAddr = ObAddr + *((*ObAddr) - Offset)
10123
10124              This is specific to the V3 ABI, of course.  */
10125
10126           dw_loc_descr_ref tmp;
10127
10128           /* Make a copy of the object address.  */
10129           tmp = new_loc_descr (DW_OP_dup, 0, 0);
10130           add_loc_descr (&loc_descr, tmp);
10131
10132           /* Extract the vtable address.  */
10133           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10134           add_loc_descr (&loc_descr, tmp);
10135
10136           /* Calculate the address of the offset.  */
10137           offset = tree_low_cst (BINFO_VPTR_FIELD (decl), 0);
10138           gcc_assert (offset < 0);
10139
10140           tmp = int_loc_descriptor (-offset);
10141           add_loc_descr (&loc_descr, tmp);
10142           tmp = new_loc_descr (DW_OP_minus, 0, 0);
10143           add_loc_descr (&loc_descr, tmp);
10144
10145           /* Extract the offset.  */
10146           tmp = new_loc_descr (DW_OP_deref, 0, 0);
10147           add_loc_descr (&loc_descr, tmp);
10148
10149           /* Add it to the object address.  */
10150           tmp = new_loc_descr (DW_OP_plus, 0, 0);
10151           add_loc_descr (&loc_descr, tmp);
10152         }
10153       else
10154         offset = tree_low_cst (BINFO_OFFSET (decl), 0);
10155     }
10156   else
10157     offset = field_byte_offset (decl);
10158
10159   if (! loc_descr)
10160     {
10161       enum dwarf_location_atom op;
10162
10163       /* The DWARF2 standard says that we should assume that the structure
10164          address is already on the stack, so we can specify a structure field
10165          address by using DW_OP_plus_uconst.  */
10166
10167 #ifdef MIPS_DEBUGGING_INFO
10168       /* ??? The SGI dwarf reader does not handle the DW_OP_plus_uconst
10169          operator correctly.  It works only if we leave the offset on the
10170          stack.  */
10171       op = DW_OP_constu;
10172 #else
10173       op = DW_OP_plus_uconst;
10174 #endif
10175
10176       loc_descr = new_loc_descr (op, offset, 0);
10177     }
10178
10179   add_AT_loc (die, DW_AT_data_member_location, loc_descr);
10180 }
10181
10182 /* Writes integer values to dw_vec_const array.  */
10183
10184 static void
10185 insert_int (HOST_WIDE_INT val, unsigned int size, unsigned char *dest)
10186 {
10187   while (size != 0)
10188     {
10189       *dest++ = val & 0xff;
10190       val >>= 8;
10191       --size;
10192     }
10193 }
10194
10195 /* Reads integers from dw_vec_const array.  Inverse of insert_int.  */
10196
10197 static HOST_WIDE_INT
10198 extract_int (const unsigned char *src, unsigned int size)
10199 {
10200   HOST_WIDE_INT val = 0;
10201
10202   src += size;
10203   while (size != 0)
10204     {
10205       val <<= 8;
10206       val |= *--src & 0xff;
10207       --size;
10208     }
10209   return val;
10210 }
10211
10212 /* Writes floating point values to dw_vec_const array.  */
10213
10214 static void
10215 insert_float (const_rtx rtl, unsigned char *array)
10216 {
10217   REAL_VALUE_TYPE rv;
10218   long val[4];
10219   int i;
10220
10221   REAL_VALUE_FROM_CONST_DOUBLE (rv, rtl);
10222   real_to_target (val, &rv, GET_MODE (rtl));
10223
10224   /* real_to_target puts 32-bit pieces in each long.  Pack them.  */
10225   for (i = 0; i < GET_MODE_SIZE (GET_MODE (rtl)) / 4; i++)
10226     {
10227       insert_int (val[i], 4, array);
10228       array += 4;
10229     }
10230 }
10231
10232 /* Attach a DW_AT_const_value attribute for a variable or a parameter which
10233    does not have a "location" either in memory or in a register.  These
10234    things can arise in GNU C when a constant is passed as an actual parameter
10235    to an inlined function.  They can also arise in C++ where declared
10236    constants do not necessarily get memory "homes".  */
10237
10238 static void
10239 add_const_value_attribute (dw_die_ref die, rtx rtl)
10240 {
10241   switch (GET_CODE (rtl))
10242     {
10243     case CONST_INT:
10244       {
10245         HOST_WIDE_INT val = INTVAL (rtl);
10246
10247         if (val < 0)
10248           add_AT_int (die, DW_AT_const_value, val);
10249         else
10250           add_AT_unsigned (die, DW_AT_const_value, (unsigned HOST_WIDE_INT) val);
10251       }
10252       break;
10253
10254     case CONST_DOUBLE:
10255       /* Note that a CONST_DOUBLE rtx could represent either an integer or a
10256          floating-point constant.  A CONST_DOUBLE is used whenever the
10257          constant requires more than one word in order to be adequately
10258          represented.  We output CONST_DOUBLEs as blocks.  */
10259       {
10260         enum machine_mode mode = GET_MODE (rtl);
10261
10262         if (SCALAR_FLOAT_MODE_P (mode))
10263           {
10264             unsigned int length = GET_MODE_SIZE (mode);
10265             unsigned char *array = ggc_alloc (length);
10266
10267             insert_float (rtl, array);
10268             add_AT_vec (die, DW_AT_const_value, length / 4, 4, array);
10269           }
10270         else
10271           {
10272             /* ??? We really should be using HOST_WIDE_INT throughout.  */
10273             gcc_assert (HOST_BITS_PER_LONG == HOST_BITS_PER_WIDE_INT);
10274
10275             add_AT_long_long (die, DW_AT_const_value,
10276                               CONST_DOUBLE_HIGH (rtl), CONST_DOUBLE_LOW (rtl));
10277           }
10278       }
10279       break;
10280
10281     case CONST_VECTOR:
10282       {
10283         enum machine_mode mode = GET_MODE (rtl);
10284         unsigned int elt_size = GET_MODE_UNIT_SIZE (mode);
10285         unsigned int length = CONST_VECTOR_NUNITS (rtl);
10286         unsigned char *array = ggc_alloc (length * elt_size);
10287         unsigned int i;
10288         unsigned char *p;
10289
10290         switch (GET_MODE_CLASS (mode))
10291           {
10292           case MODE_VECTOR_INT:
10293             for (i = 0, p = array; i < length; i++, p += elt_size)
10294               {
10295                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10296                 HOST_WIDE_INT lo, hi;
10297
10298                 switch (GET_CODE (elt))
10299                   {
10300                   case CONST_INT:
10301                     lo = INTVAL (elt);
10302                     hi = -(lo < 0);
10303                     break;
10304
10305                   case CONST_DOUBLE:
10306                     lo = CONST_DOUBLE_LOW (elt);
10307                     hi = CONST_DOUBLE_HIGH (elt);
10308                     break;
10309
10310                   default:
10311                     gcc_unreachable ();
10312                   }
10313
10314                 if (elt_size <= sizeof (HOST_WIDE_INT))
10315                   insert_int (lo, elt_size, p);
10316                 else
10317                   {
10318                     unsigned char *p0 = p;
10319                     unsigned char *p1 = p + sizeof (HOST_WIDE_INT);
10320
10321                     gcc_assert (elt_size == 2 * sizeof (HOST_WIDE_INT));
10322                     if (WORDS_BIG_ENDIAN)
10323                       {
10324                         p0 = p1;
10325                         p1 = p;
10326                       }
10327                     insert_int (lo, sizeof (HOST_WIDE_INT), p0);
10328                     insert_int (hi, sizeof (HOST_WIDE_INT), p1);
10329                   }
10330               }
10331             break;
10332
10333           case MODE_VECTOR_FLOAT:
10334             for (i = 0, p = array; i < length; i++, p += elt_size)
10335               {
10336                 rtx elt = CONST_VECTOR_ELT (rtl, i);
10337                 insert_float (elt, p);
10338               }
10339             break;
10340
10341           default:
10342             gcc_unreachable ();
10343           }
10344
10345         add_AT_vec (die, DW_AT_const_value, length, elt_size, array);
10346       }
10347       break;
10348
10349     case CONST_STRING:
10350       add_AT_string (die, DW_AT_const_value, XSTR (rtl, 0));
10351       break;
10352
10353     case SYMBOL_REF:
10354     case LABEL_REF:
10355     case CONST:
10356       add_AT_addr (die, DW_AT_const_value, rtl);
10357       VEC_safe_push (rtx, gc, used_rtx_array, rtl);
10358       break;
10359
10360     case PLUS:
10361       /* In cases where an inlined instance of an inline function is passed
10362          the address of an `auto' variable (which is local to the caller) we
10363          can get a situation where the DECL_RTL of the artificial local
10364          variable (for the inlining) which acts as a stand-in for the
10365          corresponding formal parameter (of the inline function) will look
10366          like (plus:SI (reg:SI FRAME_PTR) (const_int ...)).  This is not
10367          exactly a compile-time constant expression, but it isn't the address
10368          of the (artificial) local variable either.  Rather, it represents the
10369          *value* which the artificial local variable always has during its
10370          lifetime.  We currently have no way to represent such quasi-constant
10371          values in Dwarf, so for now we just punt and generate nothing.  */
10372       break;
10373
10374     default:
10375       /* No other kinds of rtx should be possible here.  */
10376       gcc_unreachable ();
10377     }
10378
10379 }
10380
10381 /* Determine whether the evaluation of EXPR references any variables
10382    or functions which aren't otherwise used (and therefore may not be
10383    output).  */
10384 static tree
10385 reference_to_unused (tree * tp, int * walk_subtrees,
10386                      void * data ATTRIBUTE_UNUSED)
10387 {
10388   if (! EXPR_P (*tp) && ! GIMPLE_STMT_P (*tp) && ! CONSTANT_CLASS_P (*tp))
10389     *walk_subtrees = 0;
10390
10391   if (DECL_P (*tp) && ! TREE_PUBLIC (*tp) && ! TREE_USED (*tp)
10392       && ! TREE_ASM_WRITTEN (*tp))
10393     return *tp;
10394   else if (!flag_unit_at_a_time)
10395     return NULL_TREE;
10396   /* ???  The C++ FE emits debug information for using decls, so
10397      putting gcc_unreachable here falls over.  See PR31899.  For now
10398      be conservative.  */
10399   else if (!cgraph_global_info_ready
10400            && (TREE_CODE (*tp) == VAR_DECL || TREE_CODE (*tp) == FUNCTION_DECL))
10401     return *tp;
10402   else if (DECL_P (*tp) && TREE_CODE (*tp) == VAR_DECL)
10403     {
10404       struct varpool_node *node = varpool_node (*tp);
10405       if (!node->needed)
10406         return *tp;
10407     }
10408   else if (DECL_P (*tp) && TREE_CODE (*tp) == FUNCTION_DECL
10409            && (!DECL_EXTERNAL (*tp) || DECL_DECLARED_INLINE_P (*tp)))
10410     {
10411       struct cgraph_node *node = cgraph_node (*tp);
10412       if (!node->output)
10413         return *tp;
10414     }
10415   else if (TREE_CODE (*tp) == STRING_CST && !TREE_ASM_WRITTEN (*tp))
10416     return *tp;
10417
10418   return NULL_TREE;
10419 }
10420
10421 /* Generate an RTL constant from a decl initializer INIT with decl type TYPE,
10422    for use in a later add_const_value_attribute call.  */
10423
10424 static rtx
10425 rtl_for_decl_init (tree init, tree type)
10426 {
10427   rtx rtl = NULL_RTX;
10428
10429   /* If a variable is initialized with a string constant without embedded
10430      zeros, build CONST_STRING.  */
10431   if (TREE_CODE (init) == STRING_CST && TREE_CODE (type) == ARRAY_TYPE)
10432     {
10433       tree enttype = TREE_TYPE (type);
10434       tree domain = TYPE_DOMAIN (type);
10435       enum machine_mode mode = TYPE_MODE (enttype);
10436
10437       if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_SIZE (mode) == 1
10438           && domain
10439           && integer_zerop (TYPE_MIN_VALUE (domain))
10440           && compare_tree_int (TYPE_MAX_VALUE (domain),
10441                                TREE_STRING_LENGTH (init) - 1) == 0
10442           && ((size_t) TREE_STRING_LENGTH (init)
10443               == strlen (TREE_STRING_POINTER (init)) + 1))
10444         rtl = gen_rtx_CONST_STRING (VOIDmode,
10445                                     ggc_strdup (TREE_STRING_POINTER (init)));
10446     }
10447   /* Other aggregates, and complex values, could be represented using
10448      CONCAT: FIXME!  */
10449   else if (AGGREGATE_TYPE_P (type) || TREE_CODE (type) == COMPLEX_TYPE)
10450     ;
10451   /* Vectors only work if their mode is supported by the target.
10452      FIXME: generic vectors ought to work too.  */
10453   else if (TREE_CODE (type) == VECTOR_TYPE && TYPE_MODE (type) == BLKmode)
10454     ;
10455   /* If the initializer is something that we know will expand into an
10456      immediate RTL constant, expand it now.  We must be careful not to
10457      reference variables which won't be output.  */
10458   else if (initializer_constant_valid_p (init, type)
10459            && ! walk_tree (&init, reference_to_unused, NULL, NULL))
10460     {
10461       /* Convert vector CONSTRUCTOR initializers to VECTOR_CST if
10462          possible.  */
10463       if (TREE_CODE (type) == VECTOR_TYPE)
10464         switch (TREE_CODE (init))
10465           {
10466           case VECTOR_CST:
10467             break;
10468           case CONSTRUCTOR:
10469             if (TREE_CONSTANT (init))
10470               {
10471                 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (init);
10472                 bool constant_p = true;
10473                 tree value;
10474                 unsigned HOST_WIDE_INT ix;
10475
10476                 /* Even when ctor is constant, it might contain non-*_CST
10477                    elements (e.g. { 1.0/0.0 - 1.0/0.0, 0.0 }) and those don't
10478                    belong into VECTOR_CST nodes.  */
10479                 FOR_EACH_CONSTRUCTOR_VALUE (elts, ix, value)
10480                   if (!CONSTANT_CLASS_P (value))
10481                     {
10482                       constant_p = false;
10483                       break;
10484                     }
10485
10486                 if (constant_p)
10487                   {
10488                     init = build_vector_from_ctor (type, elts);
10489                     break;
10490                   }
10491               }
10492             /* FALLTHRU */
10493
10494           default:
10495             return NULL;
10496           }
10497
10498       rtl = expand_expr (init, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
10499
10500       /* If expand_expr returns a MEM, it wasn't immediate.  */
10501       gcc_assert (!rtl || !MEM_P (rtl));
10502     }
10503
10504   return rtl;
10505 }
10506
10507 /* Generate RTL for the variable DECL to represent its location.  */
10508
10509 static rtx
10510 rtl_for_decl_location (tree decl)
10511 {
10512   rtx rtl;
10513
10514   /* Here we have to decide where we are going to say the parameter "lives"
10515      (as far as the debugger is concerned).  We only have a couple of
10516      choices.  GCC provides us with DECL_RTL and with DECL_INCOMING_RTL.
10517
10518      DECL_RTL normally indicates where the parameter lives during most of the
10519      activation of the function.  If optimization is enabled however, this
10520      could be either NULL or else a pseudo-reg.  Both of those cases indicate
10521      that the parameter doesn't really live anywhere (as far as the code
10522      generation parts of GCC are concerned) during most of the function's
10523      activation.  That will happen (for example) if the parameter is never
10524      referenced within the function.
10525
10526      We could just generate a location descriptor here for all non-NULL
10527      non-pseudo values of DECL_RTL and ignore all of the rest, but we can be
10528      a little nicer than that if we also consider DECL_INCOMING_RTL in cases
10529      where DECL_RTL is NULL or is a pseudo-reg.
10530
10531      Note however that we can only get away with using DECL_INCOMING_RTL as
10532      a backup substitute for DECL_RTL in certain limited cases.  In cases
10533      where DECL_ARG_TYPE (decl) indicates the same type as TREE_TYPE (decl),
10534      we can be sure that the parameter was passed using the same type as it is
10535      declared to have within the function, and that its DECL_INCOMING_RTL
10536      points us to a place where a value of that type is passed.
10537
10538      In cases where DECL_ARG_TYPE (decl) and TREE_TYPE (decl) are different,
10539      we cannot (in general) use DECL_INCOMING_RTL as a substitute for DECL_RTL
10540      because in these cases DECL_INCOMING_RTL points us to a value of some
10541      type which is *different* from the type of the parameter itself.  Thus,
10542      if we tried to use DECL_INCOMING_RTL to generate a location attribute in
10543      such cases, the debugger would end up (for example) trying to fetch a
10544      `float' from a place which actually contains the first part of a
10545      `double'.  That would lead to really incorrect and confusing
10546      output at debug-time.
10547
10548      So, in general, we *do not* use DECL_INCOMING_RTL as a backup for DECL_RTL
10549      in cases where DECL_ARG_TYPE (decl) != TREE_TYPE (decl).  There
10550      are a couple of exceptions however.  On little-endian machines we can
10551      get away with using DECL_INCOMING_RTL even when DECL_ARG_TYPE (decl) is
10552      not the same as TREE_TYPE (decl), but only when DECL_ARG_TYPE (decl) is
10553      an integral type that is smaller than TREE_TYPE (decl). These cases arise
10554      when (on a little-endian machine) a non-prototyped function has a
10555      parameter declared to be of type `short' or `char'.  In such cases,
10556      TREE_TYPE (decl) will be `short' or `char', DECL_ARG_TYPE (decl) will
10557      be `int', and DECL_INCOMING_RTL will point to the lowest-order byte of the
10558      passed `int' value.  If the debugger then uses that address to fetch
10559      a `short' or a `char' (on a little-endian machine) the result will be
10560      the correct data, so we allow for such exceptional cases below.
10561
10562      Note that our goal here is to describe the place where the given formal
10563      parameter lives during most of the function's activation (i.e. between the
10564      end of the prologue and the start of the epilogue).  We'll do that as best
10565      as we can. Note however that if the given formal parameter is modified
10566      sometime during the execution of the function, then a stack backtrace (at
10567      debug-time) will show the function as having been called with the *new*
10568      value rather than the value which was originally passed in.  This happens
10569      rarely enough that it is not a major problem, but it *is* a problem, and
10570      I'd like to fix it.
10571
10572      A future version of dwarf2out.c may generate two additional attributes for
10573      any given DW_TAG_formal_parameter DIE which will describe the "passed
10574      type" and the "passed location" for the given formal parameter in addition
10575      to the attributes we now generate to indicate the "declared type" and the
10576      "active location" for each parameter.  This additional set of attributes
10577      could be used by debuggers for stack backtraces. Separately, note that
10578      sometimes DECL_RTL can be NULL and DECL_INCOMING_RTL can be NULL also.
10579      This happens (for example) for inlined-instances of inline function formal
10580      parameters which are never referenced.  This really shouldn't be
10581      happening.  All PARM_DECL nodes should get valid non-NULL
10582      DECL_INCOMING_RTL values.  FIXME.  */
10583
10584   /* Use DECL_RTL as the "location" unless we find something better.  */
10585   rtl = DECL_RTL_IF_SET (decl);
10586
10587   /* When generating abstract instances, ignore everything except
10588      constants, symbols living in memory, and symbols living in
10589      fixed registers.  */
10590   if (! reload_completed)
10591     {
10592       if (rtl
10593           && (CONSTANT_P (rtl)
10594               || (MEM_P (rtl)
10595                   && CONSTANT_P (XEXP (rtl, 0)))
10596               || (REG_P (rtl)
10597                   && TREE_CODE (decl) == VAR_DECL
10598                   && TREE_STATIC (decl))))
10599         {
10600           rtl = targetm.delegitimize_address (rtl);
10601           return rtl;
10602         }
10603       rtl = NULL_RTX;
10604     }
10605   else if (TREE_CODE (decl) == PARM_DECL)
10606     {
10607       if (rtl == NULL_RTX || is_pseudo_reg (rtl))
10608         {
10609           tree declared_type = TREE_TYPE (decl);
10610           tree passed_type = DECL_ARG_TYPE (decl);
10611           enum machine_mode dmode = TYPE_MODE (declared_type);
10612           enum machine_mode pmode = TYPE_MODE (passed_type);
10613
10614           /* This decl represents a formal parameter which was optimized out.
10615              Note that DECL_INCOMING_RTL may be NULL in here, but we handle
10616              all cases where (rtl == NULL_RTX) just below.  */
10617           if (dmode == pmode)
10618             rtl = DECL_INCOMING_RTL (decl);
10619           else if (SCALAR_INT_MODE_P (dmode)
10620                    && GET_MODE_SIZE (dmode) <= GET_MODE_SIZE (pmode)
10621                    && DECL_INCOMING_RTL (decl))
10622             {
10623               rtx inc = DECL_INCOMING_RTL (decl);
10624               if (REG_P (inc))
10625                 rtl = inc;
10626               else if (MEM_P (inc))
10627                 {
10628                   if (BYTES_BIG_ENDIAN)
10629                     rtl = adjust_address_nv (inc, dmode,
10630                                              GET_MODE_SIZE (pmode)
10631                                              - GET_MODE_SIZE (dmode));
10632                   else
10633                     rtl = inc;
10634                 }
10635             }
10636         }
10637
10638       /* If the parm was passed in registers, but lives on the stack, then
10639          make a big endian correction if the mode of the type of the
10640          parameter is not the same as the mode of the rtl.  */
10641       /* ??? This is the same series of checks that are made in dbxout.c before
10642          we reach the big endian correction code there.  It isn't clear if all
10643          of these checks are necessary here, but keeping them all is the safe
10644          thing to do.  */
10645       else if (MEM_P (rtl)
10646                && XEXP (rtl, 0) != const0_rtx
10647                && ! CONSTANT_P (XEXP (rtl, 0))
10648                /* Not passed in memory.  */
10649                && !MEM_P (DECL_INCOMING_RTL (decl))
10650                /* Not passed by invisible reference.  */
10651                && (!REG_P (XEXP (rtl, 0))
10652                    || REGNO (XEXP (rtl, 0)) == HARD_FRAME_POINTER_REGNUM
10653                    || REGNO (XEXP (rtl, 0)) == STACK_POINTER_REGNUM
10654 #if ARG_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10655                    || REGNO (XEXP (rtl, 0)) == ARG_POINTER_REGNUM
10656 #endif
10657                      )
10658                /* Big endian correction check.  */
10659                && BYTES_BIG_ENDIAN
10660                && TYPE_MODE (TREE_TYPE (decl)) != GET_MODE (rtl)
10661                && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)))
10662                    < UNITS_PER_WORD))
10663         {
10664           int offset = (UNITS_PER_WORD
10665                         - GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl))));
10666
10667           rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10668                              plus_constant (XEXP (rtl, 0), offset));
10669         }
10670     }
10671   else if (TREE_CODE (decl) == VAR_DECL
10672            && rtl
10673            && MEM_P (rtl)
10674            && GET_MODE (rtl) != TYPE_MODE (TREE_TYPE (decl))
10675            && BYTES_BIG_ENDIAN)
10676     {
10677       int rsize = GET_MODE_SIZE (GET_MODE (rtl));
10678       int dsize = GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (decl)));
10679
10680       /* If a variable is declared "register" yet is smaller than
10681          a register, then if we store the variable to memory, it
10682          looks like we're storing a register-sized value, when in
10683          fact we are not.  We need to adjust the offset of the
10684          storage location to reflect the actual value's bytes,
10685          else gdb will not be able to display it.  */
10686       if (rsize > dsize)
10687         rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (decl)),
10688                            plus_constant (XEXP (rtl, 0), rsize-dsize));
10689     }
10690
10691   /* A variable with no DECL_RTL but a DECL_INITIAL is a compile-time constant,
10692      and will have been substituted directly into all expressions that use it.
10693      C does not have such a concept, but C++ and other languages do.  */
10694   if (!rtl && TREE_CODE (decl) == VAR_DECL && DECL_INITIAL (decl))
10695     rtl = rtl_for_decl_init (DECL_INITIAL (decl), TREE_TYPE (decl));
10696
10697   if (rtl)
10698     rtl = targetm.delegitimize_address (rtl);
10699
10700   /* If we don't look past the constant pool, we risk emitting a
10701      reference to a constant pool entry that isn't referenced from
10702      code, and thus is not emitted.  */
10703   if (rtl)
10704     rtl = avoid_constant_pool_reference (rtl);
10705
10706   return rtl;
10707 }
10708
10709 /* We need to figure out what section we should use as the base for the
10710    address ranges where a given location is valid.
10711    1. If this particular DECL has a section associated with it, use that.
10712    2. If this function has a section associated with it, use that.
10713    3. Otherwise, use the text section.
10714    XXX: If you split a variable across multiple sections, we won't notice.  */
10715
10716 static const char *
10717 secname_for_decl (const_tree decl)
10718 {
10719   const char *secname;
10720
10721   if (VAR_OR_FUNCTION_DECL_P (decl) && DECL_SECTION_NAME (decl))
10722     {
10723       tree sectree = DECL_SECTION_NAME (decl);
10724       secname = TREE_STRING_POINTER (sectree);
10725     }
10726   else if (current_function_decl && DECL_SECTION_NAME (current_function_decl))
10727     {
10728       tree sectree = DECL_SECTION_NAME (current_function_decl);
10729       secname = TREE_STRING_POINTER (sectree);
10730     }
10731   else if (cfun && in_cold_section_p)
10732     secname = cfun->cold_section_label;
10733   else
10734     secname = text_section_label;
10735
10736   return secname;
10737 }
10738
10739 /* Generate *either* a DW_AT_location attribute or else a DW_AT_const_value
10740    data attribute for a variable or a parameter.  We generate the
10741    DW_AT_const_value attribute only in those cases where the given variable
10742    or parameter does not have a true "location" either in memory or in a
10743    register.  This can happen (for example) when a constant is passed as an
10744    actual argument in a call to an inline function.  (It's possible that
10745    these things can crop up in other ways also.)  Note that one type of
10746    constant value which can be passed into an inlined function is a constant
10747    pointer.  This can happen for example if an actual argument in an inlined
10748    function call evaluates to a compile-time constant address.  */
10749
10750 static void
10751 add_location_or_const_value_attribute (dw_die_ref die, tree decl,
10752                                        enum dwarf_attribute attr)
10753 {
10754   rtx rtl;
10755   dw_loc_descr_ref descr;
10756   var_loc_list *loc_list;
10757   struct var_loc_node *node;
10758   if (TREE_CODE (decl) == ERROR_MARK)
10759     return;
10760
10761   gcc_assert (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL
10762               || TREE_CODE (decl) == RESULT_DECL);
10763
10764   /* See if we possibly have multiple locations for this variable.  */
10765   loc_list = lookup_decl_loc (decl);
10766
10767   /* If it truly has multiple locations, the first and last node will
10768      differ.  */
10769   if (loc_list && loc_list->first != loc_list->last)
10770     {
10771       const char *endname, *secname;
10772       dw_loc_list_ref list;
10773       rtx varloc;
10774       enum var_init_status initialized;
10775
10776       /* Now that we know what section we are using for a base,
10777          actually construct the list of locations.
10778          The first location information is what is passed to the
10779          function that creates the location list, and the remaining
10780          locations just get added on to that list.
10781          Note that we only know the start address for a location
10782          (IE location changes), so to build the range, we use
10783          the range [current location start, next location start].
10784          This means we have to special case the last node, and generate
10785          a range of [last location start, end of function label].  */
10786
10787       node = loc_list->first;
10788       varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10789       secname = secname_for_decl (decl);
10790
10791       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note))
10792         initialized = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10793       else
10794         initialized = VAR_INIT_STATUS_INITIALIZED;
10795
10796       list = new_loc_list (loc_descriptor (varloc, initialized),
10797                            node->label, node->next->label, secname, 1);
10798       node = node->next;
10799
10800       for (; node->next; node = node->next)
10801         if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10802           {
10803             /* The variable has a location between NODE->LABEL and
10804                NODE->NEXT->LABEL.  */
10805             enum var_init_status initialized =
10806               NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10807             varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10808             add_loc_descr_to_loc_list (&list, 
10809                                        loc_descriptor (varloc, initialized),
10810                                        node->label, node->next->label, secname);
10811           }
10812
10813       /* If the variable has a location at the last label
10814          it keeps its location until the end of function.  */
10815       if (NOTE_VAR_LOCATION_LOC (node->var_loc_note) != NULL_RTX)
10816         {
10817           char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
10818           enum var_init_status initialized =
10819             NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10820
10821           varloc = NOTE_VAR_LOCATION (node->var_loc_note);
10822           if (!current_function_decl)
10823             endname = text_end_label;
10824           else
10825             {
10826               ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
10827                                            current_function_funcdef_no);
10828               endname = ggc_strdup (label_id);
10829             }
10830           add_loc_descr_to_loc_list (&list, 
10831                                      loc_descriptor (varloc, initialized),
10832                                      node->label, endname, secname);
10833         }
10834
10835       /* Finally, add the location list to the DIE, and we are done.  */
10836       add_AT_loc_list (die, attr, list);
10837       return;
10838     }
10839
10840   /* Try to get some constant RTL for this decl, and use that as the value of
10841      the location.  */
10842
10843   rtl = rtl_for_decl_location (decl);
10844   if (rtl && (CONSTANT_P (rtl) || GET_CODE (rtl) == CONST_STRING))
10845     {
10846       add_const_value_attribute (die, rtl);
10847       return;
10848     }
10849
10850   /* If we have tried to generate the location otherwise, and it
10851      didn't work out (we wouldn't be here if we did), and we have a one entry
10852      location list, try generating a location from that.  */
10853   if (loc_list && loc_list->first)
10854     {
10855       enum var_init_status status;
10856       node = loc_list->first;
10857       status = NOTE_VAR_LOCATION_STATUS (node->var_loc_note);
10858       descr = loc_descriptor (NOTE_VAR_LOCATION (node->var_loc_note), status);
10859       if (descr)
10860         {
10861           add_AT_location_description (die, attr, descr);
10862           return;
10863         }
10864     }
10865
10866   /* We couldn't get any rtl, so try directly generating the location
10867      description from the tree.  */
10868   descr = loc_descriptor_from_tree (decl);
10869   if (descr)
10870     {
10871       add_AT_location_description (die, attr, descr);
10872       return;
10873     }
10874   /* None of that worked, so it must not really have a location;
10875      try adding a constant value attribute from the DECL_INITIAL.  */
10876   tree_add_const_value_attribute (die, decl);
10877 }
10878
10879 /* If we don't have a copy of this variable in memory for some reason (such
10880    as a C++ member constant that doesn't have an out-of-line definition),
10881    we should tell the debugger about the constant value.  */
10882
10883 static void
10884 tree_add_const_value_attribute (dw_die_ref var_die, tree decl)
10885 {
10886   tree init = DECL_INITIAL (decl);
10887   tree type = TREE_TYPE (decl);
10888   rtx rtl;
10889
10890   if (TREE_READONLY (decl) && ! TREE_THIS_VOLATILE (decl) && init)
10891     /* OK */;
10892   else
10893     return;
10894
10895   rtl = rtl_for_decl_init (init, type);
10896   if (rtl)
10897     add_const_value_attribute (var_die, rtl);
10898 }
10899
10900 /* Convert the CFI instructions for the current function into a
10901    location list.  This is used for DW_AT_frame_base when we targeting
10902    a dwarf2 consumer that does not support the dwarf3
10903    DW_OP_call_frame_cfa.  OFFSET is a constant to be added to all CFA
10904    expressions.  */
10905
10906 static dw_loc_list_ref
10907 convert_cfa_to_fb_loc_list (HOST_WIDE_INT offset)
10908 {
10909   dw_fde_ref fde;
10910   dw_loc_list_ref list, *list_tail;
10911   dw_cfi_ref cfi;
10912   dw_cfa_location last_cfa, next_cfa;
10913   const char *start_label, *last_label, *section;
10914
10915   fde = &fde_table[fde_table_in_use - 1];
10916
10917   section = secname_for_decl (current_function_decl);
10918   list_tail = &list;
10919   list = NULL;
10920
10921   next_cfa.reg = INVALID_REGNUM;
10922   next_cfa.offset = 0;
10923   next_cfa.indirect = 0;
10924   next_cfa.base_offset = 0;
10925
10926   start_label = fde->dw_fde_begin;
10927
10928   /* ??? Bald assumption that the CIE opcode list does not contain
10929      advance opcodes.  */
10930   for (cfi = cie_cfi_head; cfi; cfi = cfi->dw_cfi_next)
10931     lookup_cfa_1 (cfi, &next_cfa);
10932
10933   last_cfa = next_cfa;
10934   last_label = start_label;
10935
10936   for (cfi = fde->dw_fde_cfi; cfi; cfi = cfi->dw_cfi_next)
10937     switch (cfi->dw_cfi_opc)
10938       {
10939       case DW_CFA_set_loc:
10940       case DW_CFA_advance_loc1:
10941       case DW_CFA_advance_loc2:
10942       case DW_CFA_advance_loc4:
10943         if (!cfa_equal_p (&last_cfa, &next_cfa))
10944           {
10945             *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10946                                        start_label, last_label, section,
10947                                        list == NULL);
10948
10949             list_tail = &(*list_tail)->dw_loc_next;
10950             last_cfa = next_cfa;
10951             start_label = last_label;
10952           }
10953         last_label = cfi->dw_cfi_oprnd1.dw_cfi_addr;
10954         break;
10955
10956       case DW_CFA_advance_loc:
10957         /* The encoding is complex enough that we should never emit this.  */
10958       case DW_CFA_remember_state:
10959       case DW_CFA_restore_state:
10960         /* We don't handle these two in this function.  It would be possible
10961            if it were to be required.  */
10962         gcc_unreachable ();
10963
10964       default:
10965         lookup_cfa_1 (cfi, &next_cfa);
10966         break;
10967       }
10968
10969   if (!cfa_equal_p (&last_cfa, &next_cfa))
10970     {
10971       *list_tail = new_loc_list (build_cfa_loc (&last_cfa, offset),
10972                                  start_label, last_label, section,
10973                                  list == NULL);
10974       list_tail = &(*list_tail)->dw_loc_next;
10975       start_label = last_label;
10976     }
10977   *list_tail = new_loc_list (build_cfa_loc (&next_cfa, offset),
10978                              start_label, fde->dw_fde_end, section,
10979                              list == NULL);
10980
10981   return list;
10982 }
10983
10984 /* Compute a displacement from the "steady-state frame pointer" to the
10985    frame base (often the same as the CFA), and store it in
10986    frame_pointer_fb_offset.  OFFSET is added to the displacement
10987    before the latter is negated.  */
10988
10989 static void
10990 compute_frame_pointer_to_fb_displacement (HOST_WIDE_INT offset)
10991 {
10992   rtx reg, elim;
10993
10994 #ifdef FRAME_POINTER_CFA_OFFSET
10995   reg = frame_pointer_rtx;
10996   offset += FRAME_POINTER_CFA_OFFSET (current_function_decl);
10997 #else
10998   reg = arg_pointer_rtx;
10999   offset += ARG_POINTER_CFA_OFFSET (current_function_decl);
11000 #endif
11001
11002   elim = eliminate_regs (reg, VOIDmode, NULL_RTX);
11003   if (GET_CODE (elim) == PLUS)
11004     {
11005       offset += INTVAL (XEXP (elim, 1));
11006       elim = XEXP (elim, 0);
11007     }
11008   gcc_assert (elim == (frame_pointer_needed ? hard_frame_pointer_rtx
11009                        : stack_pointer_rtx));
11010
11011   frame_pointer_fb_offset = -offset;
11012 }
11013
11014 /* Generate a DW_AT_name attribute given some string value to be included as
11015    the value of the attribute.  */
11016
11017 static void
11018 add_name_attribute (dw_die_ref die, const char *name_string)
11019 {
11020   if (name_string != NULL && *name_string != 0)
11021     {
11022       if (demangle_name_func)
11023         name_string = (*demangle_name_func) (name_string);
11024
11025       add_AT_string (die, DW_AT_name, name_string);
11026     }
11027 }
11028
11029 /* Generate a DW_AT_comp_dir attribute for DIE.  */
11030
11031 static void
11032 add_comp_dir_attribute (dw_die_ref die)
11033 {
11034   const char *wd = get_src_pwd ();
11035   if (wd != NULL)
11036     add_AT_string (die, DW_AT_comp_dir, remap_debug_filename (wd));
11037 }
11038
11039 /* Given a tree node describing an array bound (either lower or upper) output
11040    a representation for that bound.  */
11041
11042 static void
11043 add_bound_info (dw_die_ref subrange_die, enum dwarf_attribute bound_attr, tree bound)
11044 {
11045   switch (TREE_CODE (bound))
11046     {
11047     case ERROR_MARK:
11048       return;
11049
11050     /* All fixed-bounds are represented by INTEGER_CST nodes.  */
11051     case INTEGER_CST:
11052       if (! host_integerp (bound, 0)
11053           || (bound_attr == DW_AT_lower_bound
11054               && (((is_c_family () || is_java ()) &&  integer_zerop (bound))
11055                   || (is_fortran () && integer_onep (bound)))))
11056         /* Use the default.  */
11057         ;
11058       else
11059         add_AT_unsigned (subrange_die, bound_attr, tree_low_cst (bound, 0));
11060       break;
11061
11062     case CONVERT_EXPR:
11063     case NOP_EXPR:
11064     case NON_LVALUE_EXPR:
11065     case VIEW_CONVERT_EXPR:
11066       add_bound_info (subrange_die, bound_attr, TREE_OPERAND (bound, 0));
11067       break;
11068
11069     case SAVE_EXPR:
11070       break;
11071
11072     case VAR_DECL:
11073     case PARM_DECL:
11074     case RESULT_DECL:
11075       {
11076         dw_die_ref decl_die = lookup_decl_die (bound);
11077
11078         /* ??? Can this happen, or should the variable have been bound
11079            first?  Probably it can, since I imagine that we try to create
11080            the types of parameters in the order in which they exist in
11081            the list, and won't have created a forward reference to a
11082            later parameter.  */
11083         if (decl_die != NULL)
11084           add_AT_die_ref (subrange_die, bound_attr, decl_die);
11085         break;
11086       }
11087
11088     default:
11089       {
11090         /* Otherwise try to create a stack operation procedure to
11091            evaluate the value of the array bound.  */
11092
11093         dw_die_ref ctx, decl_die;
11094         dw_loc_descr_ref loc;
11095
11096         loc = loc_descriptor_from_tree (bound);
11097         if (loc == NULL)
11098           break;
11099
11100         if (current_function_decl == 0)
11101           ctx = comp_unit_die;
11102         else
11103           ctx = lookup_decl_die (current_function_decl);
11104
11105         decl_die = new_die (DW_TAG_variable, ctx, bound);
11106         add_AT_flag (decl_die, DW_AT_artificial, 1);
11107         add_type_attribute (decl_die, TREE_TYPE (bound), 1, 0, ctx);
11108         add_AT_loc (decl_die, DW_AT_location, loc);
11109
11110         add_AT_die_ref (subrange_die, bound_attr, decl_die);
11111         break;
11112       }
11113     }
11114 }
11115
11116 /* Note that the block of subscript information for an array type also
11117    includes information about the element type of type given array type.  */
11118
11119 static void
11120 add_subscript_info (dw_die_ref type_die, tree type)
11121 {
11122 #ifndef MIPS_DEBUGGING_INFO
11123   unsigned dimension_number;
11124 #endif
11125   tree lower, upper;
11126   dw_die_ref subrange_die;
11127
11128   /* The GNU compilers represent multidimensional array types as sequences of
11129      one dimensional array types whose element types are themselves array
11130      types.  Here we squish that down, so that each multidimensional array
11131      type gets only one array_type DIE in the Dwarf debugging info. The draft
11132      Dwarf specification say that we are allowed to do this kind of
11133      compression in C (because there is no difference between an array or
11134      arrays and a multidimensional array in C) but for other source languages
11135      (e.g. Ada) we probably shouldn't do this.  */
11136
11137   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11138      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11139      We work around this by disabling this feature.  See also
11140      gen_array_type_die.  */
11141 #ifndef MIPS_DEBUGGING_INFO
11142   for (dimension_number = 0;
11143        TREE_CODE (type) == ARRAY_TYPE;
11144        type = TREE_TYPE (type), dimension_number++)
11145 #endif
11146     {
11147       tree domain = TYPE_DOMAIN (type);
11148
11149       /* Arrays come in three flavors: Unspecified bounds, fixed bounds,
11150          and (in GNU C only) variable bounds.  Handle all three forms
11151          here.  */
11152       subrange_die = new_die (DW_TAG_subrange_type, type_die, NULL);
11153       if (domain)
11154         {
11155           /* We have an array type with specified bounds.  */
11156           lower = TYPE_MIN_VALUE (domain);
11157           upper = TYPE_MAX_VALUE (domain);
11158
11159           /* Define the index type.  */
11160           if (TREE_TYPE (domain))
11161             {
11162               /* ??? This is probably an Ada unnamed subrange type.  Ignore the
11163                  TREE_TYPE field.  We can't emit debug info for this
11164                  because it is an unnamed integral type.  */
11165               if (TREE_CODE (domain) == INTEGER_TYPE
11166                   && TYPE_NAME (domain) == NULL_TREE
11167                   && TREE_CODE (TREE_TYPE (domain)) == INTEGER_TYPE
11168                   && TYPE_NAME (TREE_TYPE (domain)) == NULL_TREE)
11169                 ;
11170               else
11171                 add_type_attribute (subrange_die, TREE_TYPE (domain), 0, 0,
11172                                     type_die);
11173             }
11174
11175           /* ??? If upper is NULL, the array has unspecified length,
11176              but it does have a lower bound.  This happens with Fortran
11177                dimension arr(N:*)
11178              Since the debugger is definitely going to need to know N
11179              to produce useful results, go ahead and output the lower
11180              bound solo, and hope the debugger can cope.  */
11181
11182           add_bound_info (subrange_die, DW_AT_lower_bound, lower);
11183           if (upper)
11184             add_bound_info (subrange_die, DW_AT_upper_bound, upper);
11185         }
11186
11187       /* Otherwise we have an array type with an unspecified length.  The
11188          DWARF-2 spec does not say how to handle this; let's just leave out the
11189          bounds.  */
11190     }
11191 }
11192
11193 static void
11194 add_byte_size_attribute (dw_die_ref die, tree tree_node)
11195 {
11196   unsigned size;
11197
11198   switch (TREE_CODE (tree_node))
11199     {
11200     case ERROR_MARK:
11201       size = 0;
11202       break;
11203     case ENUMERAL_TYPE:
11204     case RECORD_TYPE:
11205     case UNION_TYPE:
11206     case QUAL_UNION_TYPE:
11207       size = int_size_in_bytes (tree_node);
11208       break;
11209     case FIELD_DECL:
11210       /* For a data member of a struct or union, the DW_AT_byte_size is
11211          generally given as the number of bytes normally allocated for an
11212          object of the *declared* type of the member itself.  This is true
11213          even for bit-fields.  */
11214       size = simple_type_size_in_bits (field_type (tree_node)) / BITS_PER_UNIT;
11215       break;
11216     default:
11217       gcc_unreachable ();
11218     }
11219
11220   /* Note that `size' might be -1 when we get to this point.  If it is, that
11221      indicates that the byte size of the entity in question is variable.  We
11222      have no good way of expressing this fact in Dwarf at the present time,
11223      so just let the -1 pass on through.  */
11224   add_AT_unsigned (die, DW_AT_byte_size, size);
11225 }
11226
11227 /* For a FIELD_DECL node which represents a bit-field, output an attribute
11228    which specifies the distance in bits from the highest order bit of the
11229    "containing object" for the bit-field to the highest order bit of the
11230    bit-field itself.
11231
11232    For any given bit-field, the "containing object" is a hypothetical object
11233    (of some integral or enum type) within which the given bit-field lives.  The
11234    type of this hypothetical "containing object" is always the same as the
11235    declared type of the individual bit-field itself.  The determination of the
11236    exact location of the "containing object" for a bit-field is rather
11237    complicated.  It's handled by the `field_byte_offset' function (above).
11238
11239    Note that it is the size (in bytes) of the hypothetical "containing object"
11240    which will be given in the DW_AT_byte_size attribute for this bit-field.
11241    (See `byte_size_attribute' above).  */
11242
11243 static inline void
11244 add_bit_offset_attribute (dw_die_ref die, tree decl)
11245 {
11246   HOST_WIDE_INT object_offset_in_bytes = field_byte_offset (decl);
11247   tree type = DECL_BIT_FIELD_TYPE (decl);
11248   HOST_WIDE_INT bitpos_int;
11249   HOST_WIDE_INT highest_order_object_bit_offset;
11250   HOST_WIDE_INT highest_order_field_bit_offset;
11251   HOST_WIDE_INT unsigned bit_offset;
11252
11253   /* Must be a field and a bit field.  */
11254   gcc_assert (type && TREE_CODE (decl) == FIELD_DECL);
11255
11256   /* We can't yet handle bit-fields whose offsets are variable, so if we
11257      encounter such things, just return without generating any attribute
11258      whatsoever.  Likewise for variable or too large size.  */
11259   if (! host_integerp (bit_position (decl), 0)
11260       || ! host_integerp (DECL_SIZE (decl), 1))
11261     return;
11262
11263   bitpos_int = int_bit_position (decl);
11264
11265   /* Note that the bit offset is always the distance (in bits) from the
11266      highest-order bit of the "containing object" to the highest-order bit of
11267      the bit-field itself.  Since the "high-order end" of any object or field
11268      is different on big-endian and little-endian machines, the computation
11269      below must take account of these differences.  */
11270   highest_order_object_bit_offset = object_offset_in_bytes * BITS_PER_UNIT;
11271   highest_order_field_bit_offset = bitpos_int;
11272
11273   if (! BYTES_BIG_ENDIAN)
11274     {
11275       highest_order_field_bit_offset += tree_low_cst (DECL_SIZE (decl), 0);
11276       highest_order_object_bit_offset += simple_type_size_in_bits (type);
11277     }
11278
11279   bit_offset
11280     = (! BYTES_BIG_ENDIAN
11281        ? highest_order_object_bit_offset - highest_order_field_bit_offset
11282        : highest_order_field_bit_offset - highest_order_object_bit_offset);
11283
11284   add_AT_unsigned (die, DW_AT_bit_offset, bit_offset);
11285 }
11286
11287 /* For a FIELD_DECL node which represents a bit field, output an attribute
11288    which specifies the length in bits of the given field.  */
11289
11290 static inline void
11291 add_bit_size_attribute (dw_die_ref die, tree decl)
11292 {
11293   /* Must be a field and a bit field.  */
11294   gcc_assert (TREE_CODE (decl) == FIELD_DECL
11295               && DECL_BIT_FIELD_TYPE (decl));
11296
11297   if (host_integerp (DECL_SIZE (decl), 1))
11298     add_AT_unsigned (die, DW_AT_bit_size, tree_low_cst (DECL_SIZE (decl), 1));
11299 }
11300
11301 /* If the compiled language is ANSI C, then add a 'prototyped'
11302    attribute, if arg types are given for the parameters of a function.  */
11303
11304 static inline void
11305 add_prototyped_attribute (dw_die_ref die, tree func_type)
11306 {
11307   if (get_AT_unsigned (comp_unit_die, DW_AT_language) == DW_LANG_C89
11308       && TYPE_ARG_TYPES (func_type) != NULL)
11309     add_AT_flag (die, DW_AT_prototyped, 1);
11310 }
11311
11312 /* Add an 'abstract_origin' attribute below a given DIE.  The DIE is found
11313    by looking in either the type declaration or object declaration
11314    equate table.  */
11315
11316 static inline void
11317 add_abstract_origin_attribute (dw_die_ref die, tree origin)
11318 {
11319   dw_die_ref origin_die = NULL;
11320
11321   if (TREE_CODE (origin) != FUNCTION_DECL)
11322     {
11323       /* We may have gotten separated from the block for the inlined
11324          function, if we're in an exception handler or some such; make
11325          sure that the abstract function has been written out.
11326
11327          Doing this for nested functions is wrong, however; functions are
11328          distinct units, and our context might not even be inline.  */
11329       tree fn = origin;
11330
11331       if (TYPE_P (fn))
11332         fn = TYPE_STUB_DECL (fn);
11333
11334       fn = decl_function_context (fn);
11335       if (fn)
11336         dwarf2out_abstract_function (fn);
11337     }
11338
11339   if (DECL_P (origin))
11340     origin_die = lookup_decl_die (origin);
11341   else if (TYPE_P (origin))
11342     origin_die = lookup_type_die (origin);
11343
11344   /* XXX: Functions that are never lowered don't always have correct block
11345      trees (in the case of java, they simply have no block tree, in some other
11346      languages).  For these functions, there is nothing we can really do to
11347      output correct debug info for inlined functions in all cases.  Rather
11348      than die, we'll just produce deficient debug info now, in that we will
11349      have variables without a proper abstract origin.  In the future, when all
11350      functions are lowered, we should re-add a gcc_assert (origin_die)
11351      here.  */
11352
11353   if (origin_die)
11354       add_AT_die_ref (die, DW_AT_abstract_origin, origin_die);
11355 }
11356
11357 /* We do not currently support the pure_virtual attribute.  */
11358
11359 static inline void
11360 add_pure_or_virtual_attribute (dw_die_ref die, tree func_decl)
11361 {
11362   if (DECL_VINDEX (func_decl))
11363     {
11364       add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
11365
11366       if (host_integerp (DECL_VINDEX (func_decl), 0))
11367         add_AT_loc (die, DW_AT_vtable_elem_location,
11368                     new_loc_descr (DW_OP_constu,
11369                                    tree_low_cst (DECL_VINDEX (func_decl), 0),
11370                                    0));
11371
11372       /* GNU extension: Record what type this method came from originally.  */
11373       if (debug_info_level > DINFO_LEVEL_TERSE)
11374         add_AT_die_ref (die, DW_AT_containing_type,
11375                         lookup_type_die (DECL_CONTEXT (func_decl)));
11376     }
11377 }
11378 \f
11379 /* Add source coordinate attributes for the given decl.  */
11380
11381 static void
11382 add_src_coords_attributes (dw_die_ref die, tree decl)
11383 {
11384   expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
11385
11386   add_AT_file (die, DW_AT_decl_file, lookup_filename (s.file));
11387   add_AT_unsigned (die, DW_AT_decl_line, s.line);
11388 }
11389
11390 /* Add a DW_AT_name attribute and source coordinate attribute for the
11391    given decl, but only if it actually has a name.  */
11392
11393 static void
11394 add_name_and_src_coords_attributes (dw_die_ref die, tree decl)
11395 {
11396   tree decl_name;
11397
11398   decl_name = DECL_NAME (decl);
11399   if (decl_name != NULL && IDENTIFIER_POINTER (decl_name) != NULL)
11400     {
11401       add_name_attribute (die, dwarf2_name (decl, 0));
11402       if (! DECL_ARTIFICIAL (decl))
11403         add_src_coords_attributes (die, decl);
11404
11405       if ((TREE_CODE (decl) == FUNCTION_DECL || TREE_CODE (decl) == VAR_DECL)
11406           && TREE_PUBLIC (decl)
11407           && DECL_ASSEMBLER_NAME (decl) != DECL_NAME (decl)
11408           && !DECL_ABSTRACT (decl)
11409           && !(TREE_CODE (decl) == VAR_DECL && DECL_REGISTER (decl))
11410           && !is_fortran ())
11411         add_AT_string (die, DW_AT_MIPS_linkage_name,
11412                        IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
11413     }
11414
11415 #ifdef VMS_DEBUGGING_INFO
11416   /* Get the function's name, as described by its RTL.  This may be different
11417      from the DECL_NAME name used in the source file.  */
11418   if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
11419     {
11420       add_AT_addr (die, DW_AT_VMS_rtnbeg_pd_address,
11421                    XEXP (DECL_RTL (decl), 0));
11422       VEC_safe_push (tree, gc, used_rtx_array, XEXP (DECL_RTL (decl), 0));
11423     }
11424 #endif
11425 }
11426
11427 /* Push a new declaration scope.  */
11428
11429 static void
11430 push_decl_scope (tree scope)
11431 {
11432   VEC_safe_push (tree, gc, decl_scope_table, scope);
11433 }
11434
11435 /* Pop a declaration scope.  */
11436
11437 static inline void
11438 pop_decl_scope (void)
11439 {
11440   VEC_pop (tree, decl_scope_table);
11441 }
11442
11443 /* Return the DIE for the scope that immediately contains this type.
11444    Non-named types get global scope.  Named types nested in other
11445    types get their containing scope if it's open, or global scope
11446    otherwise.  All other types (i.e. function-local named types) get
11447    the current active scope.  */
11448
11449 static dw_die_ref
11450 scope_die_for (tree t, dw_die_ref context_die)
11451 {
11452   dw_die_ref scope_die = NULL;
11453   tree containing_scope;
11454   int i;
11455
11456   /* Non-types always go in the current scope.  */
11457   gcc_assert (TYPE_P (t));
11458
11459   containing_scope = TYPE_CONTEXT (t);
11460
11461   /* Use the containing namespace if it was passed in (for a declaration).  */
11462   if (containing_scope && TREE_CODE (containing_scope) == NAMESPACE_DECL)
11463     {
11464       if (context_die == lookup_decl_die (containing_scope))
11465         /* OK */;
11466       else
11467         containing_scope = NULL_TREE;
11468     }
11469
11470   /* Ignore function type "scopes" from the C frontend.  They mean that
11471      a tagged type is local to a parmlist of a function declarator, but
11472      that isn't useful to DWARF.  */
11473   if (containing_scope && TREE_CODE (containing_scope) == FUNCTION_TYPE)
11474     containing_scope = NULL_TREE;
11475
11476   if (containing_scope == NULL_TREE)
11477     scope_die = comp_unit_die;
11478   else if (TYPE_P (containing_scope))
11479     {
11480       /* For types, we can just look up the appropriate DIE.  But
11481          first we check to see if we're in the middle of emitting it
11482          so we know where the new DIE should go.  */
11483       for (i = VEC_length (tree, decl_scope_table) - 1; i >= 0; --i)
11484         if (VEC_index (tree, decl_scope_table, i) == containing_scope)
11485           break;
11486
11487       if (i < 0)
11488         {
11489           gcc_assert (debug_info_level <= DINFO_LEVEL_TERSE
11490                       || TREE_ASM_WRITTEN (containing_scope));
11491
11492           /* If none of the current dies are suitable, we get file scope.  */
11493           scope_die = comp_unit_die;
11494         }
11495       else
11496         scope_die = lookup_type_die (containing_scope);
11497     }
11498   else
11499     scope_die = context_die;
11500
11501   return scope_die;
11502 }
11503
11504 /* Returns nonzero if CONTEXT_DIE is internal to a function.  */
11505
11506 static inline int
11507 local_scope_p (dw_die_ref context_die)
11508 {
11509   for (; context_die; context_die = context_die->die_parent)
11510     if (context_die->die_tag == DW_TAG_inlined_subroutine
11511         || context_die->die_tag == DW_TAG_subprogram)
11512       return 1;
11513
11514   return 0;
11515 }
11516
11517 /* Returns nonzero if CONTEXT_DIE is a class or namespace, for deciding
11518    whether or not to treat a DIE in this context as a declaration.  */
11519
11520 static inline int
11521 class_or_namespace_scope_p (dw_die_ref context_die)
11522 {
11523   return (context_die
11524           && (context_die->die_tag == DW_TAG_structure_type
11525               || context_die->die_tag == DW_TAG_class_type
11526               || context_die->die_tag == DW_TAG_interface_type
11527               || context_die->die_tag == DW_TAG_union_type
11528               || context_die->die_tag == DW_TAG_namespace));
11529 }
11530
11531 /* Many forms of DIEs require a "type description" attribute.  This
11532    routine locates the proper "type descriptor" die for the type given
11533    by 'type', and adds a DW_AT_type attribute below the given die.  */
11534
11535 static void
11536 add_type_attribute (dw_die_ref object_die, tree type, int decl_const,
11537                     int decl_volatile, dw_die_ref context_die)
11538 {
11539   enum tree_code code  = TREE_CODE (type);
11540   dw_die_ref type_die  = NULL;
11541
11542   /* ??? If this type is an unnamed subrange type of an integral, floating-point
11543      or fixed-point type, use the inner type.  This is because we have no
11544      support for unnamed types in base_type_die.  This can happen if this is
11545      an Ada subrange type.  Correct solution is emit a subrange type die.  */
11546   if ((code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE)
11547       && TREE_TYPE (type) != 0 && TYPE_NAME (type) == 0)
11548     type = TREE_TYPE (type), code = TREE_CODE (type);
11549
11550   if (code == ERROR_MARK
11551       /* Handle a special case.  For functions whose return type is void, we
11552          generate *no* type attribute.  (Note that no object may have type
11553          `void', so this only applies to function return types).  */
11554       || code == VOID_TYPE)
11555     return;
11556
11557   type_die = modified_type_die (type,
11558                                 decl_const || TYPE_READONLY (type),
11559                                 decl_volatile || TYPE_VOLATILE (type),
11560                                 context_die);
11561
11562   if (type_die != NULL)
11563     add_AT_die_ref (object_die, DW_AT_type, type_die);
11564 }
11565
11566 /* Given an object die, add the calling convention attribute for the
11567    function call type.  */
11568 static void
11569 add_calling_convention_attribute (dw_die_ref subr_die, tree decl)
11570 {
11571   enum dwarf_calling_convention value = DW_CC_normal;
11572
11573   value = targetm.dwarf_calling_convention (TREE_TYPE (decl));
11574
11575   /* DWARF doesn't provide a way to identify a program's source-level
11576      entry point.  DW_AT_calling_convention attributes are only meant
11577      to describe functions' calling conventions.  However, lacking a
11578      better way to signal the Fortran main program, we use this for the
11579      time being, following existing custom.  */
11580   if (is_fortran ()
11581       && !strcmp (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), "MAIN__"))
11582     value = DW_CC_program;
11583
11584   /* Only add the attribute if the backend requests it, and
11585      is not DW_CC_normal.  */
11586   if (value && (value != DW_CC_normal))
11587     add_AT_unsigned (subr_die, DW_AT_calling_convention, value);
11588 }
11589
11590 /* Given a tree pointer to a struct, class, union, or enum type node, return
11591    a pointer to the (string) tag name for the given type, or zero if the type
11592    was declared without a tag.  */
11593
11594 static const char *
11595 type_tag (const_tree type)
11596 {
11597   const char *name = 0;
11598
11599   if (TYPE_NAME (type) != 0)
11600     {
11601       tree t = 0;
11602
11603       /* Find the IDENTIFIER_NODE for the type name.  */
11604       if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
11605         t = TYPE_NAME (type);
11606
11607       /* The g++ front end makes the TYPE_NAME of *each* tagged type point to
11608          a TYPE_DECL node, regardless of whether or not a `typedef' was
11609          involved.  */
11610       else if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11611                && ! DECL_IGNORED_P (TYPE_NAME (type)))
11612         {
11613           /* We want to be extra verbose.  Don't call dwarf_name if
11614              DECL_NAME isn't set.  The default hook for decl_printable_name
11615              doesn't like that, and in this context it's correct to return
11616              0, instead of "<anonymous>" or the like.  */
11617           if (DECL_NAME (TYPE_NAME (type)))
11618             name = lang_hooks.dwarf_name (TYPE_NAME (type), 2);
11619         }
11620
11621       /* Now get the name as a string, or invent one.  */
11622       if (!name && t != 0)
11623         name = IDENTIFIER_POINTER (t);
11624     }
11625
11626   return (name == 0 || *name == '\0') ? 0 : name;
11627 }
11628
11629 /* Return the type associated with a data member, make a special check
11630    for bit field types.  */
11631
11632 static inline tree
11633 member_declared_type (const_tree member)
11634 {
11635   return (DECL_BIT_FIELD_TYPE (member)
11636           ? DECL_BIT_FIELD_TYPE (member) : TREE_TYPE (member));
11637 }
11638
11639 /* Get the decl's label, as described by its RTL. This may be different
11640    from the DECL_NAME name used in the source file.  */
11641
11642 #if 0
11643 static const char *
11644 decl_start_label (tree decl)
11645 {
11646   rtx x;
11647   const char *fnname;
11648
11649   x = DECL_RTL (decl);
11650   gcc_assert (MEM_P (x));
11651
11652   x = XEXP (x, 0);
11653   gcc_assert (GET_CODE (x) == SYMBOL_REF);
11654
11655   fnname = XSTR (x, 0);
11656   return fnname;
11657 }
11658 #endif
11659 \f
11660 /* These routines generate the internal representation of the DIE's for
11661    the compilation unit.  Debugging information is collected by walking
11662    the declaration trees passed in from dwarf2out_decl().  */
11663
11664 static void
11665 gen_array_type_die (tree type, dw_die_ref context_die)
11666 {
11667   dw_die_ref scope_die = scope_die_for (type, context_die);
11668   dw_die_ref array_die;
11669   tree element_type;
11670
11671   /* ??? The SGI dwarf reader fails for array of array of enum types unless
11672      the inner array type comes before the outer array type.  Thus we must
11673      call gen_type_die before we call new_die.  See below also.  */
11674 #ifdef MIPS_DEBUGGING_INFO
11675   gen_type_die (TREE_TYPE (type), context_die);
11676 #endif
11677
11678   array_die = new_die (DW_TAG_array_type, scope_die, type);
11679   add_name_attribute (array_die, type_tag (type));
11680   equate_type_number_to_die (type, array_die);
11681
11682   if (TREE_CODE (type) == VECTOR_TYPE)
11683     {
11684       /* The frontend feeds us a representation for the vector as a struct
11685          containing an array.  Pull out the array type.  */
11686       type = TREE_TYPE (TYPE_FIELDS (TYPE_DEBUG_REPRESENTATION_TYPE (type)));
11687       add_AT_flag (array_die, DW_AT_GNU_vector, 1);
11688     }
11689
11690   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11691   if (is_fortran ()
11692       && TREE_CODE (type) == ARRAY_TYPE
11693       && TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
11694     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11695
11696 #if 0
11697   /* We default the array ordering.  SDB will probably do
11698      the right things even if DW_AT_ordering is not present.  It's not even
11699      an issue until we start to get into multidimensional arrays anyway.  If
11700      SDB is ever caught doing the Wrong Thing for multi-dimensional arrays,
11701      then we'll have to put the DW_AT_ordering attribute back in.  (But if
11702      and when we find out that we need to put these in, we will only do so
11703      for multidimensional arrays.  */
11704   add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_row_major);
11705 #endif
11706
11707 #ifdef MIPS_DEBUGGING_INFO
11708   /* The SGI compilers handle arrays of unknown bound by setting
11709      AT_declaration and not emitting any subrange DIEs.  */
11710   if (! TYPE_DOMAIN (type))
11711     add_AT_flag (array_die, DW_AT_declaration, 1);
11712   else
11713 #endif
11714     add_subscript_info (array_die, type);
11715
11716   /* Add representation of the type of the elements of this array type.  */
11717   element_type = TREE_TYPE (type);
11718
11719   /* ??? The SGI dwarf reader fails for multidimensional arrays with a
11720      const enum type.  E.g. const enum machine_mode insn_operand_mode[2][10].
11721      We work around this by disabling this feature.  See also
11722      add_subscript_info.  */
11723 #ifndef MIPS_DEBUGGING_INFO
11724   while (TREE_CODE (element_type) == ARRAY_TYPE)
11725     element_type = TREE_TYPE (element_type);
11726
11727   gen_type_die (element_type, context_die);
11728 #endif
11729
11730   add_type_attribute (array_die, element_type, 0, 0, context_die);
11731
11732   if (get_AT (array_die, DW_AT_name))
11733     add_pubtype (type, array_die);
11734 }
11735
11736 static dw_loc_descr_ref
11737 descr_info_loc (tree val, tree base_decl)
11738 {
11739   HOST_WIDE_INT size;
11740   dw_loc_descr_ref loc, loc2;
11741   enum dwarf_location_atom op;
11742
11743   if (val == base_decl)
11744     return new_loc_descr (DW_OP_push_object_address, 0, 0);
11745
11746   switch (TREE_CODE (val))
11747     {
11748     case NOP_EXPR:
11749     case CONVERT_EXPR:
11750       return descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11751     case INTEGER_CST:
11752       if (host_integerp (val, 0))
11753         return int_loc_descriptor (tree_low_cst (val, 0));
11754       break;
11755     case INDIRECT_REF:
11756       size = int_size_in_bytes (TREE_TYPE (val));
11757       if (size < 0)
11758         break;
11759       loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11760       if (!loc)
11761         break;
11762       if (size == DWARF2_ADDR_SIZE)
11763         add_loc_descr (&loc, new_loc_descr (DW_OP_deref, 0, 0));
11764       else
11765         add_loc_descr (&loc, new_loc_descr (DW_OP_deref_size, size, 0));
11766       return loc;
11767     case POINTER_PLUS_EXPR:
11768     case PLUS_EXPR:
11769       if (host_integerp (TREE_OPERAND (val, 1), 1)
11770           && (unsigned HOST_WIDE_INT) tree_low_cst (TREE_OPERAND (val, 1), 1)
11771              < 16384)
11772         {
11773           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11774           if (!loc)
11775             break;
11776           add_loc_descr (&loc,
11777                          new_loc_descr (DW_OP_plus_uconst,
11778                                         tree_low_cst (TREE_OPERAND (val, 1),
11779                                                       1), 0));
11780         }
11781       else
11782         {
11783           op = DW_OP_plus;
11784         do_binop:
11785           loc = descr_info_loc (TREE_OPERAND (val, 0), base_decl);
11786           if (!loc)
11787             break;
11788           loc2 = descr_info_loc (TREE_OPERAND (val, 1), base_decl);
11789           if (!loc2)
11790             break;
11791           add_loc_descr (&loc, loc2);
11792           add_loc_descr (&loc2, new_loc_descr (op, 0, 0));
11793         }
11794       return loc;
11795     case MINUS_EXPR:
11796       op = DW_OP_minus;
11797       goto do_binop;
11798     case MULT_EXPR:
11799       op = DW_OP_mul;
11800       goto do_binop;
11801     case EQ_EXPR:
11802       op = DW_OP_eq;
11803       goto do_binop;
11804     case NE_EXPR:
11805       op = DW_OP_ne;
11806       goto do_binop;
11807     default:
11808       break;
11809     }
11810   return NULL;
11811 }
11812
11813 static void
11814 add_descr_info_field (dw_die_ref die, enum dwarf_attribute attr,
11815                       tree val, tree base_decl)
11816 {
11817   dw_loc_descr_ref loc;
11818
11819   if (host_integerp (val, 0))
11820     {
11821       add_AT_unsigned (die, attr, tree_low_cst (val, 0));
11822       return;
11823     }
11824
11825   loc = descr_info_loc (val, base_decl);
11826   if (!loc)
11827     return;
11828
11829   add_AT_loc (die, attr, loc);
11830 }
11831
11832 /* This routine generates DIE for array with hidden descriptor, details
11833    are filled into *info by a langhook.  */
11834
11835 static void
11836 gen_descr_array_type_die (tree type, struct array_descr_info *info,
11837                           dw_die_ref context_die)
11838 {
11839   dw_die_ref scope_die = scope_die_for (type, context_die);
11840   dw_die_ref array_die;
11841   int dim;
11842
11843   array_die = new_die (DW_TAG_array_type, scope_die, type);
11844   add_name_attribute (array_die, type_tag (type));
11845   equate_type_number_to_die (type, array_die);
11846
11847   /* For Fortran multidimensional arrays use DW_ORD_col_major ordering.  */
11848   if (is_fortran ()
11849       && info->ndimensions >= 2)
11850     add_AT_unsigned (array_die, DW_AT_ordering, DW_ORD_col_major);
11851
11852   if (info->data_location)
11853     add_descr_info_field (array_die, DW_AT_data_location, info->data_location,
11854                           info->base_decl);
11855   if (info->associated)
11856     add_descr_info_field (array_die, DW_AT_associated, info->associated,
11857                           info->base_decl);
11858   if (info->allocated)
11859     add_descr_info_field (array_die, DW_AT_allocated, info->allocated,
11860                           info->base_decl);
11861
11862   for (dim = 0; dim < info->ndimensions; dim++)
11863     {
11864       dw_die_ref subrange_die
11865         = new_die (DW_TAG_subrange_type, array_die, NULL);
11866
11867       if (info->dimen[dim].lower_bound)
11868         {
11869           /* If it is the default value, omit it.  */
11870           if ((is_c_family () || is_java ())
11871               && integer_zerop (info->dimen[dim].lower_bound))
11872             ;
11873           else if (is_fortran ()
11874                    && integer_onep (info->dimen[dim].lower_bound))
11875             ;
11876           else
11877             add_descr_info_field (subrange_die, DW_AT_lower_bound,
11878                                   info->dimen[dim].lower_bound,
11879                                   info->base_decl);
11880         }
11881       if (info->dimen[dim].upper_bound)
11882         add_descr_info_field (subrange_die, DW_AT_upper_bound,
11883                               info->dimen[dim].upper_bound,
11884                               info->base_decl);
11885       if (info->dimen[dim].stride)
11886         add_descr_info_field (subrange_die, DW_AT_byte_stride,
11887                               info->dimen[dim].stride,
11888                               info->base_decl);
11889     }
11890
11891   gen_type_die (info->element_type, context_die);
11892   add_type_attribute (array_die, info->element_type, 0, 0, context_die);
11893
11894   if (get_AT (array_die, DW_AT_name))
11895     add_pubtype (type, array_die);
11896 }
11897
11898 #if 0
11899 static void
11900 gen_entry_point_die (tree decl, dw_die_ref context_die)
11901 {
11902   tree origin = decl_ultimate_origin (decl);
11903   dw_die_ref decl_die = new_die (DW_TAG_entry_point, context_die, decl);
11904
11905   if (origin != NULL)
11906     add_abstract_origin_attribute (decl_die, origin);
11907   else
11908     {
11909       add_name_and_src_coords_attributes (decl_die, decl);
11910       add_type_attribute (decl_die, TREE_TYPE (TREE_TYPE (decl)),
11911                           0, 0, context_die);
11912     }
11913
11914   if (DECL_ABSTRACT (decl))
11915     equate_decl_number_to_die (decl, decl_die);
11916   else
11917     add_AT_lbl_id (decl_die, DW_AT_low_pc, decl_start_label (decl));
11918 }
11919 #endif
11920
11921 /* Walk through the list of incomplete types again, trying once more to
11922    emit full debugging info for them.  */
11923
11924 static void
11925 retry_incomplete_types (void)
11926 {
11927   int i;
11928
11929   for (i = VEC_length (tree, incomplete_types) - 1; i >= 0; i--)
11930     gen_type_die (VEC_index (tree, incomplete_types, i), comp_unit_die);
11931 }
11932
11933 /* Generate a DIE to represent an inlined instance of an enumeration type.  */
11934
11935 static void
11936 gen_inlined_enumeration_type_die (tree type, dw_die_ref context_die)
11937 {
11938   dw_die_ref type_die = new_die (DW_TAG_enumeration_type, context_die, type);
11939
11940   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11941      be incomplete and such types are not marked.  */
11942   add_abstract_origin_attribute (type_die, type);
11943 }
11944
11945 /* Determine what tag to use for a record type.  */
11946
11947 static enum dwarf_tag
11948 record_type_tag (tree type)
11949 {
11950   if (! lang_hooks.types.classify_record)
11951     return DW_TAG_structure_type;
11952
11953   switch (lang_hooks.types.classify_record (type))
11954     {
11955     case RECORD_IS_STRUCT:
11956       return DW_TAG_structure_type;
11957
11958     case RECORD_IS_CLASS:
11959       return DW_TAG_class_type;
11960
11961     case RECORD_IS_INTERFACE:
11962       return DW_TAG_interface_type;
11963
11964     default:
11965       gcc_unreachable ();
11966     }
11967 }
11968
11969 /* Generate a DIE to represent an inlined instance of a structure type.  */
11970
11971 static void
11972 gen_inlined_structure_type_die (tree type, dw_die_ref context_die)
11973 {
11974   dw_die_ref type_die = new_die (record_type_tag (type), context_die, type);
11975
11976   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11977      be incomplete and such types are not marked.  */
11978   add_abstract_origin_attribute (type_die, type);
11979 }
11980
11981 /* Generate a DIE to represent an inlined instance of a union type.  */
11982
11983 static void
11984 gen_inlined_union_type_die (tree type, dw_die_ref context_die)
11985 {
11986   dw_die_ref type_die = new_die (DW_TAG_union_type, context_die, type);
11987
11988   /* We do not check for TREE_ASM_WRITTEN (type) being set, as the type may
11989      be incomplete and such types are not marked.  */
11990   add_abstract_origin_attribute (type_die, type);
11991 }
11992
11993 /* Generate a DIE to represent an enumeration type.  Note that these DIEs
11994    include all of the information about the enumeration values also. Each
11995    enumerated type name/value is listed as a child of the enumerated type
11996    DIE.  */
11997
11998 static dw_die_ref
11999 gen_enumeration_type_die (tree type, dw_die_ref context_die)
12000 {
12001   dw_die_ref type_die = lookup_type_die (type);
12002
12003   if (type_die == NULL)
12004     {
12005       type_die = new_die (DW_TAG_enumeration_type,
12006                           scope_die_for (type, context_die), type);
12007       equate_type_number_to_die (type, type_die);
12008       add_name_attribute (type_die, type_tag (type));
12009     }
12010   else if (! TYPE_SIZE (type))
12011     return type_die;
12012   else
12013     remove_AT (type_die, DW_AT_declaration);
12014
12015   /* Handle a GNU C/C++ extension, i.e. incomplete enum types.  If the
12016      given enum type is incomplete, do not generate the DW_AT_byte_size
12017      attribute or the DW_AT_element_list attribute.  */
12018   if (TYPE_SIZE (type))
12019     {
12020       tree link;
12021
12022       TREE_ASM_WRITTEN (type) = 1;
12023       add_byte_size_attribute (type_die, type);
12024       if (TYPE_STUB_DECL (type) != NULL_TREE)
12025         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
12026
12027       /* If the first reference to this type was as the return type of an
12028          inline function, then it may not have a parent.  Fix this now.  */
12029       if (type_die->die_parent == NULL)
12030         add_child_die (scope_die_for (type, context_die), type_die);
12031
12032       for (link = TYPE_VALUES (type);
12033            link != NULL; link = TREE_CHAIN (link))
12034         {
12035           dw_die_ref enum_die = new_die (DW_TAG_enumerator, type_die, link);
12036           tree value = TREE_VALUE (link);
12037
12038           add_name_attribute (enum_die,
12039                               IDENTIFIER_POINTER (TREE_PURPOSE (link)));
12040
12041           if (host_integerp (value, TYPE_UNSIGNED (TREE_TYPE (value))))
12042             /* DWARF2 does not provide a way of indicating whether or
12043                not enumeration constants are signed or unsigned.  GDB
12044                always assumes the values are signed, so we output all
12045                values as if they were signed.  That means that
12046                enumeration constants with very large unsigned values
12047                will appear to have negative values in the debugger.  */
12048             add_AT_int (enum_die, DW_AT_const_value,
12049                         tree_low_cst (value, tree_int_cst_sgn (value) > 0));
12050         }
12051     }
12052   else
12053     add_AT_flag (type_die, DW_AT_declaration, 1);
12054
12055   if (get_AT (type_die, DW_AT_name))
12056     add_pubtype (type, type_die);
12057
12058   return type_die;
12059 }
12060
12061 /* Generate a DIE to represent either a real live formal parameter decl or to
12062    represent just the type of some formal parameter position in some function
12063    type.
12064
12065    Note that this routine is a bit unusual because its argument may be a
12066    ..._DECL node (i.e. either a PARM_DECL or perhaps a VAR_DECL which
12067    represents an inlining of some PARM_DECL) or else some sort of a ..._TYPE
12068    node.  If it's the former then this function is being called to output a
12069    DIE to represent a formal parameter object (or some inlining thereof).  If
12070    it's the latter, then this function is only being called to output a
12071    DW_TAG_formal_parameter DIE to stand as a placeholder for some formal
12072    argument type of some subprogram type.  */
12073
12074 static dw_die_ref
12075 gen_formal_parameter_die (tree node, dw_die_ref context_die)
12076 {
12077   dw_die_ref parm_die
12078     = new_die (DW_TAG_formal_parameter, context_die, node);
12079   tree origin;
12080
12081   switch (TREE_CODE_CLASS (TREE_CODE (node)))
12082     {
12083     case tcc_declaration:
12084       origin = decl_ultimate_origin (node);
12085       if (origin != NULL)
12086         add_abstract_origin_attribute (parm_die, origin);
12087       else
12088         {
12089           tree type = TREE_TYPE (node);
12090           add_name_and_src_coords_attributes (parm_die, node);
12091           if (DECL_BY_REFERENCE (node))
12092             type = TREE_TYPE (type);
12093           add_type_attribute (parm_die, type,
12094                               TREE_READONLY (node),
12095                               TREE_THIS_VOLATILE (node),
12096                               context_die);
12097           if (DECL_ARTIFICIAL (node))
12098             add_AT_flag (parm_die, DW_AT_artificial, 1);
12099         }
12100
12101       equate_decl_number_to_die (node, parm_die);
12102       if (! DECL_ABSTRACT (node))
12103         add_location_or_const_value_attribute (parm_die, node, DW_AT_location);
12104
12105       break;
12106
12107     case tcc_type:
12108       /* We were called with some kind of a ..._TYPE node.  */
12109       add_type_attribute (parm_die, node, 0, 0, context_die);
12110       break;
12111
12112     default:
12113       gcc_unreachable ();
12114     }
12115
12116   return parm_die;
12117 }
12118
12119 /* Generate a special type of DIE used as a stand-in for a trailing ellipsis
12120    at the end of an (ANSI prototyped) formal parameters list.  */
12121
12122 static void
12123 gen_unspecified_parameters_die (tree decl_or_type, dw_die_ref context_die)
12124 {
12125   new_die (DW_TAG_unspecified_parameters, context_die, decl_or_type);
12126 }
12127
12128 /* Generate a list of nameless DW_TAG_formal_parameter DIEs (and perhaps a
12129    DW_TAG_unspecified_parameters DIE) to represent the types of the formal
12130    parameters as specified in some function type specification (except for
12131    those which appear as part of a function *definition*).  */
12132
12133 static void
12134 gen_formal_types_die (tree function_or_method_type, dw_die_ref context_die)
12135 {
12136   tree link;
12137   tree formal_type = NULL;
12138   tree first_parm_type;
12139   tree arg;
12140
12141   if (TREE_CODE (function_or_method_type) == FUNCTION_DECL)
12142     {
12143       arg = DECL_ARGUMENTS (function_or_method_type);
12144       function_or_method_type = TREE_TYPE (function_or_method_type);
12145     }
12146   else
12147     arg = NULL_TREE;
12148
12149   first_parm_type = TYPE_ARG_TYPES (function_or_method_type);
12150
12151   /* Make our first pass over the list of formal parameter types and output a
12152      DW_TAG_formal_parameter DIE for each one.  */
12153   for (link = first_parm_type; link; )
12154     {
12155       dw_die_ref parm_die;
12156
12157       formal_type = TREE_VALUE (link);
12158       if (formal_type == void_type_node)
12159         break;
12160
12161       /* Output a (nameless) DIE to represent the formal parameter itself.  */
12162       parm_die = gen_formal_parameter_die (formal_type, context_die);
12163       if ((TREE_CODE (function_or_method_type) == METHOD_TYPE
12164            && link == first_parm_type)
12165           || (arg && DECL_ARTIFICIAL (arg)))
12166         add_AT_flag (parm_die, DW_AT_artificial, 1);
12167
12168       link = TREE_CHAIN (link);
12169       if (arg)
12170         arg = TREE_CHAIN (arg);
12171     }
12172
12173   /* If this function type has an ellipsis, add a
12174      DW_TAG_unspecified_parameters DIE to the end of the parameter list.  */
12175   if (formal_type != void_type_node)
12176     gen_unspecified_parameters_die (function_or_method_type, context_die);
12177
12178   /* Make our second (and final) pass over the list of formal parameter types
12179      and output DIEs to represent those types (as necessary).  */
12180   for (link = TYPE_ARG_TYPES (function_or_method_type);
12181        link && TREE_VALUE (link);
12182        link = TREE_CHAIN (link))
12183     gen_type_die (TREE_VALUE (link), context_die);
12184 }
12185
12186 /* We want to generate the DIE for TYPE so that we can generate the
12187    die for MEMBER, which has been defined; we will need to refer back
12188    to the member declaration nested within TYPE.  If we're trying to
12189    generate minimal debug info for TYPE, processing TYPE won't do the
12190    trick; we need to attach the member declaration by hand.  */
12191
12192 static void
12193 gen_type_die_for_member (tree type, tree member, dw_die_ref context_die)
12194 {
12195   gen_type_die (type, context_die);
12196
12197   /* If we're trying to avoid duplicate debug info, we may not have
12198      emitted the member decl for this function.  Emit it now.  */
12199   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))
12200       && ! lookup_decl_die (member))
12201     {
12202       dw_die_ref type_die;
12203       gcc_assert (!decl_ultimate_origin (member));
12204
12205       push_decl_scope (type);
12206       type_die = lookup_type_die (type);
12207       if (TREE_CODE (member) == FUNCTION_DECL)
12208         gen_subprogram_die (member, type_die);
12209       else if (TREE_CODE (member) == FIELD_DECL)
12210         {
12211           /* Ignore the nameless fields that are used to skip bits but handle
12212              C++ anonymous unions and structs.  */
12213           if (DECL_NAME (member) != NULL_TREE
12214               || TREE_CODE (TREE_TYPE (member)) == UNION_TYPE
12215               || TREE_CODE (TREE_TYPE (member)) == RECORD_TYPE)
12216             {
12217               gen_type_die (member_declared_type (member), type_die);
12218               gen_field_die (member, type_die);
12219             }
12220         }
12221       else
12222         gen_variable_die (member, type_die);
12223
12224       pop_decl_scope ();
12225     }
12226 }
12227
12228 /* Generate the DWARF2 info for the "abstract" instance of a function which we
12229    may later generate inlined and/or out-of-line instances of.  */
12230
12231 static void
12232 dwarf2out_abstract_function (tree decl)
12233 {
12234   dw_die_ref old_die;
12235   tree save_fn;
12236   tree context;
12237   int was_abstract = DECL_ABSTRACT (decl);
12238
12239   /* Make sure we have the actual abstract inline, not a clone.  */
12240   decl = DECL_ORIGIN (decl);
12241
12242   old_die = lookup_decl_die (decl);
12243   if (old_die && get_AT (old_die, DW_AT_inline))
12244     /* We've already generated the abstract instance.  */
12245     return;
12246
12247   /* Be sure we've emitted the in-class declaration DIE (if any) first, so
12248      we don't get confused by DECL_ABSTRACT.  */
12249   if (debug_info_level > DINFO_LEVEL_TERSE)
12250     {
12251       context = decl_class_context (decl);
12252       if (context)
12253         gen_type_die_for_member
12254           (context, decl, decl_function_context (decl) ? NULL : comp_unit_die);
12255     }
12256
12257   /* Pretend we've just finished compiling this function.  */
12258   save_fn = current_function_decl;
12259   current_function_decl = decl;
12260   push_cfun (DECL_STRUCT_FUNCTION (decl));
12261
12262   set_decl_abstract_flags (decl, 1);
12263   dwarf2out_decl (decl);
12264   if (! was_abstract)
12265     set_decl_abstract_flags (decl, 0);
12266
12267   current_function_decl = save_fn;
12268   pop_cfun ();
12269 }
12270
12271 /* Helper function of premark_used_types() which gets called through
12272    htab_traverse_resize().
12273
12274    Marks the DIE of a given type in *SLOT as perennial, so it never gets
12275    marked as unused by prune_unused_types.  */
12276 static int
12277 premark_used_types_helper (void **slot, void *data ATTRIBUTE_UNUSED)
12278 {
12279   tree type;
12280   dw_die_ref die;
12281
12282   type = *slot;
12283   die = lookup_type_die (type);
12284   if (die != NULL)
12285     die->die_perennial_p = 1;
12286   return 1;
12287 }
12288
12289 /* Mark all members of used_types_hash as perennial.  */
12290 static void
12291 premark_used_types (void)
12292 {
12293   if (cfun && cfun->used_types_hash)
12294     htab_traverse (cfun->used_types_hash, premark_used_types_helper, NULL);
12295 }
12296
12297 /* Generate a DIE to represent a declared function (either file-scope or
12298    block-local).  */
12299
12300 static void
12301 gen_subprogram_die (tree decl, dw_die_ref context_die)
12302 {
12303   char label_id[MAX_ARTIFICIAL_LABEL_BYTES];
12304   tree origin = decl_ultimate_origin (decl);
12305   dw_die_ref subr_die;
12306   tree fn_arg_types;
12307   tree outer_scope;
12308   dw_die_ref old_die = lookup_decl_die (decl);
12309   int declaration = (current_function_decl != decl
12310                      || class_or_namespace_scope_p (context_die));
12311
12312   premark_used_types ();
12313
12314   /* It is possible to have both DECL_ABSTRACT and DECLARATION be true if we
12315      started to generate the abstract instance of an inline, decided to output
12316      its containing class, and proceeded to emit the declaration of the inline
12317      from the member list for the class.  If so, DECLARATION takes priority;
12318      we'll get back to the abstract instance when done with the class.  */
12319
12320   /* The class-scope declaration DIE must be the primary DIE.  */
12321   if (origin && declaration && class_or_namespace_scope_p (context_die))
12322     {
12323       origin = NULL;
12324       gcc_assert (!old_die);
12325     }
12326
12327   /* Now that the C++ front end lazily declares artificial member fns, we
12328      might need to retrofit the declaration into its class.  */
12329   if (!declaration && !origin && !old_die
12330       && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
12331       && !class_or_namespace_scope_p (context_die)
12332       && debug_info_level > DINFO_LEVEL_TERSE)
12333     old_die = force_decl_die (decl);
12334
12335   if (origin != NULL)
12336     {
12337       gcc_assert (!declaration || local_scope_p (context_die));
12338
12339       /* Fixup die_parent for the abstract instance of a nested
12340          inline function.  */
12341       if (old_die && old_die->die_parent == NULL)
12342         add_child_die (context_die, old_die);
12343
12344       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12345       add_abstract_origin_attribute (subr_die, origin);
12346     }
12347   else if (old_die)
12348     {
12349       expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12350       struct dwarf_file_data * file_index = lookup_filename (s.file);
12351
12352       if (!get_AT_flag (old_die, DW_AT_declaration)
12353           /* We can have a normal definition following an inline one in the
12354              case of redefinition of GNU C extern inlines.
12355              It seems reasonable to use AT_specification in this case.  */
12356           && !get_AT (old_die, DW_AT_inline))
12357         {
12358           /* Detect and ignore this case, where we are trying to output
12359              something we have already output.  */
12360           return;
12361         }
12362
12363       /* If the definition comes from the same place as the declaration,
12364          maybe use the old DIE.  We always want the DIE for this function
12365          that has the *_pc attributes to be under comp_unit_die so the
12366          debugger can find it.  We also need to do this for abstract
12367          instances of inlines, since the spec requires the out-of-line copy
12368          to have the same parent.  For local class methods, this doesn't
12369          apply; we just use the old DIE.  */
12370       if ((old_die->die_parent == comp_unit_die || context_die == NULL)
12371           && (DECL_ARTIFICIAL (decl)
12372               || (get_AT_file (old_die, DW_AT_decl_file) == file_index
12373                   && (get_AT_unsigned (old_die, DW_AT_decl_line)
12374                       == (unsigned) s.line))))
12375         {
12376           subr_die = old_die;
12377
12378           /* Clear out the declaration attribute and the formal parameters.
12379              Do not remove all children, because it is possible that this
12380              declaration die was forced using force_decl_die(). In such
12381              cases die that forced declaration die (e.g. TAG_imported_module)
12382              is one of the children that we do not want to remove.  */
12383           remove_AT (subr_die, DW_AT_declaration);
12384           remove_child_TAG (subr_die, DW_TAG_formal_parameter);
12385         }
12386       else
12387         {
12388           subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12389           add_AT_specification (subr_die, old_die);
12390           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12391             add_AT_file (subr_die, DW_AT_decl_file, file_index);
12392           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12393             add_AT_unsigned (subr_die, DW_AT_decl_line, s.line);
12394         }
12395     }
12396   else
12397     {
12398       subr_die = new_die (DW_TAG_subprogram, context_die, decl);
12399
12400       if (TREE_PUBLIC (decl))
12401         add_AT_flag (subr_die, DW_AT_external, 1);
12402
12403       add_name_and_src_coords_attributes (subr_die, decl);
12404       if (debug_info_level > DINFO_LEVEL_TERSE)
12405         {
12406           add_prototyped_attribute (subr_die, TREE_TYPE (decl));
12407           add_type_attribute (subr_die, TREE_TYPE (TREE_TYPE (decl)),
12408                               0, 0, context_die);
12409         }
12410
12411       add_pure_or_virtual_attribute (subr_die, decl);
12412       if (DECL_ARTIFICIAL (decl))
12413         add_AT_flag (subr_die, DW_AT_artificial, 1);
12414
12415       if (TREE_PROTECTED (decl))
12416         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_protected);
12417       else if (TREE_PRIVATE (decl))
12418         add_AT_unsigned (subr_die, DW_AT_accessibility, DW_ACCESS_private);
12419     }
12420
12421   if (declaration)
12422     {
12423       if (!old_die || !get_AT (old_die, DW_AT_inline))
12424         {
12425           add_AT_flag (subr_die, DW_AT_declaration, 1);
12426
12427           /* The first time we see a member function, it is in the context of
12428              the class to which it belongs.  We make sure of this by emitting
12429              the class first.  The next time is the definition, which is
12430              handled above.  The two may come from the same source text.
12431
12432              Note that force_decl_die() forces function declaration die. It is
12433              later reused to represent definition.  */
12434           equate_decl_number_to_die (decl, subr_die);
12435         }
12436     }
12437   else if (DECL_ABSTRACT (decl))
12438     {
12439       if (DECL_DECLARED_INLINE_P (decl))
12440         {
12441           if (cgraph_function_possibly_inlined_p (decl))
12442             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_inlined);
12443           else
12444             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_declared_not_inlined);
12445         }
12446       else
12447         {
12448           if (cgraph_function_possibly_inlined_p (decl))
12449             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_inlined);
12450           else
12451             add_AT_unsigned (subr_die, DW_AT_inline, DW_INL_not_inlined);
12452         }
12453
12454       if (DECL_DECLARED_INLINE_P (decl)
12455           && lookup_attribute ("artificial", DECL_ATTRIBUTES (decl)))
12456         add_AT_flag (subr_die, DW_AT_artificial, 1);
12457
12458       equate_decl_number_to_die (decl, subr_die);
12459     }
12460   else if (!DECL_EXTERNAL (decl))
12461     {
12462       HOST_WIDE_INT cfa_fb_offset;
12463
12464       if (!old_die || !get_AT (old_die, DW_AT_inline))
12465         equate_decl_number_to_die (decl, subr_die);
12466
12467       if (!flag_reorder_blocks_and_partition)
12468         {
12469           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_BEGIN_LABEL,
12470                                        current_function_funcdef_no);
12471           add_AT_lbl_id (subr_die, DW_AT_low_pc, label_id);
12472           ASM_GENERATE_INTERNAL_LABEL (label_id, FUNC_END_LABEL,
12473                                        current_function_funcdef_no);
12474           add_AT_lbl_id (subr_die, DW_AT_high_pc, label_id);
12475
12476           add_pubname (decl, subr_die);
12477           add_arange (decl, subr_die);
12478         }
12479       else
12480         {  /* Do nothing for now; maybe need to duplicate die, one for
12481               hot section and ond for cold section, then use the hot/cold
12482               section begin/end labels to generate the aranges...  */
12483           /*
12484             add_AT_lbl_id (subr_die, DW_AT_low_pc, hot_section_label);
12485             add_AT_lbl_id (subr_die, DW_AT_high_pc, hot_section_end_label);
12486             add_AT_lbl_id (subr_die, DW_AT_lo_user, unlikely_section_label);
12487             add_AT_lbl_id (subr_die, DW_AT_hi_user, cold_section_end_label);
12488
12489             add_pubname (decl, subr_die);
12490             add_arange (decl, subr_die);
12491             add_arange (decl, subr_die);
12492            */
12493         }
12494
12495 #ifdef MIPS_DEBUGGING_INFO
12496       /* Add a reference to the FDE for this routine.  */
12497       add_AT_fde_ref (subr_die, DW_AT_MIPS_fde, current_funcdef_fde);
12498 #endif
12499
12500       cfa_fb_offset = CFA_FRAME_BASE_OFFSET (decl);
12501
12502       /* We define the "frame base" as the function's CFA.  This is more
12503          convenient for several reasons: (1) It's stable across the prologue
12504          and epilogue, which makes it better than just a frame pointer,
12505          (2) With dwarf3, there exists a one-byte encoding that allows us
12506          to reference the .debug_frame data by proxy, but failing that,
12507          (3) We can at least reuse the code inspection and interpretation
12508          code that determines the CFA position at various points in the
12509          function.  */
12510       /* ??? Use some command-line or configury switch to enable the use
12511          of dwarf3 DW_OP_call_frame_cfa.  At present there are no dwarf
12512          consumers that understand it; fall back to "pure" dwarf2 and
12513          convert the CFA data into a location list.  */
12514       {
12515         dw_loc_list_ref list = convert_cfa_to_fb_loc_list (cfa_fb_offset);
12516         if (list->dw_loc_next)
12517           add_AT_loc_list (subr_die, DW_AT_frame_base, list);
12518         else
12519           add_AT_loc (subr_die, DW_AT_frame_base, list->expr);
12520       }
12521
12522       /* Compute a displacement from the "steady-state frame pointer" to
12523          the CFA.  The former is what all stack slots and argument slots
12524          will reference in the rtl; the later is what we've told the
12525          debugger about.  We'll need to adjust all frame_base references
12526          by this displacement.  */
12527       compute_frame_pointer_to_fb_displacement (cfa_fb_offset);
12528
12529       if (cfun->static_chain_decl)
12530         add_AT_location_description (subr_die, DW_AT_static_link,
12531                  loc_descriptor_from_tree (cfun->static_chain_decl));
12532     }
12533
12534   /* Now output descriptions of the arguments for this function. This gets
12535      (unnecessarily?) complex because of the fact that the DECL_ARGUMENT list
12536      for a FUNCTION_DECL doesn't indicate cases where there was a trailing
12537      `...' at the end of the formal parameter list.  In order to find out if
12538      there was a trailing ellipsis or not, we must instead look at the type
12539      associated with the FUNCTION_DECL.  This will be a node of type
12540      FUNCTION_TYPE. If the chain of type nodes hanging off of this
12541      FUNCTION_TYPE node ends with a void_type_node then there should *not* be
12542      an ellipsis at the end.  */
12543
12544   /* In the case where we are describing a mere function declaration, all we
12545      need to do here (and all we *can* do here) is to describe the *types* of
12546      its formal parameters.  */
12547   if (debug_info_level <= DINFO_LEVEL_TERSE)
12548     ;
12549   else if (declaration)
12550     gen_formal_types_die (decl, subr_die);
12551   else
12552     {
12553       /* Generate DIEs to represent all known formal parameters.  */
12554       tree arg_decls = DECL_ARGUMENTS (decl);
12555       tree parm;
12556
12557       /* When generating DIEs, generate the unspecified_parameters DIE
12558          instead if we come across the arg "__builtin_va_alist" */
12559       for (parm = arg_decls; parm; parm = TREE_CHAIN (parm))
12560         if (TREE_CODE (parm) == PARM_DECL)
12561           {
12562             if (DECL_NAME (parm)
12563                 && !strcmp (IDENTIFIER_POINTER (DECL_NAME (parm)),
12564                             "__builtin_va_alist"))
12565               gen_unspecified_parameters_die (parm, subr_die);
12566             else
12567               gen_decl_die (parm, subr_die);
12568           }
12569
12570       /* Decide whether we need an unspecified_parameters DIE at the end.
12571          There are 2 more cases to do this for: 1) the ansi ... declaration -
12572          this is detectable when the end of the arg list is not a
12573          void_type_node 2) an unprototyped function declaration (not a
12574          definition).  This just means that we have no info about the
12575          parameters at all.  */
12576       fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
12577       if (fn_arg_types != NULL)
12578         {
12579           /* This is the prototyped case, check for....  */
12580           if (TREE_VALUE (tree_last (fn_arg_types)) != void_type_node)
12581             gen_unspecified_parameters_die (decl, subr_die);
12582         }
12583       else if (DECL_INITIAL (decl) == NULL_TREE)
12584         gen_unspecified_parameters_die (decl, subr_die);
12585     }
12586
12587   /* Output Dwarf info for all of the stuff within the body of the function
12588      (if it has one - it may be just a declaration).  */
12589   outer_scope = DECL_INITIAL (decl);
12590
12591   /* OUTER_SCOPE is a pointer to the outermost BLOCK node created to represent
12592      a function.  This BLOCK actually represents the outermost binding contour
12593      for the function, i.e. the contour in which the function's formal
12594      parameters and labels get declared. Curiously, it appears that the front
12595      end doesn't actually put the PARM_DECL nodes for the current function onto
12596      the BLOCK_VARS list for this outer scope, but are strung off of the
12597      DECL_ARGUMENTS list for the function instead.
12598
12599      The BLOCK_VARS list for the `outer_scope' does provide us with a list of
12600      the LABEL_DECL nodes for the function however, and we output DWARF info
12601      for those in decls_for_scope.  Just within the `outer_scope' there will be
12602      a BLOCK node representing the function's outermost pair of curly braces,
12603      and any blocks used for the base and member initializers of a C++
12604      constructor function.  */
12605   if (! declaration && TREE_CODE (outer_scope) != ERROR_MARK)
12606     {
12607       /* Emit a DW_TAG_variable DIE for a named return value.  */
12608       if (DECL_NAME (DECL_RESULT (decl)))
12609         gen_decl_die (DECL_RESULT (decl), subr_die);
12610
12611       current_function_has_inlines = 0;
12612       decls_for_scope (outer_scope, subr_die, 0);
12613
12614 #if 0 && defined (MIPS_DEBUGGING_INFO)
12615       if (current_function_has_inlines)
12616         {
12617           add_AT_flag (subr_die, DW_AT_MIPS_has_inlines, 1);
12618           if (! comp_unit_has_inlines)
12619             {
12620               add_AT_flag (comp_unit_die, DW_AT_MIPS_has_inlines, 1);
12621               comp_unit_has_inlines = 1;
12622             }
12623         }
12624 #endif
12625     }
12626   /* Add the calling convention attribute if requested.  */
12627   add_calling_convention_attribute (subr_die, decl);
12628
12629 }
12630
12631 /* Generate a DIE to represent a declared data object.  */
12632
12633 static void
12634 gen_variable_die (tree decl, dw_die_ref context_die)
12635 {
12636   tree origin = decl_ultimate_origin (decl);
12637   dw_die_ref var_die = new_die (DW_TAG_variable, context_die, decl);
12638
12639   dw_die_ref old_die = lookup_decl_die (decl);
12640   int declaration = (DECL_EXTERNAL (decl)
12641                      /* If DECL is COMDAT and has not actually been
12642                         emitted, we cannot take its address; there
12643                         might end up being no definition anywhere in
12644                         the program.  For example, consider the C++
12645                         test case:
12646
12647                           template <class T>
12648                           struct S { static const int i = 7; };
12649
12650                           template <class T>
12651                           const int S<T>::i;
12652
12653                           int f() { return S<int>::i; }
12654
12655                         Here, S<int>::i is not DECL_EXTERNAL, but no
12656                         definition is required, so the compiler will
12657                         not emit a definition.  */
12658                      || (TREE_CODE (decl) == VAR_DECL
12659                          && DECL_COMDAT (decl) && !TREE_ASM_WRITTEN (decl))
12660                      || class_or_namespace_scope_p (context_die));
12661
12662   if (origin != NULL)
12663     add_abstract_origin_attribute (var_die, origin);
12664
12665   /* Loop unrolling can create multiple blocks that refer to the same
12666      static variable, so we must test for the DW_AT_declaration flag.
12667
12668      ??? Loop unrolling/reorder_blocks should perhaps be rewritten to
12669      copy decls and set the DECL_ABSTRACT flag on them instead of
12670      sharing them.
12671
12672      ??? Duplicated blocks have been rewritten to use .debug_ranges.
12673
12674      ??? The declare_in_namespace support causes us to get two DIEs for one
12675      variable, both of which are declarations.  We want to avoid considering
12676      one to be a specification, so we must test that this DIE is not a
12677      declaration.  */
12678   else if (old_die && TREE_STATIC (decl) && ! declaration
12679            && get_AT_flag (old_die, DW_AT_declaration) == 1)
12680     {
12681       /* This is a definition of a C++ class level static.  */
12682       add_AT_specification (var_die, old_die);
12683       if (DECL_NAME (decl))
12684         {
12685           expanded_location s = expand_location (DECL_SOURCE_LOCATION (decl));
12686           struct dwarf_file_data * file_index = lookup_filename (s.file);
12687
12688           if (get_AT_file (old_die, DW_AT_decl_file) != file_index)
12689             add_AT_file (var_die, DW_AT_decl_file, file_index);
12690
12691           if (get_AT_unsigned (old_die, DW_AT_decl_line) != (unsigned) s.line)
12692             add_AT_unsigned (var_die, DW_AT_decl_line, s.line);
12693         }
12694     }
12695   else
12696     {
12697       tree type = TREE_TYPE (decl);
12698       if ((TREE_CODE (decl) == PARM_DECL
12699            || TREE_CODE (decl) == RESULT_DECL)
12700           && DECL_BY_REFERENCE (decl))
12701         type = TREE_TYPE (type);
12702
12703       add_name_and_src_coords_attributes (var_die, decl);
12704       add_type_attribute (var_die, type, TREE_READONLY (decl),
12705                           TREE_THIS_VOLATILE (decl), context_die);
12706
12707       if (TREE_PUBLIC (decl))
12708         add_AT_flag (var_die, DW_AT_external, 1);
12709
12710       if (DECL_ARTIFICIAL (decl))
12711         add_AT_flag (var_die, DW_AT_artificial, 1);
12712
12713       if (TREE_PROTECTED (decl))
12714         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_protected);
12715       else if (TREE_PRIVATE (decl))
12716         add_AT_unsigned (var_die, DW_AT_accessibility, DW_ACCESS_private);
12717     }
12718
12719   if (declaration)
12720     add_AT_flag (var_die, DW_AT_declaration, 1);
12721
12722   if (DECL_ABSTRACT (decl) || declaration)
12723     equate_decl_number_to_die (decl, var_die);
12724
12725   if (! declaration && ! DECL_ABSTRACT (decl))
12726     {
12727       add_location_or_const_value_attribute (var_die, decl, DW_AT_location);
12728       add_pubname (decl, var_die);
12729     }
12730   else
12731     tree_add_const_value_attribute (var_die, decl);
12732 }
12733
12734 /* Generate a DIE to represent a label identifier.  */
12735
12736 static void
12737 gen_label_die (tree decl, dw_die_ref context_die)
12738 {
12739   tree origin = decl_ultimate_origin (decl);
12740   dw_die_ref lbl_die = new_die (DW_TAG_label, context_die, decl);
12741   rtx insn;
12742   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12743
12744   if (origin != NULL)
12745     add_abstract_origin_attribute (lbl_die, origin);
12746   else
12747     add_name_and_src_coords_attributes (lbl_die, decl);
12748
12749   if (DECL_ABSTRACT (decl))
12750     equate_decl_number_to_die (decl, lbl_die);
12751   else
12752     {
12753       insn = DECL_RTL_IF_SET (decl);
12754
12755       /* Deleted labels are programmer specified labels which have been
12756          eliminated because of various optimizations.  We still emit them
12757          here so that it is possible to put breakpoints on them.  */
12758       if (insn
12759           && (LABEL_P (insn)
12760               || ((NOTE_P (insn)
12761                    && NOTE_KIND (insn) == NOTE_INSN_DELETED_LABEL))))
12762         {
12763           /* When optimization is enabled (via -O) some parts of the compiler
12764              (e.g. jump.c and cse.c) may try to delete CODE_LABEL insns which
12765              represent source-level labels which were explicitly declared by
12766              the user.  This really shouldn't be happening though, so catch
12767              it if it ever does happen.  */
12768           gcc_assert (!INSN_DELETED_P (insn));
12769
12770           ASM_GENERATE_INTERNAL_LABEL (label, "L", CODE_LABEL_NUMBER (insn));
12771           add_AT_lbl_id (lbl_die, DW_AT_low_pc, label);
12772         }
12773     }
12774 }
12775
12776 /* A helper function for gen_inlined_subroutine_die.  Add source coordinate
12777    attributes to the DIE for a block STMT, to describe where the inlined
12778    function was called from.  This is similar to add_src_coords_attributes.  */
12779
12780 static inline void
12781 add_call_src_coords_attributes (tree stmt, dw_die_ref die)
12782 {
12783   expanded_location s = expand_location (BLOCK_SOURCE_LOCATION (stmt));
12784
12785   add_AT_file (die, DW_AT_call_file, lookup_filename (s.file));
12786   add_AT_unsigned (die, DW_AT_call_line, s.line);
12787 }
12788
12789
12790 /* If STMT's abstract origin is a function declaration and STMT's
12791    first subblock's abstract origin is the function's outermost block,
12792    then we're looking at the main entry point.  */
12793 static bool
12794 is_inlined_entry_point (const_tree stmt)
12795 {
12796   tree decl, block;
12797
12798   if (!stmt || TREE_CODE (stmt) != BLOCK)
12799     return false;
12800
12801   decl = block_ultimate_origin (stmt);
12802
12803   if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
12804     return false;
12805
12806   block = BLOCK_SUBBLOCKS (stmt);
12807
12808   if (block)
12809     {
12810       if (TREE_CODE (block) != BLOCK)
12811         return false;
12812
12813       block = block_ultimate_origin (block);
12814     }
12815
12816   return block == DECL_INITIAL (decl);
12817 }
12818
12819 /* A helper function for gen_lexical_block_die and gen_inlined_subroutine_die.
12820    Add low_pc and high_pc attributes to the DIE for a block STMT.  */
12821
12822 static inline void
12823 add_high_low_attributes (tree stmt, dw_die_ref die)
12824 {
12825   char label[MAX_ARTIFICIAL_LABEL_BYTES];
12826
12827   if (BLOCK_FRAGMENT_CHAIN (stmt))
12828     {
12829       tree chain;
12830
12831       if (is_inlined_entry_point (stmt))
12832         {
12833           ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12834                                        BLOCK_NUMBER (stmt));
12835           add_AT_lbl_id (die, DW_AT_entry_pc, label);
12836         }
12837
12838       add_AT_range_list (die, DW_AT_ranges, add_ranges (stmt));
12839
12840       chain = BLOCK_FRAGMENT_CHAIN (stmt);
12841       do
12842         {
12843           add_ranges (chain);
12844           chain = BLOCK_FRAGMENT_CHAIN (chain);
12845         }
12846       while (chain);
12847       add_ranges (NULL);
12848     }
12849   else
12850     {
12851       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_BEGIN_LABEL,
12852                                    BLOCK_NUMBER (stmt));
12853       add_AT_lbl_id (die, DW_AT_low_pc, label);
12854       ASM_GENERATE_INTERNAL_LABEL (label, BLOCK_END_LABEL,
12855                                    BLOCK_NUMBER (stmt));
12856       add_AT_lbl_id (die, DW_AT_high_pc, label);
12857     }
12858 }
12859
12860 /* Generate a DIE for a lexical block.  */
12861
12862 static void
12863 gen_lexical_block_die (tree stmt, dw_die_ref context_die, int depth)
12864 {
12865   dw_die_ref stmt_die = new_die (DW_TAG_lexical_block, context_die, stmt);
12866
12867   if (! BLOCK_ABSTRACT (stmt))
12868     add_high_low_attributes (stmt, stmt_die);
12869
12870   decls_for_scope (stmt, stmt_die, depth);
12871 }
12872
12873 /* Generate a DIE for an inlined subprogram.  */
12874
12875 static void
12876 gen_inlined_subroutine_die (tree stmt, dw_die_ref context_die, int depth)
12877 {
12878   tree decl = block_ultimate_origin (stmt);
12879
12880   /* Emit info for the abstract instance first, if we haven't yet.  We
12881      must emit this even if the block is abstract, otherwise when we
12882      emit the block below (or elsewhere), we may end up trying to emit
12883      a die whose origin die hasn't been emitted, and crashing.  */
12884   dwarf2out_abstract_function (decl);
12885
12886   if (! BLOCK_ABSTRACT (stmt))
12887     {
12888       dw_die_ref subr_die
12889         = new_die (DW_TAG_inlined_subroutine, context_die, stmt);
12890
12891       add_abstract_origin_attribute (subr_die, decl);
12892       add_high_low_attributes (stmt, subr_die);
12893       add_call_src_coords_attributes (stmt, subr_die);
12894
12895       decls_for_scope (stmt, subr_die, depth);
12896       current_function_has_inlines = 1;
12897     }
12898   else
12899     /* We may get here if we're the outer block of function A that was
12900        inlined into function B that was inlined into function C.  When
12901        generating debugging info for C, dwarf2out_abstract_function(B)
12902        would mark all inlined blocks as abstract, including this one.
12903        So, we wouldn't (and shouldn't) expect labels to be generated
12904        for this one.  Instead, just emit debugging info for
12905        declarations within the block.  This is particularly important
12906        in the case of initializers of arguments passed from B to us:
12907        if they're statement expressions containing declarations, we
12908        wouldn't generate dies for their abstract variables, and then,
12909        when generating dies for the real variables, we'd die (pun
12910        intended :-)  */
12911     gen_lexical_block_die (stmt, context_die, depth);
12912 }
12913
12914 /* Generate a DIE for a field in a record, or structure.  */
12915
12916 static void
12917 gen_field_die (tree decl, dw_die_ref context_die)
12918 {
12919   dw_die_ref decl_die;
12920
12921   if (TREE_TYPE (decl) == error_mark_node)
12922     return;
12923
12924   decl_die = new_die (DW_TAG_member, context_die, decl);
12925   add_name_and_src_coords_attributes (decl_die, decl);
12926   add_type_attribute (decl_die, member_declared_type (decl),
12927                       TREE_READONLY (decl), TREE_THIS_VOLATILE (decl),
12928                       context_die);
12929
12930   if (DECL_BIT_FIELD_TYPE (decl))
12931     {
12932       add_byte_size_attribute (decl_die, decl);
12933       add_bit_size_attribute (decl_die, decl);
12934       add_bit_offset_attribute (decl_die, decl);
12935     }
12936
12937   if (TREE_CODE (DECL_FIELD_CONTEXT (decl)) != UNION_TYPE)
12938     add_data_member_location_attribute (decl_die, decl);
12939
12940   if (DECL_ARTIFICIAL (decl))
12941     add_AT_flag (decl_die, DW_AT_artificial, 1);
12942
12943   if (TREE_PROTECTED (decl))
12944     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_protected);
12945   else if (TREE_PRIVATE (decl))
12946     add_AT_unsigned (decl_die, DW_AT_accessibility, DW_ACCESS_private);
12947
12948   /* Equate decl number to die, so that we can look up this decl later on.  */
12949   equate_decl_number_to_die (decl, decl_die);
12950 }
12951
12952 #if 0
12953 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12954    Use modified_type_die instead.
12955    We keep this code here just in case these types of DIEs may be needed to
12956    represent certain things in other languages (e.g. Pascal) someday.  */
12957
12958 static void
12959 gen_pointer_type_die (tree type, dw_die_ref context_die)
12960 {
12961   dw_die_ref ptr_die
12962     = new_die (DW_TAG_pointer_type, scope_die_for (type, context_die), type);
12963
12964   equate_type_number_to_die (type, ptr_die);
12965   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12966   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12967 }
12968
12969 /* Don't generate either pointer_type DIEs or reference_type DIEs here.
12970    Use modified_type_die instead.
12971    We keep this code here just in case these types of DIEs may be needed to
12972    represent certain things in other languages (e.g. Pascal) someday.  */
12973
12974 static void
12975 gen_reference_type_die (tree type, dw_die_ref context_die)
12976 {
12977   dw_die_ref ref_die
12978     = new_die (DW_TAG_reference_type, scope_die_for (type, context_die), type);
12979
12980   equate_type_number_to_die (type, ref_die);
12981   add_type_attribute (ref_die, TREE_TYPE (type), 0, 0, context_die);
12982   add_AT_unsigned (mod_type_die, DW_AT_byte_size, PTR_SIZE);
12983 }
12984 #endif
12985
12986 /* Generate a DIE for a pointer to a member type.  */
12987
12988 static void
12989 gen_ptr_to_mbr_type_die (tree type, dw_die_ref context_die)
12990 {
12991   dw_die_ref ptr_die
12992     = new_die (DW_TAG_ptr_to_member_type,
12993                scope_die_for (type, context_die), type);
12994
12995   equate_type_number_to_die (type, ptr_die);
12996   add_AT_die_ref (ptr_die, DW_AT_containing_type,
12997                   lookup_type_die (TYPE_OFFSET_BASETYPE (type)));
12998   add_type_attribute (ptr_die, TREE_TYPE (type), 0, 0, context_die);
12999 }
13000
13001 /* Generate the DIE for the compilation unit.  */
13002
13003 static dw_die_ref
13004 gen_compile_unit_die (const char *filename)
13005 {
13006   dw_die_ref die;
13007   char producer[250];
13008   const char *language_string = lang_hooks.name;
13009   int language;
13010
13011   die = new_die (DW_TAG_compile_unit, NULL, NULL);
13012
13013   if (filename)
13014     {
13015       add_name_attribute (die, filename);
13016       /* Don't add cwd for <built-in>.  */
13017       if (!IS_ABSOLUTE_PATH (filename) && filename[0] != '<')
13018         add_comp_dir_attribute (die);
13019     }
13020
13021   sprintf (producer, "%s %s", language_string, version_string);
13022
13023 #ifdef MIPS_DEBUGGING_INFO
13024   /* The MIPS/SGI compilers place the 'cc' command line options in the producer
13025      string.  The SGI debugger looks for -g, -g1, -g2, or -g3; if they do
13026      not appear in the producer string, the debugger reaches the conclusion
13027      that the object file is stripped and has no debugging information.
13028      To get the MIPS/SGI debugger to believe that there is debugging
13029      information in the object file, we add a -g to the producer string.  */
13030   if (debug_info_level > DINFO_LEVEL_TERSE)
13031     strcat (producer, " -g");
13032 #endif
13033
13034   add_AT_string (die, DW_AT_producer, producer);
13035
13036   if (strcmp (language_string, "GNU C++") == 0)
13037     language = DW_LANG_C_plus_plus;
13038   else if (strcmp (language_string, "GNU Ada") == 0)
13039     language = DW_LANG_Ada95;
13040   else if (strcmp (language_string, "GNU F77") == 0)
13041     language = DW_LANG_Fortran77;
13042   else if (strcmp (language_string, "GNU F95") == 0)
13043     language = DW_LANG_Fortran95;
13044   else if (strcmp (language_string, "GNU Pascal") == 0)
13045     language = DW_LANG_Pascal83;
13046   else if (strcmp (language_string, "GNU Java") == 0)
13047     language = DW_LANG_Java;
13048   else if (strcmp (language_string, "GNU Objective-C") == 0)
13049     language = DW_LANG_ObjC;
13050   else if (strcmp (language_string, "GNU Objective-C++") == 0)
13051     language = DW_LANG_ObjC_plus_plus;
13052   else
13053     language = DW_LANG_C89;
13054
13055   add_AT_unsigned (die, DW_AT_language, language);
13056   return die;
13057 }
13058
13059 /* Generate the DIE for a base class.  */
13060
13061 static void
13062 gen_inheritance_die (tree binfo, tree access, dw_die_ref context_die)
13063 {
13064   dw_die_ref die = new_die (DW_TAG_inheritance, context_die, binfo);
13065
13066   add_type_attribute (die, BINFO_TYPE (binfo), 0, 0, context_die);
13067   add_data_member_location_attribute (die, binfo);
13068
13069   if (BINFO_VIRTUAL_P (binfo))
13070     add_AT_unsigned (die, DW_AT_virtuality, DW_VIRTUALITY_virtual);
13071
13072   if (access == access_public_node)
13073     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_public);
13074   else if (access == access_protected_node)
13075     add_AT_unsigned (die, DW_AT_accessibility, DW_ACCESS_protected);
13076 }
13077
13078 /* Generate a DIE for a class member.  */
13079
13080 static void
13081 gen_member_die (tree type, dw_die_ref context_die)
13082 {
13083   tree member;
13084   tree binfo = TYPE_BINFO (type);
13085   dw_die_ref child;
13086
13087   /* If this is not an incomplete type, output descriptions of each of its
13088      members. Note that as we output the DIEs necessary to represent the
13089      members of this record or union type, we will also be trying to output
13090      DIEs to represent the *types* of those members. However the `type'
13091      function (above) will specifically avoid generating type DIEs for member
13092      types *within* the list of member DIEs for this (containing) type except
13093      for those types (of members) which are explicitly marked as also being
13094      members of this (containing) type themselves.  The g++ front- end can
13095      force any given type to be treated as a member of some other (containing)
13096      type by setting the TYPE_CONTEXT of the given (member) type to point to
13097      the TREE node representing the appropriate (containing) type.  */
13098
13099   /* First output info about the base classes.  */
13100   if (binfo)
13101     {
13102       VEC(tree,gc) *accesses = BINFO_BASE_ACCESSES (binfo);
13103       int i;
13104       tree base;
13105
13106       for (i = 0; BINFO_BASE_ITERATE (binfo, i, base); i++)
13107         gen_inheritance_die (base,
13108                              (accesses ? VEC_index (tree, accesses, i)
13109                               : access_public_node), context_die);
13110     }
13111
13112   /* Now output info about the data members and type members.  */
13113   for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
13114     {
13115       /* If we thought we were generating minimal debug info for TYPE
13116          and then changed our minds, some of the member declarations
13117          may have already been defined.  Don't define them again, but
13118          do put them in the right order.  */
13119
13120       child = lookup_decl_die (member);
13121       if (child)
13122         splice_child_die (context_die, child);
13123       else
13124         gen_decl_die (member, context_die);
13125     }
13126
13127   /* Now output info about the function members (if any).  */
13128   for (member = TYPE_METHODS (type); member; member = TREE_CHAIN (member))
13129     {
13130       /* Don't include clones in the member list.  */
13131       if (DECL_ABSTRACT_ORIGIN (member))
13132         continue;
13133
13134       child = lookup_decl_die (member);
13135       if (child)
13136         splice_child_die (context_die, child);
13137       else
13138         gen_decl_die (member, context_die);
13139     }
13140 }
13141
13142 /* Generate a DIE for a structure or union type.  If TYPE_DECL_SUPPRESS_DEBUG
13143    is set, we pretend that the type was never defined, so we only get the
13144    member DIEs needed by later specification DIEs.  */
13145
13146 static void
13147 gen_struct_or_union_type_die (tree type, dw_die_ref context_die,
13148                                 enum debug_info_usage usage)
13149 {
13150   dw_die_ref type_die = lookup_type_die (type);
13151   dw_die_ref scope_die = 0;
13152   int nested = 0;
13153   int complete = (TYPE_SIZE (type)
13154                   && (! TYPE_STUB_DECL (type)
13155                       || ! TYPE_DECL_SUPPRESS_DEBUG (TYPE_STUB_DECL (type))));
13156   int ns_decl = (context_die && context_die->die_tag == DW_TAG_namespace);
13157   complete = complete && should_emit_struct_debug (type, usage);
13158
13159   if (type_die && ! complete)
13160     return;
13161
13162   if (TYPE_CONTEXT (type) != NULL_TREE
13163       && (AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13164           || TREE_CODE (TYPE_CONTEXT (type)) == NAMESPACE_DECL))
13165     nested = 1;
13166
13167   scope_die = scope_die_for (type, context_die);
13168
13169   if (! type_die || (nested && scope_die == comp_unit_die))
13170     /* First occurrence of type or toplevel definition of nested class.  */
13171     {
13172       dw_die_ref old_die = type_die;
13173
13174       type_die = new_die (TREE_CODE (type) == RECORD_TYPE
13175                           ? record_type_tag (type) : DW_TAG_union_type,
13176                           scope_die, type);
13177       equate_type_number_to_die (type, type_die);
13178       if (old_die)
13179         add_AT_specification (type_die, old_die);
13180       else
13181         add_name_attribute (type_die, type_tag (type));
13182     }
13183   else
13184     remove_AT (type_die, DW_AT_declaration);
13185
13186   /* If this type has been completed, then give it a byte_size attribute and
13187      then give a list of members.  */
13188   if (complete && !ns_decl)
13189     {
13190       /* Prevent infinite recursion in cases where the type of some member of
13191          this type is expressed in terms of this type itself.  */
13192       TREE_ASM_WRITTEN (type) = 1;
13193       add_byte_size_attribute (type_die, type);
13194       if (TYPE_STUB_DECL (type) != NULL_TREE)
13195         add_src_coords_attributes (type_die, TYPE_STUB_DECL (type));
13196
13197       /* If the first reference to this type was as the return type of an
13198          inline function, then it may not have a parent.  Fix this now.  */
13199       if (type_die->die_parent == NULL)
13200         add_child_die (scope_die, type_die);
13201
13202       push_decl_scope (type);
13203       gen_member_die (type, type_die);
13204       pop_decl_scope ();
13205
13206       /* GNU extension: Record what type our vtable lives in.  */
13207       if (TYPE_VFIELD (type))
13208         {
13209           tree vtype = DECL_FCONTEXT (TYPE_VFIELD (type));
13210
13211           gen_type_die (vtype, context_die);
13212           add_AT_die_ref (type_die, DW_AT_containing_type,
13213                           lookup_type_die (vtype));
13214         }
13215     }
13216   else
13217     {
13218       add_AT_flag (type_die, DW_AT_declaration, 1);
13219
13220       /* We don't need to do this for function-local types.  */
13221       if (TYPE_STUB_DECL (type)
13222           && ! decl_function_context (TYPE_STUB_DECL (type)))
13223         VEC_safe_push (tree, gc, incomplete_types, type);
13224     }
13225
13226   if (get_AT (type_die, DW_AT_name))
13227     add_pubtype (type, type_die);
13228 }
13229
13230 /* Generate a DIE for a subroutine _type_.  */
13231
13232 static void
13233 gen_subroutine_type_die (tree type, dw_die_ref context_die)
13234 {
13235   tree return_type = TREE_TYPE (type);
13236   dw_die_ref subr_die
13237     = new_die (DW_TAG_subroutine_type,
13238                scope_die_for (type, context_die), type);
13239
13240   equate_type_number_to_die (type, subr_die);
13241   add_prototyped_attribute (subr_die, type);
13242   add_type_attribute (subr_die, return_type, 0, 0, context_die);
13243   gen_formal_types_die (type, subr_die);
13244
13245   if (get_AT (subr_die, DW_AT_name))
13246     add_pubtype (type, subr_die);
13247 }
13248
13249 /* Generate a DIE for a type definition.  */
13250
13251 static void
13252 gen_typedef_die (tree decl, dw_die_ref context_die)
13253 {
13254   dw_die_ref type_die;
13255   tree origin;
13256
13257   if (TREE_ASM_WRITTEN (decl))
13258     return;
13259
13260   TREE_ASM_WRITTEN (decl) = 1;
13261   type_die = new_die (DW_TAG_typedef, context_die, decl);
13262   origin = decl_ultimate_origin (decl);
13263   if (origin != NULL)
13264     add_abstract_origin_attribute (type_die, origin);
13265   else
13266     {
13267       tree type;
13268
13269       add_name_and_src_coords_attributes (type_die, decl);
13270       if (DECL_ORIGINAL_TYPE (decl))
13271         {
13272           type = DECL_ORIGINAL_TYPE (decl);
13273
13274           gcc_assert (type != TREE_TYPE (decl));
13275           equate_type_number_to_die (TREE_TYPE (decl), type_die);
13276         }
13277       else
13278         type = TREE_TYPE (decl);
13279
13280       add_type_attribute (type_die, type, TREE_READONLY (decl),
13281                           TREE_THIS_VOLATILE (decl), context_die);
13282     }
13283
13284   if (DECL_ABSTRACT (decl))
13285     equate_decl_number_to_die (decl, type_die);
13286
13287   if (get_AT (type_die, DW_AT_name))
13288     add_pubtype (decl, type_die);
13289 }
13290
13291 /* Generate a type description DIE.  */
13292
13293 static void
13294 gen_type_die_with_usage (tree type, dw_die_ref context_die,
13295                                 enum debug_info_usage usage)
13296 {
13297   int need_pop;
13298   struct array_descr_info info;
13299
13300   if (type == NULL_TREE || type == error_mark_node)
13301     return;
13302
13303   if (TYPE_NAME (type) && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
13304       && DECL_ORIGINAL_TYPE (TYPE_NAME (type)))
13305     {
13306       if (TREE_ASM_WRITTEN (type))
13307         return;
13308
13309       /* Prevent broken recursion; we can't hand off to the same type.  */
13310       gcc_assert (DECL_ORIGINAL_TYPE (TYPE_NAME (type)) != type);
13311
13312       TREE_ASM_WRITTEN (type) = 1;
13313       gen_decl_die (TYPE_NAME (type), context_die);
13314       return;
13315     }
13316
13317   /* If this is an array type with hidden descriptor, handle it first.  */
13318   if (!TREE_ASM_WRITTEN (type)
13319       && lang_hooks.types.get_array_descr_info
13320       && lang_hooks.types.get_array_descr_info (type, &info))
13321     {
13322       gen_descr_array_type_die (type, &info, context_die);
13323       TREE_ASM_WRITTEN (type) = 1;
13324       return;
13325     }
13326
13327   /* We are going to output a DIE to represent the unqualified version
13328      of this type (i.e. without any const or volatile qualifiers) so
13329      get the main variant (i.e. the unqualified version) of this type
13330      now.  (Vectors are special because the debugging info is in the
13331      cloned type itself).  */
13332   if (TREE_CODE (type) != VECTOR_TYPE)
13333     type = type_main_variant (type);
13334
13335   if (TREE_ASM_WRITTEN (type))
13336     return;
13337
13338   switch (TREE_CODE (type))
13339     {
13340     case ERROR_MARK:
13341       break;
13342
13343     case POINTER_TYPE:
13344     case REFERENCE_TYPE:
13345       /* We must set TREE_ASM_WRITTEN in case this is a recursive type.  This
13346          ensures that the gen_type_die recursion will terminate even if the
13347          type is recursive.  Recursive types are possible in Ada.  */
13348       /* ??? We could perhaps do this for all types before the switch
13349          statement.  */
13350       TREE_ASM_WRITTEN (type) = 1;
13351
13352       /* For these types, all that is required is that we output a DIE (or a
13353          set of DIEs) to represent the "basis" type.  */
13354       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13355                                 DINFO_USAGE_IND_USE);
13356       break;
13357
13358     case OFFSET_TYPE:
13359       /* This code is used for C++ pointer-to-data-member types.
13360          Output a description of the relevant class type.  */
13361       gen_type_die_with_usage (TYPE_OFFSET_BASETYPE (type), context_die,
13362                                         DINFO_USAGE_IND_USE);
13363
13364       /* Output a description of the type of the object pointed to.  */
13365       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13366                                         DINFO_USAGE_IND_USE);
13367
13368       /* Now output a DIE to represent this pointer-to-data-member type
13369          itself.  */
13370       gen_ptr_to_mbr_type_die (type, context_die);
13371       break;
13372
13373     case FUNCTION_TYPE:
13374       /* Force out return type (in case it wasn't forced out already).  */
13375       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13376                                         DINFO_USAGE_DIR_USE);
13377       gen_subroutine_type_die (type, context_die);
13378       break;
13379
13380     case METHOD_TYPE:
13381       /* Force out return type (in case it wasn't forced out already).  */
13382       gen_type_die_with_usage (TREE_TYPE (type), context_die,
13383                                         DINFO_USAGE_DIR_USE);
13384       gen_subroutine_type_die (type, context_die);
13385       break;
13386
13387     case ARRAY_TYPE:
13388       gen_array_type_die (type, context_die);
13389       break;
13390
13391     case VECTOR_TYPE:
13392       gen_array_type_die (type, context_die);
13393       break;
13394
13395     case ENUMERAL_TYPE:
13396     case RECORD_TYPE:
13397     case UNION_TYPE:
13398     case QUAL_UNION_TYPE:
13399       /* If this is a nested type whose containing class hasn't been written
13400          out yet, writing it out will cover this one, too.  This does not apply
13401          to instantiations of member class templates; they need to be added to
13402          the containing class as they are generated.  FIXME: This hurts the
13403          idea of combining type decls from multiple TUs, since we can't predict
13404          what set of template instantiations we'll get.  */
13405       if (TYPE_CONTEXT (type)
13406           && AGGREGATE_TYPE_P (TYPE_CONTEXT (type))
13407           && ! TREE_ASM_WRITTEN (TYPE_CONTEXT (type)))
13408         {
13409           gen_type_die_with_usage (TYPE_CONTEXT (type), context_die, usage);
13410
13411           if (TREE_ASM_WRITTEN (type))
13412             return;
13413
13414           /* If that failed, attach ourselves to the stub.  */
13415           push_decl_scope (TYPE_CONTEXT (type));
13416           context_die = lookup_type_die (TYPE_CONTEXT (type));
13417           need_pop = 1;
13418         }
13419       else
13420         {
13421           declare_in_namespace (type, context_die);
13422           need_pop = 0;
13423         }
13424
13425       if (TREE_CODE (type) == ENUMERAL_TYPE)
13426         {
13427           /* This might have been written out by the call to
13428              declare_in_namespace.  */
13429           if (!TREE_ASM_WRITTEN (type))
13430             gen_enumeration_type_die (type, context_die);
13431         }
13432       else
13433         gen_struct_or_union_type_die (type, context_die, usage);
13434
13435       if (need_pop)
13436         pop_decl_scope ();
13437
13438       /* Don't set TREE_ASM_WRITTEN on an incomplete struct; we want to fix
13439          it up if it is ever completed.  gen_*_type_die will set it for us
13440          when appropriate.  */
13441       return;
13442
13443     case VOID_TYPE:
13444     case INTEGER_TYPE:
13445     case REAL_TYPE:
13446     case FIXED_POINT_TYPE:
13447     case COMPLEX_TYPE:
13448     case BOOLEAN_TYPE:
13449       /* No DIEs needed for fundamental types.  */
13450       break;
13451
13452     case LANG_TYPE:
13453       /* No Dwarf representation currently defined.  */
13454       break;
13455
13456     default:
13457       gcc_unreachable ();
13458     }
13459
13460   TREE_ASM_WRITTEN (type) = 1;
13461 }
13462
13463 static void
13464 gen_type_die (tree type, dw_die_ref context_die)
13465 {
13466   gen_type_die_with_usage (type, context_die, DINFO_USAGE_DIR_USE);
13467 }
13468
13469 /* Generate a DIE for a tagged type instantiation.  */
13470
13471 static void
13472 gen_tagged_type_instantiation_die (tree type, dw_die_ref context_die)
13473 {
13474   if (type == NULL_TREE || type == error_mark_node)
13475     return;
13476
13477   /* We are going to output a DIE to represent the unqualified version of
13478      this type (i.e. without any const or volatile qualifiers) so make sure
13479      that we have the main variant (i.e. the unqualified version) of this
13480      type now.  */
13481   gcc_assert (type == type_main_variant (type));
13482
13483   /* Do not check TREE_ASM_WRITTEN (type) as it may not be set if this is
13484      an instance of an unresolved type.  */
13485
13486   switch (TREE_CODE (type))
13487     {
13488     case ERROR_MARK:
13489       break;
13490
13491     case ENUMERAL_TYPE:
13492       gen_inlined_enumeration_type_die (type, context_die);
13493       break;
13494
13495     case RECORD_TYPE:
13496       gen_inlined_structure_type_die (type, context_die);
13497       break;
13498
13499     case UNION_TYPE:
13500     case QUAL_UNION_TYPE:
13501       gen_inlined_union_type_die (type, context_die);
13502       break;
13503
13504     default:
13505       gcc_unreachable ();
13506     }
13507 }
13508
13509 /* Generate a DW_TAG_lexical_block DIE followed by DIEs to represent all of the
13510    things which are local to the given block.  */
13511
13512 static void
13513 gen_block_die (tree stmt, dw_die_ref context_die, int depth)
13514 {
13515   int must_output_die = 0;
13516   tree origin;
13517   tree decl;
13518   enum tree_code origin_code;
13519
13520   /* Ignore blocks that are NULL.  */
13521   if (stmt == NULL_TREE)
13522     return;
13523
13524   /* If the block is one fragment of a non-contiguous block, do not
13525      process the variables, since they will have been done by the
13526      origin block.  Do process subblocks.  */
13527   if (BLOCK_FRAGMENT_ORIGIN (stmt))
13528     {
13529       tree sub;
13530
13531       for (sub = BLOCK_SUBBLOCKS (stmt); sub; sub = BLOCK_CHAIN (sub))
13532         gen_block_die (sub, context_die, depth + 1);
13533
13534       return;
13535     }
13536
13537   /* Determine the "ultimate origin" of this block.  This block may be an
13538      inlined instance of an inlined instance of inline function, so we have
13539      to trace all of the way back through the origin chain to find out what
13540      sort of node actually served as the original seed for the creation of
13541      the current block.  */
13542   origin = block_ultimate_origin (stmt);
13543   origin_code = (origin != NULL) ? TREE_CODE (origin) : ERROR_MARK;
13544
13545   /* Determine if we need to output any Dwarf DIEs at all to represent this
13546      block.  */
13547   if (origin_code == FUNCTION_DECL)
13548     /* The outer scopes for inlinings *must* always be represented.  We
13549        generate DW_TAG_inlined_subroutine DIEs for them.  (See below.) */
13550     must_output_die = 1;
13551   else
13552     {
13553       /* In the case where the current block represents an inlining of the
13554          "body block" of an inline function, we must *NOT* output any DIE for
13555          this block because we have already output a DIE to represent the whole
13556          inlined function scope and the "body block" of any function doesn't
13557          really represent a different scope according to ANSI C rules.  So we
13558          check here to make sure that this block does not represent a "body
13559          block inlining" before trying to set the MUST_OUTPUT_DIE flag.  */
13560       if (! is_body_block (origin ? origin : stmt))
13561         {
13562           /* Determine if this block directly contains any "significant"
13563              local declarations which we will need to output DIEs for.  */
13564           if (debug_info_level > DINFO_LEVEL_TERSE)
13565             /* We are not in terse mode so *any* local declaration counts
13566                as being a "significant" one.  */
13567             must_output_die = (BLOCK_VARS (stmt) != NULL
13568                                && (TREE_USED (stmt)
13569                                    || TREE_ASM_WRITTEN (stmt)
13570                                    || BLOCK_ABSTRACT (stmt)));
13571           else
13572             /* We are in terse mode, so only local (nested) function
13573                definitions count as "significant" local declarations.  */
13574             for (decl = BLOCK_VARS (stmt);
13575                  decl != NULL; decl = TREE_CHAIN (decl))
13576               if (TREE_CODE (decl) == FUNCTION_DECL
13577                   && DECL_INITIAL (decl))
13578                 {
13579                   must_output_die = 1;
13580                   break;
13581                 }
13582         }
13583     }
13584
13585   /* It would be a waste of space to generate a Dwarf DW_TAG_lexical_block
13586      DIE for any block which contains no significant local declarations at
13587      all.  Rather, in such cases we just call `decls_for_scope' so that any
13588      needed Dwarf info for any sub-blocks will get properly generated. Note
13589      that in terse mode, our definition of what constitutes a "significant"
13590      local declaration gets restricted to include only inlined function
13591      instances and local (nested) function definitions.  */
13592   if (must_output_die)
13593     {
13594       if (origin_code == FUNCTION_DECL)
13595         gen_inlined_subroutine_die (stmt, context_die, depth);
13596       else
13597         gen_lexical_block_die (stmt, context_die, depth);
13598     }
13599   else
13600     decls_for_scope (stmt, context_die, depth);
13601 }
13602
13603 /* Generate all of the decls declared within a given scope and (recursively)
13604    all of its sub-blocks.  */
13605
13606 static void
13607 decls_for_scope (tree stmt, dw_die_ref context_die, int depth)
13608 {
13609   tree decl;
13610   tree subblocks;
13611
13612   /* Ignore NULL blocks.  */
13613   if (stmt == NULL_TREE)
13614     return;
13615
13616   if (TREE_USED (stmt))
13617     {
13618       /* Output the DIEs to represent all of the data objects and typedefs
13619          declared directly within this block but not within any nested
13620          sub-blocks.  Also, nested function and tag DIEs have been
13621          generated with a parent of NULL; fix that up now.  */
13622       for (decl = BLOCK_VARS (stmt); decl != NULL; decl = TREE_CHAIN (decl))
13623         {
13624           dw_die_ref die;
13625
13626           if (TREE_CODE (decl) == FUNCTION_DECL)
13627             die = lookup_decl_die (decl);
13628           else if (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl))
13629             die = lookup_type_die (TREE_TYPE (decl));
13630           else
13631             die = NULL;
13632
13633           if (die != NULL && die->die_parent == NULL)
13634             add_child_die (context_die, die);
13635           /* Do not produce debug information for static variables since
13636              these might be optimized out.  We are called for these later
13637              in varpool_analyze_pending_decls. */
13638           if (TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
13639             ;
13640           else
13641             gen_decl_die (decl, context_die);
13642         }
13643     }
13644
13645   /* If we're at -g1, we're not interested in subblocks.  */
13646   if (debug_info_level <= DINFO_LEVEL_TERSE)
13647     return;
13648
13649   /* Output the DIEs to represent all sub-blocks (and the items declared
13650      therein) of this block.  */
13651   for (subblocks = BLOCK_SUBBLOCKS (stmt);
13652        subblocks != NULL;
13653        subblocks = BLOCK_CHAIN (subblocks))
13654     gen_block_die (subblocks, context_die, depth + 1);
13655 }
13656
13657 /* Is this a typedef we can avoid emitting?  */
13658
13659 static inline int
13660 is_redundant_typedef (const_tree decl)
13661 {
13662   if (TYPE_DECL_IS_STUB (decl))
13663     return 1;
13664
13665   if (DECL_ARTIFICIAL (decl)
13666       && DECL_CONTEXT (decl)
13667       && is_tagged_type (DECL_CONTEXT (decl))
13668       && TREE_CODE (TYPE_NAME (DECL_CONTEXT (decl))) == TYPE_DECL
13669       && DECL_NAME (decl) == DECL_NAME (TYPE_NAME (DECL_CONTEXT (decl))))
13670     /* Also ignore the artificial member typedef for the class name.  */
13671     return 1;
13672
13673   return 0;
13674 }
13675
13676 /* Returns the DIE for decl.  A DIE will always be returned.  */
13677
13678 static dw_die_ref
13679 force_decl_die (tree decl)
13680 {
13681   dw_die_ref decl_die;
13682   unsigned saved_external_flag;
13683   tree save_fn = NULL_TREE;
13684   decl_die = lookup_decl_die (decl);
13685   if (!decl_die)
13686     {
13687       dw_die_ref context_die;
13688       tree decl_context = DECL_CONTEXT (decl);
13689       if (decl_context)
13690         {
13691           /* Find die that represents this context.  */
13692           if (TYPE_P (decl_context))
13693             context_die = force_type_die (decl_context);
13694           else
13695             context_die = force_decl_die (decl_context);
13696         }
13697       else
13698         context_die = comp_unit_die;
13699
13700       decl_die = lookup_decl_die (decl);
13701       if (decl_die)
13702         return decl_die;
13703
13704       switch (TREE_CODE (decl))
13705         {
13706         case FUNCTION_DECL:
13707           /* Clear current_function_decl, so that gen_subprogram_die thinks
13708              that this is a declaration. At this point, we just want to force
13709              declaration die.  */
13710           save_fn = current_function_decl;
13711           current_function_decl = NULL_TREE;
13712           gen_subprogram_die (decl, context_die);
13713           current_function_decl = save_fn;
13714           break;
13715
13716         case VAR_DECL:
13717           /* Set external flag to force declaration die. Restore it after
13718            gen_decl_die() call.  */
13719           saved_external_flag = DECL_EXTERNAL (decl);
13720           DECL_EXTERNAL (decl) = 1;
13721           gen_decl_die (decl, context_die);
13722           DECL_EXTERNAL (decl) = saved_external_flag;
13723           break;
13724
13725         case NAMESPACE_DECL:
13726           dwarf2out_decl (decl);
13727           break;
13728
13729         default:
13730           gcc_unreachable ();
13731         }
13732
13733       /* We should be able to find the DIE now.  */
13734       if (!decl_die)
13735         decl_die = lookup_decl_die (decl);
13736       gcc_assert (decl_die);
13737     }
13738
13739   return decl_die;
13740 }
13741
13742 /* Returns the DIE for TYPE, that must not be a base type.  A DIE is
13743    always returned.  */
13744
13745 static dw_die_ref
13746 force_type_die (tree type)
13747 {
13748   dw_die_ref type_die;
13749
13750   type_die = lookup_type_die (type);
13751   if (!type_die)
13752     {
13753       dw_die_ref context_die;
13754       if (TYPE_CONTEXT (type))
13755         {
13756           if (TYPE_P (TYPE_CONTEXT (type)))
13757             context_die = force_type_die (TYPE_CONTEXT (type));
13758           else
13759             context_die = force_decl_die (TYPE_CONTEXT (type));
13760         }
13761       else
13762         context_die = comp_unit_die;
13763
13764       type_die = modified_type_die (type, TYPE_READONLY (type),
13765                                     TYPE_VOLATILE (type), context_die);
13766       gcc_assert (type_die);
13767     }
13768   return type_die;
13769 }
13770
13771 /* Force out any required namespaces to be able to output DECL,
13772    and return the new context_die for it, if it's changed.  */
13773
13774 static dw_die_ref
13775 setup_namespace_context (tree thing, dw_die_ref context_die)
13776 {
13777   tree context = (DECL_P (thing)
13778                   ? DECL_CONTEXT (thing) : TYPE_CONTEXT (thing));
13779   if (context && TREE_CODE (context) == NAMESPACE_DECL)
13780     /* Force out the namespace.  */
13781     context_die = force_decl_die (context);
13782
13783   return context_die;
13784 }
13785
13786 /* Emit a declaration DIE for THING (which is either a DECL or a tagged
13787    type) within its namespace, if appropriate.
13788
13789    For compatibility with older debuggers, namespace DIEs only contain
13790    declarations; all definitions are emitted at CU scope.  */
13791
13792 static void
13793 declare_in_namespace (tree thing, dw_die_ref context_die)
13794 {
13795   dw_die_ref ns_context;
13796
13797   if (debug_info_level <= DINFO_LEVEL_TERSE)
13798     return;
13799
13800   /* If this decl is from an inlined function, then don't try to emit it in its
13801      namespace, as we will get confused.  It would have already been emitted
13802      when the abstract instance of the inline function was emitted anyways.  */
13803   if (DECL_P (thing) && DECL_ABSTRACT_ORIGIN (thing))
13804     return;
13805
13806   ns_context = setup_namespace_context (thing, context_die);
13807
13808   if (ns_context != context_die)
13809     {
13810       if (DECL_P (thing))
13811         gen_decl_die (thing, ns_context);
13812       else
13813         gen_type_die (thing, ns_context);
13814     }
13815 }
13816
13817 /* Generate a DIE for a namespace or namespace alias.  */
13818
13819 static void
13820 gen_namespace_die (tree decl)
13821 {
13822   dw_die_ref context_die = setup_namespace_context (decl, comp_unit_die);
13823
13824   /* Namespace aliases have a DECL_ABSTRACT_ORIGIN of the namespace
13825      they are an alias of.  */
13826   if (DECL_ABSTRACT_ORIGIN (decl) == NULL)
13827     {
13828       /* Output a real namespace.  */
13829       dw_die_ref namespace_die
13830         = new_die (DW_TAG_namespace, context_die, decl);
13831       add_name_and_src_coords_attributes (namespace_die, decl);
13832       equate_decl_number_to_die (decl, namespace_die);
13833     }
13834   else
13835     {
13836       /* Output a namespace alias.  */
13837
13838       /* Force out the namespace we are an alias of, if necessary.  */
13839       dw_die_ref origin_die
13840         = force_decl_die (DECL_ABSTRACT_ORIGIN (decl));
13841
13842       /* Now create the namespace alias DIE.  */
13843       dw_die_ref namespace_die
13844         = new_die (DW_TAG_imported_declaration, context_die, decl);
13845       add_name_and_src_coords_attributes (namespace_die, decl);
13846       add_AT_die_ref (namespace_die, DW_AT_import, origin_die);
13847       equate_decl_number_to_die (decl, namespace_die);
13848     }
13849 }
13850
13851 /* Generate Dwarf debug information for a decl described by DECL.  */
13852
13853 static void
13854 gen_decl_die (tree decl, dw_die_ref context_die)
13855 {
13856   tree origin;
13857
13858   if (DECL_P (decl) && DECL_IGNORED_P (decl))
13859     return;
13860
13861   switch (TREE_CODE (decl))
13862     {
13863     case ERROR_MARK:
13864       break;
13865
13866     case CONST_DECL:
13867       /* The individual enumerators of an enum type get output when we output
13868          the Dwarf representation of the relevant enum type itself.  */
13869       break;
13870
13871     case FUNCTION_DECL:
13872       /* Don't output any DIEs to represent mere function declarations,
13873          unless they are class members or explicit block externs.  */
13874       if (DECL_INITIAL (decl) == NULL_TREE && DECL_CONTEXT (decl) == NULL_TREE
13875           && (current_function_decl == NULL_TREE || DECL_ARTIFICIAL (decl)))
13876         break;
13877
13878 #if 0
13879       /* FIXME */
13880       /* This doesn't work because the C frontend sets DECL_ABSTRACT_ORIGIN
13881          on local redeclarations of global functions.  That seems broken.  */
13882       if (current_function_decl != decl)
13883         /* This is only a declaration.  */;
13884 #endif
13885
13886       /* If we're emitting a clone, emit info for the abstract instance.  */
13887       if (DECL_ORIGIN (decl) != decl)
13888         dwarf2out_abstract_function (DECL_ABSTRACT_ORIGIN (decl));
13889
13890       /* If we're emitting an out-of-line copy of an inline function,
13891          emit info for the abstract instance and set up to refer to it.  */
13892       else if (cgraph_function_possibly_inlined_p (decl)
13893                && ! DECL_ABSTRACT (decl)
13894                && ! class_or_namespace_scope_p (context_die)
13895                /* dwarf2out_abstract_function won't emit a die if this is just
13896                   a declaration.  We must avoid setting DECL_ABSTRACT_ORIGIN in
13897                   that case, because that works only if we have a die.  */
13898                && DECL_INITIAL (decl) != NULL_TREE)
13899         {
13900           dwarf2out_abstract_function (decl);
13901           set_decl_origin_self (decl);
13902         }
13903
13904       /* Otherwise we're emitting the primary DIE for this decl.  */
13905       else if (debug_info_level > DINFO_LEVEL_TERSE)
13906         {
13907           /* Before we describe the FUNCTION_DECL itself, make sure that we
13908              have described its return type.  */
13909           gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13910
13911           /* And its virtual context.  */
13912           if (DECL_VINDEX (decl) != NULL_TREE)
13913             gen_type_die (DECL_CONTEXT (decl), context_die);
13914
13915           /* And its containing type.  */
13916           origin = decl_class_context (decl);
13917           if (origin != NULL_TREE)
13918             gen_type_die_for_member (origin, decl, context_die);
13919
13920           /* And its containing namespace.  */
13921           declare_in_namespace (decl, context_die);
13922         }
13923
13924       /* Now output a DIE to represent the function itself.  */
13925       gen_subprogram_die (decl, context_die);
13926       break;
13927
13928     case TYPE_DECL:
13929       /* If we are in terse mode, don't generate any DIEs to represent any
13930          actual typedefs.  */
13931       if (debug_info_level <= DINFO_LEVEL_TERSE)
13932         break;
13933
13934       /* In the special case of a TYPE_DECL node representing the declaration
13935          of some type tag, if the given TYPE_DECL is marked as having been
13936          instantiated from some other (original) TYPE_DECL node (e.g. one which
13937          was generated within the original definition of an inline function) we
13938          have to generate a special (abbreviated) DW_TAG_structure_type,
13939          DW_TAG_union_type, or DW_TAG_enumeration_type DIE here.  */
13940       if (TYPE_DECL_IS_STUB (decl) && decl_ultimate_origin (decl) != NULL_TREE
13941           && is_tagged_type (TREE_TYPE (decl)))
13942         {
13943           gen_tagged_type_instantiation_die (TREE_TYPE (decl), context_die);
13944           break;
13945         }
13946
13947       if (is_redundant_typedef (decl))
13948         gen_type_die (TREE_TYPE (decl), context_die);
13949       else
13950         /* Output a DIE to represent the typedef itself.  */
13951         gen_typedef_die (decl, context_die);
13952       break;
13953
13954     case LABEL_DECL:
13955       if (debug_info_level >= DINFO_LEVEL_NORMAL)
13956         gen_label_die (decl, context_die);
13957       break;
13958
13959     case VAR_DECL:
13960     case RESULT_DECL:
13961       /* If we are in terse mode, don't generate any DIEs to represent any
13962          variable declarations or definitions.  */
13963       if (debug_info_level <= DINFO_LEVEL_TERSE)
13964         break;
13965
13966       /* Output any DIEs that are needed to specify the type of this data
13967          object.  */
13968       if (TREE_CODE (decl) == RESULT_DECL && DECL_BY_REFERENCE (decl))
13969         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
13970       else
13971         gen_type_die (TREE_TYPE (decl), context_die);
13972
13973       /* And its containing type.  */
13974       origin = decl_class_context (decl);
13975       if (origin != NULL_TREE)
13976         gen_type_die_for_member (origin, decl, context_die);
13977
13978       /* And its containing namespace.  */
13979       declare_in_namespace (decl, context_die);
13980
13981       /* Now output the DIE to represent the data object itself.  This gets
13982          complicated because of the possibility that the VAR_DECL really
13983          represents an inlined instance of a formal parameter for an inline
13984          function.  */
13985       origin = decl_ultimate_origin (decl);
13986       if (origin != NULL_TREE && TREE_CODE (origin) == PARM_DECL)
13987         gen_formal_parameter_die (decl, context_die);
13988       else
13989         gen_variable_die (decl, context_die);
13990       break;
13991
13992     case FIELD_DECL:
13993       /* Ignore the nameless fields that are used to skip bits but handle C++
13994          anonymous unions and structs.  */
13995       if (DECL_NAME (decl) != NULL_TREE
13996           || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
13997           || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE)
13998         {
13999           gen_type_die (member_declared_type (decl), context_die);
14000           gen_field_die (decl, context_die);
14001         }
14002       break;
14003
14004     case PARM_DECL:
14005       if (DECL_BY_REFERENCE (decl))
14006         gen_type_die (TREE_TYPE (TREE_TYPE (decl)), context_die);
14007       else
14008         gen_type_die (TREE_TYPE (decl), context_die);
14009       gen_formal_parameter_die (decl, context_die);
14010       break;
14011
14012     case NAMESPACE_DECL:
14013       gen_namespace_die (decl);
14014       break;
14015
14016     default:
14017       /* Probably some frontend-internal decl.  Assume we don't care.  */
14018       gcc_assert ((int)TREE_CODE (decl) > NUM_TREE_CODES);
14019       break;
14020     }
14021 }
14022 \f
14023 /* Output debug information for global decl DECL.  Called from toplev.c after
14024    compilation proper has finished.  */
14025
14026 static void
14027 dwarf2out_global_decl (tree decl)
14028 {
14029   /* Output DWARF2 information for file-scope tentative data object
14030      declarations, file-scope (extern) function declarations (which had no
14031      corresponding body) and file-scope tagged type declarations and
14032      definitions which have not yet been forced out.  */
14033   if (TREE_CODE (decl) != FUNCTION_DECL || !DECL_INITIAL (decl))
14034     dwarf2out_decl (decl);
14035 }
14036
14037 /* Output debug information for type decl DECL.  Called from toplev.c
14038    and from language front ends (to record built-in types).  */
14039 static void
14040 dwarf2out_type_decl (tree decl, int local)
14041 {
14042   if (!local)
14043     dwarf2out_decl (decl);
14044 }
14045
14046 /* Output debug information for imported module or decl.  */
14047
14048 static void
14049 dwarf2out_imported_module_or_decl (tree decl, tree context)
14050 {
14051   dw_die_ref imported_die, at_import_die;
14052   dw_die_ref scope_die;
14053   expanded_location xloc;
14054
14055   if (debug_info_level <= DINFO_LEVEL_TERSE)
14056     return;
14057
14058   gcc_assert (decl);
14059
14060   /* To emit DW_TAG_imported_module or DW_TAG_imported_decl, we need two DIEs.
14061      We need decl DIE for reference and scope die. First, get DIE for the decl
14062      itself.  */
14063
14064   /* Get the scope die for decl context. Use comp_unit_die for global module
14065      or decl. If die is not found for non globals, force new die.  */
14066   if (!context)
14067     scope_die = comp_unit_die;
14068   else if (TYPE_P (context))
14069     {
14070       if (!should_emit_struct_debug (context, DINFO_USAGE_DIR_USE))
14071         return;
14072     scope_die = force_type_die (context);
14073     }
14074   else
14075     scope_die = force_decl_die (context);
14076
14077   /* For TYPE_DECL or CONST_DECL, lookup TREE_TYPE.  */
14078   if (TREE_CODE (decl) == TYPE_DECL || TREE_CODE (decl) == CONST_DECL)
14079     {
14080       if (is_base_type (TREE_TYPE (decl)))
14081         at_import_die = base_type_die (TREE_TYPE (decl));
14082       else
14083         at_import_die = force_type_die (TREE_TYPE (decl));
14084     }
14085   else
14086     {
14087       at_import_die = lookup_decl_die (decl);
14088       if (!at_import_die)
14089         {
14090           /* If we're trying to avoid duplicate debug info, we may not have
14091              emitted the member decl for this field.  Emit it now.  */
14092           if (TREE_CODE (decl) == FIELD_DECL)
14093             {
14094               tree type = DECL_CONTEXT (decl);
14095               dw_die_ref type_context_die;
14096
14097               if (TYPE_CONTEXT (type))
14098                 if (TYPE_P (TYPE_CONTEXT (type)))
14099                   {
14100                     if (!should_emit_struct_debug (TYPE_CONTEXT (type),
14101                                                    DINFO_USAGE_DIR_USE))
14102                       return;
14103                   type_context_die = force_type_die (TYPE_CONTEXT (type));
14104                   }
14105               else
14106                 type_context_die = force_decl_die (TYPE_CONTEXT (type));
14107               else
14108                 type_context_die = comp_unit_die;
14109               gen_type_die_for_member (type, decl, type_context_die);
14110             }
14111           at_import_die = force_decl_die (decl);
14112         }
14113     }
14114
14115   /* OK, now we have DIEs for decl as well as scope. Emit imported die.  */
14116   if (TREE_CODE (decl) == NAMESPACE_DECL)
14117     imported_die = new_die (DW_TAG_imported_module, scope_die, context);
14118   else
14119     imported_die = new_die (DW_TAG_imported_declaration, scope_die, context);
14120
14121   xloc = expand_location (input_location);
14122   add_AT_file (imported_die, DW_AT_decl_file, lookup_filename (xloc.file));
14123   add_AT_unsigned (imported_die, DW_AT_decl_line, xloc.line);
14124   add_AT_die_ref (imported_die, DW_AT_import, at_import_die);
14125 }
14126
14127 /* Write the debugging output for DECL.  */
14128
14129 void
14130 dwarf2out_decl (tree decl)
14131 {
14132   dw_die_ref context_die = comp_unit_die;
14133
14134   switch (TREE_CODE (decl))
14135     {
14136     case ERROR_MARK:
14137       return;
14138
14139     case FUNCTION_DECL:
14140       /* What we would really like to do here is to filter out all mere
14141          file-scope declarations of file-scope functions which are never
14142          referenced later within this translation unit (and keep all of ones
14143          that *are* referenced later on) but we aren't clairvoyant, so we have
14144          no idea which functions will be referenced in the future (i.e. later
14145          on within the current translation unit). So here we just ignore all
14146          file-scope function declarations which are not also definitions.  If
14147          and when the debugger needs to know something about these functions,
14148          it will have to hunt around and find the DWARF information associated
14149          with the definition of the function.
14150
14151          We can't just check DECL_EXTERNAL to find out which FUNCTION_DECL
14152          nodes represent definitions and which ones represent mere
14153          declarations.  We have to check DECL_INITIAL instead. That's because
14154          the C front-end supports some weird semantics for "extern inline"
14155          function definitions.  These can get inlined within the current
14156          translation unit (and thus, we need to generate Dwarf info for their
14157          abstract instances so that the Dwarf info for the concrete inlined
14158          instances can have something to refer to) but the compiler never
14159          generates any out-of-lines instances of such things (despite the fact
14160          that they *are* definitions).
14161
14162          The important point is that the C front-end marks these "extern
14163          inline" functions as DECL_EXTERNAL, but we need to generate DWARF for
14164          them anyway. Note that the C++ front-end also plays some similar games
14165          for inline function definitions appearing within include files which
14166          also contain `#pragma interface' pragmas.  */
14167       if (DECL_INITIAL (decl) == NULL_TREE)
14168         return;
14169
14170       /* If we're a nested function, initially use a parent of NULL; if we're
14171          a plain function, this will be fixed up in decls_for_scope.  If
14172          we're a method, it will be ignored, since we already have a DIE.  */
14173       if (decl_function_context (decl)
14174           /* But if we're in terse mode, we don't care about scope.  */
14175           && debug_info_level > DINFO_LEVEL_TERSE)
14176         context_die = NULL;
14177       break;
14178
14179     case VAR_DECL:
14180       /* Ignore this VAR_DECL if it refers to a file-scope extern data object
14181          declaration and if the declaration was never even referenced from
14182          within this entire compilation unit.  We suppress these DIEs in
14183          order to save space in the .debug section (by eliminating entries
14184          which are probably useless).  Note that we must not suppress
14185          block-local extern declarations (whether used or not) because that
14186          would screw-up the debugger's name lookup mechanism and cause it to
14187          miss things which really ought to be in scope at a given point.  */
14188       if (DECL_EXTERNAL (decl) && !TREE_USED (decl))
14189         return;
14190
14191       /* For local statics lookup proper context die.  */
14192       if (TREE_STATIC (decl) && decl_function_context (decl))
14193         context_die = lookup_decl_die (DECL_CONTEXT (decl));
14194
14195       /* If we are in terse mode, don't generate any DIEs to represent any
14196          variable declarations or definitions.  */
14197       if (debug_info_level <= DINFO_LEVEL_TERSE)
14198         return;
14199       break;
14200
14201     case NAMESPACE_DECL:
14202       if (debug_info_level <= DINFO_LEVEL_TERSE)
14203         return;
14204       if (lookup_decl_die (decl) != NULL)
14205         return;
14206       break;
14207
14208     case TYPE_DECL:
14209       /* Don't emit stubs for types unless they are needed by other DIEs.  */
14210       if (TYPE_DECL_SUPPRESS_DEBUG (decl))
14211         return;
14212
14213       /* Don't bother trying to generate any DIEs to represent any of the
14214          normal built-in types for the language we are compiling.  */
14215       if (DECL_IS_BUILTIN (decl))
14216         {
14217           /* OK, we need to generate one for `bool' so GDB knows what type
14218              comparisons have.  */
14219           if (is_cxx ()
14220               && TREE_CODE (TREE_TYPE (decl)) == BOOLEAN_TYPE
14221               && ! DECL_IGNORED_P (decl))
14222             modified_type_die (TREE_TYPE (decl), 0, 0, NULL);
14223
14224           return;
14225         }
14226
14227       /* If we are in terse mode, don't generate any DIEs for types.  */
14228       if (debug_info_level <= DINFO_LEVEL_TERSE)
14229         return;
14230
14231       /* If we're a function-scope tag, initially use a parent of NULL;
14232          this will be fixed up in decls_for_scope.  */
14233       if (decl_function_context (decl))
14234         context_die = NULL;
14235
14236       break;
14237
14238     default:
14239       return;
14240     }
14241
14242   gen_decl_die (decl, context_die);
14243 }
14244
14245 /* Output a marker (i.e. a label) for the beginning of the generated code for
14246    a lexical block.  */
14247
14248 static void
14249 dwarf2out_begin_block (unsigned int line ATTRIBUTE_UNUSED,
14250                        unsigned int blocknum)
14251 {
14252   switch_to_section (current_function_section ());
14253   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_BEGIN_LABEL, blocknum);
14254 }
14255
14256 /* Output a marker (i.e. a label) for the end of the generated code for a
14257    lexical block.  */
14258
14259 static void
14260 dwarf2out_end_block (unsigned int line ATTRIBUTE_UNUSED, unsigned int blocknum)
14261 {
14262   switch_to_section (current_function_section ());
14263   ASM_OUTPUT_DEBUG_LABEL (asm_out_file, BLOCK_END_LABEL, blocknum);
14264 }
14265
14266 /* Returns nonzero if it is appropriate not to emit any debugging
14267    information for BLOCK, because it doesn't contain any instructions.
14268
14269    Don't allow this for blocks with nested functions or local classes
14270    as we would end up with orphans, and in the presence of scheduling
14271    we may end up calling them anyway.  */
14272
14273 static bool
14274 dwarf2out_ignore_block (const_tree block)
14275 {
14276   tree decl;
14277
14278   for (decl = BLOCK_VARS (block); decl; decl = TREE_CHAIN (decl))
14279     if (TREE_CODE (decl) == FUNCTION_DECL
14280         || (TREE_CODE (decl) == TYPE_DECL && TYPE_DECL_IS_STUB (decl)))
14281       return 0;
14282
14283   return 1;
14284 }
14285
14286 /* Hash table routines for file_hash.  */
14287
14288 static int
14289 file_table_eq (const void *p1_p, const void *p2_p)
14290 {
14291   const struct dwarf_file_data * p1 = p1_p;
14292   const char * p2 = p2_p;
14293   return strcmp (p1->filename, p2) == 0;
14294 }
14295
14296 static hashval_t
14297 file_table_hash (const void *p_p)
14298 {
14299   const struct dwarf_file_data * p = p_p;
14300   return htab_hash_string (p->filename);
14301 }
14302
14303 /* Lookup FILE_NAME (in the list of filenames that we know about here in
14304    dwarf2out.c) and return its "index".  The index of each (known) filename is
14305    just a unique number which is associated with only that one filename.  We
14306    need such numbers for the sake of generating labels (in the .debug_sfnames
14307    section) and references to those files numbers (in the .debug_srcinfo
14308    and.debug_macinfo sections).  If the filename given as an argument is not
14309    found in our current list, add it to the list and assign it the next
14310    available unique index number.  In order to speed up searches, we remember
14311    the index of the filename was looked up last.  This handles the majority of
14312    all searches.  */
14313
14314 static struct dwarf_file_data *
14315 lookup_filename (const char *file_name)
14316 {
14317   void ** slot;
14318   struct dwarf_file_data * created;
14319
14320   /* Check to see if the file name that was searched on the previous
14321      call matches this file name.  If so, return the index.  */
14322   if (file_table_last_lookup
14323       && (file_name == file_table_last_lookup->filename
14324           || strcmp (file_table_last_lookup->filename, file_name) == 0))
14325     return file_table_last_lookup;
14326
14327   /* Didn't match the previous lookup, search the table.  */
14328   slot = htab_find_slot_with_hash (file_table, file_name,
14329                                    htab_hash_string (file_name), INSERT);
14330   if (*slot)
14331     return *slot;
14332
14333   created = ggc_alloc (sizeof (struct dwarf_file_data));
14334   created->filename = file_name;
14335   created->emitted_number = 0;
14336   *slot = created;
14337   return created;
14338 }
14339
14340 /* If the assembler will construct the file table, then translate the compiler
14341    internal file table number into the assembler file table number, and emit
14342    a .file directive if we haven't already emitted one yet.  The file table
14343    numbers are different because we prune debug info for unused variables and
14344    types, which may include filenames.  */
14345
14346 static int
14347 maybe_emit_file (struct dwarf_file_data * fd)
14348 {
14349   if (! fd->emitted_number)
14350     {
14351       if (last_emitted_file)
14352         fd->emitted_number = last_emitted_file->emitted_number + 1;
14353       else
14354         fd->emitted_number = 1;
14355       last_emitted_file = fd;
14356
14357       if (DWARF2_ASM_LINE_DEBUG_INFO)
14358         {
14359           fprintf (asm_out_file, "\t.file %u ", fd->emitted_number);
14360           output_quoted_string (asm_out_file,
14361                                 remap_debug_filename (fd->filename));
14362           fputc ('\n', asm_out_file);
14363         }
14364     }
14365
14366   return fd->emitted_number;
14367 }
14368
14369 /* Called by the final INSN scan whenever we see a var location.  We
14370    use it to drop labels in the right places, and throw the location in
14371    our lookup table.  */
14372
14373 static void
14374 dwarf2out_var_location (rtx loc_note)
14375 {
14376   char loclabel[MAX_ARTIFICIAL_LABEL_BYTES];
14377   struct var_loc_node *newloc;
14378   rtx prev_insn;
14379   static rtx last_insn;
14380   static const char *last_label;
14381   tree decl;
14382
14383   if (!DECL_P (NOTE_VAR_LOCATION_DECL (loc_note)))
14384     return;
14385   prev_insn = PREV_INSN (loc_note);
14386
14387   newloc = ggc_alloc_cleared (sizeof (struct var_loc_node));
14388   /* If the insn we processed last time is the previous insn
14389      and it is also a var location note, use the label we emitted
14390      last time.  */
14391   if (last_insn != NULL_RTX
14392       && last_insn == prev_insn
14393       && NOTE_P (prev_insn)
14394       && NOTE_KIND (prev_insn) == NOTE_INSN_VAR_LOCATION)
14395     {
14396       newloc->label = last_label;
14397     }
14398   else
14399     {
14400       ASM_GENERATE_INTERNAL_LABEL (loclabel, "LVL", loclabel_num);
14401       ASM_OUTPUT_DEBUG_LABEL (asm_out_file, "LVL", loclabel_num);
14402       loclabel_num++;
14403       newloc->label = ggc_strdup (loclabel);
14404     }
14405   newloc->var_loc_note = loc_note;
14406   newloc->next = NULL;
14407
14408   if (cfun && in_cold_section_p)
14409     newloc->section_label = cfun->cold_section_label;
14410   else
14411     newloc->section_label = text_section_label;
14412
14413   last_insn = loc_note;
14414   last_label = newloc->label;
14415   decl = NOTE_VAR_LOCATION_DECL (loc_note);
14416   add_var_loc_to_decl (decl, newloc);
14417 }
14418
14419 /* We need to reset the locations at the beginning of each
14420    function. We can't do this in the end_function hook, because the
14421    declarations that use the locations won't have been output when
14422    that hook is called.  Also compute have_multiple_function_sections here.  */
14423
14424 static void
14425 dwarf2out_begin_function (tree fun)
14426 {
14427   htab_empty (decl_loc_table);
14428
14429   if (function_section (fun) != text_section)
14430     have_multiple_function_sections = true;
14431
14432   dwarf2out_note_section_used ();
14433 }
14434
14435 /* Output a label to mark the beginning of a source code line entry
14436    and record information relating to this source line, in
14437    'line_info_table' for later output of the .debug_line section.  */
14438
14439 static void
14440 dwarf2out_source_line (unsigned int line, const char *filename)
14441 {
14442   if (debug_info_level >= DINFO_LEVEL_NORMAL
14443       && line != 0)
14444     {
14445       int file_num = maybe_emit_file (lookup_filename (filename));
14446
14447       switch_to_section (current_function_section ());
14448
14449       /* If requested, emit something human-readable.  */
14450       if (flag_debug_asm)
14451         fprintf (asm_out_file, "\t%s %s:%d\n", ASM_COMMENT_START,
14452                  filename, line);
14453
14454       if (DWARF2_ASM_LINE_DEBUG_INFO)
14455         {
14456           /* Emit the .loc directive understood by GNU as.  */
14457           fprintf (asm_out_file, "\t.loc %d %d 0\n", file_num, line);
14458
14459           /* Indicate that line number info exists.  */
14460           line_info_table_in_use++;
14461         }
14462       else if (function_section (current_function_decl) != text_section)
14463         {
14464           dw_separate_line_info_ref line_info;
14465           targetm.asm_out.internal_label (asm_out_file,
14466                                           SEPARATE_LINE_CODE_LABEL,
14467                                           separate_line_info_table_in_use);
14468
14469           /* Expand the line info table if necessary.  */
14470           if (separate_line_info_table_in_use
14471               == separate_line_info_table_allocated)
14472             {
14473               separate_line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14474               separate_line_info_table
14475                 = ggc_realloc (separate_line_info_table,
14476                                separate_line_info_table_allocated
14477                                * sizeof (dw_separate_line_info_entry));
14478               memset (separate_line_info_table
14479                        + separate_line_info_table_in_use,
14480                       0,
14481                       (LINE_INFO_TABLE_INCREMENT
14482                        * sizeof (dw_separate_line_info_entry)));
14483             }
14484
14485           /* Add the new entry at the end of the line_info_table.  */
14486           line_info
14487             = &separate_line_info_table[separate_line_info_table_in_use++];
14488           line_info->dw_file_num = file_num;
14489           line_info->dw_line_num = line;
14490           line_info->function = current_function_funcdef_no;
14491         }
14492       else
14493         {
14494           dw_line_info_ref line_info;
14495
14496           targetm.asm_out.internal_label (asm_out_file, LINE_CODE_LABEL,
14497                                      line_info_table_in_use);
14498
14499           /* Expand the line info table if necessary.  */
14500           if (line_info_table_in_use == line_info_table_allocated)
14501             {
14502               line_info_table_allocated += LINE_INFO_TABLE_INCREMENT;
14503               line_info_table
14504                 = ggc_realloc (line_info_table,
14505                                (line_info_table_allocated
14506                                 * sizeof (dw_line_info_entry)));
14507               memset (line_info_table + line_info_table_in_use, 0,
14508                       LINE_INFO_TABLE_INCREMENT * sizeof (dw_line_info_entry));
14509             }
14510
14511           /* Add the new entry at the end of the line_info_table.  */
14512           line_info = &line_info_table[line_info_table_in_use++];
14513           line_info->dw_file_num = file_num;
14514           line_info->dw_line_num = line;
14515         }
14516     }
14517 }
14518
14519 /* Record the beginning of a new source file.  */
14520
14521 static void
14522 dwarf2out_start_source_file (unsigned int lineno, const char *filename)
14523 {
14524   if (flag_eliminate_dwarf2_dups)
14525     {
14526       /* Record the beginning of the file for break_out_includes.  */
14527       dw_die_ref bincl_die;
14528
14529       bincl_die = new_die (DW_TAG_GNU_BINCL, comp_unit_die, NULL);
14530       add_AT_string (bincl_die, DW_AT_name, remap_debug_filename (filename));
14531     }
14532
14533   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14534     {
14535       int file_num = maybe_emit_file (lookup_filename (filename));
14536
14537       switch_to_section (debug_macinfo_section);
14538       dw2_asm_output_data (1, DW_MACINFO_start_file, "Start new file");
14539       dw2_asm_output_data_uleb128 (lineno, "Included from line number %d",
14540                                    lineno);
14541
14542       dw2_asm_output_data_uleb128 (file_num, "file %s", filename);
14543     }
14544 }
14545
14546 /* Record the end of a source file.  */
14547
14548 static void
14549 dwarf2out_end_source_file (unsigned int lineno ATTRIBUTE_UNUSED)
14550 {
14551   if (flag_eliminate_dwarf2_dups)
14552     /* Record the end of the file for break_out_includes.  */
14553     new_die (DW_TAG_GNU_EINCL, comp_unit_die, NULL);
14554
14555   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14556     {
14557       switch_to_section (debug_macinfo_section);
14558       dw2_asm_output_data (1, DW_MACINFO_end_file, "End file");
14559     }
14560 }
14561
14562 /* Called from debug_define in toplev.c.  The `buffer' parameter contains
14563    the tail part of the directive line, i.e. the part which is past the
14564    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14565
14566 static void
14567 dwarf2out_define (unsigned int lineno ATTRIBUTE_UNUSED,
14568                   const char *buffer ATTRIBUTE_UNUSED)
14569 {
14570   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14571     {
14572       switch_to_section (debug_macinfo_section);
14573       dw2_asm_output_data (1, DW_MACINFO_define, "Define macro");
14574       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14575       dw2_asm_output_nstring (buffer, -1, "The macro");
14576     }
14577 }
14578
14579 /* Called from debug_undef in toplev.c.  The `buffer' parameter contains
14580    the tail part of the directive line, i.e. the part which is past the
14581    initial whitespace, #, whitespace, directive-name, whitespace part.  */
14582
14583 static void
14584 dwarf2out_undef (unsigned int lineno ATTRIBUTE_UNUSED,
14585                  const char *buffer ATTRIBUTE_UNUSED)
14586 {
14587   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14588     {
14589       switch_to_section (debug_macinfo_section);
14590       dw2_asm_output_data (1, DW_MACINFO_undef, "Undefine macro");
14591       dw2_asm_output_data_uleb128 (lineno, "At line number %d", lineno);
14592       dw2_asm_output_nstring (buffer, -1, "The macro");
14593     }
14594 }
14595
14596 /* Set up for Dwarf output at the start of compilation.  */
14597
14598 static void
14599 dwarf2out_init (const char *filename ATTRIBUTE_UNUSED)
14600 {
14601   /* Allocate the file_table.  */
14602   file_table = htab_create_ggc (50, file_table_hash,
14603                                 file_table_eq, NULL);
14604
14605   /* Allocate the decl_die_table.  */
14606   decl_die_table = htab_create_ggc (10, decl_die_table_hash,
14607                                     decl_die_table_eq, NULL);
14608
14609   /* Allocate the decl_loc_table.  */
14610   decl_loc_table = htab_create_ggc (10, decl_loc_table_hash,
14611                                     decl_loc_table_eq, NULL);
14612
14613   /* Allocate the initial hunk of the decl_scope_table.  */
14614   decl_scope_table = VEC_alloc (tree, gc, 256);
14615
14616   /* Allocate the initial hunk of the abbrev_die_table.  */
14617   abbrev_die_table = ggc_alloc_cleared (ABBREV_DIE_TABLE_INCREMENT
14618                                         * sizeof (dw_die_ref));
14619   abbrev_die_table_allocated = ABBREV_DIE_TABLE_INCREMENT;
14620   /* Zero-th entry is allocated, but unused.  */
14621   abbrev_die_table_in_use = 1;
14622
14623   /* Allocate the initial hunk of the line_info_table.  */
14624   line_info_table = ggc_alloc_cleared (LINE_INFO_TABLE_INCREMENT
14625                                        * sizeof (dw_line_info_entry));
14626   line_info_table_allocated = LINE_INFO_TABLE_INCREMENT;
14627
14628   /* Zero-th entry is allocated, but unused.  */
14629   line_info_table_in_use = 1;
14630
14631   /* Allocate the pubtypes and pubnames vectors.  */
14632   pubname_table = VEC_alloc (pubname_entry, gc, 32);
14633   pubtype_table = VEC_alloc (pubname_entry, gc, 32);
14634
14635   /* Generate the initial DIE for the .debug section.  Note that the (string)
14636      value given in the DW_AT_name attribute of the DW_TAG_compile_unit DIE
14637      will (typically) be a relative pathname and that this pathname should be
14638      taken as being relative to the directory from which the compiler was
14639      invoked when the given (base) source file was compiled.  We will fill
14640      in this value in dwarf2out_finish.  */
14641   comp_unit_die = gen_compile_unit_die (NULL);
14642
14643   incomplete_types = VEC_alloc (tree, gc, 64);
14644
14645   used_rtx_array = VEC_alloc (rtx, gc, 32);
14646
14647   debug_info_section = get_section (DEBUG_INFO_SECTION,
14648                                     SECTION_DEBUG, NULL);
14649   debug_abbrev_section = get_section (DEBUG_ABBREV_SECTION,
14650                                       SECTION_DEBUG, NULL);
14651   debug_aranges_section = get_section (DEBUG_ARANGES_SECTION,
14652                                        SECTION_DEBUG, NULL);
14653   debug_macinfo_section = get_section (DEBUG_MACINFO_SECTION,
14654                                        SECTION_DEBUG, NULL);
14655   debug_line_section = get_section (DEBUG_LINE_SECTION,
14656                                     SECTION_DEBUG, NULL);
14657   debug_loc_section = get_section (DEBUG_LOC_SECTION,
14658                                    SECTION_DEBUG, NULL);
14659   debug_pubnames_section = get_section (DEBUG_PUBNAMES_SECTION,
14660                                         SECTION_DEBUG, NULL);
14661 #ifdef DEBUG_PUBTYPES_SECTION
14662   debug_pubtypes_section = get_section (DEBUG_PUBTYPES_SECTION,
14663                                         SECTION_DEBUG, NULL);
14664 #endif
14665   debug_str_section = get_section (DEBUG_STR_SECTION,
14666                                    DEBUG_STR_SECTION_FLAGS, NULL);
14667   debug_ranges_section = get_section (DEBUG_RANGES_SECTION,
14668                                       SECTION_DEBUG, NULL);
14669   debug_frame_section = get_section (DEBUG_FRAME_SECTION,
14670                                      SECTION_DEBUG, NULL);
14671
14672   ASM_GENERATE_INTERNAL_LABEL (text_end_label, TEXT_END_LABEL, 0);
14673   ASM_GENERATE_INTERNAL_LABEL (abbrev_section_label,
14674                                DEBUG_ABBREV_SECTION_LABEL, 0);
14675   ASM_GENERATE_INTERNAL_LABEL (text_section_label, TEXT_SECTION_LABEL, 0);
14676   ASM_GENERATE_INTERNAL_LABEL (cold_text_section_label,
14677                                COLD_TEXT_SECTION_LABEL, 0);
14678   ASM_GENERATE_INTERNAL_LABEL (cold_end_label, COLD_END_LABEL, 0);
14679
14680   ASM_GENERATE_INTERNAL_LABEL (debug_info_section_label,
14681                                DEBUG_INFO_SECTION_LABEL, 0);
14682   ASM_GENERATE_INTERNAL_LABEL (debug_line_section_label,
14683                                DEBUG_LINE_SECTION_LABEL, 0);
14684   ASM_GENERATE_INTERNAL_LABEL (ranges_section_label,
14685                                DEBUG_RANGES_SECTION_LABEL, 0);
14686   switch_to_section (debug_abbrev_section);
14687   ASM_OUTPUT_LABEL (asm_out_file, abbrev_section_label);
14688   switch_to_section (debug_info_section);
14689   ASM_OUTPUT_LABEL (asm_out_file, debug_info_section_label);
14690   switch_to_section (debug_line_section);
14691   ASM_OUTPUT_LABEL (asm_out_file, debug_line_section_label);
14692
14693   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
14694     {
14695       switch_to_section (debug_macinfo_section);
14696       ASM_GENERATE_INTERNAL_LABEL (macinfo_section_label,
14697                                    DEBUG_MACINFO_SECTION_LABEL, 0);
14698       ASM_OUTPUT_LABEL (asm_out_file, macinfo_section_label);
14699     }
14700
14701   switch_to_section (text_section);
14702   ASM_OUTPUT_LABEL (asm_out_file, text_section_label);
14703   if (flag_reorder_blocks_and_partition)
14704     {
14705       cold_text_section = unlikely_text_section ();
14706       switch_to_section (cold_text_section);
14707       ASM_OUTPUT_LABEL (asm_out_file, cold_text_section_label);
14708     }
14709 }
14710
14711 /* A helper function for dwarf2out_finish called through
14712    ht_forall.  Emit one queued .debug_str string.  */
14713
14714 static int
14715 output_indirect_string (void **h, void *v ATTRIBUTE_UNUSED)
14716 {
14717   struct indirect_string_node *node = (struct indirect_string_node *) *h;
14718
14719   if (node->form == DW_FORM_strp)
14720     {
14721       switch_to_section (debug_str_section);
14722       ASM_OUTPUT_LABEL (asm_out_file, node->label);
14723       assemble_string (node->str, strlen (node->str) + 1);
14724     }
14725
14726   return 1;
14727 }
14728
14729 #if ENABLE_ASSERT_CHECKING
14730 /* Verify that all marks are clear.  */
14731
14732 static void
14733 verify_marks_clear (dw_die_ref die)
14734 {
14735   dw_die_ref c;
14736
14737   gcc_assert (! die->die_mark);
14738   FOR_EACH_CHILD (die, c, verify_marks_clear (c));
14739 }
14740 #endif /* ENABLE_ASSERT_CHECKING */
14741
14742 /* Clear the marks for a die and its children.
14743    Be cool if the mark isn't set.  */
14744
14745 static void
14746 prune_unmark_dies (dw_die_ref die)
14747 {
14748   dw_die_ref c;
14749
14750   if (die->die_mark)
14751     die->die_mark = 0;
14752   FOR_EACH_CHILD (die, c, prune_unmark_dies (c));
14753 }
14754
14755 /* Given DIE that we're marking as used, find any other dies
14756    it references as attributes and mark them as used.  */
14757
14758 static void
14759 prune_unused_types_walk_attribs (dw_die_ref die)
14760 {
14761   dw_attr_ref a;
14762   unsigned ix;
14763
14764   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14765     {
14766       if (a->dw_attr_val.val_class == dw_val_class_die_ref)
14767         {
14768           /* A reference to another DIE.
14769              Make sure that it will get emitted.  */
14770           prune_unused_types_mark (a->dw_attr_val.v.val_die_ref.die, 1);
14771         }
14772       /* Set the string's refcount to 0 so that prune_unused_types_mark
14773          accounts properly for it.  */
14774       if (AT_class (a) == dw_val_class_str)
14775         a->dw_attr_val.v.val_str->refcount = 0;
14776     }
14777 }
14778
14779
14780 /* Mark DIE as being used.  If DOKIDS is true, then walk down
14781    to DIE's children.  */
14782
14783 static void
14784 prune_unused_types_mark (dw_die_ref die, int dokids)
14785 {
14786   dw_die_ref c;
14787
14788   if (die->die_mark == 0)
14789     {
14790       /* We haven't done this node yet.  Mark it as used.  */
14791       die->die_mark = 1;
14792
14793       /* We also have to mark its parents as used.
14794          (But we don't want to mark our parents' kids due to this.)  */
14795       if (die->die_parent)
14796         prune_unused_types_mark (die->die_parent, 0);
14797
14798       /* Mark any referenced nodes.  */
14799       prune_unused_types_walk_attribs (die);
14800
14801       /* If this node is a specification,
14802          also mark the definition, if it exists.  */
14803       if (get_AT_flag (die, DW_AT_declaration) && die->die_definition)
14804         prune_unused_types_mark (die->die_definition, 1);
14805     }
14806
14807   if (dokids && die->die_mark != 2)
14808     {
14809       /* We need to walk the children, but haven't done so yet.
14810          Remember that we've walked the kids.  */
14811       die->die_mark = 2;
14812
14813       /* If this is an array type, we need to make sure our
14814          kids get marked, even if they're types.  */
14815       if (die->die_tag == DW_TAG_array_type)
14816         FOR_EACH_CHILD (die, c, prune_unused_types_mark (c, 1));
14817       else
14818         FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14819     }
14820 }
14821
14822
14823 /* Walk the tree DIE and mark types that we actually use.  */
14824
14825 static void
14826 prune_unused_types_walk (dw_die_ref die)
14827 {
14828   dw_die_ref c;
14829
14830   /* Don't do anything if this node is already marked.  */
14831   if (die->die_mark)
14832     return;
14833
14834   switch (die->die_tag)
14835     {
14836     case DW_TAG_const_type:
14837     case DW_TAG_packed_type:
14838     case DW_TAG_pointer_type:
14839     case DW_TAG_reference_type:
14840     case DW_TAG_volatile_type:
14841     case DW_TAG_typedef:
14842     case DW_TAG_array_type:
14843     case DW_TAG_structure_type:
14844     case DW_TAG_union_type:
14845     case DW_TAG_class_type:
14846     case DW_TAG_interface_type:
14847     case DW_TAG_friend:
14848     case DW_TAG_variant_part:
14849     case DW_TAG_enumeration_type:
14850     case DW_TAG_subroutine_type:
14851     case DW_TAG_string_type:
14852     case DW_TAG_set_type:
14853     case DW_TAG_subrange_type:
14854     case DW_TAG_ptr_to_member_type:
14855     case DW_TAG_file_type:
14856       if (die->die_perennial_p)
14857         break;
14858
14859       /* It's a type node --- don't mark it.  */
14860       return;
14861
14862     default:
14863       /* Mark everything else.  */
14864       break;
14865   }
14866
14867   die->die_mark = 1;
14868
14869   /* Now, mark any dies referenced from here.  */
14870   prune_unused_types_walk_attribs (die);
14871
14872   /* Mark children.  */
14873   FOR_EACH_CHILD (die, c, prune_unused_types_walk (c));
14874 }
14875
14876 /* Increment the string counts on strings referred to from DIE's
14877    attributes.  */
14878
14879 static void
14880 prune_unused_types_update_strings (dw_die_ref die)
14881 {
14882   dw_attr_ref a;
14883   unsigned ix;
14884
14885   for (ix = 0; VEC_iterate (dw_attr_node, die->die_attr, ix, a); ix++)
14886     if (AT_class (a) == dw_val_class_str)
14887       {
14888         struct indirect_string_node *s = a->dw_attr_val.v.val_str;
14889         s->refcount++;
14890         /* Avoid unnecessarily putting strings that are used less than
14891            twice in the hash table.  */
14892         if (s->refcount
14893             == ((DEBUG_STR_SECTION_FLAGS & SECTION_MERGE) ? 1 : 2))
14894           {
14895             void ** slot;
14896             slot = htab_find_slot_with_hash (debug_str_hash, s->str,
14897                                              htab_hash_string (s->str),
14898                                              INSERT);
14899             gcc_assert (*slot == NULL);
14900             *slot = s;
14901           }
14902       }
14903 }
14904
14905 /* Remove from the tree DIE any dies that aren't marked.  */
14906
14907 static void
14908 prune_unused_types_prune (dw_die_ref die)
14909 {
14910   dw_die_ref c;
14911
14912   gcc_assert (die->die_mark);
14913   prune_unused_types_update_strings (die);
14914
14915   if (! die->die_child)
14916     return;
14917
14918   c = die->die_child;
14919   do {
14920     dw_die_ref prev = c;
14921     for (c = c->die_sib; ! c->die_mark; c = c->die_sib)
14922       if (c == die->die_child)
14923         {
14924           /* No marked children between 'prev' and the end of the list.  */
14925           if (prev == c)
14926             /* No marked children at all.  */
14927             die->die_child = NULL;
14928           else
14929             {
14930               prev->die_sib = c->die_sib;
14931               die->die_child = prev;
14932             }
14933           return;
14934         }
14935
14936     if (c != prev->die_sib)
14937       prev->die_sib = c;
14938     prune_unused_types_prune (c);
14939   } while (c != die->die_child);
14940 }
14941
14942
14943 /* Remove dies representing declarations that we never use.  */
14944
14945 static void
14946 prune_unused_types (void)
14947 {
14948   unsigned int i;
14949   limbo_die_node *node;
14950   pubname_ref pub;
14951
14952 #if ENABLE_ASSERT_CHECKING
14953   /* All the marks should already be clear.  */
14954   verify_marks_clear (comp_unit_die);
14955   for (node = limbo_die_list; node; node = node->next)
14956     verify_marks_clear (node->die);
14957 #endif /* ENABLE_ASSERT_CHECKING */
14958
14959   /* Set the mark on nodes that are actually used.  */
14960   prune_unused_types_walk (comp_unit_die);
14961   for (node = limbo_die_list; node; node = node->next)
14962     prune_unused_types_walk (node->die);
14963
14964   /* Also set the mark on nodes referenced from the
14965      pubname_table or arange_table.  */
14966   for (i = 0; VEC_iterate (pubname_entry, pubname_table, i, pub); i++)
14967     prune_unused_types_mark (pub->die, 1);
14968   for (i = 0; i < arange_table_in_use; i++)
14969     prune_unused_types_mark (arange_table[i], 1);
14970
14971   /* Get rid of nodes that aren't marked; and update the string counts.  */
14972   if (debug_str_hash)
14973     htab_empty (debug_str_hash);
14974   prune_unused_types_prune (comp_unit_die);
14975   for (node = limbo_die_list; node; node = node->next)
14976     prune_unused_types_prune (node->die);
14977
14978   /* Leave the marks clear.  */
14979   prune_unmark_dies (comp_unit_die);
14980   for (node = limbo_die_list; node; node = node->next)
14981     prune_unmark_dies (node->die);
14982 }
14983
14984 /* Set the parameter to true if there are any relative pathnames in
14985    the file table.  */
14986 static int
14987 file_table_relative_p (void ** slot, void *param)
14988 {
14989   bool *p = param;
14990   struct dwarf_file_data *d = *slot;
14991   if (!IS_ABSOLUTE_PATH (d->filename))
14992     {
14993       *p = true;
14994       return 0;
14995     }
14996   return 1;
14997 }
14998
14999 /* Output stuff that dwarf requires at the end of every file,
15000    and generate the DWARF-2 debugging info.  */
15001
15002 static void
15003 dwarf2out_finish (const char *filename)
15004 {
15005   limbo_die_node *node, *next_node;
15006   dw_die_ref die = 0;
15007
15008   /* Add the name for the main input file now.  We delayed this from
15009      dwarf2out_init to avoid complications with PCH.  */
15010   add_name_attribute (comp_unit_die, remap_debug_filename (filename));
15011   if (!IS_ABSOLUTE_PATH (filename))
15012     add_comp_dir_attribute (comp_unit_die);
15013   else if (get_AT (comp_unit_die, DW_AT_comp_dir) == NULL)
15014     {
15015       bool p = false;
15016       htab_traverse (file_table, file_table_relative_p, &p);
15017       if (p)
15018         add_comp_dir_attribute (comp_unit_die);
15019     }
15020
15021   /* Traverse the limbo die list, and add parent/child links.  The only
15022      dies without parents that should be here are concrete instances of
15023      inline functions, and the comp_unit_die.  We can ignore the comp_unit_die.
15024      For concrete instances, we can get the parent die from the abstract
15025      instance.  */
15026   for (node = limbo_die_list; node; node = next_node)
15027     {
15028       next_node = node->next;
15029       die = node->die;
15030
15031       if (die->die_parent == NULL)
15032         {
15033           dw_die_ref origin = get_AT_ref (die, DW_AT_abstract_origin);
15034
15035           if (origin)
15036             add_child_die (origin->die_parent, die);
15037           else if (die == comp_unit_die)
15038             ;
15039           else if (errorcount > 0 || sorrycount > 0)
15040             /* It's OK to be confused by errors in the input.  */
15041             add_child_die (comp_unit_die, die);
15042           else
15043             {
15044               /* In certain situations, the lexical block containing a
15045                  nested function can be optimized away, which results
15046                  in the nested function die being orphaned.  Likewise
15047                  with the return type of that nested function.  Force
15048                  this to be a child of the containing function.
15049
15050                  It may happen that even the containing function got fully
15051                  inlined and optimized out.  In that case we are lost and
15052                  assign the empty child.  This should not be big issue as
15053                  the function is likely unreachable too.  */
15054               tree context = NULL_TREE;
15055
15056               gcc_assert (node->created_for);
15057
15058               if (DECL_P (node->created_for))
15059                 context = DECL_CONTEXT (node->created_for);
15060               else if (TYPE_P (node->created_for))
15061                 context = TYPE_CONTEXT (node->created_for);
15062
15063               gcc_assert (context
15064                           && (TREE_CODE (context) == FUNCTION_DECL
15065                               || TREE_CODE (context) == NAMESPACE_DECL));
15066
15067               origin = lookup_decl_die (context);
15068               if (origin)
15069                 add_child_die (origin, die);
15070               else
15071                 add_child_die (comp_unit_die, die);
15072             }
15073         }
15074     }
15075
15076   limbo_die_list = NULL;
15077
15078   /* Walk through the list of incomplete types again, trying once more to
15079      emit full debugging info for them.  */
15080   retry_incomplete_types ();
15081
15082   if (flag_eliminate_unused_debug_types)
15083     prune_unused_types ();
15084
15085   /* Generate separate CUs for each of the include files we've seen.
15086      They will go into limbo_die_list.  */
15087   if (flag_eliminate_dwarf2_dups)
15088     break_out_includes (comp_unit_die);
15089
15090   /* Traverse the DIE's and add add sibling attributes to those DIE's
15091      that have children.  */
15092   add_sibling_attributes (comp_unit_die);
15093   for (node = limbo_die_list; node; node = node->next)
15094     add_sibling_attributes (node->die);
15095
15096   /* Output a terminator label for the .text section.  */
15097   switch_to_section (text_section);
15098   targetm.asm_out.internal_label (asm_out_file, TEXT_END_LABEL, 0);
15099   if (flag_reorder_blocks_and_partition)
15100     {
15101       switch_to_section (unlikely_text_section ());
15102       targetm.asm_out.internal_label (asm_out_file, COLD_END_LABEL, 0);
15103     }
15104
15105   /* We can only use the low/high_pc attributes if all of the code was
15106      in .text.  */
15107   if (!have_multiple_function_sections)
15108     {
15109       add_AT_lbl_id (comp_unit_die, DW_AT_low_pc, text_section_label);
15110       add_AT_lbl_id (comp_unit_die, DW_AT_high_pc, text_end_label);
15111     }
15112
15113   else
15114     {
15115       unsigned fde_idx = 0;
15116
15117       /* We need to give .debug_loc and .debug_ranges an appropriate
15118          "base address".  Use zero so that these addresses become
15119          absolute.  Historically, we've emitted the unexpected
15120          DW_AT_entry_pc instead of DW_AT_low_pc for this purpose.
15121          Emit both to give time for other tools to adapt.  */
15122       add_AT_addr (comp_unit_die, DW_AT_low_pc, const0_rtx);
15123       add_AT_addr (comp_unit_die, DW_AT_entry_pc, const0_rtx);
15124
15125       add_AT_range_list (comp_unit_die, DW_AT_ranges,
15126                          add_ranges_by_labels (text_section_label,
15127                                                text_end_label));
15128       if (flag_reorder_blocks_and_partition)
15129         add_ranges_by_labels (cold_text_section_label,
15130                               cold_end_label);
15131
15132       for (fde_idx = 0; fde_idx < fde_table_in_use; fde_idx++)
15133         {
15134           dw_fde_ref fde = &fde_table[fde_idx];
15135
15136           if (fde->dw_fde_switched_sections)
15137             {
15138               add_ranges_by_labels (fde->dw_fde_hot_section_label,
15139                                     fde->dw_fde_hot_section_end_label);
15140               add_ranges_by_labels (fde->dw_fde_unlikely_section_label,
15141                                     fde->dw_fde_unlikely_section_end_label);
15142             }
15143           else
15144             add_ranges_by_labels (fde->dw_fde_begin,
15145                                   fde->dw_fde_end);
15146         }
15147
15148       add_ranges (NULL);
15149     }
15150
15151   /* Output location list section if necessary.  */
15152   if (have_location_lists)
15153     {
15154       /* Output the location lists info.  */
15155       switch_to_section (debug_loc_section);
15156       ASM_GENERATE_INTERNAL_LABEL (loc_section_label,
15157                                    DEBUG_LOC_SECTION_LABEL, 0);
15158       ASM_OUTPUT_LABEL (asm_out_file, loc_section_label);
15159       output_location_lists (die);
15160     }
15161
15162   if (debug_info_level >= DINFO_LEVEL_NORMAL)
15163     add_AT_lineptr (comp_unit_die, DW_AT_stmt_list,
15164                     debug_line_section_label);
15165
15166   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15167     add_AT_macptr (comp_unit_die, DW_AT_macro_info, macinfo_section_label);
15168
15169   /* Output all of the compilation units.  We put the main one last so that
15170      the offsets are available to output_pubnames.  */
15171   for (node = limbo_die_list; node; node = node->next)
15172     output_comp_unit (node->die, 0);
15173
15174   output_comp_unit (comp_unit_die, 0);
15175
15176   /* Output the abbreviation table.  */
15177   switch_to_section (debug_abbrev_section);
15178   output_abbrev_section ();
15179
15180   /* Output public names table if necessary.  */
15181   if (!VEC_empty (pubname_entry, pubname_table))
15182     {
15183       switch_to_section (debug_pubnames_section);
15184       output_pubnames (pubname_table);
15185     }
15186
15187 #ifdef DEBUG_PUBTYPES_SECTION
15188   /* Output public types table if necessary.  */
15189   if (!VEC_empty (pubname_entry, pubtype_table))
15190     {
15191       switch_to_section (debug_pubtypes_section);
15192       output_pubnames (pubtype_table);
15193     }
15194 #endif
15195
15196   /* Output the address range information.  We only put functions in the arange
15197      table, so don't write it out if we don't have any.  */
15198   if (fde_table_in_use)
15199     {
15200       switch_to_section (debug_aranges_section);
15201       output_aranges ();
15202     }
15203
15204   /* Output ranges section if necessary.  */
15205   if (ranges_table_in_use)
15206     {
15207       switch_to_section (debug_ranges_section);
15208       ASM_OUTPUT_LABEL (asm_out_file, ranges_section_label);
15209       output_ranges ();
15210     }
15211
15212   /* Output the source line correspondence table.  We must do this
15213      even if there is no line information.  Otherwise, on an empty
15214      translation unit, we will generate a present, but empty,
15215      .debug_info section.  IRIX 6.5 `nm' will then complain when
15216      examining the file.  This is done late so that any filenames
15217      used by the debug_info section are marked as 'used'.  */
15218   if (! DWARF2_ASM_LINE_DEBUG_INFO)
15219     {
15220       switch_to_section (debug_line_section);
15221       output_line_info ();
15222     }
15223
15224   /* Have to end the macro section.  */
15225   if (debug_info_level >= DINFO_LEVEL_VERBOSE)
15226     {
15227       switch_to_section (debug_macinfo_section);
15228       dw2_asm_output_data (1, 0, "End compilation unit");
15229     }
15230
15231   /* If we emitted any DW_FORM_strp form attribute, output the string
15232      table too.  */
15233   if (debug_str_hash)
15234     htab_traverse (debug_str_hash, output_indirect_string, NULL);
15235 }
15236 #else
15237
15238 /* This should never be used, but its address is needed for comparisons.  */
15239 const struct gcc_debug_hooks dwarf2_debug_hooks;
15240
15241 #endif /* DWARF2_DEBUGGING_INFO */
15242
15243 #include "gt-dwarf2out.h"